Guest User

11

a guest
Sep 6th, 2016
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.99 KB | None | 0 0
  1. <?php
  2. /**
  3. * @package NoNumber Framework
  4. * @version 16.8.1269
  5. *
  6. * @author Peter van Westen <[email protected]>
  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.  
  12. defined('_JEXEC') or die;
  13.  
  14. require_once __DIR__ . '/cache.php';
  15.  
  16. /**
  17. * Framework Functions
  18. */
  19. class NNFrameworkFunctions
  20. {
  21. var $_version = '14.9.1';
  22.  
  23. public static function isFeed()
  24. {
  25. return (
  26. JFactory::getDocument()->getType() == 'feed'
  27. || JFactory::getApplication()->input->getWord('format') == 'feed'
  28. || JFactory::getApplication()->input->getWord('type') == 'rss'
  29. || JFactory::getApplication()->input->getWord('type') == 'atom'
  30. );
  31. }
  32.  
  33. public static function script($file, $version = '')
  34. {
  35. if (!$file = self::getFileByFolder('js', $file))
  36. {
  37. return;
  38. }
  39.  
  40. if (!empty($version))
  41. {
  42. $file .= '?v=' . $version;
  43. }
  44.  
  45. JFactory::getDocument()->addScript($file);
  46. }
  47.  
  48. public static function stylesheet($file, $version = '')
  49. {
  50. if (!$file = self::getFileByFolder('css', $file))
  51. {
  52. return;
  53. }
  54.  
  55. if (!empty($version))
  56. {
  57. $file .= '?v=' . $version;
  58. }
  59.  
  60. JFactory::getDocument()->addStylesheet($file);
  61. }
  62.  
  63. protected static function getFileByFolder($folder, $file)
  64. {
  65. // If http is present in filename
  66. if (strpos($file, 'http') === 0 || strpos($file, '//') === 0)
  67. {
  68. return $file;
  69. }
  70.  
  71. // Get the template
  72. $template = JFactory::getApplication()->getTemplate();
  73.  
  74. $files = array();
  75.  
  76. // Detect debug mode
  77. if (JFactory::getConfig()->get('debug') || JFactory::getApplication()->input->get('debug'))
  78. {
  79. $files[] = str_replace(array('.min.', '-min.'), '.', $file);
  80. }
  81.  
  82. $files[] = $file;
  83.  
  84. /*
  85. * Loop on 1 or 2 files and break on first found.
  86. * Add the content of the MD5SUM file located in the same folder to url to ensure cache browser refresh
  87. * This MD5SUM file must represent the signature of the folder content
  88. */
  89. foreach ($files as $check_file)
  90. {
  91. // If the file is in the template folder
  92. if ($file_found = self::fileExists('/templates/' . $template . '/' . $folder . '/' . $check_file))
  93. {
  94. return $file_found;
  95. }
  96.  
  97. // Try to deal with system files in the media folder
  98. if (strpos($check_file, '/') === false)
  99. {
  100. if ($file_found = self::fileExists('/media/system' . $folder . '/' . $check_file))
  101. {
  102. return $file_found;
  103. }
  104.  
  105. continue;
  106. }
  107.  
  108. if ($file_found = self::fileExistsByFolder($check_file, $folder))
  109. {
  110. return $file_found;
  111. }
  112. }
  113.  
  114. return false;
  115. }
  116.  
  117. private static function fileExistsByFolder($file, $folder)
  118. {
  119. $template = JFactory::getApplication()->getTemplate();
  120.  
  121. // If the file is in the template folder
  122. if ($file_found = self::fileExists('/templates/' . $template . '/' . $folder . '/' . $file))
  123. {
  124. return $file_found;
  125. }
  126.  
  127. // Try to deal with system files in the media folder
  128. if (strpos($file, '/') === false)
  129. {
  130. if ($file_found = self::fileExists('/media/system/' . $folder . '/' . $file))
  131. {
  132. return $file_found;
  133. }
  134.  
  135. return false;
  136. }
  137.  
  138. $paths = array();
  139.  
  140. // If the file contains any /: it can be in a media extension subfolder
  141. // Divide the file extracting the extension as the first part before /
  142. list($extension, $file) = explode('/', $file, 2);
  143.  
  144. $paths[] = '/media/' . $extension . '/' . $folder;
  145. $paths[] = '/templates/' . $template . '/' . $folder . '/system';
  146. $paths[] = '/media/system/' . $folder;
  147.  
  148. foreach ($paths as $path)
  149. {
  150. if ($file_found = self::fileExists($path . '/' . $file))
  151. {
  152. return $file_found;
  153. }
  154. }
  155.  
  156. return false;
  157. }
  158.  
  159. private static function fileExists($path)
  160. {
  161. if (!file_exists(JPATH_ROOT . $path))
  162. {
  163. return false;
  164. }
  165.  
  166. return JUri::root(true) . $path;
  167. }
  168.  
  169. /*
  170. * @Deprecated
  171. * Use NNFrameworkFunctions::script($url, $version) instead
  172. */
  173. public static function addScriptVersion($url)
  174. {
  175. jimport('joomla.filesystem.file');
  176.  
  177. if (!empty($_GET['debug']))
  178. {
  179. $url = str_replace('.min.', '.', $url);
  180. }
  181.  
  182. if (!JFile::exists(JPATH_SITE . $url))
  183. {
  184. return JFactory::getDocument()->addScriptVersion($url);
  185. }
  186.  
  187. JFactory::getDocument()->addScript($url . '?' . filemtime(JPATH_SITE . $url));
  188. }
  189.  
  190. /*
  191. * @Deprecated
  192. * Use NNFrameworkFunctions::stylesheet($url, $version) instead
  193. */
  194. public static function addStyleSheetVersion($url)
  195. {
  196. jimport('joomla.filesystem.file');
  197.  
  198. if (!empty($_GET['debug']))
  199. {
  200. $url = str_replace('.min.', '.', $url);
  201. }
  202.  
  203. if (!JFile::exists(JPATH_SITE . $url))
  204. {
  205. return JFactory::getDocument()->addStyleSheetVersion($url);
  206. }
  207.  
  208. JFactory::getDocument()->addStyleSheet($url . '?' . filemtime(JPATH_SITE . $url));
  209. }
  210.  
  211. public function getByUrl($url)
  212. {
  213. $hash = md5('getByUrl_' . $url);
  214.  
  215. if (NNCache::has($hash))
  216. {
  217. return NNCache::get($hash);
  218. }
  219.  
  220. // only allow url calls from administrator
  221. if (!JFactory::getApplication()->isAdmin())
  222. {
  223. die;
  224. }
  225.  
  226. // only allow when logged in
  227. $user = JFactory::getUser();
  228. if (!$user->id)
  229. {
  230. die;
  231. }
  232.  
  233. if (substr($url, 0, 4) != 'http')
  234. {
  235. $url = 'http://' . $url;
  236. }
  237.  
  238. // only allow url calls to nonumber.nl domain
  239. if (!(preg_match('#^https?://([^/]+\.)?nonumber\.nl/#', $url)))
  240. {
  241. die;
  242. }
  243.  
  244. // only allow url calls to certain files
  245. if (
  246. strpos($url, 'download.nonumber.nl/extensions.php') === false
  247. && strpos($url, 'download.nonumber.nl/extensions.json') === false
  248. && strpos($url, 'download.nonumber.nl/extensions.xml') === false
  249. )
  250. {
  251. die;
  252. }
  253.  
  254. $format = (strpos($url, '.json') !== false || strpos($url, 'format=json') !== false)
  255. ? 'application/json'
  256. : 'text/xml';
  257.  
  258. header("Pragma: public");
  259. header("Expires: 0");
  260. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  261. header("Cache-Control: public");
  262. header("Content-type: " . $format);
  263.  
  264. return NNCache::set(
  265. $hash,
  266. self::getContents($url)
  267. );
  268. }
  269.  
  270. public function getContents($url, $timeout = 20)
  271. {
  272. $hash = md5('getContents_' . $url);
  273.  
  274. if (NNCache::has($hash))
  275. {
  276. return NNCache::get($hash);
  277. }
  278.  
  279. if (JFactory::getApplication()->input->getInt('cache', 0)
  280. && $content = NNCache::read($hash)
  281. )
  282. {
  283. return $content;
  284. }
  285.  
  286. try
  287. {
  288. $content = JHttpFactory::getHttp()->get($url, null, $timeout)->body;
  289. }
  290. catch (RuntimeException $e)
  291. {
  292. return '';
  293. }
  294.  
  295. if ($ttl = JFactory::getApplication()->input->getInt('cache', 0))
  296. {
  297. return NNCache::write($hash, $content, $ttl > 1 ? $ttl : 0);
  298. }
  299.  
  300. return NNCache::set($hash, $content);
  301. }
  302.  
  303. public static function getComponentBuffer()
  304. {
  305. $buffer = JFactory::getDocument()->getBuffer('component');
  306.  
  307. if (empty($buffer) || !is_string($buffer))
  308. {
  309. return false;
  310. }
  311.  
  312. $buffer = trim($buffer);
  313.  
  314. if (empty($buffer))
  315. {
  316. return false;
  317. }
  318.  
  319. return $buffer;
  320. }
  321.  
  322. public static function getAliasAndElement(&$name)
  323. {
  324. $name = self::getNameByAlias($name);
  325. $alias = self::getAliasByName($name);
  326. $element = self::getElementByAlias($alias);
  327.  
  328. return array($alias, $element);
  329. }
  330.  
  331. public static function getNameByAlias($alias)
  332. {
  333. // Alias is a language string
  334. if (strpos($alias, ' ') === false && strtoupper($alias) == $alias)
  335. {
  336. return JText::_($alias);
  337. }
  338.  
  339. // Alias has a space and/or capitals, so is already a name
  340. if (strpos($alias, ' ') !== false || $alias !== strtolower($alias))
  341. {
  342. return $alias;
  343. }
  344.  
  345. return JText::_(self::getXMLValue('name', $alias));
  346. }
  347.  
  348. public static function getAliasByName($name)
  349. {
  350. $alias = preg_replace('#[^a-z0-9]#', '', strtolower($name));
  351.  
  352. switch ($alias)
  353. {
  354. case 'advancedmodules':
  355. return 'advancedmodulemanager';
  356.  
  357. case 'advancedtemplates':
  358. return 'advancedtemplatemanager';
  359.  
  360. case 'nonumbermanager':
  361. return 'nonumberextensionmanager';
  362.  
  363. case 'what-nothing':
  364. return 'whatnothing';
  365. }
  366.  
  367. return $alias;
  368. }
  369.  
  370. public static function getElementByAlias($alias)
  371. {
  372. $alias = self::getAliasByName($alias);
  373.  
  374. switch ($alias)
  375. {
  376. case 'advancedmodulemanager':
  377. return 'advancedmodules';
  378.  
  379. case 'advancedtemplatemanager':
  380. return 'advancedtemplates';
  381.  
  382. case 'nonumberextensionmanager':
  383. return 'nonumbermanager';
  384. }
  385.  
  386. return $alias;
  387. }
  388.  
  389. static function getXMLValue($key, $alias, $type = 'component', $folder = 'system')
  390. {
  391. if (!$xml = self::getXML($alias, $type, $folder))
  392. {
  393. return '';
  394. }
  395.  
  396. if (!isset($xml[$key]))
  397. {
  398. return '';
  399. }
  400.  
  401. return isset($xml[$key]) ? $xml[$key] : '';
  402. }
  403.  
  404. static function getXML($alias, $type = 'component', $folder = 'system')
  405. {
  406. if (!$file = self::getXMLFile($alias, $type, $folder))
  407. {
  408. return false;
  409. }
  410.  
  411. return JApplicationHelper::parseXMLInstallFile($file);
  412. }
  413.  
  414. static function getXMLFile($alias, $type = 'component', $folder = 'system')
  415. {
  416. jimport('joomla.filesystem.file');
  417.  
  418. $element = self::getElementByAlias($alias);
  419.  
  420. $files = array();
  421.  
  422. // Components
  423. if (empty($type) || $type == 'component')
  424. {
  425. $files[] = JPATH_ADMINISTRATOR . '/components/com_' . $element . '/' . $element . '.xml';
  426. $files[] = JPATH_SITE . '/components/com_' . $element . '/' . $element . '.xml';
  427. $files[] = JPATH_ADMINISTRATOR . '/components/com_' . $element . '/com_' . $element . '.xml';
  428. $files[] = JPATH_SITE . '/components/com_' . $element . '/com_' . $element . '.xml';
  429. }
  430.  
  431. // Plugins
  432. if (empty($type) || $type == 'plugin')
  433. {
  434. if (!empty($folder))
  435. {
  436. $files[] = JPATH_PLUGINS . '/' . $folder . '/' . $element . '/' . $element . '.xml';
  437. $files[] = JPATH_PLUGINS . '/' . $folder . '/' . $element . '.xml';
  438. }
  439.  
  440. // System Plugins
  441. $files[] = JPATH_PLUGINS . '/system/' . $element . '/' . $element . '.xml';
  442. $files[] = JPATH_PLUGINS . '/system/' . $element . '.xml';
  443.  
  444. // Editor Button Plugins
  445. $files[] = JPATH_PLUGINS . '/editors-xtd/' . $element . '/' . $element . '.xml';
  446. $files[] = JPATH_PLUGINS . '/editors-xtd/' . $element . '.xml';
  447. }
  448.  
  449. // Modules
  450. if (empty($type) || $type == 'module')
  451. {
  452. $files[] = JPATH_ADMINISTRATOR . '/modules/mod_' . $element . '/' . $element . '.xml';
  453. $files[] = JPATH_SITE . '/modules/mod_' . $element . '/' . $element . '.xml';
  454. $files[] = JPATH_ADMINISTRATOR . '/modules/mod_' . $element . '/mod_' . $element . '.xml';
  455. $files[] = JPATH_SITE . '/modules/mod_' . $element . '/mod_' . $element . '.xml';
  456. }
  457.  
  458. foreach ($files as $file)
  459. {
  460. if (!JFile::exists($file))
  461. {
  462. continue;
  463. }
  464.  
  465. return $file;
  466. }
  467.  
  468. return '';
  469. }
  470.  
  471. static function extensionInstalled($extension, $type = 'component', $folder = 'system')
  472. {
  473. jimport('joomla.filesystem.folder');
  474. jimport('joomla.filesystem.file');
  475.  
  476. switch ($type)
  477. {
  478. case 'component':
  479. if (JFile::exists(JPATH_ADMINISTRATOR . '/components/com_' . $extension . '/' . $extension . '.php')
  480. || JFile::exists(JPATH_ADMINISTRATOR . '/components/com_' . $extension . '/admin.' . $extension . '.php')
  481. || JFile::exists(JPATH_SITE . '/components/com_' . $extension . '/' . $extension . '.php')
  482. )
  483. {
  484. if ($extension == 'cookieconfirm')
  485. {
  486. // Only Cookie Confirm 2.0.0.rc1 and above is supported, because
  487. // previous versions don't have isCookiesAllowed()
  488. require_once JPATH_ADMINISTRATOR . '/components/com_cookieconfirm/version.php';
  489.  
  490. if (version_compare(COOKIECONFIRM_VERSION, '2.2.0.rc1', '<'))
  491. {
  492. return false;
  493. }
  494. }
  495.  
  496. return true;
  497. }
  498. break;
  499.  
  500. case 'plugin':
  501. return JFile::exists(JPATH_PLUGINS . '/' . $folder . '/' . $extension . '/' . $extension . '.php');
  502.  
  503. case 'module':
  504. return (JFile::exists(JPATH_ADMINISTRATOR . '/modules/mod_' . $extension . '/' . $extension . '.php')
  505. || JFile::exists(JPATH_ADMINISTRATOR . '/modules/mod_' . $extension . '/mod_' . $extension . '.php')
  506. || JFile::exists(JPATH_SITE . '/modules/mod_' . $extension . '/' . $extension . '.php')
  507. || JFile::exists(JPATH_SITE . '/modules/mod_' . $extension . '/mod_' . $extension . '.php')
  508. );
  509.  
  510. case 'library':
  511. return JFolder::exists(JPATH_LIBRARIES . '/' . $extension);
  512. }
  513.  
  514. return false;
  515. }
  516.  
  517. static function loadLanguage($extension = 'plg_system_nnframework', $basePath = '')
  518. {
  519. if ($basePath && JFactory::getLanguage()->load($extension, $basePath))
  520. {
  521. return true;
  522. }
  523.  
  524. $basePath = self::getExtensionPath($extension, $basePath, 'language');
  525.  
  526. return JFactory::getLanguage()->load($extension, $basePath);
  527. }
  528.  
  529. static function getExtensionPath($extension = 'plg_system_nnframework', $basePath = JPATH_ADMINISTRATOR, $check_folder = '')
  530. {
  531. $basePath = $basePath ?: JPATH_SITE;
  532.  
  533. if (!in_array($basePath, array(JPATH_ADMINISTRATOR, JPATH_SITE)))
  534. {
  535. return $basePath;
  536. }
  537.  
  538. switch (true)
  539. {
  540. case (strpos($extension, 'com_') === 0):
  541. $path = 'components/' . $extension;
  542. break;
  543.  
  544. case (strpos($extension, 'mod_') === 0):
  545. $path = 'modules/' . $extension;
  546. break;
  547.  
  548. case (strpos($extension, 'plg_system_') === 0):
  549. $path = 'plugins/system/' . substr($extension, strlen('plg_system_'));
  550. break;
  551.  
  552. case (strpos($extension, 'plg_editors-xtd_') === 0):
  553. $path = 'plugins/editors-xtd/' . substr($extension, strlen('plg_editors-xtd_'));
  554. break;
  555. }
  556.  
  557. $check_folder = $check_folder ? '/' . $check_folder : '';
  558.  
  559. if (is_dir($basePath . '/' . $path . $check_folder))
  560. {
  561. return $basePath . '/' . $path;
  562. }
  563.  
  564. if (is_dir(JPATH_ADMINISTRATOR . '/' . $path . $check_folder))
  565. {
  566. return JPATH_ADMINISTRATOR . '/' . $path;
  567. }
  568.  
  569. if (is_dir(JPATH_SITE . '/' . $path . $check_folder))
  570. {
  571. return JPATH_SITE . '/' . $path;
  572. }
  573.  
  574. return $basePath;
  575. }
  576.  
  577. static function xmlToObject($url, $root = '')
  578. {
  579. $hash = md5('xmlToObject_' . $url . '_' . $root);
  580.  
  581. if (NNCache::has($hash))
  582. {
  583. return NNCache::get($hash);
  584. }
  585.  
  586. if (JFile::exists($url))
  587. {
  588. $xml = @new SimpleXMLElement($url, LIBXML_NONET | LIBXML_NOCDATA, 1);
  589. }
  590. else
  591. {
  592. $xml = simplexml_load_string($url, "SimpleXMLElement", LIBXML_NONET | LIBXML_NOCDATA);
  593. }
  594.  
  595. if (!@count($xml))
  596. {
  597. return NNCache::set(
  598. $hash,
  599. new stdClass
  600. );
  601. }
  602.  
  603. if ($root)
  604. {
  605. if (!isset($xml->{$root}))
  606. {
  607. return NNCache::set(
  608. $hash,
  609. new stdClass
  610. );
  611. }
  612.  
  613. $xml = $xml->{$root};
  614. }
  615.  
  616. $json = json_encode($xml);
  617. $xml = json_decode($json);
  618. if (is_null($xml))
  619. {
  620. $xml = new stdClass;
  621. }
  622.  
  623. if ($root && isset($xml->{$root}))
  624. {
  625. $xml = $xml->{$root};
  626. }
  627.  
  628. return NNCache::set(
  629. $hash,
  630. $xml
  631. );
  632. }
  633.  
  634. /*
  635. * @Deprecated
  636. */
  637. protected
  638. function curl($url)
  639. {
  640. $hash = md5('curl_' . $url);
  641.  
  642. if (NNCache::has($hash))
  643. {
  644. return NNCache::get($hash);
  645. }
  646.  
  647. $timeout = JFactory::getApplication()->input->getInt('timeout', 3);
  648. $timeout = min(array(30, max(array(3, $timeout))));
  649.  
  650. $ch = curl_init($url);
  651.  
  652. curl_setopt($ch, CURLOPT_URL, $url);
  653. curl_setopt($ch, CURLOPT_HEADER, false);
  654. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  655. curl_setopt($ch, CURLOPT_USERAGENT, 'NoNumber/' . $this->_version);
  656. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  657. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  658.  
  659. //follow on location problems
  660. if (!ini_get('safe_mode') && !ini_get('open_basedir'))
  661. {
  662. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  663. $html = curl_exec($ch);
  664. }
  665. else
  666. {
  667. $html = $this->curl_redir_exec($ch);
  668. }
  669. curl_close($ch);
  670.  
  671. return NNCache::set(
  672. $hash,
  673. $html
  674. );
  675. }
  676.  
  677. /*
  678. * @Deprecated
  679. */
  680. public function curl_redir_exec($ch)
  681. {
  682. static $curl_loops = 0;
  683. static $curl_max_loops = 20;
  684.  
  685. if ($curl_loops++ >= $curl_max_loops)
  686. {
  687. $curl_loops = 0;
  688.  
  689. return false;
  690. }
  691.  
  692. curl_setopt($ch, CURLOPT_HEADER, true);
  693. $data = curl_exec($ch);
  694.  
  695. list($header, $data) = explode("\n\n", str_replace("\r", '', $data), 2);
  696. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  697.  
  698. if ($http_code != 301 && $http_code != 302)
  699. {
  700. return $data;
  701. }
  702.  
  703. $matches = array();
  704. preg_match('/Location:(.*?)\n/', $header, $matches);
  705. $url = @parse_url(trim(array_pop($matches)));
  706. if (!$url)
  707. {
  708. //couldn't process the url to redirect to
  709. $curl_loops = 0;
  710.  
  711. return $data;
  712. }
  713. $last_url = parse_url(curl_getinfo($ch, CURLINFO_EFFECTIVE_URL));
  714. if (!$url['scheme'])
  715. {
  716. $url['scheme'] = $last_url['scheme'];
  717. }
  718. if (!$url['host'])
  719. {
  720. $url['host'] = $last_url['host'];
  721. }
  722. if (!$url['path'])
  723. {
  724. $url['path'] = $last_url['path'];
  725. }
  726.  
  727. // $new_url = $url['scheme'] . '://' . $url['host'] . $
  728.  
  729. $new_url = $url['scheme'].'://'.$url['host'].$url['path'].($url['query'] ? '?'.$url['query'] : '');
  730. curl_setopt($ch, CURLOPT_URL, $new_url);
  731. return $this->curl_redir_exec($ch);
  732. }
  733. else
  734. {
  735. $curl_loops = 0;
  736. return $data;
  737. }
  738. }
  739. }
Advertisement
Add Comment
Please, Sign In to add comment