⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smarty.class.php

📁 通达OA官方提供的30源代码,感觉很实在
💻 PHP
📖 第 1 页 / 共 5 页
字号:

/*======================================================================*\
    Function:   _read_file()
    Purpose:    read in a file from line $start for $lines.
                read the entire file if $start and $lines are null.
\*======================================================================*/
    function _read_file($filename, $start=null, $lines=null)
    {
        if (!($fd = @fopen($filename, 'r'))) {
            return false;
        }
        flock($fd, LOCK_SH);
        if ($start == null && $lines == null) {
            // read the entire file
            $contents = fread($fd, filesize($filename));
        } else {
            if ( $start > 1 ) {
                // skip the first lines before $start
                for ($loop=1; $loop < $start; $loop++) {
                    fgets($fd, 65536);
                }
            }
            if ( $lines == null ) {
                // read the rest of the file
                while (!feof($fd)) {
                    $contents .= fgets($fd, 65536);
                }
            } else {
                // read up to $lines lines
                for ($loop=0; $loop < $lines; $loop++) {
                    $contents .= fgets($fd, 65536);
                    if (feof($fd)) {
                        break;
                    }
                }
            }
        }
        fclose($fd);
        return $contents;
    }

/*======================================================================*\
    Function:   _write_file()
    Purpose:    write out a file
\*======================================================================*/
    function _write_file($filename, $contents, $create_dirs = false)
    {
        if ($create_dirs)
            $this->_create_dir_structure(dirname($filename));

        if (!($fd = @fopen($filename, 'w'))) {
            $this->_trigger_error_msg("problem writing '$filename.'");
            return false;
        }

        // flock doesn't seem to work on several windows platforms (98, NT4, NT5, ?),
        // so we'll not use it at all in windows.

        if ( strtoupper(substr(PHP_OS, 0, 3)) == 'WIN' || (flock($fd, LOCK_EX)) ) {
            fwrite( $fd, $contents );
            fclose($fd);
            chmod($filename, 0644);
        }

        return true;
    }

/*======================================================================*\
    Function: _get_auto_base
    Purpose:  Get a base name for automatic files creation
\*======================================================================*/
    function _get_auto_base($auto_base, $auto_source)
    {
        $source_md5 = md5($auto_source);

        //$res = $auto_base . '/_cached/' . $source_md5;

        $res = $auto_base . '/_cached_templates';

        return $res;
    }

/*======================================================================*\
    Function: _get_auto_filename
    Purpose:  get a concrete filename for automagically created content
\*======================================================================*/
    function _get_auto_filename($auto_base, $auto_source, $auto_id = null)
    {
		$filename = (!empty($auto_source))?ereg_replace("\\.htm","",$auto_source):uniqid("");

        $res = $this->_get_auto_base($auto_base, $auto_source) .
                '/' . $filename . '.php';

        return $res;
    }

/*======================================================================*\
    Function: _rm_auto
    Purpose: delete an automagically created file by name and id
\*======================================================================*/
    function _rm_auto($auto_base, $auto_source = null, $auto_id = null)
    {
        if (!is_dir($auto_base))
          return false;

        if (!isset($auto_source)) {
            $res = $this->_rmdir($auto_base, 0);
        } else {
            if (isset($auto_id)) {
                $tname = $this->_get_auto_filename($auto_base, $auto_source, $auto_id);
                $res = is_file($tname) && unlink( $tname);
            } else {
                $tname = $this->_get_auto_base($auto_base, $auto_source);
                $res = $this->_rmdir($tname);
            }
        }

        return $res;
    }

/*======================================================================*\
    Function: _rmdir
    Purpose: delete a dir recursively (level=0 -> keep root)
    WARNING: no security whatsoever!!
\*======================================================================*/
    function _rmdir($dirname, $level = 1)
    {
        $handle = opendir($dirname);

        while ($entry = readdir($handle)) {
            if ($entry != '.' && $entry != '..') {
                if (is_dir($dirname . '/' . $entry)) {
                    $this->_rmdir($dirname . '/' . $entry, $level + 1);
                }
                else {
                    unlink($dirname . '/' . $entry);
                }
            }
        }

        closedir($handle);

        if ($level)
            @rmdir($dirname);

        return true;
    }

