📄 helper.php
字号:
<?php/** * @version $Id: helper.php 10381 2008-06-01 03:35:53Z pasamio $ * @package Joomla * @subpackage Installation * @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved. * @license GNU/GPL, see LICENSE.php * Joomla! is free software. This version may have been modified pursuant * to the GNU General Public License, and as distributed it includes or * is derivative of works licensed under the GNU General Public License or * other free or open source software licenses. * See COPYRIGHT.php for copyright notices and details. */// no direct accessdefined('_JEXEC') or die('Restricted access');/** * @package Joomla * @subpackage Installation */class JInstallationHelper{ /** * @return string A guess at the db required */ function detectDB() { $map = array ('mysql_connect' => 'mysql', 'mysqli_connect' => 'mysqli', 'mssql_connect' => 'mssql'); foreach ($map as $f => $db) { if (function_exists($f)) { return $db; } } return 'mysql'; } /** * @param array * @return string */ function errors2string(& $errors) { $buffer = ''; foreach ($errors as $error) { $buffer .= 'SQL='.$error['msg'].":\n- - - - - - - - - -\n".$error['sql']."\n= = = = = = = = = =\n\n"; } return $buffer; } /** * Creates a new database * @param object Database connector * @param string Database name * @param boolean utf-8 support * @param string Selected collation * @return boolean success */ function createDatabase(& $db, $DBname, $DButfSupport) { if ($DButfSupport) { $sql = "CREATE DATABASE `$DBname` CHARACTER SET `utf8`"; } else { $sql = "CREATE DATABASE `$DBname`"; } $db->setQuery($sql); $db->query(); $result = $db->getErrorNum(); if ($result != 0) { return false; } return true; } /** * Sets character set of the database to utf-8 with selected collation * Used in instances of pre-existing database * @param object Database object * @param string Database name * @param string Selected collation * @return boolean success */ function setDBCharset(& $db, $DBname) { if ($db->hasUTF()) { $sql = "ALTER DATABASE `$DBname` CHARACTER SET `utf8`"; $db->setQuery($sql); $db->query(); $result = $db->getErrorNum(); if ($result != 0) { return false; } } return true; } /** * Backs up existing tables * @param object Database connector * @param array An array of errors encountered */ function backupDatabase(& $db, $DBname, $DBPrefix, & $errors) { // Initialize backup prefix variable // TODO: Should this be user-defined? $BUPrefix = 'bak_'; $query = "SHOW TABLES FROM `$DBname`"; $db->setQuery($query); $errors = array (); if ($tables = $db->loadResultArray()) { foreach ($tables as $table) { if (strpos($table, $DBPrefix) === 0) { $butable = str_replace($DBPrefix, $BUPrefix, $table); $query = "DROP TABLE IF EXISTS `$butable`"; $db->setQuery($query); $db->query(); if ($db->getErrorNum()) { $errors[$db->getQuery()] = $db->getErrorMsg(); } $query = "RENAME TABLE `$table` TO `$butable`"; $db->setQuery($query); $db->query(); if ($db->getErrorNum()) { $errors[$db->getQuery()] = $db->getErrorMsg(); } } } } return count($errors); } /** * Deletes all database tables * @param object Database connector * @param array An array of errors encountered */ function deleteDatabase(& $db, $DBname, $DBPrefix, & $errors) { $query = "SHOW TABLES FROM `$DBname`"; $db->setQuery($query); $errors = array (); if ($tables = $db->loadResultArray()) { foreach ($tables as $table) { if (strpos($table, $DBPrefix) === 0) { $query = "DROP TABLE IF EXISTS `$table`"; $db->setQuery($query); $db->query(); if ($db->getErrorNum()) { $errors[$db->getQuery()] = $db->getErrorMsg(); } } } } return count($errors); } /** * */ function populateDatabase(& $db, $sqlfile, & $errors, $nexttask='mainconfig') { if( !($buffer = file_get_contents($sqlfile)) ) { return -1; } $queries = JInstallationHelper::splitSql($buffer); foreach ($queries as $query) { $query = trim($query); if ($query != '' && $query {0} != '#') { $db->setQuery($query); //echo $query .'<br />'; $db->query() or die($db->getErrorMsg()); JInstallationHelper::getDBErrors($errors, $db ); } } return count($errors); } /** * @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); } /** * Calculates the file/dir permissions mask */ function getFilePerms($input, $type = 'file') { $perms = ''; if (JArrayHelper::getValue($input, $type.'PermsMode', 0)) { $action = ($type == 'dir') ? 'Search' : 'Execute'; $perms = '0'. (JArrayHelper::getValue($input, $type.'PermsUserRead', 0) * 4 + JArrayHelper::getValue($input, $type.'PermsUserWrite', 0) * 2 + JArrayHelper::getValue($input, $type.'PermsUser'.$action, 0)). (JArrayHelper::getValue($input, $type.'PermsGroupRead', 0) * 4 + JArrayHelper::getValue($input, $type.'PermsGroupWrite', 0) * 2 + JArrayHelper::getValue($input, $type.'PermsGroup'.$action, 0)). (JArrayHelper::getValue($input, $type.'PermsWorldRead', 0) * 4 + JArrayHelper::getValue($input, $type.'PermsWorldWrite', 0) * 2 + JArrayHelper::getValue($input, $type.'PermsWorld'.$action, 0)); } return $perms; } /** * Creates the admin user */ function createAdminUser(& $vars) { $DBtype = JArrayHelper::getValue($vars, 'DBtype', 'mysql'); $DBhostname = JArrayHelper::getValue($vars, 'DBhostname', ''); $DBuserName = JArrayHelper::getValue($vars, 'DBuserName', ''); $DBpassword = JArrayHelper::getValue($vars, 'DBpassword', ''); $DBname = JArrayHelper::getValue($vars, 'DBname', ''); $DBPrefix = JArrayHelper::getValue($vars, 'DBPrefix', ''); $adminPassword = JArrayHelper::getValue($vars, 'adminPassword', ''); $adminEmail = JArrayHelper::getValue($vars, 'adminEmail', ''); jimport('joomla.user.helper'); // Create random salt/password for the admin user $salt = JUserHelper::genRandomPassword(32); $crypt = JUserHelper::getCryptedPassword($adminPassword, $salt); $cryptpass = $crypt.':'.$salt; $vars['adminLogin'] = 'admin'; $db = & JInstallationHelper::getDBO($DBtype, $DBhostname, $DBuserName, $DBpassword, $DBname, $DBPrefix); // create the admin user $installdate = date('Y-m-d H:i:s'); $nullDate = $db->getNullDate(); $query = "INSERT INTO #__users VALUES (62, 'Administrator', 'admin', ".$db->Quote($adminEmail).", ".$db->Quote($cryptpass).", 'Super Administrator', 0, 1, 25, '$installdate', '$nullDate', '', '')"; $db->setQuery($query); if (!$db->query()) { // is there already and existing admin in migrated data if ( $db->getErrorNum() == 1062 ) { $vars['adminLogin'] = JText::_('Admin login in migrated content was kept'); $vars['adminPassword'] = JText::_('Admin password in migrated content was kept'); return; } else { echo $db->getErrorMsg(); return; } } // add the ARO (Access Request Object) $query = "INSERT INTO #__core_acl_aro VALUES (10,'users','62',0,'Administrator',0)"; $db->setQuery($query); if (!$db->query()) { echo $db->getErrorMsg(); return; } // add the map between the ARO and the Group $query = "INSERT INTO #__core_acl_groups_aro_map VALUES (25,'',10)"; $db->setQuery($query); if (!$db->query()) { echo $db->getErrorMsg(); return; } } function & getDBO($driver, $host, $user, $password, $database, $prefix, $select = true) { static $db; if ( ! $db ) { jimport('joomla.database.database'); $options = array ( 'driver' => $driver, 'host' => $host, 'user' => $user, 'password' => $password, 'database' => $database, 'prefix' => $prefix, 'select' => $select ); $db = & JDatabase::getInstance( $options ); } return $db; } /** * Check the webserver user permissions for writing files/folders * * @static * @return boolean True if correct permissions exist * @since 1.5 */ function fsPermissionsCheck() { if(!is_writable(JPATH_ROOT.DS.'tmp')) { return false; } if(!mkdir(JPATH_ROOT.DS.'tmp'.DS.'test', 0755)) { return false; } if(!copy(JPATH_ROOT.DS.'tmp'.DS.'index.html', JPATH_ROOT.DS.'tmp'.DS.'test'.DS.'index.html')) { return false; } if(!chmod(JPATH_ROOT.DS.'tmp'.DS.'test'.DS.'index.html', 0777)) { return false; } if(!unlink(JPATH_ROOT.DS.'tmp'.DS.'test'.DS.'index.html')) { return false; } if(!rmdir(JPATH_ROOT.DS.'tmp'.DS.'test')) { return false; } return true; } /** * Find the ftp filesystem root for a given user/pass pair * * @static * @param string $user Username of the ftp user to determine root for * @param string $pass Password of the ftp user to determine root for * @return string Filesystem root for given FTP user * @since 1.5 */ function findFtpRoot($user, $pass, $host='127.0.0.1', $port='21') { jimport('joomla.client.ftp'); $ftpPaths = array(); // Connect and login to the FTP server (using binary transfer mode to be able to compare files) $ftp =& JFTP::getInstance($host, $port, array('type'=>FTP_BINARY)); if (!$ftp->isConnected()) { return JError::raiseError('31', 'NOCONNECT'); } if (!$ftp->login($user, $pass)) { return JError::raiseError('31', 'NOLOGIN'); } // Get the FTP CWD, in case it is not the FTP root $cwd = $ftp->pwd(); if ($cwd === false) { return JError::raiseError('SOME_ERROR_CODE', 'NOPWD'); } $cwd = rtrim($cwd, '/'); // Get list of folders in the CWD $ftpFolders = $ftp->listDetails(null, 'folders'); if ($ftpFolders === false || count($ftpFolders) == 0) { return JError::raiseError('SOME_ERROR_CODE', 'NODIRECTORYLISTING'); } for ($i=0, $n=count($ftpFolders); $i<$n; $i++) { $ftpFolders[$i] = $ftpFolders[$i]['name']; } // Check if Joomla! is installed at the FTP CWD $dirList = array('administrator', 'components', 'installation', 'language', 'libraries', 'plugins'); if (count(array_diff($dirList, $ftpFolders)) == 0) { $ftpPaths[] = $cwd.'/'; } // Process the list: cycle through all parts of JPATH_SITE, beginning from the end $parts = explode(DS, JPATH_SITE); $tmpPath = ''; for ($i=count($parts)-1; $i>=0; $i--) { $tmpPath = '/'.$parts[$i].$tmpPath; if (in_array($parts[$i], $ftpFolders)) { $ftpPaths[] = $cwd.$tmpPath; } } // Check all possible paths for the real Joomla! installation $checkValue = file_get_contents(JPATH_LIBRARIES.DS.'joomla'.DS.'version.php'); foreach ($ftpPaths as $tmpPath) { $filePath = rtrim($tmpPath, '/').'/libraries/joomla/version.php'; $buffer = null; @$ftp->read($filePath, $buffer); if ($buffer == $checkValue) { $ftpPath = $tmpPath; break; } } // Close the FTP connection $ftp->quit(); // Return the FTP root path if (isset($ftpPath)) { return $ftpPath; } else { return JError::raiseError('SOME_ERROR_CODE', 'Unable to autodetect the FTP root folder'); } } /** * Verify the FTP configuration values are valid * * @static * @param string $user Username of the ftp user to determine root for * @param string $pass Password of the ftp user to determine root for * @return mixed Boolean true on success or JError object on fail * @since 1.5 */ function FTPVerify($user, $pass, $root, $host='127.0.0.1', $port='21') { jimport('joomla.client.ftp'); $ftp = & JFTP::getInstance($host, $port); // Since the root path will be trimmed when it gets saved to configuration.php, we want to test with the same value as well $root = rtrim($root, '/'); // Verify connection if (!$ftp->isConnected()) { return JError::raiseWarning('31', 'NOCONNECT'); } // Verify username and password if (!$ftp->login($user, $pass)) { return JError::raiseWarning('31', 'NOLOGIN'); } // Verify PWD function if ($ftp->pwd() === false) { return JError::raiseError('SOME_ERROR_CODE', 'NOPWD'); } // Verify root path exists if (!$ftp->chdir($root)) { return JError::raiseWarning('31', 'NOROOT'); } // Verify NLST function
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -