]>
git.p6c8.net - jirafeau_mojo42.git/blob - lib/functions.php
bb49079768081e381d53752722c5f911ea210620
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 _('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 _('Internal error. You may not succeed in retrying. ');
89 return _('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);
121 if ($counter == 0 && file_exists ( VAR_FILES
. $md5))
123 unlink ( VAR_FILES
. $md5);
124 unlink ( VAR_FILES
. $md5. '_count');
129 * handles an uploaded file
130 * @param $file the file struct given by $_FILE[]
131 * @param $one_time_download is the file a one time download ?
132 * @param $key if not empty, protect the file with this key
133 * @param $time the time of validity of the file
134 * @param $cfg the current configuration
135 * @param $ip uploader's ip
136 * @returns an array containing some information
137 * 'error' => information on possible errors
138 * 'link' => the link name of the uploaded file
139 * 'delete_link' => the link code to delete file
142 jirafeau_upload ($file, $one_time_download, $key, $time, $cfg, $ip)
144 if (empty ($file['tmp_name']) ||
!is_uploaded_file ($file['tmp_name']))
148 array ('has_error' => true,
149 'why' => jirafeau_upload_errstr ($file['error'])),
151 'delete_link' => ''));
154 /* array representing no error */
155 $noerr = array ('has_error' => false, 'why' => '');
157 /* file informations */
158 $md5 = md5_file ($file['tmp_name']);
159 $name = trim ($file['name']);
160 $mime_type = $file['type'];
161 $size = $file['size'];
163 /* does file already exist ? */
165 if (file_exists ( VAR_FILES
. $md5))
167 $rc = unlink ($file['tmp_name']);
169 elseif (move_uploaded_file ($file['tmp_name'], VAR_FILES
. $md5))
177 array ('has_error' => true,
178 'why' => _('Internal error during file creation. ')),
180 'delete_link' => ''));
183 /* increment or create count file */
185 if (file_exists (VAR_FILES
. $md5 . '_count'))
187 $content = file ( VAR_FILES
. $md5. '_count');
188 $counter = trim ($content[0]);
191 $handle = fopen ( VAR_FILES
. $md5. '_count', 'w');
192 fwrite ($handle, $counter);
195 /* Create delete code. */
196 $delete_link_code = 0;
197 for ($i = 0; $i < 8; $i++
)
198 $delete_link_code .= dechex (rand (0, 16));
200 /* md5 password or empty */
203 $password = md5 ($key);
205 /* create link file */
206 $link_tmp_name = VAR_LINKS
. $md5.rand (0, 10000) . ' .tmp';
207 $handle = fopen ($link_tmp_name, 'w');
209 $name . NL
. $mime_type . NL
. $size . NL
. $password . NL
. $time . NL
. $md5.
210 NL
.($one_time_download ?
'O' : 'R') . NL
.date ('U') . NL
. $ip . NL
.
211 $delete_link_code . NL
);
213 $md5_link = md5_file ($link_tmp_name);
214 if (!rename ($link_tmp_name, VAR_LINKS
. $md5_link))
216 unlink ($link_tmp_name);
220 $handle = fopen ( VAR_FILES
. $md5. '_count', 'w');
221 fwrite ($handle, $counter);
226 unlink ( VAR_FILES
. $md5. '_count');
227 unlink ( VAR_FILES
. $md5);
231 array ('has_error' => true,
232 'why' => _('Internal error during file creation. ')),
234 'delete_link' => ''));
236 return (array ('error' => $noerr,
238 'delete_link' => $delete_link_code));
242 * tells if a mime-type is viewable in a browser
243 * @param $mime the mime type
244 * @returns a boolean telling if a mime type is viewable
247 jirafeau_is_viewable ($mime)
251 /* Actually, verify if mime-type is an image or a text. */
252 $viewable = array ('image', 'text');
253 $decomposed = explode ('/', $mime);
254 return in_array ($decomposed[0], $viewable);
260 // Error handling functions.
261 //! Global array that contains all registered errors.
262 $error_list = array ();
265 * Adds an error to the list of errors.
266 * @param $title the error's title
267 * @param $description is a human-friendly description of the problem.
270 add_error ($title, $description)
273 $error_list[] = '<p>' . $title. '<br />' . $description. '</p>';
277 * Informs whether any error has been registered yet.
278 * @return true if there are errors.
284 return !empty ($error_list);
288 * Displays all the errors.
296 echo '<div class="error">';
297 foreach ($error_list as $error)
patrick-canterino.de