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