widget/reply: formatting syntax for unordered lists
[minimedit.git] / upload.inc.php
1 <?php
2 function userupload($input, $target = NULL, $filename = NULL)
3 {
4         switch ($input['error']) {
5         case UPLOAD_ERR_OK:
6                 break;
7         case UPLOAD_ERR_INI_SIZE:
8         case UPLOAD_ERR_FORM_SIZE:
9                 throw new Exception('bestand te groot');
10                 break;
11         case UPLOAD_ERR_NO_FILE:
12                 return; # current
13         default:
14                 throw new Exception('bestand niet goed ontvangen: '.$input['error']);
15         }
16
17         if (isset($target)) {
18                 if (!file_exists($target) and !@mkdir($target, 0777, TRUE)) {
19                         throw new Exception("bestand kon niet geplaatst worden in $target");
20                 }
21                 $target .= '/';
22         }
23         if (isset($filename)) {
24                 $target .= $filename;
25         }
26         else {
27                 $target .= $input['name'];
28         }
29
30         if (file_exists($target)) {
31                 throw new Exception("bestandsnaam al aanwezig op $target");
32         }
33         if (!@move_uploaded_file($input['tmp_name'], $target)) {
34                 throw new Exception("bestand kon niet worden opgeslagen in $target");
35         }
36
37         foreach (@glob('thumb/*/') as $thumbres) {
38                 # attempt to remove old derivations
39                 @unlink($thumbres . '/' . $target);
40         }
41         return $target;
42 }
43
44 function messagehtml($input)
45 {
46         # convert user textarea post to formatted html
47         global $User;
48         if (empty($input)) {
49                 return;
50         }
51         if ($User->admin and preg_match('/\A<[a-z][^>]*>/', $input)) {
52                 return $input;  # allow html input as is if privileged
53         }
54         $markup = [
55                 '{&lt;((?:\w+:|/).+?)&gt;}'    => '<$1>',                # unescape link entities
56                 '{<(?:https?://)?([^>\s|]+)>}' => '<$1 $1>',             # unnamed link
57                 '{<([^>\s|]+)[\s|]([^>]+)>}'   => '<a href="$1">$2</a>', # hyperlink
58                 "/\r\n?/" => "\n",        # unix newlines
59                 "/  +\n/" => "<br />",    # trailing spaces for hard line break
60                 "/^[-*] (.*)$\n?/m"            => '<li>$1</li>',         # list item
61                 "/^(.+)$\n?/m"                 => "<p>$1</p>\n",         # paragraph
62                 "{^<p>(<li>.*</li>)(?:</p>\n)?}m" => "<ul>$1</ul>\n",    # list container
63                 '/_(?<!\w_)(.+?)_(?!\w)/'      => '<em>$1</em>',         # italic
64                 '/\*(?<!\w\*)(.+?)\*(?!\w)/'   => '<strong>$1</strong>', # bold
65                 '/~(?<!\w~)(.+?)~(?!\w)/'      => '<s>$1</s>',           # stricken
66                 '/`(?<!\w`)(.+?)`(?!\w)/'      => '<code>$1</code>',     # monospace
67         ];
68         return preg_replace(array_keys($markup), array_values($markup), htmlspecialchars($input));
69 }