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.
 
 
 
 
 
 

1178 lines
34 KiB

  1. <?php
  2. // This is our template processor.
  3. // $s is the string requiring macro substitution.
  4. // $r is an array of key value pairs (search => replace)
  5. // returns substituted string.
  6. // WARNING: this is pretty basic, and doesn't properly handle search strings that are substrings of each other.
  7. // For instance if 'test' => "foo" and 'testing' => "bar", testing could become either bar or fooing,
  8. // depending on the order in which they were declared in the array.
  9. require_once("include/template_processor.php");
  10. if(! function_exists('replace_macros')) {
  11. function replace_macros($s,$r) {
  12. global $t;
  13. return $t->replace($s,$r);
  14. }}
  15. // random string, there are 86 characters max in text mode, 128 for hex
  16. // output is urlsafe
  17. define('RANDOM_STRING_HEX', 0x00 );
  18. define('RANDOM_STRING_TEXT', 0x01 );
  19. if(! function_exists('random_string')) {
  20. function random_string($size = 64,$type = RANDOM_STRING_HEX) {
  21. // generate a bit of entropy and run it through the whirlpool
  22. $s = hash('whirlpool', (string) rand() . uniqid(rand(),true) . (string) rand(),(($type == RANDOM_STRING_TEXT) ? true : false));
  23. $s = (($type == RANDOM_STRING_TEXT) ? str_replace("\n","",base64url_encode($s,true)) : $s);
  24. return(substr($s,0,$size));
  25. }}
  26. /**
  27. * This is our primary input filter.
  28. *
  29. * The high bit hack only involved some old IE browser, forget which (IE5/Mac?)
  30. * that had an XSS attack vector due to stripping the high-bit on an 8-bit character
  31. * after cleansing, and angle chars with the high bit set could get through as markup.
  32. *
  33. * This is now disabled because it was interfering with some legitimate unicode sequences
  34. * and hopefully there aren't a lot of those browsers left.
  35. *
  36. * Use this on any text input where angle chars are not valid or permitted
  37. * They will be replaced with safer brackets. This may be filtered further
  38. * if these are not allowed either.
  39. *
  40. */
  41. if(! function_exists('notags')) {
  42. function notags($string) {
  43. return(str_replace(array("<",">"), array('[',']'), $string));
  44. // High-bit filter no longer used
  45. // return(str_replace(array("<",">","\xBA","\xBC","\xBE"), array('[',']','','',''), $string));
  46. }}
  47. // use this on "body" or "content" input where angle chars shouldn't be removed,
  48. // and allow them to be safely displayed.
  49. if(! function_exists('escape_tags')) {
  50. function escape_tags($string) {
  51. return(htmlspecialchars($string));
  52. }}
  53. // generate a string that's random, but usually pronounceable.
  54. // used to generate initial passwords
  55. if(! function_exists('autoname')) {
  56. function autoname($len) {
  57. $vowels = array('a','a','ai','au','e','e','e','ee','ea','i','ie','o','ou','u');
  58. if(mt_rand(0,5) == 4)
  59. $vowels[] = 'y';
  60. $cons = array(
  61. 'b','bl','br',
  62. 'c','ch','cl','cr',
  63. 'd','dr',
  64. 'f','fl','fr',
  65. 'g','gh','gl','gr',
  66. 'h',
  67. 'j',
  68. 'k','kh','kl','kr',
  69. 'l',
  70. 'm',
  71. 'n',
  72. 'p','ph','pl','pr',
  73. 'qu',
  74. 'r','rh',
  75. 's','sc','sh','sm','sp','st',
  76. 't','th','tr',
  77. 'v',
  78. 'w','wh',
  79. 'x',
  80. 'z','zh'
  81. );
  82. $midcons = array('ck','ct','gn','ld','lf','lm','lt','mb','mm', 'mn','mp',
  83. 'nd','ng','nk','nt','rn','rp','rt');
  84. $noend = array('bl', 'br', 'cl','cr','dr','fl','fr','gl','gr',
  85. 'kh', 'kl','kr','mn','pl','pr','rh','tr','qu','wh');
  86. $start = mt_rand(0,2);
  87. if($start == 0)
  88. $table = $vowels;
  89. else
  90. $table = $cons;
  91. $word = '';
  92. for ($x = 0; $x < $len; $x ++) {
  93. $r = mt_rand(0,count($table) - 1);
  94. $word .= $table[$r];
  95. if($table == $vowels)
  96. $table = array_merge($cons,$midcons);
  97. else
  98. $table = $vowels;
  99. }
  100. $word = substr($word,0,$len);
  101. foreach($noend as $noe) {
  102. if((strlen($word) > 2) && (substr($word,-2) == $noe)) {
  103. $word = substr($word,0,-1);
  104. break;
  105. }
  106. }
  107. if(substr($word,-1) == 'q')
  108. $word = substr($word,0,-1);
  109. return $word;
  110. }}
  111. // escape text ($str) for XML transport
  112. // returns escaped text.
  113. if(! function_exists('xmlify')) {
  114. function xmlify($str) {
  115. $buffer = '';
  116. for($x = 0; $x < mb_strlen($str); $x ++) {
  117. $char = $str[$x];
  118. switch( $char ) {
  119. case "\r" :
  120. break;
  121. case "&" :
  122. $buffer .= '&amp;';
  123. break;
  124. case "'" :
  125. $buffer .= '&apos;';
  126. break;
  127. case "\"" :
  128. $buffer .= '&quot;';
  129. break;
  130. case '<' :
  131. $buffer .= '&lt;';
  132. break;
  133. case '>' :
  134. $buffer .= '&gt;';
  135. break;
  136. case "\n" :
  137. $buffer .= "\n";
  138. break;
  139. default :
  140. $buffer .= $char;
  141. break;
  142. }
  143. }
  144. $buffer = trim($buffer);
  145. return($buffer);
  146. }}
  147. // undo an xmlify
  148. // pass xml escaped text ($s), returns unescaped text
  149. if(! function_exists('unxmlify')) {
  150. function unxmlify($s) {
  151. $ret = str_replace('&amp;','&', $s);
  152. $ret = str_replace(array('&lt;','&gt;','&quot;','&apos;'),array('<','>','"',"'"),$ret);
  153. return $ret;
  154. }}
  155. // convenience wrapper, reverse the operation "bin2hex"
  156. if(! function_exists('hex2bin')) {
  157. function hex2bin($s) {
  158. if(! (is_string($s) && strlen($s)))
  159. return '';
  160. if(! ctype_xdigit($s)) {
  161. logger('hex2bin: illegal input: ' . print_r(debug_backtrace(), true));
  162. return($s);
  163. }
  164. return(pack("H*",$s));
  165. }}
  166. // Automatic pagination.
  167. // To use, get the count of total items.
  168. // Then call $a->set_pager_total($number_items);
  169. // Optionally call $a->set_pager_itemspage($n) to the number of items to display on each page
  170. // Then call paginate($a) after the end of the display loop to insert the pager block on the page
  171. // (assuming there are enough items to paginate).
  172. // When using with SQL, the setting LIMIT %d, %d => $a->pager['start'],$a->pager['itemspage']
  173. // will limit the results to the correct items for the current page.
  174. // The actual page handling is then accomplished at the application layer.
  175. if(! function_exists('paginate')) {
  176. function paginate(&$a) {
  177. $o = '';
  178. $stripped = preg_replace('/(&page=[0-9]*)/','',$a->query_string);
  179. $stripped = str_replace('q=','',$stripped);
  180. $stripped = trim($stripped,'/');
  181. $pagenum = $a->pager['page'];
  182. $url = $a->get_baseurl() . '/' . $stripped;
  183. if($a->pager['total'] > $a->pager['itemspage']) {
  184. $o .= '<div class="pager">';
  185. if($a->pager['page'] != 1)
  186. $o .= '<span class="pager_prev">'."<a href=\"$url".'&page='.($a->pager['page'] - 1).'">' . t('prev') . '</a></span> ';
  187. $o .= "<span class=\"pager_first\"><a href=\"$url"."&page=1\">" . t('first') . "</a></span> ";
  188. $numpages = $a->pager['total'] / $a->pager['itemspage'];
  189. $numstart = 1;
  190. $numstop = $numpages;
  191. if($numpages > 14) {
  192. $numstart = (($pagenum > 7) ? ($pagenum - 7) : 1);
  193. $numstop = (($pagenum > ($numpages - 7)) ? $numpages : ($numstart + 14));
  194. }
  195. for($i = $numstart; $i <= $numstop; $i++){
  196. if($i == $a->pager['page'])
  197. $o .= '<span class="pager_current">'.(($i < 10) ? '&nbsp;'.$i : $i);
  198. else
  199. $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? '&nbsp;'.$i : $i)."</a>";
  200. $o .= '</span> ';
  201. }
  202. if(($a->pager['total'] % $a->pager['itemspage']) != 0) {
  203. if($i == $a->pager['page'])
  204. $o .= '<span class="pager_current">'.(($i < 10) ? '&nbsp;'.$i : $i);
  205. else
  206. $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? '&nbsp;'.$i : $i)."</a>";
  207. $o .= '</span> ';
  208. }
  209. $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
  210. $o .= "<span class=\"pager_last\"><a href=\"$url"."&page=$lastpage\">" . t('last') . "</a></span> ";
  211. if(($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0)
  212. $o .= '<span class="pager_next">'."<a href=\"$url"."&page=".($a->pager['page'] + 1).'">' . t('next') . '</a></span>';
  213. $o .= '</div>'."\r\n";
  214. }
  215. return $o;
  216. }}
  217. // Turn user/group ACLs stored as angle bracketed text into arrays
  218. if(! function_exists('expand_acl')) {
  219. function expand_acl($s) {
  220. // turn string array of angle-bracketed elements into numeric array
  221. // e.g. "<1><2><3>" => array(1,2,3);
  222. $ret = array();
  223. if(strlen($s)) {
  224. $t = str_replace('<','',$s);
  225. $a = explode('>',$t);
  226. foreach($a as $aa) {
  227. if(intval($aa))
  228. $ret[] = intval($aa);
  229. }
  230. }
  231. return $ret;
  232. }}
  233. // Used to wrap ACL elements in angle brackets for storage
  234. if(! function_exists('sanitise_acl')) {
  235. function sanitise_acl(&$item) {
  236. if(intval($item))
  237. $item = '<' . intval(notags(trim($item))) . '>';
  238. else
  239. unset($item);
  240. }}
  241. // Convert an ACL array to a storable string
  242. if(! function_exists('perms2str')) {
  243. function perms2str($p) {
  244. $ret = '';
  245. $tmp = $p;
  246. if(is_array($tmp)) {
  247. array_walk($tmp,'sanitise_acl');
  248. $ret = implode('',$tmp);
  249. }
  250. return $ret;
  251. }}
  252. // generate a guaranteed unique (for this domain) item ID for ATOM
  253. // safe from birthday paradox
  254. if(! function_exists('item_new_uri')) {
  255. function item_new_uri($hostname,$uid) {
  256. do {
  257. $dups = false;
  258. $hash = random_string();
  259. $uri = "urn:X-dfrn:" . $hostname . ':' . $uid . ':' . $hash;
  260. $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
  261. dbesc($uri));
  262. if(count($r))
  263. $dups = true;
  264. } while($dups == true);
  265. return $uri;
  266. }}
  267. // Generate a guaranteed unique photo ID.
  268. // safe from birthday paradox
  269. if(! function_exists('photo_new_resource')) {
  270. function photo_new_resource() {
  271. do {
  272. $found = false;
  273. $resource = hash('md5',uniqid(mt_rand(),true));
  274. $r = q("SELECT `id` FROM `photo` WHERE `resource-id` = '%s' LIMIT 1",
  275. dbesc($resource)
  276. );
  277. if(count($r))
  278. $found = true;
  279. } while($found == true);
  280. return $resource;
  281. }}
  282. // wrapper to load a view template, checking for alternate
  283. // languages before falling back to the default
  284. // obsolete, deprecated.
  285. if(! function_exists('load_view_file')) {
  286. function load_view_file($s) {
  287. global $lang, $a;
  288. if(! isset($lang))
  289. $lang = 'en';
  290. $b = basename($s);
  291. $d = dirname($s);
  292. if(file_exists("$d/$lang/$b"))
  293. return file_get_contents("$d/$lang/$b");
  294. $theme = current_theme();
  295. if(file_exists("$d/theme/$theme/$b"))
  296. return file_get_contents("$d/theme/$theme/$b");
  297. return file_get_contents($s);
  298. }}
  299. if(! function_exists('get_intltext_template')) {
  300. function get_intltext_template($s) {
  301. global $lang;
  302. if(! isset($lang))
  303. $lang = 'en';
  304. if(file_exists("view/$lang/$s"))
  305. return file_get_contents("view/$lang/$s");
  306. elseif(file_exists("view/en/$s"))
  307. return file_get_contents("view/en/$s");
  308. else
  309. return file_get_contents("view/$s");
  310. }}
  311. if(! function_exists('get_markup_template')) {
  312. function get_markup_template($s) {
  313. $a=get_app();
  314. $theme = current_theme();
  315. if(file_exists("view/theme/$theme/$s"))
  316. return file_get_contents("view/theme/$theme/$s");
  317. elseif (x($a->theme_info,"extends") && file_exists("view/theme/".$a->theme_info["extends"]."/$s"))
  318. return file_get_contents("view/theme/".$a->theme_info["extends"]."/$s");
  319. else
  320. return file_get_contents("view/$s");
  321. }}
  322. // for html,xml parsing - let's say you've got
  323. // an attribute foobar="class1 class2 class3"
  324. // and you want to find out if it contains 'class3'.
  325. // you can't use a normal sub string search because you
  326. // might match 'notclass3' and a regex to do the job is
  327. // possible but a bit complicated.
  328. // pass the attribute string as $attr and the attribute you
  329. // are looking for as $s - returns true if found, otherwise false
  330. if(! function_exists('attribute_contains')) {
  331. function attribute_contains($attr,$s) {
  332. $a = explode(' ', $attr);
  333. if(count($a) && in_array($s,$a))
  334. return true;
  335. return false;
  336. }}
  337. if(! function_exists('logger')) {
  338. function logger($msg,$level = 0) {
  339. // turn off logger in install mode
  340. global $a;
  341. global $db;
  342. if(($a->module == 'install') || (! ($db && $db->connected))) return;
  343. $debugging = get_config('system','debugging');
  344. $loglevel = intval(get_config('system','loglevel'));
  345. $logfile = get_config('system','logfile');
  346. if((! $debugging) || (! $logfile) || ($level > $loglevel))
  347. return;
  348. @file_put_contents($logfile, datetime_convert() . ':' . session_id() . ' ' . $msg . "\n", FILE_APPEND);
  349. return;
  350. }}
  351. if(! function_exists('activity_match')) {
  352. function activity_match($haystack,$needle) {
  353. if(($haystack === $needle) || ((basename($needle) === $haystack) && strstr($needle,NAMESPACE_ACTIVITY_SCHEMA)))
  354. return true;
  355. return false;
  356. }}
  357. // Pull out all #hashtags and @person tags from $s;
  358. // We also get @person@domain.com - which would make
  359. // the regex quite complicated as tags can also
  360. // end a sentence. So we'll run through our results
  361. // and strip the period from any tags which end with one.
  362. // Returns array of tags found, or empty array.
  363. if(! function_exists('get_tags')) {
  364. function get_tags($s) {
  365. $ret = array();
  366. // ignore anything in a code block
  367. $s = preg_replace('/\[code\](.*?)\[\/code\]/sm','',$s);
  368. // Match full names against @tags including the space between first and last
  369. // We will look these up afterward to see if they are full names or not recognisable.
  370. if(preg_match_all('/(@[^ \x0D\x0A,:?]+ [^ \x0D\x0A@,:?]+)([ \x0D\x0A@,:?]|$)/',$s,$match)) {
  371. foreach($match[1] as $mtch) {
  372. if(strstr($mtch,"]")) {
  373. // we might be inside a bbcode color tag - leave it alone
  374. continue;
  375. }
  376. if(substr($mtch,-1,1) === '.')
  377. $ret[] = substr($mtch,0,-1);
  378. else
  379. $ret[] = $mtch;
  380. }
  381. }
  382. // Otherwise pull out single word tags. These can be @nickname, @first_last
  383. // and #hash tags.
  384. if(preg_match_all('/([@#][^ \x0D\x0A,;:?]+)([ \x0D\x0A,;:?]|$)/',$s,$match)) {
  385. foreach($match[1] as $mtch) {
  386. if(strstr($mtch,"]")) {
  387. // we might be inside a bbcode color tag - leave it alone
  388. continue;
  389. }
  390. if(substr($mtch,-1,1) === '.')
  391. $mtch = substr($mtch,0,-1);
  392. // ignore strictly numeric tags like #1
  393. if((strpos($mtch,'#') === 0) && ctype_digit(substr($mtch,1)))
  394. continue;
  395. // try not to catch url fragments
  396. if(strpos($s,$mtch) && preg_match('/[a-zA-z0-9\/]/',substr($s,strpos($s,$mtch)-1,1)))
  397. continue;
  398. $ret[] = $mtch;
  399. }
  400. }
  401. return $ret;
  402. }}
  403. // quick and dirty quoted_printable encoding
  404. if(! function_exists('qp')) {
  405. function qp($s) {
  406. return str_replace ("%","=",rawurlencode($s));
  407. }}
  408. if(! function_exists('get_mentions')) {
  409. function get_mentions($item) {
  410. $o = '';
  411. if(! strlen($item['tag']))
  412. return $o;
  413. $arr = explode(',',$item['tag']);
  414. foreach($arr as $x) {
  415. $matches = null;
  416. if(preg_match('/@\[url=([^\]]*)\]/',$x,$matches)) {
  417. $o .= "\t\t" . '<link rel="mentioned" href="' . $matches[1] . '" />' . "\r\n";
  418. $o .= "\t\t" . '<link rel="ostatus:attention" href="' . $matches[1] . '" />' . "\r\n";
  419. }
  420. }
  421. return $o;
  422. }}
  423. if(! function_exists('contact_block')) {
  424. function contact_block() {
  425. $o = '';
  426. $a = get_app();
  427. $shown = get_pconfig($a->profile['uid'],'system','display_friend_count');
  428. if($shown === false)
  429. $shown = 24;
  430. if($shown == 0)
  431. return;
  432. if((! is_array($a->profile)) || ($a->profile['hide-friends']))
  433. return $o;
  434. $r = q("SELECT COUNT(*) AS `total` FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 and `pending` = 0 AND `hidden` = 0",
  435. intval($a->profile['uid'])
  436. );
  437. if(count($r)) {
  438. $total = intval($r[0]['total']);
  439. }
  440. if(! $total) {
  441. $contacts = t('No contacts');
  442. $micropro = Null;
  443. } else {
  444. $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 and `pending` = 0 AND `hidden` = 0 ORDER BY RAND() LIMIT %d",
  445. intval($a->profile['uid']),
  446. intval($shown)
  447. );
  448. if(count($r)) {
  449. $contacts = sprintf( tt('%d Contact','%d Contacts', $total),$total);
  450. $micropro = Array();
  451. foreach($r as $rr) {
  452. $micropro[] = micropro($rr,true,'mpfriend');
  453. }
  454. }
  455. }
  456. $tpl = get_markup_template('contact_block.tpl');
  457. $o = replace_macros($tpl, array(
  458. '$contacts' => $contacts,
  459. '$nickname' => $a->profile['nickname'],
  460. '$viewcontacts' => t('View Contacts'),
  461. '$micropro' => $micropro,
  462. ));
  463. $arr = array('contacts' => $r, 'output' => $o);
  464. call_hooks('contact_block_end', $arr);
  465. return $o;
  466. }}
  467. if(! function_exists('micropro')) {
  468. function micropro($contact, $redirect = false, $class = '', $textmode = false) {
  469. if($class)
  470. $class = ' ' . $class;
  471. $url = $contact['url'];
  472. $sparkle = '';
  473. $redir = false;
  474. if($redirect) {
  475. $a = get_app();
  476. $redirect_url = $a->get_baseurl() . '/redir/' . $contact['id'];
  477. if(local_user() && ($contact['uid'] == local_user()) && ($contact['network'] === 'dfrn')) {
  478. $redir = true;
  479. $url = $redirect_url;
  480. $sparkle = ' sparkle';
  481. }
  482. }
  483. $click = ((x($contact,'click')) ? ' onclick="' . $contact['click'] . '" ' : '');
  484. if($click)
  485. $url = '';
  486. if($textmode) {
  487. return '<div class="contact-block-textdiv' . $class . '"><a class="contact-block-link' . $class . $sparkle
  488. . (($click) ? ' fakelink' : '') . '" '
  489. . (($redir) ? ' target="redir" ' : '')
  490. . (($url) ? ' href="' . $url . '"' : '') . $click
  491. . '" title="' . $contact['name'] . ' [' . $contact['url'] . ']" alt="' . $contact['name']
  492. . '" >'. $contact['name'] . '</a></div>' . "\r\n";
  493. }
  494. else {
  495. return '<div class="contact-block-div' . $class . '"><a class="contact-block-link' . $class . $sparkle
  496. . (($click) ? ' fakelink' : '') . '" '
  497. . (($redir) ? ' target="redir" ' : '')
  498. . (($url) ? ' href="' . $url . '"' : '') . $click . ' ><img class="contact-block-img' . $class . $sparkle . '" src="'
  499. . $contact['micro'] . '" title="' . $contact['name'] . ' [' . $contact['url'] . ']" alt="' . $contact['name']
  500. . '" /></a></div>' . "\r\n";
  501. }
  502. }}
  503. if(! function_exists('search')) {
  504. function search($s,$id='search-box',$url='/search',$save = false) {
  505. $a = get_app();
  506. $o = '<div id="' . $id . '">';
  507. $o .= '<form action="' . $a->get_baseurl() . $url . '" method="get" >';
  508. $o .= '<input type="text" name="search" id="search-text" value="' . $s .'" />';
  509. $o .= '<input type="submit" name="submit" id="search-submit" value="' . t('Search') . '" />';
  510. if($save)
  511. $o .= '<input type="submit" name="save" id="search-save" value="' . t('Save') . '" />';
  512. $o .= '</form></div>';
  513. return $o;
  514. }}
  515. if(! function_exists('valid_email')) {
  516. function valid_email($x){
  517. if(preg_match('/^[_a-zA-Z0-9\-\+]+(\.[_a-zA-Z0-9\-\+]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+$/',$x))
  518. return true;
  519. return false;
  520. }}
  521. /**
  522. *
  523. * Function: linkify
  524. *
  525. * Replace naked text hyperlink with HTML formatted hyperlink
  526. *
  527. */
  528. if(! function_exists('linkify')) {
  529. function linkify($s) {
  530. $s = preg_replace("/(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\'\%\$\!\+]*)/", ' <a href="$1" target="external-link">$1</a>', $s);
  531. $s = preg_replace("/\<(.*?)(src|href)=(.*?)\&amp\;(.*?)\>/ism",'<$1$2=$3&$4>',$s);
  532. return($s);
  533. }}
  534. /**
  535. *
  536. * Function: smilies
  537. *
  538. * Description:
  539. * Replaces text emoticons with graphical images
  540. *
  541. * @Parameter: string $s
  542. *
  543. * Returns string
  544. */
  545. if(! function_exists('smilies')) {
  546. function smilies($s, $sample = false) {
  547. $a = get_app();
  548. $texts = array(
  549. '&lt;3',
  550. '&lt;/3',
  551. '&lt;\\3',
  552. ':-)',
  553. ':)',
  554. ';-)',
  555. ';)',
  556. ':-(',
  557. ':(',
  558. ':-P',
  559. ':P',
  560. ':-"',
  561. ':-&quot;',
  562. ':-x',
  563. ':-X',
  564. ':-D',
  565. ':D',
  566. '8-|',
  567. '8-O',
  568. ':-O',
  569. '\\o/',
  570. 'o.O',
  571. 'O.o',
  572. '\\.../',
  573. '\\ooo/',
  574. ":'(",
  575. ":-!",
  576. ":-/",
  577. ":-[",
  578. "8-)",
  579. ':beer',
  580. ':homebrew',
  581. ':coffee',
  582. ':facepalm',
  583. '~friendika',
  584. '~friendica',
  585. 'Diaspora*'
  586. );
  587. $icons = array(
  588. '<img src="' . $a->get_baseurl() . '/images/smiley-heart.gif" alt="<3" />',
  589. '<img src="' . $a->get_baseurl() . '/images/smiley-brokenheart.gif" alt="</3" />',
  590. '<img src="' . $a->get_baseurl() . '/images/smiley-brokenheart.gif" alt="<\\3" />',
  591. '<img src="' . $a->get_baseurl() . '/images/smiley-smile.gif" alt=":-)" />',
  592. '<img src="' . $a->get_baseurl() . '/images/smiley-smile.gif" alt=":)" />',
  593. '<img src="' . $a->get_baseurl() . '/images/smiley-wink.gif" alt=";-)" />',
  594. '<img src="' . $a->get_baseurl() . '/images/smiley-wink.gif" alt=";)"/>',
  595. '<img src="' . $a->get_baseurl() . '/images/smiley-frown.gif" alt=":-(" />',
  596. '<img src="' . $a->get_baseurl() . '/images/smiley-frown.gif" alt=":(" />',
  597. '<img src="' . $a->get_baseurl() . '/images/smiley-tongue-out.gif" alt=":-P" />',
  598. '<img src="' . $a->get_baseurl() . '/images/smiley-tongue-out.gif" alt=":P" />',
  599. '<img src="' . $a->get_baseurl() . '/images/smiley-kiss.gif" alt=":-\"" />',
  600. '<img src="' . $a->get_baseurl() . '/images/smiley-kiss.gif" alt=":-\"" />',
  601. '<img src="' . $a->get_baseurl() . '/images/smiley-kiss.gif" alt=":-x" />',
  602. '<img src="' . $a->get_baseurl() . '/images/smiley-kiss.gif" alt=":-X" />',
  603. '<img src="' . $a->get_baseurl() . '/images/smiley-laughing.gif" alt=":-D" />',
  604. '<img src="' . $a->get_baseurl() . '/images/smiley-laughing.gif" alt=":D"/>',
  605. '<img src="' . $a->get_baseurl() . '/images/smiley-surprised.gif" alt="8-|" />',
  606. '<img src="' . $a->get_baseurl() . '/images/smiley-surprised.gif" alt="8-O" />',
  607. '<img src="' . $a->get_baseurl() . '/images/smiley-surprised.gif" alt=":-O" />',
  608. '<img src="' . $a->get_baseurl() . '/images/smiley-thumbsup.gif" alt="\\o/" />',
  609. '<img src="' . $a->get_baseurl() . '/images/smiley-Oo.gif" alt="o.O" />',
  610. '<img src="' . $a->get_baseurl() . '/images/smiley-Oo.gif" alt="O.o" />',
  611. '<img src="' . $a->get_baseurl() . '/images/smiley-shaka.gif" alt="\\.../" />',
  612. '<img src="' . $a->get_baseurl() . '/images/smiley-shaka.gif" alt="\\ooo/" />',
  613. '<img src="' . $a->get_baseurl() . '/images/smiley-cry.gif" alt=":\'(" />',
  614. '<img src="' . $a->get_baseurl() . '/images/smiley-foot-in-mouth.gif" alt=":-!" />',
  615. '<img src="' . $a->get_baseurl() . '/images/smiley-undecided.gif" alt=":-/" />',
  616. '<img src="' . $a->get_baseurl() . '/images/smiley-embarassed.gif" alt=":-[" />',
  617. '<img src="' . $a->get_baseurl() . '/images/smiley-cool.gif" alt="8-)" />',
  618. '<img src="' . $a->get_baseurl() . '/images/beer_mug.gif" alt=":beer" />',
  619. '<img src="' . $a->get_baseurl() . '/images/beer_mug.gif" alt=":homebrew" />',
  620. '<img src="' . $a->get_baseurl() . '/images/coffee.gif" alt=":coffee" />',
  621. '<img src="' . $a->get_baseurl() . '/images/smiley-facepalm.gif" alt=":facepalm" />',
  622. '<a href="http://project.friendika.com">~friendika <img src="' . $a->get_baseurl() . '/images/friendika-16.png" alt="~friendika" /></a>',
  623. '<a href="http://friendica.com">~friendica <img src="' . $a->get_baseurl() . '/images/friendika-16.png" alt="~friendica" /></a>',
  624. '<a href="http://diasporafoundation.org">Diaspora<img src="' . $a->get_baseurl() . '/images/diaspora.png" alt="Diaspora*" /></a>',
  625. );
  626. $params = array('texts' => $texts, 'icons' => $icons, 'string' => $s);
  627. call_hooks('smilie', $params);
  628. if($sample) {
  629. $s = '<div class="smiley-sample">';
  630. for($x = 0; $x < count($params['texts']); $x ++) {
  631. $s .= '<dl><dt>' . $params['texts'][$x] . '</dt><dd>' . $params['icons'][$x] . '</dd></dl>';
  632. }
  633. }
  634. else {
  635. $s = str_replace($params['texts'],$params['icons'],$params['string']);
  636. }
  637. return $s;
  638. }}
  639. if(! function_exists('day_translate')) {
  640. function day_translate($s) {
  641. $ret = str_replace(array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'),
  642. array( t('Monday'), t('Tuesday'), t('Wednesday'), t('Thursday'), t('Friday'), t('Saturday'), t('Sunday')),
  643. $s);
  644. $ret = str_replace(array('January','February','March','April','May','June','July','August','September','October','November','December'),
  645. 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')),
  646. $ret);
  647. return $ret;
  648. }}
  649. if(! function_exists('normalise_link')) {
  650. function normalise_link($url) {
  651. $ret = str_replace(array('https:','//www.'), array('http:','//'), $url);
  652. return(rtrim($ret,'/'));
  653. }}
  654. /**
  655. *
  656. * Compare two URLs to see if they are the same, but ignore
  657. * slight but hopefully insignificant differences such as if one
  658. * is https and the other isn't, or if one is www.something and
  659. * the other isn't - and also ignore case differences.
  660. *
  661. * Return true if the URLs match, otherwise false.
  662. *
  663. */
  664. if(! function_exists('link_compare')) {
  665. function link_compare($a,$b) {
  666. if(strcasecmp(normalise_link($a),normalise_link($b)) === 0)
  667. return true;
  668. return false;
  669. }}
  670. // Given an item array, convert the body element from bbcode to html and add smilie icons.
  671. // If attach is true, also add icons for item attachments
  672. if(! function_exists('prepare_body')) {
  673. function prepare_body($item,$attach = false) {
  674. call_hooks('prepare_body_init', $item);
  675. $s = prepare_text($item['body']);
  676. $prep_arr = array('item' => $item, 'html' => $s);
  677. call_hooks('prepare_body', $prep_arr);
  678. $s = $prep_arr['html'];
  679. if(! $attach)
  680. return $s;
  681. $arr = explode(',',$item['attach']);
  682. if(count($arr)) {
  683. $s .= '<div class="body-attach">';
  684. foreach($arr as $r) {
  685. $matches = false;
  686. $icon = '';
  687. $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"\[\/attach\]|',$r,$matches);
  688. if($cnt) {
  689. $icontype = strtolower(substr($matches[3],0,strpos($matches[3],'/')));
  690. switch($icontype) {
  691. case 'video':
  692. case 'audio':
  693. case 'image':
  694. case 'text':
  695. $icon = '<div class="attachtype icon s22 type-' . $icontype . '"></div>';
  696. break;
  697. default:
  698. $icon = '<div class="attachtype icon s22 type-unkn"></div>';
  699. break;
  700. }
  701. $title = ((strlen(trim($matches[4]))) ? escape_tags(trim($matches[4])) : escape_tags($matches[1]));
  702. $title .= ' ' . $matches[2] . ' ' . t('bytes');
  703. $s .= '<a href="' . strip_tags($matches[1]) . '" title="' . $title . '" class="attachlink" target="external-link" >' . $icon . '</a>';
  704. }
  705. }
  706. $s .= '<div class="clear"></div></div>';
  707. }
  708. $prep_arr = array('item' => $item, 'html' => $s);
  709. call_hooks('prepare_body_final', $prep_arr);
  710. return $prep_arr['html'];
  711. }}
  712. // Given a text string, convert from bbcode to html and add smilie icons.
  713. if(! function_exists('prepare_text')) {
  714. function prepare_text($text) {
  715. require_once('include/bbcode.php');
  716. if(stristr($text,'[nosmile]'))
  717. $s = bbcode($text);
  718. else
  719. $s = smilies(bbcode($text));
  720. return $s;
  721. }}
  722. /**
  723. * return atom link elements for all of our hubs
  724. */
  725. if(! function_exists('feed_hublinks')) {
  726. function feed_hublinks() {
  727. $hub = get_config('system','huburl');
  728. $hubxml = '';
  729. if(strlen($hub)) {
  730. $hubs = explode(',', $hub);
  731. if(count($hubs)) {
  732. foreach($hubs as $h) {
  733. $h = trim($h);
  734. if(! strlen($h))
  735. continue;
  736. $hubxml .= '<link rel="hub" href="' . xmlify($h) . '" />' . "\n" ;
  737. }
  738. }
  739. }
  740. return $hubxml;
  741. }}
  742. /* return atom link elements for salmon endpoints */
  743. if(! function_exists('feed_salmonlinks')) {
  744. function feed_salmonlinks($nick) {
  745. $a = get_app();
  746. $salmon = '<link rel="salmon" href="' . xmlify($a->get_baseurl() . '/salmon/' . $nick) . '" />' . "\n" ;
  747. // old style links that status.net still needed as of 12/2010
  748. $salmon .= ' <link rel="http://salmon-protocol.org/ns/salmon-replies" href="' . xmlify($a->get_baseurl() . '/salmon/' . $nick) . '" />' . "\n" ;
  749. $salmon .= ' <link rel="http://salmon-protocol.org/ns/salmon-mention" href="' . xmlify($a->get_baseurl() . '/salmon/' . $nick) . '" />' . "\n" ;
  750. return $salmon;
  751. }}
  752. if(! function_exists('get_plink')) {
  753. function get_plink($item) {
  754. $a = get_app();
  755. if (x($item,'plink') && (! $item['private'])){
  756. return array(
  757. 'href' => $item['plink'],
  758. 'title' => t('link to source'),
  759. );
  760. } else {
  761. return false;
  762. }
  763. }}
  764. if(! function_exists('unamp')) {
  765. function unamp($s) {
  766. return str_replace('&amp;', '&', $s);
  767. }}
  768. if(! function_exists('lang_selector')) {
  769. function lang_selector() {
  770. global $lang;
  771. $o = '<div id="lang-select-icon" class="icon language" title="' . t('Select an alternate language') . '" onclick="openClose(\'language-selector\');" ></div>';
  772. $o .= '<div id="language-selector" style="display: none;" >';
  773. $o .= '<form action="#" method="post" ><select name="system_language" onchange="this.form.submit();" >';
  774. $langs = glob('view/*/strings.php');
  775. if(is_array($langs) && count($langs)) {
  776. $langs[] = '';
  777. if(! in_array('view/en/strings.php',$langs))
  778. $langs[] = 'view/en/';
  779. asort($langs);
  780. foreach($langs as $l) {
  781. if($l == '') {
  782. $default_selected = ((! x($_SESSION,'language')) ? ' selected="selected" ' : '');
  783. $o .= '<option value="" ' . $default_selected . '>' . t('default') . '</option>';
  784. continue;
  785. }
  786. $ll = substr($l,5);
  787. $ll = substr($ll,0,strrpos($ll,'/'));
  788. $selected = (($ll === $lang && (x($_SESSION['language']))) ? ' selected="selected" ' : '');
  789. $o .= '<option value="' . $ll . '"' . $selected . '>' . $ll . '</option>';
  790. }
  791. }
  792. $o .= '</select></form></div>';
  793. return $o;
  794. }}
  795. if(! function_exists('return_bytes')) {
  796. function return_bytes ($size_str) {
  797. switch (substr ($size_str, -1))
  798. {
  799. case 'M': case 'm': return (int)$size_str * 1048576;
  800. case 'K': case 'k': return (int)$size_str * 1024;
  801. case 'G': case 'g': return (int)$size_str * 1073741824;
  802. default: return $size_str;
  803. }
  804. }}
  805. function generate_user_guid() {
  806. $found = true;
  807. do {
  808. $guid = random_string(16);
  809. $x = q("SELECT `uid` FROM `user` WHERE `guid` = '%s' LIMIT 1",
  810. dbesc($guid)
  811. );
  812. if(! count($x))
  813. $found = false;
  814. } while ($found == true );
  815. return $guid;
  816. }
  817. function base64url_encode($s, $strip_padding = false) {
  818. $s = strtr(base64_encode($s),'+/','-_');
  819. if($strip_padding)
  820. $s = str_replace('=','',$s);
  821. return $s;
  822. }
  823. function base64url_decode($s) {
  824. if(is_array($s)) {
  825. logger('base64url_decode: illegal input: ' . print_r(debug_backtrace(), true));
  826. return $s;
  827. }
  828. /*
  829. * // Placeholder for new rev of salmon which strips base64 padding.
  830. * // PHP base64_decode handles the un-padded input without requiring this step
  831. * // Uncomment if you find you need it.
  832. *
  833. * $l = strlen($s);
  834. * if(! strpos($s,'=')) {
  835. * $m = $l % 4;
  836. * if($m == 2)
  837. * $s .= '==';
  838. * if($m == 3)
  839. * $s .= '=';
  840. * }
  841. *
  842. */
  843. return base64_decode(strtr($s,'-_','+/'));
  844. }
  845. if (!function_exists('str_getcsv')) {
  846. function str_getcsv($input, $delimiter = ',', $enclosure = '"', $escape = '\\', $eol = '\n') {
  847. if (is_string($input) && !empty($input)) {
  848. $output = array();
  849. $tmp = preg_split("/".$eol."/",$input);
  850. if (is_array($tmp) && !empty($tmp)) {
  851. while (list($line_num, $line) = each($tmp)) {
  852. if (preg_match("/".$escape.$enclosure."/",$line)) {
  853. while ($strlen = strlen($line)) {
  854. $pos_delimiter = strpos($line,$delimiter);
  855. $pos_enclosure_start = strpos($line,$enclosure);
  856. if (
  857. is_int($pos_delimiter) && is_int($pos_enclosure_start)
  858. && ($pos_enclosure_start < $pos_delimiter)
  859. ) {
  860. $enclosed_str = substr($line,1);
  861. $pos_enclosure_end = strpos($enclosed_str,$enclosure);
  862. $enclosed_str = substr($enclosed_str,0,$pos_enclosure_end);
  863. $output[$line_num][] = $enclosed_str;
  864. $offset = $pos_enclosure_end+3;
  865. } else {
  866. if (empty($pos_delimiter) && empty($pos_enclosure_start)) {
  867. $output[$line_num][] = substr($line,0);
  868. $offset = strlen($line);
  869. } else {
  870. $output[$line_num][] = substr($line,0,$pos_delimiter);
  871. $offset = (
  872. !empty($pos_enclosure_start)
  873. && ($pos_enclosure_start < $pos_delimiter)
  874. )
  875. ?$pos_enclosure_start
  876. :$pos_delimiter+1;
  877. }
  878. }
  879. $line = substr($line,$offset);
  880. }
  881. } else {
  882. $line = preg_split("/".$delimiter."/",$line);
  883. /*
  884. * Validating against pesky extra line breaks creating false rows.
  885. */
  886. if (is_array($line) && !empty($line[0])) {
  887. $output[$line_num] = $line;
  888. }
  889. }
  890. }
  891. return $output;
  892. } else {
  893. return false;
  894. }
  895. } else {
  896. return false;
  897. }
  898. }
  899. }
  900. function cleardiv() {
  901. return '<div class="clear"></div>';
  902. }
  903. function bb_translate_video($s) {
  904. $matches = null;
  905. $r = preg_match_all("/\[video\](.*?)\[\/video\]/ism",$s,$matches,PREG_SET_ORDER);
  906. if($r) {
  907. foreach($matches as $mtch) {
  908. if((stristr($mtch[1],'youtube')) || (stristr($mtch[1],'youtu.be')))
  909. $s = str_replace($mtch[0],'[youtube]' . $mtch[1] . '[/youtube]',$s);
  910. elseif(stristr($mtch[1],'vimeo'))
  911. $s = str_replace($mtch[0],'[vimeo]' . $mtch[1] . '[/vimeo]',$s);
  912. }
  913. }
  914. return $s;
  915. }
  916. function html2bb_video($s) {
  917. $s = preg_replace('#<object[^>]+>(.*?)https+://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+)(.*?)</object>#ism',
  918. '[youtube]$2[/youtube]', $s);
  919. $s = preg_replace('#<iframe[^>](.*?)https+://www.youtube.com/embed/([A-Za-z0-9\-_=]+)(.*?)</iframe>#ism',
  920. '[youtube]$2[/youtube]', $s);
  921. $s = preg_replace('#<iframe[^>](.*?)https+://player.vimeo.com/video/([0-9]+)(.*?)</iframe>#ism',
  922. '[vimeo]$2[/vimeo]', $s);
  923. return $s;
  924. }
  925. /**
  926. * apply xmlify() to all values of array $val, recursively
  927. */
  928. function array_xmlify($val){
  929. if (is_bool($val)) return $val?"true":"false";
  930. if (is_array($val)) return array_map('array_xmlify', $val);
  931. return xmlify((string) $val);
  932. }
  933. function reltoabs($text, $base)
  934. {
  935. if (empty($base))
  936. return $text;
  937. $base = rtrim($base,'/');
  938. $base2 = $base . "/";
  939. // Replace links
  940. $pattern = "/<a([^>]*) href=\"(?!http|https|\/)([^\"]*)\"/";
  941. $replace = "<a\${1} href=\"" . $base2 . "\${2}\"";
  942. $text = preg_replace($pattern, $replace, $text);
  943. $pattern = "/<a([^>]*) href=\"(?!http|https)([^\"]*)\"/";
  944. $replace = "<a\${1} href=\"" . $base . "\${2}\"";
  945. $text = preg_replace($pattern, $replace, $text);
  946. // Replace images
  947. $pattern = "/<img([^>]*) src=\"(?!http|https|\/)([^\"]*)\"/";
  948. $replace = "<img\${1} src=\"" . $base2 . "\${2}\"";
  949. $text = preg_replace($pattern, $replace, $text);
  950. $pattern = "/<img([^>]*) src=\"(?!http|https)([^\"]*)\"/";
  951. $replace = "<img\${1} src=\"" . $base . "\${2}\"";
  952. $text = preg_replace($pattern, $replace, $text);
  953. // Done
  954. return $text;
  955. }