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-01-09 22:42:04 -05:00
|
|
|
use Friendica\Content\ContactSelector;
|
2017-12-04 09:04:36 -05:00
|
|
|
use Friendica\Content\Feature;
|
2017-11-11 07:21:15 -05:00
|
|
|
use Friendica\Content\Smilies;
|
2018-02-14 21:33:55 -05:00
|
|
|
use Friendica\Content\Text\BBCode;
|
2018-01-17 13:42:40 -05:00
|
|
|
use Friendica\Core\Addon;
|
2017-11-06 21:22:52 -05:00
|
|
|
use Friendica\Core\Config;
|
2018-01-21 13:33:59 -05:00
|
|
|
use Friendica\Core\L10n;
|
2017-11-06 21:22:52 -05:00
|
|
|
use Friendica\Core\PConfig;
|
2018-08-11 16:40:44 -04:00
|
|
|
use Friendica\Core\Protocol;
|
2017-08-26 02:04:21 -04:00
|
|
|
use Friendica\Core\System;
|
2018-07-21 08:40:21 -04:00
|
|
|
use Friendica\Database\DBA;
|
2018-05-19 10:55:27 -04:00
|
|
|
use Friendica\Model\Contact;
|
2018-03-16 21:45:02 -04:00
|
|
|
use Friendica\Model\Event;
|
2018-02-07 15:22:40 -05:00
|
|
|
use Friendica\Model\Item;
|
2018-02-14 21:33:55 -05:00
|
|
|
use Friendica\Render\FriendicaSmarty;
|
2018-01-26 21:38:34 -05:00
|
|
|
use Friendica\Util\DateTimeFormat;
|
2017-12-13 16:37:34 -05:00
|
|
|
use Friendica\Util\Map;
|
2018-07-30 22:06:22 -04:00
|
|
|
use Friendica\Util\Proxy as ProxyUtils;
|
2017-04-30 00:07:00 -04:00
|
|
|
|
2018-10-29 16:10:35 -04:00
|
|
|
use Friendica\Core\Logger;
|
2018-10-31 10:03:42 -04:00
|
|
|
use Friendica\Core\Renderer;
|
2018-10-30 14:51:21 -04:00
|
|
|
use Friendica\Model\FileTag;
|
2018-11-05 07:40:18 -05:00
|
|
|
use Friendica\Util\XML;
|
2018-11-05 21:15:47 -05:00
|
|
|
use Friendica\Content\Text\HTML;
|
2018-10-29 16:10:35 -04:00
|
|
|
|
2017-11-29 16:27:39 -05:00
|
|
|
require_once "include/conversation.php";
|
2011-08-01 19:51:01 -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);
|
2018-01-15 08:05:12 -05:00
|
|
|
$ret = [];
|
2011-08-01 19:51:01 -04:00
|
|
|
|
2017-04-04 13:48:25 -04:00
|
|
|
if (strlen($s)) {
|
2017-05-22 07:04:30 -04:00
|
|
|
$t = str_replace('<', '', $s);
|
|
|
|
$a = explode('>', $t);
|
|
|
|
foreach ($a as $aa) {
|
2017-05-17 12:28:00 -04:00
|
|
|
if (intval($aa)) {
|
2011-08-01 19:51:01 -04:00
|
|
|
$ret[] = intval($aa);
|
2017-05-17 12:28:00 -04:00
|
|
|
}
|
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
|
|
|
/**
|
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)) {
|
2011-08-01 19:51:01 -04:00
|
|
|
$item = '<' . intval(notags(trim($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)
|
|
|
|
{
|
2013-03-03 11:44:41 -05: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-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)) {
|
2016-12-04 21:31:28 -05:00
|
|
|
$img_url = 'redir?f=1&quiet=1&url=' . urlencode($mtch[1]) . '&conurl=' . urlencode($item['author-link']);
|
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.
|
|
|
|
* @return string Formattet HTML.
|
2013-03-27 05:11:40 -04:00
|
|
|
*/
|
2011-08-01 19:51:01 -04:00
|
|
|
function prepare_text($text) {
|
2017-05-17 12:28:00 -04:00
|
|
|
if (stristr($text, '[nosmile]')) {
|
2018-02-14 21:33:55 -05:00
|
|
|
$s = BBCode::convert($text);
|
2017-05-17 12:28:00 -04:00
|
|
|
} else {
|
2018-02-14 21:33:55 -05:00
|
|
|
$s = Smilies::replace(BBCode::convert($text));
|
2017-05-17 12:28:00 -04:00
|
|
|
}
|
2011-08-01 19:51:01 -04:00
|
|
|
|
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 = [];
|
2015-11-27 20:56:36 -05:00
|
|
|
|
2017-05-22 07:04:30 -04:00
|
|
|
$matches = false;
|
|
|
|
$first = true;
|
|
|
|
$cnt = preg_match_all('/<(.*?)>/', $item['file'], $matches, PREG_SET_ORDER);
|
2017-04-04 13:48:25 -04:00
|
|
|
if ($cnt) {
|
|
|
|
foreach ($matches as $mtch) {
|
2018-01-15 08:05:12 -05:00
|
|
|
$categories[] = [
|
2018-11-05 07:40:18 -05:00
|
|
|
'name' => XML::escape(FileTag::decode($mtch[1])),
|
2015-11-27 20:56:36 -05:00
|
|
|
'url' => "#",
|
2018-11-05 07:40:18 -05:00
|
|
|
'removeurl' => ((local_user() == $item['uid'])?'filerm/' . $item['id'] . '?f=&cat=' . XML::escape(FileTag::decode($mtch[1])):""),
|
2015-11-27 20:56:36 -05:00
|
|
|
'first' => $first,
|
|
|
|
'last' => false
|
2018-01-15 08:05:12 -05:00
|
|
|
];
|
2015-11-27 20:56:36 -05:00
|
|
|
$first = false;
|
|
|
|
}
|
|
|
|
}
|
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']) {
|
2017-05-22 07:04:30 -04:00
|
|
|
$matches = false;
|
|
|
|
$first = true;
|
|
|
|
$cnt = preg_match_all('/\[(.*?)\]/', $item['file'], $matches, PREG_SET_ORDER);
|
2017-04-04 13:48:25 -04:00
|
|
|
if ($cnt) {
|
|
|
|
foreach ($matches as $mtch) {
|
2018-01-15 08:05:12 -05:00
|
|
|
$folders[] = [
|
2018-11-05 07:40:18 -05:00
|
|
|
'name' => XML::escape(FileTag::decode($mtch[1])),
|
2015-11-27 20:56:36 -05:00
|
|
|
'url' => "#",
|
2018-11-05 07:40:18 -05:00
|
|
|
'removeurl' => ((local_user() == $item['uid']) ? 'filerm/' . $item['id'] . '?f=&term=' . XML::escape(FileTag::decode($mtch[1])) : ""),
|
2015-11-27 20:56:36 -05:00
|
|
|
'first' => $first,
|
|
|
|
'last' => false
|
2018-01-15 08:05:12 -05:00
|
|
|
];
|
2015-11-27 20:56:36 -05:00
|
|
|
$first = false;
|
2012-09-23 22:22:48 -04:00
|
|
|
}
|
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
|
|
|
|
* @return number
|
|
|
|
*/
|
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;
|
|
|
|
}
|