PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

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