Filter:   InfoImg
download db.php
Language: PHP
LOC: 249
Project Info
Virtual Photo Album(vphotoalb)
Server: SourceForge
Type: cvs
...alb\vphotoalb\html\include\
   .htaccess
   antispam.php
   constants.php
   db.php
   db_my.php
   db_pg.php
   db_schema.php
   faq_strings.php
   form.php
   functions.php
   globals.php
   html.php
   html_verifier.php
   ImageProcessing.php
   input.php
   mime.php
   originals.php
   PictureFinder.php
   queries.php
   read_exif_data.php
   read_tiff_data.php
   SearchParameters.php
   security.php
   strings.en.php
   strings.fr.php
   strings.php
   tables.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
<?php
/* Load functions for preferred database.
 *
 * Written by: Chris Studholme
 * Copyright:  GPL (http://www.fsf.org/copyleft/gpl.html)
 * $Id: db.php,v 1.17 2003/04/18 23:52:02 cstudhol Exp $
 */


/* Base class for database reset sets.  This class and db_connection form
 * the bases of the database abstraction layer.
 */
class db_resultset {

  // array types
  var $NUM=1;
  var $ASSOC=2;
  var $BOTH=3;

  // number of rows in result set
  function num_rows() {
    die("db_resultset->num_rows() not implemented!");
  }

  // number of fields in result set
  function num_fields() {
    die("db_resultset->num_fields() not implemented!");
  }

  // last error message related to this resultset
  function last_error() {
    die("db_resultset->last_error() not implemented!");
  }

  // close resultset and free memory
  function close() {
  }

  // return field name
  function field_name($index) {
    die("db_resultset->field_name() not implemented!");
  }

  // fetch a specific row and column from the result set
  function result($row,$field) {
    die("db_resultset->result() not implemented!");
  }

  // reset to the first row in the result set
  function reset() {
    die("db_resultset->reset() not implemented!");
  }
  
  // return the next row as an array or false if there are no more rows
  function fetch_array($result_type=false) {
    die("db_resultset->fetch_array() not implemented!");
  }
  
  // return the next row as an array or false if there are no more rows
  function fetch_assoc() {
    return $this->fetch_array($this->ASSOC);
  }
  
  // return the next row as an array or false if there are no more rows
  function fetch_row() {
    return $this->fetch_array($this->NUM);
  }

  /* Function to return entire table as an array.
   * If $key is a column name, the array will be associative with that 
   * column as key.
   * If $value is a column name, only that column will be stored in array.
   * If $multi is true, each element of the array will be a non-associative
   * array or rows.
  */
  function fetch_table($key=false,$value=false,$multi=false) {
    $result = array();
    while ($arr=$this->fetch_assoc()) {
      $v = $value ? $arr[$value] : $arr;
      if (!$key)
	$result[] = $v;
      else if (!$multi)
	$result[$arr[$key]] = $v;
      else
	$result[$arr[$key]][] = $v;
    }
    return $result;
  }

}


/* Reset set for table represented as an array.
 */
class db_resultset_cache extends db_resultset {

  var $table;  // simple array of associative arrays
  var $row;    // current row

  // constructor: new resultset
  function db_resultset_cache($table) {
    if (is_string($table))
      $this->table = unserialize($table);
    else
      $this->table = $table;
    $this->row = 0;
  }

  // number of rows in result set
  function num_rows() {
    return count($this->table);
  }

  // number of fields in result set
  function num_fields() {
    return count($this->table[0]);
  }

  // last error message related to this resultset
  function last_error() {
    return false;
  }

  // return field name
  function field_name($index) {
    reset($this->table[0]);
    $i=0;
    while (list($field,$data)=each($this->table[0])) {
      if ($index==$i)
	return $field;
      ++$i;
    }
    return false;
  }

  // fetch a specific row and column from the result set
  function result($row,$field) {
    if (0<=$row && $row<count($this->table))
      return $table[$row][$field];
    return false;
  }

  // reset to the first row in the result set
  function reset() {
    $this->row=0;
  }
  
  // return the next row as an array or false if there are no more rows
  function fetch_array($result_type=false) {
    if ($this->row>=count($this->table))
      return false;
    switch ($result_type) {
    case $this->ASSOC: 
      $result = $this->table[$this->row];
      break;
    case $this->BOTH:   
      $result = $this->table[$this->row];
    default:    // $this->NUM   
      reset($this->table[$this->row]);
      $i=0;
      while (list($field,$data)=each($this->table[$this->row]))
	$result[$i++] = $data;
    }
    ++$this->row;
    return $result;
  }
}


/* Base class for database connections.  This class and db_resultset form
 * the bases of the database abstraction layer.
 */
class db_connection {

  var $queries = false;
  var $error_function = false;  // prototype: error_function(object,query);

  // create connection to database
  function connect($connection_string) {
    die("db_connection->connect() not implemented!");
  }

  // create persistent connection
  function pconnect($connection_string) {
    die("db_connection->pconnect() not implemented!");
  }

  // close the connection
  function close() {
    die("db_connection->close() not implemented!");
  }

  // get last error message from connection
  function last_error() {
    die("db_connection->last_error() not implemented!");
  }

  // return database name
  function name() {
    die("db_connection->name() not implemented!");
  }

  // execute a database query
  // returns a db_result_set object
  function query($query,$return_error=false) {
    die("db_connection->query() not implemented!");
  }

