View of /branches/mambo5/app/controllers/installation_controller.php
Parent Directory
|
Revision Log
Revision 2 -
(download)
(annotate)
Fri Dec 3 04:30:31 2010 UTC (2 years, 5 months ago) by enjoyman
File size: 43376 byte(s)
Fri Dec 3 04:30:31 2010 UTC (2 years, 5 months ago) by enjoyman
File size: 43376 byte(s)
first commit mambo5 with cakephp1.3.6.
<?php /** * @package Mambo * @author Mambo Foundation Inc see README.php * @copyright (C) 2000 - 2009 Mambo Foundation Inc. * See COPYRIGHT.php for copyright notices and details. * @license GNU/GPL Version 2, see http://www.opensource.org/licenses/gpl-2.0.php * * Redistributions of files must retain the above copyright notice. * * Mambo is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; version 2 of the License. */ /** * Used to perform the database portable installation of Mambo * */ class InstallationController extends AppController { public $name = 'Installation'; public $uses = array(); //No model //public $uses = null; works too public $helpers = array('Html', 'Form'); public $layout = 'installation'; /** @private string Internal variable to hold the db resource */ private $db=''; /** @private string Internal variable to hold the db platform type */ private $dbPlatform=''; /** @private string Internal variable to hold the db hostname (ex) localhost */ private $dbHostname=''; /** @private string Internal variable to hold the db username */ private $dbUsername=''; /** @private string Internal variable to hold the db password */ private $dbPassword=''; /** @private string Internal variable to hold the name of the db */ private $dbName=''; /** @private string Internal variable to hold the table prefix */ private $dbPrefix=''; /** @private string Internal variable to hold the table being acted on */ private $table=''; /** @private string Internal variable to hold the SQL statement to execute */ private $sql=''; /** @private string Internal variable to hold the data dictionary */ private $dict=''; /** * Displays the initial pre-installation page * */ function index() { App::import('Vendor', 'phpgettext'.DS.'phpgettext.class'); } /** * Displays and requires user to agree to license before installing * */ function license() { App::import('Vendor', 'phpgettext'.DS.'phpgettext.class'); if (!empty($this->data)) { //Did they submit the first form with permission issues unresolved? if (@!file_exists(CONFIGS.'database.php') || @!is_writable( CONFIGS.'database.php' ) || @!file_exists(CONFIGS.'configuration.xml') || @!is_writable( CONFIGS.'configuration.xml' ) || @!file_exists(VENDORS.'mambo'.DS.'store') || @!is_writable( VENDORS.'mambo'.DS.'store' )) { $setup_error = __('You still have unresolved file or folder permission issues. You must correct these before moving on!'); $this->setError($setup_error, 'index'); } } } /** * Displays database configuration form * */ function install1() { App::import('Vendor', 'phpgettext'.DS.'phpgettext.class'); //Check to see if valid form data has been submitted if (!empty($this->data)) { $cln_agreecheck = (int) $this->data['installation']['agreecheck']; if ($cln_agreecheck!=1) { $setup_error = __('You must read and accept the license to continue installation!'); $this->setError($setup_error, 'license'); } } } /** * Creates the Mambo database structure, loads the default data, loads sample data & backs up old tables when requested * */ function install2() { vendor('phpgettext'.DS.'phpgettext.class'); //Check to see if valid form data has been submitted or we've been redirected due to an error if (!empty($this->data)) { //Create a new Sanitize object: uses('sanitize'); $mrClean = new Sanitize(); $allowedChars = array('@', '.', '-', '_', '/', '\\', ':'); //Clean and add certain data to the array for saving (not all fields are updated) $cln_dbHostname = $mrClean->paranoid($this->data['installation']['dbHostname'], $allowedChars); $cln_dbPlatform = $mrClean->paranoid($this->data['installation']['dbPlatform']); $cln_dbUsername = $mrClean->paranoid($this->data['installation']['dbUsername']); $cln_dbPassword = $this->data['installation']['dbPassword']; $cln_dbVerifyPassword = $this->data['installation']['dbVerifyPassword']; $cln_dbName = $mrClean->paranoid($this->data['installation']['dbName'], $allowedChars); $cln_dbPrefix = $mrClean->paranoid($this->data['installation']['dbPrefix'], $allowedChars); $cln_dbBackup = (int) $this->data['installation']['dbBackup']; $cln_dbSample = (int) $this->data['installation']['dbSample']; $cln_dbPath = $mrClean->paranoid($this->data['installation']['dbPath'], $allowedChars); //Veryify the dbPath has the proper OS specific trailing slash before adding on the dbName if (substr($cln_dbPath, -1)!==DS) { $cln_dbPath = $cln_dbPath.DS; } /* Now run through the form validations. In most cases it would be better to utilize cake's invalidate function, but this appears to require the use of a model which we do not have before the database is created. */ $form_check = ''; if (empty($cln_dbHostname)) { $form_check = 'dbHostname'; } else if (empty($cln_dbPlatform)) { $form_check = 'dbPlatform'; } else if (empty($cln_dbPath) && $cln_dbPlatform=='sqlite') { $form_check = 'dbPath'; } else if (empty($cln_dbUsername) && $cln_dbPlatform!='sqlite') { $form_check = 'dbUsername'; } else if (empty($cln_dbPassword) && $cln_dbPlatform!='sqlite') { $form_check = 'dbPassword'; } else if (($cln_dbPassword !== $cln_dbVerifyPassword) && $cln_dbPlatform!='sqlite') { $form_check = 'dbPasswordMismatch'; } else if (empty($cln_dbName)) { $form_check = 'dbName'; } else if (empty($cln_dbPrefix)) { $form_check = 'dbPrefix'; } else if ($cln_dbBackup==1 && $cln_dbPlatform=='sqlite') { $this->setError('Table backups are not supported for SQLite2', 'install2'); } /* If errors store form data and redirect back to form. Clear passwords first so they don't end up in the session data. */ if ($form_check!='') { $this->data['installation']['dbPassword']=''; $this->data['installation']['dbVerifyPassword']=''; $this->Session->Write('form_data', $this->data['installation']); $this->Session->Write('form_check', $form_check); $this->redirect('/installation/install1'); exit; } //Initialize instance variables $this->dbPlatform = $cln_dbPlatform; $this->dbHostname = $cln_dbHostname; $this->dbUsername = $cln_dbUsername; $this->dbPassword = $cln_dbPassword; $this->dbName = $cln_dbName; $this->dbPath = $cln_dbPath; $this->dbPrefix = $cln_dbPrefix; $this->dbBackup = $cln_dbBackup; $this->dbSample = $cln_dbSample; //Get the database connection $db = $this->setupConnection(); $this->db = $db; //Serialize the form data (needed later in install4) $s = serialize($this->data['installation']); $fp = fopen(VENDORS.DS.'mambo'.DS.'store', "w"); fwrite($fp, $s); fclose($fp); //Configuration information: Define the schema filename, RDBMS platform, and database connection information here. $schemaFile = VENDORS.DS.'mambo'.DS.'mambo_structure.xml'; //Use the database connection to create a new adoSchema object. $schema = new adoSchema($this->db); //Set the database table prefix $schema->setPrefix($this->dbPrefix); //Create a data dictionary object $this->dict = NewDataDictionary($this->db); //If previous tables exist then backup (if requested) and drop for fresh install /* Builds an array of table names to use in the drop table sequence. Note: Could use 'show tables' in MySQL to build the array, but that is not portable across other dbs. */ $tables = $this->db->MetaTables('TABLES'); $backupPrefix = 'old_'; if ($tables) { foreach ($tables as $table) { //Check for the existance of tables with the same prefix if (strpos($table, $this->dbPrefix) === 0) { //Check to see if the user requested a backup if ($this->dbBackup==1) { //If they requested a backup then replace the org table prefix with old_ $backupTable = str_replace($this->dbPrefix, $backupPrefix, $table); //If a prior backup table exists with the same name then drop it before the rename $this->dropTable($backupTable); //Drop db sequence $this->dropSequence($table); //Perform the actual table rename $this->renameTable($table, $backupTable); } else { $this->dropTable($table); //Drop db sequence $this->dropSequence($table); } } } //end foreach $this->dropIndexes(); //Drop indexes as needed } //end if //Build DML SQL statements $schema->ParseSchema($schemaFile); //Execute DML SQL statements $result = $schema->ExecuteSchema(); if ($result) { //Load default data $this->loadSQLData('default'); if ($this->dbSample) { //Load sample data if requested $this->loadSQLData('sample'); } } //Write out the database configuration file in CakePHP's preferred format $this->writeDatabaseConfigFile(); } } /** * Collects the users specified Mambo site name * */ function install3() { vendor('phpgettext'.DS.'phpgettext.class'); //Check to see if valid form data has been submitted if (!empty($this->data)) { //Create a new Sanitize object: uses('sanitize'); $mrClean = new Sanitize(); $allowedChars = array('@', '.', '-', '_', '/', '\\', ':', '\'', ' '); //Clean and add certain data to the array for saving (not all fields are updated) $cln_Sitename = $mrClean->paranoid($this->data['installation']['Sitename'], $allowedChars); /* Now run through the form validations. In most cases it would be better to utilize cake's invalidate function, but this appears to require the use of a model which we do not have before the database is created. */ $form_check = ''; if (empty($cln_Sitename)) { $form_check = 'Sitename'; } /* If errors store form data and redirect back to form. Clear passwords first so they don't end up in the session data. */ if ($form_check!='') { $this->Session->Write('form_data', $this->data['installation']); $this->Session->Write('form_check', $form_check); $this->redirect('/installation/install2'); exit; } //Build the siteURL $root = $_SERVER['SERVER_NAME'].($_SERVER['SERVER_PORT']!=80?':'.$_SERVER['SERVER_PORT']:'').$_SERVER['PHP_SELF']; $root = str_replace('/app/webroot/index.php','',$root); $siteURL = "http://".$root; //Add some info to the session before moving to step 3 $this->Session->Write('Sitename', $cln_Sitename); $this->Session->Write('siteURL', $siteURL); $this->Session->Write('absolutePath', APP); } } /** * Collects the Mambo admin information, lands the Mambo configuration file, and inserts the admin user * */ function install4() { vendor('phpgettext'.DS.'phpgettext.class'); //Check to see if valid form data has been submitted if (!empty($this->data)) { //Create a new Sanitize object: uses('sanitize'); $mrClean = new Sanitize(); $allowedChars = array('@', '.', '-', '_', '/', '\\', ':', '\'', ' '); //Clean and add certain data to the array for saving (not all fields are updated) $cln_siteUrl = $mrClean->paranoid($this->data['installation']['siteUrl'], $allowedChars); $cln_absolutePath = $mrClean->paranoid($this->data['installation']['absolutePath'], $allowedChars); $cln_adminEmail = $mrClean->paranoid($this->data['installation']['adminEmail'], $allowedChars); $cln_adminPassword = $this->data['installation']['adminPassword']; $cln_adminVerifyPassword = $this->data['installation']['adminVerifyPassword']; //Fix for Windows $cln_absolutePath = str_replace("\\","/", $cln_absolutePath); $cln_absolutePath = str_replace("//","/", $cln_absolutePath); /* Now run through the form validations. In most cases it would be better to utilize cake's invalidate function, but this appears to require the use of a model which we do not have before the database is created. */ $form_check = ''; if (empty($cln_siteUrl)) { $form_check = 'siteUrl'; } else if (empty($cln_absolutePath)) { $form_check = 'absolutePath'; } else if (empty($cln_adminEmail) || validEmail($cln_adminEmail)===false) { $form_check = 'adminEmail'; } else if (empty($cln_adminPassword)) { $form_check = 'adminPassword'; } else if ($cln_adminPassword!==$cln_adminVerifyPassword) { $form_check = 'adminVerifyPassword'; } /* If errors store form data and redirect back to form. Clear passwords first so they don't end up in the session data. */ if ($form_check!='') { //Remove password so it does not end up in the session data $this->data['installation']['adminPassword']=''; $this->Session->Write('form_data', $this->data['installation']); $this->Session->Write('form_check', $form_check); $this->redirect('/installation/install3'); exit; } if ($this->Session->check('Sitename')) { $Sitename = $this->Session->read('Sitename'); } //Need to discuss with Andres and get his feedback (so this is just temp) $nameLang = 'English'; $lang = 'en'; //end temp //Output the new CakePHP database.php config file $config_file = CONFIGS.'configuration.xml'; if(($handle = @fopen($config_file, 'w'))===false){ $errorMsg = sprintf(__('Failed to open the %s file for writing! '), $config_file); $errorMsg .= __('Please make sure the web server user has write permissions on this file until the installation is complete'); $this->setError($errorMsg, 'install3'); } else { $configText = "<?xml version='1.0' standalone='yes'?>\n"; $configText .= "<options>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_offline</name>\n"; $configText .= " <setting>0</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_lang</name>\n"; $configText .= " <setting>{$nameLang}</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_absolute_path</name>\n"; $configText .= " <setting>{$cln_absolutePath}</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_live_site</name>\n"; $configText .= " <setting>{$cln_siteUrl}</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_sitename</name>\n"; $configText .= " <setting>{$Sitename}</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_shownoauth</name>\n"; $configText .= " <setting>0</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_useractivation</name>\n"; $configText .= " <setting>1</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_usecaptcha</name>\n"; $configText .= " <setting>0</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_uniquemail</name>\n"; $configText .= " <setting>1</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_offline_message</name>\n"; $configText .= " <setting>".__('This site is down for maintenance.<br /> Please check back again soon.')."</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_error_message</name>\n"; $configText .= " <setting>".__('This site is temporarily unavailable.<br /> Please notify the System Administrator')."</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_debug</name>\n"; $configText .= " <setting>0</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_lifetime</name>\n"; $configText .= " <setting>900</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_MetaDesc</name>\n"; $configText .= " <setting>".__('This site uses Mambo - the free, open source content management system')."</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_MetaKeys</name>\n"; $configText .= " <setting>mambo user, Mambo</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_MetaTitle</name>\n"; $configText .= " <setting>1</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_MetaAuthor</name>\n"; $configText .= " <setting>1</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_locale_debug</name>\n"; $configText .= " <setting>0</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_locale_use_gettext</name>\n"; $configText .= " <setting>0</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_locale</name>\n"; $configText .= " <setting>{$lang}</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_offset</name>\n"; $configText .= " <setting>0</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_hideAuthor</name>\n"; $configText .= " <setting>0</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_hideCreateDate</name>\n"; $configText .= " <setting>0</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_hideModifyDate</name>\n"; $configText .= " <setting>0</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_hidePdf</name>\n"; $configText .= " <setting>".intval( !is_writable( "{$cln_absolutePath}/media/" ) )."</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_hidePrint</name>\n"; $configText .= " <setting>0</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_hideEmail</name>\n"; $configText .= " <setting>0</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_enable_log_items</name>\n"; $configText .= " <setting>0</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_enable_log_searches</name>\n"; $configText .= " <setting>0</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_enable_stats</name>\n"; $configText .= " <setting>0</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_sef</name>\n"; $configText .= " <setting>0</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_vote</name>\n"; $configText .= " <setting>0</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_gzip</name>\n"; $configText .= " <setting>0</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_multipage_toc</name>\n"; $configText .= " <setting>1</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_allowUserRegistration</name>\n"; $configText .= " <setting>1</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_registration_disclaimer</name>\n"; $configText .= " <setting>0</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_registration_disclaimer_message</name>\n"; $configText .= " <setting>".__('Put your disclaimer here...')."</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_link_titles</name>\n"; $configText .= " <setting>0</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_error_reporting</name>\n"; $configText .= " <setting>-1</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_list_limit</name>\n"; $configText .= " <setting>50</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_mailer</name>\n"; $configText .= " <setting>mail</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_mailfrom</name>\n"; $configText .= " <setting>{$cln_adminEmail}</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_fromname</name>\n"; $configText .= " <setting>{$Sitename}</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_sendmail</name>\n"; $configText .= " <setting>/usr/sbin/sendmail</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_smtpauth</name>\n"; $configText .= " <setting>0</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_smtpuser</name>\n"; $configText .= " <setting></setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_smtppass</name>\n"; $configText .= " <setting></setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_smtphost</name>\n"; $configText .= " <setting>localhost</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_back_button</name>\n"; $configText .= " <setting>0</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_item_navigation</name>\n"; $configText .= " <setting>0</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_pagetitles</name>\n"; $configText .= " <setting>1</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_readmore</name>\n"; $configText .= " <setting>1</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_hits</name>\n"; $configText .= " <setting>1</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_icons</name>\n"; $configText .= " <setting>1</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_favicon</name>\n"; $configText .= " <setting>favicon.ico</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_helpurl</name>\n"; $configText .= " <setting>http://docs.mambo-foundation.org</setting>\n"; $configText .= " </option>\n"; $configText .= " <option>\n"; $configText .= " <name>mosConfig_mbf_content</name>\n"; $configText .= " <setting>0</setting>\n"; $configText .= " </option>\n"; $configText .= "</options>"; if ($handle) { fwrite($handle, $configText); fclose($handle); } //Unserialize the step 1 data for to insert the default admin user $filename = VENDORS.DS.'mambo'.DS.'store'; $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); fclose($handle); $unData = unserialize($contents); //Refresh the database connection for the insert $this->dbHostname = $unData['dbHostname']; $this->dbPlatform = $unData['dbPlatform']; $this->dbPath = $unData['dbPath']; $this->dbUsername = $unData['dbUsername']; $this->dbPassword = $unData['dbPassword']; $this->dbName = $unData['dbName']; $this->dbPrefix = $unData['dbPrefix']; $db = $this->setupConnection(); $this->db = $db; //Using SHA1 for cross platform password encryption. Not perfectly secure, but a longer hash than provided by MD5. $cryptpass=sha1($cln_adminPassword); //Build cross-platform timestamp $currentDate = date("Y-m-d"); $usersTable = $this->dbPrefix.'users'; $adminUserSQL = "INSERT INTO $usersTable (id, name, username, email, password, usertype, block, sendEmail, group_id, registerDate, lastvisitDate) VALUES (1, 'Administrator', 'admin', '$cln_adminEmail', '$cryptpass', 'Super Administrator', 0, 1, 25, '$currentDate', '$currentDate')"; //Insert the admin user $this->executeInsert($adminUserSQL, 'install3'); } } } /** * Presents/Sends survey installation information * */ function survey() { vendor('phpgettext'.DS.'phpgettext.class'); //Check to see if valid form data has been submitted if (!empty($this->data)) { //Create a new Sanitize object: uses('sanitize'); $mrClean = new Sanitize(); $allowedChars = array('@', '.', '-', '_', '/', '\\', ':', ',', ' '); //Clean and add certain data to the array $cln_name = $mrClean->paranoid($this->data['installation']['name']); $cln_email = $mrClean->paranoid($this->data['installation']['email'], $allowedChars); $cln_company = $mrClean->paranoid($this->data['installation']['company'], $allowedChars); $cln_category = $mrClean->paranoid($this->data['installation']['category']); $cln_comments = $mrClean->paranoid($this->data['installation']['comments'], $allowedChars); /* Now run through the form validations. In most cases it would be better to utilize cake's invalidate function, but this appears to require the use of a model which we do not have before the database is created. */ $form_check = ''; if (empty($cln_name)) { $form_check = 'name'; } else if (empty($cln_email) || validEmail($cln_email)===false) { $form_check = 'email'; } else if (empty($cln_category) && empty($cln_comments)) { $form_check = 'needmoreinfo'; } /* If errors store form data and redirect back to form. Clear passwords first so they don't end up in the session data. */ if ($form_check!='') { $this->Session->Write('form_data', $this->data['installation']); $this->Session->Write('form_check', $form_check); $this->redirect('/installation/install4'); exit; } //Build and send survey email $to = 'feedback@mambo-foundation.org'; $subject = 'Mambo Installation - User Comments'; $message = "$cln_name left some comments on the installation survey. "; $message .= "Here are the comments:\n"; $message .= "\n"; $message .= "Name: $cln_name\n"; $message .= "Email: $cln_email\n"; $message .= "Category: $cln_category\n"; $message .= "Company: $cln_company\n"; $message .= "Comments: $cln_comments"; $headers = "From: $cln_email" . "\r\n" . "Reply-To: $cln_email" . "\r\n" . 'X-Mailer: PHP/' . phpversion(); //No error checking on the send since this is just a nice to have mail($to, $subject, $message, $headers); $this->redirect('/main/index'); exit; } } /** * General error handling function to write form data to the session and redirect to the setup form * when errors occur. * * @param string $errorMsg * @param string $redirectPage * */ private function setError($errorMsg, $redirectPage) { $this->data['installation']['dbPassword']=''; $this->data['installation']['dbVerifyPassword']=''; $this->Session->Write('form_data', $this->data['installation']); $this->Session->Write('setup_error', $errorMsg); $this->redirect("/installation/$redirectPage"); exit; } /** * Writes out a cakePHP formatted database file based on the given installation parameters * */ private function writeDatabaseConfigFile() { $dbPlatform = $this->dbPlatform; $dbHostname = $this->dbHostname; $dbUsername = $this->dbUsername; $dbPassword = $this->dbPassword; $dbName = $this->dbName; $dbPrefix = $this->dbPrefix; $default = '$default'; if ($this->dbPlatform=='mysql' || $this->dbPlatform=='postgres') { $dbConfigText = "<?php\n"; $dbConfigText.= "class DATABASE_CONFIG {\n"; $dbConfigText.= "\n"; $dbConfigText.= " var $default = array(\n"; $dbConfigText.= " 'driver' => '$dbPlatform',\n"; $dbConfigText.= " 'persistent' => false,\n"; $dbConfigText.= " 'host' => '$dbHostname',\n"; $dbConfigText.= " 'port' => '',\n"; $dbConfigText.= " 'login' => '$dbUsername',\n"; $dbConfigText.= " 'password' => '$dbPassword',\n"; $dbConfigText.= " 'database' => '$dbName',\n"; $dbConfigText.= " 'prefix' => '$dbPrefix'\n"; $dbConfigText.= " );\n"; $dbConfigText.= "\n"; $dbConfigText.= "}\n"; $dbConfigText.= "?>"; } else if ($this->dbPlatform=='sqlite') { //Build full database variable (i.e.) path/dbname $database = $this->dbPath.$this->dbName; //No username or password needed & the database name is the full path $dbConfigText = "<?php\n"; $dbConfigText.= "class DATABASE_CONFIG {\n"; $dbConfigText.= "\n"; $dbConfigText.= " var $default = array(\n"; $dbConfigText.= " 'driver' => 'sqlite',\n"; $dbConfigText.= " 'persistent' => false,\n"; $dbConfigText.= " 'host' => '$dbHostname',\n"; $dbConfigText.= " 'port' => '',\n"; $dbConfigText.= " 'database' => '$database',\n"; $dbConfigText.= " 'prefix' => '$dbPrefix'\n"; $dbConfigText.= " );\n"; $dbConfigText.= "\n"; $dbConfigText.= "}\n"; $dbConfigText.= "?>"; } else { $this->setError('Invalid Database Platform', 'install1'); } //Output the new CakePHP database.php config file $database_file = CONFIGS.'database.php'; if(($handle = @fopen($database_file, 'w'))===false){ $errorMsg = sprintf(__('Failed to open the %s file for writing! '), $database_file); $errorMsg .= __('Please make sure the web server user has write permissions on this file until the installation is complete'); $this->setError($errorMsg, 'install1'); } if ($handle) { fwrite($handle, $dbConfigText); fclose($handle); } } /** * Takes an incoming SQL statement and adds the user specificed table prefix * * @param string $sql * @return prepared_sql */ private function setPrefix($sql) { $generic_prefix_pattern = '/#__/'; $prepared_sql = preg_replace($generic_prefix_pattern, $this->dbPrefix, $sql); return $prepared_sql; } /** * Used to load data from external SQL files * * @param string $type */ private function loadSQLData($type) { //Include the prepared SQL statements switch ($type) { case 'default': $data_file = VENDORS.'mambo'.DS.'default_data.sql'; break; case 'sample': $data_file = VENDORS.'mambo'.DS.'sample_data.sql'; break; default: echo __('Invalid type'); exit; } if (($handle = @fopen($data_file, 'r'))===false) { $errorMsg = sprintf(__('Failed to open the %s file!'), $data_file); $this->setError($errorMsg, 'install1'); } if ($handle) { $sql_statements = array(); $sql_statements = explode(PHP_EOL, fread($handle, filesize($data_file))); fclose($handle); } //Build and execute the dynamic SQL $ac = count($sql_statements); for ($i = 1; $i < $ac; $i++) { /* Call to set MS SQL Server specific IDENTITY_INSERT value. In order to insert a specific value into the PK / AUTONUM columns. MS SQL Server requires fully expanded INSERT statement with columns named and the IDENTITY_INSERT On/Off attribute. Can only set for one table at a time. */ if (substr($sql_statements[$i],0,6)=="INSERT" || (substr($sql_statements[$i],0,3)=="SET" && $this->dbPlatform=='mssql')) { $prepared_sql = $this->setPrefix($sql_statements[$i]); $this->executeInsert($prepared_sql, 'install1'); } } } /** * Used to drop indexes after a table rename on platforms that need it * */ private function dropIndexes() { //Include the prepared SQL statements if ($this->dbPlatform=='postgres') { $drop_index = VENDORS.DS.'mambo'.DS.'drop_index.sql';; if (($handle = @fopen($drop_index, 'r'))===false) { $errorMsg = sprintf(__('Failed to open the %s file!'), $drop_index); $this->setError($errorMsg, 'install1'); } if ($handle) { $sql_statements = array(); $sql_statements = explode(PHP_EOL, fread($handle, filesize($drop_index))); fclose($handle); } //Build and execute the dynamic SQL $count = count($sql_statements); for ($i = 1; $i < $count; $i++) { /* Call to set MS SQL Server specific IDENTITY_INSERT value. In order to insert a specific value into the PK / AUTONUM columns. MS SQL Server requires fully expanded INSERT statement with columns named and the IDENTITY_INSERT On/Off attribute. Can only set for one table at a time. */ if (substr($sql_statements[$i],0,4)=="DROP") { $prepared_sql = $this->setPrefix($sql_statements[$i]); $this->executeInsert($prepared_sql, 'install1'); } } } return; } /** * Generic function used to execute and error check incoming SQL statments * * @param string $sql */ private function executeInsert($sql, $redirectPage) { if ($this->db->Execute($sql)===false) { $errors = $this->db->ErrorMsg().": Erroring SQL was: $sql"; $this->setError($errors, $redirectPage); } } /** * Drops requested database tables * * @param string $table */ private function dropTable($table) { //Build the rename table SQL statement $sql = $this->dict->DropTableSQL($table); if ($this->dict->executeSQLArray($sql)===false) { $errors = $this->db->ErrorMsg().": Erroring SQL was: $sql"; $this->setError($errors, 'install1'); } } /** * If the select db platform is Oracle or Postgres we need to drop the table sequences as well. * They would drop if a drop table was done, but not with a rename so we need to drop manually before * we run the new CREATE TABLE. * * @param string $table */ private function dropSequence($table) { //Note: In reality only tables with a autoincrement column have a sequence if ($this->dbPlatform=='oracle') { $sequence = 'seq_' . $table; $sql="DROP SEQUENCE $sequence"; } else if ($this->dbPlatform=='postgres') { $sequence = $table.'_id_seq'; $sql="DROP SEQUENCE IF EXISTS $sequence CASCADE"; } else { return; } //Execute the sql if ($this->db->Execute($sql)===false) { $errors = $this->db->ErrorMsg().": Erroring SQL was: $sql"; $this->setError($errors, 'install1'); } } /** * Renames requested database tables * * @param string $table * @param string $newname */ private function renameTable($table, $newname) { //Build the rename table SQL statement $sql = $this->dict->RenameTableSQL($table,$newname); if ($this->dict->executeSQLArray($sql)===false) { $errors = $this->db->ErrorMsg().": Erroring SQL was: $sql"; $this->setError($errors, 'install1'); } } /** * Creates the ADOdb database connection to the selected db platform * * @return db */ private function setupConnection() { /* Valid ADOdb platform options include: mysql, postgres, sqlite, oracle, firebird, db2, mssql Note: The setup process has been built with all of the platforms listed above in mind. Some testing has occurred in each and the setup code can actually be used to install on all except for db2 without major modifications. However, at the moment Mambo itself is only setup through CakePHP to work with Mysql 4/5, postgres, & sqlite3. More should be added in time... */ //Include the ADOdb library files vendor('adodb'.DS.'adodb.inc'); vendor('adodb'.DS.'adodb-xmlschema.inc'); /*///////////////////////////////////////////////////////////////////// MySQL / PostgreSQL connection Parameters *////////////////////////////////////////////////////////////////////// if ($this->dbPlatform=='mysql' || $this->dbPlatform=='postgres') { $db = ADONewConnection( $this->dbPlatform ); //Debug set to 1 for verbose output $db->debug = 0; // Start by creating a normal ADODB connection. if (!@$db->Connect($this->dbHostname, $this->dbUsername, $this->dbPassword, $this->dbName, $this->dbPath)) { $this->setError($db->ErrorMsg(), 'install1'); } } /*////////////////////////////////////////////////////////////////////// SQLite2 connection Parameters */////////////////////////////////////////////////////////////////////// if ($this->dbPlatform=='sqlite') { //SQLite 3 not supported at this point $db = ADONewConnection('sqlite'); //Debug set to 1 for verbose output $db->debug = 0; //Try to connect (the db will actually be created if it does not exist and OS file permissions allow it. if (!@$db->Connect($this->dbPath.$this->dbName)) { //The normal $db->ErrorMsg() method doesn't handle SQLite errors so well. Using basic default message. $this->setError('Error creating or connecting to the SQLite database!', 'install1'); } } /*////////////////////////////////////////////////////////////////////// Oracle connection Parameters */////////////////////////////////////////////////////////////////////// //Note: Oracle support must have been compiled into PHP if ($this->dbPlatform=='oracle') { $db = ADONewConnection( 'oci8' ); //Debug set to 1 for verbose output $db->debug = 0; if (!$db->Connect($this->dbHostname, $this->dbUsername, $this->dbPassword)) { $this->setError($db->ErrorMsg(), 'install1'); } else { // Set Oracle date format for inserts $set_date_format = "ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'"; $db->Execute($set_date_format); } /************************************************************************** With oci8, you can connect in multiple ways. Note that oci8 works fine with newer versions of the Oracle, eg. 9i and 10g. a. PHP and Oracle reside on the same machine, use default SID. $db->Connect(false, 'scott', 'tiger'); b. TNS Name defined in tnsnames.ora (or ONAMES or HOSTNAMES), eg. 'myTNS' $db->PConnect(false, 'scott', 'tiger', 'myTNS'); or $db->PConnect('myTNS', 'scott', 'tiger'); c. Host Address and SID $conn->connectSID = true; $conn->Connect('192.168.0.1', 'scott', 'tiger', 'SID'); d. Host Address and Service Name $conn->Connect('192.168.0.1', 'scott', 'tiger', 'servicename'); e. Oracle connection string: $cstr = "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=$host)(PORT=$port)) (CONNECT_DATA=(SID=$sid)))"; $db->Connect($cstr, 'scott', 'tiger'); **************************************************************************/ } /*////////////////////////////////////////////////////////////////////// DB2 connection Parameters (Not yet working...) */////////////////////////////////////////////////////////////////////// if ($this->dbPlatform=='db2') { $db = ADONewConnection( $this->dbPlatform ); $dsn = "driver={IBM db2 odbc DRIVER};Database=$this->dbName;hostname=$this->dbHostname;port=50000;protocol=TCPIP;". "uid=$this->dbUsername; pwd=$this->dbPassword"; //Debug set to 1 for verbose output $db->debug = 0; if (!@$db->Connect($dsn)) { $this->setError($db->ErrorMsg(), 'install1'); } } /*////////////////////////////////////////////////////////////////////// Firebird connection Parameters (Not supported yet...) */////////////////////////////////////////////////////////////////////// //Note: ibase support must have been compiled into PHP if ($this->dbPlatform=='firebird') { $db = ADONewConnection('ibase'); //Debug set to 1 for verbose output $db->debug = 0; if (!@$db->Connect($this->dbPath,$this->dbUsername,$this->dbPassword)) { $this->setError($db->ErrorMsg(), 'install1'); } } /*////////////////////////////////////////////////////////////////////// MS SQL Server connection Parameters (tested with 2000 & 2005) */////////////////////////////////////////////////////////////////////// if ($this->dbPlatform=='mssql') { $db = ADONewConnection('odbc_mssql'); //Note: if using sql server 2005 you might need servername\instance for the Server string $dsn = "Provider=MSDASQL; Driver={SQL Server}; Server=$this->dbHostname; Database=$this->dbName;"; //Debug set to 1 for verbose output $db->debug = 0; if (!@$db->Connect($dsn,$this->dbUsername,$this->dbPassword)) { $this->setError($db->ErrorMsg(), 'install1'); } //Or if you prefer to use the mssql extension (which is limited to mssql 6.5 functionality): /* $db = ADONewConnection('mssql'); $db->debug = 0; $db->Execute('servername', 'user', 'password', 'database'); */ } return $db; } } ?>
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

