function __construct($prefix) { if (!is_dir(DATA_DIR . '/kvstore/')) { utils::mkdir_p(DATA_DIR . '/kvstore/'); } $this->rs = dba_popen(DATA_DIR . '/kvstore/dba.db', 'c'); }
private function get_db_handle($mode) { if (!isset($this->db_handles[$mode])) { $fn = self::get_db_file_name(); $h = self::get_db_handler(); $this->db_handles[$mode] = dba_popen($fn, $h, $mode); } return $this->db_handles[$mode]; }
/** * @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; }
/** * @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}; }
<?php $handler = "flatfile"; require_once dirname(__FILE__) . '/test.inc'; echo "database handler: {$handler}\n"; $db_file1 = dba_popen($db_filename, 'n-t', 'flatfile'); ?> ===DONE===
function patchworkPath($file, &$last_level = false, $level = false, $base = false) { if (false === $level) { /**/ if ('\\' === DIRECTORY_SEPARATOR) { if (isset($file[0]) && ('\\' === $file[0] || false !== strpos($file, ':'))) { return $file; } } if (isset($file[0]) && '/' === $file[0]) { return $file; } $i = 0; $level = PATCHWORK_PATH_LEVEL; } else { 0 <= $level && ($base = 0); $i = PATCHWORK_PATH_LEVEL - $level - $base; 0 > $i && ($i = 0); } /**/ if ('\\' === DIRECTORY_SEPARATOR) { false !== strpos($file, '\\') && ($file = strtr($file, '\\', '/')); } if (0 === $i) { $source = PATCHWORK_PROJECT_PATH . $file; if (file_exists($source)) { $last_level = $level; /**/ if ('\\' === DIRECTORY_SEPARATOR) { return false !== strpos($source, '/') ? strtr($source, '/', '\\') : $source; } else { return $source; } } } if ($slash = '/' === substr($file, -1)) { $file = substr($file, 0, -1); } /**/ require boot::$manager->getCurrentDir() . 'class/Patchwork/Updatedb.php'; /**/ $a = new Patchwork\Updatedb(); /**/ $a = $a->buildPathCache($GLOBALS['patchwork_path'], PATCHWORK_PATH_LEVEL, PATCHWORK_PROJECT_PATH, PATCHWORK_ZCACHE); /**/ if ($a) { static $db; if (!isset($db)) { if (!($db = @dba_popen(PATCHWORK_PROJECT_PATH . '.patchwork.paths.db', 'rd', $a))) { require boot::$manager->getCurrentDir() . 'class/Patchwork/Updatedb.php'; $db = new Patchwork\Updatedb(); $db->buildPathCache($GLOBALS['patchwork_path'], PATCHWORK_PATH_LEVEL, PATCHWORK_PROJECT_PATH, PATCHWORK_ZCACHE); if (!($db = dba_popen(PATCHWORK_PROJECT_PATH . '.patchwork.paths.db', 'rd', $a))) { exit; } } } $base = dba_fetch($file, $db); /**/ } else { $base = md5($file) . '.' . substr(md5(PATCHWORK_PROJECT_PATH), -6) . '.path.txt'; $base = PATCHWORK_ZCACHE . $base[0] . '/' . $base[1] . '/' . $base; if (file_exists($base)) { $base = file_get_contents($base); } else { $base = false; } /**/ } if (false !== $base) { $base = explode(',', $base); do { if (current($base) >= $i) { $base = (int) current($base); $last_level = $level - $base + $i; /**/ if ('\\' === DIRECTORY_SEPARATOR) { false !== strpos($file, '/') && ($file = strtr($file, '/', '\\')); } return $GLOBALS['patchwork_path'][$base] . (0 <= $last_level ? $file : substr($file, 6)) . ($slash ? DIRECTORY_SEPARATOR : ''); } } while (false !== next($base)); } return false; }
<?php require_once dirname(__FILE__) . '/test.inc'; echo "database handler: {$handler}\n"; if (($db = dba_popen($db_file, "n", $handler)) !== FALSE) { echo "Opened\n"; dba_insert("a", "Inserted", $db); echo dba_fetch("a", $db) . "\n"; dba_close($db); echo "Closed\n"; } else { echo "Error creating database\n"; } if (($db = dba_popen($db_file, "n", $handler)) !== FALSE) { echo "Opened\n"; dba_insert("a", "Inserted", $db); echo dba_fetch("a", $db) . "\n"; }
function __construct($path = "oauth.gdbm", $request_expiration = null, $access_expiration = null, $session_expiraton = null) { /*{{{*/ $this->dbh = dba_popen($path, 'c', 'gdbm'); if (!empty($request_expiration)) { $this->request_expiration = $request_expiration; } if (!empty($access_expiration)) { $this->access_expiration = $access_expiration; } if (!empty($session_expiraton)) { $this->session_expiration = $session_expiraton; } }
/** * Constructor Method. Establishes a connection to the database, * which can be verified by an if ($dbd->connection) {. * * @access public * @param string $path * @param string $mode * @param string $handler * @param boolean $persistent * */ function BDB($path, $mode = 'r', $handler = 'db3', $persistent = 0) { $this->path = $path; $this->mode = $mode; $this->handler = $handler; $this->persistent = $persistent; if ($persistent) { $this->connection = @dba_popen($path, $mode, $handler); } else { $this->connection = @dba_open($path, $mode, $handler); } }
function __construct($path = 'conf/oauth.gdbm') { /*{{{*/ /// XX DOKU_CONF $this->dbh = dba_popen($path, 'c', 'inifile'); }
protected function _connect() { if (isset($this->_profile['file']) && $this->_profile['file'] != '') { $this->_file = str_replace(array('var:', 'temp:'), array(jApp::varPath(), jApp::tempPath()), $this->_profile['file']); } else { throw new Exception('No file in the configuration of the dba driver for jKVDB'); } $mode = "cl"; if (isset($this->_profile['handler']) && $this->_profile['handler'] != '') { $handler = $this->_profile['handler']; } else { throw new Exception('No handler in the configuration of the dba driver for jKVDB'); } if (isset($this->_profile['persistant']) && $this->_profile['persistant']) { $conn = dba_popen($this->_file, $mode, $handler); } else { $conn = dba_open($this->_file, $mode, $handler); } if ($conn === false) { return null; } return $conn; }
<?php $handler = "db4"; require_once dirname(__FILE__) . '/test.inc'; echo "database handler: {$handler}\n"; echo "Test 1\n"; $db_file1 = dba_popen($db_filename, 'n', 'flatfile'); dba_insert("key1", "This is a test insert 1", $db_file1); echo dba_fetch("key1", $db_file1), "\n"; echo "Test 2\n"; $db_file2 = dba_popen($db_filename, 'n', 'flatfile'); if ($db_file1 === $db_file2) { echo "resources are the same\n"; } else { echo "resources are different\n"; } echo "Test 3 - fetch both rows from second resource\n"; dba_insert("key2", "This is a test insert 2", $db_file2); echo dba_fetch("key1", $db_file2), "\n"; echo dba_fetch("key2", $db_file2), "\n"; echo "Test 4 - fetch both rows from first resource\n"; echo dba_fetch("key1", $db_file1), "\n"; echo dba_fetch("key2", $db_file1), "\n"; echo "Test 5 - close 2nd resource\n"; dba_close($db_file2); var_dump($db_file1); var_dump($db_file2); echo "Test 6 - query after closing 2nd resource\n"; echo dba_fetch("key1", $db_file1), "\n"; echo dba_fetch("key2", $db_file1), "\n"; ?>
private function _log($m = null) { /*{{{*/ $OAdebug = $this->getConf('log_all_requests'); $OAlogdb = '/tmp/oAuth.log'; $dbh = dba_popen($OAlogfile, 'c', 'inifile'); if (!isset($OAlog) || $OAlog === false) { return; } $data = array('REMOTE_USER' => $_SERVER['REMOTE_USER'], 'HTTP_USER_AGENT' => $_SERVER['HTTP_USER_AGENT'], 'REMOTE_ADDR' => $_SERVER['REMOTE_ADDR'], 'REMOTE_PORT' => $_SERVER['REMOTE_PORT'], 'HTTP_X_REAL_IP' => $_SERVER['HTTP_X_REAL_IP'], 'HTTP_X_FORWARDED_FOR' => $_SERVER['HTTP_X_FORWARDED_FOR'], 'HTTP_COOKIE' => $_SERVER['HTTP_COOKIE'], 'REQUEST_METHOD' => $_SERVER['REQUEST_METHOD'], 'REQUEST_URI' => $_SERVER['REQUEST_URI'], 'QUERY_STRING' => $_SERVER['QUERY_STRING'], 'REQUEST_TIME' => $_SERVER['REQUEST_TIME'], 'GET' => $_GET, 'POST' => $_POST, 'REQUEST' => $_REQUEST, 'msg' => $m); if (!$dbh || !dba_insert("log_{$token}", serialize($data), $dbh)) { header("HTTP/1.1 500 Internal Server Error"); echo 'Cannot write to log: ' . $OAlogdb; return; } dba_close($this->dbh); }
function _load($fpath) { $ext = strrchr($fpath, '.'); $type = $ext ? strtolower(substr($ext, 1)) : 'gdbm'; if (!in_array($type, dba_handlers())) { trigger_error("您的 dba 扩展不支持 `{$type}` 这一数据类型", E_USER_ERROR); } $this->_dbh = dba_popen($fpath, 'r', $type); if (!$this->_dbh) { trigger_error("无法打开类型为 `{$type}` 的 dba 数据文件 `{$fpath}`", E_USER_ERROR); } }
protected function _connect() { if (isset($this->_profile['file']) && $this->_profile['file'] != '') { $this->_file = jFile::parseJelixPath($this->_profile['file']); } else { throw new Exception('No file in the configuration of the dba driver for jKVDB'); } $mode = "cl"; if (isset($this->_profile['handler']) && $this->_profile['handler'] != '') { $handler = $this->_profile['handler']; } else { throw new Exception('No handler in the configuration of the dba driver for jKVDB'); } if (isset($this->_profile['persistant']) && $this->_profile['persistant']) { $conn = dba_popen($this->_file, $mode, $handler); } else { $conn = dba_open($this->_file, $mode, $handler); } if ($conn === false) { return null; } return $conn; }
<?php $handler = 'db4'; require_once dirname(__FILE__) . '/test.inc'; $db = dba_popen($db_filename, 'c', 'db4'); dba_insert('X', 'XYZ', $db); dba_insert('Y', '123', $db); var_dump($db, dba_fetch('X', $db)); var_dump(dba_firstkey($db)); var_dump(dba_nextkey($db)); dba_close($db); ?> ===DONE===
function __construct($prefix) { $this->rs = dba_popen(DATA_DIR . '/kvstore/dba.db', 'c'); }
<?php global $id; $request_method = $_ENV['REQUEST_METHOD']; $request_uri = substr($_ENV['REQUEST_URI'], 1); $content_length = $_ENV['CONTENT_LENGTH']; $filename = "/tmp/data.db"; if ($request_method === 'GET') { $id = dba_popen($filename, "r", "gdbm"); $val = dba_fetch($request_uri, $id); echo $val; } else { if ($request_method === 'PUT') { $buffer = file_get_contents('php://input'); $id = dba_popen($filename, "c", "gdbm"); dba_replace($request_uri, $buffer, $id); } else { if ($request_method === 'DELETE') { $id = dba_popen($filename, "w", "gdbm"); dba_delete($request_uri, $id); } } }
function SimpleOAuthDataStore($path = "oauth.gdbm") { /*{{{*/ $this->dbh = dba_popen($path, 'c', 'gdbm'); }
<?php error_reporting(E_ALL); require_once dirname(__FILE__) . '/test.inc'; $db = dba_popen($db_filename, 'c'); $key = 1; $copy = $key; echo gettype($key) . "\n"; echo gettype($copy) . "\n"; dba_exists($key, $db); echo gettype($key) . "\n"; echo gettype($copy) . "\n"; dba_close($db);
<?php $handler = "db4"; require_once dirname(__FILE__) . '/test.inc'; echo "database handler: {$handler}\n"; if (($db_file = dba_popen($db_filename, "c", $handler)) !== FALSE) { echo "database file created\n"; dba_insert("key1", "This is a test insert", $db_file); echo dba_fetch("key1", $db_file), "\n"; dba_close($db_file); } else { echo "Error creating {$db_filename}\n"; }
/** * Opens a database. * * @access public * @param string $dbName The name of a database * @param string $mode The mode in which to open a database. * 'r' opens read-only. * 'w' opens read-write. * 'n' creates a new database and opens read-write. * 'c' creates a new database if the database does not * exist and opens read-write. * @param boolean $persistent Determines whether to open the database * peristently. Not supported here. * @return object PEAR_Error on failure */ function open($dbName = '', $mode = 'r', $persistent = false) { if (is_null($this->_driver)) { return $this->raiseError(DBA_ERROR_NO_DRIVER); } if ($this->_driver == 'gdbm') { $this->_hasReplace = false; } else { $this->_hasReplace = true; } if ($dbName == '') { return $this->raiseError(DBA_ERROR_NO_DBNAME); } else { $this->_dbName = $dbName; } switch ($mode) { case 'r': // open for reading $this->_writable = false; $this->_readable = true; break; case 'n': case 'c': case 'w': $this->_writable = true; $this->_readable = true; break; default: return $this->raiseError(DBA_ERROR_INVALID_MODE, NULL, NULL, 'filemode: ' . $mode); } // open the index file if ($persistent) { $this->_dba = dba_popen($dbName, $mode, $this->_driver); } else { $this->_dba = dba_open($dbName, $mode, $this->_driver); } if ($this->_dba === false) { $this->_writable = false; $this->_readable = false; return $this->raiseError(DBA_ERROR_CANNOT_OPEN, NULL, NULL, 'dbname: ' . $dbName . ' filemode: ' . $mode); } }
function __construct($path = "oauth.gdbm") { /*{{{*/ $this->dbh = dba_popen($path, 'c', 'gdbm'); }
/** * Open the database. * * @param string $fileName Filename to the database file. * @param int $mode Constants Dba::MODE_* * @param array $options * * @throws RuntimeException */ public function open(string $fileName, int $mode, array $options = []) { if (!array_key_exists('persistent', $options)) { $options['persistent'] = FALSE; } if ($options['persistent']) { $handle = @dba_popen($fileName, self::getMode($mode), $options['driverName']); } else { $handle = @dba_open($fileName, self::getMode($mode), $options['driverName']); } if (!$handle) { $message = sprintf('Cannot open Database "%s" with mode "%s"', $fileName, self::getMode($mode)); throw new RuntimeException($message); } $this->setHandle($handle); }