/** * @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; }
/** * @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; }
function doFileList($command, $options, $params) { if (count($params) !== 1) { return $this->raiseError('list-files expects 1 parameter'); } $reg =& $this->config->getRegistry(); $fp = false; if (!is_dir($params[0]) && (file_exists($params[0]) || ($fp = @fopen($params[0], 'r')))) { if ($fp) { fclose($fp); } if (!class_exists('PEAR_PackageFile')) { require_once 'PEAR/PackageFile.php'; } $pkg =& new PEAR_PackageFile($this->config, $this->_debug); PEAR::staticPushErrorHandling(PEAR_ERROR_RETURN); $info =& $pkg->fromAnyFile($params[0], PEAR_VALIDATE_NORMAL); PEAR::staticPopErrorHandling(); $headings = array('Package File', 'Install Path'); $installed = false; } else { PEAR::staticPushErrorHandling(PEAR_ERROR_RETURN); $parsed = $reg->parsePackageName($params[0], $this->config->get('default_channel')); PEAR::staticPopErrorHandling(); if (PEAR::isError($parsed)) { return $this->raiseError($parsed); } $info =& $reg->getPackage($parsed['package'], $parsed['channel']); $headings = array('Type', 'Install Path'); $installed = true; } if (PEAR::isError($info)) { return $this->raiseError($info); } if ($info === null) { return $this->raiseError("`{$params['0']}' not installed"); } $list = $info->getPackagexmlVersion() == '1.0' || $installed ? $info->getFilelist() : $info->getContents(); if ($installed) { $caption = 'Installed Files For ' . $params[0]; } else { $caption = 'Contents of ' . basename($params[0]); } $data = array('caption' => $caption, 'border' => true, 'headline' => $headings); if ($info->getPackagexmlVersion() == '1.0' || $installed) { foreach ($list as $file => $att) { if ($installed) { if (empty($att['installed_as'])) { continue; } $data['data'][] = array($att['role'], $att['installed_as']); } else { if (isset($att['baseinstalldir']) && !in_array($att['role'], array('test', 'data', 'doc'))) { $dest = $att['baseinstalldir'] . DIRECTORY_SEPARATOR . $file; } else { $dest = $file; } switch ($att['role']) { case 'test': case 'data': case 'doc': $role = $att['role']; if ($role == 'test') { $role .= 's'; } $dest = $this->config->get($role . '_dir') . DIRECTORY_SEPARATOR . $info->getPackage() . DIRECTORY_SEPARATOR . $dest; break; case 'php': default: $dest = $this->config->get('php_dir') . DIRECTORY_SEPARATOR . $dest; } $ds2 = DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR; $dest = preg_replace(array('!\\\\+!', '!/!', "!{$ds2}+!"), array(DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR), $dest); $file = preg_replace('!/+!', '/', $file); $data['data'][] = array($file, $dest); } } } else { // package.xml 2.0, not installed if (!isset($list['dir']['file'][0])) { $list['dir']['file'] = array($list['dir']['file']); } foreach ($list['dir']['file'] as $att) { $att = $att['attribs']; $file = $att['name']; $role =& PEAR_Installer_Role::factory($info, $att['role'], $this->config); $role->setup($this, $info, $att, $file); if (!$role->isInstallable()) { $dest = '(not installable)'; } else { $dest = $role->processInstallation($info, $att, $file, ''); if (PEAR::isError($dest)) { $dest = '(Unknown role "' . $att['role'] . ')'; } else { list(, , $dest) = $dest; } } $data['data'][] = array($file, $dest); } } $this->ui->outputData($data, $command); return true; }
/** * Upload an installed package - does not work with register-only packages! * @param PEAR_PackageFile_v1|PEAR_PackageFile_v2 */ public function ftpInstall($pkg) { if ($pkg->getPackageType() != 'php') { return PEAR::raiseError('Error: can only install PHP scripts remotely,' . ' no PHP extensions can be compiled remotely'); } $ftp =& $this->config->getFTP(); if (!$ftp) { return PEAR::raiseError('FTP client not initialized'); } $this->log(2, 'Connect to FTP server'); $e = $ftp->init(); if (PEAR::isError($e)) { return $e; } $pf =& $this->_registry->getPackage($pkg->getPackage(), $pkg->getChannel()); foreach ($pf->getFilelist() as $file => $atts) { if ($pf->getPackagexmlVersion() == '1.0') { $channel = 'pear.php.net'; switch ($atts['role']) { case 'doc': case 'data': case 'test': $dest_dir = $this->config->get($atts['role'] . '_dir', 'ftp', $channel) . DIRECTORY_SEPARATOR . $pf->getPackage(); unset($atts['baseinstalldir']); break; case 'ext': case 'php': $dest_dir = $this->config->get($atts['role'] . '_dir', 'ftp', $channel); break; case 'script': $dest_dir = $this->config->get('bin_dir', 'ftp', $channel); break; default: continue 2; } $save_destdir = $dest_dir; if (!empty($atts['baseinstalldir'])) { $dest_dir .= DIRECTORY_SEPARATOR . $atts['baseinstalldir']; } if (dirname($file) != '.' && empty($atts['install-as'])) { $dest_dir .= DIRECTORY_SEPARATOR . dirname($file); } if (empty($atts['install-as'])) { $dest_file = $dest_dir . DIRECTORY_SEPARATOR . basename($file); } else { $dest_file = $dest_dir . DIRECTORY_SEPARATOR . $atts['install-as']; } // Clean up the DIRECTORY_SEPARATOR mess $ds2 = DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR; $dest_file = preg_replace(array('!\\+!', '!/!', "!{$ds2}+!"), array('/', '/', '/'), $dest_file); } else { $role =& PEAR_Installer_Role::factory($pf, $atts['role'], $this->config); $role->setup($this, $pf, $atts, $file); if (!$role->isInstallable()) { continue; // this shouldn't happen } list($save_destdir, $dest_dir, $dest_file, $orig_file) = $role->processInstallation($pkg, $atts, $file, $file, 'ftp'); $dest_file = str_replace(DIRECTORY_SEPARATOR, '/', $dest_file); } $installedas = $atts['installed_as']; $this->log(2, 'Uploading "' . $installedas . '" to "' . $dest_file . '"'); if (PEAR::isError($e = $ftp->installFile($installedas, $dest_file))) { return $e; } } $this->log(2, 'Disconnect from FTP server'); return $ftp->disconnect(); }