1<?php
2
3/**
4 * Like ConfigManagerTwoLineCascadeConfig but with image support.
5 * An image pointed with value. I.e. the smileys config
6 */
7class ConfigManagerTwoLineRightImageConfigCascade extends ConfigManagerTwoLineLeftImageConfigCascade {
8
9    public function display() {
10        $this->displayTpl(DOKU_PLUGIN . 'confmanager/tpl/showConfigTwoLineRightImage.php');
11    }
12
13    /**
14     * Returns path to image file
15     *
16     * @param string $key
17     * @return string
18     */
19    protected function getImagePath($key) {
20        $configs = $this->readConfig();
21        $default = $configs['default'];
22        $local = $configs['local'];
23        $configs = array_merge($default, $local);
24
25        $path = $this->imageFolder . $configs[$key];
26
27        if (is_file(DOKU_INC . $path)) {
28            return $path;
29        }
30        return '';
31    }
32
33    /**
34     * Build path to file location
35     *
36     * @param string $key
37     * @param string $value
38     * @param string $extension
39     * @param string $filename  filename provided by upload
40     * @return string
41     */
42    protected function getImageDestination($key, $value, $extension, $filename) {
43        $use_form_value = true;
44        $ext_value    = strrpos($value, '.');
45        if ($ext_value === false) {
46            //no extension
47            $use_form_value = false;
48        } else {
49            $ext_value = strtolower(substr($value, $ext_value + 1));
50            if($ext_value != $extension) {
51                // image has different extension than predefined location
52                $use_form_value = false;
53            }
54        }
55
56        if($use_form_value) {
57            $filename = $value;
58        }
59        $filename = trim($filename);
60        if(substr($filename, 0, 6) !=  'local/') {
61            $filename = 'local/' . $filename;
62        }
63
64        return DOKU_INC . $this->imageFolder . $filename;
65    }
66
67
68    /**
69     * @return bool
70     */
71    public function deleteIcon() {
72        global $INPUT;
73
74        $key = $INPUT->str('key');
75        if ($key === '') {
76            header('Content-Type: text/plain');
77            echo $this->helper->getLang('upload_errNoConfigKeySend');
78            return false;
79        }
80
81        $configs = $this->readConfig();
82        if (isset($configs['default'][$key])) {
83            header('Content-Type: text/plain');
84            echo $this->helper->getLang('upload_errCannotOverwriteDefaultKey');
85            return false;
86        }
87
88        $path = $this->getImagePath($key);
89        if (!@unlink(DOKU_INC . $path)) {
90            echo $this->helper->getLang('iconDelete_error');
91            return false;
92        }
93
94        return true;
95    }
96}
97