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