日历

« 2008-08-30  
     12
3456789
10111213141516
17181920212223
24252627282930
31      

RSS订阅

  • 六步走完成OPENCART安装程序编写工作

    2007-10-07 11:04:10

    我们需要DW来进行辅助的可视化设计,如果你的html基本功很强大,可以用记事本

    step 1 编写css文件“stylesheet.css”:

    body, form {
      color: #FFFFFF;
      margin: 0;
      padding: 0;
      background-image: url(JDSJ042.jpg)
    }
     
    }

    body, td, th, input, select, textarea {
      font-family: Verdana, Arial, Helvetica, sans-serif;
      font-size: 11px;
      color: #000000;
    }

    #container {
     margin-top: 50px;
     width: 600px;
     position: absolute;
     left: 50%;
     margin-left: -300px;
     border: thin 1px #CCCCCC;
    }

    #header {
        color: #FFFFF;
     background-color: #00000;
     padding: 5px;
     margin: 5px;
     height: 30px;
     border: hidden;
     font-size: 24px;
     font-weight: bold;
    }

    #content {
        color: #FFFFF;
     padding: 5px;
     margin: 5px;
     border: 0px dashed #CCCCCC; 
    }

    #footer {
     padding: 5px;
     margin: 5px;
     border: 0px dashed #CCCCCC;
     text-align: center;
    }

    .required {
     color: #FF0000;
     margin-right: 5px;
    }

    .error {
     color: #FF0000;
     display: block;
     clear: both; 
    }

    .warning {
     background-color: #FFB3B5;
     padding: 3px;
    }

    .good {
     color: #669900;
     font-weight: bold;
    }

    .bad {
     color: #FF0000;
     font-weight: bold; 
    }

    step 2 生成一个html文件取名step1.html,把title改成“系统安装”,配置css文件,指向我们生成的stylesheet.css:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>系统安装</title>

    <link rel="stylesheet" type="text/css" href="stuff/stylesheet.css" />
    </head>
    <body>

    </body>
    </html>

    step 3 设计安装程序的第一个界面,你可以复制下面的源代码,再重新进行布局:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>系统安装</title>

    <link rel="stylesheet" type="text/css" href="stuff/stylesheet.css" />
    <style type="text/css">
    <!--
    body {
     background-image: url(stuff/JDSJ042.jpg);
    }
    -->
    </style></head>
    <body>
    <div id="container">
    <form action="index.php?step=1" method="post" enctype="multipart/form-data">
      <div id="header" >步骤 1 - 准备安装</div>
      <div id="content">
        <?php if (isset($error['message'])) { ?>
        <div class="warning"><?php echo $error['message']; ?></div>
        <?php } ?>
        <br>1. 请调整php.ini相关参数确保安装需要需要.</br>
        <table width="100%">
          <tr bgcolor="597380">
            <th width="35%" align="left">PHP 参数</th>
            <th width="25%" align="left">当前</th>
            <th width="25%" align="left">需要</th>
            <th width="15%" align="left">状态</th>
          </tr>
          <tr bgcolor="#AFC4CF">
            <td>PHP 版本:</td>
            <td><?php echo phpversion(); ?></td>
            <td>5.0+</td>
            <td><?php echo (phpversion() >= '5.0' ? '<span class="good">符合</span>' : '<span class="bad">错误</span>'); ?></td>
          </tr>
          <tr bgcolor="#AFC4CF">
            <td>Register Globals:</td>
            <td><?php echo (ini_get('register_globals') ? 'On' : 'Off'); ?></td>
            <td>Off</td>
            <td><?php echo (!ini_get('register_globals') ? '<span class="good">符合</span>' : '<span class="bad">错误</span>'); ?></td>
          </tr>
          <tr bgcolor="#AFC4CF">
            <td>File Uploads:</td>
            <td><?php echo (ini_get('file_uploads') ? 'On' : 'Off'); ?></td>
            <td>On</td>
            <td><?php echo (ini_get('file_uploads') ? '<span class="good">符合</span>' : '<span class="bad">错误</span>'); ?></td>
          </tr>
          <tr bgcolor="#AFC4CF">
            <td>Session Auto Start:</td>
            <td><?php echo (ini_get('session.auto_start') ? 'On' : 'Off'); ?></td>
            <td>Off</td>
            <td><?php echo (!ini_get('session.auto_start') ? '<span class="good">符合</span>' : '<span class="bad">错误</span>'); ?></td>
          </tr>
        </table>
        <br>2. 请确定下列PHP扩展已安装.</br>
        <table width="100%">
          <tr bgcolor="597380">
            <th width="35%" align="left">PHP 扩展</th>
            <th width="25%" align="left">当前设置</th>
            <th width="25%" align="left">需要设置</th>
            <th width="15%" align="left">状态</th>
          </tr>
          <tr bgcolor="#AFC4CF">
            <td>MySQL:</td>
            <td><?php echo (extension_loaded('mysql') ? 'On' : 'Off'); ?></td>
            <td>On</td>
            <td><?php echo (extension_loaded('mysql') ? '<span class="good">符合</span>' : '<span class="bad">错误</span>'); ?></td>
          </tr>
          <tr bgcolor="#AFC4CF">
            <td>GD:</td>
            <td><?php echo (extension_loaded('gd') ? 'On' : 'Off'); ?></td>
            <td>On</td>
            <td><?php echo (extension_loaded('gd') ? '<span class="good">符合</span>' : '<span class="bad">错误</span>'); ?></td>
          </tr>
        </table>
        <br>3. 请确定你有下列文件的读写权限.</br>
        <table width="100%">
          <tr bgcolor="597380">
            <th align="left">文件</th>
            <th width="15%" align="left">状态</th>
          </tr>
          <tr bgcolor="#AFC4CF">
            <td><?php echo realpath(dirname(__FILE__) . '/../config.php'); ?></td>
            <td><?php echo (is_writable(dirname(__FILE__) . '/../config.php') ? '<span class="good">可读写</span>' : '<span class="bad">只读</span>'); ?></td>
          </tr>
        </table>
        <br>4. 请确定你有下列目录的读写权限.</br>
        <table width="100%">
          <tr bgcolor="597380">
            <th align="left">目录</th>
            <th width="15%" align="left">状态</th>
          </tr>
          <tr bgcolor="#AFC4CF">
            <td><?php echo realpath(dirname(__FILE__) . '/../APP') . '/'; ?></td>
            <td><?php echo (is_writable(dirname(__FILE__) . '/../APP') ? '<span class="good">可读写</span>' : '<span class="bad">只读</span>'); ?></td>
          </tr>
          <tr bgcolor="#AFC4CF">
            <td><?php echo realpath(dirname(__FILE__) . '/../images') . '/'; ?></td>
            <td><?php echo (is_writable(dirname(__FILE__) . '/../images') ? '<span class="good">可读写</span>' : '<span class="bad">只读</span>'); ?></td>
          </tr>
          <tr bgcolor="#AFC4CF">
            <td><?php echo realpath(dirname(__FILE__) . '/../templates') . '/'; ?></td>
            <td><?php echo (is_writable(dirname(__FILE__) . '/../templates') ? '<span class="good">可读写</span>' : '<span class="bad">只读</span>'); ?></td>
          </tr>
          <tr bgcolor="#AFC4CF">
            <td><?php echo realpath(dirname(__FILE__) . '/../templates_C') . '/'; ?></td>
            <td><?php echo (is_writable(dirname(__FILE__) . '/../templates_C') ? '<span class="good">可读写</span>' : '<span class="bad">只读</span>'); ?></td>
          </tr>
          <tr bgcolor="#AFC4CF">
            <td><?php echo realpath(dirname(__FILE__) . '/../upload') . '/'; ?></td>
            <td><?php echo (is_writable(dirname(__FILE__) . '/../upload') ? '<span class="good">可读写</span>' : '<span class="bad">只读</span>'); ?></td>
          </tr>  
        </table>
      </div>
      <div id="footer">
        <input type="submit" value="继续" />
      </div>
    </form>
    </Div>
    </body>
    </html>

    图片一 安装程序的第一个界面:

    当然里面有代码啊,否则怎么叫动态网页?这个部分,套流行的说法,是MVC中V的部分,V的部分是交互的部分,也是安装程序的关键部分,这部分解决了,其它相对就容易了,下面设计第二个界面。

    step 4 设计安装程序的第二个界面,同样的,你可以复制下面的源代码,再重新进行布局,可以从step1.php复制出来,再添加<form></form>部分:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>系统安装</title>

    <link rel="stylesheet" type="text/css" href="stuff/stylesheet.css" />
    </head>
    <body>
    <div id="container">
    <form action="index.php?step=2" method="post" enctype="multipart/form-data">
      <div id="header">步骤 2 - 数据库信息</div>
      <div id="content">
        <?php if (isset($error['message'])) { ?>
        <div class="warning"><?php echo $error['message']; ?></div>
        <?php } ?>
        <p>请输入你的数据库连接信息.</p>
        <table>
          <tr bgcolor="#AFC4CF">
            <td width="185"><span class="required">*</span>主机名称:</td>
            <td><?php if (isset($_POST['db_host'])) { ?>
              <input type="text" name="db_host" value="<?php echo $_POST['db_host']; ?>" />
              <?php } elseif (isset($_SESSION['db_host'])) { ?>
              <input type="text" name="db_host" value="<?php echo $_SESSION['db_host']; ?>" />
              <?php } else {?>
              <input type="text" name="db_host" value="localhost" />
              <?php } ?>
              <?php if (isset($error['db_host'])) { ?>
              <span class="required"><?php echo $error['db_host']; ?></span>
              <?php } ?></td>
          </tr>
          <tr bgcolor="#AFC4CF">
            <td><span class="required">*</span>用户名:</td>
            <td><input type="text" name="db_user" value="<?php echo (isset($_POST['db_user']) ? $_POST['db_user'] : @$_SESSION['db_user']); ?>" />
              <?php if (isset($error['db_user'])) { ?>
              <span class="required"><?php echo $error['db_user']; ?></span>
              <?php } ?></td>
          </tr>
          <tr bgcolor="#AFC4CF">
            <td><span class="required">*</span>密码:</td>
            <td><input type="text" name="db_password" value="<?php echo (isset($_POST['db_password']) ? $_POST['db_password'] : @$_SESSION['db_password']); ?>" />
              <?php if (isset($error['db_password'])) { ?>
              <span class="required"><?php echo $error['db_password']; ?></span>
              <?php } ?></td>
          </tr>
          <tr bgcolor="#AFC4CF">
            <td><span class="required">*</span>数据库名称:</td>
            <td><input type="text" name="db_name" value="<?php echo (isset($_POST['db_name']) ? $_POST['db_name'] : @$_SESSION['db_name']); ?>" />
              <?php if (isset($error['db_name'])) { ?>
              <span class="required"><?php echo $error['db_name']; ?></span>
              <?php } ?></td>
          </tr>
        </table>
      </div>
      <div id="footer">
        <input type="submit" value="继续" />
      </div>
    </form>
    </div>
    </body>
    </html>

    图片二 安装程序的第二个界面:

    step 5 设计安装程序的第二个界面,同样的,你可以复制下面的源代码,再重新进行布局,可以从step1.php复制出来,再添加<form></form>部分:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>系统安装</title>

    <link rel="stylesheet" type="text/css" href="stuff/stylesheet.css" />
    </head>
    <body>
    <div id="container">
    <form action="index.php?step=3" method="post" enctype="multipart/form-data">
    <div id="header">步骤 3 - 安装完成!</div>
    <div id="content">
      <p>恭喜你! 演示程序已经成功地安装在你的电脑上.</p>
      <div class="warning">不要忘记删除你的安装目录!</div>
    </div>
    <div id="footer">
      <input type="submit" value="继续" />
    </div>
    </form>
    </div>

    </body>
    </html>

    图片三 安装程序的第三个界面:

    step 6 三个界面的内容已经确定,目的就是能把里面的部分代码拷贝出来,放到我们的安装程序中去:

    没有什么关子好卖,源代码列在下面,关键是程序的结构,下面的源代码也就是最终的源代码,它们经过了提炼,把共用的部分提了出来放到程序的最后,然后打开输出缓冲,先把<form>部分的内容存到变量$content中,再到最终的部分把内容吐出来,组成一个完整的HTML网页输出到浏览器上,实际设计的过程不一定会一步走到step6,可能的行为是你把代码放进去,先让index.php(安装程序)运行起来,再做提炼工作。

        另一个好玩的事情就是:安装程序总是喜欢让一个单一的文件完成所有的工作,这样简洁高效,我也赞成不必要把MVC的设计思想放到安装程序的编写上,毕竟,这是个易拉罐的壳,喝完就扔垃圾桶了。

       程序运行后,先判断"/index.php?step=1"中的那个变量step有没有设定,if语句的条件在程序第一次运行时运行时会命中,然后程序简单地让step=1后进入switch语句。沿着switch规定的线路,进入1号分支第一次,if ((strtoupper($_SERVER['REQUEST_METHOD']) == 'POST'))并不会满足,于是程序生成一个完整的“界面一”的HTML代码进行第一次人机交互;用户从界面上读取系统要求的设置,按“继续”按钮“post”,上述的if条件得到满足,程序进行一系列的判断,看相关参数是否符合要求,不符合再次给出“界面一”,否则header到step2; 与分支1相似,一开始step2的if条件不会满足,程序生成完整的HTML代码给出界面二,完成与MYSQL连接信息的设置,POST后IF条件中的语句对录入信息进行校验,并测试连接的有效性,连接有效后,把SQL文件中的数据导入MYSQL,系统进入step3,这一步要讲一下的就是系统会把收集的信息导入到config.php文件中,其它就没有什么嚼头了。

        文章到此结束,事情总是这样,没有掌握的人会有兴趣看一下这篇文章,老鸟早就拍拍翅膀飞走了,连个脚印也不会留下。哈哈,不管怎样,研读OpenCart的系列文章还在继续,这几天开始准备一篇使用OpenCart框架来做一个示例应用的文章,看看OpenCart的团队是如何的雄心勃勃,建立了一个适合开发各类PHP程序的基础框架。

    附完整的index.php源代码(版权归OpenCart所有The OpenCart owns the copyright of the below source code):

    <?php
    error_reporting(E_ALL);

    header('Content-Type: text/html; charset=utf-8');

    session_start();

     

    $ds =  DIRECTORY_SEPARATOR; // too much typing ;)

    if (!isset($_GET['step'])) {
     $step = 1;
    } else {
     $step = $_GET['step'];
    }

    switch ($step) {
     case '1':
      if ((strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')) {
         $error = array();
     
         if (phpversion() < '5.0') {
           $error['message'] = '警告: 你需要使用PHP5以上的版本以便正常工作!';
         }

         if (ini_get('register_globals')) {
           $error['message'] = '警告:演示程序要求将register_globals设置为Off!';
         }

         if (!ini_get('file_uploads')) {
           $error['message'] = '警告:file_uploads需要设置为On!';
         }

         if (ini_get('session.auto_start')) {
           $error['message'] = '警告:演示程序需要将session.auto_start设置为Off!';
         }

         if (!extension_loaded('mysql')) {
           $error['message'] = '警告:MySQL extention 需要装入,请修改php.ini相关参数!';
         }

         if (!extension_loaded('gd')) {
           $error['message'] = '警告:GD extention 需要装入,请修改php.ini相关参数!';
         }

         if (!is_writable(dirname(__FILE__) . '/../config.php')) {
           $error['message'] = '警告:config.php在安装时必须设置成可读写状态!';
         }
          

        if (!is_writable(dirname(__FILE__) . '/../APP')) {
           $error['message'] = '警告:APP 目录在安装时必须设置成可读写状态!';
         }

        if (!is_writable(dirname(__FILE__) . '/../images')) {
           $error['message'] = '警告:Images 目录在安装时必须设置成可读写状态!';
         }

        if (!is_writable(dirname(__FILE__) . '/../templates')) {
           $error['message'] = '警告:templets 目录在安装时必须设置成可读写状态!';
         }
       
        if (!is_writable(dirname(__FILE__) . '/../templates_c')) {
           $error['message'] = '警告:templets_c 目录在安装时必须设置成可读写状态!';
         }

        if (!is_writable(dirname(__FILE__) . '/../upload')) {
           $error['message'] = '警告:upload 目录在安装时必须设置成可读写状态!';
         }                       
       if (!$error) {
        header('Location: index.php?step=2');
        exit();
       }
      }
      
      ob_start();
    ?>
    <form action="index.php?step=1" method="post" enctype="multipart/form-data">
      <div id="header" >步骤 1 - 准备安装</div>
      <div id="content">
        <?php if (isset($error['message'])) { ?>
        <div class="warning"><?php echo $error['message']; ?></div>
        <?php } ?>
        <br>1. 请调整php.ini相关参数确保安装需要需要.</br>
        <table width="100%">
          <tr bgcolor="597380">
            <th width="35%" align="left">PHP 参数</th>
            <th width="25%" align="left">当前</th>
            <th width="25%" align="left">需要</th>
            <th width="15%" align="left">状态</th>
          </tr>
          <tr bgcolor="#AFC4CF">
            <td>PHP 版本:</td>
            <td><?php echo phpversion(); ?></td>
            <td>5.0+</td>
            <td><?php echo (phpversion() >= '5.0' ? '<span class="good">符合</span>' : '<span class="bad">错误</span>'); ?></td>
          </tr>
          <tr bgcolor="#AFC4CF">
            <td>Register Globals:</td>
            <td><?php echo (ini_get('register_globals') ? 'On' : 'Off'); ?></td>
            <td>Off</td>
            <td><?php echo (!ini_get('register_globals') ? '<span class="good">符合</span>' : '<span class="bad">错误</span>'); ?></td>
          </tr>
          <tr bgcolor="#AFC4CF">
            <td>File Uploads:</td>
            <td><?php echo (ini_get('file_uploads') ? 'On' : 'Off'); ?></td>
            <td>On</td>
            <td><?php echo (ini_get('file_uploads') ? '<span class="good">符合</span>' : '<span class="bad">错误</span>'); ?></td>
          </tr>
          <tr bgcolor="#AFC4CF">
            <td>Session Auto Start:</td>
            <td><?php echo (ini_get('session.auto_start') ? 'On' : 'Off'); ?></td>
            <td>Off</td>
            <td><?php echo (!ini_get('session.auto_start') ? '<span class="good">符合</span>' : '<span class="bad">错误</span>'); ?></td>
          </tr>
        </table>
        <br>2. 请确定下列PHP扩展已安装.</br>
        <table width="100%">
          <tr bgcolor="597380">
            <th width="35%" align="left">PHP 扩展</th>
            <th width="25%" align="left">当前设置</th>
            <th width="25%" align="left">需要设置</th>
            <th width="15%" align="left">状态</th>
          </tr>
          <tr bgcolor="#AFC4CF">
            <td>MySQL:</td>
            <td><?php echo (extension_loaded('mysql') ? 'On' : 'Off'); ?></td>
            <td>On</td>
            <td><?php echo (extension_loaded('mysql') ? '<span class="good">符合</span>' : '<span class="bad">错误</span>'); ?></td>
          </tr>
          <tr bgcolor="#AFC4CF">
            <td>GD:</td>
            <td><?php echo (extension_loaded('gd') ? 'On' : 'Off'); ?></td>
            <td>On</td>
            <td><?php echo (extension_loaded('gd') ? '<span class="good">符合</span>' : '<span class="bad">错误</span>'); ?></td>
          </tr>
        </table>
        <br>3. 请确定你有下列文件的读写权限.</br>
        <table width="100%">
          <tr bgcolor="597380">
            <th align="left">文件</th>
            <th width="15%" align="left">状态</th>
          </tr>
          <tr bgcolor="#AFC4CF">
            <td><?php echo realpath(dirname(__FILE__) . '/../config.php'); ?></td>
            <td><?php echo (is_writable(dirname(__FILE__) . '/../config.php') ? '<span class="good">可读写</span>' : '<span class="bad">只读</span>'); ?></td>
          </tr>
        </table>
        <br>4. 请确定你有下列目录的读写权限.</br>
        <table width="100%">
          <tr bgcolor="597380">
            <th align="left">目录</th>
            <th width="15%" align="left">状态</th>
          </tr>
          <tr bgcolor="#AFC4CF">
            <td><?php echo realpath(dirname(__FILE__) . '/../APP') . '/'; ?></td>
            <td><?php echo (is_writable(dirname(__FILE__) . '/../APP') ? '<span class="good">可读写</span>' : '<span class="bad">只读</span>'); ?></td>
          </tr>
          <tr bgcolor="#AFC4CF">
            <td><?php echo realpath(dirname(__FILE__) . '/../images') . '/'; ?></td>
            <td><?php echo (is_writable(dirname(__FILE__) . '/../images') ? '<span class="good">可读写</span>' : '<span class="bad">只读</span>'); ?></td>
          </tr>
          <tr bgcolor="#AFC4CF">
            <td><?php echo realpath(dirname(__FILE__) . '/../templates') . '/'; ?></td>
            <td><?php echo (is_writable(dirname(__FILE__) . '/../templates') ? '<span class="good">可读写</span>' : '<span class="bad">只读</span>'); ?></td>
          </tr>
          <tr bgcolor="#AFC4CF">
            <td><?php echo realpath(dirname(__FILE__) . '/../templates_C') . '/'; ?></td>
            <td><?php echo (is_writable(dirname(__FILE__) . '/../templates_C') ? '<span class="good">可读写</span>' : '<span class="bad">只读</span>'); ?></td>
          </tr>
          <tr bgcolor="#AFC4CF">
            <td><?php echo realpath(dirname(__FILE__) . '/../upload') . '/'; ?></td>
            <td><?php echo (is_writable(dirname(__FILE__) . '/../upload') ? '<span class="good">可读写</span>' : '<span class="bad">只读</span>'); ?></td>
          </tr>  
        </table>
      </div>
      <div id="footer">
        <input type="submit" value="继续" />
      </div>
    </form>
    <?php
        $content = ob_get_contents();

           ob_end_clean();
      break;
     case '2':
      if ($_POST) {
         $error = array();
     
         if (!$_POST['db_host']) {
           $error['db_host'] = '* Host 必须填写!';
         }
     
         if (!$_POST['db_user']) {
           $error['db_user'] = '* User 必须填写!';
         }
     
         if (!$_POST['db_name']) {
           $error['db_name'] = '* Database 名称必须填写!';
         }
     
         if (!$error) {
           if (!$connection = @mysql_connect($_POST['db_host'], $_POST['db_user'], $_POST['db_password'])) {
         $error['message'] = '出错: 不能连接到数据库,请确认主机名称,用户名,密码是否正确!' . "\n";
        } else {
         if (!@mysql_select_db($_POST['db_name'], $connection)) {
          $error['message'] = '出错: 数据库不存在!' . "\n";
         }   
        }
       }
       
       if (!$error) {
        mysql_query("set character set utf8", $connection);
        
        $file = dirname(__FILE__) . $ds.'openframeworkexample.sql';
       
           if ($sql = file($file)) {  //read a file into a array
           $query = '';

           foreach($sql as $line) {
                $tsl = trim($line);

           
          if (($sql != '') && (substr($tsl, 0, 2) != "--") && (substr($tsl, 0, 1) != '#')) {
                   $query .= $line;
             
             if (preg_match('/;\s*$/', $line)) {

           
                     $result = mysql_query($query, $connection);
             
               if (!$result) {
               die(mysql_error());
                     }
       
            $query = '';
                   }
                }
              }
              
         mysql_close($connection);
        } else {
         $error['message'] = '出错: 安装SQL文件 ' . $file . ' 不存在!' . "\n";
        }   
       }
           
       if (!$error) {
        $_SESSION['db_host']     = $_POST['db_host'];
        $_SESSION['db_user']     = $_POST['db_user'];
        $_SESSION['db_password'] = $_POST['db_password'];
        $_SESSION['db_name']     = $_POST['db_name'];
        
        header('Location: index.php?step=3');
        exit();
       }
      }
      
      ob_start();
    ?>
    <form action="index.php?step=2" method="post" enctype="multipart/form-data">
      <div id="header">步骤 2 - 数据库信息</div>
      <div id="content">
        <?php if (isset($error['message'])) { ?>
        <div class="warning"><?php echo $error['message']; ?></div>
        <?php } ?>
        <p>请输入你的数据库连接信息.</p>
        <table>
          <tr bgcolor="#AFC4CF">
            <td width="185"><span class="required">*</span>主机名称:</td>
            <td><?php if (isset($_POST['db_host'])) { ?>
              <input type="text" name="db_host" value="<?php echo $_POST['db_host']; ?>" />
              <?php } elseif (isset($_SESSION['db_host'])) { ?>
              <input type="text" name="db_host" value="<?php echo $_SESSION['db_host']; ?>" />
              <?php } else {?>
              <input type="text" name="db_host" value="localhost" />
              <?php } ?>
              <?php if (isset($error['db_host'])) { ?>
              <span class="required"><?php echo $error['db_host']; ?></span>
              <?php } ?></td>
          </tr>
          <tr bgcolor="#AFC4CF">
            <td><span class="required">*</span>用户名:</td>
            <td><input type="text" name="db_user" value="<?php echo (isset($_POST['db_user']) ? $_POST['db_user'] : @$_SESSION['db_user']); ?>" />
              <?php if (isset($error['db_user'])) { ?>
              <span class="required"><?php echo $error['db_user']; ?></span>
              <?php } ?></td>
          </tr>
          <tr bgcolor="#AFC4CF">
            <td><span class="required">*</span>密码:</td>
            <td><input type="text" name="db_password" value="<?php echo (isset($_POST['db_password']) ? $_POST['db_password'] : @$_SESSION['db_password']); ?>" />
              <?php if (isset($error['db_password'])) { ?>
              <span class="required"><?php echo $error['db_password']; ?></span>
              <?php } ?></td>
          </tr>
          <tr bgcolor="#AFC4CF">
            <td><span class="required">*</span>数据库名称:</td>
            <td><input type="text" name="db_name" value="<?php echo (isset($_POST['db_name']) ? $_POST['db_name'] : @$_SESSION['db_name']); ?>" />
              <?php if (isset($error['db_name'])) { ?>
              <span class="required"><?php echo $error['db_name']; ?></span>
              <?php } ?></td>
          </tr>
        </table>
      </div>
      <div id="footer">
        <input type="submit" value="继续" />
      </div>
    </form>
    <?php  
      $content = ob_get_contents();

           ob_end_clean();
      break;
     case '3':
      if ((strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')) {
         $error = array();
       
       $server = $_SERVER['HTTP_HOST'] . substr($_SERVER['PHP_SELF'], 0 , (strpos($_SERVER['PHP_SELF'], 'install/index.php')));
       
       $config = realpath(dirname(__FILE__) . '/../config.php');


       if (is_writable($config)) {
        $output  = '<?php' . "\n";
        $output .= '// HTTP' . "\n";
        $output .= 'define(\'HTTP_SERVER\', \'http://' . $server . '\');' . "\n";
        $output .= 'define(\'HTTP_IMAGE\', \'http://' . $server . 'image/\');' . "\n\n";

        $output .= '// HTTPS' . "\n";
        $output .= 'define(\'HTTPS_SERVER\', \'\');' . "\n";
        $output .= 'define(\'HTTPS_IMAGE\', \'\');' . "\n\n";

        $output .= '// DIR' . "\n";
        $output .= 'define(\'DIR_APP\', \'' . realpath(dirname(__FILE__) . '/..') . '/APP/\');' . "\n";
        $output .= 'define(\'DIR_UPLOAD\', \'' . realpath(dirname(__FILE__) . '/..') . '/upload/\');' . "\n";
        $output .= 'define(\'DIR_IMAGES\', \'' . realpath(dirname(__FILE__) . '/..') . '/imageS/\');' . "\n";
        $output .= 'define(\'DIR_LIBRARY\', \'' . realpath(dirname(__FILE__) . '/..') . '/library/\');' . "\n";
        $output .= 'define(\'DIR_TEMPLATES\', \'' . realpath(dirname(__FILE__) . '/..') . '/templates/\');' . "\n";
        $output .= 'define(\'DIR_TEMPLATES_C\', \'' . realpath(dirname(__FILE__) . '/..') . '/templates_c/\');' . "\n";
       
        $output .= '// DB' . "\n";
        $output .= 'define(\'DB_HOST\', \'' . $_SESSION['db_host'] . '\');' . "\n";
        $output .= 'define(\'DB_USER\', \'' . $_SESSION['db_user'] . '\');' . "\n";
        $output .= 'define(\'DB_PASSWORD\', \'' . $_SESSION['db_password'] . '\');' . "\n";
        $output .= 'define(\'DB_NAME\', \'' . $_SESSION['db_name'] . '\');' . "\n";
       
        $output .= '?>';    
           
        $file = fopen($config, 'w');
        
        fwrite($file, $output);
      
           fclose($file);
       } else {
        $error['message'] = '错误: 不能对config.php进行设置,请检查你是否有config目录的读写权限: ' . $config . "\n";
       }
                 
       if (!$error) {
                    $server = substr($_SERVER['PHP_SELF'], 0 , (strpos($_SERVER['PHP_SELF'], 'install/index.php')));
        header('Location: '.$server);
        exit();
       }
      }
      //header('Location: index.php?step=4');
      //exit();
      ob_start();
    ?>
    <form action="index.php?step=3" method="post" enctype="multipart/form-data">
    <div id="header">步骤 3 - 安装完成!</div>
    <div id="content">
      <p>恭喜你! 演示程序已经成功地安装在你的电脑上.</p>
      <div class="warning">不要忘记删除你的安装目录!</div>
    </div>
    <div id="footer">
      <input type="submit" value="继续" />
    </div>
    </form>
    <?php 
        $content = ob_get_contents();

           ob_end_clean();
      break;
    }
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>系统安装</title>
    <base href="<?php echo 'http://' . $_SERVER['HTTP_HOST'] . substr($_SERVER['PHP_SELF'], 0 , (strrpos($_SERVER['PHP_SELF'], "/") + 1)); ?>">
    <link rel="stylesheet" type="text/css" href="stuff/stylesheet.css" />
    </head>
    <body>
    <div id="container"><?php echo $content; ?></div>
    </body>
    </html>

  • OpenCart解读(三)控制器product.php的一个BUG

    2007-10-06 11:24:16

    试用的时候,发现OpenCart对中文支持不是很好,在后台录入商品信息时,保存后出现乱码,打开PHPMyAdmin一看表里存放的内容就是乱码,很奇怪这个问题,OpenCart设计时强调代码和数据库都使用utf8,那为什么会出现乱码呢?依旧使用ZFDemo_log,在/admin/controller/product.php中插入如下调试代码(红色部分):

    $name = $request->get('name', 'post');

    $descrīption = $request->get('descrīption', 'post');

    foreach ($request->get('name', 'post', array()) as $key => $value) {

      $sql = "insert into product_descrīption set product_id = '?', language_id = '?', name = '?', descrīption = '?'";    ZFDemo_Log::log('admin_controller_product.php(line 51)->$parse = ' . print_r($database->parse($sql, $insert_id, $key, $name[$key], $descrīption[$key]), true));

    运行后台管理系统,在log.txt捕捉到如下输出: 

    03:10:19 admin_controller_product.php(line 51)->$parse = insert into product_descrīption set product_id = '12', language_id = '1', name = 'test', descrīption = '

    test

    ' 03:10:19 admin_controller_product.php(line 51)->$parse = insert into product_descrīption set product_id = '12', language_id = '2', name = '测试', descrīption = '

    测试

    ' }

     似乎没有什么问题,但为什么会在MYSQL的product_descrīption中出现乱码呢?在PHPMyAdmin中把上面的第二句INSERT语句直接运行,果然没有问题,那说明在OpenCart进行插入操作时有什么情况和我在使用PHPMyAdmin时不一样,再次分析源代码看到第24行这么一句:

     mysql_query("set character set utf8");

    这是一句将字符集定义为utf8的SQL语句,该不会是它造成乱码吧?为了测试,就在PHPMyAdmin中测试如下语句:

    set character set utf8";

    insert into product_descrīption set product_id = '12', language_id = '2', name = '测试', descrīption = '测试';

    结果是什么?果然出现了乱码! 这句语句有什么问题?MYSQL执行上述语句出现乱码倒底是不是一个BUG?现在还不知道,把admin/controller/pruduct.php 的24行注释掉,功能恢复正常。

    (未完待续)

  • 还没有完成的OpenCart文档

    2007-10-05 23:28:14

        其实,程序员都不喜欢写文档,不管是中国人还是外国人,在OpenCart网站上刚放上一个PDF文档,里面只有十一个页面,还是个没有完成的目录,不知道要有完整的文档,要等到什么时候?

      可以去看一下:http://www.opencart.com/documentation

       还有对中文的支持,现在可以下载的最新版本是0.7.6,是没有对中文的支持的,简体和繁体都没有。论坛里对简体中文的呼声挺大,我试了一下,现在中文支持方面还有问题,取出的都是乱码,明天看一下倒底是什么原因。不过好多中国人都在国外使用这个软件。下面一个链接是要求支持中文的,不过网站好象是架在美国的。不知道是学习目的,还是确实有想把它打造成购物网站的意思: http://www.tianzhengrong.com/

      我在以前的日志里说过OpenCart里是有框架的,下面给出实际使用了OpenCart Framework的网站链接:

    http://www.pepwave.com/

    http://www.peplink.com/

    使用OpenCart开发的在线购物网站:

    http://www.beewild.com/

    http://www.mysticaldaze.co.uk/

    期待有中文的示例网站出现:)

     

  • OpenCart解读(二)设置类config

    2007-10-04 13:50:51

     

    文件config.php位于../library/application/config.php,调用后生成config对象,源代码中可以清楚地看到,config类有一个属性$data,四个方法set(),get(),has(),load(),其中load()方法还没有开发完成,也没有在系统中使用,作用是直接从存放参数的文件中读取参数到$data中。

    <?php
    //config.php

    class Config {
    var $data = array();

    function set($key, $value) {
       $this->data[$key] = $value;
    }

    function get($key) {
       //ZFDemo_Log::log('library_application_config.php(Line 10)->$this->data = ' .     print_r($this->data, true));
       return (isset($this->data[$key]) ? $this->data[$key] : NULL);
    }

    function has($key) {
       return isset($this->data[$key]);
    }

    function load($filename) {
       $file = DIR_CONFIG . $filename;

       if (file_exists($file)) {
       $cfg = array();

       include($file);
       $this->data = array_merge($this->data, $cfg);
       //ZFDemo_Log::log('library_application_config.php(Line 24)->$this->data = ' . print_r($this->data, true));
    }
    }
    }
    ?>

    在index.php中是这样对config进行调用的:

    // Settings
    $settings = $database->getRows("select * from setting where type = 'admin' or type = 'global'");

    foreach ($settings as $setting) {
       $config->set($setting['key'], $setting['value']);
      ZFDemo_Log::log('admin_index.php(line 52)->$setting=' . print_r($setting, true));
    }

    为了观察config对象的$data属性中存放了什么数据,我们把一句调试语句插入到循环中(具体如何使用ZFDemo调试工具,请参见:http://www.phpchina.com/31261/viewspace_15275.html)。

    打开调试信息文件log.txt,选取其中一段如下:

    admin_index.php(line 52)->$setting=Array
    (
    [setting_id] => 3937
    [type] => global
    [group] => cod
    [key] => cod_sort_order
    [value] => 1

    )

    我们来看红色的部分,这是被存放到config对象的$data属性中的,上列的信息表示有一个系统参数“cod_sort_order”,它的值设为1

    比较简单吧,config类的内容主要就是这些了,关于load()方法,我们可以期待后面新的版本中是否有需要实现。或者,可以在你自己开发的新的系统中开发利用,把存放在配置文件中的参数读入到$data属性中。

    (未完待续)

  • OpenCart解读(一)装载器locator

    2007-10-03 22:39:16

        OpenCart是使用面向对象编程的,同时又使用了MVC的设计思想,因此在解读其源代码时是看不到过程式的代码的。同时它把MVC框架部分单独放在library目录中,这个部分的内容一般是不作修改的。就象你显式地使用其它框架编程一样。

        library子目录下除了子目录外,就只有一个装配器文件:locator.php,这个文件中只有一个类:locator,实际上是一个调度程序,框架中的其它组件,如controller,module,session,cache,language统统由它装载调度。

        为了让系统运行起来, 象所有的PHP程序一样,OpenCart需要把系统中的一些重要的参数从config.php中,对于这个文件,按惯例,使用一句:

    require('config.php');

      连接数据库的DSN参数和相关目录设置就可用了,下面有必要将config.php文件列表在下,这有利于我们理解OpenCart的设计思想:

    <?php
    // HTTP
    define('HTTP_SERVER', 'http://localhost/opencart/');
    define('HTTP_IMAGE', 'http://localhost/opencart/image/');

    // HTTPS
    define('HTTPS_SERVER', '');
    define('HTTPS_IMAGE', '');

    // DIR
    define('DIR_CACHE', 'C:\wamp\www\opencart/cache/');
    define('DIR_DOWNLOAD', 'C:\wamp\www\opencart/download/');
    define('DIR_IMAGE', 'C:\wamp\www\opencart/image/');
    define('DIR_LIBRARY', 'C:\wamp\www\opencart/library/');
    define('DIR_MODEL', 'C:\wamp\www\opencart\catalog/model/');
    define('DIR_CONTROLLER', 'C:\wamp\www\opencart\catalog/controller/');
    define('DIR_LANGUAGE', 'C:\wamp\www\opencart\catalog/language/');
    define('DIR_EXTENSION', 'C:\wamp\www\opencart\catalog/extension/');
    define('DIR_TEMPLATE', 'C:\wamp\www\opencart\catalog/template/');

    // DB
    define('DB_HOST', 'localhost');
    define('DB_USER', 'root');
    define('DB_PASSWORD', '111111');
    define('DB_NAME', 'opencart');
    ?>

    与别的系统可能有些不同是的,OpenCart有一个config类用来从别的设置文件或数据库里存取数据,这些数据也是在程序一开始运行就需要的,因此第一个由locator装载的就是config类,装载config类就只简单地用了一句:

    // Config
    $config =& $locator->get('config');

        对应locator内部的代码如下:

     function &get($key) {
      if (!isset($this->data[$key])) {
       $this->data[$key] =& $this->create($key);
      }
      
      return $this->data[$key];
     }

        任何一个类都确保只有一个实例,如果调用相关对象时已经有其它部分创建了这个实例,get方法只是简单地返回它,如果是第一次调用,就调用create方法创建它。

        其它所有的对象这是以这种方法来进行管理。

    (未完待续)

     

     

  • 大家无形

    2007-10-02 00:26:01

    近日研读OPENCART,感悟良多:

    1 开源真好!

    2 完全可以从中拿出一个“框架”来,只要把里面的“library”子目录取出来即可,但是在圈内少有人说其是“框架”;

    3 “框架”近日有日益增多的趋势。很多朋友专门从事框架的研制工作,还有很多朋友对框架持否定态度。不可否认的是,如果是一个有多个作品的合格程序员,他其实是有自己的框架的,只不过他不把它叫做框架而已,正所谓“心中有框架,无也有;心中无框架,有也无”;

    4 很多框架实际上就是MVC结构开发加一点点方便数据库操作的功能,或者时髦一点,加上一点AJAX,然而尽量让index.php变得更短;

    5 我对“框架”的观点是一个框架之所以称为框架,首先性能不能太不靠谱,不能牺牲执行速度;其次是敏捷开发;再其次是MVC(重用方便,分工明确);

    6 PHP程序员的学习曲线应该是:无招(未入门)->有招(框架辅助)->无招(把框架消化掉);我认为最高的境界是“心中没有框架地使用框架”;

    7 框架的意义不在框架本身,衡量一个框架好与不好应该以其普及程度为标准,因此ZF最有可能成为一个好框架(原因很简单:其得天独厚的血统有利其普及),“地球人”都在用的框架,一定是世界上最好的框架。

    8 没有框架也能开发程序的程序员才是功底扎实的好程序员!

Open Toolbar