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' );
50 define ( 'GRAVITY_PARENT', 0);
51 define ( 'GRAVITY_LIKE', 3);
52 define ( 'GRAVITY_COMMENT', 6);
55 if(! class_exists('App')) {
58 public $module_loaded = false;
67 public $error = false;
81 function __construct() {
83 $this->config = array();
84 $this->page = array();
85 $this->pager= array();
87 $this->scheme = ((isset($_SERVER['HTTPS'])
88 && ($_SERVER['HTTPS'])) ? 'https' : 'http' );
89 $this->hostname = str_replace('www.','',
90 $_SERVER['SERVER_NAME']);
91 set_include_path("include/$this->hostname"
92 . PATH_SEPARATOR . 'include'
93 . PATH_SEPARATOR . '.' );
95 if(substr($_SERVER['QUERY_STRING'],0,2) == "q=")
96 $_SERVER['QUERY_STRING'] = substr($_SERVER['QUERY_STRING'],2);
97 $this->cmd = trim($_GET['q'],'/');
100 $this->argv = explode('/',$this->cmd);
101 $this->argc = count($this->argv);
102 if((array_key_exists('0',$this->argv)) && strlen($this->argv[0])) {
103 $this->module = $this->argv[0];
106 $this->module = 'home';
109 if($this->cmd == '.well-known/host-meta')
110 require_once('include/hostxrd.php');
112 $this->pager['page'] = ((x($_GET,'page')) ? $_GET['page'] : 1);
113 $this->pager['itemspage'] = 50;
114 $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
115 $this->pager['total'] = 0;
118 function get_baseurl($ssl = false) {
119 if(strlen($this->baseurl))
120 return $this->baseurl;
122 $this->baseurl = (($ssl) ? 'https' : $this->scheme) . "://" . $this->hostname
123 . ((isset($this->path) && strlen($this->path))
124 ? '/' . $this->path : '' );
125 return $this->baseurl;
128 function set_baseurl($url) {
129 $this->baseurl = $url;
130 $this->hostname = basename($url);
133 function get_hostname() {
134 return $this->hostname;
137 function set_hostname($h) {
138 $this->hostname = $h;
141 function set_path($p) {
142 $this->path = ltrim(trim($p),'/');
145 function get_path() {
149 function set_pager_total($n) {
150 $this->pager['total'] = intval($n);
153 function set_pager_itemspage($n) {
154 $this->pager['itemspage'] = intval($n);
155 $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
159 function init_pagehead() {
160 if(file_exists("view/head.tpl"))
161 $s = file_get_contents("view/head.tpl");
162 $this->page['htmlhead'] = replace_macros($s,array(
163 '$baseurl' => $this->get_baseurl()
170 if(! function_exists('x')) {
171 function x($s,$k = NULL) {
173 if((is_array($s)) && (array_key_exists($k,$s))) {
191 if(! function_exists('system_unavailable')) {
192 function system_unavailable() {
193 include('system_unavailable.php');
198 if(! function_exists('check_config')) {
199 function check_config(&$a) {
201 load_config('system');
203 $build = get_config('system','build');
205 $build = set_config('system','build',BUILD_ID);
207 $url = get_config('system','url');
209 $url = set_config('system','url',$a->get_baseurl());
211 if($build != BUILD_ID) {
212 $stored = intval($build);
213 $current = intval(BUILD_ID);
214 if(($stored < $current) && file_exists('update.php')) {
216 // We're reporting a different version than what is currently installed.
217 // Run any existing update scripts to bring the database up to current.
219 require_once('update.php');
220 for($x = $stored; $x < $current; $x ++) {
221 if(function_exists('update_' . $x)) {
222 $func = 'update_' . $x;
226 set_config('system','build', BUILD_ID);
234 if(! function_exists('replace_macros')) {
235 function replace_macros($s,$r) {
240 if(is_array($r) && count($r)) {
241 foreach ($r as $k => $v ) {
246 return str_replace($search,$replace,$s);
250 if(! function_exists('load_translation_table')) {
251 function load_translation_table($lang) {
256 if(! function_exists('t')) {
261 return $a->strings[$s];
265 if(! function_exists('fetch_url')) {
266 function fetch_url($url,$binary = false) {
267 $ch = curl_init($url);
268 if(! $ch) return false;
270 curl_setopt($ch, CURLOPT_HEADER, 0);
271 curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
272 curl_setopt($ch, CURLOPT_MAXREDIRS,8);
273 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
274 $prx = get_config('system','proxy');
276 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
277 curl_setopt($ch, CURLOPT_PROXY, $prx);
278 $prxusr = get_config('system','proxyuser');
280 curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
283 curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
291 if(! function_exists('post_url')) {
292 function post_url($url,$params) {
293 $ch = curl_init($url);
294 if(! $ch) return false;
296 curl_setopt($ch, CURLOPT_HEADER, 0);
297 curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
298 curl_setopt($ch, CURLOPT_MAXREDIRS,8);
299 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
300 curl_setopt($ch, CURLOPT_POST,1);
301 curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
302 $prx = get_config('system','proxy');
304 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
305 curl_setopt($ch, CURLOPT_PROXY, $prx);
306 $prxusr = get_config('system','proxyuser');
308 curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
317 if(! function_exists('random_string')) {
318 function random_string() {
319 return(hash('sha256',uniqid(rand(),true)));
322 if(! function_exists('notags')) {
323 function notags($string) {
324 // protect against :<> with high-bit set
325 return(str_replace(array("<",">","\xBA","\xBC","\xBE"), array('[',']','','',''), $string));
328 if(! function_exists('escape_tags')) {
329 function escape_tags($string) {
331 return(htmlspecialchars($string));
334 if(! function_exists('login')) {
335 function login($register = false) {
337 $register_html = (($register) ? file_get_contents("view/register-link.tpl") : "");
340 if(x($_SESSION,'authenticated')) {
341 $o = file_get_contents("view/logout.tpl");
344 $o = file_get_contents("view/login.tpl");
346 $o = replace_macros($o,array('$register_html' => $register_html ));
352 if(! function_exists('autoname')) {
353 function autoname($len) {
355 $vowels = array('a','a','ai','au','e','e','e','ee','ea','i','ie','o','ou','u');
356 if(mt_rand(0,5) == 4)
374 's','sc','sh','sm','sp','st',
382 $midcons = array('ck','ct','gn','ld','lf','lm','lt','mb','mm', 'mn','mp',
383 'nd','ng','nk','nt','rn','rp','rt');
385 $noend = array('bl', 'br', 'cl','cr','dr','fl','fr','gl','gr',
386 'kh', 'kl','kr','mn','pl','pr','rh','tr','qu','wh');
388 $start = mt_rand(0,2);
396 for ($x = 0; $x < $len; $x ++) {
397 $r = mt_rand(0,count($table) - 1);
400 if($table == $vowels)
401 $table = array_merge($cons,$midcons);
407 $word = substr($word,0,$len);
409 foreach($noend as $noe) {
410 if((strlen($word) > 2) && (substr($word,-2) == $noe)) {
411 $word = substr($word,0,-1);
415 if(substr($word,-1) == 'q')
416 $word = substr($word,0,-1);
420 if(! function_exists('killme')) {
422 session_write_close();
426 if(! function_exists('goaway')) {
427 function goaway($s) {
428 header("Location: $s");
433 if(! function_exists('xml_status')) {
434 function xml_status($st) {
435 header( "Content-type: text/xml" );
436 echo '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
437 echo "<result><status>$st</status></result>\r\n";
441 if(! function_exists('local_user')) {
442 function local_user() {
443 if((x($_SESSION,'authenticated')) && (x($_SESSION,'uid')))
444 return $_SESSION['uid'];
448 if(! function_exists('remote_user')) {
449 function remote_user() {
450 if((x($_SESSION,'authenticated')) && (x($_SESSION,'visitor_id')))
451 return $_SESSION['visitor_id'];
455 if(! function_exists('notice')) {
456 function notice($s) {
458 $_SESSION['sysmsg'] .= $s;
462 if(! function_exists('get_max_import_size')) {
463 function get_max_import_size() {
465 return ((x($a->config,'max_import_size')) ? $a->config['max_import_size'] : 0 );
468 if(! function_exists('xmlify')) {
469 function xmlify($str) {
472 for($x = 0; $x < strlen($str); $x ++) {
503 $buffer = trim($buffer);
507 if(! function_exists('unxmlify')) {
508 function unxmlify($s) {
509 $ret = str_replace('&','&', $s);
510 $ret = str_replace(array('<','>','"','''),array('<','>','"',"'"),$ret);
514 if(! function_exists('hex2bin')) {
515 function hex2bin($s) {
516 return(pack("H*",$s));
520 if(! function_exists('paginate')) {
521 function paginate(&$a) {
523 $stripped = ereg_replace("(&page=[0-9]*)","",$_SERVER['QUERY_STRING']);
524 $stripped = str_replace('q=','',$stripped);
525 $stripped = trim($stripped,'/');
526 $url = $a->get_baseurl() . '/' . $stripped;
529 if($a->pager['total'] > $a->pager['itemspage']) {
530 $o .= '<div class="pager">';
531 if($a->pager['page'] != 1)
532 $o .= '<span class="pager_prev">'."<a href=\"$url".'&page='.($a->pager['page'] - 1).'">' . t('prev') . '</a></span> ';
534 $o .= "<span class=\"pager_first\"><a href=\"$url"."&page=1\">" . t('first') . "</a></span> ";
536 $numpages = $a->pager['total'] / $a->pager['itemspage'];
539 $numstop = $numpages;
542 $numstart = (($pagenum > 7) ? ($pagenum - 7) : 1);
543 $numstop = (($pagenum > ($numpages - 7)) ? $numpages : ($numstart + 14));
546 for($i = $numstart; $i <= $numstop; $i++){
547 if($i == $a->pager['page'])
548 $o .= '<span class="pager_current">'.(($i < 10) ? ' '.$i : $i);
550 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? ' '.$i : $i)."</a>";
554 if(($a->pager['total'] % $a->pager['itemspage']) != 0) {
555 if($i == $a->pager['page'])
556 $o .= '<span class="pager_current">'.(($i < 10) ? ' '.$i : $i);
558 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? ' '.$i : $i)."</a>";
562 $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
563 $o .= "<span class=\"pager_last\"><a href=\"$url"."&page=$lastpage\">" . t('last') . "</a></span> ";
565 if(($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0)
566 $o .= '<span class="pager_next">'."<a href=\"$url"."&page=".($a->pager['page'] + 1).'">' . t('next') . '</a></span>';
567 $o .= '</div>'."\r\n";
572 if(! function_exists('expand_acl')) {
573 function expand_acl($s) {
576 $a = explode('<',$s);
577 for($x = 0; $x < count($a); $x ++) {
578 $a[$x] = intval(str_replace(array('<','>'),array('',''),$a[$x]));
585 if(! function_exists('sanitise_acl')) {
586 function sanitise_acl(&$item) {
588 $item = '<' . intval(notags(trim($item))) . '>';
593 if(! function_exists('load_config')) {
594 function load_config($family) {
596 $r = q("SELECT * FROM `config` WHERE `cat` = '%s'",
602 $a->config[$family][$k] = $rr['v'];
608 if(! function_exists('get_config')) {
609 function get_config($family, $key, $instore = false) {
613 if(isset($a->config[$family][$key])) {
614 if($a->config[$family][$key] == '!<unset>!')
616 return $a->config[$family][$key];
619 $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
624 $a->config[$family][$key] = $ret[0]['v'];
628 $a->config[$family][$key] = '!<unset>!';
633 if(! function_exists('set_config')) {
634 function set_config($family,$key,$value) {
637 $a->config[$family][$key] = $value;
639 if(get_config($family,$key,true) === false) {
640 $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
649 $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
659 if(! function_exists('convert_xml_element_to_array')) {
660 function convert_xml_element_to_array($xml_element, &$recursion_depth=0) {
662 // If we're getting too deep, bail out
663 if ($recursion_depth > 512) {
667 if (!is_string($xml_element) &&
668 !is_array($xml_element) &&
669 (get_class($xml_element) == 'SimpleXMLElement')) {
670 $xml_element_copy = $xml_element;
671 $xml_element = get_object_vars($xml_element);
674 if (is_array($xml_element)) {
675 $result_array = array();
676 if (count($xml_element) <= 0) {
677 return (trim(strval($xml_element_copy)));
680 foreach($xml_element as $key=>$value) {
683 $result_array[strtolower($key)] =
684 convert_xml_element_to_array($value, $recursion_depth);
687 if ($recursion_depth == 0) {
688 $temp_array = $result_array;
689 $result_array = array(
690 strtolower($xml_element_copy->getName()) => $temp_array,
694 return ($result_array);
697 return (trim(strval($xml_element)));
702 if(! function_exists('webfinger')) {
703 function webfinger($s) {
704 if(! strstr($s,'@')) {
707 $host = substr($s,strpos($s,'@') + 1);
708 $url = 'http://' . $host . '/.well-known/host-meta' ;
709 $xml = fetch_url($url);
712 $h = simplexml_load_string($xml);
713 $arr = convert_xml_element_to_array($h);
715 if(! isset($arr['xrd']['link']))
718 $link = $arr['xrd']['link'];
719 if(! isset($link[0]))
720 $links = array($link);
724 foreach($links as $link)
725 if($link['@attributes']['rel'] && $link['@attributes']['rel'] == 'lrdd')
726 $tpl = $link['@attributes']['template'];
727 if((empty($tpl)) || (! strpos($tpl, '{uri}')))
730 $pxrd = str_replace('{uri}', urlencode('acct://'.$s), $tpl);
732 $xml = fetch_url($pxrd);
735 $h = simplexml_load_string($xml);
736 $arr = convert_xml_element_to_array($h);
738 if(! isset($arr['xrd']['link']))
741 $link = $arr['xrd']['link'];
742 if(! isset($link[0]))
743 $links = array($link);
747 foreach($links as $link)
748 if($link['@attributes']['rel'] == NAMESPACE_DFRN)
749 return $link['@attributes']['href'];
753 if(! function_exists('perms2str')) {
754 function perms2str($p) {
758 array_walk($tmp,'sanitise_acl');
759 $ret = implode('',$tmp);
764 if(! function_exists('item_new_uri')) {
765 function item_new_uri($hostname,$uid) {
769 $hash = random_string();
771 $uri = "urn:X-dfrn:" . $hostname . ':' . $uid . ':' . $hash;
773 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
777 } while($dups == true);
781 if(! function_exists('photo_new_resource')) {
782 function photo_new_resource() {
786 $resource = hash('md5',uniqid(mt_rand(),true));
787 $r = q("SELECT `id` FROM `photo` WHERE `resource-id` = '%s' LIMIT 1",
792 } while($found == true);
798 if(! function_exists('get_uid')) {
800 return ((x($_SESSION,'uid')) ? intval($_SESSION['uid']) : 0) ;
803 if(! function_exists('validate_url')) {
804 function validate_url(&$url) {
805 if(substr($url,0,4) != 'http')
806 $url = 'http://' . $url;
807 $h = parse_url($url);
812 if(! checkdnsrr($h['host'], 'ANY')) {
818 if(! function_exists('allowed_url')) {
819 function allowed_url($url) {
821 $h = parse_url($url);
827 $str_allowed = get_config('system','allowed_sites');
833 $host = strtolower($h['host']);
835 // always allow our own site
837 if($host == strtolower($_SERVER['SERVER_NAME']))
840 $fnmatch = function_exists('fnmatch');
841 $allowed = explode(',',$str_allowed);
843 if(count($allowed)) {
844 foreach($allowed as $a) {
845 $pat = strtolower(trim($a));
846 if(($fnmatch && fnmatch($pat,$host)) || ($pat == $host)) {
855 if(! function_exists('format_like')) {
856 function format_like($cnt,$arr,$type,$id) {
858 $o .= $arr[0] . (($type == 'like') ? t(' likes this.') : t(' doesn\'t like this.')) . EOL ;
860 $o .= '<span class="fakelink" onclick="openClose(\'' . $type . 'list-' . $id . '\');" >'
861 . $cnt . ' ' . t('people') . '</span> ' . (($type == 'like') ? t('like this.') : t('don\'t like this.')) . EOL ;
862 $total = count($arr);
864 $arr = array_slice($arr,0,74);
866 $arr[count($arr)-1] = t('and') . ' ' . $arr[count($arr)-1];
867 $str = implode(', ', $arr);
869 $str .= t(', and ') . $total - 75 . t(' other people');
870 $str .= (($type == 'like') ? t(' like this.') : t(' don\'t like this.'));
871 $o .= '<div id="' . $type . 'list-' . $id . '" style="display: none;" >' . $str . '</div>';