f.php 10.2 KB
Newer Older
1 2 3 4
<?php
/*
 *  Jirafeau, your web file repository
 *  Copyright (C) 2008  Julien "axolotl" BERNARD <axolotl@magieeternelle.org>
5
 *  Copyright (C) 2015  Jerome Jutteau <j.jutteau@gmail.com>
6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
Dan Untenzu's avatar
Dan Untenzu committed
20
define('JIRAFEAU_ROOT', dirname(__FILE__) . '/');
21

Dan Untenzu's avatar
Dan Untenzu committed
22 23 24
require(JIRAFEAU_ROOT . 'lib/settings.php');
require(JIRAFEAU_ROOT . 'lib/functions.php');
require(JIRAFEAU_ROOT . 'lib/lang.php');
25

Dan Untenzu's avatar
Dan Untenzu committed
26
if (!isset($_GET['h']) || empty($_GET['h'])) {
27
    header('Location: ./');
28 29
    exit;
}
30

31 32 33
/* Operations may take a long time.
 * Be sure PHP's safe mode is off.
 */
34 35 36
@set_time_limit(0);
/* Remove errors. */
@error_reporting(0);
37

38
$link_name = $_GET['h'];
39

Dan Untenzu's avatar
Dan Untenzu committed
40 41
if (!preg_match('/[0-9a-zA-Z_-]+$/', $link_name)) {
    require(JIRAFEAU_ROOT.'lib/template/header.php');
42
    echo '<div class="error"><p>' . t('Sorry, the requested file is not found') . '</p></div>';
Dan Untenzu's avatar
Dan Untenzu committed
43
    require(JIRAFEAU_ROOT.'lib/template/footer.php');
44 45
    exit;
}
46

Dan Untenzu's avatar
Dan Untenzu committed
47 48 49
$link = jirafeau_get_link($link_name);
if (count($link) == 0) {
    require(JIRAFEAU_ROOT.'lib/template/header.php');
50
    echo '<div class="error"><p>' . t('Sorry, the requested file is not found') .
51
    '</p></div>';
Dan Untenzu's avatar
Dan Untenzu committed
52
    require(JIRAFEAU_ROOT.'lib/template/footer.php');
53 54
    exit;
}
55

56
$delete_code = '';
Dan Untenzu's avatar
Dan Untenzu committed
57
if (isset($_GET['d']) && !empty($_GET['d']) &&  $_GET['d'] != '1') {
58
    $delete_code = $_GET['d'];
Dan Untenzu's avatar
Dan Untenzu committed
59
}
60

61
$crypt_key = '';
Dan Untenzu's avatar
Dan Untenzu committed
62
if (isset($_GET['k']) && !empty($_GET['k'])) {
63
    $crypt_key = $_GET['k'];
Dan Untenzu's avatar
Dan Untenzu committed
64
}
65

66
$do_download = false;
Dan Untenzu's avatar
Dan Untenzu committed
67
if (isset($_GET['d']) && $_GET['d'] == '1') {
68
    $do_download = true;
Dan Untenzu's avatar
Dan Untenzu committed
69
}
70

71
$do_preview = false;
Dan Untenzu's avatar
Dan Untenzu committed
72
if (isset($_GET['p']) && !empty($_GET['p'])) {
73
    $do_preview = true;
Dan Untenzu's avatar
Dan Untenzu committed
74
}
75

Dan Untenzu's avatar
Dan Untenzu committed
76 77 78 79
$p = s2p($link['md5']);
if (!file_exists(VAR_FILES . $p . $link['md5'])) {
    jirafeau_delete_link($link_name);
    require(JIRAFEAU_ROOT.'lib/template/header.php');
80 81
    echo '<div class="error"><p>'.t('File not available.').
    '</p></div>';
Dan Untenzu's avatar
Dan Untenzu committed
82
    require(JIRAFEAU_ROOT.'lib/template/footer.php');
83 84 85
    exit;
}

