/lib/core/WikiPlugin/Negotiator/Wiki/Alias.php

https://gitlab.com/ElvisAns/tiki · PHP · 221 lines · 146 code · 35 blank · 40 comment · 28 complexity · db1284845aa880c9429850c50235e53e MD5 · raw file

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