]> git.mxchange.org Git - friendica.git/blobdiff - boot.php
provide ability to use different port
[friendica.git] / boot.php
index ff4224cfc7ced4c7bac414f8e0b4b1c1200b6302..a72fbf6734269b63a822757a369696cfc4794fe5 100644 (file)
--- a/boot.php
+++ b/boot.php
@@ -1,12 +1,8 @@
 <?php
 
-set_time_limit(0);
-ini_set('pcre.backtrack_limit', 250000);
-
-
-define ( 'FRIENDIKA_VERSION',      '2.2.1010' );
+define ( 'FRIENDIKA_VERSION',      '2.2.1052' );
 define ( 'DFRN_PROTOCOL_VERSION',  '2.21'    );
-define ( 'DB_UPDATE_VERSION',      1063      );
+define ( 'DB_UPDATE_VERSION',      1076      );
 
 define ( 'EOL',                    "<br />\r\n"     );
 define ( 'ATOM_TIME',              'Y-m-d\TH:i:s\Z' );
@@ -56,9 +52,9 @@ define ( 'REGISTER_OPEN',          2 );
  * this relationship with contact['name']
  */
 
-define ( 'REL_VIP',        1);
-define ( 'REL_FAN',        2);
-define ( 'REL_BUD',        3);
+define ( 'REL_VIP',        1);     // other person is 'following' us
+define ( 'REL_FAN',        2);     // we are 'following' other person
+define ( 'REL_BUD',        3);     // mutual relationship
 
 /**
  * Hook array order
@@ -89,6 +85,7 @@ define ( 'PAGE_FREELOVE',          3 );
  * Network and protocol family types 
  */
 
+define ( 'NETWORK_ZOT',              'zot!');    // Zot!
 define ( 'NETWORK_DFRN',             'dfrn');    // Friendika, Mistpark, other DFRN implementations
 define ( 'NETWORK_OSTATUS',          'stat');    // status.net, identi.ca, GNU-social, other OStatus implementations
 define ( 'NETWORK_FEED',             'feed');    // RSS/Atom feeds with no known "post/notify" protocol
@@ -103,6 +100,13 @@ define ( 'NETWORK_FACEBOOK',         'face');    // Facebook API
 
 define ( 'MAX_LIKERS',    75);
 
+/**
+ * Communication timeout
+ */
+
+define ( 'ZCURL_TIMEOUT' , (-1));
+
+
 /**
  * email notification options
  */
@@ -117,6 +121,7 @@ define ( 'NOTIFY_MAIL',    0x0010 );
  * various namespaces we may need to parse
  */
 
+define ( 'NAMESPACE_ZOT',             'http://purl.org/macgirvin/zot' );
 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' );
@@ -128,7 +133,9 @@ 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' );
-
+define ( 'NAMESPACE_OSTATUS',         'http://ostatus.org/schema/1.0' );
+define ( 'NAMESPACE_STATUSNET',       'http://status.net/schema/api/1/' );
+define ( 'NAMESPACE_ATOM1',           'http://www.w3.org/2005/Atom' );
 /**
  * activity stream defines
  */
@@ -168,20 +175,28 @@ define ( 'GRAVITY_COMMENT',      6);
  *
  */
 
