PageRenderTime 31ms CodeModel.GetById 5ms RepoModel.GetById 1ms app.codeStats 0ms

/administrator/components/com_jce/models/updates.php

https://github.com/cladjidane/D-mo-HTML5-CSS3
PHP | 377 lines | 237 code | 67 blank | 73 comment | 59 complexity | 3d59116bf25249e01996eaa61cbe2a1a MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: updates.php 222 2011-06-11 17:32:06Z happy_noodle_boy $
  4. * @package JCE
  5. * @copyright Copyright © 2009-2011 Ryan Demmer. All rights reserved.
  6. * @copyright Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
  7. * @license GNU/GPL 2 or later
  8. * This version may have been modified pursuant
  9. * to the GNU General Public License, and as distributed it includes or
  10. * is derivative of works licensed under the GNU General Public License or
  11. * other free or open source software licenses.
  12. */
  13. // Check to ensure this file is included in Joomla!
  14. defined('_JEXEC') or die();
  15. // load base model
  16. require_once(dirname(__FILE__) . DS . 'model.php');
  17. class WFModelUpdates extends WFModel
  18. {
  19. var $url = 'https://www.joomlacontenteditor.net/index.php?option=com_updates&format=raw';
  20. function canUpdate()
  21. {
  22. if (!function_exists('curl_init')) {
  23. return function_exists('file_get_contents') && function_exists('ini_get') && ini_get('allow_url_fopen');
  24. }
  25. return true;
  26. }
  27. /**
  28. * Get extension versions
  29. * @return Array
  30. */
  31. function getVersions()
  32. {
  33. $db =JFactory::getDBO();
  34. // Get Component xml
  35. $com_xml = JApplicationHelper::parseXMLInstallFile(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_jce'.DS.'jce.xml');
  36. $mediabox_xml_file = WF_JOOMLA15 ? JPATH_PLUGINS.DS.'system'.DS.'jcemediabox.xml' : JPATH_PLUGINS.DS.'system'.DS.'jcemediabox'.DS.'jcemediabox.xml';
  37. if (file_exists($mediabox_xml_file)) {
  38. $mediabox_xml = JApplicationHelper::parseXMLInstallFile($mediabox_xml_file);
  39. } else {
  40. $mediabox_xml['version'] = 0; // return 0 for false
  41. }
  42. $versions = array(
  43. 'joomla' => array(
  44. 'com_jce' => $com_xml['version'],
  45. 'plg_jcemediabox' => $mediabox_xml['version']
  46. ),
  47. 'jce' => array()
  48. );
  49. $model = JModel::getInstance('plugins', 'WFModel');
  50. // get all plugins
  51. $plugins = $model->getPlugins();
  52. // get all extensions
  53. $extensions = $model->getExtensions();
  54. foreach ($plugins as $plugin) {
  55. if ($plugin->core == 0) {
  56. $file = WF_EDITOR_PLUGINS . DS . $plugin->name . DS . $plugin->name .'.xml';
  57. $xml = JApplicationHelper::parseXMLInstallFile($file);
  58. $versions['jce']['jce_' . $plugin->name] = $xml['version'];
  59. }
  60. }
  61. foreach ($extensions as $extension) {
  62. if ($extension->core == 0) {
  63. $file = WF_EDITOR_EXTENSIONS . DS . $extension->folder . DS . $extension->extension .'.xml';
  64. $xml = JApplicationHelper::parseXMLInstallFile($file);
  65. $versions['jce']['jce_' . $extension->folder . '_' . $extension->extension] = $xml['version'];
  66. }
  67. }
  68. return $versions;
  69. }
  70. /**
  71. * Check for extension updates
  72. * @return String JSON string of updates
  73. */
  74. function check()
  75. {
  76. $result = false;
  77. // Get all extensions and version numbers
  78. $data = array(
  79. 'task' => 'check'
  80. );
  81. wfimport('admin.helpers.extension');
  82. $component = WFExtensionHelper::getComponent();
  83. $params = new WFParameter($component->params, '', 'preferences');
  84. // get update key
  85. $key = $params->get('updates_key', '');
  86. $type = $params->get('updates_type', '');
  87. // encode it
  88. if (! empty($key)) {
  89. $data['key'] = urlencode($key);
  90. }
  91. if ($type) {
  92. $data['type'] = $type;
  93. }
  94. $req = array();
  95. // create request data
  96. foreach ($this->getVersions() as $type => $extension) {
  97. foreach ($extension as $item => $value) {
  98. $data[$type.'['.urlencode($item).']'] = urlencode($value);
  99. }
  100. }
  101. foreach ($data as $key => $value) {
  102. $req[] = $key.'='.urlencode($value);
  103. }
  104. // connect
  105. $result = $this->connect($this->url, implode('&', $req));
  106. return $result;
  107. }
  108. /**
  109. * Download update
  110. * @return String JSON string
  111. */
  112. function download()
  113. {
  114. jimport('joomla.filesystem.folder');
  115. jimport('joomla.filesystem.file');
  116. $config =JFactory::getConfig();
  117. $result = array('error'=>WFText::_('WF_UPDATES_DOWNLOAD_ERROR'));
  118. $id = JRequest::getInt('id');
  119. $vars = array(
  120. 'task' => 'download',
  121. 'id' => $id
  122. );
  123. $file = $this->connect($this->url, $vars);
  124. if ($file) {
  125. $data = json_decode($file);
  126. // get update file
  127. if ($data->name && $data->url && $data->hash) {
  128. $tmp = $config->getValue('config.tmp_path');
  129. // create path for package file
  130. $path = $tmp.DS.basename($data->name);
  131. // download file
  132. if ($this->connect($data->url, null, $path)) {
  133. if (JFile::exists($path) && @filesize($path) > 0) {
  134. // check hash and file type
  135. if ($data->hash == md5(md5_file($path)) && preg_match('/\.(zip|tar|gz)$/', $path)) {
  136. $result = array(
  137. 'file' => basename($path),
  138. 'hash' => $data->hash,
  139. 'installer' => $data->installer,
  140. 'type' => isset($data->type) ? $data->type : ''
  141. );
  142. } else {
  143. // fail and delete file
  144. $result = array('error'=> WFText::_('WF_UPDATES_ERROR_FILE_VERIFICATION_FAIL'));
  145. if (JFile::exists($path)) {
  146. @JFile::delete($path);
  147. }
  148. }
  149. } else {
  150. $result = array('error'=>WFText::_('WF_UPDATES_ERROR_FILE_MISSING_OR_INVALID'));
  151. }
  152. } else {
  153. $result = array('error'=>WFText::_('WF_UPDATES_DOWNLOAD_ERROR_DATA_TRANSFER'));
  154. }
  155. } else {
  156. $result = array('error'=>WFText::_('WF_UPDATES_DOWNLOAD_ERROR_MISSING_DATA'));
  157. }
  158. }
  159. return json_encode($result);
  160. }
  161. /**
  162. * Install extension update
  163. * @return String JSON string
  164. */
  165. function install()
  166. {
  167. jimport('joomla.installer.installer');
  168. jimport('joomla.installer.helper');
  169. jimport('joomla.filesystem.file');
  170. $config =JFactory::getConfig();
  171. $result = array('error'=>WFText::_('WF_UPDATES_INSTALL_ERROR'));
  172. // get vars
  173. $file = JRequest::getCmd('file');
  174. $hash = JRequest::getVar('hash', '', 'POST', 'alnum');
  175. $method = JRequest::getWord('installer');
  176. $type = JRequest::getWord('type');
  177. // check for vars
  178. if ($file && $hash && $method) {
  179. $tmp = $config->getValue('config.tmp_path');
  180. $path = $tmp.DS.$file;
  181. // check if file exists
  182. if (JFile::exists($path)) {
  183. // check hash
  184. if ($hash == md5(md5_file($path))) {
  185. if ($extract = JInstallerHelper::unpack($path)) {
  186. // get new Installer instance
  187. $installer = JInstaller::getInstance();
  188. // set installer adapter
  189. if ($method == 'jce') {
  190. // create jce plugin adapter
  191. $model = JModel::getInstance('installer', 'WFModel');
  192. $installer->setAdapter($extract['type'], $model->getAdapter($extract['type']));
  193. }
  194. // install
  195. if ($installer->install($extract['extractdir'])) {
  196. // get destination path
  197. $path = $installer->getPath('extension_root');
  198. // get manifest
  199. $manifest = basename($installer->getPath('manifest'));
  200. // delete update manifest if any eg: _iframes_155_156.xml
  201. if ($type == 'patch' && preg_match('/^_[0-9a-z_\.-]+\.xml$/', $manifest)) {
  202. if (JFile::exists($path.DS.$manifest)) {
  203. @JFile::delete($path.DS.$manifest);
  204. }
  205. }
  206. // installer message
  207. $result = array('error'=>'', 'text' => WFText::_($installer->get('message'), $installer->get('message')));
  208. }
  209. // cleanup package and extract dir
  210. if (JFile::exists($extract['extractdir'])) {
  211. @JFile::delete($extract['extractdir']);
  212. }
  213. if (JFolder::exists($extract['packagefile'])) {
  214. @JFolder::delete($extract['packagefile']);
  215. }
  216. } else {
  217. $result = array('error'=>WFText::_('WF_UPDATES_ERROR_FILE_EXTRACT_FAIL'));
  218. }
  219. } else {
  220. $result = array('error'=>WFText::_('WF_UPDATES_ERROR_FILE_VERIFICATION_FAIL'));
  221. }
  222. } else {
  223. $result = array('error'=>WFText::_('WF_UPDATES_ERROR_FILE_MISSING_OR_INVALID'));
  224. }
  225. }
  226. return json_encode($result);
  227. }
  228. /**
  229. * @copyright Copyright (C) 2009 Ryan Demmer. All rights reserved.
  230. * @copyright Copyright (C) 2006-2010 Nicholas K. Dionysopoulos
  231. * @param String $url URL to resource
  232. * @param Array $data [optional] Array of key value pairs
  233. * @param String $download [optional] path to file to write to
  234. * @return Mixed Boolean or JSON String on error
  235. */
  236. function connect($url, $data = '', $download = '')
  237. {
  238. $fp = false;
  239. @error_reporting(E_ERROR);
  240. // Use curl if it exists
  241. if (function_exists('curl_init')) {
  242. $ch = curl_init($url);
  243. curl_setopt($ch, CURLOPT_HEADER, 0);
  244. // Pretend we are IE7, so that webservers play nice with us
  245. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)');
  246. //curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
  247. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  248. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  249. // The @ sign allows the next line to fail if open_basedir is set or if safe mode is enabled
  250. @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  251. @curl_setopt($ch, CURLOPT_MAXREDIRS, 20);
  252. if (strpos($url, 'https://') !== false) {
  253. @curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
  254. @curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
  255. }
  256. if ($data && !$download) {
  257. curl_setopt($ch, CURLOPT_POST, 1);
  258. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  259. }
  260. // file download
  261. if ($download) {
  262. $fp = @fopen ($download, 'wb');
  263. @curl_setopt($ch, CURLOPT_FILE, $fp);
  264. }
  265. $result = curl_exec($ch);
  266. curl_close($ch);
  267. // close fopen handler
  268. if ($fp) {
  269. fclose($fp);
  270. }
  271. if (curl_errno($ch)) {
  272. return array('error' => 'CURL ERROR : '. curl_error($ch));
  273. }
  274. return $result;
  275. // use file_get_contents
  276. } else if (function_exists('file_get_contents')) {
  277. // check for ini_get and url_fopen support
  278. if(!function_exists('ini_get'))
  279. return false;
  280. if( !ini_get('allow_url_fopen') )
  281. return false;
  282. if ($download) {
  283. // use Joomla! installer function
  284. jimport('joomla.installer.helper');
  285. return @JInstallerHelper::downloadPackage($url, $download);
  286. } else {
  287. $options = array(
  288. 'http' => array(
  289. 'method' => 'POST',
  290. 'timeout' => 5,
  291. 'content' => $data
  292. )
  293. );
  294. $context = stream_context_create($options);
  295. return @file_get_contents($url, false, $context);
  296. }
  297. // error
  298. } else {
  299. return array('error' => WFText::_('WF_UPDATES_DOWNLOAD_ERROR_NO_CONNECT'));
  300. }
  301. return array('error' => WFText::_('WF_UPDATES_DOWNLOAD_ERROR_NO_CONNECT'));
  302. }
  303. function log($msg)
  304. {
  305. jimport('joomla.error.log');
  306. $log =JLog::getInstance('updates.txt');
  307. $log->addEntry(array('comment' => 'LOG: '.$msg));
  308. }
  309. }
  310. ?>