friendica_2021.01_tupambae_.../include/text.php

170 lines
3.7 KiB
PHP
Raw Normal View History

<?php
/**
* @file include/text.php
*/
use Friendica\Content\Text\BBCode;
use Friendica\Model\FileTag;
use Friendica\Model\Group;
use Friendica\Util\Strings;
2013-03-27 10:11:40 +01:00
/**
* Wrap ACL elements in angle brackets for storage
2013-03-27 10:11:40 +01:00
* @param string $item
*/
function sanitise_acl(&$item) {
if (intval($item)) {
$item = '<' . intval(Strings::escapeTags(trim($item))) . '>';
} elseif (in_array($item, [Group::FOLLOWERS, Group::MUTUALS])) {
$item = '<' . $item . '>';
} else {
unset($item);
}
}
2013-03-27 10:11:40 +01:00
/**
* Convert an ACL array to a storable string
*
2013-03-27 10:11:40 +01:00
* Normally ACL permissions will be an array.
* We'll also allow a comma-separated string.
*
2013-03-27 10:11:40 +01: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 10:11:40 +01:00
/**
* Compare activity uri. Knows about activity namespace.
*
2013-03-27 10:11:40 +01: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 10:11:40 +01:00
/**
* quick and dirty quoted_printable encoding
*
2013-03-27 10:11:40 +01:00
* @param string $s
* @return string
*/
function qp($s) {
return str_replace("%", "=", rawurlencode($s));
}
2013-03-27 10:11:40 +01:00
/**
* @brief Given a text string, convert from bbcode to html and add smilie icons.
*
* @param string $text String with bbcode.
2019-06-21 01:31:47 +02:00
* @return string Formatted HTML
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
2013-03-27 10:11:40 +01:00
*/
function prepare_text($text)
{
$s = BBCode::convert($text);
return trim($s);
}
2013-03-27 10:11:40 +01:00
/**
2013-03-27 10:11:40 +01:00
* return array with details for categories and folders for an item
*
2013-03-27 10:11:40 +01:00
* @param array $item
* @return array
*
2013-03-27 10:11:40 +01:00
* [
* [ // categories array
* {
* 'name': 'category name',
2013-03-27 10:11:40 +01: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 10:11:40 +01: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 10:11:40 +01:00
* ]
*/
function get_cats_and_terms($item)
{
$categories = [];
$folders = [];
$first = true;
2019-05-30 13:54:17 +02: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 13:54:17 +02: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];
}
/// @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;
}