5 define ( 'BUILD_ID', 1023 );
6 define ( 'DFRN_PROTOCOL_VERSION', '2.0' );
8 define ( 'EOL', "<br />\r\n" );
9 define ( 'ATOM_TIME', 'Y-m-d\TH:i:s\Z' );
10 define ( 'DOWN_ARROW', '⇩' );
16 define ( 'LOGGER_NORMAL', 0 );
17 define ( 'LOGGER_TRACE', 1 );
18 define ( 'LOGGER_DEBUG', 2 );
19 define ( 'LOGGER_DATA', 3 );
20 define ( 'LOGGER_ALL', 4 );
23 * registration policies
26 define ( 'REGISTER_CLOSED', 0 );
27 define ( 'REGISTER_APPROVE', 1 );
28 define ( 'REGISTER_OPEN', 2 );
34 define ( 'REL_VIP', 1);
35 define ( 'REL_FAN', 2);
36 define ( 'REL_BUD', 3);
43 * PAGE_NORMAL is a typical personal profile account
44 * PAGE_SOAPBOX automatically approves all friend requests as REL_FAN, (readonly)
45 * PAGE_COMMUNITY automatically approves all friend requests as REL_FAN, but with
46 * write access to wall and comments (no email and not included in page owner's ACL lists)
47 * PAGE_FREELOVE automatically approves all friend requests as full friends (REL_BUD).
51 define ( 'PAGE_NORMAL', 0 );
52 define ( 'PAGE_SOAPBOX', 1 );
53 define ( 'PAGE_COMMUNITY', 2 );
54 define ( 'PAGE_FREELOVE', 3 );
57 * Maximum number of "people who like (or don't like) this" that we will list by name
60 define ( 'MAX_LIKERS', 75);
63 * email notification options
66 define ( 'NOTIFY_INTRO', 0x0001 );
67 define ( 'NOTIFY_CONFIRM', 0x0002 );
68 define ( 'NOTIFY_WALL', 0x0004 );
69 define ( 'NOTIFY_COMMENT', 0x0008 );
70 define ( 'NOTIFY_MAIL', 0x0010 );
73 * various namespaces we may need to parse
76 define ( 'NAMESPACE_DFRN' , 'http://purl.org/macgirvin/dfrn/1.0' );
77 define ( 'NAMESPACE_THREAD' , 'http://purl.org/syndication/thread/1.0' );
78 define ( 'NAMESPACE_TOMB' , 'http://purl.org/atompub/tombstones/1.0' );
79 define ( 'NAMESPACE_ACTIVITY', 'http://activitystrea.ms/spec/1.0/' );
80 define ( 'NAMESPACE_ACTIVITY_SCHEMA', 'http://activitystrea.ms/schema/1.0/' );
81 define ( 'NAMESPACE_MEDIA', 'http://purl.org/syndication/atommedia' );
82 define ( 'NAMESPACE_SALMON_ME', 'http://salmon-protocol.org/ns/magic-env' );
83 define ( 'NAMESPACE_OSTATUSSUB', 'http://ostatus.org/schema/1.0/subscribe' );
84 define ( 'NAMESPACE_GEORSS', 'http://www.georss.org/georss' );
85 define ( 'NAMESPACE_POCO', 'http://portablecontacts.net/spec/1.0' );
86 define ( 'NAMESPACE_FEED', 'http://schemas.google.com/g/2010#updates-from' );
89 * activity stream defines
92 define ( 'ACTIVITY_LIKE', NAMESPACE_ACTIVITY_SCHEMA . 'like' );
93 define ( 'ACTIVITY_DISLIKE', NAMESPACE_DFRN . '/dislike' );
94 define ( 'ACTIVITY_OBJ_HEART', NAMESPACE_DFRN . '/heart' );
96 define ( 'ACTIVITY_FRIEND', NAMESPACE_ACTIVITY_SCHEMA . 'make-friend' );
97 define ( 'ACTIVITY_FOLLOW', NAMESPACE_ACTIVITY_SCHEMA . 'follow' );
98 define ( 'ACTIVITY_UNFOLLOW', NAMESPACE_ACTIVITY_SCHEMA . 'unfollow' );
99 define ( 'ACTIVITY_POST', NAMESPACE_ACTIVITY_SCHEMA . 'post' );
100 define ( 'ACTIVITY_UPDATE', NAMESPACE_ACTIVITY_SCHEMA . 'update' );
101 define ( 'ACTIVITY_TAG', NAMESPACE_ACTIVITY_SCHEMA . 'tag' );
103 define ( 'ACTIVITY_OBJ_COMMENT', NAMESPACE_ACTIVITY_SCHEMA . 'comment' );
104 define ( 'ACTIVITY_OBJ_NOTE', NAMESPACE_ACTIVITY_SCHEMA . 'note' );
105 define ( 'ACTIVITY_OBJ_PERSON', NAMESPACE_ACTIVITY_SCHEMA . 'person' );
106 define ( 'ACTIVITY_OBJ_PHOTO', NAMESPACE_ACTIVITY_SCHEMA . 'photo' );
107 define ( 'ACTIVITY_OBJ_P_PHOTO', NAMESPACE_ACTIVITY_SCHEMA . 'profile-photo' );
108 define ( 'ACTIVITY_OBJ_ALBUM', NAMESPACE_ACTIVITY_SCHEMA . 'photo-album' );
111 * item weight for query ordering
114 define ( 'GRAVITY_PARENT', 0);
115 define ( 'GRAVITY_LIKE', 3);
116 define ( 'GRAVITY_COMMENT', 6);
120 * Reverse the effect of magic_quotes_gpc if it is enabled.
121 * Please disable magic_quotes_gpc so we don't have to do this.
122 * See http://php.net/manual/en/security.magicquotes.disabling.php
126 if (get_magic_quotes_gpc()) {
127 $process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
128 while (list($key, $val) = each($process)) {
129 foreach ($val as $k => $v) {
130 unset($process[$key][$k]);
132 $process[$key][stripslashes($k)] = $v;
133 $process[] = &$process[$key][stripslashes($k)];
135 $process[$key][stripslashes($k)] = stripslashes($v);
147 * Our main application structure for the life of this page
148 * Primarily deals with the URL that got us here
149 * and tries to make some sense of it, and
150 * stores our page contents and config storage
151 * and anything else that might need to be passed around
152 * before we spit the page out.
156 if(! class_exists('App')) {
159 public $module_loaded = false;
160 public $query_string;
169 public $error = false;
177 public $interactive = true;
185 private $curl_headers;
187 function __construct() {
189 $this->config = array();
190 $this->page = array();
191 $this->pager= array();
193 $this->query_string = '';
195 $this->scheme = ((isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'])) ? 'https' : 'http' );
197 if(x($_SERVER,'SERVER_NAME'))
198 $this->hostname = $_SERVER['SERVER_NAME'];
200 set_include_path("include/$this->hostname" . PATH_SEPARATOR . 'include' . PATH_SEPARATOR . '.' );
202 if((x($_SERVER,'QUERY_STRING')) && substr($_SERVER['QUERY_STRING'],0,2) === "q=")
203 $this->query_string = substr($_SERVER['QUERY_STRING'],2);
205 $this->cmd = trim($_GET['q'],'/');
208 * Figure out if we are running at the top of a domain
209 * or in a sub-directory and adjust accordingly
212 $path = trim(dirname($_SERVER['SCRIPT_NAME']),'/');
213 if(isset($path) && strlen($path) && ($path != $this->path))
219 * Break the URL path into C style argc/argv style arguments for our
220 * modules. Given "http://example.com/module/arg1/arg2", $this->argc
221 * will be 3 (integer) and $this->argv will contain:
227 * There will always be one argument. If provided a naked domain
228 * URL, $this->argv[0] is set to "home".
232 $this->argv = explode('/',$this->cmd);
233 $this->argc = count($this->argv);
234 if((array_key_exists('0',$this->argv)) && strlen($this->argv[0])) {
235 $this->module = $this->argv[0];
238 $this->module = 'home';
242 * Special handling for the webfinger/lrdd host XRD file
243 * Just spit out the contents and exit.
246 if($this->cmd === '.well-known/host-meta')
247 require_once('include/hostxrd.php');
251 * See if there is any page number information, and initialise
255 $this->pager['page'] = ((x($_GET,'page')) ? $_GET['page'] : 1);
256 $this->pager['itemspage'] = 50;
257 $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
258 $this->pager['total'] = 0;
261 function get_baseurl($ssl = false) {
262 if(strlen($this->baseurl))
263 return $this->baseurl;
265 $this->baseurl = (($ssl) ? 'https' : $this->scheme) . "://" . $this->hostname . ((isset($this->path) && strlen($this->path)) ? '/' . $this->path : '' );
266 return $this->baseurl;
269 function set_baseurl($url) {
270 $this->baseurl = $url;
271 $this->hostname = basename($url);
274 function get_hostname() {
275 return $this->hostname;
278 function set_hostname($h) {
279 $this->hostname = $h;
282 function set_path($p) {
283 $this->path = trim(trim($p),'/');
286 function get_path() {
290 function set_pager_total($n) {
291 $this->pager['total'] = intval($n);
294 function set_pager_itemspage($n) {
295 $this->pager['itemspage'] = intval($n);
296 $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
300 function init_pagehead() {
301 $tpl = load_view_file("view/head.tpl");
302 $this->page['htmlhead'] = replace_macros($tpl,array(
303 '$baseurl' => $this->get_baseurl() . '/'
307 function set_curl_code($code) {
308 $this->curl_code = $code;
311 function get_curl_code() {
312 return $this->curl_code;
315 function set_curl_headers($headers) {
316 $this->curl_headers = $headers;
319 function get_curl_headers() {
320 return $this->curl_headers;
326 // retrieve the App structure
327 // useful in functions which require it but don't get it passed to them
329 if(! function_exists('get_app')) {
336 // Multi-purpose function to check variable state.
337 // Usage: x($var) or $x($array,'key')
338 // returns false if variable/key is not set
339 // if variable is set, returns 1 if has 'non-zero' value, otherwise returns 0.
340 // e.g. x('') or x(0) returns 0;
342 if(! function_exists('x')) {
343 function x($s,$k = NULL) {
345 if((is_array($s)) && (array_key_exists($k,$s))) {
363 // called from db initialisation if db is dead.
365 if(! function_exists('system_unavailable')) {
366 function system_unavailable() {
367 include('system_unavailable.php');
371 // Primarily involved with database upgrade, but also sets the
372 // base url for use in cmdline programs which don't have
373 // $_SERVER variables.
375 if(! function_exists('check_config')) {
376 function check_config(&$a) {
378 load_config('system');
380 $build = get_config('system','build');
382 $build = set_config('system','build',BUILD_ID);
384 $url = get_config('system','url');
386 $url = set_config('system','url',$a->get_baseurl());
388 if($build != BUILD_ID) {
389 $stored = intval($build);
390 $current = intval(BUILD_ID);
391 if(($stored < $current) && file_exists('update.php')) {
392 // We're reporting a different version than what is currently installed.
393 // Run any existing update scripts to bring the database up to current.
395 require_once('update.php');
396 for($x = $stored; $x < $current; $x ++) {
397 if(function_exists('update_' . $x)) {
398 $func = 'update_' . $x;
402 set_config('system','build', BUILD_ID);
409 // This is our template processor.
410 // $s is the string requiring macro substitution.
411 // $r is an array of key value pairs (search => replace)
412 // returns substituted string.
413 // WARNING: this is pretty basic, and doesn't properly handle search strings that are substrings of each other.
414 // For instance if 'test' => "foo" and 'testing' => "bar", testing could become either bar or fooing,
415 // depending on the order in which they were declared in the array.
417 if(! function_exists('replace_macros')) {
418 function replace_macros($s,$r) {
423 if(is_array($r) && count($r)) {
424 foreach ($r as $k => $v ) {
429 return str_replace($search,$replace,$s);
433 // load string translation table for alternate language
435 if(! function_exists('load_translation_table')) {
436 function load_translation_table($lang) {
439 if(file_exists("view/$lang/strings.php"))
440 include("view/$lang/strings.php");
443 // translate string if translation exists
445 if(! function_exists('t')) {
450 if(x($a->strings,$s))
451 return $a->strings[$s];
455 // curl wrapper. If binary flag is true, return binary
458 if(! function_exists('fetch_url')) {
459 function fetch_url($url,$binary = false, &$redirects = 0) {
463 $ch = curl_init($url);
464 if(($redirects > 8) || (! $ch))
467 curl_setopt($ch, CURLOPT_HEADER, true);
468 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
471 $curl_time = intval(get_config('system','curl_timeout'));
472 curl_setopt($ch, CURLOPT_TIMEOUT, (($curl_time !== false) ? $curl_time : 60));
474 // by default we will allow self-signed certs
475 // but you can override this
477 $check_cert = get_config('system','verifyssl');
478 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (($check_cert) ? true : false));
480 $prx = get_config('system','proxy');
482 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
483 curl_setopt($ch, CURLOPT_PROXY, $prx);
484 $prxusr = get_config('system','proxyuser');
486 curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
489 curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
491 $a->set_curl_code(0);
493 // don't let curl abort the entire application
494 // if it throws any errors.
496 $s = @curl_exec($ch);
498 $http_code = intval(curl_getinfo($ch, CURLINFO_HTTP_CODE));
499 $header = substr($s,0,strpos($s,"\r\n\r\n"));
500 if(stristr($header,'100') && (strlen($header) < 30)) {
501 // 100 Continue has two headers, get the real one
502 $s = substr($s,strlen($header)+4);
503 $header = substr($s,0,strpos($s,"\r\n\r\n"));
505 if($http_code == 301 || $http_code == 302 || $http_code == 303) {
507 preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
508 $url = trim(array_pop($matches));
509 $url_parsed = parse_url($url);
510 if (isset($url_parsed)) {
512 return fetch_url($url,$binary,$redirects);
515 $a->set_curl_code($http_code);
516 $body = substr($s,strlen($header)+4);
517 $a->set_curl_headers($header);
523 // post request to $url. $params is an array of post variables.
525 if(! function_exists('post_url')) {
526 function post_url($url,$params, $headers = null, &$redirects = 0) {
528 $ch = curl_init($url);
529 if(($redirects > 8) || (! $ch))
532 curl_setopt($ch, CURLOPT_HEADER, true);
533 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
534 curl_setopt($ch, CURLOPT_POST,1);
535 curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
537 $curl_time = intval(get_config('system','curl_timeout'));
538 curl_setopt($ch, CURLOPT_TIMEOUT, (($curl_time !== false) ? $curl_time : 60));
540 if(is_array($headers))
541 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
543 $check_cert = get_config('system','verifyssl');
544 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (($check_cert) ? true : false));
545 $prx = get_config('system','proxy');
547 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
548 curl_setopt($ch, CURLOPT_PROXY, $prx);
549 $prxusr = get_config('system','proxyuser');
551 curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
554 $a->set_curl_code(0);
556 // don't let curl abort the entire application
557 // if it throws any errors.
559 $s = @curl_exec($ch);
561 $http_code = intval(curl_getinfo($ch, CURLINFO_HTTP_CODE));
562 $header = substr($s,0,strpos($s,"\r\n\r\n"));
563 if(stristr($header,'100') && (strlen($header) < 30)) {
564 // 100 Continue has two headers, get the real one
565 $s = substr($s,strlen($header)+4);
566 $header = substr($s,0,strpos($s,"\r\n\r\n"));
568 if($http_code == 301 || $http_code == 302 || $http_code == 303) {
570 preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
571 $url = trim(array_pop($matches));
572 $url_parsed = parse_url($url);
573 if (isset($url_parsed)) {
575 return post_url($url,$binary,$headers,$redirects);
578 $a->set_curl_code($http_code);
579 $body = substr($s,strlen($header)+4);
580 $a->set_curl_headers($header);
586 // random hash, 64 chars
588 if(! function_exists('random_string')) {
589 function random_string() {
590 return(hash('sha256',uniqid(rand(),true)));
594 * This is our primary input filter.
596 * The high bit hack only involved some old IE browser, forget which (IE5/Mac?)
597 * that had an XSS attack vector due to stripping the high-bit on an 8-bit character
598 * after cleansing, and angle chars with the high bit set could get through as markup.
600 * This is now disabled because it was interfering with some legitimate unicode sequences
601 * and hopefully there aren't a lot of those browsers left.
603 * Use this on any text input where angle chars are not valid or permitted
604 * They will be replaced with safer brackets. This may be filtered further
605 * if these are not allowed either.
609 if(! function_exists('notags')) {
610 function notags($string) {
612 return(str_replace(array("<",">"), array('[',']'), $string));
614 // High-bit filter no longer used
615 // return(str_replace(array("<",">","\xBA","\xBC","\xBE"), array('[',']','','',''), $string));
618 // use this on "body" or "content" input where angle chars shouldn't be removed,
619 // and allow them to be safely displayed.
621 if(! function_exists('escape_tags')) {
622 function escape_tags($string) {
624 return(htmlspecialchars($string));
627 // wrapper for adding a login box. If $register == true provide a registration
628 // link. This will most always depend on the value of $a->config['register_policy'].
629 // returns the complete html for inserting into the page
631 if(! function_exists('login')) {
632 function login($register = false) {
634 $register_html = (($register) ? load_view_file("view/register-link.tpl") : "");
636 $noid = get_config('system','no_openid');
638 $classname = 'no-openid';
639 $namelabel = t('Nickname or Email address: ');
640 $passlabel = t('Password: ');
644 $classname = 'openid';
645 $namelabel = t('Nickname/Email/OpenID: ');
646 $passlabel = t("Password \x28if not OpenID\x29: ");
649 $lostpass = t('Forgot your password?');
650 $lostlink = t('Password Reset');
652 if(x($_SESSION,'authenticated')) {
653 $tpl = load_view_file("view/logout.tpl");
656 $tpl = load_view_file("view/login.tpl");
660 $o = replace_macros($tpl,array(
661 '$register_html' => $register_html,
662 '$classname' => $classname,
663 '$namelabel' => $namelabel,
664 '$passlabel' => $passlabel,
666 '$lostpass' => $lostpass,
667 '$lostlink' => $lostlink
673 // generate a string that's random, but usually pronounceable.
674 // used to generate initial passwords
676 if(! function_exists('autoname')) {
677 function autoname($len) {
679 $vowels = array('a','a','ai','au','e','e','e','ee','ea','i','ie','o','ou','u');
680 if(mt_rand(0,5) == 4)
698 's','sc','sh','sm','sp','st',
706 $midcons = array('ck','ct','gn','ld','lf','lm','lt','mb','mm', 'mn','mp',
707 'nd','ng','nk','nt','rn','rp','rt');
709 $noend = array('bl', 'br', 'cl','cr','dr','fl','fr','gl','gr',
710 'kh', 'kl','kr','mn','pl','pr','rh','tr','qu','wh');
712 $start = mt_rand(0,2);
720 for ($x = 0; $x < $len; $x ++) {
721 $r = mt_rand(0,count($table) - 1);
724 if($table == $vowels)
725 $table = array_merge($cons,$midcons);
731 $word = substr($word,0,$len);
733 foreach($noend as $noe) {
734 if((strlen($word) > 2) && (substr($word,-2) == $noe)) {
735 $word = substr($word,0,-1);
739 if(substr($word,-1) == 'q')
740 $word = substr($word,0,-1);
744 // Used to end the current process, after saving session state.
746 if(! function_exists('killme')) {
748 session_write_close();
752 // redirect to another URL and terminate this process.
754 if(! function_exists('goaway')) {
755 function goaway($s) {
756 header("Location: $s");
760 // Generic XML return
761 // Outputs a basic dfrn XML status structure to STDOUT, with a <status> variable
762 // of $st and an optional text <message> of $message and terminates the current process.
764 if(! function_exists('xml_status')) {
765 function xml_status($st, $message = '') {
767 $xml_message = ((strlen($message)) ? "\t<message>" . xmlify($message) . "</message>\r\n" : '');
770 logger('xml_status returning non_zero: ' . $st . " message=" . $message);
772 header( "Content-type: text/xml" );
773 echo '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
774 echo "<result>\r\n\t<status>$st</status>\r\n$xml_message</result>\r\n";
778 // Returns the uid of locally logged in user or false.
780 if(! function_exists('local_user')) {
781 function local_user() {
782 if((x($_SESSION,'authenticated')) && (x($_SESSION,'uid')))
783 return intval($_SESSION['uid']);
787 // Returns contact id of authenticated site visitor or false
789 if(! function_exists('remote_user')) {
790 function remote_user() {
791 if((x($_SESSION,'authenticated')) && (x($_SESSION,'visitor_id')))
792 return intval($_SESSION['visitor_id']);
796 // contents of $s are displayed prominently on the page the next time
797 // a page is loaded. Usually used for errors or alerts.
799 if(! function_exists('notice')) {
800 function notice($s) {
803 $_SESSION['sysmsg'] .= $s;
806 // wrapper around config to limit the text length of an incoming message
808 if(! function_exists('get_max_import_size')) {
809 function get_max_import_size() {
811 return ((x($a->config,'max_import_size')) ? $a->config['max_import_size'] : 0 );
815 // escape text ($str) for XML transport
816 // returns escaped text.
818 if(! function_exists('xmlify')) {
819 function xmlify($str) {
822 for($x = 0; $x < strlen($str); $x ++) {
853 $buffer = trim($buffer);
858 // pass xml escaped text ($s), returns unescaped text
860 if(! function_exists('unxmlify')) {
861 function unxmlify($s) {
862 $ret = str_replace('&','&', $s);
863 $ret = str_replace(array('<','>','"','''),array('<','>','"',"'"),$ret);
867 // convenience wrapper, reverse the operation "bin2hex"
869 if(! function_exists('hex2bin')) {
870 function hex2bin($s) {
871 return(pack("H*",$s));
874 // Automatic pagination.
875 // To use, get the count of total items.
876 // Then call $a->set_pager_total($number_items);
877 // Optionally call $a->set_pager_itemspage($n) to the number of items to display on each page
878 // Then call paginate($a) after the end of the display loop to insert the pager block on the page
879 // (assuming there are enough items to paginate).
880 // When using with SQL, the setting LIMIT %d, %d => $a->pager['start'],$a->pager['itemspage']
881 // will limit the results to the correct items for the current page.
882 // The actual page handling is then accomplished at the application layer.
884 if(! function_exists('paginate')) {
885 function paginate(&$a) {
887 $stripped = preg_replace('/(&page=[0-9]*)/','',$a->query_string);
888 $stripped = str_replace('q=','',$stripped);
889 $stripped = trim($stripped,'/');
890 $url = $a->get_baseurl() . '/' . $stripped;
893 if($a->pager['total'] > $a->pager['itemspage']) {
894 $o .= '<div class="pager">';
895 if($a->pager['page'] != 1)
896 $o .= '<span class="pager_prev">'."<a href=\"$url".'&page='.($a->pager['page'] - 1).'">' . t('prev') . '</a></span> ';
898 $o .= "<span class=\"pager_first\"><a href=\"$url"."&page=1\">" . t('first') . "</a></span> ";
900 $numpages = $a->pager['total'] / $a->pager['itemspage'];
903 $numstop = $numpages;
906 $numstart = (($pagenum > 7) ? ($pagenum - 7) : 1);
907 $numstop = (($pagenum > ($numpages - 7)) ? $numpages : ($numstart + 14));
910 for($i = $numstart; $i <= $numstop; $i++){
911 if($i == $a->pager['page'])
912 $o .= '<span class="pager_current">'.(($i < 10) ? ' '.$i : $i);
914 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? ' '.$i : $i)."</a>";
918 if(($a->pager['total'] % $a->pager['itemspage']) != 0) {
919 if($i == $a->pager['page'])
920 $o .= '<span class="pager_current">'.(($i < 10) ? ' '.$i : $i);
922 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? ' '.$i : $i)."</a>";
926 $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
927 $o .= "<span class=\"pager_last\"><a href=\"$url"."&page=$lastpage\">" . t('last') . "</a></span> ";
929 if(($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0)
930 $o .= '<span class="pager_next">'."<a href=\"$url"."&page=".($a->pager['page'] + 1).'">' . t('next') . '</a></span>';
931 $o .= '</div>'."\r\n";
936 // Turn user/group ACLs stored as angle bracketed text into arrays
938 if(! function_exists('expand_acl')) {
939 function expand_acl($s) {
940 // turn string array of angle-bracketed elements into numeric array
941 // e.g. "<1><2><3>" => array(1,2,3);
945 $t = str_replace('<','',$s);
946 $a = explode('>',$t);
949 $ret[] = intval($aa);
955 // Used to wrap ACL elements in angle brackets for storage
957 if(! function_exists('sanitise_acl')) {
958 function sanitise_acl(&$item) {
960 $item = '<' . intval(notags(trim($item))) . '>';
965 // retrieve a "family" of config variables from database to cached storage
967 if(! function_exists('load_config')) {
968 function load_config($family) {
970 $r = q("SELECT * FROM `config` WHERE `cat` = '%s'",
976 $a->config[$family][$k] = $rr['v'];
981 // get a particular config variable given the family name
982 // and key. Returns false if not set.
983 // $instore is only used by the set_config function
984 // to determine if the key already exists in the DB
985 // If a key is found in the DB but doesn't exist in
986 // local config cache, pull it into the cache so we don't have
987 // to hit the DB again for this item.
989 if(! function_exists('get_config')) {
990 function get_config($family, $key, $instore = false) {
995 if(isset($a->config[$family][$key])) {
996 if($a->config[$family][$key] === '!<unset>!') {
999 return $a->config[$family][$key];
1002 $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
1007 $a->config[$family][$key] = $ret[0]['v'];
1008 return $ret[0]['v'];
1011 $a->config[$family][$key] = '!<unset>!';
1016 // Store a config value ($value) in the category ($family)
1017 // under the key ($key)
1018 // Return the value, or false if the database update failed
1020 if(! function_exists('set_config')) {
1021 function set_config($family,$key,$value) {
1024 $a->config[$family][$key] = $value;
1026 if(get_config($family,$key,true) === false) {
1027 $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
1036 $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
1047 if(! function_exists('get_pconfig')) {
1048 function get_pconfig($uid,$family, $key, $instore = false) {
1053 if(isset($a->config[$uid][$family][$key])) {
1054 if($a->config[$uid][$family][$key] === '!<unset>!') {
1057 return $a->config[$uid][$family][$key];
1060 $ret = q("SELECT `v` FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
1066 $a->config[$uid][$family][$key] = $ret[0]['v'];
1067 return $ret[0]['v'];
1070 $a->config[$uid][$family][$key] = '!<unset>!';
1075 if(! function_exists('del_config')) {
1076 function del_config($family,$key) {
1079 if(x($a->config[$family],$key))
1080 unset($a->config[$family][$key]);
1081 $ret = q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
1090 // Same as above functions except these are for personal config storage and take an
1091 // additional $uid argument.
1093 if(! function_exists('set_pconfig')) {
1094 function set_pconfig($uid,$family,$key,$value) {
1097 $a->config[$uid][$family][$key] = $value;
1099 if(get_pconfig($uid,$family,$key,true) === false) {
1100 $ret = q("INSERT INTO `pconfig` ( `uid`, `cat`, `k`, `v` ) VALUES ( %d, '%s', '%s', '%s' ) ",
1110 $ret = q("UPDATE `pconfig` SET `v` = '%s' WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
1121 if(! function_exists('del_pconfig')) {
1122 function del_pconfig($uid,$family,$key) {
1125 if(x($a->config[$uid][$family],$key))
1126 unset($a->config[$uid][$family][$key]);
1127 $ret = q("DELETE FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
1136 // convert an XML document to a normalised, case-corrected array
1137 // used by webfinger
1139 if(! function_exists('convert_xml_element_to_array')) {
1140 function convert_xml_element_to_array($xml_element, &$recursion_depth=0) {
1142 // If we're getting too deep, bail out
1143 if ($recursion_depth > 512) {
1147 if (!is_string($xml_element) &&
1148 !is_array($xml_element) &&
1149 (get_class($xml_element) == 'SimpleXMLElement')) {
1150 $xml_element_copy = $xml_element;
1151 $xml_element = get_object_vars($xml_element);
1154 if (is_array($xml_element)) {
1155 $result_array = array();
1156 if (count($xml_element) <= 0) {
1157 return (trim(strval($xml_element_copy)));
1160 foreach($xml_element as $key=>$value) {
1163 $result_array[strtolower($key)] =
1164 convert_xml_element_to_array($value, $recursion_depth);
1167 if ($recursion_depth == 0) {
1168 $temp_array = $result_array;
1169 $result_array = array(
1170 strtolower($xml_element_copy->getName()) => $temp_array,
1174 return ($result_array);
1177 return (trim(strval($xml_element)));
1181 // Given an email style address, perform webfinger lookup and
1182 // return the resulting DFRN profile URL, or if no DFRN profile URL
1183 // is located, returns an OStatus subscription template (prefixed
1184 // with the string 'stat:' to identify it as on OStatus template).
1185 // If this isn't an email style address just return $s.
1186 // Return an empty string if email-style addresses but webfinger fails,
1187 // or if the resultant personal XRD doesn't contain a supported
1188 // subscription/friend-request attribute.
1190 if(! function_exists('webfinger_dfrn')) {
1191 function webfinger_dfrn($s) {
1192 if(! strstr($s,'@')) {
1195 $links = webfinger($s);
1196 logger('webfinger_dfrn: ' . $s . ':' . print_r($links,true), LOGGER_DATA);
1198 foreach($links as $link)
1199 if($link['@attributes']['rel'] === NAMESPACE_DFRN)
1200 return $link['@attributes']['href'];
1201 foreach($links as $link)
1202 if($link['@attributes']['rel'] === NAMESPACE_OSTATUSSUB)
1203 return 'stat:' . $link['@attributes']['template'];
1208 // Given an email style address, perform webfinger lookup and
1209 // return the array of link attributes from the personal XRD file.
1210 // On error/failure return an empty array.
1213 if(! function_exists('webfinger')) {
1214 function webfinger($s) {
1216 if(strstr($s,'@')) {
1217 $host = substr($s,strpos($s,'@') + 1);
1220 $tpl = fetch_lrdd_template($host);
1221 logger('webfinger: lrdd template: ' . $tpl);
1223 $pxrd = str_replace('{uri}', urlencode('acct:'.$s), $tpl);
1224 $links = fetch_xrd_links($pxrd);
1225 if(! count($links)) {
1226 // try with double slashes
1227 $pxrd = str_replace('{uri}', urlencode('acct://'.$s), $tpl);
1228 $links = fetch_xrd_links($pxrd);
1236 if(! function_exists('lrdd')) {
1237 function lrdd($uri) {
1241 if(strstr($uri,'@')) {
1242 return(webfinger($uri));
1245 $html = fetch_url($uri);
1246 $headers = $a->get_curl_headers();
1247 $lines = explode("\n",$headers);
1249 foreach($lines as $line) {
1250 // TODO alter the following regex to support multiple relations (space separated)
1251 if((stristr($line,'link:')) && preg_match('/<([^>].*)>.*rel\=[\'\"]lrdd[\'\"]/',$line,$matches)) {
1252 $link = $matches[1];
1257 if(! isset($link)) {
1258 // parse the page of the supplied URL looking for rel links
1260 require_once('library/HTML5/Parser.php');
1261 $dom = HTML5_Parser::parse($html);
1264 $items = $dom->getElementsByTagName('link');
1266 foreach($items as $item) {
1267 $x = $item->getAttribute('rel');
1269 $link = $item->getAttribute('href');
1277 return(fetch_xrd_links($link));
1284 // Given a host name, locate the LRDD template from that
1285 // host. Returns the LRDD template or an empty string on
1288 if(! function_exists('fetch_lrdd_template')) {
1289 function fetch_lrdd_template($host) {
1291 $url = 'http://' . $host . '/.well-known/host-meta' ;
1292 $links = fetch_xrd_links($url);
1294 foreach($links as $link)
1295 if($link['@attributes']['rel'] && $link['@attributes']['rel'] === 'lrdd')
1296 $tpl = $link['@attributes']['template'];
1298 if(! strpos($tpl,'{uri}'))
1303 // Given a URL, retrieve the page as an XRD document.
1304 // Return an array of links.
1305 // on error/failure return empty array.
1307 if(! function_exists('fetch_xrd_links')) {
1308 function fetch_xrd_links($url) {
1311 $xml = fetch_url($url);
1315 logger('fetch_xrd_links: ' . $xml, LOGGER_DATA);
1316 $h = simplexml_load_string($xml);
1317 $arr = convert_xml_element_to_array($h);
1319 if(isset($arr['xrd']['link'])) {
1320 $link = $arr['xrd']['link'];
1321 if(! isset($link[0]))
1322 $links = array($link);
1330 // Convert an ACL array to a storable string
1332 if(! function_exists('perms2str')) {
1333 function perms2str($p) {
1336 if(is_array($tmp)) {
1337 array_walk($tmp,'sanitise_acl');
1338 $ret = implode('',$tmp);
1343 // generate a guaranteed unique (for this domain) item ID for ATOM
1344 // safe from birthday paradox
1346 if(! function_exists('item_new_uri')) {
1347 function item_new_uri($hostname,$uid) {
1351 $hash = random_string();
1353 $uri = "urn:X-dfrn:" . $hostname . ':' . $uid . ':' . $hash;
1355 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
1359 } while($dups == true);
1363 // Generate a guaranteed unique photo ID.
1364 // safe from birthday paradox
1366 if(! function_exists('photo_new_resource')) {
1367 function photo_new_resource() {
1371 $resource = hash('md5',uniqid(mt_rand(),true));
1372 $r = q("SELECT `id` FROM `photo` WHERE `resource-id` = '%s' LIMIT 1",
1377 } while($found == true);
1382 // Take a URL from the wild, prepend http:// if necessary
1383 // and check DNS to see if it's real
1384 // return true if it's OK, false if something is wrong with it
1386 if(! function_exists('validate_url')) {
1387 function validate_url(&$url) {
1388 if(substr($url,0,4) != 'http')
1389 $url = 'http://' . $url;
1390 $h = parse_url($url);
1392 if(($h) && (checkdnsrr($h['host'], 'ANY'))) {
1398 // checks that email is an actual resolvable internet address
1400 if(! function_exists('validate_email')) {
1401 function validate_email($addr) {
1403 if(! strpos($addr,'@'))
1405 $h = substr($addr,strpos($addr,'@') + 1);
1407 if(($h) && (checkdnsrr($h, 'ANY'))) {
1413 // Check $url against our list of allowed sites,
1414 // wildcards allowed. If allowed_sites is unset return true;
1415 // If url is allowed, return true.
1416 // otherwise, return false
1418 if(! function_exists('allowed_url')) {
1419 function allowed_url($url) {
1421 $h = parse_url($url);
1427 $str_allowed = get_config('system','allowed_sites');
1433 $host = strtolower($h['host']);
1435 // always allow our own site
1437 if($host == strtolower($_SERVER['SERVER_NAME']))
1440 $fnmatch = function_exists('fnmatch');
1441 $allowed = explode(',',$str_allowed);
1443 if(count($allowed)) {
1444 foreach($allowed as $a) {
1445 $pat = strtolower(trim($a));
1446 if(($fnmatch && fnmatch($pat,$host)) || ($pat == $host)) {
1455 // check if email address is allowed to register here.
1456 // Compare against our list (wildcards allowed).
1457 // Returns false if not allowed, true if allowed or if
1458 // allowed list is not configured.
1460 if(! function_exists('allowed_email')) {
1461 function allowed_email($email) {
1464 $domain = strtolower(substr($email,strpos($email,'@') + 1));
1468 $str_allowed = get_config('system','allowed_email');
1474 $fnmatch = function_exists('fnmatch');
1475 $allowed = explode(',',$str_allowed);
1477 if(count($allowed)) {
1478 foreach($allowed as $a) {
1479 $pat = strtolower(trim($a));
1480 if(($fnmatch && fnmatch($pat,$host)) || ($pat == $host)) {
1489 // Format the like/dislike text for a profile item
1490 // $cnt = number of people who like/dislike the item
1491 // $arr = array of pre-linked names of likers/dislikers
1492 // $type = one of 'like, 'dislike'
1494 // returns formatted text
1496 if(! function_exists('format_like')) {
1497 function format_like($cnt,$arr,$type,$id) {
1500 $o .= $arr[0] . (($type === 'like') ? t(' likes this.') : t(' doesn\'t like this.')) . EOL ;
1502 $o .= '<span class="fakelink" onclick="openClose(\'' . $type . 'list-' . $id . '\');" >'
1503 . $cnt . ' ' . t('people') . '</span> ' . (($type === 'like') ? t('like this.') : t('don\'t like this.')) . EOL ;
1504 $total = count($arr);
1505 if($total >= MAX_LIKERS)
1506 $arr = array_slice($arr, 0, MAX_LIKERS - 1);
1507 if($total < MAX_LIKERS)
1508 $arr[count($arr)-1] = t('and') . ' ' . $arr[count($arr)-1];
1509 $str = implode(', ', $arr);
1510 if($total >= MAX_LIKERS)
1511 $str .= t(', and ') . $total - MAX_LIKERS . t(' other people');
1512 $str .= (($type === 'like') ? t(' like this.') : t(' don\'t like this.'));
1513 $o .= "\t" . '<div id="' . $type . 'list-' . $id . '" style="display: none;" >' . $str . '</div>';
1519 // wrapper to load a view template, checking for alternate
1520 // languages before falling back to the default
1522 if(! function_exists('load_view_file')) {
1523 function load_view_file($s) {
1526 $lang = get_config('system','language');
1529 if(file_exists("$d/$lang/$b"))
1530 return file_get_contents("$d/$lang/$b");
1531 return file_get_contents($s);
1534 // for html,xml parsing - let's say you've got
1535 // an attribute foobar="class1 class2 class3"
1536 // and you want to find out if it contains 'class3'.
1537 // you can't use a normal sub string search because you
1538 // might match 'notclass3' and a regex to do the job is
1539 // possible but a bit complicated.
1540 // pass the attribute string as $attr and the attribute you
1541 // are looking for as $s - returns true if found, otherwise false
1543 if(! function_exists('attribute_contains')) {
1544 function attribute_contains($attr,$s) {
1545 $a = explode(' ', $attr);
1546 if(count($a) && in_array($s,$a))
1551 if(! function_exists('logger')) {
1552 function logger($msg,$level = 0) {
1554 $debugging = get_config('system','debugging');
1555 $loglevel = intval(get_config('system','loglevel'));
1556 $logfile = get_config('system','logfile');
1558 if((! $debugging) || (! $logfile) || ($level > $loglevel))
1561 @file_put_contents($logfile, datetime_convert() . ':' . session_id() . ' ' . $msg . "\n", FILE_APPEND);
1566 if(! function_exists('activity_match')) {
1567 function activity_match($haystack,$needle) {
1568 if(($haystack === $needle) || ((basename($needle) === $haystack) && strstr($needle,NAMESPACE_ACTIVITY_SCHEMA)))
1574 // Pull out all #hashtags and @person tags from $s;
1575 // We also get @person@domain.com - which would make
1576 // the regex quite complicated as tags can also
1577 // end a sentence. So we'll run through our results
1578 // and strip the period from any tags which end with one.
1579 // Returns array of tags found, or empty array.
1582 if(! function_exists('get_tags')) {
1583 function get_tags($s) {
1585 if(preg_match_all('/([@#][^ \x0D\x0A,:?]*)([ \x0D\x0A,:?]|$)/',$s,$match)) {
1586 foreach($match[1] as $match) {
1587 if(strstr($match,"]")) {
1588 // we might be inside a bbcode color tag - leave it alone
1591 if(substr($match,-1,1) === '.')
1592 $ret[] = substr($match,0,-1);
1602 // quick and dirty quoted_printable encoding
1604 if(! function_exists('qp')) {
1606 return str_replace ("%","=",rawurlencode($s));
1610 if(! function_exists('like_puller')) {
1611 function like_puller($a,$item,&$arr,$mode) {
1615 $verb = (($mode === 'like') ? ACTIVITY_LIKE : ACTIVITY_DISLIKE);
1617 if((activity_match($item['verb'],$verb)) && ($item['id'] != $item['parent'])) {
1618 $url = $item['author-link'];
1619 if(($item['network'] === 'dfrn') && (! $item['self']) && ($item['author-link'] == $item['url'])) {
1620 $url = $a->get_baseurl() . '/redir/' . $item['contact-id'];
1621 $sparkle = ' class="sparkle" ';
1623 if(! ((isset($arr[$item['parent'] . '-l'])) && (is_array($arr[$item['parent'] . '-l']))))
1624 $arr[$item['parent'] . '-l'] = array();
1625 if(! isset($arr[$item['parent']]))
1626 $arr[$item['parent']] = 1;
1628 $arr[$item['parent']] ++;
1629 $arr[$item['parent'] . '-l'][] = '<a href="'. $url . '"'. $sparkle .'>' . $item['author-name'] . '</a>';
1634 if(! function_exists('get_mentions')) {
1635 function get_mentions($item) {
1637 if(! strlen($item['tag']))
1640 $arr = explode(',',$item['tag']);
1641 foreach($arr as $x) {
1643 if(preg_match('/@\[url=([^\]]*)\]/',$x,$matches))
1644 $o .= "\t\t" . '<link rel="mentioned" href="' . $matches[1] . '" />' . "\r\n";
1649 if(! function_exists('contact_block')) {
1650 function contact_block() {
1653 if((! is_array($a->profile)) || ($a->profile['hide-friends']))
1655 $r = q("SELECT COUNT(*) AS `total` FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 and `pending` = 0",
1656 intval($a->profile['uid'])
1659 $total = intval($r[0]['total']);
1662 $o .= '<h4 class="contact-h4">' . t('No contacts') . '</h4>';
1665 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 and `pending` = 0 ORDER BY RAND() LIMIT 24",
1666 intval($a->profile['uid'])
1669 $o .= '<h4 class="contact-h4">' . $total . ' ' . t('Contacts') . '</h4><div id="contact-block">';
1670 foreach($r as $rr) {
1671 $redirect_url = $a->get_baseurl() . '/redir/' . $rr['id'];
1672 if(local_user() && ($rr['uid'] == local_user())
1673 && ($rr['network'] === 'dfrn')) {
1674 $url = $redirect_url;
1675 $sparkle = ' sparkle';
1682 $o .= '<div class="contact-block-div"><a class="contact-block-link' . $sparkle . '" href="' . $url . '" ><img class="contact-block-img' . $sparkle . '" src="' . $rr['micro'] . '" title="' . $rr['name'] . ' [' . $rr['url'] . ']" alt="' . $rr['name'] . '" /></a></div>' . "\r\n";
1684 $o .= '</div><div id="contact-block-end"></div>';
1685 $o .= '<div id="viewcontacts"><a id="viewcontacts-link" href="viewcontacts/' . $a->profile['nickname'] . '">' . t('View Contacts') . '</a></div>';
1692 if(! function_exists('search')) {
1693 function search($s) {
1695 $o = '<div id="search-box">';
1696 $o .= '<form action="' . $a->get_baseurl() . '/search' . '" method="get" >';
1697 $o .= '<input type="text" name="search" id="search-text" value="' . $s .'" />';
1698 $o .= '<input type="submit" name="submit" id="search-submit" value="' . t('Search') . '" />';
1699 $o .= '</form></div>';
1703 if(! function_exists('valid_email')) {
1704 function valid_email($x){
1705 if(preg_match('/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+$/',$x))
1711 if(! function_exists('gravatar_img')) {
1712 function gravatar_img($email) {
1714 $opt = 'identicon'; // psuedo-random geometric pattern if not found
1716 $hash = md5(trim(strtolower($email)));
1718 $url = 'http://www.gravatar.com/avatar/' . $hash . '.jpg'
1719 . '?s=' . $size . '&d=' . $opt . '&r=' . $rating;
1721 logger('gravatar: ' . $email . ' ' . $url);
1725 if(! function_exists('aes_decrypt')) {
1726 function aes_decrypt($val,$ky)
1728 $key="\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
1729 for($a=0;$a<strlen($ky);$a++)
1730 $key[$a%16]=chr(ord($key[$a%16]) ^ ord($ky[$a]));
1731 $mode = MCRYPT_MODE_ECB;
1732 $enc = MCRYPT_RIJNDAEL_128;
1733 $dec = @mcrypt_decrypt($enc, $key, $val, $mode, @mcrypt_create_iv( @mcrypt_get_iv_size($enc, $mode), MCRYPT_DEV_URANDOM ) );
1734 return rtrim($dec,(( ord(substr($dec,strlen($dec)-1,1))>=0 and ord(substr($dec, strlen($dec)-1,1))<=16)? chr(ord( substr($dec,strlen($dec)-1,1))):null));
1738 if(! function_exists('aes_encrypt')) {
1739 function aes_encrypt($val,$ky)
1741 $key="\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
1742 for($a=0;$a<strlen($ky);$a++)
1743 $key[$a%16]=chr(ord($key[$a%16]) ^ ord($ky[$a]));
1744 $mode=MCRYPT_MODE_ECB;
1745 $enc=MCRYPT_RIJNDAEL_128;
1746 $val=str_pad($val, (16*(floor(strlen($val) / 16)+(strlen($val) % 16==0?2:1))), chr(16-(strlen($val) % 16)));
1747 return mcrypt_encrypt($enc, $key, $val, $mode, mcrypt_create_iv( mcrypt_get_iv_size($enc, $mode), MCRYPT_DEV_URANDOM));
1755 * Replace naked text hyperlink with HTML formatted hyperlink
1759 if(! function_exists('linkify')) {
1760 function linkify($s) {
1761 $s = preg_replace("/(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\.\=\_\~\#\'\%]*)/", ' <a href="$1" >$1</a>', $s);
1771 * Replaces text emoticons with graphical images
1773 * @Parameter: string $s
1778 if(! function_exists('smilies')) {
1779 function smilies($s) {
1783 array( ':-)', ';-)', ':-(', ':(', ':-P', ':-"', ':-x', ':-X', ':-D', '8-|', '8-O'),
1785 '<img src="' . $a->get_baseurl() . '/images/smiley-smile.gif" alt=":-)" />',
1786 '<img src="' . $a->get_baseurl() . '/images/smiley-wink.gif" alt=";-)" />',
1787 '<img src="' . $a->get_baseurl() . '/images/smiley-frown.gif" alt=":-(" />',
1788 '<img src="' . $a->get_baseurl() . '/images/smiley-frown.gif" alt=":(" />',
1789 '<img src="' . $a->get_baseurl() . '/images/smiley-tongue-out.gif" alt=":-P" />',
1790 '<img src="' . $a->get_baseurl() . '/images/smiley-kiss.gif" alt=":-\"" />',
1791 '<img src="' . $a->get_baseurl() . '/images/smiley-kiss.gif" alt=":-x" />',
1792 '<img src="' . $a->get_baseurl() . '/images/smiley-kiss.gif" alt=":-X" />',
1793 '<img src="' . $a->get_baseurl() . '/images/smiley-laughing.gif" alt=":-D" />',
1794 '<img src="' . $a->get_baseurl() . '/images/smiley-surprised.gif" alt="8-|" />',
1795 '<img src="' . $a->get_baseurl() . '/images/smiley-surprised.gif" alt="8-O" />'
1802 * Function : profile_load
1804 * @parameter string $nickname
1805 * @parameter int $profile
1807 * Summary: Loads a profile into the page sidebar.
1808 * The function requires a writeable copy of the main App structure, and the nickname
1809 * of a registered local account.
1811 * If the viewer is an authenticated remote viewer, the profile displayed is the
1812 * one that has been configured for his/her viewing in the Contact manager.
1813 * Passing a non-zero profile ID can also allow a preview of a selected profile
1816 * Profile information is placed in the App structure for later retrieval.
1817 * Honours the owner's chosen theme for display.
1821 if(! function_exists('profile_load')) {
1822 function profile_load(&$a, $nickname, $profile = 0) {
1824 $r = q("SELECT `profile-id` FROM `contact` WHERE `id` = %d LIMIT 1",
1825 intval($_SESSION['visitor_id']));
1827 $profile = $r[0]['profile-id'];
1833 $profile_int = intval($profile);
1834 $r = q("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `user`.* FROM `profile`
1835 LEFT JOIN `user` ON `profile`.`uid` = `user`.`uid`
1836 WHERE `user`.`nickname` = '%s' AND `profile`.`id` = %d LIMIT 1",
1838 intval($profile_int)
1842 $r = q("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `user`.* FROM `profile`
1843 LEFT JOIN `user` ON `profile`.`uid` = `user`.`uid`
1844 WHERE `user`.`nickname` = '%s' AND `profile`.`is-default` = 1 LIMIT 1",
1849 if(($r === false) || (! count($r))) {
1850 notice( t('No profile') . EOL );
1855 $a->profile = $r[0];
1858 $a->page['title'] = $a->profile['name'];
1859 $_SESSION['theme'] = $a->profile['theme'];
1861 if(! (x($a->page,'aside')))
1862 $a->page['aside'] = '';
1864 $a->page['aside'] .= profile_sidebar($a->profile);
1865 $a->page['aside'] .= contact_block();
1873 * Function: profile_sidebar
1875 * Formats a profile for display in the sidebar.
1876 * It is very difficult to templatise the HTML completely
1877 * because of all the conditional logic.
1879 * @parameter: array $profile
1881 * Returns HTML string stuitable for sidebar inclusion
1882 * Exceptions: Returns empty string if passed $profile is wrong type or not populated
1887 if(! function_exists('profile_sidebar')) {
1888 function profile_sidebar($profile) {
1894 if((! is_array($profile)) && (! count($profile)))
1897 $fullname = '<div class="fn">' . $profile['name'] . '</div>';
1901 $photo = '<div id="profile=photo-wrapper"><img class="photo" src="' . $profile['photo'] . '" alt="' . $profile['name'] . '" /></div>';
1903 $connect = (($profile['uid'] != local_user()) ? '<li><a id="dfrn-request-link" href="dfrn_request/' . $profile['nickname'] . '">' . t('Connect') . '</a></li>' : '');
1905 if((x($profile,'address') == 1)
1906 || (x($profile,'locality') == 1)
1907 || (x($profile,'region') == 1)
1908 || (x($profile,'postal-code') == 1)
1909 || (x($profile,'country-name') == 1))
1913 $location .= '<div class="location"><span class="location-label">' . t('Location:') . '</span> <div class="adr">';
1914 $location .= ((x($profile,'address') == 1) ? '<div class="street-address">' . $profile['address'] . '</div>' : '');
1915 $location .= (((x($profile,'locality') == 1) || (x($profile,'region') == 1) || (x($profile,'postal-code') == 1))
1916 ? '<span class="city-state-zip"><span class="locality">' . $profile['locality'] . '</span>'
1917 . ((x($profile['locality']) == 1) ? t(', ') : '')
1918 . '<span class="region">' . $profile['region'] . '</span>'
1919 . ' <span class="postal-code">' . $profile['postal-code'] . '</span></span>' : '');
1920 $location .= ((x($profile,'country-name') == 1) ? ' <span class="country-name">' . $profile['country-name'] . '</span>' : '');
1921 $location .= '</div></div><div class="profile-clear"></div>';
1925 $gender = ((x($profile,'gender') == 1) ? '<div class="mf"><span class="gender-label">' . t('Gender:') . '</span> <span class="x-gender">' . $profile['gender'] . '</span></div><div class="profile-clear"></div>' : '');
1927 $pubkey = ((x($profile,'key') == 1) ? '<div class="key" style="display:none;">' . $profile['pubkey'] . '</div>' : '');
1929 $marital = ((x($profile,'marital') == 1) ? '<div class="marital"><span class="marital-label"><span class="heart">♥</span> ' . t('Status:') . ' </span><span class="marital-text">' . $profile['marital'] . '</span></div></div><div class="profile-clear"></div>' : '');
1931 $homepage = ((x($profile,'homepage') == 1) ? '<div class="homepage"><span class="homepage-label">' . t('Homepage:') . ' </span><span class="homepage-url">' . linkify($profile['homepage']) . '</span></div></div><div class="profile-clear"></div>' : '');
1933 $tpl = load_view_file('view/profile_vcard.tpl');
1935 $o .= replace_macros($tpl, array(
1936 '$fullname' => $fullname,
1939 '$connect' => $connect,
1940 '$location' => $location,
1941 '$gender' => $gender,
1942 '$pubkey' => $pubkey,
1943 '$marital' => $marital,
1944 '$homepage' => $homepage