PageRenderTime 57ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/inc/bx/collectionconfig.php

https://github.com/chregu/fluxcms
PHP | 316 lines | 260 code | 45 blank | 11 comment | 59 complexity | c81288d92a06db88728438b829404410 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, Apache-2.0, LGPL-2.1
  1. <?php
  2. class bx_collectionconfig {
  3. protected $configxml = null;
  4. protected $mode;
  5. protected $parameters = array();
  6. protected $variables = array();
  7. public function __construct($uri, $mode) {
  8. $this->uri = $uri;
  9. $this->mode = $mode;
  10. $this->loadConfigXML($this->uri);
  11. }
  12. private function loadConfigXML($url) {
  13. $this->configxml = new DomDocument();
  14. $this->configxml->load("bxconfig://".$url);
  15. $xp = new domxpath($this->configxml);
  16. $xp->registerNamespace("xi","http://www.w3.org/2001/XInclude");
  17. $xp->registerNamespace("bxcms","http://bitflux.org/config");
  18. $res = $xp->query("//xi:include");
  19. foreach( $res as $node) {
  20. $href = $node->getAttribute("href");
  21. if (substr($href,0,3) == "BX_") {
  22. if (BX_OS_WIN) {
  23. $href = preg_replace("#(BX_[A-Z_]+)://#e","'file:///'.constant('$1')",$href);
  24. } else {
  25. $href = preg_replace("#(BX_[A-Z_]+)://#e","'file://'.constant('$1')",$href);
  26. }
  27. $node->setAttribute('href',$href);
  28. }
  29. }
  30. $this->configxml->xinclude();
  31. //parse variables
  32. $res = $xp->query("/bxcms:bxcms/bxcms:variable");
  33. foreach( $res as $node) {
  34. $name = $node->getAttribute("name");
  35. if (!isset($this->variables[$name])) {
  36. $this->variables[$name] = $node->getAttribute("value");
  37. }
  38. }
  39. if (count($this->variables) == 0) {
  40. $this->variables = null;
  41. }
  42. }
  43. public function getPlugins($name, $ext, $first = false) {
  44. $ext = strtolower($ext);
  45. $plugins = array();
  46. if ($this->mode == "admin") {
  47. $xpathAddition = "";//"[@admin='true']";
  48. } else {
  49. $xpathAddition = "";
  50. }
  51. $hit = false;
  52. foreach ($this->getXPathNodes("/bxcms:bxcms/bxcms:plugins/bxcms:extension") as $node) {
  53. if ($ext == $node->getAttribute("type")) {
  54. $res = $this->getXPathNodes("bxcms:file", $node->parentNode);
  55. if($res->length > 0) {
  56. // filter matches only specific file names
  57. foreach($res as $fileNode) {
  58. if ( $name == $fileNode->getAttribute('name') ||
  59. ($fileNode->getAttribute("preg") && preg_match($fileNode->getAttribute("preg"),$name))) {
  60. $res = $this->getXPathNodes("bxcms:plugin".$xpathAddition,$node->parentNode);
  61. foreach ($res as $p) {
  62. $type = $p->getAttribute("type");
  63. $output = $this->getPluginInstance($type, $this->mode);
  64. $params = $this->getAllNodeParameters($p);
  65. $output->setParameterAll($this->uri, $params);
  66. if ($first) {
  67. return $output;
  68. }
  69. $plugins[$type] = $output;
  70. $hit = true;
  71. }
  72. // parse collection parameters
  73. $this->parameters = $this->getAllNodeParameters($node->parentNode);
  74. }
  75. }
  76. } else {
  77. $res = $this->getXPathNodes("bxcms:plugin".$xpathAddition,$node->parentNode);
  78. foreach ($res as $p) {
  79. $type = $p->getAttribute("type");
  80. $output = $this->getPluginInstance($type, $this->mode);
  81. $params = $this->getAllNodeParameters($p);
  82. $output->setParameterAll($this->uri, $params);
  83. if ($first) {
  84. return $output;
  85. }
  86. $plugins[$type] = $output;
  87. $hit = true;
  88. }
  89. // parse collection parameters
  90. $this->parameters = $this->getAllNodeParameters($node->parentNode);
  91. }
  92. if ($hit) {break;}
  93. }
  94. }
  95. //if no match, get default plugin (the one with no bxcms:extensions ;) )
  96. if (!($plugins)) {
  97. foreach ($this->getXPathNodes("/bxcms:bxcms/bxcms:plugins[not(bxcms:extension)]/bxcms:plugin".$xpathAddition) as $p) {
  98. $type = $p->getAttribute("type");
  99. $output = $this->getPluginInstance($type,$this->mode);
  100. $params = $this->getAllNodeParameters($p);
  101. $output->setParameterAll($this->uri,$params);
  102. if ($first) {
  103. return $output;
  104. }
  105. $plugins[$type] = $output;
  106. $node = $p;
  107. }
  108. $this->parameters = $this->getAllNodeParameters($node->parentNode);
  109. }
  110. //return collection plugin if extension is 'collection' and default didn't match
  111. if($ext === 'collection' && empty($plugins)) {
  112. return $this->getPluginInstance('collection', $this->mode);
  113. }
  114. if ($first ) {
  115. return NULL;
  116. }
  117. return $plugins;
  118. }
  119. public function getFirstPlugin ($name, $ext) {
  120. print "deprecated!\n";
  121. return $this->getPlugins($name, $ext, true);
  122. }
  123. public function getAdminPlugin($name, $ext) {
  124. if (($name == "" && $ext == 'configxml') || ($name == ".configxml" && $ext == 'children')) {
  125. $p = $this->getPluginInstance("admin_configxml", $this->mode);
  126. return $p;
  127. }
  128. foreach ($this->getXPathNodes("/bxcms:bxcms/bxcms:plugins/bxcms:plugin") as $node) {
  129. $type = $node->getAttribute("type");
  130. $p = $this->getPluginInstance($type, $this->mode);
  131. $params = $this->getAllNodeParameters($node);
  132. $p->setParameterAll($this->uri, $params);
  133. if ($p->adminResourceExists($this->uri, $name, $ext)) {
  134. return $p;
  135. }
  136. }
  137. // if we're here, we need a mock resource...
  138. foreach ($this->getXPathNodes("/bxcms:bxcms/bxcms:plugins/bxcms:plugin") as $node) {
  139. $type = $node->getAttribute("type");
  140. $p = $this->getPluginInstance($type, $this->mode);
  141. $params = $this->getAllNodeParameters($node);
  142. $p->setParameterAll($this->uri, $params);
  143. if ($p->adminResourceExists($this->uri, $name, $ext,true)) {
  144. return $p;
  145. }
  146. }
  147. return null;
  148. }
  149. public function getAdminMasterPlugin() {
  150. $plugin = NULL;
  151. $res = $this->getXPathNodes("/bxcms:bxcms/bxcms:plugins/bxcms:plugin[@adminmaster='true']");
  152. if($res->length > 0) {
  153. $node = $res->item(0);
  154. $type = $node->getAttribute("type");
  155. $plugin = $this->getPluginInstance($type, $this->mode);
  156. $params = $this->getAllNodeParameters($node);
  157. $plugin->setParameterAll($this->uri, $params);
  158. }
  159. return $plugin;
  160. }
  161. public function getChildrenPlugins() {
  162. $plugins = array();
  163. foreach ($this->getXPathNodes("/bxcms:bxcms/bxcms:plugins[not(@inGetChildren) or @inGetChildren != 'false']/bxcms:plugin") as $node) {
  164. $type = $node->getAttribute("type");
  165. try {
  166. $output = $this->getPluginInstance($type, $this->mode);
  167. $params = $this->getAllNodeParameters($node);
  168. $output->setParameterAll($this->uri, $params);
  169. $plugins[$type] = $output;
  170. } catch (Exception $e) {
  171. bx_log::log("plugin $type could not be loaded.");
  172. }
  173. }
  174. return $plugins;
  175. }
  176. public function getFilters($name, $ext) {
  177. $filters = new bx_filtermanager();
  178. foreach ($this->getXPathNodes("/bxcms:bxcms/bxcms:filters/bxcms:extension") as $node) {
  179. if($ext == $node->getAttribute('type')) {
  180. $res = $this->getXPathNodes("bxcms:file", $node->parentNode);
  181. if($res->length > 0) {
  182. // filter matches only specific file names
  183. foreach($res as $fileNode) {
  184. if($name == $fileNode->getAttribute('name') ||
  185. ($fileNode->getAttribute("preg") && preg_match($fileNode->getAttribute("preg"),$name))) {
  186. $filterNodeList = $this->getXPathNodes('bxcms:filter', $node->parentNode);
  187. foreach($filterNodeList as $filterNode) {
  188. $filter = $this->getFilterInstance($filterNode->getAttribute('type'), $this->mode);
  189. $params = $this->getAllNodeParameters($filterNode);
  190. $filter->setParameterAll($this->uri, $params);
  191. $filters->addFilter($filter,$name.$ext);
  192. }
  193. }
  194. }
  195. } else {
  196. // filter matches all filenames for an extension
  197. $filterNodeList = $this->getXPathNodes('bxcms:filter', $node->parentNode);
  198. foreach($filterNodeList as $filterNode) {
  199. $filter = $this->getFilterInstance($filterNode->getAttribute('type'), $this->mode);
  200. $params = $this->getAllNodeParameters($filterNode);
  201. $filter->setParameterAll($this->uri, $params);
  202. $filters->addFilter($filter,$name.$ext);
  203. }
  204. }
  205. }
  206. }
  207. return $filters;
  208. }
  209. /** helper nodes **/
  210. protected function getXPathNodes($xpath, $ctxt = NULL) {
  211. $xp = new Domxpath($this->configxml);
  212. $xp->registerNamespace("bxcms","http://bitflux.org/config");
  213. if ($ctxt) {
  214. return $xp->query($xpath,$ctxt);
  215. } else {
  216. return $xp->query($xpath);
  217. }
  218. }
  219. protected function getPluginInstance($type, $mode) {
  220. $pluginname = "bx_plugins_".$type;
  221. if (!@class_exists($pluginname)) {
  222. throw new Exception("Plugin \"$type\" ($pluginname) could not be found");
  223. }
  224. $output = call_user_func(array($pluginname,'getInstance'),$mode);
  225. $output->name = $type;
  226. return $output;
  227. }
  228. protected function getFilterInstance($type, $mode) {
  229. $pluginname = "bx_filters_".$type;
  230. $output = call_user_func(array($pluginname,'getInstance'),$mode);
  231. $output->name = $type;
  232. return $output;
  233. }
  234. protected function getAllNodeParameters($ctxNode) {
  235. $params = array();
  236. $parameterNodeList = $this->getXPathNodes('bxcms:parameter', $ctxNode);
  237. foreach($parameterNodeList as $parameterNode) {
  238. $pType = $parameterNode->getAttribute('type');
  239. $pName = $parameterNode->getAttribute('name');
  240. $pValue = $parameterNode->getAttribute('value');
  241. $pKey = $parameterNode->getAttribute('key');
  242. $pType == '' ? $pType = BX_PARAMETER_TYPE_DEFAULT : $pType;
  243. //replace variables
  244. if ($this->variables && $pValue && $pValue{0} == '$') {
  245. $vKey = substr($pValue,1);
  246. if (isset($this->variables[$vKey])) {
  247. $pValue = $this->variables[$vKey];
  248. }
  249. }
  250. if(!empty($pName)) {
  251. if ($pPrefix = $parameterNode->getAttribute('valuePrefix')) {
  252. $pValue = constant($pPrefix).$pValue;
  253. }
  254. if ($pKey) {
  255. $params[$pType][$pName][$pKey] = $pValue;
  256. } else {
  257. $params[$pType][$pName] = $pValue;
  258. }
  259. }
  260. }
  261. return $params;
  262. }
  263. public function getParameters($type = BX_PARAMETER_TYPE_DEFAULT ) {
  264. if (isset($this->parameters[$type])) {
  265. return $this->parameters[$type];
  266. } else {
  267. return array();
  268. }
  269. }
  270. }
  271. ?>