]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/util.php
allow doc and api calls from private
[quix0rs-gnu-social.git] / lib / util.php
index 2484ae6b6d6e565d80aebc97426652c8494849ea..eb5667f6a9572cafcea3b1578e76d01b4be3c92f 100644 (file)
@@ -401,9 +401,8 @@ function common_checkbox($id, $label, $checked=false, $instructions=NULL, $value
                $attrs['disabled'] = 'true';
        }
        common_element('input', $attrs);
-       # XXX: use a <label>
        common_text(' ');
-       common_element('span', 'checkbox_label', $label);
+       common_element('label', array('class' => 'checkbox_label', 'for' => $id), $label);
        common_text(' ');
        if ($instructions) {
                common_element('span', 'input_instructions', $instructions);
@@ -602,7 +601,7 @@ function common_set_cookie($key, $value, $expiration=0) {
 }
 
 define('REMEMBERME', 'rememberme');
-define('REMEMBERME_EXPIRY', 30 * 24 * 60 * 60);
+define('REMEMBERME_EXPIRY', 30 * 24 * 60 * 60); # 30 days
 
 function common_rememberme($user=NULL) {
        if (!$user) {
@@ -612,19 +611,34 @@ function common_rememberme($user=NULL) {
                        return false;
                }
        }
+
        $rm = new Remember_me();
+
        $rm->code = common_good_rand(16);
        $rm->user_id = $user->id;
+
+    # Wrap the insert in some good ol' fashioned transaction code
+
+    $rm->query('BEGIN');
+
        $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);
+    }
+
+    $rm->query('COMMIT');
+
+       common_debug('Inserted rememberme record (' . $rm->code . ', ' . $rm->user_id . '); result = ' . $result . '.', __FILE__);
+
+    $cookieval = $rm->user_id . ':' . $rm->code;
+
+       common_log(LOG_INFO, 'adding rememberme cookie "' . $cookieval . '" for ' . $user->nickname);
+
+       common_set_cookie(REMEMBERME, $cookieval, time() + REMEMBERME_EXPIRY);
+
        return true;
 }
 
@@ -807,7 +821,13 @@ function common_render_uri_thingy($matches) {
        return '<a href="' . $uri . '"' . $title . ' class="extlink">' . $uri . '</a>' . $trailer;
 }
 
