佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 903|回复: 4

object-oriented web installation (欢迎修改)

[复制链接]
发表于 31-8-2007 11:12 AM | 显示全部楼层 |阅读模式
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 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 31-8-2007 11:14 AM | 显示全部楼层
index.php


  1. <html>
  2.   <head>
  3.     <title>web installation 1.0 - by ikanyuchiew</title>
  4.         <script>

  5.         </script>
  6.   </head>
  7.   <body>
  8. <?php
  9. include_once "installation.class.php";
  10. include_once "table.class.php";
  11. include_once "mysql.class.php";

  12. $installation = new Installation();
  13. $_TABLE = $_MYSQL = $_OPTION = array();

  14. if (!$installation->isInstalled()) {

  15.   //still havent install
  16.   if (isset($_POST['install']) && $_POST['install']==true) {
  17.     echo "Installation is processing...<br/>";

  18.         //Get the variables from the POST predefined variables
  19.     $_MYSQL['host'] = $_POST['txtSqlServer'];
  20.     $_MYSQL['user'] = $_POST['txtSqlUser'];
  21.     $_MYSQL['pass'] = $_POST['txtSqlPass'];
  22.     $_MYSQL['db'] = $_POST['txtSqlDb'];

  23.     $_OPTION['isNewDb'] = $_POST['chkBoxNewDb'];
  24.         $_OPTION['isOverWrite'] = $_POST['chkBoxOw'];
  25.         $_OPTION['isAutoSetPerm'] = $_POST['chkBoxDirPerm'];

  26.     //let say 2 tables i will create : table customers and table employees
  27.           //sorry i m not strong in SQL
  28.         $_TABLE['tblCust'] = array (
  29.       "id" => array (
  30.             "type"=>"int(10)",
  31.             "collation" => "latin1_swedish_ci",
  32.                 "attributes" => "unsigned",
  33.                 "null" => 0,
  34.         "default" => null,
  35.         "extra" => "auto_increment"
  36.       ),

  37.           "name" => array (
  38.             "type"=>"varchar(50)",
  39.             "collation" => "latin1_swedish_ci",
  40.                 "attributes" => null,
  41.                 "null" => 0,
  42.         "default" => "default ''",
  43.         "extra" => null
  44.           ),
  45.           "primary_key" => "id"
  46.         );

  47.         $_TABLE['tblEmp'] =  array(
  48.       "id" => array (
  49.             "type"=>"int(10)",
  50.             "collation" => "latin1_swedish_ci",
  51.                 "attributes" => "signed",
  52.                 "null" => 0,
  53.         "default" => null,
  54.         "extra" => "auto_increment"
  55.       ),

  56.           "name" => array (
  57.             "type"=>"varchar(50)",
  58.             "collation" => "latin1_swedish_ci",
  59.                 "attributes" => null,
  60.                 "null" => 1,
  61.         "default" => "default 'ikanyu'",
  62.         "extra" => null
  63.           ),
  64.      "primary_key" => "id"
  65.         );

  66.         if ($_OPTION['isAutoSetPerm'])
  67.       $installation->autoSetDirPermission();

  68.     if ($_OPTION['isNewDb'] || $_OPTION['isOverWrite']) {
  69.       $installation->install(new Table($_TABLE),new Mysql($_MYSQL,$_OPTION['isNewDb']),$_OPTION['isOverWrite']);
  70.     }
  71.   }
  72. } else {
  73.   echo "Delete the installation file for security purpose<br/>";
  74.   die;
  75. }

  76. ?>
  77.   <form name='frmInstall' method='post' action="<?php echo $_SERVER['PHP_SELF'];?>">
  78.   <table>

  79.     <tr><td><input type="hidden" name="install" value="true"></td></tr>

  80.     <!-- database configuration-->
  81.     <tr bgcolor="lavender"><td>MySQL server</td><td><input type='text' name='txtSqlServer' value='<?php  echo $_SERVER['SERVER_NAME'];?>'></td></tr>
  82.     <tr bgcolor="lavender"><td>MySQL user</td><td><input type='text' name='txtSqlUser'></td></tr>
  83.     <tr bgcolor="lavender"><td>MySQL password</td><td><input type='text' name='txtSqlPass'></td></tr>
  84.     <tr bgcolor="lavender"><td>MySQL database</td><td><input type='text' name='txtSqlDb'></td></tr>

  85.     <!-- options-->
  86.     <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>
  87.     <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>
  88.     <tr><td>Autoset directory permission</td><td><input type='checkbox' name='chkBoxDirPerm' value='1'></tr>

  89.     <!-- new admin account-->
  90.     <tr bgcolor="#CCCCCC"><td>Admin user name</td><td><input type='text' name='txtAdminName'></td></tr>
  91.     <tr bgcolor="#CCCCCC"><td>Password</td><td><input type='text' name='txtAdminPass'></td></tr>
  92.     <tr bgcolor="#CCCCCC"><td>Confirmed password</td><td><input type='text' name='txtAdminPass2'></td></tr>

  93.    <tr><td><input type='submit' value='Install'><input type='reset' value='   reset   '></td><td></td></tr>

  94.   </table>
  95.   </form>
  96.   </body>
  97. </html>