/*======================================================================*\
    Function: _create_dir_structure
    Purpose:  create full directory structure
\*======================================================================*/
    function _create_dir_structure($dir)
    {
        if (!file_exists($dir)) {
            $dir_parts = preg_split('!/+!', $dir, -1, PREG_SPLIT_NO_EMPTY);
            $new_dir = ($dir{0} == '/') ? '/' : '';
            foreach ($dir_parts as $dir_part) {
                $new_dir .= $dir_part;
                if (!file_exists($new_dir) && !mkdir($new_dir, 0771)) {
                    $this->_trigger_error_msg("problem creating directory \"$dir\"");
                    return false;
                }
                $new_dir .= '/';
            }
        }
    }

/*======================================================================*\
    Function:   _write_cache_file
    Purpose:    Prepend the cache information to the cache file
                and write it
\*======================================================================*/
    function _write_cache_file($tpl_file, $cache_id, $compile_id, $results)
    {
        // put timestamp in cache header
        $this->_cache_info['timestamp'] = time();

        // prepend the cache header info into cache file
        $results = 'SMARTY_CACHE_INFO_HEADER'.serialize($this->_cache_info)."\n".$results;

        if (!empty($this->cache_handler_func)) {
            // use cache_handler function
			$funcname = $this->cache_handler_func;
            return $funcname('write', $this, $results, $tpl_file, $cache_id, $compile_id);
        } else {
            // use local cache file
            if (isset($compile_id) || isset($cache_id))
                $auto_id = $compile_id . $cache_id;
            else
                $auto_id = null;

            $cache_file = $this->_get_auto_filename($this->cache_dir, $tpl_file, $auto_id);
            $this->_write_file($cache_file, $results, true);
            return true;
        }
    }

/*======================================================================*\
    Function:   _read_cache_file
    Purpose:    read a cache file, determine if it needs to be
                regenerated or not
\*======================================================================*/
    function _read_cache_file($tpl_file, $cache_id, $compile_id, &$results)
    {
        if ($this->force_compile || $this->cache_lifetime == 0) {
            // force compile enabled or cache lifetime is zero, always regenerate
            return false;
        }

        if (!empty($this->cache_handler_func)) {

            // use cache_handler function
			$funcname = $this->cache_handler_func;
            $funcname('read', $this, $results, $tpl_file, $cache_id, $compile_id);

        } else {
            // use local file cache
            if (isset($compile_id) || isset($cache_id))
                $auto_id = $compile_id . $cache_id;
            else
                $auto_id = null;

            $cache_file = $this->_get_auto_filename($this->cache_dir, $tpl_file, $auto_id);
            $results = $this->_read_file($cache_file);

        }

		if (empty($results)) {
			// nothing to parse (error?), regenerate cache
			return false;
		}

        $cache_split = explode("\n", $results, 2);
        $cache_header = $cache_split[0];

        if (substr($cache_header, 0, 24) == 'SMARTY_CACHE_INFO_HEADER') {
            $cache_info = unserialize(substr($cache_header, 24));
            $cache_timestamp = $cache_info['timestamp'];

            if (time() - $cache_timestamp > $this->cache_lifetime) {
                // cache expired, regenerate
                return false;
            }

            if ($this->compile_check) {
                foreach ($cache_info as $curr_cache_info) {
                    switch ($curr_cache_info[0]) {
                        case 'template':
                            $this->_fetch_template_info($curr_cache_info[1], $template_source, $template_timestamp, false);
                            if ($cache_timestamp < $template_timestamp) {
                                // template file has changed, regenerate cache
                                return false;
                            }
                            break;

                        case 'config':
                            if ($cache_timestamp < filemtime($this->config_dir.'/'.$curr_cache_info[1])) {
                                // config file file has changed, regenerate cache
                                return false;
                            }
                            break;
                    }
                }
            }
            $results = $cache_split[1];
            return true;
        } else {
            // no cache info header, regenerate cache
            return false;
        }
    }


/*======================================================================*\
    Function:   quote_replace
    Purpose:    Quote subpattern references
\*======================================================================*/
    function quote_replace($string)
    {
        return preg_replace('![\\$]\d!', '\\\\\\0', $string);
    }


/*======================================================================*\
    Function: _trigger_error_msg
    Purpose:  trigger Smarty error
\*======================================================================*/
    function _trigger_error_msg($error_msg, $error_type = E_USER_WARNING)
    {
        trigger_error("Smarty error: $error_msg", $error_type);
    }

/*======================================================================*\
    Function:   _get_microtime
    Purpose:    Get seconds and microseconds
\*======================================================================*/
    function _get_microtime()
    {
        $mtime = microtime();
        $mtime = explode(" ", $mtime);
        $mtime = (double)($mtime[1]) + (double)($mtime[0]);
        return ($mtime);
    }

}

/* vim: set expandtab: */

?>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -