Source for file db_sqlite.class.php

Documentation is available at db_sqlite.class.php

  1. <?php
  2. /**
  3.  * Database Class provides an API to communicate with SQLite Database.
  4.  * Nearly all methods are identical to my other Oracle and MySQL classes.
  5.  * This is the PHP 5 only class, the old PHP 4 class won't be extended anymore!
  6.  * Requires dbdefs.inc.php for global access data (dbname,appname).
  7.  * @author Sascha 'SieGeL' Pfalz <php@saschapfalz.de>
  8.  * @package db_SQLite
  9.  * @version 0.20 (07-Aug-2010)
  10.  *  $Id: db_sqlite.class.php,v 1.2 2010/08/07 17:47:03 siegel Exp $
  11.  * @see dbdefs.inc.php
  12.  * @license http://opensource.org/licenses/bsd-license.php BSD License
  13.  * @filesource
  14.  */
  15.  
  16. /**
  17.  * SQLite class.
  18.  * @package db_SQLite
  19.  */
  20. class db_SQLite
  21.   {
  22.   /**
  23.    * Class version.
  24.    * @private
  25.    * @var string 
  26.    */
  27.   private $classversion '0.20';
  28.  
  29.   /**
  30.    * Internal connection handle.
  31.    * @access protected
  32.    * @var resource 
  33.    */
  34.   protected $sock = NULL;
  35.  
  36.   /**
  37.    * The Database filename.
  38.    * @access protected
  39.    * @var string 
  40.    */
  41.   protected $database = '';
  42.  
  43.   /**
  44.    * The default database filename permission mode.
  45.    * @access protected
  46.    * @var integer 
  47.    */
  48.   protected $db_mode = 0666;
  49.  
  50.   /**
  51.    * The Name of the application using this class.
  52.    * @access protected
  53.    * @var string 
  54.    */
  55.   protected $appname = '';
  56.  
  57.   /**
  58.    * Contains the actual query to be processed.
  59.    * @access protected
  60.    * @var string 
  61.    */
  62.   protected $currentQuery = '';
  63.  
  64.   /**
  65.    * Flag indicates how the class should interact with errors
  66.    * @access protected
  67.    * @var integer 
  68.    */
  69.   protected $showError = db_SQLite::DBOF_SHOW_NO_ERRORS;
  70.  
  71.   /**
  72.    * Debugstate, default is OFF.
  73.    * @access protected
  74.    * @var integer 
  75.    */
  76.   protected $debug = db_SQLite::DBOF_DEBUGOFF;
  77.  
  78.   /**
  79.    * The SAPI type of php (used to detect CLI sapi)
  80.    * @private
  81.    * @var string 
  82.    */
  83.   private $SAPI_type '';
  84.  
  85.   /**
  86.    * Email Address for the administrator of this project
  87.    * @private
  88.    * @var string 
  89.    */
  90.   private $AdminEmail '';
  91.  
  92.   /**
  93.    * Error code of last sqlite operation (set in Print_Error()).
  94.    * @private
  95.    * @var integer 
  96.    */
  97.   private $myErrno 0;
  98.  
  99.   /**
  100.    * Error string of last sqlite operation (set in Print_Error()).
  101.    * @private
  102.    * @var string 
  103.    */
  104.   var $myErrStr = '';
  105.  
  106.   /**
  107.    * Set to TRUE if Connect() should use persistant connection, else new one (Default)
  108.    * @private
  109.    * @var boolean 
  110.    */
  111.   private $usePConnect FALSE;
  112.  
  113.   /**
  114.    * How many queries where executed.
  115.    * @private
  116.    * @var integer 
  117.    */
  118.   private $querycounter 0;
  119.  
  120.   /**
  121.    * Amount of time spent executing class methods.
  122.    * @private
  123.    * @var integer 
  124.    */
  125.   private $querytime 0.000;
  126.  
  127.   /** DEBUG: No Debug Info */
  128.   const DBOF_DEBUGOFF           1;
  129.   /** DEBUG: Debug to screen */
  130.   const DBOF_DEBUGSCREEN        2;
  131.   /** DEBUG: Debug to error.log */
  132.   const DBOF_DEBUGFILE          4;
  133.  
  134.   /**#@+
  135.    * Connect and error handling.
  136.    * If NO_ERRORS is set and an error occures, the class still reports an
  137.    * an error of course but the error shown is reduced to avoid showing
  138.    * sensible informations in a productive environment.
  139.    * Set RETURN_ALL_ERRORS if you want to handle errors yourself.
  140.    */
  141.   const DBOF_SHOW_NO_ERRORS     0;
  142.   const DBOF_SHOW_ALL_ERRORS    1;
  143.   const DBOF_RETURN_ALL_ERRORS  2;
  144.   /**#@-*/
  145.  
  146.   /**#@+
  147.    * All defines for custom error codes *
  148.    */
  149.   const SQLITEDB_ERR_NO_APPNAME_DEFINE  = -1;
  150.   const SQLITEDB_ERR_NO_CONNECTION      = -2;
  151.   const SQLITEDB_ERR_NO_ACTIVE_CONNECT  = -3;
  152.   const SQLITEDB_ERR_SQL_SYNTAX_ERROR   = -4;
  153.   const SQLITEDB_ERR_EXTENSION_MISSING  = -5;
  154.   /**#@-*/
  155.  
  156.   /**
  157.    * Constructor of class.
  158.    * @param string $ext_config Pass here the full name to your define file where all external class defines are set. If empty uses "dbdefs.inc.php".
  159.    */
  160.   public function __construct($ext_config '')
  161.     {
  162.     $this->SAPI_type  @php_sapi_name();
  163.     $this->AdminEmail = isset($_SERVER['SERVER_ADMIN']strip_tags($_SERVER['SERVER_ADMIN']'';
  164.     if($ext_config == '')
  165.       {
  166.       require_once('dbdefs.inc.php');
  167.       }
  168.     else
  169.       {
  170.       require_once($ext_config);
  171.       }
  172.     if(function_exists('sqlite_open'== FALSE)
  173.       {
  174.       $this->setErrorHandling(db_SQLite::DBOF_SHOW_ALL_ERRORS);
  175.       $this->myErrStr = 'Missing function sqlite_open() - extension not loaded!';
  176.       $this->myErrno  db_SQLite::SQLITEDB_ERR_EXTENSION_MISSING;
  177.       $this->Print_Error("SQLite Extension not loaded!");
  178.       }
  179.     if(defined('SQLITEDB_APPNAME')==false)
  180.       {
  181.       $this->myErrno  db_SQLite::SQLITEDB_ERR_NO_APPNAME_DEFINE;
  182.       $this->myErrStr = 'Define SQLITEDB_APPNAME not set!';
  183.       $this->appname  = '!!! NOT SET !!!';
  184.       $this->setErrorHandling(db_SQLite::DBOF_SHOW_ALL_ERRORS);
  185.       $this->Print_Error('dbdefs.inc.php not found/wrong configured! Please check Class installation!');
  186.       }
  187.     $this->appname = SQLITEDB_APPNAME;
  188.     if(defined('SQLITEDB_ERRORMODE'))                       // You can set a default behavour for error handling in debdefs.inc.php
  189.       {
  190.       }
  191.     else
  192.       {
  193.       $this->SetErrorHandling(db_SQLite::DBOF_SHOW_NO_ERRORS);         // Default is not to show too much informations
  194.       }
  195.     // __construct()
  196.  
  197.   /**
  198.    * Performs the connection to a SQLite database file.
  199.    * If anything goes wrong calls Print_Error().
  200.    * You should set the defaults for your connection by setting
  201.    * database filename in dbdefs.inc.php and leave connect() parameters empty.
  202.    * @param string $database Optional Filename of database to use
  203.    * @param string $mode Optional the file protection bits as octal number, defaults to 0666.
  204.    * @return mixed Either the DB connection handle or NULL in case of an error.
  205.    * @see dbdefs.inc.php
  206.    * @see sqlite_open()
  207.    * @see sqlite_popen()
  208.    */
  209.   public function Connect($database ''$mode 0)
  210.     {
  211.     if($this->sock)
  212.       {
  213.       return($this->sock);
  214.       }
  215.     if($database!='')
  216.       {
  217.       $this->database = $database;
  218.       }
  219.     else
  220.       {
  221.       $this->database = SQLITEDB_DEFAULT_DB;
  222.       }
  223.     if($mode != 0)
  224.       {
  225.       $this->db_mode = $mode;
  226.       }
  227.     $start $this->GetMicrotime();
  228.     if($this->usePConnect == FALSE)
  229.       {
  230.       $this->PrintDebug('sqlite_open("'.$this->database.'",'.$this->db_mode.')');
  231.       $rc @sqlite_open($this->database,$this->db_mode,$sqlite_errmsg);
  232.       }
  233.     else
  234.       {
  235.       $this->PrintDebug('sqlite_popen("'.$this->database.'",'.$this->db_mode.')');
  236.       $rc @sqlite_popen($this->database,$this->db_mode,$sqlite_errmsg);
  237.       }
  238.     if(is_resource($rc== false)
  239.       {
  240.       $this->myErrStr = $sqlite_errmsg;
  241.       $this->myErrno  db_SQLite::SQLITEDB_ERR_NO_CONNECTION;
  242.       $this->Print_Error('Connect(): Connection to '.$this->database.' failed!');
  243.       return(0);
  244.       }
  245.     $this->sock = $rc;
  246.     return($this->sock);
  247.     // Connect()
  248.  
  249.   /**
  250.    * Disconnects from SQLite database.
  251.    * You may optionally pass an external link identifier.
  252.    * @param mixed $other_sock Optionally your own connection handle to close, else internal will be used
  253.    * @see sqlite_close()
  254.    */
  255.   public function Disconnect($other_sock=-1)
  256.     {
  257.     if($other_sock!=-1)
  258.       {
  259.       @sqlite_close($other_sock);
  260.       }
  261.     else
  262.       {
  263.       if($this->sock)
  264.         {
  265.         @sqlite_close($this->sock);
  266.         $this->sock = 0;
  267.         }
  268.       }
  269.     $this->currentQuery = '';
  270.     }
  271.  
  272.   /**
  273.    * Single-Row query method.
  274.    * Returns only the first row of a given query!
  275.    * Resflag can be one of SQLITE_NUM or SQLITE_ASSOC or SQLITE_BOTH depending on what kind of array you want to be returned.
  276.    * @param string $querystring The query to be executed.
  277.    * @param integer $resflag Decides how the result should be returned:
  278.    *   - SQLITE_ASSOC = Data is returned as associative array (default value).
  279.    *   - SQLITE_NUM   = Data is returned as numbered array.
  280.    *   - SQLITE_BOTH  = Data is returned both as numbered and as associative array.
  281.    * @param integer $no_exit Decides how the class should react on errors.
  282.    *                          If you set this to 1 the class won't automatically exit on an error but instead return the sqlite_errno value.
  283.    *                          Default of 0 means that the class calls Print_Error() and exists.
  284.    * @return mixed Either the result of the query or an error code or no return value at all
  285.    * @see Print_Error()
  286.    * @see sqlite_unbuffered_query()
  287.    * @see sqlite_fetch_array()
  288.    */
  289.   public function Query($querystring,$resflag SQLITE_ASSOC$no_exit 0)
  290.     {
  291.     if(!$this->sock)
  292.       {
  293.       $this->myErrStr = '';
  294.       $this->myErrno  db_SQLite::SQLITEDB_ERR_NO_ACTIVE_CONNECT;
  295.       return($this->Print_Error('Query(): No active connection!',$querystring));
  296.       }
  297.     if($this->debug)
  298.       {
  299.       $this->PrintDebug($querystring);
  300.       }
  301.     $this->currentQuery = $querystring;
  302.     if($this->showError == db_SQLite::DBOF_RETURN_ALL_ERRORS)
  303.       {
  304.       $no_exit 1;  // Override if user has set master define
  305.       }
  306.     $start $this->GetMicrotime();
  307.     $this->querycounter++;
  308.     $res @sqlite_unbuffered_query($this->sock,$querystring,$resflag,$sqlite_errormsg);
  309.     if($res == false)
  310.       {
  311.       if($no_exit)
  312.         {
  313.         $this->myErrno  db_SQLite::SQLITEDB_ERR_SQL_SYNTAX_ERROR;
  314.         $this->myErrStr = $sqlite_errormsg;
  315.         return($this->myErrno);
  316.         }
  317.       else
  318.         {
  319.         return($this->Print_Error($sqlite_errormsg));
  320.         }
  321.       }
  322.     if(@sqlite_has_more($res== true)
  323.       {
  324.       $retarr @sqlite_fetch_array($res,$resflag);
  325.       $this->querytime+= ($this->GetMicrotime($start);
  326.       return($retarr);
  327.       }
  328.     else
  329.       {
  330.       $this->querytime+= ($this->GetMicrotime($start);
  331.       return($res);
  332.       }
  333.     }
  334.  
  335.   /**
  336.    * Performs a multi-row query and returns result identifier.
  337.    * @param string $querystring The Query to be executed
  338.    * @param integer $no_exit The error indicator flag, can be one of:
  339.    *   - 0 = (Default), In case of an error Print_Error is called and script terminates
  340.    *   - 1 = In case of an error this function returns the error from sqlite_last_error()
  341.    * @return mixed A resource identifier or an errorcode (if $no_exit = 1)
  342.    * @see sqlite_query()
  343.    */
  344.   public function QueryResult($querystring$no_exit 0)
  345.     {
  346.     if(!$this->sock)
  347.       {
  348.       $this->myErrStr = '';
  349.       $this->myErrno  db_SQLite::SQLITEDB_ERR_NO_ACTIVE_CONNECT;
  350.       return($this->Print_Error('QueryResult(): No active Connection!',$querystring));
  351.       }
  352.     if($this->debug)
  353.       {
  354.       $this->PrintDebug($querystring);
  355.       }
  356.     $this->currentQuery = $querystring;
  357.     $start $this->GetMicrotime();
  358.     $this->querycounter++;
  359.     $res @sqlite_query($this->sock,$querystring);
  360.     if($res == false)
  361.       {
  362.       if($no_exit)
  363.         {
  364.         $this->myErrno  db_SQLite::SQLITEDB_ERR_SQL_SYNTAX_ERROR;
  365.         $this->myErrStr = $sqlite_errormsg;
  366.         return($this->myErrno);
  367.         }
  368.       else
  369.         {
  370.         return($this->Print_Error($sqlite_errormsg));
  371.         }
  372.       }
  373.     $this->querytime+= ($this->GetMicrotime($start);
  374.     return($res);
  375.     }
  376.  
  377.   /**
  378.    * Fetches next row from result handle.
  379.    * Returns either numeric array (SQL_NUM), associative array (SQL_ASSOC) or both (SQLITE_BOTH) for one data row as pointed to by result var.
  380.    * @param mixed $result The resource identifier as returned by QueryResult()
  381.    * @param integer $resflag How you want the data to be returned:
  382.    *   - SQLITE_ASSOC = Data is returned as associative array.
  383.    *   - SQLITE_NUM   = Data is returned as numbered array.
  384.    *   - SQLITE_BOTH  = Data is returned both as associative and numbered array.
  385.    * @return array One row of the resulting query or NULL if there is no data anymore to read.
  386.    * @see sqlite_fetch_array()
  387.    * @see QueryResult()
  388.    */
  389.   public function FetchResult($result,$resflag SQLITE_ASSOC)
  390.     {
  391.     if(!$result)
  392.       {
  393.       return($this->Print_Error('FetchResult(): No valid result handle!'));
  394.       }
  395.     $start $this->GetMicrotime();
  396.     $resar @sqlite_fetch_array($result,$resflag);
  397.     $this->querytime+= ($this->GetMicrotime($start);
  398.     return($resar);
  399.     }
  400.  
  401.   /**
  402.    * Frees result returned by QueryResult().
  403.    * Note that SQLite has nothing to free (?) so here only the internal query variable is set to an empty string.
  404.    * @param mixed $result The resource identifier you want to be freed.
  405.    * @see QueryResult()
  406.    * @see FetchResult()
  407.    */
  408.   public function FreeResult($result)
  409.     {
  410.     $this->currentQuery = '';
  411.     }
  412.  
  413.   /**
  414.    * Returns number of rows affected by most recent DML operation against $dbhandle.
  415.    * @param resource $sock Optional a connection handle, if none given the internal socket will be used.
  416.    * @return integer Number of affected rows.
  417.    * @see NumRows()
  418.    * @see sqlite_changes()
  419.    */
  420.   public function AffectedRows($sock = -1)
  421.     {
  422.     $rows 0;
  423.  
  424.     if($sock = -1)
  425.       {
  426.       $rows @sqlite_changes($this->sock);
  427.       }
  428.     else
  429.       {
  430.       $rows @sqlite_changes($sock);
  431.       }
  432.     return($rows);
  433.     }
  434.  
  435.   /**
  436.    * Returns number of rows in a result set.
  437.    * Note: This only works for statements returning a result set, for DML operations use AffectedRows() !
  438.    * @param resource $stmt The resource as returned from QueryResult()
  439.    * @see AffectedRows()
  440.    * @see sqlite_num_rows()
  441.    */
  442.   public function NumRows($result)
  443.     {
  444.     return(sqlite_num_rows($result));
  445.     }
  446.  
  447.   /**
  448.    * Returns last used auto_increment id.
  449.    * @param mixed $extsock Optionally an external SQLite socket to use. If not given the internal socket is used.
  450.    * @return integer The last automatic insert id that was assigned by the SQLite server.
  451.    * @see sqlite_last_insert_rowid()
  452.    */
  453.   public function LastInsertId($extsock=-1)
  454.     {
  455.     if($extsock==-1)
  456.       {
  457.       return(@sqlite_last_insert_rowid($this->sock));
  458.       }
  459.     else
  460.       {
  461.       return(@sqlite_last_insert_rowid($extsock));
  462.       }
  463.     }
  464.  
  465.   /**
  466.    * Commits current transaction.
  467.    * Note: Requires BEGIN TRANSACTION first!
  468.    * Without BEGIN TRANSACTION an auto-transaction is always auto-commited!
  469.    */
  470.   function Commit()
  471.     {
  472.     if($this->debug)
  473.       {
  474.       $this->PrintDebug('COMMIT called');
  475.       }
  476.     $this->Query('COMMIT TRANSACTION');
  477.     }
  478.  
  479.   /**
  480.    * Rollback current transaction.
  481.    * Note: Requires BEGIN TRANSACTION first!
  482.    * Without BEGIN TRANSACTION an auto-transaction is always auto-commited!
  483.    */
  484.   function Rollback()
  485.     {
  486.     if($this->debug)
  487.       {
  488.       $this->PrintDebug('ROLLBACK called');
  489.       }
  490.     $this->Query('ROLLBACK TRANSACTION');
  491.     }
  492.  
  493.   /**
  494.    * Allows to set the handling of errors.
  495.    *
  496.    * - db_SQLite::DBOF_SHOW_NO_ERRORS    => Show no security-relevant informations
  497.    * - db_SQLite::DBOF_SHOW_ALL_ERRORS   => Show all errors (useful for develop)
  498.    * - db_SQLite::DBOF_RETURN_ALL_ERRORS => No error/autoexit, just return the mysql_error code.
  499.    * @param integer $val The Error Handling mode you wish to use.
  500.    */
  501.   public function SetErrorHandling($val)
  502.     {
  503.     $this->showError = $val;
  504.     }
  505.  
  506.   /**
  507.    * Returns the current error handling mode.
  508.    * @return integer The current error handling mode.
  509.    * @see SetErrorHandling()
  510.    * @since 0.20
  511.    */
  512.   public function GetErrorHandling()
  513.     {
  514.     return($this->showError);
  515.     }
  516.  
  517.   /**
  518.    * Function allows debugging of SQL Queries.
  519.    * $state can have these values:
  520.    * - db_SQLite::DBOF_DEBUGOFF    = Turn off debugging
  521.    * - db_SQLite::DBOF_DEBUGSCREEN = Turn on debugging on screen (every Query will be dumped on screen)
  522.    * - db_SQLite::DBOF_DEBUFILE    = Turn on debugging on PHP errorlog
  523.    * You can mix the debug levels by adding the according defines!
  524.    * @param integer $state The DEBUG Level you want to be set
  525.    * @see GetDebug()
  526.    */
  527.   public function SetDebug($state)
  528.     {
  529.     $this->debug = $state;
  530.     }
  531.  
  532.   /**
  533.    * Returns the current debug setting.
  534.    * @return integer The debug setting (bitmask)
  535.    * @see SetDebug()
  536.    */
  537.   public function GetDebug()
  538.     {
  539.     return($this->debug);
  540.     }
  541.  
  542.   /**
  543.    * Handles output according to internal debug flag.
  544.    * @param string $msg The Text to be included in the debug message.
  545.    * @see error_log()
  546.    * @see SetDebug()
  547.    */
  548.   public function PrintDebug($msg)
  549.     {
  550.     if(!$this->debug)
  551.       {
  552.       return;
  553.       }
  554.     if($this->SAPI_type != 'cli')
  555.       {
  556.       $formatstr "<div align=\"left\" style=\"background-color:#ffffff; color:#000000;z-index:10000;\"><pre>DEBUG: %s</pre></div>\n";
  557.       }
  558.     else
  559.       {
  560.       $formatstr =  "DEBUG: %s\n";
  561.       }
  562.     if($this->debug db_SQLite::DBOF_DEBUGSCREEN)
  563.       {
  564.       @printf($formatstr,$msg);
  565.       }
  566.     if($this->debug db_SQLite::DBOF_DEBUGFILE)
  567.       {
  568.       @error_log('DEBUG: '.$msg,0);
  569.       }
  570.     }
  571.  
  572.   /**
  573.    * Retrieve last SQLite error number.
  574.    * @param mixed $other_sock Optionally your own connection handle to check, else internal will be used
  575.    * @return integer The SQLite error number of the last operation
  576.    * @see sqlite_last_error
  577.    */
  578.   public function GetErrno($other_sock = -1)
  579.     {
  580.     if$other_sock == -)
  581.       {
  582.       if(!$this->sock)
  583.         {
  584.         return($this->myErrno);
  585.         }
  586.       else
  587.         {
  588.         return(@sqlite_last_error($this->sock));
  589.         }
  590.       }
  591.     else
  592.       {
  593.       if(!$other_sock)
  594.         {
  595.         return($this->myErrno);
  596.         }
  597.       else
  598.         {
  599.         return(@sqlite_last_error($other_sock));
  600.         }
  601.       }
  602.     }
  603.  
  604.   /**
  605.    * Retrieve last SQLite error description.
  606.    * @param mixed $other_sock Optionally your own connection handle to check, else internal will be used
  607.    * @return string The SQLite error description of the last operation
  608.    * @see mysql_error
  609.    */
  610.   public function GetErrorText($other_sock = -1)
  611.     {
  612.     if$other_sock == -)
  613.       {
  614.       if(!$this->sock)
  615.         {
  616.         return($this->myErrStr);
  617.         }
  618.       else
  619.         {
  620.         return(@sqlite_error_string($this->GetErrno()));
  621.         }
  622.       }
  623.     else
  624.       {
  625.       if(!$other_sock)
  626.         {
  627.         return($this->myErrStr);
  628.         }
  629.       else
  630.         {
  631.         return(@sqlite_error_string($this->GetErrno($other_sock)));
  632.         }
  633.       }
  634.     }
  635.  
  636.   /**
  637.    * Prints out SQLite Error in own <div> container and exits.
  638.    * Please note that this function does not return as long as you have not set DBOF_RETURN_ALL_ERRORS!
  639.    * @param string $ustr User-defined Error string to show
  640.    * @param mixed $var2dump Optionally a variable to print out with print_r()
  641.    * @see print_r()
  642.    * @see sqlite_last_error()
  643.    * @see sqlite_error_string()
  644.    */
  645.   public function Print_Error($ustr="",$var2dump="")
  646.     {
  647.     if($this->sock)
  648.       {
  649.       $errnum @sqlite_last_error($this->sock);
  650.       $errstr @sqlite_error_string($errnum);
  651.       }
  652.     else
  653.       {
  654.       $errnum $this->myErrno;
  655.       $errstr $this->myErrStr;
  656.       }
  657.     $this->myErrno  $errnum;
  658.     $this->myErrStr = $errstr;
  659.     $filename basename($_SERVER['SCRIPT_FILENAME']);
  660.     if($errstr=='')
  661.       {
  662.       $errstr 'N/A';
  663.       }
  664.     if($errnum=='')
  665.       {
  666.       $errnum = -1;
  667.       }
  668.     @error_log($this->appname.': SQLite class error in '.$filename.': '.$ustr.' ('.chop($errstr).')',0);
  669.     if($this->showError == db_SQLite::DBOF_RETURN_ALL_ERRORS)
  670.       {
  671.       return($errnum);      // Return the error number
  672.       }
  673.     $this->SendMailOnError($errnum,$errstr,$ustr);
  674.     $crlf "\n";
  675.     $space" ";
  676.     if($this->SAPI_type != 'cli')
  677.       {
  678.       $crlf "<br>\n";
  679.       $space"&nbsp;";
  680.       echo("<br>\n<div align=\"left\" style=\"background-color: #EEEEEE; color:#000000;\">\n");
  681.       echo("<font color=\"red\" face=\"Arial, Sans-Serif\"><b>".$this->appname.": SQLite class error occured!</b></font><br>\n<br>\n<code>\n");
  682.       }
  683.     else
  684.       {
  685.       echo("\n!!! ".$this->appname.": SQLite class error occured !!!\n\n");
  686.       }
  687.     echo($space."CODE: ".$errnum.$crlf);
  688.     echo($space."DESC: ".$errstr.$crlf);
  689.     echo($space."FILE: ".$filename.$crlf);
  690.     if($this->showError == db_SQLite::DBOF_SHOW_ALL_ERRORS)
  691.       {
  692.       if($this->currentQuery!="")
  693.         {
  694.         echo("QUERY: ".$this->currentQuery.$crlf);
  695.         }
  696.       echo($space."QCNT: ".$this->querycounter.$crlf);
  697.       if($ustr!='')
  698.         {
  699.         echo($space."INFO: ".$ustr.$crlf);
  700.         }
  701.       if($var2dump!='')
  702.         {
  703.         echo($space.'DUMP: ');
  704.         if(is_array($var2dump))
  705.           {
  706.           if($this->SAPI_type != 'cli')
  707.             {
  708.             echo('<pre>');
  709.             print_r($var2dump);
  710.             echo("</pre>\n");
  711.             }
  712.           else
  713.             {
  714.             print_r($var2dump);
  715.             }
  716.           }
  717.         else
  718.           {
  719.           echo($var2dump.$crlf);
  720.           }
  721.         }
  722.       }
  723.     if($this->SAPI_type != 'cli')
  724.       {
  725.       if($this->AdminEmail != '')
  726.         {
  727.         echo("<br>\nPlease inform <a href=\"mailto:".$this->AdminEmail."\">".$this->AdminEmail."</a> about this problem.");
  728.         echo("</code>\n");
  729.         echo("</div>\n");
  730.         }
  731.       echo("<div align=\"right\"><small>PHP v".phpversion()." / SQLite Class v".$this->classversion."</small></div>\n");
  732.       }
  733.     else
  734.       {
  735.       if($this->AdminEmail != '')
  736.         {
  737.         echo("\nPlease inform ".$this->AdminEmail." about this problem.\n");
  738.         }
  739.       echo("\nRunning on PHP v".phpversion()." / SQLite Class v".$this->classversion."\n");
  740.       }
  741.     $this->Disconnect();
  742.     exit;
  743.     }
  744.  
  745.   /**
  746.    * Send error email if programmer has defined a valid email address and enabled it with the define SQLITEDB_SENTMAILONERROR.
  747.    * @param integer $merrno SQLite errno number
  748.    * @param string $merrstr SQLite error description
  749.    * @param string $uerrstr User-supplied error description
  750.    * @see dbdefs.inc.php
  751.    * @see mail()
  752.    */
  753.   private function SendMailOnError($merrno,$merrstr,$uerrstr)
  754.     {
  755.     if(!defined('SQLITEDB_SENTMAILONERROR'|| SQLITEDB_SENTMAILONERROR == || $this->AdminEmail == '')
  756.       {
  757.       return;
  758.       }
  759.     $sname    (isset($_SERVER['SERVER_NAME']== TRUE$_SERVER['SERVER_NAME''';
  760.     $saddr    (isset($_SERVER['SERVER_ADDR']== TRUE$_SERVER['SERVER_ADDR''';
  761.     $raddr    (isset($_SERVER['REMOTE_ADDR']== TRUE$_SERVER['REMOTE_ADDR''';
  762.     $uagent   (isset($_SERVER['HTTP_USER_AGENT'])) $_SERVER['HTTP_USER_AGENT''';
  763.     if($sname == '')
  764.       {
  765.       $server 'n/a';
  766.       }
  767.     else
  768.       {
  769.       $server  $sname.' ('.$saddr.')';
  770.       }
  771.     if($uagent == '')
  772.       {
  773.       $uagent 'n/a';
  774.       }
  775.     if($raddr == '')
  776.       {
  777.       $clientip 'n/a';
  778.       }
  779.     else
  780.       {
  781.       $clientip $raddr." (".@gethostbyaddr($raddr).")";
  782.       }
  783.     $message "SQLite Class v".$this->classversion.": Error occured on ".date('r')." !!!\n\n";
  784.     $message.= "      APPLICATION: ".$this->appname."\n";
  785.     $message.= "  AFFECTED SERVER: ".$server."\n";
  786.     $message.= "       USER AGENT: ".$uagent."\n";
  787.     $message.= "       PHP SCRIPT: ".$_SERVER['SCRIPT_FILENAME']."\n";
  788.     $message.= "   REMOTE IP ADDR: ".$clientip."\n";
  789.     $message.= "    DATABASE FILE: ".$this->database."\n";
  790.     $message.= "SQL ERROR MESSAGE: ".$merrstr."\n";
  791.     $message.= "   SQL ERROR CODE: ".$merrno."\n";
  792.     $message.= "    QUERY COUNTER: ".$this->querycounter."\n";
  793.     $message.= "         INFOTEXT: ".$uerrstr."\n";
  794.     if($this->currentQuery != '')
  795.       {
  796.       $message.= "        SQL QUERY:\n";
  797.       $message.= "------------------------------------------------------------------------------------\n";
  798.       $message.= $this->currentQuery."\n";
  799.       }
  800.     $message.= "------------------------------------------------------------------------------------\n";
  801.     if(defined('MYSQLDB_MAIL_EXTRAARGS'&& MYSQLDB_MAIL_EXTRAARGS != '')
  802.       {
  803.       @mail($this->AdminEmail,'SQLite Class v'.$this->classversion.' ERROR #'.$merrno.' OCCURED!',$message,MYSQLDB_MAIL_EXTRAARGS);
  804.       }
  805.     else
  806.       {
  807.       @mail($this->AdminEmail,'SQLite Class v'.$this->classversion.' ERROR #'.$merrno.' OCCURED!',$message);
  808.       }
  809.     }
  810.  
  811.   /**
  812.    * Returns version of this class.
  813.    * @return string The version of this class.
  814.    */
  815.   public function GetClassVersion()
  816.     {
  817.     return($this->classversion);
  818.     }
  819.  
  820.   /**
  821.    * Returns SQLite library version.
  822.    * @return string SQLite library version.
  823.    */
  824.   public function Version()
  825.     {
  826.     return(@sqlite_libversion());
  827.     }
  828.  
  829.   /**
  830.    * Escapes a given string with the 'sqlite_escape_string' method.
  831.    * Always use this function to avoid SQL injections when adding dynamic data to SQLite!
  832.    * This function also handles the settings for magic_quotes_gpc, if
  833.    * this setting is enabled it will call stripslashes() first.
  834.    * @param string $str The string to escape.
  835.    * @return string The escaped string.
  836.    */
  837.   public function EscapeString($str)
  838.     {
  839.     $data $str;
  840.     if(@get_magic_quotes_gpc())
  841.       {
  842.       $data @stripslashes($data);
  843.       }
  844.     return(@sqlite_escape_string($data));
  845.     }
  846.  
  847.   /**
  848.    * Returns amount of queries executed by this class.
  849.    * @return integer Querycount
  850.    */
  851.   public function GetQueryCount()
  852.     {
  853.     return($this->querycounter);
  854.     }
  855.  
  856.   /**
  857.    * Returns amount of time spend on queries executed by this class.
  858.    * @return float Time in seconds.msecs spent in executin MySQL code.
  859.    */
  860.   public function GetQueryTime()
  861.     {
  862.     return($this->querytime);
  863.     }
  864.  
  865.    /**
  866.    * Returns microtime in format s.mmmmm.
  867.    * Used to measure SQL execution time.
  868.    * @static
  869.    * @return float the current time in microseconds.
  870.    */
  871.   public static function GetMicrotime()
  872.     {
  873.     list($usec$secexplode(" ",microtime());
  874.     return (floatval($usecfloatval($sec));
  875.     }
  876.  
  877.   // End-of-class
  878. ?>

Documentation generated on Sat, 07 Aug 2010 20:01:01 +0200 by phpDocumentor 1.4.3