widget/reply: load file upload progress indicator
[minimedit.git] / upload.inc.php
1 <?php
2 $Page->api = $_SERVER['HTTP_ACCEPT'] == 'text/plain';
3
4 function userupload($input, $target = NULL, $filename = NULL)
5 {
6         switch ($input['error']) {
7         case UPLOAD_ERR_OK:
8                 break;
9         case UPLOAD_ERR_INI_SIZE:
10         case UPLOAD_ERR_FORM_SIZE:
11                 throw new Exception('bestand te groot');
12                 break;
13         case UPLOAD_ERR_NO_FILE:
14                 return; # current
15         default:
16                 throw new Exception('bestand niet goed ontvangen: '.$input['error']);
17         }
18
19         if (isset($target)) {
20                 if (!file_exists($target) and !@mkdir($target, 0777, TRUE)) {
21                         throw new Exception("bestand kon niet geplaatst worden in $target");
22                 }
23                 $target .= '/';
24         }
25         if (isset($filename)) {
26                 $target .= $filename;
27         }
28         else {
29                 $target .= $input['name'];
30         }
31
32         if (!@move_uploaded_file($input['tmp_name'], $target)) {
33                 throw new Exception("bestand kon niet worden opgeslagen in $target");
34         }
35
36         foreach (@glob('thumb/*/') as $thumbres) {
37                 # attempt to remove old derivations
38                 @unlink($thumbres . '/' . $target);
39         }
40         return $target;
41 }
42
43 function messagehtml($input)
44 {
45         # convert user textarea post to formatted html
46         global $User;
47         if (empty($input)) {
48                 return;
49         }
50         if ($User->admin and preg_match('/\A<[a-z][^>]*>/', $input)) {
51                 return $input;  # allow html input as is if privileged
52         }
53         $html = preg_replace(
54                 ["/\r?\n/", "'(?:<br />\n?){2}'"],
55                 ["<br />\n", "</p>\n\n<p>"],
56                 htmlspecialchars($input)
57         );
58         return "<p>$html</p>";
59 }