public function __construct($database, $readonly = true, $useCache = true) { $this->useCache = $useCache; $file_exists = File::exists($database); if (!$file_exists) { $readonly = false; } if ($readonly) { $opt = 'rl'; if (!is_readable($database)) { throw new Exception('database is not readable: ' . $database); return false; } } else { $opt = 'cl'; if ($file_exists) { if (!is_writable($database)) { throw new Exception('database is not writeable: ' . $database); return false; } } else { if (!is_writable(dirname($database))) { throw new Exception('database is not inside a writeable directory: ' . $database); return false; } } } $this->dbHandler = dba_open($database, $opt, self::HANDLER); if (!$this->dbHandler) { throw new Exception('cannot open database: ' . $database); return false; } return $this; }
function Indexer_dba($arena, $mode = 'r', $type, $prefix = '') { global $DBInfo; $this->index_dir = $DBInfo->cache_dir . '/index'; if (!file_exists($this->index_dir)) { mkdir($this->index_dir, 0777); } $this->prefix = $prefix; if (!empty($prefix)) { $prefix = $prefix . '.'; } $this->dbname = $this->index_dir . '/' . $prefix . $arena . '.db'; $this->arena = $arena; // check updated db file. if (empty($prefix) and file_exists($this->index_dir . '/' . $arena . '.new.db')) { if (!file_exists($this->dbname) or filemtime($this->index_dir . '/' . $arena . '.new.db') > filemtime($this->dbname)) { @touch($this->dbname); $tmpname = '.tmp_' . time(); copy($this->index_dir . '/' . $arena . '.new.db', $this->dbname . $tmpname); rename($this->dbname . $tmpname, $this->dbname); } } if (($this->db = @dba_open($this->dbname, $mode, $type)) === false) { if (($this->db = @dba_open($this->dbname, 'n', $type)) === false) { return false; } // startkey==256 dba_insert("", 1, $this->db); dba_sync($this->db); } register_shutdown_function(array(&$this, 'close')); return true; }
function run() { global $wgExtensionMessagesFiles, $wgMessageCache, $IP; $nameHash = md5(implode("\n", array_keys($wgExtensionMessagesFiles))); $dir = "{$IP}/cache/ext-msgs"; wfMkdirParents($dir); $db = dba_open("{$dir}/{$nameHash}.cdb", 'n', 'cdb'); if (!$db) { echo "Cannot open DB file\n"; exit(1); } # Load extension messages foreach ($wgExtensionMessagesFiles as $file) { $messages = $magicWords = array(); require $file; foreach ($messages as $lang => $unused) { $wgMessageCache->processMessagesArray($messages, $lang); } } # Write them to the file foreach ($wgMessageCache->mExtensionMessages as $lang => $messages) { foreach ($messages as $key => $text) { dba_insert("{$lang}:{$key}", $text, $db); } } dba_close($db); }
/** * Constructs the backend. * * @access public * @param string $config */ function __construct($config, &$degenerator) { # Pass the degenerator instance to this class $this->degenerator = $degenerator; # Validate the config items foreach ($config as $name => $value) { switch ($name) { case 'database': case 'handler': $this->config[$name] = (string) $value; break; default: throw new Exception("b8_storage_dba: Unknown configuration key: \"{$name}\""); } } # Connect to the database $dbfile = dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . $this->config['database']; if (is_file($dbfile) !== TRUE) { throw new Exception("b8_storage_dba: Database file \"{$this->config['database']}\" not found."); } if (is_readable($dbfile) !== TRUE) { throw new Exception("b8_storage_dba: Database file \"{$this->config['database']}\" is not readable."); } if (is_writeable($dbfile) !== TRUE) { throw new Exception("b8_storage_dba: Database file \"{$this->config['database']}\" is not writeable."); } $this->_db = dba_open($dbfile, 'w', $this->config['handler']); if ($this->_db === FALSE) { $this->connected = FALSE; $this->_db = NULL; throw new Exception("b8_storage_dba: Could not connect to database file \"{$this->config['database']}\"."); } # Let's see if this is a b8 database and the version is okay $this->check_database(); }
function __construct() { $file = tempnam("/tmp", ""); unlink($file); $this->_dir = "{$file}.db"; $this->_db = dba_open($this->_dir, "c", "db4") or die("Unable to run reducer, please ensure you have dba + db4"); }
function getComments($req) { $err = ""; $ra = array(); $encoder = new PhpXmlRpc\Encoder(); $msgID = $encoder->decode($req->getParam(0)); $dbh = dba_open("/tmp/comments.db", "r", "db2"); if ($dbh) { $countID = "{$msgID}_count"; if (dba_exists($countID, $dbh)) { $count = dba_fetch($countID, $dbh); for ($i = 0; $i < $count; $i++) { $name = dba_fetch("{$msgID}_name_{$i}", $dbh); $comment = dba_fetch("{$msgID}_comment_{$i}", $dbh); // push a new struct onto the return array $ra[] = array("name" => $name, "comment" => $comment); } } } // if we generated an error, create an error return response if ($err) { return new PhpXmlRpc\Response(0, PhpXmlRpc\PhpXmlRpc::$xmlrpcerruser, $err); } else { // otherwise, we create the right response return new PhpXmlRpc\Response($encoder->encode($ra)); } }
public function __construct($database, $readonly = TRUE, $use_cache = TRUE) { $this->use_cache = $use_cache; $file_exists = file_exists($database); if (!$file_exists) { $readonly = FALSE; } if ($readonly) { $opt = 'rl'; if (!is_readable($database)) { throw new Exception('database is not readable: ' . $database); return FALSE; } } else { $opt = 'cl'; if ($file_exists) { if (!is_writable($database)) { throw new Exception('database is not writeable: ' . $database); return FALSE; } } else { if (!is_writable(dirname($database))) { throw new Exception('database is not inside a writeable directory: ' . $database); return FALSE; } } } $this->db_handler = dba_open($database, $opt, FlatFileDB::HANDLER); if (!$this->db_handler) { throw new Exception('cannot open database: ' . $database); return FALSE; } return $this; }
public function __construct($fileName) { $this->handle = dba_open($fileName, 'r-', 'cdb'); if (!$this->handle) { throw new Exception('Unable to open CDB file "' . $fileName . '"'); } }
function getcomments($m) { global $xmlrpcerruser; $err = ""; $ra = array(); // get the first param if (XMLRPC_EPI_ENABLED == '1') { $msgID = xmlrpc_decode($m->getParam(0)); } else { $msgID = php_xmlrpc_decode($m->getParam(0)); } $dbh = dba_open("/tmp/comments.db", "r", "db2"); if ($dbh) { $countID = "{$msgID}_count"; if (dba_exists($countID, $dbh)) { $count = dba_fetch($countID, $dbh); for ($i = 0; $i < $count; $i++) { $name = dba_fetch("{$msgID}_name_{$i}", $dbh); $comment = dba_fetch("{$msgID}_comment_{$i}", $dbh); // push a new struct onto the return array $ra[] = array("name" => $name, "comment" => $comment); } } } // if we generated an error, create an error return response if ($err) { return new xmlrpcresp(0, $xmlrpcerruser, $err); } else { // otherwise, we create the right response // with the state name return new xmlrpcresp(php_xmlrpc_encode($ra)); } }
function buildPathCache($paths, $last, $cwd, $zcache) { $parentPaths = array(); $populated = array(); $old_db_line = false; if (file_exists($cwd . '.patchwork.paths.txt')) { $old_db = @fopen($cwd . '.patchwork.paths.txt', 'rb'); $old_db && ($old_db_line = fgets($old_db)); } else { $old_db = false; } $tmp = $cwd . '.~' . uniqid(mt_rand(), true); $db = fopen($tmp, 'wb'); $paths = array_flip($paths); unset($paths[$cwd]); uksort($paths, array($this, 'dirCmp')); foreach ($paths as $h => $level) { $this->populatePathCache($populated, $old_db, $old_db_line, $db, $parentPaths, $paths, substr($h, 0, -1), $level, $last); } $db && fclose($db); $old_db && fclose($old_db); $h = $cwd . '.patchwork.paths.txt'; '\\' === DIRECTORY_SEPARATOR && file_exists($h) && @unlink($h); rename($tmp, $h) || unlink($tmp); if (function_exists('dba_handlers')) { $h = array('cdb', 'db2', 'db3', 'db4', 'qdbm', 'gdbm', 'ndbm', 'dbm', 'flatfile', 'inifile'); $h = array_intersect($h, dba_handlers()); $h || ($h = dba_handlers()); if ($h) { foreach ($h as $db) { if ($h = @dba_open($tmp, 'nd', $db, 0600)) { break; } } } } else { $h = false; } if ($h) { foreach ($parentPaths as $paths => &$level) { sort($level); dba_insert($paths, implode(',', $level), $h); } dba_close($h); $h = $cwd . '.patchwork.paths.db'; '\\' === DIRECTORY_SEPARATOR && file_exists($h) && @unlink($h); rename($tmp, $h) || unlink($tmp); } else { $db = false; foreach ($parentPaths as $paths => &$level) { sort($level); $paths = md5($paths) . '.' . substr(md5($cwd), -6) . '.path.txt'; $h = $zcache . $paths[0] . DIRECTORY_SEPARATOR . $paths[1] . DIRECTORY_SEPARATOR; file_exists($h) || mkdir($h, 0700, true); file_put_contents($h . $paths, implode(',', $level)); } } return $db; }
function db4_destroy_create_new() { $ret = $GLOBALS['db'] = dba_open(DATABASE_NAME, 'n', DATABASE_HANDLER); if ($ret === false) { die('failed to create database'); } return $ret; }
function getWriter() { $handle = dba_open($this->mFile, 'cl', $this->mHandler); if (!$handle) { wfDebug("Unable to open DBA cache file {$this->mFile}\n"); } return $handle; }
/** * @throws Exception */ public function __construct($fileName) { $this->realFileName = $fileName; $this->tmpFileName = $fileName . '.tmp.' . mt_rand(0, 0x7fffffff); $this->handle = dba_open($this->tmpFileName, 'n', 'cdb_make'); if (!$this->handle) { throw new Exception('Unable to open CDB file for write "' . $fileName . '"'); } }
function open($mode = 'w') { if ($this->_dbh) { return; } // already open. $watchdog = $this->_timeout; global $ErrorManager; $this->_dba_open_error = false; $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_dba_open_error_handler')); // oops, you don't have DBA support. if (!function_exists("dba_open")) { echo "You don't seem to have DBA support compiled into PHP."; } // lock supported since 4.3.0: if (check_php_version(4, 3, 0) and strlen($mode) == 1) { // PHP 4.3.x Windows lock bug workaround: http://bugs.php.net/bug.php?id=23975 if (isWindows()) { $mode .= "-"; // suppress locking, or } elseif ($this->_handler != 'gdbm') { // gdbm does it internally $mode .= "d"; // else use internal locking } } while (($dbh = dba_open($this->_file, $mode, $this->_handler)) < 1) { if ($watchdog <= 0) { break; } flush(); // "c" failed, try "w" instead. if (substr($mode, 0, 1) == "c" and file_exists($this->_file)) { $mode = "w"; } // conflict: wait some random time to unlock (see ethernet) $secs = 0.5 + (double) rand(1, 32767) / 32767; sleep($secs); $watchdog -= $secs; if (strlen($mode) == 2) { $mode = substr($mode, 0, -1); } } $ErrorManager->popErrorHandler(); if (!$dbh) { if ($error = $this->_dba_open_error) { $error->errno = E_USER_ERROR; $error->errstr .= "\nfile: " . $this->_file . "\nmode: " . $mode . "\nhandler: " . $this->_handler; $ErrorManager->handleError($error); } else { trigger_error("dba_open failed", E_USER_ERROR); } } $this->_dbh = $dbh; return !empty($dbh); }
public function recovery($record) { $key = $record['key']; $store['value'] = $record['value']; $store['dateline'] = $record['dateline']; $store['ttl'] = $record['ttl']; $rs = dba_open(DATA_DIR . '/kvstore/dba.db', 'cl', $this->handle); $ret = dba_replace($this->create_key($key), serialize($store), $rs); dba_close($rs); return $ret; }
function Open($file, $mode = 'n') { $this->Close(); $ext = substr(strrchr($file, '.'), 1); $ext = strtolower($ext); if ($ext == 'cdb' && $mode != 'r') { $ext = 'cdb_make'; } $this->handler = dba_open($file, $mode, $ext); return $this->handler; }
function berekley_db_create($db_path) { if (is_file($db_path)) { return true; } $db_desttmp = @dba_open($db_path, "c", "db4"); @dba_close($db_desttmp); if (is_file($db_path)) { return true; } ToSyslog("berekley_db_create:: Failed Creating {$db_path} database"); }
/** * @param string $file the cache-file. * * @param string $handler the dba handler. * * You have to install one of this handlers before use. * * cdb = Tiny Constant Database - for reading. * cdb_make = Tiny Constant Database - for writing. * db4 = Oracle Berkeley DB 4 - for reading and writing. * qdbm = Quick Database Manager - for reading and writing. * gdbm = GNU Database Manager - for reading and writing. * flatfile = default dba extension - for reading and writing. * * Use flatfile-handler only when you cannot install one, * of the libraries required by the other handlers, * and when you cannot use bundled cdb handler. * * @param string $mode For read/write access, database creation if it doesn't currently exist. * * @param boolean $persistently * * @throws \RuntimeException If no DBA extension or handler installed. */ public function __construct($file, $handler = 'flatfile', $mode = 'c', $persistently = true) { if (false === extension_loaded('dba')) { throw new \RuntimeException('The DBA extension is required for this wrapper, but the extension is not loaded'); } if (false === in_array($handler, dba_handlers(false))) { throw new \RuntimeException('The ' . $handler . ' handler is required for the DBA extension, but the handler is not installed'); } $this->dba = true === $persistently ? dba_popen($file, $mode, $handler) : dba_open($file, $mode, $handler); $this->file = $file; $this->handler = $handler; }
function getCdbHandle() { if ( !$this->cdb ) { global $wgTrustedXffFile; if ( !file_exists( $wgTrustedXffFile ) ) { throw new MWException( 'TrustedXFF: hosts file missing. You need to download it.' ); } if ( !function_exists( 'dba_open' ) ) { throw new MWException( 'The TrustedXFF extension needs PHP\'s DBA module to work.' ); } $this->cdb = dba_open( $wgTrustedXffFile, 'r-', 'cdb' ); } return $this->cdb; }
function init($kw) { $this->mKeywords = trim($kw); if (!$this->mDba) { if (DIRECTORY_SEPARATOR == "/") { //linux $this->mDba = dba_open(__ROOT_PATH . $this->mDict, "r", "db4"); } else { //windows $this->mDba = dba_open(__ROOT_PATH . $this->mDict, "r", "inifile"); } } return $this->mDba ? true : false; }
function OpenDataBase($dbname) { global $WikiDB; // hash of all the DBM file names reset($WikiDB); while (list($key, $file) = each($WikiDB)) { while (($dbi[$key] = @dba_open($file, "c", DBM_FILE_TYPE)) < 1) { $numattempts++; if ($numattempts > MAX_DBM_ATTEMPTS) { ExitWiki("Cannot open database '{$key}' : '{$file}', giving up."); } sleep(1); } } return $dbi; }
/** * @param string $file the cache-file. * * @param string $handler the dba handler. * * You have to install one of this handlers before use. * * cdb = Tiny Constant Database - for reading. * cdb_make = Tiny Constant Database - for writing. * db4 = Oracle Berkeley DB 4 - for reading and writing. * qdbm = Quick Database Manager - for reading and writing. * gdbm = GNU Database Manager - for reading and writing. * inifile = Ini File - for reading and writing. * flatfile = default dba extension - for reading and writing. * * Use "flatfile" only when you cannot install one, of the libraries * required by the other handlers, and when you cannot use bundled cdb handler. * * @param string $mode For read/write access, database creation if it doesn't currently exist. * * r = for read access * w = for read/write access to an already existing database * c = for read/write access and database creation if it doesn't currently exist * n = for create, truncate and read/write access * * When you are absolutely sure that you do not require database locking you can use "-" as suffix. * * @param boolean $persistently * * @throws \RuntimeException If no DBA extension or handler installed. */ public function __construct($file, $handler = 'flatfile', $mode = 'c', $persistently = true) { if (!extension_loaded('dba')) { throw new \RuntimeException('missing ext/dba'); } if (!function_exists('dba_handlers') || !in_array($handler, dba_handlers(false))) { throw new \RuntimeException("dba-handler '{$handler}' not supported"); } $this->dba = true === $persistently ? @dba_popen($file, $mode, $handler) : @dba_open($file, $mode, $handler); if ($this->dba === false) { $err = error_get_last(); throw new \RuntimeException($err['message']); } $this->storage = $file; $this->handler = $handler; }
protected function _handler($mode = 'r') { $handler = '_' . $mode . 'Handler'; if (null === $this->{$handler}) { switch ($mode) { case 'r': $this->_rHandler = dba_popen($this->_options['dba_file'], 'r', $this->_options['dba_type']); break; case 'w': $this->_wHandler = dba_open($this->_options['dba_file'], 'c', $this->_options['dba_type']); $this->_rHandler = $this->_wHandler; break; default: throw new Exception('Not support mode, mode must be r or w.'); } } return $this->{$handler}; }
/** * Connect to the database and do some checks. * * @access public * @return mixed Returns TRUE on a successful database connection, otherwise returns a constant from b8. */ public function connect() { # Have we already connected? if ($this->_db !== NULL) { return TRUE; } # Open the database connection $this->_db = dba_open(dirname(__FILE__) . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . $this->config['database'], "w", $this->config['handler']); if ($this->_db === FALSE) { $this->connected = FALSE; $this->_db = NULL; return self::DATABASE_CONNECTION_FAIL; } # Everything is okay and connected $this->connected = TRUE; # Let's see if this is a b8 database and the version is okay return $this->check_database(); }
public function __construct($handle, Serialize\Iface $s = NULL) { if (is_resource($handle)) { if (get_resource_type($handle) != 'dba') { throw new Exception('invalid handle'); } $this->handle = $handle; } else { $file = $handle; if (!file_exists($file) && !touch($file)) { throw new Exception('invalid file'); } $this->handle = dba_open($file, 'cd'); if (!$this->handle) { throw new Exception('invalid handle'); } } if (!$s instanceof Serialize\Iface) { $s = new Serialize\PHP(); } $this->s = $s; }
function radius_authenticate($user, $password) { global $HTTP_COOKIE_VARS; global $REMOTE_ADDR; if (($db = dba_open("/tmp/radiuscache", "c", "ndbm")) == FALSE) { echo "Couldn't open /tmp/radiuscache<br>\n"; } $cookie = $HTTP_COOKIE_VARS["radius_test"]; if ($cookie != "") { $lastid = dba_fetch($cookie . "_id", $db); $laston = dba_fetch($cookie . "_laston", $db); $lasthost = dba_fetch($cookie . "_fromip", $db); $lastuserid = dba_fetch($cookie . "_userid", $db); } // // Sanity checking // if ($cookie == "" || $lastid == "" || $laston == 0 || $laston < time() - 15 * 60 || $lasthost != $REMOTE_ADDR || $lastuserid != $user) { // 2 -> Access-Accept // 3 -> Access-Reject if (($retval = RADIUS_AUTHENTICATION($user, $password)) == 2) { if ($cookie == "") { $cookie = md5(uniqid(rand())); } setcookie("radius_test", $cookie); dba_replace($cookie . "_id", $cookie, $db); dba_replace($cookie . "_userid", $user, $db); dba_replace($cookie . "_fromip", $REMOTE_ADDR, $db); dba_replace($cookie . "_laston", time(), $db); } } else { setcookie("radius_test", $cookie); dba_replace($cookie . "_laston", time(), $db); $retval = 2; } dba_close($db); return $retval == 2; }
function Compile() { $unix = new unix(); $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid"; $pid = @file_get_contents($pidfile); if ($unix->process_exists($pid, basename(__FILE__))) { if ($GLOBALS["VERBOSE"]) { echo "Already executed pid {$pid}\n"; } return; } $t = time(); $q = new mysql_squid_builder(); echo "**** LIST_TABLES_CATEGORIES_PERSO *****\n"; $tablescat = $q->LIST_TABLES_CATEGORIES_PERSO(); $source_dir = "/home/artica/categories_perso"; if (count($tablescat) == 0) { echo "tablescat = 0\n"; return; } $i = 0; while (list($tablename, $ligne) = each($tablescat)) { if (preg_match("#^categoryuris#", $tablename)) { continue; } $COUNT_ROWS = $q->COUNT_ROWS($tablename); if ($COUNT_ROWS == 0) { continue; } echo " **** {$tablename} {$COUNT_ROWS} ITEMS *****\n"; $Dir = "{$source_dir}/{$tablename}"; @mkdir("{$Dir}", 0777, true); echo "{$tablename}: Building {$Dir}/domains"; @unlink("{$Dir}/domains"); @chmod("{$Dir}/domains", 0777); $sql = "SELECT pattern FROM {$tablename} WHERE enabled=1 ORDER BY pattern INTO OUTFILE '{$Dir}/domains' LINES TERMINATED BY '\n';"; $q->QUERY_SQL($sql); if (!$q->ok) { echo "{$tablename}: {$q->mysql_error}\n"; continue; } $handle = @fopen("{$Dir}/domains", "r"); if (!$handle) { echo "Failed to open file {$Dir}/domains\n"; continue; } $DestDB = "{$Dir}/domains.db"; @unlink($DestDB); $db_desttmp = dba_open($DestDB, "n", "db4"); if (!$db_desttmp) { echo "Unable to Create {$DestDB}\n"; continue; } dba_close($db_desttmp); $db_dest = dba_open($DestDB, "w", "db4"); @chmod($DestDB, 0777); if (!$db_dest) { echo "Unable to open for `writing` \"{$DestDB}\"\n"; continue; } while (!feof($handle)) { $www = trim(fgets($handle, 4096)); $www = trim(str_replace('"', "", $www)); if ($www == null) { continue; } $www = strtolower($www); if (!dba_insert("{$www}", "yes", $db_dest)) { echo "dba_insert({$www},yes... false\n"; continue; } } $i++; dba_close($db_dest); @fclose($handle); @unlink("{$Dir}/domains"); } stats_admin_events(2, "1%) {$i} Personal tables compiled took:" . $unix->distanceOfTimeInWords($t, time()), null, __FILE__, __LINE__); }
<?php require_once dirname(__FILE__) . '/test.inc'; echo "database handler: {$handler}\n"; if (($db_file = dba_open($db_filename, "n", $handler)) !== FALSE) { dba_insert("key1", "Content String 1", $db_file); dba_insert("key2", "Content String 2", $db_file); $a = dba_firstkey($db_file); $i = 0; while ($a) { $a = dba_nextkey($db_file); $i++; } echo $i; for ($i = 1; $i < 3; $i++) { echo dba_exists("key{$i}", $db_file) ? "Y" : "N"; } echo "\n"; var_dump(dba_optimize()); var_dump(dba_optimize("")); var_dump(dba_optimize($db_file)); dba_close($db_file); } else { echo "Error creating database\n"; } ?> ===DONE===
function clear_cache() { // Close dbm file dba_close($this->dba); // Create new dbm file $this->dba = dba_open($this->dbm_filename, 'n', $this->cache_type); if (!$this->dba) { die('failed to clear cache/recreate dbm file: ' . $this->dbm_filename); } // Insert getID3 version number dba_insert(GETID3_VERSION, GETID3_VERSION, $this->dba); // Reregister shutdown function register_shutdown_function(array($this, '__destruct')); }
private static function _HGetDbHandler($sDbFile, $sHandler) { if (isset(self::$s_aDBHandler[$sDbFile])) { return self::$s_aDBHandler[$sDbFile]; } return self::$s_aDBHandler[$sDbFile] = dba_open($sDbFile, 'r', $sHandler); }