PageRenderTime 92ms CodeModel.GetById 25ms RepoModel.GetById 5ms app.codeStats 0ms

/master/administrator/components/com_multisites/classes/dbsharing.php

https://github.com/sherdog/GitWitty
PHP | 319 lines | 201 code | 31 blank | 87 comment | 46 complexity | 721bbc5446d3f5b8691263a3dec63368 MD5 | raw file
  1. <?php
  2. /**
  3. * @file dbsharing.php
  4. * @version 1.2.33
  5. * @author Edwin CHERONT (e.cheront@jms2win.com)
  6. * Edwin2Win sprlu (www.jms2win.com)
  7. * @copyright Joomla Multi Sites
  8. * Single Joomla! 1.5.x installation using multiple configuration (One for each 'slave' sites).
  9. * (C) 2008-2010 Edwin2Win sprlu - all right reserved.
  10. * @license This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or (at your option) any later version.
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. * A full text version of the GNU GPL version 2 can be found in the LICENSE.php file.
  22. * @par History:
  23. * - V1.2.0 28-APR-2009: Initial version
  24. * - V1.2.0 RC3 05-JUL-2009: Fix warning concerning deprecated syntax in PHP 5.x
  25. * - V1.2.0 RC4 09-JUL-2009: Add check that dbsharing.xml contain effectivelly <tables> tags to avoid
  26. * searching children when the tag is not present
  27. * - V1.2.4 25-AUG-2009: Add Joomla 1.6 compatibility
  28. * - V1.2.30 01-JUN-2010: Add the possibility to include contributors XML DB Sharing description
  29. * into the loaded XML. Call the multisites plugin onDBSharingLoaded() function
  30. * - V1.2.33 08-JUL-2010: Add the possibility to exclude tables. Now read the tag <tableexcluded ...>
  31. */
  32. // Check to ensure this file is included in Joomla!
  33. defined('_JEXEC') or die( 'Restricted access' );
  34. jimport( 'joomla.filesystem.path');
  35. // ===========================================================
  36. // Jms2WinTemplate class
  37. // ===========================================================
  38. /**
  39. * @brief This is a Template record.
  40. *
  41. * Generally used in collection, this class contain all the information of a Template.\n
  42. * A site is defined by:
  43. */
  44. class Jms2WinDBSharing
  45. {
  46. var $success = false; /**< Flag that is set to true when some information is loaded */
  47. var $_xml = null;
  48. //------------------- getInstance ---------------
  49. function &getInstance()
  50. {
  51. static $instance;
  52. if (!is_object($instance))
  53. {
  54. $instance = new Jms2WinDBSharing();
  55. }
  56. return $instance;
  57. }
  58. //------------------- Constructor ---------------
  59. function Jms2WinDBSharing()
  60. {
  61. $this->success = false;
  62. }
  63. //------------ getConfigFilename ---------------
  64. function getConfigFilename()
  65. {
  66. if ( version_compare( JVERSION, '1.6') >= 0) {
  67. $filename = dirname( dirname( __FILE__))
  68. .DS. 'patches'
  69. .DS. 'sharing'
  70. .DS. 'dbsharing_16.xml'
  71. ;
  72. }
  73. else {
  74. $filename = dirname( dirname( __FILE__))
  75. .DS. 'patches'
  76. .DS. 'sharing'
  77. .DS. 'dbsharing.xml'
  78. ;
  79. }
  80. return $filename;
  81. }
  82. //------------------- Constructor ---------------
  83. function getXML()
  84. {
  85. return $this->_xml;
  86. }
  87. //------------------- getSharedTables ---------------
  88. /**
  89. * @brief Load the DBSharing configuration.
  90. * This parse the XML document to extract all the <table> records that match the dbsharing values.
  91. * This return Table "like" conditions that match to the selected DBSharing entries (conditions).
  92. */
  93. function getSharedTables( $dbsharing)
  94. {
  95. $results = array();
  96. $results['table'] = array();
  97. $results['tableexcluded'] = array();
  98. $xml = & $this->_xml;
  99. $params =& $xml->getElementByPath('params');
  100. foreach( $dbsharing as $key => $value) {
  101. // Search for the parameters with the name = '$key'
  102. foreach( $params->children() as $param) {
  103. // If the parameter is found
  104. if ( $param->attributes( 'name') == $key) {
  105. $type = $param->attributes( 'type');
  106. if ( $type == 'checkbox') {
  107. $tables = $param->getElementByPath('tables');
  108. if ( !empty( $tables)) {
  109. foreach( $tables->children() as $table) {
  110. $name = $table->attributes( 'name');
  111. if ( $table->name() == 'tableexcluded') { $results['tableexcluded'][$name] = $name; }
  112. else { $results['table'][$name] = $name; }
  113. }
  114. }
  115. }
  116. // Radio, list, ... (every type with an option as children).
  117. else {
  118. // Search for the option that correspond to the value
  119. foreach( $param->children() as $option) {
  120. if ( $option->attributes( 'value') == $value) {
  121. $tables = $option->getElementByPath('tables');
  122. if ( !empty( $tables)) {
  123. foreach( $tables->children() as $table) {
  124. $name = $table->attributes( 'name');
  125. if ( $table->name() == 'tableexcluded') { $results['tableexcluded'][$name] = $name; }
  126. else { $results['table'][$name] = $name; }
  127. }
  128. }
  129. break;
  130. }
  131. } // Next <option value='...'
  132. }
  133. break;
  134. }
  135. } // Next <param name='...'
  136. }
  137. return $results;
  138. }
  139. //------------------- _getTables ---------------
  140. /**
  141. * @brief return an array of <table> node
  142. */
  143. function _getTables( &$children, &$tables)
  144. {
  145. if ( empty( $children)) {
  146. return;
  147. }
  148. foreach( $children as $child) {
  149. if ( $child->name() == 'table') {
  150. $tables[] = $child;
  151. }
  152. else if ( $child->name() == 'tableexcluded') {
  153. $child->excluded = true;
  154. $tables[] = $child;
  155. }
  156. $smallChildren = $child->children();
  157. Jms2WinDBSharing::_getTables( $smallChildren, $tables);
  158. }
  159. }
  160. //------------------- getTables ---------------
  161. /**
  162. * @brief return an array of <table> node
  163. */
  164. function &getTables( $xmlnode)
  165. {
  166. $tables = array();
  167. Jms2WinDBSharing::_getTables( $xmlnode, $tables);
  168. return $tables;
  169. }
  170. //------------------- cleanupOnCondition ---------------
  171. /**
  172. * @brief Remove all nodes having a condition that where the file or folder does not exists
  173. */
  174. function cleanupOnCondition( &$node)
  175. {
  176. for ($i=count($node->_children)-1;$i>=0;$i--) {
  177. $child = & $node->_children[$i];
  178. $condition = $child->attributes( 'condition');
  179. if ( !empty($condition)) {
  180. $path = str_replace( '{root}', JPATH_ROOT, $condition);
  181. if ( !file_exists( JPath::clean( $path))) {
  182. $node->removeChild( $child);
  183. }
  184. }
  185. else {
  186. $node->_children[$i] = $this->cleanupOnCondition( $child);
  187. }
  188. }
  189. return( $node);
  190. }
  191. //------------------- _indexExtensions ---------------
  192. /**
  193. * @brief Extract the extension name from the condition and create an index based on the extension name
  194. * to speed up retreiving the <tables> defintiion associated to the extension.
  195. */
  196. function _indexExtensions( &$node)
  197. {
  198. for ($i=count($node->_children)-1;$i>=0;$i--) {
  199. $child = & $node->_children[$i];
  200. $condition = $child->attributes( 'condition');
  201. if ( !empty($condition)) {
  202. $parts = explode( '/', $condition);
  203. $n = count( $parts);
  204. if ( $n > 2) {
  205. $dir = $parts[ $n - 2];
  206. if ( $dir == 'components'
  207. || $dir == 'modules'
  208. ) {
  209. $ext_name = $parts[ $n - 1];
  210. }
  211. else if ( $parts[ $n - 3] == 'plugins') {
  212. $ext_name = $dir .DS. $parts[ $n - 1];
  213. }
  214. }
  215. if ( !isset( $this->_extensions[$ext_name])) {
  216. $this->_extensions[$ext_name] = array();
  217. }
  218. $this->_extensions[$ext_name][] = & $child;
  219. }
  220. else {
  221. $this->_indexExtensions( $child);
  222. }
  223. }
  224. }
  225. //------------------- indexExtensions ---------------
  226. /**
  227. * @brief Create an index on all the extension name having a condition that describe the sharing rules.
  228. */
  229. function indexExtensions( &$node)
  230. {
  231. $this->_extensions = array();
  232. $this->_indexExtensions( $this->_xml);
  233. }
  234. //------------------- getShareInfos ---------------
  235. /**
  236. * @brief Return an array with the list of sharing information available for the extension name
  237. */
  238. function &getShareInfos( $ext_name)
  239. {
  240. if ( !$this->isLoaded()) {
  241. $this->load();
  242. }
  243. if ( isset( $this->_extensions[ $ext_name])) {
  244. return $this->_extensions[ $ext_name];
  245. }
  246. $none = array();
  247. return $none;
  248. }
  249. //------------------- isLoaded ---------------
  250. function isLoaded()
  251. {
  252. if ( isset( $this->_xml)) {
  253. return true;
  254. }
  255. return false;
  256. }
  257. //------------------- load ---------------
  258. /**
  259. * @brief Load the DBSharing configuration.
  260. */
  261. function load()
  262. {
  263. // If there is already an XML file loaded
  264. if ( isset( $this->_xml)) {
  265. $this->success = true;
  266. return $this->success;
  267. }
  268. $this->success = false;
  269. $this->_xml = null;
  270. $xmlpath = $this->getConfigFilename();
  271. // load the configuration
  272. if ( file_exists($xmlpath))
  273. {
  274. $xml =& JFactory::getXMLParser('Simple');
  275. if ($xml->loadFile($xmlpath)) {
  276. // When the XML file is loaded, give the opportunity to a plugin add description inside.
  277. JPluginHelper::importPlugin('multisites');
  278. $mainframe = &JFactory::getApplication();
  279. $results = $mainframe->triggerEvent('onDBSharingLoaded', array ( & $xml));
  280. $this->_xml =& $xml->document;
  281. $this->cleanupOnCondition( $this->_xml);
  282. $this->indexExtensions( $this->_xml);
  283. $this->success = true;
  284. }
  285. }
  286. return $this->success;
  287. }
  288. }