]> git.mxchange.org Git - friendica.git/blob - mod/tagrm.php
Merge pull request #12025 from annando/no-boot-src-module
[friendica.git] / mod / tagrm.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 use Friendica\App;
23 use Friendica\Content\Text\BBCode;
24 use Friendica\Core\Session;
25 use Friendica\Database\DBA;
26 use Friendica\DI;
27 use Friendica\Model\Post;
28 use Friendica\Model\Tag;
29
30 function tagrm_post(App $a)
31 {
32         if (!Session::getLocalUser()) {
33                 DI::baseUrl()->redirect($_SESSION['photo_return']);
34         }
35
36         if (!empty($_POST['submit']) && ($_POST['submit'] === DI::l10n()->t('Cancel'))) {
37                 DI::baseUrl()->redirect($_SESSION['photo_return']);
38         }
39
40         $tags = [];
41         foreach ($_POST['tag'] ?? [] as $tag) {
42                 $tags[] = hex2bin(trim($tag));
43         }
44
45         $item_id = $_POST['item'] ?? 0;
46         update_tags($item_id, $tags);
47
48         DI::baseUrl()->redirect($_SESSION['photo_return']);
49         // NOTREACHED
50 }
51
52 /**
53  * Updates tags from an item
54  *
55  * @param $item_id
56  * @param $tags array
57  * @throws Exception
58  */
59 function update_tags($item_id, $tags)
60 {
61         if (empty($item_id) || empty($tags)) {
62                 return;
63         }
64
65         $item = Post::selectFirst(['uri-id'], ['id' => $item_id, 'uid' => Session::getLocalUser()]);
66         if (!DBA::isResult($item)) {
67                 return;
68         }
69
70         foreach ($tags as $new_tag) {
71                 if (preg_match_all('/([#@!])\[url\=([^\[\]]*)\]([^\[\]]*)\[\/url\]/ism', $new_tag, $results, PREG_SET_ORDER)) {
72                         foreach ($results as $tag) {
73                                 Tag::removeByHash($item['uri-id'], $tag[1], $tag[3], $tag[2]);
74                         }
75                 }
76         }
77 }
78
79 function tagrm_content(App $a)
80 {
81         $o = '';
82
83         $photo_return = $_SESSION['photo_return'] ?? '';
84
85         if (!Session::getLocalUser()) {
86                 DI::baseUrl()->redirect($photo_return);
87                 // NOTREACHED
88         }
89
90         if (DI::args()->getArgc()== 3) {
91                 update_tags(DI::args()->getArgv()[1], [trim(hex2bin(DI::args()->getArgv()[2]))]);
92                 DI::baseUrl()->redirect($photo_return);
93         }
94
95         $item_id = ((DI::args()->getArgc()> 1) ? intval(DI::args()->getArgv()[1]) : 0);
96         if (!$item_id) {
97                 DI::baseUrl()->redirect($photo_return);
98                 // NOTREACHED
99         }
100
101         $item = Post::selectFirst(['uri-id'], ['id' => $item_id, 'uid' => Session::getLocalUser()]);
102         if (!DBA::isResult($item)) {
103                 DI::baseUrl()->redirect($photo_return);
104         }
105
106         $tag_text = Tag::getCSVByURIId($item['uri-id']);
107
108         $arr = explode(',', $tag_text);
109
110         if (empty($arr)) {
111                 DI::baseUrl()->redirect($photo_return);
112         }
113
114         $o .= '<h3>' . DI::l10n()->t('Remove Item Tag') . '</h3>';
115
116         $o .= '<p id="tag-remove-desc">' . DI::l10n()->t('Select a tag to remove: ') . '</p>';
117
118         $o .= '<form id="tagrm" action="tagrm" method="post" >';
119         $o .= '<input type="hidden" name="item" value="' . $item_id . '" />';
120         $o .= '<ul>';
121
122         foreach ($arr as $x) {
123                 $o .= '<li><input type="checkbox" name="tag[]" value="' . bin2hex($x) . '" >' . BBCode::convert($x) . '</input></li>';
124         }
125
126         $o .= '</ul>';
127         $o .= '<input id="tagrm-submit" type="submit" name="submit" value="' . DI::l10n()->t('Remove') .'" />';
128         $o .= '<input id="tagrm-cancel" type="submit" name="submit" value="' . DI::l10n()->t('Cancel') .'" />';
129         $o .= '</form>';
130
131         return $o;
132 }