123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- <?php
- defined('GNUSOCIAL') || die();
- class Safe_DataObject extends GS_DataObject
- {
-
- public function __destruct()
- {
- $this->free();
- if (method_exists('DB_DataObject', '__destruct')) {
- parent::__destruct();
- }
- }
-
- public function __clone()
- {
- $this->_DB_resultid = false;
- }
-
- public function __sleep()
- {
- $vars = array_keys(get_object_vars($this));
- $skip = array('_DB_resultid', '_link_loaded');
- return array_diff($vars, $skip);
- }
-
- public function __wakeup()
- {
-
-
-
- $this->_DB_resultid = null;
-
- $this->_link_loaded = false;
- }
-
- public function __call($method, $params)
- {
- $return = null;
-
-
- if ($this->_call($method, $params, $return)) {
- return $return;
- } else {
-
- throw new Exception('Call to undefined method ' .
- get_class($this) . '::' . $method);
- }
- }
-
- public function databaseStructure()
- {
- global $_DB_DATAOBJECT;
- if (!empty($args = func_get_args())) {
- if (count($args) == 1) {
-
- 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();
- }
-
- if (!empty($_DB_DATAOBJECT['INI'][$this->_database])) {
-
- if (
- empty($_DB_DATAOBJECT['INI'][$this->_database][$this->tableName()])
- && !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->tableName());
- }
- return true;
- }
- if (empty($_DB_DATAOBJECT['CONFIG'])) {
- self::_loadConfig();
- }
-
-
- $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}"]);
- }
-
- $_DB_DATAOBJECT['INI'][$this->_database] = $this->parseIniFiles($schemas);
-
-
- if (!empty($_DB_DATAOBJECT['INI'][$this->_database][$this->tableName()])) {
- return true;
- }
-
- 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->tableName());
-
- return true;
- }
- $this->debug(
- "Can't find database schema: {$this->_database}/{$this->tableName()}\n"
- . 'in links file data: '
- . print_r($_DB_DATAOBJECT['INI'], true),
- 'databaseStructure',
- 5
- );
-
-
- $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;
- }
-
- protected static $iniCache = array();
-
- protected function parseIniFiles(array $schemas)
- {
- $key = implode("|", $schemas);
- if (!isset(Safe_DataObject::$iniCache[$key])) {
- $data = array();
- foreach ($schemas as $ini) {
- if (file_exists($ini) && is_file($ini)) {
- $data = array_merge($data, 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
- );
- }
- }
- }
- Safe_DataObject::$iniCache[$key] = $data;
- }
- return Safe_DataObject::$iniCache[$key];
- }
- }
|