Dan Untenzu's avatar
Dan Untenzu committed
86 87
if (!empty($delete_code) && $delete_code == $link['link_code']) {
    require(JIRAFEAU_ROOT.'lib/template/header.php');
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    if (isset($_POST['do_delete'])) {
        jirafeau_delete_link($link_name);
        echo '<div class="message"><p>'.t('File has been deleted.').
            '</p></div>';
    } else { ?>
        <div>
        <form action="f.php" method="post" id="submit_delete_post" class="form login">
        <input type="hidden" name="do_delete" value=1/>
        <fieldset>
             <legend> <?php echo t('Confirm deletion') ?> </legend>
             <table>
             <tr><td>
             <?php echo t('You are about to delete') . ' "' . htmlspecialchars($link['file_name']) . '" (' . jirafeau_human_size($link['file_size']) . ').' ?>
             </td></tr>
             <tr><td>
                <?php echo t('By using our services, you accept our'). ' <a href="tos.php">' . t('Terms of Service') . '</a>.' ?>
             </td></tr>
             <tr><td>
                <input type="submit" id="submit_delete"  value="<?php echo t('Delete'); ?>"
                onclick="document.getElementById('submit_delete_post').action='<?php echo 'f.php?h=' . $link_name . '&amp;d=' . $delete_code . "';"; ?>
                document.getElementById('submit_delete').submit ();"/>
             </td></tr>
             </table>
         </fieldset></form></div><?php
    }
Dan Untenzu's avatar
Dan Untenzu committed
113
    require(JIRAFEAU_ROOT.'lib/template/footer.php');
114 115
    exit;
}
Jérôme Jutteau's avatar
Jérôme Jutteau committed
116

Dan Untenzu's avatar
Dan Untenzu committed
117 118 119
if ($link['time'] != JIRAFEAU_INFINITY && time() > $link['time']) {
    jirafeau_delete_link($link_name);
    require(JIRAFEAU_ROOT.'lib/template/header.php');
120 121 122 123
    echo '<div class="error"><p>'.
    t('The time limit of this file has expired.') . ' ' .
    t('File has been deleted.') .
    '</p></div>';
Dan Untenzu's avatar
Dan Untenzu committed
124
    require(JIRAFEAU_ROOT . 'lib/template/footer.php');
125 126 127
    exit;
}

Dan Untenzu's avatar
Dan Untenzu committed
128 129
if (empty($crypt_key) && $link['crypted']) {
    require(JIRAFEAU_ROOT.'lib/template/header.php');
130 131
    echo '<div class="error"><p>' . t('Sorry, the requested file is not found') .
    '</p></div>';
Dan Untenzu's avatar
Dan Untenzu committed
132
    require(JIRAFEAU_ROOT.'lib/template/footer.php');
133 134 135
    exit;
}

136
$password_challenged = false;
Dan Untenzu's avatar
Dan Untenzu committed
137 138 139
if (!empty($link['key'])) {
    if (!isset($_POST['key'])) {
        require(JIRAFEAU_ROOT.'lib/template/header.php');
140
        echo '<div>' .
141
             '<form action="f.php" method="post" id="submit_post" class="form login">'; ?>
142 143 144 145 146 147
             <input type = "hidden" name = "jirafeau" value = "<?php echo JIRAFEAU_VERSION ?>"/><?php
        echo '<fieldset>' .
             '<legend>' . t('Password protection') .
             '</legend><table><tr><td>' .
             t('Give the password of this file') . ' : ' .
             '<input type = "password" name = "key" />' .
148 149
             '</td></tr>' .
             '<tr><td>' .
150
             t('By using our services, you accept our'). ' <a href="tos.php">' . t('Terms of Service') . '</a>.' .
151 152
             '</td></tr>';

Dan Untenzu's avatar
Dan Untenzu committed
153
        if ($link['onetime'] == 'O') {
154 155 156
            echo '<tr><td id="self_destruct">' .
                 t('Warning, this file will self-destruct after being read') .
                 '</td></tr>';
Dan Untenzu's avatar
Dan Untenzu committed
157
        } ?><tr><td><input type="submit" id = "submit_download"  value="<?php echo t('Download'); ?>"
158
        onclick="document.getElementById('submit_post').action='<?php
159
        echo 'f.php?h=' . $link_name . '&amp;d=1';
Dan Untenzu's avatar
Dan Untenzu committed
160
        if (!empty($crypt_key)) {
161
            echo '&amp;k=' . urlencode($crypt_key);
Dan Untenzu's avatar
Dan Untenzu committed
162
        } ?>';
163
        document.getElementById('submit_download').submit ();"/><?php
Dan Untenzu's avatar
Dan Untenzu committed
164
        if ($cfg['preview'] && jirafeau_is_viewable($link['mime_type'])) {
165
            ?><input type="submit" id = "submit_preview"  value="<?php echo t('Preview'); ?>"
166
            onclick="document.getElementById('submit_post').action='<?php
167
            echo 'f.php?h=' . $link_name . '&amp;p=1';
Dan Untenzu's avatar
Dan Untenzu committed
168
            if (!empty($crypt_key)) {
169
                echo '&amp;k=' . urlencode($crypt_key);
Dan Untenzu's avatar
Dan Untenzu committed
170
            } ?>';
171
            document.getElementById('submit_preview').submit ();"/><?php
Dan Untenzu's avatar
Dan Untenzu committed
172

Jérôme Jutteau's avatar
Jérôme Jutteau committed
173
        }
174
        echo '</td></tr></table></fieldset></form></div>';
Dan Untenzu's avatar
Dan Untenzu committed
175
        require(JIRAFEAU_ROOT.'lib/template/footer.php');
176
        exit;
Dan Untenzu's avatar
Dan Untenzu committed
177 178
    } else {
        if ($link['key'] == md5($_POST['key'])) {
179
            $password_challenged = true;
Dan Untenzu's avatar
Dan Untenzu committed
180 181 182
        } else {
            sleep(2);
            require(JIRAFEAU_ROOT.'lib/template/header.php');
183
            echo '<div class="error"><p>' . t('Access denied') .
Jérôme Jutteau's avatar
Jérôme Jutteau committed
184
            '</p></div>';
Dan Untenzu's avatar
Dan Untenzu committed
185
            require(JIRAFEAU_ROOT.'lib/template/footer.php');
Jérôme Jutteau's avatar
Jérôme Jutteau committed
186 187
            exit;
        }
188
    }
189
}
190

Dan Untenzu's avatar
Dan Untenzu committed
191 192 193
if (!$password_challenged && !$do_download && !$do_preview) {
    require(JIRAFEAU_ROOT.'lib/template/header.php');
    echo '<div>' .
194
             '<form action="f.php" method="post" id="submit_post" class="form download">'; ?>
195
             <input type = "hidden" name = "jirafeau" value = "<?php echo JIRAFEAU_VERSION ?>"/><?php
196
        echo '<fieldset><legend>' . htmlspecialchars($link['file_name']) . '</legend><table>' .
197
             '<tr><td>' .
198
             t('You are about to download') . ' "' . htmlspecialchars($link['file_name']) . '" (' . jirafeau_human_size($link['file_size']) . ').' .
199 200
             '</td></tr>' .
             '<tr><td>' .
201
             t('By using our services, you accept our'). ' <a href="tos.php">' . t('Terms of Service') . '</a>.' .
202 203
             '</td></tr>';

Dan Untenzu's avatar
Dan Untenzu committed
204 205
    if ($link['onetime'] == 'O') {
        echo '<tr><td id="self_destruct">' .
206 207
                 t('Warning, this file will self-destruct after being read') .
                 '</td></tr>';
Dan Untenzu's avatar
Dan Untenzu committed
208
    } ?>
209
        <tr><td><input type="submit" id = "submit_download"  value="<?php echo t('Download'); ?>"
210
        onclick="document.getElementById('submit_post').action='<?php
211
        echo 'f.php?h=' . $link_name . '&amp;d=1';
Dan Untenzu's avatar
Dan Untenzu committed
212 213 214
    if (!empty($crypt_key)) {
        echo '&amp;k=' . urlencode($crypt_key);
    } ?>';
215
        document.getElementById('submit_post').submit ();"/><?php
Jérôme Jutteau's avatar
Jérôme Jutteau committed
216

Dan Untenzu's avatar
Dan Untenzu committed
217
        if ($cfg['preview'] && jirafeau_is_viewable($link['mime_type'])) {
218
            ?><input type="submit" id = "submit_preview"  value="<?php echo t('Preview'); ?>"
219
            onclick="document.getElementById('submit_post').action='<?php
220
        echo 'f.php?h=' . $link_name . '&amp;p=1';
Dan Untenzu's avatar
Dan Untenzu committed
221 222 223
            if (!empty($crypt_key)) {
                echo '&amp;k=' . urlencode($crypt_key);
            } ?>';
224
        document.getElementById('submit_post').submit ();"/><?php
Dan Untenzu's avatar
Dan Untenzu committed
225

226
        }
Dan Untenzu's avatar
Dan Untenzu committed
227 228 229 230
    echo '</td></tr>';
    echo '</table></fieldset></form></div>';
    require(JIRAFEAU_ROOT.'lib/template/footer.php');
    exit;
Jérôme Jutteau's avatar
Jérôme Jutteau committed
231
}
232

