36 if ( ! function_exists(
'pg_Connect') ) {
40 <title>PostgreSQL Support Not Present</title>
43 <h1>PostgreSQL Support Not Present</h1>
44 <h3>PHP is not configured to support the PostgreSQL database</h3>
45 <p>You need to ensure that the PostgreSQL support module is installed, and then to configure
46 it in your php.ini file by adding a line like
this:</p>
56 require_once(
"AWLUtilities.php");
61 function connect_configured_database() {
64 if ( isset($dbconn) )
return;
69 dbg_error_log(
'pgquery',
'Attempting to connect to database');
70 if ( isset($c->pg_connect) && is_array($c->pg_connect) ) {
71 foreach( $c->pg_connect AS $k => $v ) {
73 if ( $dbconn = ((isset($c->use_persistent) && $c->use_persistent) ? pg_pConnect($v) : pg_Connect($v) ) )
break;
79 <html><head><title>Database Connection Failure</title></head><body>
80 <h1>Database Error</h1>
81 <h3>Could not connect to PostgreSQL database</h3>
85 if ( isset($c->pg_connect) && is_array($c->pg_connect) ) {
86 dbg_error_log(
"ERROR",
"Failed to connect to database" );
91 if ( isset($c->db_schema) && $c->db_schema !=
'' ) {
92 $result = pg_exec( $dbconn,
"SET Search_path TO ".$c->db_schema.
",public;" );
93 $row = pg_fetch_array($result, 0);
96 $result = pg_exec( $dbconn,
"SELECT version()" );
97 $row = pg_fetch_array($result, 0);
98 $c->found_dbversion = preg_replace(
'/^PostgreSQL (\d+\.\d+)\..*$/i',
'$1', $row[0]);
109 if ( !function_exists(
'duration') ) {
121 function duration( $t1, $t2 ) {
122 list ( $ms1, $s1 ) = explode (
" ", $t1 );
123 list ( $ms2, $s2 ) = explode (
" ", $t2 );
125 $s1 = $s1 + ( $ms2 -$ms1 );
147 function qpg($str =
null) {
150 switch (strtolower(gettype($str))) {
158 $rv = $str ?
'TRUE' :
'FALSE';
162 $str = str_replace(
"'",
"''", $str);
164 $str = str_replace(
'\\',
'\\\\', $str);
166 if ( !isset($c->found_dbversion) || $c->found_dbversion > 8.0 ) $rv =
'E'.$rv;
182 function clean_string( $unclean, $type =
'full' ) {
183 if ( ! isset($unclean) )
return null;
184 if ( is_array($unclean) ) {
186 foreach( $unclean AS $k => $v ) {
187 $result[$k] = clean_string( $v, $type );
191 if ( $type !=
'basic' ) $cleaned = strtolower($unclean);
else $cleaned = &$unclean;
192 $cleaned = preg_replace(
"/['\"!\\\\()\[\]|*\/{}&%@~;:?<>]/",
'', $cleaned );
193 dbg_error_log(
"PgQuery",
"clean_string: Cleaned string from <<%s>> to <<%s>>", $unclean, $cleaned );
219 function awl_replace_sql_args() {
220 $argc = func_num_args();
221 $qry = func_get_arg(0);
222 $args = func_get_args();
224 if ( is_array($qry) ) {
227 $argc = count($args);
232 $parts = explode(
'?', $qry );
233 $querystring = $parts[0];
234 $z = min( count($parts), $argc );
236 for( $i = 1; $i < $z; $i++ ) {
238 if ( !isset($arg) ) {
239 $querystring .=
'NULL';
241 elseif ( is_array($arg) && $arg[
'plain'] !=
'' ) {
243 $querystring .= $arg[
'plain'];
246 $querystring .= qpg($arg);
248 $querystring .= $parts[$i];
250 if ( isset($parts[$z]) ) $querystring .= $parts[$z];
357 var $query_time_warning = 0.3;
371 $this->execution_time = 0;
373 $this->connection = $dbconn;
375 $argc = func_num_args();
378 $this->querystring = awl_replace_sql_args( func_get_args() );
383 $this->querystring = func_get_arg(0);
395 $this->connection = $new_connection;
412 function _log_error( $locn, $tag, $string, $line = 0, $file =
"") {
414 $string = preg_replace(
'/\s+/',
' ', $string);
416 if ( ($tag ==
'QF' || $tag ==
'SQ') && ( $line != 0 && $file !=
"" ) ) {
417 dbg_error_log(
"LOG-$locn",
" Query: %s: Error in '%s' on line %d", $tag, $file, $line );
420 while( strlen( $string ) > 0 ) {
421 dbg_error_log(
"LOG-$locn",
" Query: %s: %s", $tag, substr( $string, 0, 240) );
422 $string = substr(
"$string", 240 );
457 $rv = array(
'plain' => $field );
478 function Exec( $location =
'', $line = 0, $file =
'' ) {
479 global $debuggroups, $c, $dbconn;
480 if ( !isset($this->connection) ) {
481 if ( !isset($dbconn) ) {
482 connect_configured_database();
484 $this->connection = $dbconn;
486 $this->location = trim($location);
487 if ( $this->location ==
"" ) $this->location = substr($_SERVER[
'PHP_SELF'],1);
489 if ( isset($debuggroups[
'querystring']) || isset($c->dbg[
'querystring']) || isset($c->dbg[
'ALL']) ) {
490 $this->
_log_error( $this->location,
'DBGQ', $this->querystring, $line, $file );
494 $this->result = @pg_exec( $this->connection, $this->querystring );
495 $this->
rows = ($this->result ? pg_numrows($this->result) : -1);
497 $i_took = duration( $t1, $t2 );
498 $c->total_query_time += $i_took;
499 $this->execution_time = sprintf(
"%2.06lf", $i_took);
501 if ( !$this->result ) {
503 $this->errorstring = @pg_errormessage();
504 $this->
_log_error( $this->location,
'QF', $this->querystring, $line, $file );
505 $this->
_log_error( $this->location,
'QF', $this->errorstring, $line, $file );
507 elseif ( $this->execution_time > $this->query_time_warning ) {
509 $this->
_log_error( $this->location,
'SQ',
"Took: $this->execution_time for $this->querystring", $line, $file );
511 elseif ( isset($debuggroups[$this->location]) || isset($c->dbg[strtolower($this->location)]) || isset($c->dbg[
'ALL']) ) {
513 $this->
_log_error( $this->location,
'DBGQ',
"Took: $this->execution_time for $this->querystring to find $this->rows rows.", $line, $file );
516 return $this->result;
526 global $c, $debuggroups;
528 if ( ( isset($debuggroups[
"$this->location"]) && $debuggroups[
"$this->location"] > 2 )
529 || (isset($c) && is_object($c) && ( isset($c->dbg[strtolower($this->location)]) && isset($c->dbg[strtolower($this->location)]) )
530 || isset($c->dbg[
'ALL']) ) ) {
531 $this->
_log_error( $this->location,
"Fetch",
"$this->result Rows: $this->rows, Rownum: $this->rownum");
533 if ( ! $this->result )
return false;
534 if ( ($this->rownum + 1) >= $this->
rows )
return false;
537 if ( isset($debuggroups[
"$this->location"]) && $debuggroups[
"$this->location"] > 1 ) {
538 $this->
_log_error( $this->location,
"Fetch",
"Fetching row $this->rownum" );
542 $this->
object = pg_fetch_array($this->result, $this->rownum);
546 $this->
object = pg_fetch_object($this->result, $this->rownum);
549 return $this->object;
573 if ( $this->rownum < -1 ) $this->rownum = -1;
584 if ( isset($debuggroups[
"$this->location"]) && $debuggroups[
"$this->location"] > 2 ) {
585 $this->
_log_error( $this->location,
"FetchBackwards",
"$this->result Rows: $this->rows, Rownum: $this->rownum");
587 if ( ! $this->result )
return false;
588 if ( ($this->rownum - 1) == -1 )
return false;
589 if ( $this->rownum == -1 ) $this->rownum = $this->rows;
593 if ( isset($debuggroups[
"$this->location"]) && $debuggroups[
"$this->location"] > 1 ) {
594 $this->
_log_error( $this->location,
"Fetch",
"Fetching row $this->rownum" );
598 $this->
object = pg_fetch_array($this->result, $this->rownum);
602 $this->
object = pg_fetch_object($this->result, $this->rownum);
605 return $this->object;
615 function BuildOptionList( $current =
'', $location =
'options', $parameters =
false ) {
620 if ( isset($maxwidth) ) unset($maxwidth);
621 if ( is_array($parameters) ) {
622 if ( isset($parameters[
'maxwidth']) ) $maxwidth = max(4,intval($parameters[
'maxwidth']));
623 if ( isset($parameters[
'translate']) ) $translate =
true;
627 if ( $this->
rows > 0 || $this->
Exec($location) ) {
629 while( $row = $this->
Fetch(
true) )
631 if (is_array($current)) {
632 $selected = ( ( in_array($row[0],$current,
true) || in_array($row[1],$current,
true)) ?
' selected="selected"' :
'' );
635 $selected = ( (
"$row[0]" ==
"$current" ||
"$row[1]" ==
"$current" ) ?
' selected="selected"' :
'' );
637 $display_value = $row[1];
638 if ( isset($translate) ) $display_value = translate( $display_value );
639 if ( isset($maxwidth) ) $display_value = substr( $display_value, 0, $maxwidth);
640 $nextrow =
"<option value=\"".htmlspecialchars($row[0]).
"\"$selected>".htmlspecialchars($display_value).
"</option>";