1<?php 2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project 3// 4// All Rights Reserved. See copyright.txt for details and a complete list of authors. 5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details. 6// $Id$ 7 8class WikiPlugin_Negotiator_Wiki_Alias 9{ 10 public static function info($name) 11 { 12 global $prefs; 13 14 if (empty($name)) { 15 return false; 16 } 17 18 $name = TikiLib::strtolower($name); 19 20 $prefName = "pluginalias_" . $name; 21 22 if (! isset($prefs[$prefName])) { 23 return false; 24 } 25 26 return unserialize($prefs[$prefName]); 27 } 28 29 public static function getList() 30 { 31 global $prefs; 32 if (isset($prefs['pluginaliaslist'])) { 33 $alias = @unserialize($prefs['pluginaliaslist']); 34 $alias = array_filter($alias); 35 return $alias; 36 } 37 return []; 38 } 39 40 public static function store($name, $data) 41 { 42 /* 43 Input data structure: 44 45 implementation: other plugin_name 46 description: 47 ** Equivalent of plugin info function here ** 48 body: 49 input: use|ignore 50 default: body content to use 51 params: 52 token_name: 53 input: token_name, default uses same name above 54 default: value to use if missing 55 encoding: none|html|url - default to none 56 params: 57 ; Use input parameter directly 58 token_name: default value 59 60 ; Custom input parameter replacement 61 token_name: 62 pattern: body content to use 63 params: 64 token_name: 65 input: token_name, default uses same name above 66 default: value to use if missing 67 encoding: none|html|url - default to none 68 */ 69 if (empty($name)) { 70 return; 71 } 72 73 $name = TikiLib::strtolower($name); 74 $data['plugin_name'] = $name; 75 76 $prefName = "pluginalias_$name"; 77 $tikilib = TikiLib::lib('tiki'); 78 $tikilib->set_preference($prefName, serialize($data)); 79 80 global $prefs; 81 $list = []; 82 if (isset($prefs['pluginaliaslist'])) { 83 $list = unserialize($prefs['pluginaliaslist']); 84 } 85 86 if (! in_array($name, $list)) { 87 $list[] = $name; 88 $tikilib->set_preference('pluginaliaslist', serialize($list)); 89 } 90 91 foreach (glob('temp/cache/wikiplugin_*') as $file) { 92 unlink($file); 93 } 94 95 $cachelib = TikiLib::lib('cache'); 96 $cachelib->invalidate('plugindesc'); 97 } 98 99 100 public static function delete($name) 101 { 102 $tikilib = TikiLib::lib('tiki'); 103 $prefName = "pluginalias_" . $name; 104 105 // Remove from list 106 $list = $tikilib->get_preference('pluginaliaslist', [], true); 107 $list = array_diff($list, [ $name ]); 108 $tikilib->set_preference('pluginaliaslist', serialize($list)); 109 110 // Remove the definition 111 $tikilib->delete_preference($prefName); 112 113 // Clear cache 114 $cachelib = TikiLib::lib('cache'); 115 $cachelib->invalidate('plugindesc'); 116 foreach (glob('temp/cache/wikiplugin_*') as $file) { 117 unlink($file); 118 } 119 } 120 121 public static function getDetails($details = []) 122 { 123 if (self::findImplementation($details['name'], $details['body'], $details['args'])) { 124 return $details; 125 } else { 126 return false; 127 } 128 } 129 130 public static function findImplementation(& $implementation, & $data, & $args) 131 { 132 if ($info = self::info($implementation)) { 133 $implementation = $info['implementation']; 134 135 // Do the body conversion 136 if (isset($info['body'])) { 137 if (( isset($info['body']['input']) && $info['body']['input'] == 'ignore' ) 138 || empty($data) ) { 139 $data = isset($info['body']['default']) ? $info['body']['default'] : ''; 140 } 141 142 if (isset($info['body']['params'])) { 143 $data = self::replaceArgs($data, $info['body']['params'], $args); 144 } 145 } else { 146 $data = ''; 147 } 148 149 // Do parameter conversion 150 $params = []; 151 if (isset($info['params'])) { 152 foreach ($info['params'] as $key => $value) { 153 if (is_array($value) && isset($value['pattern']) && isset($value['params'])) { 154 $params[$key] = self::replaceArgs($value['pattern'], $value['params'], $args); 155 } else { 156 // Handle simple values 157 if (isset($args[$key])) { 158 $params[$key] = $args[$key]; 159 } else { 160 $params[$key] = $value; 161 } 162 } 163 } 164 } 165 166 $args = $params; 167 168 // Attempt to find recursively 169 self::findImplementation($implementation, $data, $args); 170 171 return true; 172 } 173 174 return false; 175 } 176 177 static function replaceArgs($content, $rules, $args) 178 { 179 $patterns = []; 180 $replacements = []; 181 182 foreach ($rules as $token => $info) { 183 $patterns[] = "%$token%"; 184 if (isset($info['input']) && ! empty($info['input'])) { 185 $token = $info['input']; 186 } 187 188 if (isset($args[$token])) { 189 $value = $args[$token]; 190 } else { 191 $value = isset($info['default']) ? $info['default'] : ''; 192 } 193 194 switch (isset($info['encoding']) ? $info['encoding'] : 'none') { 195 case 'html': 196 $replacements[] = htmlentities($value, ENT_QUOTES, 'UTF-8'); 197 break; 198 case 'url': 199 $replacements[] = rawurlencode($value); 200 break; 201 default: 202 $replacements[] = $value; 203 } 204 } 205 206 return str_replace($patterns, $replacements, $content); 207 } 208} 209