PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/kraymitchell/apex
PHP | 343 lines | 202 code | 68 blank | 73 comment | 58 complexity | f6c515ec5495496d308e8a086ef0c69f MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0, BSD-3-Clause, LGPL-2.1, GPL-3.0
  1. <?php
  2. /**
  3. * @package JCE
  4. * @copyright Copyright Š 2009-2011 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__) . DS . '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 = JApplicationHelper::parseXMLInstallFile(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_jce' . DS . '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 . DS . 'system' . DS . 'jcemediabox.xml' : JPATH_PLUGINS . DS . 'system' . DS . 'jcemediabox' . DS . 'jcemediabox.xml';
  35. // set mediabox version
  36. if(file_exists($mediabox_xml_file)) {
  37. $mediabox_xml = JApplicationHelper::parseXMLInstallFile($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 . DS . $plugin->name . DS . $plugin->name . '.xml';
  48. $xml = JApplicationHelper::parseXMLInstallFile($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 . DS . $extension->folder . DS . $extension->extension . '.xml';
  55. $xml = JApplicationHelper::parseXMLInstallFile($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 . DS . 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 . DS . $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 . DS . $manifest)) {
  180. @JFile::delete($path . DS . $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. if(JFile::exists($extract['extractdir'])) {
  188. @JFile::delete($extract['extractdir']);
  189. }
  190. if(JFolder::exists($extract['packagefile'])) {
  191. @JFolder::delete($extract['packagefile']);
  192. }
  193. } else {
  194. $result = array('error' => WFText::_('WF_UPDATES_ERROR_FILE_EXTRACT_FAIL'));
  195. }
  196. } else {
  197. $result = array('error' => WFText::_('WF_UPDATES_ERROR_FILE_VERIFICATION_FAIL'));
  198. }
  199. } else {
  200. $result = array('error' => WFText::_('WF_UPDATES_ERROR_FILE_MISSING_OR_INVALID'));
  201. }
  202. }
  203. return json_encode($result);
  204. }
  205. /**
  206. * @copyright Copyright (C) 2009 Ryan Demmer. All rights reserved.
  207. * @copyright Copyright (C) 2006-2010 Nicholas K. Dionysopoulos
  208. * @param String $url URL to resource
  209. * @param Array $data [optional] Array of key value pairs
  210. * @param String $download [optional] path to file to write to
  211. * @return Mixed Boolean or JSON String on error
  212. */
  213. function connect($url, $data ='', $download ='') {
  214. @error_reporting(E_ERROR);
  215. jimport('joomla.filesystem.file');
  216. $fp = false;
  217. $fopen = function_exists('file_get_contents') && function_exists('ini_get') && ini_get('allow_url_fopen');
  218. // try file_get_contents first (requires allow_url_fopen)
  219. if($fopen) {
  220. if ($download) {
  221. // use Joomla! installer function
  222. jimport('joomla.installer.helper');
  223. return @JInstallerHelper::downloadPackage($url, $download);
  224. } else {
  225. $options = array('http' => array('method' => 'POST', 'timeout' => 10, 'content' => $data));
  226. $context = stream_context_create($options);
  227. return @file_get_contents($url, false, $context);
  228. }
  229. // Use curl if it exists
  230. } else if(function_exists('curl_init')) {
  231. $ch = curl_init($url);
  232. curl_setopt($ch, CURLOPT_HEADER, 0);
  233. // Pretend we are IE7, so that webservers play nice with us
  234. 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)');
  235. //curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
  236. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
  237. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  238. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  239. // The @ sign allows the next line to fail if open_basedir is set or if safe mode is enabled
  240. @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  241. @curl_setopt($ch, CURLOPT_MAXREDIRS, 20);
  242. @curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  243. if($data && !$download) {
  244. curl_setopt($ch, CURLOPT_POST, 1);
  245. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  246. }
  247. // file download
  248. if($download) {
  249. $fp = @fopen($download, 'wb');
  250. @curl_setopt($ch, CURLOPT_FILE, $fp);
  251. }
  252. $result = curl_exec($ch);
  253. // file download
  254. if($download && $result === false) {
  255. die(json_encode( array('error' => 'TRANSFER ERROR : ' . curl_error($ch))));
  256. }
  257. curl_close($ch);
  258. // close fopen handler
  259. if($fp) {
  260. @fclose($fp);
  261. }
  262. return $result;
  263. // error - no update support
  264. } else {
  265. return array('error' => WFText::_('WF_UPDATES_DOWNLOAD_ERROR_NO_CONNECT'));
  266. }
  267. return array('error' => WFText::_('WF_UPDATES_DOWNLOAD_ERROR_NO_CONNECT'));
  268. }
  269. function log($msg) {
  270. jimport('joomla.error.log');
  271. $log = JLog::getInstance('updates.txt');
  272. $log->addEntry( array('comment' => 'LOG: ' . $msg));
  273. }
  274. }
  275. ?>