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 protected $imageAlignment; 12 13 /** 14 * @param $name 15 * @param $imageFolder 16 * @param $extension 17 */ 18 public function __construct($name, $imageFolder, $extension) { 19 parent::__construct($name); 20 $this->setImageFolder($imageFolder); 21 $this->extension = explode(',',$extension); 22 $this->imageAlignment = 'left'; 23 } 24 25 /** 26 * Parse template and display the form of the config manager 27 */ 28 public function display() { 29 $configs = $this->readConfig(); 30 $default = $configs['default']; 31 $local = $configs['local']; 32 $configs = array_merge($default, $local); 33 34 uksort($configs, [$this->helper, '_sortHuman']); 35 include DOKU_PLUGIN . 'confmanager/tpl/showConfigTwoLineLeftImage.php'; 36 } 37 38 /** 39 * Returns path to image file 40 * 41 * @param string $configtype 'local' or 'default' 42 * @param string $key 43 * @return string 44 */ 45 protected function getImagePath($configtype, $key) { 46 foreach($this->extension as $ext){ 47 $path = $this->imageFolder . "$key." . $ext; 48 if (is_file(DOKU_INC . $path)) { 49 return $path; 50 } 51 } 52 return ''; 53 } 54 55 /** 56 * Returns url to image file 57 * 58 * @param string $configtype 'local' or 'default' 59 * @param string $key 60 * @return string 61 */ 62 protected function getImage($configtype, $key) { 63 $path = $this->getImagePath($configtype, $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_position = strrpos($icon['name'], '.'); 112 if ($extension_position === false) { 113 header('Content-Type: text/plain'); 114 echo $this->helper->getLang('upload_errNoFileExtension'); 115 return false; 116 } 117 $extension = substr($icon['name'], $extension_position+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 $upload_name = substr($icon['name'], 0, $extension_position); 125 $destination = $this->getImageFilename($key, $value, $upload_name, $extension); 126 if(empty($destination)) { 127 header('Content-Type: text/plain'); 128 echo $this->helper->getLang('upload_errFilenameNotValid'); 129 return false; 130 } 131 132 if (!@move_uploaded_file($icon['tmp_name'], DOKU_INC . $this->imageFolder . $destination)) { 133 header('Content-Type: text/plain'); 134 echo $this->helper->getLang('upload_errCannotMoveUploadedFileToFolder'); 135 return false; 136 } 137 if (!$this->updateValue($key, $destination)) { 138 header('Content-Type: text/plain'); 139 echo $this->helper->getLang('upload_errUpdateOfConfigValueFailed'); 140 return false; 141 } 142 143 return true; 144 } 145 146 /** 147 * Build path to file location 148 * 149 * @param string $key key of entry 150 * @param string $value value of entry 151 * @param string $upload_name name of upload 152 * @param string $upload_extension extension of upload 153 * @return string 154 */ 155 protected function getImageFilename($key, $value, $upload_name, $upload_extension) { 156 return "$key." . $upload_extension; 157 } 158 159 /** 160 * Left image path cannot change by upload 161 * 162 * @param string $key 163 * @param string $value 164 * @return bool success? 165 */ 166 protected function updateValue($key, $value) { 167 return true; 168 } 169 170 /** 171 * @return bool 172 */ 173 public function deleteIcon() { 174 global $INPUT; 175 176 $key = $INPUT->str('key'); 177 if ($key === '') { 178 header('Content-Type: text/plain'); 179 echo $this->helper->getLang('upload_errNoConfigKeySend'); 180 return false; 181 } 182 183 $configs = $this->readConfig(); 184 if (isset($configs['default'][$key])) { 185 header('Content-Type: text/plain'); 186 echo $this->helper->getLang('upload_errCannotOverwriteDefaultKey'); 187 return false; 188 } 189 190 $path = $this->getImagePath('local', $key); 191 if (!@unlink(DOKU_INC . $path)) { 192 echo $this->helper->getLang('iconDelete_error'); 193 return false; 194 } 195 196 return true; 197 } 198 199 200} 201