查看: 903|回复: 4
|
object-oriented web installation (欢迎修改)
[复制链接]
|
|
Version 1.0
目的:方便和简单化web application 的安装步骤
现成功测试系统 : window system, apache 2.0.58, mysql4.1 , php5x
欢迎修改的地方--------------
1) common SQL of creating table has applied, but it's not fully complete
2) setting of directory permission is not dynamic
installation.class.php
function autoSetDirPermission() {
...
}
3) creating of admin account is not completed
mysql.class.php
function createAdminAcc() {
...
}
4) only simple validation apply
新建一个installation folder,然后将以下的*.php files 放入这个folder
或这里下载 http://rapidshare.com/files/52384136/installation.rar
[ 本帖最后由 ikanyuchiew 于 31-8-2007 11:58 AM 编辑 ] |
|
|
|
|
|
|
|

楼主 |
发表于 31-8-2007 11:14 AM
|
显示全部楼层
index.php
- <html>
- <head>
- <title>web installation 1.0 - by ikanyuchiew</title>
- <script>
- </script>
- </head>
- <body>
- <?php
- include_once "installation.class.php";
- include_once "table.class.php";
- include_once "mysql.class.php";
- $installation = new Installation();
- $_TABLE = $_MYSQL = $_OPTION = array();
- if (!$installation->isInstalled()) {
- //still havent install
- if (isset($_POST['install']) && $_POST['install']==true) {
- echo "Installation is processing...<br/>";
- //Get the variables from the POST predefined variables
- $_MYSQL['host'] = $_POST['txtSqlServer'];
- $_MYSQL['user'] = $_POST['txtSqlUser'];
- $_MYSQL['pass'] = $_POST['txtSqlPass'];
- $_MYSQL['db'] = $_POST['txtSqlDb'];
- $_OPTION['isNewDb'] = $_POST['chkBoxNewDb'];
- $_OPTION['isOverWrite'] = $_POST['chkBoxOw'];
- $_OPTION['isAutoSetPerm'] = $_POST['chkBoxDirPerm'];
- //let say 2 tables i will create : table customers and table employees
- //sorry i m not strong in SQL
- $_TABLE['tblCust'] = array (
- "id" => array (
- "type"=>"int(10)",
- "collation" => "latin1_swedish_ci",
- "attributes" => "unsigned",
- "null" => 0,
- "default" => null,
- "extra" => "auto_increment"
- ),
- "name" => array (
- "type"=>"varchar(50)",
- "collation" => "latin1_swedish_ci",
- "attributes" => null,
- "null" => 0,
- "default" => "default ''",
- "extra" => null
- ),
- "primary_key" => "id"
- );
- $_TABLE['tblEmp'] = array(
- "id" => array (
- "type"=>"int(10)",
- "collation" => "latin1_swedish_ci",
- "attributes" => "signed",
- "null" => 0,
- "default" => null,
- "extra" => "auto_increment"
- ),
- "name" => array (
- "type"=>"varchar(50)",
- "collation" => "latin1_swedish_ci",
- "attributes" => null,
- "null" => 1,
- "default" => "default 'ikanyu'",
- "extra" => null
- ),
- "primary_key" => "id"
- );
- if ($_OPTION['isAutoSetPerm'])
- $installation->autoSetDirPermission();
- if ($_OPTION['isNewDb'] || $_OPTION['isOverWrite']) {
- $installation->install(new Table($_TABLE),new Mysql($_MYSQL,$_OPTION['isNewDb']),$_OPTION['isOverWrite']);
- }
- }
- } else {
- echo "Delete the installation file for security purpose<br/>";
- die;
- }
- ?>
- <form name='frmInstall' method='post' action="<?php echo $_SERVER['PHP_SELF'];?>">
- <table>
- <tr><td><input type="hidden" name="install" value="true"></td></tr>
- <!-- database configuration-->
- <tr bgcolor="lavender"><td>MySQL server</td><td><input type='text' name='txtSqlServer' value='<?php echo $_SERVER['SERVER_NAME'];?>'></td></tr>
- <tr bgcolor="lavender"><td>MySQL user</td><td><input type='text' name='txtSqlUser'></td></tr>
- <tr bgcolor="lavender"><td>MySQL password</td><td><input type='text' name='txtSqlPass'></td></tr>
- <tr bgcolor="lavender"><td>MySQL database</td><td><input type='text' name='txtSqlDb'></td></tr>
- <!-- options-->
- <tr><td>Overwrite the existing tables</td><td><input type='checkbox' name='chkBoxOw' value='1' onclick="document.frmInstall.chkBoxNewDb.checked=false"><i>in case of if you want to overwrite the tables in existing db</i></td></tr>
- <tr><td>Create the new database</td><td><input type='checkbox' name='chkBoxNewDb' value='1' onclick="document.frmInstall.chkBoxOw.checked=false"><i>in case of if you dont want to use an existing db</i></td></tr>
- <tr><td>Autoset directory permission</td><td><input type='checkbox' name='chkBoxDirPerm' value='1'></tr>
- <!-- new admin account-->
- <tr bgcolor="#CCCCCC"><td>Admin user name</td><td><input type='text' name='txtAdminName'></td></tr>
- <tr bgcolor="#CCCCCC"><td>Password</td><td><input type='text' name='txtAdminPass'></td></tr>
- <tr bgcolor="#CCCCCC"><td>Confirmed password</td><td><input type='text' name='txtAdminPass2'></td></tr>
- <tr><td><input type='submit' value='Install'><input type='reset' value=' reset '></td><td></td></tr>
- </table>
- </form>
- </body>
- </html>
复制代码 |
|
|
|
|
|
|
|

