5 define ( 'BUILD_ID' , 1005 );
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/');
32 define ( 'ACTIVITY_LIKE', NAMESPACE_ACTIVITY_SCHEMA . 'like' );
33 define ( 'ACTIVITY_DISLIKE', NAMESPACE_DFRN . '/dislike' );
34 define ( 'ACTIVITY_OBJ_HEART', NAMESPACE_DFRN . '/heart' );
36 define ( 'ACTIVITY_FRIEND', NAMESPACE_ACTIVITY_SCHEMA . 'make-friend' );
37 define ( 'ACTIVITY_POST', NAMESPACE_ACTIVITY_SCHEMA . 'post' );
38 define ( 'ACTIVITY_UPDATE', NAMESPACE_ACTIVITY_SCHEMA . 'update' );
40 define ( 'ACTIVITY_OBJ_COMMENT', NAMESPACE_ACTIVITY_SCHEMA . 'comment' );
41 define ( 'ACTIVITY_OBJ_NOTE', NAMESPACE_ACTIVITY_SCHEMA . 'note' );
42 define ( 'ACTIVITY_OBJ_PERSON', NAMESPACE_ACTIVITY_SCHEMA . 'person' );
43 define ( 'ACTIVITY_OBJ_PHOTO', NAMESPACE_ACTIVITY_SCHEMA . 'photo' );
44 define ( 'ACTIVITY_OBJ_P_PHOTO', NAMESPACE_ACTIVITY_SCHEMA . 'profile-photo' );
45 define ( 'ACTIVITY_OBJ_ALBUM', NAMESPACE_ACTIVITY_SCHEMA . 'photo-album' );
47 define ( 'GRAVITY_PARENT', 0);
48 define ( 'GRAVITY_LIKE', 3);
49 define ( 'GRAVITY_COMMENT', 6);
52 // Our main application structure for the life of this page
53 // Primarily deals with the URL that got us here
54 // and tries to make some sense of it, and
55 // stores our page contents and config storage
56 // and anything else that might need to be passed around
57 // before we spit the page out.
59 if(! class_exists('App')) {
62 public $module_loaded = false;
71 public $error = false;
85 function __construct() {
87 $this->config = array();
88 $this->page = array();
89 $this->pager= array();
91 $this->scheme = ((isset($_SERVER['HTTPS'])
92 && ($_SERVER['HTTPS'])) ? 'https' : 'http' );
93 $this->hostname = str_replace('www.','',
94 $_SERVER['SERVER_NAME']);
95 set_include_path("include/$this->hostname"
96 . PATH_SEPARATOR . 'include'
97 . PATH_SEPARATOR . '.' );
99 if(substr($_SERVER['QUERY_STRING'],0,2) == "q=")
100 $_SERVER['QUERY_STRING'] = substr($_SERVER['QUERY_STRING'],2);
101 $this->cmd = trim($_GET['q'],'/');
104 $this->argv = explode('/',$this->cmd);
105 $this->argc = count($this->argv);
106 if((array_key_exists('0',$this->argv)) && strlen($this->argv[0])) {
107 $this->module = $this->argv[0];
110 $this->module = 'home';
113 if($this->cmd === '.well-known/host-meta')
114 require_once('include/hostxrd.php');
116 $this->pager['page'] = ((x($_GET,'page')) ? $_GET['page'] : 1);
117 $this->pager['itemspage'] = 50;
118 $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
119 $this->pager['total'] = 0;
122 function get_baseurl($ssl = false) {
123 if(strlen($this->baseurl))
124 return $this->baseurl;
126 $this->baseurl = (($ssl) ? 'https' : $this->scheme) . "://" . $this->hostname
127 . ((isset($this->path) && strlen($this->path))
128 ? '/' . $this->path : '' );
129 return $this->baseurl;
132 function set_baseurl($url) {
133 $this->baseurl = $url;
134 $this->hostname = basename($url);
137 function get_hostname() {
138 return $this->hostname;
141 function set_hostname($h) {
142 $this->hostname = $h;
145 function set_path($p) {
146 $this->path = ltrim(trim($p),'/');
149 function get_path() {
153 function set_pager_total($n) {
154 $this->pager['total'] = intval($n);
157 function set_pager_itemspage($n) {
158 $this->pager['itemspage'] = intval($n);
159 $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
163 function init_pagehead() {
164 $tpl = load_view_file("view/head.tpl");
165 $this->page['htmlhead'] = replace_macros($tpl,array(
166 '$baseurl' => $this->get_baseurl()
172 // retrieve the App structure
173 // useful in functions which require it but don't get it passed to them
175 if(! function_exists('get_app')) {
182 // Multi-purpose function to check variable state.
183 // Usage: x($var) or $x($array,'key')
184 // returns false if variable/key is not set
185 // if variable is set, returns 1 if has 'non-zero' value, otherwise returns 0.
186 // e.g. x('') or x(0) returns 0;
188 if(! function_exists('x')) {
189 function x($s,$k = NULL) {
191 if((is_array($s)) && (array_key_exists($k,$s))) {
209 // called from db initialisation if db is dead.
211 if(! function_exists('system_unavailable')) {
212 function system_unavailable() {
213 include('system_unavailable.php');
217 // Primarily involved with database upgrade, but also sets the
218 // base url for use in cmdline programs which don't have
219 // $_SERVER variables.
221 if(! function_exists('check_config')) {
222 function check_config(&$a) {
224 load_config('system');
226 $build = get_config('system','build');
228 $build = set_config('system','build',BUILD_ID);
230 $url = get_config('system','url');
232 $url = set_config('system','url',$a->get_baseurl());
234 if($build != BUILD_ID) {
235 $stored = intval($build);
236 $current = intval(BUILD_ID);
237 if(($stored < $current) && file_exists('update.php')) {
239 // We're reporting a different version than what is currently installed.
240 // Run any existing update scripts to bring the database up to current.
242 require_once('update.php');
243 for($x = $stored; $x < $current; $x ++) {
244 if(function_exists('update_' . $x)) {
245 $func = 'update_' . $x;
249 set_config('system','build', BUILD_ID);
256 // This is our template processor.
257 // $s is the string requiring macro substitution.
258 // $r is an array of key value pairs (search => replace)
259 // returns substituted string.
260 // WARNING: this is pretty basic, and doesn't properly handle search strings that are substrings of each other.
261 // For instance if 'test' => "foo" and 'testing' => "bar", testing could become either bar or fooing,
262 // depending on the order in which they were declared in the array.
264 if(! function_exists('replace_macros')) {
265 function replace_macros($s,$r) {
270 if(is_array($r) && count($r)) {
271 foreach ($r as $k => $v ) {
276 return str_replace($search,$replace,$s);
280 // load string tranlsation table for alternate language
282 if(! function_exists('load_translation_table')) {
283 function load_translation_table($lang) {
286 if(file_exists("view/$lang/strings.php"))
287 include("view/$lang/strings.php");
290 // translate string if translation exists
292 if(! function_exists('t')) {
298 return $a->strings[$s];
302 // curl wrapper. If binary flag is true, return binary
305 if(! function_exists('fetch_url')) {
306 function fetch_url($url,$binary = false) {
307 $ch = curl_init($url);
308 if(! $ch) return false;
310 curl_setopt($ch, CURLOPT_HEADER, 0);
311 curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
312 curl_setopt($ch, CURLOPT_MAXREDIRS,8);
313 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
315 // by default we will allow self-signed certs
316 // but you can override this
318 $check_cert = get_config('system','verifyssl');
319 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (($check_cert) ? true : false));
321 $prx = get_config('system','proxy');
323 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
324 curl_setopt($ch, CURLOPT_PROXY, $prx);
325 $prxusr = get_config('system','proxyuser');
327 curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
330 curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
337 // post request to $url. $params is an array of post variables.
339 if(! function_exists('post_url')) {
340 function post_url($url,$params) {
341 $ch = curl_init($url);
342 if(! $ch) return false;
344 curl_setopt($ch, CURLOPT_HEADER, 0);
345 curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
346 curl_setopt($ch, CURLOPT_MAXREDIRS,8);
347 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
348 curl_setopt($ch, CURLOPT_POST,1);
349 curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
350 $check_cert = get_config('system','verifyssl');
351 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (($check_cert) ? true : false));
352 $prx = get_config('system','proxy');
354 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
355 curl_setopt($ch, CURLOPT_PROXY, $prx);
356 $prxusr = get_config('system','proxyuser');
358 curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
366 // random hash, 64 chars
368 if(! function_exists('random_string')) {
369 function random_string() {
370 return(hash('sha256',uniqid(rand(),true)));
373 // This is our primary input filter. The high bit hack only involved some old
374 // IE browser, forget which.
375 // Use this on any text input where angle chars are not valid or permitted
376 // They will be replaced with safer brackets. This may be filtered further
377 // if these are not allowed either.
379 if(! function_exists('notags')) {
380 function notags($string) {
381 // protect against :<> with high-bit set
382 return(str_replace(array("<",">","\xBA","\xBC","\xBE"), array('[',']','','',''), $string));
385 // use this on "body" or "content" input where angle chars shouldn't be removed,
386 // and allow them to be safely displayed.
388 if(! function_exists('escape_tags')) {
389 function escape_tags($string) {
391 return(htmlspecialchars($string));
394 // wrapper for adding a login box. If $register == true provide a registration
395 // link. This will most always depend on the value of $a->config['register_policy'].
397 if(! function_exists('login')) {
398 function login($register = false) {
400 $register_html = (($register) ? load_view_file("view/register-link.tpl") : "");
403 if(x($_SESSION,'authenticated')) {
404 $o = load_view_file("view/logout.tpl");
407 $o = load_view_file("view/login.tpl");
409 $o = replace_macros($o,array('$register_html' => $register_html ));
414 // generate a string that's random, but usually pronounceable.
415 // used to generate initial passwords
417 if(! function_exists('autoname')) {
418 function autoname($len) {
420 $vowels = array('a','a','ai','au','e','e','e','ee','ea','i','ie','o','ou','u');
421 if(mt_rand(0,5) == 4)
439 's','sc','sh','sm','sp','st',
447 $midcons = array('ck','ct','gn','ld','lf','lm','lt','mb','mm', 'mn','mp',
448 'nd','ng','nk','nt','rn','rp','rt');
450 $noend = array('bl', 'br', 'cl','cr','dr','fl','fr','gl','gr',
451 'kh', 'kl','kr','mn','pl','pr','rh','tr','qu','wh');
453 $start = mt_rand(0,2);
461 for ($x = 0; $x < $len; $x ++) {
462 $r = mt_rand(0,count($table) - 1);
465 if($table == $vowels)
466 $table = array_merge($cons,$midcons);
472 $word = substr($word,0,$len);
474 foreach($noend as $noe) {
475 if((strlen($word) > 2) && (substr($word,-2) == $noe)) {
476 $word = substr($word,0,-1);
480 if(substr($word,-1) == 'q')
481 $word = substr($word,0,-1);
485 // Used to end the current process, after saving session state.
487 if(! function_exists('killme')) {
489 session_write_close();
493 // redirect to another URL and terminate this process.
495 if(! function_exists('goaway')) {
496 function goaway($s) {
497 header("Location: $s");
501 // Generic XML return
502 // Outputs a basic XML status structure to STDOUT, with a value variable
503 // of $st and terminates the current process.
505 if(! function_exists('xml_status')) {
506 function xml_status($st) {
507 header( "Content-type: text/xml" );
508 echo '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
509 echo "<result><status>$st</status></result>\r\n";
513 // Returns the uid of locally logged on user or false.
515 if(! function_exists('local_user')) {
516 function local_user() {
517 if((x($_SESSION,'authenticated')) && (x($_SESSION,'uid')))
518 return $_SESSION['uid'];
522 // Returns contact id of authenticated site visitor or false
524 if(! function_exists('remote_user')) {
525 function remote_user() {
526 if((x($_SESSION,'authenticated')) && (x($_SESSION,'visitor_id')))
527 return $_SESSION['visitor_id'];
531 // contents of $s are displayed prominently on the page the next time
532 // a page is loaded. Usually used for errors or alerts.
534 if(! function_exists('notice')) {
535 function notice($s) {
537 $_SESSION['sysmsg'] .= $s;
541 // wrapper around config to limit the text length of an incoming message
543 if(! function_exists('get_max_import_size')) {
544 function get_max_import_size() {
546 return ((x($a->config,'max_import_size')) ? $a->config['max_import_size'] : 0 );
550 // escape text ($str) for XML transport
551 // returns escaped text.
553 if(! function_exists('xmlify')) {
554 function xmlify($str) {
557 for($x = 0; $x < strlen($str); $x ++) {
588 $buffer = trim($buffer);
593 // pass xml escaped text ($s), returns unescaped text
595 if(! function_exists('unxmlify')) {
596 function unxmlify($s) {
597 $ret = str_replace('&','&', $s);
598 $ret = str_replace(array('<','>','"','''),array('<','>','"',"'"),$ret);
602 // convenience wrapper, reverse the operation "bin2hex"
604 if(! function_exists('hex2bin')) {
605 function hex2bin($s) {
606 return(pack("H*",$s));
609 // Automatic pagination.
610 // To use, get the count of total items.
611 // Then call $a->set_pager_total($number_items);
612 // Optionally call $a->set_pager_itemspage($n) to the number of items to display on each page
613 // Then call paginate($a) after the end of the display loop to insert the pager block on the page
614 // (assuming there are enough items to paginate).
615 // When using with SQL, the setting LIMIT %d, %d => $a->pager['start'],$a->pager['itemspage']
616 // will limit the results to the correct items for the current page.
617 // The actual page handling is then accomplished at the application layer.
619 if(! function_exists('paginate')) {
620 function paginate(&$a) {
622 $stripped = ereg_replace("(&page=[0-9]*)","",$_SERVER['QUERY_STRING']);
623 $stripped = str_replace('q=','',$stripped);
624 $stripped = trim($stripped,'/');
625 $url = $a->get_baseurl() . '/' . $stripped;
628 if($a->pager['total'] > $a->pager['itemspage']) {
629 $o .= '<div class="pager">';
630 if($a->pager['page'] != 1)
631 $o .= '<span class="pager_prev">'."<a href=\"$url".'&page='.($a->pager['page'] - 1).'">' . t('prev') . '</a></span> ';
633 $o .= "<span class=\"pager_first\"><a href=\"$url"."&page=1\">" . t('first') . "</a></span> ";
635 $numpages = $a->pager['total'] / $a->pager['itemspage'];
638 $numstop = $numpages;
641 $numstart = (($pagenum > 7) ? ($pagenum - 7) : 1);
642 $numstop = (($pagenum > ($numpages - 7)) ? $numpages : ($numstart + 14));
645 for($i = $numstart; $i <= $numstop; $i++){
646 if($i == $a->pager['page'])
647 $o .= '<span class="pager_current">'.(($i < 10) ? ' '.$i : $i);
649 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? ' '.$i : $i)."</a>";
653 if(($a->pager['total'] % $a->pager['itemspage']) != 0) {
654 if($i == $a->pager['page'])
655 $o .= '<span class="pager_current">'.(($i < 10) ? ' '.$i : $i);
657 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? ' '.$i : $i)."</a>";
661 $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
662 $o .= "<span class=\"pager_last\"><a href=\"$url"."&page=$lastpage\">" . t('last') . "</a></span> ";
664 if(($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0)
665 $o .= '<span class="pager_next">'."<a href=\"$url"."&page=".($a->pager['page'] + 1).'">' . t('next') . '</a></span>';
666 $o .= '</div>'."\r\n";
671 // Turn user/group ACLs stored as angle bracketed text into arrays
673 if(! function_exists('expand_acl')) {
674 function expand_acl($s) {
675 // turn string array of angle-bracketed elements into numeric array
676 // e.g. "<1><2><3>" => array(1,2,3);
680 $t = str_replace('<','',$s);
681 $a = explode('>',$t);
684 $ret[] = intval($aa);
690 // Used to wrap ACL elements in angle brackets for storage
692 if(! function_exists('sanitise_acl')) {
693 function sanitise_acl(&$item) {
695 $item = '<' . intval(notags(trim($item))) . '>';
700 // retrieve a "family" of config variables from database to cached storage
702 if(! function_exists('load_config')) {
703 function load_config($family) {
705 $r = q("SELECT * FROM `config` WHERE `cat` = '%s'",
711 $a->config[$family][$k] = $rr['v'];
716 // get a particular config variable given the family name
717 // and key. Returns false if not set.
718 // $instore is only used by the set_config function
719 // to determine if the key already exists in the DB
720 // If a key is found in the DB but doesn't exist in
721 // local config cache, pull it into the cache so we don't have
722 // to hit the DB again for this item.
724 if(! function_exists('get_config')) {
725 function get_config($family, $key, $instore = false) {
730 if(isset($a->config[$family][$key])) {
731 if($a->config[$family][$key] === '!<unset>!') {
734 return $a->config[$family][$key];
737 $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
742 $a->config[$family][$key] = $ret[0]['v'];
746 $a->config[$family][$key] = '!<unset>!';
751 // Store a config value ($value) in the category ($family)
752 // under the key ($key)
753 // Return the value, or false if the database update failed
755 if(! function_exists('set_config')) {
756 function set_config($family,$key,$value) {
759 $a->config[$family][$key] = $value;
761 if(get_config($family,$key,true) === false) {
762 $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
771 $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
781 // convert an XML document to a normalised, case-corrected array
784 if(! function_exists('convert_xml_element_to_array')) {
785 function convert_xml_element_to_array($xml_element, &$recursion_depth=0) {
787 // If we're getting too deep, bail out
788 if ($recursion_depth > 512) {
792 if (!is_string($xml_element) &&
793 !is_array($xml_element) &&
794 (get_class($xml_element) == 'SimpleXMLElement')) {
795 $xml_element_copy = $xml_element;
796 $xml_element = get_object_vars($xml_element);
799 if (is_array($xml_element)) {
800 $result_array = array();
801 if (count($xml_element) <= 0) {
802 return (trim(strval($xml_element_copy)));
805 foreach($xml_element as $key=>$value) {
808 $result_array[strtolower($key)] =
809 convert_xml_element_to_array($value, $recursion_depth);
812 if ($recursion_depth == 0) {
813 $temp_array = $result_array;
814 $result_array = array(
815 strtolower($xml_element_copy->getName()) => $temp_array,
819 return ($result_array);
822 return (trim(strval($xml_element)));
826 // Given an email style address, perform webfinger lookup and
827 // return the resulting DFRN profile URL.
828 // If this isn't an email style address just return $s.
829 // Return an empty string if email-style addresses but webfinger fails,
830 // or if the resultant personal XRD doesn't contain a DFRN profile.
832 if(! function_exists('webfinger')) {
833 function webfinger($s) {
834 if(! strstr($s,'@')) {
837 $host = substr($s,strpos($s,'@') + 1);
838 $url = 'http://' . $host . '/.well-known/host-meta' ;
839 $xml = fetch_url($url);
842 $h = simplexml_load_string($xml);
843 $arr = convert_xml_element_to_array($h);
845 if(! isset($arr['xrd']['link']))
848 $link = $arr['xrd']['link'];
849 if(! isset($link[0]))
850 $links = array($link);
854 foreach($links as $link)
855 if($link['@attributes']['rel'] && $link['@attributes']['rel'] === 'lrdd')
856 $tpl = $link['@attributes']['template'];
857 if((empty($tpl)) || (! strpos($tpl, '{uri}')))
860 $pxrd = str_replace('{uri}', urlencode('acct://'.$s), $tpl);
862 $xml = fetch_url($pxrd);
865 $h = simplexml_load_string($xml);
866 $arr = convert_xml_element_to_array($h);
868 if(! isset($arr['xrd']['link']))
871 $link = $arr['xrd']['link'];
872 if(! isset($link[0]))
873 $links = array($link);
877 foreach($links as $link)
878 if($link['@attributes']['rel'] == NAMESPACE_DFRN)
879 return $link['@attributes']['href'];
884 // Convert an ACL array to a storable string
886 if(! function_exists('perms2str')) {
887 function perms2str($p) {
891 array_walk($tmp,'sanitise_acl');
892 $ret = implode('',$tmp);
897 // generate a guaranteed unique (for this domain) item ID for ATOM
898 // safe from birthday paradox
900 if(! function_exists('item_new_uri')) {
901 function item_new_uri($hostname,$uid) {
905 $hash = random_string();
907 $uri = "urn:X-dfrn:" . $hostname . ':' . $uid . ':' . $hash;
909 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
913 } while($dups == true);
917 // Generate a guaranteed unique photo ID.
918 // safe from birthday paradox
920 if(! function_exists('photo_new_resource')) {
921 function photo_new_resource() {
925 $resource = hash('md5',uniqid(mt_rand(),true));
926 $r = q("SELECT `id` FROM `photo` WHERE `resource-id` = '%s' LIMIT 1",
931 } while($found == true);
936 // Returns logged in user ID
938 if(! function_exists('get_uid')) {
940 return ((x($_SESSION,'uid')) ? intval($_SESSION['uid']) : 0) ;
943 // Take a URL from the wild, prepend http:// if necessary
944 // and check DNS to see if it's real
945 // return true if it's OK, false if something is wrong with it
947 if(! function_exists('validate_url')) {
948 function validate_url(&$url) {
949 if(substr($url,0,4) != 'http')
950 $url = 'http://' . $url;
951 $h = parse_url($url);
956 if(! checkdnsrr($h['host'], 'ANY')) {
962 // Check $url against our list of allowed sites,
963 // wildcards allowed. If allowed_sites is unset return true;
964 // If url is allowed, return true.
965 // otherwise, return false
967 if(! function_exists('allowed_url')) {
968 function allowed_url($url) {
970 $h = parse_url($url);
976 $str_allowed = get_config('system','allowed_sites');
982 $host = strtolower($h['host']);
984 // always allow our own site
986 if($host == strtolower($_SERVER['SERVER_NAME']))
989 $fnmatch = function_exists('fnmatch');
990 $allowed = explode(',',$str_allowed);
992 if(count($allowed)) {
993 foreach($allowed as $a) {
994 $pat = strtolower(trim($a));
995 if(($fnmatch && fnmatch($pat,$host)) || ($pat == $host)) {
1004 // check if email address is allowed to register here.
1005 // Compare against our list (wildcards allowed).
1006 // Returns false if not allowed, true if allowed or if
1007 // allowed list is not configured.
1009 if(! function_exists('allowed_email')) {
1010 function allowed_email($email) {
1013 $domain = strtolower(substr($email,strpos($email,'@') + 1));
1017 $str_allowed = get_config('system','allowed_email');
1023 $fnmatch = function_exists('fnmatch');
1024 $allowed = explode(',',$str_allowed);
1026 if(count($allowed)) {
1027 foreach($allowed as $a) {
1028 $pat = strtolower(trim($a));
1029 if(($fnmatch && fnmatch($pat,$host)) || ($pat == $host)) {
1038 // Format the like/dislike text for a profile item
1039 // $cnt = number of people who like/dislike the item
1040 // $arr = array of pre-linked names of likers/dislikers
1041 // $type = one of 'like, 'dislike'
1043 // returns formatted text
1045 if(! function_exists('format_like')) {
1046 function format_like($cnt,$arr,$type,$id) {
1048 $o .= $arr[0] . (($type === 'like') ? t(' likes this.') : t(' doesn\'t like this.')) . EOL ;
1050 $o .= '<span class="fakelink" onclick="openClose(\'' . $type . 'list-' . $id . '\');" >'
1051 . $cnt . ' ' . t('people') . '</span> ' . (($type === 'like') ? t('like this.') : t('don\'t like this.')) . EOL ;
1052 $total = count($arr);
1054 $arr = array_slice($arr,0,74);
1056 $arr[count($arr)-1] = t('and') . ' ' . $arr[count($arr)-1];
1057 $str = implode(', ', $arr);
1059 $str .= t(', and ') . $total - 75 . t(' other people');
1060 $str .= (($type === 'like') ? t(' like this.') : t(' don\'t like this.'));
1061 $o .= '<div id="' . $type . 'list-' . $id . '" style="display: none;" >' . $str . '</div>';
1067 // wrapper to load a view template, checking for alternate
1068 // languages before falling back to the default
1070 if(! function_exists('load_view_file')) {
1071 function load_view_file($s) {
1074 $lang = get_config('system','language');
1075 if($lang && file_exists("$d/$lang/$b"))
1076 return file_get_contents("$d/$lang/$b");
1077 return file_get_contents($s);