]> git.p6c8.net - jirafeau.git/blob - lib/functions.php
changed label key to password
[jirafeau.git] / lib / functions.php
1 <?php
2 /*
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>
6 *
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.
11 *
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.
16 *
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/>.
19 */
20 /**
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
24 */
25 function jirafeau_ini_to_bytes ($value)
26 {
27 $modifier = substr ($value, -1);
28 $bytes = substr ($value, 0, -1);
29 switch (strtoupper ($modifier))
30 {
31 case 'P':
32 $bytes *= 1024;
33 case 'T':
34 $bytes *= 1024;
35 case 'G':
36 $bytes *= 1024;
37 case 'M':
38 $bytes *= 1024;
39 case 'K':
40 $bytes *= 1024;
41 default:
42 break;
43 }
44 return $bytes;
45 }
46
47 /**
48 * gets the maximum upload size according to php.ini
49 * @returns the maximum upload size
50 */
51 function
52 jirafeau_get_max_upload_size ()
53 {
54 return min (jirafeau_ini_to_bytes (ini_get ('post_max_size')),
55 jirafeau_ini_to_bytes (ini_get ('upload_max_filesize')));
56 }
57
58 /**
59 * gets a string explaining the error
60 * @param $code the error code
61 * @returns a string explaining the error
62 */
63 function
64 jirafeau_upload_errstr ($code)
65 {
66 switch ($code)
67 {
68 case UPLOAD_ERR_INI_SIZE:
69 case UPLOAD_ERR_FORM_SIZE:
70 return _('Your file exceeds the maximum authorized file size. ');
71 break;
72
73 case UPLOAD_ERR_PARTIAL:
74 case UPLOAD_ERR_NO_FILE:
75 return
76 _
77 ('Your file was not uploaded correctly. You may succeed in retrying. ');
78 break;
79
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. ');
84 break;
85
86 default:
87 break;
88 }
89 return _('Unknown error. ');
90 }
91
92 /** Remove link and it's file
93 * @param $link the link's name (hash)
94 */
95
96 function
97 jirafeau_delete ($link)
98 {
99 if (!file_exists ( VAR_LINKS . $link))
100 return;
101
102 $content = file ( VAR_LINKS . $link);
103 $md5 = trim ($content[5]);
104 unlink ( VAR_LINKS . $link);
105
106 $counter = 1;
107 if (file_exists ( VAR_FILES . $md5. '_count'))
108 {
109 $content = file ( VAR_FILES . $md5. '_count');
110 $counter = trim ($content[0]);
111 }
112 $counter--;
113
114 if ($counter >= 1)
115 {
116 $handle = fopen ( VAR_FILES . $md5. '_count', 'w');
117 fwrite ($handle, $counter);
118 fclose ($handle);
119 }
120
121 if ($counter == 0 && file_exists ( VAR_FILES . $md5))
122 {
123 unlink ( VAR_FILES . $md5);
124 unlink ( VAR_FILES . $md5. '_count');
125 }
126 }
127
128 /**
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
140 */
141 function
142 jirafeau_upload ($file, $one_time_download, $key, $time, $cfg, $ip)
143 {
144 if (empty ($file['tmp_name']) || !is_uploaded_file ($file['tmp_name']))
145 {
146 return (array(
147 'error' =>
148 array ('has_error' => true,
149 'why' => jirafeau_upload_errstr ($file['error'])),
150 'link' => '',
151 'delete_link' => ''));
152 }
153
154 /* array representing no error */
155 $noerr = array ('has_error' => false, 'why' => '');
156
157 /* file informations */
158 $md5 = md5_file ($file['tmp_name']);
159 $name = trim ($file['name']);
160 $mime_type = $file['type'];
161 $size = $file['size'];
162
163 /* does file already exist ? */
164 $rc = false;
165 if (file_exists ( VAR_FILES . $md5))
166 {
167 $rc = unlink ($file['tmp_name']);
168 }
169 elseif (move_uploaded_file ($file['tmp_name'], VAR_FILES . $md5))
170 {
171 $rc = true;
172 }
173 if (!$rc)
174 {
175 return (array(
176 'error' =>
177 array ('has_error' => true,
178 'why' => _('Internal error during file creation. ')),
179 'link' =>'',
180 'delete_link' => ''));
181 }
182
183 /* increment or create count file */
184 $counter = 0;
185 if (file_exists (VAR_FILES . $md5 . '_count'))
186 {
187 $content = file ( VAR_FILES . $md5. '_count');
188 $counter = trim ($content[0]);
189 }
190 $counter++;
191 $handle = fopen ( VAR_FILES . $md5. '_count', 'w');
192 fwrite ($handle, $counter);
193 fclose ($handle);
194
195 /* Create delete code. */
196 $delete_link_code = 0;
197 for ($i = 0; $i < 8; $i++)
198 $delete_link_code .= dechex (rand (0, 16));
199
200 /* md5 password or empty */
201 $password = '';
202 if (!empty ($key))
203 $password = md5 ($key);
204
205 /* create link file */
206 $link_tmp_name = VAR_LINKS . $md5.rand (0, 10000) . ' .tmp';
207 $handle = fopen ($link_tmp_name, 'w');
208 fwrite ($handle,
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);
212 fclose ($handle);
213 $md5_link = md5_file ($link_tmp_name);
214 if (!rename ($link_tmp_name, VAR_LINKS . $md5_link))
215 {
216 unlink ($link_tmp_name);
217 $counter--;
218 if ($counter >= 1)
219 {
220 $handle = fopen ( VAR_FILES . $md5. '_count', 'w');
221 fwrite ($handle, $counter);
222 fclose ($handle);
223 }
224 else
225 {
226 unlink ( VAR_FILES . $md5. '_count');
227 unlink ( VAR_FILES . $md5);
228 }
229 return (array(
230 'error' =>
231 array ('has_error' => true,
232 'why' => _('Internal error during file creation. ')),
233 'link' =>'',
234 'delete_link' => ''));
235 }
236 return (array ('error' => $noerr,
237 'link' => $md5_link,
238 'delete_link' => $delete_link_code));
239 }
240
241 /**
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
245 */
246 function
247 jirafeau_is_viewable ($mime)
248 {
249 if (!empty ($mime))
250 {
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);
255 }
256 return false;
257 }
258
259
260 // Error handling functions.
261 //! Global array that contains all registered errors.
262 $error_list = array ();
263
264 /**
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.
268 */
269 function
270 add_error ($title, $description)
271 {
272 global $error_list;
273 $error_list[] = '<p>' . $title. '<br />' . $description. '</p>';
274 }
275
276 /**
277 * Informs whether any error has been registered yet.
278 * @return true if there are errors.
279 */
280 function
281 has_error ()
282 {
283 global $error_list;
284 return !empty ($error_list);
285 }
286
287 /**
288 * Displays all the errors.
289 */
290 function
291 show_errors ()
292 {
293 if (has_error ())
294 {
295 global $error_list;
296 echo '<div class="error">';
297 foreach ($error_list as $error)
298 {
299 echo $error;
300 }
301 echo '</div>';
302 }
303 }
304
305 ?>

patrick-canterino.de