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 Formattet HTML.
144 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
146 function prepare_text($text) {
147 if (stristr($text, '[nosmile]')) {
148 $s = BBCode::convert($text);
150 $s = Smilies::replace(BBCode::convert($text));
157 * return array with details for categories and folders for an item
163 * [ // categories array
165 * 'name': 'category name',
166 * 'removeurl': 'url to remove this category',
167 * 'first': 'is the first in this array? true/false',
168 * 'last': 'is the last in this array? true/false',
174 * 'name': 'folder name',
175 * 'removeurl': 'url to remove this folder',
176 * 'first': 'is the first in this array? true/false',
177 * 'last': 'is the last in this array? true/false',
183 function get_cats_and_terms($item)
189 foreach (FileTag::fileToArray($item['file'], 'category') as $savedFolderName) {
191 'name' => $savedFolderName,
193 'removeurl' => ((local_user() == $item['uid']) ? 'filerm/' . $item['id'] . '?f=&cat=' . rawurlencode($savedFolderName) : ""),
200 if (count($categories)) {
201 $categories[count($categories) - 1]['last'] = true;
204 if (local_user() == $item['uid']) {
205 foreach (FileTag::fileToArray($item['file']) as $savedFolderName) {
207 'name' => $savedFolderName,
209 'removeurl' => ((local_user() == $item['uid']) ? 'filerm/' . $item['id'] . '?f=&term=' . rawurlencode($savedFolderName) : ""),
217 if (count($folders)) {
218 $folders[count($folders) - 1]['last'] = true;
221 return [$categories, $folders];
225 * return number of bytes in size (K, M, G)
226 * @param string $size_str
229 function return_bytes($size_str) {
230 switch (substr ($size_str, -1)) {
231 case 'M': case 'm': return (int)$size_str * 1048576;
232 case 'K': case 'k': return (int)$size_str * 1024;
233 case 'G': case 'g': return (int)$size_str * 1073741824;
234 default: return $size_str;
238 function bb_translate_video($s) {
241 $r = preg_match_all("/\[video\](.*?)\[\/video\]/ism",$s,$matches,PREG_SET_ORDER);
243 foreach ($matches as $mtch) {
244 if ((stristr($mtch[1], 'youtube')) || (stristr($mtch[1], 'youtu.be'))) {
245 $s = str_replace($mtch[0], '[youtube]' . $mtch[1] . '[/youtube]', $s);
246 } elseif (stristr($mtch[1], 'vimeo')) {
247 $s = str_replace($mtch[0], '[vimeo]' . $mtch[1] . '[/vimeo]', $s);
254 function undo_post_tagging($s) {
256 $cnt = preg_match_all('/([!#@])\[url=(.*?)\](.*?)\[\/url\]/ism', $s, $matches, PREG_SET_ORDER);
258 foreach ($matches as $mtch) {
259 if (in_array($mtch[1], ['!', '@'])) {
260 $contact = Contact::getDetailsByURL($mtch[2]);
261 $mtch[3] = empty($contact['addr']) ? $mtch[2] : $contact['addr'];
263 $s = str_replace($mtch[0], $mtch[1] . $mtch[3],$s);
269 /// @TODO Rewrite this
270 function is_a_date_arg($s) {
276 if ($i <= $y + 1 && strpos($s, '-') == 4) {
277 $m = intval(substr($s, 5));
279 if ($m > 0 && $m <= 12) {