楼主 |
发表于 31-8-2007 11:15 AM
|
显示全部楼层
mysql.class.php
- <?php
- class Mysql{
- var $_MYSQL = array();
- function __construct($_MYSQL,$isNewDb) {
- $this->Mysql($_MYSQL,$isNewDb);
- }
- function Mysql($_MYSQL,$isNewDb) {
- $this->_MYSQL = $_MYSQL;
- $this->validateMySqlData($_MYSQL); //kinda lazy to do validation
-
- //now checking the mysql connection
- echo "Connecting to mysql... ";
- @mysql_connect($_MYSQL['host'],$_MYSQL['user'], $_MYSQL['pass']) or die("Mysql Error : " . mysql_error());
- echo "Done <br />";
- // Create a new database
- if ($isNewDb) {
- echo "Creating new database...";
- @mysql_query("CREATE DATABASE {$_MYSQL['db']}") or die("Mysql Error : " . mysql_error());
- echo "Done <br />";
- }
- //select the databsae u created, see can select or not ^^"
- echo "Selecting database... ";
- @mysql_select_db($_MYSQL['db']) or die("Mysql Error : " . mysql_error());
- echo "Done <br /><br />";
- }
-
- function overWriteTable($tblName) {
- echo "Dropping existing tables by overwritting option ...";
- @mysql_query("DROP TABLE IF EXISTS {$tblName}") or die("Mysql Error : " . mysql_error());
- echo "Done<br/>";
- }
- function createNewTable($sql,$tblName) {
- echo "Creating '{$tblName}' ... "; @mysql_query($sql) or die("Mysql Error : " . mysql_error());
- echo "Done <br />";
- }
- function validateMySqlData($tbls) {
- }
- function createAdminAcc() {
- echo "Creating admin account ...";
- //mysql_query("insert into ................
- echo "Done <br />";
- }
- }
- ?>
复制代码 |
|
|
|
|
|
|
|

楼主 |
发表于 31-8-2007 11:16 AM
|
显示全部楼层
table.class.php
- <?php
- class Table {
- var $createTblSql = array();
- function __construct($tbls) {
- $this->Table($tbls);
- }
- function Table($tbls) {
- $this->validateTableData($tbls); //kinda lazy to do validation
- $arrayKeys = array_keys($tbls);
- foreach ($arrayKeys as $key) {
- foreach ($tbls[$key] as $key2 => $fields) {
- if (is_array($fields)) { //exclude primary key
- $rows .= $key2 . " " . $fields['type'] . " " . $fields['attributes'] . " " . ($fields['null']?"null":"not null") . " " . $fields['default'] . " " . $fields['extra'] . "," . "\r\n";
- } else {
- $pk = $fields;
- }
- }
- $rows .= (empty($pk) ? "" : "PRIMARY KEY({$fields})" );
- $rows = str_replace(" ", " ", $rows);
- $this->createTblSql[$key] = <<<EOF
- CREATE TABLE {$key} (
- $rows
- );
- EOF;
- $rows = "";
- } //end of foreach
- }
- function validateTableData($tbls) {
- }
- function getCreateTblSql() {
- return $this->createTblSql;
- }
- }
- ?>
复制代码 |
|
|
|
|
|
|
|

