PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/mc/rt_missioncontrol_j15/lib/updater/rokupdater.class.php

https://github.com/bhar1red/anahita
PHP | 300 lines | 214 code | 66 blank | 20 comment | 21 complexity | 61fa69746eca20f02f1dc65bda697393 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @version ? 1.5.0 April 26, 2011
  4. * @author ? ?RocketTheme http://www.rockettheme.com
  5. * @copyright Copyright (C) 2007 - 2011 RocketTheme, LLC
  6. * @license ? http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
  7. */
  8. // no direct access
  9. defined('_JEXEC') or die('Restricted index access');
  10. define('ROKUPDATER_DLMETHOD_FOPEN', 0);
  11. define('ROKUPDATER_DLMETHOD_CURL', 1);
  12. define('ROKUPDATER_VERSION', 1.0);
  13. define('ROKUPDATER_EXTRACTOR_16', 0);
  14. define('ROKUPDATER_EXTRACTOR_15', 1);
  15. define('ROKUPDATER_EXTRACTOR_PEAR', 2);
  16. require_once(dirname(__FILE__).'/libraries/installer/RokStarInstaller.php');
  17. class RokUpdater
  18. {
  19. const ROKUPDATER_DLMETHOD_FOPEN = 0;
  20. const ROKUPDATER_DLMETHOD_CURL = 1;
  21. const ROKUPDATER_VERSION = 1.0;
  22. const ROKUPDATER_EXTRACTOR_16 = 0;
  23. const ROKUPDATER_EXTRACTOR_15 = 1;
  24. const ROKUPDATER_EXTRACTOR_PEAR = 2;
  25. var $current_version;
  26. var $tmp_path;
  27. var $details_url;
  28. var $details_path;
  29. var $details_cache_time;
  30. var $download_method;
  31. var $params;
  32. var $extractor;
  33. var $versions_path;
  34. var $versions_name;
  35. var $slug;
  36. function __construct()
  37. {
  38. //turn OFF php errors, we'll handle them prettily
  39. //error_reporting(0);
  40. }
  41. function init($details_url, $slug, $params, $download_method = ROKUPDATER_DLMETHOD_FOPEN, $extractor_method = ROKUPDATER_EXTRACTOR_16)
  42. {
  43. $config =& JFactory::getConfig();
  44. $this->tmp_path = $config->getValue('config.tmp_path');
  45. $this->details_url = $details_url;
  46. $this->slug = $slug;
  47. $this->download_method = $download_method;
  48. $this->extractor = $extractor_method;
  49. $this->params = $params;
  50. $this->details_cache_time = 1200;
  51. $this->details_path = $this->tmp_path . DS . basename($details_url);
  52. $this->versions_path = JPATH_ROOT . DS . 'media' . DS . 'rokupdater' . DS . $slug . '-version.json';
  53. }
  54. function updateAvailable()
  55. {
  56. $params =& $this->params;
  57. RokUpdater::updateVersion($params->get('update_name'), $params->get('update_slug'), $params->get('current_version'), $this->details_url);
  58. // read version file
  59. $version = json_decode(JFile::read($this->versions_path));
  60. $this->current_version = $version->current_version;
  61. $alldetails = $this->_decodeJSONData($this->details_path);
  62. foreach ($alldetails as $key => $details) {
  63. if ($key == $version->slug) {
  64. $details->current_version = $this->current_version;
  65. foreach ($details->versions as $newversion) {
  66. if(version_compare($this->current_version,$newversion->minimum_version,'>=') &&
  67. version_compare($newversion->version, $this->current_version,'>')) {
  68. $details->updates = true;
  69. $details->version = $newversion->version;
  70. } else {
  71. $details->updates = false;
  72. }
  73. return $details;
  74. }
  75. }
  76. }
  77. return false;
  78. }
  79. function _decodeJSONData($path)
  80. {
  81. // determine if file should be refreshed
  82. $filemtime = @filemtime($path);
  83. // if file doesn't exist or expiration passed
  84. if (!$filemtime or (time() - $filemtime >= $this->details_cache_time)) {
  85. $result = RokUpdater::downloadFile($this->details_url, $this->details_path);
  86. if (is_object($result)) {
  87. HTML_RokError::showError('Download Failed: ' . $result->message . '(' . $result->number . ')');
  88. return false;
  89. }
  90. }
  91. $details = json_decode(JFile::read($this->details_path));
  92. return $details;
  93. }
  94. function updateVersion($name, $slug, $version, $details_url)
  95. {
  96. $version_data = new stdClass();
  97. $version_data->name = $name;
  98. $version_data->slug = $slug;
  99. $version_data->current_version = $version;
  100. $version_data->details_url = $details_url;
  101. $json = json_encode($version_data);
  102. JFile::write($this->versions_path, $json);
  103. }
  104. function installUpdate()
  105. {
  106. if (!file_exists($this->versions_path)) {
  107. $params =& $this->params;
  108. RokUpdater::updateVersion($params->get('update_name'), $params->get('update_slug'), $params->get('current_version'), $this->details_url);
  109. }
  110. // read version file
  111. jimport('joomla/filesystem.file');
  112. $version = json_decode(JFile::read($this->versions_path));
  113. $this->current_version = $version->current_version;
  114. $alldetails = $this->_decodeJSONData($this->details_path);
  115. foreach ($alldetails as $key => $details) {
  116. if ($key == $version->slug) {
  117. foreach ($details->versions as $newversion) {
  118. if(version_compare($this->current_version,$newversion->minimum_version,'>=')) {
  119. $details->download_url = $newversion->download_url;
  120. $details->version = $newversion->version;
  121. $details->md5 = $newversion->md5;
  122. }
  123. }
  124. $file_path = $this->tmp_path;
  125. $result = RokUpdater::downloadFile($details->download_url, $file_path);
  126. $final_path = $file_path.DS.$result;
  127. if (is_object($result)) {
  128. HTML_RokError::showError('Download Failed: ' . $result->message . '(' . $result->number . ')</p>');
  129. return false;
  130. }
  131. if (md5_file($final_path) != $details->md5) {
  132. HTML_RokError::showError('MD5 Mismatch: The MD5 hash for the downloaded file does not match the anticipated value.</p>');
  133. return false;
  134. }
  135. $extractor = $this->extractor;
  136. $install_path = $this->tmp_path;
  137. // Temporary folder to extract the archive into
  138. $tmpdir = uniqid('install_');
  139. $install_path = $install_path . DS . $tmpdir;
  140. switch ($extractor)
  141. {
  142. case ROKUPDATER_EXTRACTOR_16:
  143. RokUpdater::import('joomla.filesystem.archive');
  144. if (!JArchive::extract($final_path, $install_path)) {
  145. HTML_RokError::showError('Failed to extract archive!');
  146. return false;
  147. }
  148. break;
  149. case ROKUPDATER_EXTRACTOR_15:
  150. jimport('joomla.filesystem.archive');
  151. if (!JArchive::extract($final_path, $install_path)) {
  152. HTML_RokError::showError('Failed to extract archive!');
  153. return false;
  154. }
  155. break;
  156. case ROKUPDATER_EXTRACTOR_PEAR:
  157. jimport('pear.archive_tar.Archive_Tar');
  158. $extractor = new Archive_Tar($install_path);
  159. if (!$extractor->extract(JPATH_SITE)) {
  160. HTML_RokError::showError('Failed to extract archive!');
  161. return false;
  162. }
  163. break;
  164. }
  165. jimport('joomla.installer.installer');
  166. jimport('joomla.installer.helper');
  167. jimport('joomla.filesystem.folder');
  168. jimport('joomla.filesystem.file');
  169. // Get an installer instance
  170. $installer =& RokStarInstaller::getInstance();
  171. // Run the installer and catch any output
  172. ob_start();
  173. $ret = $installer->install($install_path);
  174. $output = ob_get_clean();
  175. $errors = JError::getErrors();
  176. JFolder::delete($install_path);
  177. JFile::delete($final_path);
  178. RokUpdater::updateVersion($details->name, $version->slug, $details->version, $this->details_url);
  179. return $ret;
  180. }
  181. }
  182. }
  183. function downloadFile($url, $target)
  184. {
  185. RokUpdater::import('pasamio.downloader.downloader');
  186. $downloader =& Downloader::getInstance();
  187. //$error_object = new stdClass();
  188. $adapter = null;
  189. switch ($this->download_method)
  190. {
  191. case ROKUPDATER_DLMETHOD_FOPEN:
  192. default:
  193. $adapter = $downloader->getAdapter('fopen');
  194. break;
  195. case ROKUPDATER_DLMETHOD_CURL:
  196. $adapter = $downloader->getAdapter('curl');
  197. break;
  198. }
  199. return $adapter->downloadFile($url, $target, $this->params);
  200. }
  201. function generateUAString($mask = true)
  202. {
  203. $version = new JVersion();
  204. $lang =& JFactory::getLanguage();
  205. $parts = Array();
  206. if ($mask) {
  207. $parts[] = 'Mozilla/5.0';
  208. } else {
  209. $parts[] = 'Joomla!';
  210. }
  211. $parts[] = '(Joomla; PHP; ' . PHP_OS . '; ' . $lang->getTag() . '; rv:1.9.1)';
  212. $parts[] = 'Joomla/' . $version->getShortVersion();
  213. $parts[] = 'RokUpdater/' . ROKUPDATER_VERSION;
  214. return implode(' ', $parts);
  215. }
  216. function import($path)
  217. {
  218. // attempt to load the path locally but...
  219. // unfortunately 1.5 doesn't check the file exists before including it so we mask it
  220. $res = JLoader::import($path, dirname(__FILE__) . DS . 'libraries');
  221. if (!$res) {
  222. // fall back when it doesn't work
  223. return jimport($path);
  224. }
  225. return $res;
  226. }
  227. }
  228. class HTML_RokError
  229. {
  230. function showError($message)
  231. {
  232. echo '<p class="mc-error">' . $message . '</p>';
  233. }
  234. }