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;
12 use Friendica\Model\FileTag;
13 use Friendica\Util\Strings;
14 use Friendica\Util\XML;
17 * Turn user/group ACLs stored as angle bracketed text into arrays
22 function expand_acl($s) {
23 // turn string array of angle-bracketed elements into numeric array
24 // e.g. "<1><2><3>" => array(1,2,3);
28 $t = str_replace('<', '', $s);
29 $a = explode('>', $t);
41 * Wrap ACL elements in angle brackets for storage
44 function sanitise_acl(&$item) {
46 $item = '<' . intval(Strings::escapeTags(trim($item))) . '>';
54 * Convert an ACL array to a storable string
56 * Normally ACL permissions will be an array.
57 * We'll also allow a comma-separated string.
59 * @param string|array $p
62 function perms2str($p) {
67 $tmp = explode(',', $p);
71 array_walk($tmp, 'sanitise_acl');
72 $ret = implode('', $tmp);
78 * for html,xml parsing - let's say you've got
79 * an attribute foobar="class1 class2 class3"
80 * and you want to find out if it contains 'class3'.
81 * you can't use a normal sub string search because you
82 * might match 'notclass3' and a regex to do the job is
83 * possible but a bit complicated.
84 * pass the attribute string as $attr and the attribute you
85 * are looking for as $s - returns true if found, otherwise false
87 * @param string $attr attribute value
88 * @param string $s string to search
89 * @return boolean True if found, False otherwise
91 function attribute_contains($attr, $s) {
92 $a = explode(' ', $attr);
93 return (count($a) && in_array($s,$a));
97 * Compare activity uri. Knows about activity namespace.
99 * @param string $haystack
100 * @param string $needle
103 function activity_match($haystack,$needle) {
104 return (($haystack === $needle) || ((basename($needle) === $haystack) && strstr($needle, NAMESPACE_ACTIVITY_SCHEMA)));
108 * quick and dirty quoted_printable encoding
114 return str_replace("%", "=", rawurlencode($s));
118 * @brief Find any non-embedded images in private items and add redir links to them
121 * @param array &$item The field array of an item row
123 function redir_private_images($a, &$item)
126 $cnt = preg_match_all('|\[img\](http[^\[]*?/photo/[a-fA-F0-9]+?(-[0-9]\.[\w]+?)?)\[\/img\]|', $item['body'], $matches, PREG_SET_ORDER);
128 foreach ($matches as $mtch) {
129 if (strpos($mtch[1], '/redir') !== false) {
133 if ((local_user() == $item['uid']) && ($item['private'] == 1) && ($item['contact-id'] != $a->contact['id']) && ($item['network'] == Protocol::DFRN)) {
134 $img_url = 'redir?f=1&quiet=1&url=' . urlencode($mtch[1]) . '&conurl=' . urlencode($item['author-link']);
135 $item['body'] = str_replace($mtch[0], '[img]' . $img_url . '[/img]', $item['body']);
142 * @brief Given a text string, convert from bbcode to html and add smilie icons.
144 * @param string $text String with bbcode.
145 * @return string Formattet HTML.
146 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
148 function prepare_text($text) {
149 if (stristr($text, '[nosmile]')) {
150 $s = BBCode::convert($text);
152 $s = Smilies::replace(BBCode::convert($text));
159 * return array with details for categories and folders for an item
165 * [ // categories array
167 * 'name': 'category name',
168 * 'removeurl': 'url to remove this category',
169 * 'first': 'is the first in this array? true/false',
170 * 'last': 'is the last in this array? true/false',
176 * 'name': 'folder name',
177 * 'removeurl': 'url to remove this folder',
178 * 'first': 'is the first in this array? true/false',
179 * 'last': 'is the last in this array? true/false',
185 function get_cats_and_terms($item)
192 $cnt = preg_match_all('/<(.*?)>/', $item['file'], $matches, PREG_SET_ORDER);
194 foreach ($matches as $mtch) {
196 'name' => XML::escape(FileTag::decode($mtch[1])),
198 'removeurl' => ((local_user() == $item['uid'])?'filerm/' . $item['id'] . '?f=&cat=' . XML::escape(FileTag::decode($mtch[1])):""),
206 if (count($categories)) {
207 $categories[count($categories) - 1]['last'] = true;
210 if (local_user() == $item['uid']) {
213 $cnt = preg_match_all('/\[(.*?)\]/', $item['file'], $matches, PREG_SET_ORDER);
215 foreach ($matches as $mtch) {
217 'name' => XML::escape(FileTag::decode($mtch[1])),
219 'removeurl' => ((local_user() == $item['uid']) ? 'filerm/' . $item['id'] . '?f=&term=' . XML::escape(FileTag::decode($mtch[1])) : ""),
228 if (count($folders)) {
229 $folders[count($folders) - 1]['last'] = true;
232 return [$categories, $folders];
236 * return number of bytes in size (K, M, G)
237 * @param string $size_str
240 function return_bytes($size_str) {
241 switch (substr ($size_str, -1)) {
242 case 'M': case 'm': return (int)$size_str * 1048576;
243 case 'K': case 'k': return (int)$size_str * 1024;
244 case 'G': case 'g': return (int)$size_str * 1073741824;
245 default: return $size_str;
249 function bb_translate_video($s) {
252 $r = preg_match_all("/\[video\](.*?)\[\/video\]/ism",$s,$matches,PREG_SET_ORDER);
254 foreach ($matches as $mtch) {
255 if ((stristr($mtch[1], 'youtube')) || (stristr($mtch[1], 'youtu.be'))) {
256 $s = str_replace($mtch[0], '[youtube]' . $mtch[1] . '[/youtube]', $s);
257 } elseif (stristr($mtch[1], 'vimeo')) {
258 $s = str_replace($mtch[0], '[vimeo]' . $mtch[1] . '[/vimeo]', $s);
265 function undo_post_tagging($s) {
267 $cnt = preg_match_all('/([!#@])\[url=(.*?)\](.*?)\[\/url\]/ism', $s, $matches, PREG_SET_ORDER);
269 foreach ($matches as $mtch) {
270 if (in_array($mtch[1], ['!', '@'])) {
271 $contact = Contact::getDetailsByURL($mtch[2]);
272 $mtch[3] = empty($contact['addr']) ? $mtch[2] : $contact['addr'];
274 $s = str_replace($mtch[0], $mtch[1] . $mtch[3],$s);
280 /// @TODO Rewrite this
281 function is_a_date_arg($s) {
287 if ($i <= $y + 1 && strpos($s, '-') == 4) {
288 $m = intval(substr($s, 5));
290 if ($m > 0 && $m <= 12) {