Back to index

php5  5.3.10
connect.inc
Go to the documentation of this file.
00001 <?php
00002        /*
00003        Default values are "localhost", "root",
00004        database "stest" and empty password.
00005        Change the MYSQL_TEST environment values
00006        if you want to use another configuration
00007        */
00008 
00009        $driver    = new mysqli_driver;
00010 
00011        $host      = getenv("MYSQL_TEST_HOST")     ? getenv("MYSQL_TEST_HOST") : "localhost";
00012        $port      = getenv("MYSQL_TEST_PORT")     ? getenv("MYSQL_TEST_PORT") : 3306;
00013        $user      = getenv("MYSQL_TEST_USER")     ? getenv("MYSQL_TEST_USER") : "root";
00014        $passwd    = getenv("MYSQL_TEST_PASSWD")   ? getenv("MYSQL_TEST_PASSWD") : "";
00015        $db        = getenv("MYSQL_TEST_DB")       ? getenv("MYSQL_TEST_DB") : "test";
00016        $engine    = getenv("MYSQL_TEST_ENGINE")   ? getenv("MYSQL_TEST_ENGINE") : "MyISAM";
00017        $socket    = getenv("MYSQL_TEST_SOCKET")   ? getenv("MYSQL_TEST_SOCKET") : null;
00018        $skip_on_connect_failure  = getenv("MYSQL_TEST_SKIP_CONNECT_FAILURE") ? getenv("MYSQL_TEST_SKIP_CONNECT_FAILURE") : true;
00019        $connect_flags = getenv("MYSQL_TEST_CONNECT_FLAGS") ? (int)getenv("MYSQL_TEST_CONNECT_FLAGS") : 0;
00020        if ($socket) {
00021               ini_set('mysqli.default_socket', $socket);
00022        }
00023 
00024        /* Development setting: test experimal features and/or feature requests that never worked before? */
00025        $TEST_EXPERIMENTAL = (in_array(getenv("MYSQL_TEST_EXPERIMENTAL"), array(0, 1))) ?
00026                             ((1 == getenv("MYSQL_TEST_EXPERIMENTAL")) ? true : false) :
00027                             false;
00028 
00029        $IS_MYSQLND = stristr(mysqli_get_client_info(), "mysqlnd");
00030        if (!$IS_MYSQLND) {
00031               $MYSQLND_VERSION = NULL;
00032        } else {
00033               /*
00034               The formatting of the version reported by mysqli_get_client_info()
00035               has changed significantly in the past. To get tests working properly
00036               with PHP 5.3.0 and up, we set everything that looks like prior to
00037               PHP 5.3.0 to version 5.0.4 = 5 * 10000 + 0 * 100 + 4 = 50004.
00038               PHP 5.3.0     reports mysqlnd 5.0.5 dev (= 5 * 10000 + 0 * 100 + 5 = 50005.
00039               */
00040               if (preg_match('@Revision:\s+(\d+)\s*\$@ism', mysqli_get_client_info(), $matches)) {
00041                      /* something prior to PHP 5.3.0 */
00042                      $MYSQLND_VERSION = 50004;
00043               } else if (preg_match('@^mysqlnd (\d+)\.(\d+)\.(\d+).*@ism', mysqli_get_client_info(), $matches)) {
00044                      /* formatting schema used by PHP 5.3.0 */
00045                      $MYSQLND_VERSION = (int)$matches[1] * 10000 + (int)$matches[2] * 100 + (int)$matches[3];
00046               } else if (preg_match('@^mysqlnd/PHP 6.0.0-dev@ism', mysqli_get_client_info(), $matches)) {
00047                      /*
00048                             PHP 6.0 at the time of the first PHP 5.3.0 release.
00049                             HEAD and 5.3 have been in sync when 5.3.0 was released.
00050                             It is at least 5.0.5-dev.
00051                      */
00052                      $MYSQLND_VERSION = 50005;
00053               } else {
00054                      /* unknown */
00055                      $MYSQLND_VERSION = -1;
00056               }
00057 
00058        }
00059 
00060        if (!function_exists('sys_get_temp_dir')) {
00061               function sys_get_temp_dir() {
00062 
00063                      if (!empty($_ENV['TMP']))
00064                             return realpath( $_ENV['TMP'] );
00065                      if (!empty($_ENV['TMPDIR']))
00066                             return realpath( $_ENV['TMPDIR'] );
00067                      if (!empty($_ENV['TEMP']))
00068                             return realpath( $_ENV['TEMP'] );
00069 
00070                      $temp_file = tempnam(md5(uniqid(rand(), TRUE)), '');
00071                      if ($temp_file) {
00072                             $temp_dir = realpath(dirname($temp_file));
00073                             unlink($temp_file);
00074                             return $temp_dir;
00075                      }
00076                      return FALSE;
00077               }
00078        }
00079 
00080        if (!function_exists('my_mysqli_connect')) {
00081 
00087               function my_mysqli_connect($host, $user, $passwd, $db, $port, $socket, $enable_env_flags = true) {
00088                      global $connect_flags;
00089 
00090                      $flags = ($enable_env_flags) ? $connect_flags : false;
00091 
00092                      if ($flags !== false) {
00093                             $link = mysqli_init();
00094                             if (!mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket, $flags))
00095                                    $link = false;
00096                      } else {
00097                             $link = mysqli_connect($host, $user, $passwd, $db, $port, $socket);
00098                      }
00099 
00100                      return $link;
00101               }
00102 
00108               function my_mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket, $flags = 0, $enable_env_flags = true) {
00109                      global $connect_flags;
00110 
00111                      if ($enable_env_flags)
00112                             $flags & $connect_flags;
00113 
00114                      return mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket, $flags);
00115               }
00116 
00117               class my_mysqli extends mysqli {
00118                      public function __construct($host, $user, $passwd, $db, $port, $socket, $enable_env_flags = true) {
00119                             global $connect_flags;
00120 
00121                             $flags = ($enable_env_flags) ? $connect_flags : false;
00122 
00123                             if ($flags !== false) {
00124                                    parent::init();
00125                                    $this->real_connect($host, $user, $passwd, $db, $port, $socket, $flags);
00126                             } else {
00127                                    parent::__construct($host, $user, $passwd, $db, $port, $socket);
00128                             }
00129                      }
00130               }
00131 
00132               function my_get_charsets($link) {
00133 
00134                      /* Those tree are set by SET NAMES */
00135                      $charsets = array(
00136                             'client'             => NULL,
00137                             'results'            => NULL,
00138                             'connection'  => NULL,
00139                      );
00140 
00141                      if (!($res = mysqli_query($link, "SHOW VARIABLES LIKE '%character%'"))) {
00142                             printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link));
00143                             return $charsets;
00144                      }
00145 
00146                      $names = array();
00147                      while ($row = mysqli_fetch_assoc($res)) {
00148                             $names[$row['Variable_name']] = $row['Value'];
00149                      }
00150                      mysqli_free_result($res);
00151 
00152                      if (!($res = mysqli_query($link, sprintf("SHOW CHARACTER SET LIKE '%s'", $names['character_set_client']))) ||
00153                             !($details = mysqli_fetch_assoc($res))) {
00154                             printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link));
00155                             return $charsets;
00156                      }
00157                      mysqli_free_result($res);
00158 
00159                      $charsets['client'] = array(
00160                             'charset'     => $details['Charset'],
00161                             'desc'        => $details['Description'],
00162                             'collation'   => $details['Default collation'],
00163                             'maxlen'      => $details['Maxlen'],
00164                             'nr'          => NULL,
00165                      );
00166 
00167                      if (!($res = mysqli_query($link, sprintf("SHOW COLLATION LIKE '%s'", $details['Default collation']))) ||
00168                             !($collation = mysqli_fetch_assoc($res))) {
00169                             printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link));
00170                             return $charsets;
00171                      }
00172                      mysqli_free_result($res);
00173                      $charsets['client']['nr'] = $collation['Id'];
00174 
00175                      if (!($res = mysqli_query($link, sprintf("SHOW CHARACTER SET LIKE '%s'", $names['character_set_results']))) ||
00176                             !($details = mysqli_fetch_assoc($res))) {
00177                             printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link));
00178                             return $charsets;
00179                      }
00180                      mysqli_free_result($res);
00181 
00182                      $charsets['results'] = array(
00183                             'charset'     => $details['Charset'],
00184                             'desc'        => $details['Description'],
00185                             'collation'   => $details['Default collation'],
00186                             'maxlen'      => $details['Maxlen'],
00187                             'nr'          => NULL,
00188                      );
00189 
00190                      if (!($res = mysqli_query($link, sprintf("SHOW COLLATION LIKE '%s'", $details['Default collation']))) ||
00191                             !($collation = mysqli_fetch_assoc($res))) {
00192                             printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link));
00193                             return $charsets;
00194                      }
00195                      mysqli_free_result($res);
00196                      $charsets['results']['nr'] = $collation['Id'];
00197 
00198 
00199                      if (!($res = mysqli_query($link, sprintf("SHOW CHARACTER SET LIKE '%s'", $names['character_set_connection']))) ||
00200                             !($details = mysqli_fetch_assoc($res))) {
00201                             printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link));
00202                             return $charsets;
00203                      }
00204                      mysqli_free_result($res);
00205 
00206                      $charsets['connection'] = array(
00207                             'charset'     => $details['Charset'],
00208                             'desc'        => $details['Description'],
00209                             'collation'   => $details['Default collation'],
00210                             'maxlen'      => $details['Maxlen'],
00211                             'nr'          => NULL,
00212                      );
00213 
00214                      if (!($res = mysqli_query($link, sprintf("SHOW COLLATION LIKE '%s'", $details['Default collation']))) ||
00215                             !($collation = mysqli_fetch_assoc($res))) {
00216                             printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link));
00217                             return $charsets;
00218                      }
00219                      mysqli_free_result($res);
00220                      $charsets['connection']['nr'] = $collation['Id'];
00221 
00222                      return $charsets;
00223               }
00224 
00225               function have_innodb($link) {
00226                 if (($res = $link->query("SHOW VARIABLES LIKE 'have_innodb'")) &&
00227                             ($row = $res->fetch_row()) &&
00228                             !empty($row)) {
00229                             if ($row[1] == "DISABLED" || $row[1] == "NO") {
00230                                    return false;
00231                             }
00232                             return true;
00233                 } else {
00234                             /* MySQL 5.6.1+ */
00235                             if ($res = $link->query("SHOW ENGINES")) {
00236                                    while ($row = $res->fetch_assoc()) {
00237                                           if (!isset($row['Engine']) || !isset($row['Support']))
00238                                                  return false;
00239 
00240                                           if (('InnoDB' == $row['Engine']) &&
00241                                                  (('YES' == $row['Support']) || ('DEFAULT' == $row['Support']))
00242                                                  ) {
00243                                                  return true;
00244                                           }
00245                                    }
00246                                    return false;
00247                             } else {
00248                                    return false;
00249                             }
00250                 }
00251                 return false;
00252               }
00253 
00254        } else {
00255               printf("skip Eeeek/BUG/FIXME - connect.inc included twice! skipif bug?\n");
00256        }
00257 
00258        function handle_catchable_fatal($errno, $error, $file, $line) {
00259               static $errcodes = array();
00260               if (empty($errcodes)) {
00261                      $constants = get_defined_constants();
00262                      foreach ($constants as $name => $value) {
00263                             if (substr($name, 0, 2) == "E_")
00264                                    $errcodes[$value] = $name;
00265                      }
00266               }
00267               printf("[%s] %s in %s on line %s\n",
00268                      (isset($errcodes[$errno])) ? $errcodes[$errno] : $errno,
00269                       $error, $file, $line);
00270 
00271               return true;
00272        }
00273 ?>