1<?php 2 3use dokuwiki\Extension\Event; 4 5/** 6 * Class helper_plugin_confmanager 7 */ 8class helper_plugin_confmanager extends DokuWiki_Plugin { 9 10 /** 11 * Get all registered config managers 12 * 13 * @return ConfigManagerConfigType[] 14 */ 15 public function getConfigFiles() { 16 static $configs = null; 17 if ($configs === null) { 18 $configs = []; 19 Event::createAndTrigger('CONFMANAGER_CONFIGFILES_REGISTER', $configs, null, false); 20 usort($configs, [$this, '_sortByConfigName']); 21 } 22 return $configs; 23 } 24 25 /** 26 * Get a specific config manager 27 * 28 * @param string $id Config ID 29 * @return ConfigManagerConfigType|false 30 */ 31 public function getConfigById($id) { 32 foreach ($this->getConfigFiles() as $config) { 33 if ($this->getConfigId($config) === $id) { 34 return $config; 35 } 36 } 37 return false; 38 } 39 40 /** 41 * Get the id of a config manager 42 * 43 * @param ConfigManagerConfigType $config 44 * @return string 45 */ 46 public function getConfigId(ConfigManagerConfigType $config) { 47 $hash = ''; 48 $paths = $config->getPaths(); 49 if (count($paths) == 0) return ''; 50 51 $hash .= realpath($paths[0]); 52 return md5($hash); 53 } 54 55 /** 56 * @param string[] $files 57 * @return bool 58 */ 59 public function areWriteable($files) { 60 foreach ($files as $file) { 61 if (!is_writable($file)) { 62 return false; 63 } 64 } 65 return true; 66 } 67 68 /** 69 * @param string $file filename path to file 70 * @param string $content 71 */ 72 public function saveFile($file, $content) { 73 $success = io_saveFile($file, $content); 74 if ($success !== false ) { 75 msg($this->getLang('changes applied'), 1); 76 } elseif (!is_writable($file)) { 77 msg($this->getLang('error:saving failed not writable'), -1); 78 } else { 79 msg($this->getLang('error:saving failed'), -1); 80 } 81 82 } 83 84 /** 85 * Get header for config files created by confmanager 86 * 87 * @return string 88 */ 89 public function getCoreConfigHeader() { 90 return "# This config was generated by the confmanager plugin.\n" 91 . "# Changes made to this file may be overwritten by the plugin.\n" 92 . "# Please use the confmanager in the Dokuwiki admin interface.\n"; 93 } 94 95 /** 96 * @param string $k1 97 * @param string $k2 98 * @return int 99 */ 100 public function _sortConf( $k1 , $k2 ) { 101 return strlen( $k2 ) - strlen( $k1 ); 102 } 103 104 /** 105 * @param string $k1 106 * @param string $k2 107 * @return int 108 */ 109 public function _sortHuman( $k1 , $k2 ) { 110 $k1 = strtolower($k1); 111 $k2 = strtolower($k2); 112 return strnatcmp($k1,$k2); 113 } 114 115 /** 116 * Compare config managers by name 117 * 118 * @param ConfigManagerConfigType $left 119 * @param ConfigManagerConfigType $right 120 * @return int 121 */ 122 public function _sortByConfigName(ConfigManagerConfigType $left, ConfigManagerConfigType $right) { 123 return strnatcasecmp($left->getName(), $right->getName()); 124 } 125 126 public function tplSaveButton() { 127 static $called = false; 128 if ($called) { 129 return; 130 } 131 include DOKU_PLUGIN . 'confmanager/tpl/formControls.php'; 132 $called = true; 133 } 134 135 /** 136 * Prepare entity for saving 137 * 138 * @param string $str 139 * @return string 140 */ 141 public function prepareEntity($str) { 142 $str = trim($str); 143 $str = str_replace("\n", '', $str); 144 $str = str_replace("\r", '', $str); 145 $str = str_replace('#', '\\#', $str); 146 return $str; 147 } 148} 149 150