PageRenderTime 93ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/downloader/Maged/Pear.php

https://github.com/gryzz/crystal_magento
PHP | 344 lines | 260 code | 44 blank | 40 comment | 47 complexity | 72d8f353aba1b21a824aa9bc29849518 MD5 | raw file
  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 Varien
  22. * @package Varien_Object
  23. * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.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 PEAR lib in include_path if needed
  32. $_includePath = get_include_path();
  33. $_pearDir = dirname(dirname(__FILE__)) . DS . 'pearlib';
  34. if (!getenv('PHP_PEAR_INSTALL_DIR')) {
  35. putenv('PHP_PEAR_INSTALL_DIR=' . $_pearDir);
  36. }
  37. $_pearPhpDir = $_pearDir . DS . 'php';
  38. if (strpos($_includePath, $_pearPhpDir) === false) {
  39. if (substr($_includePath, 0, 2) === '.' . PATH_SEPARATOR) {
  40. $_includePath = '.' . PATH_SEPARATOR . $_pearPhpDir . PATH_SEPARATOR . substr($_includePath, 2);
  41. } else {
  42. $_includePath = $_pearPhpDir . PATH_SEPARATOR . $_includePath;
  43. }
  44. set_include_path($_includePath);
  45. }
  46. // include necessary PEAR libs
  47. require_once "PEAR.php";
  48. require_once "PEAR/Frontend.php";
  49. require_once "PEAR/Registry.php";
  50. require_once "PEAR/Config.php";
  51. require_once "PEAR/Command.php";
  52. require_once "PEAR/Exception.php";
  53. require_once "Maged/Pear/Frontend.php";
  54. require_once "Maged/Pear/Package.php";
  55. require_once "Maged/Pear/Registry.php";
  56. require_once "Maged/Model/Pear/Request.php";
  57. class Maged_Pear
  58. {
  59. protected $_config;
  60. protected $_registry;
  61. protected $_frontend;
  62. protected $_cmdCache = array();
  63. static protected $_instance;
  64. public function __construct()
  65. {
  66. $this->getConfig();
  67. }
  68. public function getInstance()
  69. {
  70. if (!self::$_instance) {
  71. self::$_instance = new self;
  72. }
  73. return self::$_instance;
  74. }
  75. public function isSystemPackage($pkg)
  76. {
  77. return in_array($pkg, array('Archive_Tar', 'Console_Getopt', 'PEAR', 'Structures_Graph'));
  78. }
  79. public function getBaseDir()
  80. {
  81. return dirname(dirname(dirname(__FILE__)));
  82. }
  83. public function getPearDir()
  84. {
  85. return dirname(dirname(__FILE__)).DS.'pearlib';
  86. }
  87. public function getConfig()
  88. {
  89. if (!$this->_config) {
  90. $pear_dir = $this->getPearDir();
  91. $config = PEAR_Config::singleton($pear_dir.DS.'pear.ini', '-');
  92. //$config = PEAR_Config::singleton();
  93. $config->set('auto_discover', 1);
  94. $config->set('cache_ttl', 60);
  95. #$config->set('preferred_state', 'beta');
  96. $config->set('bin_dir', $pear_dir);
  97. $config->set('php_dir', $pear_dir.DS.'php');
  98. $config->set('download_dir', $pear_dir.DS.'download');
  99. $config->set('temp_dir', $pear_dir.DS.'temp');
  100. $config->set('data_dir', $pear_dir.DS.'data');
  101. $config->set('cache_dir', $pear_dir.DS.'cache');
  102. $config->set('test_dir', $pear_dir.DS.'tests');
  103. $config->set('doc_dir', $pear_dir.DS.'docs');
  104. foreach ($config->getKeys() as $key) {
  105. if (!(substr($key, 0, 5)==='mage_' && substr($key, -4)==='_dir')) {
  106. continue;
  107. }
  108. $config->set($key, preg_replace('#^\.#', addslashes($this->getBaseDir()), $config->get($key)));
  109. //echo $key.' : '.$config->get($key).'<br>';
  110. }
  111. $reg = $this->getRegistry();
  112. $config->setRegistry($reg);
  113. PEAR_DependencyDB::singleton($config, $pear_dir.DS.'php'.DS.'.depdb');
  114. PEAR_Frontend::setFrontendObject($this->getFrontend());
  115. PEAR_Command::registerCommands(false, $pear_dir.DS.'php'.DS.'PEAR'.DS.'Command'.DS);
  116. $this->_config = $config;
  117. }
  118. return $this->_config;
  119. }
  120. public function getMagentoChannels()
  121. {
  122. return array(
  123. 'connect.magentocommerce.com/core' => 'Core Team Extensions',
  124. 'connect.magentocommerce.com/community' => 'Community Extensions',
  125. );
  126. }
  127. public function getRegistry()
  128. {
  129. // return $this->getConfig()->getRegistry();
  130. if (!$this->_registry) {
  131. $this->_registry = new Maged_Pear_Registry($this->getPearDir().DS.'php');
  132. $changed = false;
  133. foreach ($this->getMagentoChannels() as $channel=>$channelName) {
  134. if (!$this->getRegistry()->channelExists($channel)) {
  135. $this->run('channel-discover', array(), array($channel));
  136. $changed = true;
  137. }
  138. }
  139. if ($changed && empty($_GET['pear_registry'])) {
  140. //TODO:refresh registry in memory to reflect discovered channels without redirect
  141. header("Location: ".$_SERVER['SCRIPT_NAME'].'?'.$_SERVER['QUERY_STRING'].'&pear_registry=1');
  142. exit;
  143. }
  144. }
  145. return $this->_registry;
  146. }
  147. public function getFrontend()
  148. {
  149. if (!$this->_frontend) {
  150. $this->_frontend = new Maged_Pear_Frontend;
  151. }
  152. return $this->_frontend;
  153. }
  154. public function getLog()
  155. {
  156. return $this->getFrontend()->getLog();
  157. }
  158. public function getOutput()
  159. {
  160. return $this->getFrontend()->getOutput();
  161. }
  162. public function cleanRegistry()
  163. {
  164. $oldDir = @getcwd();
  165. @chdir($this->getPearDir().DIRECTORY_SEPARATOR.'php');
  166. $this->delTree('.registry');
  167. @chdir($oldDir);
  168. return $this;
  169. }
  170. public function delTree($path) {
  171. if (@is_dir($path)) {
  172. $entries = @scandir($path);
  173. foreach ($entries as $entry) {
  174. if ($entry != '.' && $entry != '..') {
  175. $this->delTree($path.DS.$entry);
  176. }
  177. }
  178. @rmdir($path);
  179. } else {
  180. @unlink($path);
  181. }
  182. return $this;
  183. }
  184. public function run($command, $options=array(), $params=array())
  185. {
  186. @set_time_limit(0);
  187. @ini_set('memory_limit', '256M');
  188. if (empty($this->_cmdCache[$command])) {
  189. $cmd = PEAR_Command::factory($command, $this->getConfig());
  190. if ($cmd instanceof PEAR_Error) {
  191. return $cmd;
  192. }
  193. $this->_cmdCache[$command] = $cmd;
  194. } else {
  195. $cmd = $this->_cmdCache[$command];
  196. }
  197. #$cmd = PEAR_Command::factory($command, $this->getConfig());
  198. $result = $cmd->run($command, $options, $params);
  199. return $result;
  200. }
  201. public function setRemoteConfig($uri) #$host, $user, $password, $path='', $port=null)
  202. {
  203. #$uri = 'ftp://' . $user . ':' . $password . '@' . $host . (is_numeric($port) ? ':' . $port : '') . '/' . trim($path, '/') . '/';
  204. $this->run('config-set', array(), array('remote_config', $uri));
  205. return $this;
  206. }
  207. /**
  208. * Run PEAR command with html output console style
  209. *
  210. * @param array|Maged_Model $runParams command, options, params,
  211. * comment, success_callback, failure_callback
  212. */
  213. public function runHtmlConsole($runParams)
  214. {
  215. ob_implicit_flush();
  216. $fe = $this->getFrontend();
  217. $oldLogStream = $fe->getLogStream();
  218. $fe->setLogStream('stdout');
  219. if ($runParams instanceof Maged_Model) {
  220. $run = $runParams;
  221. } elseif (is_array($runParams)) {
  222. $run = new Maged_Model_Pear_Request($runParams);
  223. } elseif (is_string($runParams)) {
  224. $run = new Maged_Model_Pear_Request(array('comment'=>$runParams));
  225. } else {
  226. throw Maged_Exception("Invalid run parameters");
  227. }
  228. if (!$run->get('no-header')) {
  229. ?>
  230. <html><head><style type="text/css">
  231. body { margin:0px;
  232. padding:3px;
  233. background:black;
  234. color:#2EC029;
  235. font:normal 11px Lucida Console, Courier New, serif;
  236. }
  237. </style></head><body>
  238. <script type="text/javascript">
  239. if (parent && parent.disableInputs) {
  240. parent.disableInputs(true);
  241. }
  242. if (typeof auto_scroll=='undefined') {
  243. var auto_scroll = window.setInterval(console_scroll, 10);
  244. }
  245. function console_scroll()
  246. {
  247. if (typeof top.$!='function') {
  248. return;
  249. }
  250. if (top.$('pear_iframe_scroll').checked) {
  251. document.body.scrollTop+=3;
  252. }
  253. }
  254. </script>
  255. <?php
  256. }
  257. echo htmlspecialchars($run->get('comment'));
  258. if ($command = $run->get('command')) {
  259. $result = $this->run($command, $run->get('options'), $run->get('params'));
  260. if ($result instanceof PEAR_Error) {
  261. echo "\r\n\r\nPEAR ERROR: ".nl2br($result->getMessage());
  262. }
  263. echo '<script type="text/javascript">';
  264. if ($result instanceof PEAR_Error) {
  265. if ($callback = $run->get('failure_callback')) {
  266. if (is_array($callback)) {
  267. call_user_func_array($callback, array($result));
  268. } else {
  269. echo $callback;
  270. }
  271. }
  272. } else {
  273. if (!$run->get('no-footer')) {
  274. if ($callback = $run->get('success_callback')) {
  275. if (is_array($callback)) {
  276. call_user_func_array($callback, array($result));
  277. } else {
  278. echo $callback;
  279. }
  280. }
  281. }
  282. }
  283. echo '</script>';
  284. } else {
  285. $result = false;
  286. }
  287. if ($result instanceof PEAR_Error || !$run->get('no-footer')) {
  288. ?>
  289. <script type="text/javascript">
  290. if (parent && parent.disableInputs) {
  291. parent.disableInputs(false);
  292. }
  293. </script>
  294. </body></html>
  295. <?php
  296. $fe->setLogStream($oldLogStream);
  297. }
  298. return $result;
  299. }
  300. }