]> git.mxchange.org Git - friendica.git/blob - include/text.php
Move perms2str to ACLFormatter::aclToString()
[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
9 /**
10  * Compare activity uri. Knows about activity namespace.
11  *
12  * @param string $haystack
13  * @param string $needle
14  * @return boolean
15  */
16 function activity_match($haystack,$needle) {
17         return (($haystack === $needle) || ((basename($needle) === $haystack) && strstr($needle, NAMESPACE_ACTIVITY_SCHEMA)));
18 }
19
20 /**
21  * quick and dirty quoted_printable encoding
22  *
23  * @param string $s
24  * @return string
25  */
26 function qp($s) {
27         return str_replace("%", "=", rawurlencode($s));
28 }
29
30 /**
31  * @brief Given a text string, convert from bbcode to html and add smilie icons.
32  *
33  * @param string $text String with bbcode.
34  * @return string Formatted HTML
35  * @throws \Friendica\Network\HTTPException\InternalServerErrorException
36  */
37 function prepare_text($text)
38 {
39         $s = BBCode::convert($text);
40         return trim($s);
41 }
42
43 /**
44  * return array with details for categories and folders for an item
45  *
46  * @param array $item
47  * @return array
48  *
49   * [
50  *      [ // categories array
51  *          {
52  *               'name': 'category name',
53  *               'removeurl': 'url to remove this category',
54  *               'first': 'is the first in this array? true/false',
55  *               'last': 'is the last in this array? true/false',
56  *           } ,
57  *           ....
58  *       ],
59  *       [ //folders array
60  *                      {
61  *               'name': 'folder name',
62  *               'removeurl': 'url to remove this folder',
63  *               'first': 'is the first in this array? true/false',
64  *               'last': 'is the last in this array? true/false',
65  *           } ,
66  *           ....
67  *       ]
68  *  ]
69  */
70 function get_cats_and_terms($item)
71 {
72         $categories = [];
73         $folders = [];
74         $first = true;
75
76         foreach (FileTag::fileToArray($item['file'] ?? '', 'category') as $savedFolderName) {
77                 $categories[] = [
78                         'name' => $savedFolderName,
79                         'url' => "#",
80                         'removeurl' => ((local_user() == $item['uid']) ? 'filerm/' . $item['id'] . '?f=&cat=' . rawurlencode($savedFolderName) : ""),
81                         'first' => $first,
82                         'last' => false
83                 ];
84                 $first = false;
85         }
86
87         if (count($categories)) {
88                 $categories[count($categories) - 1]['last'] = true;
89         }
90
91         if (local_user() == $item['uid']) {
92                 foreach (FileTag::fileToArray($item['file'] ?? '') as $savedFolderName) {
93                         $folders[] = [
94                                 'name' => $savedFolderName,
95                                 'url' => "#",
96                                 'removeurl' => ((local_user() == $item['uid']) ? 'filerm/' . $item['id'] . '?f=&term=' . rawurlencode($savedFolderName) : ""),
97                                 'first' => $first,
98                                 'last' => false
99                         ];
100                         $first = false;
101                 }
102         }
103
104         if (count($folders)) {
105                 $folders[count($folders) - 1]['last'] = true;
106         }
107
108         return [$categories, $folders];
109 }
110
111 /// @TODO Rewrite this
112 function is_a_date_arg($s) {
113         $i = intval($s);
114
115         if ($i > 1900) {
116                 $y = date('Y');
117
118                 if ($i <= $y + 1 && strpos($s, '-') == 4) {
119                         $m = intval(substr($s, 5));
120
121                         if ($m > 0 && $m <= 12) {
122                                 return true;
123                         }
124                 }
125         }
126
127         return false;
128 }