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

/downloader/Maged/Connect.php

https://github.com/FiveDigital/magento2
PHP | 516 lines | 326 code | 35 blank | 155 comment | 71 complexity | 5b550779945bd0bc1d01abc2e621bad2 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Connect
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. error_reporting(E_ALL & ~E_NOTICE);
  27. // just a shortcut
  28. if (!defined('DS')) {
  29. define('DS', DIRECTORY_SEPARATOR);
  30. }
  31. // add Mage lib in include_path if needed
  32. $_includePath = get_include_path();
  33. $_libDir = dirname(dirname(__FILE__)) . DS . 'lib';
  34. if (strpos($_includePath, $_libDir) === false) {
  35. if (substr($_includePath, 0, 2) === '.' . PATH_SEPARATOR) {
  36. $_includePath = '.' . PATH_SEPARATOR . $_libDir . PATH_SEPARATOR . substr($_includePath, 2);
  37. } else {
  38. $_includePath = $_libDir . PATH_SEPARATOR . $_includePath;
  39. }
  40. set_include_path($_includePath);
  41. }
  42. /**
  43. * Class for connect
  44. *
  45. * @category Mage
  46. * @package Mage_Connect
  47. * @author Magento Core Team <core@magentocommerce.com>
  48. */
  49. class Maged_Connect
  50. {
  51. /**
  52. * Object of config
  53. *
  54. * @var Mage_Connect_Config
  55. */
  56. protected $_config;
  57. /**
  58. * Object of single config
  59. *
  60. * @var Mage_Connect_Singleconfig
  61. */
  62. protected $_sconfig;
  63. /**
  64. * Object of frontend
  65. *
  66. * @var Mage_Connect_Frontend
  67. */
  68. protected $_frontend;
  69. /**
  70. * Internal cache for command objects
  71. *
  72. * @var array
  73. */
  74. protected $_cmdCache = array();
  75. /**
  76. * Console Started flag
  77. *
  78. * @var boolean
  79. */
  80. protected $_consoleStarted = false;
  81. /**
  82. * Instance of class
  83. *
  84. * @var Maged_Connect
  85. */
  86. static protected $_instance;
  87. /**
  88. * Constructor loads Config, Cache Config and initializes Frontend
  89. */
  90. public function __construct()
  91. {
  92. $this->getConfig();
  93. $this->getSingleConfig();
  94. $this->getFrontend();
  95. }
  96. /**
  97. * Destructor, sends Console footer if Console started
  98. */
  99. public function __destruct()
  100. {
  101. if ($this->_consoleStarted) {
  102. $this->_consoleFooter();
  103. }
  104. }
  105. /**
  106. * Initialize instance
  107. *
  108. * @return Maged_Connect
  109. */
  110. public static function getInstance()
  111. {
  112. if (!self::$_instance) {
  113. self::$_instance = new self;
  114. }
  115. return self::$_instance;
  116. }
  117. /**
  118. * Retrieve object of config and set it to Mage_Connect_Command
  119. *
  120. * @return Mage_Connect_Config
  121. */
  122. public function getConfig()
  123. {
  124. if (!$this->_config) {
  125. $this->_config = new Mage_Connect_Config();
  126. $ftp=$this->_config->__get('remote_config');
  127. if(!empty($ftp)){
  128. $packager = new Mage_Connect_Packager();
  129. list($cache, $config, $ftpObj) = $packager->getRemoteConf($ftp);
  130. $this->_config=$config;
  131. $this->_sconfig=$cache;
  132. }
  133. $this->_config->magento_root = dirname(dirname(__FILE__)).DS.'..';
  134. Mage_Connect_Command::setConfigObject($this->_config);
  135. }
  136. return $this->_config;
  137. }
  138. /**
  139. * Retrieve object of single config and set it to Mage_Connect_Command
  140. *
  141. * @param bool $reload
  142. * @return Mage_Connect_Singleconfig
  143. */
  144. public function getSingleConfig($reload = false)
  145. {
  146. if(!$this->_sconfig || $reload) {
  147. $this->_sconfig = new Mage_Connect_Singleconfig(
  148. $this->getConfig()->magento_root . DIRECTORY_SEPARATOR
  149. . $this->getConfig()->downloader_path . DIRECTORY_SEPARATOR
  150. . Mage_Connect_Singleconfig::DEFAULT_SCONFIG_FILENAME
  151. );
  152. }
  153. Mage_Connect_Command::setSconfig($this->_sconfig);
  154. return $this->_sconfig;
  155. }
  156. /**
  157. * Retrieve object of frontend and set it to Mage_Connect_Command
  158. *
  159. * @return Maged_Connect_Frontend
  160. */
  161. public function getFrontend()
  162. {
  163. if (!$this->_frontend) {
  164. $this->_frontend = new Maged_Connect_Frontend();
  165. Mage_Connect_Command::setFrontendObject($this->_frontend);
  166. }
  167. return $this->_frontend;
  168. }
  169. /**
  170. * Retrieve lof from frontend
  171. *
  172. * @return array
  173. */
  174. public function getLog()
  175. {
  176. return $this->getFrontend()->getLog();
  177. }
  178. /**
  179. * Retrieve output from frontend
  180. *
  181. * @return array
  182. */
  183. public function getOutput()
  184. {
  185. return $this->getFrontend()->getOutput();
  186. }
  187. /**
  188. * Clean registry
  189. *
  190. * @return Maged_Connect
  191. */
  192. public function cleanSconfig()
  193. {
  194. $this->getSingleConfig()->clear();
  195. return $this;
  196. }
  197. /**
  198. * Delete directory recursively
  199. *
  200. * @param string $path
  201. * @return Maged_Connect
  202. */
  203. public function delTree($path) {
  204. if (@is_dir($path)) {
  205. $entries = @scandir($path);
  206. foreach ($entries as $entry) {
  207. if ($entry != '.' && $entry != '..') {
  208. $this->delTree($path.DS.$entry);
  209. }
  210. }
  211. @rmdir($path);
  212. } else {
  213. @unlink($path);
  214. }
  215. return $this;
  216. }
  217. /**
  218. * Run commands from Mage_Connect_Command
  219. *
  220. * @param string $command
  221. * @param array $options
  222. * @param array $params
  223. * @return boolean|Mage_Connect_Error
  224. */
  225. public function run($command, $options=array(), $params=array())
  226. {
  227. @set_time_limit(0);
  228. @ini_set('memory_limit', '256M');
  229. if (empty($this->_cmdCache[$command])) {
  230. Mage_Connect_Command::getCommands();
  231. /**
  232. * @var $cmd Mage_Connect_Command
  233. */
  234. $cmd = Mage_Connect_Command::getInstance($command);
  235. if ($cmd instanceof Mage_Connect_Error) {
  236. return $cmd;
  237. }
  238. $this->_cmdCache[$command] = $cmd;
  239. } else {
  240. /**
  241. * @var $cmd Mage_Connect_Command
  242. */
  243. $cmd = $this->_cmdCache[$command];
  244. }
  245. $ftp=$this->getConfig()->remote_config;
  246. if(strlen($ftp)>0){
  247. $options=array_merge($options, array('ftp'=>$ftp));
  248. }
  249. $cmd->run($command, $options, $params);
  250. if ($cmd->ui()->hasErrors()) {
  251. return false;
  252. } else {
  253. return true;
  254. }
  255. }
  256. /**
  257. * Set remote Config by URI
  258. *
  259. * @param $uri
  260. * @return Maged_Connect
  261. */
  262. public function setRemoteConfig($uri)
  263. {
  264. $this->getConfig()->remote_config=$uri;
  265. return $this;
  266. }
  267. /**
  268. * Show Errors
  269. *
  270. * @param array $errors Error messages
  271. * @return Maged_Connect
  272. */
  273. public function showConnectErrors($errors)
  274. {
  275. echo '<script type="text/javascript">';
  276. $run = new Maged_Model_Connect_Request();
  277. if ($callback = $run->get('failure_callback')) {
  278. if (is_array($callback)) {
  279. call_user_func_array($callback, array($errors));
  280. } else {
  281. echo $callback;
  282. }
  283. }
  284. echo '</script>';
  285. return $this;
  286. }
  287. /**
  288. * Run Mage_Connect_Command with html output console style
  289. *
  290. * @throws Maged_Exception
  291. * @param array|string|Maged_Model $runParams command, options, params, comment, success_callback, failure_callback
  292. * @return bool|Mage_Connect_Error
  293. */
  294. public function runHtmlConsole($runParams)
  295. {
  296. if (function_exists('apache_setenv')) {
  297. apache_setenv('no-gzip', '1');
  298. }
  299. @ini_set('zlib.output_compression', 0);
  300. @ini_set('implicit_flush', 1);
  301. for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
  302. ob_implicit_flush();
  303. $fe = $this->getFrontend();
  304. $oldLogStream = $fe->getLogStream();
  305. $fe->setLogStream('stdout');
  306. if ($runParams instanceof Maged_Model) {
  307. $run = $runParams;
  308. } elseif (is_array($runParams)) {
  309. $run = new Maged_Model_Connect_Request($runParams);
  310. } elseif (is_string($runParams)) {
  311. $run = new Maged_Model_Connect_Request(array('comment'=>$runParams));
  312. } else {
  313. throw Maged_Exception("Invalid run parameters");
  314. }
  315. if (!$run->get('no-header')) {
  316. $this->_consoleHeader();
  317. }
  318. echo htmlspecialchars($run->get('comment')).'<br/>';
  319. if ($command = $run->get('command')) {
  320. $result = $this->run($command, $run->get('options'), $run->get('params'));
  321. if ($this->getFrontend()->hasErrors()) {
  322. echo "<br/>CONNECT ERROR: ";
  323. foreach ($this->getFrontend()->getErrors(false) as $error) {
  324. echo nl2br($error[1]);
  325. echo '<br/>';
  326. }
  327. }
  328. echo '<script type="text/javascript">';
  329. if ($this->getFrontend()->hasErrors()) {
  330. if ($callback = $run->get('failure_callback')) {
  331. if (is_array($callback)) {
  332. call_user_func_array($callback, array($result));
  333. } else {
  334. echo $callback;
  335. }
  336. }
  337. } else {
  338. if (!$run->get('no-footer')) {
  339. if ($callback = $run->get('success_callback')) {
  340. if (is_array($callback)) {
  341. call_user_func_array($callback, array($result));
  342. } else {
  343. echo $callback;
  344. }
  345. }
  346. }
  347. }
  348. echo '</script>';
  349. } else {
  350. $result = false;
  351. }
  352. if ($this->getFrontend()->getErrors() || !$run->get('no-footer')) {
  353. //$this->_consoleFooter();
  354. $fe->setLogStream($oldLogStream);
  355. }
  356. return $result;
  357. }
  358. /**
  359. * Show HTML Console Header
  360. *
  361. * @return void
  362. */
  363. protected function _consoleHeader() {
  364. if (!$this->_consoleStarted) {
  365. ?>
  366. <html><head><style type="text/css">
  367. body { margin:0px;
  368. padding:3px;
  369. background:black;
  370. color:#2EC029;
  371. font:normal 11px Lucida Console, Courier New, serif;
  372. }
  373. </style></head><body>
  374. <script type="text/javascript">
  375. if (parent && parent.disableInputs) {
  376. parent.disableInputs(true);
  377. }
  378. if (typeof auto_scroll=='undefined') {
  379. var auto_scroll = window.setInterval(console_scroll, 10);
  380. }
  381. function console_scroll()
  382. {
  383. if (typeof top.$ != 'function') {
  384. return;
  385. }
  386. if (top.$('connect_iframe_scroll').checked) {
  387. document.body.scrollTop+=3;
  388. }
  389. }
  390. function show_message(message, newline)
  391. {
  392. var bodyElement = document.getElementsByTagName('body')[0];
  393. if (typeof newline == 'undefined') {
  394. newline = true
  395. }
  396. if (newline) {
  397. bodyElement.innerHTML += '<br/>';
  398. }
  399. bodyElement.innerHTML += message;
  400. }
  401. function clear_cache(callbacks)
  402. {
  403. if (typeof top.Ajax != 'object') {
  404. return;
  405. }
  406. var message = 'Exception during cache and session cleaning';
  407. var url = window.location.href.split('?')[0] + '?A=cleanCache';
  408. var intervalID = setInterval(function() {show_message('.', false); }, 500);
  409. var clean = 0;
  410. var maintenance = 0;
  411. if (window.location.href.indexOf('clean_sessions') >= 0) {
  412. clean = 1;
  413. }
  414. if (window.location.href.indexOf('maintenance') >= 0) {
  415. maintenance = 1;
  416. }
  417. new top.Ajax.Request(url, {
  418. method: 'post',
  419. parameters: {clean_sessions:clean, maintenance:maintenance},
  420. onCreate: function() {
  421. show_message('Cleaning cache');
  422. show_message('');
  423. },
  424. onSuccess: function(transport, json) {
  425. var result = true;
  426. try{
  427. var response = eval('(' + transport.responseText + ')');
  428. if (typeof response.result != 'undefined') {
  429. result = response.result;
  430. } else {
  431. result = false;
  432. }
  433. if (typeof response.message != 'undefined') {
  434. if (response.message.length > 0) {
  435. message = response.message;
  436. } else {
  437. message = 'Cache cleaned successfully';
  438. }
  439. }
  440. } catch (ex){
  441. result = false;
  442. }
  443. if (result) {
  444. callbacks.success();
  445. } else {
  446. callbacks.fail();
  447. }
  448. },
  449. onFailure: function() {
  450. callbacks.fail();
  451. },
  452. onComplete: function(transport) {
  453. clearInterval(intervalID);
  454. show_message(message);
  455. }
  456. });
  457. }
  458. </script>
  459. <?php
  460. $this->_consoleStarted = true;
  461. }
  462. }
  463. /**
  464. * Show HTML Console Footer
  465. *
  466. * @return void
  467. */
  468. protected function _consoleFooter() {
  469. if ($this->_consoleStarted) {
  470. ?>
  471. <script type="text/javascript">
  472. if (parent && parent.disableInputs) {
  473. parent.disableInputs(false);
  474. }
  475. </script>
  476. </body></html>
  477. <?php
  478. $this->_consoleStarted = false;
  479. }
  480. }
  481. }