}
function common_element_end($tag) {
+ static $empty_tag = array('base', 'meta', 'link', 'hr',
+ 'br', 'param', 'img', 'area',
+ 'input', 'col');
global $xw;
- # TODO: switch based on $tag
- $xw->fullEndElement();
+ # XXX: check namespace
+ if (in_array($tag, $empty_tag)) {
+ $xw->endElement();
+ } else {
+ $xw->fullEndElement();
+ }
}
function common_element($tag, $attrs=NULL, $content=NULL) {
- common_element_start($tag, $attrs);
- global $xw;
- $xw->text($content);
- common_element_end($tag);
+ common_element_start($tag, $attrs);
+ global $xw;
+ if ($content) {
+ $xw->text($content);
+ }
+ common_element_end($tag);
}
function common_start_xml($doc=NULL, $public=NULL, $system=NULL) {
$xw->flush();
}
-define('PAGE_TYPE_PREFS', 'application/xhtml+xml,text/html;q=0.7,application/xml;q=0.3,text/xml;q=0.2');
+define('PAGE_TYPE_PREFS', 'text/html,application/xhtml+xml,application/xml;q=0.3,text/xml;q=0.2');
function common_show_header($pagetitle, $callable=NULL, $data=NULL, $headercall=NULL) {
global $config, $xw;
if (is_null($user)) {
return false;
} else {
- return (0 == strcmp(common_munge_password($password, $user->id),
- $user->password));
+ if (0 == strcmp(common_munge_password($password, $user->id),
+ $user->password)) {
+ return $user;
+ } else {
+ return false;
+ }
}
}
}
}
-function common_set_user($nickname) {
- if (is_null($nickname) && common_have_session()) {
+# Three kinds of arguments:
+# 1) a user object
+# 2) a nickname
+# 3) NULL to clear
+
+function common_set_user($user) {
+ if (is_null($user) && common_have_session()) {
unset($_SESSION['userid']);
return true;
- } else {
+ } else if (is_string($user)) {
+ $nickname = $user;
$user = User::staticGet('nickname', $nickname);
- if ($user) {
- common_ensure_session();
- $_SESSION['userid'] = $user->id;
- return true;
- } else {
- return false;
- }
+ } else if (!($user instanceof User)) {
+ return false;
+ }
+
+ if ($user) {
+ common_ensure_session();
+ $_SESSION['userid'] = $user->id;
+ return $user;
}
return false;
}
define('REMEMBERME', 'rememberme');
define('REMEMBERME_EXPIRY', 30 * 24 * 60 * 60);
-function common_rememberme() {
- $user = common_current_user();
+function common_rememberme($user=NULL) {
if (!$user) {
- return false;
+ $user = common_current_user();
+ if (!$user) {
+ common_debug('No current user to remember', __FILE__);
+ return false;
+ }
}
$rm = new Remember_me();
$rm->code = common_good_rand(16);
$result = $rm->insert();
if (!$result) {
common_log_db_error($rm, 'INSERT', __FILE__);
+ common_debug('Error adding rememberme record for ' . $user->nickname, __FILE__);
return false;
}
+ common_log(LOG_INFO, 'adding rememberme cookie for ' . $user->nickname);
common_set_cookie(REMEMBERME,
implode(':', array($rm->user_id, $rm->code)),
time() + REMEMBERME_EXPIRY);
+ return true;
}
function common_remembered_user() {
common_log_db_error($rm, 'DELETE', __FILE__);
$user = NULL;
} else {
+ common_log(LOG_INFO, 'logging in ' . $user->nickname . ' using rememberme code ' . $rm->code);
common_set_user($user->nickname);
common_real_login(false);
# We issue a new cookie, so they can log in
# automatically again after this session
- common_rememberme();
+ common_rememberme($user);
}
}
}
}
function common_at_link($sender_id, $nickname) {
+ $sender = Profile::staticGet($sender_id);
+ $recipient = common_relative_profile($sender, $nickname);
+ if ($recipient) {
+ return '<a href="'.htmlspecialchars($recipient->profileurl).'" class="atlink">'.$nickname.'</a>';
+ } else {
+ return $nickname;
+ }
+}
+
+function common_relative_profile($sender, $nickname, $dt=NULL) {
# Try to find profiles this profile is subscribed to that have this nickname
$recipient = new Profile();
- # XXX: chokety and bad
+ # XXX: use a join instead of a subquery
$recipient->whereAdd('EXISTS (SELECT subscribed from subscription where subscriber = '.$sender_id.' and subscribed = id)', 'AND');
$recipient->whereAdd('nickname = "' . trim($nickname) . '"', 'AND');
if ($recipient->find(TRUE)) {
- return '<a href="'.htmlspecialchars($recipient->profileurl).'" class="atlink tolistenee">'.$nickname.'</a>';
+ # XXX: should probably differentiate between profiles with
+ # the same name by date of most recent update
+ return $recipient;
}
# Try to find profiles that listen to this profile and that have this nickname
$recipient = new Profile();
- # XXX: chokety and bad
+ # XXX: use a join instead of a subquery
$recipient->whereAdd('EXISTS (SELECT subscriber from subscription where subscribed = '.$sender_id.' and subscriber = id)', 'AND');
$recipient->whereAdd('nickname = "' . trim($nickname) . '"', 'AND');
if ($recipient->find(TRUE)) {
- return '<a href="'.htmlspecialchars($recipient->profileurl).'" class="atlink tolistener">'.$nickname.'</a>';
+ # XXX: should probably differentiate between profiles with
+ # the same name by date of most recent update
+ return $recipient;
}
# If this is a local user, try to find a local user with that nickname.
- $sender = User::staticGet($sender_id);
+ $sender = User::staticGet($sender->id);
if ($sender) {
$recipient_user = User::staticGet('nickname', $nickname);
if ($recipient_user) {
- return '<a href="'.htmlspecialchars(common_profile_url($nickname)).'" class="atlink usertouser">'.$nickname.'</a>';
+ return $recipient_user->getProfile();
}
}
# Otherwise, no links. @messages from local users to remote users,
# or from remote users to other remote users, are just
# outside our ability to make intelligent guesses about
- return $nickname;
+ return NULL;
}
// where should the avatar go for this user?
case 'openidsettings':
return common_path('settings/openid');
case 'newnotice':
- return common_path('notice/new');
+ if ($args && $args['replyto']) {
+ return common_path('notice/new?replyto='.$args['replyto']);
+ } else {
+ return common_path('notice/new');
+ }
case 'shownotice':
return common_path('notice/'.$args['notice']);
case 'xrds':
case 'subscriptions':
case 'subscribers':
case 'all':
+ case 'replies':
if ($args && $args['page']) {
return common_path($args['nickname'].'/'.$action.'?page=' . $args['page']);
} else {
}
case 'allrss':
return common_path($args['nickname'].'/all/rss');
+ case 'repliesrss':
+ return common_path($args['nickname'].'/replies/rss');
case 'userrss':
return common_path($args['nickname'].'/rss');
case 'showstream':
common_end_xml();
}
+function common_save_replies($notice) {
+ # extract all @messages
+ $cnt = preg_match_all('/(?:^|\s)@([a-z0-9]{1,64})/', $notice->content, $match);
+ if (!$cnt) {
+ return true;
+ }
+ $sender = Profile::staticGet($notice->profile_id);
+ # store replied only for first @ (what user/notice what the reply directed,
+ # we assume first @ is it)
+ for ($i=0; $i<count($match[1]); $i++) {
+ $nickname = $match[1][$i];
+ $recipient = common_relative_profile($sender, $nickname, $notice->created);
+ if (!$recipient) {
+ continue;
+ }
+ if ($i == 0) {
+ $reply_for = $recipient;
+ }
+ $reply = new Reply();
+ $reply->notice_id = $notice->id;
+ $reply->profile_id = $recipient->id;
+ if ($reply_for) {
+# $recipient_notice = $reply_for->getCurrentNotice($notice->created);
+ $recipient_notice = $reply_for->getCurrentNotice();
+ $reply->replied_id = $recipient_notice->id;
+ }
+ $id = $reply->insert();
+ if (!$id) {
+ $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
+ common_log(LOG_ERROR, 'DB error inserting reply: ' . $last_error->message);
+ common_server_error('DB error inserting reply: ' . $last_error->message);
+ return;
+ }
+ }
+}
+
function common_broadcast_notice($notice, $remote=false) {
if (common_config('queue', 'enabled')) {
# Do it later!
- common_enqueue_notice($notice);
+ return common_enqueue_notice($notice);
} else {
- common_real_broadcast($notice, $remote);
+ return common_real_broadcast($notice, $remote);
}
}
function common_enqueue_notice($notice) {
$qi = new Queue_item();
- $qi->query('BEGIN');
$qi->notice_id = $notice->id;
- $qi->created = DB_DataObject_Cast::dateTime();
- $result = $qi->insert();
- $qi->query('COMMIT');
+ $qi->created = $notice->created;
+ $result = $qi->insert();
+ if (!$result) {
+ $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
+ common_log(LOG_ERROR, 'DB error inserting queue item: ' . $last_error->message);
+ return false;
+ }
+ common_log(LOG_DEBUG, 'complete queueing notice ID = ' . $notice->id);
return $result;
}
function common_real_broadcast($notice, $remote=false) {
- // XXX: optionally use a queue system like http://code.google.com/p/microapps/wiki/NQDQ
+ $success = true;
if (!$remote) {
# Make sure we have the OMB stuff
require_once(INSTALLDIR.'/lib/omb.php');
- omb_broadcast_remote_subscribers($notice);
+ $success = omb_broadcast_remote_subscribers($notice);
+ if (!$success) {
+ common_log(LOG_ERROR, 'Error in OMB broadcast for notice ' . $notice->id);
+ }
+ }
+ if ($success) {
+ require_once(INSTALLDIR.'/lib/jabber.php');
+ $success = jabber_broadcast_notice($notice);
+ if (!$success) {
+ common_log(LOG_ERROR, 'Error in jabber broadcast for notice ' . $notice->id);
+ }
}
- require_once(INSTALLDIR.'/lib/jabber.php');
- jabber_broadcast_notice($notice);
// XXX: broadcast notices to SMS
// XXX: broadcast notices to other IM
- return true;
+ return $success;
}
function common_broadcast_profile($profile) {
function common_log_db_error(&$object, $verb, $filename=NULL) {
$objstr = common_log_objstring($object);
$last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
- common_log(LOG_ERROR, $last_error->message . '(' . $verb . ' on ' . $objstr . ')', $filename);
+ common_log(LOG_ERR, $last_error->message . '(' . $verb . ' on ' . $objstr . ')', $filename);
}
function common_log_objstring(&$object) {