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