3 * @file include/text.php
7 use Friendica\Content\Text\BBCode;
8 use Friendica\Core\Protocol;
9 use Friendica\Model\Contact;
10 use Friendica\Model\FileTag;
11 use Friendica\Model\Group;
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);
23 preg_match_all('/<(' . Group::FOLLOWERS . '|'. Group::MUTUALS . '|[0-9]+)>/', $s, $matches, PREG_PATTERN_ORDER);
30 * Wrap ACL elements in angle brackets for storage
33 function sanitise_acl(&$item) {
35 $item = '<' . intval(Strings::escapeTags(trim($item))) . '>';
36 } elseif (in_array($item, [Group::FOLLOWERS, Group::MUTUALS])) {
37 $item = '<' . $item . '>';
45 * Convert an ACL array to a storable string
47 * Normally ACL permissions will be an array.
48 * We'll also allow a comma-separated string.
50 * @param string|array $p
53 function perms2str($p) {
58 $tmp = explode(',', $p);
62 array_walk($tmp, 'sanitise_acl');
63 $ret = implode('', $tmp);
69 * for html,xml parsing - let's say you've got
70 * an attribute foobar="class1 class2 class3"
71 * and you want to find out if it contains 'class3'.
72 * you can't use a normal sub string search because you
73 * might match 'notclass3' and a regex to do the job is
74 * possible but a bit complicated.
75 * pass the attribute string as $attr and the attribute you
76 * are looking for as $s - returns true if found, otherwise false
78 * @param string $attr attribute value
79 * @param string $s string to search
80 * @return boolean True if found, False otherwise
82 function attribute_contains($attr, $s) {
83 $a = explode(' ', $attr);
84 return (count($a) && in_array($s,$a));
88 * Compare activity uri. Knows about activity namespace.
90 * @param string $haystack
91 * @param string $needle
94 function activity_match($haystack,$needle) {
95 return (($haystack === $needle) || ((basename($needle) === $haystack) && strstr($needle, NAMESPACE_ACTIVITY_SCHEMA)));
99 * quick and dirty quoted_printable encoding
105 return str_replace("%", "=", rawurlencode($s));
109 * @brief Find any non-embedded images in private items and add redir links to them
112 * @param array &$item The field array of an item row
114 function redir_private_images($a, &$item)
117 $cnt = preg_match_all('|\[img\](http[^\[]*?/photo/[a-fA-F0-9]+?(-[0-9]\.[\w]+?)?)\[\/img\]|', $item['body'], $matches, PREG_SET_ORDER);
119 foreach ($matches as $mtch) {
120 if (strpos($mtch[1], '/redir') !== false) {
124 if ((local_user() == $item['uid']) && ($item['private'] == 1) && ($item['contact-id'] != $a->contact['id']) && ($item['network'] == Protocol::DFRN)) {
125 $img_url = 'redir/' . $item['contact-id'] . '?url=' . urlencode($mtch[1]);
126 $item['body'] = str_replace($mtch[0], '[img]' . $img_url . '[/img]', $item['body']);
133 * @brief Given a text string, convert from bbcode to html and add smilie icons.
135 * @param string $text String with bbcode.
136 * @return string Formatted HTML
137 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
139 function prepare_text($text)
141 $s = BBCode::convert($text);
146 * return array with details for categories and folders for an item
152 * [ // categories array
154 * 'name': 'category name',
155 * 'removeurl': 'url to remove this category',
156 * 'first': 'is the first in this array? true/false',
157 * 'last': 'is the last in this array? true/false',
163 * 'name': 'folder name',
164 * 'removeurl': 'url to remove this folder',
165 * 'first': 'is the first in this array? true/false',
166 * 'last': 'is the last in this array? true/false',
172 function get_cats_and_terms($item)
178 foreach (FileTag::fileToArray($item['file'] ?? '', 'category') as $savedFolderName) {
180 'name' => $savedFolderName,
182 'removeurl' => ((local_user() == $item['uid']) ? 'filerm/' . $item['id'] . '?f=&cat=' . rawurlencode($savedFolderName) : ""),
189 if (count($categories)) {
190 $categories[count($categories) - 1]['last'] = true;
193 if (local_user() == $item['uid']) {
194 foreach (FileTag::fileToArray($item['file'] ?? '') as $savedFolderName) {
196 'name' => $savedFolderName,
198 'removeurl' => ((local_user() == $item['uid']) ? 'filerm/' . $item['id'] . '?f=&term=' . rawurlencode($savedFolderName) : ""),
206 if (count($folders)) {
207 $folders[count($folders) - 1]['last'] = true;
210 return [$categories, $folders];
214 * return number of bytes in size (K, M, G)
215 * @param string $size_str
218 function return_bytes($size_str) {
219 switch (substr ($size_str, -1)) {
220 case 'M': case 'm': return (int)$size_str * 1048576;
221 case 'K': case 'k': return (int)$size_str * 1024;
222 case 'G': case 'g': return (int)$size_str * 1073741824;
223 default: return $size_str;
227 function bb_translate_video($s) {
230 $r = preg_match_all("/\[video\](.*?)\[\/video\]/ism",$s,$matches,PREG_SET_ORDER);
232 foreach ($matches as $mtch) {
233 if ((stristr($mtch[1], 'youtube')) || (stristr($mtch[1], 'youtu.be'))) {
234 $s = str_replace($mtch[0], '[youtube]' . $mtch[1] . '[/youtube]', $s);
235 } elseif (stristr($mtch[1], 'vimeo')) {
236 $s = str_replace($mtch[0], '[vimeo]' . $mtch[1] . '[/vimeo]', $s);
243 function undo_post_tagging($s) {
245 $cnt = preg_match_all('/([!#@])\[url=(.*?)\](.*?)\[\/url\]/ism', $s, $matches, PREG_SET_ORDER);
247 foreach ($matches as $mtch) {
248 if (in_array($mtch[1], ['!', '@'])) {
249 $contact = Contact::getDetailsByURL($mtch[2]);
250 $mtch[3] = empty($contact['addr']) ? $mtch[2] : $contact['addr'];
252 $s = str_replace($mtch[0], $mtch[1] . $mtch[3],$s);
258 /// @TODO Rewrite this
259 function is_a_date_arg($s) {
265 if ($i <= $y + 1 && strpos($s, '-') == 4) {
266 $m = intval(substr($s, 5));
268 if ($m > 0 && $m <= 12) {