-if (get_magic_quotes_gpc()) {
-    $process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
-    while (list($key, $val) = each($process)) {
-        foreach ($val as $k => $v) {
-            unset($process[$key][$k]);
-            if (is_array($v)) {
-                $process[$key][stripslashes($k)] = $v;
-                $process[] = &$process[$key][stripslashes($k)];
-            } else {
-                $process[$key][stripslashes($k)] = stripslashes($v);
-            }
-        }
-    }
-    unset($process);
+function startup() {
+       error_reporting(E_ERROR | E_WARNING | E_PARSE);
+       set_time_limit(0);
+       ini_set('pcre.backtrack_limit', 250000);
+
+
+       if (get_magic_quotes_gpc()) {
+       $process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
+           while (list($key, $val) = each($process)) {
+           foreach ($val as $k => $v) {
+                   unset($process[$key][$k]);
+               if (is_array($v)) {
+                       $process[$key][stripslashes($k)] = $v;
+                       $process[] = &$process[$key][stripslashes($k)];
+               } else {
+                       $process[$key][stripslashes($k)] = stripslashes($v);
+               }
+               }
+       }
+           unset($process);
+       }
+
 }
 
 /*
@@ -249,11 +264,14 @@ class App {
 
                $this->query_string = '';
 
+               startup();
+
                $this->scheme = ((isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS']))      ?  'https' : 'http' );
 
                if(x($_SERVER,'SERVER_NAME')) {
                        $this->hostname = $_SERVER['SERVER_NAME'];
-
+                       if(x($_SERVER,'SERVER_PORT') && $_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 443)
+                               $this->hostname .= ':' . $_SERVER['SERVER_PORT'];
                        /** 
                         * Figure out if we are running at the top of a domain
                         * or in a sub-directory and adjust accordingly
@@ -294,6 +312,8 @@ class App {
                        $this->module = str_replace(".", "_", $this->argv[0]);
                }
                else {
+                       $this->argc = 1;
+                       $this->argv = array('home');
                        $this->module = 'home';
                }
 
@@ -477,9 +497,13 @@ function install_plugin($plugin){
        if(function_exists($plugin . '_install')) {
                $func = $plugin . '_install';
                $func();
-               $r = q("INSERT INTO `addon` (`name`, `installed`, `timestamp`) VALUES ( '%s', 1, %d ) ",
+               
+               $plugin_admin = (function_exists($plugin."_plugin_admin")?1:0);
+               
+               $r = q("INSERT INTO `addon` (`name`, `installed`, `timestamp`, `plugin_admin`) VALUES ( '%s', 1, %d , %d ) ",
                        dbesc($plugin),
-                       intval($t)
+                       intval($t),
+                       $plugin_admin
                );
        }
 }}
@@ -492,9 +516,6 @@ function install_plugin($plugin){
 if(! function_exists('check_config')) {
 function check_config(&$a) {
 
-
-       load_config('system');
-
        $build = get_config('system','build');
        if(! x($build))
                $build = set_config('system','build',DB_UPDATE_VERSION);
@@ -668,7 +689,7 @@ function fetch_url($url,$binary = false, &$redirects = 0) {
 
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
-
+       curl_setopt($ch, CURLOPT_USERAGENT, "Friendika");
 
        $curl_time = intval(get_config('system','curl_timeout'));
        curl_setopt($ch, CURLOPT_TIMEOUT, (($curl_time !== false) ? $curl_time : 60));
@@ -697,13 +718,21 @@ function fetch_url($url,$binary = false, &$redirects = 0) {
 
        $s = @curl_exec($ch);
 
-       $http_code = intval(curl_getinfo($ch, CURLINFO_HTTP_CODE));
-       $header = substr($s,0,strpos($s,"\r\n\r\n"));
-       if(stristr($header,'100') && (strlen($header) < 30)) {
-               // 100 Continue has two headers, get the real one
-               $s = substr($s,strlen($header)+4);
-               $header = substr($s,0,strpos($s,"\r\n\r\n"));
+       $base = $s;
+       $curl_info = curl_getinfo($ch);
+       $http_code = $curl_info['http_code'];
+
+       $header = '';
+
+       // Pull out multiple headers, e.g. proxy and continuation headers
+       // allow for HTTP/2.x without fixing code
+
+       while(preg_match('/^HTTP\/[1-2].+? [1-5][0-9][0-9]/',$base)) {
+               $chunk = substr($base,0,strpos($base,"\r\n\r\n")+4);
+               $header .= $chunk;
+               $base = substr($base,strlen($chunk));
        }
+
        if($http_code == 301 || $http_code == 302 || $http_code == 303 || $http_code == 307) {
         $matches = array();
         preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
@@ -714,16 +743,10 @@ function fetch_url($url,$binary = false, &$redirects = 0) {
             return fetch_url($url,$binary,$redirects);
         }
     }
-       $a->set_curl_code($http_code);
 
-       $body = substr($s,strlen($header)+4);
-
-       /* one more try to make sure there are no more headers */
+       $a->set_curl_code($http_code);
 
-       if(strpos($body,'HTTP/') === 0) {
-               $header = substr($body,0,strpos($body,"\r\n\r\n"));
-               $body = substr($body,strlen($header)+4);
-       }
+       $body = substr($s,strlen($header));
 
        $a->set_curl_headers($header);
 
@@ -744,11 +767,21 @@ function post_url($url,$params, $headers = null, &$redirects = 0) {
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_POST,1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
+       curl_setopt($ch, CURLOPT_USERAGENT, "Friendika");
 
        $curl_time = intval(get_config('system','curl_timeout'));
        curl_setopt($ch, CURLOPT_TIMEOUT, (($curl_time !== false) ? $curl_time : 60));
 
-       if(is_array($headers))
+       if(defined('LIGHTTPD')) {
+               if(!is_array($headers)) {
+                       $headers = array('Expect:');
+               } else {
+                       if(!in_array('Expect:', $headers)) {
+                               array_push($headers, 'Expect:');
+                       }
+               }
+       }
+       if($headers)
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
 
        $check_cert = get_config('system','verifyssl');
@@ -769,13 +802,21 @@ function post_url($url,$params, $headers = null, &$redirects = 0) {
 
        $s = @curl_exec($ch);
 
-       $http_code = intval(curl_getinfo($ch, CURLINFO_HTTP_CODE));
-       $header = substr($s,0,strpos($s,"\r\n\r\n"));
-       if(stristr($header,'100') && (strlen($header) < 30)) {
-               // 100 Continue has two headers, get the real one
-               $s = substr($s,strlen($header)+4);
-               $header = substr($s,0,strpos($s,"\r\n\r\n"));
+       $base = $s;
+       $curl_info = curl_getinfo($ch);
+       $http_code = $curl_info['http_code'];
+
+       $header = '';
+
+       // Pull out multiple headers, e.g. proxy and continuation headers
+       // allow for HTTP/2.x without fixing code
+
+       while(preg_match('/^HTTP\/[1-2].+? [1-5][0-9][0-9]/',$base)) {
+               $chunk = substr($base,0,strpos($base,"\r\n\r\n")+4);
+               $header .= $chunk;
+               $base = substr($base,strlen($chunk));
        }
+
        if($http_code == 301 || $http_code == 302 || $http_code == 303) {
         $matches = array();
         preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
@@ -787,14 +828,7 @@ function post_url($url,$params, $headers = null, &$redirects = 0) {
         }
     }
        $a->set_curl_code($http_code);
-       $body = substr($s,strlen($header)+4);
-
-       /* one more try to make sure there are no more headers */
-
-       if(strpos($body,'HTTP/') === 0) {
-               $header = substr($body,0,strpos($body,"\r\n\r\n"));
-               $body = substr($body,strlen($header)+4);
-       }
+       $body = substr($s,strlen($header));
 
        $a->set_curl_headers($header);
 
@@ -1212,7 +1246,11 @@ function load_config($family) {
        if(count($r)) {
                foreach($r as $rr) {
                        $k = $rr['k'];
-                       $a->config[$family][$k] = $rr['v'];
+                       if ($rr['cat'] === 'config') {
+                               $a->config[$k] = $rr['v'];
+                       } else {
+                               $a->config[$family][$k] = $rr['v'];
+                       }
                }
        }
 }}
@@ -1243,8 +1281,10 @@ function get_config($family, $key, $instore = false) {
                dbesc($key)
        );
        if(count($ret)) {
-               $a->config[$family][$key] = $ret[0]['v'];
-               return $ret[0]['v'];
+               // manage array value
+               $val = (preg_match("|^a:[0-9]+:{.*}$|", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
+               $a->config[$family][$key] = $val;
+               return $val;
        }
        else {
                $a->config[$family][$key] = '!<unset>!';
@@ -1258,22 +1298,25 @@ function get_config($family, $key, $instore = false) {
 
 if(! function_exists('set_config')) {
 function set_config($family,$key,$value) {
-
        global $a;
+       
+       // manage array value
+       $dbvalue = (is_array($value)?serialize($value):$value);
 
        if(get_config($family,$key,true) === false) {
                $a->config[$family][$key] = $value;
                $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
                        dbesc($family),
                        dbesc($key),
-                       dbesc($value)
+                       dbesc($dbvalue)
                );
                if($ret) 
                        return $value;
                return $ret;
        }
+       
        $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
-               dbesc($value),
+               dbesc($dbvalue),
                dbesc($family),
                dbesc($key)
        );
@@ -1674,8 +1717,10 @@ function fetch_lrdd_template($host) {
        $url1 = 'https://' . $host . '/.well-known/host-meta' ;
        $url2 = 'http://' . $host . '/.well-known/host-meta' ;
        $links = fetch_xrd_links($url1);
+       logger('fetch_lrdd_template from: ' . $url1);
        logger('template (https): ' . print_r($links,true));
        if(! count($links)) {
+               logger('fetch_lrdd_template from: ' . $url2);
                $links = fetch_xrd_links($url2);
                logger('template (http): ' . print_r($links,true));
        }
@@ -2004,7 +2049,10 @@ function get_tags($s) {
 
        $s = preg_replace('/\[code\](.*?)\[\/code\]/sm','',$s);
 
-       if(preg_match_all('/([@#][^ \x0D\x0A,:?]+)([ \x0D\x0A,:?]|$)/',$s,$match)) {
+       // Match full names against @tags including the space between first and last
+       // We will look these up afterward to see if they are full names or not recognisable.
+
+       if(preg_match_all('/(@[^ \x0D\x0A,:?]+ [^ \x0D\x0A,:?]+)([ \x0D\x0A,:?]|$)/',$s,$match)) {
                foreach($match[1] as $mtch) {
                        if(strstr($mtch,"]")) {
                                // we might be inside a bbcode color tag - leave it alone
@@ -2017,6 +2065,24 @@ function get_tags($s) {
                }
        }
 
+       // Otherwise pull out single word tags. These can be @nickname, @first_last
+       // and #hash tags.
+
+       if(preg_match_all('/([@#][^ \x0D\x0A,:?]+)([ \x0D\x0A,:?]|$)/',$s,$match)) {
+               foreach($match[1] as $mtch) {
+                       if(strstr($mtch,"]")) {
+                               // we might be inside a bbcode color tag - leave it alone
+                               continue;
+                       }
+                       // ignore strictly numeric tags like #1
+                       if((strpos($mtch,'#') === 0) && ctype_digit(substr($mtch,1)))
+                               continue;
+                       if(substr($mtch,-1,1) === '.')
+                               $ret[] = substr($mtch,0,-1);
+                       else
+                               $ret[] = $mtch;
+               }
+       }
        return $ret;
 }}
 
@@ -2404,7 +2470,7 @@ function profile_sidebar($profile) {
        ));
 
 
-       $arr = array('profile' => $profile, 'entry' => $o);
+       $arr = array('profile' => &$profile, 'entry' => &$o);
 
        call_hooks('profile_sidebar', $arr);
 
@@ -2880,3 +2946,27 @@ function get_plugin_info($plugin){
        }
        return $info;
 }}
+
+if(! function_exists('return_bytes')) {
+function return_bytes ($size_str) {
+    switch (substr ($size_str, -1))
+    {
+        case 'M': case 'm': return (int)$size_str * 1048576;
+        case 'K': case 'k': return (int)$size_str * 1024;
+        case 'G': case 'g': return (int)$size_str * 1073741824;
+        default: return $size_str;
+    }
+}}
+
+function generate_guid() {
+       $found = true;
+       do {
+               $guid = substr(random_string(),0,16);
+               $x = q("SELECT `uid` FROM `user` WHERE `guid` = '%s' LIMIT 1",
+                       dbesc($guid)
+               );
+               if(! count($x))
+                       $found = false;
+       } while ($found == true );
+       return $guid;
+}
\ No newline at end of file