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

/administrator/components/com_sef/models/upgrade.php

https://bitbucket.org/organicdevelopment/joomla-2.5
PHP | 428 lines | 308 code | 74 blank | 46 comment | 70 complexity | 8c7b4d3fd9c5984c2333e01f168c0fe5 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, MIT, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * SEF component for Joomla!
  4. *
  5. * @package JoomSEF
  6. * @version 4.2.8
  7. * @author ARTIO s.r.o., http://www.artio.net
  8. * @copyright Copyright (C) 2012 ARTIO s.r.o.
  9. * @license GNU/GPLv3 http://www.artio.net/license/gnu-general-public-license
  10. */
  11. // Check to ensure this file is included in Joomla!
  12. defined('_JEXEC') or die();
  13. jimport('joomla.application.component.model');
  14. jimport('joomla.filesystem.file');
  15. jimport('joomla.filesystem.folder');
  16. jimport('joomla.installer.helper');
  17. jimport('joomla.installer.installer');
  18. jimport('joomla.application.helper');
  19. require_once JPATH_ADMINISTRATOR.DS.'components'.DS.'com_sef'.DS.'classes'.DS.'seftools.php';
  20. class SEFModelUpgrade extends JModel
  21. {
  22. function getUpgradeExts()
  23. {
  24. if (!isset($this->_upgradeExts)) {
  25. $this->_loadVersions();
  26. $basedir = JPATH_ROOT.DS.'components'.DS.'com_sef'.DS.'sef_ext';
  27. $extensions = array();
  28. if( count($this->_extVersions) > 0 ) {
  29. foreach( $this->_extVersions as $key => $ext ) {
  30. $option=str_replace('ext_joomsef4_','com_',$ext->name);
  31. $xmlfile = $basedir.DS.$option.'.xml';
  32. if (!JFile::exists($xmlfile)) {
  33. continue;
  34. }
  35. $xml=JFactory::getXML($xmlfile);
  36. if (!$xml) {
  37. unset($xml);
  38. continue;
  39. }
  40. if (!is_object($xml) ||
  41. ($xml->name() != 'extension') ||
  42. version_compare($xml->getAttribute('version'), '1.6', '<') ||
  43. ($xml->getAttribute('type') != 'sef_ext' ))
  44. {
  45. unset($xml);
  46. continue;
  47. }
  48. $extension = new stdClass();
  49. $extension->new = $ext->version;
  50. $extension->type = $ext->type;
  51. $extension->option = $key;
  52. $extension->params = SEFTools::getExtParams($key);
  53. $element = $xml->name;
  54. $extension->name = $element ? $element->data() : '';
  55. $element = $xml->version;
  56. $extension->old = $element ? $element->data() : '';
  57. $extensions[$ext->name] = $extension;
  58. }
  59. }
  60. $this->_upgradeExts = $extensions;
  61. }
  62. return $this->_upgradeExts;
  63. }
  64. function getNewSEFVersion()
  65. {
  66. if( !isset($this->_newSEFVersion) ) {
  67. $this->_loadVersions();
  68. $this->_newSEFVersion=$this->_extVersions['com_joomsef4']->version;
  69. }
  70. return $this->_newSEFVersion;
  71. }
  72. function getRegisteredInfo()
  73. {
  74. if (!isset($this->_regInfo) ) {
  75. $regInfo = new stdClass();
  76. $sefConfig =& SEFConfig::getConfig();
  77. if( trim($sefConfig->artioDownloadId) != '' ) {
  78. // Send the request to ARTIO server to check registration
  79. $data = array('download_id' => trim($sefConfig->artioDownloadId),'cat'=>'com_joomsef4');
  80. $response = SEFTools::PostRequest($sefConfig->serverLicenser, null, $data);
  81. if( ($response === false) || ($response->code != 200) ) {
  82. JError::raiseNotice(100, JText::_('COM_SEF_ERROR_REG_CHECK_FAIL'));
  83. return null;
  84. }
  85. else {
  86. // Parse the response - get individual lines
  87. $lines = explode("\n", $response->content);
  88. // Get the code
  89. $pos = strpos($lines[0], ' ');
  90. if( $pos === false ) {
  91. JError::raiseNotice(100, JText::_('COM_SEF_ERROR_REG_CHECK_FAIL'));
  92. return null;
  93. }
  94. $regInfo->code = intval(substr($lines[0], 0, $pos));
  95. if( ($regInfo->code == 10) || ($regInfo->code == 20) ) {
  96. // Download id found
  97. if (count($lines) < 3) {
  98. // Wrong response
  99. JError::raiseNotice(100, JText::_('COM_SEF_ERROR_REG_CHECK_FAIL'));
  100. return null;
  101. }
  102. // Parse the date
  103. $date =& JFactory::getDate(str_replace('.', '/', trim($lines[1])));
  104. $regInfo->date = $date->format(JText::_('DATE_FORMAT_LC3'));
  105. // Parse the name
  106. $regInfo->name = trim($lines[2]);
  107. // Parse the company
  108. $regInfo->company = isset($lines[3]) ? trim($lines[3]) : '';
  109. // Is upgrade expired?
  110. if ($regInfo->code == 20) {
  111. JError::raiseNotice(100, JText::sprintf('COM_SEF_EXPIRED', 'Artio JoomSEF 4') . ' ' . JText::_('COM_SEF_INFO_YOU_GET_FREE_VERSION'));
  112. }
  113. }
  114. // Is upgrade inactive
  115. else if ($regInfo->code == 30) {
  116. JError::raiseNotice(100, JText::sprintf('COM_SEF_NOT_ACTIVATED', 'Artio JoomSEF 4') . ' ' . JText::_('COM_SEF_INFO_YOU_GET_FREE_VERSION'));
  117. $regInfo->date = JText::_('COM_SEF_NOT_ACTIVATED_YET');
  118. }
  119. else if($regInfo->code==40) {
  120. JError::raiseNotice(100,JText::sprintf('COM_SEF_ERR_DOMAIN_NOT_MATCH','Artio JoomSEF 4').' '.JText::_('COM_SEF_INFO_YOU_GET_FREE_VERSION'));
  121. return null;
  122. }
  123. else if($regInfo->code==50) {
  124. JError::raiseNotice(100,JText::sprintf('COM_SEF_DOWLOAD_ID_INVALID','Artio JoomSEF 4').' '.JText::_('COM_SEF_INFO_YOU_GET_FREE_VERSION'));
  125. return null;
  126. }
  127. else if( $regInfo->code == 90 ) {
  128. // Download id not found, do nothing
  129. JError::raiseNotice(100, JText::sprintf('COM_SEF_ERROR_DOWNLOAD_ID_NOT_FOUND',trim($sefConfig->artioDownloadId)). ' ' . JText::_('COM_SEF_INFO_YOU_GET_FREE_VERSION'));
  130. }
  131. else {
  132. // Wrong response
  133. JError::raiseNotice(100, JText::_('COM_SEF_ERROR_REG_CHECK_FAIL'));
  134. return null;
  135. }
  136. }
  137. }
  138. else {
  139. // Download ID not set
  140. $link1 = '<a href="index.php?option=com_sef&controller=config&task=edit&tab=registration">';
  141. $link2 = '</a>';
  142. $enterIdText = sprintf(JText::_('COM_SEF_INFO_ENTER_DOWNLOAD_ID'), $link1, $link2);
  143. JError::raiseNotice(100, JText::_('COM_SEF_DOWNLOAD_ID_NOT_SET') . ' ' . JText::_('COM_SEF_INFO_YOU_GET_FREE_VERSION') . ' ' . $enterIdText);
  144. return null;
  145. }
  146. $this->_regInfo = $regInfo;
  147. }
  148. return $this->_regInfo;
  149. }
  150. function _loadVersions()
  151. {
  152. if (!isset($this->_extVersions)) {
  153. $sefConfig =& SEFConfig::getConfig();
  154. $this->_extVersions = array();
  155. // Get the response from server
  156. $response = SEFTools::PostRequest($sefConfig->serverNewVersionURL);
  157. // Check the response
  158. if( ($response === false) || ($response->code != 200) ) {
  159. JError::raiseNotice(100, JText::_('COM_SEF_ERROR_NO_VERSION_INFO'));
  160. // Set dummy data
  161. $ext = new stdClass();
  162. $ext->version = '?.?.?';
  163. $ext->name = 'JoomSEF';
  164. $this->_extVersions['com_joomsef4'] = $ext;
  165. }
  166. else {
  167. $versions = $response->content;
  168. $xml = JFactory::getXML($versions,false);
  169. if (is_object($xml)) {
  170. foreach ($xml->children() as $package) {
  171. $ext = new stdClass();
  172. $ext->name = $package->element->data();
  173. $ext->version = $package->version->data();
  174. if (isset($package->buyURL)) {
  175. $ext->type = 'Paid';
  176. $ext->link = $package->buyURL->data();
  177. } else {
  178. $ext->type = 'Free';
  179. $ext->link = '';
  180. }
  181. $this->_extVersions[$ext->name]=$ext;
  182. }
  183. }
  184. }
  185. }
  186. }
  187. function &getVersions()
  188. {
  189. $this->_loadVersions();
  190. return $this->_extVersions;
  191. }
  192. function getIsPaidVersion()
  193. {
  194. if( !isset($this->_isPaidVersion) ) {
  195. $check = SEFTools::GetSEFGlobalMeta();
  196. $ctrl = md5(implode(file(JPATH_ROOT.DS.'administrator'.DS.'components'.DS.'com_sef'.DS.'sef.xml')));
  197. $this->_isPaidVersion = ($check == $ctrl);
  198. }
  199. return $this->_isPaidVersion;
  200. }
  201. function upgrade()
  202. {
  203. $extDir = JPATH_ROOT.DS.'components'.DS.'com_sef'.DS.'sef_ext';
  204. JFActory::getLanguage()->load('com_installer',JPATH_ADMINISTRATOR);
  205. $fromServer = JRequest::getVar('fromserver');
  206. $extension = JRequest::getVar('ext');
  207. if( is_null($fromServer) ) {
  208. $this->setState('message', JText::_('COM_SEF_ERROR_UPGRADE_SOURCE'));
  209. return false;
  210. }
  211. if( $fromServer == 1 ) {
  212. $package = $this->_getPackageFromServer($extension);
  213. } else {
  214. $package = $this->_getPackageFromUpload();
  215. }
  216. // was the package unpacked?
  217. if (!$package) {
  218. $this->setState('message', 'Unable to find install package.');
  219. return false;
  220. }
  221. $xmls=JFolder::files($package['extractdir'],'.xml');
  222. $xmlfile=$xmls[0];
  223. $xml=JFactory::getXML($package['extractdir'].DS.$xmlfile);
  224. $installer=JInstaller::getInstance();
  225. JTable::addIncludePath(JPATH_LIBRARIES.DS.'joomla'.DS.'database'.DS.'table');
  226. if(!$installer->update($package['dir'])) {
  227. $msg=JText::sprintf('COM_INSTALLER_MSG_UPDATE_ERROR', JText::_('COM_INSTALLER_TYPE_TYPE_'.strtoupper($package['type'])));
  228. $result=false;
  229. } else {
  230. $msg=JText::sprintf('COM_INSTALLER_MSG_UPDATE_SUCCESS', JText::_('COM_INSTALLER_TYPE_TYPE_'.strtoupper($package['type'])));
  231. $result=true;
  232. }
  233. $this->setState('message',$msg);
  234. if (!is_file($package['packagefile'])) {
  235. $config=JFactory::getConfig();
  236. $package['packagefile']=$config->get('tmp_path').'/'.$package['packagefile'];
  237. }
  238. JInstallerHelper::cleanupInstall($package['packagefile'],$package['extractdir']);
  239. return $result;
  240. }
  241. function _getPackageFromUpload()
  242. {
  243. // Get the uploaded file information
  244. $userfile = JRequest::getVar('install_package', null, 'files', 'array' );
  245. // Make sure that file uploads are enabled in php
  246. if (!(bool) ini_get('file_uploads')) {
  247. JError::raiseWarning(100, JText::_('COM_SEF_WARNINSTALLFILE'));
  248. return false;
  249. }
  250. // Make sure that zlib is loaded so that the package can be unpacked
  251. if (!extension_loaded('zlib')) {
  252. JError::raiseWarning(100, JText::_('COM_SEF_WARNINSTALLZLIB'));
  253. return false;
  254. }
  255. // If there is no uploaded file, we have a problem...
  256. if (!is_array($userfile) ) {
  257. JError::raiseWarning(100, JText::_('COM_SEF_NO_FILE_SELECTED'));
  258. return false;
  259. }
  260. // Check if there was a problem uploading the file.
  261. if ( $userfile['error'] || $userfile['size'] < 1 )
  262. {
  263. JError::raiseWarning(100, JText::_('COM_SEF_WARNINSTALLUPLOADERROR'));
  264. return false;
  265. }
  266. // Build the appropriate paths
  267. $config =& JFactory::getConfig();
  268. $tmp_dest = $config->getValue('config.tmp_path').DS.$userfile['name'];
  269. $tmp_src = $userfile['tmp_name'];
  270. // Move uploaded file
  271. jimport('joomla.filesystem.file');
  272. $uploaded = JFile::upload($tmp_src, $tmp_dest);
  273. // Unpack the downloaded package file
  274. $package = JInstallerHelper::unpack($tmp_dest);
  275. // Delete the package file
  276. JFile::delete($tmp_dest);
  277. return $package;
  278. }
  279. function _getPackageFromServer($extension)
  280. {
  281. // Make sure that zlib is loaded so that the package can be unpacked
  282. if (!extension_loaded('zlib')) {
  283. JError::raiseWarning(100, JText::_('COM_SEF_WARNINSTALLZLIB'));
  284. return false;
  285. }
  286. // build the appropriate paths
  287. $sefConfig =& SEFConfig::getConfig();
  288. $config =& JFactory::getConfig();
  289. if( strlen($extension)==0 ) {
  290. $tmp_dest = $config->getValue('config.tmp_path').DS.'joomsef.zip';
  291. }
  292. else {
  293. $tmp_dest = $config->getValue('config.tmp_path').DS.$extension.'.zip';
  294. }
  295. // Validate the upgrade on server
  296. $data = array();
  297. $data['username'] = $sefConfig->artioUserName;
  298. $data['password'] = $sefConfig->artioPassword;
  299. if( strlen($extension)==0 ) {
  300. $data['download_id'] = $sefConfig->artioDownloadId;
  301. $data['file'] = 'com_joomsef4';
  302. }
  303. else {
  304. $params =& SEFTools::getExtParams($extension);
  305. $data['download_id'] = $params->get('downloadId', '');
  306. $data['file'] = $extension;
  307. }
  308. $uri = parse_url(JURI::root());
  309. $url = $uri['host'].$uri['path'];
  310. $url = trim($url, '/');
  311. $data['site'] = $url;
  312. $data['ip'] = $_SERVER['SERVER_ADDR'];
  313. $lang =& JFactory::getLanguage();
  314. $data['lang'] = $lang->getTag();
  315. $data['cat'] = 'joomsef4';
  316. // Get the server response
  317. $response = SEFTools::PostRequest($sefConfig->serverAutoUpgrade, JURI::root(), $data);
  318. // Check the response
  319. if( ($response === false) || ($response->code != 200) ) {
  320. JError::raiseWarning(100, JText::_('COM_SEF_ERROR_SERVER_CONNECTION'));
  321. return false;
  322. }
  323. // Response OK, check what we got
  324. if( strpos($response->header, 'Content-Type: application/zip') === false ) {
  325. JError::raiseWarning(100, $response->content);
  326. return false;
  327. }
  328. // Seems we got the ZIP installation package, let's save it to disk
  329. if (!JFile::write($tmp_dest, $response->content)) {
  330. JError::raiseWarning(100, JText::_('COM_SEF_ERROR_TEMP_DIRECTORY'));
  331. return false;
  332. }
  333. // Unpack the downloaded package file
  334. $package = JInstallerHelper::unpack($tmp_dest);
  335. // Delete the package file
  336. JFile::delete($tmp_dest);
  337. return $package;
  338. }
  339. function _getXmlText($file, $variable)
  340. {
  341. // try to find variable
  342. $value = null;
  343. if (JFile::exists($file)) {
  344. $xml =& JFactory::getXMLParser('Simple');
  345. if ($xml->loadFile($file)) {
  346. $root =& $xml->document;
  347. $element =& $root->getElementByPath($variable);
  348. $value = $element ? $element->data() : '';
  349. }
  350. }
  351. return $value;
  352. }
  353. }
  354. ?>