123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491 |
- <?php
- require_once 'PEAR.php';
- define('DB_OK', 1);
- define('DB_ERROR', -1);
- define('DB_ERROR_SYNTAX', -2);
- define('DB_ERROR_CONSTRAINT', -3);
- define('DB_ERROR_NOT_FOUND', -4);
- define('DB_ERROR_ALREADY_EXISTS', -5);
- define('DB_ERROR_UNSUPPORTED', -6);
- define('DB_ERROR_MISMATCH', -7);
- define('DB_ERROR_INVALID', -8);
- define('DB_ERROR_NOT_CAPABLE', -9);
- define('DB_ERROR_TRUNCATED', -10);
- define('DB_ERROR_INVALID_NUMBER', -11);
- define('DB_ERROR_INVALID_DATE', -12);
- define('DB_ERROR_DIVZERO', -13);
- define('DB_ERROR_NODBSELECTED', -14);
- define('DB_ERROR_CANNOT_CREATE', -15);
- define('DB_ERROR_CANNOT_DROP', -17);
- define('DB_ERROR_NOSUCHTABLE', -18);
- define('DB_ERROR_NOSUCHFIELD', -19);
- define('DB_ERROR_NEED_MORE_DATA', -20);
- define('DB_ERROR_NOT_LOCKED', -21);
- define('DB_ERROR_VALUE_COUNT_ON_ROW', -22);
- define('DB_ERROR_INVALID_DSN', -23);
- define('DB_ERROR_CONNECT_FAILED', -24);
- define('DB_ERROR_EXTENSION_NOT_FOUND',-25);
- define('DB_ERROR_ACCESS_VIOLATION', -26);
- define('DB_ERROR_NOSUCHDB', -27);
- define('DB_ERROR_CONSTRAINT_NOT_NULL',-29);
- define('DB_PARAM_SCALAR', 1);
- define('DB_PARAM_OPAQUE', 2);
- define('DB_PARAM_MISC', 3);
- define('DB_BINMODE_PASSTHRU', 1);
- define('DB_BINMODE_RETURN', 2);
- define('DB_BINMODE_CONVERT', 3);
- define('DB_FETCHMODE_DEFAULT', 0);
- define('DB_FETCHMODE_ORDERED', 1);
- define('DB_FETCHMODE_ASSOC', 2);
- define('DB_FETCHMODE_OBJECT', 3);
- define('DB_FETCHMODE_FLIPPED', 4);
- define('DB_GETMODE_ORDERED', DB_FETCHMODE_ORDERED);
- define('DB_GETMODE_ASSOC', DB_FETCHMODE_ASSOC);
- define('DB_GETMODE_FLIPPED', DB_FETCHMODE_FLIPPED);
- define('DB_TABLEINFO_ORDER', 1);
- define('DB_TABLEINFO_ORDERTABLE', 2);
- define('DB_TABLEINFO_FULL', 3);
- define('DB_AUTOQUERY_INSERT', 1);
- define('DB_AUTOQUERY_UPDATE', 2);
- define('DB_PORTABILITY_NONE', 0);
- define('DB_PORTABILITY_LOWERCASE', 1);
- define('DB_PORTABILITY_RTRIM', 2);
- define('DB_PORTABILITY_DELETE_COUNT', 4);
- define('DB_PORTABILITY_NUMROWS', 8);
- define('DB_PORTABILITY_ERRORS', 16);
- define('DB_PORTABILITY_NULL_TO_EMPTY', 32);
- define('DB_PORTABILITY_ALL', 63);
- class DB
- {
-
-
- public static function factory($type, $options = false)
- {
- if (!is_array($options)) {
- $options = array('persistent' => $options);
- }
- if (isset($options['debug']) && $options['debug'] >= 2) {
-
- include_once "DB/{$type}.php";
- } else {
- @include_once "DB/{$type}.php";
- }
- $classname = "DB_${type}";
- if (!class_exists($classname)) {
- $tmp = PEAR::raiseError(null, DB_ERROR_NOT_FOUND, null, null,
- "Unable to include the DB/{$type}.php"
- . " file for '$dsn'",
- 'DB_Error', true);
- return $tmp;
- }
- @$obj = new $classname;
- foreach ($options as $option => $value) {
- $test = $obj->setOption($option, $value);
- if (DB::isError($test)) {
- return $test;
- }
- }
- return $obj;
- }
-
-
-
- public static function connect($dsn, $options = array())
- {
- $dsninfo = DB::parseDSN($dsn);
- $type = $dsninfo['phptype'];
- if (!is_array($options)) {
-
- $options = array('persistent' => $options);
- }
- if (isset($options['debug']) && $options['debug'] >= 2) {
-
- include_once "DB/${type}.php";
- } else {
- @include_once "DB/${type}.php";
- }
- $classname = "DB_${type}";
- if (!class_exists($classname)) {
- $tmp = PEAR::raiseError(null, DB_ERROR_NOT_FOUND, null, null,
- "Unable to include the DB/{$type}.php"
- . " file for '"
- . DB::getDSNString($dsn, true) . "'",
- 'DB_Error', true);
- return $tmp;
- }
- @$obj = new $classname;
- foreach ($options as $option => $value) {
- $test = $obj->setOption($option, $value);
- if (DB::isError($test)) {
- return $test;
- }
- }
- $err = $obj->connect($dsninfo, $obj->getOption('persistent'));
- if (DB::isError($err)) {
- if (is_array($dsn)) {
- $err->addUserInfo(DB::getDSNString($dsn, true));
- } else {
- $err->addUserInfo($dsn);
- }
- return $err;
- }
- return $obj;
- }
-
-
-
- function apiVersion()
- {
- return '1.8.2';
- }
-
-
-
- public static function isError($value)
- {
- return is_object($value) && is_a($value, 'DB_Error');
- }
-
-
-
- public static function isConnection($value)
- {
- return (is_object($value) &&
- is_subclass_of($value, 'db_common') &&
- method_exists($value, 'simpleQuery'));
- }
-
-
-
- public static function isManip($query)
- {
- $manips = 'INSERT|UPDATE|DELETE|REPLACE|'
- . 'CREATE|DROP|'
- . 'LOAD DATA|SELECT .* INTO .* FROM|COPY|'
- . 'ALTER|GRANT|REVOKE|'
- . 'LOCK|UNLOCK';
- if (preg_match('/^\s*"?(' . $manips . ')\s+/i', $query)) {
- return true;
- }
- return false;
- }
-
-
-
- public static function errorMessage($value)
- {
- static $errorMessages;
- if (!isset($errorMessages)) {
- $errorMessages = array(
- DB_ERROR => 'unknown error',
- DB_ERROR_ACCESS_VIOLATION => 'insufficient permissions',
- DB_ERROR_ALREADY_EXISTS => 'already exists',
- DB_ERROR_CANNOT_CREATE => 'can not create',
- DB_ERROR_CANNOT_DROP => 'can not drop',
- DB_ERROR_CONNECT_FAILED => 'connect failed',
- DB_ERROR_CONSTRAINT => 'constraint violation',
- DB_ERROR_CONSTRAINT_NOT_NULL=> 'null value violates not-null constraint',
- DB_ERROR_DIVZERO => 'division by zero',
- DB_ERROR_EXTENSION_NOT_FOUND=> 'extension not found',
- DB_ERROR_INVALID => 'invalid',
- DB_ERROR_INVALID_DATE => 'invalid date or time',
- DB_ERROR_INVALID_DSN => 'invalid DSN',
- DB_ERROR_INVALID_NUMBER => 'invalid number',
- DB_ERROR_MISMATCH => 'mismatch',
- DB_ERROR_NEED_MORE_DATA => 'insufficient data supplied',
- DB_ERROR_NODBSELECTED => 'no database selected',
- DB_ERROR_NOSUCHDB => 'no such database',
- DB_ERROR_NOSUCHFIELD => 'no such field',
- DB_ERROR_NOSUCHTABLE => 'no such table',
- DB_ERROR_NOT_CAPABLE => 'DB backend not capable',
- DB_ERROR_NOT_FOUND => 'not found',
- DB_ERROR_NOT_LOCKED => 'not locked',
- DB_ERROR_SYNTAX => 'syntax error',
- DB_ERROR_UNSUPPORTED => 'not supported',
- DB_ERROR_TRUNCATED => 'truncated',
- DB_ERROR_VALUE_COUNT_ON_ROW => 'value count on row',
- DB_OK => 'no error',
- );
- }
- if (DB::isError($value)) {
- $value = $value->getCode();
- }
- return isset($errorMessages[$value]) ? $errorMessages[$value]
- : $errorMessages[DB_ERROR];
- }
-
-
-
- public static function parseDSN($dsn)
- {
- $parsed = array(
- 'phptype' => false,
- 'dbsyntax' => false,
- 'username' => false,
- 'password' => false,
- 'protocol' => false,
- 'hostspec' => false,
- 'port' => false,
- 'socket' => false,
- 'database' => false,
- );
- if (is_array($dsn)) {
- $dsn = array_merge($parsed, $dsn);
- if (!$dsn['dbsyntax']) {
- $dsn['dbsyntax'] = $dsn['phptype'];
- }
- return $dsn;
- }
-
- if (($pos = strpos($dsn, '://')) !== false) {
- $str = substr($dsn, 0, $pos);
- $dsn = substr($dsn, $pos + 3);
- } else {
- $str = $dsn;
- $dsn = null;
- }
-
-
- if (preg_match('|^(.+?)\((.*?)\)$|', $str, $arr)) {
- $parsed['phptype'] = $arr[1];
- $parsed['dbsyntax'] = !$arr[2] ? $arr[1] : $arr[2];
- } else {
- $parsed['phptype'] = $str;
- $parsed['dbsyntax'] = $str;
- }
- if (!count($dsn)) {
- return $parsed;
- }
-
-
- if (($at = strrpos($dsn,'@')) !== false) {
- $str = substr($dsn, 0, $at);
- $dsn = substr($dsn, $at + 1);
- if (($pos = strpos($str, ':')) !== false) {
- $parsed['username'] = rawurldecode(substr($str, 0, $pos));
- $parsed['password'] = rawurldecode(substr($str, $pos + 1));
- } else {
- $parsed['username'] = rawurldecode($str);
- }
- }
-
- if (preg_match('|^([^(]+)\((.*?)\)/?(.*?)$|', $dsn, $match)) {
-
- $proto = $match[1];
- $proto_opts = $match[2] ? $match[2] : false;
- $dsn = $match[3];
- } else {
-
- if (strpos($dsn, '+') !== false) {
- list($proto, $dsn) = explode('+', $dsn, 2);
- }
- if (strpos($dsn, '/') !== false) {
- list($proto_opts, $dsn) = explode('/', $dsn, 2);
- } else {
- $proto_opts = $dsn;
- $dsn = null;
- }
- }
-
- $parsed['protocol'] = (!empty($proto)) ? $proto : 'tcp';
- $proto_opts = rawurldecode($proto_opts);
- if (strpos($proto_opts, ':') !== false) {
- list($proto_opts, $parsed['port']) = explode(':', $proto_opts);
- }
- if ($parsed['protocol'] == 'tcp') {
- $parsed['hostspec'] = $proto_opts;
- } elseif ($parsed['protocol'] == 'unix') {
- $parsed['socket'] = $proto_opts;
- }
-
-
- if ($dsn) {
- if (($pos = strpos($dsn, '?')) === false) {
-
- $parsed['database'] = rawurldecode($dsn);
- } else {
-
- $parsed['database'] = rawurldecode(substr($dsn, 0, $pos));
- $dsn = substr($dsn, $pos + 1);
- if (strpos($dsn, '&') !== false) {
- $opts = explode('&', $dsn);
- } else {
- $opts = array($dsn);
- }
- foreach ($opts as $opt) {
- list($key, $value) = explode('=', $opt);
- if (!isset($parsed[$key])) {
-
- $parsed[$key] = rawurldecode($value);
- }
- }
- }
- }
- return $parsed;
- }
-
-
-
- public static function getDSNString($dsn, $hidePassword) {
-
- $dsnArray = DB::parseDSN($dsn);
-
- if ($hidePassword) {
- $dsnArray['password'] = 'PASSWORD';
- }
-
- if (is_string($dsn) && strpos($dsn, 'tcp') === false && $dsnArray['protocol'] == 'tcp') {
- $dsnArray['protocol'] = false;
- }
-
-
- $dsnString = $dsnArray['phptype'];
- if ($dsnArray['dbsyntax']) {
- $dsnString .= '('.$dsnArray['dbsyntax'].')';
- }
- $dsnString .= '://'
- .$dsnArray['username']
- .':'
- .$dsnArray['password']
- .'@'
- .$dsnArray['protocol'];
- if ($dsnArray['socket']) {
- $dsnString .= '('.$dsnArray['socket'].')';
- }
- if ($dsnArray['protocol'] && $dsnArray['hostspec']) {
- $dsnString .= '+';
- }
- $dsnString .= $dsnArray['hostspec'];
- if ($dsnArray['port']) {
- $dsnString .= ':'.$dsnArray['port'];
- }
- $dsnString .= '/'.$dsnArray['database'];
-
-
- unset($dsnArray['phptype'],
- $dsnArray['dbsyntax'],
- $dsnArray['username'],
- $dsnArray['password'],
- $dsnArray['protocol'],
- $dsnArray['socket'],
- $dsnArray['hostspec'],
- $dsnArray['port'],
- $dsnArray['database']
- );
- if (count($dsnArray) > 0) {
- $dsnString .= '?';
- $i = 0;
- foreach ($dsnArray as $key => $value) {
- if (++$i > 1) {
- $dsnString .= '&';
- }
- $dsnString .= $key.'='.$value;
- }
- }
- return $dsnString;
- }
-
-
- }
- class DB_Error extends PEAR_Error
- {
-
-
- function DB_Error($code = DB_ERROR, $mode = PEAR_ERROR_RETURN,
- $level = E_USER_NOTICE, $debuginfo = null)
- {
- if (is_int($code)) {
- $this->PEAR_Error('DB Error: ' . DB::errorMessage($code), $code,
- $mode, $level, $debuginfo);
- } else {
- $this->PEAR_Error("DB Error: $code", DB_ERROR,
- $mode, $level, $debuginfo);
- }
- }
-
- }
- class DB_result
- {
-
-
- var $autofree;
-
- var $dbh;
-
- var $fetchmode;
-
- var $fetchmode_object_class;
-
- var $limit_count = null;
-
- var $limit_from = null;
-
- var $parameters;
-
- var $query;
-
- var $result;
-
- var $row_counter = null;
-
- var $statement;
-
-
-
- function DB_result(&$dbh, $result, $options = array())
- {
- $this->autofree = $dbh->options['autofree'];
- $this->dbh = &$dbh;
- $this->fetchmode = $dbh->fetchmode;
- $this->fetchmode_object_class = $dbh->fetchmode_object_class;
- $this->parameters = $dbh->last_parameters;
- $this->query = $dbh->last_query;
- $this->result = $result;
- $this->statement = empty($dbh->last_stmt) ? null : $dbh->last_stmt;
- foreach ($options as $key => $value) {
- $this->setOption($key, $value);
- }
- }
-
- function setOption($key, $value = null)
- {
- switch ($key) {
- case 'limit_from':
- $this->limit_from = $value;
- break;
- case 'limit_count':
- $this->limit_count = $value;
- }
- }
-
-
-
- function &fetchRow($fetchmode = DB_FETCHMODE_DEFAULT, $rownum = null)
- {
- if ($fetchmode === DB_FETCHMODE_DEFAULT) {
- $fetchmode = $this->fetchmode;
- }
- if ($fetchmode === DB_FETCHMODE_OBJECT) {
- $fetchmode = DB_FETCHMODE_ASSOC;
- $object_class = $this->fetchmode_object_class;
- }
- if (is_null($rownum) && $this->limit_from !== null) {
- if ($this->row_counter === null) {
- $this->row_counter = $this->limit_from;
-
- if ($this->dbh->features['limit'] === false) {
- $i = 0;
- while ($i++ < $this->limit_from) {
- $this->dbh->fetchInto($this->result, $arr, $fetchmode);
- }
- }
- }
- if ($this->row_counter >= ($this->limit_from + $this->limit_count))
- {
- if ($this->autofree) {
- $this->free();
- }
- $tmp = null;
- return $tmp;
- }
- if ($this->dbh->features['limit'] === 'emulate') {
- $rownum = $this->row_counter;
- }
- $this->row_counter++;
- }
- $res = $this->dbh->fetchInto($this->result, $arr, $fetchmode, $rownum);
- if ($res === DB_OK) {
- if (isset($object_class)) {
-
-
- if ($object_class == 'stdClass') {
- $arr = (object) $arr;
- } else {
- $arr = new $object_class($arr);
- }
- }
- return $arr;
- }
- if ($res == null && $this->autofree) {
- $this->free();
- }
- return $res;
- }
-
-
-
- function fetchInto(&$arr, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum = null)
- {
- if ($fetchmode === DB_FETCHMODE_DEFAULT) {
- $fetchmode = $this->fetchmode;
- }
- if ($fetchmode === DB_FETCHMODE_OBJECT) {
- $fetchmode = DB_FETCHMODE_ASSOC;
- $object_class = $this->fetchmode_object_class;
- }
- if (is_null($rownum) && $this->limit_from !== null) {
- if ($this->row_counter === null) {
- $this->row_counter = $this->limit_from;
-
- if ($this->dbh->features['limit'] === false) {
- $i = 0;
- while ($i++ < $this->limit_from) {
- $this->dbh->fetchInto($this->result, $arr, $fetchmode);
- }
- }
- }
- if ($this->row_counter >= (
- $this->limit_from + $this->limit_count))
- {
- if ($this->autofree) {
- $this->free();
- }
- return null;
- }
- if ($this->dbh->features['limit'] === 'emulate') {
- $rownum = $this->row_counter;
- }
- $this->row_counter++;
- }
- $res = $this->dbh->fetchInto($this->result, $arr, $fetchmode, $rownum);
- if ($res === DB_OK) {
- if (isset($object_class)) {
-
-
- if ($object_class == 'stdClass') {
- $arr = (object) $arr;
- } else {
- $arr = new $object_class($arr);
- }
- }
- return DB_OK;
- }
- if ($res == null && $this->autofree) {
- $this->free();
- }
- return $res;
- }
-
-
-
- function numCols()
- {
- return $this->dbh->numCols($this->result);
- }
-
-
-
- function numRows()
- {
- if ($this->dbh->features['numrows'] === 'emulate'
- && $this->dbh->options['portability'] & DB_PORTABILITY_NUMROWS)
- {
- if ($this->dbh->features['prepare']) {
- $res = $this->dbh->query($this->query, $this->parameters);
- } else {
- $res = $this->dbh->query($this->query);
- }
- if (DB::isError($res)) {
- return $res;
- }
- $i = 0;
- while ($res->fetchInto($tmp, DB_FETCHMODE_ORDERED)) {
- $i++;
- }
- $count = $i;
- } else {
- $count = $this->dbh->numRows($this->result);
- }
-
- if (($this->dbh->features['limit'] === 'emulate'
- && $this->limit_from !== null)
- || $this->dbh->phptype == 'fbsql') {
- $limit_count = is_null($this->limit_count) ? $count : $this->limit_count;
- if ($count < $this->limit_from) {
- $count = 0;
- } elseif ($count < ($this->limit_from + $limit_count)) {
- $count -= $this->limit_from;
- } else {
- $count = $limit_count;
- }
- }
- return $count;
- }
-
-
-
- function nextResult()
- {
- return $this->dbh->nextResult($this->result);
- }
-
-
-
- function free()
- {
- $err = $this->dbh->freeResult($this->result);
- if (DB::isError($err)) {
- return $err;
- }
- $this->result = false;
- $this->statement = false;
- return true;
- }
-
-
-
- function tableInfo($mode = null)
- {
- if (is_string($mode)) {
- return $this->dbh->raiseError(DB_ERROR_NEED_MORE_DATA);
- }
- return $this->dbh->tableInfo($this, $mode);
- }
-
-
-
- function getQuery()
- {
- return $this->query;
- }
-
-
-
- function getRowCounter()
- {
- return $this->row_counter;
- }
-
- }
- class DB_row
- {
-
-
- function DB_row(&$arr)
- {
- foreach ($arr as $key => $value) {
- $this->$key = &$arr[$key];
- }
- }
-
- }
- ?>
|