1<?php
2
3require_once(dirname(__FILE__).'/../conf.php');
4
5function wiki_debug($name, $obj = "", $hsc = false) {
6    if (is_scalar($obj)) {
7        if ($hsc)
8            $obj = htmlspecialchars($obj);
9        echo "<p>" . "$name = $obj", "</p>";
10    }
11    else {
12        $obj = print_r($obj, true);
13        if ($hsc)
14            $obj = htmlspecialchars($obj);
15        echo "<pre> $name = ", $obj, "</pre>";
16    }
17}
18
19// recursively delete all the files
20function delete_dir($dir) {
21    if (($dh = opendir($dir)) != false) {
22        while (($file = readdir($dh)) !== false) {
23            if ($file === '.' || $file === '..') continue;
24            $file = $dir . $file;
25            if (is_dir($file))
26                delete_dir($file . '/');
27            else
28                unlink($file);
29        }
30        closedir($dh);
31    }
32    rmdir($dir);
33}
34
35function html_color_to_RGB($color) {
36    if (strlen($color) == 7 && substr($color, 0, 1) != '#')
37        return array(0, 0, 0);
38    $R = hexdec(substr($color, 1, 2));
39    $G = hexdec(substr($color, 3, 2));
40    $B = hexdec(substr($color, 5, 2));
41    return array('R' => $R, 'G' => $G, 'B' => $B);
42}
43
44function has_extension($name, $ext) {
45    $n = strlen($ext);
46    $l = strlen($name);
47    $tail = substr($name, $l - $n);
48    return stristr($tail, $ext) != false;
49}
50
51function replace_extension($name, $from, $to) {
52    return substr($name, 0, -strlen($from)) . $to;
53}
54
55function action_button($button_name, $action = '', $hidden = NULL) {
56    global $ID;
57    $form = new Doku_Form('Form_' . $button_name);
58    if (!$action) $action = $button_name;
59    $form->addHidden('do', $action);
60    if (is_array($hidden)) foreach ($hidden as $key => $value)
61        $form->addHidden($key, $value);
62    $form->addElement(form_makeButton('submit', '', $button_name));
63    return $form->getForm();
64}
65
66function render_code(&$renderer, $code, $lang) {
67    // code
68    $renderer->doc .= "<div class=\"code_block_code\">";
69    $geshi = new GeSHi($code, $lang);
70    $geshi->set_header_type(GESHI_HEADER_DIV);
71    $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
72    $renderer->doc .= $geshi->parse_code();
73    $renderer->doc .= "</div>";
74}
75
76function button_delete($ID) {
77    global $REV;
78    if (auth_quickaclcheck($ID) < AUTH_DELETE || $REV) return '';
79    return action_button('Delete', 'save', array('id' => $ID, 'wikitext' => '', 'summary' => "delete $ID"));
80}
81
82function button_remake($ID) {
83    global $REV;
84    global $INFO;
85    if (!$INFO['writable'] || $REV) return '';
86    return action_button('Remake', 'remake', array('id' => $ID));
87}
88
89function button_remove($range, $tag) {
90    global $ID;
91    global $REV;
92    global $INFO;
93    if (!$INFO['writable'] || $REV) return '';
94    return action_button('Remove', 'remove_tag', array(
95        'tag' => $tag,
96        'range' => $range
97    ));
98}
99
100function button_add($button_name, $tag, $name="") {
101    global $ID;
102    global $REV;
103    global $INFO;
104    if (!$INFO['writable'] || $REV) return '';
105    return action_button($button_name, 'add_tag', array(
106        'tag' => $tag,
107        'name', $name
108    ));
109}
110
111function set_media_file_revision_limit($limit) {
112    global $media_file_revision_limit;
113    if (!$limit) $limit = '0';
114    $limit = trim(strtoupper($limit));
115    switch ($limit) {
116        case '0':
117        case 'OFF':
118        case 'FALSE':
119        case 'NULL':
120            $media_file_revision_limit = 0;
121            break;
122        default:
123            if (!preg_match('/(\d+) *(B|KB|MB|GB|TB)?/', $limit, $matches)) {
124                msg('Cannot understand the configuration setting for "media file revision limit"', -1);
125                break;
126            }
127            $size = $matches[1];
128            if (count($matches) > 2) switch ($matches[2]) {
129                case 'TB':
130                    $size *= 1024;
131                case 'GB':
132                    $size *= 1024;
133                case 'MB':
134                    $size *= 1024;
135                case 'KB':
136                    $size *= 1024;
137            }
138            $media_file_revision_limit = $size;
139    }
140}
141
142function copy_updated_file($from, $to) {
143    return copy($from, $to);
144}
145
146function file_mimetype($id, $project) {
147    list($ext, $mime, $dl) = mimetype($id, false);
148    if ($ext === false)
149        $mime = 'application/octet-stream';
150    if ($mime == 'application/octet-stream') {
151        $path = $project->path() . noNS($id);
152        $finfo = new finfo(FILEINFO_MIME_TYPE);
153        $mime = $finfo->file($path);
154    }
155    return $mime;
156}
157
158?>