-function common_longurl($uri)  {
+function common_longurl($short_url)  {
+    $long_url = common_shorten_link($short_url, true);
+    if ($long_url === $short_url) return false;
+    return $long_url;
+}
+
+function common_longurl2($uri)  {
        $uri_e = urlencode($uri);
        $longurl = unserialize(file_get_contents("http://api.longurl.org/v1/expand?format=php&url=$uri_e"));
        if (empty($longurl['long_url']) || $uri === $longurl['long_url']) return false;
@@ -815,13 +835,16 @@ function common_longurl($uri)  {
 }
 
 function common_shorten_links($text) {
+    if (mb_strlen($text) <= 140) return $text;
+    static $cache = array();
+    if (isset($cache[$text])) return $cache[$text];
     // \s = not a horizontal whitespace character (since PHP 5.2.4)
-       // RYM this should prevent * preceded URLs from being processed but it its a char
-//     $r = preg_replace('@[^*](https?://[^)\]>\s]+)@e', "common_shorten_link('\\1')", $r);
-       return preg_replace('@https?://[^)\]>\s]+@e', "common_shorten_link('\\0')", $text);
+       return $cache[$text] = preg_replace('@https?://[^)\]>\s]+@e', "common_shorten_link('\\0')", $text);
 }
 
-function common_shorten_link($long_url) {
+function common_shorten_link($url, $reverse = false) {
+       static $url_cache = array();
+    if ($reverse) return isset($url_cache[$url]) ? $url_cache[$url] : $url;
 
        $user = common_current_user();
 
@@ -833,38 +856,38 @@ function common_shorten_link($long_url) {
        switch($user->urlshorteningservice) {
         case 'ur1.ca':
             $short_url_service = new LilUrl;
-            $short_url = $short_url_service->shorten($long_url);
+            $short_url = $short_url_service->shorten($url);
             break;
 
         case '2tu.us':
             $short_url_service = new TightUrl;
-            $short_url = $short_url_service->shorten($long_url);
+            $short_url = $short_url_service->shorten($url);
             break;
 
         case 'ptiturl.com':
             $short_url_service = new PtitUrl;
-            $short_url = $short_url_service->shorten($long_url);
+            $short_url = $short_url_service->shorten($url);
             break;
 
         case 'bit.ly':
-                       curl_setopt($curlh, CURLOPT_URL, 'http://bit.ly/api?method=shorten&long_url='.urlencode($long_url));
+                       curl_setopt($curlh, CURLOPT_URL, 'http://bit.ly/api?method=shorten&long_url='.urlencode($url));
                        $short_url = current(json_decode(curl_exec($curlh))->results)->hashUrl;
             break;
 
                case 'is.gd':
-                       curl_setopt($curlh, CURLOPT_URL, 'http://is.gd/api.php?longurl='.urlencode($long_url));
+                       curl_setopt($curlh, CURLOPT_URL, 'http://is.gd/api.php?longurl='.urlencode($url));
                        $short_url = curl_exec($curlh);
                        break;
                case 'snipr.com':
-                       curl_setopt($curlh, CURLOPT_URL, 'http://snipr.com/site/snip?r=simple&link='.urlencode($long_url));
+                       curl_setopt($curlh, CURLOPT_URL, 'http://snipr.com/site/snip?r=simple&link='.urlencode($url));
                        $short_url = curl_exec($curlh);
                        break;
                case 'metamark.net':
-                       curl_setopt($curlh, CURLOPT_URL, 'http://metamark.net/api/rest/simple?long_url='.urlencode($long_url));
+                       curl_setopt($curlh, CURLOPT_URL, 'http://metamark.net/api/rest/simple?long_url='.urlencode($url));
                        $short_url = curl_exec($curlh);
                        break;
                case 'tinyurl.com':
-                       curl_setopt($curlh, CURLOPT_URL, 'http://tinyurl.com/api-create.php?url='.urlencode($long_url));
+                       curl_setopt($curlh, CURLOPT_URL, 'http://tinyurl.com/api-create.php?url='.urlencode($url));
                        $short_url = curl_exec($curlh);
                        break;
                default:
@@ -874,9 +897,10 @@ function common_shorten_link($long_url) {
        curl_close($curlh);
 
        if ($short_url) {
-               return $short_url;
+        $url_cache[(string)$short_url] = $url;
+               return (string)$short_url;
        }
-       return $long_url;
+       return $url;
 }
 
 function common_xml_safe_str($str) {
@@ -904,9 +928,9 @@ function common_at_link($sender_id, $nickname) {
        $sender = Profile::staticGet($sender_id);
        $recipient = common_relative_profile($sender, common_canonical_nickname($nickname));
        if ($recipient) {
-               return '<a href="'.htmlspecialchars($recipient->profileurl).'" class="atlink">'.$nickname.'</a>';
+               return '<span class="vcard"><a class="fn nickname url" rel="reply" href="'.htmlspecialchars($recipient->profileurl).'" class="atlink">'.$nickname.'</a></span>';
        } else {
-               return $nickname;
+               return '<span class="vcard"> <span class="fn nickname">'.$nickname.'</span> </span>';
        }
 }
 
@@ -1129,6 +1153,8 @@ function common_fancy_url($action, $args=NULL) {
         case 'repliesrss':
                return common_path($args['nickname'].'/replies/rss');
         case 'userrss':
+        if (isset($args['limit']))
+                   return common_path($args['nickname'].'/rss?limit=' . $args['limit']);
                return common_path($args['nickname'].'/rss');
         case 'showstream':
                if ($args && isset($args['page'])) {
@@ -1727,7 +1753,7 @@ function common_pagination($have_before, $have_after, $page, $action, $args=NULL
                $newargs = ($args) ? array_merge($args,$pargs) : $pargs;
 
                common_element_start('li', 'before');
-               common_element('a', array('href' => common_local_url($action, $newargs)),
+               common_element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'prev'),
                                           _('« After'));
                common_element_end('li');
        }
@@ -1736,7 +1762,7 @@ function common_pagination($have_before, $have_after, $page, $action, $args=NULL
                $pargs = array('page' => $page+1);
                $newargs = ($args) ? array_merge($args,$pargs) : $pargs;
                common_element_start('li', 'after');
-               common_element('a', array('href' => common_local_url($action, $newargs)),
+               common_element('a', array('href' => common_local_url($action, $newargs), 'rel' => 'next'),
                                                   _('Before »'));
                common_element_end('li');
        }
@@ -2033,10 +2059,10 @@ function common_unsubscribe_form($profile) {
                                                                           'class' => 'unsubscribe',
                                                                           'action' => common_local_url('unsubscribe')));
        common_hidden('token', common_session_token());
-       common_element('input', array('id' => 'unsubscribeto-' . $profile->nickname,
+       common_element('input', array('id' => 'unsubscribeto-' . $profile->id,
                                                                  'name' => 'unsubscribeto',
                                                                  'type' => 'hidden',
-                                                                 'value' => $profile->nickname));
+                                                                 'value' => $profile->id));
        common_element('input', array('type' => 'submit',
                                                                  'class' => 'submit',
                                                                  'value' => _('Unsubscribe')));