5 define ( 'BUILD_ID', 1022 );
6 define ( 'DFRN_PROTOCOL_VERSION', '2.0' );
8 define ( 'EOL', "<br />\r\n" );
9 define ( 'ATOM_TIME', 'Y-m-d\TH:i:s\Z' );
15 define ( 'LOGGER_NORMAL', 0 );
16 define ( 'LOGGER_TRACE', 1 );
17 define ( 'LOGGER_DEBUG', 2 );
18 define ( 'LOGGER_DATA', 3 );
19 define ( 'LOGGER_ALL', 4 );
22 * registration policies
25 define ( 'REGISTER_CLOSED', 0 );
26 define ( 'REGISTER_APPROVE', 1 );
27 define ( 'REGISTER_OPEN', 2 );
33 define ( 'REL_VIP', 1);
34 define ( 'REL_FAN', 2);
35 define ( 'REL_BUD', 3);
42 * PAGE_NORMAL is a typical personal profile account
43 * PAGE_SOAPBOX automatically approves all friend requests as REL_FAN, (readonly)
44 * PAGE_COMMUNITY automatically approves all friend requests as REL_FAN, but with
45 * write access to wall and comments (no email and not included in page owner's ACL lists)
46 * PAGE_FREELOVE automatically approves all friend requests as full friends (REL_BUD).
50 define ( 'PAGE_NORMAL', 0 );
51 define ( 'PAGE_SOAPBOX', 1 );
52 define ( 'PAGE_COMMUNITY', 2 );
53 define ( 'PAGE_FREELOVE', 3 );
56 * Maximum number of "people who like (or don't like) this" that we will list by name
59 define ( 'MAX_LIKERS', 75);
62 * email notification options
65 define ( 'NOTIFY_INTRO', 0x0001 );
66 define ( 'NOTIFY_CONFIRM', 0x0002 );
67 define ( 'NOTIFY_WALL', 0x0004 );
68 define ( 'NOTIFY_COMMENT', 0x0008 );
69 define ( 'NOTIFY_MAIL', 0x0010 );
72 * various namespaces we may need to parse
75 define ( 'NAMESPACE_DFRN' , 'http://purl.org/macgirvin/dfrn/1.0' );
76 define ( 'NAMESPACE_THREAD' , 'http://purl.org/syndication/thread/1.0' );
77 define ( 'NAMESPACE_TOMB' , 'http://purl.org/atompub/tombstones/1.0' );
78 define ( 'NAMESPACE_ACTIVITY', 'http://activitystrea.ms/spec/1.0/' );
79 define ( 'NAMESPACE_ACTIVITY_SCHEMA', 'http://activitystrea.ms/schema/1.0/' );
80 define ( 'NAMESPACE_MEDIA', 'http://purl.org/syndication/atommedia' );
81 define ( 'NAMESPACE_SALMON_ME', 'http://salmon-protocol.org/ns/magic-env' );
82 define ( 'NAMESPACE_OSTATUSSUB', 'http://ostatus.org/schema/1.0/subscribe' );
83 define ( 'NAMESPACE_GEORSS', 'http://www.georss.org/georss' );
84 define ( 'NAMESPACE_POCO', 'http://portablecontacts.net/spec/1.0' );
85 define ( 'NAMESPACE_FEED', 'http://schemas.google.com/g/2010#updates-from' );
88 * activity stream defines
91 define ( 'ACTIVITY_LIKE', NAMESPACE_ACTIVITY_SCHEMA . 'like' );
92 define ( 'ACTIVITY_DISLIKE', NAMESPACE_DFRN . '/dislike' );
93 define ( 'ACTIVITY_OBJ_HEART', NAMESPACE_DFRN . '/heart' );
95 define ( 'ACTIVITY_FRIEND', NAMESPACE_ACTIVITY_SCHEMA . 'make-friend' );
96 define ( 'ACTIVITY_FOLLOW', NAMESPACE_ACTIVITY_SCHEMA . 'follow' );
97 define ( 'ACTIVITY_UNFOLLOW', NAMESPACE_ACTIVITY_SCHEMA . 'unfollow' );
98 define ( 'ACTIVITY_POST', NAMESPACE_ACTIVITY_SCHEMA . 'post' );
99 define ( 'ACTIVITY_UPDATE', NAMESPACE_ACTIVITY_SCHEMA . 'update' );
100 define ( 'ACTIVITY_TAG', NAMESPACE_ACTIVITY_SCHEMA . 'tag' );
102 define ( 'ACTIVITY_OBJ_COMMENT', NAMESPACE_ACTIVITY_SCHEMA . 'comment' );
103 define ( 'ACTIVITY_OBJ_NOTE', NAMESPACE_ACTIVITY_SCHEMA . 'note' );
104 define ( 'ACTIVITY_OBJ_PERSON', NAMESPACE_ACTIVITY_SCHEMA . 'person' );
105 define ( 'ACTIVITY_OBJ_PHOTO', NAMESPACE_ACTIVITY_SCHEMA . 'photo' );
106 define ( 'ACTIVITY_OBJ_P_PHOTO', NAMESPACE_ACTIVITY_SCHEMA . 'profile-photo' );
107 define ( 'ACTIVITY_OBJ_ALBUM', NAMESPACE_ACTIVITY_SCHEMA . 'photo-album' );
110 * item weight for query ordering
113 define ( 'GRAVITY_PARENT', 0);
114 define ( 'GRAVITY_LIKE', 3);
115 define ( 'GRAVITY_COMMENT', 6);
119 * Reverse the effect of magic_quotes_gpc if it is enabled.
120 * Please disable magic_quotes_gpc so we don't have to do this.
121 * See http://php.net/manual/en/security.magicquotes.disabling.php
125 if (get_magic_quotes_gpc()) {
126 $process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
127 while (list($key, $val) = each($process)) {
128 foreach ($val as $k => $v) {
129 unset($process[$key][$k]);
131 $process[$key][stripslashes($k)] = $v;
132 $process[] = &$process[$key][stripslashes($k)];
134 $process[$key][stripslashes($k)] = stripslashes($v);
146 * Our main application structure for the life of this page
147 * Primarily deals with the URL that got us here
148 * and tries to make some sense of it, and
149 * stores our page contents and config storage
150 * and anything else that might need to be passed around
151 * before we spit the page out.
155 if(! class_exists('App')) {
158 public $module_loaded = false;
167 public $error = false;
175 public $interactive = true;
183 private $curl_headers;
185 function __construct() {
187 $this->config = array();
188 $this->page = array();
189 $this->pager= array();
191 $this->scheme = ((isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'])) ? 'https' : 'http' );
193 if(x($_SERVER,'SERVER_NAME'))
194 $this->hostname = $_SERVER['SERVER_NAME'];
196 set_include_path("include/$this->hostname" . PATH_SEPARATOR . 'include' . PATH_SEPARATOR . '.' );
198 if((x($_SERVER,'QUERY_STRING')) && substr($_SERVER['QUERY_STRING'],0,2) === "q=")
199 $_SERVER['QUERY_STRING'] = substr($_SERVER['QUERY_STRING'],2);
201 $this->cmd = trim($_GET['q'],'/');
203 $path = trim(dirname($_SERVER['SCRIPT_NAME']),'/');
204 if(isset($path) && strlen($path) && ($path != $this->path))
207 $this->argv = explode('/',$this->cmd);
208 $this->argc = count($this->argv);
209 if((array_key_exists('0',$this->argv)) && strlen($this->argv[0])) {
210 $this->module = $this->argv[0];
213 $this->module = 'home';
216 if($this->cmd === '.well-known/host-meta')
217 require_once('include/hostxrd.php');
220 $this->pager['page'] = ((x($_GET,'page')) ? $_GET['page'] : 1);
221 $this->pager['itemspage'] = 50;
222 $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
223 $this->pager['total'] = 0;
226 function get_baseurl($ssl = false) {
227 if(strlen($this->baseurl))
228 return $this->baseurl;
230 $this->baseurl = (($ssl) ? 'https' : $this->scheme) . "://" . $this->hostname . ((isset($this->path) && strlen($this->path)) ? '/' . $this->path : '' );
231 return $this->baseurl;
234 function set_baseurl($url) {
235 $this->baseurl = $url;
236 $this->hostname = basename($url);
239 function get_hostname() {
240 return $this->hostname;
243 function set_hostname($h) {
244 $this->hostname = $h;
247 function set_path($p) {
248 $this->path = trim(trim($p),'/');
251 function get_path() {
255 function set_pager_total($n) {
256 $this->pager['total'] = intval($n);
259 function set_pager_itemspage($n) {
260 $this->pager['itemspage'] = intval($n);
261 $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
265 function init_pagehead() {
266 $tpl = load_view_file("view/head.tpl");
267 $this->page['htmlhead'] = replace_macros($tpl,array(
268 '$baseurl' => $this->get_baseurl() . '/'
272 function set_curl_code($code) {
273 $this->curl_code = $code;
276 function get_curl_code() {
277 return $this->curl_code;
280 function set_curl_headers($headers) {
281 $this->curl_headers = $headers;
284 function get_curl_headers() {
285 return $this->curl_headers;
291 // retrieve the App structure
292 // useful in functions which require it but don't get it passed to them
294 if(! function_exists('get_app')) {
301 // Multi-purpose function to check variable state.
302 // Usage: x($var) or $x($array,'key')
303 // returns false if variable/key is not set
304 // if variable is set, returns 1 if has 'non-zero' value, otherwise returns 0.
305 // e.g. x('') or x(0) returns 0;
307 if(! function_exists('x')) {
308 function x($s,$k = NULL) {
310 if((is_array($s)) && (array_key_exists($k,$s))) {
328 // called from db initialisation if db is dead.
330 if(! function_exists('system_unavailable')) {
331 function system_unavailable() {
332 include('system_unavailable.php');
336 // Primarily involved with database upgrade, but also sets the
337 // base url for use in cmdline programs which don't have
338 // $_SERVER variables.
340 if(! function_exists('check_config')) {
341 function check_config(&$a) {
343 load_config('system');
345 $build = get_config('system','build');
347 $build = set_config('system','build',BUILD_ID);
349 $url = get_config('system','url');
351 $url = set_config('system','url',$a->get_baseurl());
353 if($build != BUILD_ID) {
354 $stored = intval($build);
355 $current = intval(BUILD_ID);
356 if(($stored < $current) && file_exists('update.php')) {
357 // We're reporting a different version than what is currently installed.
358 // Run any existing update scripts to bring the database up to current.
360 require_once('update.php');
361 for($x = $stored; $x < $current; $x ++) {
362 if(function_exists('update_' . $x)) {
363 $func = 'update_' . $x;
367 set_config('system','build', BUILD_ID);
374 // This is our template processor.
375 // $s is the string requiring macro substitution.
376 // $r is an array of key value pairs (search => replace)
377 // returns substituted string.
378 // WARNING: this is pretty basic, and doesn't properly handle search strings that are substrings of each other.
379 // For instance if 'test' => "foo" and 'testing' => "bar", testing could become either bar or fooing,
380 // depending on the order in which they were declared in the array.
382 if(! function_exists('replace_macros')) {
383 function replace_macros($s,$r) {
388 if(is_array($r) && count($r)) {
389 foreach ($r as $k => $v ) {
394 return str_replace($search,$replace,$s);
398 // load string translation table for alternate language
400 if(! function_exists('load_translation_table')) {
401 function load_translation_table($lang) {
404 if(file_exists("view/$lang/strings.php"))
405 include("view/$lang/strings.php");
408 // translate string if translation exists
410 if(! function_exists('t')) {
415 if(x($a->strings,$s))
416 return $a->strings[$s];
420 // curl wrapper. If binary flag is true, return binary
423 if(! function_exists('fetch_url')) {
424 function fetch_url($url,$binary = false, &$redirects = 0) {
428 $ch = curl_init($url);
429 if(($redirects > 8) || (! $ch))
432 curl_setopt($ch, CURLOPT_HEADER, true);
433 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
436 $curl_time = intval(get_config('system','curl_timeout'));
437 curl_setopt($ch, CURLOPT_TIMEOUT, (($curl_time !== false) ? $curl_time : 60));
439 // by default we will allow self-signed certs
440 // but you can override this
442 $check_cert = get_config('system','verifyssl');
443 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (($check_cert) ? true : false));
445 $prx = get_config('system','proxy');
447 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
448 curl_setopt($ch, CURLOPT_PROXY, $prx);
449 $prxusr = get_config('system','proxyuser');
451 curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
454 curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
456 $a->set_curl_code(0);
458 // don't let curl abort the entire application
459 // if it throws any errors.
461 $s = @curl_exec($ch);
463 $http_code = intval(curl_getinfo($ch, CURLINFO_HTTP_CODE));
464 $header = substr($s,0,strpos($s,"\r\n\r\n"));
465 if(stristr($header,'100') && (strlen($header) < 30)) {
466 // 100 Continue has two headers, get the real one
467 $s = substr($s,strlen($header)+4);
468 $header = substr($s,0,strpos($s,"\r\n\r\n"));
470 if($http_code == 301 || $http_code == 302 || $http_code == 303) {
472 preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
473 $url = trim(array_pop($matches));
474 $url_parsed = parse_url($url);
475 if (isset($url_parsed)) {
477 return fetch_url($url,$binary,$redirects);
480 $a->set_curl_code($http_code);
481 $body = substr($s,strlen($header)+4);
482 $a->set_curl_headers($header);
488 // post request to $url. $params is an array of post variables.
490 if(! function_exists('post_url')) {
491 function post_url($url,$params, $headers = null, &$redirects = 0) {
493 $ch = curl_init($url);
494 if(($redirects > 8) || (! $ch))
497 curl_setopt($ch, CURLOPT_HEADER, true);
498 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
499 curl_setopt($ch, CURLOPT_POST,1);
500 curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
502 $curl_time = intval(get_config('system','curl_timeout'));
503 curl_setopt($ch, CURLOPT_TIMEOUT, (($curl_time !== false) ? $curl_time : 60));
505 if(is_array($headers))
506 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
508 $check_cert = get_config('system','verifyssl');
509 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (($check_cert) ? true : false));
510 $prx = get_config('system','proxy');
512 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
513 curl_setopt($ch, CURLOPT_PROXY, $prx);
514 $prxusr = get_config('system','proxyuser');
516 curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
519 $a->set_curl_code(0);
521 // don't let curl abort the entire application
522 // if it throws any errors.
524 $s = @curl_exec($ch);
526 $http_code = intval(curl_getinfo($ch, CURLINFO_HTTP_CODE));
527 $header = substr($s,0,strpos($s,"\r\n\r\n"));
528 if(stristr($header,'100') && (strlen($header) < 30)) {
529 // 100 Continue has two headers, get the real one
530 $s = substr($s,strlen($header)+4);
531 $header = substr($s,0,strpos($s,"\r\n\r\n"));
533 if($http_code == 301 || $http_code == 302 || $http_code == 303) {
535 preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
536 $url = trim(array_pop($matches));
537 $url_parsed = parse_url($url);
538 if (isset($url_parsed)) {
540 return post_url($url,$binary,$headers,$redirects);
543 $a->set_curl_code($http_code);
544 $body = substr($s,strlen($header)+4);
545 $a->set_curl_headers($header);
551 // random hash, 64 chars
553 if(! function_exists('random_string')) {
554 function random_string() {
555 return(hash('sha256',uniqid(rand(),true)));
559 * This is our primary input filter.
561 * The high bit hack only involved some old IE browser, forget which (IE5/Mac?)
562 * that had an XSS attack vector due to stripping the high-bit on an 8-bit character
563 * after cleansing, and angle chars with the high bit set could get through as markup.
565 * This is now disabled because it was interfering with some legitimate unicode sequences
566 * and hopefully there aren't a lot of those browsers left.
568 * Use this on any text input where angle chars are not valid or permitted
569 * They will be replaced with safer brackets. This may be filtered further
570 * if these are not allowed either.
574 if(! function_exists('notags')) {
575 function notags($string) {
577 return(str_replace(array("<",">"), array('[',']'), $string));
579 // High-bit filter no longer used
580 // return(str_replace(array("<",">","\xBA","\xBC","\xBE"), array('[',']','','',''), $string));
583 // use this on "body" or "content" input where angle chars shouldn't be removed,
584 // and allow them to be safely displayed.
586 if(! function_exists('escape_tags')) {
587 function escape_tags($string) {
589 return(htmlspecialchars($string));
592 // wrapper for adding a login box. If $register == true provide a registration
593 // link. This will most always depend on the value of $a->config['register_policy'].
594 // returns the complete html for inserting into the page
596 if(! function_exists('login')) {
597 function login($register = false) {
599 $register_html = (($register) ? load_view_file("view/register-link.tpl") : "");
601 $noid = get_config('system','no_openid');
603 $classname = 'no-openid';
604 $namelabel = t('Nickname or Email address: ');
605 $passlabel = t('Password: ');
609 $classname = 'openid';
610 $namelabel = t('Nickname/Email/OpenID: ');
611 $passlabel = t("Password \x28if not OpenID\x29: ");
614 $lostpass = t('Forgot your password?');
615 $lostlink = t('Password Reset');
617 if(x($_SESSION,'authenticated')) {
618 $tpl = load_view_file("view/logout.tpl");
621 $tpl = load_view_file("view/login.tpl");
625 $o = replace_macros($tpl,array(
626 '$register_html' => $register_html,
627 '$classname' => $classname,
628 '$namelabel' => $namelabel,
629 '$passlabel' => $passlabel,
631 '$lostpass' => $lostpass,
632 '$lostlink' => $lostlink
638 // generate a string that's random, but usually pronounceable.
639 // used to generate initial passwords
641 if(! function_exists('autoname')) {
642 function autoname($len) {
644 $vowels = array('a','a','ai','au','e','e','e','ee','ea','i','ie','o','ou','u');
645 if(mt_rand(0,5) == 4)
663 's','sc','sh','sm','sp','st',
671 $midcons = array('ck','ct','gn','ld','lf','lm','lt','mb','mm', 'mn','mp',
672 'nd','ng','nk','nt','rn','rp','rt');
674 $noend = array('bl', 'br', 'cl','cr','dr','fl','fr','gl','gr',
675 'kh', 'kl','kr','mn','pl','pr','rh','tr','qu','wh');
677 $start = mt_rand(0,2);
685 for ($x = 0; $x < $len; $x ++) {
686 $r = mt_rand(0,count($table) - 1);
689 if($table == $vowels)
690 $table = array_merge($cons,$midcons);
696 $word = substr($word,0,$len);
698 foreach($noend as $noe) {
699 if((strlen($word) > 2) && (substr($word,-2) == $noe)) {
700 $word = substr($word,0,-1);
704 if(substr($word,-1) == 'q')
705 $word = substr($word,0,-1);
709 // Used to end the current process, after saving session state.
711 if(! function_exists('killme')) {
713 session_write_close();
717 // redirect to another URL and terminate this process.
719 if(! function_exists('goaway')) {
720 function goaway($s) {
721 header("Location: $s");
725 // Generic XML return
726 // Outputs a basic dfrn XML status structure to STDOUT, with a <status> variable
727 // of $st and an optional text <message> of $message and terminates the current process.
729 if(! function_exists('xml_status')) {
730 function xml_status($st, $message = '') {
732 $xml_message = ((strlen($message)) ? "\t<message>" . xmlify($message) . "</message>\r\n" : '');
735 logger('xml_status returning non_zero: ' . $st . " message=" . $message);
737 header( "Content-type: text/xml" );
738 echo '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
739 echo "<result>\r\n\t<status>$st</status>\r\n$xml_message</result>\r\n";
743 // Returns the uid of locally logged in user or false.
745 if(! function_exists('local_user')) {
746 function local_user() {
747 if((x($_SESSION,'authenticated')) && (x($_SESSION,'uid')))
748 return intval($_SESSION['uid']);
752 // Returns contact id of authenticated site visitor or false
754 if(! function_exists('remote_user')) {
755 function remote_user() {
756 if((x($_SESSION,'authenticated')) && (x($_SESSION,'visitor_id')))
757 return intval($_SESSION['visitor_id']);
761 // contents of $s are displayed prominently on the page the next time
762 // a page is loaded. Usually used for errors or alerts.
764 if(! function_exists('notice')) {
765 function notice($s) {
768 $_SESSION['sysmsg'] .= $s;
771 // wrapper around config to limit the text length of an incoming message
773 if(! function_exists('get_max_import_size')) {
774 function get_max_import_size() {
776 return ((x($a->config,'max_import_size')) ? $a->config['max_import_size'] : 0 );
780 // escape text ($str) for XML transport
781 // returns escaped text.
783 if(! function_exists('xmlify')) {
784 function xmlify($str) {
787 for($x = 0; $x < strlen($str); $x ++) {
818 $buffer = trim($buffer);
823 // pass xml escaped text ($s), returns unescaped text
825 if(! function_exists('unxmlify')) {
826 function unxmlify($s) {
827 $ret = str_replace('&','&', $s);
828 $ret = str_replace(array('<','>','"','''),array('<','>','"',"'"),$ret);
832 // convenience wrapper, reverse the operation "bin2hex"
834 if(! function_exists('hex2bin')) {
835 function hex2bin($s) {
836 return(pack("H*",$s));
839 // Automatic pagination.
840 // To use, get the count of total items.
841 // Then call $a->set_pager_total($number_items);
842 // Optionally call $a->set_pager_itemspage($n) to the number of items to display on each page
843 // Then call paginate($a) after the end of the display loop to insert the pager block on the page
844 // (assuming there are enough items to paginate).
845 // When using with SQL, the setting LIMIT %d, %d => $a->pager['start'],$a->pager['itemspage']
846 // will limit the results to the correct items for the current page.
847 // The actual page handling is then accomplished at the application layer.
849 if(! function_exists('paginate')) {
850 function paginate(&$a) {
852 $stripped = preg_replace('/(&page=[0-9]*)/','',$_SERVER['QUERY_STRING']);
853 $stripped = str_replace('q=','',$stripped);
854 $stripped = trim($stripped,'/');
855 $url = $a->get_baseurl() . '/' . $stripped;
858 if($a->pager['total'] > $a->pager['itemspage']) {
859 $o .= '<div class="pager">';
860 if($a->pager['page'] != 1)
861 $o .= '<span class="pager_prev">'."<a href=\"$url".'&page='.($a->pager['page'] - 1).'">' . t('prev') . '</a></span> ';
863 $o .= "<span class=\"pager_first\"><a href=\"$url"."&page=1\">" . t('first') . "</a></span> ";
865 $numpages = $a->pager['total'] / $a->pager['itemspage'];
868 $numstop = $numpages;
871 $numstart = (($pagenum > 7) ? ($pagenum - 7) : 1);
872 $numstop = (($pagenum > ($numpages - 7)) ? $numpages : ($numstart + 14));
875 for($i = $numstart; $i <= $numstop; $i++){
876 if($i == $a->pager['page'])
877 $o .= '<span class="pager_current">'.(($i < 10) ? ' '.$i : $i);
879 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? ' '.$i : $i)."</a>";
883 if(($a->pager['total'] % $a->pager['itemspage']) != 0) {
884 if($i == $a->pager['page'])
885 $o .= '<span class="pager_current">'.(($i < 10) ? ' '.$i : $i);
887 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? ' '.$i : $i)."</a>";
891 $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
892 $o .= "<span class=\"pager_last\"><a href=\"$url"."&page=$lastpage\">" . t('last') . "</a></span> ";
894 if(($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0)
895 $o .= '<span class="pager_next">'."<a href=\"$url"."&page=".($a->pager['page'] + 1).'">' . t('next') . '</a></span>';
896 $o .= '</div>'."\r\n";
901 // Turn user/group ACLs stored as angle bracketed text into arrays
903 if(! function_exists('expand_acl')) {
904 function expand_acl($s) {
905 // turn string array of angle-bracketed elements into numeric array
906 // e.g. "<1><2><3>" => array(1,2,3);
910 $t = str_replace('<','',$s);
911 $a = explode('>',$t);
914 $ret[] = intval($aa);
920 // Used to wrap ACL elements in angle brackets for storage
922 if(! function_exists('sanitise_acl')) {
923 function sanitise_acl(&$item) {
925 $item = '<' . intval(notags(trim($item))) . '>';
930 // retrieve a "family" of config variables from database to cached storage
932 if(! function_exists('load_config')) {
933 function load_config($family) {
935 $r = q("SELECT * FROM `config` WHERE `cat` = '%s'",
941 $a->config[$family][$k] = $rr['v'];
946 // get a particular config variable given the family name
947 // and key. Returns false if not set.
948 // $instore is only used by the set_config function
949 // to determine if the key already exists in the DB
950 // If a key is found in the DB but doesn't exist in
951 // local config cache, pull it into the cache so we don't have
952 // to hit the DB again for this item.
954 if(! function_exists('get_config')) {
955 function get_config($family, $key, $instore = false) {
960 if(isset($a->config[$family][$key])) {
961 if($a->config[$family][$key] === '!<unset>!') {
964 return $a->config[$family][$key];
967 $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
972 $a->config[$family][$key] = $ret[0]['v'];
976 $a->config[$family][$key] = '!<unset>!';
981 // Store a config value ($value) in the category ($family)
982 // under the key ($key)
983 // Return the value, or false if the database update failed
985 if(! function_exists('set_config')) {
986 function set_config($family,$key,$value) {
989 $a->config[$family][$key] = $value;
991 if(get_config($family,$key,true) === false) {
992 $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
1001 $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
1011 // convert an XML document to a normalised, case-corrected array
1012 // used by webfinger
1014 if(! function_exists('convert_xml_element_to_array')) {
1015 function convert_xml_element_to_array($xml_element, &$recursion_depth=0) {
1017 // If we're getting too deep, bail out
1018 if ($recursion_depth > 512) {
1022 if (!is_string($xml_element) &&
1023 !is_array($xml_element) &&
1024 (get_class($xml_element) == 'SimpleXMLElement')) {
1025 $xml_element_copy = $xml_element;
1026 $xml_element = get_object_vars($xml_element);
1029 if (is_array($xml_element)) {
1030 $result_array = array();
1031 if (count($xml_element) <= 0) {
1032 return (trim(strval($xml_element_copy)));
1035 foreach($xml_element as $key=>$value) {
1038 $result_array[strtolower($key)] =
1039 convert_xml_element_to_array($value, $recursion_depth);
1042 if ($recursion_depth == 0) {
1043 $temp_array = $result_array;
1044 $result_array = array(
1045 strtolower($xml_element_copy->getName()) => $temp_array,
1049 return ($result_array);
1052 return (trim(strval($xml_element)));
1056 // Given an email style address, perform webfinger lookup and
1057 // return the resulting DFRN profile URL, or if no DFRN profile URL
1058 // is located, returns an OStatus subscription template (prefixed
1059 // with the string 'stat:' to identify it as on OStatus template).
1060 // If this isn't an email style address just return $s.
1061 // Return an empty string if email-style addresses but webfinger fails,
1062 // or if the resultant personal XRD doesn't contain a supported
1063 // subscription/friend-request attribute.
1065 if(! function_exists('webfinger_dfrn')) {
1066 function webfinger_dfrn($s) {
1067 if(! strstr($s,'@')) {
1070 $links = webfinger($s);
1071 logger('webfinger_dfrn: ' . $s . ':' . print_r($links,true), LOGGER_DATA);
1073 foreach($links as $link)
1074 if($link['@attributes']['rel'] === NAMESPACE_DFRN)
1075 return $link['@attributes']['href'];
1076 foreach($links as $link)
1077 if($link['@attributes']['rel'] === NAMESPACE_OSTATUSSUB)
1078 return 'stat:' . $link['@attributes']['template'];
1083 // Given an email style address, perform webfinger lookup and
1084 // return the array of link attributes from the personal XRD file.
1085 // On error/failure return an empty array.
1088 if(! function_exists('webfinger')) {
1089 function webfinger($s) {
1091 if(strstr($s,'@')) {
1092 $host = substr($s,strpos($s,'@') + 1);
1095 $tpl = fetch_lrdd_template($host);
1096 logger('webfinger: lrdd template: ' . $tpl);
1098 $pxrd = str_replace('{uri}', urlencode('acct:'.$s), $tpl);
1099 $links = fetch_xrd_links($pxrd);
1100 if(! count($links)) {
1101 // try with double slashes
1102 $pxrd = str_replace('{uri}', urlencode('acct://'.$s), $tpl);
1103 $links = fetch_xrd_links($pxrd);
1111 if(! function_exists('lrdd')) {
1112 function lrdd($uri) {
1116 if(strstr($uri,'@')) {
1117 return(webfinger($uri));
1120 $html = fetch_url($uri);
1121 $headers = $a->get_curl_headers();
1122 $lines = explode("\n",$headers);
1124 foreach($lines as $line) {
1125 // TODO alter the following regex to support multiple relations (space separated)
1126 if((stristr($line,'link:')) && preg_match('/<([^>].*)>.*rel\=[\'\"]lrdd[\'\"]/',$line,$matches)) {
1127 $link = $matches[1];
1132 if(! isset($link)) {
1133 // parse the page of the supplied URL looking for rel links
1135 require_once('library/HTML5/Parser.php');
1136 $dom = HTML5_Parser::parse($html);
1139 $items = $dom->getElementsByTagName('link');
1141 foreach($items as $item) {
1142 $x = $item->getAttribute('rel');
1144 $link = $item->getAttribute('href');
1152 return(fetch_xrd_links($link));
1159 // Given a host name, locate the LRDD template from that
1160 // host. Returns the LRDD template or an empty string on
1163 if(! function_exists('fetch_lrdd_template')) {
1164 function fetch_lrdd_template($host) {
1166 $url = 'http://' . $host . '/.well-known/host-meta' ;
1167 $links = fetch_xrd_links($url);
1169 foreach($links as $link)
1170 if($link['@attributes']['rel'] && $link['@attributes']['rel'] === 'lrdd')
1171 $tpl = $link['@attributes']['template'];
1173 if(! strpos($tpl,'{uri}'))
1178 // Given a URL, retrieve the page as an XRD document.
1179 // Return an array of links.
1180 // on error/failure return empty array.
1182 if(! function_exists('fetch_xrd_links')) {
1183 function fetch_xrd_links($url) {
1186 $xml = fetch_url($url);
1190 logger('fetch_xrd_links: ' . $xml, LOGGER_DATA);
1191 $h = simplexml_load_string($xml);
1192 $arr = convert_xml_element_to_array($h);
1194 if(isset($arr['xrd']['link'])) {
1195 $link = $arr['xrd']['link'];
1196 if(! isset($link[0]))
1197 $links = array($link);
1205 // Convert an ACL array to a storable string
1207 if(! function_exists('perms2str')) {
1208 function perms2str($p) {
1211 if(is_array($tmp)) {
1212 array_walk($tmp,'sanitise_acl');
1213 $ret = implode('',$tmp);
1218 // generate a guaranteed unique (for this domain) item ID for ATOM
1219 // safe from birthday paradox
1221 if(! function_exists('item_new_uri')) {
1222 function item_new_uri($hostname,$uid) {
1226 $hash = random_string();
1228 $uri = "urn:X-dfrn:" . $hostname . ':' . $uid . ':' . $hash;
1230 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
1234 } while($dups == true);
1238 // Generate a guaranteed unique photo ID.
1239 // safe from birthday paradox
1241 if(! function_exists('photo_new_resource')) {
1242 function photo_new_resource() {
1246 $resource = hash('md5',uniqid(mt_rand(),true));
1247 $r = q("SELECT `id` FROM `photo` WHERE `resource-id` = '%s' LIMIT 1",
1252 } while($found == true);
1257 // Take a URL from the wild, prepend http:// if necessary
1258 // and check DNS to see if it's real
1259 // return true if it's OK, false if something is wrong with it
1261 if(! function_exists('validate_url')) {
1262 function validate_url(&$url) {
1263 if(substr($url,0,4) != 'http')
1264 $url = 'http://' . $url;
1265 $h = parse_url($url);
1267 if(($h) && (checkdnsrr($h['host'], 'ANY'))) {
1273 // checks that email is an actual resolvable internet address
1275 if(! function_exists('validate_email')) {
1276 function validate_email($addr) {
1278 if(! strpos($addr,'@'))
1280 $h = substr($addr,strpos($addr,'@') + 1);
1282 if(($h) && (checkdnsrr($h, 'ANY'))) {
1288 // Check $url against our list of allowed sites,
1289 // wildcards allowed. If allowed_sites is unset return true;
1290 // If url is allowed, return true.
1291 // otherwise, return false
1293 if(! function_exists('allowed_url')) {
1294 function allowed_url($url) {
1296 $h = parse_url($url);
1302 $str_allowed = get_config('system','allowed_sites');
1308 $host = strtolower($h['host']);
1310 // always allow our own site
1312 if($host == strtolower($_SERVER['SERVER_NAME']))
1315 $fnmatch = function_exists('fnmatch');
1316 $allowed = explode(',',$str_allowed);
1318 if(count($allowed)) {
1319 foreach($allowed as $a) {
1320 $pat = strtolower(trim($a));
1321 if(($fnmatch && fnmatch($pat,$host)) || ($pat == $host)) {
1330 // check if email address is allowed to register here.
1331 // Compare against our list (wildcards allowed).
1332 // Returns false if not allowed, true if allowed or if
1333 // allowed list is not configured.
1335 if(! function_exists('allowed_email')) {
1336 function allowed_email($email) {
1339 $domain = strtolower(substr($email,strpos($email,'@') + 1));
1343 $str_allowed = get_config('system','allowed_email');
1349 $fnmatch = function_exists('fnmatch');
1350 $allowed = explode(',',$str_allowed);
1352 if(count($allowed)) {
1353 foreach($allowed as $a) {
1354 $pat = strtolower(trim($a));
1355 if(($fnmatch && fnmatch($pat,$host)) || ($pat == $host)) {
1364 // Format the like/dislike text for a profile item
1365 // $cnt = number of people who like/dislike the item
1366 // $arr = array of pre-linked names of likers/dislikers
1367 // $type = one of 'like, 'dislike'
1369 // returns formatted text
1371 if(! function_exists('format_like')) {
1372 function format_like($cnt,$arr,$type,$id) {
1375 $o .= $arr[0] . (($type === 'like') ? t(' likes this.') : t(' doesn\'t like this.')) . EOL ;
1377 $o .= '<span class="fakelink" onclick="openClose(\'' . $type . 'list-' . $id . '\');" >'
1378 . $cnt . ' ' . t('people') . '</span> ' . (($type === 'like') ? t('like this.') : t('don\'t like this.')) . EOL ;
1379 $total = count($arr);
1380 if($total >= MAX_LIKERS)
1381 $arr = array_slice($arr, 0, MAX_LIKERS - 1);
1382 if($total < MAX_LIKERS)
1383 $arr[count($arr)-1] = t('and') . ' ' . $arr[count($arr)-1];
1384 $str = implode(', ', $arr);
1385 if($total >= MAX_LIKERS)
1386 $str .= t(', and ') . $total - MAX_LIKERS . t(' other people');
1387 $str .= (($type === 'like') ? t(' like this.') : t(' don\'t like this.'));
1388 $o .= "\t" . '<div id="' . $type . 'list-' . $id . '" style="display: none;" >' . $str . '</div>';
1394 // wrapper to load a view template, checking for alternate
1395 // languages before falling back to the default
1397 if(! function_exists('load_view_file')) {
1398 function load_view_file($s) {
1401 $lang = get_config('system','language');
1404 if(file_exists("$d/$lang/$b"))
1405 return file_get_contents("$d/$lang/$b");
1406 return file_get_contents($s);
1409 // for html,xml parsing - let's say you've got
1410 // an attribute foobar="class1 class2 class3"
1411 // and you want to find out if it contains 'class3'.
1412 // you can't use a normal sub string search because you
1413 // might match 'notclass3' and a regex to do the job is
1414 // possible but a bit complicated.
1415 // pass the attribute string as $attr and the attribute you
1416 // are looking for as $s - returns true if found, otherwise false
1418 if(! function_exists('attribute_contains')) {
1419 function attribute_contains($attr,$s) {
1420 $a = explode(' ', $attr);
1421 if(count($a) && in_array($s,$a))
1426 if(! function_exists('logger')) {
1427 function logger($msg,$level = 0) {
1429 $debugging = get_config('system','debugging');
1430 $loglevel = intval(get_config('system','loglevel'));
1431 $logfile = get_config('system','logfile');
1433 if((! $debugging) || (! $logfile) || ($level > $loglevel))
1436 @file_put_contents($logfile, datetime_convert() . ':' . session_id() . ' ' . $msg . "\n", FILE_APPEND);
1441 if(! function_exists('activity_match')) {
1442 function activity_match($haystack,$needle) {
1443 if(($haystack === $needle) || ((basename($needle) === $haystack) && strstr($needle,NAMESPACE_ACTIVITY_SCHEMA)))
1449 // Pull out all #hashtags and @person tags from $s;
1450 // We also get @person@domain.com - which would make
1451 // the regex quite complicated as tags can also
1452 // end a sentence. So we'll run through our results
1453 // and strip the period from any tags which end with one.
1454 // Returns array of tags found, or empty array.
1457 if(! function_exists('get_tags')) {
1458 function get_tags($s) {
1460 if(preg_match_all('/([@#][^ \x0D\x0A,:?]*)([ \x0D\x0A,:?]|$)/',$s,$match)) {
1461 foreach($match[1] as $match) {
1462 if(strstr($match,"]")) {
1463 // we might be inside a bbcode color tag - leave it alone
1466 if(substr($match,-1,1) === '.')
1467 $ret[] = substr($match,0,-1);
1477 // quick and dirty quoted_printable encoding
1479 if(! function_exists('qp')) {
1481 return str_replace ("%","=",rawurlencode($s));
1485 if(! function_exists('like_puller')) {
1486 function like_puller($a,$item,&$arr,$mode) {
1490 $verb = (($mode === 'like') ? ACTIVITY_LIKE : ACTIVITY_DISLIKE);
1492 if((activity_match($item['verb'],$verb)) && ($item['id'] != $item['parent'])) {
1493 $url = $item['author-link'];
1494 if(($item['network'] === 'dfrn') && (! $item['self']) && ($item['author-link'] == $item['url'])) {
1495 $url = $a->get_baseurl() . '/redir/' . $item['contact-id'];
1496 $sparkle = ' class="sparkle" ';
1498 if(! ((isset($arr[$item['parent'] . '-l'])) && (is_array($arr[$item['parent'] . '-l']))))
1499 $arr[$item['parent'] . '-l'] = array();
1500 if(! isset($arr[$item['parent']]))
1501 $arr[$item['parent']] = 1;
1503 $arr[$item['parent']] ++;
1504 $arr[$item['parent'] . '-l'][] = '<a href="'. $url . '"'. $sparkle .'>' . $item['author-name'] . '</a>';
1509 if(! function_exists('get_mentions')) {
1510 function get_mentions($item) {
1512 if(! strlen($item['tag']))
1515 $arr = explode(',',$item['tag']);
1516 foreach($arr as $x) {
1518 if(preg_match('/@\[url=([^\]]*)\]/',$x,$matches))
1519 $o .= "\t\t" . '<link rel="mentioned" href="' . $matches[1] . '" />' . "\r\n";
1524 if(! function_exists('contact_block')) {
1525 function contact_block() {
1528 if((! is_array($a->profile)) || ($a->profile['hide-friends']))
1530 $r = q("SELECT COUNT(*) AS `total` FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 and `pending` = 0",
1531 intval($a->profile['uid'])
1534 $total = intval($r[0]['total']);
1537 $o .= '<h4 class="contact-h4">' . t('No contacts') . '</h4>';
1540 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 and `pending` = 0 ORDER BY RAND() LIMIT 24",
1541 intval($a->profile['uid'])
1544 $o .= '<h4 class="contact-h4">' . $total . ' ' . t('Contacts') . '</h4><div id="contact-block">';
1545 foreach($r as $rr) {
1546 $redirect_url = $a->get_baseurl() . '/redir/' . $rr['id'];
1547 if(local_user() && ($rr['uid'] == local_user())
1548 && ($rr['network'] === 'dfrn')) {
1549 $url = $redirect_url;
1550 $sparkle = ' sparkle';
1557 $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";
1559 $o .= '</div><div id="contact-block-end"></div>';
1560 $o .= '<div id="viewcontacts"><a id="viewcontacts-link" href="viewcontacts/' . $a->profile['nickname'] . '">' . t('View Contacts') . '</a></div>';
1567 if(! function_exists('search')) {
1568 function search($s) {
1570 $o = '<div id="search-box">';
1571 $o .= '<form action="' . $a->get_baseurl() . '/search' . '" method="get" >';
1572 $o .= '<input type="text" name="search" id="search-text" value="' . $s .'" />';
1573 $o .= '<input type="submit" name="submit" id="search-submit" value="' . t('Search') . '" />';
1574 $o .= '</form></div>';
1578 if(! function_exists('valid_email')) {
1579 function valid_email($x){
1580 if(preg_match('/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+$/',$x))
1586 if(! function_exists('gravatar_img')) {
1587 function gravatar_img($email) {
1589 $opt = 'identicon'; // psuedo-random geometric pattern if not found
1591 $hash = md5(trim(strtolower($email)));
1593 $url = 'http://www.gravatar.com/avatar/' . $hash . '.jpg'
1594 . '?s=' . $size . '&d=' . $opt . '&r=' . $rating;
1596 logger('gravatar: ' . $email . ' ' . $url);
1600 if(! function_exists('aes_decrypt')) {
1601 function aes_decrypt($val,$ky)
1603 $key="\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
1604 for($a=0;$a<strlen($ky);$a++)
1605 $key[$a%16]=chr(ord($key[$a%16]) ^ ord($ky[$a]));
1606 $mode = MCRYPT_MODE_ECB;
1607 $enc = MCRYPT_RIJNDAEL_128;
1608 $dec = @mcrypt_decrypt($enc, $key, $val, $mode, @mcrypt_create_iv( @mcrypt_get_iv_size($enc, $mode), MCRYPT_DEV_URANDOM ) );
1609 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));
1613 if(! function_exists('aes_encrypt')) {
1614 function aes_encrypt($val,$ky)
1616 $key="\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
1617 for($a=0;$a<strlen($ky);$a++)
1618 $key[$a%16]=chr(ord($key[$a%16]) ^ ord($ky[$a]));
1619 $mode=MCRYPT_MODE_ECB;
1620 $enc=MCRYPT_RIJNDAEL_128;
1621 $val=str_pad($val, (16*(floor(strlen($val) / 16)+(strlen($val) % 16==0?2:1))), chr(16-(strlen($val) % 16)));
1622 return mcrypt_encrypt($enc, $key, $val, $mode, mcrypt_create_iv( mcrypt_get_iv_size($enc, $mode), MCRYPT_DEV_URANDOM));
1625 if(! function_exists('linkify')) {
1626 function linkify($s) {
1627 $s = preg_replace("/(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\.\=\_\~\#\'\%]*)/", ' <a href="$1" >$1</a>', $s);
1631 if(! function_exists('smilies')) {
1632 function smilies($s) {
1636 array( ':-)', ';-)', ':-(', ':(', ':-P', ':-"', ':-x', ':-X', ':-D', '8-|', '8-O'),
1638 '<img src="' . $a->get_baseurl() . '/images/smiley-smile.gif" alt=":-)" />',
1639 '<img src="' . $a->get_baseurl() . '/images/smiley-wink.gif" alt=";-)" />',
1640 '<img src="' . $a->get_baseurl() . '/images/smiley-frown.gif" alt=":-(" />',
1641 '<img src="' . $a->get_baseurl() . '/images/smiley-frown.gif" alt=":(" />',
1642 '<img src="' . $a->get_baseurl() . '/images/smiley-tongue-out.gif" alt=":-P" />',
1643 '<img src="' . $a->get_baseurl() . '/images/smiley-kiss.gif" alt=":-\"" />',
1644 '<img src="' . $a->get_baseurl() . '/images/smiley-kiss.gif" alt=":-x" />',
1645 '<img src="' . $a->get_baseurl() . '/images/smiley-kiss.gif" alt=":-X" />',
1646 '<img src="' . $a->get_baseurl() . '/images/smiley-laughing.gif" alt=":-D" />',
1647 '<img src="' . $a->get_baseurl() . '/images/smiley-surprised.gif" alt="8-|" />',
1648 '<img src="' . $a->get_baseurl() . '/images/smiley-surprised.gif" alt="8-O" />'