5 define ( 'BUILD_ID' , 1006 );
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);
16 define ( 'REL_VIP', 1);
17 define ( 'REL_FAN', 2);
18 define ( 'REL_BUD', 3);
20 define ( 'NOTIFY_INTRO', 0x0001 );
21 define ( 'NOTIFY_CONFIRM', 0x0002 );
22 define ( 'NOTIFY_WALL', 0x0004 );
23 define ( 'NOTIFY_COMMENT', 0x0008 );
24 define ( 'NOTIFY_MAIL', 0x0010 );
26 define ( 'NAMESPACE_DFRN' , 'http://purl.org/macgirvin/dfrn/1.0' );
27 define ( 'NAMESPACE_THREAD' , 'http://purl.org/syndication/thread/1.0' );
28 define ( 'NAMESPACE_TOMB' , 'http://purl.org/atompub/tombstones/1.0' );
29 define ( 'NAMESPACE_ACTIVITY', 'http://activitystrea.ms/spec/1.0/' );
30 define ( 'NAMESPACE_ACTIVITY_SCHEMA', 'http://activitystrea.ms/schema/1.0/');
31 define ( 'NAMESPACE_SALMON_ME', 'http://salmon-protocol.org/ns/magic-env');
33 define ( 'ACTIVITY_LIKE', NAMESPACE_ACTIVITY_SCHEMA . 'like' );
34 define ( 'ACTIVITY_DISLIKE', NAMESPACE_DFRN . '/dislike' );
35 define ( 'ACTIVITY_OBJ_HEART', NAMESPACE_DFRN . '/heart' );
37 define ( 'ACTIVITY_FRIEND', NAMESPACE_ACTIVITY_SCHEMA . 'make-friend' );
38 define ( 'ACTIVITY_POST', NAMESPACE_ACTIVITY_SCHEMA . 'post' );
39 define ( 'ACTIVITY_UPDATE', NAMESPACE_ACTIVITY_SCHEMA . 'update' );
41 define ( 'ACTIVITY_OBJ_COMMENT', NAMESPACE_ACTIVITY_SCHEMA . 'comment' );
42 define ( 'ACTIVITY_OBJ_NOTE', NAMESPACE_ACTIVITY_SCHEMA . 'note' );
43 define ( 'ACTIVITY_OBJ_PERSON', NAMESPACE_ACTIVITY_SCHEMA . 'person' );
44 define ( 'ACTIVITY_OBJ_PHOTO', NAMESPACE_ACTIVITY_SCHEMA . 'photo' );
45 define ( 'ACTIVITY_OBJ_P_PHOTO', NAMESPACE_ACTIVITY_SCHEMA . 'profile-photo' );
46 define ( 'ACTIVITY_OBJ_ALBUM', NAMESPACE_ACTIVITY_SCHEMA . 'photo-album' );
48 define ( 'GRAVITY_PARENT', 0);
49 define ( 'GRAVITY_LIKE', 3);
50 define ( 'GRAVITY_COMMENT', 6);
53 // Our main application structure for the life of this page
54 // Primarily deals with the URL that got us here
55 // and tries to make some sense of it, and
56 // stores our page contents and config storage
57 // and anything else that might need to be passed around
58 // before we spit the page out.
60 if(! class_exists('App')) {
63 public $module_loaded = false;
72 public $error = false;
86 function __construct() {
88 $this->config = array();
89 $this->page = array();
90 $this->pager= array();
92 $this->scheme = ((isset($_SERVER['HTTPS'])
93 && ($_SERVER['HTTPS'])) ? 'https' : 'http' );
94 $this->hostname = str_replace('www.','',
95 $_SERVER['SERVER_NAME']);
96 set_include_path("include/$this->hostname"
97 . PATH_SEPARATOR . 'include'
98 . PATH_SEPARATOR . '.' );
100 if(substr($_SERVER['QUERY_STRING'],0,2) == "q=")
101 $_SERVER['QUERY_STRING'] = substr($_SERVER['QUERY_STRING'],2);
102 $this->cmd = trim($_GET['q'],'/');
105 $this->argv = explode('/',$this->cmd);
106 $this->argc = count($this->argv);
107 if((array_key_exists('0',$this->argv)) && strlen($this->argv[0])) {
108 $this->module = $this->argv[0];
111 $this->module = 'home';
114 if($this->cmd === '.well-known/host-meta')
115 require_once('include/hostxrd.php');
117 $this->pager['page'] = ((x($_GET,'page')) ? $_GET['page'] : 1);
118 $this->pager['itemspage'] = 50;
119 $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
120 $this->pager['total'] = 0;
123 function get_baseurl($ssl = false) {
124 if(strlen($this->baseurl))
125 return $this->baseurl;
127 $this->baseurl = (($ssl) ? 'https' : $this->scheme) . "://" . $this->hostname
128 . ((isset($this->path) && strlen($this->path))
129 ? '/' . $this->path : '' );
130 return $this->baseurl;
133 function set_baseurl($url) {
134 $this->baseurl = $url;
135 $this->hostname = basename($url);
138 function get_hostname() {
139 return $this->hostname;
142 function set_hostname($h) {
143 $this->hostname = $h;
146 function set_path($p) {
147 $this->path = ltrim(trim($p),'/');
150 function get_path() {
154 function set_pager_total($n) {
155 $this->pager['total'] = intval($n);
158 function set_pager_itemspage($n) {
159 $this->pager['itemspage'] = intval($n);
160 $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
164 function init_pagehead() {
165 $tpl = load_view_file("view/head.tpl");
166 $this->page['htmlhead'] = replace_macros($tpl,array(
167 '$baseurl' => $this->get_baseurl()
173 // retrieve the App structure
174 // useful in functions which require it but don't get it passed to them
176 if(! function_exists('get_app')) {
183 // Multi-purpose function to check variable state.
184 // Usage: x($var) or $x($array,'key')
185 // returns false if variable/key is not set
186 // if variable is set, returns 1 if has 'non-zero' value, otherwise returns 0.
187 // e.g. x('') or x(0) returns 0;
189 if(! function_exists('x')) {
190 function x($s,$k = NULL) {
192 if((is_array($s)) && (array_key_exists($k,$s))) {
210 // called from db initialisation if db is dead.
212 if(! function_exists('system_unavailable')) {
213 function system_unavailable() {
214 include('system_unavailable.php');
218 // Primarily involved with database upgrade, but also sets the
219 // base url for use in cmdline programs which don't have
220 // $_SERVER variables.
222 if(! function_exists('check_config')) {
223 function check_config(&$a) {
225 load_config('system');
227 $build = get_config('system','build');
229 $build = set_config('system','build',BUILD_ID);
231 $url = get_config('system','url');
233 $url = set_config('system','url',$a->get_baseurl());
235 if($build != BUILD_ID) {
236 $stored = intval($build);
237 $current = intval(BUILD_ID);
238 if(($stored < $current) && file_exists('update.php')) {
240 // We're reporting a different version than what is currently installed.
241 // Run any existing update scripts to bring the database up to current.
243 require_once('update.php');
244 for($x = $stored; $x < $current; $x ++) {
245 if(function_exists('update_' . $x)) {
246 $func = 'update_' . $x;
250 set_config('system','build', BUILD_ID);
257 // This is our template processor.
258 // $s is the string requiring macro substitution.
259 // $r is an array of key value pairs (search => replace)
260 // returns substituted string.
261 // WARNING: this is pretty basic, and doesn't properly handle search strings that are substrings of each other.
262 // For instance if 'test' => "foo" and 'testing' => "bar", testing could become either bar or fooing,
263 // depending on the order in which they were declared in the array.
265 if(! function_exists('replace_macros')) {
266 function replace_macros($s,$r) {
271 if(is_array($r) && count($r)) {
272 foreach ($r as $k => $v ) {
277 return str_replace($search,$replace,$s);
281 // load string tranlsation table for alternate language
283 if(! function_exists('load_translation_table')) {
284 function load_translation_table($lang) {
287 if(file_exists("view/$lang/strings.php"))
288 include("view/$lang/strings.php");
291 // translate string if translation exists
293 if(! function_exists('t')) {
299 return $a->strings[$s];
303 // curl wrapper. If binary flag is true, return binary
306 if(! function_exists('fetch_url')) {
307 function fetch_url($url,$binary = false) {
308 $ch = curl_init($url);
309 if(! $ch) return false;
311 curl_setopt($ch, CURLOPT_HEADER, 0);
312 curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
313 curl_setopt($ch, CURLOPT_MAXREDIRS,8);
314 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
316 // by default we will allow self-signed certs
317 // but you can override this
319 $check_cert = get_config('system','verifyssl');
320 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (($check_cert) ? true : false));
322 $prx = get_config('system','proxy');
324 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
325 curl_setopt($ch, CURLOPT_PROXY, $prx);
326 $prxusr = get_config('system','proxyuser');
328 curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
331 curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
338 // post request to $url. $params is an array of post variables.
340 if(! function_exists('post_url')) {
341 function post_url($url,$params) {
342 $ch = curl_init($url);
343 if(! $ch) return false;
345 curl_setopt($ch, CURLOPT_HEADER, 0);
346 curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
347 curl_setopt($ch, CURLOPT_MAXREDIRS,8);
348 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
349 curl_setopt($ch, CURLOPT_POST,1);
350 curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
351 $check_cert = get_config('system','verifyssl');
352 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (($check_cert) ? true : false));
353 $prx = get_config('system','proxy');
355 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
356 curl_setopt($ch, CURLOPT_PROXY, $prx);
357 $prxusr = get_config('system','proxyuser');
359 curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
367 // random hash, 64 chars
369 if(! function_exists('random_string')) {
370 function random_string() {
371 return(hash('sha256',uniqid(rand(),true)));
374 // This is our primary input filter. The high bit hack only involved some old
375 // IE browser, forget which.
376 // Use this on any text input where angle chars are not valid or permitted
377 // They will be replaced with safer brackets. This may be filtered further
378 // if these are not allowed either.
380 if(! function_exists('notags')) {
381 function notags($string) {
382 // protect against :<> with high-bit set
383 return(str_replace(array("<",">","\xBA","\xBC","\xBE"), array('[',']','','',''), $string));
386 // use this on "body" or "content" input where angle chars shouldn't be removed,
387 // and allow them to be safely displayed.
389 if(! function_exists('escape_tags')) {
390 function escape_tags($string) {
392 return(htmlspecialchars($string));
395 // wrapper for adding a login box. If $register == true provide a registration
396 // link. This will most always depend on the value of $a->config['register_policy'].
398 if(! function_exists('login')) {
399 function login($register = false) {
401 $register_html = (($register) ? load_view_file("view/register-link.tpl") : "");
404 if(x($_SESSION,'authenticated')) {
405 $o = load_view_file("view/logout.tpl");
408 $o = load_view_file("view/login.tpl");
410 $o = replace_macros($o,array('$register_html' => $register_html ));
415 // generate a string that's random, but usually pronounceable.
416 // used to generate initial passwords
418 if(! function_exists('autoname')) {
419 function autoname($len) {
421 $vowels = array('a','a','ai','au','e','e','e','ee','ea','i','ie','o','ou','u');
422 if(mt_rand(0,5) == 4)
440 's','sc','sh','sm','sp','st',
448 $midcons = array('ck','ct','gn','ld','lf','lm','lt','mb','mm', 'mn','mp',
449 'nd','ng','nk','nt','rn','rp','rt');
451 $noend = array('bl', 'br', 'cl','cr','dr','fl','fr','gl','gr',
452 'kh', 'kl','kr','mn','pl','pr','rh','tr','qu','wh');
454 $start = mt_rand(0,2);
462 for ($x = 0; $x < $len; $x ++) {
463 $r = mt_rand(0,count($table) - 1);
466 if($table == $vowels)
467 $table = array_merge($cons,$midcons);
473 $word = substr($word,0,$len);
475 foreach($noend as $noe) {
476 if((strlen($word) > 2) && (substr($word,-2) == $noe)) {
477 $word = substr($word,0,-1);
481 if(substr($word,-1) == 'q')
482 $word = substr($word,0,-1);
486 // Used to end the current process, after saving session state.
488 if(! function_exists('killme')) {
490 session_write_close();
494 // redirect to another URL and terminate this process.
496 if(! function_exists('goaway')) {
497 function goaway($s) {
498 header("Location: $s");
502 // Generic XML return
503 // Outputs a basic dfrn XML status structure to STDOUT, with a <status> variable
504 // of $st and an optional text <message> of $message and terminates the current process.
506 if(! function_exists('xml_status')) {
507 function xml_status($st, $message = '') {
510 $xml_message = "\t<message>" . xmlify($message) . "</message>\r\n";
512 header( "Content-type: text/xml" );
513 echo '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
514 echo "<result>\r\n\t<status>$st</status>\r\n$xml_message</result>\r\n";
518 // Returns the uid of locally logged on user or false.
520 if(! function_exists('local_user')) {
521 function local_user() {
522 if((x($_SESSION,'authenticated')) && (x($_SESSION,'uid')))
523 return $_SESSION['uid'];
527 // Returns contact id of authenticated site visitor or false
529 if(! function_exists('remote_user')) {
530 function remote_user() {
531 if((x($_SESSION,'authenticated')) && (x($_SESSION,'visitor_id')))
532 return $_SESSION['visitor_id'];
536 // contents of $s are displayed prominently on the page the next time
537 // a page is loaded. Usually used for errors or alerts.
539 if(! function_exists('notice')) {
540 function notice($s) {
542 $_SESSION['sysmsg'] .= $s;
546 // wrapper around config to limit the text length of an incoming message
548 if(! function_exists('get_max_import_size')) {
549 function get_max_import_size() {
551 return ((x($a->config,'max_import_size')) ? $a->config['max_import_size'] : 0 );
555 // escape text ($str) for XML transport
556 // returns escaped text.
558 if(! function_exists('xmlify')) {
559 function xmlify($str) {
562 for($x = 0; $x < strlen($str); $x ++) {
593 $buffer = trim($buffer);
598 // pass xml escaped text ($s), returns unescaped text
600 if(! function_exists('unxmlify')) {
601 function unxmlify($s) {
602 $ret = str_replace('&','&', $s);
603 $ret = str_replace(array('<','>','"','''),array('<','>','"',"'"),$ret);
607 // convenience wrapper, reverse the operation "bin2hex"
609 if(! function_exists('hex2bin')) {
610 function hex2bin($s) {
611 return(pack("H*",$s));
614 // Automatic pagination.
615 // To use, get the count of total items.
616 // Then call $a->set_pager_total($number_items);
617 // Optionally call $a->set_pager_itemspage($n) to the number of items to display on each page
618 // Then call paginate($a) after the end of the display loop to insert the pager block on the page
619 // (assuming there are enough items to paginate).
620 // When using with SQL, the setting LIMIT %d, %d => $a->pager['start'],$a->pager['itemspage']
621 // will limit the results to the correct items for the current page.
622 // The actual page handling is then accomplished at the application layer.
624 if(! function_exists('paginate')) {
625 function paginate(&$a) {
627 $stripped = ereg_replace("(&page=[0-9]*)","",$_SERVER['QUERY_STRING']);
628 $stripped = str_replace('q=','',$stripped);
629 $stripped = trim($stripped,'/');
630 $url = $a->get_baseurl() . '/' . $stripped;
633 if($a->pager['total'] > $a->pager['itemspage']) {
634 $o .= '<div class="pager">';
635 if($a->pager['page'] != 1)
636 $o .= '<span class="pager_prev">'."<a href=\"$url".'&page='.($a->pager['page'] - 1).'">' . t('prev') . '</a></span> ';
638 $o .= "<span class=\"pager_first\"><a href=\"$url"."&page=1\">" . t('first') . "</a></span> ";
640 $numpages = $a->pager['total'] / $a->pager['itemspage'];
643 $numstop = $numpages;
646 $numstart = (($pagenum > 7) ? ($pagenum - 7) : 1);
647 $numstop = (($pagenum > ($numpages - 7)) ? $numpages : ($numstart + 14));
650 for($i = $numstart; $i <= $numstop; $i++){
651 if($i == $a->pager['page'])
652 $o .= '<span class="pager_current">'.(($i < 10) ? ' '.$i : $i);
654 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? ' '.$i : $i)."</a>";
658 if(($a->pager['total'] % $a->pager['itemspage']) != 0) {
659 if($i == $a->pager['page'])
660 $o .= '<span class="pager_current">'.(($i < 10) ? ' '.$i : $i);
662 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? ' '.$i : $i)."</a>";
666 $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
667 $o .= "<span class=\"pager_last\"><a href=\"$url"."&page=$lastpage\">" . t('last') . "</a></span> ";
669 if(($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0)
670 $o .= '<span class="pager_next">'."<a href=\"$url"."&page=".($a->pager['page'] + 1).'">' . t('next') . '</a></span>';
671 $o .= '</div>'."\r\n";
676 // Turn user/group ACLs stored as angle bracketed text into arrays
678 if(! function_exists('expand_acl')) {
679 function expand_acl($s) {
680 // turn string array of angle-bracketed elements into numeric array
681 // e.g. "<1><2><3>" => array(1,2,3);
685 $t = str_replace('<','',$s);
686 $a = explode('>',$t);
689 $ret[] = intval($aa);
695 // Used to wrap ACL elements in angle brackets for storage
697 if(! function_exists('sanitise_acl')) {
698 function sanitise_acl(&$item) {
700 $item = '<' . intval(notags(trim($item))) . '>';
705 // retrieve a "family" of config variables from database to cached storage
707 if(! function_exists('load_config')) {
708 function load_config($family) {
710 $r = q("SELECT * FROM `config` WHERE `cat` = '%s'",
716 $a->config[$family][$k] = $rr['v'];
721 // get a particular config variable given the family name
722 // and key. Returns false if not set.
723 // $instore is only used by the set_config function
724 // to determine if the key already exists in the DB
725 // If a key is found in the DB but doesn't exist in
726 // local config cache, pull it into the cache so we don't have
727 // to hit the DB again for this item.
729 if(! function_exists('get_config')) {
730 function get_config($family, $key, $instore = false) {
735 if(isset($a->config[$family][$key])) {
736 if($a->config[$family][$key] === '!<unset>!') {
739 return $a->config[$family][$key];
742 $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
747 $a->config[$family][$key] = $ret[0]['v'];
751 $a->config[$family][$key] = '!<unset>!';
756 // Store a config value ($value) in the category ($family)
757 // under the key ($key)
758 // Return the value, or false if the database update failed
760 if(! function_exists('set_config')) {
761 function set_config($family,$key,$value) {
764 $a->config[$family][$key] = $value;
766 if(get_config($family,$key,true) === false) {
767 $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
776 $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
786 // convert an XML document to a normalised, case-corrected array
789 if(! function_exists('convert_xml_element_to_array')) {
790 function convert_xml_element_to_array($xml_element, &$recursion_depth=0) {
792 // If we're getting too deep, bail out
793 if ($recursion_depth > 512) {
797 if (!is_string($xml_element) &&
798 !is_array($xml_element) &&
799 (get_class($xml_element) == 'SimpleXMLElement')) {
800 $xml_element_copy = $xml_element;
801 $xml_element = get_object_vars($xml_element);
804 if (is_array($xml_element)) {
805 $result_array = array();
806 if (count($xml_element) <= 0) {
807 return (trim(strval($xml_element_copy)));
810 foreach($xml_element as $key=>$value) {
813 $result_array[strtolower($key)] =
814 convert_xml_element_to_array($value, $recursion_depth);
817 if ($recursion_depth == 0) {
818 $temp_array = $result_array;
819 $result_array = array(
820 strtolower($xml_element_copy->getName()) => $temp_array,
824 return ($result_array);
827 return (trim(strval($xml_element)));
831 // Given an email style address, perform webfinger lookup and
832 // return the resulting DFRN profile URL.
833 // If this isn't an email style address just return $s.
834 // Return an empty string if email-style addresses but webfinger fails,
835 // or if the resultant personal XRD doesn't contain a DFRN profile.
837 if(! function_exists('webfinger')) {
838 function webfinger($s) {
839 if(! strstr($s,'@')) {
842 $host = substr($s,strpos($s,'@') + 1);
843 $url = 'http://' . $host . '/.well-known/host-meta' ;
844 $xml = fetch_url($url);
847 $h = simplexml_load_string($xml);
848 $arr = convert_xml_element_to_array($h);
850 if(! isset($arr['xrd']['link']))
853 $link = $arr['xrd']['link'];
854 if(! isset($link[0]))
855 $links = array($link);
859 foreach($links as $link)
860 if($link['@attributes']['rel'] && $link['@attributes']['rel'] === 'lrdd')
861 $tpl = $link['@attributes']['template'];
862 if((empty($tpl)) || (! strpos($tpl, '{uri}')))
865 $pxrd = str_replace('{uri}', urlencode('acct://'.$s), $tpl);
867 $xml = fetch_url($pxrd);
870 $h = simplexml_load_string($xml);
871 $arr = convert_xml_element_to_array($h);
873 if(! isset($arr['xrd']['link']))
876 $link = $arr['xrd']['link'];
877 if(! isset($link[0]))
878 $links = array($link);
882 foreach($links as $link)
883 if($link['@attributes']['rel'] == NAMESPACE_DFRN)
884 return $link['@attributes']['href'];
889 // Convert an ACL array to a storable string
891 if(! function_exists('perms2str')) {
892 function perms2str($p) {
896 array_walk($tmp,'sanitise_acl');
897 $ret = implode('',$tmp);
902 // generate a guaranteed unique (for this domain) item ID for ATOM
903 // safe from birthday paradox
905 if(! function_exists('item_new_uri')) {
906 function item_new_uri($hostname,$uid) {
910 $hash = random_string();
912 $uri = "urn:X-dfrn:" . $hostname . ':' . $uid . ':' . $hash;
914 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
918 } while($dups == true);
922 // Generate a guaranteed unique photo ID.
923 // safe from birthday paradox
925 if(! function_exists('photo_new_resource')) {
926 function photo_new_resource() {
930 $resource = hash('md5',uniqid(mt_rand(),true));
931 $r = q("SELECT `id` FROM `photo` WHERE `resource-id` = '%s' LIMIT 1",
936 } while($found == true);
941 // Returns logged in user ID
943 if(! function_exists('get_uid')) {
945 return ((x($_SESSION,'uid')) ? intval($_SESSION['uid']) : 0) ;
948 // Take a URL from the wild, prepend http:// if necessary
949 // and check DNS to see if it's real
950 // return true if it's OK, false if something is wrong with it
952 if(! function_exists('validate_url')) {
953 function validate_url(&$url) {
954 if(substr($url,0,4) != 'http')
955 $url = 'http://' . $url;
956 $h = parse_url($url);
961 if(! checkdnsrr($h['host'], 'ANY')) {
967 // Check $url against our list of allowed sites,
968 // wildcards allowed. If allowed_sites is unset return true;
969 // If url is allowed, return true.
970 // otherwise, return false
972 if(! function_exists('allowed_url')) {
973 function allowed_url($url) {
975 $h = parse_url($url);
981 $str_allowed = get_config('system','allowed_sites');
987 $host = strtolower($h['host']);
989 // always allow our own site
991 if($host == strtolower($_SERVER['SERVER_NAME']))
994 $fnmatch = function_exists('fnmatch');
995 $allowed = explode(',',$str_allowed);
997 if(count($allowed)) {
998 foreach($allowed as $a) {
999 $pat = strtolower(trim($a));
1000 if(($fnmatch && fnmatch($pat,$host)) || ($pat == $host)) {
1009 // check if email address is allowed to register here.
1010 // Compare against our list (wildcards allowed).
1011 // Returns false if not allowed, true if allowed or if
1012 // allowed list is not configured.
1014 if(! function_exists('allowed_email')) {
1015 function allowed_email($email) {
1018 $domain = strtolower(substr($email,strpos($email,'@') + 1));
1022 $str_allowed = get_config('system','allowed_email');
1028 $fnmatch = function_exists('fnmatch');
1029 $allowed = explode(',',$str_allowed);
1031 if(count($allowed)) {
1032 foreach($allowed as $a) {
1033 $pat = strtolower(trim($a));
1034 if(($fnmatch && fnmatch($pat,$host)) || ($pat == $host)) {
1043 // Format the like/dislike text for a profile item
1044 // $cnt = number of people who like/dislike the item
1045 // $arr = array of pre-linked names of likers/dislikers
1046 // $type = one of 'like, 'dislike'
1048 // returns formatted text
1050 if(! function_exists('format_like')) {
1051 function format_like($cnt,$arr,$type,$id) {
1053 $o .= $arr[0] . (($type === 'like') ? t(' likes this.') : t(' doesn\'t like this.')) . EOL ;
1055 $o .= '<span class="fakelink" onclick="openClose(\'' . $type . 'list-' . $id . '\');" >'
1056 . $cnt . ' ' . t('people') . '</span> ' . (($type === 'like') ? t('like this.') : t('don\'t like this.')) . EOL ;
1057 $total = count($arr);
1059 $arr = array_slice($arr,0,74);
1061 $arr[count($arr)-1] = t('and') . ' ' . $arr[count($arr)-1];
1062 $str = implode(', ', $arr);
1064 $str .= t(', and ') . $total - 75 . t(' other people');
1065 $str .= (($type === 'like') ? t(' like this.') : t(' don\'t like this.'));
1066 $o .= '<div id="' . $type . 'list-' . $id . '" style="display: none;" >' . $str . '</div>';
1072 // wrapper to load a view template, checking for alternate
1073 // languages before falling back to the default
1075 if(! function_exists('load_view_file')) {
1076 function load_view_file($s) {
1079 $lang = get_config('system','language');
1080 if($lang && file_exists("$d/$lang/$b"))
1081 return file_get_contents("$d/$lang/$b");
1082 return file_get_contents($s);