Dependency.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. // | Authors: Tomas V.V.Cox <cox@idecnet.com> |
  17. // | Stig Bakken <ssb@php.net> |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: Dependency.php,v 1.36 2004/01/08 17:33:12 sniper 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 <cox@idecnet.com>
  38. * @author Stig Bakken <ssb@php.net>
  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') {
  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 == 'ne') {
  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 allowed when specifying PHP, you must run PHP in PHP";
  310. return PEAR_DEPENDENCY_BAD_DEPENDENCY;
  311. }
  312. if (substr($req, 0, 2) == 'v.') {
  313. $req = substr($req,2, strlen($req) - 2);
  314. }
  315. $php_ver = phpversion();
  316. $operator = $relation;
  317. if (!version_compare("$php_ver", "$req", $operator)) {
  318. $errmsg = "PHP version " . $this->signOperator($operator) .
  319. " $req is required";
  320. return PEAR_DEPENDENCY_CONFLICT;
  321. }
  322. return false;
  323. }
  324. // }}}
  325. // {{{ checkProgram()
  326. /**
  327. * External program check method. Looks for executable files in
  328. * directories listed in the PATH environment variable.
  329. *
  330. * @param string $program which program to look for
  331. *
  332. * @return mixed bool false if no error or the error string
  333. */
  334. function checkProgram(&$errmsg, $program)
  335. {
  336. // XXX FIXME honor safe mode
  337. $exe_suffix = OS_WINDOWS ? '.exe' : '';
  338. $path_elements = explode(PATH_SEPARATOR, getenv('PATH'));
  339. foreach ($path_elements as $dir) {
  340. $file = $dir . DIRECTORY_SEPARATOR . $program . $exe_suffix;
  341. if (@file_exists($file) && @is_executable($file)) {
  342. return false;
  343. }
  344. }
  345. $errmsg = "'$program' program is not present in the PATH";
  346. return PEAR_DEPENDENCY_MISSING;
  347. }
  348. // }}}
  349. // {{{ checkSAPI()
  350. /**
  351. * SAPI backend check method. Version comparison is not yet
  352. * available here.
  353. *
  354. * @param string $name name of SAPI backend
  355. * @param string $req which version to compare
  356. * @param string $relation how to compare versions (currently
  357. * hardcoded to 'has')
  358. * @return mixed bool false if no error or the error string
  359. */
  360. function checkSAPI(&$errmsg, $name, $req = null, $relation = 'has')
  361. {
  362. // XXX Fixme: There is no way to know if the user has or
  363. // not other SAPI backends installed than the installer one
  364. $sapi_backend = php_sapi_name();
  365. // Version comparisons not supported, sapi backends don't have
  366. // version information yet.
  367. if ($sapi_backend == $name) {
  368. return false;
  369. }
  370. $errmsg = "'$sapi_backend' SAPI backend not supported";
  371. return PEAR_DEPENDENCY_CONFLICT;
  372. }
  373. // }}}
  374. // {{{ checkZend()
  375. /**
  376. * Zend version check method
  377. *
  378. * @param string $req which version to compare
  379. * @param string $relation how to compare the version
  380. *
  381. * @return mixed bool false if no error or the error string
  382. */
  383. function checkZend(&$errmsg, $req, $relation = 'ge')
  384. {
  385. if (substr($req, 0, 2) == 'v.') {
  386. $req = substr($req,2, strlen($req) - 2);
  387. }
  388. $zend_ver = zend_version();
  389. $operator = substr($relation,0,2);
  390. if (!version_compare("$zend_ver", "$req", $operator)) {
  391. $errmsg = "Zend version " . $this->signOperator($operator) .
  392. " $req is required";
  393. return PEAR_DEPENDENCY_CONFLICT;
  394. }
  395. return false;
  396. }
  397. // }}}
  398. // {{{ signOperator()
  399. /**
  400. * Converts text comparing operators to them sign equivalents
  401. *
  402. * Example: 'ge' to '>='
  403. *
  404. * @access public
  405. * @param string Operator
  406. * @return string Sign equivalent
  407. */
  408. function signOperator($operator)
  409. {
  410. switch($operator) {
  411. case 'lt': return '<';
  412. case 'le': return '<=';
  413. case 'gt': return '>';
  414. case 'ge': return '>=';
  415. case 'eq': return '==';
  416. case 'ne': return '!=';
  417. default:
  418. return $operator;
  419. }
  420. }
  421. // }}}
  422. // {{{ codeFromRelation()
  423. /**
  424. * Convert relation into corresponding code
  425. *
  426. * @access public
  427. * @param string Relation
  428. * @param string Version
  429. * @param string Requirement
  430. * @param bool Optional dependency indicator
  431. * @return integer
  432. */
  433. function codeFromRelation($relation, $version, $req, $opt = false)
  434. {
  435. $code = PEAR_DEPENDENCY_BAD_DEPENDENCY;
  436. switch ($relation) {
  437. case 'gt': case 'ge': case 'eq':
  438. // upgrade
  439. $have_major = preg_replace('/\D.*/', '', $version);
  440. $need_major = preg_replace('/\D.*/', '', $req);
  441. if ($need_major > $have_major) {
  442. $code = $opt ? PEAR_DEPENDENCY_UPGRADE_MAJOR_OPTIONAL :
  443. PEAR_DEPENDENCY_UPGRADE_MAJOR;
  444. } else {
  445. $code = $opt ? PEAR_DEPENDENCY_UPGRADE_MINOR_OPTIONAL :
  446. PEAR_DEPENDENCY_UPGRADE_MINOR;
  447. }
  448. break;
  449. case 'lt': case 'le': case 'ne':
  450. $code = $opt ? PEAR_DEPENDENCY_CONFLICT_OPTIONAL :
  451. PEAR_DEPENDENCY_CONFLICT;
  452. break;
  453. }
  454. return $code;
  455. }
  456. // }}}
  457. }
  458. ?>