includes/dbal.php
changeset 252 a007145a0ff6
parent 236 3f98d4ba1e33
child 256 f8356d9c3481
equal deleted inserted replaced
251:a1ccf990db6b 252:a007145a0ff6
   122   
   122   
   123   function connect()
   123   function connect()
   124   {
   124   {
   125     $this->enable_errorhandler();
   125     $this->enable_errorhandler();
   126     
   126     
   127     dc_here('dbal: trying to connect....');
       
   128     
       
   129     if ( defined('IN_ENANO_INSTALL') && !defined('IN_ENANO_UPGRADE') )
   127     if ( defined('IN_ENANO_INSTALL') && !defined('IN_ENANO_UPGRADE') )
   130     {
   128     {
   131       @include(ENANO_ROOT.'/config.new.php');
   129       @include(ENANO_ROOT.'/config.new.php');
   132     }
   130     }
   133     else
   131     else
   138     if ( isset($crypto_key) )
   136     if ( isset($crypto_key) )
   139       unset($crypto_key); // Get this sucker out of memory fast
   137       unset($crypto_key); // Get this sucker out of memory fast
   140     
   138     
   141     if ( !defined('ENANO_INSTALLED') && !defined('MIDGET_INSTALLED') && !defined('IN_ENANO_INSTALL') )
   139     if ( !defined('ENANO_INSTALLED') && !defined('MIDGET_INSTALLED') && !defined('IN_ENANO_INSTALL') )
   142     {
   140     {
   143       dc_here('dbal: oops, looks like Enano isn\'t set up. Constants ENANO_INSTALLED, MIDGET_INSTALLED, and IN_ENANO_INSTALL are all undefined.');
       
   144       // scriptPath isn't set yet - we need to autodetect it to avoid infinite redirects
   141       // scriptPath isn't set yet - we need to autodetect it to avoid infinite redirects
   145       if ( !defined('scriptPath') )
   142       if ( !defined('scriptPath') )
   146       {
   143       {
   147         if ( isset($_SERVER['PATH_INFO']) && !preg_match('/index\.php$/', $_SERVER['PATH_INFO']) )
   144         if ( isset($_SERVER['PATH_INFO']) && !preg_match('/index\.php$/', $_SERVER['PATH_INFO']) )
   148         {
   145         {
   167     unset($dbuser);
   164     unset($dbuser);
   168     unset($dbpasswd); // Security
   165     unset($dbpasswd); // Security
   169     
   166     
   170     if ( !$this->_conn )
   167     if ( !$this->_conn )
   171     {
   168     {
   172       dc_here('dbal: uhoh!<br />'.mysql_error());
       
   173       grinding_halt('Enano is having a problem', '<p>Error: couldn\'t connect to MySQL.<br />'.mysql_error().'</p>');
   169       grinding_halt('Enano is having a problem', '<p>Error: couldn\'t connect to MySQL.<br />'.mysql_error().'</p>');
   174     }
   170     }
   175     
   171     
   176     // Reset some variables
   172     // Reset some variables
   177     $this->query_backtrace = array();
   173     $this->query_backtrace = array();
   179     $this->query_sources = array();
   175     $this->query_sources = array();
   180     $this->num_queries = 0;
   176     $this->num_queries = 0;
   181     
   177     
   182     $this->debug = ( defined('ENANO_DEBUG') );
   178     $this->debug = ( defined('ENANO_DEBUG') );
   183     
   179     
   184     dc_here('dbal: we\'re in, selecting database...');
       
   185     $q = $this->sql_query('USE `'.$dbname.'`;');
   180     $q = $this->sql_query('USE `'.$dbname.'`;');
   186     
   181     
   187     if ( !$q )
   182     if ( !$q )
   188       $this->_die('The database could not be selected.');
   183       $this->_die('The database could not be selected.');
   189     
   184     
   190     // We're in!
   185     // We're in!
   191     dc_here('dbal: connected to MySQL');
       
   192     
       
   193     $this->disable_errorhandler();
   186     $this->disable_errorhandler();
   194     return true;
   187     return true;
   195   }
   188   }
   196   
   189   
   197   function sql_query($q)
   190   function sql_query($q)
   217     }
   210     }
   218     
   211     
   219     $this->num_queries++;
   212     $this->num_queries++;
   220     $this->query_backtrace[] = $q;
   213     $this->query_backtrace[] = $q;
   221     $this->latest_query = $q;
   214     $this->latest_query = $q;
   222     dc_here('dbal: making SQL query:<br /><tt>'.$q.'</tt>');
       
   223     // First make sure we have a connection
   215     // First make sure we have a connection
   224     if ( !$this->_conn )
   216     if ( !$this->_conn )
   225     {
   217     {
   226       $this->_die('A database connection has not yet been established.');
   218       $this->_die('A database connection has not yet been established.');
   227     }
   219     }
   245     $this->enable_errorhandler();
   237     $this->enable_errorhandler();
   246     
   238     
   247     $this->num_queries++;
   239     $this->num_queries++;
   248     $this->query_backtrace[] = '(UNBUFFERED) ' . $q;
   240     $this->query_backtrace[] = '(UNBUFFERED) ' . $q;
   249     $this->latest_query = $q;
   241     $this->latest_query = $q;
   250     dc_here('dbal: making SQL query:<br /><tt>'.$q.'</tt>');
       
   251     // First make sure we have a connection
   242     // First make sure we have a connection
   252     if ( !$this->_conn )
   243     if ( !$this->_conn )
   253     {
   244     {
   254       $this->_die('A database connection has not yet been established.');
   245       $this->_die('A database connection has not yet been established.');
   255     }
   246     }
   452     $this->disable_errorhandler();
   443     $this->disable_errorhandler();
   453     return null;
   444     return null;
   454   }
   445   }
   455   
   446   
   456   function close() {
   447   function close() {
   457     dc_here('dbal: closing MySQL connection');
       
   458     mysql_close($this->_conn);
   448     mysql_close($this->_conn);
   459     unset($this->_conn);
   449     unset($this->_conn);
   460   }
   450   }
   461   
   451   
   462   // phpBB DBAL compatibility
   452   // phpBB DBAL compatibility