" . mysql_error()); // Select database mysql_select_db(DB_DATABASE, $db_conn) or die("Database error
" . mysql_error()); ?> 14-march-2001 latest stable & CVS version always available @ http://sourceforge.net/projects/xtpl 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: xtpl.p,v 1.4 2001/03/26 23:25:02 cranx 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 end delimiter */ var $block_start_word="BEGIN:"; /* block start word */ var $block_end_word="END:"; /* block end word */ /* this makes the delimiters look like: 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 */ /***[ 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(); } /***************************************************************************/ /***[ public stuff ]********************************************************/ /***************************************************************************/ /***[ 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) { $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=preg_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=preg_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_loop ]*********************************************************/ /* inserts a loop ( call assign & parse ) */ function insert_loop($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) { echo $this->text($bname); } /***[ outfile ]*****************************************************************/ /* prints the parsed text to given file Added by Jeroen Vreuls 29/01/2003 */ function outfile ($bname, $filename) { $outf = fopen($filename, 'w'); if (!$outf) return false; flock($outf, LOCK_EX); if (!fwrite($outf, $this->text($bname))) return false; flock($outf, LOCK_UN); fclose($outf); return true; } /***[ 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="($this->block_start_word|$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="__XTemplate fatal error: file [$file] does not exist__"; } return $file_text; } /***[ r_getfile ]***********************************************************/ /* recursively gets the content of a file with {FILE "filename.tpl"} directives */ function r_getfile($file) { $text=$this->getfile($file); while (preg_match($this->file_delim,$text,$res)) { $text2=$this->getfile($res[1]); $text=preg_replace("'".preg_quote($res[0])."'",$text2,$text); } return $text; } } /* end of XTemplate class. */ /* $Log: xtpl.p,v $ 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 *** */ ?>" . mysql_error()); if ($row = mysql_fetch_array($res)) { // login user in! return true; } // if else { return false; } // else } // function function navstring($pagID, $seperator, $str = "") { $qry = "SELECT parentPagID, pagName FROM " . PAGE_TABLE . " WHERE pagID = '" . $pagID . "'"; $result = mysql_query($qry) or die("Database error
" . mysql_error()); if ($row = mysql_fetch_array($result)) { $parentPagID = $row['parentPagID']; if ($parentPagID == 0 && stripslashes($row['pagName']) != "home") { $str .= "home > " . stripslashes($row['pagName']) . ""; } // if elseif(stripslashes($row['pagName']) == "home") { $str .= "home > "; } else { $str .= navstring($parentPagID, $seperator, $str) . " " . $seperator . " " . stripslashes($row['pagName']) . ""; } // else return $str; } // if else { return ""; } // else } // function function security($pagID) { $qry = "SELECT pagSecurity FROM " . PAGE_TABLE . " WHERE pagID = '" . $pagID . "'"; $res = mysql_query($qry) or die("Database error
" . mysql_error()); if ($row = mysql_fetch_array($res)) { return $row['pagSecurity']; } // if else { return 0; } // else } // function function getParentPagID($pagID) { $qry = "SELECT parentPagID FROM " . PAGE_TABLE . " WHERE pagID = '$pagID'"; $res = mysql_query($qry) or die("Database error
" . mysql_error()); if ($row = mysql_fetch_array($res)) { return $row['parentPagID']; } // if else { return 1; } // else } // function function getParentParentPagID($pagID) { $qry = "SELECT parentPagID FROM " . PAGE_TABLE . " WHERE pagID = '$pagID'"; $res = mysql_query($qry) or die("Database error
" . mysql_error()); if ($row = mysql_fetch_array($res)) { $qry2 = "SELECT parentPagID FROM " . PAGE_TABLE . " WHERE pagID = '". $row['parentPagID'] . "'"; $res2 = mysql_query($qry2) or die("Database error
" . mysql_error()); if ($row2 = mysql_fetch_array($res2)) { return $row2['parentPagID']; } } // if else { return 1; } // else } // function function getProperty($cfgName, $useSitID) { $qry = "SELECT cfgValue FROM " . CONFIG_TABLE . " WHERE cfgName = '$cfgName' AND sitID = '" . $useSitID . "'"; $res = mysql_query($qry) or die("Database error
" . mysql_error()); if ($row = mysql_fetch_array($res)) { return $row['cfgValue']; } // if else { return false; } } // function function getSitID($id) { $qry = "SELECT sitID FROM " . PAGE_TABLE . " WHERE pagID = '" . $id . "'"; $res = mysql_query($qry); if ($row = mysql_fetch_array($res)) { return $row['sitID']; } return false; } // function /** * @param id The ID of the page * @return -1 if page could not be found * 0 if page is a level 0 page (has no parent page) * 1 if page is a level 1 page * 2 if page is a level 2 (or greater) page */ function getPageLevel($id) { $qry = "SELECT P.parentPagID FROM " . PAGE_TABLE . " P WHERE P.pagID = '" . $id . "'"; $res = mysql_query($qry) or die("Database error
" . mysql_error()); if ($row = mysql_fetch_array($res)) { if ($row['parentPagID'] == 0) { return 0; } else { $qry = "SELECT P2.parentPagID FROM " . PAGE_TABLE . " P, " . PAGE_TABLE . " P2 WHERE P.pagID = '" . $id . "' AND P.parentPagID = P2.pagID"; $res = mysql_query($qry) or die("Database error
" . mysql_error()); if ($row = mysql_fetch_array($res)) { if ($row['parentPagID'] == 0) { return 1; } else { return 2; } } } } return -1; } // function function insertGuest($pagID, $guestName, $guestEmail, $guestContent, $ip, $date, $time) { $qry = "INSERT INTO " . GUESTBOOK_TABLE . " (pagID, guestName, guestEmail, guestContent, guestIP, guestDate, guestTime) VALUES ('$pagID', '$guestName', '$guestEmail', '$guestContent', '$ip', '$date', '$time')"; $res = mysql_query($qry) or die("Database error
" . mysql_error()); if (!$res) return FALSE; else return mysql_insert_id(); } // function function insertAccount($login, $pass, $email, $cv) { $qry = "INSERT INTO " . MEMBERS_TABLE . " (memLogin, memPassword, memEmail, memActive, memCV) VALUES ('$login', '$pass', '$email',0, '$cv')"; $res = mysql_query($qry) or die("Database error
" . mysql_error()); if (!$res) return FALSE; else return mysql_insert_id(); } // function function updateMember($memID, $memLogin, $memPassword, $memFirstname, $memLastname, $memAdres, $memHuisnr, $memPostcode, $memPlaats, $memTel, $memEmail, $memOpm) { $qry = "UPDATE " . MEMBERS_TABLE . " SET memLogin = '$memLogin', "; if ($memPassword !== 0) { $qry .= " memPassword = '$memPassword', "; } // if $qry .= " memFirstname = '$memFirstname', memLastname = '$memLastname', memAdres = '$memAdres', memHuisnr = '$memHuisnr', memPostcode = '$memPostcode', memPlaats = '$memPlaats', memTel = '$memTel', memEmail = '$memEmail', memOpm = '$memOpm' WHERE memID = '$memID'"; $res = mysql_query($qry) or die("Database error1
" . mysql_error()); return $res; } // function function updatePass($memID, $memPassword) { $qry = "UPDATE " . MEMBERS_TABLE . " SET memPassword = '$memPassword' WHERE memID = '$memID'"; $res = mysql_query($qry) or die("Database error1
" . mysql_error()); return $res; } // function function activateMember($email) { $qry = "UPDATE " . MEMBERS_TABLE . " SET memActive = 1 WHERE memEmail = '$email'"; $res = mysql_query($qry) or die("Database error2
" . mysql_error()); return $res; } // function ?>pagID = $pagID; // get page security $sql = "SELECT pagSecurity FROM " . PAGE_TABLE . " WHERE pagID = '" . $this->pagID . "'"; $res = mysql_query($sql) or die("Database error
" . mysql_error()); if ($row = mysql_fetch_array($res)) { if ($row['pagSecurity'] == 1) { $this->pagSecurity = true; } } mysql_free_result($res); // get page groups $sql = "SELECT grpID FROM " . PAGGRP_TABLE . " WHERE pagID = '" . $this->pagID . "'"; $res = mysql_query($sql) or die("Database error
" . mysql_error()); while ($row = mysql_fetch_array($res)) { $this->pagGrpIDs[] = $row['grpID']; } mysql_free_result($res); } /** * Return wether a page is secured or not. */ function getPageSecurity() { return $this->pagSecurity; } /** * Tries to logn the user in */ function login() { // hier moet login afgehandeld worden if (!$this->loggedOut) { if (is_array($_POST) && isset($_POST['memLogin']) && isset($_POST['memPassword'])) { if ($this->_isValidUser($_POST['memLogin'], $_POST['memPassword'])) { // login ok // set the session data $_SESSION['memLogin'] = $_POST['memLogin']; $_SESSION['memPassword'] = md5($_POST['memPassword']); $_SESSION['memID'] = $this->memID; // set the cookie for one year header( "refresh:0;url=/". $_REQUEST['name']."/logged/true"); // cookies aan/uit zetten // setcookie('site[u]', $_POST['memLogin'], time() + 3600 * 24 * 365, '/'); // setcookie('site[p]', md5($_POST['memPassword']), time() + 3600 * 24 * 365, '/'); $this->logged = true; } else { // login failed header( "refresh:0;url=/". $_REQUEST['name']."/logged/false"); $this->logged = false; } } elseif (isset($_SESSION['memLogin']) && isset($_SESSION['memPassword'])) { if ($this->_isValidUser($_SESSION['memLogin'], $_SESSION['memPassword'], true)) { // session data are correct $this->logged = true; } else { // session date are corrupt $this->logged = false; } } elseif (isset($_COOKIE['site']['u']) && isset($_COOKIE['site']['p'])) { if ($this->_isValidUser($_COOKIE['site']['u'], $_COOKIE['site']['p'], true)) { $_SESSION['memLogin'] = $_COOKIE['site']['u']; $_SESSION['memPassword'] = $_COOKIE['site']['p']; $_SESSION['memID'] = $this->memID; $this->logged = true; } else { $this->logged = false; } } } return $this->logged; } /** * Determines wether the user has access to this specific page or not * @return boolean True is the user has access. */ function accessPage($pagID = null) { if ($pagID != null) { $sql = "SELECT P.pagSecurity, PG.grpID FROM " . PAGGRP_TABLE . " PG, " . PAGE_TABLE . " P WHERE P.pagID = '" . $pagID . "' AND PG.pagID = P.pagID"; $res = mysql_query($sql) or die("Database error
" . mysql_error()); $security = false; $grpIDs = array(); while ($row = mysql_fetch_array($res)) { $security = $row['pagSecurity'] == 1 ? true : false; $grpIDs[] = $row['grpID']; } mysql_free_result($res); if ($security) { return sizeof(array_intersect($grpIDs, $this->memGrpIDs)) > 0; } else { return true; } } else { if (sizeof(array_intersect($this->pagGrpIDs, $this->memGrpIDs)) > 0) { return $this->logged; } return false; } } /** * Tries to log the user out. */ function logout() { // end session session_unset(); session_destroy(); session_start(); header( "refresh:0;url=/"); // remove cookie by setting expiration date one hour ago setcookie('site[u]', '', time() - 3600, '/'); setcookie('site[p]', '', time() - 3600, '/'); } /** * Returns the groups that may access the page * @return Array with membergroup-ID's */ function getPageGroups() { return $this->pagGrpIDs; } /** * Returns the groups of the logged member * @return Array with membergroup-ID's */ function getMemberGroups() { return $this->memGrpIDs; } /** * Return wether the user is logged in or not * @return boolean */ function isLogged() { return $this->logged; } /** * Checks if the user is registered. * @param login The login name * @param password The password * @param isMD5 Wether the password is already encrypted or not * @return boolean If the user is valid */ function _isValidUser($login, $password, $isMD5 = false) { $memLogin = $login; $memPassword = $isMD5 ? $password : md5($password); $valid = false; $sql = "SELECT memID FROM " . MEMBERS_TABLE . " WHERE memLogin = '" . $memLogin . "' AND memPassword = '" . $memPassword . "'"; $res = mysql_query($sql) or die("Database error
" . mysql_error()); if ($row = mysql_fetch_array($res)) { $this->memID = $row['memID']; $valid = true; } mysql_free_result($res); // MemberGroups $sql = "SELECT grpID FROM " . MEMGRP_TABLE . " WHERE memID = '" . $this->memID . "'"; $res = mysql_query($sql) or die("Database error
" . mysql_error()); while ($row = mysql_fetch_array($res)) { $this->memGrpIDs[] = $row['grpID']; } mysql_free_result($res); return $valid; } } ?>