]>
git.p6c8.net - jirafeau_mojo42.git/blob - lib/functions.php
3 * Jirafeau, your web file repository
4 * Copyright (C) 2008 Julien "axolotl" BERNARD <axolotl@magieeternelle.org>
5 * Copyright (C) 2012 Jerome Jutteau <j.jutteau@gmail.com>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * transforms a php.ini string representing a value in an integer
22 * @param $value the value from php.ini
23 * @returns an integer for this value
25 function jirafeau_ini_to_bytes ($value)
27 $modifier = substr ($value, -1);
28 $bytes = substr ($value, 0, -1);
29 switch (strtoupper ($modifier))
48 * gets the maximum upload size according to php.ini
49 * @returns the maximum upload size
52 jirafeau_get_max_upload_size ()
54 return min (jirafeau_ini_to_bytes (ini_get ('post_max_size')),
55 jirafeau_ini_to_bytes (ini_get ('upload_max_filesize')));
59 * gets a string explaining the error
60 * @param $code the error code
61 * @returns a string explaining the error
64 jirafeau_upload_errstr ($code)
68 case UPLOAD_ERR_INI_SIZE
:
69 case UPLOAD_ERR_FORM_SIZE
:
70 return t('Your file exceeds the maximum authorized file size. ');
73 case UPLOAD_ERR_PARTIAL
:
74 case UPLOAD_ERR_NO_FILE
:
77 ('Your file was not uploaded correctly. You may succeed in retrying. ');
80 case UPLOAD_ERR_NO_TMP_DIR
:
81 case UPLOAD_ERR_CANT_WRITE
:
82 case UPLOAD_ERR_EXTENSION
:
83 return t('Internal error. You may not succeed in retrying. ');
89 return t('Unknown error. ');
92 /** Remove link and it's file
93 * @param $link the link's name (hash)
97 jirafeau_delete ($link)
99 if (!file_exists ( VAR_LINKS
. $link))
102 $content = file (VAR_LINKS
. $link);
103 $md5 = trim ($content[5]);
104 unlink (VAR_LINKS
. $link);
107 if (file_exists ( VAR_FILES
. $md5. '_count'))
109 $content = file ( VAR_FILES
. $md5. '_count');
110 $counter = trim ($content[0]);
116 $handle = fopen ( VAR_FILES
. $md5. '_count', 'w');
117 fwrite ($handle, $counter);
123 if (file_exists (VAR_FILES
. $md5))
124 unlink ( VAR_FILES
. $md5);
125 if (file_exists (VAR_FILES
. $md5 . '_count'))
126 unlink ( VAR_FILES
. $md5. '_count');
131 * Delete a file and it's links.
134 jirafeau_delete_file ($md5)
137 $links_dir = scandir (VAR_LINKS
);
139 foreach ($links_dir as $link)
141 if (strcmp ($link, '.') == 0 ||
strcmp ($link, '..') == 0 ||
142 preg_match ('/\.tmp/i', "$link"))
144 /* Read link informations. */
145 $l = jirafeau_get_link ($link);
146 if ($l['md5'] == $md5)
149 jirafeau_delete ($link);
153 if (file_exists (VAR_FILES
. $md5 . '_count'))
154 unlink (VAR_FILES
. $md5. '_count');
155 if (file_exists (VAR_FILES
. $md5))
156 unlink (VAR_FILES
. $md5);
162 * handles an uploaded file
163 * @param $file the file struct given by $_FILE[]
164 * @param $one_time_download is the file a one time download ?
165 * @param $key if not empty, protect the file with this key
166 * @param $time the time of validity of the file
167 * @param $cfg the current configuration
168 * @param $ip uploader's ip
169 * @returns an array containing some information
170 * 'error' => information on possible errors
171 * 'link' => the link name of the uploaded file
172 * 'delete_link' => the link code to delete file
175 jirafeau_upload ($file, $one_time_download, $key, $time, $cfg, $ip)
177 if (empty ($file['tmp_name']) ||
!is_uploaded_file ($file['tmp_name']))
181 array ('has_error' => true,
182 'why' => jirafeau_upload_errstr ($file['error'])),
184 'delete_link' => ''));
187 /* array representing no error */
188 $noerr = array ('has_error' => false, 'why' => '');
190 /* file informations */
191 $md5 = md5_file ($file['tmp_name']);
192 $name = trim ($file['name']);
193 $mime_type = $file['type'];
194 $size = $file['size'];
196 /* does file already exist ? */
198 if (file_exists ( VAR_FILES
. $md5))
200 $rc = unlink ($file['tmp_name']);
202 elseif (move_uploaded_file ($file['tmp_name'], VAR_FILES
. $md5))
210 array ('has_error' => true,
211 'why' => t('Internal error during file creation. ')),
213 'delete_link' => ''));
216 /* increment or create count file */
218 if (file_exists (VAR_FILES
. $md5 . '_count'))
220 $content = file ( VAR_FILES
. $md5. '_count');
221 $counter = trim ($content[0]);
224 $handle = fopen ( VAR_FILES
. $md5. '_count', 'w');
225 fwrite ($handle, $counter);
228 /* Create delete code. */
229 $delete_link_code = 0;
230 for ($i = 0; $i < 8; $i++
)
231 $delete_link_code .= dechex (rand (0, 16));
233 /* md5 password or empty */
236 $password = md5 ($key);
238 /* create link file */
239 $link_tmp_name = VAR_LINKS
. $md5.rand (0, 10000) . ' .tmp';
240 $handle = fopen ($link_tmp_name, 'w');
242 $name . NL
. $mime_type . NL
. $size . NL
. $password . NL
. $time . NL
. $md5.
243 NL
.($one_time_download ?
'O' : 'R') . NL
.date ('U') . NL
. $ip . NL
.
244 $delete_link_code . NL
);
246 $md5_link = md5_file ($link_tmp_name);
247 if (!rename ($link_tmp_name, VAR_LINKS
. $md5_link))
249 unlink ($link_tmp_name);
253 $handle = fopen ( VAR_FILES
. $md5. '_count', 'w');
254 fwrite ($handle, $counter);
259 unlink ( VAR_FILES
. $md5. '_count');
260 unlink ( VAR_FILES
. $md5);
264 array ('has_error' => true,
265 'why' => t('Internal error during file creation. ')),
267 'delete_link' => ''));
269 return (array ('error' => $noerr,
271 'delete_link' => $delete_link_code));
275 * tells if a mime-type is viewable in a browser
276 * @param $mime the mime type
277 * @returns a boolean telling if a mime type is viewable
280 jirafeau_is_viewable ($mime)
284 /* Actually, verify if mime-type is an image or a text. */
285 $viewable = array ('image', 'text');
286 $decomposed = explode ('/', $mime);
287 return in_array ($decomposed[0], $viewable);
293 // Error handling functions.
294 //! Global array that contains all registered errors.
295 $error_list = array ();
298 * Adds an error to the list of errors.
299 * @param $title the error's title
300 * @param $description is a human-friendly description of the problem.
303 add_error ($title, $description)
306 $error_list[] = '<p>' . $title. '<br />' . $description. '</p>';
310 * Informs whether any error has been registered yet.
311 * @return true if there are errors.
317 return !empty ($error_list);
321 * Displays all the errors.
329 echo '<div class="error">';
330 foreach ($error_list as $error)
339 * Read link informations
340 * @return array containing informations.
343 jirafeau_get_link ($hash)
346 $link = VAR_LINKS
. $hash;
348 if (!file_exists ($link))
352 $out['file_name'] = trim ($c[0]);
353 $out['mime_type'] = trim ($c[1]);
354 $out['file_size'] = trim ($c[2]);
355 $out['key'] = trim ($c[3], NL
);
356 $out['time'] = trim ($c[4]);
357 $out['md5'] = trim ($c[5]);
358 $out['onetime'] = trim ($c[6]);
359 $out['upload_date'] = trim ($c[7]);
360 $out['ip'] = trim ($c[8]);
361 $out['link_code'] = trim ($c[9]);
367 jirafeau_human_size ($octets)
369 $u = array ('B', 'KB', 'MB', 'GB', 'TB');
370 $o = max ($octets, 0);
371 $p = min (floor (($o ?
log ($o) : 0) / log (1024)), count ($u) - 1);
372 $o /= pow (1024, $p);
373 return round ($o, 1) . $u[$p];
377 * List files in admin interface.
380 jirafeau_admin_list ($name, $file_hash, $link_hash)
382 $links_dir = scandir (VAR_LINKS
);
383 echo '<fieldset><legend>';
386 if (!empty ($file_hash))
387 echo $file_hash . ' ';
388 if (!empty ($link_hash))
389 echo $link_hash . ' ';
390 if (empty ($name) && empty ($file_hash) && empty ($link_hash))
391 echo t('List all files');
395 echo '<td>' . t('Filename') . '</td>';
396 echo '<td>' . t('Type') . '</td>';
397 echo '<td>' . t('Size') . '</td>';
398 echo '<td>' . t('Expire') . '</td>';
399 echo '<td>' . t('Onetime') . '</td>';
400 echo '<td>' . t('Upload date') . '</td>';
401 echo '<td>' . t('Origin') . '</td>';
402 echo '<td>' . t('Action') . '</td>';
404 foreach ($links_dir as $link)
406 if (strcmp ($link, '.') == 0 ||
strcmp ($link, '..') == 0 ||
407 preg_match ('/\.tmp/i', "$link"))
409 /* Read link informations. */
410 $l = jirafeau_get_link ($link);
413 if (!empty ($name) && $name != $l['file_name'])
415 if (!empty ($file_hash) && $file_hash != $l['md5'])
417 if (!empty ($link_hash) && $link_hash != $link)
420 /* Print link informations. */
422 echo '<td>' . $l['file_name'] . '</td>';
423 echo '<td>' . $l['mime_type'] . '</td>';
424 echo '<td>' . jirafeau_human_size ($l['file_size']) . '</td>';
425 echo '<td>' . ($l['time'] == -1 ?
'' : strftime ('%c', $l['time'])) .
427 echo '<td>' . $l['onetime'] . '</td>';
428 echo '<td>' . strftime ('%c', $l['upload_date']) . '</td>';
429 echo '<td>' . $l['ip'] . '</td>';
431 '<form action = "admin.php" method = "post">' .
432 '<input type = "hidden" name = "action" value = "delete_link"/>' .
433 '<input type = "hidden" name = "link" value = "' . $link . '"/>' .
434 '<input type = "submit" value = "' . t('Del link') . '" />' .
436 '<form action = "admin.php" method = "post">' .
437 '<input type = "hidden" name = "action" value = "delete_file"/>' .
438 '<input type = "hidden" name = "md5" value = "' . $l['md5'] . '"/>' .
439 '<input type = "submit" value = "' . t('Del file and links') . '" />' .
444 echo '</table></fieldset>';
448 * Clean expired files.
449 * @return number of cleaned files.
452 jirafeau_admin_clean ()
455 $links_dir = scandir (VAR_LINKS
);
457 foreach ($links_dir as $link)
459 if (strcmp ($link, '.') == 0 ||
strcmp ($link, '..') == 0 ||
460 preg_match ('/\.tmp/i', "$link"))
462 /* Read link informations. */
463 $l = jirafeau_get_link ($link);
464 if ($l['time'] > 0 && $l['time'] < time () ||
// expired
465 !file_exists (VAR_FILES
. $l['md5']) ||
// invalid
466 !file_exists (VAR_FILES
. $l['md5'] . '_count')) // invalid
468 jirafeau_delete ($link);
patrick-canterino.de