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.
 
 
 
 
 
 

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