/administrator/components/com_jfusion/models/model.jfusionadmin.php
PHP | 397 lines | 296 code | 40 blank | 61 comment | 54 complexity | 7570dcc77d8af732be7d618876804c08 MD5 | raw file
Possible License(s): Apache-2.0
- <?php
-
- /**
- * @package JFusion
- * @subpackage Models
- * @author JFusion development team
- * @copyright Copyright (C) 2008 JFusion. All rights reserved.
- * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
- */
-
- // no direct access
- defined('_JEXEC' ) or die('Restricted access' );
-
- /**
- * Class for general JFusion functions for the backend
- * @package JFusion
- */
-
- class JFusionFunctionAdmin{
- /**
- * Saves the posted JFusion component variables
- * @param string $jname name of the JFusion plugin used
- * @param array $post Array of JFusion plugin parameters posted to the JFusion component
- * @param boolean $wizard Notes if function was called by wizardresult();*
- * @return true|false returns true if succesful and false if an error occurred
- */
-
- function saveParameters($jname, $post, $wizard = false)
- {
- $db = & JFactory::getDBO();
-
- if ($wizard) {
- //data submitted by the wizard so merge the data with existing params if they do indeed exist
- $query = "SELECT params FROM #__jfusion WHERE name = " . $db->Quote($jname);
- $db->setQuery($query);
- $existing_serialized = $db->loadResult();
- if (!empty($existing_serialized)) {
- $existing_params = unserialize(base64_decode($existing_serialized));
- if (is_array($existing_params)) {
- $post = array_merge($existing_params, $post);
- }
- }
- }
-
- //serialize the $post to allow storage in a SQL field
- $serialized = base64_encode(serialize($post));
-
- //set the current parameters in the jfusion table
- $query = 'UPDATE #__jfusion SET params = ' . $db->Quote($serialized) .' WHERE name = ' . $db->Quote($jname);
- $db->setQuery($query );
-
- if (!$db->query()) {
- //there was an error saving the parameters
- JError::raiseWarning(0,$db->stderr());
- $result = false;
- return $result;
- }
-
- //reset the params instance for this plugin
- JFusionFactory::getParams($jname,true);
-
- $result = true;
- return $result;
- }
-
- /**
- * Checks to see if the JFusion plugins are installed and enabled
- */
- function checkPlugin()
- {
- $userPlugin = true;
- $authPlugin = true;
- if (!JFusionFunctionAdmin::isPluginInstalled('jfusion','authentication', false)) {
- JError::raiseWarning(0,JText::_('FUSION_MISSING_AUTH'));
- $authPlugin = false;
- }
-
- if (!JFusionFunctionAdmin::isPluginInstalled('jfusion','user', false)) {
- JError::raiseWarning(0,JText::_('FUSION_MISSING_USER'));
- $userPlugin = false;
- }
-
- if ($authPlugin && $userPlugin) {
- $jAuth = JFusionFunctionAdmin::isPluginInstalled('jfusion','user',true);
- $jUser = JFusionFunctionAdmin::isPluginInstalled('jfusion','authentication',true);
- if (!$jAuth) {
- JError::raiseNotice(0,JText::_('FUSION_READY_TO_USE_AUTH'));
- }
- if (!$jUser) {
- JError::raiseNotice(0,JText::_('FUSION_READY_TO_USE_USER'));
- }
- }
- }
-
-
- /**
- * Tests if a plugin is installed with the specified name, where folder is the type (e.g. user)
- * @param string $element element name of the plugin
- * @param string $folder folder name of the plugin
- * @param integer $testPublished Variable to determine if the function should test to see if the plugin is published
- * @return true|false returns true if succesful and false if an error occurred
- */
- function isPluginInstalled($element,$folder, $testPublished)
- {
- $db =& JFactory::getDBO();
- $query = 'SELECT published FROM #__plugins WHERE element=' . $db->Quote($element) . ' AND folder=' . $db->Quote($folder);
- $db->setQuery($query);
- $result = $db->loadObject();
- if ($result) {
- if ($testPublished) {
- return($result->published == 1);
- } else {
- $result = true;
- return $result;
- }
- } else {
- $result = false;
- return $result;
- }
- }
-
- /**
- * Parses phpinfo into an array
- * @return array
- */
- function phpinfo_array()
- {
- //get the phpinfo and parse it into an array
- ob_start();
- phpinfo();
- $phpinfo = array('phpinfo' => array());
- 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)) {
- foreach ($matches as $match) {
- if (strlen($match[1])) {
- $phpinfo[$match[1]] = array();
- } else if (isset($match[3])) {
- $phpinfo[end(array_keys($phpinfo))][$match[2]] = isset($match[4]) ? array($match[3], $match[4]) : $match[3];
- } else {
- $phpinfo[end(array_keys($phpinfo))][] = $match[2];
- }
- }
- }
- return $phpinfo;
- }
-
- /**
- * Checks if url is valid using cURL
- * @param $url
- * @return error code if any
- */
- function checkURL($url)
- {
-
- $header_codes = array(100 => 'Continue',
- 101 => 'Switching Protocols',
- 102 => 'Processing',
-
- 200 => 'OK',
- 201 => 'Created',
- 202 => 'Accepted',
- 203 => 'Non-Authoritative Information',
- 204 => 'No Content',
- 205 => 'Reset Content',
- 206 => 'Partial Content',
- 207 => 'Multi-Status',
- 226 => 'IM Used',
-
- 300 => 'Multiple Choices',
- 301 => 'Moved Permanently',
- 302 => 'Found',
- 303 => 'See Other',
- 304 => 'Not Modified',
- 305 => 'Use Proxy',
- 306 => 'Reserved',
- 307 => 'Temporary Redirect',
-
- 400 => 'Bad Request',
- 401 => 'Unauthorized',
- 402 => 'Payment Required',
- 403 => 'Forbidden',
- 404 => 'Not Found',
- 405 => 'Method Not Allowed',
- 406 => 'Not Acceptable',
- 407 => 'Proxy Authentication Required',
- 408 => 'Request Timeout',
- 409 => 'Conflict',
- 410 => 'Gone',
- 411 => 'Length Required',
- 412 => 'Precondition Failed',
- 413 => 'Request Entity Too Large',
- 414 => 'Request-URI Too Long',
- 415 => 'Unsupported Media Type',
- 416 => 'Requested Range Not Satisfiable',
- 417 => 'Expectation Failed',
- 422 => 'Unprocessable Entity',
- 423 => 'Locked',
- 424 => 'Failed Dependency',
- 426 => 'Upgrade Required',
-
- 500 => 'Internal Server Error',
- 501 => 'Not Implemented',
- 502 => 'Bad Gateway',
- 503 => 'Service Unavailable',
- 504 => 'Gateway Timeout',
- 505 => 'HTTP Version Not Supported',
- 506 => 'Variant Also Negotiates',
- 507 => 'Insufficient Storage',
- 510 => 'Not Extended'
- );
-
- ob_start();
- if (function_exists('curl_init')) {
- //curl is the preferred function
- $crl = curl_init();
- $timeout = 5;
- curl_setopt($crl, CURLOPT_URL,$url);
- curl_setopt($crl, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
- $FileData = curl_exec($crl);
- $FileInfo = curl_getinfo($crl);
- curl_close($crl);
- if ($FileInfo['http_code'] >= 400) {
- //there was an error
- return $FileInfo['http_code'] . ': ' . $header_codes[$FileInfo['http_code']]. ' error for url ' . $url;
- }
- } else {
- return JText::_('CURL_DISABLED');
- }
- return null;
- }
- /**
- * Parses remote xml file
- * @param $url
- * @return Joomla xml object
- */
- function getFileData($url,$save = 0, $unpack = 0)
- {
- ob_start();
- if (function_exists('curl_init')) {
- //curl is the preferred function
- $crl = curl_init();
- $timeout = 5;
- curl_setopt($crl, CURLOPT_URL,$url);
- curl_setopt($crl, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
- $FileData = curl_exec($crl);
- $FileInfo = curl_getinfo($crl);
- curl_close($crl);
- if ($FileInfo['http_code'] != 200) {
- //there was an error
- JError::raiseWarning(0,$FileInfo['http_code'] . ' error for file:' . $url);
- return;
- }
- } else {
- //see if we can use fopen to get file
- $fopen_check = ini_get('allow_url_fopen') ;
- if (!empty($fopen_check)) {
- $FileData = file_get_contents($url);
- } else {
- JError::raiseWarning(0,JText::_('CURL_DISABLED'));
- return;
- }
- }
-
- if ($save) {
- jimport('joomla.installer.helper');
- $filename = JInstallerHelper::getFilenameFromURL($url);
- $config =& JFactory::getConfig();
- $target = $config->getValue('config.tmp_path').DS.JInstallerHelper::getFilenameFromURL($url);
- // Write buffer to file
- JFile::write($target, $FileData);
- if ($unpack) {
- $package = JInstallerHelper::unpack($target);
- ob_end_clean();
- return $package;
- } else {
- ob_end_clean();
- return $target;
- }
- } else {
- ob_end_clean();
- return $FileData;
- }
- }
-
- function getInstallXML($VersionCurrent)
- {
- //custom for development purposes / local use only; note do not commit your URL to SVN!!!
- $developmentURL = '';
-
- //get the install xml
- $url = (empty($developmentURL)) ? ($VersionCurrent=='SVN') ? 'http://jfusion.googlecode.com/svn/trunk/jfusion_install_'.$VersionCurrent.'.xml' : 'http://www.jfusion.org/jfusion_install_'.$VersionCurrent.'.xml' : $developmentURL;
- $VersionData = JFusionFunctionAdmin::getFileData($url);
- if (empty($VersionData)) {
- //try a mirror
- $url = (empty($developmentURL)) ? 'http://jfusion.googlecode.com/svn/branches/jfusion_install_'.$VersionCurrent.'.xml' : 'http://jfusion.googlecode.com/svn/trunk/jfusion_install_SVN.xml';
- $VersionData = JFusionFunctionAdmin::getFileData($url);
- if (empty($VersionData)) {
- //no data retrieval possible
- return;
- }
- }
- $parser = JFactory::getXMLParser('Simple');
- if ($parser->loadString($VersionData)) {
- if (isset($parser->document )) {
- $VersionParsed = $parser->document;
- unset($parser);
- return $VersionParsed;
- } else {
- unset($parser);
- return;
- }
- }
- }
-
- function currentVersion($includeRev = false)
- {
- //get the current JFusion version number
- $filename = JPATH_ADMINISTRATOR .DS.'components'.DS.'com_jfusion'.DS.'jfusion.xml';
- if (file_exists($filename) && is_readable($filename)) {
- //get the version number
- $parser = JFactory::getXMLParser('Simple');
- $parser->loadFile($filename);
- $VersionCurrent = $parser->document->version[0]->data();
-
- if($includeRev) {
- $RevisionCurrent = $parser->document->revision[0]->data();
- return array($VersionCurrent, $RevisionCurrent);
- } else {
- return $VersionCurrent;
- }
- }
- }
-
-
- function get_formatted_timediff($then, $now = false)
- {
-
- $INT_SECOND = 1;
- $INT_MINUTE = 60;
- $INT_HOUR = 3600;
- $INT_DAY = 86400;
- $INT_WEEK = 604800;
-
- if (!empty($now)) {
- $timediff = (time() - $then);
- } else {
- $timediff = $then;
- }
- $weeks = (int) intval($timediff / $INT_WEEK);
- $timediff = (int) intval($timediff - ($INT_WEEK * $weeks));
- $days = (int) intval($timediff / $INT_DAY);
- $timediff = (int) intval($timediff - ($INT_DAY * $days));
- $hours = (int) intval($timediff / $INT_HOUR);
- $timediff = (int) intval($timediff - ($INT_HOUR * $hours));
- $mins = (int) intval($timediff / $INT_MINUTE);
- $timediff = (int) intval($timediff - ($INT_MINUTE * $mins));
- $sec = (int) intval($timediff / $INT_SECOND);
- $timediff = (int) intval($timediff - ($sec * $INT_SECOND));
-
- $str = '';
- if ($weeks ) {
- $str .= intval($weeks);
- $str .= ($weeks > 1) ? ' weeks' : ' week';
- }
-
- if ($days ) {
- $str .= ($str) ? ', ' : '';
- $str .= intval($days);
- $str .= ($days > 1) ? ' days' : ' day';
- }
-
- if ($hours ) {
- $str .= ($str) ? ', ' : '';
- $str .= intval($hours);
- $str .= ($hours > 1) ? ' hours' : ' hour';
- }
-
- if ($mins ) {
- $str .= ($str) ? ', ' : '';
- $str .= intval($mins);
- $str .= ($mins > 1) ? ' minutes' : ' minute';
- }
-
- if ($sec ) {
- $str .= ($str) ? ', ' : '';
- $str .= intval($sec);
- $str .= ($sec > 1) ? ' seconds' : ' second';
- }
-
- if (!$weeks && !$days && !$hours && !$mins && !$sec ) {
- $str .= '0 seconds ';
- }
-
- return $str;
- }
- }