* @param array $params A hash containing any additional configuration or
* connection parameters a subclass might need.
*
- * @return Horde_Token The newly created concrete Horde_Token instance, or
- * false an error.
+ * @return Horde_Token The newly created concrete Horde_Token instance.
*/
static public function factory($driver, $params = array())
{
* @param array $params A hash containing any additional configuration or
* connection parameters a subclass might need.
*
- * @return Horde_Token The concrete Horde_Token reference, or false on
- * error.
+ * @return Horde_Token The concrete Horde_Token reference.
*/
static public function singleton($driver, $params = array())
{
* @param string $token The value of the token to check.
*
* @return boolean True if the token has not been used, false otherwise.
+ * @throws Horde_Exception
*/
public function verify($token)
{
$this->purge();
- $exists = $this->exists($token);
- if (is_a($exists, 'PEAR_Error')) {
- return $exists;
- } elseif ($exists) {
+ if ($this->exists($token)) {
return false;
- } else {
- return $this->add($token);
}
+ return $this->add($token);
}
/**
* This is an abstract method that should be overridden by a
* subclass implementation. The base implementation allows all
* token values.
+ *
+ * @throws Horde_Exception
*/
public function exists()
{
* This is an abstract method that should be overridden by a
* subclass implementation. The base implementation allows all
* token values.
+ *
+ * @throws Horde_Exception
*/
public function add()
{
- return true;
}
/**
* This is an abstract method that should be overridden by a
* subclass implementation. The base implementation allows all
* token values.
+ *
+ * @throws Horde_Exception
*/
public function purge()
{
- return true;
}
}
/**
* Deletes all expired connection id's from the SQL server.
*
- * @return boolean True on success, a PEAR_Error object on failure.
+ * @throws Horde_Exception
*/
public function purge()
{
// Make sure we have no open file descriptors before unlinking
// files.
if (!$this->_disconnect()) {
- return PEAR::raiseError('Unable to close file descriptors');
+ throw new Horde_Exception('Unable to close file descriptors');
}
/* Build stub file list. */
if (!($dir = opendir($this->_params['token_dir']))) {
- return PEAR::raiseError('Unable to open token directory');
+ throw new Horde_Exception('Unable to open token directory');
}
/* Find expired stub files */
while (($dirEntry = readdir($dir)) != '') {
- if (preg_match('|^conn_\w{8}$|', $dirEntry) && (time() - filemtime($this->_params['token_dir'] . '/' . $dirEntry) >= $this->_params['timeout'])) {
- if (!@unlink($this->_params['token_dir'] . '/' . $dirEntry)) {
- return PEAR::raiseError('Unable to purge token file.');
- }
+ if (preg_match('|^conn_\w{8}$|', $dirEntry) && (time() - filemtime($this->_params['token_dir'] . '/' . $dirEntry) >= $this->_params['timeout']) &&
+ !@unlink($this->_params['token_dir'] . '/' . $dirEntry)) {
+ throw new Horde_Exception('Unable to purge token file.');
}
}
closedir($dir);
- return true;
}
/**
* TODO
+ *
+ * @return boolean TODO
+ * @throws Horde_Exception
*/
public function exists($tokenID)
{
- if (is_a(($result = $this->_connect($tokenID)), 'PEAR_Error')) {
- return $result;
- }
+ $this->_connect($tokenID);
/* Find already used IDs. */
$fileContents = file($this->_params['token_dir'] . '/conn_' . $this->encodeRemoteAddress());
/**
* TODO
+ *
+ * @throws Horde_Exception
*/
public function add($tokenID)
{
- if (is_a(($result = $this->_connect($tokenID)), 'PEAR_Error')) {
- return $result;
- }
+ $this->_connect($tokenID);
/* Write the entry. */
fwrite($this->_fd, "$tokenID\n");
/* Return an error if the update fails, too. */
if (!$this->_disconnect()) {
- return PEAR::raiseError('Failed to close token file cleanly.');
+ throw new Horde_Exception('Failed to close token file cleanly.');
}
-
- return true;
}
/**
* Opens a file descriptor to a new or existing file.
*
- * @return boolean True on success, a PEAR_Error object on failure.
+ * @throws Horde_Exception
*/
protected function _connect($tokenID)
{
- if (!$this->_connected) {
-
- // Open a file descriptor to the token stub file.
- $this->_fd = @fopen($this->_params['token_dir'] . '/conn_' . $this->encodeRemoteAddress(), 'a');
- if (!$this->_fd) {
- return PEAR::raiseError('Failed to open token file.');
- }
+ if ($this->_connected) {
+ return;
+ }
- $this->_connected = true;
+ // Open a file descriptor to the token stub file.
+ $this->_fd = @fopen($this->_params['token_dir'] . '/conn_' . $this->encodeRemoteAddress(), 'a');
+ if (!$this->_fd) {
+ throw new Horde_Exception('Failed to open token file.');
}
- return true;
+ $this->_connected = true;
}
/**
/**
* Deletes all expired connection id's from the SQL server.
*
- * @return boolean True on success, a PEAR_Error object on failure.
+ * @throws Horde_Exception
*/
public function purge()
{
- if (is_a(($result = $this->_connect()), 'PEAR_Error')) {
- return $result;
- }
+ $this->_connect();
/* Build SQL query. */
$query = 'DELETE FROM ' . $this->_params['table']
/* Return an error if the update fails. */
$result = $this->_write_db->query($query, $values);
- if (is_a($result, 'PEAR_Error')) {
+ if ($result instanceof PEAR_Error) {
Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR);
- return $result;
+ throw new Horde_Exception($result);
}
-
- return true;
}
/**
* TODO
+ *
+ * @return boolean TODO
*/
public function exists($tokenID)
{
- if (is_a(($result = $this->_connect()), 'PEAR_Error')) {
+ try {
+ $this->_connect();
+ } catch (Horde_Exception $e) {
return false;
}
$values = array($this->encodeRemoteAddress(), $tokenID);
$result = $this->_db->getOne($query, $values);
- if (is_a($result, 'PEAR_Error')) {
+ if ($result instanceof PEAR_Error) {
Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR);
return false;
} else {
/**
* TODO
+ *
+ * @throws Horde_Exception
*/
public function add($tokenID)
{
- if (is_a(($result = $this->_connect()), 'PEAR_Error')) {
- return $result;
- }
+ $this->_connect();
/* Build SQL query. */
$query = 'INSERT INTO ' . $this->_params['table']
$values = array($this->encodeRemoteAddress(), $tokenID, time());
$result = $this->_write_db->query($query, $values);
- if (is_a($result, 'PEAR_Error')) {
+ if ($result instanceof PEAR_Error) {
Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR);
- return $result;
+ throw new Horde_Exception($result);
}
-
- return true;
}
/**
* Opens a connection to the SQL server.
*
- * @return boolean True on success, a PEAR_Error object on failure.
+ * @throws Horde_Exception
*/
protected function _connect()
{
if ($this->_connected) {
- return true;
+ return;
}
- $result = Horde_Util::assertDriverConfig($this->_params, array('phptype'), 'token SQL', array('driver' => 'token'));
- if (is_a($result, 'PEAR_Error')) {
- return $result;
- }
+ Horde_Util::assertDriverConfig($this->_params, array('phptype'), 'token SQL', array('driver' => 'token'));
if (!isset($this->_params['database'])) {
$this->_params['database'] = '';
$this->_write_db = DB::connect($this->_params,
array('persistent' => !empty($this->_params['persistent']),
'ssl' => !empty($this->_params['ssl'])));
- if (is_a($this->_write_db, 'PEAR_Error')) {
- return $this->_write_db;
+ if ($this->_write_db instanceof PEAR_Error) {
+ throw new Horde_Exception($this->_write_db);
}
// Set DB portability options.
case 'mssql':
$this->_write_db->setOption('portability', DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_ERRORS | DB_PORTABILITY_RTRIM);
break;
+
default:
$this->_write_db->setOption('portability', DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_ERRORS);
+ break;
}
/* Check if we need to set up the read DB connection
$this->_db = DB::connect($params,
array('persistent' => !empty($params['persistent']),
'ssl' => !empty($params['ssl'])));
- if (is_a($this->_db, 'PEAR_Error')) {
- return $this->_db;
+ if ($this->_db instanceof PEAR_Error) {
+ throw new Horde_Exception($this->_db);
}
// Set DB portability options.
case 'mssql':
$this->_db->setOption('portability', DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_ERRORS | DB_PORTABILITY_RTRIM);
break;
+
default:
$this->_db->setOption('portability', DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_ERRORS);
+ break;
}
} else {
}
$this->_connected = true;
- return true;
}
}
<api>beta</api>
</stability>
<license uri="http://www.gnu.org/copyleft/lesser.html">LGPL</license>
- <notes>* Initial Horde 4 package.
+ <notes>* Use exceptions, not PEAR_Errors.
+ * Initial Horde 4 package.
</notes>
<contents>
<dir name="/">
<min>1.5.0</min>
</pearinstaller>
<package>
+ <name>Exception</name>
+ <channel>pear.horde.org</channel>
+ </package>
+ <package>
<name>Util</name>
<channel>pear.horde.org</channel>
</package>