]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/util.php
require sms carrier
[quix0rs-gnu-social.git] / lib / util.php
index f4245693fb8f7499dc22d5c952c3ee87068fe01a..6f6f1dd249121be3f5ccc34a3ebd539a48a105df 100644 (file)
@@ -95,7 +95,7 @@ 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'); 
+                                                         'input', 'col');
        global $xw;
        # XXX: check namespace
        if (in_array($tag, $empty_tag)) {
@@ -194,11 +194,11 @@ function common_show_header($pagetitle, $callable=NULL, $data=NULL, $headercall=
        common_element_start('div', array('id' => 'wrap'));
        common_element_start('div', array('id' => 'header'));
        common_nav_menu();
-       if ((is_string($config['site']['logo']) && (strlen($config['site']['logo']) > 0))
+       if ((isset($config['site']['logo']) && is_string($config['site']['logo']) && (strlen($config['site']['logo']) > 0))
                || file_exists(theme_file('logo.png')))
        {
                common_element_start('a', array('href' => common_local_url('public')));
-               common_element('img', array('src' => ($config['site']['logo']) ?
+               common_element('img', array('src' => isset($config['site']['logo']) ?
                                                                        ($config['site']['logo']) : theme_path('logo.png'),
                                                                        'alt' => $config['site']['name'],
                                                                        'id' => 'logo'));
@@ -223,6 +223,14 @@ function common_show_header($pagetitle, $callable=NULL, $data=NULL, $headercall=
        common_element_start('div', array('id' => 'content'));
 }
 
+# XXX: Refactor w/common_user_error() ?
+function common_show_basic_auth_error() {
+       header('HTTP/1.1 401 Unauthorized');
+       header('Content-type: text/plain');
+       print("Could not authenticate you."); # exactly what Twitter says - no \n
+       exit();
+}
+
 function common_show_footer() {
        global $xw, $config;
        common_element_end('div'); # content div
@@ -234,11 +242,7 @@ function common_show_footer() {
        } 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);
+       $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');
@@ -249,7 +253,7 @@ function common_show_footer() {
        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']),
+                                                         'href' => $config['license']['url']),
                                   $config['license']['title']);
        common_text(_('. Contributors should be attributed by full name or nickname.'));
        common_element_end('p');
@@ -277,7 +281,8 @@ function common_nav_menu() {
                common_menu_item(common_local_url('all', array('nickname' => $user->nickname)),
                                                 _('Home'));
        }
-       common_menu_item(common_local_url('public'), _t('Public'));
+       common_menu_item(common_local_url('public'), _('Public'));
+       common_menu_item(common_local_url('peoplesearch'), _('Search'));
        common_menu_item(common_local_url('doc', array('title' => 'help')),
                                         _('Help'));
        if ($user) {
@@ -460,7 +465,7 @@ function common_set_user($user) {
        } else if (!($user instanceof User)) {
                return false;
        }
-       
+
        if ($user) {
                common_ensure_session();
                $_SESSION['userid'] = $user->id;
@@ -551,7 +556,7 @@ function common_forgetme() {
 
 # who is the current user?
 function common_current_user() {
-       if ($_REQUEST[session_name()]) {
+       if ($_REQUEST[session_name()] || $_SESSION && $_SESSION['userid']) {
                common_ensure_session();
                $id = $_SESSION['userid'];
                if ($id) {
@@ -562,6 +567,11 @@ function common_current_user() {
        }
        # that didn't work; try to remember
        $user = common_remembered_user();
+       common_debug("Got User " . $user->nickname);
+       if ($user) {
+           common_debug("Faking session on remembered user");
+           $_SESSION['userid'] = $user->id;
+       }
        return $user;
 }
 
@@ -596,8 +606,9 @@ define('URL_REGEX', '^|[ \t\r\n])((ftp|http|https|gopher|mailto|news|nntp|telnet
 function common_render_content($text, $notice) {
        $r = htmlspecialchars($text);
        $id = $notice->profile_id;
-       $r = preg_replace('@https?://\S+@', '<a href="\0" class="extlink">\0</a>', $r);
+       $r = preg_replace('@https?://[^)\]>\s]+@', '<a href="\0" class="extlink">\0</a>', $r);
        $r = preg_replace('/(^|\s+)@([a-z0-9]{1,64})/e', "'\\1@'.common_at_link($id, '\\2')", $r);
+       $r = preg_replace('/^T ([A-Z0-9]{1,64}) /e', "'T '.common_at_link($id, '\\1').' '", $r);
        # XXX: # tags
        # XXX: machine tags
        return $r;
@@ -697,7 +708,7 @@ function common_local_url($action, $args=NULL) {
 function common_fancy_url($action, $args=NULL) {
        switch (strtolower($action)) {
         case 'public':
-               if ($args && $args['page']) {
+               if ($args && isset($args['page'])) {
                        return common_path('?page=' . $args['page']);
                } else {
                        return common_path('');
@@ -727,6 +738,8 @@ function common_fancy_url($action, $args=NULL) {
                return common_path('settings/'.$action);
         case 'profilesettings':
                return common_path('settings/profile');
+        case 'emailsettings':
+               return common_path('settings/email');
         case 'openidsettings':
                return common_path('settings/openid');
         case 'newnotice':
@@ -744,7 +757,7 @@ function common_fancy_url($action, $args=NULL) {
         case 'subscribers':
         case 'all':
         case 'replies':
-               if ($args && $args['page']) {
+               if ($args && isset($args['page'])) {
                        return common_path($args['nickname'].'/'.$action.'?page=' . $args['page']);
                } else {
                        return common_path($args['nickname'].'/'.$action);
@@ -756,7 +769,7 @@ function common_fancy_url($action, $args=NULL) {
         case 'userrss':
                return common_path($args['nickname'].'/rss');
         case 'showstream':
-               if ($args && $args['page']) {
+               if ($args && isset($args['page'])) {
                        return common_path($args['nickname'].'?page=' . $args['page']);
                } else {
                        return common_path($args['nickname']);
@@ -848,6 +861,16 @@ function common_date_w3dtf($dt) {
        return date(DATE_W3C, $t);
 }
 
+function common_date_rfc2822($dt) {
+       $t = strtotime($dt);
+       return date("r", $t);   
+}
+
+function common_date_iso8601($dt) {
+       $t = strtotime($dt);
+       return date("c", $t);   
+}
+
 function common_redirect($url, $code=307) {
        static $status = array(301 => "Moved Permanently",
                                                   302 => "Found",
@@ -861,19 +884,27 @@ function common_redirect($url, $code=307) {
                                         'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
        common_element('a', array('href' => $url), $url);
        common_end_xml();
+    exit;
 }
 
 function common_save_replies($notice) {
+       # Alternative reply format
+       $tname = false;
+       if (preg_match('/^T ([A-Z0-9]{1,64}) /', $notice->content, $match)) {
+               $tname = $match[1];
+       }
        # extract all @messages
        $cnt = preg_match_all('/(?:^|\s)@([a-z0-9]{1,64})/', $notice->content, $match);
-       if (!$cnt) {
+       if (!$cnt && !$tname) {
                return true;
        }
+       # XXX: is there another way to make an array copy?
+       $names = ($tname) ? array_unique(array_merge(array(strtolower($tname)), $match[1])) : array_unique($match[1]);
        $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];
+       for ($i=0; $i<count($names); $i++) {
+               $nickname = $names[$i];
                $recipient = common_relative_profile($sender, $nickname, $notice->created);
                if (!$recipient) {
                        continue;
@@ -891,8 +922,8 @@ function common_save_replies($notice) {
                $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));
+                       common_log(LOG_ERR, 'DB error inserting reply: ' . $last_error->message);
+                       common_server_error(sprintf(_('DB error inserting reply: %s'), $last_error->message));
                        return;
                }
        }
@@ -916,7 +947,7 @@ function common_enqueue_notice($notice) {
         $result = $qi->insert();
        if (!$result) {
            $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
-           common_log(LOG_ERROR, 'DB error inserting queue item: ' . $last_error->message);
+           common_log(LOG_ERR, 'DB error inserting queue item: ' . $last_error->message);
            return false;
        }
        common_log(LOG_DEBUG, 'complete queueing notice ID = ' . $notice->id);
@@ -930,14 +961,14 @@ function common_real_broadcast($notice, $remote=false) {
                require_once(INSTALLDIR.'/lib/omb.php');
                $success = omb_broadcast_remote_subscribers($notice);
                if (!$success) {
-                       common_log(LOG_ERROR, 'Error in OMB broadcast for notice ' . $notice->id);
+                       common_log(LOG_ERR, '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);
+                       common_log(LOG_ERR, 'Error in jabber broadcast for notice ' . $notice->id);
                }
        }
        // XXX: broadcast notices to SMS
@@ -986,13 +1017,6 @@ function common_notice_form($action=NULL, $content=NULL) {
        common_element_end('form');
 }
 
-function common_mint_tag($extra) {
-       global $config;
-       return
-         'tag:'.$config['tag']['authority'].','.
-         $config['tag']['date'].':'.$config['tag']['prefix'].$extra;
-}
-
 # Should make up a reasonable root URL
 
 function common_root_url() {
@@ -1049,7 +1073,6 @@ function common_ensure_syslog() {
        static $initialized = false;
        if (!$initialized) {
                global $config;
-               define_syslog_variables();
                openlog($config['syslog']['appname'], 0, LOG_USER);
                $initialized = true;
        }
@@ -1060,7 +1083,7 @@ function common_log($priority, $msg, $filename=NULL) {
        if ($logfile) {
                $log = fopen($logfile, "a");
                if ($log) {
-                       static $syslog_priorities = array('LOG_EMERG', 'LOG_ALERT', 'LOG_CRIT', 'LOG_ERR', 
+                       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);
@@ -1271,3 +1294,35 @@ function common_markup_to_html($c) {
        $c = preg_replace('/%%(\w+).(\w+)%%/e', 'common_config(\'\\1\', \'\\2\')', $c);
        return Markdown($c);
 }
+
+function common_profile_avatar_url($profile, $size=AVATAR_PROFILE_SIZE) {
+       $avatar = $profile->getAvatar($size);
+       if ($avatar) {
+               return common_avatar_display_url($avatar);
+       } else {
+               return common_default_avatar($size);
+       }
+}
+
+function common_profile_uri($profile) {
+       if (!$profile) {
+               return NULL;
+       }
+       $user = User::staticGet($profile->id);
+       if ($user) {
+               return $user->uri;
+       }
+       
+       $remote = Remote_profile::staticGet($profile->id);
+       if ($remote) {
+               return $remote->uri;
+       }
+       # XXX: this is a very bad profile!
+       return NULL;
+}
+
+function common_canonical_sms($sms) {
+       # strip non-digits
+       preg_replace('/\D/', '', $sms);
+       return $sms;
+}
\ No newline at end of file