]>
git.p6c8.net - jirafeau.git/blob - lib/functions.php
cb2226a2a15285968dfb2a4e8a1d9e73d12aba10
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/>.
22 * transforms a php.ini string representing a value in an integer
23 * @param $value the value from php.ini
24 * @returns an integer for this value
26 function jirafeau_ini_to_bytes($value) {
27 $modifier = substr($value, -1);
28 $bytes = substr($value, 0, -1);
29 switch(strtoupper($modifier)) {
47 * gets the maximum upload size according to php.ini
48 * @returns the maximum upload size
50 function jirafeau_get_max_upload_size() {
51 return min(jirafeau_ini_to_bytes(ini_get('post_max_size')), jirafeau_ini_to_bytes(ini_get('upload_max_filesize')));
55 * detects if a given filename is present in a directory and find an alternate filename
56 * @param $name the initial filename
57 * @param $dir the directory to explore (finishing with a '/')
58 * @returns an alternate filename, possibly the initial filename
60 function jirafeau_detect_collision($name, $dir) {
61 if(!file_exists($dir . $name)) {
65 $dot = strpos($name, '.');
66 $dot = ($dot === false) ?
strlen($name) : $dot;
67 $first = substr($name, 0, $dot);
68 $second = substr($name, $dot);
71 $new_name = $first . '-' . $i . $second;
73 } while(file_exists($dir . $new_name));
79 * gets a string explaining the error
80 * @param $code the error code
81 * @returns a string explaining the error
83 function jirafeau_upload_errstr($code) {
85 case UPLOAD_ERR_INI_SIZE
:
86 case UPLOAD_ERR_FORM_SIZE
:
87 return _('Your file exceeds the maximum authorized file size.');
90 case UPLOAD_ERR_PARTIAL
:
91 case UPLOAD_ERR_NO_FILE
:
92 return _('Your file was not uploaded correctly. You may succeed in retrying.');
95 case UPLOAD_ERR_NO_TMP_DIR
:
96 case UPLOAD_ERR_CANT_WRITE
:
97 case UPLOAD_ERR_EXTENSION
:
98 return _('Internal error. You may not succeed in retrying.');
104 return _('Unknown error.');
108 * handles an uploaded file
109 * @param $file the file struct given by $_FILE[]
110 * @param $one_time_download is the file a one time download ?
111 * @param $key if not empty, protect the file with this key
112 * @param $time the time of validity of the file
113 * @param $cfg the current configuration
114 * @returns an array containing some information
115 * 'error' => information on possible errors
116 * 'link' => the link name of the uploaded file
118 function jirafeau_upload($file, $one_time_download, $key, $time, $cfg) {
119 if(empty($file['tmp_name']) ||
!is_uploaded_file($file['tmp_name'])) {
120 return(array('error' => array('has_error' => true, 'why' => jirafeau_upload_errstr($file['error'])), 'link' => ''));
123 /* array representing no error */
124 $noerr = array('has_error' => false, 'why' => '');
126 /* file informations */
127 $md5 = md5_file($file['tmp_name']);
128 $name = trim($file['name']);
129 $mime_type = $file['type'];
130 $size = $file['size'];
132 /* does file already exist ? */
134 if(file_exists(VAR_FILES
. $md5)) {
135 $rc = unlink($file['tmp_name']);
137 elseif(move_uploaded_file($file['tmp_name'], VAR_FILES
. $md5)) {
145 'why' => _('Internal error during file creation.')),
150 /* increment or create count file */
152 if(file_exists(VAR_FILES
. $md5 . '_count')) {
153 $content = file(VAR_FILES
. $md5 . '_count');
154 $counter = trim($content[0]);
157 $handle = fopen(VAR_FILES
. $md5 . '_count', 'w');
158 fwrite($handle, $counter);
161 /* create link file */
162 $link_tmp_name = VAR_LINKS
. $md5 . rand(0, 10000) . '.tmp';
163 $handle = fopen($link_tmp_name, 'w');
164 fwrite($handle, $name . NL
. $mime_type . NL
. $size . NL
. $key . NL
. $time . NL
. $md5 . NL
. ($one_time_download ?
'O' : 'R') . NL
. date('U') . NL
);
166 $md5_link = md5_file($link_tmp_name);
167 if(!rename($link_tmp_name, VAR_LINKS
. $md5_link)) {
170 $handle = fopen(VAR_FILES
. $md5 . '_count', 'w');
171 fwrite($handle, $counter);
175 unlink($link_tmp_name);
176 unlink(VAR_FILE
. $md5 . '_count');
177 unlink(VAR_FILE
. $md5);
182 'why' => _('Internal error during file creation.')),
186 return(array('error' => $noerr, 'link' => $md5_link));
190 * tells if a mime-type is viewable in a browser
191 * @param $mime the mime type
192 * @returns a boolean telling if a mime type is viewable
194 function jirafeau_is_viewable($mime) {
196 // actually, verify if mime-type is an image or a text
197 $viewable = array('image', 'text');
198 $decomposed = explode('/', $mime);
199 return in_array($decomposed[0], $viewable);
205 // Error handling functions.
206 //! Global array that contains all registered errors.
207 $error_list = array ();
210 * Adds an error to the list of errors.
211 * @param $title the error's title
212 * @param $description is a human-friendly description of the problem.
214 function add_error ($title, $description) {
216 $error_list[] = '<p>' . $title . '<br />' . $description . '</p>';
220 * Informs whether any error has been registered yet.
221 * @return true if there are errors.
223 function has_error () {
225 return !empty ($error_list);
229 * Displays all the errors.
231 function show_errors () {
234 echo '<div class="error">';
235 foreach ($error_list as $error) {
patrick-canterino.de