Registry.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Stig Bakken <ssb@php.net> |
  17. // | Tomas V.V.Cox <cox@idecnet.com> |
  18. // | |
  19. // +----------------------------------------------------------------------+
  20. //
  21. // $Id: Registry.php,v 1.50 2004/01/08 17:33:12 sniper 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. $this->lock_fp = @fopen($this->lockfile, $open_mode);
  258. if (!is_resource($this->lock_fp)) {
  259. return $this->raiseError("could not create lock file" .
  260. (isset($php_errormsg) ? ": " . $php_errormsg : ""));
  261. }
  262. if (!(int)flock($this->lock_fp, $mode)) {
  263. switch ($mode) {
  264. case LOCK_SH: $str = 'shared'; break;
  265. case LOCK_EX: $str = 'exclusive'; break;
  266. case LOCK_UN: $str = 'unlock'; break;
  267. default: $str = 'unknown'; break;
  268. }
  269. return $this->raiseError("could not acquire $str lock ($this->lockfile)",
  270. PEAR_REGISTRY_ERROR_LOCK);
  271. }
  272. }
  273. return true;
  274. }
  275. // }}}
  276. // {{{ _unlock()
  277. function _unlock()
  278. {
  279. $ret = $this->_lock(LOCK_UN);
  280. $this->lock_fp = null;
  281. return $ret;
  282. }
  283. // }}}
  284. // {{{ _packageExists()
  285. function _packageExists($package)
  286. {
  287. return file_exists($this->_packageFileName($package));
  288. }
  289. // }}}
  290. // {{{ _packageInfo()
  291. function _packageInfo($package = null, $key = null)
  292. {
  293. if ($package === null) {
  294. return array_map(array($this, '_packageInfo'),
  295. $this->_listPackages());
  296. }
  297. $fp = $this->_openPackageFile($package, 'r');
  298. if ($fp === null) {
  299. return null;
  300. }
  301. $rt = get_magic_quotes_runtime();
  302. set_magic_quotes_runtime(0);
  303. $data = fread($fp, filesize($this->_packageFileName($package)));
  304. set_magic_quotes_runtime($rt);
  305. $this->_closePackageFile($fp);
  306. $data = unserialize($data);
  307. if ($key === null) {
  308. return $data;
  309. }
  310. if (isset($data[$key])) {
  311. return $data[$key];
  312. }
  313. return null;
  314. }
  315. // }}}
  316. // {{{ _listPackages()
  317. function _listPackages()
  318. {
  319. $pkglist = array();
  320. $dp = @opendir($this->statedir);
  321. if (!$dp) {
  322. return $pkglist;
  323. }
  324. while ($ent = readdir($dp)) {
  325. if ($ent{0} == '.' || substr($ent, -4) != '.reg') {
  326. continue;
  327. }
  328. $pkglist[] = substr($ent, 0, -4);
  329. }
  330. return $pkglist;
  331. }
  332. // }}}
  333. // {{{ packageExists()
  334. function packageExists($package)
  335. {
  336. if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
  337. return $e;
  338. }
  339. $ret = $this->_packageExists($package);
  340. $this->_unlock();
  341. return $ret;
  342. }
  343. // }}}
  344. // {{{ packageInfo()
  345. function packageInfo($package = null, $key = null)
  346. {
  347. if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
  348. return $e;
  349. }
  350. $ret = $this->_packageInfo($package, $key);
  351. $this->_unlock();
  352. return $ret;
  353. }
  354. // }}}
  355. // {{{ listPackages()
  356. function listPackages()
  357. {
  358. if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
  359. return $e;
  360. }
  361. $ret = $this->_listPackages();
  362. $this->_unlock();
  363. return $ret;
  364. }
  365. // }}}
  366. // {{{ addPackage()
  367. function addPackage($package, $info)
  368. {
  369. if ($this->packageExists($package)) {
  370. return false;
  371. }
  372. if (PEAR::isError($e = $this->_lock(LOCK_EX))) {
  373. return $e;
  374. }
  375. $fp = $this->_openPackageFile($package, 'wb');
  376. if ($fp === null) {
  377. $this->_unlock();
  378. return false;
  379. }
  380. $info['_lastmodified'] = time();
  381. fwrite($fp, serialize($info));
  382. $this->_closePackageFile($fp);
  383. $this->_unlock();
  384. return true;
  385. }
  386. // }}}
  387. // {{{ deletePackage()
  388. function deletePackage($package)
  389. {
  390. if (PEAR::isError($e = $this->_lock(LOCK_EX))) {
  391. return $e;
  392. }
  393. $file = $this->_packageFileName($package);
  394. $ret = @unlink($file);
  395. $this->rebuildFileMap();
  396. $this->_unlock();
  397. return $ret;
  398. }
  399. // }}}
  400. // {{{ updatePackage()
  401. function updatePackage($package, $info, $merge = true)
  402. {
  403. $oldinfo = $this->packageInfo($package);
  404. if (empty($oldinfo)) {
  405. return false;
  406. }
  407. if (PEAR::isError($e = $this->_lock(LOCK_EX))) {
  408. return $e;
  409. }
  410. $fp = $this->_openPackageFile($package, 'w');
  411. if ($fp === null) {
  412. $this->_unlock();
  413. return false;
  414. }
  415. $info['_lastmodified'] = time();
  416. if ($merge) {
  417. fwrite($fp, serialize(array_merge($oldinfo, $info)));
  418. } else {
  419. fwrite($fp, serialize($info));
  420. }
  421. $this->_closePackageFile($fp);
  422. if (isset($info['filelist'])) {
  423. $this->rebuildFileMap();
  424. }
  425. $this->_unlock();
  426. return true;
  427. }
  428. // }}}
  429. // {{{ checkFileMap()
  430. /**
  431. * Test whether a file belongs to a package.
  432. *
  433. * @param string $path file path, absolute or relative to the pear
  434. * install dir
  435. *
  436. * @return string which package the file belongs to, or an empty
  437. * string if the file does not belong to an installed package
  438. *
  439. * @access public
  440. */
  441. function checkFileMap($path)
  442. {
  443. if (is_array($path)) {
  444. static $notempty;
  445. if (empty($notempty)) {
  446. $notempty = create_function('$a','return !empty($a);');
  447. }
  448. $pkgs = array();
  449. foreach ($path as $name => $attrs) {
  450. if (is_array($attrs) && isset($attrs['baseinstalldir'])) {
  451. $name = $attrs['baseinstalldir'].DIRECTORY_SEPARATOR.$name;
  452. }
  453. $pkgs[$name] = $this->checkFileMap($name);
  454. }
  455. return array_filter($pkgs, $notempty);
  456. }
  457. if (empty($this->filemap_cache) && PEAR::isError($this->readFileMap())) {
  458. return $err;
  459. }
  460. if (isset($this->filemap_cache[$path])) {
  461. return $this->filemap_cache[$path];
  462. }
  463. $l = strlen($this->install_dir);
  464. if (substr($path, 0, $l) == $this->install_dir) {
  465. $path = preg_replace('!^'.DIRECTORY_SEPARATOR.'+!', '', substr($path, $l));
  466. }
  467. if (isset($this->filemap_cache[$path])) {
  468. return $this->filemap_cache[$path];
  469. }
  470. return '';
  471. }
  472. // }}}
  473. }
  474. ?>