3 * Name: Advanced content Filter
4 * Description: Expression-based content filter
6 * Author: Hypolite Petovan <https://friendica.mrpetovan.com/profile/hypolite>
7 * Maintainer: Hypolite Petovan <https://friendica.mrpetovan.com/profile/hypolite>
9 * Copyright (c) 2018 Hypolite Petovan
10 * All rights reserved.
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.
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.
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;
46 require_once 'boot.php';
47 require_once 'include/conversation.php';
48 require_once 'include/dba.php';
49 require_once 'include/security.php';
51 require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
53 function advancedcontentfilter_install()
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');
59 DBStructure::update(false, true);
61 logger("installed advancedcontentfilter");
64 function advancedcontentfilter_uninstall()
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');
75 function advancedcontentfilter_dbstructure_definition(App $a, &$database)
77 $database["advancedcontentfilter_rules"] = [
78 "comment" => "Advancedcontentfilter addon rules",
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"],
90 "uid_active" => ["uid", "active"],
95 function advancedcontentfilter_prepare_body_content_filter(App $a, &$hook_data)
97 static $expressionLanguage;
99 if (is_null($expressionLanguage)) {
100 $expressionLanguage = new ExpressionLanguage\ExpressionLanguage();
108 foreach ($hook_data['item'] as $key => $value) {
109 $vars[str_replace('-', '_', $key)] = $value;
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]
122 foreach($rules as $rule) {
124 $serializedParsedExpression = new ExpressionLanguage\SerializedParsedExpression(
129 $found = (bool) $expressionLanguage->evaluate($serializedParsedExpression, $vars);
130 } catch (Exception $e) {
135 $hook_data['filter_reasons'][] = L10n::t('Filtered by rule: %s', $rule['name']);
143 function advancedcontentfilter_addon_settings(App $a, &$s)
149 $advancedcontentfilter = L10n::t('Advanced Content Filter');
152 <span class="settings-block fakelink" style="display: block;"><h3><a href="advancedcontentfilter">$advancedcontentfilter <i class="glyphicon glyphicon-share"></i></a></h3></span>
162 function advancedcontentfilter_module() {}
164 function advancedcontentfilter_init(App $a)
166 if ($a->argv[1] == 'api') {
167 $slim = new \Slim\App();
169 require __DIR__ . '/src/middlewares.php';
171 require __DIR__ . '/src/routes.php';
178 function advancedcontentfilter_content(App $a)
181 return \Friendica\Module\Login::form('/' . implode('/', $a->argv));
184 if ($a->argc > 0 && $a->argv[1] == 'help') {
185 $lang = $a->user['language'];
187 $default_dir = 'addon/advancedcontentfilter/doc/';
188 $help_file = 'advancedcontentfilter.md';
189 $help_path = $default_dir . $help_file;
190 if (file_exists($default_dir . $lang . '/' . $help_file)) {
191 $help_path = $default_dir . $lang . '/' . $help_file;
194 $content = file_get_contents($help_path);
196 $html = \Friendica\Content\Text\Markdown::convert($content, false);
198 $html = str_replace('code>', 'key>', $html);
202 $t = get_markup_template('settings.tpl', 'addon/advancedcontentfilter/');
203 return replace_macros($t, [
204 '$backtosettings' => L10n::t('Back to Addon Settings'),
205 '$title' => L10n::t('Advanced Content Filter'),
206 '$add_a_rule' => L10n::t('Add a Rule'),
207 '$help' => L10n::t('Help'),
208 '$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>.'),
209 '$your_rules' => L10n::t('Your rules'),
210 '$no_rules' => L10n::t('You have no rules yet! Start adding one by clicking on the button above next to the title.'),
211 '$disabled' => L10n::t('Disabled'),
212 '$enabled' => L10n::t('Enabled'),
213 '$disable_this_rule' => L10n::t('Disable this rule'),
214 '$enable_this_rule' => L10n::t('Enable this rule'),
215 '$edit_this_rule' => L10n::t('Edit this rule'),
216 '$edit_the_rule' => L10n::t('Edit the rule'),
217 '$save_this_rule' => L10n::t('Save this rule'),
218 '$delete_this_rule' => L10n::t('Delete this rule'),
219 '$rule' => L10n::t('Rule'),
220 '$close' => L10n::t('Close'),
221 '$addtitle' => L10n::t('Add new rule'),
222 '$rule_name' => L10n::t('Rule Name'),
223 '$rule_expression' => L10n::t('Rule Expression'),
224 '$examples' => L10n::t('<p>Examples:</p><ul><li><pre>author_link == \'https://friendica.mrpetovan.com/profile/hypolite\'</pre></li><li>tags</li></ul>'),
225 '$cancel' => L10n::t('Cancel'),
226 '$rules' => advancedcontentfilter_get_rules(),
227 '$baseurl' => System::baseUrl(true),
228 '$form_security_token' => get_form_security_token()
236 function advancedcontentfilter_build_fields($data)
240 if (!empty($data['name'])) {
241 $fields['name'] = $data['name'];
244 if (!empty($data['expression'])) {
246 'author_id', 'author_link', 'author_name', 'author_avatar',
247 'owner_id', 'owner_link', 'owner_name', 'owner_avatar',
248 'contact_id', 'uid', 'id', 'parent', 'uri',
249 'thr_parent', 'parent_uri',
251 'commented', 'created', 'edited', 'received',
252 'verb', 'object_type', 'postopts', 'plink', 'guid', 'wall', 'private', 'starred',
254 'file', 'event_id', 'location', 'coord', 'app', 'attach',
255 'rendered_hash', 'rendered_html', 'object',
256 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid',
257 'item_id', 'item_network', 'author_thumb', 'owner_thumb',
258 'network', 'url', 'name', 'writable', 'self',
260 'event_created', 'event_edited', 'event_start', 'event_finish', 'event_summary',
261 'event_desc', 'event_location', 'event_type', 'event_nofinish', 'event_adjust', 'event_ignore',
262 'children', 'pagedrop', 'tags', 'hashtags', 'mentions',
265 $expressionLanguage = new ExpressionLanguage\ExpressionLanguage();
267 $parsedExpression = $expressionLanguage->parse($data['expression'], $allowed_keys);
269 $serialized = serialize($parsedExpression->getNodes());
271 $fields['expression'] = $data['expression'];
272 $fields['serialized'] = $serialized;
275 if (isset($data['active'])) {
276 $fields['active'] = intval($data['active']);
278 $fields['active'] = 1;
288 function advancedcontentfilter_get_rules()
291 throw new HTTPException\UnauthorizedException(L10n::t('You must be logged in to use this method'));
294 $rules = dba::inArray(dba::select('advancedcontentfilter_rules', [], ['uid' => local_user()]));
296 return json_encode($rules);
299 function advancedcontentfilter_get_rules_id(ServerRequestInterface $request, ResponseInterface $response, $args)
302 throw new HTTPException\UnauthorizedException(L10n::t('You must be logged in to use this method'));
305 $rule = dba::selectFirst('advancedcontentfilter_rules', [], ['id' => $args['id'], 'uid' => local_user()]);
307 return json_encode($rule);
310 function advancedcontentfilter_post_rules(ServerRequestInterface $request)
313 throw new HTTPException\UnauthorizedException(L10n::t('You must be logged in to use this method'));
316 if (!check_form_security_token()) {
317 throw new HTTPException\BadRequestException(L10n::t('Invalid form security token, please refresh the page.'));
320 $data = json_decode($request->getBody(), true);
323 $fields = advancedcontentfilter_build_fields($data);
324 } catch (Exception $e) {
325 throw new HTTPException\BadRequestException($e->getMessage(), 0, $e);
328 if (empty($fields['name']) || empty($fields['expression'])) {
329 throw new HTTPException\BadRequestException(L10n::t('The rule name and expression are required.'));
332 $fields['uid'] = local_user();
333 $fields['created'] = \Friendica\Util\DateTimeFormat::utcNow();
335 if (!dba::insert('advancedcontentfilter_rules', $fields)) {
336 throw new HTTPException\ServiceUnavaiableException(dba::errorMessage());
339 $rule = dba::selectFirst('advancedcontentfilter_rules', [], ['id' => dba::lastInsertId()]);
341 return json_encode(['message' => L10n::t('Rule successfully added'), 'rule' => $rule]);
344 function advancedcontentfilter_put_rules_id(ServerRequestInterface $request, ResponseInterface $response, $args)
347 throw new HTTPException\UnauthorizedException(L10n::t('You must be logged in to use this method'));
350 if (!check_form_security_token()) {
351 throw new HTTPException\BadRequestException(L10n::t('Invalid form security token, please refresh the page.'));
354 if (!dba::exists('advancedcontentfilter_rules', ['id' => $args['id'], 'uid' => local_user()])) {
355 throw new HTTPException\NotFoundException(L10n::t('Rule doesn\'t exist or doesn\'t belong to you.'));
358 $data = json_decode($request->getBody(), true);
361 $fields = advancedcontentfilter_build_fields($data);
362 } catch (Exception $e) {
363 throw new HTTPException\BadRequestException($e->getMessage(), 0, $e);
366 if (!dba::update('advancedcontentfilter_rules', $fields, ['id' => $args['id']])) {
367 throw new HTTPException\ServiceUnavaiableException(dba::errorMessage());
370 return json_encode(['message' => L10n::t('Rule successfully updated')]);
373 function advancedcontentfilter_delete_rules_id(ServerRequestInterface $request, ResponseInterface $response, $args)
376 throw new HTTPException\UnauthorizedException(L10n::t('You must be logged in to use this method'));
379 if (!check_form_security_token()) {
380 throw new HTTPException\BadRequestException(L10n::t('Invalid form security token, please refresh the page.'));
383 if (!dba::exists('advancedcontentfilter_rules', ['id' => $args['id'], 'uid' => local_user()])) {
384 throw new HTTPException\NotFoundException(L10n::t('Rule doesn\'t exist or doesn\'t belong to you.'));
387 if (!dba::delete('advancedcontentfilter_rules', ['id' => $args['id']])) {
388 throw new HTTPException\ServiceUnavaiableException(dba::errorMessage());
391 return json_encode(['message' => L10n::t('Rule successfully deleted')]);
394 function advancedcontentfilter_get_variables_guid(ServerRequestInterface $request, ResponseInterface $response, $args)
397 throw new HTTPException\UnauthorizedException(L10n::t('You must be logged in to use this method'));
400 if (!isset($args['guid'])) {
401 throw new HTTPException\BadRequestException(L10n::t('Missing argument: guid.'));
404 $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());
406 if (!\Friendica\Database\DBM::is_result($item)) {
407 throw new HTTPException\NotFoundException(L10n::t('Unknown post with guid: %s', $args['guid']));
410 $tags = \Friendica\Model\Term::populateTagsFromItem($item);
412 $item['tags'] = $tags['tags'];
413 $item['hashtags'] = $tags['hashtags'];
414 $item['mentions'] = $tags['mentions'];
417 foreach ($item as $key => $value) {
418 $return[str_replace('-', '_', $key)] = $value;
421 return json_encode(['variables' => str_replace('\\\'', '\'', var_export($return, true))]);