]> git.p6c8.net - jirafeau_mojo42.git/blob - lib/functions.php
7a79840c2b2690c7184450b31202ddbd8872bd5c
[jirafeau_mojo42.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 /**
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
25 */
26 function jirafeau_ini_to_bytes($value) {
27 $modifier = substr($value, -1);
28 $bytes = substr($value, 0, -1);
29 switch(strtoupper($modifier)) {
30 case 'P':
31 $bytes *= 1024;
32 case 'T':
33 $bytes *= 1024;
34 case 'G':
35 $bytes *= 1024;
36 case 'M':
37 $bytes *= 1024;
38 case 'K':
39 $bytes *= 1024;
40 default:
41 break;
42 }
43 return $bytes;
44 }
45
46 /**
47 * gets the maximum upload size according to php.ini
48 * @returns the maximum upload size
49 */
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')));
52 }
53
54 /**
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
59 */
60 function jirafeau_detect_collision($name, $dir) {
61 if(!file_exists($dir . $name)) {
62 return $name;
63 }
64
65 $dot = strpos($name, '.');
66 $dot = ($dot === false) ? strlen($name) : $dot;
67 $first = substr($name, 0, $dot);
68 $second = substr($name, $dot);
69 $i = 1;
70 do {
71 $new_name = $first . '-' . $i . $second;
72 $i++;
73 } while(file_exists($dir . $new_name));
74
75 return $new_name;
76 }
77
78 /**
79 * gets a string explaining the error
80 * @param $code the error code
81 * @returns a string explaining the error
82 */
83 function jirafeau_upload_errstr($code) {
84 switch($code) {
85 case UPLOAD_ERR_INI_SIZE:
86 case UPLOAD_ERR_FORM_SIZE:
87 return _('Your file exceeds the maximum authorized file size.');
88 break;
89
90 case UPLOAD_ERR_PARTIAL:
91 case UPLOAD_ERR_NO_FILE:
92 return _('Your file was not uploaded correctly. You may succeed in retrying.');
93 break;
94
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.');
99 break;
100
101 default:
102 break;
103 }
104 return _('Unknown error.');
105 }
106
107 /** Remove link and it's file
108 * @param $link the link's name (hash)
109 */
110
111 function jirafeau_delete($link) {
112 if(!file_exists(VAR_LINKS . $link))
113 return;
114
115 $content = file(VAR_LINKS . $link);
116 $md5 = trim($content[5]);
117 unlink(VAR_LINKS . $link);
118
119 $counter = 1;
120 if (file_exists(VAR_FILES . $md5 . '_count')) {
121 $content = file(VAR_FILES . $md5 . '_count');
122 $counter = trim($content[0]);
123 }
124 $counter--;
125
126 if ($counter >= 1) {
127 $handle = fopen(VAR_FILES . $md5 . '_count', 'w');
128 fwrite($handle, $counter);
129 fclose($handle);
130 }
131
132 if ($counter == 0 && file_exists(VAR_FILES. $md5)) {
133 unlink (VAR_FILES . $md5);
134 unlink (VAR_FILES . $md5 . '_count');
135 }
136 }
137
138 /**
139 * handles an uploaded file
140 * @param $file the file struct given by $_FILE[]
141 * @param $one_time_download is the file a one time download ?
142 * @param $key if not empty, protect the file with this key
143 * @param $time the time of validity of the file
144 * @param $cfg the current configuration
145 * @param $ip uploader's ip
146 * @returns an array containing some information
147 * 'error' => information on possible errors
148 * 'link' => the link name of the uploaded file
149 */
150 function jirafeau_upload($file, $one_time_download, $key, $time, $cfg, $ip) {
151 if(empty($file['tmp_name']) || !is_uploaded_file($file['tmp_name'])) {
152 return(array('error' => array('has_error' => true, 'why' => jirafeau_upload_errstr($file['error'])), 'link' => ''));
153 }
154
155 /* array representing no error */
156 $noerr = array('has_error' => false, 'why' => '');
157
158 /* file informations */
159 $md5 = md5_file($file['tmp_name']);
160 $name = trim($file['name']);
161 $mime_type = $file['type'];
162 $size = $file['size'];
163
164 /* does file already exist ? */
165 $rc = false;
166 if(file_exists(VAR_FILES . $md5)) {
167 $rc = unlink($file['tmp_name']);
168 }
169 elseif(move_uploaded_file($file['tmp_name'], VAR_FILES . $md5)) {
170 $rc = true;
171 }
172 if(!$rc)
173 {
174 return(array(
175 'error' => array(
176 'has_error' => true,
177 'why' => _('Internal error during file creation.')),
178 'link' => '')
179 );
180 }
181
182 /* increment or create count file */
183 $counter=0;
184 if(file_exists(VAR_FILES . $md5 . '_count')) {
185 $content = file(VAR_FILES . $md5 . '_count');
186 $counter = trim($content[0]);
187 }
188 $counter++;
189 $handle = fopen(VAR_FILES . $md5 . '_count', 'w');
190 fwrite($handle, $counter);
191 fclose($handle);
192
193 /* create link file */
194 $link_tmp_name = VAR_LINKS . $md5 . rand(0, 10000) . '.tmp';
195 $handle = fopen($link_tmp_name, 'w');
196 fwrite($handle, $name . NL . $mime_type . NL . $size . NL . $key . NL . $time . NL . $md5 . NL . ($one_time_download ? 'O' : 'R') . NL . date('U') . NL . $ip . NL);
197 fclose($handle);
198 $md5_link = md5_file($link_tmp_name);
199 if(!rename($link_tmp_name, VAR_LINKS . $md5_link)) {
200 unlink($link_tmp_name);
201 $counter--;
202 if ($counter >= 1) {
203 $handle = fopen(VAR_FILES . $md5 . '_count', 'w');
204 fwrite($handle, $counter);
205 fclose($handle);
206 }
207 else {
208 unlink(VAR_FILES . $md5 . '_count');
209 unlink(VAR_FILES . $md5);
210 }
211 return(array(
212 'error' => array(
213 'has_error' => true,
214 'why' => _('Internal error during file creation.')),
215 'link' => '')
216 );
217 }
218 return(array('error' => $noerr, 'link' => $md5_link));
219 }
220
221 /**
222 * tells if a mime-type is viewable in a browser
223 * @param $mime the mime type
224 * @returns a boolean telling if a mime type is viewable
225 */
226 function jirafeau_is_viewable($mime) {
227 if(!empty($mime)) {
228 // actually, verify if mime-type is an image or a text
229 $viewable = array('image', 'text');
230 $decomposed = explode('/', $mime);
231 return in_array($decomposed[0], $viewable);
232 }
233 return false;
234 }
235
236
237 // Error handling functions.
238 //! Global array that contains all registered errors.
239 $error_list = array ();
240
241 /**
242 * Adds an error to the list of errors.
243 * @param $title the error's title
244 * @param $description is a human-friendly description of the problem.
245 */
246 function add_error ($title, $description) {
247 global $error_list;
248 $error_list[] = '<p>' . $title . '<br />' . $description . '</p>';
249 }
250
251 /**
252 * Informs whether any error has been registered yet.
253 * @return true if there are errors.
254 */
255 function has_error () {
256 global $error_list;
257 return !empty ($error_list);
258 }
259
260 /**
261 * Displays all the errors.
262 */
263 function show_errors () {
264 if (has_error ()) {
265 global $error_list;
266 echo '<div class="error">';
267 foreach ($error_list as $error) {
268 echo $error;
269 }
270 echo '</div>';
271 }
272 }
273
274 ?>

patrick-canterino.de