Example #1
0
 function validate(\PEAR2\Pyrus\PackageInterface $package, array $file)
 {
     $parser = new \PEAR2\Pyrus\XMLParser();
     $schemapath = \PEAR2\Pyrus\Main::getDataPath();
     if (!file_exists(\PEAR2\Pyrus\Main::getDataPath() . '/customcommand-2.0.xsd')) {
         $schemapath = realpath(__DIR__ . '/../../../../../data');
     }
     $taskschema = $schemapath . '/customcommand-2.0.xsd';
     try {
         $taskinfo = $parser->parse($package->getFilePath($file['attribs']['name']), $taskschema);
     } catch (\Exception $e) {
         throw new \PEAR2\Pyrus\Installer\Role\Exception('Invalid custom command definition file,' . ' file does not conform to the schema', $e);
     }
 }
Example #2
0
 function getFrom()
 {
     if ($this->from) {
         return $this->from->getFrom();
     }
     return $this;
 }
    /**
     * Validate the basic contents of a <postinstallscript> tag
     * @param PEAR_Pyrus_PackageFileInterface
     * @param array
     * @param array the entire parsed <file> tag
     * @param string the filename of the package.xml
     * @throws \PEAR2\Pyrus\Task\Exception\InvalidTask
     */
    static function validateXml(\PEAR2\Pyrus\PackageInterface $pkg, $xml, $fileXml, $file)
    {
        if ($fileXml['role'] != 'php') {
            throw new Exception\InvalidTask('postinstallscript', $file,
                                                             'Post-install script "' .
                                                             $fileXml['name'] . '" must be role="php"');
        }
        try {
            $filecontents = $pkg->getFileContents($fileXml['name']);
        } catch (\Exception $e) {
            throw new Exception\InvalidTask('postinstallscript', $file,
                                                             'Post-install script "' .
                                                             $fileXml['name'] . '" is not valid: ' .
                                                             $e->getMessage(), $e);
        }

        $validator = $pkg->getValidator();
        $analysis = $validator->analyzeSourceCode($filecontents, true);
        if (!$analysis) {
            $warnings = array();
            // iterate over the problems
            foreach ($validator->getErrors() as $warn) {
                $warnings[] = $warn->getMessage();
            }
            $warnings = implode($warnings);
            throw new Exception\InvalidTask('postinstallscript', $file,
                                                             'Analysis of post-install script "' .
                                                             $fileXml['name'] . '" failed: ' . $warnings,
                                                             $validator->getErrors());
        }
        if (count($analysis['declared_classes']) != 1) {
            throw new Exception\InvalidTask('postinstallscript', $file,
                                                             'Post-install script "' .
                                                             $fileXml['name'] .
                                                             '" must declare exactly 1 class');
        }
        $class = $analysis['declared_classes'][0];
        if ($class != str_replace(array('/', '.php'), array('_', ''),
              $fileXml['name']) . '_postinstall') {
            throw new Exception\InvalidTask('postinstallscript', $file,
                                                             'Post-install script "' .
                                                             $fileXml['name'] . '" class "' .
                                                             $class . '" must be named "' .
                                                             str_replace(array('/', '.php'),
                                                                         array('_', ''),
                                                                         $fileXml['name']) .
                                                             '_postinstall"');
        }
        if (!isset($analysis['declared_methods'][$class])) {
            throw new Exception\InvalidTask('postinstallscript', $file,
                                                             'Post-install script "' .
                                                             $fileXml['name'] .
                                                             '" must declare methods init2() and run2()');
        }
        $methods = array('init2' => 0, 'run2' => 1);
        foreach ($analysis['declared_methods'][$class] as $method) {
            if (isset($methods[$method])) {
                unset($methods[$method]);
            }
        }
        if (count($methods)) {
            throw new Exception\InvalidTask('postinstallscript', $file,
                                                             'Post-install script "' .
                                                             $fileXml['name'] .
                                                             '" must declare methods init2() and run2()');
        }
        $definedparams = array();
        $tasksNamespace = $pkg->getTasksNs() . ':';
        if (!isset($xml[$tasksNamespace . 'paramgroup'])) {
            return true;
        }
        $params = $xml[$tasksNamespace . 'paramgroup'];
        if (!is_array($params) || !isset($params[0])) {
            $params = array($params);
        }
        foreach ($params as $param) {
            if (!isset($param[$tasksNamespace . 'id'])) {
                throw new Exception\InvalidTask('postinstallscript', $file,
                                                             'Post-install script "' .
                                                             $fileXml['name'] .
                                                             '" <paramgroup> must have ' .
                                                             'an <id> tag');
            }
            if (isset($param[$tasksNamespace . 'name'])) {
                if (!in_array($param[$tasksNamespace . 'name'], $definedparams)) {
                    throw new Exception\InvalidTask('postinstallscript', $file,
                                                    'Post-install script "' .
                                                    $fileXml['name'] . '" ' .
                                                    '<paramgroup> id "' .
                                                    $param[$tasksNamespace .'id'] .
                                                    '" conditiontype parameter "' .
                                                    $param[$tasksNamespace . 'name'] .
                                                    '" has not been previously defined');
                }
                if (!isset($param[$tasksNamespace . 'conditiontype'])) {
                    throw new Exception\InvalidTask('postinstallscript', $file,
                                                    'Post-install script "' .
                                                    $fileXml['name'] . '" ' .
                                                    '<paramgroup> id "' .
                                                    $param[$tasksNamespace . 'id'] .
                                                    '" must have a ' .
                                                    '<conditiontype> tag ' .
                                                    'containing either "=", ' .
                                                    '"!=", or "preg_match"');
                }
                if (!in_array($param[$tasksNamespace . 'conditiontype'],
                      array('=', '!=', 'preg_match'))) {
                    throw new Exception\InvalidTask('postinstallscript', $file,
                                                    'Post-install script "' .
                                                    $fileXml['name'] . '" ' .
                                                    '<paramgroup> id "' .
                                                    $param[$tasksNamespace . 'id'] .
                                                    '" must have a ' .
                                                    '<conditiontype> tag ' .
                                                    'containing either "=", ' .
                                                    '"!=", or "preg_match"');
                }
                if (!isset($param[$tasksNamespace . 'value'])) {
                    throw new Exception\InvalidTask('postinstallscript', $file,
                                                    'Post-install script "' .
                                                    $fileXml['name'] . '" ' .
                                                    '<paramgroup> id "' .
                                                    $param[$tasksNamespace . 'id'] .
                                                    '" must have a ' .
                                                    '<value> tag containing ' .
                                                    'expected parameter value');
                }
            }
            if (isset($param[$tasksNamespace . 'instructions'])) {
                if (!is_string($param[$tasksNamespace . 'instructions'])) {
                    throw new Exception\InvalidTask('postinstallscript', $file,
                                                    'Post-install script "' .
                                                    $fileXml['name'] . '" ' .
                                                    '<paramgroup> id "' .
                                                    $param[$tasksNamespace . 'id'] .
                                                    '" <instructions> must be simple text');
                }
            }
            if (!isset($param[$tasksNamespace . 'param'])) {
                continue; // <param> is no longer required
            }
            $subparams = $param[$tasksNamespace . 'param'];
            if (!is_array($subparams) || !isset($subparams[0])) {
                $subparams = array($subparams);
            }
            foreach ($subparams as $subparam) {
                if (!isset($subparam[$tasksNamespace . 'name'])) {
                    throw new Exception\InvalidTask('postinstallscript', $file,
                                                    'Post-install script "' .
                                                    $fileXml['name'] . '" parameter for ' .
                                                    '<paramgroup> id "' .
                                                    $param[$tasksNamespace . 'id'] .
                                                    '" must have ' .
                                                    'a <name> tag');
                }
                if (!preg_match('/[a-zA-Z0-9]+/',
                      $subparam[$tasksNamespace . 'name'])) {
                    throw new Exception\InvalidTask('postinstallscript', $file,
                                                    'Post-install script "' .
                                                    $fileXml['name'] . '" parameter "' .
                                                    $subparam[$tasksNamespace . 'name'] .
                                                    '" for ' .
                                                    '<paramgroup> id "' .
                                                    $param[$tasksNamespace . 'id'] .
                                                    '" is not a valid name.  Must ' .
                                                    'contain only alphanumeric characters');
                }
                if (!isset($subparam[$tasksNamespace . 'prompt'])) {
                    throw new Exception\InvalidTask('postinstallscript', $file,
                                                    'Post-install script "' .
                                                    $fileXml['name'] . '" parameter "' .
                                                    $subparam[$tasksNamespace . 'name'] .
                                                    '" for ' .
                                                    '<paramgroup> id "' .
                                                    $param[$tasksNamespace . 'id'] .
                                                    '" must have a ' .
                                                    '<prompt> tag');
                }
                if (!isset($subparam[$tasksNamespace . 'type'])) {
                    throw new Exception\InvalidTask('postinstallscript', $file,
                                                    'Post-install script "' .
                                                    $fileXml['name'] . '" parameter "' .
                                                    $subparam[$tasksNamespace . 'name'] .
                                                    '" for ' .
                                                    '<paramgroup> id "' .
                                                    $param[$tasksNamespace . 'id'] .
                                                    '" must have a ' .
                                                    '<type> tag');
                }
                $definedparams[] = $param[$tasksNamespace . 'id'] . '::' .
                $subparam[$tasksNamespace . 'name'];
            }
        }
        return true;
    }