5 define ( 'BUILD_ID' , 1003 );
7 define ( 'EOL', "<br />\r\n");
8 define ( 'ATOM_TIME', 'Y-m-d\TH:i:s\Z' );
10 define ( 'REGISTER_CLOSED', 0);
11 define ( 'REGISTER_APPROVE', 1);
12 define ( 'REGISTER_OPEN', 2);
14 define ( 'DIRECTION_NONE', 0); // No relationship
15 define ( 'DIRECTION_IN', 1); // VIP e.g. has FAN
16 define ( 'DIRECTION_OUT', 2); // FAN to a VIP
17 define ( 'DIRECTION_BOTH', 3); // Mutual Friends
19 define ( 'REL_VIP', 1);
20 define ( 'REL_FAN', 2);
21 define ( 'REL_BUD', 3);
23 define ( 'NOTIFY_INTRO', 0x0001 );
24 define ( 'NOTIFY_CONFIRM', 0x0002 );
25 define ( 'NOTIFY_WALL', 0x0004 );
26 define ( 'NOTIFY_COMMENT', 0x0008 );
27 define ( 'NOTIFY_MAIL', 0x0010 );
29 define ( 'NAMESPACE_DFRN' , 'http://purl.org/macgirvin/dfrn/1.0' );
30 define ( 'NAMESPACE_THREAD' , 'http://purl.org/syndication/thread/1.0' );
31 define ( 'NAMESPACE_TOMB' , 'http://purl.org/atompub/tombstones/1.0' );
32 define ( 'NAMESPACE_ACTIVITY', 'http://activitystrea.ms/spec/1.0/' );
33 define ( 'NAMESPACE_ACTIVITY_SCHEMA', 'http://activitystrea.ms/schema/1.0/');
35 define ( 'ACTIVITY_LIKE', NAMESPACE_ACTIVITY_SCHEMA . 'like' );
36 define ( 'ACTIVITY_DISLIKE', NAMESPACE_DFRN . '/dislike' );
37 define ( 'ACTIVITY_OBJ_HEART', NAMESPACE_DFRN . '/heart' );
39 define ( 'ACTIVITY_FRIEND', NAMESPACE_ACTIVITY_SCHEMA . 'make-friend' );
40 define ( 'ACTIVITY_POST', NAMESPACE_ACTIVITY_SCHEMA . 'post' );
41 define ( 'ACTIVITY_UPDATE', NAMESPACE_ACTIVITY_SCHEMA . 'update' );
43 define ( 'ACTIVITY_OBJ_COMMENT', NAMESPACE_ACTIVITY_SCHEMA . 'comment' );
44 define ( 'ACTIVITY_OBJ_NOTE', NAMESPACE_ACTIVITY_SCHEMA . 'note' );
45 define ( 'ACTIVITY_OBJ_PERSON', NAMESPACE_ACTIVITY_SCHEMA . 'person' );
46 define ( 'ACTIVITY_OBJ_PHOTO', NAMESPACE_ACTIVITY_SCHEMA . 'photo' );
47 define ( 'ACTIVITY_OBJ_P_PHOTO', NAMESPACE_ACTIVITY_SCHEMA . 'profile-photo' );
48 define ( 'ACTIVITY_OBJ_ALBUM', NAMESPACE_ACTIVITY_SCHEMA . 'photo-album' );
53 if(! class_exists('App')) {
56 public $module_loaded = false;
65 public $error = false;
79 function __construct() {
81 $this->config = array();
82 $this->page = array();
83 $this->pager= array();
85 $this->scheme = ((isset($_SERVER['HTTPS'])
86 && ($_SERVER['HTTPS'])) ? 'https' : 'http' );
87 $this->hostname = str_replace('www.','',
88 $_SERVER['SERVER_NAME']);
89 set_include_path("include/$this->hostname"
90 . PATH_SEPARATOR . 'include'
91 . PATH_SEPARATOR . '.' );
93 if(substr($_SERVER['QUERY_STRING'],0,2) == "q=")
94 $_SERVER['QUERY_STRING'] = substr($_SERVER['QUERY_STRING'],2);
95 $this->cmd = trim($_GET['q'],'/');
98 $this->argv = explode('/',$this->cmd);
99 $this->argc = count($this->argv);
100 if((array_key_exists('0',$this->argv)) && strlen($this->argv[0])) {
101 $this->module = $this->argv[0];
104 $this->module = 'home';
107 if($this->cmd == '.well-known/host-meta')
108 require_once('include/hostxrd.php');
110 $this->pager['page'] = ((x($_GET,'page')) ? $_GET['page'] : 1);
111 $this->pager['itemspage'] = 50;
112 $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
113 $this->pager['total'] = 0;
116 function get_baseurl($ssl = false) {
117 if(strlen($this->baseurl))
118 return $this->baseurl;
120 $this->baseurl = (($ssl) ? 'https' : $this->scheme) . "://" . $this->hostname
121 . ((isset($this->path) && strlen($this->path))
122 ? '/' . $this->path : '' );
123 return $this->baseurl;
126 function set_baseurl($url) {
127 $this->baseurl = $url;
128 $this->hostname = basename($url);
131 function get_hostname() {
132 return $this->hostname;
135 function set_hostname($h) {
136 $this->hostname = $h;
139 function set_path($p) {
140 $this->path = ltrim(trim($p),'/');
143 function get_path() {
147 function set_pager_total($n) {
148 $this->pager['total'] = intval($n);
151 function set_pager_itemspage($n) {
152 $this->pager['itemspage'] = intval($n);
153 $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
157 function init_pagehead() {
158 if(file_exists("view/head.tpl"))
159 $s = file_get_contents("view/head.tpl");
160 $this->page['htmlhead'] = replace_macros($s,array(
161 '$baseurl' => $this->get_baseurl()
168 if(! function_exists('x')) {
169 function x($s,$k = NULL) {
171 if((is_array($s)) && (array_key_exists($k,$s))) {
189 if(! function_exists('system_unavailable')) {
190 function system_unavailable() {
191 include('system_unavailable.php');
196 if(! function_exists('check_config')) {
197 function check_config(&$a) {
199 load_config('system');
201 $build = get_config('system','build');
203 $build = set_config('system','build',BUILD_ID);
205 $url = get_config('system','url');
207 $url = set_config('system','url',$a->get_baseurl());
209 if($build != BUILD_ID) {
210 $stored = intval($build);
211 $current = intval(BUILD_ID);
212 if(($stored < $current) && file_exists('update.php')) {
214 // We're reporting a different version than what is currently installed.
215 // Run any existing update scripts to bring the database up to current.
217 require_once('update.php');
218 for($x = $stored; $x < $current; $x ++) {
219 if(function_exists('update_' . $x)) {
220 $func = 'update_' . $x;
224 set_config('system','build', BUILD_ID);
232 if(! function_exists('replace_macros')) {
233 function replace_macros($s,$r) {
238 if(is_array($r) && count($r)) {
239 foreach ($r as $k => $v ) {
244 return str_replace($search,$replace,$s);
248 if(! function_exists('load_translation_table')) {
249 function load_translation_table($lang) {
254 if(! function_exists('t')) {
259 return $a->strings[$s];
263 if(! function_exists('fetch_url')) {
264 function fetch_url($url,$binary = false) {
265 $ch = curl_init($url);
266 if(! $ch) return false;
268 curl_setopt($ch, CURLOPT_HEADER, 0);
269 curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
270 curl_setopt($ch, CURLOPT_MAXREDIRS,8);
271 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
272 $prx = get_config('system','proxy');
274 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
275 curl_setopt($ch, CURLOPT_PROXY, $prx);
276 $prxusr = get_config('system','proxyuser');
278 curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
281 curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
289 if(! function_exists('post_url')) {
290 function post_url($url,$params) {
291 $ch = curl_init($url);
292 if(! $ch) return false;
294 curl_setopt($ch, CURLOPT_HEADER, 0);
295 curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
296 curl_setopt($ch, CURLOPT_MAXREDIRS,8);
297 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
298 curl_setopt($ch, CURLOPT_POST,1);
299 curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
300 $prx = get_config('system','proxy');
302 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
303 curl_setopt($ch, CURLOPT_PROXY, $prx);
304 $prxusr = get_config('system','proxyuser');
306 curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
315 if(! function_exists('random_string')) {
316 function random_string() {
317 return(hash('sha256',uniqid(rand(),true)));
320 if(! function_exists('notags')) {
321 function notags($string) {
322 // protect against :<> with high-bit set
323 return(str_replace(array("<",">","\xBA","\xBC","\xBE"), array('[',']','','',''), $string));
326 if(! function_exists('escape_tags')) {
327 function escape_tags($string) {
329 return(htmlspecialchars($string));
332 if(! function_exists('login')) {
333 function login($register = false) {
335 $register_html = (($register) ? file_get_contents("view/register-link.tpl") : "");
338 if(x($_SESSION,'authenticated')) {
339 $o = file_get_contents("view/logout.tpl");
342 $o = file_get_contents("view/login.tpl");
344 $o = replace_macros($o,array('$register_html' => $register_html ));
350 if(! function_exists('autoname')) {
351 function autoname($len) {
353 $vowels = array('a','a','ai','au','e','e','e','ee','ea','i','ie','o','ou','u');
354 if(mt_rand(0,5) == 4)
372 's','sc','sh','sm','sp','st',
380 $midcons = array('ck','ct','gn','ld','lf','lm','lt','mb','mm', 'mn','mp',
381 'nd','ng','nk','nt','rn','rp','rt');
383 $noend = array('bl', 'br', 'cl','cr','dr','fl','fr','gl','gr',
384 'kh', 'kl','kr','mn','pl','pr','rh','tr','qu','wh');
386 $start = mt_rand(0,2);
394 for ($x = 0; $x < $len; $x ++) {
395 $r = mt_rand(0,count($table) - 1);
398 if($table == $vowels)
399 $table = array_merge($cons,$midcons);
405 $word = substr($word,0,$len);
407 foreach($noend as $noe) {
408 if((strlen($word) > 2) && (substr($word,-2) == $noe)) {
409 $word = substr($word,0,-1);
413 if(substr($word,-1) == 'q')
414 $word = substr($word,0,-1);
418 if(! function_exists('killme')) {
420 session_write_close();
424 if(! function_exists('goaway')) {
425 function goaway($s) {
426 header("Location: $s");
431 if(! function_exists('xml_status')) {
432 function xml_status($st) {
433 header( "Content-type: text/xml" );
434 echo '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
435 echo "<result><status>$st</status></result>\r\n";
439 if(! function_exists('local_user')) {
440 function local_user() {
441 if((x($_SESSION,'authenticated')) && (x($_SESSION,'uid')))
442 return $_SESSION['uid'];
446 if(! function_exists('remote_user')) {
447 function remote_user() {
448 if((x($_SESSION,'authenticated')) && (x($_SESSION,'visitor_id')))
449 return $_SESSION['visitor_id'];
453 if(! function_exists('notice')) {
454 function notice($s) {
456 $_SESSION['sysmsg'] .= $s;
460 if(! function_exists('get_max_import_size')) {
461 function get_max_import_size() {
463 return ((x($a->config,'max_import_size')) ? $a->config['max_import_size'] : 0 );
466 if(! function_exists('xmlify')) {
467 function xmlify($str) {
470 for($x = 0; $x < strlen($str); $x ++) {
501 $buffer = trim($buffer);
505 if(! function_exists('unxmlify')) {
506 function unxmlify($s) {
507 $ret = str_replace('&','&', $s);
508 $ret = str_replace(array('<','>','"','''),array('<','>','"',"'"),$ret);
512 if(! function_exists('hex2bin')) {
513 function hex2bin($s) {
514 return(pack("H*",$s));
518 if(! function_exists('paginate')) {
519 function paginate(&$a) {
521 $stripped = ereg_replace("(&page=[0-9]*)","",$_SERVER['QUERY_STRING']);
522 $stripped = str_replace('q=','',$stripped);
523 $stripped = trim($stripped,'/');
524 $url = $a->get_baseurl() . '/' . $stripped;
527 if($a->pager['total'] > $a->pager['itemspage']) {
528 $o .= '<div class="pager">';
529 if($a->pager['page'] != 1)
530 $o .= '<span class="pager_prev">'."<a href=\"$url".'&page='.($a->pager['page'] - 1).'">' . t('prev') . '</a></span> ';
532 $o .= "<span class=\"pager_first\"><a href=\"$url"."&page=1\">" . t('first') . "</a></span> ";
534 $numpages = $a->pager['total'] / $a->pager['itemspage'];
537 $numstop = $numpages;
540 $numstart = (($pagenum > 7) ? ($pagenum - 7) : 1);
541 $numstop = (($pagenum > ($numpages - 7)) ? $numpages : ($numstart + 14));
544 for($i = $numstart; $i <= $numstop; $i++){
545 if($i == $a->pager['page'])
546 $o .= '<span class="pager_current">'.(($i < 10) ? ' '.$i : $i);
548 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? ' '.$i : $i)."</a>";
552 if(($a->pager['total'] % $a->pager['itemspage']) != 0) {
553 if($i == $a->pager['page'])
554 $o .= '<span class="pager_current">'.(($i < 10) ? ' '.$i : $i);
556 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? ' '.$i : $i)."</a>";
560 $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
561 $o .= "<span class=\"pager_last\"><a href=\"$url"."&page=$lastpage\">" . t('last') . "</a></span> ";
563 if(($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0)
564 $o .= '<span class="pager_next">'."<a href=\"$url"."&page=".($a->pager['page'] + 1).'">' . t('next') . '</a></span>';
565 $o .= '</div>'."\r\n";
570 if(! function_exists('expand_acl')) {
571 function expand_acl($s) {
574 $a = explode('<',$s);
575 for($x = 0; $x < count($a); $x ++) {
576 $a[$x] = intval(str_replace(array('<','>'),array('',''),$a[$x]));
583 if(! function_exists('sanitise_acl')) {
584 function sanitise_acl(&$item) {
586 $item = '<' . intval(notags(trim($item))) . '>';
591 if(! function_exists('load_config')) {
592 function load_config($family) {
594 $r = q("SELECT * FROM `config` WHERE `cat` = '%s'",
600 $a->config[$family][$k] = $rr['v'];
606 if(! function_exists('get_config')) {
607 function get_config($family, $key, $instore = false) {
611 if(isset($a->config[$family][$key])) {
612 if($a->config[$family][$key] == '!<unset>!')
614 return $a->config[$family][$key];
617 $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
622 $a->config[$family][$key] = $ret[0]['v'];
626 $a->config[$family][$key] = '!<unset>!';
631 if(! function_exists('set_config')) {
632 function set_config($family,$key,$value) {
635 $a->config[$family][$key] = $value;
637 if(get_config($family,$key,true) === false) {
638 $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
647 $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
657 if(! function_exists('convert_xml_element_to_array')) {
658 function convert_xml_element_to_array($xml_element, &$recursion_depth=0) {
660 // If we're getting too deep, bail out
661 if ($recursion_depth > 512) {
665 if (!is_string($xml_element) &&
666 !is_array($xml_element) &&
667 (get_class($xml_element) == 'SimpleXMLElement')) {
668 $xml_element_copy = $xml_element;
669 $xml_element = get_object_vars($xml_element);
672 if (is_array($xml_element)) {
673 $result_array = array();
674 if (count($xml_element) <= 0) {
675 return (trim(strval($xml_element_copy)));
678 foreach($xml_element as $key=>$value) {
681 $result_array[strtolower($key)] =
682 convert_xml_element_to_array($value, $recursion_depth);
685 if ($recursion_depth == 0) {
686 $temp_array = $result_array;
687 $result_array = array(
688 strtolower($xml_element_copy->getName()) => $temp_array,
692 return ($result_array);
695 return (trim(strval($xml_element)));
700 if(! function_exists('webfinger')) {
701 function webfinger($s) {
702 if(! strstr($s,'@')) {
705 $host = substr($s,strpos($s,'@') + 1);
706 $url = 'http://' . $host . '/.well-known/host-meta' ;
707 $xml = fetch_url($url);
710 $h = simplexml_load_string($xml);
711 $arr = convert_xml_element_to_array($h);
713 if(! isset($arr['xrd']['link']))
716 $link = $arr['xrd']['link'];
717 if(! isset($link[0]))
718 $links = array($link);
722 foreach($links as $link)
723 if($link['@attributes']['rel'] && $link['@attributes']['rel'] == 'lrdd')
724 $tpl = $link['@attributes']['template'];
725 if((empty($tpl)) || (! strpos($tpl, '{uri}')))
728 $pxrd = str_replace('{uri}', urlencode('acct://'.$s), $tpl);
730 $xml = fetch_url($pxrd);
733 $h = simplexml_load_string($xml);
734 $arr = convert_xml_element_to_array($h);
736 if(! isset($arr['xrd']['link']))
739 $link = $arr['xrd']['link'];
740 if(! isset($link[0]))
741 $links = array($link);
745 foreach($links as $link)
746 if($link['@attributes']['rel'] == NAMESPACE_DFRN)
747 return $link['@attributes']['href'];
751 if(! function_exists('perms2str')) {
752 function perms2str($p) {
756 array_walk($tmp,'sanitise_acl');
757 $ret = implode('',$tmp);
762 if(! function_exists('item_new_uri')) {
763 function item_new_uri($hostname,$uid) {
767 $hash = random_string();
769 $uri = "urn:X-dfrn:" . $hostname . ':' . $uid . ':' . $hash;
771 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
775 } while($dups == true);
779 if(! function_exists('get_uid')) {
781 return ((x($_SESSION,'uid')) ? intval($_SESSION['uid']) : 0) ;
784 if(! function_exists('validate_url')) {
785 function validate_url($url) {
786 if(substr($url,0,4) != 'http')
787 $url = 'http://' . $url;
788 $h = parse_url($url);
792 if(! checkdnsrr($h['host'], 'ANY'))