PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/source/administrator/components/com_paypalaccess/lib/helper.php

https://github.com/yireo/pkg_paypalaccess
PHP | 329 lines | 170 code | 33 blank | 126 comment | 43 complexity | 75184cc25def953fe320f5d07ef5c513 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /*
  3. * Joomla! Yireo Library
  4. *
  5. * @author Yireo (info@yireo.com)
  6. * @package YireoLib
  7. * @copyright Copyright 2014
  8. * @license GNU Public License
  9. * @link http://www.yireo.com
  10. * @version 0.6.0
  11. */
  12. // Check to ensure this file is included in Joomla!
  13. defined('_JEXEC') or die();
  14. // Import the loader
  15. require_once dirname(__FILE__).'/loader.php';
  16. /**
  17. * Yireo Helper
  18. */
  19. class YireoHelper
  20. {
  21. /*
  22. * Helper-method to get the Joomla! DBO
  23. *
  24. * @param null
  25. * @return bool
  26. */
  27. static public function getDBO()
  28. {
  29. if (YireoHelper::isJoomla15()) {
  30. return JFactory::getDBO();
  31. }
  32. return JFactory::getDbo();
  33. }
  34. /*
  35. * Helper-method to parse the data defined in this component
  36. *
  37. * @param null
  38. * @return bool
  39. */
  40. static public function getData($name = null)
  41. {
  42. $file = JPATH_COMPONENT.'/helpers/abstract.php';
  43. if (is_file($file)) {
  44. require_once $file ;
  45. $class = 'HelperAbstract';
  46. if (class_exists($class)) {
  47. $object = new $class;
  48. $data = $object->getStructure();
  49. if (isset($data[$name])) {
  50. return $data[$name];
  51. }
  52. }
  53. }
  54. return null;
  55. }
  56. /*
  57. * Helper-method to return the HTML-ending of a form
  58. *
  59. * @param null
  60. * @return bool
  61. */
  62. static public function getFormEnd($id = 0)
  63. {
  64. echo '<input type="hidden" name="option" value="'.JRequest::getCmd('option').'" />';
  65. echo '<input type="hidden" name="cid[]" value="'.$id.'" />';
  66. echo '<input type="hidden" name="task" value="" />';
  67. echo JHTML::_( 'form.token' );
  68. }
  69. /*
  70. * Helper-method to check whether the current Joomla! version equals some value
  71. *
  72. * @param $version string|array
  73. * @return bool
  74. */
  75. static public function isJoomla($version)
  76. {
  77. JLoader::import( 'joomla.version' );
  78. $jversion = new JVersion();
  79. if (!is_array($version)) $version = array($version);
  80. foreach($version as $v) {
  81. if (version_compare( $jversion->RELEASE, $v, 'eq')) {
  82. return true;
  83. }
  84. }
  85. return false;
  86. }
  87. /*
  88. * Helper-method to check whether the current Joomla! version is 3.5
  89. *
  90. * @param null
  91. * @return bool
  92. */
  93. static public function isJoomla35()
  94. {
  95. return self::isJoomla(array('3.0', '3.1', '3.2', '3.5'));
  96. }
  97. /*
  98. * Helper-method to check whether the current Joomla! version is 2.5
  99. *
  100. * @param null
  101. * @return bool
  102. */
  103. static public function isJoomla25()
  104. {
  105. if(self::isJoomla('2.5') || self::isJoomla('1.7') || self::isJoomla('1.6')) {
  106. return true;
  107. }
  108. return false;
  109. }
  110. /*
  111. * Helper-method to check whether the current Joomla! version is 1.5
  112. *
  113. * @param null
  114. * @return bool
  115. */
  116. static public function isJoomla15()
  117. {
  118. return self::isJoomla('1.5');
  119. }
  120. /**
  121. * Method to get the current version
  122. *
  123. * @access public
  124. * @param null
  125. * @return string
  126. */
  127. static public function getCurrentVersion()
  128. {
  129. $option = JRequest::getCmd('option');
  130. $name = preg_replace('/^com_/', '', $option);
  131. $file = JPATH_ADMINISTRATOR.'/components/'.$option.'/'.$name.'.xml';
  132. if(class_exists('JInstaller') && method_exists('JInstaller', 'parseXMLInstallFile')) {
  133. $data = JInstaller::parseXMLInstallFile($file);
  134. return $data['version'];
  135. } elseif(method_exists('JApplicationHelper', 'parseXMLInstallFile')) {
  136. $data = JApplicationHelper::parseXMLInstallFile($file);
  137. return $data['version'];
  138. }
  139. return null;
  140. }
  141. /**
  142. * Method to fetch a specific page
  143. *
  144. * @access public
  145. * @param string $url
  146. * @param string $useragent
  147. * @return bool
  148. */
  149. static public function fetchRemote($url, $useragent = null)
  150. {
  151. if (function_exists('curl_init') == true) {
  152. $ch = curl_init();
  153. curl_setopt($ch, CURLOPT_URL, $url);
  154. curl_setopt($ch, CURLOPT_HEADER, 0);
  155. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  156. curl_setopt($ch, CURLOPT_USERAGENT, (!empty($useragent)) ? $useragent : $_SERVER['HTTP_USER_AGENT']);
  157. $contents = curl_exec($ch);
  158. } else {
  159. $contents = file_get_contents($url);
  160. }
  161. return $contents;
  162. }
  163. /*
  164. * Convert an object or string to JParameter or JRegistry
  165. *
  166. * @param mixed $params
  167. * @param string $file
  168. * @return JParameter|JRegistry
  169. */
  170. static public function toRegistry($params = null, $file = null)
  171. {
  172. if ($params instanceof JParameter || $params instanceof JRegistry) {
  173. return $params;
  174. }
  175. if (is_string($params)) $params = trim($params);
  176. if (self::isJoomla15()) {
  177. jimport('joomla.html.parameter');
  178. $params = @new JParameter($params, $file);
  179. } else {
  180. jimport('joomla.registry.registry');
  181. $registry = @new JRegistry();
  182. if(!empty($params) && is_string($params)) $registry->loadString($params);
  183. if(!empty($params) && is_array($params)) $registry->loadArray($params);
  184. $fileContents = @file_get_contents($file);
  185. if(preg_match('/\.xml$/', $fileContents)) {
  186. $registry->loadFile($file, 'XML');
  187. } elseif(preg_match('/\.json$/', $fileContents)) {
  188. $registry->loadFile($file, 'JSON');
  189. }
  190. $params = $registry;
  191. }
  192. return $params;
  193. }
  194. /*
  195. * Deprecated shortcut for self::toRegistry()
  196. *
  197. * @param mixed $params
  198. * @param string $file
  199. * @return JParameter|JRegistry
  200. * @deprecated
  201. */
  202. static public function toParameter($params = null, $file = null)
  203. {
  204. return self::toRegistry($params, $file);
  205. }
  206. /*
  207. * Add in Bootstrap
  208. *
  209. * @access public
  210. * @subpackage Yireo
  211. * @param null
  212. * @return null
  213. */
  214. static public function bootstrap()
  215. {
  216. if (self::isJoomla25()) {
  217. // Check if bootstrap is loaded already
  218. $application = JFactory::getApplication();
  219. if(method_exists($application, 'set')) $application->set('bootstrap', true);
  220. $document = JFactory::getDocument();
  221. $document->addStyleSheet('//netdna.bootstrapcdn.com/bootstrap/2.3.2/css/bootstrap.min.css');
  222. $document->addStyleSheet('//netdna.bootstrapcdn.com/bootstrap/2.3.2/js/bootstrap.min.js');
  223. } else {
  224. JHtml::_('bootstrap.framework');
  225. }
  226. }
  227. /*
  228. * Method to check whether Bootstrap is used
  229. *
  230. * @access public
  231. * @subpackage Yireo
  232. * @param null
  233. * @return boolean
  234. */
  235. static public function hasBootstrap()
  236. {
  237. $application = JFactory::getApplication();
  238. if (method_exists($application, 'get') && $application->get('bootstrap') == true) {
  239. return true;
  240. }
  241. return false;
  242. }
  243. /*
  244. * Add in jQuery
  245. *
  246. * @access public
  247. * @subpackage Yireo
  248. * @param null
  249. * @return null
  250. */
  251. static public function jquery()
  252. {
  253. // Do not load when having no HTML-document
  254. $document = JFactory::getDocument();
  255. if(stristr(get_class($document), 'html') == false) {
  256. return;
  257. }
  258. // Load jQuery using the framework (Joomla! 3.0 and higher)
  259. if(YireoHelper::isJoomla15() == false && YireoHelper::isJoomla25() == false) {
  260. return JHtml::_('jquery.framework');
  261. }
  262. // Check if jQuery is loaded already
  263. $application = JFactory::getApplication();
  264. if (method_exists($application, 'get') && $application->get('jquery') == true) {
  265. return;
  266. }
  267. // Do not load this for specific extensions
  268. if(JRequest::getCmd('option') == 'com_virtuemart') return false;
  269. // Load jQuery
  270. $option = JRequest::getCmd('option');
  271. if (file_exists(JPATH_SITE.'/media/'.$option.'/js/jquery.js')) {
  272. $document->addScript(JURI::root().'media/'.$option.'/js/jquery.js');
  273. $document->addCustomTag('<script type="text/javascript">jQuery.noConflict();</script>');
  274. // Set the flag that jQuery has been loaded
  275. if(method_exists($application, 'set')) $application->set('jquery', true);
  276. }
  277. }
  278. /*
  279. * Helper-method to load additional language-files
  280. *
  281. * @access public
  282. * @subpackage Yireo
  283. * @param string $title
  284. * @return null
  285. */
  286. public static function loadLanguageFile()
  287. {
  288. $application = JFactory::getApplication();
  289. $language = JFactory::getLanguage();
  290. $extension = 'lib_yireo';
  291. $folder = ($application->isSite()) ? JPATH_SITE : JPATH_ADMINISTRATOR;
  292. $tag = $language->getTag();
  293. $reload = true;
  294. $language->load($extension, $folder, $tag, $reload);
  295. }
  296. }