friendica/include/text.php

283 lines
6.8 KiB
PHP
Raw Normal View History

<?php
/**
* @file include/text.php
*/
use Friendica\App;
use Friendica\Content\Smilies;
use Friendica\Content\Text\BBCode;
use Friendica\Core\Protocol;
use Friendica\Model\Contact;
use Friendica\Model\FileTag;
use Friendica\Util\Strings;
2013-03-27 09:11:40 +00:00
/**
* Turn user/group ACLs stored as angle bracketed text into arrays
*
2013-03-27 09:11:40 +00:00
* @param string $s
* @return array
*/
function expand_acl($s) {
// turn string array of angle-bracketed elements into numeric array
// e.g. "<1><2><3>" => array(1,2,3);
$ret = [];
if (strlen($s)) {
$t = str_replace('<', '', $s);
$a = explode('>', $t);
foreach ($a as $aa) {
if (intval($aa)) {
$ret[] = intval($aa);
}
}
}
return $ret;
}
2013-03-27 09:11:40 +00:00
/**
* Wrap ACL elements in angle brackets for storage
2013-03-27 09:11:40 +00:00
* @param string $item
*/
function sanitise_acl(&$item) {
if (intval($item)) {
$item = '<' . intval(Strings::escapeTags(trim($item))) . '>';
} else {
unset($item);
}
}
2013-03-27 09:11:40 +00:00
/**
* Convert an ACL array to a storable string
*
2013-03-27 09:11:40 +00:00
* Normally ACL permissions will be an array.
* We'll also allow a comma-separated string.
*
2013-03-27 09:11:40 +00:00
* @param string|array $p
* @return string
*/
function perms2str($p) {
$ret = '';
if (is_array($p)) {
$tmp = $p;
} else {
$tmp = explode(',', $p);
}
if (is_array($tmp)) {
array_walk($tmp, 'sanitise_acl');
$ret = implode('', $tmp);
}
return $ret;
}
2013-03-27 09:11:40 +00:00
/**
* for html,xml parsing - let's say you've got
* an attribute foobar="class1 class2 class3"
* and you want to find out if it contains 'class3'.
* you can't use a normal sub string search because you
* might match 'notclass3' and a regex to do the job is
* possible but a bit complicated.
* pass the attribute string as $attr and the attribute you
2013-03-27 09:11:40 +00:00
* are looking for as $s - returns true if found, otherwise false
*
2013-03-27 09:11:40 +00:00
* @param string $attr attribute value
* @param string $s string to search
* @return boolean True if found, False otherwise
*/
function attribute_contains($attr, $s) {
$a = explode(' ', $attr);
return (count($a) && in_array($s,$a));
}
2013-03-27 09:11:40 +00:00
/**
* Compare activity uri. Knows about activity namespace.
*
2013-03-27 09:11:40 +00:00
* @param string $haystack
* @param string $needle
* @return boolean
*/
function activity_match($haystack,$needle) {
return (($haystack === $needle) || ((basename($needle) === $haystack) && strstr($needle, NAMESPACE_ACTIVITY_SCHEMA)));
}
2013-03-27 09:11:40 +00:00
/**
* quick and dirty quoted_printable encoding
*
2013-03-27 09:11:40 +00:00
* @param string $s
* @return string
*/
function qp($s) {
return str_replace("%", "=", rawurlencode($s));
}
2013-03-27 09:11:40 +00:00
/**
* @brief Find any non-embedded images in private items and add redir links to them
*
2013-03-27 09:11:40 +00:00
* @param App $a
* @param array &$item The field array of an item row
2013-03-27 09:11:40 +00:00
*/
function redir_private_images($a, &$item)
{
2019-01-21 16:38:12 +00:00
$matches = [];
$cnt = preg_match_all('|\[img\](http[^\[]*?/photo/[a-fA-F0-9]+?(-[0-9]\.[\w]+?)?)\[\/img\]|', $item['body'], $matches, PREG_SET_ORDER);
if ($cnt) {
foreach ($matches as $mtch) {
2016-12-08 04:11:53 +00:00
if (strpos($mtch[1], '/redir') !== false) {
continue;
}
if ((local_user() == $item['uid']) && ($item['private'] == 1) && ($item['contact-id'] != $a->contact['id']) && ($item['network'] == Protocol::DFRN)) {
$img_url = 'redir?f=1&quiet=1&url=' . urlencode($mtch[1]) . '&conurl=' . urlencode($item['author-link']);
$item['body'] = str_replace($mtch[0], '[img]' . $img_url . '[/img]', $item['body']);
}
}
}
}
2013-03-27 09:11:40 +00:00
/**
* @brief Given a text string, convert from bbcode to html and add smilie icons.
*
* @param string $text String with bbcode.
* @return string Formattet HTML.
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
2013-03-27 09:11:40 +00:00
*/
function prepare_text($text)
{
$s = BBCode::convert($text);
return trim($s);
}
2013-03-27 09:11:40 +00:00
/**
2013-03-27 09:11:40 +00:00
* return array with details for categories and folders for an item
*
2013-03-27 09:11:40 +00:00
* @param array $item
* @return array
*
2013-03-27 09:11:40 +00:00
* [
* [ // categories array
* {
* 'name': 'category name',
2013-03-27 09:11:40 +00:00
* 'removeurl': 'url to remove this category',
* 'first': 'is the first in this array? true/false',
* 'last': 'is the last in this array? true/false',
* } ,
* ....
* ],
2013-03-27 09:11:40 +00:00
* [ //folders array
* {
* 'name': 'folder name',
* 'removeurl': 'url to remove this folder',
* 'first': 'is the first in this array? true/false',
* 'last': 'is the last in this array? true/false',
* } ,
* ....
* ]
2013-03-27 09:11:40 +00:00
* ]
*/
function get_cats_and_terms($item)
{
$categories = [];
$folders = [];
$first = true;
2019-05-30 11:54:17 +00:00
foreach (FileTag::fileToArray($item['file'] ?? '', 'category') as $savedFolderName) {
$categories[] = [
'name' => $savedFolderName,
'url' => "#",
'removeurl' => ((local_user() == $item['uid']) ? 'filerm/' . $item['id'] . '?f=&cat=' . rawurlencode($savedFolderName) : ""),
'first' => $first,
'last' => false
];
$first = false;
}
if (count($categories)) {
$categories[count($categories) - 1]['last'] = true;
}
if (local_user() == $item['uid']) {
2019-05-30 11:54:17 +00:00
foreach (FileTag::fileToArray($item['file'] ?? '') as $savedFolderName) {
$folders[] = [
'name' => $savedFolderName,
'url' => "#",
'removeurl' => ((local_user() == $item['uid']) ? 'filerm/' . $item['id'] . '?f=&term=' . rawurlencode($savedFolderName) : ""),
'first' => $first,
'last' => false
];
$first = false;
}
}
if (count($folders)) {
$folders[count($folders) - 1]['last'] = true;
}
return [$categories, $folders];
}
2013-03-27 09:11:40 +00:00
/**
* return number of bytes in size (K, M, G)
* @param string $size_str
2019-01-21 16:36:01 +00:00
* @return int
2013-03-27 09:11:40 +00:00
*/
function return_bytes($size_str) {
switch (substr ($size_str, -1)) {
case 'M': case 'm': return (int)$size_str * 1048576;
case 'K': case 'k': return (int)$size_str * 1024;
case 'G': case 'g': return (int)$size_str * 1073741824;
default: return $size_str;
}
}
function bb_translate_video($s) {
$matches = null;
$r = preg_match_all("/\[video\](.*?)\[\/video\]/ism",$s,$matches,PREG_SET_ORDER);
if ($r) {
foreach ($matches as $mtch) {
if ((stristr($mtch[1], 'youtube')) || (stristr($mtch[1], 'youtu.be'))) {
$s = str_replace($mtch[0], '[youtube]' . $mtch[1] . '[/youtube]', $s);
} elseif (stristr($mtch[1], 'vimeo')) {
$s = str_replace($mtch[0], '[vimeo]' . $mtch[1] . '[/vimeo]', $s);
}
}
}
return $s;
}
2012-03-22 23:17:10 +00:00
function undo_post_tagging($s) {
$matches = null;
$cnt = preg_match_all('/([!#@])\[url=(.*?)\](.*?)\[\/url\]/ism', $s, $matches, PREG_SET_ORDER);
if ($cnt) {
foreach ($matches as $mtch) {
if (in_array($mtch[1], ['!', '@'])) {
$contact = Contact::getDetailsByURL($mtch[2]);
$mtch[3] = empty($contact['addr']) ? $mtch[2] : $contact['addr'];
}
2012-03-22 23:17:10 +00:00
$s = str_replace($mtch[0], $mtch[1] . $mtch[3],$s);
}
}
return $s;
}
/// @TODO Rewrite this
function is_a_date_arg($s) {
$i = intval($s);
if ($i > 1900) {
$y = date('Y');
if ($i <= $y + 1 && strpos($s, '-') == 4) {
$m = intval(substr($s, 5));
if ($m > 0 && $m <= 12) {
return true;
}
}
}
return false;
}