diff options
-rw-r--r-- | extlib/DB.php | 1489 | ||||
-rw-r--r-- | extlib/DB/DataObject.php | 4152 | ||||
-rw-r--r-- | extlib/Mail.php | 238 | ||||
-rw-r--r-- | extlib/Validate.php | 1051 |
4 files changed, 6930 insertions, 0 deletions
diff --git a/extlib/DB.php b/extlib/DB.php new file mode 100644 index 000000000..a511979e6 --- /dev/null +++ b/extlib/DB.php @@ -0,0 +1,1489 @@ +<?php + +/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ + +/** + * Database independent query interface + * + * PHP versions 4 and 5 + * + * LICENSE: This source file is subject to version 3.0 of the PHP license + * that is available through the world-wide-web at the following URI: + * http://www.php.net/license/3_0.txt. If you did not receive a copy of + * the PHP License and are unable to obtain it through the web, please + * send a note to license@php.net so we can mail you a copy immediately. + * + * @category Database + * @package DB + * @author Stig Bakken <ssb@php.net> + * @author Tomas V.V.Cox <cox@idecnet.com> + * @author Daniel Convissor <danielc@php.net> + * @copyright 1997-2007 The PHP Group + * @license http://www.php.net/license/3_0.txt PHP License 3.0 + * @version CVS: $Id: DB.php,v 1.88 2007/08/12 05:27:25 aharvey Exp $ + * @link http://pear.php.net/package/DB + */ + +/** + * Obtain the PEAR class so it can be extended from + */ +require_once 'PEAR.php'; + + +// {{{ constants +// {{{ error codes + +/**#@+ + * One of PEAR DB's portable error codes. + * @see DB_common::errorCode(), DB::errorMessage() + * + * {@internal If you add an error code here, make sure you also add a textual + * version of it in DB::errorMessage().}} + */ + +/** + * The code returned by many methods upon success + */ +define('DB_OK', 1); + +/** + * Unkown error + */ +define('DB_ERROR', -1); + +/** + * Syntax error + */ +define('DB_ERROR_SYNTAX', -2); + +/** + * Tried to insert a duplicate value into a primary or unique index + */ +define('DB_ERROR_CONSTRAINT', -3); + +/** + * An identifier in the query refers to a non-existant object + */ +define('DB_ERROR_NOT_FOUND', -4); + +/** + * Tried to create a duplicate object + */ +define('DB_ERROR_ALREADY_EXISTS', -5); + +/** + * The current driver does not support the action you attempted + */ +define('DB_ERROR_UNSUPPORTED', -6); + +/** + * The number of parameters does not match the number of placeholders + */ +define('DB_ERROR_MISMATCH', -7); + +/** + * A literal submitted did not match the data type expected + */ +define('DB_ERROR_INVALID', -8); + +/** + * The current DBMS does not support the action you attempted + */ +define('DB_ERROR_NOT_CAPABLE', -9); + +/** + * A literal submitted was too long so the end of it was removed + */ +define('DB_ERROR_TRUNCATED', -10); + +/** + * A literal number submitted did not match the data type expected + */ +define('DB_ERROR_INVALID_NUMBER', -11); + +/** + * A literal date submitted did not match the data type expected + */ +define('DB_ERROR_INVALID_DATE', -12); + +/** + * Attempt to divide something by zero + */ +define('DB_ERROR_DIVZERO', -13); + +/** + * A database needs to be selected + */ +define('DB_ERROR_NODBSELECTED', -14); + +/** + * Could not create the object requested + */ +define('DB_ERROR_CANNOT_CREATE', -15); + +/** + * Could not drop the database requested because it does not exist + */ +define('DB_ERROR_CANNOT_DROP', -17); + +/** + * An identifier in the query refers to a non-existant table + */ +define('DB_ERROR_NOSUCHTABLE', -18); + +/** + * An identifier in the query refers to a non-existant column + */ +define('DB_ERROR_NOSUCHFIELD', -19); + +/** + * The data submitted to the method was inappropriate + */ +define('DB_ERROR_NEED_MORE_DATA', -20); + +/** + * The attempt to lock the table failed + */ +define('DB_ERROR_NOT_LOCKED', -21); + +/** + * The number of columns doesn't match the number of values + */ +define('DB_ERROR_VALUE_COUNT_ON_ROW', -22); + +/** + * The DSN submitted has problems + */ +define('DB_ERROR_INVALID_DSN', -23); + +/** + * Could not connect to the database + */ +define('DB_ERROR_CONNECT_FAILED', -24); + +/** + * The PHP extension needed for this DBMS could not be found + */ +define('DB_ERROR_EXTENSION_NOT_FOUND',-25); + +/** + * The present user has inadequate permissions to perform the task requestd + */ +define('DB_ERROR_ACCESS_VIOLATION', -26); + +/** + * The database requested does not exist + */ +define('DB_ERROR_NOSUCHDB', -27); + +/** + * Tried to insert a null value into a column that doesn't allow nulls + */ +define('DB_ERROR_CONSTRAINT_NOT_NULL',-29); +/**#@-*/ + + +// }}} +// {{{ prepared statement-related + + +/**#@+ + * Identifiers for the placeholders used in prepared statements. + * @see DB_common::prepare() + */ + +/** + * Indicates a scalar (<kbd>?</kbd>) placeholder was used + * + * Quote and escape the value as necessary. + */ +define('DB_PARAM_SCALAR', 1); + +/** + * Indicates an opaque (<kbd>&</kbd>) placeholder was used + * + * The value presented is a file name. Extract the contents of that file + * and place them in this column. + */ +define('DB_PARAM_OPAQUE', 2); + +/** + * Indicates a misc (<kbd>!</kbd>) placeholder was used + * + * The value should not be quoted or escaped. + */ +define('DB_PARAM_MISC', 3); +/**#@-*/ + + +// }}} +// {{{ binary data-related + + +/**#@+ + * The different ways of returning binary data from queries. + */ + +/** + * Sends the fetched data straight through to output + */ +define('DB_BINMODE_PASSTHRU', 1); + +/** + * Lets you return data as usual + */ +define('DB_BINMODE_RETURN', 2); + +/** + * Converts the data to hex format before returning it + * + * For example the string "123" would become "313233". + */ +define('DB_BINMODE_CONVERT', 3); +/**#@-*/ + + +// }}} +// {{{ fetch modes + + +/**#@+ + * Fetch Modes. + * @see DB_common::setFetchMode() + */ + +/** + * Indicates the current default fetch mode should be used + * @see DB_common::$fetchmode + */ +define('DB_FETCHMODE_DEFAULT', 0); + +/** + * Column data indexed by numbers, ordered from 0 and up + */ +define('DB_FETCHMODE_ORDERED', 1); + +/** + * Column data indexed by column names + */ +define('DB_FETCHMODE_ASSOC', 2); + +/** + * Column data as object properties + */ +define('DB_FETCHMODE_OBJECT', 3); + +/** + * For multi-dimensional results, make the column name the first level + * of the array and put the row number in the second level of the array + * + * This is flipped from the normal behavior, which puts the row numbers + * in the first level of the array and the column names in the second level. + */ +define('DB_FETCHMODE_FLIPPED', 4); +/**#@-*/ + +/**#@+ + * Old fetch modes. Left here for compatibility. + */ +define('DB_GETMODE_ORDERED', DB_FETCHMODE_ORDERED); +define('DB_GETMODE_ASSOC', DB_FETCHMODE_ASSOC); +define('DB_GETMODE_FLIPPED', DB_FETCHMODE_FLIPPED); +/**#@-*/ + + +// }}} +// {{{ tableInfo() && autoPrepare()-related + + +/**#@+ + * The type of information to return from the tableInfo() method. + * + * Bitwised constants, so they can be combined using <kbd>|</kbd> + * and removed using <kbd>^</kbd>. + * + * @see DB_common::tableInfo() + * + * {@internal Since the TABLEINFO constants are bitwised, if more of them are + * added in the future, make sure to adjust DB_TABLEINFO_FULL accordingly.}} + */ +define('DB_TABLEINFO_ORDER', 1); +define('DB_TABLEINFO_ORDERTABLE', 2); +define('DB_TABLEINFO_FULL', 3); +/**#@-*/ + + +/**#@+ + * The type of query to create with the automatic query building methods. + * @see DB_common::autoPrepare(), DB_common::autoExecute() + */ +define('DB_AUTOQUERY_INSERT', 1); +define('DB_AUTOQUERY_UPDATE', 2); +/**#@-*/ + + +// }}} +// {{{ portability modes + + +/**#@+ + * Portability Modes. + * + * Bitwised constants, so they can be combined using <kbd>|</kbd> + * and removed using <kbd>^</kbd>. + * + * @see DB_common::setOption() + * + * {@internal Since the PORTABILITY constants are bitwised, if more of them are + * added in the future, make sure to adjust DB_PORTABILITY_ALL accordingly.}} + */ + +/** + * Turn off all portability features + */ +define('DB_PORTABILITY_NONE', 0); + +/** + * Convert names of tables and fields to lower case + * when using the get*(), fetch*() and tableInfo() methods + */ +define('DB_PORTABILITY_LOWERCASE', 1); + +/** + * Right trim the data output by get*() and fetch*() + */ +define('DB_PORTABILITY_RTRIM', 2); + +/** + * Force reporting the number of rows deleted + */ +define('DB_PORTABILITY_DELETE_COUNT', 4); + +/** + * Enable hack that makes numRows() work in Oracle + */ +define('DB_PORTABILITY_NUMROWS', 8); + +/** + * Makes certain error messages in certain drivers compatible + * with those from other DBMS's + * + * + mysql, mysqli: change unique/primary key constraints + * DB_ERROR_ALREADY_EXISTS -> DB_ERROR_CONSTRAINT + * + * + odbc(access): MS's ODBC driver reports 'no such field' as code + * 07001, which means 'too few parameters.' When this option is on + * that code gets mapped to DB_ERROR_NOSUCHFIELD. + */ +define('DB_PORTABILITY_ERRORS', 16); + +/** + * Convert null values to empty strings in data output by + * get*() and fetch*() + */ +define('DB_PORTABILITY_NULL_TO_EMPTY', 32); + +/** + * Turn on all portability features + */ +define('DB_PORTABILITY_ALL', 63); +/**#@-*/ + +// }}} + + +// }}} +// {{{ class DB + +/** + * Database independent query interface + * + * The main "DB" class is simply a container class with some static + * methods for creating DB objects as well as some utility functions + * common to all parts of DB. + * + * The object model of DB is as follows (indentation means inheritance): + * <pre> + * DB The main DB class. This is simply a utility class + * with some "static" methods for creating DB objects as + * well as common utility functions for other DB classes. + * + * DB_common The base for each DB implementation. Provides default + * | implementations (in OO lingo virtual methods) for + * | the actual DB implementations as well as a bunch of + * | query utility functions. + * | + * +-DB_mysql The DB implementation for MySQL. Inherits DB_common. + * When calling DB::factory or DB::connect for MySQL + * connections, the object returned is an instance of this + * class. + * </pre> + * + * @category Database + * @package DB + * @author Stig Bakken <ssb@php.net> + * @author Tomas V.V.Cox <cox@idecnet.com> + * @author Daniel Convissor <danielc@php.net> + * @copyright 1997-2007 The PHP Group + * @license http://www.php.net/license/3_0.txt PHP License 3.0 + * @version Release: 1.7.13 + * @link http://pear.php.net/package/DB + */ +class DB +{ + // {{{ &factory() + + /** + * Create a new DB object for the specified database type but don't + * connect to the database + * + * @param string $type the database type (eg "mysql") + * @param array $options an associative array of option names and values + * + * @return object a new DB object. A DB_Error object on failure. + * + * @see DB_common::setOption() + */ + function &factory($type, $options = false) + { + if (!is_array($options)) { + $options = array('persistent' => $options); + } + + if (isset($options['debug']) && $options['debug'] >= 2) { + // expose php errors with sufficient debug level + 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; + } + + // }}} + // {{{ &connect() + + /** + * Create a new DB object including a connection to the specified database + * + * Example 1. + * <code> + * require_once 'DB.php'; + * + * $dsn = 'pgsql://user:password@host/database'; + * $options = array( + * 'debug' => 2, + * 'portability' => DB_PORTABILITY_ALL, + * ); + * + * $db =& DB::connect($dsn, $options); + * if (PEAR::isError($db)) { + * die($db->getMessage()); + * } + * </code> + * + * @param mixed $dsn the string "data source name" or array in the + * format returned by DB::parseDSN() + * @param array $options an associative array of option names and values + * + * @return object a new DB object. A DB_Error object on failure. + * + * @uses DB_dbase::connect(), DB_fbsql::connect(), DB_ibase::connect(), + * DB_ifx::connect(), DB_msql::connect(), DB_mssql::connect(), + * DB_mysql::connect(), DB_mysqli::connect(), DB_oci8::connect(), + * DB_odbc::connect(), DB_pgsql::connect(), DB_sqlite::connect(), + * DB_sybase::connect() + * + * @uses DB::parseDSN(), DB_common::setOption(), PEAR::isError() + */ + function &connect($dsn, $options = array()) + { + $dsninfo = DB::parseDSN($dsn); + $type = $dsninfo['phptype']; + + if (!is_array($options)) { + /* + * For backwards compatibility. $options used to be boolean, + * indicating whether the connection should be persistent. + */ + $options = array('persistent' => $options); + } + + if (isset($options['debug']) && $options['debug'] >= 2) { + // expose php errors with sufficient debug level + 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; + } + } + + $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; + } + + // }}} + // {{{ apiVersion() + + /** + * Return the DB API version + * + * @return string the DB API version number + */ + function apiVersion() + { + return '1.7.13'; + } + + // }}} + // {{{ isError() + + /** + * Determines if a variable is a DB_Error object + * + * @param mixed $value the variable to check + * + * @return bool whether $value is DB_Error object + */ + function isError($value) + { + return is_a($value, 'DB_Error'); + } + + // }}} + // {{{ isConnection() + + /** + * Determines if a value is a DB_<driver> object + * + * @param mixed $value the value to test + * + * @return bool whether $value is a DB_<driver> object + */ + function isConnection($value) + { + return (is_object($value) && + is_subclass_of($value, 'db_common') && + method_exists($value, 'simpleQuery')); + } + + // }}} + // {{{ isManip() + + /** + * Tell whether a query is a data manipulation or data definition query + * + * Examples of data manipulation queries are INSERT, UPDATE and DELETE. + * Examples of data definition queries are CREATE, DROP, ALTER, GRANT, + * REVOKE. + * + * @param string $query the query + * + * @return boolean whether $query is a data manipulation query + */ + 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; + } + + // }}} + // {{{ errorMessage() + + /** + * Return a textual error message for a DB error code + * + * @param integer $value the DB error code + * + * @return string the error message or false if the error code was + * not recognized + */ + 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]; + } + + // }}} + // {{{ parseDSN() + + /** + * Parse a data source name + * + * Additional keys can be added by appending a URI query string to the + * end of the DSN. + * + * The format of the supplied DSN is in its fullest form: + * <code> + * phptype(dbsyntax)://username:password@protocol+hostspec/database?option=8&another=true + * </code> + * + * Most variations are allowed: + * <code> + * phptype://username:password@protocol+hostspec:110//usr/db_file.db?mode=0644 + * phptype://username:password@hostspec/database_name + * phptype://username:password@hostspec + * phptype://username@hostspec + * phptype://hostspec/database + * phptype://hostspec + * phptype(dbsyntax) + * phptype + * </code> + * + * @param string $dsn Data Source Name to be parsed + * + * @return array an associative array with the following keys: + * + phptype: Database backend used in PHP (mysql, odbc etc.) + * + dbsyntax: Database used with regards to SQL syntax etc. + * + protocol: Communication protocol to use (tcp, unix etc.) + * + hostspec: Host specification (hostname[:port]) + * + database: Database to use on the DBMS server + * + username: User name for login + * + password: Password for login + */ + 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; + } + + // Find phptype and dbsyntax + if (($pos = strpos($dsn, '://')) !== false) { + $str = substr($dsn, 0, $pos); + $dsn = substr($dsn, $pos + 3); + } else { + $str = $dsn; + $dsn = null; + } + + // Get phptype and dbsyntax + // $str => phptype(dbsyntax) + 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; + } + + // Get (if found): username and password + // $dsn => username:password@protocol+hostspec/database + 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); + } + } + + // Find protocol and hostspec + + if (preg_match('|^([^(]+)\((.*?)\)/?(.*?)$|', $dsn, $match)) { + // $dsn => proto(proto_opts)/database + $proto = $match[1]; + $proto_opts = $match[2] ? $match[2] : false; + $dsn = $match[3]; + + } else { + // $dsn => protocol+hostspec/database (old format) + 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; + } + } + + // process the different protocol options + $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; + } + + // Get dabase if any + // $dsn => database + if ($dsn) { + if (($pos = strpos($dsn, '?')) === false) { + // /database + $parsed['database'] = rawurldecode($dsn); + } else { + // /database?param1=value1¶m2=value2 + $parsed['database'] = rawurldecode(substr($dsn, 0, $pos)); + $dsn = substr($dsn, $pos + 1); + if (strpos($dsn, '&') !== false) { + $opts = explode('&', $dsn); + } else { // database?param1=value1 + $opts = array($dsn); + } + foreach ($opts as $opt) { + list($key, $value) = explode('=', $opt); + if (!isset($parsed[$key])) { + // don't allow params overwrite + $parsed[$key] = rawurldecode($value); + } + } + } + } + + return $parsed; + } + + // }}} + // {{{ getDSNString() + + /** + * Returns the given DSN in a string format suitable for output. + * + * @param array|string the DSN to parse and format + * @param boolean true to hide the password, false to include it + * @return string + */ + function getDSNString($dsn, $hidePassword) { + /* Calling parseDSN will ensure that we have all the array elements + * defined, and means that we deal with strings and array in the same + * manner. */ + $dsnArray = DB::parseDSN($dsn); + + if ($hidePassword) { + $dsnArray['password'] = 'PASSWORD'; + } + + /* Protocol is special-cased, as using the default "tcp" along with an + * Oracle TNS connection string fails. */ + if (is_string($dsn) && strpos($dsn, 'tcp') === false && $dsnArray['protocol'] == 'tcp') { + $dsnArray['protocol'] = false; + } + + // Now we just have to construct the actual string. This is ugly. + $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']; + + /* Option handling. Unfortunately, parseDSN simply places options into + * the top-level array, so we'll first get rid of the fields defined by + * DB and see what's left. */ + 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 + +/** + * DB_Error implements a class for reporting portable database error + * messages + * + * @category Database + * @package DB + * @author Stig Bakken <ssb@php.net> + * @copyright 1997-2007 The PHP Group + * @license http://www.php.net/license/3_0.txt PHP License 3.0 + * @version Release: 1.7.13 + * @link http://pear.php.net/package/DB + */ +class DB_Error extends PEAR_Error +{ + // {{{ constructor + + /** + * DB_Error constructor + * + * @param mixed $code DB error code, or string with error message + * @param int $mode what "error mode" to operate in + * @param int $level what error level to use for $mode & + * PEAR_ERROR_TRIGGER + * @param mixed $debuginfo additional debug info, such as the last query + * + * @see 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 + +/** + * This class implements a wrapper for a DB result set + * + * A new instance of this class will be returned by the DB implementation + * after processing a query that returns data. + * + * @category Database + * @package DB + * @author Stig Bakken <ssb@php.net> + * @copyright 1997-2007 The PHP Group + * @license http://www.php.net/license/3_0.txt PHP License 3.0 + * @version Release: 1.7.13 + * @link http://pear.php.net/package/DB + */ +class DB_result +{ + // {{{ properties + + /** + * Should results be freed automatically when there are no more rows? + * @var boolean + * @see DB_common::$options + */ + var $autofree; + + /** + * A reference to the DB_<driver> object + * @var object + */ + var $dbh; + + /** + * The current default fetch mode + * @var integer + * @see DB_common::$fetchmode + */ + var $fetchmode; + + /** + * The name of the class into which results should be fetched when + * DB_FETCHMODE_OBJECT is in effect + * + * @var string + * @see DB_common::$fetchmode_object_class + */ + var $fetchmode_object_class; + + /** + * The number of rows to fetch from a limit query + * @var integer + */ + var $limit_count = null; + + /** + * The row to start fetching from in limit queries + * @var integer + */ + var $limit_from = null; + + /** + * The execute parameters that created this result + * @var array + * @since Property available since Release 1.7.0 + */ + var $parameters; + + /** + * The query string that created this result + * + * Copied here incase it changes in $dbh, which is referenced + * + * @var string + * @since Property available since Release 1.7.0 + */ + var $query; + + /** + * The query result resource id created by PHP + * @var resource + */ + var $result; + + /** + * The present row being dealt with + * @var integer + */ + var $row_counter = null; + + /** + * The prepared statement resource id created by PHP in $dbh + * + * This resource is only available when the result set was created using + * a driver's native execute() method, not PEAR DB's emulated one. + * + * Copied here incase it changes in $dbh, which is referenced + * + * {@internal Mainly here because the InterBase/Firebird API is only + * able to retrieve data from result sets if the statemnt handle is + * still in scope.}} + * + * @var resource + * @since Property available since Release 1.7.0 + */ + var $statement; + + + // }}} + // {{{ constructor + + /** + * This constructor sets the object's properties + * + * @param object &$dbh the DB object reference + * @param resource $result the result resource id + * @param array $options an associative array with result options + * + * @return void + */ + 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); + } + } + + /** + * Set options for the DB_result object + * + * @param string $key the option to set + * @param mixed $value the value to set the option to + * + * @return void + */ + function setOption($key, $value = null) + { + switch ($key) { + case 'limit_from': + $this->limit_from = $value; + break; + case 'limit_count': + $this->limit_count = $value; + } + } + + // }}} + // {{{ fetchRow() + + /** + * Fetch a row of data and return it by reference into an array + * + * The type of array returned can be controlled either by setting this + * method's <var>$fetchmode</var> parameter or by changing the default + * fetch mode setFetchMode() before calling this method. + * + * There are two options for standardizing the information returned + * from databases, ensuring their values are consistent when changing + * DBMS's. These portability options can be turned on when creating a + * new DB object or by using setOption(). + * + * + <var>DB_PORTABILITY_LOWERCASE</var> + * convert names of fields to lower case + * + * + <var>DB_PORTABILITY_RTRIM</var> + * right trim the data + * + * @param int $fetchmode the constant indicating how to format the data + * @param int $rownum the row number to fetch (index starts at 0) + * + * @return mixed an array or object containing the row's data, + * NULL when the end of the result set is reached + * or a DB_Error object on failure. + * + * @see DB_common::setOption(), DB_common::setFetchMode() + */ + 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; + // Skip rows + 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)) { + // The default mode is specified in the + // DB_common::fetchmode_object_class property + if ($object_class == 'stdClass') { + $arr = (object) $arr; + } else { + $arr = new $object_class($arr); + } + } + return $arr; + } + if ($res == null && $this->autofree) { + $this->free(); + } + return $res; + } + + // }}} + // {{{ fetchInto() + + /** + * Fetch a row of data into an array which is passed by reference + * + * The type of array returned can be controlled either by setting this + * method's <var>$fetchmode</var> parameter or by changing the default + * fetch mode setFetchMode() before calling this method. + * + * There are two options for standardizing the information returned + * from databases, ensuring their values are consistent when changing + * DBMS's. These portability options can be turned on when creating a + * new DB object or by using setOption(). + * + * + <var>DB_PORTABILITY_LOWERCASE</var> + * convert names of fields to lower case + * + * + <var>DB_PORTABILITY_RTRIM</var> + * right trim the data + * + * @param array &$arr the variable where the data should be placed + * @param int $fetchmode the constant indicating how to format the data + * @param int $rownum the row number to fetch (index starts at 0) + * + * @return mixed DB_OK if a row is processed, NULL when the end of the + * result set is reached or a DB_Error object on failure + * + * @see DB_common::setOption(), DB_common::setFetchMode() + */ + 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; + // Skip rows + 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)) { + // default mode specified in the + // DB_common::fetchmode_object_class property + 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; + } + + // }}} + // {{{ numCols() + + /** + * Get the the number of columns in a result set + * + * @return int the number of columns. A DB_Error object on failure. + */ + function numCols() + { + return $this->dbh->numCols($this->result); + } + + // }}} + // {{{ numRows() + + /** + * Get the number of rows in a result set + * + * @return int the number of rows. A DB_Error object on failure. + */ + 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); + } + + /* fbsql is checked for here because limit queries are implemented + * using a TOP() function, which results in fbsql_num_rows still + * returning the total number of rows that would have been returned, + * rather than the real number. As a result, we'll just do the limit + * calculations for fbsql in the same way as a database with emulated + * limits. Unfortunately, we can't just do this in DB_fbsql::numRows() + * because that only gets the result resource, rather than the full + * DB_Result object. */ + 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; + } + + // }}} + // {{{ nextResult() + + /** + * Get the next result if a batch of queries was executed + * + * @return bool true if a new result is available or false if not + */ + function nextResult() + { + return $this->dbh->nextResult($this->result); + } + + // }}} + // {{{ free() + + /** + * Frees the resources allocated for this result set + * + * @return bool true on success. A DB_Error object on failure. + */ + function free() + { + $err = $this->dbh->freeResult($this->result); + if (DB::isError($err)) { + return $err; + } + $this->result = false; + $this->statement = false; + return true; + } + + // }}} + // {{{ tableInfo() + + /** + * @see DB_common::tableInfo() + * @deprecated Method deprecated some time before Release 1.2 + */ + function tableInfo($mode = null) + { + if (is_string($mode)) { + return $this->dbh->raiseError(DB_ERROR_NEED_MORE_DATA); + } + return $this->dbh->tableInfo($this, $mode); + } + + // }}} + // {{{ getQuery() + + /** + * Determine the query string that created this result + * + * @return string the query string + * + * @since Method available since Release 1.7.0 + */ + function getQuery() + { + return $this->query; + } + + // }}} + // {{{ getRowCounter() + + /** + * Tells which row number is currently being processed + * + * @return integer the current row being looked at. Starts at 1. + */ + function getRowCounter() + { + return $this->row_counter; + } + + // }}} +} + +// }}} +// {{{ class DB_row + +/** + * PEAR DB Row Object + * + * The object contains a row of data from a result set. Each column's data + * is placed in a property named for the column. + * + * @category Database + * @package DB + * @author Stig Bakken <ssb@php.net> + * @copyright 1997-2007 The PHP Group + * @license http://www.php.net/license/3_0.txt PHP License 3.0 + * @version Release: 1.7.13 + * @link http://pear.php.net/package/DB + * @see DB_common::setFetchMode() + */ +class DB_row +{ + // {{{ constructor + + /** + * The constructor places a row's data into properties of this object + * + * @param array the array containing the row's data + * + * @return void + */ + function DB_row(&$arr) + { + foreach ($arr as $key => $value) { + $this->$key = &$arr[$key]; + } + } + + // }}} +} + +// }}} + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + */ + +?> diff --git a/extlib/DB/DataObject.php b/extlib/DB/DataObject.php new file mode 100644 index 000000000..b1a1a4e21 --- /dev/null +++ b/extlib/DB/DataObject.php @@ -0,0 +1,4152 @@ +<?php +/** + * Object Based Database Query Builder and data store + * + * PHP versions 4 and 5 + * + * LICENSE: This source file is subject to version 3.0 of the PHP license + * that is available through the world-wide-web at the following URI: + * http://www.php.net/license/3_0.txt. If you did not receive a copy of + * the PHP License and are unable to obtain it through the web, please + * send a note to license@php.net so we can mail you a copy immediately. + * + * @category Database + * @package DB_DataObject + * @author Alan Knowles <alan@akbkhome.com> + * @copyright 1997-2006 The PHP Group + * @license http://www.php.net/license/3_0.txt PHP License 3.0 + * @version CVS: $Id: DataObject.php,v 1.439 2008/01/30 02:14:06 alan_k Exp $ + * @link http://pear.php.net/package/DB_DataObject + */ + + +/* =========================================================================== + * + * !!!!!!!!!!!!! W A R N I N G !!!!!!!!!!! + * + * THIS MAY SEGFAULT PHP IF YOU ARE USING THE ZEND OPTIMIZER (to fix it, + * just add "define('DB_DATAOBJECT_NO_OVERLOAD',true);" before you include + * this file. reducing the optimization level may also solve the segfault. + * =========================================================================== + */ + +/** + * The main "DB_DataObject" class is really a base class for your own tables classes + * + * // Set up the class by creating an ini file (refer to the manual for more details + * [DB_DataObject] + * database = mysql:/username:password@host/database + * schema_location = /home/myapplication/database + * class_location = /home/myapplication/DBTables/ + * clase_prefix = DBTables_ + * + * + * //Start and initialize...................... - dont forget the & + * $config = parse_ini_file('example.ini',true); + * $options = &PEAR::getStaticProperty('DB_DataObject','options'); + * $options = $config['DB_DataObject']; + * + * // example of a class (that does not use the 'auto generated tables data') + * class mytable extends DB_DataObject { + * // mandatory - set the table + * var $_database_dsn = "mysql://username:password@localhost/database"; + * var $__table = "mytable"; + * function table() { + * return array( + * 'id' => 1, // integer or number + * 'name' => 2, // string + * ); + * } + * function keys() { + * return array('id'); + * } + * } + * + * // use in the application + * + * + * Simple get one row + * + * $instance = new mytable; + * $instance->get("id",12); + * echo $instance->somedata; + * + * + * Get multiple rows + * + * $instance = new mytable; + * $instance->whereAdd("ID > 12"); + * $instance->whereAdd("ID < 14"); + * $instance->find(); + * while ($instance->fetch()) { + * echo $instance->somedata; + * } + + +/** + * Needed classes + * - we use getStaticProperty from PEAR pretty extensively (cant remove it ATM) + */ + +require_once 'PEAR.php'; + +/** + * We are duping fetchmode constants to be compatible with + * both DB and MDB2 + */ +define('DB_DATAOBJECT_FETCHMODE_ORDERED',1); +define('DB_DATAOBJECT_FETCHMODE_ASSOC',2); + + + + + +/** + * these are constants for the get_table array + * user to determine what type of escaping is required around the object vars. + */ +define('DB_DATAOBJECT_INT', 1); // does not require '' +define('DB_DATAOBJECT_STR', 2); // requires '' + +define('DB_DATAOBJECT_DATE', 4); // is date #TODO +define('DB_DATAOBJECT_TIME', 8); // is time #TODO +define('DB_DATAOBJECT_BOOL', 16); // is boolean #TODO +define('DB_DATAOBJECT_TXT', 32); // is long text #TODO +define('DB_DATAOBJECT_BLOB', 64); // is blob type + + +define('DB_DATAOBJECT_NOTNULL', 128); // not null col. +define('DB_DATAOBJECT_MYSQLTIMESTAMP' , 256); // mysql timestamps (ignored by update/insert) +/* + * Define this before you include DataObjects.php to disable overload - if it segfaults due to Zend optimizer.. + */ +//define('DB_DATAOBJECT_NO_OVERLOAD',true) + + +/** + * Theses are the standard error codes, most methods will fail silently - and return false + * to access the error message either use $table->_lastError + * or $last_error = PEAR::getStaticProperty('DB_DataObject','lastError'); + * the code is $last_error->code, and the message is $last_error->message (a standard PEAR error) + */ + +define('DB_DATAOBJECT_ERROR_INVALIDARGS', -1); // wrong args to function +define('DB_DATAOBJECT_ERROR_NODATA', -2); // no data available +define('DB_DATAOBJECT_ERROR_INVALIDCONFIG', -3); // something wrong with the config +define('DB_DATAOBJECT_ERROR_NOCLASS', -4); // no class exists +define('DB_DATAOBJECT_ERROR_INVALID_CALL' ,-7); // overlad getter/setter failure + +/** + * Used in methods like delete() and count() to specify that the method should + * build the condition only out of the whereAdd's and not the object parameters. + */ +define('DB_DATAOBJECT_WHEREADD_ONLY', true); + +/** + * + * storage for connection and result objects, + * it is done this way so that print_r()'ing the is smaller, and + * it reduces the memory size of the object. + * -- future versions may use $this->_connection = & PEAR object.. + * although will need speed tests to see how this affects it. + * - includes sub arrays + * - connections = md5 sum mapp to pear db object + * - results = [id] => map to pear db object + * - resultseq = sequence id for results & results field + * - resultfields = [id] => list of fields return from query (for use with toArray()) + * - ini = mapping of database to ini file results + * - links = mapping of database to links file + * - lasterror = pear error objects for last error event. + * - config = aliased view of PEAR::getStaticPropery('DB_DataObject','options') * done for performance. + * - array of loaded classes by autoload method - to stop it doing file access request over and over again! + */ +$GLOBALS['_DB_DATAOBJECT']['RESULTS'] = array(); +$GLOBALS['_DB_DATAOBJECT']['RESULTSEQ'] = 1; +$GLOBALS['_DB_DATAOBJECT']['RESULTFIELDS'] = array(); +$GLOBALS['_DB_DATAOBJECT']['CONNECTIONS'] = array(); +$GLOBALS['_DB_DATAOBJECT']['INI'] = array(); +$GLOBALS['_DB_DATAOBJECT']['LINKS'] = array(); +$GLOBALS['_DB_DATAOBJECT']['SEQUENCE'] = array(); +$GLOBALS['_DB_DATAOBJECT']['LASTERROR'] = null; +$GLOBALS['_DB_DATAOBJECT']['CONFIG'] = array(); +$GLOBALS['_DB_DATAOBJECT']['CACHE'] = array(); +$GLOBALS['_DB_DATAOBJECT']['OVERLOADED'] = false; +$GLOBALS['_DB_DATAOBJECT']['QUERYENDTIME'] = 0; + + + +// this will be horrifically slow!!!! +// NOTE: Overload SEGFAULTS ON PHP4 + Zend Optimizer (see define before..) +// these two are BC/FC handlers for call in PHP4/5 + +if ( substr(phpversion(),0,1) == 5) { + class DB_DataObject_Overload + { + function __call($method,$args) + { + $return = null; + $this->_call($method,$args,$return); + return $return; + } + function __sleep() + { + return array_keys(get_object_vars($this)) ; + } + } +} else { + if (version_compare(phpversion(),'4.3.10','eq') && !defined('DB_DATAOBJECT_NO_OVERLOAD')) { + trigger_error( + "overload does not work with PHP4.3.10, either upgrade + (snaps.php.net) or more recent version + or define DB_DATAOBJECT_NO_OVERLOAD as per the manual. + ",E_USER_ERROR); + } + + if (!function_exists('clone')) { + // emulate clone - as per php_compact, slow but really the correct behaviour.. + eval('function clone($t) { $r = $t; if (method_exists($r,"__clone")) { $r->__clone(); } return $r; }'); + } + eval(' + class DB_DataObject_Overload { + function __call($method,$args,&$return) { + return $this->_call($method,$args,$return); + } + } + '); +} + + + + + + + /* + * + * @package DB_DataObject + * @author Alan Knowles <alan@akbkhome.com> + * @since PHP 4.0 + */ + +class DB_DataObject extends DB_DataObject_Overload +{ + /** + * The Version - use this to check feature changes + * + * @access private + * @var string + */ + var $_DB_DataObject_version = "1.8.8"; + + /** + * The Database table (used by table extends) + * + * @access private + * @var string + */ + var $__table = ''; // database table + + /** + * The Number of rows returned from a query + * + * @access public + * @var int + */ + var $N = 0; // Number of rows returned from a query + + /* ============================================================= */ + /* Major Public Methods */ + /* (designed to be optionally then called with parent::method()) */ + /* ============================================================= */ + + + /** + * Get a result using key, value. + * + * for example + * $object->get("ID",1234); + * Returns Number of rows located (usually 1) for success, + * and puts all the table columns into this classes variables + * + * see the fetch example on how to extend this. + * + * if no value is entered, it is assumed that $key is a value + * and get will then use the first key in keys() + * to obtain the key. + * + * @param string $k column + * @param string $v value + * @access public + * @return int No. of rows + */ + function get($k = null, $v = null) + { + global $_DB_DATAOBJECT; + if (empty($_DB_DATAOBJECT['CONFIG'])) { + DB_DataObject::_loadConfig(); + } + $keys = array(); + + if ($v === null) { + $v = $k; + $keys = $this->keys(); + if (!$keys) { + $this->raiseError("No Keys available for {$this->__table}", DB_DATAOBJECT_ERROR_INVALIDCONFIG); + return false; + } + $k = $keys[0]; + } + if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { + $this->debug("$k $v " .print_r($keys,true), "GET"); + } + + if ($v === null) { + $this->raiseError("No Value specified for get", DB_DATAOBJECT_ERROR_INVALIDARGS); + return false; + } + $this->$k = $v; + return $this->find(1); + } + + /** + * An autoloading, caching static get method using key, value (based on get) + * + * Usage: + * $object = DB_DataObject::staticGet("DbTable_mytable",12); + * or + * $object = DB_DataObject::staticGet("DbTable_mytable","name","fred"); + * + * or write it into your extended class: + * function &staticGet($k,$v=NULL) { return DB_DataObject::staticGet("This_Class",$k,$v); } + * + * @param string $class class name + * @param string $k column (or value if using keys) + * @param string $v value (optional) + * @access public + * @return object + */ + function &staticGet($class, $k, $v = null) + { + $lclass = strtolower($class); + global $_DB_DATAOBJECT; + if (empty($_DB_DATAOBJECT['CONFIG'])) { + DB_DataObject::_loadConfig(); + } + + + + $key = "$k:$v"; + if ($v === null) { + $key = $k; + } + if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { + DB_DataObject::debug("$class $key","STATIC GET - TRY CACHE"); + } + if (!empty($_DB_DATAOBJECT['CACHE'][$lclass][$key])) { + return $_DB_DATAOBJECT['CACHE'][$lclass][$key]; + } + if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { + DB_DataObject::debug("$class $key","STATIC GET - NOT IN CACHE"); + } + + $obj = DB_DataObject::factory(substr($class,strlen($_DB_DATAOBJECT['CONFIG']['class_prefix']))); + if (PEAR::isError($obj)) { + DB_DataObject::raiseError("could not autoload $class", DB_DATAOBJECT_ERROR_NOCLASS); + $r = false; + return $r; + } + + if (!isset($_DB_DATAOBJECT['CACHE'][$lclass])) { + $_DB_DATAOBJECT['CACHE'][$lclass] = array(); + } + if (!$obj->get($k,$v)) { + DB_DataObject::raiseError("No Data return from get $k $v", DB_DATAOBJECT_ERROR_NODATA); + + $r = false; + return $r; + } + $_DB_DATAOBJECT['CACHE'][$lclass][$key] = $obj; + return $_DB_DATAOBJECT['CACHE'][$lclass][$key]; + } + + /** + * find results, either normal or crosstable + * + * for example + * + * $object = new mytable(); + * $object->ID = 1; + * $object->find(); + * + * + * will set $object->N to number of rows, and expects next command to fetch rows + * will return $object->N + * + * @param boolean $n Fetch first result + * @access public + * @return mixed (number of rows returned, or true if numRows fetching is not supported) + */ + function find($n = false) + { + global $_DB_DATAOBJECT; + if ($this->_query === false) { + $this->raiseError( + "You cannot do two queries on the same object (copy it before finding)", + DB_DATAOBJECT_ERROR_INVALIDARGS); + return false; + } + + if (empty($_DB_DATAOBJECT['CONFIG'])) { + DB_DataObject::_loadConfig(); + } + + if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { + $this->debug($n, "find",1); + } + if (!$this->__table) { + // xdebug can backtrace this! + trigger_error("NO \$__table SPECIFIED in class definition",E_USER_ERROR); + } + $this->N = 0; + $query_before = $this->_query; + $this->_build_condition($this->table()) ; + + $quoteIdentifiers = !empty($_DB_DATAOBJECT['CONFIG']['quote_identifiers']); + $this->_connect(); + $DB = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; + + /* We are checking for method modifyLimitQuery as it is PEAR DB specific */ + $sql = 'SELECT ' . + $this->_query['data_select'] . " \n" . + ' FROM ' . ($quoteIdentifiers ? $DB->quoteIdentifier($this->__table) : $this->__table) . " \n" . + $this->_join . " \n" . + $this->_query['condition'] . " \n" . + $this->_query['group_by'] . " \n" . + $this->_query['having'] . " \n" . + $this->_query['order_by'] . " \n"; + + if ((!isset($_DB_DATAOBJECT['CONFIG']['db_driver'])) || + ($_DB_DATAOBJECT['CONFIG']['db_driver'] == 'DB')) { + /* PEAR DB specific */ + + if (isset($this->_query['limit_start']) && strlen($this->_query['limit_start'] . $this->_query['limit_count'])) { + $sql = $DB->modifyLimitQuery($sql,$this->_query['limit_start'], $this->_query['limit_count']); + } + } else { + /* theoretically MDB2! */ + if (isset($this->_query['limit_start']) && strlen($this->_query['limit_start'] . $this->_query['limit_count'])) { + $DB->setLimit($this->_query['limit_count'],$this->_query['limit_start']); + } + } + + + $this->_query($sql); + + if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { + $this->debug("CHECK autofetchd $n", "find", 1); + } + + // find(true) + + $ret = $this->N; + if (!$ret && !empty($_DB_DATAOBJECT['RESULTS'][$this->_DB_resultid])) { + // clear up memory if nothing found!? + unset($_DB_DATAOBJECT['RESULTS'][$this->_DB_resultid]); + } + + if ($n && $this->N > 0 ) { + if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { + $this->debug("ABOUT TO AUTOFETCH", "find", 1); + } + $fs = $this->fetch(); + // if fetch returns false (eg. failed), then the backend doesnt support numRows (eg. ret=true) + // - hence find() also returns false.. + $ret = ($ret === true) ? $fs : $ret; + } + if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { + $this->debug("DONE", "find", 1); + } + $this->_query = $query_before; + return $ret; + } + + /** + * fetches next row into this objects var's + * + * returns 1 on success 0 on failure + * + * + * + * Example + * $object = new mytable(); + * $object->name = "fred"; + * $object->find(); + * $store = array(); + * while ($object->fetch()) { + * echo $this->ID; + * $store[] = $object; // builds an array of object lines. + * } + * + * to add features to a fetch + * function fetch () { + * $ret = parent::fetch(); + * $this->date_formated = date('dmY',$this->date); + * return $ret; + * } + * + * @access public + * @return boolean on success + */ + function fetch() + { + + global $_DB_DATAOBJECT; + if (empty($_DB_DATAOBJECT['CONFIG'])) { + DB_DataObject::_loadConfig(); + } + if (empty($this->N)) { + if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { + $this->debug("No data returned from FIND (eg. N is 0)","FETCH", 3); + } + return false; + } + + if (empty($_DB_DATAOBJECT['RESULTS'][$this->_DB_resultid]) || + !is_object($result = &$_DB_DATAOBJECT['RESULTS'][$this->_DB_resultid])) + { + if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { + $this->debug('fetched on object after fetch completed (no results found)'); + } + return false; + } + + + $array = $result->fetchRow(DB_DATAOBJECT_FETCHMODE_ASSOC); + if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { + $this->debug(serialize($array),"FETCH"); + } + + // fetched after last row.. + if ($array === null) { + if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { + $t= explode(' ',microtime()); + + $this->debug("Last Data Fetch'ed after " . + ($t[0]+$t[1]- $_DB_DATAOBJECT['QUERYENDTIME'] ) . + " seconds", + "FETCH", 1); + } + // reduce the memory usage a bit... (but leave the id in, so count() works ok on it) + unset($_DB_DATAOBJECT['RESULTS'][$this->_DB_resultid]); + + // we need to keep a copy of resultfields locally so toArray() still works + // however we dont want to keep it in the global cache.. + + if (!empty($_DB_DATAOBJECT['RESULTFIELDS'][$this->_DB_resultid])) { + $this->_resultFields = $_DB_DATAOBJECT['RESULTFIELDS'][$this->_DB_resultid]; + unset($_DB_DATAOBJECT['RESULTFIELDS'][$this->_DB_resultid]); + } + // this is probably end of data!! + //DB_DataObject::raiseError("fetch: no data returned", DB_DATAOBJECT_ERROR_NODATA); + return false; + } + // make sure resultFields is always empty.. + $this->_resultFields = false; + + if (!isset($_DB_DATAOBJECT['RESULTFIELDS'][$this->_DB_resultid])) { + // note: we dont declare this to keep the print_r size down. + $_DB_DATAOBJECT['RESULTFIELDS'][$this->_DB_resultid]= array_flip(array_keys($array)); + } + + foreach($array as $k=>$v) { + $kk = str_replace(".", "_", $k); + $kk = str_replace(" ", "_", $kk); + if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { + $this->debug("$kk = ". $array[$k], "fetchrow LINE", 3); + } + $this->$kk = $array[$k]; + } + + // set link flag + $this->_link_loaded=false; + if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { + $this->debug("{$this->__table} DONE", "fetchrow",2); + } + if (($this->_query !== false) && empty($_DB_DATAOBJECT['CONFIG']['keep_query_after_fetch'])) { + $this->_query = false; + } + return true; + } + + /** + * Adds a condition to the WHERE statement, defaults to AND + * + * $object->whereAdd(); //reset or cleaer ewhwer + * $object->whereAdd("ID > 20"); + * $object->whereAdd("age > 20","OR"); + * + * @param string $cond condition + * @param string $logic optional logic "OR" (defaults to "AND") + * @access public + * @return string|PEAR::Error - previous condition or Error when invalid args found + */ + function whereAdd($cond = false, $logic = 'AND') + { + // for PHP5.2.3 - there is a bug with setting array properties of an object. + $_query = $this->_query; + + if (!isset($this->_query) || ($_query === false)) { + return $this->raiseError( + "You cannot do two queries on the same object (clone it before finding)", + DB_DATAOBJECT_ERROR_INVALIDARGS); + } + + if ($cond === false) { + $r = $this->_query['condition']; + $_query['condition'] = ''; + $this->_query = $_query; + return preg_replace('/^\s+WHERE\s+/','',$r); + } + // check input...= 0 or ' ' == error! + if (!trim($cond)) { + return $this->raiseError("WhereAdd: No Valid Arguments", DB_DATAOBJECT_ERROR_INVALIDARGS); + } + $r = $_query['condition']; + if ($_query['condition']) { + $_query['condition'] .= " {$logic} ( {$cond} )"; + $this->_query = $_query; + return $r; + } + $_query['condition'] = " WHERE ( {$cond} ) "; + $this->_query = $_query; + return $r; + } + + /** + * Adds a order by condition + * + * $object->orderBy(); //clears order by + * $object->orderBy("ID"); + * $object->orderBy("ID,age"); + * + * @param string $order Order + * @access public + * @return none|PEAR::Error - invalid args only + */ + function orderBy($order = false) + { + if ($this->_query === false) { + $this->raiseError( + "You cannot do two queries on the same object (copy it before finding)", + DB_DATAOBJECT_ERROR_INVALIDARGS); + return false; + } + if ($order === false) { + $this->_query['order_by'] = ''; + return; + } + // check input...= 0 or ' ' == error! + if (!trim($order)) { + return $this->raiseError("orderBy: No Valid Arguments", DB_DATAOBJECT_ERROR_INVALIDARGS); + } + + if (!$this->_query['order_by']) { + $this->_query['order_by'] = " ORDER BY {$order} "; + return; + } + $this->_query['order_by'] .= " , {$order}"; + } + + /** + * Adds a group by condition + * + * $object->groupBy(); //reset the grouping + * $object->groupBy("ID DESC"); + * $object->groupBy("ID,age"); + * + * @param string $group Grouping + * @access public + * @return none|PEAR::Error - invalid args only + */ + function groupBy($group = false) + { + if ($this->_query === false) { + $this->raiseError( + "You cannot do two queries on the same object (copy it before finding)", + DB_DATAOBJECT_ERROR_INVALIDARGS); + return false; + } + if ($group === false) { + $this->_query['group_by'] = ''; + return; + } + // check input...= 0 or ' ' == error! + if (!trim($group)) { + return $this->raiseError("groupBy: No Valid Arguments", DB_DATAOBJECT_ERROR_INVALIDARGS); + } + + + if (!$this->_query['group_by']) { + $this->_query['group_by'] = " GROUP BY {$group} "; + return; + } + $this->_query['group_by'] .= " , {$group}"; + } + + /** + * Adds a having clause + * + * $object->having(); //reset the grouping + * $object->having("sum(value) > 0 "); + * + * @param string $having condition + * @access public + * @return none|PEAR::Error - invalid args only + */ + function having($having = false) + { + if ($this->_query === false) { + $this->raiseError( + "You cannot do two queries on the same object (copy it before finding)", + DB_DATAOBJECT_ERROR_INVALIDARGS); + return false; + } + if ($having === false) { + $this->_query['having'] = ''; + return; + } + // check input...= 0 or ' ' == error! + if (!trim($having)) { + return $this->raiseError("Having: No Valid Arguments", DB_DATAOBJECT_ERROR_INVALIDARGS); + } + + + if (!$this->_query['having']) { + $this->_query['having'] = " HAVING {$having} "; + return; + } + $this->_query['having'] .= " AND {$having}"; + } + + /** + * Sets the Limit + * + * $boject->limit(); // clear limit + * $object->limit(12); + * $object->limit(12,10); + * + * Note this will emit an error on databases other than mysql/postgress + * as there is no 'clean way' to implement it. - you should consider refering to + * your database manual to decide how you want to implement it. + * + * @param string $a limit start (or number), or blank to reset + * @param string $b number + * @access public + * @return none|PEAR::Error - invalid args only + */ + function limit($a = null, $b = null) + { + if ($this->_query === false) { + $this->raiseError( + "You cannot do two queries on the same object (copy it before finding)", + DB_DATAOBJECT_ERROR_INVALIDARGS); + return false; + } + + if ($a === null) { + $this->_query['limit_start'] = ''; + $this->_query['limit_count'] = ''; + return; + } + // check input...= 0 or ' ' == error! + if ((!is_int($a) && ((string)((int)$a) !== (string)$a)) + || (($b !== null) && (!is_int($b) && ((string)((int)$b) !== (string)$b)))) { + return $this->raiseError("limit: No Valid Arguments", DB_DATAOBJECT_ERROR_INVALIDARGS); + } + global $_DB_DATAOBJECT; + $this->_connect(); + $DB = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; + + $this->_query['limit_start'] = ($b == null) ? 0 : (int)$a; + $this->_query['limit_count'] = ($b == null) ? (int)$a : (int)$b; + + } + + /** + * Adds a select columns + * + * $object->selectAdd(); // resets select to nothing! + * $object->selectAdd("*"); // default select + * $object->selectAdd("unixtime(DATE) as udate"); + * $object->selectAdd("DATE"); + * + * to prepend distict: + * $object->selectAdd('distinct ' . $object->selectAdd()); + * + * @param string $k + * @access public + * @return mixed null or old string if you reset it. + */ + function selectAdd($k = null) + { + if ($this->_query === false) { + $this->raiseError( + "You cannot do two queries on the same object (copy it before finding)", + DB_DATAOBJECT_ERROR_INVALIDARGS); + return false; + } + if ($k === null) { + $old = $this->_query['data_select']; + $this->_query['data_select'] = ''; + return $old; + } + + // check input...= 0 or ' ' == error! + if (!trim($k)) { + return $this->raiseError("selectAdd: No Valid Arguments", DB_DATAOBJECT_ERROR_INVALIDARGS); + } + + if ($this->_query['data_select']) { + $this->_query['data_select'] .= ', '; + } + $this->_query['data_select'] .= " $k "; + } + /** + * Adds multiple Columns or objects to select with formating. + * + * $object->selectAs(null); // adds "table.colnameA as colnameA,table.colnameB as colnameB,......" + * // note with null it will also clear the '*' default select + * $object->selectAs(array('a','b'),'%s_x'); // adds "a as a_x, b as b_x" + * $object->selectAs(array('a','b'),'ddd_%s','ccc'); // adds "ccc.a as ddd_a, ccc.b as ddd_b" + * $object->selectAdd($object,'prefix_%s'); // calls $object->get_table and adds it all as + * objectTableName.colnameA as prefix_colnameA + * + * @param array|object|null the array or object to take column names from. + * @param string format in sprintf format (use %s for the colname) + * @param string table name eg. if you have joinAdd'd or send $from as an array. + * @access public + * @return void + */ + function selectAs($from = null,$format = '%s',$tableName=false) + { + global $_DB_DATAOBJECT; + + if ($this->_query === false) { + $this->raiseError( + "You cannot do two queries on the same object (copy it before finding)", + DB_DATAOBJECT_ERROR_INVALIDARGS); + return false; + } + + if ($from === null) { + // blank the '*' + $this->selectAdd(); + $from = $this; + } + + + $table = $this->__table; + if (is_object($from)) { + $table = $from->__table; + $from = array_keys($from->table()); + } + + if ($tableName !== false) { + $table = $tableName; + } + $s = '%s'; + if (!empty($_DB_DATAOBJECT['CONFIG']['quote_identifiers'])) { + $this->_connect(); + $DB = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; + $s = $DB->quoteIdentifier($s); + $format = $DB->quoteIdentifier($format); + } + foreach ($from as $k) { + $this->selectAdd(sprintf("{$s}.{$s} as {$format}",$table,$k,$k)); + } + $this->_query['data_select'] .= "\n"; + } + /** + * Insert the current objects variables into the database + * + * Returns the ID of the inserted element (if auto increment or sequences are used.) + * + * for example + * + * Designed to be extended + * + * $object = new mytable(); + * $object->name = "fred"; + * echo $object->insert(); + * + * @access public + * @return mixed false on failure, int when auto increment or sequence used, otherwise true on success + */ + function insert() + { + global $_DB_DATAOBJECT; + + // we need to write to the connection (For nextid) - so us the real + // one not, a copyied on (as ret-by-ref fails with overload!) + + if (!isset($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5])) { + $this->_connect(); + } + + $quoteIdentifiers = !empty($_DB_DATAOBJECT['CONFIG']['quote_identifiers']); + + $DB = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; + + $items = isset($_DB_DATAOBJECT['INI'][$this->_database][$this->__table]) ? + $_DB_DATAOBJECT['INI'][$this->_database][$this->__table] : $this->table(); + + if (!$items) { + $this->raiseError("insert:No table definition for {$this->__table}", + DB_DATAOBJECT_ERROR_INVALIDCONFIG); + return false; + } + $options = &$_DB_DATAOBJECT['CONFIG']; + + + $datasaved = 1; + $leftq = ''; + $rightq = ''; + + $seqKeys = isset($_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->__table]) ? + $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->__table] : + $this->sequenceKey(); + + $key = isset($seqKeys[0]) ? $seqKeys[0] : false; + $useNative = isset($seqKeys[1]) ? $seqKeys[1] : false; + $seq = isset($seqKeys[2]) ? $seqKeys[2] : false; + + $dbtype = $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->dsn["phptype"]; + + + // nativeSequences or Sequences.. + + // big check for using sequences + + if (($key !== false) && !$useNative) { + + if (!$seq) { + $keyvalue = $DB->nextId($this->__table); + } else { + $f = $DB->getOption('seqname_format'); + $DB->setOption('seqname_format','%s'); + $keyvalue = $DB->nextId($seq); + $DB->setOption('seqname_format',$f); + } + if (PEAR::isError($keyvalue)) { + $this->raiseError($keyvalue->toString(), DB_DATAOBJECT_ERROR_INVALIDCONFIG); + return false; + } + $this->$key = $keyvalue; + } + + + + foreach($items as $k => $v) { + + // if we are using autoincrement - skip the column... + if ($key && ($k == $key) && $useNative) { + continue; + } + + + if (!isset($this->$k)) { + continue; + } + // dont insert data into mysql timestamps + // use query() if you really want to do this!!!! + if ($v & DB_DATAOBJECT_MYSQLTIMESTAMP) { + continue; + } + + if ($leftq) { + $leftq .= ', '; + $rightq .= ', '; + } + + $leftq .= ($quoteIdentifiers ? ($DB->quoteIdentifier($k) . ' ') : "$k "); + + if (is_a($this->$k,'DB_DataObject_Cast')) { + $value = $this->$k->toString($v,$DB); + if (PEAR::isError($value)) { + $this->raiseError($value->toString() ,DB_DATAOBJECT_ERROR_INVALIDARGS); + return false; + } + $rightq .= $value; + continue; + } + + + + if (!isset($options['disable_null_strings']) && is_string($this->$k) && (strtolower($this->$k) === 'null') && !($v & DB_DATAOBJECT_NOTNULL)) { + $rightq .= " NULL "; + continue; + } + // DATE is empty... on a col. that can be null.. + // note: this may be usefull for time as well.. + if (!$this->$k && + (($v & DB_DATAOBJECT_DATE) || ($v & DB_DATAOBJECT_TIME)) && + !($v & DB_DATAOBJECT_NOTNULL)) { + + $rightq .= " NULL "; + continue; + } + + + if ($v & DB_DATAOBJECT_STR) { + $rightq .= $this->_quote((string) ( + ($v & DB_DATAOBJECT_BOOL) ? + // this is thanks to the braindead idea of postgres to + // use t/f for boolean. + (($this->$k === 'f') ? 0 : (int)(bool) $this->$k) : + $this->$k + )) . " "; + continue; + } + if (is_numeric($this->$k)) { + $rightq .=" {$this->$k} "; + continue; + } + /* flag up string values - only at debug level... !!!??? */ + if (is_object($this->$k) || is_array($this->$k)) { + $this->debug('ODD DATA: ' .$k . ' ' . print_r($this->$k,true),'ERROR'); + } + + // at present we only cast to integers + // - V2 may store additional data about float/int + $rightq .= ' ' . intval($this->$k) . ' '; + + } + + // not sure why we let empty insert here.. - I guess to generate a blank row.. + + + if ($leftq || $useNative) { + $table = ($quoteIdentifiers ? $DB->quoteIdentifier($this->__table) : $this->__table); + + $r = $this->_query("INSERT INTO {$table} ($leftq) VALUES ($rightq) "); + + + + if (PEAR::isError($r)) { + $this->raiseError($r); + return false; + } + + if ($r < 1) { + return 0; + } + + + // now do we have an integer key! + + if ($key && $useNative) { + switch ($dbtype) { + case 'mysql': + case 'mysqli': + $method = "{$dbtype}_insert_id"; + $this->$key = $method( + $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->connection + ); + break; + + case 'mssql': + // note this is not really thread safe - you should wrapp it with + // transactions = eg. + // $db->query('BEGIN'); + // $db->insert(); + // $db->query('COMMIT'); + $db_driver = empty($options['db_driver']) ? 'DB' : $options['db_driver']; + $method = ($db_driver == 'DB') ? 'getOne' : 'queryOne'; + $mssql_key = $DB->$method("SELECT @@IDENTITY"); + if (PEAR::isError($mssql_key)) { + $this->raiseError($mssql_key); + return false; + } + $this->$key = $mssql_key; + break; + + case 'pgsql': + if (!$seq) { + $seq = $DB->getSequenceName(strtolower($this->__table)); + } + $db_driver = empty($options['db_driver']) ? 'DB' : $options['db_driver']; + $method = ($db_driver == 'DB') ? 'getOne' : 'queryOne'; + $pgsql_key = $DB->$method("SELECT currval('".$seq . "')"); + + + if (PEAR::isError($pgsql_key)) { + $this->raiseError($pgsql_key); + return false; + } + $this->$key = $pgsql_key; + break; + + case 'ifx': + $this->$key = array_shift ( + ifx_fetch_row ( + ifx_query( + "select DBINFO('sqlca.sqlerrd1') FROM systables where tabid=1", + $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->connection, + IFX_SCROLL + ), + "FIRST" + ) + ); + break; + + } + + } + + if (isset($_DB_DATAOBJECT['CACHE'][strtolower(get_class($this))])) { + $this->_clear_cache(); + } + if ($key) { + return $this->$key; + } + return true; + } + $this->raiseError("insert: No Data specifed for query", DB_DATAOBJECT_ERROR_NODATA); + return false; + } + + /** + * Updates current objects variables into the database + * uses the keys() to decide how to update + * Returns the true on success + * + * for example + * + * $object = DB_DataObject::factory('mytable'); + * $object->get("ID",234); + * $object->email="testing@test.com"; + * if(!$object->update()) + * echo "UPDATE FAILED"; + * + * to only update changed items : + * $dataobject->get(132); + * $original = $dataobject; // clone/copy it.. + * $dataobject->setFrom($_POST); + * if ($dataobject->validate()) { + * $dataobject->update($original); + * } // otherwise an error... + * + * performing global updates: + * $object = DB_DataObject::factory('mytable'); + * $object->status = "dead"; + * $object->whereAdd('age > 150'); + * $object->update(DB_DATAOBJECT_WHEREADD_ONLY); + * + * @param object dataobject (optional) | DB_DATAOBJECT_WHEREADD_ONLY - used to only update changed items. + * @access public + * @return int rows affected or false on failure + */ + function update($dataObject = false) + { + global $_DB_DATAOBJECT; + // connect will load the config! + $this->_connect(); + + + $original_query = $this->_query; + + $items = isset($_DB_DATAOBJECT['INI'][$this->_database][$this->__table]) ? + $_DB_DATAOBJECT['INI'][$this->_database][$this->__table] : $this->table(); + + // only apply update against sequence key if it is set????? + + $seq = $this->sequenceKey(); + if ($seq[0] !== false) { + $keys = array($seq[0]); + if (empty($this->{$keys[0]}) && $dataObject !== true) { + $this->raiseError("update: trying to perform an update without + the key set, and argument to update is not + DB_DATAOBJECT_WHEREADD_ONLY + ", DB_DATAOBJECT_ERROR_INVALIDARGS); + return false; + } + } else { + $keys = $this->keys(); + } + + + if (!$items) { + $this->raiseError("update:No table definition for {$this->__table}", DB_DATAOBJECT_ERROR_INVALIDCONFIG); + return false; + } + $datasaved = 1; + $settings = ''; + $this->_connect(); + + $DB = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; + $dbtype = $DB->dsn["phptype"]; + $quoteIdentifiers = !empty($_DB_DATAOBJECT['CONFIG']['quote_identifiers']); + $options = $_DB_DATAOBJECT['CONFIG']; + + + foreach($items as $k => $v) { + if (!isset($this->$k)) { + continue; + } + // ignore stuff thats + + // dont write things that havent changed.. + if (($dataObject !== false) && isset($dataObject->$k) && ($dataObject->$k === $this->$k)) { + continue; + } + + // - dont write keys to left.!!! + if (in_array($k,$keys)) { + continue; + } + + // dont insert data into mysql timestamps + // use query() if you really want to do this!!!! + if ($v & DB_DATAOBJECT_MYSQLTIMESTAMP) { + continue; + } + + + if ($settings) { + $settings .= ', '; + } + + $kSql = ($quoteIdentifiers ? $DB->quoteIdentifier($k) : $k); + + if (is_a($this->$k,'DB_DataObject_Cast')) { + $value = $this->$k->toString($v,$DB); + if (PEAR::isError($value)) { + $this->raiseError($value->getMessage() ,DB_DATAOBJECT_ERROR_INVALIDARG); + return false; + } + $settings .= "$kSql = $value "; + continue; + } + + // special values ... at least null is handled... + if (!isset($options['disable_null_strings']) && (strtolower($this->$k) === 'null') && !($v & DB_DATAOBJECT_NOTNULL)) { + $settings .= "$kSql = NULL "; + continue; + } + // DATE is empty... on a col. that can be null.. + // note: this may be usefull for time as well.. + if (!$this->$k && + (($v & DB_DATAOBJECT_DATE) || ($v & DB_DATAOBJECT_TIME)) && + !($v & DB_DATAOBJECT_NOTNULL)) { + + $settings .= "$kSql = NULL "; + continue; + } + + + if ($v & DB_DATAOBJECT_STR) { + $settings .= "$kSql = ". $this->_quote((string) ( + ($v & DB_DATAOBJECT_BOOL) ? + // this is thanks to the braindead idea of postgres to + // use t/f for boolean. + (($this->$k === 'f') ? 0 : (int)(bool) $this->$k) : + $this->$k + )) . ' '; + continue; + } + if (is_numeric($this->$k)) { + $settings .= "$kSql = {$this->$k} "; + continue; + } + // at present we only cast to integers + // - V2 may store additional data about float/int + $settings .= "$kSql = " . intval($this->$k) . ' '; + } + + + if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { + $this->debug("got keys as ".serialize($keys),3); + } + if ($dataObject !== true) { + $this->_build_condition($items,$keys); + } else { + // prevent wiping out of data! + if (empty($this->_query['condition'])) { + $this->raiseError("update: global table update not available + do \$do->whereAdd('1=1'); if you really want to do that. + ", DB_DATAOBJECT_ERROR_INVALIDARGS); + return false; + } + } + + + + // echo " $settings, $this->condition "; + if ($settings && isset($this->_query) && $this->_query['condition']) { + + $table = ($quoteIdentifiers ? $DB->quoteIdentifier($this->__table) : $this->__table); + + $r = $this->_query("UPDATE {$table} SET {$settings} {$this->_query['condition']} "); + + // restore original query conditions. + $this->_query = $original_query; + + if (PEAR::isError($r)) { + $this->raiseError($r); + return false; + } + if ($r < 1) { + return 0; + } + + $this->_clear_cache(); + return $r; + } + // restore original query conditions. + $this->_query = $original_query; + + // if you manually specified a dataobject, and there where no changes - then it's ok.. + if ($dataObject !== false) { + return true; + } + + $this->raiseError( + "update: No Data specifed for query $settings , {$this->_query['condition']}", + DB_DATAOBJECT_ERROR_NODATA); + return false; + } + + /** + * Deletes items from table which match current objects variables + * + * Returns the true on success + * + * for example + * + * Designed to be extended + * + * $object = new mytable(); + * $object->ID=123; + * echo $object->delete(); // builds a conditon + * + * $object = new mytable(); + * $object->whereAdd('age > 12'); + * $object->limit(1); + * $object->orderBy('age DESC'); + * $object->delete(true); // dont use object vars, use the conditions, limit and order. + * + * @param bool $useWhere (optional) If DB_DATAOBJECT_WHEREADD_ONLY is passed in then + * we will build the condition only using the whereAdd's. Default is to + * build the condition only using the object parameters. + * + * @access public + * @return mixed True on success, false on failure, 0 on no data affected + */ + function delete($useWhere = false) + { + global $_DB_DATAOBJECT; + // connect will load the config! + $this->_connect(); + $DB = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; + $quoteIdentifiers = !empty($_DB_DATAOBJECT['CONFIG']['quote_identifiers']); + + $extra_cond = ' ' . (isset($this->_query['order_by']) ? $this->_query['order_by'] : ''); + + if (!$useWhere) { + + $keys = $this->keys(); + $this->_query = array(); // as it's probably unset! + $this->_query['condition'] = ''; // default behaviour not to use where condition + $this->_build_condition($this->table(),$keys); + // if primary keys are not set then use data from rest of object. + if (!$this->_query['condition']) { + $this->_build_condition($this->table(),array(),$keys); + } + $extra_cond = ''; + } + + + // don't delete without a condition + if (($this->_query !== false) && $this->_query['condition']) { + + $table = ($quoteIdentifiers ? $DB->quoteIdentifier($this->__table) : $this->__table); + $sql = "DELETE FROM {$table} {$this->_query['condition']}{$extra_cond}"; + + // add limit.. + + if (isset($this->_query['limit_start']) && strlen($this->_query['limit_start'] . $this->_query['limit_count'])) { + + if (!isset($_DB_DATAOBJECT['CONFIG']['db_driver']) || + ($_DB_DATAOBJECT['CONFIG']['db_driver'] == 'DB')) { + // pear DB + $sql = $DB->modifyLimitQuery($sql,$this->_query['limit_start'], $this->_query['limit_count']); + + } else { + // MDB2 + $DB->setLimit( $this->_query['limit_count'],$this->_query['limit_start']); + } + + } + + + $r = $this->_query($sql); + + + if (PEAR::isError($r)) { + $this->raiseError($r); + return false; + } + if ($r < 1) { + return 0; + } + $this->_clear_cache(); + return $r; + } else { + $this->raiseError("delete: No condition specifed for query", DB_DATAOBJECT_ERROR_NODATA); + return false; + } + } + + /** + * fetches a specific row into this object variables + * + * Not recommended - better to use fetch() + * + * Returens true on success + * + * @param int $row row + * @access public + * @return boolean true on success + */ + function fetchRow($row = null) + { + global $_DB_DATAOBJECT; + if (empty($_DB_DATAOBJECT['CONFIG'])) { + $this->_loadConfig(); + } + if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { + $this->debug("{$this->__table} $row of {$this->N}", "fetchrow",3); + } + if (!$this->__table) { + $this->raiseError("fetchrow: No table", DB_DATAOBJECT_ERROR_INVALIDCONFIG); + return false; + } + if ($row === null) { + $this->raiseError("fetchrow: No row specified", DB_DATAOBJECT_ERROR_INVALIDARGS); + return false; + } + if (!$this->N) { + $this->raiseError("fetchrow: No results avaiable", DB_DATAOBJECT_ERROR_NODATA); + return false; + } + if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { + $this->debug("{$this->__table} $row of {$this->N}", "fetchrow",3); + } + + + $result = &$_DB_DATAOBJECT['RESULTS'][$this->_DB_resultid]; + $array = $result->fetchrow(DB_DATAOBJECT_FETCHMODE_ASSOC,$row); + if (!is_array($array)) { + $this->raiseError("fetchrow: No results available", DB_DATAOBJECT_ERROR_NODATA); + return false; + } + + foreach($array as $k => $v) { + $kk = str_replace(".", "_", $k); + if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { + $this->debug("$kk = ". $array[$k], "fetchrow LINE", 3); + } + $this->$kk = $array[$k]; + } + + if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { + $this->debug("{$this->__table} DONE", "fetchrow", 3); + } + return true; + } + + /** + * Find the number of results from a simple query + * + * for example + * + * $object = new mytable(); + * $object->name = "fred"; + * echo $object->count(); + * echo $object->count(true); // dont use object vars. + * echo $object->count('distinct mycol'); count distinct mycol. + * echo $object->count('distinct mycol',true); // dont use object vars. + * echo $object->count('distinct'); // count distinct id (eg. the primary key) + * + * + * @param bool|string (optional) + * (true|false => see below not on whereAddonly) + * (string) + * "DISTINCT" => does a distinct count on the tables 'key' column + * otherwise => normally it counts primary keys - you can use + * this to do things like $do->count('distinct mycol'); + * + * @param bool $whereAddOnly (optional) If DB_DATAOBJECT_WHEREADD_ONLY is passed in then + * we will build the condition only using the whereAdd's. Default is to + * build the condition using the object parameters as well. + * + * @access public + * @return int + */ + function count($countWhat = false,$whereAddOnly = false) + { + global $_DB_DATAOBJECT; + + if (is_bool($countWhat)) { + $whereAddOnly = $countWhat; + } + + $t = clone($this); + $items = $t->table(); + + $quoteIdentifiers = !empty($_DB_DATAOBJECT['CONFIG']['quote_identifiers']); + + + if (!isset($t->_query)) { + $this->raiseError( + "You cannot do run count after you have run fetch()", + DB_DATAOBJECT_ERROR_INVALIDARGS); + return false; + } + $this->_connect(); + $DB = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; + + + if (!$whereAddOnly && $items) { + $t->_build_condition($items); + } + $keys = $this->keys(); + + if (!$keys[0] && !is_string($countWhat)) { + $this->raiseError( + "You cannot do run count without keys - use \$do->keys('id');", + DB_DATAOBJECT_ERROR_INVALIDARGS,PEAR_ERROR_DIE); + return false; + + } + $table = ($quoteIdentifiers ? $DB->quoteIdentifier($this->__table) : $this->__table); + $key_col = ($quoteIdentifiers ? $DB->quoteIdentifier($keys[0]) : $keys[0]); + $as = ($quoteIdentifiers ? $DB->quoteIdentifier('DATAOBJECT_NUM') : 'DATAOBJECT_NUM'); + + // support distinct on default keys. + $countWhat = (strtoupper($countWhat) == 'DISTINCT') ? + "DISTINCT {$table}.{$key_col}" : $countWhat; + + $countWhat = is_string($countWhat) ? $countWhat : "{$table}.{$key_col}"; + + $r = $t->_query( + "SELECT count({$countWhat}) as $as + FROM $table {$t->_join} {$t->_query['condition']}"); + if (PEAR::isError($r)) { + return false; + } + + $result = &$_DB_DATAOBJECT['RESULTS'][$t->_DB_resultid]; + $l = $result->fetchRow(DB_DATAOBJECT_FETCHMODE_ORDERED); + // free the results - essential on oracle. + $t->free(); + + return (int) $l[0]; + } + + /** + * sends raw query to database + * + * Since _query has to be a private 'non overwriteable method', this is a relay + * + * @param string $string SQL Query + * @access public + * @return void or DB_Error + */ + function query($string) + { + return $this->_query($string); + } + + + /** + * an escape wrapper around DB->escapeSimple() + * can be used when adding manual queries or clauses + * eg. + * $object->query("select * from xyz where abc like '". $object->escape($_GET['name']) . "'"); + * + * @param string $string value to be escaped + * @param bool $likeEscape escapes % and _ as well. - so like queries can be protected. + * @access public + * @return string + */ + function escape($string, $likeEscape=false) + { + global $_DB_DATAOBJECT; + $this->_connect(); + $DB = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; + // mdb2 uses escape... + $dd = empty($_DB_DATAOBJECT['CONFIG']['db_driver']) ? 'DB' : $_DB_DATAOBJECT['CONFIG']['db_driver']; + $ret = ($dd == 'DB') ? $DB->escapeSimple($string) : $DB->escape($string); + if ($likeEscape) { + $ret = str_replace(array('_','%'), array('\_','\%'), $ret); + } + return $ret; + + } + + /* ==================================================== */ + /* Major Private Vars */ + /* ==================================================== */ + + /** + * The Database connection dsn (as described in the PEAR DB) + * only used really if you are writing a very simple application/test.. + * try not to use this - it is better stored in configuration files.. + * + * @access private + * @var string + */ + var $_database_dsn = ''; + + /** + * The Database connection id (md5 sum of databasedsn) + * + * @access private + * @var string + */ + var $_database_dsn_md5 = ''; + + /** + * The Database name + * created in __connection + * + * @access private + * @var string + */ + var $_database = ''; + + + + /** + * The QUERY rules + * This replaces alot of the private variables + * used to build a query, it is unset after find() is run. + * + * + * + * @access private + * @var array + */ + var $_query = array( + 'condition' => '', // the WHERE condition + 'group_by' => '', // the GROUP BY condition + 'order_by' => '', // the ORDER BY condition + 'having' => '', // the HAVING condition + 'limit_start' => '', // the LIMIT condition + 'limit_count' => '', // the LIMIT condition + 'data_select' => '*', // the columns to be SELECTed + ); + + + + + /** + * Database result id (references global $_DB_DataObject[results] + * + * @access private + * @var integer + */ + var $_DB_resultid; + + /** + * ResultFields - on the last call to fetch(), resultfields is sent here, + * so we can clean up the memory. + * + * @access public + * @var array + */ + var $_resultFields = false; + + + /* ============================================================== */ + /* Table definition layer (started of very private but 'came out'*/ + /* ============================================================== */ + + /** + * Autoload or manually load the table definitions + * + * + * usage : + * DB_DataObject::databaseStructure( 'databasename', + * parse_ini_file('mydb.ini',true), + * parse_ini_file('mydb.link.ini',true)); + * + * obviously you dont have to use ini files.. (just return array similar to ini files..) + * + * It should append to the table structure array + * + * + * @param optional string name of database to assign / read + * @param optional array structure of database, and keys + * @param optional array table links + * + * @access public + * @return true or PEAR:error on wrong paramenters.. or false if no file exists.. + * or the array(tablename => array(column_name=>type)) if called with 1 argument.. (databasename) + */ + function databaseStructure() + { + + global $_DB_DATAOBJECT; + + // Assignment code + + if ($args = func_get_args()) { + + if (count($args) == 1) { + + // this returns all the tables and their structure.. + if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { + $this->debug("Loading Generator as databaseStructure called with args",1); + } + + $x = new DB_DataObject; + $x->_database = $args[0]; + $this->_connect(); + $DB = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; + + $tables = $DB->getListOf('tables'); + class_exists('DB_DataObject_Generator') ? '' : + require_once 'DB/DataObject/Generator.php'; + + foreach($tables as $table) { + $y = new DB_DataObject_Generator; + $y->fillTableSchema($x->_database,$table); + } + return $_DB_DATAOBJECT['INI'][$x->_database]; + } else { + + $_DB_DATAOBJECT['INI'][$args[0]] = isset($_DB_DATAOBJECT['INI'][$args[0]]) ? + $_DB_DATAOBJECT['INI'][$args[0]] + $args[1] : $args[1]; + + if (isset($args[1])) { + $_DB_DATAOBJECT['LINKS'][$args[0]] = isset($_DB_DATAOBJECT['LINKS'][$args[0]]) ? + $_DB_DATAOBJECT['LINKS'][$args[0]] + $args[2] : $args[2]; + } + return true; + } + + } + + + + if (!$this->_database) { + $this->_connect(); + } + + // loaded already? + if (!empty($_DB_DATAOBJECT['INI'][$this->_database])) { + + // database loaded - but this is table is not available.. + if ( + empty($_DB_DATAOBJECT['INI'][$this->_database][$this->__table]) + && !empty($_DB_DATAOBJECT['CONFIG']['proxy']) + ) { + if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { + $this->debug("Loading Generator to fetch Schema",1); + } + class_exists('DB_DataObject_Generator') ? '' : + require_once 'DB/DataObject/Generator.php'; + + + $x = new DB_DataObject_Generator; + $x->fillTableSchema($this->_database,$this->__table); + } + return true; + } + + + if (empty($_DB_DATAOBJECT['CONFIG'])) { + DB_DataObject::_loadConfig(); + } + + // if you supply this with arguments, then it will take those + // as the database and links array... + + $schemas = isset($_DB_DATAOBJECT['CONFIG']['schema_location']) ? + array("{$_DB_DATAOBJECT['CONFIG']['schema_location']}/{$this->_database}.ini") : + array() ; + + if (isset($_DB_DATAOBJECT['CONFIG']["ini_{$this->_database}"])) { + $schemas = is_array($_DB_DATAOBJECT['CONFIG']["ini_{$this->_database}"]) ? + $_DB_DATAOBJECT['CONFIG']["ini_{$this->_database}"] : + explode(PATH_SEPARATOR,$_DB_DATAOBJECT['CONFIG']["ini_{$this->_database}"]); + } + + + + foreach ($schemas as $ini) { + if (file_exists($ini) && is_file($ini)) { + $_DB_DATAOBJECT['INI'][$this->_database] = parse_ini_file($ini, true); + if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { + if (!is_readable ($ini)) { + $this->debug("ini file is not readable: $ini","databaseStructure",1); + } else { + $this->debug("Loaded ini file: $ini","databaseStructure",1); + } + } + } else { + if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { + $this->debug("Missing ini file: $ini","databaseStructure",1); + } + } + + } + // now have we loaded the structure.. + + if (!empty($_DB_DATAOBJECT['INI'][$this->_database][$this->__table])) { + return true; + } + // - if not try building it.. + if (!empty($_DB_DATAOBJECT['CONFIG']['proxy'])) { + class_exists('DB_DataObject_Generator') ? '' : + require_once 'DB/DataObject/Generator.php'; + + $x = new DB_DataObject_Generator; + $x->fillTableSchema($this->_database,$this->__table); + // should this fail!!!??? + return true; + } + $this->debug("Cant find database schema: {$this->_database}/{$this->__table} \n". + "in links file data: " . print_r($_DB_DATAOBJECT['INI'],true),"databaseStructure",5); + // we have to die here!! - it causes chaos if we dont (including looping forever!) + $this->raiseError( "Unable to load schema for database and table (turn debugging up to 5 for full error message)", DB_DATAOBJECT_ERROR_INVALIDARGS, PEAR_ERROR_DIE); + return false; + + + } + + + + + /** + * Return or assign the name of the current table + * + * + * @param string optinal table name to set + * @access public + * @return string The name of the current table + */ + function tableName() + { + $args = func_get_args(); + if (count($args)) { + $this->__table = $args[0]; + } + return $this->__table; + } + + /** + * Return or assign the name of the current database + * + * @param string optional database name to set + * @access public + * @return string The name of the current database + */ + function database() + { + $args = func_get_args(); + if (count($args)) { + $this->_database = $args[0]; + } + return $this->_database; + } + + /** + * get/set an associative array of table columns + * + * @access public + * @param array key=>type array + * @return array (associative) + */ + function table() + { + + // for temporary storage of database fields.. + // note this is not declared as we dont want to bloat the print_r output + $args = func_get_args(); + if (count($args)) { + $this->_database_fields = $args[0]; + } + if (isset($this->_database_fields)) { + return $this->_database_fields; + } + + + global $_DB_DATAOBJECT; + if (!isset($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5])) { + $this->_connect(); + } + + if (isset($_DB_DATAOBJECT['INI'][$this->_database][$this->__table])) { + return $_DB_DATAOBJECT['INI'][$this->_database][$this->__table]; + } + + $this->databaseStructure(); + + + $ret = array(); + if (isset($_DB_DATAOBJECT['INI'][$this->_database][$this->__table])) { + $ret = $_DB_DATAOBJECT['INI'][$this->_database][$this->__table]; + } + + return $ret; + } + + /** + * get/set an array of table primary keys + * + * set usage: $do->keys('id','code'); + * + * This is defined in the table definition if it gets it wrong, + * or you do not want to use ini tables, you can override this. + * @param string optional set the key + * @param * optional set more keys + * @access private + * @return array + */ + function keys() + { + // for temporary storage of database fields.. + // note this is not declared as we dont want to bloat the print_r output + $args = func_get_args(); + if (count($args)) { + $this->_database_keys = $args; + } + if (isset($this->_database_keys)) { + return $this->_database_keys; + } + + global $_DB_DATAOBJECT; + if (!isset($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5])) { + $this->_connect(); + } + if (isset($_DB_DATAOBJECT['INI'][$this->_database][$this->__table."__keys"])) { + return array_keys($_DB_DATAOBJECT['INI'][$this->_database][$this->__table."__keys"]); + } + $this->databaseStructure(); + + if (isset($_DB_DATAOBJECT['INI'][$this->_database][$this->__table."__keys"])) { + return array_keys($_DB_DATAOBJECT['INI'][$this->_database][$this->__table."__keys"]); + } + return array(); + } + /** + * get/set an sequence key + * + * by default it returns the first key from keys() + * set usage: $do->sequenceKey('id',true); + * + * override this to return array(false,false) if table has no real sequence key. + * + * @param string optional the key sequence/autoinc. key + * @param boolean optional use native increment. default false + * @param false|string optional native sequence name + * @access private + * @return array (column,use_native,sequence_name) + */ + function sequenceKey() + { + global $_DB_DATAOBJECT; + + // call setting + if (!$this->_database) { + $this->_connect(); + } + + if (!isset($_DB_DATAOBJECT['SEQUENCE'][$this->_database])) { + $_DB_DATAOBJECT['SEQUENCE'][$this->_database] = array(); + } + + + $args = func_get_args(); + if (count($args)) { + $args[1] = isset($args[1]) ? $args[1] : false; + $args[2] = isset($args[2]) ? $args[2] : false; + $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->__table] = $args; + } + if (isset($_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->__table])) { + return $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->__table]; + } + // end call setting (eg. $do->sequenceKeys(a,b,c); ) + + + + + $keys = $this->keys(); + if (!$keys) { + return $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->__table] + = array(false,false,false); + } + + + $table = isset($_DB_DATAOBJECT['INI'][$this->_database][$this->__table]) ? + $_DB_DATAOBJECT['INI'][$this->_database][$this->__table] : $this->table(); + + $dbtype = $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->dsn['phptype']; + + $usekey = $keys[0]; + + + + $seqname = false; + + if (!empty($_DB_DATAOBJECT['CONFIG']['sequence_'.$this->__table])) { + $usekey = $_DB_DATAOBJECT['CONFIG']['sequence_'.$this->__table]; + if (strpos($usekey,':') !== false) { + list($usekey,$seqname) = explode(':',$usekey); + } + } + + + // if the key is not an integer - then it's not a sequence or native + if (empty($table[$usekey]) || !($table[$usekey] & DB_DATAOBJECT_INT)) { + return $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->__table] = array(false,false,false); + } + + + if (!empty($_DB_DATAOBJECT['CONFIG']['ignore_sequence_keys'])) { + $ignore = $_DB_DATAOBJECT['CONFIG']['ignore_sequence_keys']; + if (is_string($ignore) && (strtoupper($ignore) == 'ALL')) { + return $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->__table] = array(false,false,$seqname); + } + if (is_string($ignore)) { + $ignore = $_DB_DATAOBJECT['CONFIG']['ignore_sequence_keys'] = explode(',',$ignore); + } + if (in_array($this->__table,$ignore)) { + return $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->__table] = array(false,false,$seqname); + } + } + + + $realkeys = $_DB_DATAOBJECT['INI'][$this->_database][$this->__table."__keys"]; + + // if you are using an old ini file - go back to old behaviour... + if (is_numeric($realkeys[$usekey])) { + $realkeys[$usekey] = 'N'; + } + + // multiple unique primary keys without a native sequence... + if (($realkeys[$usekey] == 'K') && (count($keys) > 1)) { + return $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->__table] = array(false,false,$seqname); + } + // use native sequence keys... + // technically postgres native here... + // we need to get the new improved tabledata sorted out first. + + if ( in_array($dbtype , array('psql', 'mysql', 'mysqli', 'mssql', 'ifx')) && + ($table[$usekey] & DB_DATAOBJECT_INT) && + isset($realkeys[$usekey]) && ($realkeys[$usekey] == 'N') + ) { + return $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->__table] = array($usekey,true,$seqname); + } + // if not a native autoinc, and we have not assumed all primary keys are sequence + if (($realkeys[$usekey] != 'N') && + !empty($_DB_DATAOBJECT['CONFIG']['dont_use_pear_sequences'])) { + return array(false,false,false); + } + // I assume it's going to try and be a nextval DB sequence.. (not native) + return $_DB_DATAOBJECT['SEQUENCE'][$this->_database][$this->__table] = array($usekey,false,$seqname); + } + + + + /* =========================================================== */ + /* Major Private Methods - the core part! */ + /* =========================================================== */ + + + + /** + * clear the cache values for this class - normally done on insert/update etc. + * + * @access private + * @return void + */ + function _clear_cache() + { + global $_DB_DATAOBJECT; + + $class = strtolower(get_class($this)); + + if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { + $this->debug("Clearing Cache for ".$class,1); + } + + if (!empty($_DB_DATAOBJECT['CACHE'][$class])) { + unset($_DB_DATAOBJECT['CACHE'][$class]); + } + } + + + /** + * backend wrapper for quoting, as MDB2 and DB do it differently... + * + * @access private + * @return string quoted + */ + + function _quote($str) + { + global $_DB_DATAOBJECT; + return (empty($_DB_DATAOBJECT['CONFIG']['db_driver']) || + ($_DB_DATAOBJECT['CONFIG']['db_driver'] == 'DB')) + ? $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->quoteSmart($str) + : $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->quote($str); + } + + + /** + * connects to the database + * + * + * TODO: tidy this up - This has grown to support a number of connection options like + * a) dynamic changing of ini file to change which database to connect to + * b) multi data via the table_{$table} = dsn ini option + * c) session based storage. + * + * @access private + * @return true | PEAR::error + */ + function _connect() + { + global $_DB_DATAOBJECT; + if (empty($_DB_DATAOBJECT['CONFIG'])) { + $this->_loadConfig(); + } + // Set database driver for reference + $db_driver = empty($_DB_DATAOBJECT['CONFIG']['db_driver']) ? 'DB' : $_DB_DATAOBJECT['CONFIG']['db_driver']; + // is it already connected ? + + if ($this->_database_dsn_md5 && !empty($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5])) { + if (PEAR::isError($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5])) { + return $this->raiseError( + $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->message, + $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->code, PEAR_ERROR_DIE + ); + + } + + if (!$this->_database) { + $this->_database = $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->dsn['database']; + $hasGetDatabase = method_exists($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5], 'getDatabase'); + + $this->_database = ($db_driver != 'DB' && $hasGetDatabase) + ? $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->getDatabase() + : $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->dsn['database']; + + + + if (($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->dsn['phptype'] == 'sqlite') + && is_file($this->_database)) { + $this->_database = basename($this->_database); + } + if ($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->dsn['phptype'] == 'ibase') { + $this->_database = substr(basename($this->_database), 0, -4); + } + + } + // theoretically we have a md5, it's listed in connections and it's not an error. + // so everything is ok! + return true; + + } + + // it's not currently connected! + // try and work out what to use for the dsn ! + + $options= &$_DB_DATAOBJECT['CONFIG']; + $dsn = isset($this->_database_dsn) ? $this->_database_dsn : null; + + if (!$dsn) { + if (!$this->_database) { + $this->_database = isset($options["table_{$this->__table}"]) ? $options["table_{$this->__table}"] : null; + } + if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { + $this->debug("Checking for database database_{$this->_database} in options","CONNECT"); + } + + if ($this->_database && !empty($options["database_{$this->_database}"])) { + + $dsn = $options["database_{$this->_database}"]; + } else if (!empty($options['database'])) { + $dsn = $options['database']; + } + } + + // if still no database... + if (!$dsn) { + return $this->raiseError( + "No database name / dsn found anywhere", + DB_DATAOBJECT_ERROR_INVALIDCONFIG, PEAR_ERROR_DIE + ); + + } + + + if (is_string($dsn)) { + $this->_database_dsn_md5 = md5($dsn); + } else { + /// support array based dsn's + $this->_database_dsn_md5 = md5(serialize($dsn)); + } + + if (!empty($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5])) { + if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { + $this->debug("USING CACHED CONNECTION", "CONNECT",3); + } + if (!$this->_database) { + + $hasGetDatabase = method_exists($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5], 'getDatabase'); + $this->_database = ($db_driver != 'DB' && $hasGetDatabase) + ? $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->getDatabase() + : $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->dsn['database']; + + if (($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->dsn['phptype'] == 'sqlite') + && is_file($this->_database)) + { + $this->_database = basename($this->_database); + } + } + return true; + } + if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { + $this->debug("NEW CONNECTION", "CONNECT",3); + /* actualy make a connection */ + $this->debug(print_r($dsn,true) ." {$this->_database_dsn_md5}", "CONNECT",3); + } + + // Note this is verbose deliberatly! + + if ($db_driver == 'DB') { + + /* PEAR DB connect */ + + // this allows the setings of compatibility on DB + $db_options = PEAR::getStaticProperty('DB','options'); + require_once 'DB.php'; + if ($db_options) { + $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5] = &DB::connect($dsn,$db_options); + } else { + $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5] = &DB::connect($dsn); + } + + } else { + /* assumption is MDB2 */ + require_once 'MDB2.php'; + // this allows the setings of compatibility on MDB2 + $db_options = PEAR::getStaticProperty('MDB2','options'); + $db_options = is_array($db_options) ? $db_options : array(); + $db_options['portability'] = isset($db_options['portability'] ) + ? $db_options['portability'] : MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_FIX_CASE; + $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5] = &MDB2::connect($dsn,$db_options); + + } + + + if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { + $this->debug(serialize($_DB_DATAOBJECT['CONNECTIONS']), "CONNECT",5); + } + if (PEAR::isError($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5])) { + $this->debug($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->toString(), "CONNECT FAILED",5); + return $this->raiseError( + "Connect failed, turn on debugging to 5 see why", + $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->code, PEAR_ERROR_DIE + ); + + } + + if (!$this->_database) { + $hasGetDatabase = method_exists($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5], 'getDatabase'); + + $this->_database = ($db_driver != 'DB' && $hasGetDatabase) + ? $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->getDatabase() + : $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->dsn['database']; + + + if (($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->dsn['phptype'] == 'sqlite') + && is_file($this->_database)) + { + $this->_database = basename($this->_database); + } + } + + // Oracle need to optimize for portibility - not sure exactly what this does though :) + $c = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; + + return true; + } + + /** + * sends query to database - this is the private one that must work + * - internal functions use this rather than $this->query() + * + * @param string $string + * @access private + * @return mixed none or PEAR_Error + */ + function _query($string) + { + global $_DB_DATAOBJECT; + $this->_connect(); + + + $DB = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; + + $options = &$_DB_DATAOBJECT['CONFIG']; + + $_DB_driver = empty($_DB_DATAOBJECT['CONFIG']['db_driver']) ? + 'DB': $_DB_DATAOBJECT['CONFIG']['db_driver']; + + if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { + $this->debug($string,$log="QUERY"); + + } + + if (strtoupper($string) == 'BEGIN') { + if ($_DB_driver == 'DB') { + $DB->autoCommit(false); + } else { + $DB->beginTransaction(); + } + // db backend adds begin anyway from now on.. + return true; + } + if (strtoupper($string) == 'COMMIT') { + $res = $DB->commit(); + if ($_DB_driver == 'DB') { + $DB->autoCommit(true); + } + return $res; + } + + if (strtoupper($string) == 'ROLLBACK') { + $DB->rollback(); + if ($_DB_driver == 'DB') { + $DB->autoCommit(true); + } + return true; + } + + + if (!empty($options['debug_ignore_updates']) && + (strtolower(substr(trim($string), 0, 6)) != 'select') && + (strtolower(substr(trim($string), 0, 4)) != 'show') && + (strtolower(substr(trim($string), 0, 8)) != 'describe')) { + + $this->debug('Disabling Update as you are in debug mode'); + return $this->raiseError("Disabling Update as you are in debug mode", null) ; + + } + //if (@$_DB_DATAOBJECT['CONFIG']['debug'] > 1) { + // this will only work when PEAR:DB supports it. + //$this->debug($DB->getAll('explain ' .$string,DB_DATAOBJECT_FETCHMODE_ASSOC), $log="sql",2); + //} + + // some sim + $t= explode(' ',microtime()); + $_DB_DATAOBJECT['QUERYENDTIME'] = $time = $t[0]+$t[1]; + + + if ($_DB_driver == 'DB') { + $result = $DB->query($string); + } else { + switch (strtolower(substr(trim($string),0,6))) { + + case 'insert': + case 'update': + case 'delete': + $result = $DB->exec($string); + break; + + default: + $result = $DB->query($string); + break; + } + } + + + + if (is_a($result,'PEAR_Error')) { + if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { + $this->debug($result->toString(), "Query Error",1 ); + } + return $this->raiseError($result); + } + if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { + $t= explode(' ',microtime()); + $_DB_DATAOBJECT['QUERYENDTIME'] = $t[0]+$t[1]; + $this->debug('QUERY DONE IN '.($t[0]+$t[1]-$time)." seconds", 'query',1); + } + switch (strtolower(substr(trim($string),0,6))) { + case 'insert': + case 'update': + case 'delete': + if ($_DB_driver == 'DB') { + // pear DB specific + return $DB->affectedRows(); + } + return $result; + } + if (is_object($result)) { + // lets hope that copying the result object is OK! + + $_DB_resultid = $GLOBALS['_DB_DATAOBJECT']['RESULTSEQ']++; + $_DB_DATAOBJECT['RESULTS'][$_DB_resultid] = $result; + $this->_DB_resultid = $_DB_resultid; + } + $this->N = 0; + if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { + $this->debug(serialize($result), 'RESULT',5); + } + if (method_exists($result, 'numrows')) { + if ($_DB_driver == 'DB') { + $DB->expectError(DB_ERROR_UNSUPPORTED); + } else { + $DB->expectError(MDB2_ERROR_UNSUPPORTED); + } + $this->N = $result->numrows(); + if (is_a($this->N,'PEAR_Error')) { + $this->N = true; + } + $DB->popExpect(); + } + } + + /** + * Builds the WHERE based on the values of of this object + * + * @param mixed $keys + * @param array $filter (used by update to only uses keys in this filter list). + * @param array $negative_filter (used by delete to prevent deleting using the keys mentioned..) + * @access private + * @return string + */ + function _build_condition($keys, $filter = array(),$negative_filter=array()) + { + global $_DB_DATAOBJECT; + $this->_connect(); + $DB = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; + + $quoteIdentifiers = !empty($_DB_DATAOBJECT['CONFIG']['quote_identifiers']); + $options = $_DB_DATAOBJECT['CONFIG']; + + // if we dont have query vars.. - reset them. + if ($this->_query === false) { + $x = new DB_DataObject; + $this->_query= $x->_query; + } + + foreach($keys as $k => $v) { + // index keys is an indexed array + /* these filter checks are a bit suspicious.. + - need to check that update really wants to work this way */ + + if ($filter) { + if (!in_array($k, $filter)) { + continue; + } + } + if ($negative_filter) { + if (in_array($k, $negative_filter)) { + continue; + } + } + if (!isset($this->$k)) { + continue; + } + + $kSql = $quoteIdentifiers + ? ( $DB->quoteIdentifier($this->__table) . '.' . $DB->quoteIdentifier($k) ) + : "{$this->__table}.{$k}"; + + + + if (is_a($this->$k,'DB_DataObject_Cast')) { + $dbtype = $DB->dsn["phptype"]; + $value = $this->$k->toString($v,$DB); + if (PEAR::isError($value)) { + $this->raiseError($value->getMessage() ,DB_DATAOBJECT_ERROR_INVALIDARG); + return false; + } + if ((strtolower($value) === 'null') && !($v & DB_DATAOBJECT_NOTNULL)) { + $this->whereAdd(" $kSql IS NULL"); + continue; + } + $this->whereAdd(" $kSql = $value"); + continue; + } + + if (!isset($options['disable_null_strings']) && (strtolower($this->$k) === 'null') && !($v & DB_DATAOBJECT_NOTNULL)) { + $this->whereAdd(" $kSql IS NULL"); + continue; + } + + + if ($v & DB_DATAOBJECT_STR) { + $this->whereAdd(" $kSql = " . $this->_quote((string) ( + ($v & DB_DATAOBJECT_BOOL) ? + // this is thanks to the braindead idea of postgres to + // use t/f for boolean. + (($this->$k === 'f') ? 0 : (int)(bool) $this->$k) : + $this->$k + )) ); + continue; + } + if (is_numeric($this->$k)) { + $this->whereAdd(" $kSql = {$this->$k}"); + continue; + } + /* this is probably an error condition! */ + $this->whereAdd(" $kSql = ".intval($this->$k)); + } + } + + /** + * autoload Class relating to a table + * (depreciated - use ::factory) + * + * @param string $table table + * @access private + * @return string classname on Success + */ + function staticAutoloadTable($table) + { + global $_DB_DATAOBJECT; + if (empty($_DB_DATAOBJECT['CONFIG'])) { + DB_DataObject::_loadConfig(); + } + $p = isset($_DB_DATAOBJECT['CONFIG']['class_prefix']) ? + $_DB_DATAOBJECT['CONFIG']['class_prefix'] : ''; + $class = $p . preg_replace('/[^A-Z0-9]/i','_',ucfirst($table)); + + $ce = substr(phpversion(),0,1) > 4 ? class_exists($class,false) : class_exists($class); + $class = $ce ? $class : DB_DataObject::_autoloadClass($class); + return $class; + } + + + /** + * classic factory method for loading a table class + * usage: $do = DB_DataObject::factory('person') + * WARNING - this may emit a include error if the file does not exist.. + * use @ to silence it (if you are sure it is acceptable) + * eg. $do = @DB_DataObject::factory('person') + * + * table name will eventually be databasename/table + * - and allow modular dataobjects to be written.. + * (this also helps proxy creation) + * + * + * @param string $table tablename (use blank to create a new instance of the same class.) + * @access private + * @return DataObject|PEAR_Error + */ + + + + function factory($table = '') { + global $_DB_DATAOBJECT; + if (empty($_DB_DATAOBJECT['CONFIG'])) { + DB_DataObject::_loadConfig(); + } + + if ($table === '') { + if (is_a($this,'DB_DataObject') && strlen($this->__table)) { + $table = $this->__table; + } else { + return DB_DataObject::raiseError( + "factory did not recieve a table name", + DB_DATAOBJECT_ERROR_INVALIDARGS); + } + } + + + $p = isset($_DB_DATAOBJECT['CONFIG']['class_prefix']) ? + $_DB_DATAOBJECT['CONFIG']['class_prefix'] : ''; + $class = $p . preg_replace('/[^A-Z0-9]/i','_',ucfirst($table)); + + $ce = substr(phpversion(),0,1) > 4 ? class_exists($class,false) : class_exists($class); + $class = $ce ? $class : DB_DataObject::_autoloadClass($class); + + // proxy = full|light + if (!$class && isset($_DB_DATAOBJECT['CONFIG']['proxy'])) { + $proxyMethod = 'getProxy'.$_DB_DATAOBJECT['CONFIG']['proxy']; + class_exists('DB_DataObject_Generator') ? '' : + require_once 'DB/DataObject/Generator.php'; + + $d = new DB_DataObject; + + $d->__table = $table; + if (is_a($ret = $d->_connect(), 'PEAR_Error')) { + return $ret; + } + + $x = new DB_DataObject_Generator; + return $x->$proxyMethod( $d->_database, $table); + } + + if (!$class) { + return DB_DataObject::raiseError( + "factory could not find class $class from $table", + DB_DATAOBJECT_ERROR_INVALIDCONFIG); + } + + return new $class; + } + /** + * autoload Class + * + * @param string $class Class + * @access private + * @return string classname on Success + */ + function _autoloadClass($class) + { + global $_DB_DATAOBJECT; + + if (empty($_DB_DATAOBJECT['CONFIG'])) { + DB_DataObject::_loadConfig(); + } + $class_prefix = empty($_DB_DATAOBJECT['CONFIG']['class_prefix']) ? + '' : $_DB_DATAOBJECT['CONFIG']['class_prefix']; + + $table = substr($class,strlen($class_prefix)); + + // only include the file if it exists - and barf badly if it has parse errors :) + if (!empty($_DB_DATAOBJECT['CONFIG']['proxy']) || empty($_DB_DATAOBJECT['CONFIG']['class_location'])) { + return false; + } + + + if (strpos($_DB_DATAOBJECT['CONFIG']['class_location'],'%s') !== false) { + $file = sprintf($_DB_DATAOBJECT['CONFIG']['class_location'], preg_replace('/[^A-Z0-9]/i','_',ucfirst($table))); + } else { + $file = $_DB_DATAOBJECT['CONFIG']['class_location'].'/'.preg_replace('/[^A-Z0-9]/i','_',ucfirst($table)).".php"; + } + + if (!file_exists($file)) { + $found = false; + foreach(explode(PATH_SEPARATOR, ini_get('include_path')) as $p) { + if (file_exists("$p/$file")) { + $file = "$p/$file"; + $found = true; + break; + } + } + if (!$found) { + DB_DataObject::raiseError( + "autoload:Could not find class {$class} using class_location value", + DB_DATAOBJECT_ERROR_INVALIDCONFIG); + return false; + } + } + + include_once $file; + + + $ce = substr(phpversion(),0,1) > 4 ? class_exists($class,false) : class_exists($class); + + if (!$ce) { + DB_DataObject::raiseError( + "autoload:Could not autoload {$class}", + DB_DATAOBJECT_ERROR_INVALIDCONFIG); + return false; + } + return $class; + } + + + + /** + * Have the links been loaded? + * if they have it contains a array of those variables. + * + * @access private + * @var boolean | array + */ + var $_link_loaded = false; + + /** + * Get the links associate array as defined by the links.ini file. + * + * + * Experimental... - + * Should look a bit like + * [local_col_name] => "related_tablename:related_col_name" + * + * + * @return array|null + * array = if there are links defined for this table. + * empty array - if there is a links.ini file, but no links on this table + * null - if no links.ini exists for this database (hence try auto_links). + * @access public + * @see DB_DataObject::getLinks(), DB_DataObject::getLink() + */ + + function links() + { + global $_DB_DATAOBJECT; + if (empty($_DB_DATAOBJECT['CONFIG'])) { + $this->_loadConfig(); + } + // have to connect.. -> otherwise things break later. + $this->_connect(); + + if (isset($_DB_DATAOBJECT['LINKS'][$this->_database][$this->__table])) { + return $_DB_DATAOBJECT['LINKS'][$this->_database][$this->__table]; + } + + + + + + // attempt to load links file here.. + + if (!isset($_DB_DATAOBJECT['LINKS'][$this->_database])) { + $schemas = isset($_DB_DATAOBJECT['CONFIG']['schema_location']) ? + array("{$_DB_DATAOBJECT['CONFIG']['schema_location']}/{$this->_database}.ini") : + array() ; + + if (isset($_DB_DATAOBJECT['CONFIG']["ini_{$this->_database}"])) { + $schemas = is_array($_DB_DATAOBJECT['CONFIG']["ini_{$this->_database}"]) ? + $_DB_DATAOBJECT['CONFIG']["ini_{$this->_database}"] : + explode(PATH_SEPARATOR,$_DB_DATAOBJECT['CONFIG']["ini_{$this->_database}"]); + } + + + + foreach ($schemas as $ini) { + + $links = + isset($_DB_DATAOBJECT['CONFIG']["links_{$this->_database}"]) ? + $_DB_DATAOBJECT['CONFIG']["links_{$this->_database}"] : + str_replace('.ini','.links.ini',$ini); + + if (empty($_DB_DATAOBJECT['LINKS'][$this->_database]) && file_exists($links) && is_file($links)) { + /* not sure why $links = ... here - TODO check if that works */ + $_DB_DATAOBJECT['LINKS'][$this->_database] = parse_ini_file($links, true); + if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { + $this->debug("Loaded links.ini file: $links","links",1); + } + } else { + if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { + $this->debug("Missing links.ini file: $links","links",1); + } + } + } + } + + + // if there is no link data at all on the file! + // we return null. + if (!isset($_DB_DATAOBJECT['LINKS'][$this->_database])) { + return null; + } + + if (isset($_DB_DATAOBJECT['LINKS'][$this->_database][$this->__table])) { + return $_DB_DATAOBJECT['LINKS'][$this->_database][$this->__table]; + } + + return array(); + } + /** + * load related objects + * + * There are two ways to use this, one is to set up a <dbname>.links.ini file + * into a static property named <dbname>.links and specifies the table joins, + * the other highly dependent on naming columns 'correctly' :) + * using colname = xxxxx_yyyyyy + * xxxxxx = related table; (yyyyy = user defined..) + * looks up table xxxxx, for value id=$this->xxxxx + * stores it in $this->_xxxxx_yyyyy + * you can change what object vars the links are stored in by + * changeing the format parameter + * + * + * @param string format (default _%s) where %s is the table name. + * @author Tim White <tim@cyface.com> + * @access public + * @return boolean , true on success + */ + function getLinks($format = '_%s') + { + + // get table will load the options. + if ($this->_link_loaded) { + return true; + } + $this->_link_loaded = false; + $cols = $this->table(); + $links = $this->links(); + + $loaded = array(); + + if ($links) { + foreach($links as $key => $match) { + list($table,$link) = explode(':', $match); + $k = sprintf($format, str_replace('.', '_', $key)); + // makes sure that '.' is the end of the key; + if ($p = strpos($key,'.')) { + $key = substr($key, 0, $p); + } + + $this->$k = $this->getLink($key, $table, $link); + + if (is_object($this->$k)) { + $loaded[] = $k; + } + } + $this->_link_loaded = $loaded; + return true; + } + // this is the autonaming stuff.. + // it sends the column name down to getLink and lets that sort it out.. + // if there is a links file then it is not used! + // IT IS DEPRECIATED!!!! - USE + if (!is_null($links)) { + return false; + } + + + foreach (array_keys($cols) as $key) { + if (!($p = strpos($key, '_'))) { + continue; + } + // does the table exist. + $k =sprintf($format, $key); + $this->$k = $this->getLink($key); + if (is_object($this->$k)) { + $loaded[] = $k; + } + } + $this->_link_loaded = $loaded; + return true; + } + + /** + * return name from related object + * + * There are two ways to use this, one is to set up a <dbname>.links.ini file + * into a static property named <dbname>.links and specifies the table joins, + * the other is highly dependant on naming columns 'correctly' :) + * + * NOTE: the naming convention is depreciated!!! - use links.ini + * + * using colname = xxxxx_yyyyyy + * xxxxxx = related table; (yyyyy = user defined..) + * looks up table xxxxx, for value id=$this->xxxxx + * stores it in $this->_xxxxx_yyyyy + * + * you can also use $this->getLink('thisColumnName','otherTable','otherTableColumnName') + * + * + * @param string $row either row or row.xxxxx + * @param string $table name of table to look up value in + * @param string $link name of column in other table to match + * @author Tim White <tim@cyface.com> + * @access public + * @return mixed object on success + */ + function getLink($row, $table = null, $link = false) + { + + + // GUESS THE LINKED TABLE.. (if found - recursevly call self) + + if ($table === null) { + $links = $this->links(); + + if (is_array($links)) { + + if ($links[$row]) { + list($table,$link) = explode(':', $links[$row]); + if ($p = strpos($row,".")) { + $row = substr($row,0,$p); + } + return $this->getLink($row,$table,$link); + + } + + $this->raiseError( + "getLink: $row is not defined as a link (normally this is ok)", + DB_DATAOBJECT_ERROR_NODATA); + + $r = false; + return $r;// technically a possible error condition? + + } + // use the old _ method - this shouldnt happen if called via getLinks() + if (!($p = strpos($row, '_'))) { + $r = null; + return $r; + } + $table = substr($row, 0, $p); + return $this->getLink($row, $table); + + + } + + + + if (!isset($this->$row)) { + $this->raiseError("getLink: row not set $row", DB_DATAOBJECT_ERROR_NODATA); + return false; + } + + // check to see if we know anything about this table.. + + $obj = $this->factory($table); + + if (!is_a($obj,'DB_DataObject')) { + $this->raiseError( + "getLink:Could not find class for row $row, table $table", + DB_DATAOBJECT_ERROR_INVALIDCONFIG); + return false; + } + if ($link) { + if ($obj->get($link, $this->$row)) { + $obj->free(); + return $obj; + } + return false; + } + + if ($obj->get($this->$row)) { + $obj->free(); + return $obj; + } + return false; + + } + + /** + * IS THIS SUPPORTED/USED ANYMORE???? + *return a list of options for a linked table + * + * This is highly dependant on naming columns 'correctly' :) + * using colname = xxxxx_yyyyyy + * xxxxxx = related table; (yyyyy = user defined..) + * looks up table xxxxx, for value id=$this->xxxxx + * stores it in $this->_xxxxx_yyyyy + * + * @access public + * @return array of results (empty array on failure) + */ + function &getLinkArray($row, $table = null) + { + + $ret = array(); + if (!$table) { + $links = $this->links(); + + if (is_array($links)) { + if (!isset($links[$row])) { + // failed.. + return $ret; + } + list($table,$link) = explode(':',$links[$row]); + } else { + if (!($p = strpos($row,'_'))) { + return $ret; + } + $table = substr($row,0,$p); + } + } + + $c = $this->factory($table); + + if (!is_a($c,'DB_DataObject')) { + $this->raiseError( + "getLinkArray:Could not find class for row $row, table $table", + DB_DATAOBJECT_ERROR_INVALIDCONFIG + ); + return $ret; + } + + // if the user defined method list exists - use it... + if (method_exists($c, 'listFind')) { + $c->listFind($this->id); + } else { + $c->find(); + } + while ($c->fetch()) { + $ret[] = $c; + } + return $ret; + } + + /** + * The JOIN condition + * + * @access private + * @var string + */ + var $_join = ''; + + /** + * joinAdd - adds another dataobject to this, building a joined query. + * + * example (requires links.ini to be set up correctly) + * // get all the images for product 24 + * $i = new DataObject_Image(); + * $pi = new DataObjects_Product_image(); + * $pi->product_id = 24; // set the product id to 24 + * $i->joinAdd($pi); // add the product_image connectoin + * $i->find(); + * while ($i->fetch()) { + * // do stuff + * } + * // an example with 2 joins + * // get all the images linked with products or productgroups + * $i = new DataObject_Image(); + * $pi = new DataObject_Product_image(); + * $pgi = new DataObject_Productgroup_image(); + * $i->joinAdd($pi); + * $i->joinAdd($pgi); + * $i->find(); + * while ($i->fetch()) { + * // do stuff + * } + * + * + * @param optional $obj object |array the joining object (no value resets the join) + * If you use an array here it should be in the format: + * array('local_column','remotetable:remote_column'); + * if remotetable does not have a definition, you should + * use @ to hide the include error message.. + * + * + * @param optional $joinType string | array + * 'LEFT'|'INNER'|'RIGHT'|'' Inner is default, '' indicates + * just select ... from a,b,c with no join and + * links are added as where items. + * + * If second Argument is array, it is assumed to be an associative + * array with arguments matching below = eg. + * 'joinType' => 'INNER', + * 'joinAs' => '...' + * 'joinCol' => .... + * 'useWhereAsOn' => false, + * + * @param optional $joinAs string if you want to select the table as anther name + * useful when you want to select multiple columsn + * from a secondary table. + + * @param optional $joinCol string The column on This objects table to match (needed + * if this table links to the child object in + * multiple places eg. + * user->friend (is a link to another user) + * user->mother (is a link to another user..) + * + * optional 'useWhereAsOn' bool default false; + * convert the where argments from the object being added + * into ON arguments. + * + * + * @return none + * @access public + * @author Stijn de Reede <sjr@gmx.co.uk> + */ + function joinAdd($obj = false, $joinType='INNER', $joinAs=false, $joinCol=false) + { + global $_DB_DATAOBJECT; + if ($obj === false) { + $this->_join = ''; + return; + } + + + $useWhereAsOn = false; + // support for 2nd argument as an array of options + if (is_array($joinType)) { + // new options can now go in here... (dont forget to document them) + $useWhereAsOn = !empty($joinType['useWhereAsOn']); + $joinCol = isset($joinType['joinCol']) ? $joinType['joinCol'] : $joinCol; + $joinAs = isset($joinType['joinAs']) ? $joinType['joinAs'] : $joinAs; + $joinType = isset($joinType['joinType']) ? $joinType['joinType'] : 'INNER'; + } + // support for array as first argument + // this assumes that you dont have a links.ini for the specified table. + // and it doesnt exist as am extended dataobject!! - experimental. + + $ofield = false; // object field + $tfield = false; // this field + $toTable = false; + if (is_array($obj)) { + $tfield = $obj[0]; + list($toTable,$ofield) = explode(':',$obj[1]); + $obj = DB_DataObject::factory($toTable); + + if (!$obj || is_a($obj,'PEAR_Error')) { + $obj = new DB_DataObject; + $obj->__table = $toTable; + } + $obj->_connect(); + // set the table items to nothing.. - eg. do not try and match + // things in the child table...??? + $items = array(); + } + + if (!is_object($obj) || !is_a($obj,'DB_DataObject')) { + return $this->raiseError("joinAdd: called without an object", DB_DATAOBJECT_ERROR_NODATA,PEAR_ERROR_DIE); + } + /* make sure $this->_database is set. */ + $this->_connect(); + $DB = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; + + + + + /* look up the links for obj table */ + //print_r($obj->links()); + if (!$ofield && ($olinks = $obj->links())) { + + foreach ($olinks as $k => $v) { + /* link contains {this column} = {linked table}:{linked column} */ + $ar = explode(':', $v); + + // Feature Request #4266 - Allow joins with multiple keys + + $links_key_array = strpos($k,','); + if ($links_key_array !== false) { + $k = explode(',', $k); + } + + $ar_array = strpos($ar[1],','); + if ($ar_array !== false) { + $ar[1] = explode(',', $ar[1]); + } + + if ($ar[0] == $this->__table) { + + // you have explictly specified the column + // and the col is listed here.. + // not sure if 1:1 table could cause probs here.. + + if ($joinCol !== false) { + $this->raiseError( + "joinAdd: You cannot target a join column in the " . + "'link from' table ({$obj->__table}). " . + "Either remove the fourth argument to joinAdd() ". + "({$joinCol}), or alter your links.ini file.", + DB_DATAOBJECT_ERROR_NODATA); + return false; + } + + $ofield = $k; + $tfield = $ar[1]; + break; + } + } + } + + /* otherwise see if there are any links from this table to the obj. */ + //print_r($this->links()); + if (($ofield === false) && ($links = $this->links())) { + foreach ($links as $k => $v) { + /* link contains {this column} = {linked table}:{linked column} */ + $ar = explode(':', $v); + // Feature Request #4266 - Allow joins with multiple keys + if (strpos($k, ',') !== false) { + $k = explode(',', $k); + } + if (strpos($ar[1], ',') !== false) { + $ar[1] = explode(',', $ar[1]); + } + + if ($ar[0] == $obj->__table) { + if ($joinCol !== false) { + if ($k == $joinCol) { + $tfield = $k; + $ofield = $ar[1]; + break; + } else { + continue; + } + } else { + $tfield = $k; + $ofield = $ar[1]; + break; + } + } + } + } + // finally if these two table have column names that match do a join by default on them + + if (($ofield === false) && $joinCol) { + $ofield = $joinCol; + $tfield = $joinCol; + + } + /* did I find a conneciton between them? */ + + if ($ofield === false) { + $this->raiseError( + "joinAdd: {$obj->__table} has no link with {$this->__table}", + DB_DATAOBJECT_ERROR_NODATA); + return false; + } + $joinType = strtoupper($joinType); + + // we default to joining as the same name (this is remvoed later..) + + if ($joinAs === false) { + $joinAs = $obj->__table; + } + + $quoteIdentifiers = !empty($_DB_DATAOBJECT['CONFIG']['quote_identifiers']); + $options = $_DB_DATAOBJECT['CONFIG']; + + // not sure how portable adding database prefixes is.. + $objTable = $quoteIdentifiers ? + $DB->quoteIdentifier($obj->__table) : + $obj->__table ; + + $dbPrefix = ''; + if (strlen($obj->_database) && in_array($DB->dsn['phptype'],array('mysql','mysqli'))) { + $dbPrefix = ($quoteIdentifiers + ? $DB->quoteIdentifier($obj->_database) + : $obj->_database) . '.'; + } + + // if they are the same, then dont add a prefix... + if ($obj->_database == $this->_database) { + $dbPrefix = ''; + } + // as far as we know only mysql supports database prefixes.. + // prefixing the database name is now the default behaviour, + // as it enables joining mutiple columns from multiple databases... + + // prefix database (quoted if neccessary..) + $objTable = $dbPrefix . $objTable; + + $cond = ''; + + // if obj only a dataobject - eg. no extended class has been defined.. + // it obvioulsy cant work out what child elements might exist... + // until we get on the fly querying of tables.. + // note: we have already checked that it is_a(db_dataobject earlier) + if ( strtolower(get_class($obj)) != 'db_dataobject') { + + // now add where conditions for anything that is set in the object + + + + $items = $obj->table(); + // will return an array if no items.. + + // only fail if we where expecting it to work (eg. not joined on a array) + + if (!$items) { + $this->raiseError( + "joinAdd: No table definition for {$obj->__table}", + DB_DATAOBJECT_ERROR_INVALIDCONFIG); + return false; + } + + foreach($items as $k => $v) { + if (!isset($obj->$k)) { + continue; + } + + $kSql = ($quoteIdentifiers ? $DB->quoteIdentifier($k) : $k); + + + if ($v & DB_DATAOBJECT_STR) { + $obj->whereAdd("{$joinAs}.{$kSql} = " . $this->_quote((string) ( + ($v & DB_DATAOBJECT_BOOL) ? + // this is thanks to the braindead idea of postgres to + // use t/f for boolean. + (($obj->$k === 'f') ? 0 : (int)(bool) $obj->$k) : + $obj->$k + ))); + continue; + } + if (is_numeric($obj->$k)) { + $obj->whereAdd("{$joinAs}.{$kSql} = {$obj->$k}"); + continue; + } + + if (is_a($obj->$k,'DB_DataObject_Cast')) { + $value = $obj->$k->toString($v,$DB); + if (PEAR::isError($value)) { + $this->raiseError($value->getMessage() ,DB_DATAOBJECT_ERROR_INVALIDARG); + return false; + } + if (!isset($options['disable_null_strings']) && strtolower($value) === 'null') { + $obj->whereAdd("{$joinAs}.{$kSql} IS NULL"); + continue; + } else { + $obj->whereAdd("{$joinAs}.{$kSql} = $value"); + continue; + } + } + + + /* this is probably an error condition! */ + $obj->whereAdd("{$joinAs}.{$kSql} = 0"); + } + if ($this->_query === false) { + $this->raiseError( + "joinAdd can not be run from a object that has had a query run on it, + clone the object or create a new one and use setFrom()", + DB_DATAOBJECT_ERROR_INVALIDARGS); + return false; + } + } + + // and finally merge the whereAdd from the child.. + if ($obj->_query['condition']) { + $cond = preg_replace('/^\sWHERE/i','',$obj->_query['condition']); + + if (!$useWhereAsOn) { + $this->whereAdd($cond); + } + } + + + + + // nested (join of joined objects..) + $appendJoin = ''; + if ($obj->_join) { + // postgres allows nested queries, with ()'s + // not sure what the results are with other databases.. + // may be unpredictable.. + if (in_array($DB->dsn["phptype"],array('pgsql'))) { + $objTable = "($objTable {$obj->_join})"; + } else { + $appendJoin = $obj->_join; + } + } + + + // fix for #2216 + // add the joinee object's conditions to the ON clause instead of the WHERE clause + if ($useWhereAsOn && strlen($cond)) { + $appendJoin = ' AND ' . $cond . ' ' . $appendJoin; + } + + + + $table = $this->__table; + + if ($quoteIdentifiers) { + $joinAs = $DB->quoteIdentifier($joinAs); + $table = $DB->quoteIdentifier($table); + $ofield = (is_array($ofield)) ? array_map(array($DB, 'quoteIdentifier'), $ofield) : $DB->quoteIdentifier($ofield); + $tfield = (is_array($tfield)) ? array_map(array($DB, 'quoteIdentifier'), $tfield) : $DB->quoteIdentifier($tfield); + } + // add database prefix if they are different databases + + + $fullJoinAs = ''; + $addJoinAs = ($quoteIdentifiers ? $DB->quoteIdentifier($obj->__table) : $obj->__table) != $joinAs; + if ($addJoinAs) { + // join table a AS b - is only supported by a few databases and is probably not needed + // , however since it makes the whole Statement alot clearer we are leaving it in + // for those databases. + $fullJoinAs = in_array($DB->dsn["phptype"],array('mysql','mysqli','pgsql')) ? "AS {$joinAs}" : $joinAs; + } else { + // if + $joinAs = $dbPrefix . $joinAs; + } + + + switch ($joinType) { + case 'INNER': + case 'LEFT': + case 'RIGHT': // others??? .. cross, left outer, right outer, natural..? + + // Feature Request #4266 - Allow joins with multiple keys + $this->_join .= "\n {$joinType} JOIN {$objTable} {$fullJoinAs}"; + if (is_array($ofield)) { + $key_count = count($ofield); + for($i = 0; $i < $key_count; $i++) { + if ($i == 0) { + $this->_join .= " ON ({$joinAs}.{$ofield[$i]}={$table}.{$tfield[$i]}) "; + } + else { + $this->_join .= " AND {$joinAs}.{$ofield[$i]}={$table}.{$tfield[$i]} "; + } + } + $this->_join .= ' ' . $appendJoin . ' '; + } else { + $this->_join .= " ON ({$joinAs}.{$ofield}={$table}.{$tfield}) {$appendJoin} "; + } + + break; + + case '': // this is just a standard multitable select.. + $this->_join .= "\n , {$objTable} {$fullJoinAs} {$appendJoin}"; + $this->whereAdd("{$joinAs}.{$ofield}={$table}.{$tfield}"); + } + + + return true; + + } + + /** + * Copies items that are in the table definitions from an + * array or object into the current object + * will not override key values. + * + * + * @param array | object $from + * @param string $format eg. map xxxx_name to $object->name using 'xxxx_%s' (defaults to %s - eg. name -> $object->name + * @param boolean $skipEmpty (dont assign empty values if a column is empty (eg. '' / 0 etc...) + * @access public + * @return true on success or array of key=>setValue error message + */ + function setFrom($from, $format = '%s', $skipEmpty=false) + { + global $_DB_DATAOBJECT; + $keys = $this->keys(); + $items = $this->table(); + if (!$items) { + $this->raiseError( + "setFrom:Could not find table definition for {$this->__table}", + DB_DATAOBJECT_ERROR_INVALIDCONFIG); + return; + } + $overload_return = array(); + foreach (array_keys($items) as $k) { + if (in_array($k,$keys)) { + continue; // dont overwrite keys + } + if (!$k) { + continue; // ignore empty keys!!! what + } + if (is_object($from) && isset($from->{sprintf($format,$k)})) { + $kk = (strtolower($k) == 'from') ? '_from' : $k; + if (method_exists($this,'set'.$kk)) { + $ret = $this->{'set'.$kk}($from->{sprintf($format,$k)}); + if (is_string($ret)) { + $overload_return[$k] = $ret; + } + continue; + } + $this->$k = $from->{sprintf($format,$k)}; + continue; + } + + if (is_object($from)) { + continue; + } + + if (empty($from[$k]) && $skipEmpty) { + continue; + } + + if (!isset($from[sprintf($format,$k)])) { + continue; + } + + $kk = (strtolower($k) == 'from') ? '_from' : $k; + if (method_exists($this,'set'. $kk)) { + $ret = $this->{'set'.$kk}($from[sprintf($format,$k)]); + if (is_string($ret)) { + $overload_return[$k] = $ret; + } + continue; + } + if (is_object($from[sprintf($format,$k)])) { + continue; + } + if (is_array($from[sprintf($format,$k)])) { + continue; + } + $ret = $this->fromValue($k,$from[sprintf($format,$k)]); + if ($ret !== true) { + $overload_return[$k] = 'Not A Valid Value'; + } + //$this->$k = $from[sprintf($format,$k)]; + } + if ($overload_return) { + return $overload_return; + } + return true; + } + + /** + * Returns an associative array from the current data + * (kind of oblivates the idea behind DataObjects, but + * is usefull if you use it with things like QuickForms. + * + * you can use the format to return things like user[key] + * by sending it $object->toArray('user[%s]') + * + * will also return links converted to arrays. + * + * @param string sprintf format for array + * @param bool empty only return elemnts that have a value set. + * + * @access public + * @return array of key => value for row + */ + + function toArray($format = '%s', $hideEmpty = false) + { + global $_DB_DATAOBJECT; + $ret = array(); + $rf = ($this->_resultFields !== false) ? $this->_resultFields : + (isset($_DB_DATAOBJECT['RESULTFIELDS'][$this->_DB_resultid]) ? $_DB_DATAOBJECT['RESULTFIELDS'][$this->_DB_resultid] : false); + $ar = ($rf !== false) ? + array_merge($_DB_DATAOBJECT['RESULTFIELDS'][$this->_DB_resultid],$this->table()) : + $this->table(); + + foreach($ar as $k=>$v) { + + if (!isset($this->$k)) { + if (!$hideEmpty) { + $ret[sprintf($format,$k)] = ''; + } + continue; + } + // call the overloaded getXXXX() method. - except getLink and getLinks + if (method_exists($this,'get'.$k) && !in_array(strtolower($k),array('links','link'))) { + $ret[sprintf($format,$k)] = $this->{'get'.$k}(); + continue; + } + // should this call toValue() ??? + $ret[sprintf($format,$k)] = $this->$k; + } + if (!$this->_link_loaded) { + return $ret; + } + foreach($this->_link_loaded as $k) { + $ret[sprintf($format,$k)] = $this->$k->toArray(); + + } + + return $ret; + } + + /** + * validate the values of the object (usually prior to inserting/updating..) + * + * Note: This was always intended as a simple validation routine. + * It lacks understanding of field length, whether you are inserting or updating (and hence null key values) + * + * This should be moved to another class: DB_DataObject_Validate + * FEEL FREE TO SEND ME YOUR VERSION FOR CONSIDERATION!!! + * + * Usage: + * if (is_array($ret = $obj->validate())) { ... there are problems with the data ... } + * + * Logic: + * - defaults to only testing strings/numbers if numbers or strings are the correct type and null values are correct + * - validate Column methods : "validate{ROWNAME}()" are called if they are defined. + * These methods should return + * true = everything ok + * false|object = something is wrong! + * + * - This method loads and uses the PEAR Validate Class. + * + * + * @access public + * @return array of validation results (where key=>value, value=false|object if it failed) or true (if they all succeeded) + */ + function validate() + { + global $_DB_DATAOBJECT; + require_once 'Validate.php'; + $table = $this->table(); + $ret = array(); + $seq = $this->sequenceKey(); + $options = $_DB_DATAOBJECT['CONFIG']; + foreach($table as $key => $val) { + + + // call user defined validation always... + $method = "Validate" . ucfirst($key); + if (method_exists($this, $method)) { + $ret[$key] = $this->$method(); + continue; + } + + // if not null - and it's not set....... + + if (!isset($this->$key) && ($val & DB_DATAOBJECT_NOTNULL)) { + // dont check empty sequence key values.. + if (($key == $seq[0]) && ($seq[1] == true)) { + continue; + } + $ret[$key] = false; + continue; + } + + + if (!isset($options['disable_null_strings']) && is_string($this->$key) && (strtolower($this->$key) == 'null')) { + if ($val & DB_DATAOBJECT_NOTNULL) { + $this->debug("'null' field used for '$key', but it is defined as NOT NULL", 'VALIDATION', 4); + $ret[$key] = false; + continue; + } + continue; + } + + // ignore things that are not set. ? + + if (!isset($this->$key)) { + continue; + } + + // if the string is empty.. assume it is ok.. + if (!is_object($this->$key) && !is_array($this->$key) && !strlen((string) $this->$key)) { + continue; + } + + // dont try and validate cast objects - assume they are problably ok.. + if (is_object($this->$key) && is_a($this->$key,'DB_DataObject_Cast')) { + continue; + } + // at this point if you have set something to an object, and it's not expected + // the Validate will probably break!!... - rightly so! (your design is broken, + // so issuing a runtime error like PEAR_Error is probably not appropriate.. + + switch (true) { + // todo: date time..... + case ($val & DB_DATAOBJECT_STR): + $ret[$key] = Validate::string($this->$key, VALIDATE_PUNCTUATION . VALIDATE_NAME); + continue; + case ($val & DB_DATAOBJECT_INT): + $ret[$key] = Validate::number($this->$key, array('decimal'=>'.')); + continue; + } + } + // if any of the results are false or an object (eg. PEAR_Error).. then return the array.. + foreach ($ret as $key => $val) { + if ($val !== true) { + return $ret; + } + } + return true; // everything is OK. + } + + /** + * Gets the DB object related to an object - so you can use funky peardb stuf with it :) + * + * @access public + * @return object The DB connection + */ + function &getDatabaseConnection() + { + global $_DB_DATAOBJECT; + + if (($e = $this->_connect()) !== true) { + return $e; + } + if (!isset($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5])) { + $r = false; + return $r; + } + return $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; + } + + + /** + * Gets the DB result object related to the objects active query + * - so you can use funky pear stuff with it - like pager for example.. :) + * + * @access public + * @return object The DB result object + */ + + function &getDatabaseResult() + { + global $_DB_DATAOBJECT; + $this->_connect(); + if (!isset($_DB_DATAOBJECT['RESULTS'][$this->_DB_resultid])) { + $r = false; + return $r; + } + return $_DB_DATAOBJECT['RESULTS'][$this->_DB_resultid]; + } + + /** + * Overload Extension support + * - enables setCOLNAME/getCOLNAME + * if you define a set/get method for the item it will be called. + * otherwise it will just return/set the value. + * NOTE this currently means that a few Names are NO-NO's + * eg. links,link,linksarray, from, Databaseconnection,databaseresult + * + * note + * - set is automatically called by setFrom. + * - get is automatically called by toArray() + * + * setters return true on success. = strings on failure + * getters return the value! + * + * this fires off trigger_error - if any problems.. pear_error, + * has problems with 4.3.2RC2 here + * + * @access public + * @return true? + * @see overload + */ + + + function _call($method,$params,&$return) { + + //$this->debug("ATTEMPTING OVERLOAD? $method"); + // ignore constructors : - mm + if (strtolower($method) == strtolower(get_class($this))) { + return true; + } + $type = strtolower(substr($method,0,3)); + $class = get_class($this); + if (($type != 'set') && ($type != 'get')) { + return false; + } + + + + // deal with naming conflick of setFrom = this is messy ATM! + + if (strtolower($method) == 'set_from') { + $return = $this->toValue('from',isset($params[0]) ? $params[0] : null); + return true; + } + + $element = substr($method,3); + + // dont you just love php's case insensitivity!!!! + + $array = array_keys(get_class_vars($class)); + /* php5 version which segfaults on 5.0.3 */ + if (class_exists('ReflectionClass')) { + $reflection = new ReflectionClass($class); + $array = array_keys($reflection->getdefaultProperties()); + } + + if (!in_array($element,$array)) { + // munge case + foreach($array as $k) { + $case[strtolower($k)] = $k; + } + if ((substr(phpversion(),0,1) == 5) && isset($case[strtolower($element)])) { + trigger_error("PHP5 set/get calls should match the case of the variable",E_USER_WARNING); + $element = strtolower($element); + } + + // does it really exist? + if (!isset($case[$element])) { + return false; + } + // use the mundged case + $element = $case[$element]; // real case ! + } + + + if ($type == 'get') { + $return = $this->toValue($element,isset($params[0]) ? $params[0] : null); + return true; + } + + + $return = $this->fromValue($element, $params[0]); + + return true; + + + } + + + /** + * standard set* implementation. + * + * takes data and uses it to set dates/strings etc. + * normally called from __call.. + * + * Current supports + * date = using (standard time format, or unixtimestamp).... so you could create a method : + * function setLastread($string) { $this->fromValue('lastread',strtotime($string)); } + * + * time = using strtotime + * datetime = using same as date - accepts iso standard or unixtimestamp. + * string = typecast only.. + * + * TODO: add formater:: eg. d/m/Y for date! ??? + * + * @param string column of database + * @param mixed value to assign + * + * @return true| false (False on error) + * @access public + * @see DB_DataObject::_call + */ + + + function fromValue($col,$value) + { + global $_DB_DATAOBJECT; + $options = $_DB_DATAOBJECT['CONFIG']; + $cols = $this->table(); + // dont know anything about this col.. + if (!isset($cols[$col])) { + $this->$col = $value; + return true; + } + //echo "FROM VALUE $col, {$cols[$col]}, $value\n"; + switch (true) { + // set to null and column is can be null... + case (!isset($options['disable_null_strings']) && (strtolower($value) == 'null') && (!($cols[$col] & DB_DATAOBJECT_NOTNULL))): + case (is_object($value) && is_a($value,'DB_DataObject_Cast')): + $this->$col = $value; + return true; + + // fail on setting null on a not null field.. + case (!isset($options['disable_null_strings']) && (strtolower($value) == 'null') && ($cols[$col] & DB_DATAOBJECT_NOTNULL)): + return false; + + case (($cols[$col] & DB_DATAOBJECT_DATE) && ($cols[$col] & DB_DATAOBJECT_TIME)): + // empty values get set to '' (which is inserted/updated as NULl + if (!$value) { + $this->$col = ''; + } + + if (is_numeric($value)) { + $this->$col = date('Y-m-d H:i:s', $value); + return true; + } + + // eak... - no way to validate date time otherwise... + $this->$col = (string) $value; + return true; + + case ($cols[$col] & DB_DATAOBJECT_DATE): + // empty values get set to '' (which is inserted/updated as NULl + + if (!$value) { + $this->$col = ''; + return true; + } + + if (is_numeric($value)) { + $this->$col = date('Y-m-d',$value); + return true; + } + + // try date!!!! + require_once 'Date.php'; + $x = new Date($value); + $this->$col = $x->format("%Y-%m-%d"); + return true; + + case ($cols[$col] & DB_DATAOBJECT_TIME): + // empty values get set to '' (which is inserted/updated as NULl + if (!$value) { + $this->$col = ''; + } + + $guess = strtotime($value); + if ($guess != -1) { + $this->$col = date('H:i:s', $guess); + return $return = true; + } + // otherwise an error in type... + return false; + + case ($cols[$col] & DB_DATAOBJECT_STR): + + $this->$col = (string) $value; + return true; + + // todo : floats numerics and ints... + default: + $this->$col = $value; + return true; + } + + + + } + /** + * standard get* implementation. + * + * with formaters.. + * supported formaters: + * date/time : %d/%m/%Y (eg. php strftime) or pear::Date + * numbers : %02d (eg. sprintf) + * NOTE you will get unexpected results with times like 0000-00-00 !!! + * + * + * + * @param string column of database + * @param format foramt + * + * @return true Description + * @access public + * @see DB_DataObject::_call(),strftime(),Date::format() + */ + function toValue($col,$format = null) + { + if (is_null($format)) { + return $this->$col; + } + $cols = $this->table(); + switch (true) { + case (($cols[$col] & DB_DATAOBJECT_DATE) && ($cols[$col] & DB_DATAOBJECT_TIME)): + if (!$this->$col) { + return ''; + } + $guess = strtotime($this->$col); + if ($guess != -1) { + return strftime($format, $guess); + } + // eak... - no way to validate date time otherwise... + return $this->$col; + case ($cols[$col] & DB_DATAOBJECT_DATE): + if (!$this->$col) { + return ''; + } + $guess = strtotime($this->$col); + if ($guess != -1) { + return strftime($format,$guess); + } + // try date!!!! + require_once 'Date.php'; + $x = new Date($this->$col); + return $x->format($format); + + case ($cols[$col] & DB_DATAOBJECT_TIME): + if (!$this->$col) { + return ''; + } + $guess = strtotime($this->$col); + if ($guess > -1) { + return strftime($format, $guess); + } + // otherwise an error in type... + return $this->$col; + + case ($cols[$col] & DB_DATAOBJECT_MYSQLTIMESTAMP): + if (!$this->$col) { + return ''; + } + require_once 'Date.php'; + + $x = new Date($this->$col); + + return $x->format($format); + + + case ($cols[$col] & DB_DATAOBJECT_BOOL): + + if ($cols[$col] & DB_DATAOBJECT_STR) { + // it's a 't'/'f' ! + return ($this->$col === 't'); + } + return (bool) $this->$col; + + + default: + return sprintf($format,$this->col); + } + + + } + + + /* ----------------------- Debugger ------------------ */ + + /** + * Debugger. - use this in your extended classes to output debugging information. + * + * Uses DB_DataObject::DebugLevel(x) to turn it on + * + * @param string $message - message to output + * @param string $logtype - bold at start + * @param string $level - output level + * @access public + * @return none + */ + function debug($message, $logtype = 0, $level = 1) + { + global $_DB_DATAOBJECT; + + if (empty($_DB_DATAOBJECT['CONFIG']['debug']) || + (is_numeric($_DB_DATAOBJECT['CONFIG']['debug']) && $_DB_DATAOBJECT['CONFIG']['debug'] < $level)) { + return; + } + // this is a bit flaky due to php's wonderfull class passing around crap.. + // but it's about as good as it gets.. + $class = (isset($this) && is_a($this,'DB_DataObject')) ? get_class($this) : 'DB_DataObject'; + + if (!is_string($message)) { + $message = print_r($message,true); + } + if (!is_numeric( $_DB_DATAOBJECT['CONFIG']['debug']) && is_callable( $_DB_DATAOBJECT['CONFIG']['debug'])) { + return call_user_func($_DB_DATAOBJECT['CONFIG']['debug'], $class, $message, $logtype, $level); + } + + if (!ini_get('html_errors')) { + echo "$class : $logtype : $message\n"; + flush(); + return; + } + if (!is_string($message)) { + $message = print_r($message,true); + } + $colorize = ($logtype == 'ERROR') ? '<font color="red">' : '<font>'; + echo "<code>{$colorize}<B>$class: $logtype:</B> ". nl2br(htmlspecialchars($message)) . "</font></code><BR>\n"; + } + + /** + * sets and returns debug level + * eg. DB_DataObject::debugLevel(4); + * + * @param int $v level + * @access public + * @return none + */ + function debugLevel($v = null) + { + global $_DB_DATAOBJECT; + if (empty($_DB_DATAOBJECT['CONFIG'])) { + DB_DataObject::_loadConfig(); + } + if ($v !== null) { + $r = isset($_DB_DATAOBJECT['CONFIG']['debug']) ? $_DB_DATAOBJECT['CONFIG']['debug'] : 0; + $_DB_DATAOBJECT['CONFIG']['debug'] = $v; + return $r; + } + return isset($_DB_DATAOBJECT['CONFIG']['debug']) ? $_DB_DATAOBJECT['CONFIG']['debug'] : 0; + } + + /** + * Last Error that has occured + * - use $this->_lastError or + * $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError'); + * + * @access public + * @var object PEAR_Error (or false) + */ + var $_lastError = false; + + /** + * Default error handling is to create a pear error, but never return it. + * if you need to handle errors you should look at setting the PEAR_Error callback + * this is due to the fact it would wreck havoc on the internal methods! + * + * @param int $message message + * @param int $type type + * @param int $behaviour behaviour (die or continue!); + * @access public + * @return error object + */ + function raiseError($message, $type = null, $behaviour = null) + { + global $_DB_DATAOBJECT; + + if ($behaviour == PEAR_ERROR_DIE && !empty($_DB_DATAOBJECT['CONFIG']['dont_die'])) { + $behaviour = null; + } + $error = &PEAR::getStaticProperty('DB_DataObject','lastError'); + + // this will never work totally with PHP's object model. + // as this is passed on static calls (like staticGet in our case) + + if (isset($this) && is_object($this) && is_subclass_of($this,'db_dataobject')) { + $this->_lastError = $error; + } + + $_DB_DATAOBJECT['LASTERROR'] = $error; + + // no checks for production here?....... - we log errors before we throw them. + DB_DataObject::debug($message,'ERROR',1); + + + if (PEAR::isError($message)) { + $error = $message; + } else { + require_once 'DB/DataObject/Error.php'; + $error = PEAR::raiseError($message, $type, $behaviour, + $opts=null, $userinfo=null, 'DB_DataObject_Error' + ); + } + + return $error; + } + + /** + * Define the global $_DB_DATAOBJECT['CONFIG'] as an alias to PEAR::getStaticProperty('DB_DataObject','options'); + * + * After Profiling DB_DataObject, I discoved that the debug calls where taking + * considerable time (well 0.1 ms), so this should stop those calls happening. as + * all calls to debug are wrapped with direct variable queries rather than actually calling the funciton + * THIS STILL NEEDS FURTHER INVESTIGATION + * + * @access public + * @return object an error object + */ + function _loadConfig() + { + global $_DB_DATAOBJECT; + + $_DB_DATAOBJECT['CONFIG'] = &PEAR::getStaticProperty('DB_DataObject','options'); + + + } + /** + * Free global arrays associated with this object. + * + * + * @access public + * @return none + */ + function free() + { + global $_DB_DATAOBJECT; + + if (isset($_DB_DATAOBJECT['RESULTFIELDS'][$this->_DB_resultid])) { + unset($_DB_DATAOBJECT['RESULTFIELDS'][$this->_DB_resultid]); + } + if (isset($_DB_DATAOBJECT['RESULTS'][$this->_DB_resultid])) { + unset($_DB_DATAOBJECT['RESULTS'][$this->_DB_resultid]); + } + // clear the staticGet cache as well. + $this->_clear_cache(); + // this is a huge bug in DB! + if (isset($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5])) { + $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]->num_rows = array(); + } + + } + + + /* ---- LEGACY BC METHODS - NOT DOCUMENTED - See Documentation on New Methods. ---*/ + + function _get_table() { return $this->table(); } + function _get_keys() { return $this->keys(); } + + + + +} +// technially 4.3.2RC1 was broken!! +// looks like 4.3.3 may have problems too.... +if (!defined('DB_DATAOBJECT_NO_OVERLOAD')) { + + if ((phpversion() != '4.3.2-RC1') && (version_compare( phpversion(), "4.3.1") > 0)) { + if (version_compare( phpversion(), "5") < 0) { + overload('DB_DataObject'); + } + $GLOBALS['_DB_DATAOBJECT']['OVERLOADED'] = true; + } +} + diff --git a/extlib/Mail.php b/extlib/Mail.php new file mode 100644 index 000000000..3a0c1a9cb --- /dev/null +++ b/extlib/Mail.php @@ -0,0 +1,238 @@ +<?php +// +// +----------------------------------------------------------------------+ +// | PHP Version 4 | +// +----------------------------------------------------------------------+ +// | Copyright (c) 1997-2003 The PHP Group | +// +----------------------------------------------------------------------+ +// | This source file is subject to version 2.02 of the PHP license, | +// | that is bundled with this package in the file LICENSE, and is | +// | available at through the world-wide-web at | +// | http://www.php.net/license/2_02.txt. | +// | If you did not receive a copy of the PHP license and are unable to | +// | obtain it through the world-wide-web, please send a note to | +// | license@php.net so we can mail you a copy immediately. | +// +----------------------------------------------------------------------+ +// | Author: Chuck Hagenbuch <chuck@horde.org> | +// +----------------------------------------------------------------------+ +// +// $Id: Mail.php,v 1.17 2006/09/15 03:41:18 jon Exp $ + +require_once 'PEAR.php'; + +/** + * PEAR's Mail:: interface. Defines the interface for implementing + * mailers under the PEAR hierarchy, and provides supporting functions + * useful in multiple mailer backends. + * + * @access public + * @version $Revision: 1.17 $ + * @package Mail + */ +class Mail +{ + /** + * Line terminator used for separating header lines. + * @var string + */ + var $sep = "\r\n"; + + /** + * Provides an interface for generating Mail:: objects of various + * types + * + * @param string $driver The kind of Mail:: object to instantiate. + * @param array $params The parameters to pass to the Mail:: object. + * @return object Mail a instance of the driver class or if fails a PEAR Error + * @access public + */ + function &factory($driver, $params = array()) + { + $driver = strtolower($driver); + @include_once 'Mail/' . $driver . '.php'; + $class = 'Mail_' . $driver; + if (class_exists($class)) { + $mailer = new $class($params); + return $mailer; + } else { + return PEAR::raiseError('Unable to find class for driver ' . $driver); + } + } + + /** + * Implements Mail::send() function using php's built-in mail() + * command. + * + * @param mixed $recipients Either a comma-seperated list of recipients + * (RFC822 compliant), or an array of recipients, + * each RFC822 valid. This may contain recipients not + * specified in the headers, for Bcc:, resending + * messages, etc. + * + * @param array $headers The array of headers to send with the mail, in an + * associative array, where the array key is the + * header name (ie, 'Subject'), and the array value + * is the header value (ie, 'test'). The header + * produced from those values would be 'Subject: + * test'. + * + * @param string $body The full text of the message body, including any + * Mime parts, etc. + * + * @return mixed Returns true on success, or a PEAR_Error + * containing a descriptive error message on + * failure. + * @access public + * @deprecated use Mail_mail::send instead + */ + function send($recipients, $headers, $body) + { + $this->_sanitizeHeaders($headers); + + // if we're passed an array of recipients, implode it. + if (is_array($recipients)) { + $recipients = implode(', ', $recipients); + } + + // get the Subject out of the headers array so that we can + // pass it as a seperate argument to mail(). + $subject = ''; + if (isset($headers['Subject'])) { + $subject = $headers['Subject']; + unset($headers['Subject']); + } + + // flatten the headers out. + list(,$text_headers) = Mail::prepareHeaders($headers); + + return mail($recipients, $subject, $body, $text_headers); + + } + + /** + * Sanitize an array of mail headers by removing any additional header + * strings present in a legitimate header's value. The goal of this + * filter is to prevent mail injection attacks. + * + * @param array $headers The associative array of headers to sanitize. + * + * @access private + */ + function _sanitizeHeaders(&$headers) + { + foreach ($headers as $key => $value) { + $headers[$key] = + preg_replace('=((<CR>|<LF>|0x0A/%0A|0x0D/%0D|\\n|\\r)\S).*=i', + null, $value); + } + } + + /** + * Take an array of mail headers and return a string containing + * text usable in sending a message. + * + * @param array $headers The array of headers to prepare, in an associative + * array, where the array key is the header name (ie, + * 'Subject'), and the array value is the header + * value (ie, 'test'). The header produced from those + * values would be 'Subject: test'. + * + * @return mixed Returns false if it encounters a bad address, + * otherwise returns an array containing two + * elements: Any From: address found in the headers, + * and the plain text version of the headers. + * @access private + */ + function prepareHeaders($headers) + { + $lines = array(); + $from = null; + + foreach ($headers as $key => $value) { + if (strcasecmp($key, 'From') === 0) { + include_once 'Mail/RFC822.php'; + $parser = &new Mail_RFC822(); + $addresses = $parser->parseAddressList($value, 'localhost', false); + if (PEAR::isError($addresses)) { + return $addresses; + } + + $from = $addresses[0]->mailbox . '@' . $addresses[0]->host; + + // Reject envelope From: addresses with spaces. + if (strstr($from, ' ')) { + return false; + } + + $lines[] = $key . ': ' . $value; + } elseif (strcasecmp($key, 'Received') === 0) { + $received = array(); + if (is_array($value)) { + foreach ($value as $line) { + $received[] = $key . ': ' . $line; + } + } + else { + $received[] = $key . ': ' . $value; + } + // Put Received: headers at the top. Spam detectors often + // flag messages with Received: headers after the Subject: + // as spam. + $lines = array_merge($received, $lines); + } else { + // If $value is an array (i.e., a list of addresses), convert + // it to a comma-delimited string of its elements (addresses). + if (is_array($value)) { + $value = implode(', ', $value); + } + $lines[] = $key . ': ' . $value; + } + } + + return array($from, join($this->sep, $lines)); + } + + /** + * Take a set of recipients and parse them, returning an array of + * bare addresses (forward paths) that can be passed to sendmail + * or an smtp server with the rcpt to: command. + * + * @param mixed Either a comma-seperated list of recipients + * (RFC822 compliant), or an array of recipients, + * each RFC822 valid. + * + * @return mixed An array of forward paths (bare addresses) or a PEAR_Error + * object if the address list could not be parsed. + * @access private + */ + function parseRecipients($recipients) + { + include_once 'Mail/RFC822.php'; + + // if we're passed an array, assume addresses are valid and + // implode them before parsing. + if (is_array($recipients)) { + $recipients = implode(', ', $recipients); + } + + // Parse recipients, leaving out all personal info. This is + // for smtp recipients, etc. All relevant personal information + // should already be in the headers. + $addresses = Mail_RFC822::parseAddressList($recipients, 'localhost', false); + + // If parseAddressList() returned a PEAR_Error object, just return it. + if (PEAR::isError($addresses)) { + return $addresses; + } + + $recipients = array(); + if (is_array($addresses)) { + foreach ($addresses as $ob) { + $recipients[] = $ob->mailbox . '@' . $ob->host; + } + } + + return $recipients; + } + +} diff --git a/extlib/Validate.php b/extlib/Validate.php new file mode 100644 index 000000000..4c05506b3 --- /dev/null +++ b/extlib/Validate.php @@ -0,0 +1,1051 @@ +<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4: */
+// +----------------------------------------------------------------------+
+// | Copyright (c) 1997-2006 Pierre-Alain Joye,Tomas V.V.Cox, Amir Saied |
+// +----------------------------------------------------------------------+
+// | This source file is subject to the New BSD license, That is bundled |
+// | with this package in the file LICENSE, and is available through |
+// | the world-wide-web at |
+// | http://www.opensource.org/licenses/bsd-license.php |
+// | If you did not receive a copy of the new BSDlicense and are unable |
+// | to obtain it through the world-wide-web, please send a note to |
+// | pajoye@php.net so we can mail you a copy immediately. |
+// +----------------------------------------------------------------------+
+// | Author: Tomas V.V.Cox <cox@idecnet.com> |
+// | Pierre-Alain Joye <pajoye@php.net> |
+// | Amir Mohammad Saied <amir@php.net> |
+// +----------------------------------------------------------------------+
+//
+/**
+ * Validation class
+ *
+ * Package to validate various datas. It includes :
+ * - numbers (min/max, decimal or not)
+ * - email (syntax, domain check)
+ * - string (predifined type alpha upper and/or lowercase, numeric,...)
+ * - date (min, max, rfc822 compliant)
+ * - uri (RFC2396)
+ * - possibility valid multiple data with a single method call (::multiple)
+ *
+ * @category Validate
+ * @package Validate
+ * @author Tomas V.V.Cox <cox@idecnet.com>
+ * @author Pierre-Alain Joye <pajoye@php.net>
+ * @author Amir Mohammad Saied <amir@php.net>
+ * @copyright 1997-2006 Pierre-Alain Joye,Tomas V.V.Cox,Amir Mohammad Saied
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @version CVS: $Id: Validate.php,v 1.123 2007/12/12 16:45:51 davidc Exp $
+ * @link http://pear.php.net/package/Validate
+ */
+
+/**
+ * Methods for common data validations
+ */
+define('VALIDATE_NUM', '0-9');
+define('VALIDATE_SPACE', '\s');
+define('VALIDATE_ALPHA_LOWER', 'a-z');
+define('VALIDATE_ALPHA_UPPER', 'A-Z');
+define('VALIDATE_ALPHA', VALIDATE_ALPHA_LOWER . VALIDATE_ALPHA_UPPER);
+define('VALIDATE_EALPHA_LOWER', VALIDATE_ALPHA_LOWER . 'áéíóúýàèìòùäëïöüÿâêîôûãñõ¨åæç½ðøþß');
+define('VALIDATE_EALPHA_UPPER', VALIDATE_ALPHA_UPPER . 'ÁÉÍÓÚÝÀÈÌÒÙÄËÏÖܾÂÊÎÔÛÃÑÕ¦ÅÆǼÐØÞ');
+define('VALIDATE_EALPHA', VALIDATE_EALPHA_LOWER . VALIDATE_EALPHA_UPPER);
+define('VALIDATE_PUNCTUATION', VALIDATE_SPACE . '\.,;\:&"\'\?\!\(\)');
+define('VALIDATE_NAME', VALIDATE_EALPHA . VALIDATE_SPACE . "'" . "-");
+define('VALIDATE_STREET', VALIDATE_NUM . VALIDATE_NAME . "/\\ºª\.");
+
+define('VALIDATE_ITLD_EMAILS', 1);
+define('VALIDATE_GTLD_EMAILS', 2);
+define('VALIDATE_CCTLD_EMAILS', 4);
+define('VALIDATE_ALL_EMAILS', 8);
+
+/**
+ * Validation class
+ *
+ * Package to validate various datas. It includes :
+ * - numbers (min/max, decimal or not)
+ * - email (syntax, domain check)
+ * - string (predifined type alpha upper and/or lowercase, numeric,...)
+ * - date (min, max)
+ * - uri (RFC2396)
+ * - possibility valid multiple data with a single method call (::multiple)
+ *
+ * @category Validate
+ * @package Validate
+ * @author Tomas V.V.Cox <cox@idecnet.com>
+ * @author Pierre-Alain Joye <pajoye@php.net>
+ * @author Amir Mohammad Saied <amir@php.net>
+ * @copyright 1997-2006 Pierre-Alain Joye,Tomas V.V.Cox,Amir Mohammad Saied
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @version Release: @package_version@
+ * @link http://pear.php.net/package/Validate
+ */
+class Validate
+{
+ /**
+ * International Top-Level Domain
+ *
+ * This is an array of the known international
+ * top-level domain names.
+ *
+ * @access protected
+ * @var array $_iTld (International top-level domains)
+ */
+ var $_itld = array(
+ 'arpa',
+ 'root',
+ );
+
+ /**
+ * Generic top-level domain
+ *
+ * This is an array of the official
+ * generic top-level domains.
+ *
+ * @access protected
+ * @var array $_gTld (Generic top-level domains)
+ */
+ var $_gtld = array(
+ 'aero',
+ 'biz',
+ 'cat',
+ 'com',
+ 'coop',
+ 'edu',
+ 'gov',
+ 'info',
+ 'int',
+ 'jobs',
+ 'mil',
+ 'mobi',
+ 'museum',
+ 'name',
+ 'net',
+ 'org',
+ 'pro',
+ 'travel',
+ 'asia',
+ 'post',
+ 'tel',
+ 'geo',
+ );
+
+ /**
+ * Country code top-level domains
+ *
+ * This is an array of the official country
+ * codes top-level domains
+ *
+ * @access protected
+ * @var array $_ccTld (Country Code Top-Level Domain)
+ */
+ var $_cctld = array(
+ 'ac',
+ 'ad','ae','af','ag',
+ 'ai','al','am','an',
+ 'ao','aq','ar','as',
+ 'at','au','aw','ax',
+ 'az','ba','bb','bd',
+ 'be','bf','bg','bh',
+ 'bi','bj','bm','bn',
+ 'bo','br','bs','bt',
+ 'bu','bv','bw','by',
+ 'bz','ca','cc','cd',
+ 'cf','cg','ch','ci',
+ 'ck','cl','cm','cn',
+ 'co','cr','cs','cu',
+ 'cv','cx','cy','cz',
+ 'de','dj','dk','dm',
+ 'do','dz','ec','ee',
+ 'eg','eh','er','es',
+ 'et','eu','fi','fj',
+ 'fk','fm','fo','fr',
+ 'ga','gb','gd','ge',
+ 'gf','gg','gh','gi',
+ 'gl','gm','gn','gp',
+ 'gq','gr','gs','gt',
+ 'gu','gw','gy','hk',
+ 'hm','hn','hr','ht',
+ 'hu','id','ie','il',
+ 'im','in','io','iq',
+ 'ir','is','it','je',
+ 'jm','jo','jp','ke',
+ 'kg','kh','ki','km',
+ 'kn','kp','kr','kw',
+ 'ky','kz','la','lb',
+ 'lc','li','lk','lr',
+ 'ls','lt','lu','lv',
+ 'ly','ma','mc','md',
+ 'me','mg','mh','mk',
+ 'ml','mm','mn','mo',
+ 'mp','mq','mr','ms',
+ 'mt','mu','mv','mw',
+ 'mx','my','mz','na',
+ 'nc','ne','nf','ng',
+ 'ni','nl','no','np',
+ 'nr','nu','nz','om',
+ 'pa','pe','pf','pg',
+ 'ph','pk','pl','pm',
+ 'pn','pr','ps','pt',
+ 'pw','py','qa','re',
+ 'ro','rs','ru','rw',
+ 'sa','sb','sc','sd',
+ 'se','sg','sh','si',
+ 'sj','sk','sl','sm',
+ 'sn','so','sr','st',
+ 'su','sv','sy','sz',
+ 'tc','td','tf','tg',
+ 'th','tj','tk','tl',
+ 'tm','tn','to','tp',
+ 'tr','tt','tv','tw',
+ 'tz','ua','ug','uk',
+ 'us','uy','uz','va',
+ 'vc','ve','vg','vi',
+ 'vn','vu','wf','ws',
+ 'ye','yt','yu','za',
+ 'zm','zw',
+ );
+
+
+ /**
+ * Validate a number
+ *
+ * @param string $number Number to validate
+ * @param array $options array where:
+ * 'decimal' is the decimal char or false when decimal not allowed
+ * i.e. ',.' to allow both ',' and '.'
+ * 'dec_prec' Number of allowed decimals
+ * 'min' minimum value
+ * 'max' maximum value
+ *
+ * @return boolean true if valid number, false if not
+ *
+ * @access public
+ */
+ function number($number, $options = array())
+ {
+ $decimal = $dec_prec = $min = $max = null;
+ if (is_array($options)) {
+ extract($options);
+ }
+
+ $dec_prec = $dec_prec ? "{1,$dec_prec}" : '+';
+ $dec_regex = $decimal ? "[$decimal][0-9]$dec_prec" : '';
+
+ if (!preg_match("|^[-+]?\s*[0-9]+($dec_regex)?\$|", $number)) {
+ return false;
+ }
+
+ if ($decimal != '.') {
+ $number = strtr($number, $decimal, '.');
+ }
+
+ $number = (float)str_replace(' ', '', $number);
+ if ($min !== null && $min > $number) {
+ return false;
+ }
+
+ if ($max !== null && $max < $number) {
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Converting a string to UTF-7 (RFC 2152)
+ *
+ * @param $string string to be converted
+ *
+ * @return string converted string
+ *
+ * @access private
+ */
+ function __stringToUtf7($string) {
+ $return = '';
+ $utf7 = array(
+ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
+ 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
+ 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
+ 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
+ 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2',
+ '3', '4', '5', '6', '7', '8', '9', '+', ','
+ );
+
+ $state = 0;
+ if (!empty($string)) {
+ $i = 0;
+ while ($i <= strlen($string)) {
+ $char = substr($string, $i, 1);
+ if ($state == 0) {
+ if ((ord($char) >= 0x7F) || (ord($char) <= 0x1F)) {
+ if ($char) {
+ $return .= '&';
+ }
+ $state = 1;
+ } elseif ($char == '&') {
+ $return .= '&-';
+ } else {
+ $return .= $char;
+ }
+ } elseif (($i == strlen($string) ||
+ !((ord($char) >= 0x7F)) || (ord($char) <= 0x1F))) {
+ if ($state != 1) {
+ if (ord($char) > 64) {
+ $return .= '';
+ } else {
+ $return .= $utf7[ord($char)];
+ }
+ }
+ $return .= '-';
+ $state = 0;
+ } else {
+ switch($state) {
+ case 1:
+ $return .= $utf7[ord($char) >> 2];
+ $residue = (ord($char) & 0x03) << 4;
+ $state = 2;
+ break;
+ case 2:
+ $return .= $utf7[$residue | (ord($char) >> 4)];
+ $residue = (ord($char) & 0x0F) << 2;
+ $state = 3;
+ break;
+ case 3:
+ $return .= $utf7[$residue | (ord($char) >> 6)];
+ $return .= $utf7[ord($char) & 0x3F];
+ $state = 1;
+ break;
+ }
+ }
+ $i++;
+ }
+ return $return;
+ }
+ return '';
+ }
+
+ /**
+ * Validate an email according to full RFC822 (inclusive human readable part)
+ *
+ * @param string $email email to validate,
+ * will return the address for optional dns validation
+ * @param array $options email() options
+ *
+ * @return boolean true if valid email, false if not
+ *
+ * @access private
+ */
+ function __emailRFC822(&$email, &$options)
+ {
+ if (Validate::__stringToUtf7($email) != $email) {
+ return false;
+ }
+ static $address = null;
+ static $uncomment = null;
+ if (!$address) {
+ // atom = 1*<any CHAR except specials, SPACE and CTLs>
+ $atom = '[^][()<>@,;:\\".\s\000-\037\177-\377]+\s*';
+ // qtext = <any CHAR excepting <">, ; => may be folded
+ // "\" & CR, and including linear-white-space>
+ $qtext = '[^"\\\\\r]';
+ // quoted-pair = "\" CHAR ; may quote any char
+ $quoted_pair = '\\\\.';
+ // quoted-string = <"> *(qtext/quoted-pair) <">; Regular qtext or
+ // ; quoted chars.
+ $quoted_string = '"(?:' . $qtext . '|' . $quoted_pair . ')*"\s*';
+ // word = atom / quoted-string
+ $word = '(?:' . $atom . '|' . $quoted_string . ')';
+ // local-part = word *("." word) ; uninterpreted
+ // ; case-preserved
+ $local_part = $word . '(?:\.\s*' . $word . ')*';
+ // dtext = <any CHAR excluding "[", ; => may be folded
+ // "]", "\" & CR, & including linear-white-space>
+ $dtext = '[^][\\\\\r]';
+ // domain-literal = "[" *(dtext / quoted-pair) "]"
+ $domain_literal = '\[(?:' . $dtext . '|' . $quoted_pair . ')*\]\s*';
+ // sub-domain = domain-ref / domain-literal
+ // domain-ref = atom ; symbolic reference
+ $sub_domain = '(?:' . $atom . '|' . $domain_literal . ')';
+ // domain = sub-domain *("." sub-domain)
+ $domain = $sub_domain . '(?:\.\s*' . $sub_domain . ')*';
+ // addr-spec = local-part "@" domain ; global address
+ $addr_spec = $local_part . '@\s*' . $domain;
+ // route = 1#("@" domain) ":" ; path-relative
+ $route = '@' . $domain . '(?:,@\s*' . $domain . ')*:\s*';
+ // route-addr = "<" [route] addr-spec ">"
+ $route_addr = '<\s*(?:' . $route . ')?' . $addr_spec . '>\s*';
+ // phrase = 1*word ; Sequence of words
+ $phrase = $word . '+';
+ // mailbox = addr-spec ; simple address
+ // / phrase route-addr ; name & addr-spec
+ $mailbox = '(?:' . $addr_spec . '|' . $phrase . $route_addr . ')';
+ // group = phrase ":" [#mailbox] ";"
+ $group = $phrase . ':\s*(?:' . $mailbox . '(?:,\s*' . $mailbox . ')*)?;\s*';
+ // address = mailbox ; one addressee
+ // / group ; named list
+ $address = '/^\s*(?:' . $mailbox . '|' . $group . ')$/';
+ $uncomment =
+ '/((?:(?:\\\\"|[^("])*(?:' . $quoted_string .
+ ')?)*)((?<!\\\\)\((?:(?2)|.)*?(?<!\\\\)\))/';
+ }
+ // strip comments
+ $email = preg_replace($uncomment, '$1 ', $email);
+ return preg_match($address, $email);
+ }
+
+ /**
+ * Full TLD Validation function
+ *
+ * This function is used to make a much more proficient validation
+ * against all types of official domain names.
+ *
+ * @access protected
+ * @param string $email The email address to check.
+ * @param array $options The options for validation
+ * @return bool True if validating succeeds
+ */
+ function _fullTLDValidation($email, $options)
+ {
+ $validate = array();
+
+ switch ($options) {
+ /** 1 */
+ case VALIDATE_ITLD_EMAILS:
+ array_push($validate, 'itld');
+ break;
+
+ /** 2 */
+ case VALIDATE_GTLD_EMAILS:
+ array_push($validate, 'gtld');
+ break;
+
+ /** 3 */
+ case VALIDATE_ITLD_EMAILS | VALIDATE_GTLD_EMAILS:
+ array_push($validate, 'itld');
+ array_push($validate, 'gtld');
+ break;
+
+ /** 4 */
+ case VALIDATE_CCTLD_EMAILS:
+ array_push($validate, 'cctld');
+ break;
+
+ /** 5 */
+ case VALIDATE_CCTLD_EMAILS | VALIDATE_ITLD_EMAILS:
+ array_push($validate, 'cctld');
+ array_push($validate, 'itld');
+ break;
+
+ /** 6 */
+ case VALIDATE_CCTLD_EMAILS ^ VALIDATE_ITLD_EMAILS:
+ array_push($validate, 'cctld');
+ array_push($validate, 'itld');
+ break;
+
+ /** 7 - 8 */
+ case VALIDATE_CCTLD_EMAILS | VALIDATE_ITLD_EMAILS | VALIDATE_GTLD_EMAILS:
+ case VALIDATE_ALL_EMAILS:
+ array_push($validate, 'cctld');
+ array_push($validate, 'itld');
+ array_push($validate, 'gtld');
+ break;
+ }
+
+ /**
+ * Debugging still, not implemented but code is somewhat here.
+ */
+
+ $self = new Validate;
+
+ $toValidate = array();
+
+ foreach ($validate as $valid) {
+ $tmpVar = '_' . (string)$valid;
+ $toValidate[$valid] = $self->{$tmpVar};
+ }
+
+ $e = $self->executeFullEmailValidation($email, $toValidate);
+
+ return $e;
+ }
+ // {{{ protected function executeFullEmailValidation
+ /**
+ * Execute the validation
+ *
+ * This function will execute the full email vs tld
+ * validation using an array of tlds passed to it.
+ *
+ * @access public
+ * @param string $email The email to validate.
+ * @param array $arrayOfTLDs The array of the TLDs to validate
+ * @return true or false (Depending on if it validates or if it does not)
+ */
+ function executeFullEmailValidation($email, $arrayOfTLDs)
+ {
+ $emailEnding = explode('.', $email);
+ $emailEnding = $emailEnding[count($emailEnding)-1];
+
+ foreach ($arrayOfTLDs as $validator => $keys) {
+ if (in_array($emailEnding, $keys)) {
+ return true;
+ }
+ }
+ return false;
+ }
+ // }}}
+
+ /**
+ * Validate an email
+ *
+ * @param string $email email to validate
+ * @param mixed boolean (BC) $check_domain Check or not if the domain exists
+ * array $options associative array of options
+ * 'check_domain' boolean Check or not if the domain exists
+ * 'use_rfc822' boolean Apply the full RFC822 grammar
+ *
+ * @return boolean true if valid email, false if not
+ *
+ * @access public
+ */
+ function email($email, $options = null)
+ {
+ $check_domain = false;
+ $use_rfc822 = false;
+ if (is_bool($options)) {
+ $check_domain = $options;
+ } elseif (is_array($options)) {
+ extract($options);
+ }
+
+ /**
+ * @todo Fix bug here.. even if it passes this, it won't be passing
+ * The regular expression below
+ */
+ if (isset($fullTLDValidation)) {
+ $valid = Validate::_fullTLDValidation($email, $fullTLDValidation);
+
+ if (!$valid) {
+ return false;
+ }
+ }
+
+ // the base regexp for address
+ $regex = '&^(?: # recipient:
+ ("\s*(?:[^"\f\n\r\t\v\b\s]+\s*)+")| #1 quoted name
+ ([-\w!\#\$%\&\'*+~/^`|{}]+(?:\.[-\w!\#\$%\&\'*+~/^`|{}]+)*)) #2 OR dot-atom
+ @(((\[)? #3 domain, 4 as IPv4, 5 optionally bracketed
+ (?:(?:(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:[0-1]?[0-9]?[0-9]))\.){3}
+ (?:(?:25[0-5])|(?:2[0-4][0-9])|(?:[0-1]?[0-9]?[0-9]))))(?(5)\])|
+ ((?:[a-z0-9](?:[-a-z0-9]*[a-z0-9])?\.)*[a-z0-9](?:[-a-z0-9]*[a-z0-9])?) #6 domain as hostname
+ \.((?:([^- ])[-a-z]*[-a-z]))) #7 TLD
+ $&xi';
+
+ if ($use_rfc822? Validate::__emailRFC822($email, $options) :
+ preg_match($regex, $email)) {
+ if ($check_domain && function_exists('checkdnsrr')) {
+ list (, $domain) = explode('@', $email);
+ if (checkdnsrr($domain, 'MX') || checkdnsrr($domain, 'A')) {
+ return true;
+ }
+ return false;
+ }
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Validate a string using the given format 'format'
+ *
+ * @param string $string String to validate
+ * @param array $options Options array where:
+ * 'format' is the format of the string
+ * Ex: VALIDATE_NUM . VALIDATE_ALPHA (see constants)
+ * 'min_length' minimum length
+ * 'max_length' maximum length
+ *
+ * @return boolean true if valid string, false if not
+ *
+ * @access public
+ */
+ function string($string, $options)
+ {
+ $format = null;
+ $min_length = $max_length = 0;
+ if (is_array($options)) {
+ extract($options);
+ }
+ if ($format && !preg_match("|^[$format]*\$|s", $string)) {
+ return false;
+ }
+ if ($min_length && strlen($string) < $min_length) {
+ return false;
+ }
+ if ($max_length && strlen($string) > $max_length) {
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Validate an URI (RFC2396)
+ * This function will validate 'foobarstring' by default, to get it to validate
+ * only http, https, ftp and such you have to pass it in the allowed_schemes
+ * option, like this:
+ * <code>
+ * $options = array('allowed_schemes' => array('http', 'https', 'ftp'))
+ * var_dump(Validate::uri('http://www.example.org', $options));
+ * </code>
+ *
+ * NOTE 1: The rfc2396 normally allows middle '-' in the top domain
+ * e.g. http://example.co-m should be valid
+ * However, as '-' is not used in any known TLD, it is invalid
+ * NOTE 2: As double shlashes // are allowed in the path part, only full URIs
+ * including an authority can be valid, no relative URIs
+ * the // are mandatory (optionally preceeded by the 'sheme:' )
+ * NOTE 3: the full complience to rfc2396 is not achieved by default
+ * the characters ';/?:@$,' will not be accepted in the query part
+ * if not urlencoded, refer to the option "strict'"
+ *
+ * @param string $url URI to validate
+ * @param array $options Options used by the validation method.
+ * key => type
+ * 'domain_check' => boolean
+ * Whether to check the DNS entry or not
+ * 'allowed_schemes' => array, list of protocols
+ * List of allowed schemes ('http',
+ * 'ssh+svn', 'mms')
+ * 'strict' => string the refused chars
+ * in query and fragment parts
+ * default: ';/?:@$,'
+ * empty: accept all rfc2396 foreseen chars
+ *
+ * @return boolean true if valid uri, false if not
+ *
+ * @access public
+ */
+ function uri($url, $options = null)
+ {
+ $strict = ';/?:@$,';
+ $domain_check = false;
+ $allowed_schemes = null;
+ if (is_array($options)) {
+ extract($options);
+ }
+ if (preg_match(
+ '&^(?:([a-z][-+.a-z0-9]*):)? # 1. scheme
+ (?:// # authority start
+ (?:((?:%[0-9a-f]{2}|[-a-z0-9_.!~*\'();:\&=+$,])*)@)? # 2. authority-userinfo
+ (?:((?:[a-z0-9](?:[-a-z0-9]*[a-z0-9])?\.)*[a-z](?:[a-z0-9]+)?\.?) # 3. authority-hostname OR
+ |([0-9]{1,3}(?:\.[0-9]{1,3}){3})) # 4. authority-ipv4
+ (?::([0-9]*))?) # 5. authority-port
+ ((?:/(?:%[0-9a-f]{2}|[-a-z0-9_.!~*\'():@\&=+$,;])*)*/?)? # 6. path
+ (?:\?([^#]*))? # 7. query
+ (?:\#((?:%[0-9a-f]{2}|[-a-z0-9_.!~*\'();/?:@\&=+$,])*))? # 8. fragment
+ $&xi', $url, $matches)) {
+ $scheme = isset($matches[1]) ? $matches[1] : '';
+ $authority = isset($matches[3]) ? $matches[3] : '' ;
+ if (is_array($allowed_schemes) &&
+ !in_array($scheme,$allowed_schemes)
+ ) {
+ return false;
+ }
+ if (!empty($matches[4])) {
+ $parts = explode('.', $matches[4]);
+ foreach ($parts as $part) {
+ if ($part > 255) {
+ return false;
+ }
+ }
+ } elseif ($domain_check && function_exists('checkdnsrr')) {
+ if (!checkdnsrr($authority, 'A')) {
+ return false;
+ }
+ }
+ if ($strict) {
+ $strict = '#[' . preg_quote($strict, '#') . ']#';
+ if ((!empty($matches[7]) && preg_match($strict, $matches[7]))
+ || (!empty($matches[8]) && preg_match($strict, $matches[8]))) {
+ return false;
+ }
+ }
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Validate date and times. Note that this method need the Date_Calc class
+ *
+ * @param string $date Date to validate
+ * @param array $options array options where :
+ * 'format' The format of the date (%d-%m-%Y)
+ * or rfc822_compliant
+ * 'min' The date has to be greater
+ * than this array($day, $month, $year)
+ * or PEAR::Date object
+ * 'max' The date has to be smaller than
+ * this array($day, $month, $year)
+ * or PEAR::Date object
+ *
+ * @return boolean true if valid date/time, false if not
+ *
+ * @access public
+ */
+ function date($date, $options)
+ {
+ $max = $min = false;
+ $format = '';
+ if (is_array($options)) {
+ extract($options);
+ }
+
+ if (strtolower($format) == 'rfc822_compliant') {
+ $preg = '&^(?:(Mon|Tue|Wed|Thu|Fri|Sat|Sun),) \s+
+ (?:(\d{2})?) \s+
+ (?:(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)?) \s+
+ (?:(\d{2}(\d{2})?)?) \s+
+ (?:(\d{2}?)):(?:(\d{2}?))(:(?:(\d{2}?)))? \s+
+ (?:[+-]\d{4}|UT|GMT|EST|EDT|CST|CDT|MST|MDT|PST|PDT|[A-IK-Za-ik-z])$&xi';
+
+ if (!preg_match($preg, $date, $matches)) {
+ return false;
+ }
+
+ $year = (int)$matches[4];
+ $months = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
+ 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
+ $month = array_keys($months, $matches[3]);
+ $month = (int)$month[0]+1;
+ $day = (int)$matches[2];
+ $weekday= $matches[1];
+ $hour = (int)$matches[6];
+ $minute = (int)$matches[7];
+ isset($matches[9]) ? $second = (int)$matches[9] : $second = 0;
+
+ if ((strlen($year) != 4) ||
+ ($day > 31 || $day < 1)||
+ ($hour > 23) ||
+ ($minute > 59) ||
+ ($second > 59)) {
+ return false;
+ }
+ } else {
+ $date_len = strlen($format);
+ for ($i = 0; $i < $date_len; $i++) {
+ $c = $format{$i};
+ if ($c == '%') {
+ $next = $format{$i + 1};
+ switch ($next) {
+ case 'j':
+ case 'd':
+ if ($next == 'j') {
+ $day = (int)Validate::_substr($date, 1, 2);
+ } else {
+ $day = (int)Validate::_substr($date, 0, 2);
+ }
+ if ($day < 1 || $day > 31) {
+ return false;
+ }
+ break;
+ case 'm':
+ case 'n':
+ if ($next == 'm') {
+ $month = (int)Validate::_substr($date, 0, 2);
+ } else {
+ $month = (int)Validate::_substr($date, 1, 2);
+ }
+ if ($month < 1 || $month > 12) {
+ return false;
+ }
+ break;
+ case 'Y':
+ case 'y':
+ if ($next == 'Y') {
+ $year = Validate::_substr($date, 4);
+ $year = (int)$year?$year:'';
+ } else {
+ $year = (int)(substr(date('Y'), 0, 2) .
+ Validate::_substr($date, 2));
+ }
+ if (strlen($year) != 4 || $year < 0 || $year > 9999) {
+ return false;
+ }
+ break;
+ case 'g':
+ case 'h':
+ if ($next == 'g') {
+ $hour = Validate::_substr($date, 1, 2);
+ } else {
+ $hour = Validate::_substr($date, 2);
+ }
+ if (!preg_match('/^\d+$/', $hour) || $hour < 0 || $hour > 12) {
+ return false;
+ }
+ break;
+ case 'G':
+ case 'H':
+ if ($next == 'G') {
+ $hour = Validate::_substr($date, 1, 2);
+ } else {
+ $hour = Validate::_substr($date, 2);
+ }
+ if (!preg_match('/^\d+$/', $hour) || $hour < 0 || $hour > 24) {
+ return false;
+ }
+ break;
+ case 's':
+ case 'i':
+ $t = Validate::_substr($date, 2);
+ if (!preg_match('/^\d+$/', $t) || $t < 0 || $t > 59) {
+ return false;
+ }
+ break;
+ default:
+ trigger_error("Not supported char `$next' after % in offset " . ($i+2), E_USER_WARNING);
+ }
+ $i++;
+ } else {
+ //literal
+ if (Validate::_substr($date, 1) != $c) {
+ return false;
+ }
+ }
+ }
+ }
+ // there is remaing data, we don't want it
+ if (strlen($date) && (strtolower($format) != 'rfc822_compliant')) {
+ return false;
+ }
+
+ if (isset($day) && isset($month) && isset($year)) {
+ if (!checkdate($month, $day, $year)) {
+ return false;
+ }
+
+ if (strtolower($format) == 'rfc822_compliant') {
+ if ($weekday != date("D", mktime(0, 0, 0, $month, $day, $year))) {
+ return false;
+ }
+ }
+
+ if ($min) {
+ include_once 'Date/Calc.php';
+ if (is_a($min, 'Date') &&
+ (Date_Calc::compareDates($day, $month, $year,
+ $min->getDay(), $min->getMonth(), $min->getYear()) < 0))
+ {
+ return false;
+ } elseif (is_array($min) &&
+ (Date_Calc::compareDates($day, $month, $year,
+ $min[0], $min[1], $min[2]) < 0))
+ {
+ return false;
+ }
+ }
+
+ if ($max) {
+ include_once 'Date/Calc.php';
+ if (is_a($max, 'Date') &&
+ (Date_Calc::compareDates($day, $month, $year,
+ $max->getDay(), $max->getMonth(), $max->getYear()) > 0))
+ {
+ return false;
+ } elseif (is_array($max) &&
+ (Date_Calc::compareDates($day, $month, $year,
+ $max[0], $max[1], $max[2]) > 0))
+ {
+ return false;
+ }
+ }
+ }
+
+ return true;
+ }
+
+ function _substr(&$date, $num, $opt = false)
+ {
+ if ($opt && strlen($date) >= $opt && preg_match('/^[0-9]{'.$opt.'}/', $date, $m)) {
+ $ret = $m[0];
+ } else {
+ $ret = substr($date, 0, $num);
+ }
+ $date = substr($date, strlen($ret));
+ return $ret;
+ }
+
+ function _modf($val, $div) {
+ if (function_exists('bcmod')) {
+ return bcmod($val, $div);
+ } elseif (function_exists('fmod')) {
+ return fmod($val, $div);
+ }
+ $r = $val / $div;
+ $i = intval($r);
+ return intval($val - $i * $div + .1);
+ }
+
+ /**
+ * Calculates sum of product of number digits with weights
+ *
+ * @param string $number number string
+ * @param array $weights reference to array of weights
+ *
+ * @returns int returns product of number digits with weights
+ *
+ * @access protected
+ */
+ function _multWeights($number, &$weights) {
+ if (!is_array($weights)) {
+ return -1;
+ }
+ $sum = 0;
+
+ $count = min(count($weights), strlen($number));
+ if ($count == 0) { // empty string or weights array
+ return -1;
+ }
+ for ($i = 0; $i < $count; ++$i) {
+ $sum += intval(substr($number, $i, 1)) * $weights[$i];
+ }
+
+ return $sum;
+ }
+
+ /**
+ * Calculates control digit for a given number
+ *
+ * @param string $number number string
+ * @param array $weights reference to array of weights
+ * @param int $modulo (optionsl) number
+ * @param int $subtract (optional) number
+ * @param bool $allow_high (optional) true if function can return number higher than 10
+ *
+ * @returns int -1 calculated control number is returned
+ *
+ * @access protected
+ */
+ function _getControlNumber($number, &$weights, $modulo = 10, $subtract = 0, $allow_high = false) {
+ // calc sum
+ $sum = Validate::_multWeights($number, $weights);
+ if ($sum == -1) {
+ return -1;
+ }
+ $mod = Validate::_modf($sum, $modulo); // calculate control digit
+
+ if ($subtract > $mod && $mod > 0) {
+ $mod = $subtract - $mod;
+ }
+ if ($allow_high === false) {
+ $mod %= 10; // change 10 to zero
+ }
+ return $mod;
+ }
+
+ /**
+ * Validates a number
+ *
+ * @param string $number number to validate
+ * @param array $weights reference to array of weights
+ * @param int $modulo (optionsl) number
+ * @param int $subtract (optional) numbier
+ *
+ * @returns bool true if valid, false if not
+ *
+ * @access protected
+ */
+ function _checkControlNumber($number, &$weights, $modulo = 10, $subtract = 0) {
+ if (strlen($number) < count($weights)) {
+ return false;
+ }
+ $target_digit = substr($number, count($weights), 1);
+ $control_digit = Validate::_getControlNumber($number, $weights, $modulo, $subtract, $modulo > 10);
+
+ if ($control_digit == -1) {
+ return false;
+ }
+ if ($target_digit === 'X' && $control_digit == 10) {
+ return true;
+ }
+ if ($control_digit != $target_digit) {
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Bulk data validation for data introduced in the form of an
+ * assoc array in the form $var_name => $value.
+ * Can be used on any of Validate subpackages
+ *
+ * @param array $data Ex: array('name' => 'toto', 'email' => 'toto@thing.info');
+ * @param array $val_type Contains the validation type and all parameters used in.
+ * 'val_type' is not optional
+ * others validations properties must have the same name as the function
+ * parameters.
+ * Ex: array('toto'=>array('type'=>'string','format'='toto@thing.info','min_length'=>5));
+ * @param boolean $remove if set, the elements not listed in data will be removed
+ *
+ * @return array value name => true|false the value name comes from the data key
+ *
+ * @access public
+ */
+ function multiple(&$data, &$val_type, $remove = false)
+ {
+ $keys = array_keys($data);
+ $valid = array();
+ foreach ($keys as $var_name) {
+ if (!isset($val_type[$var_name])) {
+ if ($remove) {
+ unset($data[$var_name]);
+ }
+ continue;
+ }
+ $opt = $val_type[$var_name];
+ $methods = get_class_methods('Validate');
+ $val2check = $data[$var_name];
+ // core validation method
+ if (in_array(strtolower($opt['type']), $methods)) {
+ //$opt[$opt['type']] = $data[$var_name];
+ $method = $opt['type'];
+ unset($opt['type']);
+
+ if (sizeof($opt) == 1 && is_array(reset($opt))) {
+ $opt = array_pop($opt);
+ }
+ $valid[$var_name] = call_user_func(array('Validate', $method), $val2check, $opt);
+
+ /**
+ * external validation method in the form:
+ * "<class name><underscore><method name>"
+ * Ex: us_ssn will include class Validate/US.php and call method ssn()
+ */
+ } elseif (strpos($opt['type'], '_') !== false) {
+ $validateType = explode('_', $opt['type']);
+ $method = array_pop($validateType);
+ $class = implode('_', $validateType);
+ $classPath = str_replace('_', DIRECTORY_SEPARATOR, $class);
+ $class = 'Validate_' . $class;
+ if (!@include_once "Validate/$classPath.php") {
+ trigger_error("$class isn't installed or you may have some permissoin issues", E_USER_ERROR);
+ }
+
+ $ce = substr(phpversion(), 0, 1) > 4 ? class_exists($class, false) : class_exists($class);
+ if (!$ce ||
+ !in_array($method, get_class_methods($class)))
+ {
+ trigger_error("Invalid validation type $class::$method", E_USER_WARNING);
+ continue;
+ }
+ unset($opt['type']);
+ if (sizeof($opt) == 1) {
+ $opt = array_pop($opt);
+ }
+ $valid[$var_name] = call_user_func(array($class, $method), $data[$var_name], $opt);
+ } else {
+ trigger_error("Invalid validation type {$opt['type']}", E_USER_WARNING);
+ }
+ }
+ return $valid;
+ }
+}
+
|