From: <ma...@us...> - 2002-06-26 12:07:08
|
Update of /cvsroot/nasm/www/include In directory usw-pr-cvs1:/tmp/cvs-serv9716/include Added Files: DB.class.php XTemplate.class.php functions.inc.php Log Message: Added new functionality --- NEW FILE: DB.class.php --- <? /// // Database access class - MySQL version // // $Id: DB.class.php,v 1.1 2002/06/26 12:07:05 madfire Exp $ // // Copyright (c) 2002 Stanislav Karchebny <be...@ma...> /// class DB { //private: var $db; var $connection; //public: function DB ($database = "nasm", $username = "nasm", $password = "nasmrulez") { $this->connection = mysql_connect("localhost", $username, $password) or die("Could not connect to MySQL server"); $this->db = mysql_select_db($database, $this->connection) or die("Could not select database $database"); } function close () { mysql_close($this->connection); } // return results of SQL query function query ($sql) { $sql_result = mysql_query($sql, $this->connection) or die("Error in query \"$sql\""); if($row = mysql_fetch_array($sql_result)) { $out = $row; } else { $out = NULL; } mysql_free_result($sql_result); return $out; } // return multiple results of SQL query function query_array ($sql) { $sql_result = mysql_query($sql, $this->connection) or die("Error in query \"$sql\""); $out = array(); while($row = mysql_fetch_array($sql_result)) { $out[] = $row; } mysql_free_result($sql_result); return $out; } // return plain MySQL result function query_result ($sql, $row, $field) { $sql_result = mysql_query($sql, $this->connection) or die("Error in query \"$sql\""); $data = mysql_result($sql_result, $row, $field); mysql_free_result($sql_result); return $data; } // return plain MySQL result handle function query_raw ($sql) { $sql_result = mysql_query($sql, $this->connection) or die("Error in query \"$sql\""); return $sql_result; } // return id of last insert function insert_id ($result_id = 0) { return mysql_insert_id($this->connection); } }; ?> --- NEW FILE: XTemplate.class.php --- <? class XTemplate { /* xtemplate class 0.2.4-3 html generation with templates - fast & easy copyright (c) 2000 barnabás debreceni [cr...@us...] code optimization by Ivar Smolin <ok...@li...> 14-march-2001 latest stable & CVS version always available @ http://sourceforge.net/projects/xtpl code changes by Stanislav Karchebny <be...@ma...>: - introduced context switching, - added include files search in open file directory, - added preg_quote to block matching pattern, - tested with php 4.1.1 win32. - added gzip compression of output - added iconv output conversion tested with php 3.0.11 and 4.0.4pl1 This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details at http://www.gnu.org/copyleft/lgpl.html You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. $Id: XTemplate.class.php,v 1.1 2002/06/26 12:07:05 madfire Exp $ */ /***[ variables ]***********************************************************/ var $filecontents=""; /* raw contents of template file */ var $blocks=array(); /* unparsed blocks */ var $parsed_blocks=array(); /* parsed blocks */ var $block_parse_order=array(); /* block parsing order for recursive parsing (sometimes reverse:) */ var $sub_blocks=array(); /* store sub-block names for fast resetting */ var $VARS=array(); /* variables array */ var $file_delim="/\{FILE\s*\"([^\"]+)\"\s*\}/m"; /* regexp for file includes */ var $block_start_delim="<"; /* block start delimiter */ var $block_end_delim=">"; /* block end delimiter */ var $block_start_word="tmpl:"; /* block start word */ var $block_end_word="/tmpl:"; /* block end word */ /* this makes the delimiters look like: <tmpl:block_name> if you use my syntax. */ var $NULL_STRING=array(""=>""); /* null string for unassigned vars */ var $NULL_BLOCK=array(""=>""); /* null string for unassigned blocks */ var $mainblock=""; var $ERROR=""; var $AUTORESET=1; /* auto-reset sub blocks */ var $context = ""; /* current block context */ var $ctx_delim = "/"; /* context path delimiter, also root context character */ var $iconv_from = "iso-8859-1"; /* iconv conversion: input format and */ var $iconv_to = "iso-8859-1"; /* output format */ var $make_me_pretty = 1; /* make more prettier HTML output at the expense of speed (very rough!) */ /* if you're in need of faster output, better turn this off */ /***[ constructor ]*********************************************************/ function XTemplate ($file,$mainblock="main") { $this->mainblock=$mainblock; $this->filecontents=$this->r_getfile($file); /* read in template file */ $this->blocks=$this->maketree($this->filecontents,$mainblock); /* preprocess some stuff */ $this->scan_globals(); ob_start("ob_gzhandler"); } /***************************************************************************/ /***[ public stuff ]********************************************************/ /***************************************************************************/ /***[ set_iconv ]***********************************************************/ /* Set conversion information. */ function set_iconv($from, $to) { $this->iconv_from = $from; $this->iconv_to = $to; } /***[ context ]*************************************************************/ /* return current context and possibly switch current context (if $newctx is not empty) */ function context ($newctx = "") { $ctx = $this->context; if( $newctx ) { if( $newctx === $this->ctx_delim ) $this->context = ""; else $this->context = str_replace($this->ctx_delim,".",$newctx)."."; } return $ctx; } /***[ assign ]**************************************************************/ /* assign a variable */ function assign ($name,$val="") { if (gettype($name)=="array") while (list($k,$v)=each($name)) $this->VARS[$k]=$v; else $this->VARS[$name]=$val; } /***[ parse ]***************************************************************/ /* parse a block */ function parse ($bname) { $bname = $this->context.$bname; // switch to current context $copy=$this->blocks[$bname]; if (!isset($this->blocks[$bname])) $this->set_error ("parse: blockname [$bname] does not exist"); preg_match_all("/\{([A-Za-z0-9\._]+?)}/",$this->blocks[$bname],$var_array); $var_array=$var_array[1]; while (list($k,$v)=each($var_array)) { $sub=explode(".",$v); if ($sub[0]=="_BLOCK_") { unset($sub[0]); $bname2=implode(".",$sub); $var=$this->parsed_blocks[$bname2]; $nul=(!isset($this->NULL_BLOCK[$bname2])) ? $this->NULL_BLOCK[""] : $this->NULL_BLOCK[$bname2]; $var=(empty($var))?$nul:trim($var); $copy=str_replace("{".$v."}","$var",$copy); } else { $var=$this->VARS; while(list($k1,$v1)=each($sub)) $var=$var[$v1]; $nul=(!isset($this->NULL_STRING[$v])) ? ($this->NULL_STRING[""]) : ($this->NULL_STRING[$v]); $var=(!isset($var))?$nul:$var; $copy=str_replace("{".$v."}","$var",$copy); } } $this->parsed_blocks[$bname].=$copy; // reset sub-blocks if ($this->AUTORESET && (!empty($this->sub_blocks[$bname]))) { reset($this->sub_blocks[$bname]); while (list($k,$v)=each($this->sub_blocks[$bname])) $this->reset($v); } } /***[ rparse ]**************************************************************/ /* returns the parsed text for a block, including all sub-blocks. */ function rparse($bname) { if (!empty($this->sub_blocks[$bname])) { reset($this->sub_blocks[$bname]); while (list($k,$v)=each($this->sub_blocks[$bname])) if (!empty($v)) $this->rparse($v,$indent."\t"); } $this->parse($bname); } /***[ insert ]**************************************************************/ /* inserts a loop ( call assign & parse ) */ function insert($bname,$var,$value="") { $this->assign($var,$value); $this->parse($bname); } /***[ text ]****************************************************************/ /* returns the parsed text for a block */ function text($bname) { return $this->parsed_blocks[isset($bname) ? $bname :$this->mainblock]; } /***[ out ]*****************************************************************/ /* prints the parsed text */ function out ($bname) { $output = $this->text($bname); if( $this->make_me_pretty ) $output = trim(preg_replace("/(\r\n|\n)+/","\r\n",$output)); // strip down to single \r\n if( $this->iconv_from != $this->iconv_to ) $output = iconv($this->iconv_from, $this->iconv_to, $output); echo $output; header("Expires: 0"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 header("Pragma: no-cache"); ob_end_flush(); } /***[ reset ]***************************************************************/ /* resets the parsed text */ function reset ($bname) { $this->parsed_blocks[$bname]=""; } /***[ parsed ]**************************************************************/ /* returns true if block was parsed, false if not */ function parsed ($bname) { return (!empty($this->parsed_blocks[$bname])); } /***[ SetNullString ]*******************************************************/ /* sets the string to replace in case the var was not assigned */ function SetNullString($str,$varname="") { $this->NULL_STRING[$varname]=$str; } /***[ SetNullBlock ]********************************************************/ /* sets the string to replace in case the block was not parsed */ function SetNullBlock($str,$bname="") { $this->NULL_BLOCK[$bname]=$str; } /***[ set_autoreset ]*******************************************************/ /* sets AUTORESET to 1. (default is 1) if set to 1, parse() automatically resets the parsed blocks' sub blocks (for multiple level blocks) */ function set_autoreset() { $this->AUTORESET=1; } /***[ clear_autoreset ]*****************************************************/ /* sets AUTORESET to 0. (default is 1) if set to 1, parse() automatically resets the parsed blocks' sub blocks (for multiple level blocks) */ function clear_autoreset() { $this->AUTORESET=0; } /***[ scan_globals ]********************************************************/ /* scans global variables */ function scan_globals() { reset($GLOBALS); while (list($k,$v)=each($GLOBALS)) $GLOB[$k]=$v; $this->assign("PHP",$GLOB); /* access global variables as {PHP.HTTP_HOST} in your template! */ } /****** WARNING PUBLIC FUNCTIONS BELOW THIS LINE DIDN'T GET TESTED ******/ /***************************************************************************/ /***[ private stuff ]*******************************************************/ /***************************************************************************/ /***[ maketree ]************************************************************/ /* generates the array containing to-be-parsed stuff: $blocks["main"],$blocks["main.table"],$blocks["main.table.row"], etc. also builds the reverse parse order. */ function maketree($con,$block) { $con2=explode($this->block_start_delim,$con); $level=0; $block_names=array(); $blocks=array(); reset($con2); while(list($k,$v)=each($con2)) { $patt="(".preg_quote($this->block_start_word,"/")."|".preg_quote($this->block_end_word,"/").")\s*(\w+)\s*$this->block_end_delim(.*)"; if (preg_match_all("/$patt/ims",$v,$res, PREG_SET_ORDER)) { // $res[0][1] = BEGIN or END // $res[0][2] = block name // $res[0][3] = kinda content if ($res[0][1]==$this->block_start_word) { $parent_name=implode(".",$block_names); $block_names[++$level]=$res[0][2]; /* add one level - array("main","table","row")*/ $cur_block_name=implode(".",$block_names); /* make block name (main.table.row) */ $this->block_parse_order[]=$cur_block_name; /* build block parsing order (reverse) */ $blocks[$cur_block_name].=$res[0][3]; /* add contents */ $blocks[$parent_name].="{_BLOCK_.$cur_block_name}"; /* add {_BLOCK_.blockname} string to parent block */ $this->sub_blocks[$parent_name][]=$cur_block_name; /* store sub block names for autoresetting and recursive parsing */ $this->sub_blocks[$cur_block_name][]=""; /* store sub block names for autoresetting */ } else if ($res[0][1]==$this->block_end_word) { unset($block_names[$level--]); $parent_name=implode(".",$block_names); $blocks[$parent_name].=$res[0][3]; /* add rest of block to parent block */ } } else { /* no block delimiters found */ $blocks[implode(".",$block_names)].=$this->block_start_delim.$v; } } return $blocks; } /***[ error stuff ]*********************************************************/ /* sets and gets error */ function get_error() { return ($this->ERROR=="")?0:$this->ERROR; } function set_error($str) { $this->ERROR=$str; } /***[ getfile ]*************************************************************/ /* returns the contents of a file */ function getfile($file) { if (!isset($file)) { $this->set_error("!isset file name!"); return ""; } if (is_file($file)) { if (!($fh=fopen($file,"r"))) { $this->set_error("Cannot open file: $file"); return ""; } $file_text=fread($fh,filesize($file)); fclose($fh); } else { $this->set_error("[$file] does not exist"); $file_text="<b>__XTemplate fatal error: file [$file] does not exist__</b>"; } return $file_text; } /***[ r_getfile ]***********************************************************/ /* recursively gets the content of a file with {FILE "filename.tpl"} directives */ function r_getfile($file) { if( ($pos = strrpos($file, "/")) === false ) $path = ""; else $path = substr($file, 0, $pos+1); $text=$this->getfile($file); while (preg_match($this->file_delim,$text,$res)) { $text2=$this->getfile($path.$res[1]); $text=str_replace( $res[0], $text2, $text ); } return $text; } } /* end of XTemplate class. */ /* $Log: XTemplate.class.php,v $ Revision 1.1 2002/06/26 12:07:05 madfire Added new functionality Revision 1.4 2001/03/26 23:25:02 cranx added keyword expansion to be more clear Revision 1.3 2001/03/26 23:14:56 cranx *** empty log message *** */ ?> --- NEW FILE: functions.inc.php --- <? //======================================================================================= // // FUNCTIONS LIBRARY // //======================================================================================= function email_conv( $addr ) { return str_replace( "@", " at ", str_replace( ".", " dot ", $addr ) ); } function menu_item($item, $url = "", $width = 0, $height = 0) { if( $url == "" ) $url = "$item.php"; if( $width == 0 ) $width = 160; if( $height == 0 ) $height = 24; ?> <tr><td><? if($item == "-") { // separator ?><img src="images/spacer.gif" width="<?=$width?>" height="<?=$height?>" border="0" alt=""><? } else { ?><a href="<?=$url?>"><img name="menu_<?=$item?>" src="images/menu_<?=$item?>.gif" width="<?=$width?>" height="<?=$height?>" border="0" alt="<?=$item?>" onmouseover="active('menu_<?=$item?>')" onmouseout="inactive('menu_<?=$item?>')"></a><? } ?></td></tr> <? } // return true if argument is not numeric function not_numeric($x) { return !ereg("^([\+\-]{0,1}[0-9.]+([Ee][0-9]*){0,1}){1}$", $x); } function notset($var) { return !isset($var) || $var == ""; } function redirect( $newurl ) { ob_end_clean(); exit(header("Location: $newurl")); } ?> |