239f3d9fa8f251dc956966e1c4f6cd839e96b215
[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\Content\Text\Markdown;
38 use Friendica\Core\Addon;
39 use Friendica\Core\Cache;
40 use Friendica\Core\L10n;
41 use Friendica\Core\System;
42 use Friendica\Database\dba;
43 use Friendica\Database\DBM;
44 use Friendica\Database\DBStructure;
45 use Friendica\Model\Item;
46 use Friendica\Model\Term;
47 use Friendica\Module\Login;
48 use Friendica\Network\HTTPException;
49 use Friendica\Util\DateTimeFormat;
50 use Psr\Http\Message\ResponseInterface;
51 use Psr\Http\Message\ServerRequestInterface;
52 use Symfony\Component\ExpressionLanguage;
53
54 require_once 'boot.php';
55 require_once 'include/conversation.php';
56 require_once 'include/dba.php';
57 require_once 'include/security.php';
58
59 require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
60
61 function advancedcontentfilter_install()
62 {
63         Addon::registerHook('dbstructure_definition'     , __FILE__, 'advancedcontentfilter_dbstructure_definition');
64         Addon::registerHook('prepare_body_content_filter', __FILE__, 'advancedcontentfilter_prepare_body_content_filter');
65         Addon::registerHook('addon_settings'             , __FILE__, 'advancedcontentfilter_addon_settings');
66
67         DBStructure::update(false, true);
68
69         logger("installed advancedcontentfilter");
70 }
71
72 function advancedcontentfilter_uninstall()
73 {
74         Addon::unregisterHook('dbstructure_definition'     , __FILE__, 'advancedcontentfilter_dbstructure_definition');
75         Addon::unregisterHook('prepare_body_content_filter', __FILE__, 'advancedcontentfilter_prepare_body_content_filter');
76         Addon::unregisterHook('addon_settings'             , __FILE__, 'advancedcontentfilter_addon_settings');
77 }
78
79 /*
80  * Hooks
81  */
82
83 function advancedcontentfilter_dbstructure_definition(App $a, &$database)
84 {
85         $database["advancedcontentfilter_rules"] = [
86                 "comment" => "Advancedcontentfilter addon rules",
87                 "fields" => [
88                         "id"         => ["type" => "int unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1", "comment" => "Auto incremented rule id"],
89                         "uid"        => ["type" => "int unsigned", "not null" => "1", "comment" => "Owner user id"],
90                         "name"       => ["type" => "varchar(255)", "not null" => "1", "comment" => "Rule name"],
91                         "expression" => ["type" => "mediumtext"  , "not null" => "1", "comment" => "Expression text"],
92                         "serialized" => ["type" => "mediumtext"  , "not null" => "1", "comment" => "Serialized parsed expression"],
93                         "active"     => ["type" => "boolean"     , "not null" => "1", "default" => "1", "comment" => "Whether the rule is active or not"],
94                         "created"    => ["type" => "datetime"    , "not null" => "1", "default" => NULL_DATE, "comment" => "Creation date"],
95                 ],
96                 "indexes" => [
97                         "PRIMARY" => ["id"],
98                         "uid_active" => ["uid", "active"],
99                 ]
100         ];
101 }
102
103 function advancedcontentfilter_prepare_body_content_filter(App $a, &$hook_data)
104 {
105         static $expressionLanguage;
106
107         if (is_null($expressionLanguage)) {
108                 $expressionLanguage = new ExpressionLanguage\ExpressionLanguage();
109         }
110
111         if (!local_user()) {
112                 return;
113         }
114
115         $vars = [];
116         foreach ($hook_data['item'] as $key => $value) {
117                 $vars[str_replace('-', '_', $key)] = $value;
118         }
119
120         $rules = Cache::get('rules_' . local_user());
121         if (!isset($rules)) {
122                 $rules = dba::inArray(dba::select(
123                         'advancedcontentfilter_rules',
124                         ['name', 'expression', 'serialized'],
125                         ['uid' => local_user(), 'active' => true]
126                 ));
127         }
128
129         if ($rules) {
130                 foreach($rules as $rule) {
131                         try {
132                                 $serializedParsedExpression = new ExpressionLanguage\SerializedParsedExpression(
133                                         $rule['expression'],
134                                         $rule['serialized']
135                                 );
136
137                                 $found = (bool) $expressionLanguage->evaluate($serializedParsedExpression, $vars);
138                         } catch (Exception $e) {
139                                 $found = false;
140                         }
141
142                         if ($found) {
143                                 $hook_data['filter_reasons'][] = L10n::t('Filtered by rule: %s', $rule['name']);
144                                 break;
145                         }
146                 }
147         }
148 }
149
150
151 function advancedcontentfilter_addon_settings(App $a, &$s)
152 {
153         if (!local_user()) {
154                 return;
155         }
156
157         $advancedcontentfilter = L10n::t('Advanced Content Filter');
158
159         $s .= <<<HTML
160                 <span class="settings-block fakelink" style="display: block;"><h3><a href="advancedcontentfilter">$advancedcontentfilter <i class="glyphicon glyphicon-share"></i></a></h3></span>
161 HTML;
162
163         return;
164 }
165
166 /*
167  * Module
168  */
169
170 function advancedcontentfilter_module() {}
171
172 function advancedcontentfilter_init(App $a)
173 {
174         if ($a->argv[1] == 'api') {
175                 $slim = new \Slim\App();
176
177                 require __DIR__ . '/src/middlewares.php';
178
179                 require __DIR__ . '/src/routes.php';
180                 $slim->run();
181
182                 exit;
183         }
184 }
185
186 function advancedcontentfilter_content(App $a)
187 {
188         if (!local_user()) {
189                 return Login::form('/' . implode('/', $a->argv));
190         }
191
192         if ($a->argc > 0 && $a->argv[1] == 'help') {
193                 $lang = $a->user['language'];
194
195                 $default_dir = 'addon/advancedcontentfilter/doc/';
196                 $help_file = 'advancedcontentfilter.md';
197                 $help_path = $default_dir . $help_file;
198                 if (file_exists($default_dir . $lang . '/' . $help_file)) {
199                         $help_path = $default_dir . $lang . '/' . $help_file;
200                 }
201
202                 $content = file_get_contents($help_path);
203
204                 $html = Markdown::convert($content, false);
205
206                 $html = str_replace('code>', 'key>', $html);
207
208                 return $html;
209         } else {
210                 $t = get_markup_template('settings.tpl', 'addon/advancedcontentfilter/');
211                 return replace_macros($t, [
212                         '$current_theme' => $a->getCurrentTheme(),
213                         '$backtosettings' => L10n::t('Back to Addon Settings'),
214                         '$title' => L10n::t('Advanced Content Filter'),
215                         '$add_a_rule' => L10n::t('Add a Rule'),
216                         '$help' => L10n::t('Help'),
217                         '$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>.'),
218                         '$your_rules' => L10n::t('Your rules'),
219                         '$no_rules' => L10n::t('You have no rules yet! Start adding one by clicking on the button above next to the title.'),
220                         '$disabled' => L10n::t('Disabled'),
221                         '$enabled' => L10n::t('Enabled'),
222                         '$disable_this_rule' => L10n::t('Disable this rule'),
223                         '$enable_this_rule' => L10n::t('Enable this rule'),
224                         '$edit_this_rule' => L10n::t('Edit this rule'),
225                         '$edit_the_rule' => L10n::t('Edit the rule'),
226                         '$save_this_rule' => L10n::t('Save this rule'),
227                         '$delete_this_rule' => L10n::t('Delete this rule'),
228                         '$rule' => L10n::t('Rule'),
229                         '$close' => L10n::t('Close'),
230                         '$addtitle' => L10n::t('Add new rule'),
231                         '$rule_name' => L10n::t('Rule Name'),
232                         '$rule_expression' => L10n::t('Rule Expression'),
233                         '$examples' => L10n::t('<p>Examples:</p><ul><li><pre>author_link == \'https://friendica.mrpetovan.com/profile/hypolite\'</pre></li><li>tags</li></ul>'),
234                         '$cancel' => L10n::t('Cancel'),
235                         '$rules' => advancedcontentfilter_get_rules(),
236                         '$baseurl' => System::baseUrl(true),
237                         '$form_security_token' => get_form_security_token()
238                 ]);
239         }
240 }
241
242 /*
243  * Common functions
244  */
245 function advancedcontentfilter_build_fields($data)
246 {
247         $fields = [];
248
249         if (!empty($data['name'])) {
250                 $fields['name'] = $data['name'];
251         }
252
253         if (!empty($data['expression'])) {
254                 $allowed_keys = [
255                         'author_id', 'author_link', 'author_name', 'author_avatar',
256                         'owner_id', 'owner_link', 'owner_name', 'owner_avatar',
257                         'contact_id', 'uid', 'id', 'parent', 'uri',
258                         'thr_parent', 'parent_uri',
259                         'content_warning',
260                         'commented', 'created', 'edited', 'received',
261                         'verb', 'object_type', 'postopts', 'plink', 'guid', 'wall', 'private', 'starred',
262                         'title', 'body',
263                         'file', 'event_id', 'location', 'coord', 'app', 'attach',
264                         'rendered_hash', 'rendered_html', 'object',
265                         'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid',
266                         'item_id', 'item_network', 'author_thumb', 'owner_thumb',
267                         'network', 'url', 'name', 'writable', 'self',
268                         'cid', 'alias',
269                         'event_created', 'event_edited', 'event_start', 'event_finish', 'event_summary',
270                         'event_desc', 'event_location', 'event_type', 'event_nofinish', 'event_adjust', 'event_ignore',
271                         'children', 'pagedrop', 'tags', 'hashtags', 'mentions',
272                 ];
273
274                 $expressionLanguage = new ExpressionLanguage\ExpressionLanguage();
275
276                 $parsedExpression = $expressionLanguage->parse($data['expression'], $allowed_keys);
277
278                 $serialized = serialize($parsedExpression->getNodes());
279
280                 $fields['expression'] = $data['expression'];
281                 $fields['serialized'] = $serialized;
282         }
283
284         if (isset($data['active'])) {
285                 $fields['active'] = intval($data['active']);
286         } else {
287                 $fields['active'] = 1;
288         }
289
290         return $fields;
291 }
292
293 /*
294  * API
295  */
296
297 function advancedcontentfilter_get_rules()
298 {
299         if (!local_user()) {
300                 throw new HTTPException\UnauthorizedException(L10n::t('You must be logged in to use this method'));
301         }
302
303         $rules = dba::inArray(dba::select('advancedcontentfilter_rules', [], ['uid' => local_user()]));
304
305         return json_encode($rules);
306 }
307
308 function advancedcontentfilter_get_rules_id(ServerRequestInterface $request, ResponseInterface $response, $args)
309 {
310         if (!local_user()) {
311                 throw new HTTPException\UnauthorizedException(L10n::t('You must be logged in to use this method'));
312         }
313
314         $rule = dba::selectFirst('advancedcontentfilter_rules', [], ['id' => $args['id'], 'uid' => local_user()]);
315
316         return json_encode($rule);
317 }
318
319 function advancedcontentfilter_post_rules(ServerRequestInterface $request)
320 {
321         if (!local_user()) {
322                 throw new HTTPException\UnauthorizedException(L10n::t('You must be logged in to use this method'));
323         }
324
325         if (!check_form_security_token()) {
326                 throw new HTTPException\BadRequestException(L10n::t('Invalid form security token, please refresh the page.'));
327         }
328
329         $data = json_decode($request->getBody(), true);
330
331         try {
332                 $fields = advancedcontentfilter_build_fields($data);
333         } catch (Exception $e) {
334                 throw new HTTPException\BadRequestException($e->getMessage(), 0, $e);
335         }
336
337         if (empty($fields['name']) || empty($fields['expression'])) {
338                 throw new HTTPException\BadRequestException(L10n::t('The rule name and expression are required.'));
339         }
340
341         $fields['uid'] = local_user();
342         $fields['created'] = DateTimeFormat::utcNow();
343
344         if (!dba::insert('advancedcontentfilter_rules', $fields)) {
345                 throw new HTTPException\ServiceUnavaiableException(dba::errorMessage());
346         }
347
348         $rule = dba::selectFirst('advancedcontentfilter_rules', [], ['id' => dba::lastInsertId()]);
349
350         return json_encode(['message' => L10n::t('Rule successfully added'), 'rule' => $rule]);
351 }
352
353 function advancedcontentfilter_put_rules_id(ServerRequestInterface $request, ResponseInterface $response, $args)
354 {
355         if (!local_user()) {
356                 throw new HTTPException\UnauthorizedException(L10n::t('You must be logged in to use this method'));
357         }
358
359         if (!check_form_security_token()) {
360                 throw new HTTPException\BadRequestException(L10n::t('Invalid form security token, please refresh the page.'));
361         }
362
363         if (!dba::exists('advancedcontentfilter_rules', ['id' => $args['id'], 'uid' => local_user()])) {
364                 throw new HTTPException\NotFoundException(L10n::t('Rule doesn\'t exist or doesn\'t belong to you.'));
365         }
366
367         $data = json_decode($request->getBody(), true);
368
369         try {
370                 $fields = advancedcontentfilter_build_fields($data);
371         } catch (Exception $e) {
372                 throw new HTTPException\BadRequestException($e->getMessage(), 0, $e);
373         }
374
375         if (!dba::update('advancedcontentfilter_rules', $fields, ['id' => $args['id']])) {
376                 throw new HTTPException\ServiceUnavaiableException(dba::errorMessage());
377         }
378
379         return json_encode(['message' => L10n::t('Rule successfully updated')]);
380 }
381
382 function advancedcontentfilter_delete_rules_id(ServerRequestInterface $request, ResponseInterface $response, $args)
383 {
384         if (!local_user()) {
385                 throw new HTTPException\UnauthorizedException(L10n::t('You must be logged in to use this method'));
386         }
387
388         if (!check_form_security_token()) {
389                 throw new HTTPException\BadRequestException(L10n::t('Invalid form security token, please refresh the page.'));
390         }
391
392         if (!dba::exists('advancedcontentfilter_rules', ['id' => $args['id'], 'uid' => local_user()])) {
393                 throw new HTTPException\NotFoundException(L10n::t('Rule doesn\'t exist or doesn\'t belong to you.'));
394         }
395
396         if (!dba::delete('advancedcontentfilter_rules', ['id' => $args['id']])) {
397                 throw new HTTPException\ServiceUnavaiableException(dba::errorMessage());
398         }
399
400         return json_encode(['message' => L10n::t('Rule successfully deleted')]);
401 }
402
403 function advancedcontentfilter_get_variables_guid(ServerRequestInterface $request, ResponseInterface $response, $args)
404 {
405         if (!local_user()) {
406                 throw new HTTPException\UnauthorizedException(L10n::t('You must be logged in to use this method'));
407         }
408
409         if (!isset($args['guid'])) {
410                 throw new HTTPException\BadRequestException(L10n::t('Missing argument: guid.'));
411         }
412
413         $condition = ["`guid` = ? AND (`uid` = ? OR `uid` = 0)", $args['guid'], local_user()];
414         $params = ['order' => ['uid' => true]];
415         $item = Item::selectFirstForUser(local_user(), [], $condition, $params);
416
417         if (!DBM::is_result($item)) {
418                 throw new HTTPException\NotFoundException(L10n::t('Unknown post with guid: %s', $args['guid']));
419         }
420
421         $tags = Term::populateTagsFromItem($item);
422
423         $item['tags'] = $tags['tags'];
424         $item['hashtags'] = $tags['hashtags'];
425         $item['mentions'] = $tags['mentions'];
426
427         $return = [];
428         foreach ($item as $key => $value) {
429                 $return[str_replace('-', '_', $key)] = $value;
430         }
431
432         return json_encode(['variables' => str_replace('\\\'', '\'', var_export($return, true))]);
433 }