/** * Scan through the Command directory looking for classes * and see what commands they implement. * @param string which directory to look for classes, defaults to * the Installer/Roles subdirectory of * the directory from where this file (__FILE__) is * included. * * @return bool TRUE on success, a PEAR error on failure * @access public * @static */ function registerRoles($dir = null) { $GLOBALS['_PEAR_INSTALLER_ROLES'] = array(); $parser = new PEAR_XMLParser(); if ($dir === null) { $dir = dirname(__FILE__) . '/Role'; } if (!file_exists($dir) || !is_dir($dir)) { return PEAR::raiseError("registerRoles: opendir({$dir}) failed"); } $dp = @opendir($dir); if (empty($dp)) { return PEAR::raiseError("registerRoles: opendir({$dir}) failed"); } while ($entry = readdir($dp)) { if ($entry[0] == '.' || substr($entry, -4) != '.xml') { continue; } $class = "PEAR_Installer_Role_" . substr($entry, 0, -4); // List of roles if (!isset($GLOBALS['_PEAR_INSTALLER_ROLES'][$class])) { $file = "{$dir}/{$entry}"; $parser->parse(file_get_contents($file)); $data = $parser->getData(); if (!is_array($data['releasetypes'])) { $data['releasetypes'] = array($data['releasetypes']); } $GLOBALS['_PEAR_INSTALLER_ROLES'][$class] = $data; } } closedir($dp); ksort($GLOBALS['_PEAR_INSTALLER_ROLES']); PEAR_Installer_Role::getBaseinstallRoles(true); PEAR_Installer_Role::getInstallableRoles(true); PEAR_Installer_Role::getPhpRoles(true); PEAR_Installer_Role::getValidRoles('****', true); return true; }
/** * @param PEAR_PackageFile_v1|PEAR_PackageFile_v2 * @param string filename * @param array attributes from <file> tag in package.xml * @param string path to install the file in * @param array options from command-line * @access private */ function _installFile2(&$pkg, $file, $atts, $tmp_path, $options) { if (!isset($this->_registry)) { $this->_registry =& $this->config->getRegistry(); } $channel = $pkg->getChannel(); // {{{ assemble the destination paths if (!in_array($atts['attribs']['role'], PEAR_Installer_Role::getValidRoles($pkg->getPackageType()))) { return $this->raiseError('Invalid role `' . $atts['attribs']['role'] . "' for file {$file}"); } $role =& PEAR_Installer_Role::factory($pkg, $atts['attribs']['role'], $this->config); $err = $role->setup($this, $pkg, $atts['attribs'], $file); if (PEAR::isError($err)) { return $err; } if (!$role->isInstallable()) { return; } $info = $role->processInstallation($pkg, $atts['attribs'], $file, $tmp_path); if (PEAR::isError($info)) { return $info; } else { list($save_destdir, $dest_dir, $dest_file, $orig_file) = $info; } $final_dest_file = $installed_as = $dest_file; if (isset($this->_options['packagingroot'])) { $final_dest_file = $this->_prependPath($final_dest_file, $this->_options['packagingroot']); } $dest_dir = dirname($final_dest_file); $dest_file = $dest_dir . DIRECTORY_SEPARATOR . '.tmp' . basename($final_dest_file); // }}} if (empty($this->_options['register-only'])) { if (!@is_dir($dest_dir)) { if (!$this->mkDirHier($dest_dir)) { return $this->raiseError("failed to mkdir {$dest_dir}", PEAR_INSTALLER_FAILED); } $this->log(3, "+ mkdir {$dest_dir}"); } } $attribs = $atts['attribs']; unset($atts['attribs']); // pretty much nothing happens if we are only registering the install if (empty($this->_options['register-only'])) { if (!count($atts)) { // no tasks if (!file_exists($orig_file)) { return $this->raiseError("file {$orig_file} does not exist", PEAR_INSTALLER_FAILED); } if (!@copy($orig_file, $dest_file)) { return $this->raiseError("failed to write {$dest_file}", PEAR_INSTALLER_FAILED); } $this->log(3, "+ cp {$orig_file} {$dest_file}"); if (isset($attribs['md5sum'])) { $md5sum = md5_file($dest_file); } } else { // file with tasks if (!file_exists($orig_file)) { return $this->raiseError("file {$orig_file} does not exist", PEAR_INSTALLER_FAILED); } if (function_exists('file_get_contents')) { $contents = file_get_contents($orig_file); } else { $fp = fopen($orig_file, "r"); $contents = @fread($fp, filesize($orig_file)); // filesize can be 0 fclose($fp); } if ($contents === false) { $contents = ''; } if (isset($attribs['md5sum'])) { $md5sum = md5($contents); } foreach ($atts as $tag => $raw) { $tag = str_replace($pkg->getTasksNs() . ':', '', $tag); $task = "PEAR_Task_{$tag}"; $task =& new $task($this->config, $this, PEAR_TASK_INSTALL); if (!$task->isScript()) { // scripts are only handled after installation $task->init($raw, $attribs, $pkg->getLastInstalledVersion()); $res = $task->startSession($pkg, $contents, $final_dest_file); if ($res === false) { continue; // skip this file } if (PEAR::isError($res)) { return $res; } $contents = $res; // save changes } $wp = @fopen($dest_file, "wb"); if (!is_resource($wp)) { return $this->raiseError("failed to create {$dest_file}: {$php_errormsg}", PEAR_INSTALLER_FAILED); } if (fwrite($wp, $contents) === false) { return $this->raiseError("failed writing to {$dest_file}: {$php_errormsg}", PEAR_INSTALLER_FAILED); } fclose($wp); } } // {{{ check the md5 if (isset($md5sum)) { if (strtolower($md5sum) == strtolower($attribs['md5sum'])) { $this->log(2, "md5sum ok: {$final_dest_file}"); } else { if (empty($options['force'])) { // delete the file @unlink($dest_file); if (!isset($options['ignore-errors'])) { return $this->raiseError("bad md5sum for file {$final_dest_file}", PEAR_INSTALLER_FAILED); } else { if (!isset($options['soft'])) { $this->log(0, "warning : bad md5sum for file {$final_dest_file}"); } } } else { if (!isset($options['soft'])) { $this->log(0, "warning : bad md5sum for file {$final_dest_file}"); } } } } // }}} // {{{ set file permissions if (!OS_WINDOWS) { if ($role->isExecutable()) { $mode = 0777 & ~(int) octdec($this->config->get('umask')); $this->log(3, "+ chmod +x {$dest_file}"); } else { $mode = 0666 & ~(int) octdec($this->config->get('umask')); } $this->addFileOperation("chmod", array($mode, $dest_file)); if (!@chmod($dest_file, $mode)) { if (!isset($options['soft'])) { $this->log(0, "failed to change mode of {$dest_file}"); } } } // }}} $this->addFileOperation("rename", array($dest_file, $final_dest_file, $role->isExtension())); } // Store the full path where the file was installed for easy uninstall $this->addFileOperation("installed_as", array($file, $installed_as, $save_destdir, dirname(substr($dest_file, strlen($save_destdir))))); //$this->log(2, "installed: $dest_file"); return PEAR_INSTALLER_OK; }
function _invalidFileRole($file, $dir, $role) { $this->_stack->push(__FUNCTION__, 'error', array('file' => $file, 'dir' => $dir, 'role' => $role, 'roles' => PEAR_Installer_Role::getValidRoles($this->_pf->getPackageType())), 'File "%file%" in directory "%dir%" has invalid role "%role%", should be one of %roles%'); }
/** * Scan through the Command directory looking for classes * and see what commands they implement. * @param string which directory to look for classes, defaults to * the Installer/Roles subdirectory of * the directory from where this file (__FILE__) is * included. * * @return bool TRUE on success, a PEAR error on failure * @access public * @static */ function registerRoles($dir = null) { if ($dir === null) { $dir = dirname(__FILE__) . '/Role'; } $dp = @opendir($dir); if (empty($dp)) { return PEAR::raiseError("registerRoles: opendir({$dir}) failed"); } while ($entry = readdir($dp)) { if ($entry[0] == '.' || substr($entry, -4) != '.php' || $entry == 'Common.php') { continue; } $class = "PEAR_Installer_Role_" . substr($entry, 0, -4); $file = "{$dir}/{$entry}"; include_once $file; // List of roles if (empty($GLOBALS['_PEAR_INSTALLER_ROLES'][$class])) { $GLOBALS['_PEAR_INSTALLER_ROLES'][$class] = call_user_func(array($class, 'getInfo')); } } @closedir($dp); ksort($GLOBALS['_PEAR_INSTALLER_ROLES']); PEAR_Installer_Role::getBaseinstallRoles(true); PEAR_Installer_Role::getInstallableRoles(true); PEAR_Installer_Role::getPhpRoles(true); PEAR_Installer_Role::getValidRoles('****', true); return true; }
/** * @param PEAR_PackageFile_v1|PEAR_PackageFile_v2 * @param string filename * @param array attributes from <file> tag in package.xml * @param string path to install the file in * @param array options from command-line * @access private */ function _installFile2(&$pkg, $file, &$real_atts, $tmp_path, $options) { $atts = $real_atts; if (!isset($this->_registry)) { $this->_registry =& $this->config->getRegistry(); } $channel = $pkg->getChannel(); // assemble the destination paths $roles = PEAR_Installer_Role::getValidRoles($pkg->getPackageType()); if (!in_array($atts['attribs']['role'], $roles)) { return $this->raiseError('Invalid role `' . $atts['attribs']['role'] . "' for file {$file}"); } $role =& PEAR_Installer_Role::factory($pkg, $atts['attribs']['role'], $this->config); $err = $role->setup($this, $pkg, $atts['attribs'], $file); if (PEAR::isError($err)) { return $err; } if (!$role->isInstallable()) { return; } $info = $role->processInstallation($pkg, $atts['attribs'], $file, $tmp_path); if (PEAR::isError($info)) { return $info; } list($save_destdir, $dest_dir, $dest_file, $orig_file) = $info; if (preg_match('~/\\.\\.(/|\\z)|^\\.\\./~', str_replace('\\', '/', $dest_file))) { return $this->raiseError("SECURITY ERROR: file {$file} (installed to {$dest_file}) contains parent directory reference ..", PEAR_INSTALLER_FAILED); } $final_dest_file = $installed_as = $dest_file; if (isset($this->_options['packagingroot'])) { $final_dest_file = $this->_prependPath($final_dest_file, $this->_options['packagingroot']); } $dest_dir = dirname($final_dest_file); $dest_file = $dest_dir . DIRECTORY_SEPARATOR . '.tmp' . basename($final_dest_file); if (empty($this->_options['register-only'])) { if (!file_exists($dest_dir) || !is_dir($dest_dir)) { if (!$this->mkDirHier($dest_dir)) { return $this->raiseError("failed to mkdir {$dest_dir}", PEAR_INSTALLER_FAILED); } $this->log(3, "+ mkdir {$dest_dir}"); } } $attribs = $atts['attribs']; unset($atts['attribs']); // pretty much nothing happens if we are only registering the install $htype = 'md5sum'; if (empty($this->_options['register-only'])) { if (!count($atts)) { // no tasks if (!file_exists($orig_file)) { return $this->raiseError("file {$orig_file} does not exist", PEAR_INSTALLER_FAILED); } if (!@copy($orig_file, $dest_file)) { return $this->raiseError("failed to write {$dest_file}: {$php_errormsg}", PEAR_INSTALLER_FAILED); } $this->log(3, "+ cp {$orig_file} {$dest_file}"); if (isset($attribs['sha1sum'])) { $hash = sha1_file($dest_file); $htype = 'sha1sum'; } elseif (isset($attribs['md5sum'])) { $hash = md5_file($dest_file); $htype = 'md5sum'; } } else { // file with tasks if (!file_exists($orig_file)) { return $this->raiseError("file {$orig_file} does not exist", PEAR_INSTALLER_FAILED); } $contents = file_get_contents($orig_file); if ($contents === false) { $contents = ''; } if (isset($attribs['sha1sum'])) { $hash = sha1($contents); $htype = 'sha1sum'; } elseif (isset($attribs['md5sum'])) { $hash = md5($contents); $htype = 'md5sum'; } foreach ($atts as $tag => $raw) { $tag = str_replace(array($pkg->getTasksNs() . ':', '-'), array('', '_'), $tag); $task = "PEAR_Task_{$tag}"; $task =& new $task($this->config, $this, PEAR_TASK_INSTALL); if (!$task->isScript()) { // scripts are only handled after installation $task->init($raw, $attribs, $pkg->getLastInstalledVersion()); $res = $task->startSession($pkg, $contents, $final_dest_file); if ($res === false) { continue; // skip this file } if (PEAR::isError($res)) { return $res; } $contents = $res; // save changes } $wp = @fopen($dest_file, "wb"); if (!is_resource($wp)) { return $this->raiseError("failed to create {$dest_file}: {$php_errormsg}", PEAR_INSTALLER_FAILED); } if (fwrite($wp, $contents) === false) { return $this->raiseError("failed writing to {$dest_file}: {$php_errormsg}", PEAR_INSTALLER_FAILED); } fclose($wp); } } // check the hash if (isset($hash)) { // Make sure the original hash sum matches with expected if (strtolower($hash) === strtolower($attribs[$htype])) { $this->log(2, $htype . " ok: {$final_dest_file}"); if (isset($contents)) { // set hash sum based on $content in case any tasks were run. $real_atts['attribs'][$htype] = $htype == 'sha1sum' ? sha1($contents) : md5($contents); } } else { if (empty($options['force'])) { // delete the file if (file_exists($dest_file)) { unlink($dest_file); } if (!isset($options['ignore-errors'])) { return $this->raiseError("bad " . $htype . " for file {$final_dest_file}", PEAR_INSTALLER_FAILED); } if (!isset($options['soft'])) { $this->log(0, "warning : bad " . $htype . " for file {$final_dest_file}"); } } else { if (!isset($options['soft'])) { $this->log(0, "warning : bad " . $htype . " for file {$final_dest_file}"); } } } } else { $real_atts['attribs'][$htype] = $htype == 'sha1sum' ? sha1_file($dest_file) : md5_file($dest_file); } //set file permissions if (!OS_WINDOWS) { if ($role->isExecutable()) { $mode = 0777 & ~(int) octdec($this->config->get('umask')); $this->log(3, "+ chmod +x {$dest_file}"); } else { $mode = 0666 & ~(int) octdec($this->config->get('umask')); } if ($attribs['role'] != 'src') { $this->addFileOperation("chmod", array($mode, $dest_file)); if (!@chmod($dest_file, $mode)) { if (!isset($options['soft'])) { $this->log(0, "failed to change mode of {$dest_file}: {$php_errormsg}"); } } } } if ($attribs['role'] == 'src') { rename($dest_file, $final_dest_file); $this->log(2, "renamed source file {$dest_file} to {$final_dest_file}"); } else { $this->addFileOperation("rename", array($dest_file, $final_dest_file, $role->isExtension())); } } // Store the full path where the file was installed for easy uninstall if ($attribs['role'] != 'src') { $loc = $this->config->get($role->getLocationConfig(), null, $channel); $this->addFileOperation('installed_as', array($file, $installed_as, $loc, dirname(substr($installed_as, strlen($loc))))); } //$this->log(2, "installed: $dest_file"); return PEAR_INSTALLER_OK; }
/** * Add an extension/role mapping to the role mapping option * * Roles influence both where a file is installed and how it is installed. * Files with role="data" are in a completely different directory hierarchy * from the program files of role="php" * * In PEAR 1.3b2, these roles are * - php (most common) * - data * - doc * - test * - script (gives the file an executable attribute) * - src * * @param string $extension file extension * @param string $role file role * * @return void|PEAR_Error * @throws PEAR_PACKAGEFILEMANAGER2_INVALID_ROLE * @access public * @since 1.6.0a1 */ function addRole($extension, $role) { include_once 'PEAR/Installer/Role.php'; $roles = PEAR_Installer_Role::getValidRoles($this->getPackageType()); if (!in_array($role, $roles)) { return $this->raiseError(PEAR_PACKAGEFILEMANAGER2_INVALID_ROLE, implode($roles, ', '), $role); } $this->_options['roles'][$extension] = $role; }