1<?php
2
3/**
4 * Like ConfigManagerTwoLineCascadeConfig but with image support.
5 * An image can be assigned for every key. I.e. the mime or interwiki config
6 */
7class ConfigManagerTwoLineLeftImageConfigCascade extends ConfigManagerTwoLineCascadeConfig implements ConfigManagerUploadable {
8
9    protected $imageFolder;
10    protected $extension;
11
12    /**
13     * @param $name
14     * @param $imageFolder
15     * @param $extension
16     */
17    public function __construct($name, $imageFolder, $extension) {
18        parent::__construct($name);
19         $this->setImageFolder($imageFolder);
20        $this->extension = explode(',',$extension);
21    }
22
23    public function display() {
24        $this->displayTpl(DOKU_PLUGIN . 'confmanager/tpl/showConfigTwoLineLeftImage.php');
25    }
26
27    /**
28     * @param string $tpl file path
29     */
30    protected function displayTpl($tpl) {
31        $configs = $this->readConfig();
32        $default = $configs['default'];
33        $local = $configs['local'];
34        $configs = array_merge($default, $local);
35
36        uksort($configs, array($this->helper, '_sortHuman'));
37        include $tpl;
38    }
39
40    /**
41     * Returns path to image file
42     *
43     * @param string $key
44     * @return string
45     */
46    protected function getImagePath($key) {
47        foreach($this->extension as $ext){
48            $path = $this->imageFolder . "$key." . $ext;
49             if (is_file(DOKU_INC . $path)) {
50                return $path;
51            }
52        }
53        return '';
54    }
55
56    /**
57     * Returns url to image file
58     *
59     * @param string $key
60     * @return string
61     */
62    protected function getImage($key) {
63        $path = $this->getImagePath($key);
64        if($path) {
65            return DOKU_BASE . $path;
66        }
67        return '';
68    }
69
70    /**
71     * @param string $imageFolder
72     */
73    public function setImageFolder($imageFolder) {
74        if (substr($imageFolder, strlen($imageFolder) -1) !== '/') {
75            $imageFolder = "$imageFolder/";
76        }
77        $this->imageFolder = $imageFolder;
78    }
79
80    /**
81     * @return bool
82     */
83    public function upload() {
84        global $INPUT;
85        if (!isset($_FILES['icon'])) {
86            header('Content-Type: text/plain');
87            echo $this->helper->getLang('upload_errNoFileSend');
88            return false;
89        }
90        $icon = $_FILES['icon'];
91        $key = $INPUT->str('key');
92        $value = $INPUT->str('value');
93        if ($key === '') {
94            header('Content-Type: text/plain');
95            echo $this->helper->getLang('upload_errNoConfigKeySend');
96            return false;
97        }
98        $configs = $this->readConfig();
99        if (isset($configs['default'][$key])) {
100            header('Content-Type: text/plain');
101            echo $this->helper->getLang('upload_errCannotOverwriteDefaultKey');
102            return false;
103        }
104
105        if ($icon['error'] != UPLOAD_ERR_OK) {
106            header('Content-Type: text/plain');
107            echo $this->helper->getLang('upload_errUploadError');
108            return false;
109        }
110
111        $extension = strrpos($icon['name'], '.');
112        if ($extension === false) {
113            header('Content-Type: text/plain');
114            echo $this->helper->getLang('upload_errNoFileExtension');
115            return false;
116        }
117        $extension = substr($icon['name'], $extension+1);
118        if (!in_array($extension, $this->extension)) {
119            header('Content-Type: text/plain');
120            echo $this->helper->getLang('upload_errWrongFileExtension');
121            return false;
122        }
123
124        $destination = $this->getImageDestination($key, $value, $extension, $icon['name']);
125        if (!@move_uploaded_file($icon['tmp_name'], $destination)) {
126            header('Content-Type: text/plain');
127            echo $this->helper->getLang('upload_errCannotMoveUploadedFileToFolder');
128            return false;
129        }
130
131        return true;
132    }
133
134    /**
135     * Build path to file location
136     *
137     * @param string $key
138     * @param string $value
139     * @param string $extension
140     * @return string
141     */
142    protected function getImageDestination($key, $value, $extension, $filename) {
143         return DOKU_INC . $this->imageFolder . "$key." . $extension;
144    }
145
146    /**
147     * @return bool
148     */
149    public function deleteIcon() {
150        global $INPUT;
151
152        $key = $INPUT->str('key');
153        if ($key === '') {
154            header('Content-Type: text/plain');
155            echo $this->helper->getLang('upload_errNoConfigKeySend');
156            return false;
157        }
158
159        $configs = $this->readConfig();
160        if (isset($configs['default'][$key])) {
161            header('Content-Type: text/plain');
162            echo $this->helper->getLang('upload_errCannotOverwriteDefaultKey');
163            return false;
164        }
165
166        $path = $this->getImagePath($key);
167        if (!@unlink(DOKU_INC . $path)) {
168            echo $this->helper->getLang('iconDelete_error');
169            return false;
170        }
171
172        return true;
173    }
174
175
176}
177