3 * @file include/text.php
7 use Friendica\Content\Smilies;
8 use Friendica\Content\Text\BBCode;
9 use Friendica\Core\Protocol;
10 use Friendica\Model\Contact;
11 use Friendica\Model\FileTag;
12 use Friendica\Util\Strings;
15 * Turn user/group ACLs stored as angle bracketed text into arrays
20 function expand_acl($s) {
21 // turn string array of angle-bracketed elements into numeric array
22 // e.g. "<1><2><3>" => array(1,2,3);
26 $t = str_replace('<', '', $s);
27 $a = explode('>', $t);
39 * Wrap ACL elements in angle brackets for storage
42 function sanitise_acl(&$item) {
44 $item = '<' . intval(Strings::escapeTags(trim($item))) . '>';
52 * Convert an ACL array to a storable string
54 * Normally ACL permissions will be an array.
55 * We'll also allow a comma-separated string.
57 * @param string|array $p
60 function perms2str($p) {
65 $tmp = explode(',', $p);
69 array_walk($tmp, 'sanitise_acl');
70 $ret = implode('', $tmp);
76 * for html,xml parsing - let's say you've got
77 * an attribute foobar="class1 class2 class3"
78 * and you want to find out if it contains 'class3'.
79 * you can't use a normal sub string search because you
80 * might match 'notclass3' and a regex to do the job is
81 * possible but a bit complicated.
82 * pass the attribute string as $attr and the attribute you
83 * are looking for as $s - returns true if found, otherwise false
85 * @param string $attr attribute value
86 * @param string $s string to search
87 * @return boolean True if found, False otherwise
89 function attribute_contains($attr, $s) {
90 $a = explode(' ', $attr);
91 return (count($a) && in_array($s,$a));
95 * Compare activity uri. Knows about activity namespace.
97 * @param string $haystack
98 * @param string $needle
101 function activity_match($haystack,$needle) {
102 return (($haystack === $needle) || ((basename($needle) === $haystack) && strstr($needle, NAMESPACE_ACTIVITY_SCHEMA)));
106 * quick and dirty quoted_printable encoding
112 return str_replace("%", "=", rawurlencode($s));
116 * @brief Find any non-embedded images in private items and add redir links to them
119 * @param array &$item The field array of an item row
121 function redir_private_images($a, &$item)
124 $cnt = preg_match_all('|\[img\](http[^\[]*?/photo/[a-fA-F0-9]+?(-[0-9]\.[\w]+?)?)\[\/img\]|', $item['body'], $matches, PREG_SET_ORDER);
126 foreach ($matches as $mtch) {
127 if (strpos($mtch[1], '/redir') !== false) {
131 if ((local_user() == $item['uid']) && ($item['private'] == 1) && ($item['contact-id'] != $a->contact['id']) && ($item['network'] == Protocol::DFRN)) {
132 $img_url = 'redir?f=1&quiet=1&url=' . urlencode($mtch[1]) . '&conurl=' . urlencode($item['author-link']);
133 $item['body'] = str_replace($mtch[0], '[img]' . $img_url . '[/img]', $item['body']);
140 * @brief Given a text string, convert from bbcode to html and add smilie icons.
142 * @param string $text String with bbcode.
143 * @return string Formatted HTML
144 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
146 function prepare_text($text)
148 $s = BBCode::convert($text);
153 * return array with details for categories and folders for an item
159 * [ // categories array
161 * 'name': 'category name',
162 * 'removeurl': 'url to remove this category',
163 * 'first': 'is the first in this array? true/false',
164 * 'last': 'is the last in this array? true/false',
170 * 'name': 'folder name',
171 * 'removeurl': 'url to remove this folder',
172 * 'first': 'is the first in this array? true/false',
173 * 'last': 'is the last in this array? true/false',
179 function get_cats_and_terms($item)
185 foreach (FileTag::fileToArray($item['file'] ?? '', 'category') as $savedFolderName) {
187 'name' => $savedFolderName,
189 'removeurl' => ((local_user() == $item['uid']) ? 'filerm/' . $item['id'] . '?f=&cat=' . rawurlencode($savedFolderName) : ""),
196 if (count($categories)) {
197 $categories[count($categories) - 1]['last'] = true;
200 if (local_user() == $item['uid']) {
201 foreach (FileTag::fileToArray($item['file'] ?? '') as $savedFolderName) {
203 'name' => $savedFolderName,
205 'removeurl' => ((local_user() == $item['uid']) ? 'filerm/' . $item['id'] . '?f=&term=' . rawurlencode($savedFolderName) : ""),
213 if (count($folders)) {
214 $folders[count($folders) - 1]['last'] = true;
217 return [$categories, $folders];
221 * return number of bytes in size (K, M, G)
222 * @param string $size_str
225 function return_bytes($size_str) {
226 switch (substr ($size_str, -1)) {
227 case 'M': case 'm': return (int)$size_str * 1048576;
228 case 'K': case 'k': return (int)$size_str * 1024;
229 case 'G': case 'g': return (int)$size_str * 1073741824;
230 default: return $size_str;
234 function bb_translate_video($s) {
237 $r = preg_match_all("/\[video\](.*?)\[\/video\]/ism",$s,$matches,PREG_SET_ORDER);
239 foreach ($matches as $mtch) {
240 if ((stristr($mtch[1], 'youtube')) || (stristr($mtch[1], 'youtu.be'))) {
241 $s = str_replace($mtch[0], '[youtube]' . $mtch[1] . '[/youtube]', $s);
242 } elseif (stristr($mtch[1], 'vimeo')) {
243 $s = str_replace($mtch[0], '[vimeo]' . $mtch[1] . '[/vimeo]', $s);
250 function undo_post_tagging($s) {
252 $cnt = preg_match_all('/([!#@])\[url=(.*?)\](.*?)\[\/url\]/ism', $s, $matches, PREG_SET_ORDER);
254 foreach ($matches as $mtch) {
255 if (in_array($mtch[1], ['!', '@'])) {
256 $contact = Contact::getDetailsByURL($mtch[2]);
257 $mtch[3] = empty($contact['addr']) ? $mtch[2] : $contact['addr'];
259 $s = str_replace($mtch[0], $mtch[1] . $mtch[3],$s);
265 /// @TODO Rewrite this
266 function is_a_date_arg($s) {
272 if ($i <= $y + 1 && strpos($s, '-') == 4) {
273 $m = intval(substr($s, 5));
275 if ($m > 0 && $m <= 12) {