专为新手写的结合smarty的类第1/3页
author:一佰互联 2019-05-01   click:143

专为新手写的结合smarty的类,诚挚邀请大家多提宝贵意见
这个是为新手(也为自己)写的结合smarty一起使用的类,是未完成的,现在放出来的目的不是马上让新手使用,所以也没有把注解写的非常详细
希望各位高手多多提意见,我尽量完善它。
    首先声明,我写这个的目的也是为了锻炼自己,尽管我知道现在已经有很多类似的类了,但是我还是决定来写一个,
所以请大家在浏览的时候口下留情。 还有文件我也已经打包上传了,放在下面,请大家多多下载,多多提意见。有什么问题直接Q我 目前该类包括以下功能(使用范例,设$m = new Machine_m())
[数据库]
目前支持MYSQL与ACCESS两种数据库
配置参考config.php文件
使用:$m->send_query(SQL语句)//
$m->select_query(SQL语句,是否返回资源,默认为false即返回一个二维数组) [错误处理]
分为系统错误和用户错误
系统错误:
$this->sys_err( "配置错误,请检查config配置文件", "die");
第一个参数将记录错误信息到/lib/error/system.err中,第二个参数是处理方法(keep or die),如果需要修改浏览器端提示的话,还可以设置第三个参数,它默认为“很抱歉,本站发生系统错误,请稍候再试。”
用户错误:
$m->user_err( "注册系统关闭", "die", $_SERVER["HTTP_REFERER"] );
第一个参数是显示到浏览器端的提示,第二个参数是处理方法(keep or die),第三个参数是跳转页面,如果需要记录错误信息的话,还可以设置第四个参数,它将记录错误信息
到/lib/error/user.err中,不设默认不保存。 浏览器端的错误提示默认调用/lib/error/下的err_page.htm模版文件,也可以设置自己的错误模版文件,然后用$m->err_page=加载。 [静态生成]
只用短短一行就自动生成静态页面,跳转时可设置静态页面过期时间
(现在还是不完全的静态,完全的比较复杂,目前没有整合,大家如果要实现完全静态的话,可以结合我的create_html函数和文本操作系列函数实现)
使用方法:
$m->create_html(模版文件,静态输出路径,输出的文件名);
跳转:
$m->goto_html();
输出文件名默认等于当前php文件的文件名,提供这个参数目的是,当有需要使用静态分页时,可以用这个参数设置 [二维数组排序 (推荐)]
可以让二维数组作类似:“先按字段a升序,再按字段b降序”这样的排序
使用方法:
设有这样一个数组:$x = array( array("name"=>"machine_马", "age"=>23),array("name"=>"tom",age=>28),…… )
我们现在要把此此数组先按name升序排,再按age降序排
则用法为m_sort($x,"name",SORT_ASC,"age",SORT_DESC) [动态加载]
对于不常用的功能,我采用加载的方法来使用,个人认为这样可以节约资源
例如,如果我们要使用m_sort函数时,默认是没有加载这个函数的
需要这样加载:$m->load_func("m_sort")
然后就可以使用m_sort函数了 [分页]
这里我也不知道做得好不好,我是先写了一个类,再写一个函数来调它,目的是使用得时候比较方便
使用方法:m_page(数据条数,当前页号,每页多少行,显示多少个跳转链接)
函数返回一个数组:array(
  "rows" => 每页显示多少行,
  "prve" => 上一大页页号,//所谓大页,就是类似上7页,下7页这样的跳转
  "next" => 下一大页页号,
  "pages" => 共有多少页,
  "start" => SQL查询的开始记录数,
  "count" => 共有多少条记录,
  "links" => 链接页号,//如果共有13页,链接数是7个,当前又在第二大页,则输出array(8,9,10,11,12,13)
  "current_page" => 当前页号
  ); [验证表单]
