]> git.mxchange.org Git - friendica.git/blobdiff - boot.php
pull some template strings
[friendica.git] / boot.php
index 6b23d38e0422dd5fb9990c89e12a87f560ea0941..f0d40544f4ff0e066bda9b35bc294c9145d0ef56 100644 (file)
--- a/boot.php
+++ b/boot.php
@@ -2,12 +2,20 @@
 
 set_time_limit(0);
 
-define ( 'BUILD_ID',               1011   );
+define ( 'BUILD_ID',               1017   );
 define ( 'DFRN_PROTOCOL_VERSION',  '2.0'  );
 
 define ( 'EOL',                    "<br />\r\n"     );
 define ( 'ATOM_TIME',              'Y-m-d\TH:i:s\Z' );
 
+// log levels
+
+define ( 'LOGGER_NORMAL',          0 );
+define ( 'LOGGER_TRACE',           1 );
+define ( 'LOGGER_DEBUG',           2 );
+define ( 'LOGGER_DATA',            3 );
+define ( 'LOGGER_ALL',             4 );
+
 // registration policy
 
 define ( 'REGISTER_CLOSED',        0 );
@@ -52,10 +60,13 @@ define ( 'NAMESPACE_DFRN' ,           'http://purl.org/macgirvin/dfrn/1.0' );
 define ( 'NAMESPACE_THREAD' ,         'http://purl.org/syndication/thread/1.0' );
 define ( 'NAMESPACE_TOMB' ,           'http://purl.org/atompub/tombstones/1.0' );
 define ( 'NAMESPACE_ACTIVITY',        'http://activitystrea.ms/spec/1.0/' );
-define ( 'NAMESPACE_ACTIVITY_SCHEMA', 'http://activitystrea.ms/schema/1.0/');
-define ( 'NAMESPACE_SALMON_ME',       'http://salmon-protocol.org/ns/magic-env');
-define ( 'NAMESPACE_OSTATUSSUB',      'http://ostatus.org/schema/1.0/subscribe');
-define ( 'NAMESPACE_GEORSS',          'http://www.georss.org/georss');
+define ( 'NAMESPACE_ACTIVITY_SCHEMA', 'http://activitystrea.ms/schema/1.0/' );
+define ( 'NAMESPACE_MEDIA',           'http://purl.org/syndication/atommedia' );
+define ( 'NAMESPACE_SALMON_ME',       'http://salmon-protocol.org/ns/magic-env' );
+define ( 'NAMESPACE_OSTATUSSUB',      'http://ostatus.org/schema/1.0/subscribe' );
+define ( 'NAMESPACE_GEORSS',          'http://www.georss.org/georss' );
+define ( 'NAMESPACE_POCO',            'http://portablecontacts.net/spec/1.0' );
+define ( 'NAMESPACE_FEED',            'http://schemas.google.com/g/2010#updates-from' );
 
 // activity stream defines
 
@@ -64,8 +75,11 @@ define ( 'ACTIVITY_DISLIKE',     NAMESPACE_DFRN            . '/dislike' );
 define ( 'ACTIVITY_OBJ_HEART',   NAMESPACE_DFRN            . '/heart' );
 
 define ( 'ACTIVITY_FRIEND',      NAMESPACE_ACTIVITY_SCHEMA . 'make-friend' );
+define ( 'ACTIVITY_FOLLOW',      NAMESPACE_ACTIVITY_SCHEMA . 'follow' );
+define ( 'ACTIVITY_UNFOLLOW',    NAMESPACE_ACTIVITY_SCHEMA . 'unfollow' );
 define ( 'ACTIVITY_POST',        NAMESPACE_ACTIVITY_SCHEMA . 'post' );
 define ( 'ACTIVITY_UPDATE',      NAMESPACE_ACTIVITY_SCHEMA . 'update' );
+define ( 'ACTIVITY_TAG',         NAMESPACE_ACTIVITY_SCHEMA . 'tag' );
 
 define ( 'ACTIVITY_OBJ_COMMENT', NAMESPACE_ACTIVITY_SCHEMA . 'comment' );
 define ( 'ACTIVITY_OBJ_NOTE',    NAMESPACE_ACTIVITY_SCHEMA . 'note' );
