Dependency.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. // | Authors: Tomas V.V.Cox <[email protected]> |
  17. // | Stig Bakken <[email protected]> |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: Dependency.php,v 1.36.4.1 2004/12/27 07:04:19 cellog Exp $
  21. require_once "PEAR.php";
  22. define('PEAR_DEPENDENCY_MISSING', -1);
  23. define('PEAR_DEPENDENCY_CONFLICT', -2);
  24. define('PEAR_DEPENDENCY_UPGRADE_MINOR', -3);
  25. define('PEAR_DEPENDENCY_UPGRADE_MAJOR', -4);
  26. define('PEAR_DEPENDENCY_BAD_DEPENDENCY', -5);
  27. define('PEAR_DEPENDENCY_MISSING_OPTIONAL', -6);
  28. define('PEAR_DEPENDENCY_CONFLICT_OPTIONAL', -7);
  29. define('PEAR_DEPENDENCY_UPGRADE_MINOR_OPTIONAL', -8);
  30. define('PEAR_DEPENDENCY_UPGRADE_MAJOR_OPTIONAL', -9);
  31. /**
  32. * Dependency check for PEAR packages
  33. *
  34. * The class is based on the dependency RFC that can be found at
  35. * http://cvs.php.net/cvs.php/pearweb/rfc. It requires PHP >= 4.1
  36. *
  37. * @author Tomas V.V.Vox <[email protected]>
  38. * @author Stig Bakken <[email protected]>
  39. */
  40. class PEAR_Dependency
  41. {
  42. // {{{ constructor
  43. /**
  44. * Constructor
  45. *
  46. * @access public
  47. * @param object Registry object
  48. * @return void
  49. */
  50. function PEAR_Dependency(&$registry)
  51. {
  52. $this->registry = &$registry;
  53. }
  54. // }}}
  55. // {{{ callCheckMethod()
  56. /**
  57. * This method maps the XML dependency definition to the
  58. * corresponding one from PEAR_Dependency
  59. *
  60. * <pre>
  61. * $opts => Array
  62. * (
  63. * [type] => pkg
  64. * [rel] => ge
  65. * [version] => 3.4
  66. * [name] => HTML_Common
  67. * [optional] => false
  68. * )
  69. * </pre>
  70. *
  71. * @param string Error message
  72. * @param array Options
  73. * @return boolean
  74. */
  75. function callCheckMethod(&$errmsg, $opts)
  76. {
  77. $rel = isset($opts['rel']) ? $opts['rel'] : 'has';
  78. $req = isset($opts['version']) ? $opts['version'] : null;
  79. $name = isset($opts['name']) ? $opts['name'] : null;
  80. $opt = (isset($opts['optional']) && $opts['optional'] == 'yes') ?
  81. $opts['optional'] : null;
  82. $errmsg = '';
  83. switch ($opts['type']) {
  84. case 'pkg':
  85. return $this->checkPackage($errmsg, $name, $req, $rel, $opt);
  86. break;
  87. case 'ext':
  88. return $this->checkExtension($errmsg, $name, $req, $rel, $opt);
  89. break;
  90. case 'php':
  91. return $this->checkPHP($errmsg, $req, $rel);
  92. break;
  93. case 'prog':
  94. return $this->checkProgram($errmsg, $name);
  95. break;
  96. case 'os':
  97. return $this->checkOS($errmsg, $name);
  98. break;
  99. case 'sapi':
  100. return $this->checkSAPI($errmsg, $name);
  101. break;
  102. case 'zend':
  103. return $this->checkZend($errmsg, $name);
  104. break;
  105. default:
  106. return "'{$opts['type']}' dependency type not supported";
  107. }
  108. }
  109. // }}}
  110. // {{{ checkPackage()
  111. /**
  112. * Package dependencies check method
  113. *
  114. * @param string $errmsg Empty string, it will be populated with an error message, if any
  115. * @param string $name Name of the package to test
  116. * @param string $req The package version required
  117. * @param string $relation How to compare versions with each other
  118. * @param bool $opt Whether the relationship is optional
  119. *
  120. * @return mixed bool false if no error or the error string
  121. */
  122. function checkPackage(&$errmsg, $name, $req = null, $relation = 'has',
  123. $opt = false)
  124. {
  125. if (is_string($req) && substr($req, 0, 2) == 'v.') {
  126. $req = substr($req, 2);
  127. }
  128. switch ($relation) {
  129. case 'has':
  130. if (!$this->registry->packageExists($name)) {
  131. if ($opt) {
  132. $errmsg = "package `$name' is recommended to utilize some features.";
  133. return PEAR_DEPENDENCY_MISSING_OPTIONAL;
  134. }
  135. $errmsg = "requires package `$name'";
  136. return PEAR_DEPENDENCY_MISSING;
  137. }
  138. return false;
  139. case 'not':
  140. if ($this->registry->packageExists($name)) {
  141. $errmsg = "conflicts with package `$name'";
  142. return PEAR_DEPENDENCY_CONFLICT;
  143. }
  144. return false;
  145. case 'lt':
  146. case 'le':
  147. case 'eq':
  148. case 'ne':
  149. case 'ge':
  150. case 'gt':
  151. $version = $this->registry->packageInfo($name, 'version');
  152. if (!$this->registry->packageExists($name)
  153. || !version_compare("$version", "$req", $relation))
  154. {
  155. $code = $this->codeFromRelation($relation, $version, $req, $opt);
  156. if ($opt) {
  157. $errmsg = "package `$name' version " . $this->signOperator($relation) .
  158. " $req is recommended to utilize some features.";
  159. if ($version) {
  160. $errmsg .= " Installed version is $version";
  161. }
  162. return $code;
  163. }
  164. $errmsg = "requires package `$name' " .
  165. $this->signOperator($relation) . " $req";
  166. return $code;
  167. }
  168. return false;
  169. }
  170. $errmsg = "relation '$relation' with requirement '$req' is not supported (name=$name)";
  171. return PEAR_DEPENDENCY_BAD_DEPENDENCY;
  172. }
  173. // }}}
  174. // {{{ checkPackageUninstall()
  175. /**
  176. * Check package dependencies on uninstall
  177. *
  178. * @param string $error The resultant error string
  179. * @param string $warning The resultant warning string
  180. * @param string $name Name of the package to test
  181. *
  182. * @return bool true if there were errors
  183. */
  184. function checkPackageUninstall(&$error, &$warning, $package)
  185. {
  186. $error = null;
  187. $packages = $this->registry->listPackages();
  188. foreach ($packages as $pkg) {
  189. if ($pkg == $package) {
  190. continue;
  191. }
  192. $deps = $this->registry->packageInfo($pkg, 'release_deps');
  193. if (empty($deps)) {
  194. continue;
  195. }
  196. foreach ($deps as $dep) {
  197. if ($dep['type'] == 'pkg' && strcasecmp($dep['name'], $package) == 0) {
  198. if ($dep['rel'] == 'ne' || $dep['rel'] == 'not') {
  199. continue;
  200. }
  201. if (isset($dep['optional']) && $dep['optional'] == 'yes') {
  202. $warning .= "\nWarning: Package '$pkg' optionally depends on '$package'";
  203. } else {
  204. $error .= "Package '$pkg' depends on '$package'\n";
  205. }
  206. }
  207. }
  208. }
  209. return ($error) ? true : false;
  210. }
  211. // }}}
  212. // {{{ checkExtension()
  213. /**
  214. * Extension dependencies check method
  215. *
  216. * @param string $name Name of the extension to test
  217. * @param string $req_ext_ver Required extension version to compare with
  218. * @param string $relation How to compare versions with eachother
  219. * @param bool $opt Whether the relationship is optional
  220. *
  221. * @return mixed bool false if no error or the error string
  222. */
  223. function checkExtension(&$errmsg, $name, $req = null, $relation = 'has',
  224. $opt = false)
  225. {
  226. if ($relation == 'not') {
  227. if (extension_loaded($name)) {
  228. $errmsg = "conflicts with PHP extension '$name'";
  229. return PEAR_DEPENDENCY_CONFLICT;
  230. } else {
  231. return false;
  232. }
  233. }
  234. if (!extension_loaded($name)) {
  235. if ($relation == 'not') {
  236. return false;
  237. }
  238. if ($opt) {
  239. $errmsg = "'$name' PHP extension is recommended to utilize some features";
  240. return PEAR_DEPENDENCY_MISSING_OPTIONAL;
  241. }
  242. $errmsg = "'$name' PHP extension is not installed";
  243. return PEAR_DEPENDENCY_MISSING;
  244. }
  245. if ($relation == 'has') {
  246. return false;
  247. }
  248. $code = false;
  249. if (is_string($req) && substr($req, 0, 2) == 'v.') {
  250. $req = substr($req, 2);
  251. }
  252. $ext_ver = phpversion($name);
  253. $operator = $relation;
  254. // Force params to be strings, otherwise the comparation will fail (ex. 0.9==0.90)
  255. if (!version_compare("$ext_ver", "$req", $operator)) {
  256. $errmsg = "'$name' PHP extension version " .
  257. $this->signOperator($operator) . " $req is required";
  258. $code = $this->codeFromRelation($relation, $ext_ver, $req, $opt);
  259. if ($opt) {
  260. $errmsg = "'$name' PHP extension version " . $this->signOperator($operator) .
  261. " $req is recommended to utilize some features";
  262. return $code;
  263. }
  264. }
  265. return $code;
  266. }
  267. // }}}
  268. // {{{ checkOS()
  269. /**
  270. * Operating system dependencies check method
  271. *
  272. * @param string $os Name of the operating system
  273. *
  274. * @return mixed bool false if no error or the error string
  275. */
  276. function checkOS(&$errmsg, $os)
  277. {
  278. // XXX Fixme: Implement a more flexible way, like
  279. // comma separated values or something similar to PEAR_OS
  280. static $myos;
  281. if (empty($myos)) {
  282. include_once "OS/Guess.php";
  283. $myos = new OS_Guess();
  284. }
  285. // only 'has' relation is currently supported
  286. if ($myos->matchSignature($os)) {
  287. return false;
  288. }
  289. $errmsg = "'$os' operating system not supported";
  290. return PEAR_DEPENDENCY_CONFLICT;
  291. }
  292. // }}}
  293. // {{{ checkPHP()
  294. /**
  295. * PHP version check method
  296. *
  297. * @param string $req which version to compare
  298. * @param string $relation how to compare the version
  299. *
  300. * @return mixed bool false if no error or the error string
  301. */
  302. function checkPHP(&$errmsg, $req, $relation = 'ge')
  303. {
  304. // this would be a bit stupid, but oh well :)
  305. if ($relation == 'has') {
  306. return false;
  307. }
  308. if ($relation == 'not') {
  309. $errmsg = 'Invalid dependency - "not" is not allowed for php dependencies, ' .
  310. 'php cannot conflict with itself';
  311. return PEAR_DEPENDENCY_BAD_DEPENDENCY;
  312. }
  313. if (substr($req, 0, 2) == 'v.') {
  314. $req = substr($req,2, strlen($req) - 2);
  315. }
  316. $php_ver = phpversion();
  317. $operator = $relation;
  318. if (!version_compare("$php_ver", "$req", $operator)) {
  319. $errmsg = "PHP version " . $this->signOperator($operator) .
  320. " $req is required";
  321. return PEAR_DEPENDENCY_CONFLICT;
  322. }
  323. return false;
  324. }
  325. // }}}
  326. // {{{ checkProgram()
  327. /**
  328. * External program check method. Looks for executable files in
  329. * directories listed in the PATH environment variable.
  330. *
  331. * @param string $program which program to look for
  332. *
  333. * @return mixed bool false if no error or the error string
  334. */
  335. function checkProgram(&$errmsg, $program)
  336. {
  337. // XXX FIXME honor safe mode
  338. $exe_suffix = OS_WINDOWS ? '.exe' : '';
  339. $path_elements = explode(PATH_SEPARATOR, getenv('PATH'));
  340. foreach ($path_elements as $dir) {
  341. $file = $dir . DIRECTORY_SEPARATOR . $program . $exe_suffix;
  342. if (@file_exists($file) && @is_executable($file)) {
  343. return false;
  344. }
  345. }
  346. $errmsg = "'$program' program is not present in the PATH";
  347. return PEAR_DEPENDENCY_MISSING;
  348. }
  349. // }}}
  350. // {{{ checkSAPI()
  351. /**
  352. * SAPI backend check method. Version comparison is not yet
  353. * available here.
  354. *
  355. * @param string $name name of SAPI backend
  356. * @param string $req which version to compare
  357. * @param string $relation how to compare versions (currently
  358. * hardcoded to 'has')
  359. * @return mixed bool false if no error or the error string
  360. */
  361. function checkSAPI(&$errmsg, $name, $req = null, $relation = 'has')
  362. {
  363. // XXX Fixme: There is no way to know if the user has or
  364. // not other SAPI backends installed than the installer one
  365. $sapi_backend = php_sapi_name();
  366. // Version comparisons not supported, sapi backends don't have
  367. // version information yet.
  368. if ($sapi_backend == $name) {
  369. return false;
  370. }
  371. $errmsg = "'$sapi_backend' SAPI backend not supported";
  372. return PEAR_DEPENDENCY_CONFLICT;
  373. }
  374. // }}}
  375. // {{{ checkZend()
  376. /**
  377. * Zend version check method
  378. *
  379. * @param string $req which version to compare
  380. * @param string $relation how to compare the version
  381. *
  382. * @return mixed bool false if no error or the error string
  383. */
  384. function checkZend(&$errmsg, $req, $relation = 'ge')
  385. {
  386. if (substr($req, 0, 2) == 'v.') {
  387. $req = substr($req,2, strlen($req) - 2);
  388. }
  389. $zend_ver = zend_version();
  390. $operator = substr($relation,0,2);
  391. if (!version_compare("$zend_ver", "$req", $operator)) {
  392. $errmsg = "Zend version " . $this->signOperator($operator) .
  393. " $req is required";
  394. return PEAR_DEPENDENCY_CONFLICT;
  395. }
  396. return false;
  397. }
  398. // }}}
  399. // {{{ signOperator()
  400. /**
  401. * Converts text comparing operators to them sign equivalents
  402. *
  403. * Example: 'ge' to '>='
  404. *
  405. * @access public
  406. * @param string Operator
  407. * @return string Sign equivalent
  408. */
  409. function signOperator($operator)
  410. {
  411. switch($operator) {
  412. case 'lt': return '<';
  413. case 'le': return '<=';
  414. case 'gt': return '>';
  415. case 'ge': return '>=';
  416. case 'eq': return '==';
  417. case 'ne': return '!=';
  418. default:
  419. return $operator;
  420. }
  421. }
  422. // }}}
  423. // {{{ codeFromRelation()
  424. /**
  425. * Convert relation into corresponding code
  426. *
  427. * @access public
  428. * @param string Relation
  429. * @param string Version
  430. * @param string Requirement
  431. * @param bool Optional dependency indicator
  432. * @return integer
  433. */
  434. function codeFromRelation($relation, $version, $req, $opt = false)
  435. {
  436. $code = PEAR_DEPENDENCY_BAD_DEPENDENCY;
  437. switch ($relation) {
  438. case 'gt': case 'ge': case 'eq':
  439. // upgrade
  440. $have_major = preg_replace('/\D.*/', '', $version);
  441. $need_major = preg_replace('/\D.*/', '', $req);
  442. if ($need_major > $have_major) {
  443. $code = $opt ? PEAR_DEPENDENCY_UPGRADE_MAJOR_OPTIONAL :
  444. PEAR_DEPENDENCY_UPGRADE_MAJOR;
  445. } else {
  446. $code = $opt ? PEAR_DEPENDENCY_UPGRADE_MINOR_OPTIONAL :
  447. PEAR_DEPENDENCY_UPGRADE_MINOR;
  448. }
  449. break;
  450. case 'lt': case 'le': case 'ne':
  451. $code = $opt ? PEAR_DEPENDENCY_CONFLICT_OPTIONAL :
  452. PEAR_DEPENDENCY_CONFLICT;
  453. break;
  454. }
  455. return $code;
  456. }
  457. // }}}
  458. }
  459. ?>