PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/php/PEAR/Task/Postinstallscript.php

https://bitbucket.org/adarshj/convenient_website
PHP | 323 lines | 242 code | 5 blank | 76 comment | 43 complexity | 9eed944f3f795c52caba0cb87665cd7f MD5 | raw file
Possible License(s): Apache-2.0, MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-2-Clause, GPL-2.0, LGPL-3.0
  1. <?php
  2. /**
  3. * <tasks:postinstallscript>
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * @category pear
  8. * @package PEAR
  9. * @author Greg Beaver <cellog@php.net>
  10. * @copyright 1997-2009 The Authors
  11. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  12. * @version CVS: $Id: Postinstallscript.php 313023 2011-07-06 19:17:11Z dufuz $
  13. * @link http://pear.php.net/package/PEAR
  14. * @since File available since Release 1.4.0a1
  15. */
  16. /**
  17. * Base class
  18. */
  19. require_once 'PEAR/Task/Common.php';
  20. /**
  21. * Implements the postinstallscript file task.
  22. *
  23. * Note that post-install scripts are handled separately from installation, by the
  24. * "pear run-scripts" command
  25. * @category pear
  26. * @package PEAR
  27. * @author Greg Beaver <cellog@php.net>
  28. * @copyright 1997-2009 The Authors
  29. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  30. * @version Release: 1.9.4
  31. * @link http://pear.php.net/package/PEAR
  32. * @since Class available since Release 1.4.0a1
  33. */
  34. class PEAR_Task_Postinstallscript extends PEAR_Task_Common
  35. {
  36. var $type = 'script';
  37. var $_class;
  38. var $_params;
  39. var $_obj;
  40. /**
  41. *
  42. * @var PEAR_PackageFile_v2
  43. */
  44. var $_pkg;
  45. var $_contents;
  46. var $phase = PEAR_TASK_INSTALL;
  47. /**
  48. * Validate the raw xml at parsing-time.
  49. *
  50. * This also attempts to validate the script to make sure it meets the criteria
  51. * for a post-install script
  52. * @param PEAR_PackageFile_v2
  53. * @param array The XML contents of the <postinstallscript> tag
  54. * @param PEAR_Config
  55. * @param array the entire parsed <file> tag
  56. * @static
  57. */
  58. function validateXml($pkg, $xml, $config, $fileXml)
  59. {
  60. if ($fileXml['role'] != 'php') {
  61. return array(PEAR_TASK_ERROR_INVALID, 'Post-install script "' .
  62. $fileXml['name'] . '" must be role="php"');
  63. }
  64. PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
  65. $file = $pkg->getFileContents($fileXml['name']);
  66. if (PEAR::isError($file)) {
  67. PEAR::popErrorHandling();
  68. return array(PEAR_TASK_ERROR_INVALID, 'Post-install script "' .
  69. $fileXml['name'] . '" is not valid: ' .
  70. $file->getMessage());
  71. } elseif ($file === null) {
  72. return array(PEAR_TASK_ERROR_INVALID, 'Post-install script "' .
  73. $fileXml['name'] . '" could not be retrieved for processing!');
  74. } else {
  75. $analysis = $pkg->analyzeSourceCode($file, true);
  76. if (!$analysis) {
  77. PEAR::popErrorHandling();
  78. $warnings = '';
  79. foreach ($pkg->getValidationWarnings() as $warn) {
  80. $warnings .= $warn['message'] . "\n";
  81. }
  82. return array(PEAR_TASK_ERROR_INVALID, 'Analysis of post-install script "' .
  83. $fileXml['name'] . '" failed: ' . $warnings);
  84. }
  85. if (count($analysis['declared_classes']) != 1) {
  86. PEAR::popErrorHandling();
  87. return array(PEAR_TASK_ERROR_INVALID, 'Post-install script "' .
  88. $fileXml['name'] . '" must declare exactly 1 class');
  89. }
  90. $class = $analysis['declared_classes'][0];
  91. if ($class != str_replace(array('/', '.php'), array('_', ''),
  92. $fileXml['name']) . '_postinstall') {
  93. PEAR::popErrorHandling();
  94. return array(PEAR_TASK_ERROR_INVALID, 'Post-install script "' .
  95. $fileXml['name'] . '" class "' . $class . '" must be named "' .
  96. str_replace(array('/', '.php'), array('_', ''),
  97. $fileXml['name']) . '_postinstall"');
  98. }
  99. if (!isset($analysis['declared_methods'][$class])) {
  100. PEAR::popErrorHandling();
  101. return array(PEAR_TASK_ERROR_INVALID, 'Post-install script "' .
  102. $fileXml['name'] . '" must declare methods init() and run()');
  103. }
  104. $methods = array('init' => 0, 'run' => 1);
  105. foreach ($analysis['declared_methods'][$class] as $method) {
  106. if (isset($methods[$method])) {
  107. unset($methods[$method]);
  108. }
  109. }
  110. if (count($methods)) {
  111. PEAR::popErrorHandling();
  112. return array(PEAR_TASK_ERROR_INVALID, 'Post-install script "' .
  113. $fileXml['name'] . '" must declare methods init() and run()');
  114. }
  115. }
  116. PEAR::popErrorHandling();
  117. $definedparams = array();
  118. $tasksNamespace = $pkg->getTasksNs() . ':';
  119. if (!isset($xml[$tasksNamespace . 'paramgroup']) && isset($xml['paramgroup'])) {
  120. // in order to support the older betas, which did not expect internal tags
  121. // to also use the namespace
  122. $tasksNamespace = '';
  123. }
  124. if (isset($xml[$tasksNamespace . 'paramgroup'])) {
  125. $params = $xml[$tasksNamespace . 'paramgroup'];
  126. if (!is_array($params) || !isset($params[0])) {
  127. $params = array($params);
  128. }
  129. foreach ($params as $param) {
  130. if (!isset($param[$tasksNamespace . 'id'])) {
  131. return array(PEAR_TASK_ERROR_INVALID, 'Post-install script "' .
  132. $fileXml['name'] . '" <paramgroup> must have ' .
  133. 'an ' . $tasksNamespace . 'id> tag');
  134. }
  135. if (isset($param[$tasksNamespace . 'name'])) {
  136. if (!in_array($param[$tasksNamespace . 'name'], $definedparams)) {
  137. return array(PEAR_TASK_ERROR_INVALID, 'Post-install script "' .
  138. $fileXml['name'] . '" ' . $tasksNamespace .
  139. 'paramgroup> id "' . $param[$tasksNamespace . 'id'] .
  140. '" parameter "' . $param[$tasksNamespace . 'name'] .
  141. '" has not been previously defined');
  142. }
  143. if (!isset($param[$tasksNamespace . 'conditiontype'])) {
  144. return array(PEAR_TASK_ERROR_INVALID, 'Post-install script "' .
  145. $fileXml['name'] . '" ' . $tasksNamespace .
  146. 'paramgroup> id "' . $param[$tasksNamespace . 'id'] .
  147. '" must have a ' . $tasksNamespace .
  148. 'conditiontype> tag containing either "=", ' .
  149. '"!=", or "preg_match"');
  150. }
  151. if (!in_array($param[$tasksNamespace . 'conditiontype'],
  152. array('=', '!=', 'preg_match'))) {
  153. return array(PEAR_TASK_ERROR_INVALID, 'Post-install script "' .
  154. $fileXml['name'] . '" ' . $tasksNamespace .
  155. 'paramgroup> id "' . $param[$tasksNamespace . 'id'] .
  156. '" must have a ' . $tasksNamespace .
  157. 'conditiontype> tag containing either "=", ' .
  158. '"!=", or "preg_match"');
  159. }
  160. if (!isset($param[$tasksNamespace . 'value'])) {
  161. return array(PEAR_TASK_ERROR_INVALID, 'Post-install script "' .
  162. $fileXml['name'] . '" ' . $tasksNamespace .
  163. 'paramgroup> id "' . $param[$tasksNamespace . 'id'] .
  164. '" must have a ' . $tasksNamespace .
  165. 'value> tag containing expected parameter value');
  166. }
  167. }
  168. if (isset($param[$tasksNamespace . 'instructions'])) {
  169. if (!is_string($param[$tasksNamespace . 'instructions'])) {
  170. return array(PEAR_TASK_ERROR_INVALID, 'Post-install script "' .
  171. $fileXml['name'] . '" ' . $tasksNamespace .
  172. 'paramgroup> id "' . $param[$tasksNamespace . 'id'] .
  173. '" ' . $tasksNamespace . 'instructions> must be simple text');
  174. }
  175. }
  176. if (!isset($param[$tasksNamespace . 'param'])) {
  177. continue; // <param> is no longer required
  178. }
  179. $subparams = $param[$tasksNamespace . 'param'];
  180. if (!is_array($subparams) || !isset($subparams[0])) {
  181. $subparams = array($subparams);
  182. }
  183. foreach ($subparams as $subparam) {
  184. if (!isset($subparam[$tasksNamespace . 'name'])) {
  185. return array(PEAR_TASK_ERROR_INVALID, 'Post-install script "' .
  186. $fileXml['name'] . '" parameter for ' .
  187. $tasksNamespace . 'paramgroup> id "' .
  188. $param[$tasksNamespace . 'id'] . '" must have ' .
  189. 'a ' . $tasksNamespace . 'name> tag');
  190. }
  191. if (!preg_match('/[a-zA-Z0-9]+/',
  192. $subparam[$tasksNamespace . 'name'])) {
  193. return array(PEAR_TASK_ERROR_INVALID, 'Post-install script "' .
  194. $fileXml['name'] . '" parameter "' .
  195. $subparam[$tasksNamespace . 'name'] .
  196. '" for ' . $tasksNamespace . 'paramgroup> id "' .
  197. $param[$tasksNamespace . 'id'] .
  198. '" is not a valid name. Must contain only alphanumeric characters');
  199. }
  200. if (!isset($subparam[$tasksNamespace . 'prompt'])) {
  201. return array(PEAR_TASK_ERROR_INVALID, 'Post-install script "' .
  202. $fileXml['name'] . '" parameter "' .
  203. $subparam[$tasksNamespace . 'name'] .
  204. '" for ' . $tasksNamespace . 'paramgroup> id "' .
  205. $param[$tasksNamespace . 'id'] .
  206. '" must have a ' . $tasksNamespace . 'prompt> tag');
  207. }
  208. if (!isset($subparam[$tasksNamespace . 'type'])) {
  209. return array(PEAR_TASK_ERROR_INVALID, 'Post-install script "' .
  210. $fileXml['name'] . '" parameter "' .
  211. $subparam[$tasksNamespace . 'name'] .
  212. '" for ' . $tasksNamespace . 'paramgroup> id "' .
  213. $param[$tasksNamespace . 'id'] .
  214. '" must have a ' . $tasksNamespace . 'type> tag');
  215. }
  216. $definedparams[] = $param[$tasksNamespace . 'id'] . '::' .
  217. $subparam[$tasksNamespace . 'name'];
  218. }
  219. }
  220. }
  221. return true;
  222. }
  223. /**
  224. * Initialize a task instance with the parameters
  225. * @param array raw, parsed xml
  226. * @param array attributes from the <file> tag containing this task
  227. * @param string|null last installed version of this package, if any (useful for upgrades)
  228. */
  229. function init($xml, $fileattribs, $lastversion)
  230. {
  231. $this->_class = str_replace('/', '_', $fileattribs['name']);
  232. $this->_filename = $fileattribs['name'];
  233. $this->_class = str_replace ('.php', '', $this->_class) . '_postinstall';
  234. $this->_params = $xml;
  235. $this->_lastversion = $lastversion;
  236. }
  237. /**
  238. * Strip the tasks: namespace from internal params
  239. *
  240. * @access private
  241. */
  242. function _stripNamespace($params = null)
  243. {
  244. if ($params === null) {
  245. $params = array();
  246. if (!is_array($this->_params)) {
  247. return;
  248. }
  249. foreach ($this->_params as $i => $param) {
  250. if (is_array($param)) {
  251. $param = $this->_stripNamespace($param);
  252. }
  253. $params[str_replace($this->_pkg->getTasksNs() . ':', '', $i)] = $param;
  254. }
  255. $this->_params = $params;
  256. } else {
  257. $newparams = array();
  258. foreach ($params as $i => $param) {
  259. if (is_array($param)) {
  260. $param = $this->_stripNamespace($param);
  261. }
  262. $newparams[str_replace($this->_pkg->getTasksNs() . ':', '', $i)] = $param;
  263. }
  264. return $newparams;
  265. }
  266. }
  267. /**
  268. * Unlike other tasks, the installed file name is passed in instead of the file contents,
  269. * because this task is handled post-installation
  270. * @param PEAR_PackageFile_v1|PEAR_PackageFile_v2
  271. * @param string file name
  272. * @return bool|PEAR_Error false to skip this file, PEAR_Error to fail
  273. * (use $this->throwError)
  274. */
  275. function startSession($pkg, $contents)
  276. {
  277. if ($this->installphase != PEAR_TASK_INSTALL) {
  278. return false;
  279. }
  280. // remove the tasks: namespace if present
  281. $this->_pkg = $pkg;
  282. $this->_stripNamespace();
  283. $this->logger->log(0, 'Including external post-installation script "' .
  284. $contents . '" - any errors are in this script');
  285. include_once $contents;
  286. if (class_exists($this->_class)) {
  287. $this->logger->log(0, 'Inclusion succeeded');
  288. } else {
  289. return $this->throwError('init of post-install script class "' . $this->_class
  290. . '" failed');
  291. }
  292. $this->_obj = new $this->_class;
  293. $this->logger->log(1, 'running post-install script "' . $this->_class . '->init()"');
  294. PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
  295. $res = $this->_obj->init($this->config, $pkg, $this->_lastversion);
  296. PEAR::popErrorHandling();
  297. if ($res) {
  298. $this->logger->log(0, 'init succeeded');
  299. } else {
  300. return $this->throwError('init of post-install script "' . $this->_class .
  301. '->init()" failed');
  302. }
  303. $this->_contents = $contents;
  304. return true;
  305. }
  306. /**
  307. * No longer used
  308. * @see PEAR_PackageFile_v2::runPostinstallScripts()
  309. * @param array an array of tasks
  310. * @param string install or upgrade
  311. * @access protected
  312. * @static
  313. */
  314. function run()
  315. {
  316. }
  317. }
  318. ?>