[advancedcontentfilter] Stop using advancedcontentfilter_get_rules() outside of route...
[friendica-addons.git/.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\BaseModule;
38 use Friendica\Content\Text\Markdown;
39 use Friendica\Core\Hook;
40 use Friendica\Core\Logger;
41 use Friendica\Core\Renderer;
42 use Friendica\Database\DBA;
43 use Friendica\Database\DBStructure;
44 use Friendica\DI;
45 use Friendica\Model\Item;
46 use Friendica\Model\Post;
47 use Friendica\Model\Tag;
48 use Friendica\Model\User;
49 use Friendica\Module\Security\Login;
50 use Friendica\Network\HTTPException;
51 use Friendica\Util\DateTimeFormat;
52 use Psr\Http\Message\ResponseInterface;
53 use Psr\Http\Message\ServerRequestInterface;
54 use Symfony\Component\ExpressionLanguage;
55
56 require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
57
58 function advancedcontentfilter_install()
59 {
60         Hook::register('dbstructure_definition'     , __FILE__, 'advancedcontentfilter_dbstructure_definition');
61         Hook::register('prepare_body_content_filter', __FILE__, 'advancedcontentfilter_prepare_body_content_filter');
62         Hook::register('addon_settings'             , __FILE__, 'advancedcontentfilter_addon_settings');
63
64         Hook::add('dbstructure_definition'          , __FILE__, 'advancedcontentfilter_dbstructure_definition');
65         DBStructure::performUpdate();
66
67         Logger::notice('installed advancedcontentfilter');
68 }
69
70 /*
71  * Hooks
72  */
73
74 function advancedcontentfilter_dbstructure_definition(&$database)
75 {
76         $database['advancedcontentfilter_rules'] = [
77                 'comment' => 'Advancedcontentfilter addon rules',
78                 'fields' => [
79                         'id'         => ['type' => 'int unsigned', 'not null' => '1', 'extra' => 'auto_increment', 'primary' => '1', 'comment' => 'Auto incremented rule id'],
80                         'uid'        => ['type' => 'int unsigned', 'not null' => '1', 'comment' => 'Owner user id'],
81                         'name'       => ['type' => 'varchar(255)', 'not null' => '1', 'comment' => 'Rule name'],
82                         'expression' => ['type' => 'mediumtext'  , 'not null' => '1', 'comment' => 'Expression text'],
83                         'serialized' => ['type' => 'mediumtext'  , 'not null' => '1', 'comment' => 'Serialized parsed expression'],
84                         'active'     => ['type' => 'boolean'     , 'not null' => '1', 'default' => '1', 'comment' => 'Whether the rule is active or not'],
85                         'created'    => ['type' => 'datetime'    , 'not null' => '1', 'default' => DBA::NULL_DATETIME, 'comment' => 'Creation date'],
86                 ],
87                 'indexes' => [
88                         'PRIMARY' => ['id'],
89                         'uid_active' => ['uid', 'active'],
90                 ]
91         ];
92 }
93
94 /**
95  * @param array $item Prepared by either Model\Item::prepareBody or advancedcontentfilter_prepare_item_row
96  * @return array
97  */
98 function advancedcontentfilter_get_filter_fields(array $item)
99 {
100         $vars = [];
101
102         // Convert the language JSON text into a filterable format
103         if (!empty($item['language']) && ($languages = json_decode($item['language'], true))) {
104                 foreach ($languages as $key => $value) {
105                         $vars['language_' . strtolower($key)] = $value;
106                 }
107         }
108
109         foreach ($item as $key => $value) {
110                 $vars[str_replace('-', '_', $key)] = $value;
111         }
112
113         ksort($vars);
114
115         return $vars;
116 }
117
118 function advancedcontentfilter_prepare_body_content_filter(&$hook_data)
119 {
120         static $expressionLanguage;
121
122         if (is_null($expressionLanguage)) {
123                 $expressionLanguage = new ExpressionLanguage\ExpressionLanguage();
124         }
125
126         if (!DI::userSession()->getLocalUserId()) {
127                 return;
128         }
129
130         $vars = advancedcontentfilter_get_filter_fields($hook_data['item']);
131
132         $rules = DI::cache()->get('rules_' . DI::userSession()->getLocalUserId());
133         if (!isset($rules)) {
134                 $rules = DBA::toArray(DBA::select(
135                         'advancedcontentfilter_rules',
136                         ['name', 'expression', 'serialized'],
137                         ['uid' => DI::userSession()->getLocalUserId(), 'active' => true]
138                 ));
139
140                 DI::cache()->set('rules_' . DI::userSession()->getLocalUserId(), $rules);
141         }
142
143         if ($rules) {
144                 foreach($rules as $rule) {
145                         try {
146                                 $serializedParsedExpression = new ExpressionLanguage\SerializedParsedExpression(
147                                         $rule['expression'],
148                                         $rule['serialized']
149                                 );
150
151                                 // The error suppression operator is used because of potentially broken user-supplied regular expressions
152                                 $found = (bool) @$expressionLanguage->evaluate($serializedParsedExpression, $vars);
153                         } catch (Exception $e) {
154                                 $found = false;
155                         }
156
157                         if ($found) {
158                                 $hook_data['filter_reasons'][] = DI::l10n()->t('Filtered by rule: %s', $rule['name']);
159                                 break;
160                         }
161                 }
162         }
163 }
164
165
166 function advancedcontentfilter_addon_settings(array &$data)
167 {
168         if (!DI::userSession()->getLocalUserId()) {
169                 return;
170         }
171
172         $data = [
173                 'addon' => 'advancedcontentfilter',
174                 'title' => DI::l10n()->t('Advanced Content Filter'),
175                 'href'  => 'advancedcontentfilter',
176         ];
177 }
178
179 /*
180  * Module
181  */
182
183 /**
184  * This is a statement rather than an actual function definition. The simple
185  * existence of this method is checked to figure out if the addon offers a
186  * module.
187  */
188 function advancedcontentfilter_module() {}
189
190 function advancedcontentfilter_init()
191 {
192         if (DI::args()->getArgc() > 1 && DI::args()->getArgv()[1] == 'api') {
193                 $slim = \Slim\Factory\AppFactory::create();
194
195                 require __DIR__ . '/src/middlewares.php';
196
197                 require __DIR__ . '/src/routes.php';
198                 $slim->run();
199
200                 exit;
201         }
202 }
203
204 function advancedcontentfilter_content()
205 {
206         if (!DI::userSession()->getLocalUserId()) {
207                 return Login::form('/' . implode('/', DI::args()->getArgv()));
208         }
209
210         if (DI::args()->getArgc() > 1 && DI::args()->getArgv()[1] == 'help') {
211                 $user = User::getById(DI::userSession()->getLocalUserId());
212
213                 $lang = $user['language'];
214
215                 $default_dir = 'addon/advancedcontentfilter/doc/';
216                 $help_file = 'advancedcontentfilter.md';
217                 $help_path = $default_dir . $help_file;
218                 if (file_exists($default_dir . $lang . '/' . $help_file)) {
219                         $help_path = $default_dir . $lang . '/' . $help_file;
220                 }
221
222                 $content = file_get_contents($help_path);
223
224                 $html = Markdown::convert($content, false);
225
226                 $html = str_replace('code>', 'key>', $html);
227
228                 return $html;
229         } else {
230                 $t = Renderer::getMarkupTemplate('settings.tpl', 'addon/advancedcontentfilter/');
231                 return Renderer::replaceMacros($t, [
232                         '$messages' => [
233                                 'backtosettings'    => DI::l10n()->t('Back to Addon Settings'),
234                                 'title'             => DI::l10n()->t('Advanced Content Filter'),
235                                 'add_a_rule'        => DI::l10n()->t('Add a Rule'),
236                                 'help'              => DI::l10n()->t('Help'),
237                                 'intro'             => DI::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 help page.'),
238                                 'your_rules'        => DI::l10n()->t('Your rules'),
239                                 'no_rules'          => DI::l10n()->t('You have no rules yet! Start adding one by clicking on the button above next to the title.'),
240                                 'disabled'          => DI::l10n()->t('Disabled'),
241                                 'enabled'           => DI::l10n()->t('Enabled'),
242                                 'disable_this_rule' => DI::l10n()->t('Disable this rule'),
243                                 'enable_this_rule'  => DI::l10n()->t('Enable this rule'),
244                                 'edit_this_rule'    => DI::l10n()->t('Edit this rule'),
245                                 'edit_the_rule'     => DI::l10n()->t('Edit the rule'),
246                                 'save_this_rule'    => DI::l10n()->t('Save this rule'),
247                                 'delete_this_rule'  => DI::l10n()->t('Delete this rule'),
248                                 'rule'              => DI::l10n()->t('Rule'),
249                                 'close'             => DI::l10n()->t('Close'),
250                                 'addtitle'          => DI::l10n()->t('Add new rule'),
251                                 'rule_name'         => DI::l10n()->t('Rule Name'),
252                                 'rule_expression'   => DI::l10n()->t('Rule Expression'),
253                                 'cancel'            => DI::l10n()->t('Cancel'),
254                         ],
255                         '$current_theme' => DI::app()->getCurrentTheme(),
256                         '$rules' => DBA::toArray(DBA::select('advancedcontentfilter_rules', [], ['uid' => DI::userSession()->getLocalUserId()])),
257                         '$form_security_token' => BaseModule::getFormSecurityToken()
258                 ]);
259         }
260 }
261
262 /*
263  * Common functions
264  */
265 function advancedcontentfilter_build_fields($data)
266 {
267         $fields = [];
268
269         if (!empty($data['name'])) {
270                 $fields['name'] = $data['name'];
271         }
272
273         if (!empty($data['expression'])) {
274                 // Using a dummy item to validate the field existence
275                 $condition = ["(`uid` = ? OR `uid` = 0)", DI::userSession()->getLocalUserId()];
276                 $params = ['order' => ['uid' => true]];
277                 $item_row = Post::selectFirstForUser(DI::userSession()->getLocalUserId(), [], $condition, $params);
278
279                 if (!DBA::isResult($item_row)) {
280                         throw new HTTPException\NotFoundException(DI::l10n()->t('This addon requires this node having at least one post'));
281                 }
282
283                 $expressionLanguage = new ExpressionLanguage\ExpressionLanguage();
284                 $parsedExpression = $expressionLanguage->parse(
285                         $data['expression'],
286                         array_keys(advancedcontentfilter_get_filter_fields(advancedcontentfilter_prepare_item_row($item_row)))
287                 );
288
289                 $serialized = serialize($parsedExpression->getNodes());
290
291                 $fields['expression'] = $data['expression'];
292                 $fields['serialized'] = $serialized;
293         }
294
295         if (isset($data['active'])) {
296                 $fields['active'] = intval($data['active']);
297         } else {
298                 $fields['active'] = 1;
299         }
300
301         return $fields;
302 }
303
304 /*
305  * API
306  */
307
308 function advancedcontentfilter_get_rules(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
309 {
310         if (!DI::userSession()->getLocalUserId()) {
311                 throw new HTTPException\UnauthorizedException(DI::l10n()->t('You must be logged in to use this method'));
312         }
313
314         $rules = DBA::toArray(DBA::select('advancedcontentfilter_rules', [], ['uid' => DI::userSession()->getLocalUserId()]));
315
316         $response->getBody()->write(json_encode($rules));
317         return $response->withHeader('Content-Type', 'application/json');
318 }
319
320 function advancedcontentfilter_get_rules_id(ServerRequestInterface $request, ResponseInterface $response, $args)
321 {
322         if (!DI::userSession()->getLocalUserId()) {
323                 throw new HTTPException\UnauthorizedException(DI::l10n()->t('You must be logged in to use this method'));
324         }
325
326         $rule = DBA::selectFirst('advancedcontentfilter_rules', [], ['id' => $args['id'], 'uid' => DI::userSession()->getLocalUserId()]);
327
328         $response->getBody()->write(json_encode($rule));
329         return $response->withHeader('Content-Type', 'application/json');
330 }
331
332 function advancedcontentfilter_post_rules(ServerRequestInterface $request, ResponseInterface $response)
333 {
334         if (!DI::userSession()->getLocalUserId()) {
335                 throw new HTTPException\UnauthorizedException(DI::l10n()->t('You must be logged in to use this method'));
336         }
337
338         if (!BaseModule::checkFormSecurityToken()) {
339                 throw new HTTPException\BadRequestException(DI::l10n()->t('Invalid form security token, please refresh the page.'));
340         }
341
342         $data = json_decode($request->getBody(), true);
343
344         try {
345                 $fields = advancedcontentfilter_build_fields($data);
346         } catch (Exception $e) {
347                 throw new HTTPException\BadRequestException($e->getMessage(), $e);
348         }
349
350         if (empty($fields['name']) || empty($fields['expression'])) {
351                 throw new HTTPException\BadRequestException(DI::l10n()->t('The rule name and expression are required.'));
352         }
353
354         $fields['uid'] = DI::userSession()->getLocalUserId();
355         $fields['created'] = DateTimeFormat::utcNow();
356
357         if (!DBA::insert('advancedcontentfilter_rules', $fields)) {
358                 throw new HTTPException\ServiceUnavailableException(DBA::errorMessage());
359         }
360
361         $rule = DBA::selectFirst('advancedcontentfilter_rules', [], ['id' => DBA::lastInsertId()]);
362
363         DI::cache()->delete('rules_' . DI::userSession()->getLocalUserId());
364
365         $response->getBody()->write(json_encode(['message' => DI::l10n()->t('Rule successfully added'), 'rule' => $rule]));
366         return $response->withHeader('Content-Type', 'application/json');
367 }
368
369 function advancedcontentfilter_put_rules_id(ServerRequestInterface $request, ResponseInterface $response, $args)
370 {
371         if (!DI::userSession()->getLocalUserId()) {
372                 throw new HTTPException\UnauthorizedException(DI::l10n()->t('You must be logged in to use this method'));
373         }
374
375         if (!BaseModule::checkFormSecurityToken()) {
376                 throw new HTTPException\BadRequestException(DI::l10n()->t('Invalid form security token, please refresh the page.'));
377         }
378
379         if (!DBA::exists('advancedcontentfilter_rules', ['id' => $args['id'], 'uid' => DI::userSession()->getLocalUserId()])) {
380                 throw new HTTPException\NotFoundException(DI::l10n()->t('Rule doesn\'t exist or doesn\'t belong to you.'));
381         }
382
383         $data = json_decode($request->getBody(), true);
384
385         try {
386                 $fields = advancedcontentfilter_build_fields($data);
387         } catch (Exception $e) {
388                 throw new HTTPException\BadRequestException($e->getMessage(), $e);
389         }
390
391         if (!DBA::update('advancedcontentfilter_rules', $fields, ['id' => $args['id']])) {
392                 throw new HTTPException\ServiceUnavailableException(DBA::errorMessage());
393         }
394
395         DI::cache()->delete('rules_' . DI::userSession()->getLocalUserId());
396
397         $response->getBody()->write(json_encode(['message' => DI::l10n()->t('Rule successfully updated')]));
398         return $response->withHeader('Content-Type', 'application/json');
399 }
400
401 function advancedcontentfilter_delete_rules_id(ServerRequestInterface $request, ResponseInterface $response, $args)
402 {
403         if (!DI::userSession()->getLocalUserId()) {
404                 throw new HTTPException\UnauthorizedException(DI::l10n()->t('You must be logged in to use this method'));
405         }
406
407         if (!BaseModule::checkFormSecurityToken()) {
408                 throw new HTTPException\BadRequestException(DI::l10n()->t('Invalid form security token, please refresh the page.'));
409         }
410
411         if (!DBA::exists('advancedcontentfilter_rules', ['id' => $args['id'], 'uid' => DI::userSession()->getLocalUserId()])) {
412                 throw new HTTPException\NotFoundException(DI::l10n()->t('Rule doesn\'t exist or doesn\'t belong to you.'));
413         }
414
415         if (!DBA::delete('advancedcontentfilter_rules', ['id' => $args['id']])) {
416                 throw new HTTPException\ServiceUnavailableException(DBA::errorMessage());
417         }
418
419         DI::cache()->delete('rules_' . DI::userSession()->getLocalUserId());
420
421         $response->getBody()->write(json_encode(['message' => DI::l10n()->t('Rule successfully deleted')]));
422         return $response->withHeader('Content-Type', 'application/json');
423 }
424
425 function advancedcontentfilter_get_variables_guid(ServerRequestInterface $request, ResponseInterface $response, $args)
426 {
427         if (!DI::userSession()->getLocalUserId()) {
428                 throw new HTTPException\UnauthorizedException(DI::l10n()->t('You must be logged in to use this method'));
429         }
430
431         if (!isset($args['guid'])) {
432                 throw new HTTPException\BadRequestException(DI::l10n()->t('Missing argument: guid.'));
433         }
434
435         $condition = ["`guid` = ? AND (`uid` = ? OR `uid` = 0)", $args['guid'], DI::userSession()->getLocalUserId()];
436         $params = ['order' => ['uid' => true]];
437         $item_row = Post::selectFirstForUser(DI::userSession()->getLocalUserId(), [], $condition, $params);
438
439         if (!DBA::isResult($item_row)) {
440                 throw new HTTPException\NotFoundException(DI::l10n()->t('Unknown post with guid: %s', $args['guid']));
441         }
442
443         $return = advancedcontentfilter_get_filter_fields(advancedcontentfilter_prepare_item_row($item_row));
444
445         $response->getBody()->write(json_encode(['variables' => str_replace('\\\'', '\'', var_export($return, true))]));
446         return $response->withHeader('Content-Type', 'application/json');
447 }
448
449 /**
450  * This mimimcs the processing performed in Model\Item::prepareBody
451  *
452  * @param array $item_row
453  * @return array
454  * @throws HTTPException\InternalServerErrorException
455  * @throws ImagickException
456  */
457 function advancedcontentfilter_prepare_item_row(array $item_row): array
458 {
459         $tags = Tag::populateFromItem($item_row);
460
461         $item_row['tags'] = $tags['tags'];
462         $item_row['hashtags'] = $tags['hashtags'];
463         $item_row['mentions'] = $tags['mentions'];
464         $item_row['attachments'] = DI::postMediaRepository()->splitAttachments($item_row['uri-id']);
465
466         return $item_row;
467 }