PHP static class
Last Update:2017-05-14
Source: Internet
Author: User
PHP static class Class Shtml
{
Var $ Templet;
Var $ DataSource;
Var $ Dir;
Var $ fileName;
Var $ mod;
Var $ handle;
Function Shtml ($ fileName = "")
{
$ This-> fileName = $ fileName;
$ This-> mod = "wb ";
$ This-> handle = false;
$ This-> Templet = "";
$ This-> DataSource = array ();
$ This-> Dir = "";
}
// Bind the data source. the parameter is an array.
Function BindData ($ arr)
{
$ This-> DataSource = $ arr;
}
// Set the file storage path
Function SetDir ($ dir)
{
$ This-> Dir = $ dir;
}
Function SetFileName ($ fileName)
{
Return $ this-> fileName = $ fileName;
}
Function GetMod ()
{
Return $ this-> mod;
}
Function SetMod ($ mod)
{
Return $ this-> mod = $ mod;
}
Function Open ()
{
If (substr ($ this-> fileName, 0, 1) = "/")
$ This-> fileName = $ _ SERVER ['document_root ']. $ this-> fileName;
If ($ this-> handle = fopen ($ this-> fileName, $ this-> mod ))
Return $ this-> handle;
Else
Return false;
}
Function Close ()
{
Return fclose ($ this-> handle );
}
Function Write ($ content)
{
Return fwrite ($ this-> handle, $ content );
}
Function MkDir ($ pathname)
{
$ CurrentPath = "";
Str_replace ("\", "/", $ pathname );
$ PathArr = split ("/", $ pathname );
If ($ pathArr [0] = "") {// use the absolute path
$ CurrentPath = $ _ SERVER ['document_root '];
} Else {
$ CurrentPath = $ _ SERVER ['document_root ']. dirname ($ _ SERVER ['php_self']);
}
For ($ I = 0; $ I <count ($ pathArr); $ I ++ ){
If ($ pathArr [$ I] = "")
Continue;
Else
If (is_dir ($ currentPath. "/". $ pathArr [$ I])
$ CurrentPath = $ currentPath. "/". $ pathArr [$ I];
Else
Mkdir ($ currentPath = $ currentPath. "/". $ pathArr [$ I]);
}
}
// Generate static files
Function Create ()
{
$ Tmp = $ this-> Templet;
Foreach ($ this-> DataSource as $ key => $ value ){
$ Tmp = str_replace ("<FIELD _". $ key. ">", $ value, $ tmp );
}
$ This-> MkDir (dirname ($ this-> fileName ));
$ This-> Open ();
$ This-> Write ($ tmp );
$ This-> Close ();
}
}
Function CreateShtml (){
Ob_start ("callback_CteateShtml ");
}
Function callback_CteateShtml ($ buffer ){
$ Page = intval (@ $ _ REQUEST ["page"]);
$ Shtml = new Shtml ();
$ Shtml-> SetFileName ($ _ SERVER ['document_root ']. dirname ($ _ SERVER ['php_self ']). "/". basename ($ _ SERVER ['php_self '], ". php "). ($ page = 0? "": "_". Strval ($ page). ". html ");
$ Shtml-> Templet = $ buffer;
$ Shtml-> Create ();
Return $ buffer;
}
?>