]> git.mxchange.org Git - friendica-addons.git/blob - advancedcontentfilter/advancedcontentfilter.php
[advancedcontentfilter] Add addon files
[friendica-addons.git] / advancedcontentfilter / advancedcontentfilter.php
1 <?php
2 /**
3  * Name: Advanced content Filter
4  * Description: Expression-based content filter
5  * Version: 1.0
6  * Author: Hypolite Petovan <https://friendica.mrpetovan.com/profile/hypolite>
7  * Maintainer: Hypolite Petovan <https://friendica.mrpetovan.com/profile/hypolite>
8  *
9  * Copyright (c) 2018 Hypolite Petovan
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  *    * Redistributions of source code must retain the above copyright notice,
15  *     this list of conditions and the following disclaimer.
16  *    * Redistributions in binary form must reproduce the above
17  *    * copyright notice, this list of conditions and the following disclaimer in
18  *      the documentation and/or other materials provided with the distribution.
19  *    * Neither the name of Friendica nor the names of its contributors
20  *      may be used to endorse or promote products derived from this software
21  *      without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL FRIENDICA BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
31  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  */
35
36 use Friendica\App;
37 use Friendica\Core\Addon;
38 use Friendica\Core\L10n;
39 use Friendica\Core\System;
40 use Friendica\Database\DBStructure;
41 use Friendica\Network\HTTPException;
42 use Psr\Http\Message\ResponseInterface;
43 use Psr\Http\Message\ServerRequestInterface;
44 use Symfony\Component\ExpressionLanguage;
45
46 require_once 'boot.php';
47 require_once 'include/conversation.php';
48 require_once 'include/dba.php';
49 require_once 'include/security.php';
50
51 require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
52
53 function advancedcontentfilter_install()
54 {
55         Addon::registerHook('dbstructure_definition'     , __FILE__, 'advancedcontentfilter_dbstructure_definition');
56         Addon::registerHook('prepare_body_content_filter', __FILE__, 'advancedcontentfilter_prepare_body_content_filter');
57         Addon::registerHook('addon_settings'             , __FILE__, 'advancedcontentfilter_addon_settings');
58
59         DBStructure::update(false, true);
60
61         logger("installed advancedcontentfilter");
62 }
63
64 function advancedcontentfilter_uninstall()
65 {
66         Addon::unregisterHook('dbstructure_definition'     , __FILE__, 'advancedcontentfilter_dbstructure_definition');
67         Addon::unregisterHook('prepare_body_content_filter', __FILE__, 'advancedcontentfilter_prepare_body_content_filter');
68         Addon::unregisterHook('addon_settings'             , __FILE__, 'advancedcontentfilter_addon_settings');
69 }
70
71 /*
72  * Hooks
73  */
74
75 function advancedcontentfilter_dbstructure_definition(App $a, &$database)
76 {
77         $database["advancedcontentfilter_rules"] = [
78                 "comment" => "Advancedcontentfilter addon rules",
79                 "fields" => [
80                         "id"         => ["type" => "int unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1", "comment" => "Auto incremented rule id"],
81                         "uid"        => ["type" => "int unsigned", "not null" => "1", "comment" => "Owner user id"],
82                         "name"       => ["type" => "varchar(255)", "not null" => "1", "comment" => "Rule name"],
83                         "expression" => ["type" => "mediumtext"  , "not null" => "1", "comment" => "Expression text"],
84                         "serialized" => ["type" => "mediumtext"  , "not null" => "1", "comment" => "Serialized parsed expression"],
85                         "active"     => ["type" => "boolean"     , "not null" => "1", "default" => "1", "comment" => "Whether the rule is active or not"],
86                         "created"    => ["type" => "datetime"    , "not null" => "1", "default" => NULL_DATE, "comment" => "Creation date"],
87                 ],
88                 "indexes" => [
89                         "PRIMARY" => ["id"],
90                         "uid_active" => ["uid", "active"],
91                 ]
92         ];
93 }
94
95 function advancedcontentfilter_prepare_body_content_filter(App $a, &$hook_data)
96 {
97         static $expressionLanguage;
98
99         if (is_null($expressionLanguage)) {
100                 $expressionLanguage = new ExpressionLanguage\ExpressionLanguage();
101         }
102
103         if (!local_user()) {
104                 return;
105         }
106
107         $vars = [];
108         foreach ($hook_data['item'] as $key => $value) {
109                 $vars[str_replace('-', '_', $key)] = $value;
110         }
111
112         $rules = Friendica\Core\Cache::get('rules_' . local_user());
113         if (!isset($rules)) {
114                 $rules = dba::inArray(dba::select(
115                         'advancedcontentfilter_rules',
116                         ['name', 'expression', 'serialized'],
117                         ['uid' => local_user(), 'active' => true]
118                 ));
119         }
120
121         foreach($rules as $rule) {
122                 try {
123                         $serializedParsedExpression = new ExpressionLanguage\SerializedParsedExpression(
124                                 $rule['expression'],
125                                 $rule['serialized']
126                         );
127
128                         $found = (bool) $expressionLanguage->evaluate($serializedParsedExpression, $vars);
129                 } catch (Exception $e) {
130                         $found = false;
131                 }
132
133                 if ($found) {
134                         $hook_data['filter_reasons'][] = L10n::t('Filtered by rule: %s', $rule['name']);
135                         break;
136                 }
137         }
138 }
139
140
141 function advancedcontentfilter_addon_settings(App $a, &$s)
142 {
143         if (!local_user()) {
144                 return;
145         }
146
147         $advancedcontentfilter = L10n::t('Advanced Content Filter');
148
149         $s .= <<<HTML
150                 <span class="settings-block fakelink" style="display: block;"><h3><a href="advancedcontentfilter">$advancedcontentfilter <i class="glyphicon glyphicon-share"></i></a></h3></span>
151 HTML;
152
153         return;
154 }
155
156 /*
157  * Module
158  */
159
160 function advancedcontentfilter_module() {}
161
162 function advancedcontentfilter_init(App $a)
163 {
164         if ($a->argv[1] == 'api') {
165                 $slim = new \Slim\App();
166
167                 require __DIR__ . '/src/middlewares.php';
168
169                 require __DIR__ . '/src/routes.php';
170                 $slim->run();
171
172                 exit;
173         }
174 }
175
176 function advancedcontentfilter_content(App $a)
177 {
178         if (!local_user()) {
179                 return \Friendica\Module\Login::form('/' . implode('/', $a->argv));
180         }
181
182         if ($a->argc > 0 && $a->argv[1] == 'help') {
183                 $lang = $a->user['language'];
184
185                 $default_dir = 'addon/advancedcontentfilter/doc/';
186                 $help_file = 'advancedcontentfilter.md';
187                 $help_path = $default_dir . $help_file;
188                 if (file_exists($default_dir . $lang . '/' . $help_file)) {
189                         $help_path = $default_dir . $lang . '/' . $help_file;
190                 }
191
192                 $content = file_get_contents($help_path);
193
194                 $html = \Friendica\Content\Text\Markdown::convert($content, false);
195
196                 $html = str_replace('code>', 'key>', $html);
197
198                 return $html;
199         } else {
200                 $t = get_markup_template('settings.tpl', 'addon/advancedcontentfilter/');
201                 return replace_macros($t, [
202                         '$backtosettings' => L10n::t('Back to Addon Settings'),
203                         '$title' => L10n::t('Advanced Content Filter'),
204                         '$add_a_rule' => L10n::t('Add a Rule'),
205                         '$help' => L10n::t('Help'),
206                         '$advanced_content_filter_intro' => L10n::t('Add and manage your personal content filter rules in this screen. Rules have a name and an arbitrary expression that will be matched against post data. For a complete reference of the available operations and variables, check the <a href="advancedcontentfilter/help">help page</a>.'),
207                         '$your_rules' => L10n::t('Your rules'),
208                         '$no_rules' => L10n::t('You have no rules yet! Start adding one by clicking on the button above next to the title.'),
209                         '$disabled' => L10n::t('Disabled'),
210                         '$enabled' => L10n::t('Enabled'),
211                         '$disable_this_rule' => L10n::t('Disable this rule'),
212                         '$enable_this_rule' => L10n::t('Enable this rule'),
213                         '$edit_this_rule' => L10n::t('Edit this rule'),
214                         '$edit_the_rule' => L10n::t('Edit the rule'),
215                         '$save_this_rule' => L10n::t('Save this rule'),
216                         '$delete_this_rule' => L10n::t('Delete this rule'),
217                         '$rule' => L10n::t('Rule'),
218                         '$close' => L10n::t('Close'),
219                         '$addtitle' => L10n::t('Add new rule'),
220                         '$rule_name' => L10n::t('Rule Name'),
221                         '$rule_expression' => L10n::t('Rule Expression'),
222                         '$examples' => L10n::t('<p>Examples:</p><ul><li><pre>author_link == \'https://friendica.mrpetovan.com/profile/hypolite\'</pre></li><li>tags</li></ul>'),
223                         '$cancel' => L10n::t('Cancel'),
224                         '$rules' => advancedcontentfilter_get_rules(),
225                         '$baseurl' => System::baseUrl(true),
226                         '$form_security_token' => get_form_security_token()
227                 ]);
228         }
229 }
230
231 /*
232  * Common functions
233  */
234 function advancedcontentfilter_build_fields($data)
235 {
236         $fields = [];
237
238         if (!empty($data['name'])) {
239                 $fields['name'] = $data['name'];
240         }
241
242         if (!empty($data['expression'])) {
243                 $allowed_keys = [
244                         'author_id', 'author_link', 'author_name', 'author_avatar',
245                         'owner_id', 'owner_link', 'owner_name', 'owner_avatar',
246                         'contact_id', 'uid', 'id', 'parent', 'uri',
247                         'thr_parent', 'parent_uri',
248                         'content_warning',
249                         'commented', 'created', 'edited', 'received',
250                         'verb', 'object_type', 'postopts', 'plink', 'guid', 'wall', 'private', 'starred',
251                         'title', 'body',
252                         'file', 'event_id', 'location', 'coord', 'app', 'attach',
253                         'rendered_hash', 'rendered_html', 'object',
254                         'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid',
255                         'item_id', 'item_network', 'author_thumb', 'owner_thumb',
256                         'network', 'url', 'name', 'writable', 'self',
257                         'cid', 'alias',
258                         'event_created', 'event_edited', 'event_start', 'event_finish', 'event_summary',
259                         'event_desc', 'event_location', 'event_type', 'event_nofinish', 'event_adjust', 'event_ignore',
260                         'children', 'pagedrop', 'tags', 'hashtags', 'mentions',
261                 ];
262
263                 $expressionLanguage = new ExpressionLanguage\ExpressionLanguage();
264
265                 $parsedExpression = $expressionLanguage->parse($data['expression'], $allowed_keys);
266
267                 $serialized = serialize($parsedExpression->getNodes());
268
269                 $fields['expression'] = $data['expression'];
270                 $fields['serialized'] = $serialized;
271         }
272
273         if (isset($data['active'])) {
274                 $fields['active'] = intval($data['active']);
275         } else {
276                 $fields['active'] = 1;
277         }
278
279         return $fields;
280 }
281
282 /*
283  * API
284  */
285
286 function advancedcontentfilter_get_rules()
287 {
288         if (!local_user()) {
289                 throw new HTTPException\UnauthorizedException(L10n::t('You must be logged in to use this method'));
290         }
291
292         $rules = dba::inArray(dba::select('advancedcontentfilter_rules', [], ['uid' => local_user()]));
293
294         return json_encode($rules);
295 }
296
297 function advancedcontentfilter_get_rules_id(ServerRequestInterface $request, ResponseInterface $response, $args)
298 {
299         if (!local_user()) {
300                 throw new HTTPException\UnauthorizedException(L10n::t('You must be logged in to use this method'));
301         }
302
303         $rule = dba::selectFirst('advancedcontentfilter_rules', [], ['id' => $args['id'], 'uid' => local_user()]);
304
305         return json_encode($rule);
306 }
307
308 function advancedcontentfilter_post_rules(ServerRequestInterface $request)
309 {
310         if (!local_user()) {
311                 throw new HTTPException\UnauthorizedException(L10n::t('You must be logged in to use this method'));
312         }
313
314         if (!check_form_security_token()) {
315                 throw new HTTPException\BadRequestException(L10n::t('Invalid form security token, please refresh the page.'));
316         }
317
318         $data = json_decode($request->getBody(), true);
319
320         try {
321                 $fields = advancedcontentfilter_build_fields($data);
322         } catch (Exception $e) {
323                 throw new HTTPException\BadRequestException($e->getMessage(), 0, $e);
324         }
325
326         if (empty($fields['name']) || empty($fields['expression'])) {
327                 throw new HTTPException\BadRequestException(L10n::t('The rule name and expression are required.'));
328         }
329
330         $fields['uid'] = local_user();
331         $fields['created'] = \Friendica\Util\DateTimeFormat::utcNow();
332
333         if (!dba::insert('advancedcontentfilter_rules', $fields)) {
334                 throw new HTTPException\ServiceUnavaiableException(dba::errorMessage());
335         }
336
337         $rule = dba::selectFirst('advancedcontentfilter_rules', [], ['id' => dba::lastInsertId()]);
338
339         return json_encode(['message' => L10n::t('Rule successfully added'), 'rule' => $rule]);
340 }
341
342 function advancedcontentfilter_put_rules_id(ServerRequestInterface $request, ResponseInterface $response, $args)
343 {
344         if (!local_user()) {
345                 throw new HTTPException\UnauthorizedException(L10n::t('You must be logged in to use this method'));
346         }
347
348         if (!check_form_security_token()) {
349                 throw new HTTPException\BadRequestException(L10n::t('Invalid form security token, please refresh the page.'));
350         }
351
352         if (!dba::exists('advancedcontentfilter_rules', ['id' => $args['id'], 'uid' => local_user()])) {
353                 throw new HTTPException\NotFoundException(L10n::t('Rule doesn\'t exist or doesn\'t belong to you.'));
354         }
355
356         $data = json_decode($request->getBody(), true);
357
358         try {
359                 $fields = advancedcontentfilter_build_fields($data);
360         } catch (Exception $e) {
361                 throw new HTTPException\BadRequestException($e->getMessage(), 0, $e);
362         }
363
364         if (!dba::update('advancedcontentfilter_rules', $fields, ['id' => $args['id']])) {
365                 throw new HTTPException\ServiceUnavaiableException(dba::errorMessage());
366         }
367
368         return json_encode(['message' => L10n::t('Rule successfully updated')]);
369 }
370
371 function advancedcontentfilter_delete_rules_id(ServerRequestInterface $request, ResponseInterface $response, $args)
372 {
373         if (!local_user()) {
374                 throw new HTTPException\UnauthorizedException(L10n::t('You must be logged in to use this method'));
375         }
376
377         if (!check_form_security_token()) {
378                 throw new HTTPException\BadRequestException(L10n::t('Invalid form security token, please refresh the page.'));
379         }
380
381         if (!dba::exists('advancedcontentfilter_rules', ['id' => $args['id'], 'uid' => local_user()])) {
382                 throw new HTTPException\NotFoundException(L10n::t('Rule doesn\'t exist or doesn\'t belong to you.'));
383         }
384
385         if (!dba::delete('advancedcontentfilter_rules', ['id' => $args['id']])) {
386                 throw new HTTPException\ServiceUnavaiableException(dba::errorMessage());
387         }
388
389         return json_encode(['message' => L10n::t('Rule successfully deleted')]);
390 }
391
392 function advancedcontentfilter_get_variables_guid(ServerRequestInterface $request, ResponseInterface $response, $args)
393 {
394         if (!local_user()) {
395                 throw new HTTPException\UnauthorizedException(L10n::t('You must be logged in to use this method'));
396         }
397
398         if (!isset($args['guid'])) {
399                 throw new HTTPException\BadRequestException(L10n::t('Missing argument: guid.'));
400         }
401
402         $item = dba::fetch_first(item_query() . " AND `item`.`guid` = ? AND (`item`.`uid` = ? OR `item`.`uid` = 0) ORDER BY `item`.`uid` DESC", $args['guid'], local_user());
403
404         if (!\Friendica\Database\DBM::is_result($item)) {
405                 throw new HTTPException\NotFoundException(L10n::t('Unknown post with guid: %s', $args['guid']));
406         }
407
408         $tags = \Friendica\Model\Term::populateTagsFromItem($item);
409
410         $item['tags'] = $tags['tags'];
411         $item['hashtags'] = $tags['hashtags'];
412         $item['mentions'] = $tags['mentions'];
413
414         $return = [];
415         foreach ($item as $key => $value) {
416                 $return[str_replace('-', '_', $key)] = $value;
417         }
418
419         return str_replace('\\\'', '\'', var_export($return, true));
420 }