3 // This is our template processor.
4 // $s is the string requiring macro substitution.
5 // $r is an array of key value pairs (search => replace)
6 // returns substituted string.
7 // WARNING: this is pretty basic, and doesn't properly handle search strings that are substrings of each other.
8 // For instance if 'test' => "foo" and 'testing' => "bar", testing could become either bar or fooing,
9 // depending on the order in which they were declared in the array.
11 require_once("include/template_processor.php");
13 if(! function_exists('replace_macros')) {
14 function replace_macros($s,$r) {
17 return $t->replace($s,$r);
22 // random hash, 64 chars
24 if(! function_exists('random_string')) {
25 function random_string() {
26 return(hash('sha256',uniqid(rand(),true)));
30 * This is our primary input filter.
32 * The high bit hack only involved some old IE browser, forget which (IE5/Mac?)
33 * that had an XSS attack vector due to stripping the high-bit on an 8-bit character
34 * after cleansing, and angle chars with the high bit set could get through as markup.
36 * This is now disabled because it was interfering with some legitimate unicode sequences
37 * and hopefully there aren't a lot of those browsers left.
39 * Use this on any text input where angle chars are not valid or permitted
40 * They will be replaced with safer brackets. This may be filtered further
41 * if these are not allowed either.
45 if(! function_exists('notags')) {
46 function notags($string) {
48 return(str_replace(array("<",">"), array('[',']'), $string));
50 // High-bit filter no longer used
51 // return(str_replace(array("<",">","\xBA","\xBC","\xBE"), array('[',']','','',''), $string));
54 // use this on "body" or "content" input where angle chars shouldn't be removed,
55 // and allow them to be safely displayed.
57 if(! function_exists('escape_tags')) {
58 function escape_tags($string) {
60 return(htmlspecialchars($string));
64 // generate a string that's random, but usually pronounceable.
65 // used to generate initial passwords
67 if(! function_exists('autoname')) {
68 function autoname($len) {
70 $vowels = array('a','a','ai','au','e','e','e','ee','ea','i','ie','o','ou','u');
89 's','sc','sh','sm','sp','st',
97 $midcons = array('ck','ct','gn','ld','lf','lm','lt','mb','mm', 'mn','mp',
98 'nd','ng','nk','nt','rn','rp','rt');
100 $noend = array('bl', 'br', 'cl','cr','dr','fl','fr','gl','gr',
101 'kh', 'kl','kr','mn','pl','pr','rh','tr','qu','wh');
103 $start = mt_rand(0,2);
111 for ($x = 0; $x < $len; $x ++) {
112 $r = mt_rand(0,count($table) - 1);
115 if($table == $vowels)
116 $table = array_merge($cons,$midcons);
122 $word = substr($word,0,$len);
124 foreach($noend as $noe) {
125 if((strlen($word) > 2) && (substr($word,-2) == $noe)) {
126 $word = substr($word,0,-1);
130 if(substr($word,-1) == 'q')
131 $word = substr($word,0,-1);
136 // escape text ($str) for XML transport
137 // returns escaped text.
139 if(! function_exists('xmlify')) {
140 function xmlify($str) {
143 for($x = 0; $x < strlen($str); $x ++) {
173 $buffer = trim($buffer);
178 // pass xml escaped text ($s), returns unescaped text
180 if(! function_exists('unxmlify')) {
181 function unxmlify($s) {
182 $ret = str_replace('&','&', $s);
183 $ret = str_replace(array('<','>','"','''),array('<','>','"',"'"),$ret);
187 // convenience wrapper, reverse the operation "bin2hex"
189 if(! function_exists('hex2bin')) {
190 function hex2bin($s) {
191 if(! ctype_xdigit($s)) {
192 logger('hex2bin: illegal input: ' . print_r(debug_backtrace(), true));
196 return(pack("H*",$s));
199 // Automatic pagination.
200 // To use, get the count of total items.
201 // Then call $a->set_pager_total($number_items);
202 // Optionally call $a->set_pager_itemspage($n) to the number of items to display on each page
203 // Then call paginate($a) after the end of the display loop to insert the pager block on the page
204 // (assuming there are enough items to paginate).
205 // When using with SQL, the setting LIMIT %d, %d => $a->pager['start'],$a->pager['itemspage']
206 // will limit the results to the correct items for the current page.
207 // The actual page handling is then accomplished at the application layer.
209 if(! function_exists('paginate')) {
210 function paginate(&$a) {
212 $stripped = preg_replace('/(&page=[0-9]*)/','',$a->query_string);
213 $stripped = str_replace('q=','',$stripped);
214 $stripped = trim($stripped,'/');
215 $pagenum = $a->pager['page'];
216 $url = $a->get_baseurl() . '/' . $stripped;
219 if($a->pager['total'] > $a->pager['itemspage']) {
220 $o .= '<div class="pager">';
221 if($a->pager['page'] != 1)
222 $o .= '<span class="pager_prev">'."<a href=\"$url".'&page='.($a->pager['page'] - 1).'">' . t('prev') . '</a></span> ';
224 $o .= "<span class=\"pager_first\"><a href=\"$url"."&page=1\">" . t('first') . "</a></span> ";
226 $numpages = $a->pager['total'] / $a->pager['itemspage'];
229 $numstop = $numpages;
232 $numstart = (($pagenum > 7) ? ($pagenum - 7) : 1);
233 $numstop = (($pagenum > ($numpages - 7)) ? $numpages : ($numstart + 14));
236 for($i = $numstart; $i <= $numstop; $i++){
237 if($i == $a->pager['page'])
238 $o .= '<span class="pager_current">'.(($i < 10) ? ' '.$i : $i);
240 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? ' '.$i : $i)."</a>";
244 if(($a->pager['total'] % $a->pager['itemspage']) != 0) {
245 if($i == $a->pager['page'])
246 $o .= '<span class="pager_current">'.(($i < 10) ? ' '.$i : $i);
248 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? ' '.$i : $i)."</a>";
252 $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
253 $o .= "<span class=\"pager_last\"><a href=\"$url"."&page=$lastpage\">" . t('last') . "</a></span> ";
255 if(($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0)
256 $o .= '<span class="pager_next">'."<a href=\"$url"."&page=".($a->pager['page'] + 1).'">' . t('next') . '</a></span>';
257 $o .= '</div>'."\r\n";
262 // Turn user/group ACLs stored as angle bracketed text into arrays
264 if(! function_exists('expand_acl')) {
265 function expand_acl($s) {
266 // turn string array of angle-bracketed elements into numeric array
267 // e.g. "<1><2><3>" => array(1,2,3);
271 $t = str_replace('<','',$s);
272 $a = explode('>',$t);
275 $ret[] = intval($aa);
281 // Used to wrap ACL elements in angle brackets for storage
283 if(! function_exists('sanitise_acl')) {
284 function sanitise_acl(&$item) {
286 $item = '<' . intval(notags(trim($item))) . '>';
292 // Convert an ACL array to a storable string
294 if(! function_exists('perms2str')) {
295 function perms2str($p) {
299 array_walk($tmp,'sanitise_acl');
300 $ret = implode('',$tmp);
305 // generate a guaranteed unique (for this domain) item ID for ATOM
306 // safe from birthday paradox
308 if(! function_exists('item_new_uri')) {
309 function item_new_uri($hostname,$uid) {
313 $hash = random_string();
315 $uri = "urn:X-dfrn:" . $hostname . ':' . $uid . ':' . $hash;
317 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
321 } while($dups == true);
325 // Generate a guaranteed unique photo ID.
326 // safe from birthday paradox
328 if(! function_exists('photo_new_resource')) {
329 function photo_new_resource() {
333 $resource = hash('md5',uniqid(mt_rand(),true));
334 $r = q("SELECT `id` FROM `photo` WHERE `resource-id` = '%s' LIMIT 1",
339 } while($found == true);
344 // wrapper to load a view template, checking for alternate
345 // languages before falling back to the default
347 // obsolete, deprecated.
349 if(! function_exists('load_view_file')) {
350 function load_view_file($s) {
356 if(file_exists("$d/$lang/$b"))
357 return file_get_contents("$d/$lang/$b");
359 $theme = current_theme();
361 if(file_exists("$d/theme/$theme/$b"))
362 return file_get_contents("$d/theme/$theme/$b");
364 return file_get_contents($s);
367 if(! function_exists('get_intltext_template')) {
368 function get_intltext_template($s) {
374 if(file_exists("view/$lang/$s"))
375 return file_get_contents("view/$lang/$s");
376 elseif(file_exists("view/en/$s"))
377 return file_get_contents("view/en/$s");
379 return file_get_contents("view/$s");
382 if(! function_exists('get_markup_template')) {
383 function get_markup_template($s) {
385 $theme = current_theme();
387 if(file_exists("view/theme/$theme/$s"))
388 return file_get_contents("view/theme/$theme/$s");
390 return file_get_contents("view/$s");
398 // for html,xml parsing - let's say you've got
399 // an attribute foobar="class1 class2 class3"
400 // and you want to find out if it contains 'class3'.
401 // you can't use a normal sub string search because you
402 // might match 'notclass3' and a regex to do the job is
403 // possible but a bit complicated.
404 // pass the attribute string as $attr and the attribute you
405 // are looking for as $s - returns true if found, otherwise false
407 if(! function_exists('attribute_contains')) {
408 function attribute_contains($attr,$s) {
409 $a = explode(' ', $attr);
410 if(count($a) && in_array($s,$a))
415 if(! function_exists('logger')) {
416 function logger($msg,$level = 0) {
417 $debugging = get_config('system','debugging');
418 $loglevel = intval(get_config('system','loglevel'));
419 $logfile = get_config('system','logfile');
421 if((! $debugging) || (! $logfile) || ($level > $loglevel))
424 @file_put_contents($logfile, datetime_convert() . ':' . session_id() . ' ' . $msg . "\n", FILE_APPEND);
429 if(! function_exists('activity_match')) {
430 function activity_match($haystack,$needle) {
431 if(($haystack === $needle) || ((basename($needle) === $haystack) && strstr($needle,NAMESPACE_ACTIVITY_SCHEMA)))
437 // Pull out all #hashtags and @person tags from $s;
438 // We also get @person@domain.com - which would make
439 // the regex quite complicated as tags can also
440 // end a sentence. So we'll run through our results
441 // and strip the period from any tags which end with one.
442 // Returns array of tags found, or empty array.
445 if(! function_exists('get_tags')) {
446 function get_tags($s) {
449 // ignore anything in a code block
451 $s = preg_replace('/\[code\](.*?)\[\/code\]/sm','',$s);
453 // Match full names against @tags including the space between first and last
454 // We will look these up afterward to see if they are full names or not recognisable.
456 if(preg_match_all('/(@[^ \x0D\x0A,:?]+ [^ \x0D\x0A,:?]+)([ \x0D\x0A,:?]|$)/',$s,$match)) {
457 foreach($match[1] as $mtch) {
458 if(strstr($mtch,"]")) {
459 // we might be inside a bbcode color tag - leave it alone
462 if(substr($mtch,-1,1) === '.')
463 $ret[] = substr($mtch,0,-1);
469 // Otherwise pull out single word tags. These can be @nickname, @first_last
472 if(preg_match_all('/([@#][^ \x0D\x0A,:?]+)([ \x0D\x0A,:?]|$)/',$s,$match)) {
473 foreach($match[1] as $mtch) {
474 if(strstr($mtch,"]")) {
475 // we might be inside a bbcode color tag - leave it alone
478 // ignore strictly numeric tags like #1
479 if((strpos($mtch,'#') === 0) && ctype_digit(substr($mtch,1)))
481 if(substr($mtch,-1,1) === '.')
482 $ret[] = substr($mtch,0,-1);
491 // quick and dirty quoted_printable encoding
493 if(! function_exists('qp')) {
495 return str_replace ("%","=",rawurlencode($s));
500 if(! function_exists('get_mentions')) {
501 function get_mentions($item) {
503 if(! strlen($item['tag']))
506 $arr = explode(',',$item['tag']);
507 foreach($arr as $x) {
509 if(preg_match('/@\[url=([^\]]*)\]/',$x,$matches)) {
510 $o .= "\t\t" . '<link rel="mentioned" href="' . $matches[1] . '" />' . "\r\n";
511 $o .= "\t\t" . '<link rel="ostatus:attention" href="' . $matches[1] . '" />' . "\r\n";
517 if(! function_exists('contact_block')) {
518 function contact_block() {
522 $shown = get_pconfig($a->profile['uid'],'system','display_friend_count');
526 if((! is_array($a->profile)) || ($a->profile['hide-friends']))
528 $r = q("SELECT COUNT(*) AS `total` FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 and `pending` = 0",
529 intval($a->profile['uid'])
532 $total = intval($r[0]['total']);
535 $o .= '<h4 class="contact-h4">' . t('No contacts') . '</h4>';
538 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 and `pending` = 0 ORDER BY RAND() LIMIT %d",
539 intval($a->profile['uid']),
543 $o .= '<h4 class="contact-h4">' . sprintf( tt('%d Contact','%d Contacts', $total),$total) . '</h4><div id="contact-block">';
545 $o .= micropro($rr,true,'mpfriend');
547 $o .= '</div><div id="contact-block-end"></div>';
548 $o .= '<div id="viewcontacts"><a id="viewcontacts-link" href="viewcontacts/' . $a->profile['nickname'] . '">' . t('View Contacts') . '</a></div>';
552 $arr = array('contacts' => $r, 'output' => $o);
554 call_hooks('contact_block_end', $arr);
559 if(! function_exists('micropro')) {
560 function micropro($contact, $redirect = false, $class = '', $textmode = false) {
563 $class = ' ' . $class;
565 $url = $contact['url'];
570 $redirect_url = $a->get_baseurl() . '/redir/' . $contact['id'];
571 if(local_user() && ($contact['uid'] == local_user()) && ($contact['network'] === 'dfrn')) {
572 $url = $redirect_url;
573 $sparkle = ' sparkle';
576 $click = ((x($contact,'click')) ? ' onclick="' . $contact['click'] . '" ' : '');
580 return '<div class="contact-block-textdiv' . $class . '"><a class="contact-block-link' . $class . $sparkle
581 . (($click) ? ' fakelink' : '') . '" '
582 . (($url) ? ' href="' . $url . '"' : '') . $click
583 . '" title="' . $contact['name'] . ' [' . $contact['url'] . ']" alt="' . $contact['name']
584 . '" >'. $contact['name'] . '</a></div>' . "\r\n";
587 return '<div class="contact-block-div' . $class . '"><a class="contact-block-link' . $class . $sparkle
588 . (($click) ? ' fakelink' : '') . '" '
589 . (($url) ? ' href="' . $url . '"' : '') . $click . ' ><img class="contact-block-img' . $class . $sparkle . '" src="'
590 . $contact['micro'] . '" title="' . $contact['name'] . ' [' . $contact['url'] . ']" alt="' . $contact['name']
591 . '" /></a></div>' . "\r\n";
597 if(! function_exists('search')) {
598 function search($s,$id='search-box',$url='/search') {
600 $o = '<div id="' . $id . '">';
601 $o .= '<form action="' . $a->get_baseurl() . $url . '" method="get" >';
602 $o .= '<input type="text" name="search" id="search-text" value="' . $s .'" />';
603 $o .= '<input type="submit" name="submit" id="search-submit" value="' . t('Search') . '" />';
604 $o .= '</form></div>';
608 if(! function_exists('valid_email')) {
609 function valid_email($x){
610 if(preg_match('/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+$/',$x))
616 if(! function_exists('aes_decrypt')) {
617 function aes_decrypt($val,$ky)
619 $key="\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
620 for($a=0;$a<strlen($ky);$a++)
621 $key[$a%16]=chr(ord($key[$a%16]) ^ ord($ky[$a]));
622 $mode = MCRYPT_MODE_ECB;
623 $enc = MCRYPT_RIJNDAEL_128;
624 $dec = @mcrypt_decrypt($enc, $key, $val, $mode, @mcrypt_create_iv( @mcrypt_get_iv_size($enc, $mode), MCRYPT_DEV_URANDOM ) );
625 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));
629 if(! function_exists('aes_encrypt')) {
630 function aes_encrypt($val,$ky)
632 $key="\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
633 for($a=0;$a<strlen($ky);$a++)
634 $key[$a%16]=chr(ord($key[$a%16]) ^ ord($ky[$a]));
635 $mode=MCRYPT_MODE_ECB;
636 $enc=MCRYPT_RIJNDAEL_128;
637 $val=str_pad($val, (16*(floor(strlen($val) / 16)+(strlen($val) % 16==0?2:1))), chr(16-(strlen($val) % 16)));
638 return mcrypt_encrypt($enc, $key, $val, $mode, mcrypt_create_iv( mcrypt_get_iv_size($enc, $mode), MCRYPT_DEV_URANDOM));
646 * Replace naked text hyperlink with HTML formatted hyperlink
650 if(! function_exists('linkify')) {
651 function linkify($s) {
652 $s = preg_replace("/(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\.\=\_\~\#\'\%\$\!\+]*)/", ' <a href="$1" target="external-link">$1</a>', $s);
662 * Replaces text emoticons with graphical images
664 * @Parameter: string $s
669 if(! function_exists('smilies')) {
670 function smilies($s) {
674 array( '<3', '</3', '<\\3', ':-)', ':)', ';-)', ':-(', ':(', ':-P', ':P', ':-"', ':-x', ':-X', ':-D', '8-|', '8-O'),
676 '<img src="' . $a->get_baseurl() . '/images/smiley-heart.gif" alt="<3" />',
677 '<img src="' . $a->get_baseurl() . '/images/smiley-brokenheart.gif" alt="</3" />',
678 '<img src="' . $a->get_baseurl() . '/images/smiley-brokenheart.gif" alt="<\\3" />',
679 '<img src="' . $a->get_baseurl() . '/images/smiley-smile.gif" alt=":-)" />',
680 '<img src="' . $a->get_baseurl() . '/images/smiley-smile.gif" alt=":)" />',
681 '<img src="' . $a->get_baseurl() . '/images/smiley-wink.gif" alt=";-)" />',
682 '<img src="' . $a->get_baseurl() . '/images/smiley-frown.gif" alt=":-(" />',
683 '<img src="' . $a->get_baseurl() . '/images/smiley-frown.gif" alt=":(" />',
684 '<img src="' . $a->get_baseurl() . '/images/smiley-tongue-out.gif" alt=":-P" />',
685 '<img src="' . $a->get_baseurl() . '/images/smiley-tongue-out.gif" alt=":P" />',
686 '<img src="' . $a->get_baseurl() . '/images/smiley-kiss.gif" alt=":-\"" />',
687 '<img src="' . $a->get_baseurl() . '/images/smiley-kiss.gif" alt=":-x" />',
688 '<img src="' . $a->get_baseurl() . '/images/smiley-kiss.gif" alt=":-X" />',
689 '<img src="' . $a->get_baseurl() . '/images/smiley-laughing.gif" alt=":-D" />',
690 '<img src="' . $a->get_baseurl() . '/images/smiley-surprised.gif" alt="8-|" />',
691 '<img src="' . $a->get_baseurl() . '/images/smiley-surprised.gif" alt="8-O" />'
697 if(! function_exists('day_translate')) {
698 function day_translate($s) {
699 $ret = str_replace(array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'),
700 array( t('Monday'), t('Tuesday'), t('Wednesday'), t('Thursday'), t('Friday'), t('Saturday'), t('Sunday')),
703 $ret = str_replace(array('January','February','March','April','May','June','July','August','September','October','November','December'),
704 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')),
711 if(! function_exists('normalise_link')) {
712 function normalise_link($url) {
713 $ret = str_replace(array('https:','//www.'), array('http:','//'), $url);
714 return(rtrim($ret,'/'));
719 * Compare two URLs to see if they are the same, but ignore
720 * slight but hopefully insignificant differences such as if one
721 * is https and the other isn't, or if one is www.something and
722 * the other isn't - and also ignore case differences.
724 * Return true if the URLs match, otherwise false.
728 if(! function_exists('link_compare')) {
729 function link_compare($a,$b) {
730 if(strcasecmp(normalise_link($a),normalise_link($b)) === 0)
735 // Given an item array, convert the body element from bbcode to html and add smilie icons.
736 // If attach is true, also add icons for item attachments
739 if(! function_exists('prepare_body')) {
740 function prepare_body($item,$attach = false) {
742 $s = prepare_text($item['body']);
746 $arr = explode(',',$item['attach']);
748 $s .= '<div class="body-attach">';
749 foreach($arr as $r) {
752 $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"\[\/attach\]|',$r,$matches);
754 $icontype = strtolower(substr($matches[3],0,strpos($matches[3],'/')));
760 $icon = '<div class="attachtype type-' . $icontype . '"></div>';
763 $icon = '<div class="attachtype type-unkn"></div>';
766 $title = ((strlen(trim($matches[4]))) ? escape_tags(trim($matches[4])) : escape_tags($matches[1]));
767 $title .= ' ' . $matches[2] . ' ' . t('bytes');
769 $s .= '<a href="' . strip_tags($matches[1]) . '" title="' . $title . '" class="attachlink" target="external-link" >' . $icon . '</a>';
772 $s .= '<div class="clear"></div></div>';
778 // Given a text string, convert from bbcode to html and add smilie icons.
780 if(! function_exists('prepare_text')) {
781 function prepare_text($text) {
783 require_once('include/bbcode.php');
785 $s = smilies(bbcode($text));
792 * return atom link elements for all of our hubs
795 if(! function_exists('feed_hublinks')) {
796 function feed_hublinks() {
798 $hub = get_config('system','huburl');
802 $hubs = explode(',', $hub);
804 foreach($hubs as $h) {
808 $hubxml .= '<link rel="hub" href="' . xmlify($h) . '" />' . "\n" ;
815 /* return atom link elements for salmon endpoints */
817 if(! function_exists('feed_salmonlinks')) {
818 function feed_salmonlinks($nick) {
822 $salmon = '<link rel="salmon" href="' . xmlify($a->get_baseurl() . '/salmon/' . $nick) . '" />' . "\n" ;
824 // old style links that status.net still needed as of 12/2010
826 $salmon .= ' <link rel="http://salmon-protocol.org/ns/salmon-replies" href="' . xmlify($a->get_baseurl() . '/salmon/' . $nick) . '" />' . "\n" ;
827 $salmon .= ' <link rel="http://salmon-protocol.org/ns/salmon-mention" href="' . xmlify($a->get_baseurl() . '/salmon/' . $nick) . '" />' . "\n" ;
831 if(! function_exists('get_plink')) {
832 function get_plink($item) {
834 $plink = (((x($item,'plink')) && (! $item['private'])) ? '<div class="wall-item-links-wrapper"><a href="'
835 . $item['plink'] . '" title="' . t('link to source') . '" target="external-link" class="icon remote-link"></a></div>' : '');
839 if(! function_exists('unamp')) {
841 return str_replace('&', '&', $s);
847 if(! function_exists('lang_selector')) {
848 function lang_selector() {
850 $o = '<div id="lang-select-icon" class="icon language" title="' . t('Select an alternate language') . '" onclick="openClose(\'language-selector\');" ></div>';
851 $o .= '<div id="language-selector" style="display: none;" >';
852 $o .= '<form action="" method="post" ><select name="system_language" onchange="this.form.submit();" >';
853 $langs = glob('view/*/strings.php');
854 if(is_array($langs) && count($langs)) {
855 if(! in_array('view/en/strings.php',$langs))
856 $langs[] = 'view/en/';
858 foreach($langs as $l) {
860 $ll = substr($ll,0,strrpos($ll,'/'));
861 $selected = (($ll === $lang) ? ' selected="selected" ' : '');
862 $o .= '<option value="' . $ll . '"' . $selected . '>' . $ll . '</option>';
865 $o .= '</select></form></div>';
870 if(! function_exists('return_bytes')) {
871 function return_bytes ($size_str) {
872 switch (substr ($size_str, -1))
874 case 'M': case 'm': return (int)$size_str * 1048576;
875 case 'K': case 'k': return (int)$size_str * 1024;
876 case 'G': case 'g': return (int)$size_str * 1073741824;
877 default: return $size_str;
881 function generate_guid() {
884 $guid = substr(random_string(),0,16);
885 $x = q("SELECT `uid` FROM `user` WHERE `guid` = '%s' LIMIT 1",
890 } while ($found == true );
895 function pkcs5_pad ($text, $blocksize)
897 $pad = $blocksize - (strlen($text) % $blocksize);
898 return $text . str_repeat(chr($pad), $pad);
901 function pkcs5_unpad($text)
903 $pad = ord($text{strlen($text)-1});
904 if ($pad > strlen($text)) return false;
905 if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return false;
906 return substr($text, 0, -1 * $pad);
910 function base64url_encode($s, $strip_padding = false) {
912 $s = strtr(base64_encode($s),'+/','-_');
915 $s = str_replace('=','',$s);
920 function base64url_decode($s) {
923 * // Placeholder for new rev of salmon which strips base64 padding.
924 * // PHP base64_decode handles the un-padded input without requiring this step
925 * // Uncomment if you find you need it.
928 * if(! strpos($s,'=')) {
938 return base64_decode(strtr($s,'-_','+/'));