X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=include%2Ftext.php;h=c0b716afb238d0aa5ae5cef47135ecf99e6c4164;hb=34e7206ba49b2835ce21a762a59a8a3626f29b60;hp=075d345cb2d21938d40a687992542c253dd2d045;hpb=18f71c1050f3333e88b69e0ef18d9bdef3e09190;p=friendica.git
diff --git a/include/text.php b/include/text.php
index 075d345cb2..c0b716afb2 100644
--- a/include/text.php
+++ b/include/text.php
@@ -6,40 +6,35 @@
// returns substituted string.
// WARNING: this is pretty basic, and doesn't properly handle search strings that are substrings of each other.
// For instance if 'test' => "foo" and 'testing' => "bar", testing could become either bar or fooing,
-// depending on the order in which they were declared in the array.
+// depending on the order in which they were declared in the array.
require_once("include/template_processor.php");
require_once("include/friendica_smarty.php");
-if(! function_exists('replace_macros')) {
+if(! function_exists('replace_macros')) {
+/**
+ * This is our template processor
+ *
+ * @param string|FriendicaSmarty $s the string requiring macro substitution,
+ * or an instance of FriendicaSmarty
+ * @param array $r key value pairs (search => replace)
+ * @return string substituted string
+ */
function replace_macros($s,$r) {
- global $t;
+
+ $stamp1 = microtime(true);
-// $ts = microtime();
$a = get_app();
- if($a->theme['template_engine'] === 'smarty3') {
- $template = '';
- if(gettype($s) === 'string') {
- $template = $s;
- $s = new FriendicaSmarty();
- }
- foreach($r as $key=>$value) {
- if($key[0] === '$') {
- $key = substr($key, 1);
- }
- $s->assign($key, $value);
- }
- $output = $s->parsed($template);
- }
- else {
- $r = $t->replace($s,$r);
-
- $output = template_unescape($r);
+ $t = $a->template_engine();
+ try {
+ $output = $t->replace_macros($s,$r);
+ } catch (Exception $e) {
+ echo "
';
@@ -745,6 +933,12 @@ function search($s,$id='search-box',$url='/search',$save = false) {
}}
if(! function_exists('valid_email')) {
+/**
+ * Check if $x is a valid email string
+ *
+ * @param string $x
+ * @return boolean
+ */
function valid_email($x){
if(get_config('system','disable_email_validation'))
@@ -756,21 +950,26 @@ function valid_email($x){
}}
+if(! function_exists('linkify')) {
/**
- *
- * Function: linkify
- *
* Replace naked text hyperlink with HTML formatted hyperlink
*
+ * @param string $s
*/
-
-if(! function_exists('linkify')) {
function linkify($s) {
$s = preg_replace("/(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\'\%\$\!\+]*)/", '
$1', $s);
$s = preg_replace("/\<(.*?)(src|href)=(.*?)\&\;(.*?)\>/ism",'<$1$2=$3&$4>',$s);
return($s);
}}
+
+/**
+ * Load poke verbs
+ *
+ * @return array index is present tense verb
+ value is array containing past tense verb, translation of present, translation of past
+ * @hook poke_verbs pokes array
+ */
function get_poke_verbs() {
// index is present tense verb
@@ -788,11 +987,13 @@ function get_poke_verbs() {
return $arr;
}
+/**
+ * Load moods
+ * @return array index is mood, value is translated mood
+ * @hook mood_verbs moods array
+ */
function get_mood_verbs() {
- // index is present tense verb
- // value is array containing past tense verb, translation of present, translation of past
-
$arr = array(
'happy' => t('happy'),
'sad' => t('sad'),
@@ -821,17 +1022,11 @@ function get_mood_verbs() {
}
+
+if(! function_exists('smilies')) {
/**
- *
- * Function: smilies
- *
- * Description:
* Replaces text emoticons with graphical images
*
- * @Parameter: string $s
- *
- * Returns string
- *
* It is expected that this function will be called using HTML text.
* We will escape text between HTML pre and code blocks from being
* processed.
@@ -840,11 +1035,12 @@ function get_mood_verbs() {
* function from being executed by the prepare_text() routine when preparing
* bbcode source for HTML display
*
+ * @param string $s
+ * @param boolean $sample
+ * @return string
+ * @hook smilie ('texts' => smilies texts array, 'icons' => smilies html array, 'string' => $s)
*/
-
-if(! function_exists('smilies')) {
function smilies($s, $sample = false) {
-
$a = get_app();
if(intval(get_config('system','no_smilies'))
@@ -887,7 +1083,8 @@ function smilies($s, $sample = false) {
':facepalm',
':like',
':dislike',
- '~friendica'
+ '~friendica',
+ 'red#'
);
@@ -924,7 +1121,8 @@ function smilies($s, $sample = false) {
'
',
'
',
'
',
- '
~friendica '
+ '
~friendica ',
+ '
red '
);
$params = array('texts' => $texts, 'icons' => $icons, 'string' => $s);
@@ -956,8 +1154,13 @@ function smile_decode($m) {
return(str_replace($m[1],base64url_decode($m[1]),$m[0]));
}
-// expand <3333 to the correct number of hearts
+/**
+ * expand <3333 to the correct number of hearts
+ *
+ * @param string $x
+ * @return string
+ */
function preg_heart($x) {
$a = get_app();
if(strlen($x[1]) == 1)
@@ -971,6 +1174,12 @@ function preg_heart($x) {
if(! function_exists('day_translate')) {
+/**
+ * Translate days and months names
+ *
+ * @param string $s
+ * @return string
+ */
function day_translate($s) {
$ret = str_replace(array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'),
array( t('Monday'), t('Tuesday'), t('Wednesday'), t('Thursday'), t('Friday'), t('Saturday'), t('Sunday')),
@@ -985,50 +1194,139 @@ function day_translate($s) {
if(! function_exists('normalise_link')) {
+/**
+ * Normalize url
+ *
+ * @param string $url
+ * @return string
+ */
function normalise_link($url) {
$ret = str_replace(array('https:','//www.'), array('http:','//'), $url);
return(rtrim($ret,'/'));
}}
+
+
+if(! function_exists('link_compare')) {
/**
- *
* Compare two URLs to see if they are the same, but ignore
* slight but hopefully insignificant differences such as if one
* is https and the other isn't, or if one is www.something and
* the other isn't - and also ignore case differences.
*
- * Return true if the URLs match, otherwise false.
+ * @param string $a first url
+ * @param string $b second url
+ * @return boolean True if the URLs match, otherwise False
*
- */
-
-if(! function_exists('link_compare')) {
+ */
function link_compare($a,$b) {
if(strcasecmp(normalise_link($a),normalise_link($b)) === 0)
return true;
return false;
}}
+
+if(! function_exists('redir_private_images')) {
+/**
+ * Find any non-embedded images in private items and add redir links to them
+ *
+ * @param App $a
+ * @param array $item
+ */
+function redir_private_images($a, &$item) {
+
+ $matches = false;
+ $cnt = preg_match_all('|\[img\](http[^\[]*?/photo/[a-fA-F0-9]+?(-[0-9]\.[\w]+?)?)\[\/img\]|', $item['body'], $matches, PREG_SET_ORDER);
+ if($cnt) {
+ //logger("redir_private_images: matches = " . print_r($matches, true));
+ foreach($matches as $mtch) {
+ if(strpos($mtch[1], '/redir') !== false)
+ continue;
+
+ if((local_user() == $item['uid']) && ($item['private'] != 0) && ($item['contact-id'] != $a->contact['id']) && ($item['network'] == NETWORK_DFRN)) {
+ //logger("redir_private_images: redir");
+ $img_url = $a->get_baseurl() . '/redir?f=1&quiet=1&url=' . $mtch[1] . '&conurl=' . $item['author-link'];
+ $item['body'] = str_replace($mtch[0], "[img]".$img_url."[/img]", $item['body']);
+ }
+ }
+ }
+
+}}
+
+
// Given an item array, convert the body element from bbcode to html and add smilie icons.
// If attach is true, also add icons for item attachments
-
if(! function_exists('prepare_body')) {
-function prepare_body($item,$attach = false) {
+/**
+ * Given an item array, convert the body element from bbcode to html and add smilie icons.
+ * If attach is true, also add icons for item attachments
+ *
+ * @param array $item
+ * @param boolean $attach
+ * @return string item body html
+ * @hook prepare_body_init item array before any work
+ * @hook prepare_body ('item'=>item array, 'html'=>body string) after first bbcode to html
+ * @hook prepare_body_final ('item'=>item array, 'html'=>body string) after attach icons and blockquote special case handling (spoiler, author)
+ */
+function prepare_body(&$item,$attach = false) {
$a = get_app();
call_hooks('prepare_body_init', $item);
- $cachefile = get_cachefile($item["guid"]."-".strtotime($item["edited"])."-".hash("crc32", $item['body']));
+ $searchpath = $a->get_baseurl()."/search?tag=";
+
+ $tags=array();
+ $hashtags = array();
+ $mentions = array();
+
+ if (!get_config('system','suppress_tags')) {
+ $taglist = q("SELECT `type`, `term`, `url` FROM `term` WHERE `otype` = %d AND `oid` = %d AND `type` IN (%d, %d) ORDER BY `tid`",
+ intval(TERM_OBJ_POST), intval($item['id']), intval(TERM_HASHTAG), intval(TERM_MENTION));
+
+ foreach($taglist as $tag) {
+
+ if ($tag["url"] == "")
+ $tag["url"] = $searchpath.strtolower($tag["term"]);
+
+ if ($tag["type"] == TERM_HASHTAG) {
+ $hashtags[] = "#
".$tag["term"]."";
+ $prefix = "#";
+ } elseif ($tag["type"] == TERM_MENTION) {
+ $mentions[] = "@
".$tag["term"]."";
+ $prefix = "@";
+ }
+ $tags[] = $prefix."
".$tag["term"]."";
+ }
+ }
+
+ $item['tags'] = $tags;
+ $item['hashtags'] = $hashtags;
+ $item['mentions'] = $mentions;
+
+
+ //$cachefile = get_cachefile($item["guid"]."-".strtotime($item["edited"])."-".hash("crc32", $item['body']));
+ $cachefile = get_cachefile($item["guid"]."-".hash("md5", $item['body']));
if (($cachefile != '')) {
- if (file_exists($cachefile))
+ if (file_exists($cachefile)) {
+ $stamp1 = microtime(true);
$s = file_get_contents($cachefile);
- else {
+ $a->save_timestamp($stamp1, "file");
+ } else {
+ redir_private_images($a, $item);
$s = prepare_text($item['body']);
+
+ $stamp1 = microtime(true);
file_put_contents($cachefile, $s);
+ $a->save_timestamp($stamp1, "file");
+
+ logger('prepare_body: put item '.$item["id"].' into cachefile '.$cachefile);
}
- } else
+ } else {
+ redir_private_images($a, $item);
$s = prepare_text($item['body']);
+ }
$prep_arr = array('item' => $item, 'html' => $s);
@@ -1042,16 +1340,58 @@ function prepare_body($item,$attach = false) {
return $s;
}
+ $as = '';
+ $vhead = false;
$arr = explode('[/attach],',$item['attach']);
if(count($arr)) {
- $s .= '
';
+ $as .= '
';
foreach($arr as $r) {
$matches = false;
$icon = '';
$cnt = preg_match_all('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"|',$r,$matches, PREG_SET_ORDER);
if($cnt) {
foreach($matches as $mtch) {
- $icontype = strtolower(substr($mtch[3],0,strpos($mtch[3],'/')));
+ $mime = $mtch[3];
+
+ if((local_user() == $item['uid']) && ($item['contact-id'] != $a->contact['id']) && ($item['network'] == NETWORK_DFRN))
+ $the_url = $a->get_baseurl() . '/redir/' . $item['contact-id'] . '?f=1&url=' . $mtch[1];
+ else
+ $the_url = $mtch[1];
+
+ if(strpos($mime, 'video') !== false) {
+ if(!$vhead) {
+ $vhead = true;
+ $a->page['htmlhead'] .= replace_macros(get_markup_template('videos_head.tpl'), array(
+ '$baseurl' => $a->get_baseurl(),
+ ));
+ $a->page['end'] .= replace_macros(get_markup_template('videos_end.tpl'), array(
+ '$baseurl' => $a->get_baseurl(),
+ ));
+ }
+
+ $id = end(explode('/', $the_url));
+ $as .= replace_macros(get_markup_template('video_top.tpl'), array(
+ '$video' => array(
+ 'id' => $id,
+ 'title' => t('View Video'),
+ 'src' => $the_url,
+ 'mime' => $mime,
+ ),
+ ));
+ }
+
+ $filetype = strtolower(substr( $mime, 0, strpos($mime,'/') ));
+ if($filetype) {
+ $filesubtype = strtolower(substr( $mime, strpos($mime,'/') + 1 ));
+ $filesubtype = str_replace('.', '-', $filesubtype);
+ }
+ else {
+ $filetype = 'unkn';
+ $filesubtype = 'unkn';
+ }
+
+ $icon = '
';
+ /*$icontype = strtolower(substr($mtch[3],0,strpos($mtch[3],'/')));
switch($icontype) {
case 'video':
case 'audio':
@@ -1062,20 +1402,18 @@ function prepare_body($item,$attach = false) {
default:
$icon = '
';
break;
- }
+ }*/
+
$title = ((strlen(trim($mtch[4]))) ? escape_tags(trim($mtch[4])) : escape_tags($mtch[1]));
$title .= ' ' . $mtch[2] . ' ' . t('bytes');
- if((local_user() == $item['uid']) && ($item['contact-id'] != $a->contact['id']) && ($item['network'] == NETWORK_DFRN))
- $the_url = $a->get_baseurl() . '/redir/' . $item['contact-id'] . '?f=1&url=' . $mtch[1];
- else
- $the_url = $mtch[1];
- $s .= '
' . $icon . '';
+ $as .= '
' . $icon . '';
}
}
}
- $s .= '
';
+ $as .= '
';
}
+ $s = $s . $as;
// Look for spoiler
@@ -1115,9 +1453,13 @@ function prepare_body($item,$attach = false) {
}}
-// Given a text string, convert from bbcode to html and add smilie icons.
-
if(! function_exists('prepare_text')) {
+/**
+ * Given a text string, convert from bbcode to html and add smilie icons.
+ *
+ * @param string $text
+ * @return string
+ */
function prepare_text($text) {
require_once('include/bbcode.php');
@@ -1131,19 +1473,25 @@ function prepare_text($text) {
}}
+
/**
- * returns
- * [
- * //categories [
+ * return array with details for categories and folders for an item
+ *
+ * @param array $item
+ * @return array
+ *
+ * [
+ * [ // categories array
* {
* 'name': 'category name',
- * 'removeurl': 'url to remove this category',
- * 'first': 'is the first in this array? true/false',
+ * 'removeurl': 'url to remove this category',
+ * 'first': 'is the first in this array? true/false',
* 'last': 'is the last in this array? true/false',
* } ,
* ....
* ],
- * // folders [
+ * [ //folders array
+ * {
* 'name': 'folder name',
* 'removeurl': 'url to remove this folder',
* 'first': 'is the first in this array? true/false',
@@ -1151,9 +1499,10 @@ function prepare_text($text) {
* } ,
* ....
* ]
- * ]
+ * ]
*/
function get_cats_and_terms($item) {
+
$a = get_app();
$categories = array();
$folders = array();
@@ -1198,11 +1547,12 @@ function get_cats_and_terms($item) {
}
+
+if(! function_exists('feed_hublinks')) {
/**
* return atom link elements for all of our hubs
+ * @return string hub link xml elements
*/
-
-if(! function_exists('feed_hublinks')) {
function feed_hublinks() {
$hub = get_config('system','huburl');
@@ -1222,9 +1572,13 @@ function feed_hublinks() {
return $hubxml;
}}
-/* return atom link elements for salmon endpoints */
if(! function_exists('feed_salmonlinks')) {
+/**
+ * return atom link elements for salmon endpoints
+ * @param string $nick user nickname
+ * @return string salmon link xml elements
+ */
function feed_salmonlinks($nick) {
$a = get_app();
@@ -1239,6 +1593,11 @@ function feed_salmonlinks($nick) {
}}
if(! function_exists('get_plink')) {
+/**
+ * get private link for item
+ * @param array $item
+ * @return boolean|array False if item has not plink, otherwise array('href'=>plink url, 'title'=>translated title)
+ */
function get_plink($item) {
$a = get_app();
if (x($item,'plink') && ($item['private'] != 1)) {
@@ -1253,6 +1612,11 @@ function get_plink($item) {
}}
if(! function_exists('unamp')) {
+/**
+ * replace html amp entity with amp char
+ * @param string $s
+ * @return string
+ */
function unamp($s) {
return str_replace('&', '&', $s);
}}
@@ -1261,6 +1625,12 @@ function unamp($s) {
if(! function_exists('lang_selector')) {
+/**
+ * get html for language selector
+ * @global string $lang
+ * @return string
+ * @template lang_selector.tpl
+ */
function lang_selector() {
global $lang;
@@ -1297,6 +1667,11 @@ function lang_selector() {
if(! function_exists('return_bytes')) {
+/**
+ * return number of bytes in size (K, M, G)
+ * @param string $size_str
+ * @return number
+ */
function return_bytes ($size_str) {
switch (substr ($size_str, -1))
{
@@ -1307,6 +1682,9 @@ function return_bytes ($size_str) {
}
}}
+/**
+ * @return string
+ */
function generate_user_guid() {
$found = true;
do {
@@ -1321,7 +1699,11 @@ function generate_user_guid() {
}
-
+/**
+ * @param string $s
+ * @param boolean $strip_padding
+ * @return string
+ */
function base64url_encode($s, $strip_padding = false) {
$s = strtr(base64_encode($s),'+/','-_');
@@ -1332,6 +1714,10 @@ function base64url_encode($s, $strip_padding = false) {
return $s;
}
+/**
+ * @param string $s
+ * @return string
+ */
function base64url_decode($s) {
if(is_array($s)) {
@@ -1360,6 +1746,16 @@ function base64url_decode($s) {
if (!function_exists('str_getcsv')) {
+ /**
+ * Parse csv string
+ *
+ * @param string $input
+ * @param string $delimiter
+ * @param string $enclosure
+ * @param string $escape
+ * @param string $eol
+ * @return boolean|array False on error, otherwise array[row][column]
+ */
function str_getcsv($input, $delimiter = ',', $enclosure = '"', $escape = '\\', $eol = '\n') {
if (is_string($input) && !empty($input)) {
$output = array();
@@ -1416,6 +1812,11 @@ if (!function_exists('str_getcsv')) {
}
}
+/**
+ * return div element with class 'clear'
+ * @return string
+ * @deprecated
+ */
function cleardiv() {
return '
';
}
@@ -1452,6 +1853,8 @@ function html2bb_video($s) {
/**
* apply xmlify() to all values of array $val, recursively
+ * @param array $val
+ * @return array
*/
function array_xmlify($val){
if (is_bool($val)) return $val?"true":"false";
@@ -1460,6 +1863,13 @@ function array_xmlify($val){
}
+/**
+ * transorm link href and img src from relative to absolute
+ *
+ * @param string $text
+ * @param string $base base url
+ * @return string
+ */
function reltoabs($text, $base)
{
if (empty($base))
@@ -1492,6 +1902,12 @@ function reltoabs($text, $base)
return $text;
}
+/**
+ * get translated item type
+ *
+ * @param array $itme
+ * @return string
+ */
function item_post_type($item) {
if(intval($item['event-id']))
return t('event');