PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/joomla/libraries/regularlabs/helpers/functions.php

https://gitlab.com/ricardosanchez/prueba
PHP | 647 lines | 472 code | 126 blank | 49 comment | 94 complexity | c31ab071109ca2ac657b8f7ca350e27d MD5 | raw file
  1. <?php
  2. /**
  3. * @package Regular Labs Library
  4. * @version 16.11.9943
  5. *
  6. * @author Peter van Westen <info@regularlabs.com>
  7. * @link http://www.regularlabs.com
  8. * @copyright Copyright © 2016 Regular Labs All Rights Reserved
  9. * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
  10. */
  11. defined('_JEXEC') or die;
  12. require_once __DIR__ . '/cache.php';
  13. /**
  14. * Framework Functions
  15. */
  16. class RLFunctions
  17. {
  18. var $_version = '14.9.1';
  19. public static function isFeed()
  20. {
  21. return (
  22. JFactory::getDocument()->getType() == 'feed'
  23. || JFactory::getApplication()->input->getWord('format') == 'feed'
  24. || JFactory::getApplication()->input->getWord('type') == 'rss'
  25. || JFactory::getApplication()->input->getWord('type') == 'atom'
  26. );
  27. }
  28. /*
  29. * @Deprecated
  30. * Use RLFunctions::script($url, $version) instead
  31. * Can be removed before going public!
  32. */
  33. public static function addScriptVersion($url)
  34. {
  35. jimport('joomla.filesystem.file');
  36. if (!empty($_GET['debug']))
  37. {
  38. $url = str_replace('.min.', '.', $url);
  39. }
  40. if (!JFile::exists(JPATH_SITE . $url))
  41. {
  42. return JFactory::getDocument()->addScriptVersion($url);
  43. }
  44. JFactory::getDocument()->addScript($url . '?' . filemtime(JPATH_SITE . $url));
  45. }
  46. /*
  47. * @Deprecated
  48. * Use RLFunctions::stylesheet($url, $version) instead
  49. * Can be removed before going public!
  50. */
  51. public static function addStyleSheetVersion($url)
  52. {
  53. jimport('joomla.filesystem.file');
  54. if (!empty($_GET['debug']))
  55. {
  56. $url = str_replace('.min.', '.', $url);
  57. }
  58. if (!JFile::exists(JPATH_SITE . $url))
  59. {
  60. return JFactory::getDocument()->addStyleSheetVersion($url);
  61. }
  62. JFactory::getDocument()->addStyleSheet($url . '?' . filemtime(JPATH_SITE . $url));
  63. }
  64. public static function script($file, $version = '')
  65. {
  66. if (strpos($file, 'regularlabs/') === 0)
  67. {
  68. $version = '16.11.9943';
  69. }
  70. if (!$file = self::getFileByFolder('js', $file))
  71. {
  72. return;
  73. }
  74. if (!empty($version))
  75. {
  76. $file .= '?v=' . $version;
  77. }
  78. JFactory::getDocument()->addScript($file);
  79. }
  80. public static function stylesheet($file, $version = '')
  81. {
  82. if (strpos($file, 'regularlabs/') === 0)
  83. {
  84. $version = '16.11.9943';
  85. }
  86. if (!$file = self::getFileByFolder('css', $file))
  87. {
  88. return;
  89. }
  90. if (!empty($version))
  91. {
  92. $file .= '?v=' . $version;
  93. }
  94. JFactory::getDocument()->addStylesheet($file);
  95. }
  96. protected static function getFileByFolder($folder, $file)
  97. {
  98. // If http is present in filename
  99. if (strpos($file, 'http') === 0 || strpos($file, '//') === 0)
  100. {
  101. return $file;
  102. }
  103. // Get the template
  104. $template = JFactory::getApplication()->getTemplate();
  105. $files = array();
  106. // Detect debug mode
  107. if (JFactory::getConfig()->get('debug') || JFactory::getApplication()->input->get('debug'))
  108. {
  109. $files[] = str_replace(array('.min.', '-min.'), '.', $file);
  110. }
  111. $files[] = $file;
  112. /*
  113. * Loop on 1 or 2 files and break on first found.
  114. * Add the content of the MD5SUM file located in the same folder to url to ensure cache browser refresh
  115. * This MD5SUM file must represent the signature of the folder content
  116. */
  117. foreach ($files as $check_file)
  118. {
  119. // If the file is in the template folder
  120. if ($file_found = self::fileExists('/templates/' . $template . '/' . $folder . '/' . $check_file))
  121. {
  122. return $file_found;
  123. }
  124. // Try to deal with system files in the media folder
  125. if (strpos($check_file, '/') === false)
  126. {
  127. if ($file_found = self::fileExists('/media/system' . $folder . '/' . $check_file))
  128. {
  129. return $file_found;
  130. }
  131. continue;
  132. }
  133. if ($file_found = self::fileExistsByFolder($check_file, $folder))
  134. {
  135. return $file_found;
  136. }
  137. }
  138. return false;
  139. }
  140. private static function fileExistsByFolder($file, $folder)
  141. {
  142. $template = JFactory::getApplication()->getTemplate();
  143. // If the file is in the template folder
  144. if ($file_found = self::fileExists('/templates/' . $template . '/' . $folder . '/' . $file))
  145. {
  146. return $file_found;
  147. }
  148. // Try to deal with system files in the media folder
  149. if (strpos($file, '/') === false)
  150. {
  151. if ($file_found = self::fileExists('/media/system/' . $folder . '/' . $file))
  152. {
  153. return $file_found;
  154. }
  155. return false;
  156. }
  157. $paths = array();
  158. // If the file contains any /: it can be in a media extension subfolder
  159. // Divide the file extracting the extension as the first part before /
  160. list($extension, $file) = explode('/', $file, 2);
  161. $paths[] = '/media/' . $extension . '/' . $folder;
  162. $paths[] = '/templates/' . $template . '/' . $folder . '/system';
  163. $paths[] = '/media/system/' . $folder;
  164. foreach ($paths as $path)
  165. {
  166. if ($file_found = self::fileExists($path . '/' . $file))
  167. {
  168. return $file_found;
  169. }
  170. }
  171. return false;
  172. }
  173. private static function fileExists($path)
  174. {
  175. if (!file_exists(JPATH_ROOT . $path))
  176. {
  177. return false;
  178. }
  179. return JUri::root(true) . $path;
  180. }
  181. public static function getByUrl($url)
  182. {
  183. $hash = md5('getByUrl_' . $url);
  184. if (RLCache::has($hash))
  185. {
  186. return RLCache::get($hash);
  187. }
  188. // only allow url calls from administrator
  189. if (!JFactory::getApplication()->isAdmin())
  190. {
  191. die;
  192. }
  193. // only allow when logged in
  194. $user = JFactory::getUser();
  195. if (!$user->id)
  196. {
  197. die;
  198. }
  199. if (substr($url, 0, 4) != 'http')
  200. {
  201. $url = 'http://' . $url;
  202. }
  203. // only allow url calls to regularlabs.com domain
  204. if (!(preg_match('#^https?://([^/]+\.)?regularlabs\.com/#', $url)))
  205. {
  206. die;
  207. }
  208. // only allow url calls to certain files
  209. if (
  210. strpos($url, 'download.regularlabs.com/extensions.php') === false
  211. && strpos($url, 'download.regularlabs.com/extensions.json') === false
  212. && strpos($url, 'download.regularlabs.com/extensions.xml') === false
  213. )
  214. {
  215. die;
  216. }
  217. $format = (strpos($url, '.json') !== false || strpos($url, 'format=json') !== false)
  218. ? 'application/json'
  219. : 'text/xml';
  220. header("Pragma: public");
  221. header("Expires: 0");
  222. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  223. header("Cache-Control: public");
  224. header("Content-type: " . $format);
  225. return RLCache::set(
  226. $hash,
  227. self::getContents($url)
  228. );
  229. }
  230. public static function getContents($url, $timeout = 20)
  231. {
  232. $hash = md5('getContents_' . $url);
  233. if (RLCache::has($hash))
  234. {
  235. return RLCache::get($hash);
  236. }
  237. if (JFactory::getApplication()->input->getInt('cache', 0)
  238. && $content = RLCache::read($hash)
  239. )
  240. {
  241. return $content;
  242. }
  243. try
  244. {
  245. $content = JHttpFactory::getHttp()->get($url, null, $timeout)->body;
  246. }
  247. catch (RuntimeException $e)
  248. {
  249. return '';
  250. }
  251. if ($ttl = JFactory::getApplication()->input->getInt('cache', 0))
  252. {
  253. return RLCache::write($hash, $content, $ttl > 1 ? $ttl : 0);
  254. }
  255. return RLCache::set($hash, $content);
  256. }
  257. public static function getComponentBuffer()
  258. {
  259. $buffer = JFactory::getDocument()->getBuffer('component');
  260. if (empty($buffer) || !is_string($buffer))
  261. {
  262. return false;
  263. }
  264. $buffer = trim($buffer);
  265. if (empty($buffer))
  266. {
  267. return false;
  268. }
  269. return $buffer;
  270. }
  271. public static function getAliasAndElement(&$name)
  272. {
  273. $name = self::getNameByAlias($name);
  274. $alias = self::getAliasByName($name);
  275. $element = self::getElementByAlias($alias);
  276. return array($alias, $element);
  277. }
  278. public static function getNameByAlias($alias)
  279. {
  280. // Alias is a language string
  281. if (strpos($alias, ' ') === false && strtoupper($alias) == $alias)
  282. {
  283. return JText::_($alias);
  284. }
  285. // Alias has a space and/or capitals, so is already a name
  286. if (strpos($alias, ' ') !== false || $alias !== strtolower($alias))
  287. {
  288. return $alias;
  289. }
  290. return JText::_(self::getXMLValue('name', $alias));
  291. }
  292. public static function getAliasByName($name)
  293. {
  294. $alias = preg_replace('#[^a-z0-9]#', '', strtolower($name));
  295. switch ($alias)
  296. {
  297. case 'advancedmodules':
  298. return 'advancedmodulemanager';
  299. case 'advancedtemplates':
  300. return 'advancedtemplatemanager';
  301. case 'nonumbermanager':
  302. return 'nonumberextensionmanager';
  303. case 'what-nothing':
  304. return 'whatnothing';
  305. }
  306. return $alias;
  307. }
  308. public static function getElementByAlias($alias)
  309. {
  310. $alias = self::getAliasByName($alias);
  311. switch ($alias)
  312. {
  313. case 'advancedmodulemanager':
  314. return 'advancedmodules';
  315. case 'advancedtemplatemanager':
  316. return 'advancedtemplates';
  317. case 'nonumberextensionmanager':
  318. return 'nonumbermanager';
  319. }
  320. return $alias;
  321. }
  322. public static function getXMLValue($key, $alias, $type = 'component', $folder = 'system')
  323. {
  324. if (!$xml = self::getXML($alias, $type, $folder))
  325. {
  326. return '';
  327. }
  328. if (!isset($xml[$key]))
  329. {
  330. return '';
  331. }
  332. return isset($xml[$key]) ? $xml[$key] : '';
  333. }
  334. public static function getXML($alias, $type = 'component', $folder = 'system')
  335. {
  336. if (!$file = self::getXMLFile($alias, $type, $folder))
  337. {
  338. return false;
  339. }
  340. return JApplicationHelper::parseXMLInstallFile($file);
  341. }
  342. public static function getXMLFile($alias, $type = 'component', $folder = 'system')
  343. {
  344. jimport('joomla.filesystem.file');
  345. $element = self::getElementByAlias($alias);
  346. $files = array();
  347. // Components
  348. if (empty($type) || $type == 'component')
  349. {
  350. $files[] = JPATH_ADMINISTRATOR . '/components/com_' . $element . '/' . $element . '.xml';
  351. $files[] = JPATH_SITE . '/components/com_' . $element . '/' . $element . '.xml';
  352. $files[] = JPATH_ADMINISTRATOR . '/components/com_' . $element . '/com_' . $element . '.xml';
  353. $files[] = JPATH_SITE . '/components/com_' . $element . '/com_' . $element . '.xml';
  354. }
  355. // Plugins
  356. if (empty($type) || $type == 'plugin')
  357. {
  358. if (!empty($folder))
  359. {
  360. $files[] = JPATH_PLUGINS . '/' . $folder . '/' . $element . '/' . $element . '.xml';
  361. $files[] = JPATH_PLUGINS . '/' . $folder . '/' . $element . '.xml';
  362. }
  363. // System Plugins
  364. $files[] = JPATH_PLUGINS . '/system/' . $element . '/' . $element . '.xml';
  365. $files[] = JPATH_PLUGINS . '/system/' . $element . '.xml';
  366. // Editor Button Plugins
  367. $files[] = JPATH_PLUGINS . '/editors-xtd/' . $element . '/' . $element . '.xml';
  368. $files[] = JPATH_PLUGINS . '/editors-xtd/' . $element . '.xml';
  369. }
  370. // Modules
  371. if (empty($type) || $type == 'module')
  372. {
  373. $files[] = JPATH_ADMINISTRATOR . '/modules/mod_' . $element . '/' . $element . '.xml';
  374. $files[] = JPATH_SITE . '/modules/mod_' . $element . '/' . $element . '.xml';
  375. $files[] = JPATH_ADMINISTRATOR . '/modules/mod_' . $element . '/mod_' . $element . '.xml';
  376. $files[] = JPATH_SITE . '/modules/mod_' . $element . '/mod_' . $element . '.xml';
  377. }
  378. foreach ($files as $file)
  379. {
  380. if (!JFile::exists($file))
  381. {
  382. continue;
  383. }
  384. return $file;
  385. }
  386. return '';
  387. }
  388. public static function extensionInstalled($extension, $type = 'component', $folder = 'system')
  389. {
  390. jimport('joomla.filesystem.folder');
  391. jimport('joomla.filesystem.file');
  392. switch ($type)
  393. {
  394. case 'component':
  395. if (JFile::exists(JPATH_ADMINISTRATOR . '/components/com_' . $extension . '/' . $extension . '.php')
  396. || JFile::exists(JPATH_ADMINISTRATOR . '/components/com_' . $extension . '/admin.' . $extension . '.php')
  397. || JFile::exists(JPATH_SITE . '/components/com_' . $extension . '/' . $extension . '.php')
  398. )
  399. {
  400. if ($extension == 'cookieconfirm' && JFile::exists(JPATH_ADMINISTRATOR . '/components/com_cookieconfirm/version.php'))
  401. {
  402. // Only Cookie Confirm 2.0.0.rc1 and above is supported, because
  403. // previous versions don't have isCookiesAllowed()
  404. require_once JPATH_ADMINISTRATOR . '/components/com_cookieconfirm/version.php';
  405. if (version_compare(COOKIECONFIRM_VERSION, '2.2.0.rc1', '<'))
  406. {
  407. return false;
  408. }
  409. }
  410. return true;
  411. }
  412. break;
  413. case 'plugin':
  414. return JFile::exists(JPATH_PLUGINS . '/' . $folder . '/' . $extension . '/' . $extension . '.php');
  415. case 'module':
  416. return (JFile::exists(JPATH_ADMINISTRATOR . '/modules/mod_' . $extension . '/' . $extension . '.php')
  417. || JFile::exists(JPATH_ADMINISTRATOR . '/modules/mod_' . $extension . '/mod_' . $extension . '.php')
  418. || JFile::exists(JPATH_SITE . '/modules/mod_' . $extension . '/' . $extension . '.php')
  419. || JFile::exists(JPATH_SITE . '/modules/mod_' . $extension . '/mod_' . $extension . '.php')
  420. );
  421. case 'library':
  422. return JFolder::exists(JPATH_LIBRARIES . '/' . $extension);
  423. }
  424. return false;
  425. }
  426. public static function loadLanguage($extension = 'plg_system_regularlabs', $basePath = '', $reload = false)
  427. {
  428. if ($basePath && JFactory::getLanguage()->load($extension, $basePath, null, $reload))
  429. {
  430. return true;
  431. }
  432. $basePath = self::getExtensionPath($extension, $basePath, 'language');
  433. return JFactory::getLanguage()->load($extension, $basePath, null, $reload);
  434. }
  435. public static function getExtensionPath($extension = 'plg_system_regularlabs', $basePath = JPATH_ADMINISTRATOR, $check_folder = '')
  436. {
  437. $basePath = $basePath ?: JPATH_SITE;
  438. if (!in_array($basePath, array(JPATH_ADMINISTRATOR, JPATH_SITE)))
  439. {
  440. return $basePath;
  441. }
  442. $extension = str_replace('.sys', '', $extension);
  443. switch (true)
  444. {
  445. case (strpos($extension, 'com_') === 0):
  446. $path = 'components/' . $extension;
  447. break;
  448. case (strpos($extension, 'mod_') === 0):
  449. $path = 'modules/' . $extension;
  450. break;
  451. case (strpos($extension, 'plg_system_') === 0):
  452. $path = 'plugins/system/' . substr($extension, strlen('plg_system_'));
  453. break;
  454. case (strpos($extension, 'plg_editors-xtd_') === 0):
  455. $path = 'plugins/editors-xtd/' . substr($extension, strlen('plg_editors-xtd_'));
  456. break;
  457. }
  458. $check_folder = $check_folder ? '/' . $check_folder : '';
  459. if (is_dir($basePath . '/' . $path . $check_folder))
  460. {
  461. return $basePath . '/' . $path;
  462. }
  463. if (is_dir(JPATH_ADMINISTRATOR . '/' . $path . $check_folder))
  464. {
  465. return JPATH_ADMINISTRATOR . '/' . $path;
  466. }
  467. if (is_dir(JPATH_SITE . '/' . $path . $check_folder))
  468. {
  469. return JPATH_SITE . '/' . $path;
  470. }
  471. return $basePath;
  472. }
  473. public static function xmlToObject($url, $root = '')
  474. {
  475. $hash = md5('xmlToObject_' . $url . '_' . $root);
  476. if (RLCache::has($hash))
  477. {
  478. return RLCache::get($hash);
  479. }
  480. if (JFile::exists($url))
  481. {
  482. $xml = @new SimpleXMLElement($url, LIBXML_NONET | LIBXML_NOCDATA, 1);
  483. }
  484. else
  485. {
  486. $xml = simplexml_load_string($url, "SimpleXMLElement", LIBXML_NONET | LIBXML_NOCDATA);
  487. }
  488. if (!@count($xml))
  489. {
  490. return RLCache::set(
  491. $hash,
  492. new stdClass
  493. );
  494. }
  495. if ($root)
  496. {
  497. if (!isset($xml->{$root}))
  498. {
  499. return RLCache::set(
  500. $hash,
  501. new stdClass
  502. );
  503. }
  504. $xml = $xml->{$root};
  505. }
  506. $json = json_encode($xml);
  507. $xml = json_decode($json);
  508. if (is_null($xml))
  509. {
  510. $xml = new stdClass;
  511. }
  512. if ($root && isset($xml->{$root}))
  513. {
  514. $xml = $xml->{$root};
  515. }
  516. return RLCache::set(
  517. $hash,
  518. $xml
  519. );
  520. }
  521. }