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