Registry.php 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388
  1. <?php
  2. /**
  3. * PEAR_Registry
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * @category pear
  8. * @package PEAR
  9. * @author Stig Bakken <ssb@php.net>
  10. * @author Tomas V. V. Cox <cox@idecnet.com>
  11. * @author Greg Beaver <cellog@php.net>
  12. * @copyright 1997-2009 The Authors
  13. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  14. * @link http://pear.php.net/package/PEAR
  15. * @since File available since Release 0.1
  16. */
  17. /**
  18. * for PEAR_Error
  19. */
  20. require_once 'PEAR.php';
  21. require_once 'PEAR/DependencyDB.php';
  22. define('PEAR_REGISTRY_ERROR_LOCK', -2);
  23. define('PEAR_REGISTRY_ERROR_FORMAT', -3);
  24. define('PEAR_REGISTRY_ERROR_FILE', -4);
  25. define('PEAR_REGISTRY_ERROR_CONFLICT', -5);
  26. define('PEAR_REGISTRY_ERROR_CHANNEL_FILE', -6);
  27. /**
  28. * Administration class used to maintain the installed package database.
  29. * @category pear
  30. * @package PEAR
  31. * @author Stig Bakken <ssb@php.net>
  32. * @author Tomas V. V. Cox <cox@idecnet.com>
  33. * @author Greg Beaver <cellog@php.net>
  34. * @copyright 1997-2009 The Authors
  35. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  36. * @version Release: 1.10.10
  37. * @link http://pear.php.net/package/PEAR
  38. * @since Class available since Release 1.4.0a1
  39. */
  40. class PEAR_Registry extends PEAR
  41. {
  42. /**
  43. * File containing all channel information.
  44. * @var string
  45. */
  46. var $channels = '';
  47. /** Directory where registry files are stored.
  48. * @var string
  49. */
  50. var $statedir = '';
  51. /** File where the file map is stored
  52. * @var string
  53. */
  54. var $filemap = '';
  55. /** Directory where registry files for channels are stored.
  56. * @var string
  57. */
  58. var $channelsdir = '';
  59. /** Name of file used for locking the registry
  60. * @var string
  61. */
  62. var $lockfile = '';
  63. /** File descriptor used during locking
  64. * @var resource
  65. */
  66. var $lock_fp = null;
  67. /** Mode used during locking
  68. * @var int
  69. */
  70. var $lock_mode = 0; // XXX UNUSED
  71. /** Cache of package information. Structure:
  72. * array(
  73. * 'package' => array('id' => ... ),
  74. * ... )
  75. * @var array
  76. */
  77. var $pkginfo_cache = array();
  78. /** Cache of file map. Structure:
  79. * array( '/path/to/file' => 'package', ... )
  80. * @var array
  81. */
  82. var $filemap_cache = array();
  83. /**
  84. * @var false|PEAR_ChannelFile
  85. */
  86. var $_pearChannel;
  87. /**
  88. * @var false|PEAR_ChannelFile
  89. */
  90. var $_peclChannel;
  91. /**
  92. * @var false|PEAR_ChannelFile
  93. */
  94. var $_docChannel;
  95. /**
  96. * @var PEAR_DependencyDB
  97. */
  98. var $_dependencyDB;
  99. /**
  100. * @var PEAR_Config
  101. */
  102. var $_config;
  103. /**
  104. * PEAR_Registry constructor.
  105. *
  106. * @param string (optional) PEAR install directory (for .php files)
  107. * @param PEAR_ChannelFile PEAR_ChannelFile object representing the PEAR channel, if
  108. * default values are not desired. Only used the very first time a PEAR
  109. * repository is initialized
  110. * @param PEAR_ChannelFile PEAR_ChannelFile object representing the PECL channel, if
  111. * default values are not desired. Only used the very first time a PEAR
  112. * repository is initialized
  113. *
  114. * @access public
  115. */
  116. function __construct($pear_install_dir = PEAR_INSTALL_DIR, $pear_channel = false,
  117. $pecl_channel = false, $pear_metadata_dir = '')
  118. {
  119. parent::__construct();
  120. $this->setInstallDir($pear_install_dir, $pear_metadata_dir);
  121. $this->_pearChannel = $pear_channel;
  122. $this->_peclChannel = $pecl_channel;
  123. $this->_config = false;
  124. }
  125. function setInstallDir($pear_install_dir = PEAR_INSTALL_DIR, $pear_metadata_dir = '')
  126. {
  127. $ds = DIRECTORY_SEPARATOR;
  128. $this->install_dir = $pear_install_dir;
  129. if (!$pear_metadata_dir) {
  130. $pear_metadata_dir = $pear_install_dir;
  131. }
  132. $this->channelsdir = $pear_metadata_dir.$ds.'.channels';
  133. $this->statedir = $pear_metadata_dir.$ds.'.registry';
  134. $this->filemap = $pear_metadata_dir.$ds.'.filemap';
  135. $this->lockfile = $pear_metadata_dir.$ds.'.lock';
  136. }
  137. function hasWriteAccess()
  138. {
  139. if (!file_exists($this->install_dir)) {
  140. $dir = $this->install_dir;
  141. while ($dir && $dir != '.') {
  142. $olddir = $dir;
  143. $dir = dirname($dir);
  144. if ($dir != '.' && file_exists($dir)) {
  145. if (is_writeable($dir)) {
  146. return true;
  147. }
  148. return false;
  149. }
  150. if ($dir == $olddir) { // this can happen in safe mode
  151. return @is_writable($dir);
  152. }
  153. }
  154. return false;
  155. }
  156. return is_writeable($this->install_dir);
  157. }
  158. function setConfig(&$config, $resetInstallDir = true)
  159. {
  160. $this->_config = &$config;
  161. if ($resetInstallDir) {
  162. $this->setInstallDir($config->get('php_dir'), $config->get('metadata_dir'));
  163. }
  164. }
  165. function _initializeChannelDirs()
  166. {
  167. static $running = false;
  168. if (!$running) {
  169. $running = true;
  170. $ds = DIRECTORY_SEPARATOR;
  171. if (!is_dir($this->channelsdir) ||
  172. !file_exists($this->channelsdir . $ds . 'pear.php.net.reg') ||
  173. !file_exists($this->channelsdir . $ds . 'pecl.php.net.reg') ||
  174. !file_exists($this->channelsdir . $ds . 'doc.php.net.reg') ||
  175. !file_exists($this->channelsdir . $ds . '__uri.reg')) {
  176. if (!file_exists($this->channelsdir . $ds . 'pear.php.net.reg')) {
  177. $pear_channel = $this->_pearChannel;
  178. if (!is_a($pear_channel, 'PEAR_ChannelFile') || !$pear_channel->validate()) {
  179. if (!class_exists('PEAR_ChannelFile')) {
  180. require_once 'PEAR/ChannelFile.php';
  181. }
  182. $pear_channel = new PEAR_ChannelFile;
  183. $pear_channel->setAlias('pear');
  184. $pear_channel->setServer('pear.php.net');
  185. $pear_channel->setSummary('PHP Extension and Application Repository');
  186. $pear_channel->setDefaultPEARProtocols();
  187. $pear_channel->setBaseURL('REST1.0', 'http://pear.php.net/rest/');
  188. $pear_channel->setBaseURL('REST1.1', 'http://pear.php.net/rest/');
  189. $pear_channel->setBaseURL('REST1.3', 'http://pear.php.net/rest/');
  190. //$pear_channel->setBaseURL('REST1.4', 'http://pear.php.net/rest/');
  191. } else {
  192. $pear_channel->setServer('pear.php.net');
  193. $pear_channel->setAlias('pear');
  194. }
  195. $pear_channel->validate();
  196. $this->_addChannel($pear_channel);
  197. }
  198. if (!file_exists($this->channelsdir . $ds . 'pecl.php.net.reg')) {
  199. $pecl_channel = $this->_peclChannel;
  200. if (!is_a($pecl_channel, 'PEAR_ChannelFile') || !$pecl_channel->validate()) {
  201. if (!class_exists('PEAR_ChannelFile')) {
  202. require_once 'PEAR/ChannelFile.php';
  203. }
  204. $pecl_channel = new PEAR_ChannelFile;
  205. $pecl_channel->setAlias('pecl');
  206. $pecl_channel->setServer('pecl.php.net');
  207. $pecl_channel->setSummary('PHP Extension Community Library');
  208. $pecl_channel->setDefaultPEARProtocols();
  209. $pecl_channel->setBaseURL('REST1.0', 'http://pecl.php.net/rest/');
  210. $pecl_channel->setBaseURL('REST1.1', 'http://pecl.php.net/rest/');
  211. $pecl_channel->setValidationPackage('PEAR_Validator_PECL', '1.0');
  212. } else {
  213. $pecl_channel->setServer('pecl.php.net');
  214. $pecl_channel->setAlias('pecl');
  215. }
  216. $pecl_channel->validate();
  217. $this->_addChannel($pecl_channel);
  218. }
  219. if (!file_exists($this->channelsdir . $ds . 'doc.php.net.reg')) {
  220. $doc_channel = $this->_docChannel;
  221. if (!is_a($doc_channel, 'PEAR_ChannelFile') || !$doc_channel->validate()) {
  222. if (!class_exists('PEAR_ChannelFile')) {
  223. require_once 'PEAR/ChannelFile.php';
  224. }
  225. $doc_channel = new PEAR_ChannelFile;
  226. $doc_channel->setAlias('phpdocs');
  227. $doc_channel->setServer('doc.php.net');
  228. $doc_channel->setSummary('PHP Documentation Team');
  229. $doc_channel->setDefaultPEARProtocols();
  230. $doc_channel->setBaseURL('REST1.0', 'http://doc.php.net/rest/');
  231. $doc_channel->setBaseURL('REST1.1', 'http://doc.php.net/rest/');
  232. $doc_channel->setBaseURL('REST1.3', 'http://doc.php.net/rest/');
  233. } else {
  234. $doc_channel->setServer('doc.php.net');
  235. $doc_channel->setAlias('doc');
  236. }
  237. $doc_channel->validate();
  238. $this->_addChannel($doc_channel);
  239. }
  240. if (!file_exists($this->channelsdir . $ds . '__uri.reg')) {
  241. if (!class_exists('PEAR_ChannelFile')) {
  242. require_once 'PEAR/ChannelFile.php';
  243. }
  244. $private = new PEAR_ChannelFile;
  245. $private->setName('__uri');
  246. $private->setDefaultPEARProtocols();
  247. $private->setBaseURL('REST1.0', '****');
  248. $private->setSummary('Pseudo-channel for static packages');
  249. $this->_addChannel($private);
  250. }
  251. $this->_rebuildFileMap();
  252. }
  253. $running = false;
  254. }
  255. }
  256. function _initializeDirs()
  257. {
  258. $ds = DIRECTORY_SEPARATOR;
  259. // XXX Compatibility code should be removed in the future
  260. // rename all registry files if any to lowercase
  261. if (!OS_WINDOWS && file_exists($this->statedir) && is_dir($this->statedir) &&
  262. $handle = opendir($this->statedir)) {
  263. $dest = $this->statedir . $ds;
  264. while (false !== ($file = readdir($handle))) {
  265. if (preg_match('/^.*[A-Z].*\.reg\\z/', $file)) {
  266. rename($dest . $file, $dest . strtolower($file));
  267. }
  268. }
  269. closedir($handle);
  270. }
  271. $this->_initializeChannelDirs();
  272. if (!file_exists($this->filemap)) {
  273. $this->_rebuildFileMap();
  274. }
  275. $this->_initializeDepDB();
  276. }
  277. function _initializeDepDB()
  278. {
  279. if (!isset($this->_dependencyDB)) {
  280. static $initializing = false;
  281. if (!$initializing) {
  282. $initializing = true;
  283. if (!$this->_config) { // never used?
  284. $file = OS_WINDOWS ? 'pear.ini' : '.pearrc';
  285. $this->_config = new PEAR_Config($this->statedir . DIRECTORY_SEPARATOR .
  286. $file);
  287. $this->_config->setRegistry($this);
  288. $this->_config->set('php_dir', $this->install_dir);
  289. }
  290. $this->_dependencyDB = &PEAR_DependencyDB::singleton($this->_config);
  291. if (PEAR::isError($this->_dependencyDB)) {
  292. // attempt to recover by removing the dep db
  293. if (file_exists($this->_config->get('metadata_dir', null, 'pear.php.net') .
  294. DIRECTORY_SEPARATOR . '.depdb')) {
  295. @unlink($this->_config->get('metadata_dir', null, 'pear.php.net') .
  296. DIRECTORY_SEPARATOR . '.depdb');
  297. }
  298. $this->_dependencyDB = &PEAR_DependencyDB::singleton($this->_config);
  299. if (PEAR::isError($this->_dependencyDB)) {
  300. echo $this->_dependencyDB->getMessage();
  301. echo 'Unrecoverable error';
  302. exit(1);
  303. }
  304. }
  305. $initializing = false;
  306. }
  307. }
  308. }
  309. /**
  310. * PEAR_Registry destructor. Makes sure no locks are forgotten.
  311. *
  312. * @access private
  313. */
  314. function _PEAR_Registry()
  315. {
  316. parent::_PEAR();
  317. if (is_resource($this->lock_fp)) {
  318. $this->_unlock();
  319. }
  320. }
  321. /**
  322. * Make sure the directory where we keep registry files exists.
  323. *
  324. * @return bool TRUE if directory exists, FALSE if it could not be
  325. * created
  326. *
  327. * @access private
  328. */
  329. function _assertStateDir($channel = false)
  330. {
  331. if ($channel && $this->_getChannelFromAlias($channel) != 'pear.php.net') {
  332. return $this->_assertChannelStateDir($channel);
  333. }
  334. static $init = false;
  335. if (!file_exists($this->statedir)) {
  336. if (!$this->hasWriteAccess()) {
  337. return false;
  338. }
  339. require_once 'System.php';
  340. if (!System::mkdir(array('-p', $this->statedir))) {
  341. return $this->raiseError("could not create directory '{$this->statedir}'");
  342. }
  343. $init = true;
  344. } elseif (!is_dir($this->statedir)) {
  345. return $this->raiseError('Cannot create directory ' . $this->statedir . ', ' .
  346. 'it already exists and is not a directory');
  347. }
  348. $ds = DIRECTORY_SEPARATOR;
  349. if (!file_exists($this->channelsdir)) {
  350. if (!file_exists($this->channelsdir . $ds . 'pear.php.net.reg') ||
  351. !file_exists($this->channelsdir . $ds . 'pecl.php.net.reg') ||
  352. !file_exists($this->channelsdir . $ds . 'doc.php.net.reg') ||
  353. !file_exists($this->channelsdir . $ds . '__uri.reg')) {
  354. $init = true;
  355. }
  356. } elseif (!is_dir($this->channelsdir)) {
  357. return $this->raiseError('Cannot create directory ' . $this->channelsdir . ', ' .
  358. 'it already exists and is not a directory');
  359. }
  360. if ($init) {
  361. static $running = false;
  362. if (!$running) {
  363. $running = true;
  364. $this->_initializeDirs();
  365. $running = false;
  366. $init = false;
  367. }
  368. } else {
  369. $this->_initializeDepDB();
  370. }
  371. return true;
  372. }
  373. /**
  374. * Make sure the directory where we keep registry files exists for a non-standard channel.
  375. *
  376. * @param string channel name
  377. * @return bool TRUE if directory exists, FALSE if it could not be
  378. * created
  379. *
  380. * @access private
  381. */
  382. function _assertChannelStateDir($channel)
  383. {
  384. $ds = DIRECTORY_SEPARATOR;
  385. if (!$channel || $this->_getChannelFromAlias($channel) == 'pear.php.net') {
  386. if (!file_exists($this->channelsdir . $ds . 'pear.php.net.reg')) {
  387. $this->_initializeChannelDirs();
  388. }
  389. return $this->_assertStateDir($channel);
  390. }
  391. $channelDir = $this->_channelDirectoryName($channel);
  392. if (!is_dir($this->channelsdir) ||
  393. !file_exists($this->channelsdir . $ds . 'pear.php.net.reg')) {
  394. $this->_initializeChannelDirs();
  395. }
  396. if (!file_exists($channelDir)) {
  397. if (!$this->hasWriteAccess()) {
  398. return false;
  399. }
  400. require_once 'System.php';
  401. if (!System::mkdir(array('-p', $channelDir))) {
  402. return $this->raiseError("could not create directory '" . $channelDir .
  403. "'");
  404. }
  405. } elseif (!is_dir($channelDir)) {
  406. return $this->raiseError("could not create directory '" . $channelDir .
  407. "', already exists and is not a directory");
  408. }
  409. return true;
  410. }
  411. /**
  412. * Make sure the directory where we keep registry files for channels exists
  413. *
  414. * @return bool TRUE if directory exists, FALSE if it could not be
  415. * created
  416. *
  417. * @access private
  418. */
  419. function _assertChannelDir()
  420. {
  421. if (!file_exists($this->channelsdir)) {
  422. if (!$this->hasWriteAccess()) {
  423. return false;
  424. }
  425. require_once 'System.php';
  426. if (!System::mkdir(array('-p', $this->channelsdir))) {
  427. return $this->raiseError("could not create directory '{$this->channelsdir}'");
  428. }
  429. } elseif (!is_dir($this->channelsdir)) {
  430. return $this->raiseError("could not create directory '{$this->channelsdir}" .
  431. "', it already exists and is not a directory");
  432. }
  433. if (!file_exists($this->channelsdir . DIRECTORY_SEPARATOR . '.alias')) {
  434. if (!$this->hasWriteAccess()) {
  435. return false;
  436. }
  437. require_once 'System.php';
  438. if (!System::mkdir(array('-p', $this->channelsdir . DIRECTORY_SEPARATOR . '.alias'))) {
  439. return $this->raiseError("could not create directory '{$this->channelsdir}/.alias'");
  440. }
  441. } elseif (!is_dir($this->channelsdir . DIRECTORY_SEPARATOR . '.alias')) {
  442. return $this->raiseError("could not create directory '{$this->channelsdir}" .
  443. "/.alias', it already exists and is not a directory");
  444. }
  445. return true;
  446. }
  447. /**
  448. * Get the name of the file where data for a given package is stored.
  449. *
  450. * @param string channel name, or false if this is a PEAR package
  451. * @param string package name
  452. *
  453. * @return string registry file name
  454. *
  455. * @access public
  456. */
  457. function _packageFileName($package, $channel = false)
  458. {
  459. if ($channel && $this->_getChannelFromAlias($channel) != 'pear.php.net') {
  460. return $this->_channelDirectoryName($channel) . DIRECTORY_SEPARATOR .
  461. strtolower($package) . '.reg';
  462. }
  463. return $this->statedir . DIRECTORY_SEPARATOR . strtolower($package) . '.reg';
  464. }
  465. /**
  466. * Get the name of the file where data for a given channel is stored.
  467. * @param string channel name
  468. * @return string registry file name
  469. */
  470. function _channelFileName($channel, $noaliases = false)
  471. {
  472. if (!$noaliases) {
  473. if (file_exists($this->_getChannelAliasFileName($channel))) {
  474. $channel = implode('', file($this->_getChannelAliasFileName($channel)));
  475. }
  476. }
  477. return $this->channelsdir . DIRECTORY_SEPARATOR . str_replace('/', '_',
  478. strtolower($channel)) . '.reg';
  479. }
  480. /**
  481. * @param string
  482. * @return string
  483. */
  484. function _getChannelAliasFileName($alias)
  485. {
  486. return $this->channelsdir . DIRECTORY_SEPARATOR . '.alias' .
  487. DIRECTORY_SEPARATOR . str_replace('/', '_', strtolower($alias)) . '.txt';
  488. }
  489. /**
  490. * Get the name of a channel from its alias
  491. */
  492. function _getChannelFromAlias($channel)
  493. {
  494. if (!$this->_channelExists($channel)) {
  495. if ($channel == 'pear.php.net') {
  496. return 'pear.php.net';
  497. }
  498. if ($channel == 'pecl.php.net') {
  499. return 'pecl.php.net';
  500. }
  501. if ($channel == 'doc.php.net') {
  502. return 'doc.php.net';
  503. }
  504. if ($channel == '__uri') {
  505. return '__uri';
  506. }
  507. return false;
  508. }
  509. $channel = strtolower($channel);
  510. if (file_exists($this->_getChannelAliasFileName($channel))) {
  511. // translate an alias to an actual channel
  512. return implode('', file($this->_getChannelAliasFileName($channel)));
  513. }
  514. return $channel;
  515. }
  516. /**
  517. * Get the alias of a channel from its alias or its name
  518. */
  519. function _getAlias($channel)
  520. {
  521. if (!$this->_channelExists($channel)) {
  522. if ($channel == 'pear.php.net') {
  523. return 'pear';
  524. }
  525. if ($channel == 'pecl.php.net') {
  526. return 'pecl';
  527. }
  528. if ($channel == 'doc.php.net') {
  529. return 'phpdocs';
  530. }
  531. return false;
  532. }
  533. $channel = $this->_getChannel($channel);
  534. if (PEAR::isError($channel)) {
  535. return $channel;
  536. }
  537. return $channel->getAlias();
  538. }
  539. /**
  540. * Get the name of the file where data for a given package is stored.
  541. *
  542. * @param string channel name, or false if this is a PEAR package
  543. * @param string package name
  544. *
  545. * @return string registry file name
  546. *
  547. * @access public
  548. */
  549. function _channelDirectoryName($channel)
  550. {
  551. if (!$channel || $this->_getChannelFromAlias($channel) == 'pear.php.net') {
  552. return $this->statedir;
  553. }
  554. $ch = $this->_getChannelFromAlias($channel);
  555. if (!$ch) {
  556. $ch = $channel;
  557. }
  558. return $this->statedir . DIRECTORY_SEPARATOR . strtolower('.channel.' .
  559. str_replace('/', '_', $ch));
  560. }
  561. function _openPackageFile($package, $mode, $channel = false)
  562. {
  563. if (!$this->_assertStateDir($channel)) {
  564. return null;
  565. }
  566. if (!in_array($mode, array('r', 'rb')) && !$this->hasWriteAccess()) {
  567. return null;
  568. }
  569. $file = $this->_packageFileName($package, $channel);
  570. if (!file_exists($file) && $mode == 'r' || $mode == 'rb') {
  571. return null;
  572. }
  573. $fp = @fopen($file, $mode);
  574. if (!$fp) {
  575. return null;
  576. }
  577. return $fp;
  578. }
  579. function _closePackageFile($fp)
  580. {
  581. fclose($fp);
  582. }
  583. function _openChannelFile($channel, $mode)
  584. {
  585. if (!$this->_assertChannelDir()) {
  586. return null;
  587. }
  588. if (!in_array($mode, array('r', 'rb')) && !$this->hasWriteAccess()) {
  589. return null;
  590. }
  591. $file = $this->_channelFileName($channel);
  592. if (!file_exists($file) && $mode == 'r' || $mode == 'rb') {
  593. return null;
  594. }
  595. $fp = @fopen($file, $mode);
  596. if (!$fp) {
  597. return null;
  598. }
  599. return $fp;
  600. }
  601. function _closeChannelFile($fp)
  602. {
  603. fclose($fp);
  604. }
  605. function _rebuildFileMap()
  606. {
  607. if (!class_exists('PEAR_Installer_Role')) {
  608. require_once 'PEAR/Installer/Role.php';
  609. }
  610. $channels = $this->_listAllPackages();
  611. $files = array();
  612. foreach ($channels as $channel => $packages) {
  613. foreach ($packages as $package) {
  614. $version = $this->_packageInfo($package, 'version', $channel);
  615. $filelist = $this->_packageInfo($package, 'filelist', $channel);
  616. if (!is_array($filelist)) {
  617. continue;
  618. }
  619. foreach ($filelist as $name => $attrs) {
  620. if (isset($attrs['attribs'])) {
  621. $attrs = $attrs['attribs'];
  622. }
  623. // it is possible for conflicting packages in different channels to
  624. // conflict with data files/doc files
  625. if ($name == 'dirtree') {
  626. continue;
  627. }
  628. if (isset($attrs['role']) && !in_array($attrs['role'],
  629. PEAR_Installer_Role::getInstallableRoles())) {
  630. // these are not installed
  631. continue;
  632. }
  633. if (isset($attrs['role']) && !in_array($attrs['role'],
  634. PEAR_Installer_Role::getBaseinstallRoles())) {
  635. $attrs['baseinstalldir'] = $package;
  636. }
  637. if (isset($attrs['baseinstalldir'])) {
  638. $file = $attrs['baseinstalldir'].DIRECTORY_SEPARATOR.$name;
  639. } else {
  640. $file = $name;
  641. }
  642. $file = preg_replace(',^/+,', '', $file);
  643. if ($channel != 'pear.php.net') {
  644. if (!isset($files[$attrs['role']])) {
  645. $files[$attrs['role']] = array();
  646. }
  647. $files[$attrs['role']][$file] = array(strtolower($channel),
  648. strtolower($package));
  649. } else {
  650. if (!isset($files[$attrs['role']])) {
  651. $files[$attrs['role']] = array();
  652. }
  653. $files[$attrs['role']][$file] = strtolower($package);
  654. }
  655. }
  656. }
  657. }
  658. $this->_assertStateDir();
  659. if (!$this->hasWriteAccess()) {
  660. return false;
  661. }
  662. $fp = @fopen($this->filemap, 'wb');
  663. if (!$fp) {
  664. return false;
  665. }
  666. $this->filemap_cache = $files;
  667. fwrite($fp, serialize($files));
  668. fclose($fp);
  669. return true;
  670. }
  671. function _readFileMap()
  672. {
  673. if (!file_exists($this->filemap)) {
  674. return array();
  675. }
  676. $fp = @fopen($this->filemap, 'r');
  677. if (!$fp) {
  678. return $this->raiseError('PEAR_Registry: could not open filemap "' . $this->filemap . '"', PEAR_REGISTRY_ERROR_FILE, null, null, $php_errormsg);
  679. }
  680. clearstatcache();
  681. $fsize = filesize($this->filemap);
  682. fclose($fp);
  683. $data = file_get_contents($this->filemap);
  684. $tmp = unserialize($data);
  685. if (!$tmp && $fsize > 7) {
  686. return $this->raiseError('PEAR_Registry: invalid filemap data', PEAR_REGISTRY_ERROR_FORMAT, null, null, $data);
  687. }
  688. $this->filemap_cache = $tmp;
  689. return true;
  690. }
  691. /**
  692. * Lock the registry.
  693. *
  694. * @param integer lock mode, one of LOCK_EX, LOCK_SH or LOCK_UN.
  695. * See flock manual for more information.
  696. *
  697. * @return bool TRUE on success, FALSE if locking failed, or a
  698. * PEAR error if some other error occurs (such as the
  699. * lock file not being writable).
  700. *
  701. * @access private
  702. */
  703. function _lock($mode = LOCK_EX)
  704. {
  705. if (stristr(php_uname(), 'Windows 9')) {
  706. return true;
  707. }
  708. if ($mode != LOCK_UN && is_resource($this->lock_fp)) {
  709. // XXX does not check type of lock (LOCK_SH/LOCK_EX)
  710. return true;
  711. }
  712. if (!$this->_assertStateDir()) {
  713. if ($mode == LOCK_EX) {
  714. return $this->raiseError('Registry directory is not writeable by the current user');
  715. }
  716. return true;
  717. }
  718. $open_mode = 'w';
  719. // XXX People reported problems with LOCK_SH and 'w'
  720. if ($mode === LOCK_SH || $mode === LOCK_UN) {
  721. if (!file_exists($this->lockfile)) {
  722. touch($this->lockfile);
  723. }
  724. $open_mode = 'r';
  725. }
  726. if (!is_resource($this->lock_fp)) {
  727. $this->lock_fp = @fopen($this->lockfile, $open_mode);
  728. }
  729. if (!is_resource($this->lock_fp)) {
  730. $this->lock_fp = null;
  731. return $this->raiseError("could not create lock file" .
  732. (isset($php_errormsg) ? ": " . $php_errormsg : ""));
  733. }
  734. if (!(int)flock($this->lock_fp, $mode)) {
  735. switch ($mode) {
  736. case LOCK_SH: $str = 'shared'; break;
  737. case LOCK_EX: $str = 'exclusive'; break;
  738. case LOCK_UN: $str = 'unlock'; break;
  739. default: $str = 'unknown'; break;
  740. }
  741. //is resource at this point, close it on error.
  742. fclose($this->lock_fp);
  743. $this->lock_fp = null;
  744. return $this->raiseError("could not acquire $str lock ($this->lockfile)",
  745. PEAR_REGISTRY_ERROR_LOCK);
  746. }
  747. return true;
  748. }
  749. function _unlock()
  750. {
  751. $ret = $this->_lock(LOCK_UN);
  752. if (is_resource($this->lock_fp)) {
  753. fclose($this->lock_fp);
  754. }
  755. $this->lock_fp = null;
  756. return $ret;
  757. }
  758. function _packageExists($package, $channel = false)
  759. {
  760. return file_exists($this->_packageFileName($package, $channel));
  761. }
  762. /**
  763. * Determine whether a channel exists in the registry
  764. *
  765. * @param string Channel name
  766. * @param bool if true, then aliases will be ignored
  767. * @return boolean
  768. */
  769. function _channelExists($channel, $noaliases = false)
  770. {
  771. $a = file_exists($this->_channelFileName($channel, $noaliases));
  772. if (!$a && $channel == 'pear.php.net') {
  773. return true;
  774. }
  775. if (!$a && $channel == 'pecl.php.net') {
  776. return true;
  777. }
  778. if (!$a && $channel == 'doc.php.net') {
  779. return true;
  780. }
  781. return $a;
  782. }
  783. /**
  784. * Determine whether a mirror exists within the default channel in the registry
  785. *
  786. * @param string Channel name
  787. * @param string Mirror name
  788. *
  789. * @return boolean
  790. */
  791. function _mirrorExists($channel, $mirror)
  792. {
  793. $data = $this->_channelInfo($channel);
  794. if (!isset($data['servers']['mirror'])) {
  795. return false;
  796. }
  797. foreach ($data['servers']['mirror'] as $m) {
  798. if ($m['attribs']['host'] == $mirror) {
  799. return true;
  800. }
  801. }
  802. return false;
  803. }
  804. /**
  805. * @param PEAR_ChannelFile Channel object
  806. * @param donotuse
  807. * @param string Last-Modified HTTP tag from remote request
  808. * @return boolean|PEAR_Error True on creation, false if it already exists
  809. */
  810. function _addChannel($channel, $update = false, $lastmodified = false)
  811. {
  812. if (!is_a($channel, 'PEAR_ChannelFile')) {
  813. return false;
  814. }
  815. if (!$channel->validate()) {
  816. return false;
  817. }
  818. if (file_exists($this->_channelFileName($channel->getName()))) {
  819. if (!$update) {
  820. return false;
  821. }
  822. $checker = $this->_getChannel($channel->getName());
  823. if (PEAR::isError($checker)) {
  824. return $checker;
  825. }
  826. if ($channel->getAlias() != $checker->getAlias()) {
  827. if (file_exists($this->_getChannelAliasFileName($checker->getAlias()))) {
  828. @unlink($this->_getChannelAliasFileName($checker->getAlias()));
  829. }
  830. }
  831. } else {
  832. if ($update && !in_array($channel->getName(), array('pear.php.net', 'pecl.php.net', 'doc.php.net'))) {
  833. return false;
  834. }
  835. }
  836. $ret = $this->_assertChannelDir();
  837. if (PEAR::isError($ret)) {
  838. return $ret;
  839. }
  840. $ret = $this->_assertChannelStateDir($channel->getName());
  841. if (PEAR::isError($ret)) {
  842. return $ret;
  843. }
  844. if ($channel->getAlias() != $channel->getName()) {
  845. if (file_exists($this->_getChannelAliasFileName($channel->getAlias())) &&
  846. $this->_getChannelFromAlias($channel->getAlias()) != $channel->getName()) {
  847. $channel->setAlias($channel->getName());
  848. }
  849. if (!$this->hasWriteAccess()) {
  850. return false;
  851. }
  852. $fp = @fopen($this->_getChannelAliasFileName($channel->getAlias()), 'w');
  853. if (!$fp) {
  854. return false;
  855. }
  856. fwrite($fp, $channel->getName());
  857. fclose($fp);
  858. }
  859. if (!$this->hasWriteAccess()) {
  860. return false;
  861. }
  862. $fp = @fopen($this->_channelFileName($channel->getName()), 'wb');
  863. if (!$fp) {
  864. return false;
  865. }
  866. $info = $channel->toArray();
  867. if ($lastmodified) {
  868. $info['_lastmodified'] = $lastmodified;
  869. } else {
  870. $info['_lastmodified'] = self::getSourceDateEpoch();
  871. }
  872. fwrite($fp, serialize($info));
  873. fclose($fp);
  874. return true;
  875. }
  876. /**
  877. * Deletion fails if there are any packages installed from the channel
  878. * @param string|PEAR_ChannelFile channel name
  879. * @return boolean|PEAR_Error True on deletion, false if it doesn't exist
  880. */
  881. function _deleteChannel($channel)
  882. {
  883. if (!is_string($channel)) {
  884. if (!is_a($channel, 'PEAR_ChannelFile')) {
  885. return false;
  886. }
  887. if (!$channel->validate()) {
  888. return false;
  889. }
  890. $channel = $channel->getName();
  891. }
  892. if ($this->_getChannelFromAlias($channel) == '__uri') {
  893. return false;
  894. }
  895. if ($this->_getChannelFromAlias($channel) == 'pecl.php.net') {
  896. return false;
  897. }
  898. if ($this->_getChannelFromAlias($channel) == 'doc.php.net') {
  899. return false;
  900. }
  901. if (!$this->_channelExists($channel)) {
  902. return false;
  903. }
  904. if (!$channel || $this->_getChannelFromAlias($channel) == 'pear.php.net') {
  905. return false;
  906. }
  907. $channel = $this->_getChannelFromAlias($channel);
  908. if ($channel == 'pear.php.net') {
  909. return false;
  910. }
  911. $test = $this->_listChannelPackages($channel);
  912. if (count($test)) {
  913. return false;
  914. }
  915. $test = @rmdir($this->_channelDirectoryName($channel));
  916. if (!$test) {
  917. return false;
  918. }
  919. $file = $this->_getChannelAliasFileName($this->_getAlias($channel));
  920. if (file_exists($file)) {
  921. $test = @unlink($file);
  922. if (!$test) {
  923. return false;
  924. }
  925. }
  926. $file = $this->_channelFileName($channel);
  927. $ret = true;
  928. if (file_exists($file)) {
  929. $ret = @unlink($file);
  930. }
  931. return $ret;
  932. }
  933. /**
  934. * Determine whether a channel exists in the registry
  935. * @param string Channel Alias
  936. * @return boolean
  937. */
  938. function _isChannelAlias($alias)
  939. {
  940. return file_exists($this->_getChannelAliasFileName($alias));
  941. }
  942. /**
  943. * @param string|null
  944. * @param string|null
  945. * @param string|null
  946. * @return array|null
  947. * @access private
  948. */
  949. function _packageInfo($package = null, $key = null, $channel = 'pear.php.net')
  950. {
  951. if ($package === null) {
  952. if ($channel === null) {
  953. $channels = $this->_listChannels();
  954. $ret = array();
  955. foreach ($channels as $channel) {
  956. $channel = strtolower($channel);
  957. $ret[$channel] = array();
  958. $packages = $this->_listPackages($channel);
  959. foreach ($packages as $package) {
  960. $ret[$channel][] = $this->_packageInfo($package, null, $channel);
  961. }
  962. }
  963. return $ret;
  964. }
  965. $ps = $this->_listPackages($channel);
  966. if (!count($ps)) {
  967. return array();
  968. }
  969. return array_map(array(&$this, '_packageInfo'),
  970. $ps, array_fill(0, count($ps), null),
  971. array_fill(0, count($ps), $channel));
  972. }
  973. $fp = $this->_openPackageFile($package, 'r', $channel);
  974. if ($fp === null) {
  975. return null;
  976. }
  977. clearstatcache();
  978. $this->_closePackageFile($fp);
  979. $data = file_get_contents($this->_packageFileName($package, $channel));
  980. $data = unserialize($data);
  981. if ($key === null) {
  982. return $data;
  983. }
  984. // compatibility for package.xml version 2.0
  985. if (isset($data['old'][$key])) {
  986. return $data['old'][$key];
  987. }
  988. if (isset($data[$key])) {
  989. return $data[$key];
  990. }
  991. return null;
  992. }
  993. /**
  994. * @param string Channel name
  995. * @param bool whether to strictly retrieve info of channels, not just aliases
  996. * @return array|null
  997. */
  998. function _channelInfo($channel, $noaliases = false)
  999. {
  1000. if (!$this->_channelExists($channel, $noaliases)) {
  1001. return null;
  1002. }
  1003. $fp = $this->_openChannelFile($channel, 'r');
  1004. if ($fp === null) {
  1005. return null;
  1006. }
  1007. clearstatcache();
  1008. $this->_closeChannelFile($fp);
  1009. $data = file_get_contents($this->_channelFileName($channel));
  1010. $data = unserialize($data);
  1011. return $data;
  1012. }
  1013. function _listChannels()
  1014. {
  1015. $channellist = array();
  1016. if (!file_exists($this->channelsdir) || !is_dir($this->channelsdir)) {
  1017. return array('pear.php.net', 'pecl.php.net', 'doc.php.net', '__uri');
  1018. }
  1019. $dp = opendir($this->channelsdir);
  1020. while ($ent = readdir($dp)) {
  1021. if ($ent[0] == '.' || substr($ent, -4) != '.reg') {
  1022. continue;
  1023. }
  1024. if ($ent == '__uri.reg') {
  1025. $channellist[] = '__uri';
  1026. continue;
  1027. }
  1028. $channellist[] = str_replace('_', '/', substr($ent, 0, -4));
  1029. }
  1030. closedir($dp);
  1031. if (!in_array('pear.php.net', $channellist)) {
  1032. $channellist[] = 'pear.php.net';
  1033. }
  1034. if (!in_array('pecl.php.net', $channellist)) {
  1035. $channellist[] = 'pecl.php.net';
  1036. }
  1037. if (!in_array('doc.php.net', $channellist)) {
  1038. $channellist[] = 'doc.php.net';
  1039. }
  1040. if (!in_array('__uri', $channellist)) {
  1041. $channellist[] = '__uri';
  1042. }
  1043. natsort($channellist);
  1044. return $channellist;
  1045. }
  1046. function _listPackages($channel = false)
  1047. {
  1048. if ($channel && $this->_getChannelFromAlias($channel) != 'pear.php.net') {
  1049. return $this->_listChannelPackages($channel);
  1050. }
  1051. if (!file_exists($this->statedir) || !is_dir($this->statedir)) {
  1052. return array();
  1053. }
  1054. $pkglist = array();
  1055. $dp = opendir($this->statedir);
  1056. if (!$dp) {
  1057. return $pkglist;
  1058. }
  1059. while ($ent = readdir($dp)) {
  1060. if ($ent[0] == '.' || substr($ent, -4) != '.reg') {
  1061. continue;
  1062. }
  1063. $pkglist[] = substr($ent, 0, -4);
  1064. }
  1065. closedir($dp);
  1066. return $pkglist;
  1067. }
  1068. function _listChannelPackages($channel)
  1069. {
  1070. $pkglist = array();
  1071. if (!file_exists($this->_channelDirectoryName($channel)) ||
  1072. !is_dir($this->_channelDirectoryName($channel))) {
  1073. return array();
  1074. }
  1075. $dp = opendir($this->_channelDirectoryName($channel));
  1076. if (!$dp) {
  1077. return $pkglist;
  1078. }
  1079. while ($ent = readdir($dp)) {
  1080. if ($ent[0] == '.' || substr($ent, -4) != '.reg') {
  1081. continue;
  1082. }
  1083. $pkglist[] = substr($ent, 0, -4);
  1084. }
  1085. closedir($dp);
  1086. return $pkglist;
  1087. }
  1088. function _listAllPackages()
  1089. {
  1090. $ret = array();
  1091. foreach ($this->_listChannels() as $channel) {
  1092. $ret[$channel] = $this->_listPackages($channel);
  1093. }
  1094. return $ret;
  1095. }
  1096. /**
  1097. * Add an installed package to the registry
  1098. * @param string package name
  1099. * @param array package info (parsed by PEAR_Common::infoFrom*() methods)
  1100. * @return bool success of saving
  1101. * @access private
  1102. */
  1103. function _addPackage($package, $info)
  1104. {
  1105. if ($this->_packageExists($package)) {
  1106. return false;
  1107. }
  1108. $fp = $this->_openPackageFile($package, 'wb');
  1109. if ($fp === null) {
  1110. return false;
  1111. }
  1112. $info['_lastmodified'] = self::getSourceDateEpoch();
  1113. fwrite($fp, serialize($info));
  1114. $this->_closePackageFile($fp);
  1115. if (isset($info['filelist'])) {
  1116. $this->_rebuildFileMap();
  1117. }
  1118. return true;
  1119. }
  1120. /**
  1121. * @param PEAR_PackageFile_v1|PEAR_PackageFile_v2
  1122. * @return bool
  1123. * @access private
  1124. */
  1125. function _addPackage2($info)
  1126. {
  1127. if (!is_a($info, 'PEAR_PackageFile_v1') && !is_a($info, 'PEAR_PackageFile_v2')) {
  1128. return false;
  1129. }
  1130. if (!$info->validate()) {
  1131. if (class_exists('PEAR_Common')) {
  1132. $ui = PEAR_Frontend::singleton();
  1133. if ($ui) {
  1134. foreach ($info->getValidationWarnings() as $err) {
  1135. $ui->log($err['message'], true);
  1136. }
  1137. }
  1138. }
  1139. return false;
  1140. }
  1141. $channel = $info->getChannel();
  1142. $package = $info->getPackage();
  1143. $save = $info;
  1144. if ($this->_packageExists($package, $channel)) {
  1145. return false;
  1146. }
  1147. if (!$this->_channelExists($channel, true)) {
  1148. return false;
  1149. }
  1150. $info = $info->toArray(true);
  1151. if (!$info) {
  1152. return false;
  1153. }
  1154. $fp = $this->_openPackageFile($package, 'wb', $channel);
  1155. if ($fp === null) {
  1156. return false;
  1157. }
  1158. $info['_lastmodified'] = self::getSourceDateEpoch();
  1159. fwrite($fp, serialize($info));
  1160. $this->_closePackageFile($fp);
  1161. $this->_rebuildFileMap();
  1162. return true;
  1163. }
  1164. /**
  1165. * @param string Package name
  1166. * @param array parsed package.xml 1.0
  1167. * @param bool this parameter is only here for BC. Don't use it.
  1168. * @access private
  1169. */
  1170. function _updatePackage($package, $info, $merge = true)
  1171. {
  1172. $oldinfo = $this->_packageInfo($package);
  1173. if (empty($oldinfo)) {
  1174. return false;
  1175. }
  1176. $fp = $this->_openPackageFile($package, 'w');
  1177. if ($fp === null) {
  1178. return false;
  1179. }
  1180. if (is_object($info)) {
  1181. $info = $info->toArray();
  1182. }
  1183. $info['_lastmodified'] = self::getSourceDateEpoch();
  1184. $newinfo = $info;
  1185. if ($merge) {
  1186. $info = array_merge($oldinfo, $info);
  1187. } else {
  1188. $diff = $info;
  1189. }
  1190. fwrite($fp, serialize($info));
  1191. $this->_closePackageFile($fp);
  1192. if (isset($newinfo['filelist'])) {
  1193. $this->_rebuildFileMap();
  1194. }
  1195. return true;
  1196. }
  1197. /**
  1198. * @param PEAR_PackageFile_v1|PEAR_PackageFile_v2
  1199. * @return bool
  1200. * @access private
  1201. */
  1202. function _updatePackage2($info)
  1203. {
  1204. if (!$this->_packageExists($info->getPackage(), $info->getChannel())) {
  1205. return false;
  1206. }
  1207. $fp = $this->_openPackageFile($info->getPackage(), 'w', $info->getChannel());
  1208. if ($fp === null) {
  1209. return false;
  1210. }
  1211. $save = $info;
  1212. $info = $save->getArray(true);
  1213. $info['_lastmodified'] = self::getSourceDateEpoch();
  1214. fwrite($fp, serialize($info));
  1215. $this->_closePackageFile($fp);
  1216. $this->_rebuildFileMap();
  1217. return true;
  1218. }
  1219. /**
  1220. * @param string Package name
  1221. * @param string Channel name
  1222. * @return PEAR_PackageFile_v1|PEAR_PackageFile_v2|null
  1223. * @access private
  1224. */
  1225. function &_getPackage($package, $channel = 'pear.php.net')
  1226. {
  1227. $info = $this->_packageInfo($package, null, $channel);
  1228. if ($info === null) {
  1229. return $info;
  1230. }
  1231. $a = $this->_config;
  1232. if (!$a) {
  1233. $this->_config = new PEAR_Config;
  1234. $this->_config->set('php_dir', $this->statedir);
  1235. }
  1236. if (!class_exists('PEAR_PackageFile')) {
  1237. require_once 'PEAR/PackageFile.php';
  1238. }
  1239. $pkg = new PEAR_PackageFile($this->_config);
  1240. $pf = &$pkg->fromArray($info);
  1241. return $pf;
  1242. }
  1243. /**
  1244. * @param string channel name
  1245. * @param bool whether to strictly retrieve channel names
  1246. * @return PEAR_ChannelFile|PEAR_Error
  1247. * @access private
  1248. */
  1249. function &_getChannel($channel, $noaliases = false)
  1250. {
  1251. $ch = false;
  1252. if ($this->_channelExists($channel, $noaliases)) {
  1253. $chinfo = $this->_channelInfo($channel, $noaliases);
  1254. if ($chinfo) {
  1255. if (!class_exists('PEAR_ChannelFile')) {
  1256. require_once 'PEAR/ChannelFile.php';
  1257. }
  1258. $ch = &PEAR_ChannelFile::fromArrayWithErrors($chinfo);
  1259. }
  1260. }
  1261. if ($ch) {
  1262. if ($ch->validate()) {
  1263. return $ch;
  1264. }
  1265. foreach ($ch->getErrors(true) as $err) {
  1266. $message = $err['message'] . "\n";
  1267. }
  1268. $ch = PEAR::raiseError($message);
  1269. return $ch;
  1270. }
  1271. if ($this->_getChannelFromAlias($channel) == 'pear.php.net') {
  1272. // the registry is not properly set up, so use defaults
  1273. if (!class_exists('PEAR_ChannelFile')) {
  1274. require_once 'PEAR/ChannelFile.php';
  1275. }
  1276. $pear_channel = new PEAR_ChannelFile;
  1277. $pear_channel->setServer('pear.php.net');
  1278. $pear_channel->setAlias('pear');
  1279. $pear_channel->setSummary('PHP Extension and Application Repository');
  1280. $pear_channel->setDefaultPEARProtocols();
  1281. $pear_channel->setBaseURL('REST1.0', 'http://pear.php.net/rest/');
  1282. $pear_channel->setBaseURL('REST1.1', 'http://pear.php.net/rest/');
  1283. $pear_channel->setBaseURL('REST1.3', 'http://pear.php.net/rest/');
  1284. return $pear_channel;
  1285. }
  1286. if ($this->_getChannelFromAlias($channel) == 'pecl.php.net') {
  1287. // the registry is not properly set up, so use defaults
  1288. if (!class_exists('PEAR_ChannelFile')) {
  1289. require_once 'PEAR/ChannelFile.php';
  1290. }
  1291. $pear_channel = new PEAR_ChannelFile;
  1292. $pear_channel->setServer('pecl.php.net');
  1293. $pear_channel->setAlias('pecl');
  1294. $pear_channel->setSummary('PHP Extension Community Library');
  1295. $pear_channel->setDefaultPEARProtocols();
  1296. $pear_channel->setBaseURL('REST1.0', 'http://pecl.php.net/rest/');
  1297. $pear_channel->setBaseURL('REST1.1', 'http://pecl.php.net/rest/');
  1298. $pear_channel->setValidationPackage('PEAR_Validator_PECL', '1.0');
  1299. return $pear_channel;
  1300. }
  1301. if ($this->_getChannelFromAlias($channel) == 'doc.php.net') {
  1302. // the registry is not properly set up, so use defaults
  1303. if (!class_exists('PEAR_ChannelFile')) {
  1304. require_once 'PEAR/ChannelFile.php';
  1305. }
  1306. $doc_channel = new PEAR_ChannelFile;
  1307. $doc_channel->setServer('doc.php.net');
  1308. $doc_channel->setAlias('phpdocs');
  1309. $doc_channel->setSummary('PHP Documentation Team');
  1310. $doc_channel->setDefaultPEARProtocols();
  1311. $doc_channel->setBaseURL('REST1.0', 'http://doc.php.net/rest/');
  1312. $doc_channel->setBaseURL('REST1.1', 'http://doc.php.net/rest/');
  1313. $doc_channel->setBaseURL('REST1.3', 'http://doc.php.net/rest/');
  1314. return $doc_channel;
  1315. }
  1316. if ($this->_getChannelFromAlias($channel) == '__uri') {
  1317. // the registry is not properly set up, so use defaults
  1318. if (!class_exists('PEAR_ChannelFile')) {
  1319. require_once 'PEAR/ChannelFile.php';
  1320. }
  1321. $private = new PEAR_ChannelFile;
  1322. $private->setName('__uri');
  1323. $private->setDefaultPEARProtocols();
  1324. $private->setBaseURL('REST1.0', '****');
  1325. $private->setSummary('Pseudo-channel for static packages');
  1326. return $private;
  1327. }
  1328. return $ch;
  1329. }
  1330. /**
  1331. * @param string Package name
  1332. * @param string Channel name
  1333. * @return bool
  1334. */
  1335. function packageExists($package, $channel = 'pear.php.net')
  1336. {
  1337. if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
  1338. return $e;
  1339. }
  1340. $ret = $this->_packageExists($package, $channel);
  1341. $this->_unlock();
  1342. return $ret;
  1343. }
  1344. // }}}
  1345. // {{{ channelExists()
  1346. /**
  1347. * @param string channel name
  1348. * @param bool if true, then aliases will be ignored
  1349. * @return bool
  1350. */
  1351. function channelExists($channel, $noaliases = false)
  1352. {
  1353. if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
  1354. return $e;
  1355. }
  1356. $ret = $this->_channelExists($channel, $noaliases);
  1357. $this->_unlock();
  1358. return $ret;
  1359. }
  1360. // }}}
  1361. /**
  1362. * @param string channel name mirror is in
  1363. * @param string mirror name
  1364. *
  1365. * @return bool
  1366. */
  1367. function mirrorExists($channel, $mirror)
  1368. {
  1369. if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
  1370. return $e;
  1371. }
  1372. $ret = $this->_mirrorExists($channel, $mirror);
  1373. $this->_unlock();
  1374. return $ret;
  1375. }
  1376. // {{{ isAlias()
  1377. /**
  1378. * Determines whether the parameter is an alias of a channel
  1379. * @param string
  1380. * @return bool
  1381. */
  1382. function isAlias($alias)
  1383. {
  1384. if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
  1385. return $e;
  1386. }
  1387. $ret = $this->_isChannelAlias($alias);
  1388. $this->_unlock();
  1389. return $ret;
  1390. }
  1391. // }}}
  1392. // {{{ packageInfo()
  1393. /**
  1394. * @param string|null
  1395. * @param string|null
  1396. * @param string
  1397. * @return array|null
  1398. */
  1399. function packageInfo($package = null, $key = null, $channel = 'pear.php.net')
  1400. {
  1401. if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
  1402. return $e;
  1403. }
  1404. $ret = $this->_packageInfo($package, $key, $channel);
  1405. $this->_unlock();
  1406. return $ret;
  1407. }
  1408. // }}}
  1409. // {{{ channelInfo()
  1410. /**
  1411. * Retrieve a raw array of channel data.
  1412. *
  1413. * Do not use this, instead use {@link getChannel()} for normal
  1414. * operations. Array structure is undefined in this method
  1415. * @param string channel name
  1416. * @param bool whether to strictly retrieve information only on non-aliases
  1417. * @return array|null|PEAR_Error
  1418. */
  1419. function channelInfo($channel = null, $noaliases = false)
  1420. {
  1421. if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
  1422. return $e;
  1423. }
  1424. $ret = $this->_channelInfo($channel, $noaliases);
  1425. $this->_unlock();
  1426. return $ret;
  1427. }
  1428. // }}}
  1429. /**
  1430. * @param string
  1431. */
  1432. function channelName($channel)
  1433. {
  1434. if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
  1435. return $e;
  1436. }
  1437. $ret = $this->_getChannelFromAlias($channel);
  1438. $this->_unlock();
  1439. return $ret;
  1440. }
  1441. /**
  1442. * @param string
  1443. */
  1444. function channelAlias($channel)
  1445. {
  1446. if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
  1447. return $e;
  1448. }
  1449. $ret = $this->_getAlias($channel);
  1450. $this->_unlock();
  1451. return $ret;
  1452. }
  1453. // {{{ listPackages()
  1454. function listPackages($channel = false)
  1455. {
  1456. if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
  1457. return $e;
  1458. }
  1459. $ret = $this->_listPackages($channel);
  1460. $this->_unlock();
  1461. return $ret;
  1462. }
  1463. // }}}
  1464. // {{{ listAllPackages()
  1465. function listAllPackages()
  1466. {
  1467. if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
  1468. return $e;
  1469. }
  1470. $ret = $this->_listAllPackages();
  1471. $this->_unlock();
  1472. return $ret;
  1473. }
  1474. // }}}
  1475. // {{{ listChannel()
  1476. function listChannels()
  1477. {
  1478. if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
  1479. return $e;
  1480. }
  1481. $ret = $this->_listChannels();
  1482. $this->_unlock();
  1483. return $ret;
  1484. }
  1485. // }}}
  1486. // {{{ addPackage()
  1487. /**
  1488. * Add an installed package to the registry
  1489. * @param string|PEAR_PackageFile_v1|PEAR_PackageFile_v2 package name or object
  1490. * that will be passed to {@link addPackage2()}
  1491. * @param array package info (parsed by PEAR_Common::infoFrom*() methods)
  1492. * @return bool success of saving
  1493. */
  1494. function addPackage($package, $info)
  1495. {
  1496. if (is_object($info)) {
  1497. return $this->addPackage2($info);
  1498. }
  1499. if (PEAR::isError($e = $this->_lock(LOCK_EX))) {
  1500. return $e;
  1501. }
  1502. $ret = $this->_addPackage($package, $info);
  1503. $this->_unlock();
  1504. if ($ret) {
  1505. if (!class_exists('PEAR_PackageFile_v1')) {
  1506. require_once 'PEAR/PackageFile/v1.php';
  1507. }
  1508. $pf = new PEAR_PackageFile_v1;
  1509. $pf->setConfig($this->_config);
  1510. $pf->fromArray($info);
  1511. $this->_dependencyDB->uninstallPackage($pf);
  1512. $this->_dependencyDB->installPackage($pf);
  1513. }
  1514. return $ret;
  1515. }
  1516. // }}}
  1517. // {{{ addPackage2()
  1518. function addPackage2($info)
  1519. {
  1520. if (!is_object($info)) {
  1521. return $this->addPackage($info['package'], $info);
  1522. }
  1523. if (PEAR::isError($e = $this->_lock(LOCK_EX))) {
  1524. return $e;
  1525. }
  1526. $ret = $this->_addPackage2($info);
  1527. $this->_unlock();
  1528. if ($ret) {
  1529. $this->_dependencyDB->uninstallPackage($info);
  1530. $this->_dependencyDB->installPackage($info);
  1531. }
  1532. return $ret;
  1533. }
  1534. // }}}
  1535. // {{{ updateChannel()
  1536. /**
  1537. * For future expandibility purposes, separate this
  1538. * @param PEAR_ChannelFile
  1539. */
  1540. function updateChannel($channel, $lastmodified = null)
  1541. {
  1542. if ($channel->getName() == '__uri') {
  1543. return false;
  1544. }
  1545. return $this->addChannel($channel, $lastmodified, true);
  1546. }
  1547. // }}}
  1548. // {{{ deleteChannel()
  1549. /**
  1550. * Deletion fails if there are any packages installed from the channel
  1551. * @param string|PEAR_ChannelFile channel name
  1552. * @return boolean|PEAR_Error True on deletion, false if it doesn't exist
  1553. */
  1554. function deleteChannel($channel)
  1555. {
  1556. if (PEAR::isError($e = $this->_lock(LOCK_EX))) {
  1557. return $e;
  1558. }
  1559. $ret = $this->_deleteChannel($channel);
  1560. $this->_unlock();
  1561. if ($ret && is_a($this->_config, 'PEAR_Config')) {
  1562. $this->_config->setChannels($this->listChannels());
  1563. }
  1564. return $ret;
  1565. }
  1566. // }}}
  1567. // {{{ addChannel()
  1568. /**
  1569. * @param PEAR_ChannelFile Channel object
  1570. * @param string Last-Modified header from HTTP for caching
  1571. * @return boolean|PEAR_Error True on creation, false if it already exists
  1572. */
  1573. function addChannel($channel, $lastmodified = false, $update = false)
  1574. {
  1575. if (!is_a($channel, 'PEAR_ChannelFile') || !$channel->validate()) {
  1576. return false;
  1577. }
  1578. if (PEAR::isError($e = $this->_lock(LOCK_EX))) {
  1579. return $e;
  1580. }
  1581. $ret = $this->_addChannel($channel, $update, $lastmodified);
  1582. $this->_unlock();
  1583. if (!$update && $ret && is_a($this->_config, 'PEAR_Config')) {
  1584. $this->_config->setChannels($this->listChannels());
  1585. }
  1586. return $ret;
  1587. }
  1588. // }}}
  1589. // {{{ deletePackage()
  1590. function deletePackage($package, $channel = 'pear.php.net')
  1591. {
  1592. if (PEAR::isError($e = $this->_lock(LOCK_EX))) {
  1593. return $e;
  1594. }
  1595. $file = $this->_packageFileName($package, $channel);
  1596. $ret = file_exists($file) ? @unlink($file) : false;
  1597. $this->_rebuildFileMap();
  1598. $this->_unlock();
  1599. $p = array('channel' => $channel, 'package' => $package);
  1600. $this->_dependencyDB->uninstallPackage($p);
  1601. return $ret;
  1602. }
  1603. // }}}
  1604. // {{{ updatePackage()
  1605. function updatePackage($package, $info, $merge = true)
  1606. {
  1607. if (is_object($info)) {
  1608. return $this->updatePackage2($info, $merge);
  1609. }
  1610. if (PEAR::isError($e = $this->_lock(LOCK_EX))) {
  1611. return $e;
  1612. }
  1613. $ret = $this->_updatePackage($package, $info, $merge);
  1614. $this->_unlock();
  1615. if ($ret) {
  1616. if (!class_exists('PEAR_PackageFile_v1')) {
  1617. require_once 'PEAR/PackageFile/v1.php';
  1618. }
  1619. $pf = new PEAR_PackageFile_v1;
  1620. $pf->setConfig($this->_config);
  1621. $pf->fromArray($this->packageInfo($package));
  1622. $this->_dependencyDB->uninstallPackage($pf);
  1623. $this->_dependencyDB->installPackage($pf);
  1624. }
  1625. return $ret;
  1626. }
  1627. // }}}
  1628. // {{{ updatePackage2()
  1629. function updatePackage2($info)
  1630. {
  1631. if (!is_object($info)) {
  1632. return $this->updatePackage($info['package'], $info, $merge);
  1633. }
  1634. if (!$info->validate(PEAR_VALIDATE_DOWNLOADING)) {
  1635. return false;
  1636. }
  1637. if (PEAR::isError($e = $this->_lock(LOCK_EX))) {
  1638. return $e;
  1639. }
  1640. $ret = $this->_updatePackage2($info);
  1641. $this->_unlock();
  1642. if ($ret) {
  1643. $this->_dependencyDB->uninstallPackage($info);
  1644. $this->_dependencyDB->installPackage($info);
  1645. }
  1646. return $ret;
  1647. }
  1648. // }}}
  1649. // {{{ getChannel()
  1650. /**
  1651. * @param string channel name
  1652. * @param bool whether to strictly return raw channels (no aliases)
  1653. * @return PEAR_ChannelFile|PEAR_Error
  1654. */
  1655. function getChannel($channel, $noaliases = false)
  1656. {
  1657. if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
  1658. return $e;
  1659. }
  1660. $ret = $this->_getChannel($channel, $noaliases);
  1661. $this->_unlock();
  1662. if (!$ret) {
  1663. return PEAR::raiseError('Unknown channel: ' . $channel);
  1664. }
  1665. return $ret;
  1666. }
  1667. // }}}
  1668. // {{{ getPackage()
  1669. /**
  1670. * @param string package name
  1671. * @param string channel name
  1672. * @return PEAR_PackageFile_v1|PEAR_PackageFile_v2|null
  1673. */
  1674. function &getPackage($package, $channel = 'pear.php.net')
  1675. {
  1676. if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
  1677. return $e;
  1678. }
  1679. $pf = &$this->_getPackage($package, $channel);
  1680. $this->_unlock();
  1681. return $pf;
  1682. }
  1683. // }}}
  1684. /**
  1685. * Get PEAR_PackageFile_v[1/2] objects representing the contents of
  1686. * a dependency group that are installed.
  1687. *
  1688. * This is used at uninstall-time
  1689. * @param array
  1690. * @return array|false
  1691. */
  1692. function getInstalledGroup($group)
  1693. {
  1694. $ret = array();
  1695. if (isset($group['package'])) {
  1696. if (!isset($group['package'][0])) {
  1697. $group['package'] = array($group['package']);
  1698. }
  1699. foreach ($group['package'] as $package) {
  1700. $depchannel = isset($package['channel']) ? $package['channel'] : '__uri';
  1701. $p = &$this->getPackage($package['name'], $depchannel);
  1702. if ($p) {
  1703. $save = &$p;
  1704. $ret[] = &$save;
  1705. }
  1706. }
  1707. }
  1708. if (isset($group['subpackage'])) {
  1709. if (!isset($group['subpackage'][0])) {
  1710. $group['subpackage'] = array($group['subpackage']);
  1711. }
  1712. foreach ($group['subpackage'] as $package) {
  1713. $depchannel = isset($package['channel']) ? $package['channel'] : '__uri';
  1714. $p = &$this->getPackage($package['name'], $depchannel);
  1715. if ($p) {
  1716. $save = &$p;
  1717. $ret[] = &$save;
  1718. }
  1719. }
  1720. }
  1721. if (!count($ret)) {
  1722. return false;
  1723. }
  1724. return $ret;
  1725. }
  1726. // {{{ getChannelValidator()
  1727. /**
  1728. * @param string channel name
  1729. * @return PEAR_Validate|false
  1730. */
  1731. function &getChannelValidator($channel)
  1732. {
  1733. $chan = $this->getChannel($channel);
  1734. if (PEAR::isError($chan)) {
  1735. return $chan;
  1736. }
  1737. $val = $chan->getValidationObject();
  1738. return $val;
  1739. }
  1740. // }}}
  1741. // {{{ getChannels()
  1742. /**
  1743. * @param string channel name
  1744. * @return array an array of PEAR_ChannelFile objects representing every installed channel
  1745. */
  1746. function &getChannels()
  1747. {
  1748. $ret = array();
  1749. if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
  1750. return $e;
  1751. }
  1752. foreach ($this->_listChannels() as $channel) {
  1753. $e = &$this->_getChannel($channel);
  1754. if (!$e || PEAR::isError($e)) {
  1755. continue;
  1756. }
  1757. $ret[] = $e;
  1758. }
  1759. $this->_unlock();
  1760. return $ret;
  1761. }
  1762. // }}}
  1763. // {{{ checkFileMap()
  1764. /**
  1765. * Test whether a file or set of files belongs to a package.
  1766. *
  1767. * If an array is passed in
  1768. * @param string|array file path, absolute or relative to the pear
  1769. * install dir
  1770. * @param string|array name of PEAR package or array('package' => name, 'channel' =>
  1771. * channel) of a package that will be ignored
  1772. * @param string API version - 1.1 will exclude any files belonging to a package
  1773. * @param array private recursion variable
  1774. * @return array|false which package and channel the file belongs to, or an empty
  1775. * string if the file does not belong to an installed package,
  1776. * or belongs to the second parameter's package
  1777. */
  1778. function checkFileMap($path, $package = false, $api = '1.0', $attrs = false)
  1779. {
  1780. if (is_array($path)) {
  1781. static $notempty;
  1782. if (empty($notempty)) {
  1783. if (!class_exists('PEAR_Installer_Role')) {
  1784. require_once 'PEAR/Installer/Role.php';
  1785. }
  1786. $notempty = function($a) { return !empty($a); };
  1787. }
  1788. $package = is_array($package) ? array(strtolower($package[0]), strtolower($package[1]))
  1789. : strtolower($package);
  1790. $pkgs = array();
  1791. foreach ($path as $name => $attrs) {
  1792. if (is_array($attrs)) {
  1793. if (isset($attrs['install-as'])) {
  1794. $name = $attrs['install-as'];
  1795. }
  1796. if (!in_array($attrs['role'], PEAR_Installer_Role::getInstallableRoles())) {
  1797. // these are not installed
  1798. continue;
  1799. }
  1800. if (!in_array($attrs['role'], PEAR_Installer_Role::getBaseinstallRoles())) {
  1801. $attrs['baseinstalldir'] = is_array($package) ? $package[1] : $package;
  1802. }
  1803. if (isset($attrs['baseinstalldir'])) {
  1804. $name = $attrs['baseinstalldir'] . DIRECTORY_SEPARATOR . $name;
  1805. }
  1806. }
  1807. $pkgs[$name] = $this->checkFileMap($name, $package, $api, $attrs);
  1808. if (PEAR::isError($pkgs[$name])) {
  1809. return $pkgs[$name];
  1810. }
  1811. }
  1812. return array_filter($pkgs, $notempty);
  1813. }
  1814. if (empty($this->filemap_cache)) {
  1815. if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
  1816. return $e;
  1817. }
  1818. $err = $this->_readFileMap();
  1819. $this->_unlock();
  1820. if (PEAR::isError($err)) {
  1821. return $err;
  1822. }
  1823. }
  1824. if (!$attrs) {
  1825. $attrs = array('role' => 'php'); // any old call would be for PHP role only
  1826. }
  1827. if (isset($this->filemap_cache[$attrs['role']][$path])) {
  1828. if ($api >= '1.1' && $this->filemap_cache[$attrs['role']][$path] == $package) {
  1829. return false;
  1830. }
  1831. return $this->filemap_cache[$attrs['role']][$path];
  1832. }
  1833. $l = strlen($this->install_dir);
  1834. if (substr($path, 0, $l) == $this->install_dir) {
  1835. $path = preg_replace('!^'.DIRECTORY_SEPARATOR.'+!', '', substr($path, $l));
  1836. }
  1837. if (isset($this->filemap_cache[$attrs['role']][$path])) {
  1838. if ($api >= '1.1' && $this->filemap_cache[$attrs['role']][$path] == $package) {
  1839. return false;
  1840. }
  1841. return $this->filemap_cache[$attrs['role']][$path];
  1842. }
  1843. return false;
  1844. }
  1845. // }}}
  1846. // {{{ flush()
  1847. /**
  1848. * Force a reload of the filemap
  1849. * @since 1.5.0RC3
  1850. */
  1851. function flushFileMap()
  1852. {
  1853. $this->filemap_cache = null;
  1854. clearstatcache(); // ensure that the next read gets the full, current filemap
  1855. }
  1856. // }}}
  1857. // {{{ apiVersion()
  1858. /**
  1859. * Get the expected API version. Channels API is version 1.1, as it is backwards
  1860. * compatible with 1.0
  1861. * @return string
  1862. */
  1863. function apiVersion()
  1864. {
  1865. return '1.1';
  1866. }
  1867. // }}}
  1868. /**
  1869. * Parse a package name, or validate a parsed package name array
  1870. * @param string|array pass in an array of format
  1871. * array(
  1872. * 'package' => 'pname',
  1873. * ['channel' => 'channame',]
  1874. * ['version' => 'version',]
  1875. * ['state' => 'state',]
  1876. * ['group' => 'groupname'])
  1877. * or a string of format
  1878. * [channel://][channame/]pname[-version|-state][/group=groupname]
  1879. * @return array|PEAR_Error
  1880. */
  1881. function parsePackageName($param, $defaultchannel = 'pear.php.net')
  1882. {
  1883. $saveparam = $param;
  1884. if (is_array($param)) {
  1885. // convert to string for error messages
  1886. $saveparam = $this->parsedPackageNameToString($param);
  1887. // process the array
  1888. if (!isset($param['package'])) {
  1889. return PEAR::raiseError('parsePackageName(): array $param ' .
  1890. 'must contain a valid package name in index "param"',
  1891. 'package', null, null, $param);
  1892. }
  1893. if (!isset($param['uri'])) {
  1894. if (!isset($param['channel'])) {
  1895. $param['channel'] = $defaultchannel;
  1896. }
  1897. } else {
  1898. $param['channel'] = '__uri';
  1899. }
  1900. } else {
  1901. $components = @parse_url((string) $param);
  1902. if (isset($components['scheme'])) {
  1903. if ($components['scheme'] == 'http') {
  1904. // uri package
  1905. $param = array('uri' => $param, 'channel' => '__uri');
  1906. } elseif($components['scheme'] != 'channel') {
  1907. return PEAR::raiseError('parsePackageName(): only channel:// uris may ' .
  1908. 'be downloaded, not "' . $param . '"', 'invalid', null, null, $param);
  1909. }
  1910. }
  1911. if (!isset($components['path'])) {
  1912. return PEAR::raiseError('parsePackageName(): array $param ' .
  1913. 'must contain a valid package name in "' . $param . '"',
  1914. 'package', null, null, $param);
  1915. }
  1916. if (isset($components['host'])) {
  1917. // remove the leading "/"
  1918. $components['path'] = substr($components['path'], 1);
  1919. }
  1920. if (!isset($components['scheme'])) {
  1921. if (strpos($components['path'], '/') !== false) {
  1922. if ($components['path'][0] == '/') {
  1923. return PEAR::raiseError('parsePackageName(): this is not ' .
  1924. 'a package name, it begins with "/" in "' . $param . '"',
  1925. 'invalid', null, null, $param);
  1926. }
  1927. $parts = explode('/', $components['path']);
  1928. $components['host'] = array_shift($parts);
  1929. if (count($parts) > 1) {
  1930. $components['path'] = array_pop($parts);
  1931. $components['host'] .= '/' . implode('/', $parts);
  1932. } else {
  1933. $components['path'] = implode('/', $parts);
  1934. }
  1935. } else {
  1936. $components['host'] = $defaultchannel;
  1937. }
  1938. } else {
  1939. if (strpos($components['path'], '/')) {
  1940. $parts = explode('/', $components['path']);
  1941. $components['path'] = array_pop($parts);
  1942. $components['host'] .= '/' . implode('/', $parts);
  1943. }
  1944. }
  1945. if (is_array($param)) {
  1946. $param['package'] = $components['path'];
  1947. } else {
  1948. $param = array(
  1949. 'package' => $components['path']
  1950. );
  1951. if (isset($components['host'])) {
  1952. $param['channel'] = $components['host'];
  1953. }
  1954. }
  1955. if (isset($components['fragment'])) {
  1956. $param['group'] = $components['fragment'];
  1957. }
  1958. if (isset($components['user'])) {
  1959. $param['user'] = $components['user'];
  1960. }
  1961. if (isset($components['pass'])) {
  1962. $param['pass'] = $components['pass'];
  1963. }
  1964. if (isset($components['query'])) {
  1965. parse_str($components['query'], $param['opts']);
  1966. }
  1967. // check for extension
  1968. $pathinfo = pathinfo($param['package']);
  1969. if (isset($pathinfo['extension']) &&
  1970. in_array(strtolower($pathinfo['extension']), array('tgz', 'tar'))) {
  1971. $param['extension'] = $pathinfo['extension'];
  1972. $param['package'] = substr($pathinfo['basename'], 0,
  1973. strlen($pathinfo['basename']) - 4);
  1974. }
  1975. // check for version
  1976. if (strpos($param['package'], '-')) {
  1977. $test = explode('-', $param['package']);
  1978. if (count($test) != 2) {
  1979. return PEAR::raiseError('parsePackageName(): only one version/state ' .
  1980. 'delimiter "-" is allowed in "' . $saveparam . '"',
  1981. 'version', null, null, $param);
  1982. }
  1983. list($param['package'], $param['version']) = $test;
  1984. }
  1985. }
  1986. // validation
  1987. $info = $this->channelExists($param['channel']);
  1988. if (PEAR::isError($info)) {
  1989. return $info;
  1990. }
  1991. if (!$info) {
  1992. return PEAR::raiseError('unknown channel "' . $param['channel'] .
  1993. '" in "' . $saveparam . '"', 'channel', null, null, $param);
  1994. }
  1995. $chan = $this->getChannel($param['channel']);
  1996. if (PEAR::isError($chan)) {
  1997. return $chan;
  1998. }
  1999. if (!$chan) {
  2000. return PEAR::raiseError("Exception: corrupt registry, could not " .
  2001. "retrieve channel " . $param['channel'] . " information",
  2002. 'registry', null, null, $param);
  2003. }
  2004. $param['channel'] = $chan->getName();
  2005. $validate = $chan->getValidationObject();
  2006. $vpackage = $chan->getValidationPackage();
  2007. // validate package name
  2008. if (!$validate->validPackageName($param['package'], $vpackage['_content'])) {
  2009. return PEAR::raiseError('parsePackageName(): invalid package name "' .
  2010. $param['package'] . '" in "' . $saveparam . '"',
  2011. 'package', null, null, $param);
  2012. }
  2013. if (isset($param['group'])) {
  2014. if (!PEAR_Validate::validGroupName($param['group'])) {
  2015. return PEAR::raiseError('parsePackageName(): dependency group "' . $param['group'] .
  2016. '" is not a valid group name in "' . $saveparam . '"', 'group', null, null,
  2017. $param);
  2018. }
  2019. }
  2020. if (isset($param['state'])) {
  2021. if (!in_array(strtolower($param['state']), $validate->getValidStates())) {
  2022. return PEAR::raiseError('parsePackageName(): state "' . $param['state']
  2023. . '" is not a valid state in "' . $saveparam . '"',
  2024. 'state', null, null, $param);
  2025. }
  2026. }
  2027. if (isset($param['version'])) {
  2028. if (isset($param['state'])) {
  2029. return PEAR::raiseError('parsePackageName(): cannot contain both ' .
  2030. 'a version and a stability (state) in "' . $saveparam . '"',
  2031. 'version/state', null, null, $param);
  2032. }
  2033. // check whether version is actually a state
  2034. if (in_array(strtolower($param['version']), $validate->getValidStates())) {
  2035. $param['state'] = strtolower($param['version']);
  2036. unset($param['version']);
  2037. } else {
  2038. if (!$validate->validVersion($param['version'])) {
  2039. return PEAR::raiseError('parsePackageName(): "' . $param['version'] .
  2040. '" is neither a valid version nor a valid state in "' .
  2041. $saveparam . '"', 'version/state', null, null, $param);
  2042. }
  2043. }
  2044. }
  2045. return $param;
  2046. }
  2047. /**
  2048. * @param array
  2049. * @return string
  2050. */
  2051. function parsedPackageNameToString($parsed, $brief = false)
  2052. {
  2053. if (is_string($parsed)) {
  2054. return $parsed;
  2055. }
  2056. if (is_object($parsed)) {
  2057. $p = $parsed;
  2058. $parsed = array(
  2059. 'package' => $p->getPackage(),
  2060. 'channel' => $p->getChannel(),
  2061. 'version' => $p->getVersion(),
  2062. );
  2063. }
  2064. if (isset($parsed['uri'])) {
  2065. return $parsed['uri'];
  2066. }
  2067. if ($brief) {
  2068. if ($channel = $this->channelAlias($parsed['channel'])) {
  2069. return $channel . '/' . $parsed['package'];
  2070. }
  2071. }
  2072. $upass = '';
  2073. if (isset($parsed['user'])) {
  2074. $upass = $parsed['user'];
  2075. if (isset($parsed['pass'])) {
  2076. $upass .= ':' . $parsed['pass'];
  2077. }
  2078. $upass = "$upass@";
  2079. }
  2080. $ret = 'channel://' . $upass . $parsed['channel'] . '/' . $parsed['package'];
  2081. if (isset($parsed['version']) || isset($parsed['state'])) {
  2082. $ver = isset($parsed['version']) ? $parsed['version'] : '';
  2083. $ver .= isset($parsed['state']) ? $parsed['state'] : '';
  2084. $ret .= '-' . $ver;
  2085. }
  2086. if (isset($parsed['extension'])) {
  2087. $ret .= '.' . $parsed['extension'];
  2088. }
  2089. if (isset($parsed['opts'])) {
  2090. $ret .= '?';
  2091. foreach ($parsed['opts'] as $name => $value) {
  2092. $parsed['opts'][$name] = "$name=$value";
  2093. }
  2094. $ret .= implode('&', $parsed['opts']);
  2095. }
  2096. if (isset($parsed['group'])) {
  2097. $ret .= '#' . $parsed['group'];
  2098. }
  2099. return $ret;
  2100. }
  2101. }