5 define ( 'BUILD_ID' , 1007 );
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 // Maximum number of "people who like (or don't like) this" we will list by name
22 define ( 'MAX_LIKERS', 75);
25 define ( 'NOTIFY_INTRO', 0x0001 );
26 define ( 'NOTIFY_CONFIRM', 0x0002 );
27 define ( 'NOTIFY_WALL', 0x0004 );
28 define ( 'NOTIFY_COMMENT', 0x0008 );
29 define ( 'NOTIFY_MAIL', 0x0010 );
31 define ( 'NAMESPACE_DFRN' , 'http://purl.org/macgirvin/dfrn/1.0' );
32 define ( 'NAMESPACE_THREAD' , 'http://purl.org/syndication/thread/1.0' );
33 define ( 'NAMESPACE_TOMB' , 'http://purl.org/atompub/tombstones/1.0' );
34 define ( 'NAMESPACE_ACTIVITY', 'http://activitystrea.ms/spec/1.0/' );
35 define ( 'NAMESPACE_ACTIVITY_SCHEMA', 'http://activitystrea.ms/schema/1.0/');
36 define ( 'NAMESPACE_SALMON_ME', 'http://salmon-protocol.org/ns/magic-env');
38 define ( 'ACTIVITY_LIKE', NAMESPACE_ACTIVITY_SCHEMA . 'like' );
39 define ( 'ACTIVITY_DISLIKE', NAMESPACE_DFRN . '/dislike' );
40 define ( 'ACTIVITY_OBJ_HEART', NAMESPACE_DFRN . '/heart' );
42 define ( 'ACTIVITY_FRIEND', NAMESPACE_ACTIVITY_SCHEMA . 'make-friend' );
43 define ( 'ACTIVITY_POST', NAMESPACE_ACTIVITY_SCHEMA . 'post' );
44 define ( 'ACTIVITY_UPDATE', NAMESPACE_ACTIVITY_SCHEMA . 'update' );
46 define ( 'ACTIVITY_OBJ_COMMENT', NAMESPACE_ACTIVITY_SCHEMA . 'comment' );
47 define ( 'ACTIVITY_OBJ_NOTE', NAMESPACE_ACTIVITY_SCHEMA . 'note' );
48 define ( 'ACTIVITY_OBJ_PERSON', NAMESPACE_ACTIVITY_SCHEMA . 'person' );
49 define ( 'ACTIVITY_OBJ_PHOTO', NAMESPACE_ACTIVITY_SCHEMA . 'photo' );
50 define ( 'ACTIVITY_OBJ_P_PHOTO', NAMESPACE_ACTIVITY_SCHEMA . 'profile-photo' );
51 define ( 'ACTIVITY_OBJ_ALBUM', NAMESPACE_ACTIVITY_SCHEMA . 'photo-album' );
53 define ( 'GRAVITY_PARENT', 0);
54 define ( 'GRAVITY_LIKE', 3);
55 define ( 'GRAVITY_COMMENT', 6);
58 // Our main application structure for the life of this page
59 // Primarily deals with the URL that got us here
60 // and tries to make some sense of it, and
61 // stores our page contents and config storage
62 // and anything else that might need to be passed around
63 // before we spit the page out.
65 if(! class_exists('App')) {
68 public $module_loaded = false;
77 public $error = false;
91 function __construct() {
93 $this->config = array();
94 $this->page = array();
95 $this->pager= array();
97 $this->scheme = ((isset($_SERVER['HTTPS'])
98 && ($_SERVER['HTTPS'])) ? 'https' : 'http' );
99 $this->hostname = str_replace('www.','',
100 $_SERVER['SERVER_NAME']);
101 set_include_path("include/$this->hostname"
102 . PATH_SEPARATOR . 'include'
103 . PATH_SEPARATOR . '.' );
105 if(substr($_SERVER['QUERY_STRING'],0,2) == "q=")
106 $_SERVER['QUERY_STRING'] = substr($_SERVER['QUERY_STRING'],2);
107 $this->cmd = trim($_GET['q'],'/');
110 $this->argv = explode('/',$this->cmd);
111 $this->argc = count($this->argv);
112 if((array_key_exists('0',$this->argv)) && strlen($this->argv[0])) {
113 $this->module = $this->argv[0];
116 $this->module = 'home';
119 if($this->cmd === '.well-known/host-meta')
120 require_once('include/hostxrd.php');
122 $this->pager['page'] = ((x($_GET,'page')) ? $_GET['page'] : 1);
123 $this->pager['itemspage'] = 50;
124 $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
125 $this->pager['total'] = 0;
128 function get_baseurl($ssl = false) {
129 if(strlen($this->baseurl))
130 return $this->baseurl;
132 $this->baseurl = (($ssl) ? 'https' : $this->scheme) . "://" . $this->hostname
133 . ((isset($this->path) && strlen($this->path))
134 ? '/' . $this->path : '' );
135 return $this->baseurl;
138 function set_baseurl($url) {
139 $this->baseurl = $url;
140 $this->hostname = basename($url);
143 function get_hostname() {
144 return $this->hostname;
147 function set_hostname($h) {
148 $this->hostname = $h;
151 function set_path($p) {
152 $this->path = ltrim(trim($p),'/');
155 function get_path() {
159 function set_pager_total($n) {
160 $this->pager['total'] = intval($n);
163 function set_pager_itemspage($n) {
164 $this->pager['itemspage'] = intval($n);
165 $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
169 function init_pagehead() {
170 $tpl = load_view_file("view/head.tpl");
171 $this->page['htmlhead'] = replace_macros($tpl,array(
172 '$baseurl' => $this->get_baseurl()
178 // retrieve the App structure
179 // useful in functions which require it but don't get it passed to them
181 if(! function_exists('get_app')) {
188 // Multi-purpose function to check variable state.
189 // Usage: x($var) or $x($array,'key')
190 // returns false if variable/key is not set
191 // if variable is set, returns 1 if has 'non-zero' value, otherwise returns 0.
192 // e.g. x('') or x(0) returns 0;
194 if(! function_exists('x')) {
195 function x($s,$k = NULL) {
197 if((is_array($s)) && (array_key_exists($k,$s))) {
215 // called from db initialisation if db is dead.
217 if(! function_exists('system_unavailable')) {
218 function system_unavailable() {
219 include('system_unavailable.php');
223 // Primarily involved with database upgrade, but also sets the
224 // base url for use in cmdline programs which don't have
225 // $_SERVER variables.
227 if(! function_exists('check_config')) {
228 function check_config(&$a) {
230 load_config('system');
232 $build = get_config('system','build');
234 $build = set_config('system','build',BUILD_ID);
236 $url = get_config('system','url');
238 $url = set_config('system','url',$a->get_baseurl());
240 if($build != BUILD_ID) {
241 $stored = intval($build);
242 $current = intval(BUILD_ID);
243 if(($stored < $current) && file_exists('update.php')) {
244 // We're reporting a different version than what is currently installed.
245 // Run any existing update scripts to bring the database up to current.
247 require_once('update.php');
248 for($x = $stored; $x < $current; $x ++) {
249 if(function_exists('update_' . $x)) {
250 $func = 'update_' . $x;
254 set_config('system','build', BUILD_ID);
261 // This is our template processor.
262 // $s is the string requiring macro substitution.
263 // $r is an array of key value pairs (search => replace)
264 // returns substituted string.
265 // WARNING: this is pretty basic, and doesn't properly handle search strings that are substrings of each other.
266 // For instance if 'test' => "foo" and 'testing' => "bar", testing could become either bar or fooing,
267 // depending on the order in which they were declared in the array.
269 if(! function_exists('replace_macros')) {
270 function replace_macros($s,$r) {
275 if(is_array($r) && count($r)) {
276 foreach ($r as $k => $v ) {
281 return str_replace($search,$replace,$s);
285 // load string tranlsation table for alternate language
287 if(! function_exists('load_translation_table')) {
288 function load_translation_table($lang) {
291 if(file_exists("view/$lang/strings.php"))
292 include("view/$lang/strings.php");
295 // translate string if translation exists
297 if(! function_exists('t')) {
303 return $a->strings[$s];
307 // curl wrapper. If binary flag is true, return binary
310 if(! function_exists('fetch_url')) {
311 function fetch_url($url,$binary = false) {
312 $ch = curl_init($url);
313 if(! $ch) return false;
315 curl_setopt($ch, CURLOPT_HEADER, 0);
316 curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
317 curl_setopt($ch, CURLOPT_MAXREDIRS,8);
318 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
320 // by default we will allow self-signed certs
321 // but you can override this
323 $check_cert = get_config('system','verifyssl');
324 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (($check_cert) ? true : false));
326 $prx = get_config('system','proxy');
328 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
329 curl_setopt($ch, CURLOPT_PROXY, $prx);
330 $prxusr = get_config('system','proxyuser');
332 curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
335 curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
342 // post request to $url. $params is an array of post variables.
344 if(! function_exists('post_url')) {
345 function post_url($url,$params) {
346 $ch = curl_init($url);
347 if(! $ch) return false;
349 curl_setopt($ch, CURLOPT_HEADER, 0);
350 curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
351 curl_setopt($ch, CURLOPT_MAXREDIRS,8);
352 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
353 curl_setopt($ch, CURLOPT_POST,1);
354 curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
355 $check_cert = get_config('system','verifyssl');
356 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (($check_cert) ? true : false));
357 $prx = get_config('system','proxy');
359 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
360 curl_setopt($ch, CURLOPT_PROXY, $prx);
361 $prxusr = get_config('system','proxyuser');
363 curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
371 // random hash, 64 chars
373 if(! function_exists('random_string')) {
374 function random_string() {
375 return(hash('sha256',uniqid(rand(),true)));
378 // This is our primary input filter. The high bit hack only involved some old
379 // IE browser, forget which.
380 // Use this on any text input where angle chars are not valid or permitted
381 // They will be replaced with safer brackets. This may be filtered further
382 // if these are not allowed either.
384 if(! function_exists('notags')) {
385 function notags($string) {
386 // protect against :<> with high-bit set
387 return(str_replace(array("<",">","\xBA","\xBC","\xBE"), array('[',']','','',''), $string));
390 // use this on "body" or "content" input where angle chars shouldn't be removed,
391 // and allow them to be safely displayed.
393 if(! function_exists('escape_tags')) {
394 function escape_tags($string) {
396 return(htmlspecialchars($string));
399 // wrapper for adding a login box. If $register == true provide a registration
400 // link. This will most always depend on the value of $a->config['register_policy'].
402 if(! function_exists('login')) {
403 function login($register = false) {
405 $register_html = (($register) ? load_view_file("view/register-link.tpl") : "");
408 if(x($_SESSION,'authenticated')) {
409 $o = load_view_file("view/logout.tpl");
412 $o = load_view_file("view/login.tpl");
414 $o = replace_macros($o,array('$register_html' => $register_html ));
419 // generate a string that's random, but usually pronounceable.
420 // used to generate initial passwords
422 if(! function_exists('autoname')) {
423 function autoname($len) {
425 $vowels = array('a','a','ai','au','e','e','e','ee','ea','i','ie','o','ou','u');
426 if(mt_rand(0,5) == 4)
444 's','sc','sh','sm','sp','st',
452 $midcons = array('ck','ct','gn','ld','lf','lm','lt','mb','mm', 'mn','mp',
453 'nd','ng','nk','nt','rn','rp','rt');
455 $noend = array('bl', 'br', 'cl','cr','dr','fl','fr','gl','gr',
456 'kh', 'kl','kr','mn','pl','pr','rh','tr','qu','wh');
458 $start = mt_rand(0,2);
466 for ($x = 0; $x < $len; $x ++) {
467 $r = mt_rand(0,count($table) - 1);
470 if($table == $vowels)
471 $table = array_merge($cons,$midcons);
477 $word = substr($word,0,$len);
479 foreach($noend as $noe) {
480 if((strlen($word) > 2) && (substr($word,-2) == $noe)) {
481 $word = substr($word,0,-1);
485 if(substr($word,-1) == 'q')
486 $word = substr($word,0,-1);
490 // Used to end the current process, after saving session state.
492 if(! function_exists('killme')) {
494 session_write_close();
498 // redirect to another URL and terminate this process.
500 if(! function_exists('goaway')) {
501 function goaway($s) {
502 header("Location: $s");
506 // Generic XML return
507 // Outputs a basic dfrn XML status structure to STDOUT, with a <status> variable
508 // of $st and an optional text <message> of $message and terminates the current process.
510 if(! function_exists('xml_status')) {
511 function xml_status($st, $message = '') {
514 $xml_message = "\t<message>" . xmlify($message) . "</message>\r\n";
516 header( "Content-type: text/xml" );
517 echo '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
518 echo "<result>\r\n\t<status>$st</status>\r\n$xml_message</result>\r\n";
522 // Returns the uid of locally logged on user or false.
524 if(! function_exists('local_user')) {
525 function local_user() {
526 if((x($_SESSION,'authenticated')) && (x($_SESSION,'uid')))
527 return $_SESSION['uid'];
531 // Returns contact id of authenticated site visitor or false
533 if(! function_exists('remote_user')) {
534 function remote_user() {
535 if((x($_SESSION,'authenticated')) && (x($_SESSION,'visitor_id')))
536 return $_SESSION['visitor_id'];
540 // contents of $s are displayed prominently on the page the next time
541 // a page is loaded. Usually used for errors or alerts.
543 if(! function_exists('notice')) {
544 function notice($s) {
546 $_SESSION['sysmsg'] .= $s;
550 // wrapper around config to limit the text length of an incoming message
552 if(! function_exists('get_max_import_size')) {
553 function get_max_import_size() {
555 return ((x($a->config,'max_import_size')) ? $a->config['max_import_size'] : 0 );
559 // escape text ($str) for XML transport
560 // returns escaped text.
562 if(! function_exists('xmlify')) {
563 function xmlify($str) {
566 for($x = 0; $x < strlen($str); $x ++) {
597 $buffer = trim($buffer);
602 // pass xml escaped text ($s), returns unescaped text
604 if(! function_exists('unxmlify')) {
605 function unxmlify($s) {
606 $ret = str_replace('&','&', $s);
607 $ret = str_replace(array('<','>','"','''),array('<','>','"',"'"),$ret);
611 // convenience wrapper, reverse the operation "bin2hex"
613 if(! function_exists('hex2bin')) {
614 function hex2bin($s) {
615 return(pack("H*",$s));
618 // Automatic pagination.
619 // To use, get the count of total items.
620 // Then call $a->set_pager_total($number_items);
621 // Optionally call $a->set_pager_itemspage($n) to the number of items to display on each page
622 // Then call paginate($a) after the end of the display loop to insert the pager block on the page
623 // (assuming there are enough items to paginate).
624 // When using with SQL, the setting LIMIT %d, %d => $a->pager['start'],$a->pager['itemspage']
625 // will limit the results to the correct items for the current page.
626 // The actual page handling is then accomplished at the application layer.
628 if(! function_exists('paginate')) {
629 function paginate(&$a) {
631 $stripped = ereg_replace("(&page=[0-9]*)","",$_SERVER['QUERY_STRING']);
632 $stripped = str_replace('q=','',$stripped);
633 $stripped = trim($stripped,'/');
634 $url = $a->get_baseurl() . '/' . $stripped;
637 if($a->pager['total'] > $a->pager['itemspage']) {
638 $o .= '<div class="pager">';
639 if($a->pager['page'] != 1)
640 $o .= '<span class="pager_prev">'."<a href=\"$url".'&page='.($a->pager['page'] - 1).'">' . t('prev') . '</a></span> ';
642 $o .= "<span class=\"pager_first\"><a href=\"$url"."&page=1\">" . t('first') . "</a></span> ";
644 $numpages = $a->pager['total'] / $a->pager['itemspage'];
647 $numstop = $numpages;
650 $numstart = (($pagenum > 7) ? ($pagenum - 7) : 1);
651 $numstop = (($pagenum > ($numpages - 7)) ? $numpages : ($numstart + 14));
654 for($i = $numstart; $i <= $numstop; $i++){
655 if($i == $a->pager['page'])
656 $o .= '<span class="pager_current">'.(($i < 10) ? ' '.$i : $i);
658 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? ' '.$i : $i)."</a>";
662 if(($a->pager['total'] % $a->pager['itemspage']) != 0) {
663 if($i == $a->pager['page'])
664 $o .= '<span class="pager_current">'.(($i < 10) ? ' '.$i : $i);
666 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? ' '.$i : $i)."</a>";
670 $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
671 $o .= "<span class=\"pager_last\"><a href=\"$url"."&page=$lastpage\">" . t('last') . "</a></span> ";
673 if(($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0)
674 $o .= '<span class="pager_next">'."<a href=\"$url"."&page=".($a->pager['page'] + 1).'">' . t('next') . '</a></span>';
675 $o .= '</div>'."\r\n";
680 // Turn user/group ACLs stored as angle bracketed text into arrays
682 if(! function_exists('expand_acl')) {
683 function expand_acl($s) {
684 // turn string array of angle-bracketed elements into numeric array
685 // e.g. "<1><2><3>" => array(1,2,3);
689 $t = str_replace('<','',$s);
690 $a = explode('>',$t);
693 $ret[] = intval($aa);
699 // Used to wrap ACL elements in angle brackets for storage
701 if(! function_exists('sanitise_acl')) {
702 function sanitise_acl(&$item) {
704 $item = '<' . intval(notags(trim($item))) . '>';
709 // retrieve a "family" of config variables from database to cached storage
711 if(! function_exists('load_config')) {
712 function load_config($family) {
714 $r = q("SELECT * FROM `config` WHERE `cat` = '%s'",
720 $a->config[$family][$k] = $rr['v'];
725 // get a particular config variable given the family name
726 // and key. Returns false if not set.
727 // $instore is only used by the set_config function
728 // to determine if the key already exists in the DB
729 // If a key is found in the DB but doesn't exist in
730 // local config cache, pull it into the cache so we don't have
731 // to hit the DB again for this item.
733 if(! function_exists('get_config')) {
734 function get_config($family, $key, $instore = false) {
739 if(isset($a->config[$family][$key])) {
740 if($a->config[$family][$key] === '!<unset>!') {
743 return $a->config[$family][$key];
746 $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
751 $a->config[$family][$key] = $ret[0]['v'];
755 $a->config[$family][$key] = '!<unset>!';
760 // Store a config value ($value) in the category ($family)
761 // under the key ($key)
762 // Return the value, or false if the database update failed
764 if(! function_exists('set_config')) {
765 function set_config($family,$key,$value) {
768 $a->config[$family][$key] = $value;
770 if(get_config($family,$key,true) === false) {
771 $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
780 $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
790 // convert an XML document to a normalised, case-corrected array
793 if(! function_exists('convert_xml_element_to_array')) {
794 function convert_xml_element_to_array($xml_element, &$recursion_depth=0) {
796 // If we're getting too deep, bail out
797 if ($recursion_depth > 512) {
801 if (!is_string($xml_element) &&
802 !is_array($xml_element) &&
803 (get_class($xml_element) == 'SimpleXMLElement')) {
804 $xml_element_copy = $xml_element;
805 $xml_element = get_object_vars($xml_element);
808 if (is_array($xml_element)) {
809 $result_array = array();
810 if (count($xml_element) <= 0) {
811 return (trim(strval($xml_element_copy)));
814 foreach($xml_element as $key=>$value) {
817 $result_array[strtolower($key)] =
818 convert_xml_element_to_array($value, $recursion_depth);
821 if ($recursion_depth == 0) {
822 $temp_array = $result_array;
823 $result_array = array(
824 strtolower($xml_element_copy->getName()) => $temp_array,
828 return ($result_array);
831 return (trim(strval($xml_element)));
835 // Given an email style address, perform webfinger lookup and
836 // return the resulting DFRN profile URL.
837 // If this isn't an email style address just return $s.
838 // Return an empty string if email-style addresses but webfinger fails,
839 // or if the resultant personal XRD doesn't contain a DFRN profile.
841 if(! function_exists('webfinger_dfrn')) {
842 function webfinger_dfrn($s) {
843 if(! strstr($s,'@')) {
846 $links = webfinger($s);
848 foreach($links as $link)
849 if($link['@attributes']['rel'] === NAMESPACE_DFRN)
850 return $link['@attributes']['href'];
855 // Given an email style address, perform webfinger lookup and
856 // return the array of link attributes from the personal XRD file.
857 // On error/failure return an empty array.
860 if(! function_exists('webfinger')) {
861 function webfinger($s) {
864 $host = substr($s,strpos($s,'@') + 1);
867 $tpl = fetch_lrdd_template($host);
869 $pxrd = str_replace('{uri}', urlencode('acct://'.$s), $tpl);
870 $links = fetch_xrd_links($pxrd);
871 if(! count($links)) {
872 // try without the double slashes
873 $pxrd = str_replace('{uri}', urlencode('acct:'.$s), $tpl);
874 $links = fetch_xrd_links($pxrd);
882 // Given a host name, locate the LRDD template from that
883 // host. Returns the LRDD template or an empty string on
886 if(! function_exists('fetch_lrdd_template')) {
887 function fetch_lrdd_template($host) {
889 $url = 'http://' . $host . '/.well-known/host-meta' ;
890 $links = fetch_xrd_links($url);
892 foreach($links as $link)
893 if($link['@attributes']['rel'] && $link['@attributes']['rel'] === 'lrdd')
894 $tpl = $link['@attributes']['template'];
896 if(! strpos($tpl,'{uri}'))
901 // Given a URL, retrieve the page as an XRD document.
902 // Return an array of links.
903 // on error/failure return empty array.
905 if(! function_exists('fetch_xrd_links')) {
906 function fetch_xrd_links($url) {
908 $xml = fetch_url($url);
911 $h = simplexml_load_string($xml);
912 $arr = convert_xml_element_to_array($h);
914 if(isset($arr['xrd']['link'])) {
915 $link = $arr['xrd']['link'];
916 if(! isset($link[0]))
917 $links = array($link);
925 // Convert an ACL array to a storable string
927 if(! function_exists('perms2str')) {
928 function perms2str($p) {
932 array_walk($tmp,'sanitise_acl');
933 $ret = implode('',$tmp);
938 // generate a guaranteed unique (for this domain) item ID for ATOM
939 // safe from birthday paradox
941 if(! function_exists('item_new_uri')) {
942 function item_new_uri($hostname,$uid) {
946 $hash = random_string();
948 $uri = "urn:X-dfrn:" . $hostname . ':' . $uid . ':' . $hash;
950 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
954 } while($dups == true);
958 // Generate a guaranteed unique photo ID.
959 // safe from birthday paradox
961 if(! function_exists('photo_new_resource')) {
962 function photo_new_resource() {
966 $resource = hash('md5',uniqid(mt_rand(),true));
967 $r = q("SELECT `id` FROM `photo` WHERE `resource-id` = '%s' LIMIT 1",
972 } while($found == true);
977 // Returns logged in user ID
979 if(! function_exists('get_uid')) {
981 return ((x($_SESSION,'uid')) ? intval($_SESSION['uid']) : 0) ;
984 // Take a URL from the wild, prepend http:// if necessary
985 // and check DNS to see if it's real
986 // return true if it's OK, false if something is wrong with it
988 if(! function_exists('validate_url')) {
989 function validate_url(&$url) {
990 if(substr($url,0,4) != 'http')
991 $url = 'http://' . $url;
992 $h = parse_url($url);
997 if(! checkdnsrr($h['host'], 'ANY')) {
1003 // Check $url against our list of allowed sites,
1004 // wildcards allowed. If allowed_sites is unset return true;
1005 // If url is allowed, return true.
1006 // otherwise, return false
1008 if(! function_exists('allowed_url')) {
1009 function allowed_url($url) {
1011 $h = parse_url($url);
1017 $str_allowed = get_config('system','allowed_sites');
1023 $host = strtolower($h['host']);
1025 // always allow our own site
1027 if($host == strtolower($_SERVER['SERVER_NAME']))
1030 $fnmatch = function_exists('fnmatch');
1031 $allowed = explode(',',$str_allowed);
1033 if(count($allowed)) {
1034 foreach($allowed as $a) {
1035 $pat = strtolower(trim($a));
1036 if(($fnmatch && fnmatch($pat,$host)) || ($pat == $host)) {
1045 // check if email address is allowed to register here.
1046 // Compare against our list (wildcards allowed).
1047 // Returns false if not allowed, true if allowed or if
1048 // allowed list is not configured.
1050 if(! function_exists('allowed_email')) {
1051 function allowed_email($email) {
1054 $domain = strtolower(substr($email,strpos($email,'@') + 1));
1058 $str_allowed = get_config('system','allowed_email');
1064 $fnmatch = function_exists('fnmatch');
1065 $allowed = explode(',',$str_allowed);
1067 if(count($allowed)) {
1068 foreach($allowed as $a) {
1069 $pat = strtolower(trim($a));
1070 if(($fnmatch && fnmatch($pat,$host)) || ($pat == $host)) {
1079 // Format the like/dislike text for a profile item
1080 // $cnt = number of people who like/dislike the item
1081 // $arr = array of pre-linked names of likers/dislikers
1082 // $type = one of 'like, 'dislike'
1084 // returns formatted text
1086 if(! function_exists('format_like')) {
1087 function format_like($cnt,$arr,$type,$id) {
1089 $o .= $arr[0] . (($type === 'like') ? t(' likes this.') : t(' doesn\'t like this.')) . EOL ;
1091 $o .= '<span class="fakelink" onclick="openClose(\'' . $type . 'list-' . $id . '\');" >'
1092 . $cnt . ' ' . t('people') . '</span> ' . (($type === 'like') ? t('like this.') : t('don\'t like this.')) . EOL ;
1093 $total = count($arr);
1094 if($total >= MAX_LIKERS)
1095 $arr = array_slice($arr, 0, MAX_LIKERS - 1);
1096 if($total < MAX_LIKERS)
1097 $arr[count($arr)-1] = t('and') . ' ' . $arr[count($arr)-1];
1098 $str = implode(', ', $arr);
1099 if($total >= MAX_LIKERS)
1100 $str .= t(', and ') . $total - MAX_LIKERS . t(' other people');
1101 $str .= (($type === 'like') ? t(' like this.') : t(' don\'t like this.'));
1102 $o .= '<div id="' . $type . 'list-' . $id . '" style="display: none;" >' . $str . '</div>';
1108 // wrapper to load a view template, checking for alternate
1109 // languages before falling back to the default
1111 if(! function_exists('load_view_file')) {
1112 function load_view_file($s) {
1115 $lang = get_config('system','language');
1116 if($lang && file_exists("$d/$lang/$b"))
1117 return file_get_contents("$d/$lang/$b");
1118 return file_get_contents($s);