X-Git-Url: https://git.p6c8.net/jirafeau_project.git/blobdiff_plain/3cf8b39b331dd5f80c4ac674d13a6387d01d49b9..0019ed49158d0b3cbdcd63113f0dc4d640684186:/lib/functions.php?ds=sidebyside diff --git a/lib/functions.php b/lib/functions.php index bb49079..7b63148 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -67,26 +67,26 @@ jirafeau_upload_errstr ($code) { case UPLOAD_ERR_INI_SIZE: case UPLOAD_ERR_FORM_SIZE: - return _('Your file exceeds the maximum authorized file size. '); + return t('Your file exceeds the maximum authorized file size. '); break; case UPLOAD_ERR_PARTIAL: case UPLOAD_ERR_NO_FILE: return - _ + t ('Your file was not uploaded correctly. You may succeed in retrying. '); break; case UPLOAD_ERR_NO_TMP_DIR: case UPLOAD_ERR_CANT_WRITE: case UPLOAD_ERR_EXTENSION: - return _('Internal error. You may not succeed in retrying. '); + return t('Internal error. You may not succeed in retrying. '); break; default: break; } - return _('Unknown error. '); + return t('Unknown error. '); } /** Remove link and it's file @@ -99,9 +99,9 @@ jirafeau_delete ($link) if (!file_exists ( VAR_LINKS . $link)) return; - $content = file ( VAR_LINKS . $link); + $content = file (VAR_LINKS . $link); $md5 = trim ($content[5]); - unlink ( VAR_LINKS . $link); + unlink (VAR_LINKS . $link); $counter = 1; if (file_exists ( VAR_FILES . $md5. '_count')) @@ -118,13 +118,46 @@ jirafeau_delete ($link) fclose ($handle); } - if ($counter == 0 && file_exists ( VAR_FILES . $md5)) + if ($counter == 0) { - unlink ( VAR_FILES . $md5); + if (file_exists (VAR_FILES . $md5)) + unlink ( VAR_FILES . $md5); + if (file_exists (VAR_FILES . $md5 . '_count')) unlink ( VAR_FILES . $md5. '_count'); } } +/** + * Delete a file and it's links. + */ +function +jirafeau_delete_file ($md5) +{ + $count = 0; + $links_dir = scandir (VAR_LINKS); + + foreach ($links_dir as $link) + { + if (strcmp ($link, '.') == 0 || strcmp ($link, '..') == 0 || + preg_match ('/\.tmp/i', "$link")) + continue; + /* Read link informations. */ + $l = jirafeau_get_link ($link); + if ($l['md5'] == $md5) + { + $count++; + jirafeau_delete ($link); + } + } + + if (file_exists (VAR_FILES . $md5 . '_count')) + unlink (VAR_FILES . $md5. '_count'); + if (file_exists (VAR_FILES . $md5)) + unlink (VAR_FILES . $md5); + + return $count; +} + /** * handles an uploaded file * @param $file the file struct given by $_FILE[] @@ -175,7 +208,7 @@ jirafeau_upload ($file, $one_time_download, $key, $time, $cfg, $ip) return (array( 'error' => array ('has_error' => true, - 'why' => _('Internal error during file creation. ')), + 'why' => t('Internal error during file creation. ')), 'link' =>'', 'delete_link' => '')); } @@ -229,7 +262,7 @@ jirafeau_upload ($file, $one_time_download, $key, $time, $cfg, $ip) return (array( 'error' => array ('has_error' => true, - 'why' => _('Internal error during file creation. ')), + 'why' => t('Internal error during file creation. ')), 'link' =>'', 'delete_link' => '')); } @@ -302,4 +335,140 @@ show_errors () } } +/** + * Read link informations + * @return array containing informations. + */ +function +jirafeau_get_link ($hash) +{ + $out = array (); + $link = VAR_LINKS . $hash; + + if (!file_exists ($link)) + return $out; + + $c = file ($link); + $out['file_name'] = trim ($c[0]); + $out['mime_type'] = trim ($c[1]); + $out['file_size'] = trim ($c[2]); + $out['key'] = trim ($c[3], NL); + $out['time'] = trim ($c[4]); + $out['md5'] = trim ($c[5]); + $out['onetime'] = trim ($c[6]); + $out['upload_date'] = trim ($c[7]); + $out['ip'] = trim ($c[8]); + $out['link_code'] = trim ($c[9]); + + return $out; +} + +function +jirafeau_human_size ($octets) +{ + $u = array ('B', 'KB', 'MB', 'GB', 'TB'); + $o = max ($octets, 0); + $p = min (floor (($o ? log ($o) : 0) / log (1024)), count ($u) - 1); + $o /= pow (1024, $p); + return round ($o, 1) . $u[$p]; +} + +/** + * List files in admin interface. + */ +function +jirafeau_admin_list ($name, $file_hash, $link_hash) +{ + $links_dir = scandir (VAR_LINKS); + echo '
'; + if (!empty ($name)) + echo $name . ' '; + if (!empty ($file_hash)) + echo $file_hash . ' '; + if (!empty ($link_hash)) + echo $link_hash . ' '; + if (empty ($name) && empty ($file_hash) && empty ($link_hash)) + echo t('List all files'); + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + foreach ($links_dir as $link) + { + if (strcmp ($link, '.') == 0 || strcmp ($link, '..') == 0 || + preg_match ('/\.tmp/i', "$link")) + continue; + /* Read link informations. */ + $l = jirafeau_get_link ($link); + + /* Filter. */ + if (!empty ($name) && $name != $l['file_name']) + continue; + if (!empty ($file_hash) && $file_hash != $l['md5']) + continue; + if (!empty ($link_hash) && $link_hash != $link) + continue; + + /* Print link informations. */ + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + } + echo '
' . t('Filename') . '' . t('Type') . '' . t('Size') . '' . t('Expire') . '' . t('Onetime') . '' . t('Upload date') . '' . t('Origin') . '' . t('Action') . '
' . $l['file_name'] . '' . $l['mime_type'] . '' . jirafeau_human_size ($l['file_size']) . '' . ($l['time'] == -1 ? '' : strftime ('%c', $l['time'])) . + '' . $l['onetime'] . '' . strftime ('%c', $l['upload_date']) . '' . $l['ip'] . '' . + '
' . + '' . + '' . + '' . + '
' . + '
' . + '' . + '' . + '' . + '
' . + '
'; +} + +/** + * Clean expired files. + * @return number of cleaned files. + */ +function +jirafeau_admin_clean () +{ + $c = 0; + $links_dir = scandir (VAR_LINKS); + + foreach ($links_dir as $link) + { + if (strcmp ($link, '.') == 0 || strcmp ($link, '..') == 0 || + preg_match ('/\.tmp/i', "$link")) + continue; + /* Read link informations. */ + $l = jirafeau_get_link ($link); + if ($l['time'] > 0 && $l['time'] < time () || // expired + !file_exists (VAR_FILES . $l['md5']) || // invalid + !file_exists (VAR_FILES . $l['md5'] . '_count')) // invalid + { + jirafeau_delete ($link); + $c++; + } + } + return $c; +} ?>