Dan Untenzu's avatar
Dan Untenzu committed
233 234 235 236 237 238 239 240 241
header('HTTP/1.0 200 OK');
header('Content-Length: ' . $link['file_size']);
if (!jirafeau_is_viewable($link['mime_type']) || !$cfg['preview'] || $do_download) {
    header('Content-Disposition: attachment; filename="' . $link['file_name'] . '"');
} else {
    header('Content-Disposition: filename="' . $link['file_name'] . '"');
}
header('Content-Type: ' . $link['mime_type']);
header('Content-MD5: ' . hex_to_base64($link['md5']));
242

243
/* Read encrypted file. */
Dan Untenzu's avatar
Dan Untenzu committed
244
if ($link['crypted']) {
245 246
    /* Init module */
    $m = mcrypt_module_open('rijndael-256', '', 'ofb', '');
Jérôme Jutteau's avatar
Jérôme Jutteau committed
247
    /* Extract key and iv. */
Dan Untenzu's avatar
Dan Untenzu committed
248 249
    $md5_key = md5($crypt_key);
    $iv = jirafeau_crypt_create_iv($md5_key, mcrypt_enc_get_iv_size($m));
Jérôme Jutteau's avatar
Jérôme Jutteau committed
250
    /* Init module. */
Dan Untenzu's avatar
Dan Untenzu committed
251
    mcrypt_generic_init($m, $md5_key, $iv);
252
    /* Decrypt file. */
Dan Untenzu's avatar
Dan Untenzu committed
253 254 255
    $r = fopen(VAR_FILES . $p . $link['md5'], 'r');
    while (!feof($r)) {
        $dec = mdecrypt_generic($m, fread($r, 1024));
256 257 258
        print $dec;
        ob_flush();
    }
Dan Untenzu's avatar
Dan Untenzu committed
259
    fclose($r);
260 261 262 263 264
    /* Cleanup. */
    mcrypt_generic_deinit($m);
    mcrypt_module_close($m);
}
/* Read file. */
Dan Untenzu's avatar
Dan Untenzu committed
265 266 267 268
else {
    $r = fopen(VAR_FILES . $p . $link['md5'], 'r');
    while (!feof($r)) {
        print fread($r, 1024);
269 270
        ob_flush();
    }
Dan Untenzu's avatar
Dan Untenzu committed
271
    fclose($r);
272
}
273

Dan Untenzu's avatar
Dan Untenzu committed
274 275 276
if ($link['onetime'] == 'O') {
    jirafeau_delete_link($link_name);
}
277
exit;
278 279

?>