]> git.mxchange.org Git - friendica.git/blob - include/text.php
More code standard
[friendica.git] / include / text.php
1 <?php
2 /**
3  * @file include/text.php
4  */
5
6 use Friendica\App;
7 use Friendica\Content\ContactSelector;
8 use Friendica\Content\Feature;
9 use Friendica\Content\Smilies;
10 use Friendica\Content\Text\BBCode;
11 use Friendica\Core\Addon;
12 use Friendica\Core\Config;
13 use Friendica\Core\L10n;
14 use Friendica\Core\PConfig;
15 use Friendica\Core\Protocol;
16 use Friendica\Core\System;
17 use Friendica\Database\DBA;
18 use Friendica\Model\Contact;
19 use Friendica\Model\Event;
20 use Friendica\Model\Item;
21 use Friendica\Render\FriendicaSmarty;
22 use Friendica\Util\DateTimeFormat;
23 use Friendica\Util\Map;
24 use Friendica\Util\Proxy as ProxyUtils;
25
26 use Friendica\Core\Logger;
27 use Friendica\Core\Renderer;
28 use Friendica\Model\FileTag;
29 use Friendica\Util\Strings;
30 use Friendica\Util\XML;
31 use Friendica\Content\Text\HTML;
32
33 /**
34  * Turn user/group ACLs stored as angle bracketed text into arrays
35  *
36  * @param string $s
37  * @return array
38  */
39 function expand_acl($s) {
40         // turn string array of angle-bracketed elements into numeric array
41         // e.g. "<1><2><3>" => array(1,2,3);
42         $ret = [];
43
44         if (strlen($s)) {
45                 $t = str_replace('<', '', $s);
46                 $a = explode('>', $t);
47                 foreach ($a as $aa) {
48                         if (intval($aa)) {
49                                 $ret[] = intval($aa);
50                         }
51                 }
52         }
53         return $ret;
54 }
55
56
57 /**
58  * Wrap ACL elements in angle brackets for storage
59  * @param string $item
60  */
61 function sanitise_acl(&$item) {
62         if (intval($item)) {
63                 $item = '<' . intval(Strings::escapeTags(trim($item))) . '>';
64         } else {
65                 unset($item);
66         }
67 }
68
69
70 /**
71  * Convert an ACL array to a storable string
72  *
73  * Normally ACL permissions will be an array.
74  * We'll also allow a comma-separated string.
75  *
76  * @param string|array $p
77  * @return string
78  */
79 function perms2str($p) {
80         $ret = '';
81         if (is_array($p)) {
82                 $tmp = $p;
83         } else {
84                 $tmp = explode(',', $p);
85         }
86
87         if (is_array($tmp)) {
88                 array_walk($tmp, 'sanitise_acl');
89                 $ret = implode('', $tmp);
90         }
91         return $ret;
92 }
93
94 /**
95  *  for html,xml parsing - let's say you've got
96  *  an attribute foobar="class1 class2 class3"
97  *  and you want to find out if it contains 'class3'.
98  *  you can't use a normal sub string search because you
99  *  might match 'notclass3' and a regex to do the job is
100  *  possible but a bit complicated.
101  *  pass the attribute string as $attr and the attribute you
102  *  are looking for as $s - returns true if found, otherwise false
103  *
104  * @param string $attr attribute value
105  * @param string $s string to search
106  * @return boolean True if found, False otherwise
107  */
108 function attribute_contains($attr, $s) {
109         $a = explode(' ', $attr);
110         return (count($a) && in_array($s,$a));
111 }
112
113 /**
114  * Compare activity uri. Knows about activity namespace.
115  *
116  * @param string $haystack
117  * @param string $needle
118  * @return boolean
119  */
120 function activity_match($haystack,$needle) {
121         return (($haystack === $needle) || ((basename($needle) === $haystack) && strstr($needle, NAMESPACE_ACTIVITY_SCHEMA)));
122 }
123
124 /**
125  * quick and dirty quoted_printable encoding
126  *
127  * @param string $s
128  * @return string
129  */
130 function qp($s) {
131         return str_replace("%", "=", rawurlencode($s));
132 }
133
134 /**
135  * @brief Find any non-embedded images in private items and add redir links to them
136  *
137  * @param App $a
138  * @param array &$item The field array of an item row
139  */
140 function redir_private_images($a, &$item)
141 {
142         $matches = false;
143         $cnt = preg_match_all('|\[img\](http[^\[]*?/photo/[a-fA-F0-9]+?(-[0-9]\.[\w]+?)?)\[\/img\]|', $item['body'], $matches, PREG_SET_ORDER);
144         if ($cnt) {
145                 foreach ($matches as $mtch) {
146                         if (strpos($mtch[1], '/redir') !== false) {
147                                 continue;
148                         }
149
150                         if ((local_user() == $item['uid']) && ($item['private'] == 1) && ($item['contact-id'] != $a->contact['id']) && ($item['network'] == Protocol::DFRN)) {
151                                 $img_url = 'redir?f=1&quiet=1&url=' . urlencode($mtch[1]) . '&conurl=' . urlencode($item['author-link']);
152                                 $item['body'] = str_replace($mtch[0], '[img]' . $img_url . '[/img]', $item['body']);
153                         }
154                 }
155         }
156 }
157
158 /**
159  * @brief Given a text string, convert from bbcode to html and add smilie icons.
160  *
161  * @param string $text String with bbcode.
162  * @return string Formattet HTML.
163  */
164 function prepare_text($text) {
165         if (stristr($text, '[nosmile]')) {
166                 $s = BBCode::convert($text);
167         } else {
168                 $s = Smilies::replace(BBCode::convert($text));
169         }
170
171         return trim($s);
172 }
173
174 /**
175  * return array with details for categories and folders for an item
176  *
177  * @param array $item
178  * @return array
179  *
180   * [
181  *      [ // categories array
182  *          {
183  *               'name': 'category name',
184  *               'removeurl': 'url to remove this category',
185  *               'first': 'is the first in this array? true/false',
186  *               'last': 'is the last in this array? true/false',
187  *           } ,
188  *           ....
189  *       ],
190  *       [ //folders array
191  *                      {
192  *               'name': 'folder name',
193  *               'removeurl': 'url to remove this folder',
194  *               'first': 'is the first in this array? true/false',
195  *               'last': 'is the last in this array? true/false',
196  *           } ,
197  *           ....
198  *       ]
199  *  ]
200  */
201 function get_cats_and_terms($item)
202 {
203         $categories = [];
204         $folders = [];
205
206         $matches = false;
207         $first = true;
208         $cnt = preg_match_all('/<(.*?)>/', $item['file'], $matches, PREG_SET_ORDER);
209         if ($cnt) {
210                 foreach ($matches as $mtch) {
211                         $categories[] = [
212                                 'name' => XML::escape(FileTag::decode($mtch[1])),
213                                 'url' =>  "#",
214                                 'removeurl' => ((local_user() == $item['uid'])?'filerm/' . $item['id'] . '?f=&cat=' . XML::escape(FileTag::decode($mtch[1])):""),
215                                 'first' => $first,
216                                 'last' => false
217                         ];
218                         $first = false;
219                 }
220         }
221
222         if (count($categories)) {
223                 $categories[count($categories) - 1]['last'] = true;
224         }
225
226         if (local_user() == $item['uid']) {
227                 $matches = false;
228                 $first = true;
229                 $cnt = preg_match_all('/\[(.*?)\]/', $item['file'], $matches, PREG_SET_ORDER);
230                 if ($cnt) {
231                         foreach ($matches as $mtch) {
232                                 $folders[] = [
233                                         'name' => XML::escape(FileTag::decode($mtch[1])),
234                                         'url' =>  "#",
235                                         'removeurl' => ((local_user() == $item['uid']) ? 'filerm/' . $item['id'] . '?f=&term=' . XML::escape(FileTag::decode($mtch[1])) : ""),
236                                         'first' => $first,
237                                         'last' => false
238                                 ];
239                                 $first = false;
240                         }
241                 }
242         }
243
244         if (count($folders)) {
245                 $folders[count($folders) - 1]['last'] = true;
246         }
247
248         return [$categories, $folders];
249 }
250
251 /**
252  * return number of bytes in size (K, M, G)
253  * @param string $size_str
254  * @return number
255  */
256 function return_bytes($size_str) {
257         switch (substr ($size_str, -1)) {
258                 case 'M': case 'm': return (int)$size_str * 1048576;
259                 case 'K': case 'k': return (int)$size_str * 1024;
260                 case 'G': case 'g': return (int)$size_str * 1073741824;
261                 default: return $size_str;
262         }
263 }
264
265 function bb_translate_video($s) {
266
267         $matches = null;
268         $r = preg_match_all("/\[video\](.*?)\[\/video\]/ism",$s,$matches,PREG_SET_ORDER);
269         if ($r) {
270                 foreach ($matches as $mtch) {
271                         if ((stristr($mtch[1], 'youtube')) || (stristr($mtch[1], 'youtu.be'))) {
272                                 $s = str_replace($mtch[0], '[youtube]' . $mtch[1] . '[/youtube]', $s);
273                         } elseif (stristr($mtch[1], 'vimeo')) {
274                                 $s = str_replace($mtch[0], '[vimeo]' . $mtch[1] . '[/vimeo]', $s);
275                         }
276                 }
277         }
278         return $s;
279 }
280
281 function undo_post_tagging($s) {
282         $matches = null;
283         $cnt = preg_match_all('/([!#@])\[url=(.*?)\](.*?)\[\/url\]/ism', $s, $matches, PREG_SET_ORDER);
284         if ($cnt) {
285                 foreach ($matches as $mtch) {
286                         if (in_array($mtch[1], ['!', '@'])) {
287                                 $contact = Contact::getDetailsByURL($mtch[2]);
288                                 $mtch[3] = empty($contact['addr']) ? $mtch[2] : $contact['addr'];
289                         }
290                         $s = str_replace($mtch[0], $mtch[1] . $mtch[3],$s);
291                 }
292         }
293         return $s;
294 }
295
296 /// @TODO Rewrite this
297 function is_a_date_arg($s) {
298         $i = intval($s);
299
300         if ($i > 1900) {
301                 $y = date('Y');
302
303                 if ($i <= $y + 1 && strpos($s, '-') == 4) {
304                         $m = intval(substr($s, 5));
305
306                         if ($m > 0 && $m <= 12) {
307                                 return true;
308                         }
309                 }
310         }
311
312         return false;
313 }