query($query); } function link_id() { return $this->Link_ID; } function query_id() { return $this->Query_ID; } function connect($Database = "", $Host = "", $User = "", $Password = "") { if ("" == $Database) $Database = $this->Database; if ("" == $Host) $Host = $this->Host; if ("" == $User) $User = $this->User; if ("" == $Password) $Password = $this->Password; if ( 0 == $this->Link_ID ) { $this->Link_ID=mysql_pconnect($Host, $User, $Password); if (!$this->Link_ID) { $this->halt("pconnect($Host, $User, \$Password) failed."); return 0; } if (!@mysql_select_db($Database,$this->Link_ID)) { $this->halt("cannot use database ".$this->Database); return 0; } } return $this->Link_ID; } function free() { @mysql_free_result($this->Query_ID); $this->Query_ID = 0; } function query($Query_String) { if ($Query_String == "") return 0; if (!$this->connect()) { return 0; }; // New query, discard previous result. if ($this->Query_ID) { $this->free(); } if ($this->Debug) printf("Debug: query = %s
\n", $Query_String); $this->Query_ID = @mysql_query($Query_String,$this->Link_ID); $this->Row = 0; $this->Errno = mysql_errno(); $this->Error = mysql_error(); if (!$this->Query_ID) { $this->halt("Invalid SQL: ".$Query_String); } // Will return nada if it fails. That's fine. return $this->Query_ID; } function query_get_id($Query_String) { if ($Query_String == "") return 0; if (!$this->connect()) { return 0; }; // New query, discard previous result. if ($this->Query_ID) { $this->free(); } if ($this->Debug) printf("Debug: query = %s
\n", $Query_String); $this->Query_ID = @mysql_query($Query_String,$this->Link_ID); $this->Row = 0; $this->Errno = mysql_errno(); $this->Error = mysql_error(); if (!$this->Query_ID) { $this->halt("Invalid SQL: ".$Query_String); } // Will return nada if it fails. That's fine. return mysql_insert_id(); } function insert_id() { $insert_id = @mysql_insert_id($this->Link_ID); return $insert_id; } function next_record() { if (!$this->Query_ID) { $this->halt("next_record called with no query pending."); return 0; } $this->Record = @mysql_fetch_array($this->Query_ID); $this->Row += 1; $this->Errno = mysql_errno(); $this->Error = mysql_error(); $stat = is_array($this->Record); if (!$stat && $this->Auto_Free) { $this->free(); } return $stat; } function seek($pos = 0) { $status = @mysql_data_seek($this->Query_ID, $pos); if ($status) $this->Row = $pos; else { $this->halt("seek($pos) failed: result has ".$this->num_rows()." rows"); @mysql_data_seek($this->Query_ID, $this->num_rows()); $this->Row = $this->num_rows; return 0; } return 1; } function lock($table, $mode="write") { $this->connect(); $query="lock tables "; if (is_array($table)) { while (list($key,$value)=each($table)) { if ($key=="read" && $key!=0) { $query.="$value read, "; } else { $query.="$value $mode, "; } } $query=substr($query,0,-2); } else { $query.="$table $mode"; } $res = @mysql_query($query, $this->Link_ID); if (!$res) { $this->halt("lock($table, $mode) failed."); return 0; } return $res; } function unlock() { $this->connect(); $res = @mysql_query("unlock tables", $this->Link_ID); if (!$res) { $this->halt("unlock() failed."); return 0; } return $res; } function affected_rows() { return @mysql_affected_rows($this->Link_ID); } function num_rows() { return @mysql_num_rows($this->Query_ID); } function num_fields() { return @mysql_num_fields($this->Query_ID); } function nf() { return $this->num_rows(); } function np() { print $this->num_rows(); } function f($Name) { return $this->Record[$Name]; } function p($Name) { print $this->Record[$Name]; } function nextid($seq_name) { $this->connect(); if ($this->lock($this->Seq_Table)) { $q = sprintf("select nextid from %s where seq_name = '%s'", $this->Seq_Table, $seq_name); $id = @mysql_query($q, $this->Link_ID); $res = @mysql_fetch_array($id); if (!is_array($res)) { $currentid = 0; $q = sprintf("insert into %s values('%s', %s)", $this->Seq_Table, $seq_name, $currentid); $id = @mysql_query($q, $this->Link_ID); } else { $currentid = $res["nextid"]; } $nextid = $currentid + 1; $q = sprintf("update %s set nextid = '%s' where seq_name = '%s'", $this->Seq_Table, $nextid, $seq_name); $id = @mysql_query($q, $this->Link_ID); $this->unlock(); } else { $this->halt("cannot lock ".$this->Seq_Table." - has it been created?"); return 0; } return $nextid; } function metadata($table='',$full=false) { $count = 0; $id = 0; $res = array(); if ($table) { $this->connect(); $id = @mysql_list_fields($this->Database, $table); if (!$id) $this->halt("Metadata query failed."); } else { $id = $this->Query_ID; if (!$id) $this->halt("No query specified."); } $count = @mysql_num_fields($id); if (!$full) { for ($i=0; $i<$count; $i++) { $res[$i]["table"] = @mysql_field_table ($id, $i); $res[$i]["name"] = @mysql_field_name ($id, $i); $res[$i]["type"] = @mysql_field_type ($id, $i); $res[$i]["len"] = @mysql_field_len ($id, $i); $res[$i]["flags"] = @mysql_field_flags ($id, $i); } } else { $res["num_fields"]= $count; for ($i=0; $i<$count; $i++) { $res[$i]["table"] = @mysql_field_table ($id, $i); $res[$i]["name"] = @mysql_field_name ($id, $i); $res[$i]["type"] = @mysql_field_type ($id, $i); $res[$i]["len"] = @mysql_field_len ($id, $i); $res[$i]["flags"] = @mysql_field_flags ($id, $i); $res["meta"][$res[$i]["name"]] = $i; } } if ($table) @mysql_free_result($id); return $res; } function halt($msg) { $this->Error = @mysql_error($this->Link_ID); $this->Errno = @mysql_errno($this->Link_ID); if ($this->Halt_On_Error == "no") return; $this->haltmsg($msg); if ($this->Halt_On_Error != "report") die("Session halted."); } function haltmsg($msg) { printf("Database error: %s
\n", $msg); printf("MySQL Error: %s (%s)
\n", $this->Errno, $this->Error); } function table_names() { $this->query("SHOW TABLES"); $i=0; while ($info=mysql_fetch_row($this->Query_ID)) { $return[$i]["table_name"]= $info[0]; $return[$i]["tablespace_name"]=$this->Database; $return[$i]["database"]=$this->Database; $i++; } return $return; } } ?> set_root($root); $this->set_unknowns($unknowns); } function set_root($root) { if (!is_dir($root)) { $this->halt("set_root: $root is not a directory."); return false; } $this->root = $root; return true; } function set_unknowns($unknowns = "keep") { $this->unknowns = $unknowns; } function set_file($handle, $filename = "") { if (!is_array($handle)) { if ($filename == "") { $this->halt("set_file: For handle $handle filename is empty."); return false; } $this->file[$handle] = $this->filename($filename); } else { reset($handle); while(list($h, $f) = each($handle)) { $this->file[$h] = $this->filename($f); } } } function set_block($parent, $handle, $name = "") { if (!$this->loadfile($parent)) { $this->halt("subst: unable to load $parent."); return false; } if ($name == "") $name = $handle; $str = $this->get_var($parent); $reg = "/(.*)\n\s*/sm"; preg_match_all($reg, $str, $m); $str = preg_replace($reg, "{" . "$name}", $str); $this->set_var($handle, $m[1][0]); $this->set_var($parent, $str); } function set_var($varname, $value = "") { if (!is_array($varname)) { if (!empty($varname)) if ($this->debug) print "scalar: set *$varname* to *$value*
\n"; $this->varkeys[$varname] = "/".$this->varname($varname)."/"; $this->varvals[$varname] = $value; } else { reset($varname); while(list($k, $v) = each($varname)) { if (!empty($k)) if ($this->debug) print "array: set *$k* to *$v*
\n"; $this->varkeys[$k] = "/".$this->varname($k)."/"; $this->varvals[$k] = $v; } } } function subst($handle) { if (!$this->loadfile($handle)) { $this->halt("subst: unable to load $handle."); return false; } $str = $this->get_var($handle); $str = @preg_replace($this->varkeys, $this->varvals, $str); return $str; } function psubst($handle) { print $this->subst($handle); return false; } function parse($target, $handle, $append = false) { if (!is_array($handle)) { $str = $this->subst($handle); if ($append) { $this->set_var($target, $this->get_var($target) . $str); } else { $this->set_var($target, $str); } } else { reset($handle); while(list($i, $h) = each($handle)) { $str = $this->subst($h); $this->set_var($target, $str); } } return $str; } function pparse($target, $handle, $append = false) { print $this->parse($target, $handle, $append); return false; } function get_vars() { reset($this->varkeys); while(list($k, $v) = each($this->varkeys)) { $result[$k] = $this->varvals[$k]; } return $result; } function get_var($varname) { if (!is_array($varname)) { return $this->varvals[$varname]; } else { reset($varname); while(list($k, $v) = each($varname)) { $result[$k] = $this->varvals[$k]; } return $result; } } function get_undefined($handle) { if (!$this->loadfile($handle)) { $this->halt("get_undefined: unable to load $handle."); return false; } preg_match_all("/\{([^}]+)\}/", $this->get_var($handle), $m); $m = $m[1]; if (!is_array($m)) return false; reset($m); while(list($k, $v) = each($m)) { if (!isset($this->varkeys[$v])) $result[$v] = $v; } if (count($result)) return $result; else return false; } function finish($str) { switch ($this->unknowns) { case "keep": break; case "remove": $str = preg_replace('/{[^ \t\r\n}]+}/', "", $str); break; case "comment": $str = preg_replace('/{([^ \t\r\n}]+)}/', "", $str); break; } return $str; } function p($varname) { print $this->finish($this->get_var($varname)); } function get($varname) { return $this->finish($this->get_var($varname)); } function filename($filename) { if (substr($filename, 0, 1) != "/") { $filename = $this->root."/".$filename; } if (!file_exists($filename)) $this->halt("filename: file $filename does not exist."); return $filename; } function varname($varname) { return preg_quote("{".$varname."}"); } function loadfile($handle) { if (isset($this->varkeys[$handle]) and !empty($this->varvals[$handle])) return true; if (!isset($this->file[$handle])) { $this->halt("loadfile: $handle is not a valid handle."); return false; } $filename = $this->file[$handle]; $str = implode("", @file($filename)); if (empty($str)) { $this->halt("loadfile: While loading $handle, $filename does not exist or is empty."); return false; } $this->set_var($handle, $str); return true; } function halt($msg) { $this->last_error = $msg; if ($this->halt_on_error != "no") $this->haltmsg($msg); if ($this->halt_on_error == "yes") die("Halted."); return false; } function haltmsg($msg) { printf("Template Error: %s
\n", $msg); } } ?>