复制代码
回复

使用道具 举报

 楼主| 发表于 31-8-2007 11:15 AM | 显示全部楼层
mysql.class.php


  1. <?php

  2. class Mysql{

  3.   var $_MYSQL = array();

  4.   function __construct($_MYSQL,$isNewDb) {
  5.     $this->Mysql($_MYSQL,$isNewDb);
  6.   }

  7.   function Mysql($_MYSQL,$isNewDb) {
  8.     $this->_MYSQL = $_MYSQL;
  9.     $this->validateMySqlData($_MYSQL); //kinda lazy to do validation
  10.    
  11.     //now checking the mysql connection
  12.     echo "Connecting to mysql...  ";
  13.         @mysql_connect($_MYSQL['host'],$_MYSQL['user'], $_MYSQL['pass']) or die("Mysql Error : " . mysql_error());
  14.         echo "Done <br />";

  15.     // Create a new database
  16.         if ($isNewDb) {
  17.           echo "Creating new database...";
  18.           @mysql_query("CREATE DATABASE {$_MYSQL['db']}") or die("Mysql Error : " . mysql_error());
  19.           echo "Done <br />";
  20.         }

  21.     //select the databsae u created, see can select or not ^^"
  22.         echo "Selecting database...  ";
  23.         @mysql_select_db($_MYSQL['db']) or die("Mysql Error : " . mysql_error());
  24.         echo "Done <br /><br />";
  25.   }
  26.   
  27.   function overWriteTable($tblName) {
  28.     echo "Dropping existing tables by overwritting option ...";
  29.     @mysql_query("DROP TABLE IF EXISTS {$tblName}") or die("Mysql Error : " . mysql_error());
  30.         echo "Done<br/>";
  31.   }

  32.   function createNewTable($sql,$tblName) {
  33.       echo "Creating '{$tblName}' ...  "; @mysql_query($sql) or die("Mysql Error : " . mysql_error());
  34.           echo "Done <br />";
  35.   }

  36.   function validateMySqlData($tbls) {

  37.   }

  38.   function createAdminAcc() {
  39.     echo "Creating admin account ...";
  40.     //mysql_query("insert into ................
  41.         echo "Done <br />";
  42.   }
  43. }

  44. ?>
复制代码
回复

使用道具 举报

 楼主| 发表于 31-8-2007 11:16 AM | 显示全部楼层
table.class.php


  1. <?php
  2. class Table {

  3.   var $createTblSql = array();

  4.   function __construct($tbls) {
  5.     $this->Table($tbls);
  6.   }

  7.   function Table($tbls) {
  8.     $this->validateTableData($tbls); //kinda lazy to do validation

  9.     $arrayKeys = array_keys($tbls);
  10.         foreach ($arrayKeys as $key) {
  11.           foreach ($tbls[$key] as $key2 => $fields) {
  12.         if (is_array($fields)) { //exclude primary key
  13.           $rows .=  $key2 . " " . $fields['type'] . " " . $fields['attributes'] . " " .  ($fields['null']?"null":"not null") . " " . $fields['default'] . " " . $fields['extra'] . "," . "\r\n";
  14.         } else {
  15.           $pk = $fields;
  16.             }
  17.           }
  18.           $rows .=  (empty($pk) ? "" : "PRIMARY KEY({$fields})" );
  19.       $rows  =  str_replace("  ", " ", $rows);
  20.       $this->createTblSql[$key] = <<<EOF
  21. CREATE TABLE {$key} (
  22. $rows
  23. );
  24. EOF;
  25.       $rows = "";
  26.         } //end of foreach
  27.   }

  28.   function validateTableData($tbls) {

  29.   }

  30.   function getCreateTblSql() {
  31.     return $this->createTblSql;
  32.   }
  33. }

  34. ?>
复制代码
回复

使用道具 举报

 楼主| 发表于 31-8-2007 11:16 AM | 显示全部楼层