楼主 |
发表于 31-8-2007 11:16 AM
|
显示全部楼层
installation.class.php
- <?php
- class Installation {
- var $confFile; // configuration file
- var $isInstalled=false; //already installed ?
- var $root;
- function __construct() {
- $this->Installation();
- }
- function Installation() {
- $this->validateRoot();
- }
- function validateRoot() {
- if(!empty($_SERVER['WINDIR'])) {
- $slash="\";
- } else {
- $slash="/";
- }
- $path = explode($slash,dirname(__FILE__));
- $install_path = $path[count($path)-1];
- unset($path[count($path)-1]);
- $this->root=implode($slash, $path);
- $this->root=addslashes($this->root);
-
- if(!is_dir($this->root)) { //if directory is missing
- echo "<strong>Directory path is not found!</strong>";
- die;
- }
- }
- function install(Table $tbl, Mysql $mysql,$isOverWrite=false) {
- $tblSqlArray = $tbl->getCreateTblSql();
- if (is_array($tblSqlArray) && count($tblSqlArray)>0) {
- while ($sql = current($tblSqlArray)) {
- if ($isOverWrite) $mysql->overWriteTable(key($tblSqlArray));
- $mysql->createNewTable($sql,key($tblSqlArray));
- next($tblSqlArray);
- }
- //without facing to the die/exit command, here we create an admin account
- $mysql->createAdminAcc();
- $this->writeConfigFile($mysql->_MYSQL);
- }
- }
- function writeConfigFile($_MYSQL) {
- //time to write The config file !!!
- $conf_dat=<<<EOF
- <?php
- \$done=true;
- \$root="{$this->root}";
- \$txtServer="{$_MYSQL['host']}";
- // Your MYSQL server
- \$txtUser="{$_MYSQL['user']}";
- // Your MySQL username
- \$txtPass="{$_MYSQL['pass']}";
- // Your MySQL password
- \$txtDB="{$_MYSQL['db']}";
- // Your MySQL database name
- ?>
- EOF;
- echo "Writing data to configuration file... ";
- // Write the conf file to save the installation info
- $w=@fopen(dirname(__FILE__)."/config.php","w+") or die("Cannot write to ./config.php ! Please check the directory permission");
- if($w) {
- fputs($w,$conf_dat);
- fclose($w);
- }
- echo "Done<br/>";
- exit;
- }
-
- function autoSetDirPermission() {
- //now i cannot test in linux os ~_~
- //should be more dynamic, autoSetDirPermission($directory, $setToPerm)
- //if it is not a Win system, check for valid directory permissions
- if(empty($_SERVER['WINDIR'])) {
- echo "Trying to set directory/file permisions... ";
- echo "<br/>";
- echo $this->root . (@chmod($this->root, 0777) or (" <strong>Failed..</strong><br />"));
- echo "<br/>";
- echo $this->root . "/uploadfiles " . (@chmod($this->root."/uploadfiles", 0777) or (" <strong>Failed..</strong><br />"));
- echo "<br/>";
- echo $this->root . "/images " . (@chmod($this->root."/images", 0777) or (" <strong>Failed..</strong><br />"));
- echo "Done <br/>";
-
- //check the result
- $perm=fileperms($this->root);
- if($perm != '16895')
- echo "Directory permission not 777 ! - {$this->root}";
- echo "<br/>";
- $perm=fileperms($this->root."/images");
- if($perm != '16895')
- echo "Directory permission not 777 ! - {$this->root}/images";
- echo "<br/>";
- $perm=fileperms($this->root."/uploadfiles");
- if($perm != '16895')
- echo "Directory permission not 777 ! - {this->root}/uploadfiles";
- echo "<br/>";
- } else {
- echo "Changes on directory permission only applicable to linux/unix server<br/>";
- }
- echo "<p/>";
- }
- function isInstalled() {
- @include dirname(__FILE__)."\config.php";
- if (empty($txtDB)) { //one of the variable in the text file;
- echo "You are new to this web installation<br/>";
- return false;
- } else {
- if (isset($txtDB)) {
- mysql_connect($txtHost,$txtUser,$txtPass);
- $db = mysql_select_db($txtDB);
- mysql_close();
- if ($db) {
- echo "u have already installed the database by using this service<br/>";
- return true;
- } else {
- echo "Error : Database is lost ! please re-install<br/>";
- return false;
- }
- }
- }
- }
- }
- ?>
复制代码 |
|
|
|
|
|
|
| |
本周最热论坛帖子
|