]> git.mxchange.org Git - friendica.git/blob - include/text.php
289802136c2670beab1306e39dac84cadaa48b92
[friendica.git] / include / text.php
1 <?php
2 /**
3  * @file include/text.php
4  */
5
6 use Friendica\Content\Text\BBCode;
7 use Friendica\Model\FileTag;
8 use Friendica\Model\Group;
9 use Friendica\Util\Strings;
10
11 /**
12  * Wrap ACL elements in angle brackets for storage
13  * @param string $item
14  */
15 function sanitise_acl(&$item) {
16         if (intval($item)) {
17                 $item = '<' . intval(Strings::escapeTags(trim($item))) . '>';
18         } elseif (in_array($item, [Group::FOLLOWERS, Group::MUTUALS])) {
19                 $item = '<' . $item . '>';
20         } else {
21                 unset($item);
22         }
23 }
24
25
26 /**
27  * Convert an ACL array to a storable string
28  *
29  * Normally ACL permissions will be an array.
30  * We'll also allow a comma-separated string.
31  *
32  * @param string|array $p
33  * @return string
34  */
35 function perms2str($p) {
36         $ret = '';
37         if (is_array($p)) {
38                 $tmp = $p;
39         } else {
40                 $tmp = explode(',', $p);
41         }
42
43         if (is_array($tmp)) {
44                 array_walk($tmp, 'sanitise_acl');
45                 $ret = implode('', $tmp);
46         }
47         return $ret;
48 }
49
50 /**
51  * Compare activity uri. Knows about activity namespace.
52  *
53  * @param string $haystack
54  * @param string $needle
55  * @return boolean
56  */
57 function activity_match($haystack,$needle) {
58         return (($haystack === $needle) || ((basename($needle) === $haystack) && strstr($needle, NAMESPACE_ACTIVITY_SCHEMA)));
59 }
60
61 /**
62  * quick and dirty quoted_printable encoding
63  *
64  * @param string $s
65  * @return string
66  */
67 function qp($s) {
68         return str_replace("%", "=", rawurlencode($s));
69 }
70
71 /**
72  * @brief Given a text string, convert from bbcode to html and add smilie icons.
73  *
74  * @param string $text String with bbcode.
75  * @return string Formatted HTML
76  * @throws \Friendica\Network\HTTPException\InternalServerErrorException
77  */
78 function prepare_text($text)
79 {
80         $s = BBCode::convert($text);
81         return trim($s);
82 }
83
84 /**
85  * return array with details for categories and folders for an item
86  *
87  * @param array $item
88  * @return array
89  *
90   * [
91  *      [ // categories array
92  *          {
93  *               'name': 'category name',
94  *               'removeurl': 'url to remove this category',
95  *               'first': 'is the first in this array? true/false',
96  *               'last': 'is the last in this array? true/false',
97  *           } ,
98  *           ....
99  *       ],
100  *       [ //folders array
101  *                      {
102  *               'name': 'folder name',
103  *               'removeurl': 'url to remove this folder',
104  *               'first': 'is the first in this array? true/false',
105  *               'last': 'is the last in this array? true/false',
106  *           } ,
107  *           ....
108  *       ]
109  *  ]
110  */
111 function get_cats_and_terms($item)
112 {
113         $categories = [];
114         $folders = [];
115         $first = true;
116
117         foreach (FileTag::fileToArray($item['file'] ?? '', 'category') as $savedFolderName) {
118                 $categories[] = [
119                         'name' => $savedFolderName,
120                         'url' => "#",
121                         'removeurl' => ((local_user() == $item['uid']) ? 'filerm/' . $item['id'] . '?f=&cat=' . rawurlencode($savedFolderName) : ""),
122                         'first' => $first,
123                         'last' => false
124                 ];
125                 $first = false;
126         }
127
128         if (count($categories)) {
129                 $categories[count($categories) - 1]['last'] = true;
130         }
131
132         if (local_user() == $item['uid']) {
133                 foreach (FileTag::fileToArray($item['file'] ?? '') as $savedFolderName) {
134                         $folders[] = [
135                                 'name' => $savedFolderName,
136                                 'url' => "#",
137                                 'removeurl' => ((local_user() == $item['uid']) ? 'filerm/' . $item['id'] . '?f=&term=' . rawurlencode($savedFolderName) : ""),
138                                 'first' => $first,
139                                 'last' => false
140                         ];
141                         $first = false;
142                 }
143         }
144
145         if (count($folders)) {
146                 $folders[count($folders) - 1]['last'] = true;
147         }
148
149         return [$categories, $folders];
150 }
151
152 /// @TODO Rewrite this
153 function is_a_date_arg($s) {
154         $i = intval($s);
155
156         if ($i > 1900) {
157                 $y = date('Y');
158
159                 if ($i <= $y + 1 && strpos($s, '-') == 4) {
160                         $m = intval(substr($s, 5));
161
162                         if ($m > 0 && $m <= 12) {
163                                 return true;
164                         }
165                 }
166         }
167
168         return false;
169 }