PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/administrator/components/com_jfusion/models/model.jfusionadmin.php

http://jfusion.googlecode.com/
PHP | 397 lines | 296 code | 40 blank | 61 comment | 54 complexity | 7570dcc77d8af732be7d618876804c08 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. /**
  3. * @package JFusion
  4. * @subpackage Models
  5. * @author JFusion development team
  6. * @copyright Copyright (C) 2008 JFusion. All rights reserved.
  7. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
  8. */
  9. // no direct access
  10. defined('_JEXEC' ) or die('Restricted access' );
  11. /**
  12. * Class for general JFusion functions for the backend
  13. * @package JFusion
  14. */
  15. class JFusionFunctionAdmin{
  16. /**
  17. * Saves the posted JFusion component variables
  18. * @param string $jname name of the JFusion plugin used
  19. * @param array $post Array of JFusion plugin parameters posted to the JFusion component
  20. * @param boolean $wizard Notes if function was called by wizardresult();*
  21. * @return true|false returns true if succesful and false if an error occurred
  22. */
  23. function saveParameters($jname, $post, $wizard = false)
  24. {
  25. $db = & JFactory::getDBO();
  26. if ($wizard) {
  27. //data submitted by the wizard so merge the data with existing params if they do indeed exist
  28. $query = "SELECT params FROM #__jfusion WHERE name = " . $db->Quote($jname);
  29. $db->setQuery($query);
  30. $existing_serialized = $db->loadResult();
  31. if (!empty($existing_serialized)) {
  32. $existing_params = unserialize(base64_decode($existing_serialized));
  33. if (is_array($existing_params)) {
  34. $post = array_merge($existing_params, $post);
  35. }
  36. }
  37. }
  38. //serialize the $post to allow storage in a SQL field
  39. $serialized = base64_encode(serialize($post));
  40. //set the current parameters in the jfusion table
  41. $query = 'UPDATE #__jfusion SET params = ' . $db->Quote($serialized) .' WHERE name = ' . $db->Quote($jname);
  42. $db->setQuery($query );
  43. if (!$db->query()) {
  44. //there was an error saving the parameters
  45. JError::raiseWarning(0,$db->stderr());
  46. $result = false;
  47. return $result;
  48. }
  49. //reset the params instance for this plugin
  50. JFusionFactory::getParams($jname,true);
  51. $result = true;
  52. return $result;
  53. }
  54. /**
  55. * Checks to see if the JFusion plugins are installed and enabled
  56. */
  57. function checkPlugin()
  58. {
  59. $userPlugin = true;
  60. $authPlugin = true;
  61. if (!JFusionFunctionAdmin::isPluginInstalled('jfusion','authentication', false)) {
  62. JError::raiseWarning(0,JText::_('FUSION_MISSING_AUTH'));
  63. $authPlugin = false;
  64. }
  65. if (!JFusionFunctionAdmin::isPluginInstalled('jfusion','user', false)) {
  66. JError::raiseWarning(0,JText::_('FUSION_MISSING_USER'));
  67. $userPlugin = false;
  68. }
  69. if ($authPlugin && $userPlugin) {
  70. $jAuth = JFusionFunctionAdmin::isPluginInstalled('jfusion','user',true);
  71. $jUser = JFusionFunctionAdmin::isPluginInstalled('jfusion','authentication',true);
  72. if (!$jAuth) {
  73. JError::raiseNotice(0,JText::_('FUSION_READY_TO_USE_AUTH'));
  74. }
  75. if (!$jUser) {
  76. JError::raiseNotice(0,JText::_('FUSION_READY_TO_USE_USER'));
  77. }
  78. }
  79. }
  80. /**
  81. * Tests if a plugin is installed with the specified name, where folder is the type (e.g. user)
  82. * @param string $element element name of the plugin
  83. * @param string $folder folder name of the plugin
  84. * @param integer $testPublished Variable to determine if the function should test to see if the plugin is published
  85. * @return true|false returns true if succesful and false if an error occurred
  86. */
  87. function isPluginInstalled($element,$folder, $testPublished)
  88. {
  89. $db =& JFactory::getDBO();
  90. $query = 'SELECT published FROM #__plugins WHERE element=' . $db->Quote($element) . ' AND folder=' . $db->Quote($folder);
  91. $db->setQuery($query);
  92. $result = $db->loadObject();
  93. if ($result) {
  94. if ($testPublished) {
  95. return($result->published == 1);
  96. } else {
  97. $result = true;
  98. return $result;
  99. }
  100. } else {
  101. $result = false;
  102. return $result;
  103. }
  104. }
  105. /**
  106. * Parses phpinfo into an array
  107. * @return array
  108. */
  109. function phpinfo_array()
  110. {
  111. //get the phpinfo and parse it into an array
  112. ob_start();
  113. phpinfo();
  114. $phpinfo = array('phpinfo' => array());
  115. if (preg_match_all('#(?:<h2>(?:<a name=".*?">)?(.*?)(?:</a>)?</h2>)|(?:<tr(?: class=".*?")?><t[hd](?: class=".*?")?>(.*?)\s*</t[hd]>(?:<t[hd](?: class=".*?")?>(.*?)\s*</t[hd]>(?:<t[hd](?: class=".*?")?>(.*?)\s*</t[hd]>)?)?</tr>)#s', ob_get_clean(), $matches, PREG_SET_ORDER)) {
  116. foreach ($matches as $match) {
  117. if (strlen($match[1])) {
  118. $phpinfo[$match[1]] = array();
  119. } else if (isset($match[3])) {
  120. $phpinfo[end(array_keys($phpinfo))][$match[2]] = isset($match[4]) ? array($match[3], $match[4]) : $match[3];
  121. } else {
  122. $phpinfo[end(array_keys($phpinfo))][] = $match[2];
  123. }
  124. }
  125. }
  126. return $phpinfo;
  127. }
  128. /**
  129. * Checks if url is valid using cURL
  130. * @param $url
  131. * @return error code if any
  132. */
  133. function checkURL($url)
  134. {
  135. $header_codes = array(100 => 'Continue',
  136. 101 => 'Switching Protocols',
  137. 102 => 'Processing',
  138. 200 => 'OK',
  139. 201 => 'Created',
  140. 202 => 'Accepted',
  141. 203 => 'Non-Authoritative Information',
  142. 204 => 'No Content',
  143. 205 => 'Reset Content',
  144. 206 => 'Partial Content',
  145. 207 => 'Multi-Status',
  146. 226 => 'IM Used',
  147. 300 => 'Multiple Choices',
  148. 301 => 'Moved Permanently',
  149. 302 => 'Found',
  150. 303 => 'See Other',
  151. 304 => 'Not Modified',
  152. 305 => 'Use Proxy',
  153. 306 => 'Reserved',
  154. 307 => 'Temporary Redirect',
  155. 400 => 'Bad Request',
  156. 401 => 'Unauthorized',
  157. 402 => 'Payment Required',
  158. 403 => 'Forbidden',
  159. 404 => 'Not Found',
  160. 405 => 'Method Not Allowed',
  161. 406 => 'Not Acceptable',
  162. 407 => 'Proxy Authentication Required',
  163. 408 => 'Request Timeout',
  164. 409 => 'Conflict',
  165. 410 => 'Gone',
  166. 411 => 'Length Required',
  167. 412 => 'Precondition Failed',
  168. 413 => 'Request Entity Too Large',
  169. 414 => 'Request-URI Too Long',
  170. 415 => 'Unsupported Media Type',
  171. 416 => 'Requested Range Not Satisfiable',
  172. 417 => 'Expectation Failed',
  173. 422 => 'Unprocessable Entity',
  174. 423 => 'Locked',
  175. 424 => 'Failed Dependency',
  176. 426 => 'Upgrade Required',
  177. 500 => 'Internal Server Error',
  178. 501 => 'Not Implemented',
  179. 502 => 'Bad Gateway',
  180. 503 => 'Service Unavailable',
  181. 504 => 'Gateway Timeout',
  182. 505 => 'HTTP Version Not Supported',
  183. 506 => 'Variant Also Negotiates',
  184. 507 => 'Insufficient Storage',
  185. 510 => 'Not Extended'
  186. );
  187. ob_start();
  188. if (function_exists('curl_init')) {
  189. //curl is the preferred function
  190. $crl = curl_init();
  191. $timeout = 5;
  192. curl_setopt($crl, CURLOPT_URL,$url);
  193. curl_setopt($crl, CURLOPT_RETURNTRANSFER, 1);
  194. curl_setopt($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
  195. $FileData = curl_exec($crl);
  196. $FileInfo = curl_getinfo($crl);
  197. curl_close($crl);
  198. if ($FileInfo['http_code'] >= 400) {
  199. //there was an error
  200. return $FileInfo['http_code'] . ': ' . $header_codes[$FileInfo['http_code']]. ' error for url ' . $url;
  201. }
  202. } else {
  203. return JText::_('CURL_DISABLED');
  204. }
  205. return null;
  206. }
  207. /**
  208. * Parses remote xml file
  209. * @param $url
  210. * @return Joomla xml object
  211. */
  212. function getFileData($url,$save = 0, $unpack = 0)
  213. {
  214. ob_start();
  215. if (function_exists('curl_init')) {
  216. //curl is the preferred function
  217. $crl = curl_init();
  218. $timeout = 5;
  219. curl_setopt($crl, CURLOPT_URL,$url);
  220. curl_setopt($crl, CURLOPT_RETURNTRANSFER, 1);
  221. curl_setopt($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
  222. $FileData = curl_exec($crl);
  223. $FileInfo = curl_getinfo($crl);
  224. curl_close($crl);
  225. if ($FileInfo['http_code'] != 200) {
  226. //there was an error
  227. JError::raiseWarning(0,$FileInfo['http_code'] . ' error for file:' . $url);
  228. return;
  229. }
  230. } else {
  231. //see if we can use fopen to get file
  232. $fopen_check = ini_get('allow_url_fopen') ;
  233. if (!empty($fopen_check)) {
  234. $FileData = file_get_contents($url);
  235. } else {
  236. JError::raiseWarning(0,JText::_('CURL_DISABLED'));
  237. return;
  238. }
  239. }
  240. if ($save) {
  241. jimport('joomla.installer.helper');
  242. $filename = JInstallerHelper::getFilenameFromURL($url);
  243. $config =& JFactory::getConfig();
  244. $target = $config->getValue('config.tmp_path').DS.JInstallerHelper::getFilenameFromURL($url);
  245. // Write buffer to file
  246. JFile::write($target, $FileData);
  247. if ($unpack) {
  248. $package = JInstallerHelper::unpack($target);
  249. ob_end_clean();
  250. return $package;
  251. } else {
  252. ob_end_clean();
  253. return $target;
  254. }
  255. } else {
  256. ob_end_clean();
  257. return $FileData;
  258. }
  259. }
  260. function getInstallXML($VersionCurrent)
  261. {
  262. //custom for development purposes / local use only; note do not commit your URL to SVN!!!
  263. $developmentURL = '';
  264. //get the install xml
  265. $url = (empty($developmentURL)) ? ($VersionCurrent=='SVN') ? 'http://jfusion.googlecode.com/svn/trunk/jfusion_install_'.$VersionCurrent.'.xml' : 'http://www.jfusion.org/jfusion_install_'.$VersionCurrent.'.xml' : $developmentURL;
  266. $VersionData = JFusionFunctionAdmin::getFileData($url);
  267. if (empty($VersionData)) {
  268. //try a mirror
  269. $url = (empty($developmentURL)) ? 'http://jfusion.googlecode.com/svn/branches/jfusion_install_'.$VersionCurrent.'.xml' : 'http://jfusion.googlecode.com/svn/trunk/jfusion_install_SVN.xml';
  270. $VersionData = JFusionFunctionAdmin::getFileData($url);
  271. if (empty($VersionData)) {
  272. //no data retrieval possible
  273. return;
  274. }
  275. }
  276. $parser = JFactory::getXMLParser('Simple');
  277. if ($parser->loadString($VersionData)) {
  278. if (isset($parser->document )) {
  279. $VersionParsed = $parser->document;
  280. unset($parser);
  281. return $VersionParsed;
  282. } else {
  283. unset($parser);
  284. return;
  285. }
  286. }
  287. }
  288. function currentVersion($includeRev = false)
  289. {
  290. //get the current JFusion version number
  291. $filename = JPATH_ADMINISTRATOR .DS.'components'.DS.'com_jfusion'.DS.'jfusion.xml';
  292. if (file_exists($filename) && is_readable($filename)) {
  293. //get the version number
  294. $parser = JFactory::getXMLParser('Simple');
  295. $parser->loadFile($filename);
  296. $VersionCurrent = $parser->document->version[0]->data();
  297. if($includeRev) {
  298. $RevisionCurrent = $parser->document->revision[0]->data();
  299. return array($VersionCurrent, $RevisionCurrent);
  300. } else {
  301. return $VersionCurrent;
  302. }
  303. }
  304. }
  305. function get_formatted_timediff($then, $now = false)
  306. {
  307. $INT_SECOND = 1;
  308. $INT_MINUTE = 60;
  309. $INT_HOUR = 3600;
  310. $INT_DAY = 86400;
  311. $INT_WEEK = 604800;
  312. if (!empty($now)) {
  313. $timediff = (time() - $then);
  314. } else {
  315. $timediff = $then;
  316. }
  317. $weeks = (int) intval($timediff / $INT_WEEK);
  318. $timediff = (int) intval($timediff - ($INT_WEEK * $weeks));
  319. $days = (int) intval($timediff / $INT_DAY);
  320. $timediff = (int) intval($timediff - ($INT_DAY * $days));
  321. $hours = (int) intval($timediff / $INT_HOUR);
  322. $timediff = (int) intval($timediff - ($INT_HOUR * $hours));
  323. $mins = (int) intval($timediff / $INT_MINUTE);
  324. $timediff = (int) intval($timediff - ($INT_MINUTE * $mins));
  325. $sec = (int) intval($timediff / $INT_SECOND);
  326. $timediff = (int) intval($timediff - ($sec * $INT_SECOND));
  327. $str = '';
  328. if ($weeks ) {
  329. $str .= intval($weeks);
  330. $str .= ($weeks > 1) ? ' weeks' : ' week';
  331. }
  332. if ($days ) {
  333. $str .= ($str) ? ', ' : '';
  334. $str .= intval($days);
  335. $str .= ($days > 1) ? ' days' : ' day';
  336. }
  337. if ($hours ) {
  338. $str .= ($str) ? ', ' : '';
  339. $str .= intval($hours);
  340. $str .= ($hours > 1) ? ' hours' : ' hour';
  341. }
  342. if ($mins ) {
  343. $str .= ($str) ? ', ' : '';
  344. $str .= intval($mins);
  345. $str .= ($mins > 1) ? ' minutes' : ' minute';
  346. }
  347. if ($sec ) {
  348. $str .= ($str) ? ', ' : '';
  349. $str .= intval($sec);
  350. $str .= ($sec > 1) ? ' seconds' : ' second';
  351. }
  352. if (!$weeks && !$days && !$hours && !$mins && !$sec ) {
  353. $str .= '0 seconds ';
  354. }
  355. return $str;
  356. }
  357. }