把需要验证的表单事先写进函数类,判别的时候只需要把$_POST传入就可以了
使用方法:这个大家自己看函数就应该看得明白,这个函数需要大家按照自己的需求来修改的 [防止跨站攻击]
把这个功能也写道一个函数内了 [中文截取函数]
不是我写的,拿来修改了一下而已 [上传文件]
m_up_file($_FILES,上传路径,文件类型,大小限制)
其中上传路径可以这样设置,1:直接写文件夹路径,2:array("gif"=>"file/gif","jpg"=>"file=>jpg"),这样gif文件自动放入file/gif文件夹,jpg文件放入file/jpg文件夹
文件类型:写法1:"jpg",写法2:array("jpg","jpeg","gif")
返回array( "arr" => 已上传的文件数组, "err_msg" => 上传过程中的错误信息, "num" => 上传成功数 ) [文本操作 (推荐)]
假设有这样一个字符串 $str="你好<!--content-->phpchina<!--/content-->";
我们可以这样修改 $new_str=m_txt_replace("content","machine_马",$str);
现在$new_str的值为"你好<!--content-->machine_马<!--/content-->"
其他的几个函数,如:m_txt_add,m_txt_delete,m_txt_get都是类似的,大家可以自行参看。 注意:这个就是生成静态页面之后,修改的方法。
可以参见6to23并思考为什么他一篇帖子放了那么多回帖速度还那么快
答:因为它的回帖没有进数据库而是直接写到静态文件里面,再使用类似我上面的方法来修改的。你可以看他的源代码,找找<!--你就知道了。
目前已经基本实现就是以上功能了
接下来还打算做 [图片处理]
[UBB代码输出] 等一些其他常用功能 希望各位高手多多提意见
主类:
复制代码 代码如下: <?php 
session_cache_limiter( "private,must-revalidate"); 
session_start();  if ( !defined( "THIS_DIR" ) ) 

    die( "系统错误:路径常量丢失" ); 

