5 define ( 'BUILD_ID' , 1001 );
7 define ( 'EOL', "<br />\r\n");
9 define ( 'REGISTER_CLOSED', 0);
10 define ( 'REGISTER_APPROVE', 1);
11 define ( 'REGISTER_OPEN', 2);
13 define ( 'DIRECTION_NONE', 0); // No relationship
14 define ( 'DIRECTION_IN', 1); // VIP e.g. has FAN
15 define ( 'DIRECTION_OUT', 2); // FAN to a VIP
16 define ( 'DIRECTION_BOTH', 3); // Mutual Friends
18 define ( 'NOTIFY_INTRO', 0x0001 );
19 define ( 'NOTIFY_CONFIRM', 0x0002 );
20 define ( 'NOTIFY_WALL', 0x0004 );
21 define ( 'NOTIFY_COMMENT', 0x0008 );
22 define ( 'NOTIFY_MAIL', 0x0010 );
24 define ( 'NAMESPACE_DFRN' , 'http://purl.org/macgirvin/dfrn/1.0' );
25 define ( 'NAMESPACE_ACTIVITY', 'http://activitystrea.ms/spec/1.0/' );
26 define ( 'NAMESPACE_ACTIVITY_SCHEMA', 'http://activitystrea.ms/schema/1.0/');
27 define ( 'ACTIVITY_LIKE', NAMESPACE_ACTIVITY_SCHEMA . 'like' );
28 define ( 'ACTIVITY_FRIEND', NAMESPACE_ACTIVITY_SCHEMA . 'make-friend' );
29 define ( 'ACTIVITY_POST', NAMESPACE_ACTIVITY_SCHEMA . 'post' );
30 define ( 'ACTIVITY_UPDATE', NAMESPACE_ACTIVITY_SCHEMA . 'update' );
32 define ( 'ACTIVITY_OBJ_COMMENT', NAMESPACE_ACTIVITY_SCHEMA . 'comment' );
33 define ( 'ACTIVITY_OBJ_NOTE', NAMESPACE_ACTIVITY_SCHEMA . 'note' );
34 define ( 'ACTIVITY_OBJ_PERSON', NAMESPACE_ACTIVITY_SCHEMA . 'person' );
35 define ( 'ACTIVITY_OBJ_PHOTO', NAMESPACE_ACTIVITY_SCHEMA . 'photo' );
36 define ( 'ACTIVITY_OBJ_P_PHOTO', NAMESPACE_ACTIVITY_SCHEMA . 'profile-photo' );
37 define ( 'ACTIVITY_OBJ_ALBUM', NAMESPACE_ACTIVITY_SCHEMA . 'photo-album' );
40 define ( 'ACTIVITY_OBJ_HEART', NAMESPACE_DFRN . '/heart' );
44 if(! class_exists('App')) {
47 public $module_loaded = false;
56 public $error = false;
70 function __construct() {
72 $this->config = array();
73 $this->page = array();
74 $this->pager= array();
76 $this->scheme = ((isset($_SERVER['HTTPS'])
77 && ($_SERVER['HTTPS'])) ? 'https' : 'http' );
78 $this->hostname = str_replace('www.','',
79 $_SERVER['SERVER_NAME']);
80 set_include_path("include/$this->hostname"
81 . PATH_SEPARATOR . 'include'
82 . PATH_SEPARATOR . '.' );
84 if(substr($_SERVER['QUERY_STRING'],0,2) == "q=")
85 $_SERVER['QUERY_STRING'] = substr($_SERVER['QUERY_STRING'],2);
86 $this->cmd = trim($_GET['q'],'/');
89 $this->argv = explode('/',$this->cmd);
90 $this->argc = count($this->argv);
91 if((array_key_exists('0',$this->argv)) && strlen($this->argv[0])) {
92 $this->module = $this->argv[0];
95 $this->module = 'home';
98 if($this->cmd == '.well-known/host-meta')
99 require_once('include/hostxrd.php');
101 $this->pager['page'] = ((x($_GET,'page')) ? $_GET['page'] : 1);
102 $this->pager['itemspage'] = 50;
103 $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
104 $this->pager['total'] = 0;
107 function get_baseurl($ssl = false) {
108 if(strlen($this->baseurl))
109 return $this->baseurl;
111 $this->baseurl = (($ssl) ? 'https' : $this->scheme) . "://" . $this->hostname
112 . ((isset($this->path) && strlen($this->path))
113 ? '/' . $this->path : '' );
114 return $this->baseurl;
117 function set_baseurl($url) {
118 $this->baseurl = $url;
119 $this->hostname = basename($url);
122 function get_hostname() {
123 return $this->hostname;
126 function set_hostname($h) {
127 $this->hostname = $h;
130 function set_path($p) {
131 $this->path = ltrim(trim($p),'/');
134 function get_path() {
138 function set_pager_total($n) {
139 $this->pager['total'] = intval($n);
142 function set_pager_itemspage($n) {
143 $this->pager['itemspage'] = intval($n);
144 $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
148 function init_pagehead() {
149 if(file_exists("view/head.tpl"))
150 $s = file_get_contents("view/head.tpl");
151 $this->page['htmlhead'] = replace_macros($s,array(
152 '$baseurl' => $this->get_baseurl()
159 if(! function_exists('x')) {
160 function x($s,$k = NULL) {
162 if((is_array($s)) && (array_key_exists($k,$s))) {
180 if(! function_exists('system_unavailable')) {
181 function system_unavailable() {
182 include('system_unavailable.php');
187 if(! function_exists('check_config')) {
188 function check_config(&$a) {
190 load_config('system');
192 $build = get_config('system','build');
194 $build = set_config('system','build',BUILD_ID);
196 $url = get_config('system','url');
198 $url = set_config('system','url',$a->get_baseurl());
200 if($build != BUILD_ID) {
201 $stored = intval($build);
202 $current = intval(BUILD_ID);
203 if(($stored < $current) && file_exists('update.php')) {
205 // We're reporting a different version than what is currently installed.
206 // Run any existing update scripts to bring the database up to current.
208 require_once('update.php');
209 for($x = $stored; $x < $current; $x ++) {
210 if(function_exists('update_' . $x)) {
211 $func = 'update_' . $x;
215 set_config('system','build', BUILD_ID);
223 if(! function_exists('replace_macros')) {
224 function replace_macros($s,$r) {
229 if(is_array($r) && count($r)) {
230 foreach ($r as $k => $v ) {
235 return str_replace($search,$replace,$s);
239 if(! function_exists('load_translation_table')) {
240 function load_translation_table($lang) {
245 if(! function_exists('t')) {
250 return $a->strings[$s];
254 if(! function_exists('fetch_url')) {
255 function fetch_url($url,$binary = false) {
256 $ch = curl_init($url);
257 if(! $ch) return false;
259 curl_setopt($ch, CURLOPT_HEADER, 0);
260 curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
261 curl_setopt($ch, CURLOPT_MAXREDIRS,8);
262 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
263 $prx = get_config('system','proxy');
265 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
266 curl_setopt($ch, CURLOPT_PROXY, $prx);
267 $prxusr = get_config('system','proxyuser');
269 curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
272 curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
280 if(! function_exists('post_url')) {
281 function post_url($url,$params) {
282 $ch = curl_init($url);
283 if(! $ch) return false;
285 curl_setopt($ch, CURLOPT_HEADER, 0);
286 curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
287 curl_setopt($ch, CURLOPT_MAXREDIRS,8);
288 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
289 curl_setopt($ch, CURLOPT_POST,1);
290 curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
291 $prx = get_config('system','proxy');
293 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
294 curl_setopt($ch, CURLOPT_PROXY, $prx);
295 $prxusr = get_config('system','proxyuser');
297 curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
306 if(! function_exists('random_string')) {
307 function random_string() {
308 return(hash('sha256',uniqid(rand(),true)));
311 if(! function_exists('notags')) {
312 function notags($string) {
313 // protect against :<> with high-bit set
314 return(str_replace(array("<",">","\xBA","\xBC","\xBE"), array('[',']','','',''), $string));
317 if(! function_exists('escape_tags')) {
318 function escape_tags($string) {
320 return(htmlspecialchars($string));
323 if(! function_exists('login')) {
324 function login($register = false) {
326 $register_html = (($register) ? file_get_contents("view/register-link.tpl") : "");
329 if(x($_SESSION,'authenticated')) {
330 $o = file_get_contents("view/logout.tpl");
333 $o = file_get_contents("view/login.tpl");
335 $o = replace_macros($o,array('$register_html' => $register_html ));
341 if(! function_exists('autoname')) {
342 function autoname($len) {
344 $vowels = array('a','a','ai','au','e','e','e','ee','ea','i','ie','o','ou','u');
345 if(mt_rand(0,5) == 4)
363 's','sc','sh','sm','sp','st',
371 $midcons = array('ck','ct','gn','ld','lf','lm','lt','mb','mm', 'mn','mp',
372 'nd','ng','nk','nt','rn','rp','rt');
374 $noend = array('bl', 'br', 'cl','cr','dr','fl','fr','gl','gr',
375 'kh', 'kl','kr','mn','pl','pr','rh','tr','qu','wh');
377 $start = mt_rand(0,2);
385 for ($x = 0; $x < $len; $x ++) {
386 $r = mt_rand(0,count($table) - 1);
389 if($table == $vowels)
390 $table = array_merge($cons,$midcons);
396 $word = substr($word,0,$len);
398 foreach($noend as $noe) {
399 if((strlen($word) > 2) && (substr($word,-2) == $noe)) {
400 $word = substr($word,0,-1);
404 if(substr($word,-1) == 'q')
405 $word = substr($word,0,-1);
409 if(! function_exists('killme')) {
411 session_write_close();
415 if(! function_exists('goaway')) {
416 function goaway($s) {
417 header("Location: $s");
422 if(! function_exists('xml_status')) {
423 function xml_status($st) {
424 header( "Content-type: text/xml" );
425 echo '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
426 echo "<result><status>$st</status></result>\r\n";
430 if(! function_exists('local_user')) {
431 function local_user() {
432 if((x($_SESSION,'authenticated')) && (x($_SESSION,'uid')))
433 return $_SESSION['uid'];
437 if(! function_exists('remote_user')) {
438 function remote_user() {
439 if((x($_SESSION,'authenticated')) && (x($_SESSION,'visitor_id')))
440 return $_SESSION['visitor_id'];
444 if(! function_exists('notice')) {
445 function notice($s) {
447 $_SESSION['sysmsg'] .= $s;
451 if(! function_exists('get_max_import_size')) {
452 function get_max_import_size() {
454 return ((x($a->config,'max_import_size')) ? $a->config['max_import_size'] : 0 );
457 if(! function_exists('xmlify')) {
458 function xmlify($str) {
461 for($x = 0; $x < strlen($str); $x ++) {
492 $buffer = trim($buffer);
496 if(! function_exists('unxmlify')) {
497 function unxmlify($s) {
498 $ret = str_replace('&','&', $s);
499 $ret = str_replace(array('<','>','"','''),array('<','>','"',"'"),$ret);
503 if(! function_exists('hex2bin')) {
504 function hex2bin($s) {
505 return(pack("H*",$s));
509 if(! function_exists('paginate')) {
510 function paginate(&$a) {
512 $stripped = ereg_replace("(&page=[0-9]*)","",$_SERVER['QUERY_STRING']);
513 $stripped = str_replace('q=','',$stripped);
514 $stripped = trim($stripped,'/');
515 $url = $a->get_baseurl() . '/' . $stripped;
518 if($a->pager['total'] > $a->pager['itemspage']) {
519 $o .= '<div class="pager">';
520 if($a->pager['page'] != 1)
521 $o .= '<span class="pager_prev">'."<a href=\"$url".'&page='.($a->pager['page'] - 1).'">' . t('prev') . '</a></span> ';
523 $o .= "<span class=\"pager_first\"><a href=\"$url"."&page=1\">" . t('first') . "</a></span> ";
525 $numpages = $a->pager['total'] / $a->pager['itemspage'];
528 $numstop = $numpages;
531 $numstart = (($pagenum > 7) ? ($pagenum - 7) : 1);
532 $numstop = (($pagenum > ($numpages - 7)) ? $numpages : ($numstart + 14));
535 for($i = $numstart; $i <= $numstop; $i++){
536 if($i == $a->pager['page'])
537 $o .= '<span class="pager_current">'.(($i < 10) ? ' '.$i : $i);
539 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? ' '.$i : $i)."</a>";
543 if(($a->pager['total'] % $a->pager['itemspage']) != 0) {
544 if($i == $a->pager['page'])
545 $o .= '<span class="pager_current">'.(($i < 10) ? ' '.$i : $i);
547 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? ' '.$i : $i)."</a>";
551 $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
552 $o .= "<span class=\"pager_last\"><a href=\"$url"."&page=$lastpage\">" . t('last') . "</a></span> ";
554 if(($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0)
555 $o .= '<span class="pager_next">'."<a href=\"$url"."&page=".($a->pager['page'] + 1).'">' . t('next') . '</a></span>';
556 $o .= '</div>'."\r\n";
561 if(! function_exists('expand_acl')) {
562 function expand_acl($s) {
565 $a = explode('<',$s);
566 for($x = 0; $x < count($a); $x ++) {
567 $a[$x] = intval(str_replace(array('<','>'),array('',''),$a[$x]));
574 if(! function_exists('sanitise_acl')) {
575 function sanitise_acl(&$item) {
577 $item = '<' . intval(notags(trim($item))) . '>';
582 if(! function_exists('load_config')) {
583 function load_config($family) {
585 $r = q("SELECT * FROM `config` WHERE `cat` = '%s'",
591 $a->config[$family][$k] = $rr['v'];
597 if(! function_exists('get_config')) {
598 function get_config($family, $key, $instore = false) {
602 if(isset($a->config[$family][$key])) {
603 if($a->config[$family][$key] == '!<unset>!')
605 return $a->config[$family][$key];
608 $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
613 $a->config[$family][$key] = $ret[0]['v'];
617 $a->config[$family][$key] = '!<unset>!';
622 if(! function_exists('set_config')) {
623 function set_config($family,$key,$value) {
626 $a->config[$family][$key] = $value;
628 if(get_config($family,$key,true) === false) {
629 $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
638 $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
648 if(! function_exists('convert_xml_element_to_array')) {
649 function convert_xml_element_to_array($xml_element, &$recursion_depth=0) {
651 // If we're getting too deep, bail out
652 if ($recursion_depth > 512) {
656 if (!is_string($xml_element) &&
657 !is_array($xml_element) &&
658 (get_class($xml_element) == 'SimpleXMLElement')) {
659 $xml_element_copy = $xml_element;
660 $xml_element = get_object_vars($xml_element);
663 if (is_array($xml_element)) {
664 $result_array = array();
665 if (count($xml_element) <= 0) {
666 return (trim(strval($xml_element_copy)));
669 foreach($xml_element as $key=>$value) {
672 $result_array[strtolower($key)] =
673 convert_xml_element_to_array($value, $recursion_depth);
676 if ($recursion_depth == 0) {
677 $temp_array = $result_array;
678 $result_array = array(
679 strtolower($xml_element_copy->getName()) => $temp_array,
683 return ($result_array);
686 return (trim(strval($xml_element)));
691 if(! function_exists('webfinger')) {
692 function webfinger($s) {
693 if(! strstr($s,'@')) {
696 $host = substr($s,strpos($s,'@') + 1);
697 $url = 'http://' . $host . '/.well-known/host-meta' ;
698 $xml = fetch_url($url);
701 $h = simplexml_load_string($xml);
702 $arr = convert_xml_element_to_array($h);
704 if(! isset($arr['xrd']['link']))
707 $link = $arr['xrd']['link'];
708 if(! isset($link[0]))
709 $links = array($link);
713 foreach($links as $link)
714 if($link['@attributes']['rel'] && $link['@attributes']['rel'] == 'lrdd')
715 $tpl = $link['@attributes']['template'];
716 if((empty($tpl)) || (! strpos($tpl, '{uri}')))
719 $pxrd = str_replace('{uri}', urlencode('acct://'.$s), $tpl);
721 $xml = fetch_url($pxrd);
724 $h = simplexml_load_string($xml);
725 $arr = convert_xml_element_to_array($h);
727 if(! isset($arr['xrd']['link']))
730 $link = $arr['xrd']['link'];
731 if(! isset($link[0]))
732 $links = array($link);
736 foreach($links as $link)
737 if($link['@attributes']['rel'] == NAMESPACE_DFRN)
738 return $link['@attributes']['href'];
742 if(! function_exists('perms2str')) {
743 function perms2str($p) {
747 array_walk($tmp,'sanitise_acl');
748 $ret = implode('',$tmp);
753 if(! function_exists('item_new_uri')) {
754 function item_new_uri($hostname,$uid) {
758 $hash = random_string();
760 $uri = "urn:X-dfrn:" . $hostname . ':' . $uid . ':' . $hash;
762 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
766 } while($dups == true);
770 if(! function_exists('get_uid')) {
772 return ((x($_SESSION,'uid')) ? intval($_SESSION['uid']) : 0) ;
775 if(! function_exists('validate_url')) {
776 function validate_url($url) {
777 if(substr($url,0,4) != 'http')
778 $url = 'http://' . $url;
779 $h = parse_url($url);
783 if(! checkdnsrr($h['host'], 'ANY'))