Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1447 lines
39 KiB

  1. <?php
  2. set_time_limit(0);
  3. define ( 'BUILD_ID', 1016 );
  4. define ( 'DFRN_PROTOCOL_VERSION', '2.0' );
  5. define ( 'EOL', "<br />\r\n" );
  6. define ( 'ATOM_TIME', 'Y-m-d\TH:i:s\Z' );
  7. // log levels
  8. define ( 'LOGGER_NORMAL', 0 );
  9. define ( 'LOGGER_TRACE', 1 );
  10. define ( 'LOGGER_DEBUG', 2 );
  11. define ( 'LOGGER_DATA', 3 );
  12. define ( 'LOGGER_ALL', 4 );
  13. // registration policy
  14. define ( 'REGISTER_CLOSED', 0 );
  15. define ( 'REGISTER_APPROVE', 1 );
  16. define ( 'REGISTER_OPEN', 2 );
  17. // relationship types
  18. define ( 'REL_VIP', 1);
  19. define ( 'REL_FAN', 2);
  20. define ( 'REL_BUD', 3);
  21. // page/profile types
  22. // PAGE_NORMAL is a typical personal profile account
  23. // PAGE_SOAPBOX automatically approves all friend requests as REL_FAN, (readonly)
  24. // PAGE_COMMUNITY automatically approves all friend requests as REL_FAN, but with
  25. // write access to wall and comments (no email and not included in page owner's ACL lists)
  26. // PAGE_FREELOVE automatically approves all friend requests as full friends (REL_BUD).
  27. define ( 'PAGE_NORMAL', 0 );
  28. define ( 'PAGE_SOAPBOX', 1 );
  29. define ( 'PAGE_COMMUNITY', 2 );
  30. define ( 'PAGE_FREELOVE', 3 );
  31. // Maximum number of "people who like (or don't like) this"
  32. // that we will list by name
  33. define ( 'MAX_LIKERS', 75);
  34. // email notification options
  35. define ( 'NOTIFY_INTRO', 0x0001 );
  36. define ( 'NOTIFY_CONFIRM', 0x0002 );
  37. define ( 'NOTIFY_WALL', 0x0004 );
  38. define ( 'NOTIFY_COMMENT', 0x0008 );
  39. define ( 'NOTIFY_MAIL', 0x0010 );
  40. // various namespaces we may need to parse
  41. define ( 'NAMESPACE_DFRN' , 'http://purl.org/macgirvin/dfrn/1.0' );
  42. define ( 'NAMESPACE_THREAD' , 'http://purl.org/syndication/thread/1.0' );
  43. define ( 'NAMESPACE_TOMB' , 'http://purl.org/atompub/tombstones/1.0' );
  44. define ( 'NAMESPACE_ACTIVITY', 'http://activitystrea.ms/spec/1.0/' );
  45. define ( 'NAMESPACE_ACTIVITY_SCHEMA', 'http://activitystrea.ms/schema/1.0/' );
  46. define ( 'NAMESPACE_MEDIA', 'http://purl.org/syndication/atommedia' );
  47. define ( 'NAMESPACE_SALMON_ME', 'http://salmon-protocol.org/ns/magic-env' );
  48. define ( 'NAMESPACE_OSTATUSSUB', 'http://ostatus.org/schema/1.0/subscribe' );
  49. define ( 'NAMESPACE_GEORSS', 'http://www.georss.org/georss' );
  50. define ( 'NAMESPACE_POCO', 'http://portablecontacts.net/spec/1.0' );
  51. define ( 'NAMESPACE_FEED', 'http://schemas.google.com/g/2010#updates-from' );
  52. // activity stream defines
  53. define ( 'ACTIVITY_LIKE', NAMESPACE_ACTIVITY_SCHEMA . 'like' );
  54. define ( 'ACTIVITY_DISLIKE', NAMESPACE_DFRN . '/dislike' );
  55. define ( 'ACTIVITY_OBJ_HEART', NAMESPACE_DFRN . '/heart' );
  56. define ( 'ACTIVITY_FRIEND', NAMESPACE_ACTIVITY_SCHEMA . 'make-friend' );
  57. define ( 'ACTIVITY_FOLLOW', NAMESPACE_ACTIVITY_SCHEMA . 'follow' );
  58. define ( 'ACTIVITY_UNFOLLOW', NAMESPACE_ACTIVITY_SCHEMA . 'unfollow' );
  59. define ( 'ACTIVITY_POST', NAMESPACE_ACTIVITY_SCHEMA . 'post' );
  60. define ( 'ACTIVITY_UPDATE', NAMESPACE_ACTIVITY_SCHEMA . 'update' );
  61. define ( 'ACTIVITY_TAG', NAMESPACE_ACTIVITY_SCHEMA . 'tag' );
  62. define ( 'ACTIVITY_OBJ_COMMENT', NAMESPACE_ACTIVITY_SCHEMA . 'comment' );
  63. define ( 'ACTIVITY_OBJ_NOTE', NAMESPACE_ACTIVITY_SCHEMA . 'note' );
  64. define ( 'ACTIVITY_OBJ_PERSON', NAMESPACE_ACTIVITY_SCHEMA . 'person' );
  65. define ( 'ACTIVITY_OBJ_PHOTO', NAMESPACE_ACTIVITY_SCHEMA . 'photo' );
  66. define ( 'ACTIVITY_OBJ_P_PHOTO', NAMESPACE_ACTIVITY_SCHEMA . 'profile-photo' );
  67. define ( 'ACTIVITY_OBJ_ALBUM', NAMESPACE_ACTIVITY_SCHEMA . 'photo-album' );
  68. // item weight for query ordering
  69. define ( 'GRAVITY_PARENT', 0);
  70. define ( 'GRAVITY_LIKE', 3);
  71. define ( 'GRAVITY_COMMENT', 6);
  72. // Our main application structure for the life of this page
  73. // Primarily deals with the URL that got us here
  74. // and tries to make some sense of it, and
  75. // stores our page contents and config storage
  76. // and anything else that might need to be passed around
  77. // before we spit the page out.
  78. if(! class_exists('App')) {
  79. class App {
  80. public $module_loaded = false;
  81. public $config;
  82. public $page;
  83. public $profile;
  84. public $user;
  85. public $cid;
  86. public $contact;
  87. public $content;
  88. public $data;
  89. public $error = false;
  90. public $cmd;
  91. public $argv;
  92. public $argc;
  93. public $module;
  94. public $pager;
  95. public $strings;
  96. public $path;
  97. public $interactive = true;
  98. private $scheme;
  99. private $hostname;
  100. private $baseurl;
  101. private $db;
  102. private $curl_code;
  103. private $curl_headers;
  104. function __construct() {
  105. $this->config = array();
  106. $this->page = array();
  107. $this->pager= array();
  108. $this->scheme = ((isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'])) ? 'https' : 'http' );
  109. if(x($_SERVER,'SERVER_NAME'))
  110. $this->hostname = $_SERVER['SERVER_NAME'];
  111. set_include_path("include/$this->hostname" . PATH_SEPARATOR . 'include' . PATH_SEPARATOR . '.' );
  112. if((x($_SERVER,'QUERY_STRING')) && substr($_SERVER['QUERY_STRING'],0,2) == "q=")
  113. $_SERVER['QUERY_STRING'] = substr($_SERVER['QUERY_STRING'],2);
  114. if(x($_GET,'q'))
  115. $this->cmd = trim($_GET['q'],'/');
  116. $this->argv = explode('/',$this->cmd);
  117. $this->argc = count($this->argv);
  118. if((array_key_exists('0',$this->argv)) && strlen($this->argv[0])) {
  119. $this->module = $this->argv[0];
  120. }
  121. else {
  122. $this->module = 'home';
  123. }
  124. if($this->cmd === '.well-known/host-meta')
  125. require_once('include/hostxrd.php');
  126. $this->pager['page'] = ((x($_GET,'page')) ? $_GET['page'] : 1);
  127. $this->pager['itemspage'] = 50;
  128. $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
  129. $this->pager['total'] = 0;
  130. }
  131. function get_baseurl($ssl = false) {
  132. if(strlen($this->baseurl))
  133. return $this->baseurl;
  134. $this->baseurl = (($ssl) ? 'https' : $this->scheme) . "://" . $this->hostname . ((isset($this->path) && strlen($this->path)) ? '/' . $this->path : '' );
  135. return $this->baseurl;
  136. }
  137. function set_baseurl($url) {
  138. $this->baseurl = $url;
  139. $this->hostname = basename($url);
  140. }
  141. function get_hostname() {
  142. return $this->hostname;
  143. }
  144. function set_hostname($h) {
  145. $this->hostname = $h;
  146. }
  147. function set_path($p) {
  148. $this->path = trim(trim($p),'/');
  149. }
  150. function get_path() {
  151. return $this->path;
  152. }
  153. function set_pager_total($n) {
  154. $this->pager['total'] = intval($n);
  155. }
  156. function set_pager_itemspage($n) {
  157. $this->pager['itemspage'] = intval($n);
  158. $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
  159. }
  160. function init_pagehead() {
  161. $tpl = load_view_file("view/head.tpl");
  162. $this->page['htmlhead'] = replace_macros($tpl,array(
  163. '$baseurl' => $this->get_baseurl()
  164. ));
  165. }
  166. function set_curl_code($code) {
  167. $this->curl_code = $code;
  168. }
  169. function get_curl_code() {
  170. return $this->curl_code;
  171. }
  172. function set_curl_headers($headers) {
  173. $this->curl_headers = $headers;
  174. }
  175. function get_curl_headers() {
  176. return $this->curl_headers;
  177. }
  178. }}
  179. // retrieve the App structure
  180. // useful in functions which require it but don't get it passed to them
  181. if(! function_exists('get_app')) {
  182. function get_app() {
  183. global $a;
  184. return $a;
  185. }};
  186. // Multi-purpose function to check variable state.
  187. // Usage: x($var) or $x($array,'key')
  188. // returns false if variable/key is not set
  189. // if variable is set, returns 1 if has 'non-zero' value, otherwise returns 0.
  190. // e.g. x('') or x(0) returns 0;
  191. if(! function_exists('x')) {
  192. function x($s,$k = NULL) {
  193. if($k != NULL) {
  194. if((is_array($s)) && (array_key_exists($k,$s))) {
  195. if($s[$k])
  196. return (int) 1;
  197. return (int) 0;
  198. }
  199. return false;
  200. }
  201. else {
  202. if(isset($s)) {
  203. if($s) {
  204. return (int) 1;
  205. }
  206. return (int) 0;
  207. }
  208. return false;
  209. }
  210. }}
  211. // called from db initialisation if db is dead.
  212. if(! function_exists('system_unavailable')) {
  213. function system_unavailable() {
  214. include('system_unavailable.php');
  215. killme();
  216. }}
  217. // Primarily involved with database upgrade, but also sets the
  218. // base url for use in cmdline programs which don't have
  219. // $_SERVER variables.
  220. if(! function_exists('check_config')) {
  221. function check_config(&$a) {
  222. load_config('system');
  223. $build = get_config('system','build');
  224. if(! x($build))
  225. $build = set_config('system','build',BUILD_ID);
  226. $url = get_config('system','url');
  227. if(! x($url))
  228. $url = set_config('system','url',$a->get_baseurl());
  229. if($build != BUILD_ID) {
  230. $stored = intval($build);
  231. $current = intval(BUILD_ID);
  232. if(($stored < $current) && file_exists('update.php')) {
  233. // We're reporting a different version than what is currently installed.
  234. // Run any existing update scripts to bring the database up to current.
  235. require_once('update.php');
  236. for($x = $stored; $x < $current; $x ++) {
  237. if(function_exists('update_' . $x)) {
  238. $func = 'update_' . $x;
  239. $func($a);
  240. }
  241. }
  242. set_config('system','build', BUILD_ID);
  243. }
  244. }
  245. return;
  246. }}
  247. // This is our template processor.
  248. // $s is the string requiring macro substitution.
  249. // $r is an array of key value pairs (search => replace)
  250. // returns substituted string.
  251. // WARNING: this is pretty basic, and doesn't properly handle search strings that are substrings of each other.
  252. // For instance if 'test' => "foo" and 'testing' => "bar", testing could become either bar or fooing,
  253. // depending on the order in which they were declared in the array.
  254. if(! function_exists('replace_macros')) {
  255. function replace_macros($s,$r) {
  256. $search = array();
  257. $replace = array();
  258. if(is_array($r) && count($r)) {
  259. foreach ($r as $k => $v ) {
  260. $search[] = $k;
  261. $replace[] = $v;
  262. }
  263. }
  264. return str_replace($search,$replace,$s);
  265. }}
  266. // load string translation table for alternate language
  267. if(! function_exists('load_translation_table')) {
  268. function load_translation_table($lang) {
  269. global $a;
  270. if(file_exists("view/$lang/strings.php"))
  271. include("view/$lang/strings.php");
  272. }}
  273. // translate string if translation exists
  274. if(! function_exists('t')) {
  275. function t($s) {
  276. $a = get_app();
  277. if(x($a->strings,$s))
  278. return $a->strings[$s];
  279. return $s;
  280. }}
  281. // curl wrapper. If binary flag is true, return binary
  282. // results.
  283. if(! function_exists('fetch_url')) {
  284. function fetch_url($url,$binary = false, &$redirects = 0) {
  285. $ch = curl_init($url);
  286. if(($redirects > 8) || (! $ch))
  287. return false;
  288. curl_setopt($ch, CURLOPT_HEADER, true);
  289. curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
  290. // by default we will allow self-signed certs
  291. // but you can override this
  292. $check_cert = get_config('system','verifyssl');
  293. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (($check_cert) ? true : false));
  294. $prx = get_config('system','proxy');
  295. if(strlen($prx)) {
  296. curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
  297. curl_setopt($ch, CURLOPT_PROXY, $prx);
  298. $prxusr = get_config('system','proxyuser');
  299. if(strlen($prxusr))
  300. curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
  301. }
  302. if($binary)
  303. curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
  304. $s = curl_exec($ch);
  305. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  306. $header = substr($s,0,strpos($s,"\r\n\r\n"));
  307. if(stristr($header,'100') && (strlen($header) < 30)) {
  308. // 100 Continue has two headers, get the real one
  309. $s = substr($s,strlen($header)+4);
  310. $header = substr($s,0,strpos($s,"\r\n\r\n"));
  311. }
  312. if($http_code == 301 || $http_code == 302 || $http_code == 303) {
  313. $matches = array();
  314. preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
  315. $url = trim(array_pop($matches));
  316. $url_parsed = parse_url($url);
  317. if (isset($url_parsed)) {
  318. $redirects++;
  319. return fetch_url($url,$binary,$redirects);
  320. }
  321. }
  322. $a = get_app();
  323. $a->set_curl_code($http_code);
  324. $body = substr($s,strlen($header)+4);
  325. $a->set_curl_headers($header);
  326. curl_close($ch);
  327. return($body);
  328. }}
  329. // post request to $url. $params is an array of post variables.
  330. if(! function_exists('post_url')) {
  331. function post_url($url,$params, $headers = null, &$redirects = 0) {
  332. $ch = curl_init($url);
  333. if(($redirects > 8) || (! $ch))
  334. return false;
  335. curl_setopt($ch, CURLOPT_HEADER, true);
  336. curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
  337. curl_setopt($ch, CURLOPT_POST,1);
  338. curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
  339. if(is_array($headers))
  340. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  341. $check_cert = get_config('system','verifyssl');
  342. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (($check_cert) ? true : false));
  343. $prx = get_config('system','proxy');
  344. if(strlen($prx)) {
  345. curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
  346. curl_setopt($ch, CURLOPT_PROXY, $prx);
  347. $prxusr = get_config('system','proxyuser');
  348. if(strlen($prxusr))
  349. curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
  350. }
  351. $s = curl_exec($ch);
  352. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  353. $header = substr($s,0,strpos($s,"\r\n\r\n"));
  354. if(stristr($header,'100') && (strlen($header) < 30)) {
  355. // 100 Continue has two headers, get the real one
  356. $s = substr($s,strlen($header)+4);
  357. $header = substr($s,0,strpos($s,"\r\n\r\n"));
  358. }
  359. if($http_code == 301 || $http_code == 302 || $http_code == 303) {
  360. $matches = array();
  361. preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
  362. $url = trim(array_pop($matches));
  363. $url_parsed = parse_url($url);
  364. if (isset($url_parsed)) {
  365. $redirects++;
  366. return post_url($url,$binary,$headers,$redirects);
  367. }
  368. }
  369. $a = get_app();
  370. $a->set_curl_code($http_code);
  371. $body = substr($s,strlen($header)+4);
  372. $a->set_curl_headers($header);
  373. curl_close($ch);
  374. return($body);
  375. }}
  376. // random hash, 64 chars
  377. if(! function_exists('random_string')) {
  378. function random_string() {
  379. return(hash('sha256',uniqid(rand(),true)));
  380. }}
  381. // This is our primary input filter. The high bit hack only involved some old
  382. // IE browser, forget which.
  383. // Use this on any text input where angle chars are not valid or permitted
  384. // They will be replaced with safer brackets. This may be filtered further
  385. // if these are not allowed either.
  386. if(! function_exists('notags')) {
  387. function notags($string) {
  388. // protect against :<> with high-bit set
  389. return(str_replace(array("<",">","\xBA","\xBC","\xBE"), array('[',']','','',''), $string));
  390. }}
  391. // use this on "body" or "content" input where angle chars shouldn't be removed,
  392. // and allow them to be safely displayed.
  393. if(! function_exists('escape_tags')) {
  394. function escape_tags($string) {
  395. return(htmlspecialchars($string));
  396. }}
  397. // wrapper for adding a login box. If $register == true provide a registration
  398. // link. This will most always depend on the value of $a->config['register_policy'].
  399. // returns the complete html for inserting into the page
  400. if(! function_exists('login')) {
  401. function login($register = false) {
  402. $o = "";
  403. $register_html = (($register) ? load_view_file("view/register-link.tpl") : "");
  404. if(x($_SESSION,'authenticated')) {
  405. $o = load_view_file("view/logout.tpl");
  406. }
  407. else {
  408. $o = load_view_file("view/login.tpl");
  409. $o = replace_macros($o,array('$register_html' => $register_html ));
  410. }
  411. return $o;
  412. }}
  413. // generate a string that's random, but usually pronounceable.
  414. // used to generate initial passwords
  415. if(! function_exists('autoname')) {
  416. function autoname($len) {
  417. $vowels = array('a','a','ai','au','e','e','e','ee','ea','i','ie','o','ou','u');
  418. if(mt_rand(0,5) == 4)
  419. $vowels[] = 'y';
  420. $cons = array(
  421. 'b','bl','br',
  422. 'c','ch','cl','cr',
  423. 'd','dr',
  424. 'f','fl','fr',
  425. 'g','gh','gl','gr',
  426. 'h',
  427. 'j',
  428. 'k','kh','kl','kr',
  429. 'l',
  430. 'm',
  431. 'n',
  432. 'p','ph','pl','pr',
  433. 'qu',
  434. 'r','rh',
  435. 's','sc','sh','sm','sp','st',
  436. 't','th','tr',
  437. 'v',
  438. 'w','wh',
  439. 'x',
  440. 'z','zh'
  441. );
  442. $midcons = array('ck','ct','gn','ld','lf','lm','lt','mb','mm', 'mn','mp',
  443. 'nd','ng','nk','nt','rn','rp','rt');
  444. $noend = array('bl', 'br', 'cl','cr','dr','fl','fr','gl','gr',
  445. 'kh', 'kl','kr','mn','pl','pr','rh','tr','qu','wh');
  446. $start = mt_rand(0,2);
  447. if($start == 0)
  448. $table = $vowels;
  449. else
  450. $table = $cons;
  451. $word = '';
  452. for ($x = 0; $x < $len; $x ++) {
  453. $r = mt_rand(0,count($table) - 1);
  454. $word .= $table[$r];
  455. if($table == $vowels)
  456. $table = array_merge($cons,$midcons);
  457. else
  458. $table = $vowels;
  459. }
  460. $word = substr($word,0,$len);
  461. foreach($noend as $noe) {
  462. if((strlen($word) > 2) && (substr($word,-2) == $noe)) {
  463. $word = substr($word,0,-1);
  464. break;
  465. }
  466. }
  467. if(substr($word,-1) == 'q')
  468. $word = substr($word,0,-1);
  469. return $word;
  470. }}
  471. // Used to end the current process, after saving session state.
  472. if(! function_exists('killme')) {
  473. function killme() {
  474. session_write_close();
  475. exit;
  476. }}
  477. // redirect to another URL and terminate this process.
  478. if(! function_exists('goaway')) {
  479. function goaway($s) {
  480. header("Location: $s");
  481. killme();
  482. }}
  483. // Generic XML return
  484. // Outputs a basic dfrn XML status structure to STDOUT, with a <status> variable
  485. // of $st and an optional text <message> of $message and terminates the current process.
  486. if(! function_exists('xml_status')) {
  487. function xml_status($st, $message = '') {
  488. $xml_message = ((strlen($message)) ? "\t<message>" . xmlify($message) . "</message>\r\n" : '');
  489. header( "Content-type: text/xml" );
  490. echo '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
  491. echo "<result>\r\n\t<status>$st</status>\r\n$xml_message</result>\r\n";
  492. killme();
  493. }}
  494. // Returns the uid of locally logged in user or false.
  495. if(! function_exists('local_user')) {
  496. function local_user() {
  497. if((x($_SESSION,'authenticated')) && (x($_SESSION,'uid')))
  498. return intval($_SESSION['uid']);
  499. return false;
  500. }}
  501. // Returns contact id of authenticated site visitor or false
  502. if(! function_exists('remote_user')) {
  503. function remote_user() {
  504. if((x($_SESSION,'authenticated')) && (x($_SESSION,'visitor_id')))
  505. return intval($_SESSION['visitor_id']);
  506. return false;
  507. }}
  508. // contents of $s are displayed prominently on the page the next time
  509. // a page is loaded. Usually used for errors or alerts.
  510. if(! function_exists('notice')) {
  511. function notice($s) {
  512. $a = get_app();
  513. if($a->interactive)
  514. $_SESSION['sysmsg'] .= $s;
  515. }}
  516. // wrapper around config to limit the text length of an incoming message
  517. if(! function_exists('get_max_import_size')) {
  518. function get_max_import_size() {
  519. global $a;
  520. return ((x($a->config,'max_import_size')) ? $a->config['max_import_size'] : 0 );
  521. }}
  522. // escape text ($str) for XML transport
  523. // returns escaped text.
  524. if(! function_exists('xmlify')) {
  525. function xmlify($str) {
  526. $buffer = '';
  527. for($x = 0; $x < strlen($str); $x ++) {
  528. $char = $str[$x];
  529. switch( $char ) {
  530. case "\r" :
  531. break;
  532. case "&" :
  533. $buffer .= '&amp;';
  534. break;
  535. case "'" :
  536. $buffer .= '&apos;';
  537. break;
  538. case "\"" :
  539. $buffer .= '&quot;';
  540. break;
  541. case '<' :
  542. $buffer .= '&lt;';
  543. break;
  544. case '>' :
  545. $buffer .= '&gt;';
  546. break;
  547. case "\n" :
  548. $buffer .= "\n";
  549. break;
  550. default :
  551. $buffer .= $char;
  552. break;
  553. }
  554. }
  555. $buffer = trim($buffer);
  556. return($buffer);
  557. }}
  558. // undo an xmlify
  559. // pass xml escaped text ($s), returns unescaped text
  560. if(! function_exists('unxmlify')) {
  561. function unxmlify($s) {
  562. $ret = str_replace('&amp;','&', $s);
  563. $ret = str_replace(array('&lt;','&gt;','&quot;','&apos;'),array('<','>','"',"'"),$ret);
  564. return $ret;
  565. }}
  566. // convenience wrapper, reverse the operation "bin2hex"
  567. if(! function_exists('hex2bin')) {
  568. function hex2bin($s) {
  569. return(pack("H*",$s));
  570. }}
  571. // Automatic pagination.
  572. // To use, get the count of total items.
  573. // Then call $a->set_pager_total($number_items);
  574. // Optionally call $a->set_pager_itemspage($n) to the number of items to display on each page
  575. // Then call paginate($a) after the end of the display loop to insert the pager block on the page
  576. // (assuming there are enough items to paginate).
  577. // When using with SQL, the setting LIMIT %d, %d => $a->pager['start'],$a->pager['itemspage']
  578. // will limit the results to the correct items for the current page.
  579. // The actual page handling is then accomplished at the application layer.
  580. if(! function_exists('paginate')) {
  581. function paginate(&$a) {
  582. $o = '';
  583. $stripped = ereg_replace("(&page=[0-9]*)","",$_SERVER['QUERY_STRING']);
  584. $stripped = str_replace('q=','',$stripped);
  585. $stripped = trim($stripped,'/');
  586. $url = $a->get_baseurl() . '/' . $stripped;
  587. if($a->pager['total'] > $a->pager['itemspage']) {
  588. $o .= '<div class="pager">';
  589. if($a->pager['page'] != 1)
  590. $o .= '<span class="pager_prev">'."<a href=\"$url".'&page='.($a->pager['page'] - 1).'">' . t('prev') . '</a></span> ';
  591. $o .= "<span class=\"pager_first\"><a href=\"$url"."&page=1\">" . t('first') . "</a></span> ";
  592. $numpages = $a->pager['total'] / $a->pager['itemspage'];
  593. $numstart = 1;
  594. $numstop = $numpages;
  595. if($numpages > 14) {
  596. $numstart = (($pagenum > 7) ? ($pagenum - 7) : 1);
  597. $numstop = (($pagenum > ($numpages - 7)) ? $numpages : ($numstart + 14));
  598. }
  599. for($i = $numstart; $i <= $numstop; $i++){
  600. if($i == $a->pager['page'])
  601. $o .= '<span class="pager_current">'.(($i < 10) ? '&nbsp;'.$i : $i);
  602. else
  603. $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? '&nbsp;'.$i : $i)."</a>";
  604. $o .= '</span> ';
  605. }
  606. if(($a->pager['total'] % $a->pager['itemspage']) != 0) {
  607. if($i == $a->pager['page'])
  608. $o .= '<span class="pager_current">'.(($i < 10) ? '&nbsp;'.$i : $i);
  609. else
  610. $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? '&nbsp;'.$i : $i)."</a>";
  611. $o .= '</span> ';
  612. }
  613. $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
  614. $o .= "<span class=\"pager_last\"><a href=\"$url"."&page=$lastpage\">" . t('last') . "</a></span> ";
  615. if(($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0)
  616. $o .= '<span class="pager_next">'."<a href=\"$url"."&page=".($a->pager['page'] + 1).'">' . t('next') . '</a></span>';
  617. $o .= '</div>'."\r\n";
  618. }
  619. return $o;
  620. }}
  621. // Turn user/group ACLs stored as angle bracketed text into arrays
  622. if(! function_exists('expand_acl')) {
  623. function expand_acl($s) {
  624. // turn string array of angle-bracketed elements into numeric array
  625. // e.g. "<1><2><3>" => array(1,2,3);
  626. $ret = array();
  627. if(strlen($s)) {
  628. $t = str_replace('<','',$s);
  629. $a = explode('>',$t);
  630. foreach($a as $aa) {
  631. if(intval($aa))
  632. $ret[] = intval($aa);
  633. }
  634. }
  635. return $ret;
  636. }}
  637. // Used to wrap ACL elements in angle brackets for storage
  638. if(! function_exists('sanitise_acl')) {
  639. function sanitise_acl(&$item) {
  640. if(intval($item))
  641. $item = '<' . intval(notags(trim($item))) . '>';
  642. else
  643. unset($item);
  644. }}
  645. // retrieve a "family" of config variables from database to cached storage
  646. if(! function_exists('load_config')) {
  647. function load_config($family) {
  648. global $a;
  649. $r = q("SELECT * FROM `config` WHERE `cat` = '%s'",
  650. dbesc($family)
  651. );
  652. if(count($r)) {
  653. foreach($r as $rr) {
  654. $k = $rr['k'];
  655. $a->config[$family][$k] = $rr['v'];
  656. }
  657. }
  658. }}
  659. // get a particular config variable given the family name
  660. // and key. Returns false if not set.
  661. // $instore is only used by the set_config function
  662. // to determine if the key already exists in the DB
  663. // If a key is found in the DB but doesn't exist in
  664. // local config cache, pull it into the cache so we don't have
  665. // to hit the DB again for this item.
  666. if(! function_exists('get_config')) {
  667. function get_config($family, $key, $instore = false) {
  668. global $a;
  669. if(! $instore) {
  670. if(isset($a->config[$family][$key])) {
  671. if($a->config[$family][$key] === '!<unset>!') {
  672. return false;
  673. }
  674. return $a->config[$family][$key];
  675. }
  676. }
  677. $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
  678. dbesc($family),
  679. dbesc($key)
  680. );
  681. if(count($ret)) {
  682. $a->config[$family][$key] = $ret[0]['v'];
  683. return $ret[0]['v'];
  684. }
  685. else {
  686. $a->config[$family][$key] = '!<unset>!';
  687. }
  688. return false;
  689. }}
  690. // Store a config value ($value) in the category ($family)
  691. // under the key ($key)
  692. // Return the value, or false if the database update failed
  693. if(! function_exists('set_config')) {
  694. function set_config($family,$key,$value) {
  695. global $a;
  696. $a->config[$family][$key] = $value;
  697. if(get_config($family,$key,true) === false) {
  698. $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
  699. dbesc($family),
  700. dbesc($key),
  701. dbesc($value)
  702. );
  703. if($ret)
  704. return $value;
  705. return $ret;
  706. }
  707. $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
  708. dbesc($value),
  709. dbesc($family),
  710. dbesc($key)
  711. );
  712. if($ret)
  713. return $value;
  714. return $ret;
  715. }}
  716. // convert an XML document to a normalised, case-corrected array
  717. // used by webfinger
  718. if(! function_exists('convert_xml_element_to_array')) {
  719. function convert_xml_element_to_array($xml_element, &$recursion_depth=0) {
  720. // If we're getting too deep, bail out
  721. if ($recursion_depth > 512) {
  722. return(null);
  723. }
  724. if (!is_string($xml_element) &&
  725. !is_array($xml_element) &&
  726. (get_class($xml_element) == 'SimpleXMLElement')) {
  727. $xml_element_copy = $xml_element;
  728. $xml_element = get_object_vars($xml_element);
  729. }
  730. if (is_array($xml_element)) {
  731. $result_array = array();
  732. if (count($xml_element) <= 0) {
  733. return (trim(strval($xml_element_copy)));
  734. }
  735. foreach($xml_element as $key=>$value) {
  736. $recursion_depth++;
  737. $result_array[strtolower($key)] =
  738. convert_xml_element_to_array($value, $recursion_depth);
  739. $recursion_depth--;
  740. }
  741. if ($recursion_depth == 0) {
  742. $temp_array = $result_array;
  743. $result_array = array(
  744. strtolower($xml_element_copy->getName()) => $temp_array,
  745. );
  746. }
  747. return ($result_array);
  748. } else {
  749. return (trim(strval($xml_element)));
  750. }
  751. }}
  752. // Given an email style address, perform webfinger lookup and
  753. // return the resulting DFRN profile URL, or if no DFRN profile URL
  754. // is located, returns an OStatus subscription template (prefixed
  755. // with the string 'stat:' to identify it as on OStatus template).
  756. // If this isn't an email style address just return $s.
  757. // Return an empty string if email-style addresses but webfinger fails,
  758. // or if the resultant personal XRD doesn't contain a supported
  759. // subscription/friend-request attribute.
  760. if(! function_exists('webfinger_dfrn')) {
  761. function webfinger_dfrn($s) {
  762. if(! strstr($s,'@')) {
  763. return $s;
  764. }
  765. $links = webfinger($s);
  766. logger('webfinger_dfrn: ' . $s . ':' . print_r($links,true), LOGGER_DATA);
  767. if(count($links)) {
  768. foreach($links as $link)
  769. if($link['@attributes']['rel'] === NAMESPACE_DFRN)
  770. return $link['@attributes']['href'];
  771. foreach($links as $link)
  772. if($link['@attributes']['rel'] === NAMESPACE_OSTATUSSUB)
  773. return 'stat:' . $link['@attributes']['template'];
  774. }
  775. return '';
  776. }}
  777. // Given an email style address, perform webfinger lookup and
  778. // return the array of link attributes from the personal XRD file.
  779. // On error/failure return an empty array.
  780. if(! function_exists('webfinger')) {
  781. function webfinger($s) {
  782. $host = '';
  783. if(strstr($s,'@')) {
  784. $host = substr($s,strpos($s,'@') + 1);
  785. }
  786. if(strlen($host)) {
  787. $tpl = fetch_lrdd_template($host);
  788. logger('webfinger: lrdd template: ' . $tpl);
  789. if(strlen($tpl)) {
  790. $pxrd = str_replace('{uri}', urlencode('acct:'.$s), $tpl);
  791. $links = fetch_xrd_links($pxrd);
  792. if(! count($links)) {
  793. // try with double slashes
  794. $pxrd = str_replace('{uri}', urlencode('acct://'.$s), $tpl);
  795. $links = fetch_xrd_links($pxrd);
  796. }
  797. return $links;
  798. }
  799. }
  800. return array();
  801. }}
  802. if(! function_exists('lrdd')) {
  803. function lrdd($uri) {
  804. $a = get_app();
  805. if(strstr($uri,'@')) {
  806. return(webfinger($uri));
  807. }
  808. else {
  809. $html = fetch_url($uri);
  810. $headers = $a->get_curl_headers();
  811. $lines = explode("\n",$headers);
  812. if(count($lines)) {
  813. foreach($lines as $line) {
  814. // TODO alter the following regex to support multiple relations (space separated)
  815. if((stristr($line,'link:')) && preg_match('/<([^>].*)>.*rel\=[\'\"]lrdd[\'\"]/',$line,$matches)) {
  816. $link = $matches[1];
  817. break;
  818. }
  819. }
  820. }
  821. if(! isset($link)) {
  822. // parse the page of the supplied URL looking for rel links
  823. require_once('library/HTML5/Parser.php');
  824. $dom = HTML5_Parser::parse($html);
  825. if($dom) {
  826. $items = $dom->getElementsByTagName('link');
  827. foreach($items as $item) {
  828. $x = $item->getAttribute('rel');
  829. if($x == "lrdd") {
  830. $link = $item->getAttribute('href');
  831. break;
  832. }
  833. }
  834. }
  835. }
  836. if(isset($link))
  837. return(fetch_xrd_links($link));
  838. }
  839. return array();
  840. }}
  841. // Given a host name, locate the LRDD template from that
  842. // host. Returns the LRDD template or an empty string on
  843. // error/failure.
  844. if(! function_exists('fetch_lrdd_template')) {
  845. function fetch_lrdd_template($host) {
  846. $tpl = '';
  847. $url = 'http://' . $host . '/.well-known/host-meta' ;
  848. $links = fetch_xrd_links($url);
  849. if(count($links)) {
  850. foreach($links as $link)
  851. if($link['@attributes']['rel'] && $link['@attributes']['rel'] === 'lrdd')
  852. $tpl = $link['@attributes']['template'];
  853. }
  854. if(! strpos($tpl,'{uri}'))
  855. $tpl = '';
  856. return $tpl;
  857. }}
  858. // Given a URL, retrieve the page as an XRD document.
  859. // Return an array of links.
  860. // on error/failure return empty array.
  861. if(! function_exists('fetch_xrd_links')) {
  862. function fetch_xrd_links($url) {
  863. $xml = fetch_url($url);
  864. if (! $xml)
  865. return array();
  866. logger('fetch_xrd_links: ' . $xml, LOGGER_DATA);
  867. $h = simplexml_load_string($xml);
  868. $arr = convert_xml_element_to_array($h);
  869. if(isset($arr['xrd']['link'])) {
  870. $link = $arr['xrd']['link'];
  871. if(! isset($link[0]))
  872. $links = array($link);
  873. else
  874. $links = $link;
  875. return $links;
  876. }
  877. return array();
  878. }}
  879. // Convert an ACL array to a storable string
  880. if(! function_exists('perms2str')) {
  881. function perms2str($p) {
  882. $ret = '';
  883. $tmp = $p;
  884. if(is_array($tmp)) {
  885. array_walk($tmp,'sanitise_acl');
  886. $ret = implode('',$tmp);
  887. }
  888. return $ret;
  889. }}
  890. // generate a guaranteed unique (for this domain) item ID for ATOM
  891. // safe from birthday paradox
  892. if(! function_exists('item_new_uri')) {
  893. function item_new_uri($hostname,$uid) {
  894. do {
  895. $dups = false;
  896. $hash = random_string();
  897. $uri = "urn:X-dfrn:" . $hostname . ':' . $uid . ':' . $hash;
  898. $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
  899. dbesc($uri));
  900. if(count($r))
  901. $dups = true;
  902. } while($dups == true);
  903. return $uri;
  904. }}
  905. // Generate a guaranteed unique photo ID.
  906. // safe from birthday paradox
  907. if(! function_exists('photo_new_resource')) {
  908. function photo_new_resource() {
  909. do {
  910. $found = false;
  911. $resource = hash('md5',uniqid(mt_rand(),true));
  912. $r = q("SELECT `id` FROM `photo` WHERE `resource-id` = '%s' LIMIT 1",
  913. dbesc($resource)
  914. );
  915. if(count($r))
  916. $found = true;
  917. } while($found == true);
  918. return $resource;
  919. }}
  920. // Take a URL from the wild, prepend http:// if necessary
  921. // and check DNS to see if it's real
  922. // return true if it's OK, false if something is wrong with it
  923. if(! function_exists('validate_url')) {
  924. function validate_url(&$url) {
  925. if(substr($url,0,4) != 'http')
  926. $url = 'http://' . $url;
  927. $h = parse_url($url);
  928. if(! $h) {
  929. return false;
  930. }
  931. if(! checkdnsrr($h['host'], 'ANY')) {
  932. return false;
  933. }
  934. return true;
  935. }}
  936. // Check $url against our list of allowed sites,
  937. // wildcards allowed. If allowed_sites is unset return true;
  938. // If url is allowed, return true.
  939. // otherwise, return false
  940. if(! function_exists('allowed_url')) {
  941. function allowed_url($url) {
  942. $h = parse_url($url);
  943. if(! $h) {
  944. return false;
  945. }
  946. $str_allowed = get_config('system','allowed_sites');
  947. if(! $str_allowed)
  948. return true;
  949. $found = false;
  950. $host = strtolower($h['host']);
  951. // always allow our own site
  952. if($host == strtolower($_SERVER['SERVER_NAME']))
  953. return true;
  954. $fnmatch = function_exists('fnmatch');
  955. $allowed = explode(',',$str_allowed);
  956. if(count($allowed)) {
  957. foreach($allowed as $a) {
  958. $pat = strtolower(trim($a));
  959. if(($fnmatch && fnmatch($pat,$host)) || ($pat == $host)) {
  960. $found = true;
  961. break;
  962. }
  963. }
  964. }
  965. return $found;
  966. }}
  967. // check if email address is allowed to register here.
  968. // Compare against our list (wildcards allowed).
  969. // Returns false if not allowed, true if allowed or if
  970. // allowed list is not configured.
  971. if(! function_exists('allowed_email')) {
  972. function allowed_email($email) {
  973. $domain = strtolower(substr($email,strpos($email,'@') + 1));
  974. if(! $domain)
  975. return false;
  976. $str_allowed = get_config('system','allowed_email');
  977. if(! $str_allowed)
  978. return true;
  979. $found = false;
  980. $fnmatch = function_exists('fnmatch');
  981. $allowed = explode(',',$str_allowed);
  982. if(count($allowed)) {
  983. foreach($allowed as $a) {
  984. $pat = strtolower(trim($a));
  985. if(($fnmatch && fnmatch($pat,$host)) || ($pat == $host)) {
  986. $found = true;
  987. break;
  988. }
  989. }
  990. }
  991. return $found;
  992. }}
  993. // Format the like/dislike text for a profile item
  994. // $cnt = number of people who like/dislike the item
  995. // $arr = array of pre-linked names of likers/dislikers
  996. // $type = one of 'like, 'dislike'
  997. // $id = item id
  998. // returns formatted text
  999. if(! function_exists('format_like')) {
  1000. function format_like($cnt,$arr,$type,$id) {
  1001. $o = '';
  1002. if($cnt == 1)
  1003. $o .= $arr[0] . (($type === 'like') ? t(' likes this.') : t(' doesn\'t like this.')) . EOL ;
  1004. else {
  1005. $o .= '<span class="fakelink" onclick="openClose(\'' . $type . 'list-' . $id . '\');" >'
  1006. . $cnt . ' ' . t('people') . '</span> ' . (($type === 'like') ? t('like this.') : t('don\'t like this.')) . EOL ;
  1007. $total = count($arr);
  1008. if($total >= MAX_LIKERS)
  1009. $arr = array_slice($arr, 0, MAX_LIKERS - 1);
  1010. if($total < MAX_LIKERS)
  1011. $arr[count($arr)-1] = t('and') . ' ' . $arr[count($arr)-1];
  1012. $str = implode(', ', $arr);
  1013. if($total >= MAX_LIKERS)
  1014. $str .= t(', and ') . $total - MAX_LIKERS . t(' other people');
  1015. $str .= (($type === 'like') ? t(' like this.') : t(' don\'t like this.'));
  1016. $o .= "\t" . '<div id="' . $type . 'list-' . $id . '" style="display: none;" >' . $str . '</div>';
  1017. }
  1018. return $o;
  1019. }}
  1020. // wrapper to load a view template, checking for alternate
  1021. // languages before falling back to the default
  1022. if(! function_exists('load_view_file')) {
  1023. function load_view_file($s) {
  1024. $b = basename($s);
  1025. $d = dirname($s);
  1026. $lang = get_config('system','language');
  1027. if($lang && file_exists("$d/$lang/$b"))
  1028. return file_get_contents("$d/$lang/$b");
  1029. return file_get_contents($s);
  1030. }}
  1031. // for html,xml parsing - let's say you've got
  1032. // an attribute foobar="class1 class2 class3"
  1033. // and you want to find out if it contains 'class3'.
  1034. // you can't use a normal sub string search because you
  1035. // might match 'notclass3' and a regex to do the job is
  1036. // possible but a bit complicated.
  1037. // pass the attribute string as $attr and the attribute you
  1038. // are looking for as $s - returns true if found, otherwise false
  1039. if(! function_exists('attribute_contains')) {
  1040. function attribute_contains($attr,$s) {
  1041. $a = explode(' ', $attr);
  1042. if(count($a) && in_array($s,$a))
  1043. return true;
  1044. return false;
  1045. }}
  1046. if(! function_exists('logger')) {
  1047. function logger($msg,$level = 0) {
  1048. $debugging = get_config('system','debugging');
  1049. $loglevel = intval(get_config('system','loglevel'));
  1050. $logfile = get_config('system','logfile');
  1051. if((! $debugging) || (! $logfile) || ($level > $loglevel))
  1052. return;
  1053. @file_put_contents($logfile, datetime_convert() . ':' . session_id() . ' ' . $msg . "\n", FILE_APPEND);
  1054. return;
  1055. }}
  1056. if(! function_exists('activity_match')) {
  1057. function activity_match($haystack,$needle) {
  1058. if(($haystack === $needle) || ((basename($needle) === $haystack) && strstr($needle,NAMESPACE_ACTIVITY_SCHEMA)))
  1059. return true;
  1060. return false;
  1061. }}
  1062. // Pull out all #hashtags and @person tags from $s;
  1063. // We also get @person@domain.com - which would make
  1064. // the regex quite complicated as tags can also
  1065. // end a sentence. So we'll run through our results
  1066. // and strip the period from any tags which end with one.
  1067. // Returns array of tags found, or empty array.
  1068. if(! function_exists('get_tags')) {
  1069. function get_tags($s) {
  1070. $ret = array();
  1071. if(preg_match_all('/([@#][^ ,:?]*)([ ,:?]|$)/',$s,$match)) {
  1072. foreach($match[1] as $match) {
  1073. if(substr($match,-1,1) === '.')
  1074. $ret[] = substr($match,0,-1);
  1075. else
  1076. $ret[] = $match;
  1077. }
  1078. }
  1079. return $ret;
  1080. }}
  1081. // quick and dirty quoted_printable encoding
  1082. if(! function_exists('qp')) {
  1083. function qp($s) {
  1084. return str_replace ("%","=",rawurlencode($s));
  1085. }}
  1086. if(! function_exists('like_puller')) {
  1087. function like_puller($a,$item,&$arr,$mode) {
  1088. $url = '';
  1089. $sparkle = '';
  1090. $verb = (($mode === 'like') ? ACTIVITY_LIKE : ACTIVITY_DISLIKE);
  1091. if((activity_match($item['verb'],$verb)) && ($item['id'] != $item['parent'])) {
  1092. $url = $item['url'];
  1093. if(($item['network'] === 'dfrn') && (! $item['self'])) {
  1094. $url = $a->get_baseurl() . '/redir/' . $item['contact-id'];
  1095. $sparkle = ' class="sparkle" ';
  1096. }
  1097. if(! ((isset($arr[$item['parent'] . '-l'])) && (is_array($arr[$item['parent'] . '-l']))))
  1098. $arr[$item['parent'] . '-l'] = array();
  1099. if(! isset($arr[$item['parent']]))
  1100. $arr[$item['parent']] = 1;
  1101. else
  1102. $arr[$item['parent']] ++;
  1103. $arr[$item['parent'] . '-l'][] = '<a href="'. $url . '"'. $sparkle .'>' . $item['name'] . '</a>';
  1104. }
  1105. return;
  1106. }}
  1107. if(! function_exists('get_mentions')) {
  1108. function get_mentions($item) {
  1109. $o = '';
  1110. if(! strlen($item['tag']))
  1111. return $o;
  1112. $arr = explode(',',$item['tag']);
  1113. foreach($arr as $x) {
  1114. $matches = null;
  1115. if(preg_match('/@\[url=([^\]]*)\]/',$x,$matches))
  1116. $o .= "\t\t" . '<link rel="mentioned" href="' . $matches[1] . '" />' . "\r\n";
  1117. }
  1118. return $o;
  1119. }}
  1120. if(! function_exists('contact_block')) {
  1121. function contact_block() {
  1122. $o = '';
  1123. $a = get_app();
  1124. if((! is_array($a->profile)) || ($a->profile['hide-friends']))
  1125. return $o;
  1126. $r = q("SELECT COUNT(*) AS `total` FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 and `pending` = 0",
  1127. intval($a->profile['uid'])
  1128. );
  1129. if(count($r)) {
  1130. $total = intval($r[0]['total']);
  1131. }
  1132. if(! $total) {
  1133. $o .= '<h4 class="contact-h4">' . t('No contacts') . '</h4>';
  1134. return $o;
  1135. }
  1136. $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 and `pending` = 0 ORDER BY RAND() LIMIT 24",
  1137. intval($a->profile['uid'])
  1138. );
  1139. if(count($r)) {
  1140. $o .= '<h4 class="contact-h4">' . $total . ' ' . t('Contacts') . '</h4><div id="contact-block">';
  1141. foreach($r as $rr) {
  1142. $redirect_url = $a->get_baseurl() . '/redir/' . $rr['id'];
  1143. if(local_user() && ($rr['uid'] == local_user())
  1144. && ($rr['network'] === 'dfrn')) {
  1145. $url = $redirect_url;
  1146. $sparkle = ' sparkle';
  1147. }
  1148. else {
  1149. $url = $rr['url'];
  1150. $sparkle = '';
  1151. }
  1152. $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";
  1153. }
  1154. $o .= '</div><div id="contact-block-end"></div>';
  1155. $o .= '<div id="viewcontacts"><a id="viewcontacts-link" href="viewcontacts/' . $a->profile['nickname'] . '">' . t('View Contacts') . '</a></div>';
  1156. }
  1157. return $o;
  1158. }}
  1159. if(! function_exists('search')) {
  1160. function search($s) {
  1161. $a = get_app();
  1162. $o = '<div id="search-box">';
  1163. $o .= '<form action="' . $a->get_baseurl() . '/search' . '" method="get" >';
  1164. $o .= '<input type="text" name="search" id="search-text" value="' . $s .'" />';
  1165. $o .= '<input type="submit" name="submit" id="search-submit" value="' . t('Search') . '" />';
  1166. $o .= '</form></div>';
  1167. return $o;
  1168. }}