installation.class.php


  1. <?php
  2. class Installation {

  3.   var $confFile; // configuration file
  4.   var $isInstalled=false; //already installed ?
  5.   var $root;

  6.   function __construct() {
  7.     $this->Installation();
  8.   }

  9.   function Installation() {
  10.     $this->validateRoot();
  11.   }

  12.   function validateRoot() {

  13.     if(!empty($_SERVER['WINDIR'])) {
  14.           $slash="\";
  15.         } else {
  16.           $slash="/";
  17.         }

  18.     $path = explode($slash,dirname(__FILE__));
  19.     $install_path = $path[count($path)-1];
  20.     unset($path[count($path)-1]);
  21.         $this->root=implode($slash, $path);
  22.         $this->root=addslashes($this->root);
  23.    
  24.         if(!is_dir($this->root)) {  //if directory is missing
  25.           echo "<strong>Directory path is not found!</strong>";
  26.       die;
  27.         }
  28.   }

  29.   function install(Table $tbl, Mysql $mysql,$isOverWrite=false) {
  30.         $tblSqlArray = $tbl->getCreateTblSql();

  31.     if (is_array($tblSqlArray) && count($tblSqlArray)>0) {
  32.       while ($sql = current($tblSqlArray)) {

  33.                 if ($isOverWrite) $mysql->overWriteTable(key($tblSqlArray));

  34.         $mysql->createNewTable($sql,key($tblSqlArray));
  35.         next($tblSqlArray);
  36.       }

  37.           //without facing to the die/exit command, here we create an admin account
  38.           $mysql->createAdminAcc();
  39.           $this->writeConfigFile($mysql->_MYSQL);
  40.     }
  41.   }

  42.   function writeConfigFile($_MYSQL) {
  43.         //time to write The config file !!!
  44.     $conf_dat=<<<EOF
  45. <?php

  46. \$done=true;

  47. \$root="{$this->root}";

  48. \$txtServer="{$_MYSQL['host']}";
  49.         // Your MYSQL server

  50. \$txtUser="{$_MYSQL['user']}";
  51.         // Your MySQL username

  52. \$txtPass="{$_MYSQL['pass']}";
  53.         // Your MySQL password

  54. \$txtDB="{$_MYSQL['db']}";
  55.         // Your MySQL database name
  56. ?>
  57. EOF;

  58.      echo "Writing data to configuration file...  ";
  59.      // Write the conf file to save the installation info

  60.      $w=@fopen(dirname(__FILE__)."/config.php","w+") or  die("Cannot write to ./config.php ! Please check the directory permission");
  61.          if($w) {
  62.                 fputs($w,$conf_dat);
  63.                 fclose($w);
  64.          }
  65.          echo "Done<br/>";
  66.      exit;
  67.   }


  68.   function autoSetDirPermission() {
  69.     //now i cannot test in linux os ~_~
  70.           //should be more dynamic, autoSetDirPermission($directory, $setToPerm)
  71.             //if it is not a Win system, check for valid directory permissions
  72.     if(empty($_SERVER['WINDIR'])) {

  73.       echo "Trying to set directory/file permisions...  ";
  74.           echo "<br/>";
  75.       echo $this->root . (@chmod($this->root, 0777) or (" <strong>Failed..</strong><br />"));
  76.           echo "<br/>";
  77.       echo $this->root . "/uploadfiles " . (@chmod($this->root."/uploadfiles", 0777) or (" <strong>Failed..</strong><br />"));
  78.           echo "<br/>";
  79.       echo $this->root . "/images " . (@chmod($this->root."/images", 0777) or (" <strong>Failed..</strong><br />"));
  80.       echo "Done <br/>";
  81.       
  82.           //check the result
  83.       $perm=fileperms($this->root);
  84.       if($perm != '16895')
  85.         echo "Directory permission not 777 ! - {$this->root}";
  86.             echo "<br/>";
  87.       $perm=fileperms($this->root."/images");
  88.       if($perm != '16895')
  89.             echo "Directory permission not 777 ! - {$this->root}/images";
  90.                echo "<br/>";   
  91.       $perm=fileperms($this->root."/uploadfiles");
  92.       if($perm != '16895')
  93.             echo "Directory permission not 777 ! - {this->root}/uploadfiles";
  94.                 echo "<br/>";
  95.     } else {
  96.       echo "Changes on directory permission only applicable to linux/unix server<br/>";
  97.     }
  98.         echo "<p/>";
  99.   }

  100.   function isInstalled() {
  101.     @include dirname(__FILE__)."\config.php";

  102.     if (empty($txtDB)) { //one of the variable in the text file;
  103.           echo "You are new to this web installation<br/>";
  104.           return false;
  105.    } else {
  106.      if (isset($txtDB)) {
  107.        mysql_connect($txtHost,$txtUser,$txtPass);
  108.            $db = mysql_select_db($txtDB);
  109.            mysql_close();
  110.            if ($db) {
  111.                  echo "u have already installed the database by using this service<br/>";
  112.              return true;
  113.        } else {
  114.          echo "Error : Database is lost ! please re-install<br/>";
  115.          return false;
  116.            }
  117.      }
  118.    }
  119. }
  120. }
  121. ?>
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 15-9-2025 01:32 PM , Processed in 0.123148 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表