PHP class for generating static pages
The code is as follows:
Class html
{
Var $ dir; // dir for the htmls (/)
Var $ rootdir; // root of html files (without/): html
Var $ name; // html file storage path
Var $ dirname; // The specified folder name.
Var $ url; // The Source webpage address for retrieving html file information
Var $ time; // time When html file information is added
Var $ dirtype; // Directory storage method: year, month ,,,,
Var $ nametype; // name of the html file: name
Function html ($ nametype = 'name', $ dirtype = 'year', $ rootdir = 'html ')
{
$ This-> setvar ($ nametype, $ dirtype, $ rootdir );
}
Function setvar ($ nametype = 'name', $ dirtype = 'year', $ rootdir = 'HTML ')
{
$ This-> rootdir = $ rootdir;
$ This-> dirtype = $ dirtype;
$ This-> nametype = $ nametype;
}
Function createdir ($ dir = '')
{
$ This-> dir = $ dir? $ Dir: $ this-> dir;
If (! Is_dir ($ this-> dir ))
{
$ Temp = explode ('/', $ this-> dir );
$ Cur_dir = '';
For ($ I = 0; $ I {
$ Cur_dir. = $ temp [$ I]. '/';
If (! Is_dir ($ cur_dir ))
{
@ Mkdir ($ cur_dir, 0777 );
}
}
}
}
Function getdir ($ dirname = '', $ time = 0)
{
$ This-> time = $ time? $ Time: $ this-> time;
$ This-> dirname = $ dirname? $ Dirname: $ this-> dirname;
Switch ($ this-> dirtype)
{
Case 'name ':
If (empty ($ this-> dirname ))
$ This-> dir = $ this-> rootdir;
Else
$ This-> dir = $ this-> rootdir. '/'. $ this-> dirname;
Break;
Case 'year ':
$ This-> dir = $ this-> rootdir. '/'. date ("Y", $ this-> time );
Break;
Case 'month ':
$ This-> dir = $ this-> rootdir. '/'. date ("Y-m", $ this-> time );
Break;
Case 'day ':
$ This-> dir = $ this-> rootdir. '/'. date ("Y-m-d", $ this-> time );
Break;
}
$ This-> createdir ();
Return $ this-> dir;
}
Function geturlname ($ url = '')
{
$ This-> url = $ url? $ Url: $ this-> url;
$ Filename = basename ($ this-> url );
$ Filename = explode (".", $ filename );
Return $ filename [0];
}
Function geturlquery ($ url = '')
{
$ This-> url = $ url? $ Url: $ this-> url;
$ Durl = parse_url ($ this-> url );
$ Durl = explode ("&", $ durl [query]);
Foreach ($ durl as $ surl)
{
$ Gurl = explode ("=", $ surl );
$ Eurl [] = $ gurl [1];
}
Return join ("_", $ eurl );
}
Function getname ($ url = '', $ time = 0, $ dirname = '')
{
$ This-> url = $ url? $ Url: $ this-> url;
$ This-> dirname = $ dirname? $ Dirname: $ this-> dirname;
$ This-> time = $ time? $ Time: $ this-> time;
$ This-> getdir ();
Switch ($ this-> nametype)
{
Case 'name ':
$ Filename = $ this-> geturlname().'.htm ';
$ This-> name = $ this-> dir. '/'. $ filename;
Break;
Case 'Time ':
$ This-> name = $ this-> dir. '/'. $ this-> time.'.htm ';
Break;
Case 'query ':
$ This-> name = $ this-> dir. '/'. $ this-> geturlquery().'.htm ';
Break;
Case 'namequery ':
$ This-> name = $ this-> dir. '/'. $ this-> geturlname (). '-'. $ this-> geturlquery().'.htm ';
Break;
Case 'nametime ':
$ This-> name = $ this-> dir. '/'. $ this-> geturlname (). '-'. $ this-> time.'.htm ';
Break;
}
Return $ this-> name;
}
Function createhtml ($ url = '', $ time = 0, $ dirname ='', $ htmlname = '')
{
$ This-> url = $ url? $ Url: $ this-> url;
$ This-> dirname = $ dirname? $ Dirname: $ this-> dirname;
$ This-> time = $ time? $ Time: $ this-> time;
// Make sure that the variable is assigned to the class member without repeated modification.
If (empty ($ htmlname ))
$ This-> getname ();
Else
$ This-> name = $ dirname. '/'. $ htmlname; // Get the name
$ Content = file ($ this-> url) or die ("Failed to open the url". $ this-> url ."! ");;
//// // Key step --- Use file to read $ this-> url
$ Content = join ("", $ content );
$ Fp = @ fopen ($ this-> name, "w") or die ("Failed to open the file". $ this-> name ."! ");
If (@ fwrite ($ fp, $ content ))
Return true;
Else
Return false;
Fclose ($ fp );
}
/// // Generate html with name
Function deletehtml ($ url = '', $ time = 0, $ dirname = '')
{
$ This-> url = $ url? $ Url: $ this-> url;
$ This-> time = $ time? $ Time: $ this-> time;
$ This-> getname ();
If (@ unlink ($ this-> name ))
Return true;
Else
Return false;
}
/**
* Function: deletedir ()
* Delete a directory
* @ Param $ file directory name (/)
* @ Return
*/
Function deletedir ($ file)
{
If (file_exists ($ file ))
{
If (is_dir ($ file ))
{
$ Handle = opendir ($ file );
While (false! ==( $ Filename = readdir ($ handle )))
{
If ($ filename! = "." & $ Filename! = "..")
$ This-> deletedir ($ file. "/". $ filename );
}
Closedir ($ handle );
Rmdir ($ file );
Return true;
} Else {
Unlink ($ file );
}
}
}
}
?>