5 define ( 'BUILD_ID', 1031 );
6 define ( 'FRIENDIKA_VERSION', '2.01.1000' );
7 define ( 'DFRN_PROTOCOL_VERSION', '2.0' );
9 define ( 'EOL', "<br />\r\n" );
10 define ( 'ATOM_TIME', 'Y-m-d\TH:i:s\Z' );
11 define ( 'DOWN_ARROW', '⇩' );
17 define ( 'LOGGER_NORMAL', 0 );
18 define ( 'LOGGER_TRACE', 1 );
19 define ( 'LOGGER_DEBUG', 2 );
20 define ( 'LOGGER_DATA', 3 );
21 define ( 'LOGGER_ALL', 4 );
24 * registration policies
27 define ( 'REGISTER_CLOSED', 0 );
28 define ( 'REGISTER_APPROVE', 1 );
29 define ( 'REGISTER_OPEN', 2 );
35 define ( 'REL_VIP', 1);
36 define ( 'REL_FAN', 2);
37 define ( 'REL_BUD', 3);
43 define ( 'HOOK_HOOK', 0);
44 define ( 'HOOK_FILE', 1);
45 define ( 'HOOK_FUNCTION', 2);
51 * PAGE_NORMAL is a typical personal profile account
52 * PAGE_SOAPBOX automatically approves all friend requests as REL_FAN, (readonly)
53 * PAGE_COMMUNITY automatically approves all friend requests as REL_FAN, but with
54 * write access to wall and comments (no email and not included in page owner's ACL lists)
55 * PAGE_FREELOVE automatically approves all friend requests as full friends (REL_BUD).
59 define ( 'PAGE_NORMAL', 0 );
60 define ( 'PAGE_SOAPBOX', 1 );
61 define ( 'PAGE_COMMUNITY', 2 );
62 define ( 'PAGE_FREELOVE', 3 );
65 * Maximum number of "people who like (or don't like) this" that we will list by name
68 define ( 'MAX_LIKERS', 75);
71 * email notification options
74 define ( 'NOTIFY_INTRO', 0x0001 );
75 define ( 'NOTIFY_CONFIRM', 0x0002 );
76 define ( 'NOTIFY_WALL', 0x0004 );
77 define ( 'NOTIFY_COMMENT', 0x0008 );
78 define ( 'NOTIFY_MAIL', 0x0010 );
81 * various namespaces we may need to parse
84 define ( 'NAMESPACE_DFRN' , 'http://purl.org/macgirvin/dfrn/1.0' );
85 define ( 'NAMESPACE_THREAD' , 'http://purl.org/syndication/thread/1.0' );
86 define ( 'NAMESPACE_TOMB' , 'http://purl.org/atompub/tombstones/1.0' );
87 define ( 'NAMESPACE_ACTIVITY', 'http://activitystrea.ms/spec/1.0/' );
88 define ( 'NAMESPACE_ACTIVITY_SCHEMA', 'http://activitystrea.ms/schema/1.0/' );
89 define ( 'NAMESPACE_MEDIA', 'http://purl.org/syndication/atommedia' );
90 define ( 'NAMESPACE_SALMON_ME', 'http://salmon-protocol.org/ns/magic-env' );
91 define ( 'NAMESPACE_OSTATUSSUB', 'http://ostatus.org/schema/1.0/subscribe' );
92 define ( 'NAMESPACE_GEORSS', 'http://www.georss.org/georss' );
93 define ( 'NAMESPACE_POCO', 'http://portablecontacts.net/spec/1.0' );
94 define ( 'NAMESPACE_FEED', 'http://schemas.google.com/g/2010#updates-from' );
97 * activity stream defines
100 define ( 'ACTIVITY_LIKE', NAMESPACE_ACTIVITY_SCHEMA . 'like' );
101 define ( 'ACTIVITY_DISLIKE', NAMESPACE_DFRN . '/dislike' );
102 define ( 'ACTIVITY_OBJ_HEART', NAMESPACE_DFRN . '/heart' );
104 define ( 'ACTIVITY_FRIEND', NAMESPACE_ACTIVITY_SCHEMA . 'make-friend' );
105 define ( 'ACTIVITY_FOLLOW', NAMESPACE_ACTIVITY_SCHEMA . 'follow' );
106 define ( 'ACTIVITY_UNFOLLOW', NAMESPACE_ACTIVITY_SCHEMA . 'stop-following' );
107 define ( 'ACTIVITY_POST', NAMESPACE_ACTIVITY_SCHEMA . 'post' );
108 define ( 'ACTIVITY_UPDATE', NAMESPACE_ACTIVITY_SCHEMA . 'update' );
109 define ( 'ACTIVITY_TAG', NAMESPACE_ACTIVITY_SCHEMA . 'tag' );
111 define ( 'ACTIVITY_OBJ_COMMENT', NAMESPACE_ACTIVITY_SCHEMA . 'comment' );
112 define ( 'ACTIVITY_OBJ_NOTE', NAMESPACE_ACTIVITY_SCHEMA . 'note' );
113 define ( 'ACTIVITY_OBJ_PERSON', NAMESPACE_ACTIVITY_SCHEMA . 'person' );
114 define ( 'ACTIVITY_OBJ_PHOTO', NAMESPACE_ACTIVITY_SCHEMA . 'photo' );
115 define ( 'ACTIVITY_OBJ_P_PHOTO', NAMESPACE_ACTIVITY_SCHEMA . 'profile-photo' );
116 define ( 'ACTIVITY_OBJ_ALBUM', NAMESPACE_ACTIVITY_SCHEMA . 'photo-album' );
119 * item weight for query ordering
122 define ( 'GRAVITY_PARENT', 0);
123 define ( 'GRAVITY_LIKE', 3);
124 define ( 'GRAVITY_COMMENT', 6);
128 * Reverse the effect of magic_quotes_gpc if it is enabled.
129 * Please disable magic_quotes_gpc so we don't have to do this.
130 * See http://php.net/manual/en/security.magicquotes.disabling.php
134 if (get_magic_quotes_gpc()) {
135 $process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
136 while (list($key, $val) = each($process)) {
137 foreach ($val as $k => $v) {
138 unset($process[$key][$k]);
140 $process[$key][stripslashes($k)] = $v;
141 $process[] = &$process[$key][stripslashes($k)];
143 $process[$key][stripslashes($k)] = stripslashes($v);
155 * Our main application structure for the life of this page
156 * Primarily deals with the URL that got us here
157 * and tries to make some sense of it, and
158 * stores our page contents and config storage
159 * and anything else that might need to be passed around
160 * before we spit the page out.
164 if(! class_exists('App')) {
167 public $module_loaded = false;
168 public $query_string;
177 public $error = false;
187 public $interactive = true;
196 private $curl_headers;
198 function __construct() {
200 $this->config = array();
201 $this->page = array();
202 $this->pager= array();
204 $this->query_string = '';
206 $this->scheme = ((isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'])) ? 'https' : 'http' );
208 if(x($_SERVER,'SERVER_NAME'))
209 $this->hostname = $_SERVER['SERVER_NAME'];
211 set_include_path("include/$this->hostname" . PATH_SEPARATOR . 'include' . PATH_SEPARATOR . '.' );
213 if((x($_SERVER,'QUERY_STRING')) && substr($_SERVER['QUERY_STRING'],0,2) === "q=")
214 $this->query_string = substr($_SERVER['QUERY_STRING'],2);
216 $this->cmd = trim($_GET['q'],'/\\');
219 * Figure out if we are running at the top of a domain
220 * or in a sub-directory and adjust accordingly
223 $path = trim(dirname($_SERVER['SCRIPT_NAME']),'/\\');
224 if(isset($path) && strlen($path) && ($path != $this->path))
230 * Break the URL path into C style argc/argv style arguments for our
231 * modules. Given "http://example.com/module/arg1/arg2", $this->argc
232 * will be 3 (integer) and $this->argv will contain:
238 * There will always be one argument. If provided a naked domain
239 * URL, $this->argv[0] is set to "home".
243 $this->argv = explode('/',$this->cmd);
244 $this->argc = count($this->argv);
245 if((array_key_exists('0',$this->argv)) && strlen($this->argv[0])) {
246 $this->module = $this->argv[0];
249 $this->module = 'home';
253 * Special handling for the webfinger/lrdd host XRD file
254 * Just spit out the contents and exit.
257 if($this->cmd === '.well-known/host-meta')
258 require_once('include/hostxrd.php');
262 * See if there is any page number information, and initialise
266 $this->pager['page'] = ((x($_GET,'page')) ? $_GET['page'] : 1);
267 $this->pager['itemspage'] = 50;
268 $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
269 $this->pager['total'] = 0;
272 function get_baseurl($ssl = false) {
273 if(strlen($this->baseurl))
274 return $this->baseurl;
276 $this->baseurl = (($ssl) ? 'https' : $this->scheme) . "://" . $this->hostname . ((isset($this->path) && strlen($this->path)) ? '/' . $this->path : '' );
277 return $this->baseurl;
280 function set_baseurl($url) {
281 $this->baseurl = $url;
282 $this->hostname = basename($url);
285 function get_hostname() {
286 return $this->hostname;
289 function set_hostname($h) {
290 $this->hostname = $h;
293 function set_path($p) {
294 $this->path = trim(trim($p),'/');
297 function get_path() {
301 function set_pager_total($n) {
302 $this->pager['total'] = intval($n);
305 function set_pager_itemspage($n) {
306 $this->pager['itemspage'] = intval($n);
307 $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
311 function init_pagehead() {
312 $this->page['title'] = $this->config['sitename'];
313 $tpl = load_view_file("view/head.tpl");
314 $this->page['htmlhead'] = replace_macros($tpl,array(
315 '$baseurl' => $this->get_baseurl() . '/',
316 '$generator' => 'Friendika' . ' ' . FRIENDIKA_VERSION
320 function set_curl_code($code) {
321 $this->curl_code = $code;
324 function get_curl_code() {
325 return $this->curl_code;
328 function set_curl_headers($headers) {
329 $this->curl_headers = $headers;
332 function get_curl_headers() {
333 return $this->curl_headers;
339 // retrieve the App structure
340 // useful in functions which require it but don't get it passed to them
342 if(! function_exists('get_app')) {
349 // Multi-purpose function to check variable state.
350 // Usage: x($var) or $x($array,'key')
351 // returns false if variable/key is not set
352 // if variable is set, returns 1 if has 'non-zero' value, otherwise returns 0.
353 // e.g. x('') or x(0) returns 0;
355 if(! function_exists('x')) {
356 function x($s,$k = NULL) {
358 if((is_array($s)) && (array_key_exists($k,$s))) {
376 // called from db initialisation if db is dead.
378 if(! function_exists('system_unavailable')) {
379 function system_unavailable() {
380 include('system_unavailable.php');
384 // Primarily involved with database upgrade, but also sets the
385 // base url for use in cmdline programs which don't have
386 // $_SERVER variables, and synchronising the state of installed plugins.
389 if(! function_exists('check_config')) {
390 function check_config(&$a) {
392 load_config('system');
394 $build = get_config('system','build');
396 $build = set_config('system','build',BUILD_ID);
398 $url = get_config('system','url');
400 $url = set_config('system','url',$a->get_baseurl());
402 if($build != BUILD_ID) {
403 $stored = intval($build);
404 $current = intval(BUILD_ID);
405 if(($stored < $current) && file_exists('update.php')) {
406 // We're reporting a different version than what is currently installed.
407 // Run any existing update scripts to bring the database up to current.
409 require_once('update.php');
410 for($x = $stored; $x < $current; $x ++) {
411 if(function_exists('update_' . $x)) {
412 $func = 'update_' . $x;
416 set_config('system','build', BUILD_ID);
422 * Synchronise plugins:
424 * $a->config['system']['addon'] contains a comma-separated list of names
425 * of plugins/addons which are used on this system.
426 * Go through the database list of already installed addons, and if we have
427 * an entry, but it isn't in the config list, call the uninstall procedure
428 * and mark it uninstalled in the database (for now we'll remove it).
429 * Then go through the config list and if we have a plugin that isn't installed,
430 * call the install procedure and add it to the database.
434 $r = q("SELECT * FROM `addon` WHERE `installed` = 1");
438 $installed = array();
440 $plugins = get_config('system','addon');
441 $plugins_arr = array();
444 $plugins_arr = explode(',',str_replace(' ', '',$plugins));
446 $installed_arr = array();
448 if(count($installed)) {
449 foreach($installed as $i) {
450 if(! in_array($i['name'],$plugins_arr)) {
451 logger("Addons: uninstalling " . $i['name']);
452 q("DELETE FROM `addon` WHERE `id` = %d LIMIT 1",
456 @include_once('addon/' . $i['name'] . '/' . $i['name'] . '.php');
457 if(function_exists($i['name'] . '_uninstall')) {
458 $func = $i['name'] . '_uninstall';
463 $installed_arr[] = $i['name'];
467 if(count($plugins_arr)) {
468 foreach($plugins_arr as $p) {
469 if(! in_array($p,$installed_arr)) {
470 logger("Addons: installing " . $p);
471 @include_once('addon/' . $p . '/' . $p . '.php');
472 if(function_exists($p . '_install')) {
473 $func = $p . '_install';
475 $r = q("INSERT INTO `addon` (`name`, `installed`) VALUES ( '%s', 1 ) ",
487 // This is our template processor.
488 // $s is the string requiring macro substitution.
489 // $r is an array of key value pairs (search => replace)
490 // returns substituted string.
491 // WARNING: this is pretty basic, and doesn't properly handle search strings that are substrings of each other.
492 // For instance if 'test' => "foo" and 'testing' => "bar", testing could become either bar or fooing,
493 // depending on the order in which they were declared in the array.
495 if(! function_exists('replace_macros')) {
496 function replace_macros($s,$r) {
501 if(is_array($r) && count($r)) {
502 foreach ($r as $k => $v ) {
507 return str_replace($search,$replace,$s);
511 // load string translation table for alternate language
513 if(! function_exists('load_translation_table')) {
514 function load_translation_table($lang) {
517 if(file_exists("view/$lang/strings.php"))
518 include("view/$lang/strings.php");
521 // translate string if translation exists
523 if(! function_exists('t')) {
528 if(x($a->strings,$s))
529 return $a->strings[$s];
533 // curl wrapper. If binary flag is true, return binary
536 if(! function_exists('fetch_url')) {
537 function fetch_url($url,$binary = false, &$redirects = 0) {
541 $ch = curl_init($url);
542 if(($redirects > 8) || (! $ch))
545 curl_setopt($ch, CURLOPT_HEADER, true);
546 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
549 $curl_time = intval(get_config('system','curl_timeout'));
550 curl_setopt($ch, CURLOPT_TIMEOUT, (($curl_time !== false) ? $curl_time : 60));
552 // by default we will allow self-signed certs
553 // but you can override this
555 $check_cert = get_config('system','verifyssl');
556 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (($check_cert) ? true : false));
558 $prx = get_config('system','proxy');
560 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
561 curl_setopt($ch, CURLOPT_PROXY, $prx);
562 $prxusr = get_config('system','proxyuser');
564 curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
567 curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
569 $a->set_curl_code(0);
571 // don't let curl abort the entire application
572 // if it throws any errors.
574 $s = @curl_exec($ch);
576 $http_code = intval(curl_getinfo($ch, CURLINFO_HTTP_CODE));
577 $header = substr($s,0,strpos($s,"\r\n\r\n"));
578 if(stristr($header,'100') && (strlen($header) < 30)) {
579 // 100 Continue has two headers, get the real one
580 $s = substr($s,strlen($header)+4);
581 $header = substr($s,0,strpos($s,"\r\n\r\n"));
583 if($http_code == 301 || $http_code == 302 || $http_code == 303) {
585 preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
586 $url = trim(array_pop($matches));
587 $url_parsed = parse_url($url);
588 if (isset($url_parsed)) {
590 return fetch_url($url,$binary,$redirects);
593 $a->set_curl_code($http_code);
594 $body = substr($s,strlen($header)+4);
595 $a->set_curl_headers($header);
601 // post request to $url. $params is an array of post variables.
603 if(! function_exists('post_url')) {
604 function post_url($url,$params, $headers = null, &$redirects = 0) {
606 $ch = curl_init($url);
607 if(($redirects > 8) || (! $ch))
610 curl_setopt($ch, CURLOPT_HEADER, true);
611 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
612 curl_setopt($ch, CURLOPT_POST,1);
613 curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
615 $curl_time = intval(get_config('system','curl_timeout'));
616 curl_setopt($ch, CURLOPT_TIMEOUT, (($curl_time !== false) ? $curl_time : 60));
618 if(is_array($headers))
619 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
621 $check_cert = get_config('system','verifyssl');
622 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (($check_cert) ? true : false));
623 $prx = get_config('system','proxy');
625 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
626 curl_setopt($ch, CURLOPT_PROXY, $prx);
627 $prxusr = get_config('system','proxyuser');
629 curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
632 $a->set_curl_code(0);
634 // don't let curl abort the entire application
635 // if it throws any errors.
637 $s = @curl_exec($ch);
639 $http_code = intval(curl_getinfo($ch, CURLINFO_HTTP_CODE));
640 $header = substr($s,0,strpos($s,"\r\n\r\n"));
641 if(stristr($header,'100') && (strlen($header) < 30)) {
642 // 100 Continue has two headers, get the real one
643 $s = substr($s,strlen($header)+4);
644 $header = substr($s,0,strpos($s,"\r\n\r\n"));
646 if($http_code == 301 || $http_code == 302 || $http_code == 303) {
648 preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
649 $url = trim(array_pop($matches));
650 $url_parsed = parse_url($url);
651 if (isset($url_parsed)) {
653 return post_url($url,$binary,$headers,$redirects);
656 $a->set_curl_code($http_code);
657 $body = substr($s,strlen($header)+4);
658 $a->set_curl_headers($header);
664 // random hash, 64 chars
666 if(! function_exists('random_string')) {
667 function random_string() {
668 return(hash('sha256',uniqid(rand(),true)));
672 * This is our primary input filter.
674 * The high bit hack only involved some old IE browser, forget which (IE5/Mac?)
675 * that had an XSS attack vector due to stripping the high-bit on an 8-bit character
676 * after cleansing, and angle chars with the high bit set could get through as markup.
678 * This is now disabled because it was interfering with some legitimate unicode sequences
679 * and hopefully there aren't a lot of those browsers left.
681 * Use this on any text input where angle chars are not valid or permitted
682 * They will be replaced with safer brackets. This may be filtered further
683 * if these are not allowed either.
687 if(! function_exists('notags')) {
688 function notags($string) {
690 return(str_replace(array("<",">"), array('[',']'), $string));
692 // High-bit filter no longer used
693 // return(str_replace(array("<",">","\xBA","\xBC","\xBE"), array('[',']','','',''), $string));
696 // use this on "body" or "content" input where angle chars shouldn't be removed,
697 // and allow them to be safely displayed.
699 if(! function_exists('escape_tags')) {
700 function escape_tags($string) {
702 return(htmlspecialchars($string));
705 // wrapper for adding a login box. If $register == true provide a registration
706 // link. This will most always depend on the value of $a->config['register_policy'].
707 // returns the complete html for inserting into the page
709 if(! function_exists('login')) {
710 function login($register = false) {
712 $register_html = (($register) ? load_view_file("view/register-link.tpl") : "");
714 $noid = get_config('system','no_openid');
716 $classname = 'no-openid';
717 $namelabel = t('Nickname or Email address: ');
718 $passlabel = t('Password: ');
722 $classname = 'openid';
723 $namelabel = t('Nickname/Email/OpenID: ');
724 $passlabel = t("Password \x28if not OpenID\x29: ");
727 $lostpass = t('Forgot your password?');
728 $lostlink = t('Password Reset');
730 if(x($_SESSION,'authenticated')) {
731 $tpl = load_view_file("view/logout.tpl");
734 $tpl = load_view_file("view/login.tpl");
738 $o = replace_macros($tpl,array(
739 '$register_html' => $register_html,
740 '$classname' => $classname,
741 '$namelabel' => $namelabel,
742 '$passlabel' => $passlabel,
744 '$lostpass' => $lostpass,
745 '$lostlink' => $lostlink
751 // generate a string that's random, but usually pronounceable.
752 // used to generate initial passwords
754 if(! function_exists('autoname')) {
755 function autoname($len) {
757 $vowels = array('a','a','ai','au','e','e','e','ee','ea','i','ie','o','ou','u');
758 if(mt_rand(0,5) == 4)
776 's','sc','sh','sm','sp','st',
784 $midcons = array('ck','ct','gn','ld','lf','lm','lt','mb','mm', 'mn','mp',
785 'nd','ng','nk','nt','rn','rp','rt');
787 $noend = array('bl', 'br', 'cl','cr','dr','fl','fr','gl','gr',
788 'kh', 'kl','kr','mn','pl','pr','rh','tr','qu','wh');
790 $start = mt_rand(0,2);
798 for ($x = 0; $x < $len; $x ++) {
799 $r = mt_rand(0,count($table) - 1);
802 if($table == $vowels)
803 $table = array_merge($cons,$midcons);
809 $word = substr($word,0,$len);
811 foreach($noend as $noe) {
812 if((strlen($word) > 2) && (substr($word,-2) == $noe)) {
813 $word = substr($word,0,-1);
817 if(substr($word,-1) == 'q')
818 $word = substr($word,0,-1);
822 // Used to end the current process, after saving session state.
824 if(! function_exists('killme')) {
826 session_write_close();
830 // redirect to another URL and terminate this process.
832 if(! function_exists('goaway')) {
833 function goaway($s) {
834 header("Location: $s");
838 // Generic XML return
839 // Outputs a basic dfrn XML status structure to STDOUT, with a <status> variable
840 // of $st and an optional text <message> of $message and terminates the current process.
842 if(! function_exists('xml_status')) {
843 function xml_status($st, $message = '') {
845 $xml_message = ((strlen($message)) ? "\t<message>" . xmlify($message) . "</message>\r\n" : '');
848 logger('xml_status returning non_zero: ' . $st . " message=" . $message);
850 header( "Content-type: text/xml" );
851 echo '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
852 echo "<result>\r\n\t<status>$st</status>\r\n$xml_message</result>\r\n";
856 // Returns the uid of locally logged in user or false.
858 if(! function_exists('local_user')) {
859 function local_user() {
860 if((x($_SESSION,'authenticated')) && (x($_SESSION,'uid')))
861 return intval($_SESSION['uid']);
865 // Returns contact id of authenticated site visitor or false
867 if(! function_exists('remote_user')) {
868 function remote_user() {
869 if((x($_SESSION,'authenticated')) && (x($_SESSION,'visitor_id')))
870 return intval($_SESSION['visitor_id']);
874 // contents of $s are displayed prominently on the page the next time
875 // a page is loaded. Usually used for errors or alerts.
877 if(! function_exists('notice')) {
878 function notice($s) {
881 $_SESSION['sysmsg'] .= $s;
884 // wrapper around config to limit the text length of an incoming message
886 if(! function_exists('get_max_import_size')) {
887 function get_max_import_size() {
889 return ((x($a->config,'max_import_size')) ? $a->config['max_import_size'] : 0 );
893 // escape text ($str) for XML transport
894 // returns escaped text.
896 if(! function_exists('xmlify')) {
897 function xmlify($str) {
900 for($x = 0; $x < strlen($str); $x ++) {
931 $buffer = trim($buffer);
936 // pass xml escaped text ($s), returns unescaped text
938 if(! function_exists('unxmlify')) {
939 function unxmlify($s) {
940 $ret = str_replace('&','&', $s);
941 $ret = str_replace(array('<','>','"','''),array('<','>','"',"'"),$ret);
945 // convenience wrapper, reverse the operation "bin2hex"
947 if(! function_exists('hex2bin')) {
948 function hex2bin($s) {
949 return(pack("H*",$s));
952 // Automatic pagination.
953 // To use, get the count of total items.
954 // Then call $a->set_pager_total($number_items);
955 // Optionally call $a->set_pager_itemspage($n) to the number of items to display on each page
956 // Then call paginate($a) after the end of the display loop to insert the pager block on the page
957 // (assuming there are enough items to paginate).
958 // When using with SQL, the setting LIMIT %d, %d => $a->pager['start'],$a->pager['itemspage']
959 // will limit the results to the correct items for the current page.
960 // The actual page handling is then accomplished at the application layer.
962 if(! function_exists('paginate')) {
963 function paginate(&$a) {
965 $stripped = preg_replace('/(&page=[0-9]*)/','',$a->query_string);
966 $stripped = str_replace('q=','',$stripped);
967 $stripped = trim($stripped,'/');
968 $url = $a->get_baseurl() . '/' . $stripped;
971 if($a->pager['total'] > $a->pager['itemspage']) {
972 $o .= '<div class="pager">';
973 if($a->pager['page'] != 1)
974 $o .= '<span class="pager_prev">'."<a href=\"$url".'&page='.($a->pager['page'] - 1).'">' . t('prev') . '</a></span> ';
976 $o .= "<span class=\"pager_first\"><a href=\"$url"."&page=1\">" . t('first') . "</a></span> ";
978 $numpages = $a->pager['total'] / $a->pager['itemspage'];
981 $numstop = $numpages;
984 $numstart = (($pagenum > 7) ? ($pagenum - 7) : 1);
985 $numstop = (($pagenum > ($numpages - 7)) ? $numpages : ($numstart + 14));
988 for($i = $numstart; $i <= $numstop; $i++){
989 if($i == $a->pager['page'])
990 $o .= '<span class="pager_current">'.(($i < 10) ? ' '.$i : $i);
992 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? ' '.$i : $i)."</a>";
996 if(($a->pager['total'] % $a->pager['itemspage']) != 0) {
997 if($i == $a->pager['page'])
998 $o .= '<span class="pager_current">'.(($i < 10) ? ' '.$i : $i);
1000 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? ' '.$i : $i)."</a>";
1004 $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
1005 $o .= "<span class=\"pager_last\"><a href=\"$url"."&page=$lastpage\">" . t('last') . "</a></span> ";
1007 if(($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0)
1008 $o .= '<span class="pager_next">'."<a href=\"$url"."&page=".($a->pager['page'] + 1).'">' . t('next') . '</a></span>';
1009 $o .= '</div>'."\r\n";
1014 // Turn user/group ACLs stored as angle bracketed text into arrays
1016 if(! function_exists('expand_acl')) {
1017 function expand_acl($s) {
1018 // turn string array of angle-bracketed elements into numeric array
1019 // e.g. "<1><2><3>" => array(1,2,3);
1023 $t = str_replace('<','',$s);
1024 $a = explode('>',$t);
1025 foreach($a as $aa) {
1027 $ret[] = intval($aa);
1033 // Used to wrap ACL elements in angle brackets for storage
1035 if(! function_exists('sanitise_acl')) {
1036 function sanitise_acl(&$item) {
1038 $item = '<' . intval(notags(trim($item))) . '>';
1043 // retrieve a "family" of config variables from database to cached storage
1045 if(! function_exists('load_config')) {
1046 function load_config($family) {
1048 $r = q("SELECT * FROM `config` WHERE `cat` = '%s'",
1052 foreach($r as $rr) {
1054 $a->config[$family][$k] = $rr['v'];
1059 // get a particular config variable given the family name
1060 // and key. Returns false if not set.
1061 // $instore is only used by the set_config function
1062 // to determine if the key already exists in the DB
1063 // If a key is found in the DB but doesn't exist in
1064 // local config cache, pull it into the cache so we don't have
1065 // to hit the DB again for this item.
1067 if(! function_exists('get_config')) {
1068 function get_config($family, $key, $instore = false) {
1073 if(isset($a->config[$family][$key])) {
1074 if($a->config[$family][$key] === '!<unset>!') {
1077 return $a->config[$family][$key];
1080 $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
1085 $a->config[$family][$key] = $ret[0]['v'];
1086 return $ret[0]['v'];
1089 $a->config[$family][$key] = '!<unset>!';
1094 // Store a config value ($value) in the category ($family)
1095 // under the key ($key)
1096 // Return the value, or false if the database update failed
1098 if(! function_exists('set_config')) {
1099 function set_config($family,$key,$value) {
1103 if(get_config($family,$key,true) === false) {
1104 $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
1113 $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
1119 $a->config[$family][$key] = $value;
1127 if(! function_exists('load_pconfig')) {
1128 function load_pconfig($uid,$family) {
1130 $r = q("SELECT * FROM `pconfig` WHERE `cat` = '%s' AND `uid` = %d",
1135 foreach($r as $rr) {
1137 $a->config[$uid][$family][$k] = $rr['v'];
1144 if(! function_exists('get_pconfig')) {
1145 function get_pconfig($uid,$family, $key, $instore = false) {
1150 if(isset($a->config[$uid][$family][$key])) {
1151 if($a->config[$uid][$family][$key] === '!<unset>!') {
1154 return $a->config[$uid][$family][$key];
1158 $ret = q("SELECT `v` FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
1165 $a->config[$uid][$family][$key] = $ret[0]['v'];
1166 return $ret[0]['v'];
1169 $a->config[$uid][$family][$key] = '!<unset>!';
1174 if(! function_exists('del_config')) {
1175 function del_config($family,$key) {
1178 if(x($a->config[$family],$key))
1179 unset($a->config[$family][$key]);
1180 $ret = q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
1189 // Same as above functions except these are for personal config storage and take an
1190 // additional $uid argument.
1192 if(! function_exists('set_pconfig')) {
1193 function set_pconfig($uid,$family,$key,$value) {
1197 if(get_pconfig($uid,$family,$key,true) === false) {
1198 $ret = q("INSERT INTO `pconfig` ( `uid`, `cat`, `k`, `v` ) VALUES ( %d, '%s', '%s', '%s' ) ",
1208 $ret = q("UPDATE `pconfig` SET `v` = '%s' WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
1215 $a->config[$uid][$family][$key] = $value;
1222 if(! function_exists('del_pconfig')) {
1223 function del_pconfig($uid,$family,$key) {
1226 if(x($a->config[$uid][$family],$key))
1227 unset($a->config[$uid][$family][$key]);
1228 $ret = q("DELETE FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
1237 // convert an XML document to a normalised, case-corrected array
1238 // used by webfinger
1240 if(! function_exists('convert_xml_element_to_array')) {
1241 function convert_xml_element_to_array($xml_element, &$recursion_depth=0) {
1243 // If we're getting too deep, bail out
1244 if ($recursion_depth > 512) {
1248 if (!is_string($xml_element) &&
1249 !is_array($xml_element) &&
1250 (get_class($xml_element) == 'SimpleXMLElement')) {
1251 $xml_element_copy = $xml_element;
1252 $xml_element = get_object_vars($xml_element);
1255 if (is_array($xml_element)) {
1256 $result_array = array();
1257 if (count($xml_element) <= 0) {
1258 return (trim(strval($xml_element_copy)));
1261 foreach($xml_element as $key=>$value) {
1264 $result_array[strtolower($key)] =
1265 convert_xml_element_to_array($value, $recursion_depth);
1268 if ($recursion_depth == 0) {
1269 $temp_array = $result_array;
1270 $result_array = array(
1271 strtolower($xml_element_copy->getName()) => $temp_array,
1275 return ($result_array);
1278 return (trim(strval($xml_element)));
1282 // Given an email style address, perform webfinger lookup and
1283 // return the resulting DFRN profile URL, or if no DFRN profile URL
1284 // is located, returns an OStatus subscription template (prefixed
1285 // with the string 'stat:' to identify it as on OStatus template).
1286 // If this isn't an email style address just return $s.
1287 // Return an empty string if email-style addresses but webfinger fails,
1288 // or if the resultant personal XRD doesn't contain a supported
1289 // subscription/friend-request attribute.
1291 if(! function_exists('webfinger_dfrn')) {
1292 function webfinger_dfrn($s) {
1293 if(! strstr($s,'@')) {
1296 $links = webfinger($s);
1297 logger('webfinger_dfrn: ' . $s . ':' . print_r($links,true), LOGGER_DATA);
1299 foreach($links as $link)
1300 if($link['@attributes']['rel'] === NAMESPACE_DFRN)
1301 return $link['@attributes']['href'];
1302 foreach($links as $link)
1303 if($link['@attributes']['rel'] === NAMESPACE_OSTATUSSUB)
1304 return 'stat:' . $link['@attributes']['template'];
1309 // Given an email style address, perform webfinger lookup and
1310 // return the array of link attributes from the personal XRD file.
1311 // On error/failure return an empty array.
1314 if(! function_exists('webfinger')) {
1315 function webfinger($s) {
1317 if(strstr($s,'@')) {
1318 $host = substr($s,strpos($s,'@') + 1);
1321 $tpl = fetch_lrdd_template($host);
1322 logger('webfinger: lrdd template: ' . $tpl);
1324 $pxrd = str_replace('{uri}', urlencode('acct:'.$s), $tpl);
1325 $links = fetch_xrd_links($pxrd);
1326 if(! count($links)) {
1327 // try with double slashes
1328 $pxrd = str_replace('{uri}', urlencode('acct://'.$s), $tpl);
1329 $links = fetch_xrd_links($pxrd);
1337 if(! function_exists('lrdd')) {
1338 function lrdd($uri) {
1342 if(strstr($uri,'@')) {
1343 return(webfinger($uri));
1346 $html = fetch_url($uri);
1347 $headers = $a->get_curl_headers();
1348 $lines = explode("\n",$headers);
1350 foreach($lines as $line) {
1351 // TODO alter the following regex to support multiple relations (space separated)
1352 if((stristr($line,'link:')) && preg_match('/<([^>].*)>.*rel\=[\'\"]lrdd[\'\"]/',$line,$matches)) {
1353 $link = $matches[1];
1358 if(! isset($link)) {
1359 // parse the page of the supplied URL looking for rel links
1361 require_once('library/HTML5/Parser.php');
1362 $dom = HTML5_Parser::parse($html);
1365 $items = $dom->getElementsByTagName('link');
1367 foreach($items as $item) {
1368 $x = $item->getAttribute('rel');
1370 $link = $item->getAttribute('href');
1378 return(fetch_xrd_links($link));
1385 // Given a host name, locate the LRDD template from that
1386 // host. Returns the LRDD template or an empty string on
1389 if(! function_exists('fetch_lrdd_template')) {
1390 function fetch_lrdd_template($host) {
1392 $url = 'http://' . $host . '/.well-known/host-meta' ;
1393 $links = fetch_xrd_links($url);
1395 foreach($links as $link)
1396 if($link['@attributes']['rel'] && $link['@attributes']['rel'] === 'lrdd')
1397 $tpl = $link['@attributes']['template'];
1399 if(! strpos($tpl,'{uri}'))
1404 // Given a URL, retrieve the page as an XRD document.
1405 // Return an array of links.
1406 // on error/failure return empty array.
1408 if(! function_exists('fetch_xrd_links')) {
1409 function fetch_xrd_links($url) {
1412 $xml = fetch_url($url);
1416 logger('fetch_xrd_links: ' . $xml, LOGGER_DATA);
1417 $h = simplexml_load_string($xml);
1418 $arr = convert_xml_element_to_array($h);
1420 if(isset($arr['xrd']['link'])) {
1421 $link = $arr['xrd']['link'];
1422 if(! isset($link[0]))
1423 $links = array($link);
1431 // Convert an ACL array to a storable string
1433 if(! function_exists('perms2str')) {
1434 function perms2str($p) {
1437 if(is_array($tmp)) {
1438 array_walk($tmp,'sanitise_acl');
1439 $ret = implode('',$tmp);
1444 // generate a guaranteed unique (for this domain) item ID for ATOM
1445 // safe from birthday paradox
1447 if(! function_exists('item_new_uri')) {
1448 function item_new_uri($hostname,$uid) {
1452 $hash = random_string();
1454 $uri = "urn:X-dfrn:" . $hostname . ':' . $uid . ':' . $hash;
1456 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
1460 } while($dups == true);
1464 // Generate a guaranteed unique photo ID.
1465 // safe from birthday paradox
1467 if(! function_exists('photo_new_resource')) {
1468 function photo_new_resource() {
1472 $resource = hash('md5',uniqid(mt_rand(),true));
1473 $r = q("SELECT `id` FROM `photo` WHERE `resource-id` = '%s' LIMIT 1",
1478 } while($found == true);
1483 // Take a URL from the wild, prepend http:// if necessary
1484 // and check DNS to see if it's real
1485 // return true if it's OK, false if something is wrong with it
1487 if(! function_exists('validate_url')) {
1488 function validate_url(&$url) {
1489 if(substr($url,0,4) != 'http')
1490 $url = 'http://' . $url;
1491 $h = parse_url($url);
1493 if(($h) && (dns_get_record($h['host'], DNS_A + DNS_CNAME + DNS_PTR))) {
1499 // checks that email is an actual resolvable internet address
1501 if(! function_exists('validate_email')) {
1502 function validate_email($addr) {
1504 if(! strpos($addr,'@'))
1506 $h = substr($addr,strpos($addr,'@') + 1);
1508 if(($h) && (dns_get_record($h, DNS_A + DNS_CNAME + DNS_PTR + DNS_MX))) {
1514 // Check $url against our list of allowed sites,
1515 // wildcards allowed. If allowed_sites is unset return true;
1516 // If url is allowed, return true.
1517 // otherwise, return false
1519 if(! function_exists('allowed_url')) {
1520 function allowed_url($url) {
1522 $h = parse_url($url);
1528 $str_allowed = get_config('system','allowed_sites');
1534 $host = strtolower($h['host']);
1536 // always allow our own site
1538 if($host == strtolower($_SERVER['SERVER_NAME']))
1541 $fnmatch = function_exists('fnmatch');
1542 $allowed = explode(',',$str_allowed);
1544 if(count($allowed)) {
1545 foreach($allowed as $a) {
1546 $pat = strtolower(trim($a));
1547 if(($fnmatch && fnmatch($pat,$host)) || ($pat == $host)) {
1556 // check if email address is allowed to register here.
1557 // Compare against our list (wildcards allowed).
1558 // Returns false if not allowed, true if allowed or if
1559 // allowed list is not configured.
1561 if(! function_exists('allowed_email')) {
1562 function allowed_email($email) {
1565 $domain = strtolower(substr($email,strpos($email,'@') + 1));
1569 $str_allowed = get_config('system','allowed_email');
1575 $fnmatch = function_exists('fnmatch');
1576 $allowed = explode(',',$str_allowed);
1578 if(count($allowed)) {
1579 foreach($allowed as $a) {
1580 $pat = strtolower(trim($a));
1581 if(($fnmatch && fnmatch($pat,$host)) || ($pat == $host)) {
1590 // Format the like/dislike text for a profile item
1591 // $cnt = number of people who like/dislike the item
1592 // $arr = array of pre-linked names of likers/dislikers
1593 // $type = one of 'like, 'dislike'
1595 // returns formatted text
1597 if(! function_exists('format_like')) {
1598 function format_like($cnt,$arr,$type,$id) {
1601 $o .= $arr[0] . (($type === 'like') ? t(' likes this.') : t(' doesn\'t like this.')) . EOL ;
1603 $o .= '<span class="fakelink" onclick="openClose(\'' . $type . 'list-' . $id . '\');" >'
1604 . $cnt . ' ' . t('people') . '</span> ' . (($type === 'like') ? t('like this.') : t('don\'t like this.')) . EOL ;
1605 $total = count($arr);
1606 if($total >= MAX_LIKERS)
1607 $arr = array_slice($arr, 0, MAX_LIKERS - 1);
1608 if($total < MAX_LIKERS)
1609 $arr[count($arr)-1] = t('and') . ' ' . $arr[count($arr)-1];
1610 $str = implode(', ', $arr);
1611 if($total >= MAX_LIKERS)
1612 $str .= t(', and ') . $total - MAX_LIKERS . t(' other people');
1613 $str .= (($type === 'like') ? t(' like this.') : t(' don\'t like this.'));
1614 $o .= "\t" . '<div id="' . $type . 'list-' . $id . '" style="display: none;" >' . $str . '</div>';
1620 // wrapper to load a view template, checking for alternate
1621 // languages before falling back to the default
1623 if(! function_exists('load_view_file')) {
1624 function load_view_file($s) {
1627 $lang = get_config('system','language');
1630 if(file_exists("$d/$lang/$b"))
1631 return file_get_contents("$d/$lang/$b");
1632 return file_get_contents($s);
1635 // for html,xml parsing - let's say you've got
1636 // an attribute foobar="class1 class2 class3"
1637 // and you want to find out if it contains 'class3'.
1638 // you can't use a normal sub string search because you
1639 // might match 'notclass3' and a regex to do the job is
1640 // possible but a bit complicated.
1641 // pass the attribute string as $attr and the attribute you
1642 // are looking for as $s - returns true if found, otherwise false
1644 if(! function_exists('attribute_contains')) {
1645 function attribute_contains($attr,$s) {
1646 $a = explode(' ', $attr);
1647 if(count($a) && in_array($s,$a))
1652 if(! function_exists('logger')) {
1653 function logger($msg,$level = 0) {
1655 $debugging = get_config('system','debugging');
1656 $loglevel = intval(get_config('system','loglevel'));
1657 $logfile = get_config('system','logfile');
1659 if((! $debugging) || (! $logfile) || ($level > $loglevel))
1662 @file_put_contents($logfile, datetime_convert() . ':' . session_id() . ' ' . $msg . "\n", FILE_APPEND);
1667 if(! function_exists('activity_match')) {
1668 function activity_match($haystack,$needle) {
1669 if(($haystack === $needle) || ((basename($needle) === $haystack) && strstr($needle,NAMESPACE_ACTIVITY_SCHEMA)))
1675 // Pull out all #hashtags and @person tags from $s;
1676 // We also get @person@domain.com - which would make
1677 // the regex quite complicated as tags can also
1678 // end a sentence. So we'll run through our results
1679 // and strip the period from any tags which end with one.
1680 // Returns array of tags found, or empty array.
1683 if(! function_exists('get_tags')) {
1684 function get_tags($s) {
1686 if(preg_match_all('/([@#][^ \x0D\x0A,:?]*)([ \x0D\x0A,:?]|$)/',$s,$match)) {
1687 foreach($match[1] as $match) {
1688 if(strstr($match,"]")) {
1689 // we might be inside a bbcode color tag - leave it alone
1692 if(substr($match,-1,1) === '.')
1693 $ret[] = substr($match,0,-1);
1703 // quick and dirty quoted_printable encoding
1705 if(! function_exists('qp')) {
1707 return str_replace ("%","=",rawurlencode($s));
1711 if(! function_exists('like_puller')) {
1712 function like_puller($a,$item,&$arr,$mode) {
1716 $verb = (($mode === 'like') ? ACTIVITY_LIKE : ACTIVITY_DISLIKE);
1718 if((activity_match($item['verb'],$verb)) && ($item['id'] != $item['parent'])) {
1719 $url = $item['author-link'];
1720 if(($item['network'] === 'dfrn') && (! $item['self']) && ($item['author-link'] == $item['url'])) {
1721 $url = $a->get_baseurl() . '/redir/' . $item['contact-id'];
1722 $sparkle = ' class="sparkle" ';
1724 if(! ((isset($arr[$item['parent'] . '-l'])) && (is_array($arr[$item['parent'] . '-l']))))
1725 $arr[$item['parent'] . '-l'] = array();
1726 if(! isset($arr[$item['parent']]))
1727 $arr[$item['parent']] = 1;
1729 $arr[$item['parent']] ++;
1730 $arr[$item['parent'] . '-l'][] = '<a href="'. $url . '"'. $sparkle .'>' . $item['author-name'] . '</a>';
1735 if(! function_exists('get_mentions')) {
1736 function get_mentions($item) {
1738 if(! strlen($item['tag']))
1741 $arr = explode(',',$item['tag']);
1742 foreach($arr as $x) {
1744 if(preg_match('/@\[url=([^\]]*)\]/',$x,$matches))
1745 $o .= "\t\t" . '<link rel="mentioned" href="' . $matches[1] . '" />' . "\r\n";
1750 if(! function_exists('contact_block')) {
1751 function contact_block() {
1755 $shown = get_pconfig($a->profile['uid'],'system','display_friend_count');
1759 if((! is_array($a->profile)) || ($a->profile['hide-friends']))
1761 $r = q("SELECT COUNT(*) AS `total` FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 and `pending` = 0",
1762 intval($a->profile['uid'])
1765 $total = intval($r[0]['total']);
1768 $o .= '<h4 class="contact-h4">' . t('No contacts') . '</h4>';
1771 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 and `pending` = 0 ORDER BY RAND() LIMIT %d",
1772 intval($a->profile['uid']),
1776 $o .= '<h4 class="contact-h4">' . $total . ' ' . t('Contacts') . '</h4><div id="contact-block">';
1777 foreach($r as $rr) {
1778 $redirect_url = $a->get_baseurl() . '/redir/' . $rr['id'];
1779 if(local_user() && ($rr['uid'] == local_user())
1780 && ($rr['network'] === 'dfrn')) {
1781 $url = $redirect_url;
1782 $sparkle = ' sparkle';
1789 $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";
1791 $o .= '</div><div id="contact-block-end"></div>';
1792 $o .= '<div id="viewcontacts"><a id="viewcontacts-link" href="viewcontacts/' . $a->profile['nickname'] . '">' . t('View Contacts') . '</a></div>';
1796 $arr = array('contacts' => $r, 'output' => $o);
1798 call_hooks('contact_block_end', $arr);
1803 if(! function_exists('search')) {
1804 function search($s) {
1806 $o = '<div id="search-box">';
1807 $o .= '<form action="' . $a->get_baseurl() . '/search' . '" method="get" >';
1808 $o .= '<input type="text" name="search" id="search-text" value="' . $s .'" />';
1809 $o .= '<input type="submit" name="submit" id="search-submit" value="' . t('Search') . '" />';
1810 $o .= '</form></div>';
1814 if(! function_exists('valid_email')) {
1815 function valid_email($x){
1816 if(preg_match('/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+$/',$x))
1822 if(! function_exists('gravatar_img')) {
1823 function gravatar_img($email) {
1825 $opt = 'identicon'; // psuedo-random geometric pattern if not found
1827 $hash = md5(trim(strtolower($email)));
1829 $url = 'http://www.gravatar.com/avatar/' . $hash . '.jpg'
1830 . '?s=' . $size . '&d=' . $opt . '&r=' . $rating;
1832 logger('gravatar: ' . $email . ' ' . $url);
1836 if(! function_exists('aes_decrypt')) {
1837 function aes_decrypt($val,$ky)
1839 $key="\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
1840 for($a=0;$a<strlen($ky);$a++)
1841 $key[$a%16]=chr(ord($key[$a%16]) ^ ord($ky[$a]));
1842 $mode = MCRYPT_MODE_ECB;
1843 $enc = MCRYPT_RIJNDAEL_128;
1844 $dec = @mcrypt_decrypt($enc, $key, $val, $mode, @mcrypt_create_iv( @mcrypt_get_iv_size($enc, $mode), MCRYPT_DEV_URANDOM ) );
1845 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));
1849 if(! function_exists('aes_encrypt')) {
1850 function aes_encrypt($val,$ky)
1852 $key="\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
1853 for($a=0;$a<strlen($ky);$a++)
1854 $key[$a%16]=chr(ord($key[$a%16]) ^ ord($ky[$a]));
1855 $mode=MCRYPT_MODE_ECB;
1856 $enc=MCRYPT_RIJNDAEL_128;
1857 $val=str_pad($val, (16*(floor(strlen($val) / 16)+(strlen($val) % 16==0?2:1))), chr(16-(strlen($val) % 16)));
1858 return mcrypt_encrypt($enc, $key, $val, $mode, mcrypt_create_iv( mcrypt_get_iv_size($enc, $mode), MCRYPT_DEV_URANDOM));
1866 * Replace naked text hyperlink with HTML formatted hyperlink
1870 if(! function_exists('linkify')) {
1871 function linkify($s) {
1872 $s = preg_replace("/(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\.\=\_\~\#\'\%]*)/", ' <a href="$1" >$1</a>', $s);
1882 * Replaces text emoticons with graphical images
1884 * @Parameter: string $s
1889 if(! function_exists('smilies')) {
1890 function smilies($s) {
1894 array( ':-)', ';-)', ':-(', ':(', ':-P', ':-"', ':-x', ':-X', ':-D', '8-|', '8-O'),
1896 '<img src="' . $a->get_baseurl() . '/images/smiley-smile.gif" alt=":-)" />',
1897 '<img src="' . $a->get_baseurl() . '/images/smiley-wink.gif" alt=";-)" />',
1898 '<img src="' . $a->get_baseurl() . '/images/smiley-frown.gif" alt=":-(" />',
1899 '<img src="' . $a->get_baseurl() . '/images/smiley-frown.gif" alt=":(" />',
1900 '<img src="' . $a->get_baseurl() . '/images/smiley-tongue-out.gif" alt=":-P" />',
1901 '<img src="' . $a->get_baseurl() . '/images/smiley-kiss.gif" alt=":-\"" />',
1902 '<img src="' . $a->get_baseurl() . '/images/smiley-kiss.gif" alt=":-x" />',
1903 '<img src="' . $a->get_baseurl() . '/images/smiley-kiss.gif" alt=":-X" />',
1904 '<img src="' . $a->get_baseurl() . '/images/smiley-laughing.gif" alt=":-D" />',
1905 '<img src="' . $a->get_baseurl() . '/images/smiley-surprised.gif" alt="8-|" />',
1906 '<img src="' . $a->get_baseurl() . '/images/smiley-surprised.gif" alt="8-O" />'
1913 * Function : profile_load
1915 * @parameter string $nickname
1916 * @parameter int $profile
1918 * Summary: Loads a profile into the page sidebar.
1919 * The function requires a writeable copy of the main App structure, and the nickname
1920 * of a registered local account.
1922 * If the viewer is an authenticated remote viewer, the profile displayed is the
1923 * one that has been configured for his/her viewing in the Contact manager.
1924 * Passing a non-zero profile ID can also allow a preview of a selected profile
1927 * Profile information is placed in the App structure for later retrieval.
1928 * Honours the owner's chosen theme for display.
1932 if(! function_exists('profile_load')) {
1933 function profile_load(&$a, $nickname, $profile = 0) {
1935 $r = q("SELECT `profile-id` FROM `contact` WHERE `id` = %d LIMIT 1",
1936 intval($_SESSION['visitor_id']));
1938 $profile = $r[0]['profile-id'];
1944 $profile_int = intval($profile);
1945 $r = q("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `user`.* FROM `profile`
1946 LEFT JOIN `user` ON `profile`.`uid` = `user`.`uid`
1947 WHERE `user`.`nickname` = '%s' AND `profile`.`id` = %d LIMIT 1",
1949 intval($profile_int)
1953 $r = q("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `user`.* FROM `profile`
1954 LEFT JOIN `user` ON `profile`.`uid` = `user`.`uid`
1955 WHERE `user`.`nickname` = '%s' AND `profile`.`is-default` = 1 LIMIT 1",
1960 if(($r === false) || (! count($r))) {
1961 notice( t('No profile') . EOL );
1966 $a->profile = $r[0];
1969 $a->page['title'] = $a->profile['name'] . " @ " . $a->config['sitename'];
1970 $_SESSION['theme'] = $a->profile['theme'];
1972 if(! (x($a->page,'aside')))
1973 $a->page['aside'] = '';
1975 $a->page['aside'] .= profile_sidebar($a->profile);
1976 $a->page['aside'] .= contact_block();
1984 * Function: profile_sidebar
1986 * Formats a profile for display in the sidebar.
1987 * It is very difficult to templatise the HTML completely
1988 * because of all the conditional logic.
1990 * @parameter: array $profile
1992 * Returns HTML string stuitable for sidebar inclusion
1993 * Exceptions: Returns empty string if passed $profile is wrong type or not populated
1998 if(! function_exists('profile_sidebar')) {
1999 function profile_sidebar($profile) {
2005 if((! is_array($profile)) && (! count($profile)))
2008 $fullname = '<div class="fn">' . $profile['name'] . '</div>';
2012 $photo = '<div id="profile=photo-wrapper"><img class="photo" src="' . $profile['photo'] . '" alt="' . $profile['name'] . '" /></div>';
2014 $connect = (($profile['uid'] != local_user()) ? '<li><a id="dfrn-request-link" href="dfrn_request/' . $profile['nickname'] . '">' . t('Connect') . '</a></li>' : '');
2016 if((x($profile,'address') == 1)
2017 || (x($profile,'locality') == 1)
2018 || (x($profile,'region') == 1)
2019 || (x($profile,'postal-code') == 1)
2020 || (x($profile,'country-name') == 1))
2024 $location .= '<div class="location"><span class="location-label">' . t('Location:') . '</span> <div class="adr">';
2025 $location .= ((x($profile,'address') == 1) ? '<div class="street-address">' . $profile['address'] . '</div>' : '');
2026 $location .= (((x($profile,'locality') == 1) || (x($profile,'region') == 1) || (x($profile,'postal-code') == 1))
2027 ? '<span class="city-state-zip"><span class="locality">' . $profile['locality'] . '</span>'
2028 . ((x($profile['locality']) == 1) ? t(', ') : '')
2029 . '<span class="region">' . $profile['region'] . '</span>'
2030 . ' <span class="postal-code">' . $profile['postal-code'] . '</span></span>' : '');
2031 $location .= ((x($profile,'country-name') == 1) ? ' <span class="country-name">' . $profile['country-name'] . '</span>' : '');
2032 $location .= '</div></div><div class="profile-clear"></div>';
2036 $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>' : '');
2038 $pubkey = ((x($profile,'pubkey') == 1) ? '<div class="key" style="display:none;">' . $profile['pubkey'] . '</div>' : '');
2040 $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>' : '');
2042 $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>' : '');
2044 $tpl = load_view_file('view/profile_vcard.tpl');
2046 $o .= replace_macros($tpl, array(
2047 '$fullname' => $fullname,
2050 '$connect' => $connect,
2051 '$location' => $location,
2052 '$gender' => $gender,
2053 '$pubkey' => $pubkey,
2054 '$marital' => $marital,
2055 '$homepage' => $homepage
2058 call_hooks('profile_sidebar', $o);
2064 if(! function_exists('register_hook')) {
2065 function register_hook($hook,$file,$function) {
2067 $r = q("SELECT * FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s' LIMIT 1",
2075 $r = q("INSERT INTO `hook` (`hook`, `file`, `function`) VALUES ( '%s', '%s', '%s' ) ",
2083 if(! function_exists('unregister_hook')) {
2084 function unregister_hook($hook,$file,$function) {
2086 $r = q("DELETE FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s' LIMIT 1",
2095 if(! function_exists('load_hooks')) {
2096 function load_hooks() {
2098 $r = q("SELECT * FROM `hook` WHERE 1");
2100 foreach($r as $rr) {
2101 $a->hooks[] = array($rr['hook'], $rr['file'], $rr['function']);
2107 if(! function_exists('call_hooks')) {
2108 function call_hooks($name, &$data = null) {
2111 if(count($a->hooks)) {
2112 foreach($a->hooks as $hook) {
2113 if($hook[HOOK_HOOK] === $name) {
2114 @include_once($hook[HOOK_FILE]);
2115 if(function_exists($hook[HOOK_FUNCTION])) {
2116 $func = $hook[HOOK_FUNCTION];
2125 if(! function_exists('day_translate')) {
2126 function day_translate($s) {
2127 $ret = str_replace(array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'),
2128 array( t('Monday'), t('Tuesday'), t('Wednesday'), t('Thursday'), t('Friday'), t('Saturday'), t('Sunday')),
2131 $ret = str_replace(array('January','February','March','April','May','June','July','August','September','October','November','December'),
2132 array( t('January'), t('February'), t('March'), t('April'), t('May'), t('June'), t('July'), t('August'), t('September'), t('October'), t('November'), t('December')),
2138 if(! function_exists('get_birthdays')) {
2139 function get_birthdays() {
2147 $bd_format = get_config('system','birthday_format');
2149 $bd_format = 'g A l F d' ; // 8 AM Friday January 18
2151 $r = q("SELECT `event`.*, `event`.`id` AS `eid`, `contact`.* FROM `event`
2152 LEFT JOIN `contact` ON `contact`.`id` = `event`.`cid`
2153 WHERE `event`.`uid` = %d AND `type` = 'birthday' AND `start` < '%s' AND `finish` > '%s'
2154 ORDER BY `start` DESC ",
2155 intval(local_user()),
2156 dbesc(datetime_convert('UTC','UTC','now + 6 days')),
2157 dbesc(datetime_convert('UTC','UTC','now'))
2160 if($r && count($r)) {
2161 $o .= '<div id="birthday-wrapper"><div id="birthday-title">' . t('Birthdays this week:') . '</div>';
2162 $o .= '<div id="birthday-adjust">' . t("\x28Adjusted for local time\x29") . '</div>';
2163 $o .= '<div id="birthday-title-end"></div>';
2165 foreach($r as $rr) {
2166 $now = strtotime('now');
2167 $today = (((strtotime($rr['start']) < $now) && (strtotime($rr['finish']) > $now)) ? true : false);
2169 $o .= '<div class="birthday-list" id="birthday-' . $rr['eid'] . '"><a class="sparkle" href="'
2170 . $a->get_baseurl() . '/redir/' . $rr['cid'] . '">' . $rr['name'] . '</a> '
2171 . day_translate(datetime_convert('UTC', $a->timezone, $rr['start'], $bd_format)) . (($today) ? ' ' . t('[today]') : '') ;