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