X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=lib%2Futil.php;h=f4245693fb8f7499dc22d5c952c3ee87068fe01a;hb=67921b1c1871e881f71f8f15aa7fc9c2b11da64d;hp=e865a2b4179ed997325809540b81e3f03f00eeb6;hpb=a9cf27c4e69b8ab3f2f276af6e8459f00a30367f;p=quix0rs-gnu-social.git diff --git a/lib/util.php b/lib/util.php index e865a2b417..f4245693fb 100644 --- a/lib/util.php +++ b/lib/util.php @@ -44,7 +44,7 @@ function common_server_error($msg, $code=500) { } # Show a user error -function common_client_error($msg, $code=400) { +function common_user_error($msg, $code=400) { static $status = array(400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', @@ -93,14 +93,22 @@ function common_element_start($tag, $attrs=NULL) { } function common_element_end($tag) { + static $empty_tag = array('base', 'meta', 'link', 'hr', + 'br', 'param', 'img', 'area', + 'input', 'col'); global $xw; - $xw->endElement(); + # 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); + common_element_start($tag, $attrs); + global $xw; if ($content) { - global $xw; $xw->text($content); } common_element_end($tag); @@ -123,7 +131,7 @@ function common_end_xml() { $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; @@ -136,7 +144,7 @@ function common_show_header($pagetitle, $callable=NULL, $data=NULL, $headercall= common_accept_to_prefs(PAGE_TYPE_PREFS)); if (!$type) { - common_client_error(_t('This page is not available in a media type you accept'), 406); + common_user_error(_('This page is not available in a media type you accept'), 406); exit(0); } @@ -170,6 +178,9 @@ function common_show_header($pagetitle, $callable=NULL, $data=NULL, $headercall= common_element('script', array('type' => 'text/javascript', 'src' => common_path('js/jquery.min.js')), ' '); + common_element('script', array('type' => 'text/javascript', + 'src' => common_path('js/util.js')), + ' '); if ($callable) { if ($data) { @@ -217,26 +228,30 @@ function common_show_footer() { common_element_end('div'); # content div common_foot_menu(); common_element_start('div', array('id' => 'footer')); - common_element_start('p', 'laconica'); - common_text(_t('This site is running the ')); - common_element('a', array('class' => 'software', - href => 'http://laconi.ca/'), - 'Laconica'); - common_text(_t('microblogging tool, version ' . LACONICA_VERSION . ', available under the ')); - common_element('a', array(href => 'http://www.fsf.org/licensing/licenses/agpl-3.0.html'), - 'GNU Affero General Public License'); - common_text(_t('.')); - common_element_end('p'); + common_element_start('div', 'laconica'); + if (common_config('site', 'broughtby')) { + $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). '); + } else { + $instr = _('**%%site.name%%** is a microblogging service. '); + } + $instr .= sprintf(_('It runs the [Laconica](http://laconi.ca/) ' . + 'microblogging software, version %s, ' . + 'available under the ' . + '[GNU Affero General Public License]' . + '(http://www.fsf.org/licensing/licenses/agpl-3.0.html).'), LACONICA_VERSION); + $output = common_markup_to_html($instr); + common_raw($output); + common_element_end('div'); common_element('img', array('id' => 'cc', 'src' => $config['license']['image'], 'alt' => $config['license']['title'])); common_element_start('p'); - common_text(_t('Unless otherwise specified, contents of this site are copyright by the contributors and available under the ')); + common_text(_('Unless otherwise specified, contents of this site are copyright by the contributors and available under the ')); common_element('a', array('class' => 'license', 'rel' => 'license', href => $config['license']['url']), $config['license']['title']); - common_text(_t('. Contributors should be attributed by full name or nickname.')); + common_text(_('. Contributors should be attributed by full name or nickname.')); common_element_end('p'); common_element_end('div'); common_element_end('div'); @@ -260,20 +275,20 @@ function common_nav_menu() { common_element_start('ul', array('id' => 'nav')); if ($user) { common_menu_item(common_local_url('all', array('nickname' => $user->nickname)), - _t('Home')); + _('Home')); } common_menu_item(common_local_url('public'), _t('Public')); common_menu_item(common_local_url('doc', array('title' => 'help')), - _t('Help')); + _('Help')); if ($user) { common_menu_item(common_local_url('profilesettings'), - _t('Settings')); + _('Settings')); common_menu_item(common_local_url('logout'), - _t('Logout')); + _('Logout')); } else { - common_menu_item(common_local_url('login'), _t('Login')); - common_menu_item(common_local_url('register'), _t('Register')); - common_menu_item(common_local_url('openidlogin'), _t('OpenID')); + common_menu_item(common_local_url('login'), _('Login')); + common_menu_item(common_local_url('register'), _('Register')); + common_menu_item(common_local_url('openidlogin'), _('OpenID')); } common_element_end('ul'); } @@ -281,11 +296,15 @@ function common_nav_menu() { function common_foot_menu() { common_element_start('ul', array('id' => 'nav_sub')); common_menu_item(common_local_url('doc', array('title' => 'about')), - _t('About')); + _('About')); + common_menu_item(common_local_url('doc', array('title' => 'faq')), + _('FAQ')); common_menu_item(common_local_url('doc', array('title' => 'privacy')), - _t('Privacy')); + _('Privacy')); common_menu_item(common_local_url('doc', array('title' => 'source')), - _t('Source')); + _('Source')); + common_menu_item(common_local_url('doc', array('title' => 'contact')), + _('Contact')); common_element_end('ul'); } @@ -308,6 +327,7 @@ function common_input($id, $label, $value=NULL,$instructions=NULL) { common_element('label', array('for' => $id), $label); $attrs = array('name' => $id, 'type' => 'text', + 'class' => 'input_text', 'id' => $id); if ($value) { $attrs['value'] = htmlspecialchars($value); @@ -324,6 +344,7 @@ function common_checkbox($id, $label, $checked=false, $instructions=NULL, $value common_element_start('p'); $attrs = array('name' => $id, 'type' => 'checkbox', + 'class' => 'checkbox', 'id' => $id); if ($value) { $attrs['value'] = htmlspecialchars($value); @@ -354,6 +375,7 @@ function common_password($id, $label, $instructions=NULL) { common_element('label', array('for' => $id), $label); $attrs = array('name' => $id, 'type' => 'password', + 'class' => 'password', 'id' => $id); common_element('input', $attrs); if ($instructions) { @@ -380,7 +402,7 @@ function common_textarea($id, $label, $content=NULL, $instructions=NULL) { 'cols' => 40, 'name' => $id, 'id' => $id), - ($content) ? $content : ' '); + ($content) ? $content : ''); if ($instructions) { common_element('span', 'input_instructions', $instructions); } @@ -399,8 +421,12 @@ function common_check_user($nickname, $password) { 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; + } } } @@ -419,19 +445,26 @@ function common_ensure_session() { } } -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; } @@ -455,10 +488,13 @@ function common_set_cookie($key, $value, $expiration=0) { 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); @@ -466,11 +502,14 @@ function common_rememberme() { $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() { @@ -490,11 +529,12 @@ 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); } } } @@ -564,34 +604,48 @@ function common_render_content($text, $notice) { } function common_at_link($sender_id, $nickname) { + $sender = Profile::staticGet($sender_id); + $recipient = common_relative_profile($sender, $nickname); + if ($recipient) { + return ''.$nickname.''; + } 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 ''.$nickname.''; + # 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 ''.$nickname.''; + # 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 ''.$nickname.''; + 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? @@ -615,6 +669,15 @@ function common_avatar_url($filename) { return common_path('avatar/'.$filename); } +function common_avatar_display_url($avatar) { + $server = common_config('avatar', 'server'); + if ($server) { + return 'http://'.$server.'/'.$avatar->filename; + } else { + return $avatar->url; + } +} + function common_default_avatar($size) { static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile', AVATAR_STREAM_SIZE => 'stream', @@ -651,6 +714,12 @@ function common_fancy_url($action, $args=NULL) { case 'subscribe': case 'unsubscribe': return common_path('main/'.$action); + case 'remotesubscribe': + if ($args && $args['nickname']) { + return common_path('main/remote?nickname=' . $args['nickname']); + } else { + return common_path('main/remote'); + } case 'openidlogin': return common_path('main/openid'); case 'avatar': @@ -661,7 +730,11 @@ function common_fancy_url($action, $args=NULL) { 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': @@ -670,6 +743,7 @@ function common_fancy_url($action, $args=NULL) { case 'subscriptions': case 'subscribers': case 'all': + case 'replies': if ($args && $args['page']) { return common_path($args['nickname'].'/'.$action.'?page=' . $args['page']); } else { @@ -677,6 +751,8 @@ function common_fancy_url($action, $args=NULL) { } 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': @@ -697,6 +773,14 @@ function common_fancy_url($action, $args=NULL) { return common_path($path); case 'imsettings': return common_path('settings/im'); + case 'peoplesearch': + return common_path('search/people' . (($args) ? ('?' . http_build_query($args)) : '')); + case 'noticesearch': + return common_path('search/notice' . (($args) ? ('?' . http_build_query($args)) : '')); + case 'noticesearchrss': + return common_path('search/notice/rss' . (($args) ? ('?' . http_build_query($args)) : '')); + case 'avatarbynickname': + return common_path($args['nickname'].'/avatar/'.$args['size']); default: return common_simple_url($action, $args); } @@ -723,7 +807,40 @@ function common_path($relative) { function common_date_string($dt) { // XXX: do some sexy date formatting // return date(DATE_RFC822, $dt); - return $dt; + $t = strtotime($dt); + $now = time(); + $diff = $now - $t; + + if ($now < $t) { # that shouldn't happen! + return common_exact_date($dt); + } else if ($diff < 60) { + return _('a few seconds ago'); + } else if ($diff < 92) { + return _('about a minute ago'); + } else if ($diff < 3300) { + return sprintf(_('about %d minutes ago'), round($diff/60)); + } else if ($diff < 5400) { + return _('about an hour ago'); + } else if ($diff < 22 * 3600) { + return sprintf(_('about %d hours ago'), round($diff/3600)); + } else if ($diff < 37 * 3600) { + return _('about a day ago'); + } else if ($diff < 24 * 24 * 3600) { + return sprintf(_('about %d days ago'), round($diff/(24*3600))); + } else if ($diff < 46 * 24 * 3600) { + return _('about a month ago'); + } else if ($diff < 330 * 24 * 3600) { + return sprintf(_('about %d months ago'), round($diff/(30*24*3600))); + } else if ($diff < 480 * 24 * 3600) { + return _('about a year ago'); + } else { + return common_exact_date($dt); + } +} + +function common_exact_date($dt) { + $t = strtotime($dt); + return date(DATE_RFC850, $t); } function common_date_w3dtf($dt) { @@ -746,18 +863,86 @@ function common_redirect($url, $code=307) { 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; $icreated); + if (!$recipient) { + continue; + } + if ($i == 0 && ($recipient->id != $sender->id)) { # Don't save reply to self + $reply_for = $recipient; + $recipient_notice = $reply_for->getCurrentNotice(); + $orig = clone($notice); + $notice->reply_to = $recipient_notice->id; + $notice->update($orig); + } + $reply = new Reply(); + $reply->notice_id = $notice->id; + $reply->profile_id = $recipient->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: %s', $last_error->message)); + return; + } + } +} + function common_broadcast_notice($notice, $remote=false) { - // XXX: optionally use a queue system like http://code.google.com/p/microapps/wiki/NQDQ + if (common_config('queue', 'enabled')) { + # Do it later! + return common_enqueue_notice($notice); + } else { + return common_real_broadcast($notice, $remote); + } +} + +# Stick the notice on the queue + +function common_enqueue_notice($notice) { + $qi = new Queue_item(); + $qi->notice_id = $notice->id; + $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) { + $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) { @@ -778,22 +963,25 @@ function common_notice_form($action=NULL, $content=NULL) { $user = common_current_user(); assert(!is_null($user)); common_element_start('form', array('id' => 'status_form', - 'method' => 'POST', + 'method' => 'post', 'action' => common_local_url('newnotice'))); common_element_start('p'); - common_element('label', array('for' => 'status_update', + common_element('label', array('for' => 'status_textarea', 'id' => 'status_label'), - _t('What\'s up, ').$user->nickname.'?'); + sprintf(_('What\'s up, %s?'), $user->nickname)); + common_element('span', array('id' => 'counter', 'class' => 'counter'), '140'); common_element('textarea', array('id' => 'status_textarea', + 'cols' => 60, + 'rows' => 3, 'name' => 'status_textarea'), - ($content) ? $content : ' '); + ($content) ? $content : ''); if ($action) { common_hidden('returnto', $action); } common_element('input', array('id' => 'status_submit', 'name' => 'status_submit', 'type' => 'submit', - 'value' => _t('Send'))); + 'value' => _('Send'))); common_element_end('p'); common_element_end('form'); } @@ -857,12 +1045,6 @@ function common_timestamp() { return date('YmdHis'); } -// XXX: set up gettext - -function _t($str) { - return $str; -} - function common_ensure_syslog() { static $initialized = false; if (!$initialized) { @@ -874,8 +1056,20 @@ function common_ensure_syslog() { } function common_log($priority, $msg, $filename=NULL) { - common_ensure_syslog(); - syslog($priority, $msg); + $logfile = common_config('site', 'logfile'); + if ($logfile) { + $log = fopen($logfile, "a"); + if ($log) { + static $syslog_priorities = array('LOG_EMERG', 'LOG_ALERT', 'LOG_CRIT', 'LOG_ERR', + 'LOG_WARNING', 'LOG_NOTICE', 'LOG_INFO', 'LOG_DEBUG'); + $output = date('Y-m-d H:i:s') . ' ' . $syslog_priorities[$priority] . ': ' . $msg . "\n"; + fwrite($log, $output); + fclose($log); + } + } else { + common_ensure_syslog(); + syslog($priority, $msg); + } } function common_debug($msg, $filename=NULL) { @@ -889,7 +1083,7 @@ function common_debug($msg, $filename=NULL) { 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) { @@ -932,7 +1126,7 @@ function common_pagination($have_before, $have_after, $page, $action, $args=NULL common_element_start('li', 'before'); common_element('a', array('href' => common_local_url($action, $newargs)), - _t('« After')); + _('« After')); common_element_end('li'); } @@ -941,7 +1135,7 @@ function common_pagination($have_before, $have_after, $page, $action, $args=NULL $newargs = ($args) ? array_merge($args,$pargs) : $pargs; common_element_start('li', 'after'); common_element('a', array('href' => common_local_url($action, $newargs)), - _t('Before »')); + _('Before »')); common_element_end('li'); } @@ -1062,7 +1256,18 @@ function common_confirmation_code($bits) { for ($i = 0; $i < $chars; $i++) { # XXX: convert to string and back $num = hexdec(common_good_rand(1)); + # XXX: randomness is too precious to throw away almost + # 40% of the bits we get! $code .= $codechars[$num%32]; } return $code; } + +# convert markup to HTML + +function common_markup_to_html($c) { + $c = preg_replace('/%%action.(\w+)%%/e', "common_local_url('\\1')", $c); + $c = preg_replace('/%%doc.(\w+)%%/e', "common_local_url('doc', array('title'=>'\\1'))", $c); + $c = preg_replace('/%%(\w+).(\w+)%%/e', 'common_config(\'\\1\', \'\\2\')', $c); + return Markdown($c); +}