Merge pull request #7255 from annando/issue-7223
[friendica.git/.git] / include / text.php
1 <?php
2 /**
3  * @file include/text.php
4  */
5
6 use Friendica\App;
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;
13
14 /**
15  * Turn user/group ACLs stored as angle bracketed text into arrays
16  *
17  * @param string $s
18  * @return array
19  */
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         $ret = [];
24
25         if (strlen($s)) {
26                 $t = str_replace('<', '', $s);
27                 $a = explode('>', $t);
28                 foreach ($a as $aa) {
29                         if (intval($aa)) {
30                                 $ret[] = intval($aa);
31                         }
32                 }
33         }
34         return $ret;
35 }
36
37
38 /**
39  * Wrap ACL elements in angle brackets for storage
40  * @param string $item
41  */
42 function sanitise_acl(&$item) {
43         if (intval($item)) {
44                 $item = '<' . intval(Strings::escapeTags(trim($item))) . '>';
45         } else {
46                 unset($item);
47         }
48 }
49
50
51 /**
52  * Convert an ACL array to a storable string
53  *
54  * Normally ACL permissions will be an array.
55  * We'll also allow a comma-separated string.
56  *
57  * @param string|array $p
58  * @return string
59  */
60 function perms2str($p) {
61         $ret = '';
62         if (is_array($p)) {
63                 $tmp = $p;
64         } else {
65                 $tmp = explode(',', $p);
66         }
67
68         if (is_array($tmp)) {
69                 array_walk($tmp, 'sanitise_acl');
70                 $ret = implode('', $tmp);
71         }
72         return $ret;
73 }
74
75 /**
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
84  *
85  * @param string $attr attribute value
86  * @param string $s string to search
87  * @return boolean True if found, False otherwise
88  */
89 function attribute_contains($attr, $s) {
90         $a = explode(' ', $attr);
91         return (count($a) && in_array($s,$a));
92 }
93
94 /**
95  * Compare activity uri. Knows about activity namespace.
96  *
97  * @param string $haystack
98  * @param string $needle
99  * @return boolean
100  */
101 function activity_match($haystack,$needle) {
102         return (($haystack === $needle) || ((basename($needle) === $haystack) && strstr($needle, NAMESPACE_ACTIVITY_SCHEMA)));
103 }
104
105 /**
106  * quick and dirty quoted_printable encoding
107  *
108  * @param string $s
109  * @return string
110  */
111 function qp($s) {
112         return str_replace("%", "=", rawurlencode($s));
113 }
114
115 /**
116  * @brief Find any non-embedded images in private items and add redir links to them
117  *
118  * @param App $a
119  * @param array &$item The field array of an item row
120  */
121 function redir_private_images($a, &$item)
122 {
123         $matches = [];
124         $cnt = preg_match_all('|\[img\](http[^\[]*?/photo/[a-fA-F0-9]+?(-[0-9]\.[\w]+?)?)\[\/img\]|', $item['body'], $matches, PREG_SET_ORDER);
125         if ($cnt) {
126                 foreach ($matches as $mtch) {
127                         if (strpos($mtch[1], '/redir') !== false) {
128                                 continue;
129                         }
130
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']);
134                         }
135                 }
136         }
137 }
138
139 /**
140  * @brief Given a text string, convert from bbcode to html and add smilie icons.
141  *
142  * @param string $text String with bbcode.
143  * @return string Formattet HTML.
144  * @throws \Friendica\Network\HTTPException\InternalServerErrorException
145  */
146 function prepare_text($text) {
147         if (stristr($text, '[nosmile]')) {
148                 $s = BBCode::convert($text);
149         } else {
150                 $s = Smilies::replace(BBCode::convert($text));
151         }
152
153         return trim($s);
154 }
155
156 /**
157  * return array with details for categories and folders for an item
158  *
159  * @param array $item
160  * @return array
161  *
162   * [
163  *      [ // categories array
164  *          {
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',
169  *           } ,
170  *           ....
171  *       ],
172  *       [ //folders array
173  *                      {
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',
178  *           } ,
179  *           ....
180  *       ]
181  *  ]
182  */
183 function get_cats_and_terms($item)
184 {
185         $categories = [];
186         $folders = [];
187         $first = true;
188
189         foreach (FileTag::fileToArray($item['file'] ?? '', 'category') as $savedFolderName) {
190                 $categories[] = [
191                         'name' => $savedFolderName,
192                         'url' => "#",
193                         'removeurl' => ((local_user() == $item['uid']) ? 'filerm/' . $item['id'] . '?f=&cat=' . rawurlencode($savedFolderName) : ""),
194                         'first' => $first,
195                         'last' => false
196                 ];
197                 $first = false;
198         }
199
200         if (count($categories)) {
201                 $categories[count($categories) - 1]['last'] = true;
202         }
203
204         if (local_user() == $item['uid']) {
205                 foreach (FileTag::fileToArray($item['file'] ?? '') as $savedFolderName) {
206                         $folders[] = [
207                                 'name' => $savedFolderName,
208                                 'url' => "#",
209                                 'removeurl' => ((local_user() == $item['uid']) ? 'filerm/' . $item['id'] . '?f=&term=' . rawurlencode($savedFolderName) : ""),
210                                 'first' => $first,
211                                 'last' => false
212                         ];
213                         $first = false;
214                 }
215         }
216
217         if (count($folders)) {
218                 $folders[count($folders) - 1]['last'] = true;
219         }
220
221         return [$categories, $folders];
222 }
223
224 /**
225  * return number of bytes in size (K, M, G)
226  * @param string $size_str
227  * @return int
228  */
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;
235         }
236 }
237
238 function bb_translate_video($s) {
239
240         $matches = null;
241         $r = preg_match_all("/\[video\](.*?)\[\/video\]/ism",$s,$matches,PREG_SET_ORDER);
242         if ($r) {
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);
248                         }
249                 }
250         }
251         return $s;
252 }
253
254 function undo_post_tagging($s) {
255         $matches = null;
256         $cnt = preg_match_all('/([!#@])\[url=(.*?)\](.*?)\[\/url\]/ism', $s, $matches, PREG_SET_ORDER);
257         if ($cnt) {
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'];
262                         }
263                         $s = str_replace($mtch[0], $mtch[1] . $mtch[3],$s);
264                 }
265         }
266         return $s;
267 }
268
269 /// @TODO Rewrite this
270 function is_a_date_arg($s) {
271         $i = intval($s);
272
273         if ($i > 1900) {
274                 $y = date('Y');
275
276                 if ($i <= $y + 1 && strpos($s, '-') == 4) {
277                         $m = intval(substr($s, 5));
278
279                         if ($m > 0 && $m <= 12) {
280                                 return true;
281                         }
282                 }
283         }
284
285         return false;
286 }