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