2011-08-01 23:51:01 +00:00
|
|
|
<?php
|
2017-12-04 13:33:49 +00:00
|
|
|
/**
|
|
|
|
* @file include/text.php
|
|
|
|
*/
|
2018-01-25 02:08:45 +00:00
|
|
|
|
2017-04-30 04:07:00 +00:00
|
|
|
use Friendica\App;
|
2018-02-15 02:33:55 +00:00
|
|
|
use Friendica\Content\Text\BBCode;
|
2018-08-11 20:40:44 +00:00
|
|
|
use Friendica\Core\Protocol;
|
2018-10-30 18:51:21 +00:00
|
|
|
use Friendica\Model\FileTag;
|
2019-07-15 01:48:35 +00:00
|
|
|
use Friendica\Model\Group;
|
2018-11-08 15:14:37 +00:00
|
|
|
use Friendica\Util\Strings;
|
2018-10-29 20:10:35 +00:00
|
|
|
|
2013-03-27 09:11:40 +00:00
|
|
|
/**
|
|
|
|
* Turn user/group ACLs stored as angle bracketed text into arrays
|
2013-12-09 23:13:19 +00:00
|
|
|
*
|
2013-03-27 09:11:40 +00:00
|
|
|
* @param string $s
|
|
|
|
* @return array
|
|
|
|
*/
|
2011-08-01 23:51:01 +00:00
|
|
|
function expand_acl($s) {
|
|
|
|
// turn string array of angle-bracketed elements into numeric array
|
|
|
|
// e.g. "<1><2><3>" => array(1,2,3);
|
2019-07-15 01:48:35 +00:00
|
|
|
preg_match_all('/<(' . Group::FOLLOWERS . '|'. Group::MUTUALS . '|[0-9]+)>/', $s, $matches, PREG_PATTERN_ORDER);
|
2011-08-01 23:51:01 +00:00
|
|
|
|
2019-07-15 01:48:35 +00:00
|
|
|
return $matches[1];
|
2017-11-11 06:42:39 +00:00
|
|
|
}
|
|
|
|
|
2011-08-01 23:51:01 +00:00
|
|
|
|
2013-03-27 09:11:40 +00:00
|
|
|
/**
|
2014-07-30 13:23:36 +00:00
|
|
|
* Wrap ACL elements in angle brackets for storage
|
2013-03-27 09:11:40 +00:00
|
|
|
* @param string $item
|
|
|
|
*/
|
2011-08-01 23:51:01 +00:00
|
|
|
function sanitise_acl(&$item) {
|
2017-05-17 16:42:40 +00:00
|
|
|
if (intval($item)) {
|
2018-11-09 18:29:42 +00:00
|
|
|
$item = '<' . intval(Strings::escapeTags(trim($item))) . '>';
|
2019-07-15 01:48:35 +00:00
|
|
|
} elseif (in_array($item, [Group::FOLLOWERS, Group::MUTUALS])) {
|
|
|
|
$item = '<' . $item . '>';
|
2017-05-17 16:42:40 +00:00
|
|
|
} else {
|
2011-08-01 23:51:01 +00:00
|
|
|
unset($item);
|
2017-05-17 16:42:40 +00:00
|
|
|
}
|
2017-11-11 06:42:39 +00:00
|
|
|
}
|
2011-08-01 23:51:01 +00:00
|
|
|
|
|
|
|
|
2013-03-27 09:11:40 +00:00
|
|
|
/**
|
|
|
|
* Convert an ACL array to a storable string
|
2014-07-30 13:23:36 +00:00
|
|
|
*
|
2013-03-27 09:11:40 +00:00
|
|
|
* Normally ACL permissions will be an array.
|
|
|
|
* We'll also allow a comma-separated string.
|
2014-07-30 13:23:36 +00:00
|
|
|
*
|
2013-03-27 09:11:40 +00:00
|
|
|
* @param string|array $p
|
|
|
|
* @return string
|
|
|
|
*/
|
2011-08-01 23:51:01 +00:00
|
|
|
function perms2str($p) {
|
|
|
|
$ret = '';
|
2017-05-17 16:42:40 +00:00
|
|
|
if (is_array($p)) {
|
2012-11-11 21:56:21 +00:00
|
|
|
$tmp = $p;
|
2017-05-17 16:42:40 +00:00
|
|
|
} else {
|
2017-01-26 15:07:30 +00:00
|
|
|
$tmp = explode(',', $p);
|
2017-05-17 16:42:40 +00:00
|
|
|
}
|
2012-11-11 21:56:21 +00:00
|
|
|
|
2017-04-04 17:48:25 +00:00
|
|
|
if (is_array($tmp)) {
|
2017-05-22 11:04:30 +00:00
|
|
|
array_walk($tmp, 'sanitise_acl');
|
|
|
|
$ret = implode('', $tmp);
|
2011-08-01 23:51:01 +00:00
|
|
|
}
|
|
|
|
return $ret;
|
2017-11-11 06:42:39 +00:00
|
|
|
}
|
2011-08-01 23:51:01 +00:00
|
|
|
|
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
|
2014-07-30 13:23:36 +00:00
|
|
|
* 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
|
2014-07-30 13:23:36 +00:00
|
|
|
*
|
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
|
|
|
|
*/
|
2017-05-17 16:28:00 +00:00
|
|
|
function attribute_contains($attr, $s) {
|
2011-08-01 23:51:01 +00:00
|
|
|
$a = explode(' ', $attr);
|
2017-05-17 16:28:00 +00:00
|
|
|
return (count($a) && in_array($s,$a));
|
2017-11-11 06:42:39 +00:00
|
|
|
}
|
|
|
|
|
2013-03-27 09:11:40 +00:00
|
|
|
/**
|
|
|
|
* Compare activity uri. Knows about activity namespace.
|
2014-07-30 13:23:36 +00:00
|
|
|
*
|
2013-03-27 09:11:40 +00:00
|
|
|
* @param string $haystack
|
|
|
|
* @param string $needle
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2011-08-01 23:51:01 +00:00
|
|
|
function activity_match($haystack,$needle) {
|
2017-05-17 16:28:00 +00:00
|
|
|
return (($haystack === $needle) || ((basename($needle) === $haystack) && strstr($needle, NAMESPACE_ACTIVITY_SCHEMA)));
|
2017-11-11 06:42:39 +00:00
|
|
|
}
|
2011-08-01 23:51:01 +00:00
|
|
|
|
2013-03-27 09:11:40 +00:00
|
|
|
/**
|
|
|
|
* quick and dirty quoted_printable encoding
|
2014-07-30 13:23:36 +00:00
|
|
|
*
|
2013-03-27 09:11:40 +00:00
|
|
|
* @param string $s
|
|
|
|
* @return string
|
2014-07-30 13:23:36 +00:00
|
|
|
*/
|
2011-08-01 23:51:01 +00:00
|
|
|
function qp($s) {
|
2017-05-17 16:28:00 +00:00
|
|
|
return str_replace("%", "=", rawurlencode($s));
|
2017-11-11 06:42:39 +00:00
|
|
|
}
|
|
|
|
|
2013-03-27 09:11:40 +00:00
|
|
|
/**
|
2016-11-19 03:05:56 +00:00
|
|
|
* @brief Find any non-embedded images in private items and add redir links to them
|
2014-07-30 13:23:36 +00:00
|
|
|
*
|
2013-03-27 09:11:40 +00:00
|
|
|
* @param App $a
|
2016-11-19 03:05:56 +00:00
|
|
|
* @param array &$item The field array of an item row
|
2013-03-27 09:11:40 +00:00
|
|
|
*/
|
2016-11-19 03:05:56 +00:00
|
|
|
function redir_private_images($a, &$item)
|
|
|
|
{
|
2019-01-21 16:38:12 +00:00
|
|
|
$matches = [];
|
2013-03-03 16:44:41 +00:00
|
|
|
$cnt = preg_match_all('|\[img\](http[^\[]*?/photo/[a-fA-F0-9]+?(-[0-9]\.[\w]+?)?)\[\/img\]|', $item['body'], $matches, PREG_SET_ORDER);
|
2016-11-19 03:05:56 +00:00
|
|
|
if ($cnt) {
|
|
|
|
foreach ($matches as $mtch) {
|
2016-12-08 04:11:53 +00:00
|
|
|
if (strpos($mtch[1], '/redir') !== false) {
|
2013-03-03 16:44:41 +00:00
|
|
|
continue;
|
2016-11-19 03:05:56 +00:00
|
|
|
}
|
2013-03-03 16:44:41 +00:00
|
|
|
|
2018-08-11 20:40:44 +00:00
|
|
|
if ((local_user() == $item['uid']) && ($item['private'] == 1) && ($item['contact-id'] != $a->contact['id']) && ($item['network'] == Protocol::DFRN)) {
|
2019-09-09 21:37:26 +00:00
|
|
|
$img_url = 'redir/' . $item['contact-id'] . '?url=' . urlencode($mtch[1]);
|
2016-11-19 03:05:56 +00:00
|
|
|
$item['body'] = str_replace($mtch[0], '[img]' . $img_url . '[/img]', $item['body']);
|
2013-03-03 16:44:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-19 03:05:56 +00:00
|
|
|
}
|
2013-03-03 16:44:41 +00:00
|
|
|
|
2013-03-27 09:11:40 +00:00
|
|
|
/**
|
2017-10-13 17:42:27 +00:00
|
|
|
* @brief Given a text string, convert from bbcode to html and add smilie icons.
|
2014-07-30 13:23:36 +00:00
|
|
|
*
|
2017-10-13 17:42:27 +00:00
|
|
|
* @param string $text String with bbcode.
|
2019-06-20 23:31:47 +00:00
|
|
|
* @return string Formatted HTML
|
2019-01-07 15:24:06 +00:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
2013-03-27 09:11:40 +00:00
|
|
|
*/
|
2019-06-20 19:00:33 +00:00
|
|
|
function prepare_text($text)
|
|
|
|
{
|
|
|
|
$s = BBCode::convert($text);
|
2014-09-07 13:48:44 +00:00
|
|
|
return trim($s);
|
2017-10-13 17:42:27 +00:00
|
|
|
}
|
2013-03-27 09:11:40 +00:00
|
|
|
|
2012-09-20 07:46:49 +00:00
|
|
|
/**
|
2013-03-27 09:11:40 +00:00
|
|
|
* return array with details for categories and folders for an item
|
2014-07-30 13:23:36 +00:00
|
|
|
*
|
2013-03-27 09:11:40 +00:00
|
|
|
* @param array $item
|
|
|
|
* @return array
|
2014-07-30 13:23:36 +00:00
|
|
|
*
|
2013-03-27 09:11:40 +00:00
|
|
|
* [
|
|
|
|
* [ // categories array
|
2012-09-20 07:46:49 +00:00
|
|
|
* {
|
|
|
|
* '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',
|
2012-09-20 07:46:49 +00:00
|
|
|
* 'last': 'is the last in this array? true/false',
|
|
|
|
* } ,
|
|
|
|
* ....
|
|
|
|
* ],
|
2013-03-27 09:11:40 +00:00
|
|
|
* [ //folders array
|
|
|
|
* {
|
2012-09-20 07:46:49 +00:00
|
|
|
* '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',
|
|
|
|
* } ,
|
2014-07-30 13:23:36 +00:00
|
|
|
* ....
|
2012-09-20 07:46:49 +00:00
|
|
|
* ]
|
2013-03-27 09:11:40 +00:00
|
|
|
* ]
|
2012-09-20 07:46:49 +00:00
|
|
|
*/
|
2018-01-04 02:12:19 +00:00
|
|
|
function get_cats_and_terms($item)
|
|
|
|
{
|
2018-01-15 13:05:12 +00:00
|
|
|
$categories = [];
|
|
|
|
$folders = [];
|
2017-05-22 11:04:30 +00:00
|
|
|
$first = true;
|
2019-05-27 21:17:53 +00:00
|
|
|
|
2019-05-30 11:54:17 +00:00
|
|
|
foreach (FileTag::fileToArray($item['file'] ?? '', 'category') as $savedFolderName) {
|
2019-05-27 21:17:53 +00:00
|
|
|
$categories[] = [
|
|
|
|
'name' => $savedFolderName,
|
|
|
|
'url' => "#",
|
|
|
|
'removeurl' => ((local_user() == $item['uid']) ? 'filerm/' . $item['id'] . '?f=&cat=' . rawurlencode($savedFolderName) : ""),
|
|
|
|
'first' => $first,
|
|
|
|
'last' => false
|
|
|
|
];
|
|
|
|
$first = false;
|
2015-11-28 01:56:36 +00:00
|
|
|
}
|
2014-07-30 13:23:36 +00:00
|
|
|
|
2017-05-17 16:28:00 +00:00
|
|
|
if (count($categories)) {
|
|
|
|
$categories[count($categories) - 1]['last'] = true;
|
|
|
|
}
|
2012-09-20 07:46:49 +00:00
|
|
|
|
2017-04-04 17:48:25 +00:00
|
|
|
if (local_user() == $item['uid']) {
|
2019-05-30 11:54:17 +00:00
|
|
|
foreach (FileTag::fileToArray($item['file'] ?? '') as $savedFolderName) {
|
2019-05-27 21:17:53 +00:00
|
|
|
$folders[] = [
|
|
|
|
'name' => $savedFolderName,
|
|
|
|
'url' => "#",
|
|
|
|
'removeurl' => ((local_user() == $item['uid']) ? 'filerm/' . $item['id'] . '?f=&term=' . rawurlencode($savedFolderName) : ""),
|
|
|
|
'first' => $first,
|
|
|
|
'last' => false
|
|
|
|
];
|
|
|
|
$first = false;
|
2015-11-28 01:56:36 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-20 07:46:49 +00:00
|
|
|
|
2017-05-17 16:28:00 +00:00
|
|
|
if (count($folders)) {
|
|
|
|
$folders[count($folders) - 1]['last'] = true;
|
|
|
|
}
|
2012-09-24 02:22:48 +00:00
|
|
|
|
2018-01-15 13:05:12 +00:00
|
|
|
return [$categories, $folders];
|
2012-09-20 07:46:49 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2017-11-11 06:42:39 +00:00
|
|
|
function return_bytes($size_str) {
|
2015-11-28 01:56:36 +00:00
|
|
|
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;
|
|
|
|
}
|
2017-11-11 06:42:39 +00:00
|
|
|
}
|
|
|
|
|
2018-05-13 20:50:09 +00:00
|
|
|
/// @TODO Rewrite this
|
2012-06-12 02:36:04 +00:00
|
|
|
function is_a_date_arg($s) {
|
|
|
|
$i = intval($s);
|
2018-05-13 20:50:09 +00:00
|
|
|
|
2017-04-04 17:48:25 +00:00
|
|
|
if ($i > 1900) {
|
2012-06-12 02:36:04 +00:00
|
|
|
$y = date('Y');
|
2018-05-13 20:50:09 +00:00
|
|
|
|
2017-05-17 16:28:00 +00:00
|
|
|
if ($i <= $y + 1 && strpos($s, '-') == 4) {
|
2017-01-26 15:01:56 +00:00
|
|
|
$m = intval(substr($s, 5));
|
2018-05-13 20:50:09 +00:00
|
|
|
|
2017-07-19 21:16:54 +00:00
|
|
|
if ($m > 0 && $m <= 12) {
|
2012-06-12 02:36:04 +00:00
|
|
|
return true;
|
2017-01-26 15:01:56 +00:00
|
|
|
}
|
2012-06-12 02:36:04 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-13 20:50:09 +00:00
|
|
|
|
2012-06-12 02:36:04 +00:00
|
|
|
return false;
|
|
|
|
}
|