X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=include%2Ftext.php;h=052207842f9779142c503bc86515a2c60e340cb4;hb=29092ace4428aeb796b31856aafa14cef1632bac;hp=9f8114d926ddcdb5b7a5bfe6c01cfce88dfb200e;hpb=f8d433685c8788321e6f49b0d60324253eb996b6;p=friendica.git diff --git a/include/text.php b/include/text.php index 9f8114d926..052207842f 100644 --- a/include/text.php +++ b/include/text.php @@ -21,13 +21,17 @@ if(! function_exists('replace_macros')) { * @return string substituted string */ function replace_macros($s,$r) { - + $stamp1 = microtime(true); $a = get_app(); $t = $a->template_engine(); - $output = $t->replace_macros($s,$r); + try { + $output = $t->replace_macros($s,$r); + } catch (Exception $e) { + echo "
".__function__.": ".$e->getMessage()."
"; killme(); + } $a->save_timestamp($stamp1, "rendering"); @@ -51,7 +55,7 @@ function random_string($size = 64,$type = RANDOM_STRING_HEX) { if(! function_exists('notags')) { /** - * This is our primary input filter. + * This is our primary input filter. * * The high bit hack only involved some old IE browser, forget which (IE5/Mac?) * that had an XSS attack vector due to stripping the high-bit on an 8-bit character @@ -214,14 +218,16 @@ function xmlify($str) { break; } }*/ - + /* $buffer = mb_ereg_replace("&", "&", $str); $buffer = mb_ereg_replace("'", "'", $buffer); - $buffer = mb_ereg_replace("\"", """, $buffer); + $buffer = mb_ereg_replace('"', """, $buffer); $buffer = mb_ereg_replace("<", "<", $buffer); $buffer = mb_ereg_replace(">", ">", $buffer); - + */ + $buffer = htmlspecialchars($str, ENT_QUOTES); $buffer = trim($buffer); + return($buffer); }} @@ -234,11 +240,13 @@ if(! function_exists('unxmlify')) { function unxmlify($s) { // $ret = str_replace('&','&', $s); // $ret = str_replace(array('<','>','"','''),array('<','>','"',"'"),$ret); - $ret = mb_ereg_replace('&', '&', $s); + /*$ret = mb_ereg_replace('&', '&', $s); $ret = mb_ereg_replace(''', "'", $ret); $ret = mb_ereg_replace('"', '"', $ret); $ret = mb_ereg_replace('<', "<", $ret); $ret = mb_ereg_replace('>', ">", $ret); + */ + $ret = htmlspecialchars_decode($s, ENT_QUOTES); return $ret; }} @@ -260,75 +268,113 @@ function hex2bin($s) { }} -if(! function_exists('paginate')) { +if(! function_exists('paginate_data')) { /** - * Automatic pagination. + * Automatica pagination data. * - * To use, get the count of total items. - * Then call $a->set_pager_total($number_items); - * Optionally call $a->set_pager_itemspage($n) to the number of items to display on each page - * Then call paginate($a) after the end of the display loop to insert the pager block on the page - * (assuming there are enough items to paginate). - * When using with SQL, the setting LIMIT %d, %d => $a->pager['start'],$a->pager['itemspage'] - * will limit the results to the correct items for the current page. - * The actual page handling is then accomplished at the application layer. - * * @param App $a App instance - * @return string html for pagination #FIXME remove html + * @param int $count [optional] item count (used with alt pager) + * @return Array data for pagination template */ -function paginate(&$a) { - $o = ''; +function paginate_data(&$a, $count=null) { $stripped = preg_replace('/(&page=[0-9]*)/','',$a->query_string); -// $stripped = preg_replace('/&zrl=(.*?)([\?&]|$)/ism','',$stripped); - $stripped = str_replace('q=','',$stripped); $stripped = trim($stripped,'/'); $pagenum = $a->pager['page']; + + if (($a->page_offset != "") AND !strstr($stripped, "&offset=")) + $stripped .= "&offset=".urlencode($a->page_offset); + if (!strpos($stripped, "?")) { + if ($pos = strpos($stripped, "&")) + $stripped = substr($stripped, 0, $pos)."?".substr($stripped, $pos + 1); + } + $url = $a->get_baseurl() . '/' . $stripped; + $data = array(); + function _l(&$d, $name, $url, $text, $class="") { - if($a->pager['total'] > $a->pager['itemspage']) { - $o .= '
'; - if($a->pager['page'] != 1) - $o .= ''."pager['page'] - 1).'">' . t('prev') . ' '; + $d[$name] = array('url'=>$url, 'text'=>$text, 'class'=>$class); + } + + if (!is_null($count)){ + // alt pager + if($a->pager['page']>1) + _l($data, "prev", $url.'&page='.($a->pager['page'] - 1), t('newer')); + if($count>0) + _l($data, "next", $url.'&page='.($a->pager['page'] + 1), t('older')); + } else { + // full pager + if($a->pager['total'] > $a->pager['itemspage']) { + if($a->pager['page'] != 1) + _l($data, "prev", $url.'&page='.($a->pager['page'] - 1), t('prev')); + + _l($data, "first", $url."&page=1", t('first')); - $o .= "" . t('first') . " "; - $numpages = $a->pager['total'] / $a->pager['itemspage']; + $numpages = $a->pager['total'] / $a->pager['itemspage']; $numstart = 1; - $numstop = $numpages; + $numstop = $numpages; - if($numpages > 14) { - $numstart = (($pagenum > 7) ? ($pagenum - 7) : 1); - $numstop = (($pagenum > ($numpages - 7)) ? $numpages : ($numstart + 14)); - } - - for($i = $numstart; $i <= $numstop; $i++){ - if($i == $a->pager['page']) - $o .= ''.(($i < 10) ? ' '.$i : $i); - else - $o .= "".(($i < 10) ? ' '.$i : $i).""; - $o .= ' '; - } + if($numpages > 14) { + $numstart = (($pagenum > 7) ? ($pagenum - 7) : 1); + $numstop = (($pagenum > ($numpages - 7)) ? $numpages : ($numstart + 14)); + } - if(($a->pager['total'] % $a->pager['itemspage']) != 0) { - if($i == $a->pager['page']) - $o .= ''.(($i < 10) ? ' '.$i : $i); - else - $o .= "".(($i < 10) ? ' '.$i : $i).""; - $o .= ' '; - } + $pages = array(); + + for($i = $numstart; $i <= $numstop; $i++){ + if($i == $a->pager['page']) + _l($pages, $i, "#", $i, "current"); + else + _l($pages, $i, $url."&page=$i", $i, "n"); + } + + if(($a->pager['total'] % $a->pager['itemspage']) != 0) { + if($i == $a->pager['page']) + _l($pages, $i, "#", $i, "current"); + else + _l($pages, $i, $url."&page=$i", $i, "n"); + } + + $data['pages'] = $pages; - $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages); - $o .= "" . t('last') . " "; + $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages); + _l($data, "last", $url."&page=$lastpage", t('last')); - if(($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0) - $o .= ''."pager['page'] + 1).'">' . t('next') . ''; - $o .= '
'."\r\n"; + if(($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0) + _l($data, "next", $url."&page=".($a->pager['page'] + 1), t('next')); + + } } - return $o; + return $data; + +}} + +if(! function_exists('paginate')) { +/** + * Automatic pagination. + * + * To use, get the count of total items. + * Then call $a->set_pager_total($number_items); + * Optionally call $a->set_pager_itemspage($n) to the number of items to display on each page + * Then call paginate($a) after the end of the display loop to insert the pager block on the page + * (assuming there are enough items to paginate). + * When using with SQL, the setting LIMIT %d, %d => $a->pager['start'],$a->pager['itemspage'] + * will limit the results to the correct items for the current page. + * The actual page handling is then accomplished at the application layer. + * + * @param App $a App instance + * @return string html for pagination #FIXME remove html + */ +function paginate(&$a) { + + $data = paginate_data($a); + $tpl = get_markup_template("paginate.tpl"); + return replace_macros($tpl, array("pager" => $data)); + }} if(! function_exists('alt_pager')) { @@ -339,34 +385,18 @@ if(! function_exists('alt_pager')) { * @return string html for pagination #FIXME remove html */ function alt_pager(&$a, $i) { - $o = ''; - $stripped = preg_replace('/(&page=[0-9]*)/','',$a->query_string); - $stripped = str_replace('q=','',$stripped); - $stripped = trim($stripped,'/'); - $pagenum = $a->pager['page']; - $url = $a->get_baseurl() . '/' . $stripped; - $o .= '
'; + $data = paginate_data($a, $i); + $tpl = get_markup_template("paginate.tpl"); + return replace_macros($tpl, array('pager' => $data)); - if($a->pager['page']>1) - $o .= "pager['page'] - 1).'" class="pager_newer">' . t('newer') . ''; - if($i>0) { - if($a->pager['page']>1) - $o .= " - "; - $o .= "pager['page'] + 1).'" class="pager_older">' . t('older') . ''; - } - - - $o .= '
'."\r\n"; - - return $o; }} if(! function_exists('expand_acl')) { /** * Turn user/group ACLs stored as angle bracketed text into arrays - * + * * @param string $s * @return array */ @@ -456,9 +486,9 @@ if(! function_exists('photo_new_resource')) { /** * Generate a guaranteed unique photo ID. * safe from birthday paradox - * + * * @return string - */ + */ function photo_new_resource() { do { @@ -479,7 +509,7 @@ if(! function_exists('load_view_file')) { * @deprecated * wrapper to load a view template, checking for alternate * languages before falling back to the default - * + * * @global string $lang * @global App $a * @param string $s view name @@ -564,8 +594,11 @@ function get_markup_template($s, $root = '') { $a = get_app(); $t = $a->template_engine(); - - $template = $t->get_template_file($s, $root); + try { + $template = $t->get_template_file($s, $root); + } catch (Exception $e) { + echo "
".__function__.": ".$e->getMessage()."
"; killme(); + } $a->save_timestamp($stamp1, "file"); @@ -935,7 +968,7 @@ if(! function_exists('linkify')) { * @param string $s */ function linkify($s) { - $s = preg_replace("/(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\'\%\$\!\+]*)/", ' $1', $s); + $s = preg_replace("/(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\'\%\$\!\+]*)/", ' $1', $s); $s = preg_replace("/\<(.*?)(src|href)=(.*?)\&\;(.*?)\>/ism",'<$1$2=$3&$4>',$s); return($s); }} @@ -1100,7 +1133,7 @@ function smilies($s, $sample = false) { ':like', ':dislike', '~friendica ~friendica', - 'red red' + 'red red' ); $params = array('texts' => $texts, 'icons' => $icons, 'string' => $s); @@ -1247,11 +1280,42 @@ if(! function_exists('prepare_body')) { * @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) { +function prepare_body(&$item,$attach = false) { $a = get_app(); call_hooks('prepare_body_init', $item); + $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'])); @@ -1354,7 +1418,7 @@ function prepare_body($item,$attach = false) { $title = ((strlen(trim($mtch[4]))) ? escape_tags(trim($mtch[4])) : escape_tags($mtch[1])); $title .= ' ' . $mtch[2] . ' ' . t('bytes'); - $as .= '' . $icon . ''; + $as .= '' . $icon . ''; } } } @@ -1501,7 +1565,7 @@ if(! function_exists('feed_hublinks')) { * @return string hub link xml elements */ function feed_hublinks() { - + $a = get_app(); $hub = get_config('system','huburl'); $hubxml = ''; @@ -1512,6 +1576,8 @@ function feed_hublinks() { $h = trim($h); if(! strlen($h)) continue; + if ($h === '[internal]') + $h = $a->get_baseurl() . '/pubsubhubbub'; $hubxml .= '' . "\n" ; } } @@ -1546,16 +1612,30 @@ if(! function_exists('get_plink')) { * @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)) { - return array( - 'href' => $item['plink'], - 'title' => t('link to source'), - ); - } - else { - return false; - } + $a = get_app(); + + if ($a->user['nickname'] != "") { + $ret = array( + 'href' => $a->get_baseurl()."/display/".$a->user['nickname']."/".$item['id'], + 'title' => t('link to source'), + ); + $ret["orig"] = $ret["href"]; + + if (x($item,'plink')) + $ret["href"] = $item['plink']; + + } elseif (x($item,'plink') && ($item['private'] != 1)) + $ret = array( + 'href' => $item['plink'], + 'orig' => $item['plink'], + 'title' => t('link to source'), + ); + else + $ret = array(); + + //if (x($item,'plink') && ($item['private'] != 1)) + + return($ret); }} if(! function_exists('unamp')) { @@ -1948,10 +2028,12 @@ function file_tag_update_pconfig($uid,$file_old,$file_new,$type = 'file') { if($type == 'file') { $lbracket = '['; $rbracket = ']'; + $termtype = TERM_FILE; } else { $lbracket = '<'; $rbracket = '>'; + $termtype = TERM_CATEGORY; } $filetags_updated = $saved; @@ -1977,9 +2059,15 @@ function file_tag_update_pconfig($uid,$file_old,$file_new,$type = 'file') { } foreach($deleted_tags as $key => $tag) { - $r = q("select file from item where uid = %d " . file_tag_file_query('item',$tag,$type), - intval($uid) - ); + $r = q("SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d", + dbesc($tag), + intval(TERM_OBJ_POST), + intval($termtype), + intval($uid)); + + //$r = q("select file from item where uid = %d " . file_tag_file_query('item',$tag,$type), + // intval($uid) + //); if(count($r)) { unset($deleted_tags[$key]); @@ -2002,6 +2090,8 @@ function file_tag_update_pconfig($uid,$file_old,$file_new,$type = 'file') { } function file_tag_save_file($uid,$item,$file) { + require_once("include/files.php"); + $result = false; if(! intval($uid)) return false; @@ -2011,11 +2101,14 @@ function file_tag_save_file($uid,$item,$file) { ); if(count($r)) { if(! stristr($r[0]['file'],'[' . file_tag_encode($file) . ']')) - q("update item set file = '%s' where id = %d and uid = %d limit 1", + q("update item set file = '%s' where id = %d and uid = %d", dbesc($r[0]['file'] . '[' . file_tag_encode($file) . ']'), intval($item), intval($uid) ); + + create_files_from_item($item); + $saved = get_pconfig($uid,'system','filetags'); if((! strlen($saved)) || (! stristr($saved,'[' . file_tag_encode($file) . ']'))) set_pconfig($uid,'system','filetags',$saved . '[' . file_tag_encode($file) . ']'); @@ -2025,14 +2118,19 @@ function file_tag_save_file($uid,$item,$file) { } function file_tag_unsave_file($uid,$item,$file,$cat = false) { + require_once("include/files.php"); + $result = false; if(! intval($uid)) return false; - if($cat == true) + if($cat == true) { $pattern = '<' . file_tag_encode($file) . '>' ; - else + $termtype = TERM_CATEGORY; + } else { $pattern = '[' . file_tag_encode($file) . ']' ; + $termtype = TERM_FILE; + } $r = q("select file from item where id = %d and uid = %d limit 1", @@ -2042,15 +2140,22 @@ function file_tag_unsave_file($uid,$item,$file,$cat = false) { if(! count($r)) return false; - q("update item set file = '%s' where id = %d and uid = %d limit 1", + q("update item set file = '%s' where id = %d and uid = %d", dbesc(str_replace($pattern,'',$r[0]['file'])), intval($item), intval($uid) ); - $r = q("select file from item where uid = %d and deleted = 0 " . file_tag_file_query('item',$file,(($cat) ? 'category' : 'file')), - intval($uid) - ); + create_files_from_item($item); + + $r = q("SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d", + dbesc($file), + intval(TERM_OBJ_POST), + intval($termtype), + intval($uid)); + + //$r = q("select file from item where uid = %d and deleted = 0 " . file_tag_file_query('item',$file,(($cat) ? 'category' : 'file')), + //); if(! count($r)) { $saved = get_pconfig($uid,'system','filetags');