1 <?php
  2 /**
  3  * @package     Joomla.Legacy
  4  * @subpackage  Library
  5  *
  6  * @copyright   Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
  7  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  8  */
  9 
 10 defined('JPATH_PLATFORM') or die;
 11 
 12 use Joomla\Registry\Registry;
 13 
 14 /**
 15  * Library helper class
 16  *
 17  * @since  3.2
 18  */
 19 class JLibraryHelper
 20 {
 21     /**
 22      * The component list cache
 23      *
 24      * @var    array
 25      * @since  3.2
 26      */
 27     protected static $libraries = array();
 28 
 29     /**
 30      * Get the library information.
 31      *
 32      * @param   string   $element  Element of the library in the extensions table.
 33      * @param   boolean  $strict   If set and the library does not exist, the enabled attribute will be set to false.
 34      *
 35      * @return  stdClass   An object with the library's information.
 36      *
 37      * @since   3.2
 38      */
 39     public static function getLibrary($element, $strict = false)
 40     {
 41         // Is already cached?
 42         if (isset(static::$libraries[$element]) || static::loadLibrary($element))
 43         {
 44             $result = static::$libraries[$element];
 45 
 46             // Convert the params to an object.
 47             if (is_string($result->params))
 48             {
 49                 $result->params = new Registry($result->params);
 50             }
 51         }
 52         else
 53         {
 54             $result = new stdClass;
 55             $result->enabled = $strict ? false : true;
 56             $result->params = new Registry;
 57         }
 58 
 59         return $result;
 60     }
 61 
 62     /**
 63      * Checks if a library is enabled
 64      *
 65      * @param   string  $element  Element of the library in the extensions table.
 66      *
 67      * @return  boolean
 68      *
 69      * @since   3.2
 70      */
 71     public static function isEnabled($element)
 72     {
 73         return static::getLibrary($element, true)->enabled;
 74     }
 75 
 76     /**
 77      * Gets the parameter object for the library
 78      *
 79      * @param   string   $element  Element of the library in the extensions table.
 80      * @param   boolean  $strict   If set and the library does not exist, false will be returned
 81      *
 82      * @return  Registry  A Registry object.
 83      *
 84      * @see     Registry
 85      * @since   3.2
 86      */
 87     public static function getParams($element, $strict = false)
 88     {
 89         return static::getLibrary($element, $strict)->params;
 90     }
 91 
 92     /**
 93      * Save the parameters object for the library
 94      *
 95      * @param   string    $element  Element of the library in the extensions table.
 96      * @param   Registry  $params   Params to save
 97      *
 98      * @return  Registry  A Registry object.
 99      *
100      * @see     Registry
101      * @since   3.2
102      */
103     public static function saveParams($element, $params)
104     {
105         if (static::isEnabled($element))
106         {
107             // Save params in DB
108             $db = JFactory::getDbo();
109             $query = $db->getQuery(true)
110                 ->update($db->quoteName('#__extensions'))
111                 ->set($db->quoteName('params') . ' = ' . $db->quote($params->toString()))
112                 ->where($db->quoteName('type') . ' = ' . $db->quote('library'))
113                 ->where($db->quoteName('element') . ' = ' . $db->quote($element));
114             $db->setQuery($query);
115 
116             $result = $db->execute();
117 
118             // Update params in libraries cache
119             if ($result && isset(static::$libraries[$element]))
120             {
121                 static::$libraries[$element]->params = $params;
122             }
123 
124             return $result;
125         }
126 
127         return false;
128     }
129 
130     /**
131      * Load the installed library into the libraries property.
132      *
133      * @param   string  $element  The element value for the extension
134      *
135      * @return  boolean  True on success
136      *
137      * @since   3.2
138      * @deprecated  4.0  Use JLibraryHelper::loadLibrary() instead
139      */
140     protected static function _load($element)
141     {
142         return static::loadLibrary($element);
143     }
144 
145     /**
146      * Load the installed library into the libraries property.
147      *
148      * @param   string  $element  The element value for the extension
149      *
150      * @return  boolean  True on success
151      *
152      * @since   3.7.0
153      */
154     protected static function loadLibrary($element)
155     {
156         $loader = function($element)
157         {
158             $db = JFactory::getDbo();
159             $query = $db->getQuery(true)
160                 ->select($db->quoteName(array('extension_id', 'element', 'params', 'enabled'), array('id', 'option', null, null)))
161                 ->from($db->quoteName('#__extensions'))
162                 ->where($db->quoteName('type') . ' = ' . $db->quote('library'))
163                 ->where($db->quoteName('element') . ' = ' . $db->quote($element));
164             $db->setQuery($query);
165 
166             return $db->loadObject();
167         };
168 
169         /** @var JCacheControllerCallback $cache */
170         $cache = JFactory::getCache('_system', 'callback');
171 
172         try
173         {
174             static::$libraries[$element] = $cache->get($loader, array($element), __METHOD__ . $element);
175         }
176         catch (JCacheException $e)
177         {
178             static::$libraries[$element] = $loader($element);
179         }
180 
181         if (empty(static::$libraries[$element]))
182         {
183             // Fatal error.
184             $error = JText::_('JLIB_APPLICATION_ERROR_LIBRARY_NOT_FOUND');
185             JLog::add(JText::sprintf('JLIB_APPLICATION_ERROR_LIBRARY_NOT_LOADING', $element, $error), JLog::WARNING, 'jerror');
186 
187             return false;
188         }
189 
190         return true;
191     }
192 }
193