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.

911 lines
21 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. <?php
  2. set_time_limit(0);
  3. define ( 'BUILD_ID' , 1003 );
  4. define ( 'EOL', "<br />\r\n");
  5. define ( 'ATOM_TIME', 'Y-m-d\TH:i:s\Z' );
  6. define ( 'REGISTER_CLOSED', 0);
  7. define ( 'REGISTER_APPROVE', 1);
  8. define ( 'REGISTER_OPEN', 2);
  9. // relationship types
  10. define ( 'REL_VIP', 1);
  11. define ( 'REL_FAN', 2);
  12. define ( 'REL_BUD', 3);
  13. define ( 'NOTIFY_INTRO', 0x0001 );
  14. define ( 'NOTIFY_CONFIRM', 0x0002 );
  15. define ( 'NOTIFY_WALL', 0x0004 );
  16. define ( 'NOTIFY_COMMENT', 0x0008 );
  17. define ( 'NOTIFY_MAIL', 0x0010 );
  18. define ( 'NAMESPACE_DFRN' , 'http://purl.org/macgirvin/dfrn/1.0' );
  19. define ( 'NAMESPACE_THREAD' , 'http://purl.org/syndication/thread/1.0' );
  20. define ( 'NAMESPACE_TOMB' , 'http://purl.org/atompub/tombstones/1.0' );
  21. define ( 'NAMESPACE_ACTIVITY', 'http://activitystrea.ms/spec/1.0/' );
  22. define ( 'NAMESPACE_ACTIVITY_SCHEMA', 'http://activitystrea.ms/schema/1.0/');
  23. define ( 'ACTIVITY_LIKE', NAMESPACE_ACTIVITY_SCHEMA . 'like' );
  24. define ( 'ACTIVITY_DISLIKE', NAMESPACE_DFRN . '/dislike' );
  25. define ( 'ACTIVITY_OBJ_HEART', NAMESPACE_DFRN . '/heart' );
  26. define ( 'ACTIVITY_FRIEND', NAMESPACE_ACTIVITY_SCHEMA . 'make-friend' );
  27. define ( 'ACTIVITY_POST', NAMESPACE_ACTIVITY_SCHEMA . 'post' );
  28. define ( 'ACTIVITY_UPDATE', NAMESPACE_ACTIVITY_SCHEMA . 'update' );
  29. define ( 'ACTIVITY_OBJ_COMMENT', NAMESPACE_ACTIVITY_SCHEMA . 'comment' );
  30. define ( 'ACTIVITY_OBJ_NOTE', NAMESPACE_ACTIVITY_SCHEMA . 'note' );
  31. define ( 'ACTIVITY_OBJ_PERSON', NAMESPACE_ACTIVITY_SCHEMA . 'person' );
  32. define ( 'ACTIVITY_OBJ_PHOTO', NAMESPACE_ACTIVITY_SCHEMA . 'photo' );
  33. define ( 'ACTIVITY_OBJ_P_PHOTO', NAMESPACE_ACTIVITY_SCHEMA . 'profile-photo' );
  34. define ( 'ACTIVITY_OBJ_ALBUM', NAMESPACE_ACTIVITY_SCHEMA . 'photo-album' );
  35. define ( 'GRAVITY_PARENT', 0);
  36. define ( 'GRAVITY_LIKE', 3);
  37. define ( 'GRAVITY_COMMENT', 6);
  38. if(! class_exists('App')) {
  39. class App {
  40. public $module_loaded = false;
  41. public $config;
  42. public $page;
  43. public $profile;
  44. public $user;
  45. public $cid;
  46. public $contact;
  47. public $content;
  48. public $data;
  49. public $error = false;
  50. public $cmd;
  51. public $argv;
  52. public $argc;
  53. public $module;
  54. public $pager;
  55. public $strings;
  56. public $path;
  57. private $scheme;
  58. private $hostname;
  59. private $baseurl;
  60. private $db;
  61. function __construct() {
  62. $this->config = array();
  63. $this->page = array();
  64. $this->pager= array();
  65. $this->scheme = ((isset($_SERVER['HTTPS'])
  66. && ($_SERVER['HTTPS'])) ? 'https' : 'http' );
  67. $this->hostname = str_replace('www.','',
  68. $_SERVER['SERVER_NAME']);
  69. set_include_path("include/$this->hostname"
  70. . PATH_SEPARATOR . 'include'
  71. . PATH_SEPARATOR . '.' );
  72. if(substr($_SERVER['QUERY_STRING'],0,2) == "q=")
  73. $_SERVER['QUERY_STRING'] = substr($_SERVER['QUERY_STRING'],2);
  74. $this->cmd = trim($_GET['q'],'/');
  75. $this->argv = explode('/',$this->cmd);
  76. $this->argc = count($this->argv);
  77. if((array_key_exists('0',$this->argv)) && strlen($this->argv[0])) {
  78. $this->module = $this->argv[0];
  79. }
  80. else {
  81. $this->module = 'home';
  82. }
  83. if($this->cmd === '.well-known/host-meta')
  84. require_once('include/hostxrd.php');
  85. $this->pager['page'] = ((x($_GET,'page')) ? $_GET['page'] : 1);
  86. $this->pager['itemspage'] = 50;
  87. $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
  88. $this->pager['total'] = 0;
  89. }
  90. function get_baseurl($ssl = false) {
  91. if(strlen($this->baseurl))
  92. return $this->baseurl;
  93. $this->baseurl = (($ssl) ? 'https' : $this->scheme) . "://" . $this->hostname
  94. . ((isset($this->path) && strlen($this->path))
  95. ? '/' . $this->path : '' );
  96. return $this->baseurl;
  97. }
  98. function set_baseurl($url) {
  99. $this->baseurl = $url;
  100. $this->hostname = basename($url);
  101. }
  102. function get_hostname() {
  103. return $this->hostname;
  104. }
  105. function set_hostname($h) {
  106. $this->hostname = $h;
  107. }
  108. function set_path($p) {
  109. $this->path = ltrim(trim($p),'/');
  110. }
  111. function get_path() {
  112. return $this->path;
  113. }
  114. function set_pager_total($n) {
  115. $this->pager['total'] = intval($n);
  116. }
  117. function set_pager_itemspage($n) {
  118. $this->pager['itemspage'] = intval($n);
  119. $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
  120. }
  121. function init_pagehead() {
  122. $tpl = load_view_file("view/head.tpl");
  123. $this->page['htmlhead'] = replace_macros($tpl,array(
  124. '$baseurl' => $this->get_baseurl()
  125. ));
  126. }
  127. }}
  128. if(! function_exists('x')) {
  129. function x($s,$k = NULL) {
  130. if($k != NULL) {
  131. if((is_array($s)) && (array_key_exists($k,$s))) {
  132. if($s[$k])
  133. return (int) 1;
  134. return (int) 0;
  135. }
  136. return false;
  137. }
  138. else {
  139. if(isset($s)) {
  140. if($s) {
  141. return (int) 1;
  142. }
  143. return (int) 0;
  144. }
  145. return false;
  146. }
  147. }}
  148. if(! function_exists('system_unavailable')) {
  149. function system_unavailable() {
  150. include('system_unavailable.php');
  151. killme();
  152. }}
  153. if(! function_exists('check_config')) {
  154. function check_config(&$a) {
  155. load_config('system');
  156. $build = get_config('system','build');
  157. if(! x($build))
  158. $build = set_config('system','build',BUILD_ID);
  159. $url = get_config('system','url');
  160. if(! x($url))
  161. $url = set_config('system','url',$a->get_baseurl());
  162. if($build != BUILD_ID) {
  163. $stored = intval($build);
  164. $current = intval(BUILD_ID);
  165. if(($stored < $current) && file_exists('update.php')) {
  166. // We're reporting a different version than what is currently installed.
  167. // Run any existing update scripts to bring the database up to current.
  168. require_once('update.php');
  169. for($x = $stored; $x < $current; $x ++) {
  170. if(function_exists('update_' . $x)) {
  171. $func = 'update_' . $x;
  172. $func($a);
  173. }
  174. }
  175. set_config('system','build', BUILD_ID);
  176. }
  177. }
  178. return;
  179. }}
  180. if(! function_exists('replace_macros')) {
  181. function replace_macros($s,$r) {
  182. $search = array();
  183. $replace = array();
  184. if(is_array($r) && count($r)) {
  185. foreach ($r as $k => $v ) {
  186. $search[] = $k;
  187. $replace[] = $v;
  188. }
  189. }
  190. return str_replace($search,$replace,$s);
  191. }}
  192. if(! function_exists('load_translation_table')) {
  193. function load_translation_table($lang) {
  194. global $a;
  195. }}
  196. if(! function_exists('t')) {
  197. function t($s) {
  198. global $a;
  199. if($a->strings[$s])
  200. return $a->strings[$s];
  201. return $s;
  202. }}
  203. if(! function_exists('fetch_url')) {
  204. function fetch_url($url,$binary = false) {
  205. $ch = curl_init($url);
  206. if(! $ch) return false;
  207. curl_setopt($ch, CURLOPT_HEADER, 0);
  208. curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
  209. curl_setopt($ch, CURLOPT_MAXREDIRS,8);
  210. curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
  211. $prx = get_config('system','proxy');
  212. if(strlen($prx)) {
  213. curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
  214. curl_setopt($ch, CURLOPT_PROXY, $prx);
  215. $prxusr = get_config('system','proxyuser');
  216. if(strlen($prxusr))
  217. curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
  218. }
  219. if($binary)
  220. curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
  221. $s = curl_exec($ch);
  222. curl_close($ch);
  223. return($s);
  224. }}
  225. if(! function_exists('post_url')) {
  226. function post_url($url,$params) {
  227. $ch = curl_init($url);
  228. if(! $ch) return false;
  229. curl_setopt($ch, CURLOPT_HEADER, 0);
  230. curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
  231. curl_setopt($ch, CURLOPT_MAXREDIRS,8);
  232. curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
  233. curl_setopt($ch, CURLOPT_POST,1);
  234. curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
  235. $prx = get_config('system','proxy');
  236. if(strlen($prx)) {
  237. curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
  238. curl_setopt($ch, CURLOPT_PROXY, $prx);
  239. $prxusr = get_config('system','proxyuser');
  240. if(strlen($prxusr))
  241. curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
  242. }
  243. $s = curl_exec($ch);
  244. curl_close($ch);
  245. return($s);
  246. }}
  247. if(! function_exists('random_string')) {
  248. function random_string() {
  249. return(hash('sha256',uniqid(rand(),true)));
  250. }}
  251. if(! function_exists('notags')) {
  252. function notags($string) {
  253. // protect against :<> with high-bit set
  254. return(str_replace(array("<",">","\xBA","\xBC","\xBE"), array('[',']','','',''), $string));
  255. }}
  256. if(! function_exists('escape_tags')) {
  257. function escape_tags($string) {
  258. return(htmlspecialchars($string));
  259. }}
  260. if(! function_exists('login')) {
  261. function login($register = false) {
  262. $o = "";
  263. $register_html = (($register) ? load_view_file("view/register-link.tpl") : "");
  264. if(x($_SESSION,'authenticated')) {
  265. $o = load_view_file("view/logout.tpl");
  266. }
  267. else {
  268. $o = load_view_file("view/login.tpl");
  269. $o = replace_macros($o,array('$register_html' => $register_html ));
  270. }
  271. return $o;
  272. }}
  273. if(! function_exists('autoname')) {
  274. function autoname($len) {
  275. $vowels = array('a','a','ai','au','e','e','e','ee','ea','i','ie','o','ou','u');
  276. if(mt_rand(0,5) == 4)
  277. $vowels[] = 'y';
  278. $cons = array(
  279. 'b','bl','br',
  280. 'c','ch','cl','cr',
  281. 'd','dr',
  282. 'f','fl','fr',
  283. 'g','gh','gl','gr',
  284. 'h',
  285. 'j',
  286. 'k','kh','kl','kr',
  287. 'l',
  288. 'm',
  289. 'n',
  290. 'p','ph','pl','pr',
  291. 'qu',
  292. 'r','rh',
  293. 's','sc','sh','sm','sp','st',
  294. 't','th','tr',
  295. 'v',
  296. 'w','wh',
  297. 'x',
  298. 'z','zh'
  299. );
  300. $midcons = array('ck','ct','gn','ld','lf','lm','lt','mb','mm', 'mn','mp',
  301. 'nd','ng','nk','nt','rn','rp','rt');
  302. $noend = array('bl', 'br', 'cl','cr','dr','fl','fr','gl','gr',
  303. 'kh', 'kl','kr','mn','pl','pr','rh','tr','qu','wh');
  304. $start = mt_rand(0,2);
  305. if($start == 0)
  306. $table = $vowels;
  307. else
  308. $table = $cons;
  309. $word = '';
  310. for ($x = 0; $x < $len; $x ++) {
  311. $r = mt_rand(0,count($table) - 1);
  312. $word .= $table[$r];
  313. if($table == $vowels)
  314. $table = array_merge($cons,$midcons);
  315. else
  316. $table = $vowels;
  317. }
  318. $word = substr($word,0,$len);
  319. foreach($noend as $noe) {
  320. if((strlen($word) > 2) && (substr($word,-2) == $noe)) {
  321. $word = substr($word,0,-1);
  322. break;
  323. }
  324. }
  325. if(substr($word,-1) == 'q')
  326. $word = substr($word,0,-1);
  327. return $word;
  328. }}
  329. if(! function_exists('killme')) {
  330. function killme() {
  331. session_write_close();
  332. exit;
  333. }}
  334. if(! function_exists('goaway')) {
  335. function goaway($s) {
  336. header("Location: $s");
  337. killme();
  338. }}
  339. if(! function_exists('xml_status')) {
  340. function xml_status($st) {
  341. header( "Content-type: text/xml" );
  342. echo '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
  343. echo "<result><status>$st</status></result>\r\n";
  344. killme();
  345. }}
  346. if(! function_exists('local_user')) {
  347. function local_user() {
  348. if((x($_SESSION,'authenticated')) && (x($_SESSION,'uid')))
  349. return $_SESSION['uid'];
  350. return false;
  351. }}
  352. if(! function_exists('remote_user')) {
  353. function remote_user() {
  354. if((x($_SESSION,'authenticated')) && (x($_SESSION,'visitor_id')))
  355. return $_SESSION['visitor_id'];
  356. return false;
  357. }}
  358. if(! function_exists('notice')) {
  359. function notice($s) {
  360. $_SESSION['sysmsg'] .= $s;
  361. }}
  362. if(! function_exists('get_max_import_size')) {
  363. function get_max_import_size() {
  364. global $a;
  365. return ((x($a->config,'max_import_size')) ? $a->config['max_import_size'] : 0 );
  366. }}
  367. if(! function_exists('xmlify')) {
  368. function xmlify($str) {
  369. $buffer = '';
  370. for($x = 0; $x < strlen($str); $x ++) {
  371. $char = $str[$x];
  372. switch( $char ) {
  373. case "\r" :
  374. break;
  375. case "&" :
  376. $buffer .= '&amp;';
  377. break;
  378. case "'" :
  379. $buffer .= '&apos;';
  380. break;
  381. case "\"" :
  382. $buffer .= '&quot;';
  383. break;
  384. case '<' :
  385. $buffer .= '&lt;';
  386. break;
  387. case '>' :
  388. $buffer .= '&gt;';
  389. break;
  390. case "\n" :
  391. $buffer .= ' ';
  392. break;
  393. default :
  394. $buffer .= $char;
  395. break;
  396. }
  397. }
  398. $buffer = trim($buffer);
  399. return($buffer);
  400. }}
  401. if(! function_exists('unxmlify')) {
  402. function unxmlify($s) {
  403. $ret = str_replace('&amp;','&', $s);
  404. $ret = str_replace(array('&lt;','&gt;','&quot;','&apos;'),array('<','>','"',"'"),$ret);
  405. return $ret;
  406. }}
  407. if(! function_exists('hex2bin')) {
  408. function hex2bin($s) {
  409. return(pack("H*",$s));
  410. }}
  411. if(! function_exists('paginate')) {
  412. function paginate(&$a) {
  413. $o = '';
  414. $stripped = ereg_replace("(&page=[0-9]*)","",$_SERVER['QUERY_STRING']);
  415. $stripped = str_replace('q=','',$stripped);
  416. $stripped = trim($stripped,'/');
  417. $url = $a->get_baseurl() . '/' . $stripped;
  418. if($a->pager['total'] > $a->pager['itemspage']) {
  419. $o .= '<div class="pager">';
  420. if($a->pager['page'] != 1)
  421. $o .= '<span class="pager_prev">'."<a href=\"$url".'&page='.($a->pager['page'] - 1).'">' . t('prev') . '</a></span> ';
  422. $o .= "<span class=\"pager_first\"><a href=\"$url"."&page=1\">" . t('first') . "</a></span> ";
  423. $numpages = $a->pager['total'] / $a->pager['itemspage'];
  424. $numstart = 1;
  425. $numstop = $numpages;
  426. if($numpages > 14) {
  427. $numstart = (($pagenum > 7) ? ($pagenum - 7) : 1);
  428. $numstop = (($pagenum > ($numpages - 7)) ? $numpages : ($numstart + 14));
  429. }
  430. for($i = $numstart; $i <= $numstop; $i++){
  431. if($i == $a->pager['page'])
  432. $o .= '<span class="pager_current">'.(($i < 10) ? '&nbsp;'.$i : $i);
  433. else
  434. $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? '&nbsp;'.$i : $i)."</a>";
  435. $o .= '</span> ';
  436. }
  437. if(($a->pager['total'] % $a->pager['itemspage']) != 0) {
  438. if($i == $a->pager['page'])
  439. $o .= '<span class="pager_current">'.(($i < 10) ? '&nbsp;'.$i : $i);
  440. else
  441. $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? '&nbsp;'.$i : $i)."</a>";
  442. $o .= '</span> ';
  443. }
  444. $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
  445. $o .= "<span class=\"pager_last\"><a href=\"$url"."&page=$lastpage\">" . t('last') . "</a></span> ";
  446. if(($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0)
  447. $o .= '<span class="pager_next">'."<a href=\"$url"."&page=".($a->pager['page'] + 1).'">' . t('next') . '</a></span>';
  448. $o .= '</div>'."\r\n";
  449. }
  450. return $o;
  451. }}
  452. if(! function_exists('expand_acl')) {
  453. function expand_acl($s) {
  454. if(strlen($s)) {
  455. $a = explode('<',$s);
  456. for($x = 0; $x < count($a); $x ++) {
  457. $a[$x] = intval(str_replace(array('<','>'),array('',''),$a[$x]));
  458. }
  459. return $a;
  460. }
  461. return array();
  462. }}
  463. if(! function_exists('sanitise_acl')) {
  464. function sanitise_acl(&$item) {
  465. if(intval($item))
  466. $item = '<' . intval(notags(trim($item))) . '>';
  467. else
  468. unset($item);
  469. }}
  470. if(! function_exists('load_config')) {
  471. function load_config($family) {
  472. global $a;
  473. $r = q("SELECT * FROM `config` WHERE `cat` = '%s'",
  474. dbesc($family)
  475. );
  476. if(count($r)) {
  477. foreach($r as $rr) {
  478. $k = $rr['k'];
  479. $a->config[$family][$k] = $rr['v'];
  480. }
  481. }
  482. }}
  483. if(! function_exists('get_config')) {
  484. function get_config($family, $key, $instore = false) {
  485. global $a;
  486. if(! $instore) {
  487. if(isset($a->config[$family][$key])) {
  488. if($a->config[$family][$key] === '!<unset>!') {
  489. return false;
  490. }
  491. return $a->config[$family][$key];
  492. }
  493. }
  494. $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
  495. dbesc($family),
  496. dbesc($key)
  497. );
  498. if(count($ret)) {
  499. $a->config[$family][$key] = $ret[0]['v'];
  500. return $ret[0]['v'];
  501. }
  502. else {
  503. $a->config[$family][$key] = '!<unset>!';
  504. }
  505. return false;
  506. }}
  507. if(! function_exists('set_config')) {
  508. function set_config($family,$key,$value) {
  509. global $a;
  510. $a->config[$family][$key] = $value;
  511. if(get_config($family,$key,true) === false) {
  512. $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
  513. dbesc($family),
  514. dbesc($key),
  515. dbesc($value)
  516. );
  517. if($ret)
  518. return $value;
  519. return $ret;
  520. }
  521. $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
  522. dbesc($value),
  523. dbesc($family),
  524. dbesc($key)
  525. );
  526. if($ret)
  527. return $value;
  528. return $ret;
  529. }}
  530. if(! function_exists('convert_xml_element_to_array')) {
  531. function convert_xml_element_to_array($xml_element, &$recursion_depth=0) {
  532. // If we're getting too deep, bail out
  533. if ($recursion_depth > 512) {
  534. return(null);
  535. }
  536. if (!is_string($xml_element) &&
  537. !is_array($xml_element) &&
  538. (get_class($xml_element) == 'SimpleXMLElement')) {
  539. $xml_element_copy = $xml_element;
  540. $xml_element = get_object_vars($xml_element);
  541. }
  542. if (is_array($xml_element)) {
  543. $result_array = array();
  544. if (count($xml_element) <= 0) {
  545. return (trim(strval($xml_element_copy)));
  546. }
  547. foreach($xml_element as $key=>$value) {
  548. $recursion_depth++;
  549. $result_array[strtolower($key)] =
  550. convert_xml_element_to_array($value, $recursion_depth);
  551. $recursion_depth--;
  552. }
  553. if ($recursion_depth == 0) {
  554. $temp_array = $result_array;
  555. $result_array = array(
  556. strtolower($xml_element_copy->getName()) => $temp_array,
  557. );
  558. }
  559. return ($result_array);
  560. } else {
  561. return (trim(strval($xml_element)));
  562. }
  563. }}
  564. if(! function_exists('webfinger')) {
  565. function webfinger($s) {
  566. if(! strstr($s,'@')) {
  567. return $s;
  568. }
  569. $host = substr($s,strpos($s,'@') + 1);
  570. $url = 'http://' . $host . '/.well-known/host-meta' ;
  571. $xml = fetch_url($url);
  572. if (! $xml)
  573. return '';
  574. $h = simplexml_load_string($xml);
  575. $arr = convert_xml_element_to_array($h);
  576. if(! isset($arr['xrd']['link']))
  577. return '';
  578. $link = $arr['xrd']['link'];
  579. if(! isset($link[0]))
  580. $links = array($link);
  581. else
  582. $links = $link;
  583. foreach($links as $link)
  584. if($link['@attributes']['rel'] && $link['@attributes']['rel'] === 'lrdd')
  585. $tpl = $link['@attributes']['template'];
  586. if((empty($tpl)) || (! strpos($tpl, '{uri}')))
  587. return '';
  588. $pxrd = str_replace('{uri}', urlencode('acct://'.$s), $tpl);
  589. $xml = fetch_url($pxrd);
  590. if (! $xml)
  591. return '';
  592. $h = simplexml_load_string($xml);
  593. $arr = convert_xml_element_to_array($h);
  594. if(! isset($arr['xrd']['link']))
  595. return '';
  596. $link = $arr['xrd']['link'];
  597. if(! isset($link[0]))
  598. $links = array($link);
  599. else
  600. $links = $link;
  601. foreach($links as $link)
  602. if($link['@attributes']['rel'] == NAMESPACE_DFRN)
  603. return $link['@attributes']['href'];
  604. return '';
  605. }}
  606. if(! function_exists('perms2str')) {
  607. function perms2str($p) {
  608. $ret = '';
  609. $tmp = $p;
  610. if(is_array($tmp)) {
  611. array_walk($tmp,'sanitise_acl');
  612. $ret = implode('',$tmp);
  613. }
  614. return $ret;
  615. }}
  616. if(! function_exists('item_new_uri')) {
  617. function item_new_uri($hostname,$uid) {
  618. do {
  619. $dups = false;
  620. $hash = random_string();
  621. $uri = "urn:X-dfrn:" . $hostname . ':' . $uid . ':' . $hash;
  622. $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
  623. dbesc($uri));
  624. if(count($r))
  625. $dups = true;
  626. } while($dups == true);
  627. return $uri;
  628. }}
  629. if(! function_exists('photo_new_resource')) {
  630. function photo_new_resource() {
  631. do {
  632. $found = false;
  633. $resource = hash('md5',uniqid(mt_rand(),true));
  634. $r = q("SELECT `id` FROM `photo` WHERE `resource-id` = '%s' LIMIT 1",
  635. dbesc($resource)
  636. );
  637. if(count($r))
  638. $found = true;
  639. } while($found == true);
  640. return $resource;
  641. }}
  642. if(! function_exists('get_uid')) {
  643. function get_uid() {
  644. return ((x($_SESSION,'uid')) ? intval($_SESSION['uid']) : 0) ;
  645. }}
  646. if(! function_exists('validate_url')) {
  647. function validate_url(&$url) {
  648. if(substr($url,0,4) != 'http')
  649. $url = 'http://' . $url;
  650. $h = parse_url($url);
  651. if(! $h) {
  652. return false;
  653. }
  654. if(! checkdnsrr($h['host'], 'ANY')) {
  655. return false;
  656. }
  657. return true;
  658. }}
  659. if(! function_exists('allowed_url')) {
  660. function allowed_url($url) {
  661. $h = parse_url($url);
  662. if(! $h) {
  663. return false;
  664. }
  665. $str_allowed = get_config('system','allowed_sites');
  666. if(! $str_allowed)
  667. return true;
  668. $found = false;
  669. $host = strtolower($h['host']);
  670. // always allow our own site
  671. if($host == strtolower($_SERVER['SERVER_NAME']))
  672. return true;
  673. $fnmatch = function_exists('fnmatch');
  674. $allowed = explode(',',$str_allowed);
  675. if(count($allowed)) {
  676. foreach($allowed as $a) {
  677. $pat = strtolower(trim($a));
  678. if(($fnmatch && fnmatch($pat,$host)) || ($pat == $host)) {
  679. $found = true;
  680. break;
  681. }
  682. }
  683. }
  684. return $found;
  685. }}
  686. if(! function_exists('allowed_email')) {
  687. function allowed_email($email) {
  688. $domain = strtolower(substr($email,strpos($email,'@') + 1));
  689. if(! $domain)
  690. return false;
  691. $str_allowed = get_config('system','allowed_email');
  692. if(! $str_allowed)
  693. return true;
  694. $found = false;
  695. $fnmatch = function_exists('fnmatch');
  696. $allowed = explode(',',$str_allowed);
  697. if(count($allowed)) {
  698. foreach($allowed as $a) {
  699. $pat = strtolower(trim($a));
  700. if(($fnmatch && fnmatch($pat,$host)) || ($pat == $host)) {
  701. $found = true;
  702. break;
  703. }
  704. }
  705. }
  706. return $found;
  707. }}
  708. if(! function_exists('format_like')) {
  709. function format_like($cnt,$arr,$type,$id) {
  710. if($cnt == 1)
  711. $o .= $arr[0] . (($type === 'like') ? t(' likes this.') : t(' doesn\'t like this.')) . EOL ;
  712. else {
  713. $o .= '<span class="fakelink" onclick="openClose(\'' . $type . 'list-' . $id . '\');" >'
  714. . $cnt . ' ' . t('people') . '</span> ' . (($type === 'like') ? t('like this.') : t('don\'t like this.')) . EOL ;
  715. $total = count($arr);
  716. if($total >= 75)
  717. $arr = array_slice($arr,0,74);
  718. if($total < 75)
  719. $arr[count($arr)-1] = t('and') . ' ' . $arr[count($arr)-1];
  720. $str = implode(', ', $arr);
  721. if($total >= 75)
  722. $str .= t(', and ') . $total - 75 . t(' other people');
  723. $str .= (($type === 'like') ? t(' like this.') : t(' don\'t like this.'));
  724. $o .= '<div id="' . $type . 'list-' . $id . '" style="display: none;" >' . $str . '</div>';
  725. }
  726. return $o;
  727. }}
  728. if(! function_exists('load_view_file')) {
  729. function load_view_file($s) {
  730. $b = basename($s);
  731. $d = dirname($s);
  732. $lang = get_config('system','language');
  733. if($lang && file_exists("$d/$lang/$b"))
  734. return file_get_contents("$d/$lang/$b");
  735. return file_get_contents($s);
  736. }}