@@ -125,12 +139,16 @@ class App {
                $this->pager= array();
 
                $this->scheme = ((isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS']))      ?  'https' : 'http' );
-               $this->hostname = $_SERVER['SERVER_NAME'];
+
+               if(x($_SERVER,'SERVER_NAME'))
+                       $this->hostname = $_SERVER['SERVER_NAME'];
+
                set_include_path("include/$this->hostname" . PATH_SEPARATOR . 'include' . PATH_SEPARATOR . '.' );
 
-               if(substr($_SERVER['QUERY_STRING'],0,2) == "q=")
+               if((x($_SERVER,'QUERY_STRING')) && substr($_SERVER['QUERY_STRING'],0,2) === "q=")
                        $_SERVER['QUERY_STRING'] = substr($_SERVER['QUERY_STRING'],2);
-               $this->cmd = trim($_GET['q'],'/');
+               if(x($_GET,'q'))
+                       $this->cmd = trim($_GET['q'],'/');
 
 
                $this->argv = explode('/',$this->cmd);
@@ -145,6 +163,7 @@ class App {
                if($this->cmd === '.well-known/host-meta')
                        require_once('include/hostxrd.php');
 
+
                $this->pager['page'] = ((x($_GET,'page')) ? $_GET['page'] : 1);
                $this->pager['itemspage'] = 50;
                $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
@@ -337,10 +356,10 @@ function load_translation_table($lang) {
 
 if(! function_exists('t')) {
 function t($s) {
-       
+
        $a = get_app();
 
-       if($a->strings[$s])
+       if(x($a->strings,$s))
                return $a->strings[$s];
        return $s;
 }}
@@ -357,6 +376,11 @@ function fetch_url($url,$binary = false, &$redirects = 0) {
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
 
+
+       $curl_time = intval(get_config('system','curl_timeout'));
+       if($curl_time)
+               curl_setopt($ch, CURLOPT_TIMEOUT, $curl_time);
+
        // by default we will allow self-signed certs
        // but you can override this
 
@@ -405,7 +429,7 @@ function fetch_url($url,$binary = false, &$redirects = 0) {
 // post request to $url. $params is an array of post variables.
 
 if(! function_exists('post_url')) {
-function post_url($url,$params, &$redirects = 0) {
+function post_url($url,$params, $headers = null, &$redirects = 0) {
        $ch = curl_init($url);
        if(($redirects > 8) || (! $ch)) 
                return false;
@@ -415,6 +439,13 @@ function post_url($url,$params, &$redirects = 0) {
        curl_setopt($ch, CURLOPT_POST,1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
 
+       $curl_time = intval(get_config('system','curl_timeout'));
+       if($curl_time)
+               curl_setopt($ch, CURLOPT_TIMEOUT, $curl_time);
+
+       if(is_array($headers))
+               curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
+
        $check_cert = get_config('system','verifyssl');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (($check_cert) ? true : false));
        $prx = get_config('system','proxy');
@@ -442,7 +473,7 @@ function post_url($url,$params, &$redirects = 0) {
         $url_parsed = parse_url($url);
         if (isset($url_parsed)) {
             $redirects++;
-            return post_url($url,$binary,$redirects);
+            return post_url($url,$binary,$headers,$redirects);
         }
     }
        $a = get_app();
@@ -597,8 +628,7 @@ function goaway($s) {
 if(! function_exists('xml_status')) {
 function xml_status($st, $message = '') {
 
-       if(strlen($message))
-               $xml_message = "\t<message>" . xmlify($message) . "</message>\r\n";
+       $xml_message = ((strlen($message)) ? "\t<message>" . xmlify($message) . "</message>\r\n" : '');
 
        header( "Content-type: text/xml" );
        echo '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
@@ -674,7 +704,7 @@ function xmlify($str) {
                                $buffer .= '&gt;';
                                break;
                        case "\n" :
-                               $buffer .= ' ';
+                               $buffer .= "\n";
                                break;
                        default :
                                $buffer .= $char;
@@ -715,7 +745,7 @@ function hex2bin($s) {
 if(! function_exists('paginate')) {
 function paginate(&$a) {
        $o = '';
-       $stripped = ereg_replace("(&page=[0-9]*)","",$_SERVER['QUERY_STRING']);
+       $stripped = preg_replace('/(&page=[0-9]*)/','',$_SERVER['QUERY_STRING']);
        $stripped = str_replace('q=','',$stripped);
        $stripped = trim($stripped,'/');
        $url = $a->get_baseurl() . '/' . $stripped;
@@ -934,6 +964,7 @@ function webfinger_dfrn($s) {
                return $s;
        }
        $links = webfinger($s);
+       logger('webfinger_dfrn: ' . $s . ':' . print_r($links,true), LOGGER_DATA);
        if(count($links)) {
                foreach($links as $link)
                        if($link['@attributes']['rel'] === NAMESPACE_DFRN)
@@ -958,12 +989,13 @@ function webfinger($s) {
        }
        if(strlen($host)) {
                $tpl = fetch_lrdd_template($host);
+               logger('webfinger: lrdd template: ' . $tpl);
                if(strlen($tpl)) {
-                       $pxrd = str_replace('{uri}', urlencode('acct://'.$s), $tpl);
+                       $pxrd = str_replace('{uri}', urlencode('acct:'.$s), $tpl);
                        $links = fetch_xrd_links($pxrd);
                        if(! count($links)) {
-                               // try without the double slashes
-                               $pxrd = str_replace('{uri}', urlencode('acct:'.$s), $tpl);
+                               // try with double slashes
+                               $pxrd = str_replace('{uri}', urlencode('acct://'.$s), $tpl);
                                $links = fetch_xrd_links($pxrd);
                        }
                        return $links;
@@ -972,6 +1004,54 @@ function webfinger($s) {
        return array();
 }}
 
+if(! function_exists('lrdd')) {
+function lrdd($uri) {
+
+       $a = get_app();
+
+       if(strstr($uri,'@')) {  
+               return(webfinger($uri));
+       }
+       else {
+               $html = fetch_url($uri);
+               $headers = $a->get_curl_headers();
+               $lines = explode("\n",$headers);
+               if(count($lines)) {
+                       foreach($lines as $line) {                              
+                               // TODO alter the following regex to support multiple relations (space separated)
+                               if((stristr($line,'link:')) && preg_match('/<([^>].*)>.*rel\=[\'\"]lrdd[\'\"]/',$line,$matches)) {
+                                       $link = $matches[1];
+                                       break;
+                               }
+                       }
+               }
+               if(! isset($link)) {
+                       // parse the page of the supplied URL looking for rel links
+
+                       require_once('library/HTML5/Parser.php');
+                       $dom = HTML5_Parser::parse($html);
+
+                       if($dom) {
+                               $items = $dom->getElementsByTagName('link');
+
+                               foreach($items as $item) {
+                                       $x = $item->getAttribute('rel');
+                                       if($x == "lrdd") {
+                                               $link = $item->getAttribute('href');
+                                               break;
+                                       }
+                               }
+                       }
+               }
+
+               if(isset($link))
+                       return(fetch_xrd_links($link));
+       }
+       return array();
+}}
+
+
+
 // Given a host name, locate the LRDD template from that
 // host. Returns the LRDD template or an empty string on
 // error/failure.
@@ -998,9 +1078,12 @@ function fetch_lrdd_template($host) {
 if(! function_exists('fetch_xrd_links')) {
 function fetch_xrd_links($url) {
 
+
        $xml = fetch_url($url);
        if (! $xml)
                return array();
+
+       logger('fetch_xrd_links: ' . $xml, LOGGER_DATA);
        $h = simplexml_load_string($xml);
        $arr = convert_xml_element_to_array($h);
 
@@ -1077,13 +1160,25 @@ function validate_url(&$url) {
                $url = 'http://' . $url;
        $h = parse_url($url);
 
-       if(! $h) {
-               return false;
+       if(($h) && (checkdnsrr($h['host'], 'ANY'))) {
+               return true;
        }
-       if(! checkdnsrr($h['host'], 'ANY')) {
+       return false;
+}}
+
+// checks that email is an actual resolvable internet address
+
+if(! function_exists('validate_email')) {
+function validate_email($addr) {
+
+       if(! strpos($addr,'@'))
                return false;
+       $h = substr($addr,strpos($addr,'@') + 1);
+
+       if(($h) && (checkdnsrr($h, 'ANY'))) {
+               return true;
        }
-       return true;
+       return false;
 }}
 
 // Check $url against our list of allowed sites,
@@ -1171,6 +1266,7 @@ function allowed_email($email) {
 
 if(! function_exists('format_like')) {
 function format_like($cnt,$arr,$type,$id) {
+       $o = '';
        if($cnt == 1)
                $o .= $arr[0] . (($type === 'like') ? t(' likes this.') : t(' doesn\'t like this.')) . EOL ;
        else {
@@ -1199,9 +1295,197 @@ function load_view_file($s) {
        $b = basename($s);
        $d = dirname($s);
        $lang = get_config('system','language');
-       if($lang && file_exists("$d/$lang/$b"))
+       if($lang === false)
+               $lang = 'en';
+       if(file_exists("$d/$lang/$b"))
                return file_get_contents("$d/$lang/$b");
        return file_get_contents($s);
 }}
 
+// for html,xml parsing - let's say you've got
+// an attribute foobar="class1 class2 class3"
+// and you want to find out if it contains 'class3'.
+// you can't use a normal sub string search because you
+// might match 'notclass3' and a regex to do the job is 
+// possible but a bit complicated. 
+// pass the attribute string as $attr and the attribute you 
+// are looking for as $s - returns true if found, otherwise false
+
+if(! function_exists('attribute_contains')) {
+function attribute_contains($attr,$s) {
+       $a = explode(' ', $attr);
+       if(count($a) && in_array($s,$a))
+               return true;
+       return false;
+}}
+
+if(! function_exists('logger')) {
+function logger($msg,$level = 0) {
+
+       $debugging = get_config('system','debugging');
+       $loglevel  = intval(get_config('system','loglevel'));
+       $logfile   = get_config('system','logfile');
+
+       if((! $debugging) || (! $logfile) || ($level > $loglevel))
+               return;
+       
+       @file_put_contents($logfile, datetime_convert() . ':' . session_id() . ' ' . $msg . "\n", FILE_APPEND);
+       return;
+}}
+
+
+if(! function_exists('activity_match')) {
+function activity_match($haystack,$needle) {
+       if(($haystack === $needle) || ((basename($needle) === $haystack) && strstr($needle,NAMESPACE_ACTIVITY_SCHEMA)))
+               return true;
+       return false;
+}}
+
+
+// Pull out all #hashtags and @person tags from $s;
+// We also get @person@domain.com - which would make 
+// the regex quite complicated as tags can also
+// end a sentence. So we'll run through our results
+// and strip the period from any tags which end with one.
+// Returns array of tags found, or empty array.
+
+
+if(! function_exists('get_tags')) {
+function get_tags($s) {
+       $ret = array();
+       if(preg_match_all('/([@#][^ ,:?]*)([ ,:?]|$)/',$s,$match)) {
+               foreach($match[1] as $match) {
+                       if(substr($match,-1,1) === '.')
+                               $ret[] = substr($match,0,-1);
+                       else
+                               $ret[] = $match;
+               }
+       }
+
+       return $ret;
+}}
+
+
+// quick and dirty quoted_printable encoding
+
+if(! function_exists('qp')) {
+function qp($s) {
+return str_replace ("%","=",rawurlencode($s));
+}} 
+
+
+if(! function_exists('like_puller')) {
+function like_puller($a,$item,&$arr,$mode) {
+
+       $url = '';
+       $sparkle = '';
+       $verb = (($mode === 'like') ? ACTIVITY_LIKE : ACTIVITY_DISLIKE);
+
+       if((activity_match($item['verb'],$verb)) && ($item['id'] != $item['parent'])) {
+               $url = $item['url'];
+               if(($item['network'] === 'dfrn') && (! $item['self'])) {
+                       $url = $a->get_baseurl() . '/redir/' . $item['contact-id'];
+                       $sparkle = ' class="sparkle" ';
+               }
+               if(! ((isset($arr[$item['parent'] . '-l'])) && (is_array($arr[$item['parent'] . '-l']))))
+                       $arr[$item['parent'] . '-l'] = array();
+               if(! isset($arr[$item['parent']]))
+                       $arr[$item['parent']] = 1;
+               else    
+                       $arr[$item['parent']] ++;
+               $arr[$item['parent'] . '-l'][] = '<a href="'. $url . '"'. $sparkle .'>' . $item['name'] . '</a>';
+       }
+       return;
+}}
+
+if(! function_exists('get_mentions')) {
+function get_mentions($item) {
+       $o = '';
+       if(! strlen($item['tag']))
+               return $o;
+
+       $arr = explode(',',$item['tag']);
+       foreach($arr as $x) {
+               $matches = null;
+               if(preg_match('/@\[url=([^\]]*)\]/',$x,$matches))
+                       $o .= "\t\t" . '<link rel="mentioned" href="' . $matches[1] . '" />' . "\r\n";
+       }
+       return $o;
+}}
+
+if(! function_exists('contact_block')) {
+function contact_block() {
+       $o = '';
+       $a = get_app();
+       if((! is_array($a->profile)) || ($a->profile['hide-friends']))
+               return $o;
+       $r = q("SELECT COUNT(*) AS `total` FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 and `pending` = 0",
+                       intval($a->profile['uid'])
+       );
+       if(count($r)) {
+               $total = intval($r[0]['total']);
+       }
+       if(! $total) {
+               $o .= '<h4 class="contact-h4">' . t('No contacts') . '</h4>';
+               return $o;
+       }
+       $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 and `pending` = 0 ORDER BY RAND() LIMIT 24",
+                       intval($a->profile['uid'])
+       );
+       if(count($r)) {
+               $o .= '<h4 class="contact-h4">' . $total . ' ' . t('Contacts') . '</h4><div id="contact-block">';
+               foreach($r as $rr) {
+                       $redirect_url = $a->get_baseurl() . '/redir/' . $rr['id'];
+                       if(local_user() && ($rr['uid'] == local_user())
+                               && ($rr['network'] === 'dfrn')) {
+                               $url = $redirect_url;
+                               $sparkle = ' sparkle';
+                       }
+                       else {
+                               $url = $rr['url'];
+                               $sparkle = '';
+                       }
+
+                       $o .= '<div class="contact-block-div"><a class="contact-block-link' . $sparkle . '" href="' . $url . '" ><img class="contact-block-img' . $sparkle . '" src="' . $rr['micro'] . '" title="' . $rr['name'] . ' [' . $rr['url'] . ']" alt="' . $rr['name'] . '" /></a></div>' . "\r\n";
+               }
+               $o .= '</div><div id="contact-block-end"></div>';
+               $o .=  '<div id="viewcontacts"><a id="viewcontacts-link" href="viewcontacts/' . $a->profile['nickname'] . '">' . t('View Contacts') . '</a></div>';
+               
+       }
+       return $o;
+
+}}
+
+if(! function_exists('search')) {
+function search($s) {
+       $a = get_app();
+       $o  = '<div id="search-box">';
+       $o .= '<form action="' . $a->get_baseurl() . '/search' . '" method="get" >';
+       $o .= '<input type="text" name="search" id="search-text" value="' . $s .'" />';
+       $o .= '<input type="submit" name="submit" id="search-submit" value="' . t('Search') . '" />'; 
+       $o .= '</form></div>';
+       return $o;
+}}
+
+if(! function_exists('valid_email')) {
+function valid_email($x){
+       if(preg_match('/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+$/',$x))
+               return true;
+       return false;
+}}
+
+
+if(! function_exists('gravatar_img')) {
+function gravatar_img($email) {
+       $size = 175;
+       $opt = 'identicon';   // psuedo-random geometric pattern if not found
+       $rating = 'pg';
+       $hash = md5(trim(strtolower($email)));
+       
+       $url = 'http://www.gravatar.com/avatar/' . $hash . '.jpg' 
+               . '?s=' . $size . '&d=' . $opt . '&r=' . $rating;
+
+       logger('gravatar: ' . $email . ' ' . $url);
+       return $url;
+}}