📄 installer.php
字号:
return count($files);
}
/**
* Method to parse through a files element of the installation manifest and remove
* the files that were installed
*
* @access public
* @param object $element The xml node to process
* @param int $cid Application ID of application to remove from
* @return boolean True on success
* @since 1.5
*/
function removeFiles($element, $cid=0)
{
// Initialize variables
$removefiles = array ();
$retval = true;
// Get the client info
jimport('joomla.application.helper');
$client =& JApplicationHelper::getClientInfo($cid);
if (!is_a($element, 'JSimpleXMLElement') || !count($element->children())) {
// Either the tag does not exist or has no children therefore we return zero files processed.
return true;
}
// Get the array of file nodes to process
$files = $element->children();
if (count($files) == 0) {
// No files to process
return true;
}
/*
* Here we set the folder we are going to remove the files from. There are a few
* special cases that need to be considered for certain reserved tags.
*/
switch ($element->name())
{
case 'media':
if ($element->attributes('destination')) {
$folder = $element->attributes('destination');
} else {
$folder = 'stories';
}
$source = $client->path.DS.'images'.DS.$folder;
break;
case 'languages':
$source = $client->path.DS.'language';
break;
default:
if ($client) {
$pathname = 'extension_'.$client->name;
$source = $this->getPath($pathname);
} else {
$pathname = 'extension_root';
$source = $this->getPath($pathname);
}
break;
}
// Process each file in the $files array (children of $tagName).
foreach ($files as $file)
{
/*
* If the file is a language, we must handle it differently. Language files
* go in a subdirectory based on the language code, ie.
*
* <language tag="en_US">en_US.mycomponent.ini</language>
*
* would go in the en_US subdirectory of the languages directory.
*/
if ($file->name() == 'language' && $file->attributes('tag') != '') {
$path = $source.DS.$file->attributes('tag').DS.basename($file->data());
// If the language folder is not present, then the core pack hasn't been installed... ignore
if (!JFolder::exists(dirname($path))) {
continue;
}
} else {
$path = $source.DS.$file->data();
}
/*
* Actually delete the files/folders
*/
if (is_dir($path)) {
$val = JFolder::delete($path);
} else {
$val = JFile::delete($path);
}
if ($val === false) {
$retval = false;
}
}
return $retval;
}
/**
* Copies the installation manifest file to the extension folder in the given client
*
* @access public
* @param int $cid Where to copy the installfile [optional: defaults to 1 (admin)]
* @return boolean True on success, False on error
* @since 1.5
*/
function copyManifest($cid=1)
{
// Get the client info
jimport('joomla.application.helper');
$client =& JApplicationHelper::getClientInfo($cid);
$path['src'] = $this->getPath('manifest');
if ($client) {
$pathname = 'extension_'.$client->name;
$path['dest'] = $this->getPath($pathname).DS.basename($this->getPath('manifest'));
} else {
$pathname = 'extension_root';
$path['dest'] = $this->getPath($pathname).DS.basename($this->getPath('manifest'));
}
return $this->copyFiles(array ($path), true);
}
/**
* Tries to find the package manifest file
*
* @access private
* @return boolean True on success, False on error
* @since 1.0
*/
function _findManifest()
{
// Get an array of all the xml files from teh installation directory
$xmlfiles = JFolder::files($this->getPath('source'), '.xml$', 1, true);
// If at least one xml file exists
if (count($xmlfiles) > 0) {
foreach ($xmlfiles as $file)
{
// Is it a valid joomla installation manifest file?
$manifest = $this->_isManifest($file);
if (!is_null($manifest)) {
// If the root method attribute is set to upgrade, allow file overwrite
$root =& $manifest->document;
if ($root->attributes('method') == 'upgrade') {
$this->_overwrite = true;
}
// Set the manifest object and path
$this->_manifest =& $manifest;
$this->setPath('manifest', $file);
// Set the installation source path to that of the manifest file
$this->setPath('source', dirname($file));
return true;
}
}
// None of the xml files found were valid install files
JError::raiseWarning(1, 'JInstaller::install: '.JText::_('ERRORJOSXMLSETUP'));
return false;
} else {
// No xml files were found in the install folder
JError::raiseWarning(1, 'JInstaller::install: '.JText::_('ERRORXMLSETUP'));
return false;
}
}
/**
* Is the xml file a valid Joomla installation manifest file
*
* @access private
* @param string $file An xmlfile path to check
* @return mixed A JSimpleXML document, or null if the file failed to parse
* @since 1.5
*/
function &_isManifest($file)
{
// Initialize variables
$null = null;
$xml =& JFactory::getXMLParser('Simple');
// If we cannot load the xml file return null
if (!$xml->loadFile($file)) {
// Free up xml parser memory and return null
unset ($xml);
return $null;
}
/*
* Check for a valid XML root tag.
* @todo: Remove backwards compatability in a future version
* Should be 'install', but for backward compatability we will accept 'mosinstall'.
*/
$root =& $xml->document;
if ($root->name() != 'install' && $root->name() != 'mosinstall') {
// Free up xml parser memory and return null
unset ($xml);
return $null;
}
// Valid manifest file return the object
return $xml;
}
}
/**
* Installer helper class
*
* @static
* @author Louis Landry <louis.landry@joomla.org>
* @package Joomla.Framework
* @subpackage Installer
* @since 1.5
*/
class JInstallerHelper
{
/**
* Downloads a package
*
* @static
* @param string URL of file to download
* @param string Download target filename [optional]
* @return mixed Path to downloaded package or boolean false on failure
* @since 1.5
*/
function downloadPackage($url, $target = false)
{
$config =& JFactory::getConfig();
// Capture PHP errors
$php_errormsg = 'Error Unknown';
ini_set('track_errors', true);
// Set user agent
ini_set('user_agent', "Joomla! 1.5 Installer");
// Open the remote server socket for reading
$inputHandle = @ fopen($url, "r");
if (!$inputHandle) {
JError::raiseWarning(42, 'Remote Server connection failed: '.$php_errormsg);
return false;
}
$meta_data = stream_get_meta_data($inputHandle);
foreach ($meta_data['wrapper_data'] as $wrapper_data)
{
if (substr($wrapper_data, 0, strlen("Content-Disposition")) == "Content-Disposition") {
$contentfilename = explode ("\"", $wrapper_data);
$target = $contentfilename[1];
}
}
// Set the target path if not given
if (!$target) {
$target = $config->getValue('config.tmp_path').DS.JInstallerHelper::getFilenameFromURL($url);
} else {
$target = $config->getValue('config.tmp_path').DS.basename($target);
}
// Initialize contents buffer
$contents = null;
while (!feof($inputHandle))
{
$contents .= fread($inputHandle, 4096);
if ($contents == false) {
JError::raiseWarning(44, 'Failed reading network resource: '.$php_errormsg);
return false;
}
}
// Write buffer to file
JFile::write($target, $contents);
// Close file pointer resource
fclose($inputHandle);
// Return the name of the downloaded package
return basename($target);
}
/**
* Unpacks a file and verifies it as a Joomla element package
* Supports .gz .tar .tar.gz and .zip
*
* @static
* @param string $p_filename The uploaded package filename or install directory
* @return boolean True on success, False on error
* @since 1.5
*/
function unpack($p_filename)
{
// Path to the archive
$archivename = $p_filename;
// Temporary folder to extract the archive into
$tmpdir = uniqid('install_');
// Clean the paths to use for archive extraction
$extractdir = JPath::clean(dirname($p_filename).DS.$tmpdir);
$archivename = JPath::clean($archivename);
// do the unpacking of the archive
$result = JArchive::extract( $archivename, $extractdir);
if ( $result === false ) {
return false;
}
/*
* Lets set the extraction directory and package file in the result array so we can
* cleanup everything properly later on.
*/
$retval['extractdir'] = $extractdir;
$retval['packagefile'] = $archivename;
/*
* Try to find the correct install directory. In case the package is inside a
* subdirectory detect this and set the install directory to the correct path.
*
* List all the items in the installation directory. If there is only one, and
* it is a folder, then we will set that folder to be the installation folder.
*/
$dirList = array_merge(JFolder::files($extractdir, ''), JFolder::folders($extractdir, ''));
if (count($dirList) == 1)
{
if (JFolder::exists($extractdir.DS.$dirList[0]))
{
$extractdir = JPath::clean($extractdir.DS.$dirList[0]);
}
}
/*
* We have found the install directory so lets set it and then move on
* to detecting the extension type.
*/
$retval['dir'] = $extractdir;
/*
* Get the extension type and return the directory/type array on success or
* false on fail.
*/
if ($retval['type'] = JInstallerHelper::detectType($extractdir))
{
return $retval;
} else
{
return false;
}
}
/**
* Method to detect the extension type from a package directory
*
* @static
* @param string $p_dir Path to package directory
* @return mixed Extension type string or boolean false on fail
* @since 1.5
*/
function detectType($p_dir)
{
// Search the install dir for an xml file
$files = JFolder::files($p_dir, '\.xml$', 1, true);
if (count($files) > 0)
{
foreach ($files as $file)
{
$xmlDoc = & JFactory::getXMLParser();
$xmlDoc->resolveErrors(true);
if (!$xmlDoc->loadXML($file, false, true))
{
// Free up memory from DOMIT parser
unset ($xmlDoc);
continue;
}
$root = & $xmlDoc->documentElement;
if ($root->getTagName() != "install" && $root->getTagName() != 'mosinstall')
{
continue;
}
$type = $root->getAttribute('type');
// Free up memory from DOMIT parser
unset ($xmlDoc);
return $type;
}
JError::raiseWarning(1, JText::_('ERRORNOTFINDJOOMLAXMLSETUPFILE'));
// Free up memory from DOMIT parser
unset ($xmlDoc);
return false;
} else
{
JError::raiseWarning(1, JText::_('ERRORNOTFINDXMLSETUPFILE'));
return false;
}
}
/**
* Gets a file name out of a url
*
* @static
* @param string $url URL to get name from
* @return mixed String filename or boolean false if failed
* @since 1.5
*/
function getFilenameFromURL($url)
{
if (is_string($url)) {
$parts = explode('/', $url);
return $parts[count($parts) - 1];
}
return false;
}
/**
* Clean up temporary uploaded package and unpacked extension
*
* @static
* @param string $p_file Path to the uploaded package file
* @param string $resultdir Path to the unpacked extension
* @return boolean True on success
* @since 1.5
*/
function cleanupInstall($package, $resultdir)
{
$config =& JFactory::getConfig();
// Does the unpacked extension directory exist?
if (is_dir($resultdir)) {
JFolder::delete($resultdir);
}
// Is the package file a valid file?
if (is_file($package)) {
JFile::delete($package);
} elseif (is_file(JPath::clean($config->getValue('config.tmp_path').DS.$package))) {
// It might also be just a base filename
JFile::delete(JPath::clean($config->getValue('config.tmp_path').DS.$package));
}
}
/**
* Splits contents of a sql file into array of discreet queries
* queries need to be delimited with end of statement marker ';'
* @param string
* @return array
*/
function splitSql($sql)
{
$sql = trim($sql);
$sql = preg_replace("/\n\#[^\n]*/", '', "\n".$sql);
$buffer = array ();
$ret = array ();
$in_string = false;
for ($i = 0; $i < strlen($sql) - 1; $i ++)
{
if ($sql[$i] == ";" && !$in_string) {
$ret[] = substr($sql, 0, $i);
$sql = substr($sql, $i +1);
$i = 0;
}
if ($in_string && ($sql[$i] == $in_string) && $buffer[1] != "\\") {
$in_string = false;
} elseif (!$in_string && ($sql[$i] == '"' || $sql[$i] == "'") && (!isset ($buffer[0]) || $buffer[0] != "\\")) {
$in_string = $sql[$i];
} if (isset ($buffer[1])) {
$buffer[0] = $buffer[1];
}
$buffer[1] = $sql[$i];
}
if (!empty ($sql)) {
$ret[] = $sql;
}
return ($ret);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -