PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/application/component/helper.php

https://gitlab.com/endomorphosis/greenrenaissancejoomla
PHP | 210 lines | 114 code | 32 blank | 64 comment | 13 complexity | 31bc0df252319b8659ae83d10b80530d MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: helper.php 10111 2008-03-07 11:56:46Z eddieajau $
  4. * @package Joomla.Framework
  5. * @subpackage Application
  6. * @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
  7. * @license GNU/GPL, see LICENSE.php
  8. * Joomla! is free software. This version may have been modified pursuant
  9. * to the GNU General Public License, and as distributed it includes or
  10. * is derivative of works licensed under the GNU General Public License or
  11. * other free or open source software licenses.
  12. * See COPYRIGHT.php for copyright notices and details.
  13. */
  14. // Check to ensure this file is within the rest of the framework
  15. defined('JPATH_BASE') or die();
  16. /**
  17. * Component helper class
  18. *
  19. * @static
  20. * @package Joomla.Framework
  21. * @subpackage Application
  22. * @since 1.5
  23. */
  24. class JComponentHelper
  25. {
  26. /**
  27. * Get the component info
  28. *
  29. * @access public
  30. * @param string $name The component name
  31. * @param boolean $string If set and a component does not exist, the enabled attribue will be set to false
  32. * @return object A JComponent object
  33. */
  34. function &getComponent( $name, $strict = false )
  35. {
  36. $result = null;
  37. $components = JComponentHelper::_load();
  38. if (isset( $components[$name] ))
  39. {
  40. $result = &$components[$name];
  41. }
  42. else
  43. {
  44. $result = new stdClass();
  45. $result->enabled = $strict ? false : true;
  46. $result->params = null;
  47. }
  48. return $result;
  49. }
  50. /**
  51. * Checks if the component is enabled
  52. *
  53. * @access public
  54. * @param string $component The component name
  55. * @param boolean $string If set and a component does not exist, false will be returned
  56. * @return boolean
  57. */
  58. function isEnabled( $component, $strict = false )
  59. {
  60. global $mainframe;
  61. $result = &JComponentHelper::getComponent( $component, $strict );
  62. return ($result->enabled | $mainframe->isAdmin());
  63. }
  64. /**
  65. * Gets the parameter object for the component
  66. *
  67. * @access public
  68. * @param string $name The component name
  69. * @return object A JParameter object
  70. */
  71. function &getParams( $name )
  72. {
  73. static $instances;
  74. if (!isset( $instances[$name] ))
  75. {
  76. $component = &JComponentHelper::getComponent( $name );
  77. $instances[$name] = new JParameter($component->params);
  78. }
  79. return $instances[$name];
  80. }
  81. function renderComponent($name = null, $params = array())
  82. {
  83. global $mainframe, $option;
  84. if(empty($name)) {
  85. // Throw 404 if no component
  86. JError::raiseError(404, JText::_("Component Not Found"));
  87. return;
  88. }
  89. $scope = $mainframe->scope; //record the scope
  90. $mainframe->scope = $name; //set scope to component name
  91. $task = JRequest::getString( 'task' );
  92. // Build the component path
  93. $name = preg_replace('/[^A-Z0-9_\.-]/i', '', $name);
  94. $file = substr( $name, 4 );
  95. // Define component path
  96. define( 'JPATH_COMPONENT', JPATH_BASE.DS.'components'.DS.$name);
  97. define( 'JPATH_COMPONENT_SITE', JPATH_SITE.DS.'components'.DS.$name);
  98. define( 'JPATH_COMPONENT_ADMINISTRATOR', JPATH_ADMINISTRATOR.DS.'components'.DS.$name);
  99. // get component path
  100. if ( $mainframe->isAdmin() && file_exists(JPATH_COMPONENT.DS.'admin.'.$file.'.php') ) {
  101. $path = JPATH_COMPONENT.DS.'admin.'.$file.'.php';
  102. } else {
  103. $path = JPATH_COMPONENT.DS.$file.'.php';
  104. }
  105. // If component disabled throw error
  106. if (!JComponentHelper::isEnabled( $name ) || !file_exists($path)) {
  107. JError::raiseError( 404, JText::_( 'Component Not Found' ) );
  108. }
  109. // Handle legacy globals if enabled
  110. if ($mainframe->getCfg('legacy'))
  111. {
  112. // Include legacy globals
  113. global $my, $database, $id, $acl, $task;
  114. // For backwards compatibility extract the config vars as globals
  115. $registry =& JFactory::getConfig();
  116. foreach (get_object_vars($registry->toObject()) as $k => $v)
  117. {
  118. $varname = 'mosConfig_'.$k;
  119. $$varname = $v;
  120. }
  121. $contentConfig = &JComponentHelper::getParams( 'com_content' );
  122. foreach (get_object_vars($contentConfig->toObject()) as $k => $v)
  123. {
  124. $varname = 'mosConfig_'.$k;
  125. $$varname = $v;
  126. }
  127. $usersConfig = &JComponentHelper::getParams( 'com_users' );
  128. foreach (get_object_vars($usersConfig->toObject()) as $k => $v)
  129. {
  130. $varname = 'mosConfig_'.$k;
  131. $$varname = $v;
  132. }
  133. }
  134. // Load common language files
  135. $lang =& JFactory::getLanguage();
  136. $lang->load($name);
  137. // Handle template preview outlining
  138. $contents = null;
  139. // Execute the component
  140. ob_start();
  141. require_once $path;
  142. $contents = ob_get_contents();
  143. ob_end_clean();
  144. // Build the component toolbar
  145. jimport( 'joomla.application.helper' );
  146. if (($path = JApplicationHelper::getPath( 'toolbar' )) && $mainframe->isAdmin()) {
  147. // Get the task again, in case it has changed
  148. $task = JRequest::getString( 'task' );
  149. // Make the toolbar
  150. include_once( $path );
  151. }
  152. $mainframe->scope = $scope; //revert the scope
  153. return $contents;
  154. }
  155. /**
  156. * Load components
  157. *
  158. * @access private
  159. * @return array
  160. */
  161. function _load()
  162. {
  163. static $components;
  164. if (isset($components)) {
  165. return $components;
  166. }
  167. $db = &JFactory::getDBO();
  168. $query = 'SELECT *' .
  169. ' FROM #__components' .
  170. ' WHERE parent = 0';
  171. $db->setQuery( $query );
  172. if (!($components = $db->loadObjectList( 'option' ))) {
  173. JError::raiseWarning( 'SOME_ERROR_CODE', "Error loading Components: " . $db->getErrorMsg());
  174. return false;
  175. }
  176. return $components;
  177. }
  178. }