📄 helper.php
字号:
if (($rootList = $ftp->listNames()) === false) { return JError::raiseError('SOME_ERROR_CODE', 'NONLST'); } // Verify LIST function if ($ftp->listDetails() === false) { return JError::raiseError('SOME_ERROR_CODE', 'NOLIST'); } // Verify SYST function if ($ftp->syst() === false) { return JError::raiseError('SOME_ERROR_CODE', 'NOSYST'); } // Verify valid root path, part one $checkList = array('CHANGELOG.php', 'COPYRIGHT.php', 'index.php', 'INSTALL.php', 'LICENSE.php'); if (count(array_diff($checkList, $rootList))) { return JError::raiseWarning('31', 'INVALIDROOT'); } // Verify RETR function $buffer = null; if ($ftp->read($root.'/libraries/joomla/version.php', $buffer) === false) { return JError::raiseError('SOME_ERROR_CODE', 'NORETR'); } // Verify valid root path, part two $checkValue = file_get_contents(JPATH_LIBRARIES.DS.'joomla'.DS.'version.php'); if ($buffer !== $checkValue) { return JError::raiseWarning('31', 'INVALIDROOT'); } // Verify STOR function if ($ftp->create($root.'/ftp_testfile') === false) { return JError::raiseError('SOME_ERROR_CODE', 'NOSTOR'); } // Verify DELE function if ($ftp->delete($root.'/ftp_testfile') === false) { return JError::raiseError('SOME_ERROR_CODE', 'NODELE'); } // Verify MKD function if ($ftp->mkdir($root.'/ftp_testdir') === false) { return JError::raiseError('SOME_ERROR_CODE', 'NOMKD'); } // Verify RMD function if ($ftp->delete($root.'/ftp_testdir') === false) { return JError::raiseError('SOME_ERROR_CODE', 'NORMD'); } $ftp->quit(); return true; } /** * Set default folder permissions * * @param string $path The full file path * @param string $buffer The buffer to write * @return boolean True on success * @since 1.5 */ function setDirPerms($dir, &$srv) { jimport('joomla.filesystem.path'); /* * Initialize variables */ $ftpFlag = false; $ftpRoot = $srv['ftpRoot']; /* * First we need to determine if the path is chmodable */ if (!JPath::canChmod(JPath::clean(JPATH_SITE.DS.$dir))) { $ftpFlag = true; } // Do NOT use ftp if it is not enabled if (!$srv['ftpEnable']) { $ftpFlag = false; } if ($ftpFlag == true) { // Connect the FTP client jimport('joomla.client.ftp'); $ftp = & JFTP::getInstance($srv['ftpHost'], $srv['ftpPort']); $ftp->login($srv['ftpUser'],$srv['ftpPassword']); //Translate path for the FTP account $path = JPath::clean($ftpRoot."/".$dir); /* * chmod using ftp */ if (!$ftp->chmod($path, '0755')) { $ret = false; } $ftp->quit(); $ret = true; } else { $path = JPath::clean(JPATH_SITE.DS.$dir); if (!@ chmod($path, octdec('0755'))) { $ret = false; } else { $ret = true; } } return $ret; } function findMigration( &$args ) { print_r($args); jexit(); } /** * Uploads a sql script and executes it. Script can be text file or zip/gz packed * * @static * @param array The installation variables * @param boolean true if the script is a migration script * @return string Success or error messages * @since 1.5 */ function uploadSql( &$args, $migration = false, $preconverted = false ) { global $mainframe; $archive = ''; $script = ''; /* * Check for iconv */ if ($migration && !$preconverted && !function_exists( 'iconv' ) ) { return JText::_( 'WARNICONV' ); } /* * Get the uploaded file information */ if( $migration ) { $sqlFile = JRequest::getVar('migrationFile', '', 'files', 'array'); } else { $sqlFile = JRequest::getVar('sqlFile', '', 'files', 'array'); } /* * Make sure that file uploads are enabled in php */ if (!(bool) ini_get('file_uploads')) { return JText::_('WARNINSTALLFILE'); } /* * Make sure that zlib is loaded so that the package can be unpacked */ if (!extension_loaded('zlib')) { return JText::_('WARNINSTALLZLIB'); } /* * If there is no uploaded file, we have a problem... */ if (!is_array($sqlFile) || $sqlFile['size'] < 1) { return JText::_('WARNNOFILE'); } /* * Move uploaded file */ // Set permissions for tmp dir JInstallationHelper::_chmod(JPATH_SITE.DS.'tmp', 0777); jimport('joomla.filesystem.file'); $uploaded = JFile::upload($sqlFile['tmp_name'], JPATH_SITE.DS.'tmp'.DS.$sqlFile['name']); if(!$uploaded) { return JText::_('WARNUPLOADFAILURE'); } if( !eregi('.sql$', $sqlFile['name']) ) { $archive = JPATH_SITE.DS.'tmp'.DS.$sqlFile['name']; } else { $script = JPATH_SITE.DS.'tmp'.DS.$sqlFile['name']; } // unpack archived sql files if ($archive ) { $package = JInstallationHelper::unpack( $archive, $args ); if ( $package === false ) { return JText::_('WARNUNPACK'); } $script = $package['folder'].DS.$package['script']; } $db = & JInstallationHelper::getDBO($args['DBtype'], $args['DBhostname'], $args['DBuserName'], $args['DBpassword'], $args['DBname'], $args['DBPrefix']); /* * If migration perform manipulations on script file before population */ if ( $migration ) { $script = JInstallationHelper::preMigrate($script, $args, $db); if ( $script == false ) { return JText::_( 'Script operations failed' ); } } $errors = null; $msg = ''; $result = JInstallationHelper::populateDatabase($db, $script, $errors); /* * If migration, perform post population manipulations (menu table construction) */ $migErrors = null; if ( $migration ) { $migResult = JInstallationHelper::postMigrate( $db, $migErrors, $args ); if ( $migResult != 0 ) { /* * Merge populate and migrate processing errors */ if( $result == 0 ) { $result = $migResult; $errors = $migErrors; } else { $result += $migResult; $errors = array_merge( $errors, $migErrors ); } } } /* * prepare sql error messages if returned from populate and migrate */ if (!is_null($errors)) { foreach($errors as $error) { $msg .= stripslashes( $error['msg'] ); $msg .= chr(13)."-------------".chr(13); $txt = '<textarea cols="40" rows="4" name="instDefault" readonly="readonly" >'.JText::_("Database Errors Reported").chr(13).$msg.'</textarea>'; } } else { // consider other possible errors from populate $msg = $result == 0 ? JText::_('SQL script installed successfully') : JText::_('Error installing SQL script') ; $txt = '<input size="50" value="'.$msg.'" readonly="readonly" />'; } /* * Clean up */ if ($archive) { JFile::delete( $archive ); JFolder::delete( $package['folder'] ); } else { JFile::delete( $script ); } return $txt; } /** * Unpacks a compressed script file either as zip or gz/ Assumes single file in archive * * @static * @param string $p_filename The uploaded package filename or install directory * @return unpacked filename on success, False on error * @since 1.5 */ function unpack($p_filename, &$vars) { /* * Initialize variables */ // 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); $result = JArchive::extract( $archivename, $extractdir); if ( $result === false ) { return false; } /* * return the file found in the extract folder and also folder name */ if ($handle = opendir( $extractdir )) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $script = $file; continue; } } closedir($handle); } $retval['script'] = $script; $retval['folder'] = $extractdir; return $retval; } function return_bytes($val) { $val = trim($val); $last = strtolower($val{strlen($val)-1}); switch($last) { // The 'G' modifier is available since PHP 5.1.0 case 'g': $val *= 1024; case 'm': $val *= 1024; case 'k': $val *= 1024; } return $val; } function replaceBuffer(&$buffer, $oldPrefix, $newPrefix, $srcEncoding) { $buffer = str_replace( $oldPrefix, $newPrefix, $buffer ); /* * give temp name to menu and modules tables */ $buffer = str_replace ( $newPrefix.'modules', $newPrefix.'modules_migration', $buffer ); $buffer = str_replace ( $newPrefix.'menu', $newPrefix.'menu_migration', $buffer ); /* * rename two aro_acl... field names */ $buffer = preg_replace ( '/group_id(?!.{15,25}aro_id)/', 'id', $buffer ); $buffer = preg_replace ( '/aro_id(?=.{1,6}section_value)/', 'id', $buffer ); /* * convert to utf-8 */ if(function_exists('iconv')) { $buffer = iconv( $srcEncoding, 'utf-8//TRANSLIT', $buffer ); } } function appendFile(&$buffer, $filename) { $fh = fopen($filename, 'a'); fwrite($fh, $buffer); fclose($fh); } /** * Performs pre-populate conversions on a migration script * * @static * @param string $scriptName The uploaded / unpacked script file * $param array $args The installation varibables * @return converted filename on success, False on error * @since 1.5 */ function preMigrate( $scriptName, &$args, $db ) { $maxread = 0; jimport('joomla.filesystem.file'); if(function_exists('memory_get_usage')) { $memlimit = JInstallationHelper::return_bytes(ini_get('memory_limit')); $maxread = $memlimit / 16; // Read only a eigth of our max amount of memory, we could be up to a lot by now // By default this pegs us at 0.5MB } $buffer = ''; $newPrefix = $args['DBPrefix']; /* * search and replace table prefixes */ $oldPrefix = trim( $args['oldPrefix']); $oldPrefix = rtrim( $oldPrefix, '_' ) . '_'; $srcEncoding = $args['srcEncoding']; if(!is_file($scriptName)) return false; // not a file? $newFile = dirname( $scriptName ).DS.'converted.sql'; $tfilesize = filesize($scriptName); if($maxread > 0 && $tfilesize > 0 && $maxread < $tfilesize) { $parts = ceil($tfilesize / $maxread); file_put_contents( $newFile, '' ); // cleanse the file first for($i = 0; $i < $parts; $i++) { $buffer = JFile::read($scriptName, false, $maxread, $maxread,($i * $maxread)); // Lets try and read a portion of the file JInstallationHelper::replaceBuffer($buffer, $oldPrefix, $newPrefix, $srcEncoding); JInstallationHelper::appendFile($buffer, $newFile); unset($buffer); } JFile::delete( $scriptName ); } else { /* * read script file into buffer */ if(is_file($scriptName)) { $buffer = file_get_contents( $scriptName ); } else return false; if( $buffer == false ) return false; JInstallationHelper::replaceBuffer($buffer, $oldPrefix, $newPrefix, $srcEncoding); /* * write to file */ //$newFile = dirname( $scriptName ).DS.'converted.sql'; $ret = file_put_contents( $newFile, $buffer ); unset($buffer); // Release the memory used by the buffer jimport('joomla.filesystem.file'); JFile::delete( $scriptName ); } /* * Create two empty temporary tables */ $query = 'DROP TABLE IF EXISTS '.$newPrefix.'modules_migration'; $db->setQuery( $query ); $db->query(); $query = 'DROP TABLE IF EXISTS '.$newPrefix.'menu_migration'; $db->setQuery( $query ); $db->query(); $query = 'CREATE TABLE '.$newPrefix.'modules_migration SELECT * FROM '.$newPrefix.'modules WHERE 0'; $db->setQuery( $query ); $db->query(); $query = 'CREATE TABLE '.$newPrefix.'modules_migration_menu SELECT * FROM '.$newPrefix.'modules_menu WHERE 0'; $db->setQuery( $query ); $db->Query(); $query = 'CREATE TABLE '.$newPrefix.'menu_migration SELECT * FROM '.$newPrefix.'menu WHERE 0'; $db->setQuery( $query ); $db->query(); return $newFile; } /** * Performs post-populate conversions after importing a migration script * These include constructing an appropriate menu table for core content items
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -