<?php
/**
* Entier Studio
*
* LICENSE
*
* Copyright 2006 Entier Studio team.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @package entier.studio
* @copyright Copyright (c) 2006 Entier Studio team. All rights reserved.
* @version $Id: proc.DbInstallation.php 114 2008-03-07 22:18:46Z yannromefort $
*/
//-----------------------------------------------------------------------------
// namespace
if (!defined("DefDBInstallation")) {
//-------------------------------------------------------------------------
// Define
define("DefDBInstallation", "1");
//
define(DATAERR_HOST, 1); // bad hostname string
define(DATAERR_USER, 2); // bad username string
define(DATAERR_NAME, 4); // bad database string
define(DATAERR_PASS, 8); // bad password string
//
define(DATAERR_SESS, 0); // mysql session error
define(DATAERR_OPEN, 1); // cant't open connection
define(DATAERR_LIFE, 2); // existing database
define(DATAERR_BASE, 3); // can't create database
define(DATAERR_FILE, 4); // can't open file
define(DATAERR_DATA, 5); // can't read datas
define(DATAERR_EXEC, 6); // query run error
//
define(CONFERR_FILE, 1); // can't create file
define(CONFERR_TEXT, 2); // can't write text
//
define(INSTDIR_NAME, "install");
define(SQLFILE_NAME, "entierstudio.sql");
define(CONFDIR_NAME, "config");
define(CFGFILE_NAME, "cfg.config.db.php");
//-------------------------------------------------------------------------
// Class
class DBInstallation {
//---------------------------------------------------------------------
// Attributes
/**
* host name
* @var string
* @see
*/
var $m_hostName = "";
/**
* host port number
* @var integer
* @see
*/
var $m_hostPort = 0;
/**
* user name
* @var string
* @see
*/
var $m_userName = "";
/**
* password
* @var string
* @see
*/
var $m_passWord = "";
/**
* database name
* @var string
* @see
*/
var $m_dataBase = "";
/**
* SQL Query List
* @var array
*/
var $m_querySet = array();
/**
* SQL Query Number
* @var array
*/
var $m_QueryNum = 0;
/**
* Err Query Number
* @var array
*/
var $m_QueryStr = 0;
/**
* SQL Error Number
* @var array
*/
var $m_ErrorNum = 0;
/**
* SQL Error String
* @var array
*/
var $m_ErrorStr = 0;
//---------------------------------------------------------------------
// Constructor
/**
* DBInstallation constructor.
*
* @access public
*
* @param string
* @param integer
* @param string
* @param string
* @param string
*/
function DBInstallation($hostName = "", $hostPort = 0, $userName = "", $passWord = "", $dataBase = "") {
//
$this->m_hostName = $hostName;
$this->m_hostPort = $hostPort;
$this->m_userName = $userName;
$this->m_passWord = $passWord;
$this->m_dataBase = $dataBase;
}
//---------------------------------------------------------------------
// Properties
/**
* @return string
*/
function CFGFileName() {
return (@dirname(__FILE__) . "/../" . CONFDIR_NAME . "/" . CFGFILE_NAME);
}
/**
* @return string
*/
function SQLFileName() {
return (@dirname(__FILE__) . "/../" . INSTDIR_NAME . "/" . SQLFILE_NAME);
}
/**
* @return integer
*/
function SQLFileSize() {
return (@filesize($this->SQLFileName()));
}
/**
* @return integer
*/
function queryNumber() {
return ($this->m_QueryNum);
}
/**
* @return string
*/
function queryString() {
return ($this->m_QueryStr);
}
/**
* @return integer
*/
function errorNumber() {
return ($this->m_ErrorNum);
}
/**
* @return integer
*/
function errorString() {
return ($this->m_ErrorStr);
}
//---------------------------------------------------------------------
// Methods
/**
* sql file parsing and commands execution
* code found in mediawiki database scriptS
* @param string
* @param resource
* @access private
* @return boolean
*/
function sourceFile($filename, $database) {
//
@set_time_limit(360);
//
$fp = fopen($filename, 'r');
if (false === $fp) {
$this->m_ErrorNum = DATAERR_FILE;
//
return (false);
}
$cmd = "";
$done = false;
$count = 0;
while (!feof($fp)) {
$line = trim(fgets($fp));
$sl = strlen($line) -1;
if ($sl < 0) {
continue;
}
if ('-' == $line{0} && '-' == $line{1}) {
continue;
}
if (';' == $line{$sl} && ($sl < 2 || ';' != $line{$sl-1})) {
$done = true;
$line = substr($line, 0, $sl);
}
if ('' != $cmd) {
$cmd.= ' ';
}
$cmd.= "$line\n";
if ($done) {
$cmd = str_replace(';;', ";", $cmd);
$res = @mysql_query($cmd, $database);
if (false === $res) {
//
$this->m_ErrorNum = DATAERR_EXEC;
$this->m_ErrorStr = @mysql_error($database);
$this->m_QueryStr = $queryStr;
fclose($fp);
return (false);
}
$cmd = '';
$done = false;
$count++;
if (is_resource($res))
@mysql_free_result($res);
}
}
fclose($fp);
return ($count > 0);
}
/**
* write db configuration parameters in include/cfg.config.db
* @access public
* @return boolean
*/
function createConfigFile() {
//
$bresult = true;
//
$textBuff = "<?php\n";
$textBuff.= "\t//\n";
$textBuff.= ("\tdefine( \"EN_SERVER_CODE\", \"" . md5(uniqid("")) . "\" );\n");
$textBuff.= "\t//\n";
$textBuff.= ("\tdefine( \"DB_SERVER_HOST\", \"" . $this->m_hostName . "\" );\n");
$textBuff.= ("\tdefine( \"DB_SERVER_PORT\", \"" . $this->m_hostPort . "\" );\n");
$textBuff.= ("\tdefine( \"DB_SERVER_USER\", \"" . $this->m_userName . "\" );\n");
$textBuff.= ("\tdefine( \"DB_SERVER_PASS\", \"" . $this->m_passWord . "\" );\n");
$textBuff.= ("\tdefine( \"DB_ENTIER_BASE\", \"" . $this->m_dataBase . "\" );\n");
$textBuff.= "?>";
//
$fileopId = @fopen($this->CFGFileName() , "w");
if (false === $fileopId) {
$this->m_ErrorNum = CONFERR_FILE;
//
return (false);
}
//
if (@fwrite($fileopId, $textBuff, strlen($textBuff)) == false) {
$this->m_ErrorNum = CONFERR_TEXT;
//
$bresult = false;
}
//
@fclose($fileopId);
//
return ($bresult);
}
/**
*
* @access public
* @return boolean
*/
function createDatabase() {
//
$bresult = true;
$handleId = NULL;
$resultId = NULL;
$fileopId = NULL;
$textBuff = "";
//
$handleId = @mysql_pconnect($this->m_hostName, $this->m_userName, $this->m_passWord);
if (false == $handleId) {
$this->m_ErrorNum = DATAERR_OPEN;
//
return (false);
}
//
if (@mysql_select_db($this->m_dataBase) == true) {
$this->m_ErrorNum = DATAERR_LIFE;
//
$bresult = false;
} else {
//
$resultId = @mysql_query("CREATE DATABASE " . $this->m_dataBase);
if (false === $resultId) {
$this->m_ErrorNum = DATAERR_BASE;
//
$bresult = false;
} else {
//
if (@mysql_select_db($this->m_dataBase) == false) {
$this->m_ErrorNum = DATAERR_SESS;
//
$bresult = false;
} else {
$bresult = $this->sourceFile($this->SQLFileName() , $handleId);
}
}
}
//
@mysql_close($handleId);
//
return ($bresult);
}
/**
*
* @access public
* @return boolean
*/
function checkParameters() {
//
if (empty($this->m_hostName) || empty($this->m_userName) || empty($this->m_dataBase)) {
//
if (empty($this->m_hostName)) $this->m_ErrorNum+= DATAERR_HOST;
if (empty($this->m_userName)) $this->m_ErrorNum+= DATAERR_USER;
if (empty($this->m_dataBase)) $this->m_ErrorNum+= DATAERR_NAME;
//
return (false);
}
//
return (true);
}
};
// Class
//-------------------------------------------------------------------------
}
// namespace
//-----------------------------------------------------------------------------
?>