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.
 
 
 
 
 
 

2883 lines
77 KiB

  1. <?php
  2. set_time_limit(0);
  3. ini_set('pcre.backtrack_limit', 250000);
  4. define ( 'FRIENDIKA_VERSION', '2.2.1010' );
  5. define ( 'DFRN_PROTOCOL_VERSION', '2.21' );
  6. define ( 'DB_UPDATE_VERSION', 1063 );
  7. define ( 'EOL', "<br />\r\n" );
  8. define ( 'ATOM_TIME', 'Y-m-d\TH:i:s\Z' );
  9. define ( 'DOWN_ARROW', '&#x21e9;' );
  10. /**
  11. *
  12. * Image storage quality. Lower numbers save space at cost of image detail.
  13. * For ease of upgrade, please do not change here. Change jpeg quality with
  14. * set_config('system','jpeg_quality',n) in .htconfig.php
  15. * where n is netween 1 and 100, and with very poor results below about 50
  16. *
  17. */
  18. define ( 'JPEG_QUALITY', 100 );
  19. /**
  20. * SSL redirection policies
  21. */
  22. define ( 'SSL_POLICY_NONE', 0 );
  23. define ( 'SSL_POLICY_FULL', 1 );
  24. define ( 'SSL_POLICY_SELFSIGN', 2 );
  25. /**
  26. * log levels
  27. */
  28. define ( 'LOGGER_NORMAL', 0 );
  29. define ( 'LOGGER_TRACE', 1 );
  30. define ( 'LOGGER_DEBUG', 2 );
  31. define ( 'LOGGER_DATA', 3 );
  32. define ( 'LOGGER_ALL', 4 );
  33. /**
  34. * registration policies
  35. */
  36. define ( 'REGISTER_CLOSED', 0 );
  37. define ( 'REGISTER_APPROVE', 1 );
  38. define ( 'REGISTER_OPEN', 2 );
  39. /**
  40. * relationship types
  41. * When used in contact records, this indicates that 'uid' has
  42. * this relationship with contact['name']
  43. */
  44. define ( 'REL_VIP', 1);
  45. define ( 'REL_FAN', 2);
  46. define ( 'REL_BUD', 3);
  47. /**
  48. * Hook array order
  49. */
  50. define ( 'HOOK_HOOK', 0);
  51. define ( 'HOOK_FILE', 1);
  52. define ( 'HOOK_FUNCTION', 2);
  53. /**
  54. *
  55. * page/profile types
  56. *
  57. * PAGE_NORMAL is a typical personal profile account
  58. * PAGE_SOAPBOX automatically approves all friend requests as REL_FAN, (readonly)
  59. * PAGE_COMMUNITY automatically approves all friend requests as REL_FAN, but with
  60. * write access to wall and comments (no email and not included in page owner's ACL lists)
  61. * PAGE_FREELOVE automatically approves all friend requests as full friends (REL_BUD).
  62. *
  63. */
  64. define ( 'PAGE_NORMAL', 0 );
  65. define ( 'PAGE_SOAPBOX', 1 );
  66. define ( 'PAGE_COMMUNITY', 2 );
  67. define ( 'PAGE_FREELOVE', 3 );
  68. /**
  69. * Network and protocol family types
  70. */
  71. define ( 'NETWORK_DFRN', 'dfrn'); // Friendika, Mistpark, other DFRN implementations
  72. define ( 'NETWORK_OSTATUS', 'stat'); // status.net, identi.ca, GNU-social, other OStatus implementations
  73. define ( 'NETWORK_FEED', 'feed'); // RSS/Atom feeds with no known "post/notify" protocol
  74. define ( 'NETWORK_DIASPORA', 'dspr'); // Diaspora
  75. define ( 'NETWORK_MAIL', 'mail'); // IMAP/POP
  76. define ( 'NETWORK_FACEBOOK', 'face'); // Facebook API
  77. /**
  78. * Maximum number of "people who like (or don't like) this" that we will list by name
  79. */
  80. define ( 'MAX_LIKERS', 75);
  81. /**
  82. * email notification options
  83. */
  84. define ( 'NOTIFY_INTRO', 0x0001 );
  85. define ( 'NOTIFY_CONFIRM', 0x0002 );
  86. define ( 'NOTIFY_WALL', 0x0004 );
  87. define ( 'NOTIFY_COMMENT', 0x0008 );
  88. define ( 'NOTIFY_MAIL', 0x0010 );
  89. /**
  90. * various namespaces we may need to parse
  91. */
  92. define ( 'NAMESPACE_DFRN' , 'http://purl.org/macgirvin/dfrn/1.0' );
  93. define ( 'NAMESPACE_THREAD' , 'http://purl.org/syndication/thread/1.0' );
  94. define ( 'NAMESPACE_TOMB' , 'http://purl.org/atompub/tombstones/1.0' );
  95. define ( 'NAMESPACE_ACTIVITY', 'http://activitystrea.ms/spec/1.0/' );
  96. define ( 'NAMESPACE_ACTIVITY_SCHEMA', 'http://activitystrea.ms/schema/1.0/' );
  97. define ( 'NAMESPACE_MEDIA', 'http://purl.org/syndication/atommedia' );
  98. define ( 'NAMESPACE_SALMON_ME', 'http://salmon-protocol.org/ns/magic-env' );
  99. define ( 'NAMESPACE_OSTATUSSUB', 'http://ostatus.org/schema/1.0/subscribe' );
  100. define ( 'NAMESPACE_GEORSS', 'http://www.georss.org/georss' );
  101. define ( 'NAMESPACE_POCO', 'http://portablecontacts.net/spec/1.0' );
  102. define ( 'NAMESPACE_FEED', 'http://schemas.google.com/g/2010#updates-from' );
  103. /**
  104. * activity stream defines
  105. */
  106. define ( 'ACTIVITY_LIKE', NAMESPACE_ACTIVITY_SCHEMA . 'like' );
  107. define ( 'ACTIVITY_DISLIKE', NAMESPACE_DFRN . '/dislike' );
  108. define ( 'ACTIVITY_OBJ_HEART', NAMESPACE_DFRN . '/heart' );
  109. define ( 'ACTIVITY_FRIEND', NAMESPACE_ACTIVITY_SCHEMA . 'make-friend' );
  110. define ( 'ACTIVITY_FOLLOW', NAMESPACE_ACTIVITY_SCHEMA . 'follow' );
  111. define ( 'ACTIVITY_UNFOLLOW', NAMESPACE_ACTIVITY_SCHEMA . 'stop-following' );
  112. define ( 'ACTIVITY_POST', NAMESPACE_ACTIVITY_SCHEMA . 'post' );
  113. define ( 'ACTIVITY_UPDATE', NAMESPACE_ACTIVITY_SCHEMA . 'update' );
  114. define ( 'ACTIVITY_TAG', NAMESPACE_ACTIVITY_SCHEMA . 'tag' );
  115. define ( 'ACTIVITY_OBJ_COMMENT', NAMESPACE_ACTIVITY_SCHEMA . 'comment' );
  116. define ( 'ACTIVITY_OBJ_NOTE', NAMESPACE_ACTIVITY_SCHEMA . 'note' );
  117. define ( 'ACTIVITY_OBJ_PERSON', NAMESPACE_ACTIVITY_SCHEMA . 'person' );
  118. define ( 'ACTIVITY_OBJ_PHOTO', NAMESPACE_ACTIVITY_SCHEMA . 'photo' );
  119. define ( 'ACTIVITY_OBJ_P_PHOTO', NAMESPACE_ACTIVITY_SCHEMA . 'profile-photo' );
  120. define ( 'ACTIVITY_OBJ_ALBUM', NAMESPACE_ACTIVITY_SCHEMA . 'photo-album' );
  121. define ( 'ACTIVITY_OBJ_EVENT', NAMESPACE_ACTIVITY_SCHEMA . 'event' );
  122. /**
  123. * item weight for query ordering
  124. */
  125. define ( 'GRAVITY_PARENT', 0);
  126. define ( 'GRAVITY_LIKE', 3);
  127. define ( 'GRAVITY_COMMENT', 6);
  128. /**
  129. *
  130. * Reverse the effect of magic_quotes_gpc if it is enabled.
  131. * Please disable magic_quotes_gpc so we don't have to do this.
  132. * See http://php.net/manual/en/security.magicquotes.disabling.php
  133. *
  134. */
  135. if (get_magic_quotes_gpc()) {
  136. $process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
  137. while (list($key, $val) = each($process)) {
  138. foreach ($val as $k => $v) {
  139. unset($process[$key][$k]);
  140. if (is_array($v)) {
  141. $process[$key][stripslashes($k)] = $v;
  142. $process[] = &$process[$key][stripslashes($k)];
  143. } else {
  144. $process[$key][stripslashes($k)] = stripslashes($v);
  145. }
  146. }
  147. }
  148. unset($process);
  149. }
  150. /*
  151. * translation system
  152. */
  153. require_once("include/pgettext.php");
  154. /**
  155. *
  156. * class: App
  157. *
  158. * Our main application structure for the life of this page
  159. * Primarily deals with the URL that got us here
  160. * and tries to make some sense of it, and
  161. * stores our page contents and config storage
  162. * and anything else that might need to be passed around
  163. * before we spit the page out.
  164. *
  165. */
  166. if(! class_exists('App')) {
  167. class App {
  168. public $module_loaded = false;
  169. public $query_string;
  170. public $config;
  171. public $page;
  172. public $profile;
  173. public $user;
  174. public $cid;
  175. public $contact;
  176. public $contacts;
  177. public $page_contact;
  178. public $content;
  179. public $data;
  180. public $error = false;
  181. public $cmd;
  182. public $argv;
  183. public $argc;
  184. public $module;
  185. public $pager;
  186. public $strings;
  187. public $path;
  188. public $hooks;
  189. public $timezone;
  190. public $interactive = true;
  191. public $plugins;
  192. public $apps;
  193. public $identities;
  194. private $scheme;
  195. private $hostname;
  196. private $baseurl;
  197. private $db;
  198. private $curl_code;
  199. private $curl_headers;
  200. function __construct() {
  201. $this->config = array();
  202. $this->page = array();
  203. $this->pager= array();
  204. $this->query_string = '';
  205. $this->scheme = ((isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'])) ? 'https' : 'http' );
  206. if(x($_SERVER,'SERVER_NAME')) {
  207. $this->hostname = $_SERVER['SERVER_NAME'];
  208. /**
  209. * Figure out if we are running at the top of a domain
  210. * or in a sub-directory and adjust accordingly
  211. */
  212. $path = trim(dirname($_SERVER['SCRIPT_NAME']),'/\\');
  213. if(isset($path) && strlen($path) && ($path != $this->path))
  214. $this->path = $path;
  215. }
  216. set_include_path("include/$this->hostname" . PATH_SEPARATOR . 'include' . PATH_SEPARATOR . '.' );
  217. if((x($_SERVER,'QUERY_STRING')) && substr($_SERVER['QUERY_STRING'],0,2) === "q=")
  218. $this->query_string = substr($_SERVER['QUERY_STRING'],2);
  219. if(x($_GET,'q'))
  220. $this->cmd = trim($_GET['q'],'/\\');
  221. /**
  222. *
  223. * Break the URL path into C style argc/argv style arguments for our
  224. * modules. Given "http://example.com/module/arg1/arg2", $this->argc
  225. * will be 3 (integer) and $this->argv will contain:
  226. * [0] => 'module'
  227. * [1] => 'arg1'
  228. * [2] => 'arg2'
  229. *
  230. *
  231. * There will always be one argument. If provided a naked domain
  232. * URL, $this->argv[0] is set to "home".
  233. *
  234. */
  235. $this->argv = explode('/',$this->cmd);
  236. $this->argc = count($this->argv);
  237. if((array_key_exists('0',$this->argv)) && strlen($this->argv[0])) {
  238. $this->module = str_replace(".", "_", $this->argv[0]);
  239. }
  240. else {
  241. $this->module = 'home';
  242. }
  243. /**
  244. * Special handling for the webfinger/lrdd host XRD file
  245. * Just spit out the contents and exit.
  246. */
  247. if($this->cmd === '.well-known/host-meta') {
  248. require_once('include/hostxrd.php');
  249. hostxrd($this->get_baseurl());
  250. // NOTREACHED
  251. }
  252. /**
  253. * See if there is any page number information, and initialise
  254. * pagination
  255. */
  256. $this->pager['page'] = ((x($_GET,'page')) ? $_GET['page'] : 1);
  257. $this->pager['itemspage'] = 50;
  258. $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
  259. $this->pager['total'] = 0;
  260. }
  261. function get_baseurl($ssl = false) {
  262. $scheme = $this->scheme;
  263. if(x($this->config,'ssl_policy')) {
  264. if(($ssl) || ($this->config['ssl_policy'] == SSL_POLICY_FULL))
  265. $scheme = 'https';
  266. if(($this->config['ssl_policy'] == SSL_POLICY_SELFSIGN) && (local_user() || x($_POST,'auth-params')))
  267. $scheme = 'https';
  268. }
  269. $this->baseurl = $scheme . "://" . $this->hostname . ((isset($this->path) && strlen($this->path)) ? '/' . $this->path : '' );
  270. return $this->baseurl;
  271. }
  272. function set_baseurl($url) {
  273. $parsed = @parse_url($url);
  274. $this->baseurl = $url;
  275. if($parsed) {
  276. $this->scheme = $parsed['scheme'];
  277. $this->hostname = $parsed['host'];
  278. if(x($parsed,'port'))
  279. $this->hostname .= ':' . $parsed['port'];
  280. if(x($parsed,'path'))
  281. $this->path = trim($parsed['path'],'\\/');
  282. }
  283. }
  284. function get_hostname() {
  285. return $this->hostname;
  286. }
  287. function set_hostname($h) {
  288. $this->hostname = $h;
  289. }
  290. function set_path($p) {
  291. $this->path = trim(trim($p),'/');
  292. }
  293. function get_path() {
  294. return $this->path;
  295. }
  296. function set_pager_total($n) {
  297. $this->pager['total'] = intval($n);
  298. }
  299. function set_pager_itemspage($n) {
  300. $this->pager['itemspage'] = intval($n);
  301. $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
  302. }
  303. function init_pagehead() {
  304. $this->page['title'] = $this->config['sitename'];
  305. $tpl = file_get_contents('view/head.tpl');
  306. $this->page['htmlhead'] = replace_macros($tpl,array(
  307. '$baseurl' => $this->get_baseurl(),
  308. '$generator' => 'Friendika' . ' ' . FRIENDIKA_VERSION,
  309. '$delitem' => t('Delete this item?'),
  310. '$comment' => t('Comment')
  311. ));
  312. }
  313. function set_curl_code($code) {
  314. $this->curl_code = $code;
  315. }
  316. function get_curl_code() {
  317. return $this->curl_code;
  318. }
  319. function set_curl_headers($headers) {
  320. $this->curl_headers = $headers;
  321. }
  322. function get_curl_headers() {
  323. return $this->curl_headers;
  324. }
  325. }}
  326. // retrieve the App structure
  327. // useful in functions which require it but don't get it passed to them
  328. if(! function_exists('get_app')) {
  329. function get_app() {
  330. global $a;
  331. return $a;
  332. }};
  333. // Multi-purpose function to check variable state.
  334. // Usage: x($var) or $x($array,'key')
  335. // returns false if variable/key is not set
  336. // if variable is set, returns 1 if has 'non-zero' value, otherwise returns 0.
  337. // e.g. x('') or x(0) returns 0;
  338. if(! function_exists('x')) {
  339. function x($s,$k = NULL) {
  340. if($k != NULL) {
  341. if((is_array($s)) && (array_key_exists($k,$s))) {
  342. if($s[$k])
  343. return (int) 1;
  344. return (int) 0;
  345. }
  346. return false;
  347. }
  348. else {
  349. if(isset($s)) {
  350. if($s) {
  351. return (int) 1;
  352. }
  353. return (int) 0;
  354. }
  355. return false;
  356. }
  357. }}
  358. // called from db initialisation if db is dead.
  359. if(! function_exists('system_unavailable')) {
  360. function system_unavailable() {
  361. include('system_unavailable.php');
  362. system_down();
  363. killme();
  364. }}
  365. // install and uninstall plugin
  366. if (! function_exists('uninstall_plugin')){
  367. function uninstall_plugin($plugin){
  368. logger("Addons: uninstalling " . $plugin);
  369. q("DELETE FROM `addon` WHERE `name` = '%s' LIMIT 1",
  370. dbesc($plugin)
  371. );
  372. @include_once('addon/' . $plugin . '/' . $plugin . '.php');
  373. if(function_exists($plugin . '_uninstall')) {
  374. $func = $plugin . '_uninstall';
  375. $func();
  376. }
  377. }}
  378. if (! function_exists('install_plugin')){
  379. function install_plugin($plugin){
  380. logger("Addons: installing " . $plugin);
  381. $t = filemtime('addon/' . $plugin . '/' . $plugin . '.php');
  382. @include_once('addon/' . $plugin . '/' . $plugin . '.php');
  383. if(function_exists($plugin . '_install')) {
  384. $func = $plugin . '_install';
  385. $func();
  386. $r = q("INSERT INTO `addon` (`name`, `installed`, `timestamp`) VALUES ( '%s', 1, %d ) ",
  387. dbesc($plugin),
  388. intval($t)
  389. );
  390. }
  391. }}
  392. // Primarily involved with database upgrade, but also sets the
  393. // base url for use in cmdline programs which don't have
  394. // $_SERVER variables, and synchronising the state of installed plugins.
  395. if(! function_exists('check_config')) {
  396. function check_config(&$a) {
  397. load_config('system');
  398. $build = get_config('system','build');
  399. if(! x($build))
  400. $build = set_config('system','build',DB_UPDATE_VERSION);
  401. $url = get_config('system','url');
  402. // if the url isn't set or the stored url is radically different
  403. // than the currently visited url, store the current value accordingly.
  404. // "Radically different" ignores common variations such as http vs https
  405. // and www.example.com vs example.com.
  406. if((! x($url)) || (! link_compare($url,$a->get_baseurl())))
  407. $url = set_config('system','url',$a->get_baseurl());
  408. if($build != DB_UPDATE_VERSION) {
  409. $stored = intval($build);
  410. $current = intval(DB_UPDATE_VERSION);
  411. if(($stored < $current) && file_exists('update.php')) {
  412. // We're reporting a different version than what is currently installed.
  413. // Run any existing update scripts to bring the database up to current.
  414. require_once('update.php');
  415. // make sure that boot.php and update.php are the same release, we might be
  416. // updating right this very second and the correct version of the update.php
  417. // file may not be here yet. This can happen on a very busy site.
  418. if(DB_UPDATE_VERSION == UPDATE_VERSION) {
  419. for($x = $stored; $x < $current; $x ++) {
  420. if(function_exists('update_' . $x)) {
  421. $func = 'update_' . $x;
  422. $func($a);
  423. }
  424. }
  425. set_config('system','build', DB_UPDATE_VERSION);
  426. }
  427. }
  428. }
  429. /**
  430. *
  431. * Synchronise plugins:
  432. *
  433. * $a->config['system']['addon'] contains a comma-separated list of names
  434. * of plugins/addons which are used on this system.
  435. * Go through the database list of already installed addons, and if we have
  436. * an entry, but it isn't in the config list, call the uninstall procedure
  437. * and mark it uninstalled in the database (for now we'll remove it).
  438. * Then go through the config list and if we have a plugin that isn't installed,
  439. * call the install procedure and add it to the database.
  440. *
  441. */
  442. $r = q("SELECT * FROM `addon` WHERE `installed` = 1");
  443. if(count($r))
  444. $installed = $r;
  445. else
  446. $installed = array();
  447. $plugins = get_config('system','addon');
  448. $plugins_arr = array();
  449. if($plugins)
  450. $plugins_arr = explode(',',str_replace(' ', '',$plugins));
  451. $a->plugins = $plugins_arr;
  452. $installed_arr = array();
  453. if(count($installed)) {
  454. foreach($installed as $i) {
  455. if(! in_array($i['name'],$plugins_arr)) {
  456. uninstall_plugin($i['name']);
  457. }
  458. else
  459. $installed_arr[] = $i['name'];
  460. }
  461. }
  462. if(count($plugins_arr)) {
  463. foreach($plugins_arr as $p) {
  464. if(! in_array($p,$installed_arr)) {
  465. install_plugin($p);
  466. }
  467. }
  468. }
  469. load_hooks();
  470. return;
  471. }}
  472. // reload all updated plugins
  473. if(! function_exists('reload_plugins')) {
  474. function reload_plugins() {
  475. $plugins = get_config('system','addon');
  476. if(strlen($plugins)) {
  477. $r = q("SELECT * FROM `addon` WHERE `installed` = 1");
  478. if(count($r))
  479. $installed = $r;
  480. else
  481. $installed = array();
  482. $parr = explode(',',$plugins);
  483. if(count($parr)) {
  484. foreach($parr as $pl) {
  485. $pl = trim($pl);
  486. $t = filemtime('addon/' . $pl . '/' . $pl . '.php');
  487. foreach($installed as $i) {
  488. if(($i['name'] == $pl) && ($i['timestamp'] != $t)) {
  489. logger('Reloading plugin: ' . $i['name']);
  490. @include_once('addon/' . $pl . '/' . $pl . '.php');
  491. if(function_exists($pl . '_uninstall')) {
  492. $func = $pl . '_uninstall';
  493. $func();
  494. }
  495. if(function_exists($pl . '_install')) {
  496. $func = $pl . '_install';
  497. $func();
  498. }
  499. q("UPDATE `addon` SET `timestamp` = %d WHERE `id` = %d LIMIT 1",
  500. intval($t),
  501. intval($i['id'])
  502. );
  503. }
  504. }
  505. }
  506. }
  507. }
  508. }}
  509. // This is our template processor.
  510. // $s is the string requiring macro substitution.
  511. // $r is an array of key value pairs (search => replace)
  512. // returns substituted string.
  513. // WARNING: this is pretty basic, and doesn't properly handle search strings that are substrings of each other.
  514. // For instance if 'test' => "foo" and 'testing' => "bar", testing could become either bar or fooing,
  515. // depending on the order in which they were declared in the array.
  516. require_once("include/template_processor.php");
  517. if(! function_exists('replace_macros')) {
  518. function replace_macros($s,$r) {
  519. global $t;
  520. return $t->replace($s,$r);
  521. }}
  522. // curl wrapper. If binary flag is true, return binary
  523. // results.
  524. if(! function_exists('fetch_url')) {
  525. function fetch_url($url,$binary = false, &$redirects = 0) {
  526. $a = get_app();
  527. $ch = curl_init($url);
  528. if(($redirects > 8) || (! $ch))
  529. return false;
  530. curl_setopt($ch, CURLOPT_HEADER, true);
  531. curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
  532. $curl_time = intval(get_config('system','curl_timeout'));
  533. curl_setopt($ch, CURLOPT_TIMEOUT, (($curl_time !== false) ? $curl_time : 60));
  534. // by default we will allow self-signed certs
  535. // but you can override this
  536. $check_cert = get_config('system','verifyssl');
  537. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (($check_cert) ? true : false));
  538. $prx = get_config('system','proxy');
  539. if(strlen($prx)) {
  540. curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
  541. curl_setopt($ch, CURLOPT_PROXY, $prx);
  542. $prxusr = get_config('system','proxyuser');
  543. if(strlen($prxusr))
  544. curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
  545. }
  546. if($binary)
  547. curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
  548. $a->set_curl_code(0);
  549. // don't let curl abort the entire application
  550. // if it throws any errors.
  551. $s = @curl_exec($ch);
  552. $http_code = intval(curl_getinfo($ch, CURLINFO_HTTP_CODE));
  553. $header = substr($s,0,strpos($s,"\r\n\r\n"));
  554. if(stristr($header,'100') && (strlen($header) < 30)) {
  555. // 100 Continue has two headers, get the real one
  556. $s = substr($s,strlen($header)+4);
  557. $header = substr($s,0,strpos($s,"\r\n\r\n"));
  558. }
  559. if($http_code == 301 || $http_code == 302 || $http_code == 303 || $http_code == 307) {
  560. $matches = array();
  561. preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
  562. $url = trim(array_pop($matches));
  563. $url_parsed = @parse_url($url);
  564. if (isset($url_parsed)) {
  565. $redirects++;
  566. return fetch_url($url,$binary,$redirects);
  567. }
  568. }
  569. $a->set_curl_code($http_code);
  570. $body = substr($s,strlen($header)+4);
  571. /* one more try to make sure there are no more headers */
  572. if(strpos($body,'HTTP/') === 0) {
  573. $header = substr($body,0,strpos($body,"\r\n\r\n"));
  574. $body = substr($body,strlen($header)+4);
  575. }
  576. $a->set_curl_headers($header);
  577. curl_close($ch);
  578. return($body);
  579. }}
  580. // post request to $url. $params is an array of post variables.
  581. if(! function_exists('post_url')) {
  582. function post_url($url,$params, $headers = null, &$redirects = 0) {
  583. $a = get_app();
  584. $ch = curl_init($url);
  585. if(($redirects > 8) || (! $ch))
  586. return false;
  587. curl_setopt($ch, CURLOPT_HEADER, true);
  588. curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
  589. curl_setopt($ch, CURLOPT_POST,1);
  590. curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
  591. $curl_time = intval(get_config('system','curl_timeout'));
  592. curl_setopt($ch, CURLOPT_TIMEOUT, (($curl_time !== false) ? $curl_time : 60));
  593. if(is_array($headers))
  594. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  595. $check_cert = get_config('system','verifyssl');
  596. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (($check_cert) ? true : false));
  597. $prx = get_config('system','proxy');
  598. if(strlen($prx)) {
  599. curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
  600. curl_setopt($ch, CURLOPT_PROXY, $prx);
  601. $prxusr = get_config('system','proxyuser');
  602. if(strlen($prxusr))
  603. curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
  604. }
  605. $a->set_curl_code(0);
  606. // don't let curl abort the entire application
  607. // if it throws any errors.
  608. $s = @curl_exec($ch);
  609. $http_code = intval(curl_getinfo($ch, CURLINFO_HTTP_CODE));
  610. $header = substr($s,0,strpos($s,"\r\n\r\n"));
  611. if(stristr($header,'100') && (strlen($header) < 30)) {
  612. // 100 Continue has two headers, get the real one
  613. $s = substr($s,strlen($header)+4);
  614. $header = substr($s,0,strpos($s,"\r\n\r\n"));
  615. }
  616. if($http_code == 301 || $http_code == 302 || $http_code == 303) {
  617. $matches = array();
  618. preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
  619. $url = trim(array_pop($matches));
  620. $url_parsed = @parse_url($url);
  621. if (isset($url_parsed)) {
  622. $redirects++;
  623. return post_url($url,$binary,$headers,$redirects);
  624. }
  625. }
  626. $a->set_curl_code($http_code);
  627. $body = substr($s,strlen($header)+4);
  628. /* one more try to make sure there are no more headers */
  629. if(strpos($body,'HTTP/') === 0) {
  630. $header = substr($body,0,strpos($body,"\r\n\r\n"));
  631. $body = substr($body,strlen($header)+4);
  632. }
  633. $a->set_curl_headers($header);
  634. curl_close($ch);
  635. return($body);
  636. }}
  637. // random hash, 64 chars
  638. if(! function_exists('random_string')) {
  639. function random_string() {
  640. return(hash('sha256',uniqid(rand(),true)));
  641. }}
  642. /**
  643. * This is our primary input filter.
  644. *
  645. * The high bit hack only involved some old IE browser, forget which (IE5/Mac?)
  646. * that had an XSS attack vector due to stripping the high-bit on an 8-bit character
  647. * after cleansing, and angle chars with the high bit set could get through as markup.
  648. *
  649. * This is now disabled because it was interfering with some legitimate unicode sequences
  650. * and hopefully there aren't a lot of those browsers left.
  651. *
  652. * Use this on any text input where angle chars are not valid or permitted
  653. * They will be replaced with safer brackets. This may be filtered further
  654. * if these are not allowed either.
  655. *
  656. */
  657. if(! function_exists('notags')) {
  658. function notags($string) {
  659. return(str_replace(array("<",">"), array('[',']'), $string));
  660. // High-bit filter no longer used
  661. // return(str_replace(array("<",">","\xBA","\xBC","\xBE"), array('[',']','','',''), $string));
  662. }}
  663. // use this on "body" or "content" input where angle chars shouldn't be removed,
  664. // and allow them to be safely displayed.
  665. if(! function_exists('escape_tags')) {
  666. function escape_tags($string) {
  667. return(htmlspecialchars($string));
  668. }}
  669. // wrapper for adding a login box. If $register == true provide a registration
  670. // link. This will most always depend on the value of $a->config['register_policy'].
  671. // returns the complete html for inserting into the page
  672. if(! function_exists('login')) {
  673. function login($register = false) {
  674. $o = "";
  675. $register_tpl = (($register) ? get_markup_template("register-link.tpl") : "");
  676. $register_html = replace_macros($register_tpl,array(
  677. '$title' => t('Create a New Account'),
  678. '$desc' => t('Register')
  679. ));
  680. $noid = get_config('system','no_openid');
  681. if($noid) {
  682. $classname = 'no-openid';
  683. $namelabel = t('Nickname or Email address: ');
  684. $passlabel = t('Password: ');
  685. $login = t('Login');
  686. }
  687. else {
  688. $classname = 'openid';
  689. $namelabel = t('Nickname/Email/OpenID: ');
  690. $passlabel = t("Password \x28if not OpenID\x29: ");
  691. $login = t('Login');
  692. }
  693. $lostpass = t('Forgot your password?');
  694. $lostlink = t('Password Reset');
  695. if(local_user()) {
  696. $tpl = get_markup_template("logout.tpl");
  697. }
  698. else {
  699. $tpl = get_markup_template("login.tpl");
  700. }
  701. $o = '<script type="text/javascript"> $(document).ready(function() { $("#login-name").focus();} );</script>';
  702. $o .= replace_macros($tpl,array(
  703. '$logout' => t('Logout'),
  704. '$register_html' => $register_html,
  705. '$classname' => $classname,
  706. '$namelabel' => $namelabel,
  707. '$passlabel' => $passlabel,
  708. '$login' => $login,
  709. '$lostpass' => $lostpass,
  710. '$lostlink' => $lostlink
  711. ));
  712. return $o;
  713. }}
  714. // generate a string that's random, but usually pronounceable.
  715. // used to generate initial passwords
  716. if(! function_exists('autoname')) {
  717. function autoname($len) {
  718. $vowels = array('a','a','ai','au','e','e','e','ee','ea','i','ie','o','ou','u');
  719. if(mt_rand(0,5) == 4)
  720. $vowels[] = 'y';
  721. $cons = array(
  722. 'b','bl','br',
  723. 'c','ch','cl','cr',
  724. 'd','dr',
  725. 'f','fl','fr',
  726. 'g','gh','gl','gr',
  727. 'h',
  728. 'j',
  729. 'k','kh','kl','kr',
  730. 'l',
  731. 'm',
  732. 'n',
  733. 'p','ph','pl','pr',
  734. 'qu',
  735. 'r','rh',
  736. 's','sc','sh','sm','sp','st',
  737. 't','th','tr',
  738. 'v',
  739. 'w','wh',
  740. 'x',
  741. 'z','zh'
  742. );
  743. $midcons = array('ck','ct','gn','ld','lf','lm','lt','mb','mm', 'mn','mp',
  744. 'nd','ng','nk','nt','rn','rp','rt');
  745. $noend = array('bl', 'br', 'cl','cr','dr','fl','fr','gl','gr',
  746. 'kh', 'kl','kr','mn','pl','pr','rh','tr','qu','wh');
  747. $start = mt_rand(0,2);
  748. if($start == 0)
  749. $table = $vowels;
  750. else
  751. $table = $cons;
  752. $word = '';
  753. for ($x = 0; $x < $len; $x ++) {
  754. $r = mt_rand(0,count($table) - 1);
  755. $word .= $table[$r];
  756. if($table == $vowels)
  757. $table = array_merge($cons,$midcons);
  758. else
  759. $table = $vowels;
  760. }
  761. $word = substr($word,0,$len);
  762. foreach($noend as $noe) {
  763. if((strlen($word) > 2) && (substr($word,-2) == $noe)) {
  764. $word = substr($word,0,-1);
  765. break;
  766. }
  767. }
  768. if(substr($word,-1) == 'q')
  769. $word = substr($word,0,-1);
  770. return $word;
  771. }}
  772. // Used to end the current process, after saving session state.
  773. if(! function_exists('killme')) {
  774. function killme() {
  775. session_write_close();
  776. exit;
  777. }}
  778. // redirect to another URL and terminate this process.
  779. if(! function_exists('goaway')) {
  780. function goaway($s) {
  781. header("Location: $s");
  782. killme();
  783. }}
  784. // Generic XML return
  785. // Outputs a basic dfrn XML status structure to STDOUT, with a <status> variable
  786. // of $st and an optional text <message> of $message and terminates the current process.
  787. if(! function_exists('xml_status')) {
  788. function xml_status($st, $message = '') {
  789. $xml_message = ((strlen($message)) ? "\t<message>" . xmlify($message) . "</message>\r\n" : '');
  790. if($st)
  791. logger('xml_status returning non_zero: ' . $st . " message=" . $message);
  792. header( "Content-type: text/xml" );
  793. echo '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
  794. echo "<result>\r\n\t<status>$st</status>\r\n$xml_message</result>\r\n";
  795. killme();
  796. }}
  797. // Returns the uid of locally logged in user or false.
  798. if(! function_exists('local_user')) {
  799. function local_user() {
  800. if((x($_SESSION,'authenticated')) && (x($_SESSION,'uid')))
  801. return intval($_SESSION['uid']);
  802. return false;
  803. }}
  804. // Returns contact id of authenticated site visitor or false
  805. if(! function_exists('remote_user')) {
  806. function remote_user() {
  807. if((x($_SESSION,'authenticated')) && (x($_SESSION,'visitor_id')))
  808. return intval($_SESSION['visitor_id']);
  809. return false;
  810. }}
  811. // contents of $s are displayed prominently on the page the next time
  812. // a page is loaded. Usually used for errors or alerts.
  813. if(! function_exists('notice')) {
  814. function notice($s) {
  815. $a = get_app();
  816. if($a->interactive)
  817. $_SESSION['sysmsg'] .= $s;
  818. }}
  819. if(! function_exists('info')) {
  820. function info($s) {
  821. $a = get_app();
  822. if($a->interactive)
  823. $_SESSION['sysmsg_info'] .= $s;
  824. }}
  825. // wrapper around config to limit the text length of an incoming message
  826. if(! function_exists('get_max_import_size')) {
  827. function get_max_import_size() {
  828. global $a;
  829. return ((x($a->config,'max_import_size')) ? $a->config['max_import_size'] : 0 );
  830. }}
  831. // escape text ($str) for XML transport
  832. // returns escaped text.
  833. if(! function_exists('xmlify')) {
  834. function xmlify($str) {
  835. $buffer = '';
  836. for($x = 0; $x < strlen($str); $x ++) {
  837. $char = $str[$x];
  838. switch( $char ) {
  839. case "\r" :
  840. break;
  841. case "&" :
  842. $buffer .= '&amp;';
  843. break;
  844. case "'" :
  845. $buffer .= '&apos;';
  846. break;
  847. case "\"" :
  848. $buffer .= '&quot;';
  849. break;
  850. case '<' :
  851. $buffer .= '&lt;';
  852. break;
  853. case '>' :
  854. $buffer .= '&gt;';
  855. break;
  856. case "\n" :
  857. $buffer .= "\n";
  858. break;
  859. default :
  860. $buffer .= $char;
  861. break;
  862. }
  863. }
  864. $buffer = trim($buffer);
  865. return($buffer);
  866. }}
  867. // undo an xmlify
  868. // pass xml escaped text ($s), returns unescaped text
  869. if(! function_exists('unxmlify')) {
  870. function unxmlify($s) {
  871. $ret = str_replace('&amp;','&', $s);
  872. $ret = str_replace(array('&lt;','&gt;','&quot;','&apos;'),array('<','>','"',"'"),$ret);
  873. return $ret;
  874. }}
  875. // convenience wrapper, reverse the operation "bin2hex"
  876. if(! function_exists('hex2bin')) {
  877. function hex2bin($s) {
  878. if(! ctype_xdigit($s)) {
  879. logger('hex2bin: illegal input: ' . print_r(debug_backtrace(), true));
  880. return($s);
  881. }
  882. return(pack("H*",$s));
  883. }}
  884. // Automatic pagination.
  885. // To use, get the count of total items.
  886. // Then call $a->set_pager_total($number_items);
  887. // Optionally call $a->set_pager_itemspage($n) to the number of items to display on each page
  888. // Then call paginate($a) after the end of the display loop to insert the pager block on the page
  889. // (assuming there are enough items to paginate).
  890. // When using with SQL, the setting LIMIT %d, %d => $a->pager['start'],$a->pager['itemspage']
  891. // will limit the results to the correct items for the current page.
  892. // The actual page handling is then accomplished at the application layer.
  893. if(! function_exists('paginate')) {
  894. function paginate(&$a) {
  895. $o = '';
  896. $stripped = preg_replace('/(&page=[0-9]*)/','',$a->query_string);
  897. $stripped = str_replace('q=','',$stripped);
  898. $stripped = trim($stripped,'/');
  899. $pagenum = $a->pager['page'];
  900. $url = $a->get_baseurl() . '/' . $stripped;
  901. if($a->pager['total'] > $a->pager['itemspage']) {
  902. $o .= '<div class="pager">';
  903. if($a->pager['page'] != 1)
  904. $o .= '<span class="pager_prev">'."<a href=\"$url".'&page='.($a->pager['page'] - 1).'">' . t('prev') . '</a></span> ';
  905. $o .= "<span class=\"pager_first\"><a href=\"$url"."&page=1\">" . t('first') . "</a></span> ";
  906. $numpages = $a->pager['total'] / $a->pager['itemspage'];
  907. $numstart = 1;
  908. $numstop = $numpages;
  909. if($numpages > 14) {
  910. $numstart = (($pagenum > 7) ? ($pagenum - 7) : 1);
  911. $numstop = (($pagenum > ($numpages - 7)) ? $numpages : ($numstart + 14));
  912. }
  913. for($i = $numstart; $i <= $numstop; $i++){
  914. if($i == $a->pager['page'])
  915. $o .= '<span class="pager_current">'.(($i < 10) ? '&nbsp;'.$i : $i);
  916. else
  917. $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? '&nbsp;'.$i : $i)."</a>";
  918. $o .= '</span> ';
  919. }
  920. if(($a->pager['total'] % $a->pager['itemspage']) != 0) {
  921. if($i == $a->pager['page'])
  922. $o .= '<span class="pager_current">'.(($i < 10) ? '&nbsp;'.$i : $i);
  923. else
  924. $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? '&nbsp;'.$i : $i)."</a>";
  925. $o .= '</span> ';
  926. }
  927. $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
  928. $o .= "<span class=\"pager_last\"><a href=\"$url"."&page=$lastpage\">" . t('last') . "</a></span> ";
  929. if(($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0)
  930. $o .= '<span class="pager_next">'."<a href=\"$url"."&page=".($a->pager['page'] + 1).'">' . t('next') . '</a></span>';
  931. $o .= '</div>'."\r\n";
  932. }
  933. return $o;
  934. }}
  935. // Turn user/group ACLs stored as angle bracketed text into arrays
  936. if(! function_exists('expand_acl')) {
  937. function expand_acl($s) {
  938. // turn string array of angle-bracketed elements into numeric array
  939. // e.g. "<1><2><3>" => array(1,2,3);
  940. $ret = array();
  941. if(strlen($s)) {
  942. $t = str_replace('<','',$s);
  943. $a = explode('>',$t);
  944. foreach($a as $aa) {
  945. if(intval($aa))
  946. $ret[] = intval($aa);
  947. }
  948. }
  949. return $ret;
  950. }}
  951. // Used to wrap ACL elements in angle brackets for storage
  952. if(! function_exists('sanitise_acl')) {
  953. function sanitise_acl(&$item) {
  954. if(intval($item))
  955. $item = '<' . intval(notags(trim($item))) . '>';
  956. else
  957. unset($item);
  958. }}
  959. // retrieve a "family" of config variables from database to cached storage
  960. if(! function_exists('load_config')) {
  961. function load_config($family) {
  962. global $a;
  963. $r = q("SELECT * FROM `config` WHERE `cat` = '%s'",
  964. dbesc($family)
  965. );
  966. if(count($r)) {
  967. foreach($r as $rr) {
  968. $k = $rr['k'];
  969. $a->config[$family][$k] = $rr['v'];
  970. }
  971. }
  972. }}
  973. // get a particular config variable given the family name
  974. // and key. Returns false if not set.
  975. // $instore is only used by the set_config function
  976. // to determine if the key already exists in the DB
  977. // If a key is found in the DB but doesn't exist in
  978. // local config cache, pull it into the cache so we don't have
  979. // to hit the DB again for this item.
  980. if(! function_exists('get_config')) {
  981. function get_config($family, $key, $instore = false) {
  982. global $a;
  983. if(! $instore) {
  984. if(isset($a->config[$family][$key])) {
  985. if($a->config[$family][$key] === '!<unset>!') {
  986. return false;
  987. }
  988. return $a->config[$family][$key];
  989. }
  990. }
  991. $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
  992. dbesc($family),
  993. dbesc($key)
  994. );
  995. if(count($ret)) {
  996. $a->config[$family][$key] = $ret[0]['v'];
  997. return $ret[0]['v'];
  998. }
  999. else {
  1000. $a->config[$family][$key] = '!<unset>!';
  1001. }
  1002. return false;
  1003. }}
  1004. // Store a config value ($value) in the category ($family)
  1005. // under the key ($key)
  1006. // Return the value, or false if the database update failed
  1007. if(! function_exists('set_config')) {
  1008. function set_config($family,$key,$value) {
  1009. global $a;
  1010. if(get_config($family,$key,true) === false) {
  1011. $a->config[$family][$key] = $value;
  1012. $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
  1013. dbesc($family),
  1014. dbesc($key),
  1015. dbesc($value)
  1016. );
  1017. if($ret)
  1018. return $value;
  1019. return $ret;
  1020. }
  1021. $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
  1022. dbesc($value),
  1023. dbesc($family),
  1024. dbesc($key)
  1025. );
  1026. $a->config[$family][$key] = $value;
  1027. if($ret)
  1028. return $value;
  1029. return $ret;
  1030. }}
  1031. if(! function_exists('load_pconfig')) {
  1032. function load_pconfig($uid,$family) {
  1033. global $a;
  1034. $r = q("SELECT * FROM `pconfig` WHERE `cat` = '%s' AND `uid` = %d",
  1035. dbesc($family),
  1036. intval($uid)
  1037. );
  1038. if(count($r)) {
  1039. foreach($r as $rr) {
  1040. $k = $rr['k'];
  1041. $a->config[$uid][$family][$k] = $rr['v'];
  1042. }
  1043. }
  1044. }}
  1045. if(! function_exists('get_pconfig')) {
  1046. function get_pconfig($uid,$family, $key, $instore = false) {
  1047. global $a;
  1048. if(! $instore) {
  1049. if(isset($a->config[$uid][$family][$key])) {
  1050. if($a->config[$uid][$family][$key] === '!<unset>!') {
  1051. return false;
  1052. }
  1053. return $a->config[$uid][$family][$key];
  1054. }
  1055. }
  1056. $ret = q("SELECT `v` FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
  1057. intval($uid),
  1058. dbesc($family),
  1059. dbesc($key)
  1060. );
  1061. if(count($ret)) {
  1062. $a->config[$uid][$family][$key] = $ret[0]['v'];
  1063. return $ret[0]['v'];
  1064. }
  1065. else {
  1066. $a->config[$uid][$family][$key] = '!<unset>!';
  1067. }
  1068. return false;
  1069. }}
  1070. if(! function_exists('del_config')) {
  1071. function del_config($family,$key) {
  1072. global $a;
  1073. if(x($a->config[$family],$key))
  1074. unset($a->config[$family][$key]);
  1075. $ret = q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
  1076. dbesc($cat),
  1077. dbesc($key)
  1078. );
  1079. return $ret;
  1080. }}
  1081. // Same as above functions except these are for personal config storage and take an
  1082. // additional $uid argument.
  1083. if(! function_exists('set_pconfig')) {
  1084. function set_pconfig($uid,$family,$key,$value) {
  1085. global $a;
  1086. if(get_pconfig($uid,$family,$key,true) === false) {
  1087. $a->config[$uid][$family][$key] = $value;
  1088. $ret = q("INSERT INTO `pconfig` ( `uid`, `cat`, `k`, `v` ) VALUES ( %d, '%s', '%s', '%s' ) ",
  1089. intval($uid),
  1090. dbesc($family),
  1091. dbesc($key),
  1092. dbesc($value)
  1093. );
  1094. if($ret)
  1095. return $value;
  1096. return $ret;
  1097. }
  1098. $ret = q("UPDATE `pconfig` SET `v` = '%s' WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
  1099. dbesc($value),
  1100. intval($uid),
  1101. dbesc($family),
  1102. dbesc($key)
  1103. );
  1104. $a->config[$uid][$family][$key] = $value;
  1105. if($ret)
  1106. return $value;
  1107. return $ret;
  1108. }}
  1109. if(! function_exists('del_pconfig')) {
  1110. function del_pconfig($uid,$family,$key) {
  1111. global $a;
  1112. if(x($a->config[$uid][$family],$key))
  1113. unset($a->config[$uid][$family][$key]);
  1114. $ret = q("DELETE FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
  1115. intval($uid),
  1116. dbesc($family),
  1117. dbesc($key)
  1118. );
  1119. return $ret;
  1120. }}
  1121. // convert an XML document to a normalised, case-corrected array
  1122. // used by webfinger
  1123. if(! function_exists('convert_xml_element_to_array')) {
  1124. function convert_xml_element_to_array($xml_element, &$recursion_depth=0) {
  1125. // If we're getting too deep, bail out
  1126. if ($recursion_depth > 512) {
  1127. return(null);
  1128. }
  1129. if (!is_string($xml_element) &&
  1130. !is_array($xml_element) &&
  1131. (get_class($xml_element) == 'SimpleXMLElement')) {
  1132. $xml_element_copy = $xml_element;
  1133. $xml_element = get_object_vars($xml_element);
  1134. }
  1135. if (is_array($xml_element)) {
  1136. $result_array = array();
  1137. if (count($xml_element) <= 0) {
  1138. return (trim(strval($xml_element_copy)));
  1139. }
  1140. foreach($xml_element as $key=>$value) {
  1141. $recursion_depth++;
  1142. $result_array[strtolower($key)] =
  1143. convert_xml_element_to_array($value, $recursion_depth);
  1144. $recursion_depth--;
  1145. }
  1146. if ($recursion_depth == 0) {
  1147. $temp_array = $result_array;
  1148. $result_array = array(
  1149. strtolower($xml_element_copy->getName()) => $temp_array,
  1150. );
  1151. }
  1152. return ($result_array);
  1153. } else {
  1154. return (trim(strval($xml_element)));
  1155. }
  1156. }}
  1157. // Given an email style address, perform webfinger lookup and
  1158. // return the resulting DFRN profile URL, or if no DFRN profile URL
  1159. // is located, returns an OStatus subscription template (prefixed
  1160. // with the string 'stat:' to identify it as on OStatus template).
  1161. // If this isn't an email style address just return $s.
  1162. // Return an empty string if email-style addresses but webfinger fails,
  1163. // or if the resultant personal XRD doesn't contain a supported
  1164. // subscription/friend-request attribute.
  1165. if(! function_exists('webfinger_dfrn')) {
  1166. function webfinger_dfrn($s) {
  1167. if(! strstr($s,'@')) {
  1168. return $s;
  1169. }
  1170. $links = webfinger($s);
  1171. logger('webfinger_dfrn: ' . $s . ':' . print_r($links,true), LOGGER_DATA);
  1172. if(count($links)) {
  1173. foreach($links as $link)
  1174. if($link['@attributes']['rel'] === NAMESPACE_DFRN)
  1175. return $link['@attributes']['href'];
  1176. foreach($links as $link)
  1177. if($link['@attributes']['rel'] === NAMESPACE_OSTATUSSUB)
  1178. return 'stat:' . $link['@attributes']['template'];
  1179. }
  1180. return '';
  1181. }}
  1182. // Given an email style address, perform webfinger lookup and
  1183. // return the array of link attributes from the personal XRD file.
  1184. // On error/failure return an empty array.
  1185. if(! function_exists('webfinger')) {
  1186. function webfinger($s) {
  1187. $host = '';
  1188. if(strstr($s,'@')) {
  1189. $host = substr($s,strpos($s,'@') + 1);
  1190. }
  1191. if(strlen($host)) {
  1192. $tpl = fetch_lrdd_template($host);
  1193. logger('webfinger: lrdd template: ' . $tpl);
  1194. if(strlen($tpl)) {
  1195. $pxrd = str_replace('{uri}', urlencode('acct:' . $s), $tpl);
  1196. logger('webfinger: pxrd: ' . $pxrd);
  1197. $links = fetch_xrd_links($pxrd);
  1198. if(! count($links)) {
  1199. // try with double slashes
  1200. $pxrd = str_replace('{uri}', urlencode('acct://' . $s), $tpl);
  1201. logger('webfinger: pxrd: ' . $pxrd);
  1202. $links = fetch_xrd_links($pxrd);
  1203. }
  1204. return $links;
  1205. }
  1206. }
  1207. return array();
  1208. }}
  1209. if(! function_exists('lrdd')) {
  1210. function lrdd($uri) {
  1211. $a = get_app();
  1212. // default priority is host priority, host-meta first
  1213. $priority = 'host';
  1214. // All we have is an email address. Resource-priority is irrelevant
  1215. // because our URI isn't directly resolvable.
  1216. if(strstr($uri,'@')) {
  1217. return(webfinger($uri));
  1218. }
  1219. // get the host meta file
  1220. $host = @parse_url($uri);
  1221. if($host) {
  1222. $url = ((x($host,'scheme')) ? $host['scheme'] : 'http') . '://';
  1223. $url .= $host['host'] . '/.well-known/host-meta' ;
  1224. }
  1225. else
  1226. return array();
  1227. logger('lrdd: constructed url: ' . $url);
  1228. $xml = fetch_url($url);
  1229. $headers = $a->get_curl_headers();
  1230. if (! $xml)
  1231. return array();
  1232. logger('lrdd: host_meta: ' . $xml, LOGGER_DATA);
  1233. $h = parse_xml_string($xml);
  1234. if(! $h)
  1235. return array();
  1236. $arr = convert_xml_element_to_array($h);
  1237. if(isset($arr['xrd']['property'])) {
  1238. $property = $arr['crd']['property'];
  1239. if(! isset($property[0]))
  1240. $properties = array($property);
  1241. else
  1242. $properties = $property;
  1243. foreach($properties as $prop)
  1244. if((string) $prop['@attributes'] === 'http://lrdd.net/priority/resource')
  1245. $priority = 'resource';
  1246. }
  1247. // save the links in case we need them
  1248. $links = array();
  1249. if(isset($arr['xrd']['link'])) {
  1250. $link = $arr['xrd']['link'];
  1251. if(! isset($link[0]))
  1252. $links = array($link);
  1253. else
  1254. $links = $link;
  1255. }
  1256. // do we have a template or href?
  1257. if(count($links)) {
  1258. foreach($links as $link) {
  1259. if($link['@attributes']['rel'] && attribute_contains($link['@attributes']['rel'],'lrdd')) {
  1260. if(x($link['@attributes'],'template'))
  1261. $tpl = $link['@attributes']['template'];
  1262. elseif(x($link['@attributes'],'href'))
  1263. $href = $link['@attributes']['href'];
  1264. }
  1265. }
  1266. }
  1267. if((! isset($tpl)) || (! strpos($tpl,'{uri}')))
  1268. $tpl = '';
  1269. if($priority === 'host') {
  1270. if(strlen($tpl))
  1271. $pxrd = str_replace('{uri}', urlencode($uri), $tpl);
  1272. elseif(isset($href))
  1273. $pxrd = $href;
  1274. if(isset($pxrd)) {
  1275. logger('lrdd: (host priority) pxrd: ' . $pxrd);
  1276. $links = fetch_xrd_links($pxrd);
  1277. return $links;
  1278. }
  1279. $lines = explode("\n",$headers);
  1280. if(count($lines)) {
  1281. foreach($lines as $line) {
  1282. if((stristr($line,'link:')) && preg_match('/<([^>].*)>.*rel\=[\'\"]lrdd[\'\"]/',$line,$matches)) {
  1283. return(fetch_xrd_links($matches[1]));
  1284. break;
  1285. }
  1286. }
  1287. }
  1288. }
  1289. // priority 'resource'
  1290. $html = fetch_url($uri);
  1291. $headers = $a->get_curl_headers();
  1292. logger('lrdd: headers=' . $headers, LOGGER_DEBUG);
  1293. // don't try and parse raw xml as html
  1294. if(! strstr($html,'<?xml')) {
  1295. require_once('library/HTML5/Parser.php');
  1296. $dom = @HTML5_Parser::parse($html);
  1297. if($dom) {
  1298. $items = $dom->getElementsByTagName('link');
  1299. foreach($items as $item) {
  1300. $x = $item->getAttribute('rel');
  1301. if($x == "lrdd") {
  1302. $pagelink = $item->getAttribute('href');
  1303. break;
  1304. }
  1305. }
  1306. }
  1307. }
  1308. if(isset($pagelink))
  1309. return(fetch_xrd_links($pagelink));
  1310. // next look in HTTP headers
  1311. $lines = explode("\n",$headers);
  1312. if(count($lines)) {
  1313. foreach($lines as $line) {
  1314. // TODO alter the following regex to support multiple relations (space separated)
  1315. if((stristr($line,'link:')) && preg_match('/<([^>].*)>.*rel\=[\'\"]lrdd[\'\"]/',$line,$matches)) {
  1316. $pagelink = $matches[1];
  1317. break;
  1318. }
  1319. // don't try and run feeds through the html5 parser
  1320. if(stristr($line,'content-type:') && ((stristr($line,'application/atom+xml')) || (stristr($line,'application/rss+xml'))))
  1321. return array();
  1322. if(stristr($html,'<rss') || stristr($html,'<feed'))
  1323. return array();
  1324. }
  1325. }
  1326. if(isset($pagelink))
  1327. return(fetch_xrd_links($pagelink));
  1328. // If we haven't found any links, return the host xrd links (which we have already fetched)
  1329. if(isset($links))
  1330. return $links;
  1331. return array();
  1332. }}
  1333. // Given a host name, locate the LRDD template from that
  1334. // host. Returns the LRDD template or an empty string on
  1335. // error/failure.
  1336. if(! function_exists('fetch_lrdd_template')) {
  1337. function fetch_lrdd_template($host) {
  1338. $tpl = '';
  1339. $url1 = 'https://' . $host . '/.well-known/host-meta' ;
  1340. $url2 = 'http://' . $host . '/.well-known/host-meta' ;
  1341. $links = fetch_xrd_links($url1);
  1342. logger('template (https): ' . print_r($links,true));
  1343. if(! count($links)) {
  1344. $links = fetch_xrd_links($url2);
  1345. logger('template (http): ' . print_r($links,true));
  1346. }
  1347. if(count($links)) {
  1348. foreach($links as $link)
  1349. if($link['@attributes']['rel'] && $link['@attributes']['rel'] === 'lrdd')
  1350. $tpl = $link['@attributes']['template'];
  1351. }
  1352. if(! strpos($tpl,'{uri}'))
  1353. $tpl = '';
  1354. return $tpl;
  1355. }}
  1356. // Given a URL, retrieve the page as an XRD document.
  1357. // Return an array of links.
  1358. // on error/failure return empty array.
  1359. if(! function_exists('fetch_xrd_links')) {
  1360. function fetch_xrd_links($url) {
  1361. $xml = fetch_url($url);
  1362. if (! $xml)
  1363. return array();
  1364. logger('fetch_xrd_links: ' . $xml, LOGGER_DATA);
  1365. $h = parse_xml_string($xml);
  1366. if(! $h)
  1367. return array();
  1368. $arr = convert_xml_element_to_array($h);
  1369. $links = array();
  1370. if(isset($arr['xrd']['link'])) {
  1371. $link = $arr['xrd']['link'];
  1372. if(! isset($link[0]))
  1373. $links = array($link);
  1374. else
  1375. $links = $link;
  1376. }
  1377. if(isset($arr['xrd']['alias'])) {
  1378. $alias = $arr['xrd']['alias'];
  1379. if(! isset($alias[0]))
  1380. $aliases = array($alias);
  1381. else
  1382. $aliases = $alias;
  1383. foreach($aliases as $alias) {
  1384. $links[]['@attributes'] = array('rel' => 'alias' , 'href' => $alias);
  1385. }
  1386. }
  1387. logger('fetch_xrd_links: ' . print_r($links,true), LOGGER_DATA);
  1388. return $links;
  1389. }}
  1390. // Convert an ACL array to a storable string
  1391. if(! function_exists('perms2str')) {
  1392. function perms2str($p) {
  1393. $ret = '';
  1394. $tmp = $p;
  1395. if(is_array($tmp)) {
  1396. array_walk($tmp,'sanitise_acl');
  1397. $ret = implode('',$tmp);
  1398. }
  1399. return $ret;
  1400. }}
  1401. // generate a guaranteed unique (for this domain) item ID for ATOM
  1402. // safe from birthday paradox
  1403. if(! function_exists('item_new_uri')) {
  1404. function item_new_uri($hostname,$uid) {
  1405. do {
  1406. $dups = false;
  1407. $hash = random_string();
  1408. $uri = "urn:X-dfrn:" . $hostname . ':' . $uid . ':' . $hash;
  1409. $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
  1410. dbesc($uri));
  1411. if(count($r))
  1412. $dups = true;
  1413. } while($dups == true);
  1414. return $uri;
  1415. }}
  1416. // Generate a guaranteed unique photo ID.
  1417. // safe from birthday paradox
  1418. if(! function_exists('photo_new_resource')) {
  1419. function photo_new_resource() {
  1420. do {
  1421. $found = false;
  1422. $resource = hash('md5',uniqid(mt_rand(),true));
  1423. $r = q("SELECT `id` FROM `photo` WHERE `resource-id` = '%s' LIMIT 1",
  1424. dbesc($resource)
  1425. );
  1426. if(count($r))
  1427. $found = true;
  1428. } while($found == true);
  1429. return $resource;
  1430. }}
  1431. // Take a URL from the wild, prepend http:// if necessary
  1432. // and check DNS to see if it's real
  1433. // return true if it's OK, false if something is wrong with it
  1434. if(! function_exists('validate_url')) {
  1435. function validate_url(&$url) {
  1436. if(substr($url,0,4) != 'http')
  1437. $url = 'http://' . $url;
  1438. $h = @parse_url($url);
  1439. if(($h) && (dns_get_record($h['host'], DNS_A + DNS_CNAME + DNS_PTR))) {
  1440. return true;
  1441. }
  1442. return false;
  1443. }}
  1444. // checks that email is an actual resolvable internet address
  1445. if(! function_exists('validate_email')) {
  1446. function validate_email($addr) {
  1447. if(! strpos($addr,'@'))
  1448. return false;
  1449. $h = substr($addr,strpos($addr,'@') + 1);
  1450. if(($h) && (dns_get_record($h, DNS_A + DNS_CNAME + DNS_PTR + DNS_MX))) {
  1451. return true;
  1452. }
  1453. return false;
  1454. }}
  1455. // Check $url against our list of allowed sites,
  1456. // wildcards allowed. If allowed_sites is unset return true;
  1457. // If url is allowed, return true.
  1458. // otherwise, return false
  1459. if(! function_exists('allowed_url')) {
  1460. function allowed_url($url) {
  1461. $h = @parse_url($url);
  1462. if(! $h) {
  1463. return false;
  1464. }
  1465. $str_allowed = get_config('system','allowed_sites');
  1466. if(! $str_allowed)
  1467. return true;
  1468. $found = false;
  1469. $host = strtolower($h['host']);
  1470. // always allow our own site
  1471. if($host == strtolower($_SERVER['SERVER_NAME']))
  1472. return true;
  1473. $fnmatch = function_exists('fnmatch');
  1474. $allowed = explode(',',$str_allowed);
  1475. if(count($allowed)) {
  1476. foreach($allowed as $a) {
  1477. $pat = strtolower(trim($a));
  1478. if(($fnmatch && fnmatch($pat,$host)) || ($pat == $host)) {
  1479. $found = true;
  1480. break;
  1481. }
  1482. }
  1483. }
  1484. return $found;
  1485. }}
  1486. // check if email address is allowed to register here.
  1487. // Compare against our list (wildcards allowed).
  1488. // Returns false if not allowed, true if allowed or if
  1489. // allowed list is not configured.
  1490. if(! function_exists('allowed_email')) {
  1491. function allowed_email($email) {
  1492. $domain = strtolower(substr($email,strpos($email,'@') + 1));
  1493. if(! $domain)
  1494. return false;
  1495. $str_allowed = get_config('system','allowed_email');
  1496. if(! $str_allowed)
  1497. return true;
  1498. $found = false;
  1499. $fnmatch = function_exists('fnmatch');
  1500. $allowed = explode(',',$str_allowed);
  1501. if(count($allowed)) {
  1502. foreach($allowed as $a) {
  1503. $pat = strtolower(trim($a));
  1504. if(($fnmatch && fnmatch($pat,$domain)) || ($pat == $domain)) {
  1505. $found = true;
  1506. break;
  1507. }
  1508. }
  1509. }
  1510. return $found;
  1511. }}
  1512. // wrapper to load a view template, checking for alternate
  1513. // languages before falling back to the default
  1514. // obsolete, deprecated.
  1515. if(! function_exists('load_view_file')) {
  1516. function load_view_file($s) {
  1517. global $lang, $a;
  1518. if(! isset($lang))
  1519. $lang = 'en';
  1520. $b = basename($s);
  1521. $d = dirname($s);
  1522. if(file_exists("$d/$lang/$b"))
  1523. return file_get_contents("$d/$lang/$b");
  1524. $theme = current_theme();
  1525. if(file_exists("$d/theme/$theme/$b"))
  1526. return file_get_contents("$d/theme/$theme/$b");
  1527. return file_get_contents($s);
  1528. }}
  1529. if(! function_exists('get_intltext_template')) {
  1530. function get_intltext_template($s) {
  1531. global $lang;
  1532. if(! isset($lang))
  1533. $lang = 'en';
  1534. if(file_exists("view/$lang/$s"))
  1535. return file_get_contents("view/$lang/$s");
  1536. elseif(file_exists("view/en/$s"))
  1537. return file_get_contents("view/en/$s");
  1538. else
  1539. return file_get_contents("view/$s");
  1540. }}
  1541. if(! function_exists('get_markup_template')) {
  1542. function get_markup_template($s) {
  1543. $theme = current_theme();
  1544. if(file_exists("view/theme/$theme/$s"))
  1545. return file_get_contents("view/theme/$theme/$s");
  1546. else
  1547. return file_get_contents("view/$s");
  1548. }}
  1549. // for html,xml parsing - let's say you've got
  1550. // an attribute foobar="class1 class2 class3"
  1551. // and you want to find out if it contains 'class3'.
  1552. // you can't use a normal sub string search because you
  1553. // might match 'notclass3' and a regex to do the job is
  1554. // possible but a bit complicated.
  1555. // pass the attribute string as $attr and the attribute you
  1556. // are looking for as $s - returns true if found, otherwise false
  1557. if(! function_exists('attribute_contains')) {
  1558. function attribute_contains($attr,$s) {
  1559. $a = explode(' ', $attr);
  1560. if(count($a) && in_array($s,$a))
  1561. return true;
  1562. return false;
  1563. }}
  1564. if(! function_exists('logger')) {
  1565. function logger($msg,$level = 0) {
  1566. $debugging = get_config('system','debugging');
  1567. $loglevel = intval(get_config('system','loglevel'));
  1568. $logfile = get_config('system','logfile');
  1569. if((! $debugging) || (! $logfile) || ($level > $loglevel))
  1570. return;
  1571. @file_put_contents($logfile, datetime_convert() . ':' . session_id() . ' ' . $msg . "\n", FILE_APPEND);
  1572. return;
  1573. }}
  1574. if(! function_exists('activity_match')) {
  1575. function activity_match($haystack,$needle) {
  1576. if(($haystack === $needle) || ((basename($needle) === $haystack) && strstr($needle,NAMESPACE_ACTIVITY_SCHEMA)))
  1577. return true;
  1578. return false;
  1579. }}
  1580. // Pull out all #hashtags and @person tags from $s;
  1581. // We also get @person@domain.com - which would make
  1582. // the regex quite complicated as tags can also
  1583. // end a sentence. So we'll run through our results
  1584. // and strip the period from any tags which end with one.
  1585. // Returns array of tags found, or empty array.
  1586. if(! function_exists('get_tags')) {
  1587. function get_tags($s) {
  1588. $ret = array();
  1589. // ignore anything in a code block
  1590. $s = preg_replace('/\[code\](.*?)\[\/code\]/sm','',$s);
  1591. if(preg_match_all('/([@#][^ \x0D\x0A,:?]+)([ \x0D\x0A,:?]|$)/',$s,$match)) {
  1592. foreach($match[1] as $mtch) {
  1593. if(strstr($mtch,"]")) {
  1594. // we might be inside a bbcode color tag - leave it alone
  1595. continue;
  1596. }
  1597. if(substr($mtch,-1,1) === '.')
  1598. $ret[] = substr($mtch,0,-1);
  1599. else
  1600. $ret[] = $mtch;
  1601. }
  1602. }
  1603. return $ret;
  1604. }}
  1605. // quick and dirty quoted_printable encoding
  1606. if(! function_exists('qp')) {
  1607. function qp($s) {
  1608. return str_replace ("%","=",rawurlencode($s));
  1609. }}
  1610. if(! function_exists('get_mentions')) {
  1611. function get_mentions($item) {
  1612. $o = '';
  1613. if(! strlen($item['tag']))
  1614. return $o;
  1615. $arr = explode(',',$item['tag']);
  1616. foreach($arr as $x) {
  1617. $matches = null;
  1618. if(preg_match('/@\[url=([^\]]*)\]/',$x,$matches)) {
  1619. $o .= "\t\t" . '<link rel="mentioned" href="' . $matches[1] . '" />' . "\r\n";
  1620. $o .= "\t\t" . '<link rel="ostatus:attention" href="' . $matches[1] . '" />' . "\r\n";
  1621. }
  1622. }
  1623. return $o;
  1624. }}
  1625. if(! function_exists('contact_block')) {
  1626. function contact_block() {
  1627. $o = '';
  1628. $a = get_app();
  1629. $shown = get_pconfig($a->profile['uid'],'system','display_friend_count');
  1630. if(! $shown)
  1631. $shown = 24;
  1632. if((! is_array($a->profile)) || ($a->profile['hide-friends']))
  1633. return $o;
  1634. $r = q("SELECT COUNT(*) AS `total` FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 and `pending` = 0",
  1635. intval($a->profile['uid'])
  1636. );
  1637. if(count($r)) {
  1638. $total = intval($r[0]['total']);
  1639. }
  1640. if(! $total) {
  1641. $o .= '<h4 class="contact-h4">' . t('No contacts') . '</h4>';
  1642. return $o;
  1643. }
  1644. $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 and `pending` = 0 ORDER BY RAND() LIMIT %d",
  1645. intval($a->profile['uid']),
  1646. intval($shown)
  1647. );
  1648. if(count($r)) {
  1649. $o .= '<h4 class="contact-h4">' . sprintf( tt('%d Contact','%d Contacts', $total),$total) . '</h4><div id="contact-block">';
  1650. foreach($r as $rr) {
  1651. $o .= micropro($rr,true,'mpfriend');
  1652. }
  1653. $o .= '</div><div id="contact-block-end"></div>';
  1654. $o .= '<div id="viewcontacts"><a id="viewcontacts-link" href="viewcontacts/' . $a->profile['nickname'] . '">' . t('View Contacts') . '</a></div>';
  1655. }
  1656. $arr = array('contacts' => $r, 'output' => $o);
  1657. call_hooks('contact_block_end', $arr);
  1658. return $o;
  1659. }}
  1660. if(! function_exists('micropro')) {
  1661. function micropro($contact, $redirect = false, $class = '', $textmode = false) {
  1662. if($class)
  1663. $class = ' ' . $class;
  1664. $url = $contact['url'];
  1665. $sparkle = '';
  1666. if($redirect) {
  1667. $a = get_app();
  1668. $redirect_url = $a->get_baseurl() . '/redir/' . $contact['id'];
  1669. if(local_user() && ($contact['uid'] == local_user()) && ($contact['network'] === 'dfrn')) {
  1670. $url = $redirect_url;
  1671. $sparkle = ' sparkle';
  1672. }
  1673. }
  1674. $click = ((x($contact,'click')) ? ' onclick="' . $contact['click'] . '" ' : '');
  1675. if($click)
  1676. $url = '';
  1677. if($textmode) {
  1678. return '<div class="contact-block-textdiv' . $class . '"><a class="contact-block-link' . $class . $sparkle
  1679. . (($click) ? ' fakelink' : '') . '" '
  1680. . (($url) ? ' href="' . $url . '"' : '') . $click
  1681. . '" title="' . $contact['name'] . ' [' . $contact['url'] . ']" alt="' . $contact['name']
  1682. . '" >'. $contact['name'] . '</a></div>' . "\r\n";
  1683. }
  1684. else {
  1685. return '<div class="contact-block-div' . $class . '"><a class="contact-block-link' . $class . $sparkle
  1686. . (($click) ? ' fakelink' : '') . '" '
  1687. . (($url) ? ' href="' . $url . '"' : '') . $click . ' ><img class="contact-block-img' . $class . $sparkle . '" src="'
  1688. . $contact['micro'] . '" title="' . $contact['name'] . ' [' . $contact['url'] . ']" alt="' . $contact['name']
  1689. . '" /></a></div>' . "\r\n";
  1690. }
  1691. }}
  1692. if(! function_exists('search')) {
  1693. function search($s,$id='search-box',$url='/search') {
  1694. $a = get_app();
  1695. $o = '<div id="' . $id . '">';
  1696. $o .= '<form action="' . $a->get_baseurl() . $url . '" method="get" >';
  1697. $o .= '<input type="text" name="search" id="search-text" value="' . $s .'" />';
  1698. $o .= '<input type="submit" name="submit" id="search-submit" value="' . t('Search') . '" />';
  1699. $o .= '</form></div>';
  1700. return $o;
  1701. }}
  1702. if(! function_exists('valid_email')) {
  1703. function valid_email($x){
  1704. if(preg_match('/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+$/',$x))
  1705. return true;
  1706. return false;
  1707. }}
  1708. if(! function_exists('gravatar_img')) {
  1709. function gravatar_img($email) {
  1710. $size = 175;
  1711. $opt = 'identicon'; // psuedo-random geometric pattern if not found
  1712. $rating = 'pg';
  1713. $hash = md5(trim(strtolower($email)));
  1714. $url = 'http://www.gravatar.com/avatar/' . $hash . '.jpg'
  1715. . '?s=' . $size . '&d=' . $opt . '&r=' . $rating;
  1716. logger('gravatar: ' . $email . ' ' . $url);
  1717. return $url;
  1718. }}
  1719. if(! function_exists('aes_decrypt')) {
  1720. function aes_decrypt($val,$ky)
  1721. {
  1722. $key="\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
  1723. for($a=0;$a<strlen($ky);$a++)
  1724. $key[$a%16]=chr(ord($key[$a%16]) ^ ord($ky[$a]));
  1725. $mode = MCRYPT_MODE_ECB;
  1726. $enc = MCRYPT_RIJNDAEL_128;
  1727. $dec = @mcrypt_decrypt($enc, $key, $val, $mode, @mcrypt_create_iv( @mcrypt_get_iv_size($enc, $mode), MCRYPT_DEV_URANDOM ) );
  1728. 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));
  1729. }}
  1730. if(! function_exists('aes_encrypt')) {
  1731. function aes_encrypt($val,$ky)
  1732. {
  1733. $key="\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
  1734. for($a=0;$a<strlen($ky);$a++)
  1735. $key[$a%16]=chr(ord($key[$a%16]) ^ ord($ky[$a]));
  1736. $mode=MCRYPT_MODE_ECB;
  1737. $enc=MCRYPT_RIJNDAEL_128;
  1738. $val=str_pad($val, (16*(floor(strlen($val) / 16)+(strlen($val) % 16==0?2:1))), chr(16-(strlen($val) % 16)));
  1739. return mcrypt_encrypt($enc, $key, $val, $mode, mcrypt_create_iv( mcrypt_get_iv_size($enc, $mode), MCRYPT_DEV_URANDOM));
  1740. }}
  1741. /**
  1742. *
  1743. * Function: linkify
  1744. *
  1745. * Replace naked text hyperlink with HTML formatted hyperlink
  1746. *
  1747. */
  1748. if(! function_exists('linkify')) {
  1749. function linkify($s) {
  1750. $s = preg_replace("/(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\.\=\_\~\#\'\%\$\!\+]*)/", ' <a href="$1" target="external-link">$1</a>', $s);
  1751. return($s);
  1752. }}
  1753. /**
  1754. *
  1755. * Function: smilies
  1756. *
  1757. * Description:
  1758. * Replaces text emoticons with graphical images
  1759. *
  1760. * @Parameter: string $s
  1761. *
  1762. * Returns string
  1763. */
  1764. if(! function_exists('smilies')) {
  1765. function smilies($s) {
  1766. $a = get_app();
  1767. return str_replace(
  1768. array( '&lt;3', '&lt;/3', '&lt;\\3', ':-)', ':)', ';-)', ':-(', ':(', ':-P', ':P', ':-"', ':-x', ':-X', ':-D', '8-|', '8-O'),
  1769. array(
  1770. '<img src="' . $a->get_baseurl() . '/images/smiley-heart.gif" alt="<3" />',
  1771. '<img src="' . $a->get_baseurl() . '/images/smiley-brokenheart.gif" alt="</3" />',
  1772. '<img src="' . $a->get_baseurl() . '/images/smiley-brokenheart.gif" alt="<\\3" />',
  1773. '<img src="' . $a->get_baseurl() . '/images/smiley-smile.gif" alt=":-)" />',
  1774. '<img src="' . $a->get_baseurl() . '/images/smiley-smile.gif" alt=":)" />',
  1775. '<img src="' . $a->get_baseurl() . '/images/smiley-wink.gif" alt=";-)" />',
  1776. '<img src="' . $a->get_baseurl() . '/images/smiley-frown.gif" alt=":-(" />',
  1777. '<img src="' . $a->get_baseurl() . '/images/smiley-frown.gif" alt=":(" />',
  1778. '<img src="' . $a->get_baseurl() . '/images/smiley-tongue-out.gif" alt=":-P" />',
  1779. '<img src="' . $a->get_baseurl() . '/images/smiley-tongue-out.gif" alt=":P" />',
  1780. '<img src="' . $a->get_baseurl() . '/images/smiley-kiss.gif" alt=":-\"" />',
  1781. '<img src="' . $a->get_baseurl() . '/images/smiley-kiss.gif" alt=":-x" />',
  1782. '<img src="' . $a->get_baseurl() . '/images/smiley-kiss.gif" alt=":-X" />',
  1783. '<img src="' . $a->get_baseurl() . '/images/smiley-laughing.gif" alt=":-D" />',
  1784. '<img src="' . $a->get_baseurl() . '/images/smiley-surprised.gif" alt="8-|" />',
  1785. '<img src="' . $a->get_baseurl() . '/images/smiley-surprised.gif" alt="8-O" />'
  1786. ), $s);
  1787. }}
  1788. /**
  1789. *
  1790. * Function : profile_load
  1791. * @parameter App $a
  1792. * @parameter string $nickname
  1793. * @parameter int $profile
  1794. *
  1795. * Summary: Loads a profile into the page sidebar.
  1796. * The function requires a writeable copy of the main App structure, and the nickname
  1797. * of a registered local account.
  1798. *
  1799. * If the viewer is an authenticated remote viewer, the profile displayed is the
  1800. * one that has been configured for his/her viewing in the Contact manager.
  1801. * Passing a non-zero profile ID can also allow a preview of a selected profile
  1802. * by the owner.
  1803. *
  1804. * Profile information is placed in the App structure for later retrieval.
  1805. * Honours the owner's chosen theme for display.
  1806. *
  1807. */
  1808. if(! function_exists('profile_load')) {
  1809. function profile_load(&$a, $nickname, $profile = 0) {
  1810. if(remote_user()) {
  1811. $r = q("SELECT `profile-id` FROM `contact` WHERE `id` = %d LIMIT 1",
  1812. intval($_SESSION['visitor_id']));
  1813. if(count($r))
  1814. $profile = $r[0]['profile-id'];
  1815. }
  1816. $r = null;
  1817. if($profile) {
  1818. $profile_int = intval($profile);
  1819. $r = q("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `user`.* FROM `profile`
  1820. LEFT JOIN `user` ON `profile`.`uid` = `user`.`uid`
  1821. WHERE `user`.`nickname` = '%s' AND `profile`.`id` = %d LIMIT 1",
  1822. dbesc($nickname),
  1823. intval($profile_int)
  1824. );
  1825. }
  1826. if(! count($r)) {
  1827. $r = q("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `user`.* FROM `profile`
  1828. LEFT JOIN `user` ON `profile`.`uid` = `user`.`uid`
  1829. WHERE `user`.`nickname` = '%s' AND `profile`.`is-default` = 1 LIMIT 1",
  1830. dbesc($nickname)
  1831. );
  1832. }
  1833. if(($r === false) || (! count($r))) {
  1834. notice( t('No profile') . EOL );
  1835. $a->error = 404;
  1836. return;
  1837. }
  1838. $a->profile = $r[0];
  1839. $a->page['title'] = $a->profile['name'] . " @ " . $a->config['sitename'];
  1840. $_SESSION['theme'] = $a->profile['theme'];
  1841. if(! (x($a->page,'aside')))
  1842. $a->page['aside'] = '';
  1843. $a->page['aside'] .= profile_sidebar($a->profile);
  1844. $a->page['aside'] .= contact_block();
  1845. return;
  1846. }}
  1847. /**
  1848. *
  1849. * Function: profile_sidebar
  1850. *
  1851. * Formats a profile for display in the sidebar.
  1852. * It is very difficult to templatise the HTML completely
  1853. * because of all the conditional logic.
  1854. *
  1855. * @parameter: array $profile
  1856. *
  1857. * Returns HTML string stuitable for sidebar inclusion
  1858. * Exceptions: Returns empty string if passed $profile is wrong type or not populated
  1859. *
  1860. */
  1861. if(! function_exists('profile_sidebar')) {
  1862. function profile_sidebar($profile) {
  1863. $o = '';
  1864. $location = '';
  1865. $address = false;
  1866. if((! is_array($profile)) && (! count($profile)))
  1867. return $o;
  1868. call_hooks('profile_sidebar_enter', $profile);
  1869. $fullname = '<div class="fn">' . $profile['name'] . '</div>';
  1870. $pdesc = '<div class="title">' . $profile['pdesc'] . '</div>';
  1871. $tabs = '';
  1872. $photo = '<div id="profile-photo-wrapper"><img class="photo" src="' . $profile['photo'] . '" alt="' . $profile['name'] . '" /></div>';
  1873. // don't show connect link to yourself
  1874. $connect = (($profile['uid'] != local_user()) ? '<li><a id="dfrn-request-link" href="dfrn_request/' . $profile['nickname'] . '">' . t('Connect') . '</a></li>' : '');
  1875. // don't show connect link to authenticated visitors either
  1876. if((remote_user()) && ($_SESSION['visitor_visiting'] == $profile['uid']))
  1877. $connect = '';
  1878. if((x($profile,'address') == 1)
  1879. || (x($profile,'locality') == 1)
  1880. || (x($profile,'region') == 1)
  1881. || (x($profile,'postal-code') == 1)
  1882. || (x($profile,'country-name') == 1))
  1883. $address = true;
  1884. if($address) {
  1885. $location .= '<div class="location"><span class="location-label">' . t('Location:') . '</span> <div class="adr">';
  1886. $location .= ((x($profile,'address') == 1) ? '<div class="street-address">' . $profile['address'] . '</div>' : '');
  1887. $location .= (((x($profile,'locality') == 1) || (x($profile,'region') == 1) || (x($profile,'postal-code') == 1))
  1888. ? '<span class="city-state-zip"><span class="locality">' . $profile['locality'] . '</span>'
  1889. . ((x($profile['locality']) == 1) ? t(', ') : '')
  1890. . '<span class="region">' . $profile['region'] . '</span>'
  1891. . ' <span class="postal-code">' . $profile['postal-code'] . '</span></span>' : '');
  1892. $location .= ((x($profile,'country-name') == 1) ? ' <span class="country-name">' . $profile['country-name'] . '</span>' : '');
  1893. $location .= '</div></div><div class="profile-clear"></div>';
  1894. }
  1895. $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>' : '');
  1896. $pubkey = ((x($profile,'pubkey') == 1) ? '<div class="key" style="display:none;">' . $profile['pubkey'] . '</div>' : '');
  1897. $marital = ((x($profile,'marital') == 1) ? '<div class="marital"><span class="marital-label"><span class="heart">&hearts;</span> ' . t('Status:') . ' </span><span class="marital-text">' . $profile['marital'] . '</span></div><div class="profile-clear"></div>' : '');
  1898. $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 class="profile-clear"></div>' : '');
  1899. if($profile['hidewall'] && (! local_user()) && (! remote_user())) {
  1900. $location = $gender = $marital = $homepage = '';
  1901. }
  1902. $tpl = get_markup_template('profile_vcard.tpl');
  1903. $o .= replace_macros($tpl, array(
  1904. '$fullname' => $fullname,
  1905. '$pdesc' => $pdesc,
  1906. '$tabs' => $tabs,
  1907. '$photo' => $photo,
  1908. '$connect' => $connect,
  1909. '$location' => $location,
  1910. '$gender' => $gender,
  1911. '$pubkey' => $pubkey,
  1912. '$marital' => $marital,
  1913. '$homepage' => $homepage
  1914. ));
  1915. $arr = array('profile' => $profile, 'entry' => $o);
  1916. call_hooks('profile_sidebar', $arr);
  1917. return $o;
  1918. }}
  1919. if(! function_exists('register_hook')) {
  1920. function register_hook($hook,$file,$function) {
  1921. $r = q("SELECT * FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s' LIMIT 1",
  1922. dbesc($hook),
  1923. dbesc($file),
  1924. dbesc($function)
  1925. );
  1926. if(count($r))
  1927. return true;
  1928. $r = q("INSERT INTO `hook` (`hook`, `file`, `function`) VALUES ( '%s', '%s', '%s' ) ",
  1929. dbesc($hook),
  1930. dbesc($file),
  1931. dbesc($function)
  1932. );
  1933. return $r;
  1934. }}
  1935. if(! function_exists('unregister_hook')) {
  1936. function unregister_hook($hook,$file,$function) {
  1937. $r = q("DELETE FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s' LIMIT 1",
  1938. dbesc($hook),
  1939. dbesc($file),
  1940. dbesc($function)
  1941. );
  1942. return $r;
  1943. }}
  1944. if(! function_exists('load_hooks')) {
  1945. function load_hooks() {
  1946. $a = get_app();
  1947. $a->hooks = array();
  1948. $r = q("SELECT * FROM `hook` WHERE 1");
  1949. if(count($r)) {
  1950. foreach($r as $rr) {
  1951. $a->hooks[] = array($rr['hook'], $rr['file'], $rr['function']);
  1952. }
  1953. }
  1954. }}
  1955. if(! function_exists('call_hooks')) {
  1956. function call_hooks($name, &$data = null) {
  1957. $a = get_app();
  1958. if(count($a->hooks)) {
  1959. foreach($a->hooks as $hook) {
  1960. if($hook[HOOK_HOOK] === $name) {
  1961. @include_once($hook[HOOK_FILE]);
  1962. if(function_exists($hook[HOOK_FUNCTION])) {
  1963. $func = $hook[HOOK_FUNCTION];
  1964. $func($a,$data);
  1965. }
  1966. }
  1967. }
  1968. }
  1969. }}
  1970. if(! function_exists('day_translate')) {
  1971. function day_translate($s) {
  1972. $ret = str_replace(array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'),
  1973. array( t('Monday'), t('Tuesday'), t('Wednesday'), t('Thursday'), t('Friday'), t('Saturday'), t('Sunday')),
  1974. $s);
  1975. $ret = str_replace(array('January','February','March','April','May','June','July','August','September','October','November','December'),
  1976. 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')),
  1977. $ret);
  1978. return $ret;
  1979. }}
  1980. if(! function_exists('get_birthdays')) {
  1981. function get_birthdays() {
  1982. $a = get_app();
  1983. $o = '';
  1984. if(! local_user())
  1985. return $o;
  1986. $bd_format = t('g A l F d') ; // 8 AM Friday January 18
  1987. $r = q("SELECT `event`.*, `event`.`id` AS `eid`, `contact`.* FROM `event`
  1988. LEFT JOIN `contact` ON `contact`.`id` = `event`.`cid`
  1989. WHERE `event`.`uid` = %d AND `type` = 'birthday' AND `start` < '%s' AND `finish` > '%s'
  1990. ORDER BY `start` DESC ",
  1991. intval(local_user()),
  1992. dbesc(datetime_convert('UTC','UTC','now + 6 days')),
  1993. dbesc(datetime_convert('UTC','UTC','now'))
  1994. );
  1995. if($r && count($r)) {
  1996. $total = 0;
  1997. foreach($r as $rr)
  1998. if(strlen($rr['name']))
  1999. $total ++;
  2000. if($total) {
  2001. $o .= '<div id="birthday-notice" class="birthday-notice fakelink" onclick=openClose(\'birthday-wrapper\'); >' . t('Birthday Reminders') . ' ' . '(' . $total . ')' . '</div>';
  2002. $o .= '<div id="birthday-wrapper" style="display: none;" ><div id="birthday-title">' . t('Birthdays this week:') . '</div>';
  2003. $o .= '<div id="birthday-adjust">' . t("\x28Adjusted for local time\x29") . '</div>';
  2004. $o .= '<div id="birthday-title-end"></div>';
  2005. foreach($r as $rr) {
  2006. if(! strlen($rr['name']))
  2007. continue;
  2008. $now = strtotime('now');
  2009. $today = (((strtotime($rr['start'] . ' +00:00') < $now) && (strtotime($rr['finish'] . ' +00:00') > $now)) ? true : false);
  2010. $o .= '<div class="birthday-list" id="birthday-' . $rr['eid'] . '"><a class="sparkle" href="'
  2011. . $a->get_baseurl() . '/redir/' . $rr['cid'] . '">' . $rr['name'] . '</a> '
  2012. . day_translate(datetime_convert('UTC', $a->timezone, $rr['start'], $bd_format)) . (($today) ? ' ' . t('[today]') : '')
  2013. . '</div>' ;
  2014. }
  2015. $o .= '</div></div>';
  2016. }
  2017. }
  2018. return $o;
  2019. }}
  2020. if(! function_exists('normalise_link')) {
  2021. function normalise_link($url) {
  2022. $ret = str_replace(array('https:','//www.'), array('http:','//'), $url);
  2023. return(rtrim($ret,'/'));
  2024. }}
  2025. /**
  2026. *
  2027. * Compare two URLs to see if they are the same, but ignore
  2028. * slight but hopefully insignificant differences such as if one
  2029. * is https and the other isn't, or if one is www.something and
  2030. * the other isn't - and also ignore case differences.
  2031. *
  2032. * Return true if the URLs match, otherwise false.
  2033. *
  2034. */
  2035. if(! function_exists('link_compare')) {
  2036. function link_compare($a,$b) {
  2037. if(strcasecmp(normalise_link($a),normalise_link($b)) === 0)
  2038. return true;
  2039. return false;
  2040. }}
  2041. if(! function_exists('prepare_body')) {
  2042. function prepare_body($item,$attach = false) {
  2043. $s = prepare_text($item['body']);
  2044. if(! $attach)
  2045. return $s;
  2046. $arr = explode(',',$item['attach']);
  2047. if(count($arr)) {
  2048. $s .= '<div class="body-attach">';
  2049. foreach($arr as $r) {
  2050. $matches = false;
  2051. $icon = '';
  2052. $cnt = preg_match('|\[attach\]href=\"(.*?)\" size=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"\[\/attach\]|',$r,$matches);
  2053. if($cnt) {
  2054. $icontype = strtolower(substr($matches[3],0,strpos($matches[3],'/')));
  2055. switch($icontype) {
  2056. case 'video':
  2057. case 'audio':
  2058. case 'image':
  2059. case 'text':
  2060. $icon = '<div class="attachtype type-' . $icontype . '"></div>';
  2061. break;
  2062. default:
  2063. $icon = '<div class="attachtype type-unkn"></div>';
  2064. break;
  2065. }
  2066. $title = ((strlen(trim($matches[4]))) ? escape_tags(trim($matches[4])) : escape_tags($matches[1]));
  2067. $title .= ' ' . $matches[2] . ' ' . t('bytes');
  2068. $s .= '<a href="' . strip_tags($matches[1]) . '" title="' . $title . '" class="attachlink" target="external-link" >' . $icon . '</a>';
  2069. }
  2070. }
  2071. $s .= '<div class="clear"></div></div>';
  2072. }
  2073. return $s;
  2074. }}
  2075. if(! function_exists('prepare_text')) {
  2076. function prepare_text($text) {
  2077. require_once('include/bbcode.php');
  2078. $s = smilies(bbcode($text));
  2079. return $s;
  2080. }}
  2081. /**
  2082. *
  2083. * Wrap calls to proc_close(proc_open()) and call hook
  2084. * so plugins can take part in process :)
  2085. *
  2086. * args:
  2087. * $cmd program to run
  2088. * next args are passed as $cmd command line
  2089. *
  2090. * e.g.: proc_run("ls","-la","/tmp");
  2091. *
  2092. * $cmd and string args are surrounded with ""
  2093. */
  2094. if(! function_exists('proc_run')) {
  2095. function proc_run($cmd){
  2096. $a = get_app();
  2097. $args = func_get_args();
  2098. call_hooks("proc_run", $args);
  2099. if(count($args) && $args[0] === 'php')
  2100. $args[0] = ((x($a->config,'php_path')) && (strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
  2101. foreach ($args as $arg){
  2102. $arg = escapeshellarg($arg);
  2103. }
  2104. $cmdline = implode($args," ");
  2105. proc_close(proc_open($cmdline." &",array(),$foo));
  2106. }}
  2107. if(! function_exists('current_theme')) {
  2108. function current_theme(){
  2109. $app_base_themes = array('duepuntozero', 'loozah');
  2110. $a = get_app();
  2111. $system_theme = ((isset($a->config['system']['theme'])) ? $a->config['system']['theme'] : '');
  2112. $theme_name = ((is_array($_SESSION) && x($_SESSION,'theme')) ? $_SESSION['theme'] : $system_theme);
  2113. if($theme_name && file_exists('view/theme/' . $theme_name . '/style.css'))
  2114. return($theme_name);
  2115. foreach($app_base_themes as $t) {
  2116. if(file_exists('view/theme/' . $t . '/style.css'))
  2117. return($t);
  2118. }
  2119. $fallback = glob('view/theme/*/style.css');
  2120. if(count($fallback))
  2121. return (str_replace('view/theme/','', str_replace("/style.css","",$fallback[0])));
  2122. }}
  2123. /*
  2124. * Return full URL to theme which is currently in effect.
  2125. * Provide a sane default if nothing is chosen or the specified theme does not exist.
  2126. */
  2127. if(! function_exists('current_theme_url')) {
  2128. function current_theme_url() {
  2129. global $a;
  2130. $t = current_theme();
  2131. return($a->get_baseurl() . '/view/theme/' . $t . '/style.css');
  2132. }}
  2133. if(! function_exists('feed_birthday')) {
  2134. function feed_birthday($uid,$tz) {
  2135. /**
  2136. *
  2137. * Determine the next birthday, but only if the birthday is published
  2138. * in the default profile. We _could_ also look for a private profile that the
  2139. * recipient can see, but somebody could get mad at us if they start getting
  2140. * public birthday greetings when they haven't made this info public.
  2141. *
  2142. * Assuming we are able to publish this info, we are then going to convert
  2143. * the start time from the owner's timezone to UTC.
  2144. *
  2145. * This will potentially solve the problem found with some social networks
  2146. * where birthdays are converted to the viewer's timezone and salutations from
  2147. * elsewhere in the world show up on the wrong day. We will convert it to the
  2148. * viewer's timezone also, but first we are going to convert it from the birthday
  2149. * person's timezone to GMT - so the viewer may find the birthday starting at
  2150. * 6:00PM the day before, but that will correspond to midnight to the birthday person.
  2151. *
  2152. */
  2153. $birthday = '';
  2154. $p = q("SELECT `dob` FROM `profile` WHERE `is-default` = 1 AND `uid` = %d LIMIT 1",
  2155. intval($uid)
  2156. );
  2157. if($p && count($p)) {
  2158. $tmp_dob = substr($p[0]['dob'],5);
  2159. if(intval($tmp_dob)) {
  2160. $y = datetime_convert($tz,$tz,'now','Y');
  2161. $bd = $y . '-' . $tmp_dob . ' 00:00';
  2162. $t_dob = strtotime($bd);
  2163. $now = strtotime(datetime_convert($tz,$tz,'now'));
  2164. if($t_dob < $now)
  2165. $bd = $y + 1 . '-' . $tmp_dob . ' 00:00';
  2166. $birthday = datetime_convert($tz,'UTC',$bd,ATOM_TIME);
  2167. }
  2168. }
  2169. return $birthday;
  2170. }}
  2171. /**
  2172. * return atom link elements for all of our hubs
  2173. */
  2174. if(! function_exists('feed_hublinks')) {
  2175. function feed_hublinks() {
  2176. $hub = get_config('system','huburl');
  2177. $hubxml = '';
  2178. if(strlen($hub)) {
  2179. $hubs = explode(',', $hub);
  2180. if(count($hubs)) {
  2181. foreach($hubs as $h) {
  2182. $h = trim($h);
  2183. if(! strlen($h))
  2184. continue;
  2185. $hubxml .= '<link rel="hub" href="' . xmlify($h) . '" />' . "\n" ;
  2186. }
  2187. }
  2188. }
  2189. return $hubxml;
  2190. }}
  2191. /* return atom link elements for salmon endpoints */
  2192. if(! function_exists('feed_salmonlinks')) {
  2193. function feed_salmonlinks($nick) {
  2194. $a = get_app();
  2195. $salmon = '<link rel="salmon" href="' . xmlify($a->get_baseurl() . '/salmon/' . $nick) . '" />' . "\n" ;
  2196. // old style links that status.net still needed as of 12/2010
  2197. $salmon .= ' <link rel="http://salmon-protocol.org/ns/salmon-replies" href="' . xmlify($a->get_baseurl() . '/salmon/' . $nick) . '" />' . "\n" ;
  2198. $salmon .= ' <link rel="http://salmon-protocol.org/ns/salmon-mention" href="' . xmlify($a->get_baseurl() . '/salmon/' . $nick) . '" />' . "\n" ;
  2199. return $salmon;
  2200. }}
  2201. if(! function_exists('get_plink')) {
  2202. function get_plink($item) {
  2203. $a = get_app();
  2204. $plink = (((x($item,'plink')) && (! $item['private'])) ? '<div class="wall-item-links-wrapper"><a href="'
  2205. . $item['plink'] . '" title="' . t('link to source') . '" target="external-link" class="icon remote-link"></a></div>' : '');
  2206. return $plink;
  2207. }}
  2208. if(! function_exists('unamp')) {
  2209. function unamp($s) {
  2210. return str_replace('&amp;', '&', $s);
  2211. }}
  2212. if(! function_exists('lang_selector')) {
  2213. function lang_selector() {
  2214. global $lang;
  2215. $o .= '<div id="lang-select-icon" class="icon language" title="' . t('Select an alternate language') . '" onclick="openClose(\'language-selector\');" ></div>';
  2216. $o .= '<div id="language-selector" style="display: none;" >';
  2217. $o .= '<form action="" method="post" ><select name="system_language" onchange="this.form.submit();" >';
  2218. $langs = glob('view/*/strings.php');
  2219. if(is_array($langs) && count($langs)) {
  2220. if(! in_array('view/en/strings.php',$langs))
  2221. $langs[] = 'view/en/';
  2222. asort($langs);
  2223. foreach($langs as $l) {
  2224. $ll = substr($l,5);
  2225. $ll = substr($ll,0,strrpos($ll,'/'));
  2226. $selected = (($ll === $lang) ? ' selected="selected" ' : '');
  2227. $o .= '<option value="' . $ll . '"' . $selected . '>' . $ll . '</option>';
  2228. }
  2229. }
  2230. $o .= '</select></form></div>';
  2231. return $o;
  2232. }}
  2233. if(! function_exists('parse_xml_string')) {
  2234. function parse_xml_string($s,$strict = true) {
  2235. if($strict) {
  2236. if(! strstr($s,'<?xml'))
  2237. return false;
  2238. $s2 = substr($s,strpos($s,'<?xml'));
  2239. }
  2240. else
  2241. $s2 = $s;
  2242. libxml_use_internal_errors(true);
  2243. $x = @simplexml_load_string($s2);
  2244. if(! $x) {
  2245. logger('libxml: parse: error: ' . $s2, LOGGER_DATA);
  2246. foreach(libxml_get_errors() as $err)
  2247. logger('libxml: parse: ' . $err->code." at ".$err->line.":".$err->column." : ".$err->message, LOGGER_DATA);
  2248. libxml_clear_errors();
  2249. }
  2250. return $x;
  2251. }}
  2252. if(! function_exists('is_site_admin')) {
  2253. function is_site_admin() {
  2254. $a = get_app();
  2255. if(local_user() && x($a->user,'email') && x($a->config,'admin_email') && ($a->user['email'] === $a->config['admin_email']))
  2256. return true;
  2257. return false;
  2258. }}
  2259. /*
  2260. * parse plugin comment in search of plugin infos.
  2261. * like
  2262. *
  2263. * * Name: Plugin
  2264. * * Description: A plugin which plugs in
  2265. * * Version: 1.2.3
  2266. * * Author: John <profile url>
  2267. * * Author: Jane <email>
  2268. * *
  2269. */
  2270. if (! function_exists('get_plugin_info')){
  2271. function get_plugin_info($plugin){
  2272. if (!is_file("addon/$plugin/$plugin.php")) return false;
  2273. $f = file_get_contents("addon/$plugin/$plugin.php");
  2274. $r = preg_match("|/\*.*\*/|msU", $f, $m);
  2275. $info=Array(
  2276. 'name' => $plugin,
  2277. 'description' => "",
  2278. 'author' => array(),
  2279. 'version' => ""
  2280. );
  2281. if ($r){
  2282. $ll = explode("\n", $m[0]);
  2283. foreach( $ll as $l ) {
  2284. $l = trim($l,"\t\n\r */");
  2285. if ($l!=""){
  2286. list($k,$v) = array_map("trim", explode(":",$l,2));
  2287. $k= strtolower($k);
  2288. if ($k=="author"){
  2289. $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
  2290. if ($r) {
  2291. $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
  2292. } else {
  2293. $info['author'][] = array('name'=>$v);
  2294. }
  2295. } else {
  2296. if (array_key_exists($k,$info)){
  2297. $info[$k]=$v;
  2298. }
  2299. }
  2300. }
  2301. }
  2302. }
  2303. return $info;
  2304. }}