  function query_cache($query,$return_error=false) {
    global $db_enable_caching;
    if (!$db_enable_caching)
      return $this->query($query,$return_error);
    // check for query in cache
    $h = md5($query);
    $q = "SELECT * FROM cache WHERE hash='".$h."'";
    $result = $this->query($q,$return_error);
    if ($result && $arr=$result->fetch_assoc()) {
      // query is in cache
      return new db_resultset_cache($arr["data"]);
    }
    // query not in cache, execute and add to cache
    $result = $this->query($query,$return_error);
    $table = $result->fetch_table();
    $result->reset();
    $data = $this->quote_string(serialize($table));
    $query = $this->sql_insert("cache",
			       array("hash"=>$this->quote_string($h),
				     "data"=>$data,
				     "date_create"=>$this->date_now()));
    @$this->query($query,true);  // we don't care if this fails
    return $result;
  }

  function clear_cache() {
    $query = $this->sql_delete("cache",false);
    $this->query($query); 
  }

  // start tracking queries
  function track_queries() {
    $this->queries = array();
  }

  // get array of all queries executed on connection thus far
  // returns false if queries are not being tracked
  function get_queries() {
    return $this->queries;
  }

  // set error function to call when database query fails
  function set_error_function($error_function) {
    $this->error_function = $error_function;
  }

  /**** transactions ****/

  function transaction_begin() {
    return true;
  }

  function transaction_commit() {
    return true;
  }

  function transaction_rollback() {
    return false;
  }


  /**** simple SQL queries ****/

  // SQL INSERT query
  function sql_insert($table,$fields) {
    reset($fields);
    list($columns,$values)=each($fields);
    while (list($column,$value)=each($fields)) {
      $columns.=",".$column;
      $values.=",".$value;
    }
    return "INSERT INTO ".$table." (".$columns.") VALUES (".$values.")";
  }

  // SQL UPDATE query
  function sql_update($table,$fields,$where) {
    reset($fields);
    list($column,$value)=each($fields);
    $sets = $column."=".$value;
    while (list($column,$value)=each($fields))
      $sets .= ", ".$column."=".$value;
    return "UPDATE ".$table." SET ".$sets." WHERE ".$where;
  }
  
  // SQL DELETE query
  function sql_delete($table,$where) {
    return "DELETE FROM ".$table.($where?" WHERE ".$where:"");
  }



  /**** SQL feature set ****/

  // returns an associative array of features
  function sql_features() {
    return array("except"=>true,           // supports EXCEPT 
		 "subselect_from"=>true,   // supports subquery in FROM
		 "subselect_select"=>true, // supports subquery in SELECT
		 "subselect_where"=>true,  // supports subquery in WHERE
		 "date_format"=>"YYYY-MM-DD", // format for inputing dates
		 );
  }


  /**** methods to help create queries ****/

  // sql representing current date/time
  function date_now() {
    return "now()";
  }

  // return appropriate date string from UNIX timestamp
  // if $quote is true, valid dates are quoted, non-valid dates are returned
  // as null, 
  // if $quote is false, valid dates are not-quoted, non-valid dates are
  // returned as false
  function date($timestamp,$quote=true) {
    if (is_numeric($timestamp))
      return date($quote?"'Y-m-d'":"Y-m-d",(int)$timestamp);
    return $quote ? "null" : false;
  }

  // return appropriate date/time string from UNIX timestamp
  // if $quote is true, valid dates are quoted, non-valid dates are returned
  // as null, 
  // if $quote is false, valid dates are not-quoted, non-valid dates are
  // returned as false
  function datetime($timestamp,$quote=true) {
    if (is_numeric($timestamp))
      return date($quote?"'Y-m-d H:i:s'":"Y-m-d H:i:s",(int)$timestamp);
    return $quote ? "null" : false;
  }

  // $bit should be a number with only one bit set
  function bit_set($field,$bit) {
    return "(".$field."%".($bit<<1).">=".$bit.")";
  }
  
  // $bit should be a number with only one bit set
  function bit_unset($field,$bit) {
    return "(".$field."%".($bit<<1)."<".$bit.")";
  }

  /* Quote string for use in an SQL query.  If $output_null is true then
   * an empty string will be returned as "null" (unquoted of course).
   */
  function quote_string($str,$output_null=false) {
    $str = strval($str);
    if ($output_null&&!$str)
      return "null";
    return "'".addslashes($str)."'";
  }

}


/* Function to report database error:
 *   - format_error is called to display error
 *   - any outstanding transaction is rolled back
 *   - end_body() is called
 *   - script terminates
 */
function db_error_function(&$conn,$query) {
  $strings = get_strings();
  format_error($strings["err_db_query"],$strings["err_db_query_long"]);
  //echo "Query: ".$query;
  $conn->transaction_rollback();
  end_body(false);
  exit();
}

// database specific classes
require("include/db_".$db_type.".php");

// backwards compatibility:
// returns false if table does not exist
function db_fieldlist(&$conn,$table) {
  @$dbresult = $conn->query("SELECT * FROM ".$table." WHERE false",true);
  if (!$dbresult)
    return false;
  $result = array();
  for ($i=0; $i<$dbresult->num_fields(); ++$i)
    $result[] = $dbresult->field_name($i);
  return $result;
}


// read config table
if ($conn) {
  @$result = $conn->query("SELECT key,value FROM config",true);
  if ($result) {
    $new_config = $result->fetch_table("key","value");
    $config = array_merge($config,$new_config);
  }
  else if (!$db_conn_error) 
    die ("Failed to read 'config' table.");  //STRING
}

?>