if ( @!include_once( THIS_DIR . "lib/smarty/Smarty.class.php" ) ) 

    die( "系统错误:smarty文件丢失" ); 
}  unset( $GLOBALS ); 
unset( $HTTP_COOKIE_VARS ); 
unset( $HTTP_ENV_VARS ); 
unset( $HTTP_GET_VARS ); 
unset( $HTTP_POST_FILES ); 
unset( $HTTP_POST_VARS ); 
unset( $HTTP_SERVER_VARS ); 
unset( $HTTP_SESSION_VARS );  class Machine_m extends Smarty 

    // 数据库资源 
    private $conn; 
    // 路径 
    private $the_dir; 
    // 配置文件 
    private $config = array(); 
    private $config_url; 
    // 外部函数列表 
    private $func_list = array(); 
    private $func_list_url; 
    // 错误提示页面 
    public $err_page = "lib/error/err_page.htm"; 
    // 静态生成 
    public $html_cache = "html"; 
    public $html_cache_lifetime = 86400;      // 构造函数 
    public function __construct( $test = false ) 
    { 
        // 保留smarty类的构造部分 
        $this->assign("SCRIPT_NAME", isset($_SERVER["SCRIPT_NAME"]) ? $_SERVER["SCRIPT_NAME"] 
        : @$GLOBALS["HTTP_SERVER_VARS"]["SCRIPT_NAME"]);          // 现在是machine_m的构造部分 
        $this->left_delimiter = "<%"; 
        $this->right_delimiter = "%>"; 
        $this->the_dir = THIS_DIR; 
        $this->config_url = "{$this->the_dir}lib/config/config.php"; 
        $this->config = $this->parse_ini(); 
        $this->func_list_url = "{$this->the_dir}lib/config/func_list.php"; 
        $this->func_list = $this->parse_func(); 
        $this->state( $test ); 
        $this->load_func( array( "m_addslashes", "m_stripslashes" ) ); 
        $this->connect(); 
    }      // 析构函数 
    public function __destruct() 
    { 
    }      // 设置网站状态函数 
    private function state( $test ) 
    { 
        if ( $test == true) 
        { 
            $this->load_func( array( "m_print_r", "m_var_dump" ) ); 
            $this->compile_check = true; 
            error_reporting(E_ALL); 
        } 
        else 
        { 
            $this->compile_check = false; 
            error_reporting(0); 
        } 
    }      // 解析配置文件函数 
    private function parse_ini() 
    { 
        if ( !file_exists( $this->config_url ) ) 
        { 
            $this->sys_err( "config配置文件丢失", "die" ); 
        } 
        $config = parse_ini_file( $this->config_url ); 
        if ( !array_key_exists( "host_name", $config ) 
        || !array_key_exists( "user_name", $config ) 
        || !array_key_exists( "db_name", $config ) 
        || !array_key_exists( "password", $config ) ) 
        { 
            $this->sys_err( "配置错误,请检查config配置文件", "die"); 
        } 
        $config = $this->decode_config( $config ); 
        settype( $config, "object"); 
        return $config; 
    }      // 解析函数列表函数 
    private function parse_func() 
    { 
        if ( !file_exists( $this->func_list_url ) ) 
        { 
            $this->sys_err( "func_list配置文件丢失", "die" ); 
        } 
        $func_list = parse_ini_file( $this->func_list_url ); 
        return $func_list; 
    }      // 外部函数加载函数 
    public function load_func( $func ) 
    { 
        if ( is_array( $func ) ) 
        { 
            foreach ( $func as $func_name ) 
            { 
                $this->include_file( $this->func_list[$func_name] ); 
            } 
        } 
        else 
        { 
            $this->include_file( $this->func_list[$func] ); 
        } 
    }      // 外部函数包含函数 
    public function include_file( $file_url ) 
    { 
        $file_url = $this->the_dir .$file_url; 
        @$ok = include_once( $file_url ); 
        if ( $ok != true ) 
        { 
            $this->sys_err( "文件{{$file_url}}加载失败", "die" ); 
        } 
    }      // 对config文件解码函数(将数据库用户名和密码明文纪录是不安全的,最好先加密,再在此解密,本函数可以重载) 
    protected function decode_config( $config ) 
    { 
        return $config; 
    }      // 连接数据库函数 
    private function connect() 
    { 
        switch ( strtoupper( $this->config->database ) ) 
        { 
            case "MYSQL" : 
                $this->connect_mysql(); 
                break; 
            case "ACCESS" : 
                $this->connect_access(); 
                break; 
            default : 
                $this->sys_err( "数据库类型错误,该类目前只支持MYSQL与ACCESS两种数据库", "die"); 
                break; 
        } 
    }      // 连接MYSQL数据库函数 
    private function connect_mysql() 
    { 
        if ( $this->conn != null ) 
        { 
            @mysql_close( $this->conn ); 
            $this->conn = null; 
        } 
        @$this->conn = mysql_connect( $this->config->host_name, $this->config->user_name, $this->config->password ); 
        if ( $this->conn == false ) 
        { 
            $mysql_err = mysql_error(); 
            $this->sys_err( "MYSQL数据库连接失败,原因是:{{$mysql_err}}", "die" ); 
        } 
        @$db = mysql_select_db( $this->config->db_name, $this->conn ) ; 
        if ( $db == false ) 
        { 
            $mysql_err = mysql_error(); 
            $this->sys_err( "数据表连接失败,原因是:{{$mysql_err}}", "die" ); 
        } 
    }      // 连接ACCESS数据库函数 
    private function connect_access() 
    { 
        if ( $this->conn != null ) 
        { 
            @odbc_close( $this->conn ); 
            $this->conn = null; 
        } 
        $dsn = "Driver=Microsoft Access Driver (*.mdb);dbq=" . realpath( $this->the_dir . $this->config->db_name ); 
        @$this->conn = odbc_connect( $dsn, $this->config->user_name, $this->config->password ); 
        if ( $this->conn == false ) 
        { 
            @$odbc_err = odbc_errormsg( $this->conn ); 
            $this->sys_err( "ACCESS数据库连接失败,原因是:{{$odbc_err}}", "die" ); 
        } 
    }  
123下一页阅读全文