summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Bridge.php40
-rw-r--r--src/PhpBB3.php33
-rw-r--r--src/Util.php7
3 files changed, 53 insertions, 27 deletions
diff --git a/src/Bridge.php b/src/Bridge.php
index 05689f8..6b83230 100644
--- a/src/Bridge.php
+++ b/src/Bridge.php
@@ -1,6 +1,7 @@
<?php
require_once(__DIR__ . '/BridgeConf.php');
+require_once(__DIR__ . '/Util.php');
class Bridge {
protected $db;
@@ -13,29 +14,28 @@ class Bridge {
}
public function getPostId($messageId) {
+ throw_if_null($messageId);
+
$sql = 'SELECT post_id FROM posts ' .
'WHERE message_id = ' . $this->db->quote($messageId);
$row = $this->get_exactly_one_row($sql);
- return $row['post_id'];
+ return $row ? $row['post_id'] : false;
}
public function getMessageId($postId) {
+ throw_if_null($postId);
+
$sql = 'SELECT message_id FROM posts ' .
'WHERE post_id = ' . $this->db->quote($postId);
$row = $this->get_exactly_one_row($sql);
- return $row['message_id'];
+ return $row ? $row['message_id'] : false;
}
public function setPostId($messageId, $postId) {
- if ($messageId === null) {
- throw new Exception('message id is null');
- }
-
- if ($postId === null) {
- throw new Exception('post id is null');
- }
+ throw_if_null($messageId);
+ throw_if_null($postId);
$sql = 'UPDATE posts SET ' .
'post_id = ' . $postId . ' ' .
@@ -49,23 +49,23 @@ class Bridge {
}
public function getDefaultForumId($list) {
+ throw_if_null($list);
+
$sql = 'SELECT forum_id FROM forums ' .
'WHERE list_name = ' . $this->db->quote($list);
$row = $this->get_exactly_one_row($sql);
- return $row['forum_id'];
+ return $row ? $row['forum_id'] : false;
}
- public function registerMessage($messageId, $inReplyTo, $refs) {
- if ($messageId === null) {
- throw new Exception('message id is null');
- }
+ public function registerMessage($messageId, $inReplyTo, $references) {
+ throw_if_null($messageId);
$sql = 'INSERT IGNORE INTO posts ' .
'(message_id, in_reply_to, refs) ' .
'VALUES (' . $this->db->quote($messageId) . ', '
- . $this->db->quote($inReplyTo) . ', '
- . $this->db->quote($refs) . ')';
+ . $this->quote($inReplyTo) . ', '
+ . $this->quote($references) . ')';
$count = $this->db->exec($sql);
@@ -80,17 +80,19 @@ class Bridge {
switch (count($rows)) {
case 0:
- throw new Exception("No rows returned: $sql");
- break;
+ return false;
case 1:
return $rows[0];
default:
throw new Exception("Too many rows returned: $sql");
- break;
}
}
+
+ protected function quote($arg) {
+ return $arg === null ? 'NULL' : $this->db->quote($arg);
+ }
}
?>
diff --git a/src/PhpBB3.php b/src/PhpBB3.php
index 37c4bdf..aca8333 100644
--- a/src/PhpBB3.php
+++ b/src/PhpBB3.php
@@ -1,5 +1,7 @@
<?php
+require_once(__DIR__ . '/Util.php');
+
# phpBB setup
define('IN_PHPBB', true);
require_once(__DIR__ . '/PhpBB3Conf.php');
@@ -13,16 +15,20 @@ class PhpBB3 {
}
public function getUserId($from) {
+ throw_if_null($from);
+
global $db;
$sql = 'SELECT user_id FROM ' . USERS_TABLE . ' ' .
'WHERE user_email = "' . $db->sql_escape($from) . '"';
$row = $this->get_exactly_one_row($sql);
- return $row['user_id'];
+ return $row ? $row['user_id'] : false;
}
public function getUserName($id) {
+ throw_if_null($id);
+
# NB: user_get_id_name is pass-by-reference; we copy $id to prevent
# it from being modified, as we might need it for error messages
$ids = array($id);
@@ -39,16 +45,20 @@ class PhpBB3 {
}
public function getTopicAndForumIds($post_id) {
+ throw_if_null($post_id);
+
global $db;
$sql = 'SELECT topic_id, forum_id FROM ' . POSTS_TABLE . ' ' .
'WHERE post_id = "' . $db->sql_escape($post_id) . '"';
$row = $this->get_exactly_one_row($sql);
- return $row;
+ return $row;
}
public function forumExists($forumId) {
+ throw_if_null($forumId);
+
global $db;
$sql = 'SELECT 1 FROM ' . FORUMS_TABLE . ' ' .
@@ -73,6 +83,8 @@ class PhpBB3 {
}
public function topicExists($topicId) {
+ throw_if_null($topicId);
+
global $db;
$sql = 'SELECT 1 FROM ' . TOPICS_TABLE . ' ' .
@@ -97,6 +109,8 @@ class PhpBB3 {
}
public function postMessage($postType, $forumId, $topicId, $msg) {
+ throw_if_null($msg);
+
if ($postType != 'post' && $postType != 'reply') {
throw new Exception('bad post type: ' . $postType);
}
@@ -109,16 +123,19 @@ class PhpBB3 {
throw new Exception('topic does not exist: ' . $topicId);
}
- if ($msg === null) {
- throw new Exception('message is null');
+ $userId = $this->getUserId($msg->getFrom());
+ if ($userId === false) {
+ throw new Exception('unrecognized email address: ' . $msg->getFrom());
+ }
+
+ $userName = $this->getUserName($userId);
+ if ($userName === false) {
+ throw new Exception('unrecognized user id: ' . $userId);
}
$subject = $msg->getSubject();
$message = 'foo'; # FIXME: fill in with acutal message contents
- $userId = $this->getUserId($msg->getFrom());
- $userName = $this->getUserName($userId);
-
# bring in the PhpBB globals
global $phpEx, $phpbb_root_path, $user, $auth,
$template, $cache, $db, $config;
@@ -185,7 +202,7 @@ class PhpBB3 {
switch (count($rows)) {
case 0:
- throw new Exception("No rows returned: $sql");
+ return false;
case 1:
return $rows[0];
diff --git a/src/Util.php b/src/Util.php
new file mode 100644
index 0000000..817e8a1
--- /dev/null
+++ b/src/Util.php
@@ -0,0 +1,7 @@
+<?php
+
+function throw_if_null($arg) {
+ if ($arg === null) throw new Exception('argument is null');
+}
+
+?>