Registry.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 5 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available through the world-wide-web at the following url: |
  11. // | http://www.php.net/license/3_0.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | [email protected] so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Stig Bakken <[email protected]> |
  17. // | Tomas V.V.Cox <[email protected]> |
  18. // | |
  19. // +----------------------------------------------------------------------+
  20. //
  21. // $Id: Registry.php,v 1.50.4.3 2004/10/26 19:19:56 cellog Exp $
  22. /*
  23. TODO:
  24. - Transform into singleton()
  25. - Add application level lock (avoid change the registry from the cmdline
  26. while using the GTK interface, for ex.)
  27. */
  28. require_once "System.php";
  29. require_once "PEAR.php";
  30. define('PEAR_REGISTRY_ERROR_LOCK', -2);
  31. define('PEAR_REGISTRY_ERROR_FORMAT', -3);
  32. define('PEAR_REGISTRY_ERROR_FILE', -4);
  33. /**
  34. * Administration class used to maintain the installed package database.
  35. */
  36. class PEAR_Registry extends PEAR
  37. {
  38. // {{{ properties
  39. /** Directory where registry files are stored.
  40. * @var string
  41. */
  42. var $statedir = '';
  43. /** File where the file map is stored
  44. * @var string
  45. */
  46. var $filemap = '';
  47. /** Name of file used for locking the registry
  48. * @var string
  49. */
  50. var $lockfile = '';
  51. /** File descriptor used during locking
  52. * @var resource
  53. */
  54. var $lock_fp = null;
  55. /** Mode used during locking
  56. * @var int
  57. */
  58. var $lock_mode = 0; // XXX UNUSED
  59. /** Cache of package information. Structure:
  60. * array(
  61. * 'package' => array('id' => ... ),
  62. * ... )
  63. * @var array
  64. */
  65. var $pkginfo_cache = array();
  66. /** Cache of file map. Structure:
  67. * array( '/path/to/file' => 'package', ... )
  68. * @var array
  69. */
  70. var $filemap_cache = array();
  71. // }}}
  72. // {{{ constructor
  73. /**
  74. * PEAR_Registry constructor.
  75. *
  76. * @param string (optional) PEAR install directory (for .php files)
  77. *
  78. * @access public
  79. */
  80. function PEAR_Registry($pear_install_dir = PEAR_INSTALL_DIR)
  81. {
  82. parent::PEAR();
  83. $ds = DIRECTORY_SEPARATOR;
  84. $this->install_dir = $pear_install_dir;
  85. $this->statedir = $pear_install_dir.$ds.'.registry';
  86. $this->filemap = $pear_install_dir.$ds.'.filemap';
  87. $this->lockfile = $pear_install_dir.$ds.'.lock';
  88. // XXX Compatibility code should be removed in the future
  89. // rename all registry files if any to lowercase
  90. if (!OS_WINDOWS && $handle = @opendir($this->statedir)) {
  91. $dest = $this->statedir . DIRECTORY_SEPARATOR;
  92. while (false !== ($file = readdir($handle))) {
  93. if (preg_match('/^.*[A-Z].*\.reg$/', $file)) {
  94. rename($dest . $file, $dest . strtolower($file));
  95. }
  96. }
  97. closedir($handle);
  98. }
  99. if (!file_exists($this->filemap)) {
  100. $this->rebuildFileMap();
  101. }
  102. }
  103. // }}}
  104. // {{{ destructor
  105. /**
  106. * PEAR_Registry destructor. Makes sure no locks are forgotten.
  107. *
  108. * @access private
  109. */
  110. function _PEAR_Registry()
  111. {
  112. parent::_PEAR();
  113. if (is_resource($this->lock_fp)) {
  114. $this->_unlock();
  115. }
  116. }
  117. // }}}
  118. // {{{ _assertStateDir()
  119. /**
  120. * Make sure the directory where we keep registry files exists.
  121. *
  122. * @return bool TRUE if directory exists, FALSE if it could not be
  123. * created
  124. *
  125. * @access private
  126. */
  127. function _assertStateDir()
  128. {
  129. if (!@is_dir($this->statedir)) {
  130. if (!System::mkdir(array('-p', $this->statedir))) {
  131. return $this->raiseError("could not create directory '{$this->statedir}'");
  132. }
  133. }
  134. return true;
  135. }
  136. // }}}
  137. // {{{ _packageFileName()
  138. /**
  139. * Get the name of the file where data for a given package is stored.
  140. *
  141. * @param string package name
  142. *
  143. * @return string registry file name
  144. *
  145. * @access public
  146. */
  147. function _packageFileName($package)
  148. {
  149. return $this->statedir . DIRECTORY_SEPARATOR . strtolower($package) . '.reg';
  150. }
  151. // }}}
  152. // {{{ _openPackageFile()
  153. function _openPackageFile($package, $mode)
  154. {
  155. $this->_assertStateDir();
  156. $file = $this->_packageFileName($package);
  157. $fp = @fopen($file, $mode);
  158. if (!$fp) {
  159. return null;
  160. }
  161. return $fp;
  162. }
  163. // }}}
  164. // {{{ _closePackageFile()
  165. function _closePackageFile($fp)
  166. {
  167. fclose($fp);
  168. }
  169. // }}}
  170. // {{{ rebuildFileMap()
  171. function rebuildFileMap()
  172. {
  173. $packages = $this->listPackages();
  174. $files = array();
  175. foreach ($packages as $package) {
  176. $version = $this->packageInfo($package, 'version');
  177. $filelist = $this->packageInfo($package, 'filelist');
  178. if (!is_array($filelist)) {
  179. continue;
  180. }
  181. foreach ($filelist as $name => $attrs) {
  182. if (isset($attrs['role']) && $attrs['role'] != 'php') {
  183. continue;
  184. }
  185. if (isset($attrs['baseinstalldir'])) {
  186. $file = $attrs['baseinstalldir'].DIRECTORY_SEPARATOR.$name;
  187. } else {
  188. $file = $name;
  189. }
  190. $file = preg_replace(',^/+,', '', $file);
  191. $files[$file] = $package;
  192. }
  193. }
  194. $this->_assertStateDir();
  195. $fp = @fopen($this->filemap, 'wb');
  196. if (!$fp) {
  197. return false;
  198. }
  199. $this->filemap_cache = $files;
  200. fwrite($fp, serialize($files));
  201. fclose($fp);
  202. return true;
  203. }
  204. // }}}
  205. // {{{ readFileMap()
  206. function readFileMap()
  207. {
  208. $fp = @fopen($this->filemap, 'r');
  209. if (!$fp) {
  210. return $this->raiseError('PEAR_Registry: could not open filemap', PEAR_REGISTRY_ERROR_FILE, null, null, $php_errormsg);
  211. }
  212. $fsize = filesize($this->filemap);
  213. $rt = get_magic_quotes_runtime();
  214. set_magic_quotes_runtime(0);
  215. $data = fread($fp, $fsize);
  216. set_magic_quotes_runtime($rt);
  217. fclose($fp);
  218. $tmp = unserialize($data);
  219. if (!$tmp && $fsize > 7) {
  220. return $this->raiseError('PEAR_Registry: invalid filemap data', PEAR_REGISTRY_ERROR_FORMAT, null, null, $data);
  221. }
  222. $this->filemap_cache = $tmp;
  223. return true;
  224. }
  225. // }}}
  226. // {{{ _lock()
  227. /**
  228. * Lock the registry.
  229. *
  230. * @param integer lock mode, one of LOCK_EX, LOCK_SH or LOCK_UN.
  231. * See flock manual for more information.
  232. *
  233. * @return bool TRUE on success, FALSE if locking failed, or a
  234. * PEAR error if some other error occurs (such as the
  235. * lock file not being writable).
  236. *
  237. * @access private
  238. */
  239. function _lock($mode = LOCK_EX)
  240. {
  241. if (!eregi('Windows 9', php_uname())) {
  242. if ($mode != LOCK_UN && is_resource($this->lock_fp)) {
  243. // XXX does not check type of lock (LOCK_SH/LOCK_EX)
  244. return true;
  245. }
  246. if (PEAR::isError($err = $this->_assertStateDir())) {
  247. return $err;
  248. }
  249. $open_mode = 'w';
  250. // XXX People reported problems with LOCK_SH and 'w'
  251. if ($mode === LOCK_SH || $mode === LOCK_UN) {
  252. if (@!is_file($this->lockfile)) {
  253. touch($this->lockfile);
  254. }
  255. $open_mode = 'r';
  256. }
  257. if (!is_resource($this->lock_fp)) {
  258. $this->lock_fp = @fopen($this->lockfile, $open_mode);
  259. }
  260. if (!is_resource($this->lock_fp)) {
  261. return $this->raiseError("could not create lock file" .
  262. (isset($php_errormsg) ? ": " . $php_errormsg : ""));
  263. }
  264. if (!(int)flock($this->lock_fp, $mode)) {
  265. switch ($mode) {
  266. case LOCK_SH: $str = 'shared'; break;
  267. case LOCK_EX: $str = 'exclusive'; break;
  268. case LOCK_UN: $str = 'unlock'; break;
  269. default: $str = 'unknown'; break;
  270. }
  271. return $this->raiseError("could not acquire $str lock ($this->lockfile)",
  272. PEAR_REGISTRY_ERROR_LOCK);
  273. }
  274. }
  275. return true;
  276. }
  277. // }}}
  278. // {{{ _unlock()
  279. function _unlock()
  280. {
  281. $ret = $this->_lock(LOCK_UN);
  282. if (is_resource($this->lock_fp)) {
  283. fclose($this->lock_fp);
  284. }
  285. $this->lock_fp = null;
  286. return $ret;
  287. }
  288. // }}}
  289. // {{{ _packageExists()
  290. function _packageExists($package)
  291. {
  292. return file_exists($this->_packageFileName($package));
  293. }
  294. // }}}
  295. // {{{ _packageInfo()
  296. function _packageInfo($package = null, $key = null)
  297. {
  298. if ($package === null) {
  299. return array_map(array($this, '_packageInfo'),
  300. $this->_listPackages());
  301. }
  302. $fp = $this->_openPackageFile($package, 'r');
  303. if ($fp === null) {
  304. return null;
  305. }
  306. $rt = get_magic_quotes_runtime();
  307. set_magic_quotes_runtime(0);
  308. $data = fread($fp, filesize($this->_packageFileName($package)));
  309. set_magic_quotes_runtime($rt);
  310. $this->_closePackageFile($fp);
  311. $data = unserialize($data);
  312. if ($key === null) {
  313. return $data;
  314. }
  315. if (isset($data[$key])) {
  316. return $data[$key];
  317. }
  318. return null;
  319. }
  320. // }}}
  321. // {{{ _listPackages()
  322. function _listPackages()
  323. {
  324. $pkglist = array();
  325. $dp = @opendir($this->statedir);
  326. if (!$dp) {
  327. return $pkglist;
  328. }
  329. while ($ent = readdir($dp)) {
  330. if ($ent{0} == '.' || substr($ent, -4) != '.reg') {
  331. continue;
  332. }
  333. $pkglist[] = substr($ent, 0, -4);
  334. }
  335. return $pkglist;
  336. }
  337. // }}}
  338. // {{{ packageExists()
  339. function packageExists($package)
  340. {
  341. if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
  342. return $e;
  343. }
  344. $ret = $this->_packageExists($package);
  345. $this->_unlock();
  346. return $ret;
  347. }
  348. // }}}
  349. // {{{ packageInfo()
  350. function packageInfo($package = null, $key = null)
  351. {
  352. if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
  353. return $e;
  354. }
  355. $ret = $this->_packageInfo($package, $key);
  356. $this->_unlock();
  357. return $ret;
  358. }
  359. // }}}
  360. // {{{ listPackages()
  361. function listPackages()
  362. {
  363. if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
  364. return $e;
  365. }
  366. $ret = $this->_listPackages();
  367. $this->_unlock();
  368. return $ret;
  369. }
  370. // }}}
  371. // {{{ addPackage()
  372. function addPackage($package, $info)
  373. {
  374. if ($this->packageExists($package)) {
  375. return false;
  376. }
  377. if (PEAR::isError($e = $this->_lock(LOCK_EX))) {
  378. return $e;
  379. }
  380. $fp = $this->_openPackageFile($package, 'wb');
  381. if ($fp === null) {
  382. $this->_unlock();
  383. return false;
  384. }
  385. $info['_lastmodified'] = time();
  386. fwrite($fp, serialize($info));
  387. $this->_closePackageFile($fp);
  388. $this->_unlock();
  389. return true;
  390. }
  391. // }}}
  392. // {{{ deletePackage()
  393. function deletePackage($package)
  394. {
  395. if (PEAR::isError($e = $this->_lock(LOCK_EX))) {
  396. return $e;
  397. }
  398. $file = $this->_packageFileName($package);
  399. $ret = @unlink($file);
  400. $this->rebuildFileMap();
  401. $this->_unlock();
  402. return $ret;
  403. }
  404. // }}}
  405. // {{{ updatePackage()
  406. function updatePackage($package, $info, $merge = true)
  407. {
  408. $oldinfo = $this->packageInfo($package);
  409. if (empty($oldinfo)) {
  410. return false;
  411. }
  412. if (PEAR::isError($e = $this->_lock(LOCK_EX))) {
  413. return $e;
  414. }
  415. $fp = $this->_openPackageFile($package, 'w');
  416. if ($fp === null) {
  417. $this->_unlock();
  418. return false;
  419. }
  420. $info['_lastmodified'] = time();
  421. if ($merge) {
  422. fwrite($fp, serialize(array_merge($oldinfo, $info)));
  423. } else {
  424. fwrite($fp, serialize($info));
  425. }
  426. $this->_closePackageFile($fp);
  427. if (isset($info['filelist'])) {
  428. $this->rebuildFileMap();
  429. }
  430. $this->_unlock();
  431. return true;
  432. }
  433. // }}}
  434. // {{{ checkFileMap()
  435. /**
  436. * Test whether a file belongs to a package.
  437. *
  438. * @param string $path file path, absolute or relative to the pear
  439. * install dir
  440. *
  441. * @return string which package the file belongs to, or an empty
  442. * string if the file does not belong to an installed package
  443. *
  444. * @access public
  445. */
  446. function checkFileMap($path)
  447. {
  448. if (is_array($path)) {
  449. static $notempty;
  450. if (empty($notempty)) {
  451. $notempty = create_function('$a','return !empty($a);');
  452. }
  453. $pkgs = array();
  454. foreach ($path as $name => $attrs) {
  455. if (is_array($attrs) && isset($attrs['baseinstalldir'])) {
  456. $name = $attrs['baseinstalldir'].DIRECTORY_SEPARATOR.$name;
  457. }
  458. $pkgs[$name] = $this->checkFileMap($name);
  459. }
  460. return array_filter($pkgs, $notempty);
  461. }
  462. if (empty($this->filemap_cache) && PEAR::isError($this->readFileMap())) {
  463. return $err;
  464. }
  465. if (isset($this->filemap_cache[$path])) {
  466. return $this->filemap_cache[$path];
  467. }
  468. $l = strlen($this->install_dir);
  469. if (substr($path, 0, $l) == $this->install_dir) {
  470. $path = preg_replace('!^'.DIRECTORY_SEPARATOR.'+!', '', substr($path, $l));
  471. }
  472. if (isset($this->filemap_cache[$path])) {
  473. return $this->filemap_cache[$path];
  474. }
  475. return '';
  476. }
  477. // }}}
  478. }
  479. ?>