2018-04-16 22:21:51 -04:00
< ? php
/**
* Name : Advanced content Filter
* Description : Expression - based content filter
* Version : 1.0
* Author : Hypolite Petovan < https :// friendica . mrpetovan . com / profile / hypolite >
* Maintainer : Hypolite Petovan < https :// friendica . mrpetovan . com / profile / hypolite >
*
* Copyright ( c ) 2018 Hypolite Petovan
* All rights reserved .
*
* Redistribution and use in source and binary forms , with or without
* modification , are permitted provided that the following conditions are met :
* * Redistributions of source code must retain the above copyright notice ,
* this list of conditions and the following disclaimer .
* * Redistributions in binary form must reproduce the above
* * copyright notice , this list of conditions and the following disclaimer in
* the documentation and / or other materials provided with the distribution .
* * Neither the name of Friendica nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission .
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS " AS IS " AND
* ANY EXPRESS OR IMPLIED WARRANTIES , INCLUDING , BUT NOT LIMITED TO , THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED . IN NO EVENT SHALL FRIENDICA BE LIABLE FOR ANY DIRECT ,
* INDIRECT , INCIDENTAL , SPECIAL , EXEMPLARY , OR CONSEQUENTIAL DAMAGES ( INCLUDING ,
* BUT NOT LIMITED TO , PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ; LOSS OF USE ,
* DATA , OR PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY , WHETHER IN CONTRACT , STRICT LIABILITY , OR TORT ( INCLUDING NEGLIGENCE
* OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE , EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE .
*
*/
use Friendica\App ;
2018-10-17 15:34:15 -04:00
use Friendica\BaseModule ;
2018-07-19 22:18:02 -04:00
use Friendica\Content\Text\Markdown ;
use Friendica\Core\Cache ;
2018-12-26 02:28:16 -05:00
use Friendica\Core\Hook ;
2018-04-16 22:21:51 -04:00
use Friendica\Core\L10n ;
2018-10-29 19:40:18 -04:00
use Friendica\Core\Logger ;
2018-10-31 10:55:15 -04:00
use Friendica\Core\Renderer ;
2018-07-20 08:20:48 -04:00
use Friendica\Database\DBA ;
2018-04-16 22:21:51 -04:00
use Friendica\Database\DBStructure ;
2018-07-19 22:18:02 -04:00
use Friendica\Model\Item ;
use Friendica\Model\Term ;
use Friendica\Module\Login ;
2018-04-16 22:21:51 -04:00
use Friendica\Network\HTTPException ;
2018-07-19 22:18:02 -04:00
use Friendica\Util\DateTimeFormat ;
2018-04-16 22:21:51 -04:00
use Psr\Http\Message\ResponseInterface ;
use Psr\Http\Message\ServerRequestInterface ;
use Symfony\Component\ExpressionLanguage ;
require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php' ;
function advancedcontentfilter_install ()
{
2018-12-26 02:28:16 -05:00
Hook :: register ( 'dbstructure_definition' , __FILE__ , 'advancedcontentfilter_dbstructure_definition' );
Hook :: register ( 'prepare_body_content_filter' , __FILE__ , 'advancedcontentfilter_prepare_body_content_filter' );
Hook :: register ( 'addon_settings' , __FILE__ , 'advancedcontentfilter_addon_settings' );
2018-04-16 22:21:51 -04:00
DBStructure :: update ( false , true );
2018-10-29 19:40:18 -04:00
Logger :: log ( " installed advancedcontentfilter " );
2018-04-16 22:21:51 -04:00
}
function advancedcontentfilter_uninstall ()
{
2018-12-26 02:28:16 -05:00
Hook :: unregister ( 'dbstructure_definition' , __FILE__ , 'advancedcontentfilter_dbstructure_definition' );
Hook :: unregister ( 'prepare_body_content_filter' , __FILE__ , 'advancedcontentfilter_prepare_body_content_filter' );
Hook :: unregister ( 'addon_settings' , __FILE__ , 'advancedcontentfilter_addon_settings' );
2018-04-16 22:21:51 -04:00
}
/*
* Hooks
*/
function advancedcontentfilter_dbstructure_definition ( App $a , & $database )
{
$database [ " advancedcontentfilter_rules " ] = [
" comment " => " Advancedcontentfilter addon rules " ,
" fields " => [
" id " => [ " type " => " int unsigned " , " not null " => " 1 " , " extra " => " auto_increment " , " primary " => " 1 " , " comment " => " Auto incremented rule id " ],
" uid " => [ " type " => " int unsigned " , " not null " => " 1 " , " comment " => " Owner user id " ],
" name " => [ " type " => " varchar(255) " , " not null " => " 1 " , " comment " => " Rule name " ],
" expression " => [ " type " => " mediumtext " , " not null " => " 1 " , " comment " => " Expression text " ],
" serialized " => [ " type " => " mediumtext " , " not null " => " 1 " , " comment " => " Serialized parsed expression " ],
" active " => [ " type " => " boolean " , " not null " => " 1 " , " default " => " 1 " , " comment " => " Whether the rule is active or not " ],
2018-10-21 02:53:07 -04:00
" created " => [ " type " => " datetime " , " not null " => " 1 " , " default " => DBA :: NULL_DATETIME , " comment " => " Creation date " ],
2018-04-16 22:21:51 -04:00
],
" indexes " => [
" PRIMARY " => [ " id " ],
" uid_active " => [ " uid " , " active " ],
]
];
}
function advancedcontentfilter_prepare_body_content_filter ( App $a , & $hook_data )
{
static $expressionLanguage ;
if ( is_null ( $expressionLanguage )) {
$expressionLanguage = new ExpressionLanguage\ExpressionLanguage ();
}
if ( ! local_user ()) {
return ;
}
$vars = [];
foreach ( $hook_data [ 'item' ] as $key => $value ) {
$vars [ str_replace ( '-' , '_' , $key )] = $value ;
}
2018-07-19 22:18:02 -04:00
$rules = Cache :: get ( 'rules_' . local_user ());
2018-04-16 22:21:51 -04:00
if ( ! isset ( $rules )) {
2018-07-20 22:16:16 -04:00
$rules = DBA :: toArray ( DBA :: select (
2018-04-16 22:21:51 -04:00
'advancedcontentfilter_rules' ,
[ 'name' , 'expression' , 'serialized' ],
[ 'uid' => local_user (), 'active' => true ]
));
}
2018-05-01 08:39:15 -04:00
if ( $rules ) {
foreach ( $rules as $rule ) {
try {
$serializedParsedExpression = new ExpressionLanguage\SerializedParsedExpression (
$rule [ 'expression' ],
$rule [ 'serialized' ]
);
2018-08-22 20:27:00 -04:00
// The error suppression operator is used because of potentially broken user-supplied regular expressions
$found = ( bool ) @ $expressionLanguage -> evaluate ( $serializedParsedExpression , $vars );
2018-05-01 08:39:15 -04:00
} catch ( Exception $e ) {
$found = false ;
}
if ( $found ) {
$hook_data [ 'filter_reasons' ][] = L10n :: t ( 'Filtered by rule: %s' , $rule [ 'name' ]);
break ;
}
2018-04-16 22:21:51 -04:00
}
}
}
function advancedcontentfilter_addon_settings ( App $a , & $s )
{
if ( ! local_user ()) {
return ;
}
$advancedcontentfilter = L10n :: t ( 'Advanced Content Filter' );
$s .= <<< HTML
< span class = " settings-block fakelink " style = " display: block; " >< h3 >< a href = " advancedcontentfilter " > $advancedcontentfilter < i class = " glyphicon glyphicon-share " ></ i ></ a ></ h3 ></ span >
HTML ;
return ;
}
/*
* Module
*/
function advancedcontentfilter_module () {}
function advancedcontentfilter_init ( App $a )
{
2018-08-05 08:26:24 -04:00
if ( $a -> argc > 1 && $a -> argv [ 1 ] == 'api' ) {
2018-04-16 22:21:51 -04:00
$slim = new \Slim\App ();
require __DIR__ . '/src/middlewares.php' ;
require __DIR__ . '/src/routes.php' ;
$slim -> run ();
exit ;
}
}
function advancedcontentfilter_content ( App $a )
{
if ( ! local_user ()) {
2018-07-19 22:18:02 -04:00
return Login :: form ( '/' . implode ( '/' , $a -> argv ));
2018-04-16 22:21:51 -04:00
}
2018-08-05 08:26:24 -04:00
if ( $a -> argc > 1 && $a -> argv [ 1 ] == 'help' ) {
2018-04-16 22:21:51 -04:00
$lang = $a -> user [ 'language' ];
$default_dir = 'addon/advancedcontentfilter/doc/' ;
$help_file = 'advancedcontentfilter.md' ;
$help_path = $default_dir . $help_file ;
if ( file_exists ( $default_dir . $lang . '/' . $help_file )) {
$help_path = $default_dir . $lang . '/' . $help_file ;
}
$content = file_get_contents ( $help_path );
2018-07-19 22:18:02 -04:00
$html = Markdown :: convert ( $content , false );
2018-04-16 22:21:51 -04:00
$html = str_replace ( 'code>' , 'key>' , $html );
return $html ;
} else {
2018-10-31 10:55:15 -04:00
$t = Renderer :: getMarkupTemplate ( 'settings.tpl' , 'addon/advancedcontentfilter/' );
return Renderer :: replaceMacros ( $t , [
2018-08-05 08:26:48 -04:00
'$messages' => [
'backtosettings' => L10n :: t ( 'Back to Addon Settings' ),
'title' => L10n :: t ( 'Advanced Content Filter' ),
'add_a_rule' => L10n :: t ( 'Add a Rule' ),
'help' => L10n :: t ( 'Help' ),
'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 help page.' ),
'your_rules' => L10n :: t ( 'Your rules' ),
'no_rules' => L10n :: t ( 'You have no rules yet! Start adding one by clicking on the button above next to the title.' ),
'disabled' => L10n :: t ( 'Disabled' ),
'enabled' => L10n :: t ( 'Enabled' ),
'disable_this_rule' => L10n :: t ( 'Disable this rule' ),
'enable_this_rule' => L10n :: t ( 'Enable this rule' ),
'edit_this_rule' => L10n :: t ( 'Edit this rule' ),
'edit_the_rule' => L10n :: t ( 'Edit the rule' ),
'save_this_rule' => L10n :: t ( 'Save this rule' ),
'delete_this_rule' => L10n :: t ( 'Delete this rule' ),
'rule' => L10n :: t ( 'Rule' ),
'close' => L10n :: t ( 'Close' ),
'addtitle' => L10n :: t ( 'Add new rule' ),
'rule_name' => L10n :: t ( 'Rule Name' ),
'rule_expression' => L10n :: t ( 'Rule Expression' ),
'cancel' => L10n :: t ( 'Cancel' ),
],
2018-05-01 08:39:45 -04:00
'$current_theme' => $a -> getCurrentTheme (),
2018-04-16 22:21:51 -04:00
'$rules' => advancedcontentfilter_get_rules (),
2018-10-17 15:34:15 -04:00
'$form_security_token' => BaseModule :: getFormSecurityToken ()
2018-04-16 22:21:51 -04:00
]);
}
}
/*
* Common functions
*/
function advancedcontentfilter_build_fields ( $data )
{
$fields = [];
if ( ! empty ( $data [ 'name' ])) {
$fields [ 'name' ] = $data [ 'name' ];
}
if ( ! empty ( $data [ 'expression' ])) {
$allowed_keys = [
'author_id' , 'author_link' , 'author_name' , 'author_avatar' ,
'owner_id' , 'owner_link' , 'owner_name' , 'owner_avatar' ,
'contact_id' , 'uid' , 'id' , 'parent' , 'uri' ,
'thr_parent' , 'parent_uri' ,
'content_warning' ,
'commented' , 'created' , 'edited' , 'received' ,
'verb' , 'object_type' , 'postopts' , 'plink' , 'guid' , 'wall' , 'private' , 'starred' ,
'title' , 'body' ,
'file' , 'event_id' , 'location' , 'coord' , 'app' , 'attach' ,
'rendered_hash' , 'rendered_html' , 'object' ,
'allow_cid' , 'allow_gid' , 'deny_cid' , 'deny_gid' ,
'item_id' , 'item_network' , 'author_thumb' , 'owner_thumb' ,
'network' , 'url' , 'name' , 'writable' , 'self' ,
'cid' , 'alias' ,
'event_created' , 'event_edited' , 'event_start' , 'event_finish' , 'event_summary' ,
'event_desc' , 'event_location' , 'event_type' , 'event_nofinish' , 'event_adjust' , 'event_ignore' ,
'children' , 'pagedrop' , 'tags' , 'hashtags' , 'mentions' ,
];
$expressionLanguage = new ExpressionLanguage\ExpressionLanguage ();
$parsedExpression = $expressionLanguage -> parse ( $data [ 'expression' ], $allowed_keys );
$serialized = serialize ( $parsedExpression -> getNodes ());
$fields [ 'expression' ] = $data [ 'expression' ];
$fields [ 'serialized' ] = $serialized ;
}
if ( isset ( $data [ 'active' ])) {
$fields [ 'active' ] = intval ( $data [ 'active' ]);
} else {
$fields [ 'active' ] = 1 ;
}
return $fields ;
}
/*
* API
*/
function advancedcontentfilter_get_rules ()
{
if ( ! local_user ()) {
throw new HTTPException\UnauthorizedException ( L10n :: t ( 'You must be logged in to use this method' ));
}
2018-07-20 22:16:16 -04:00
$rules = DBA :: toArray ( DBA :: select ( 'advancedcontentfilter_rules' , [], [ 'uid' => local_user ()]));
2018-04-16 22:21:51 -04:00
return json_encode ( $rules );
}
function advancedcontentfilter_get_rules_id ( ServerRequestInterface $request , ResponseInterface $response , $args )
{
if ( ! local_user ()) {
throw new HTTPException\UnauthorizedException ( L10n :: t ( 'You must be logged in to use this method' ));
}
2018-07-20 08:20:48 -04:00
$rule = DBA :: selectFirst ( 'advancedcontentfilter_rules' , [], [ 'id' => $args [ 'id' ], 'uid' => local_user ()]);
2018-04-16 22:21:51 -04:00
return json_encode ( $rule );
}
function advancedcontentfilter_post_rules ( ServerRequestInterface $request )
{
if ( ! local_user ()) {
throw new HTTPException\UnauthorizedException ( L10n :: t ( 'You must be logged in to use this method' ));
}
2018-10-17 15:34:15 -04:00
if ( ! BaseModule :: checkFormSecurityToken ()) {
2018-04-16 22:21:51 -04:00
throw new HTTPException\BadRequestException ( L10n :: t ( 'Invalid form security token, please refresh the page.' ));
}
$data = json_decode ( $request -> getBody (), true );
try {
$fields = advancedcontentfilter_build_fields ( $data );
} catch ( Exception $e ) {
throw new HTTPException\BadRequestException ( $e -> getMessage (), 0 , $e );
}
if ( empty ( $fields [ 'name' ]) || empty ( $fields [ 'expression' ])) {
throw new HTTPException\BadRequestException ( L10n :: t ( 'The rule name and expression are required.' ));
}
$fields [ 'uid' ] = local_user ();
2018-07-19 22:18:02 -04:00
$fields [ 'created' ] = DateTimeFormat :: utcNow ();
2018-04-16 22:21:51 -04:00
2018-07-20 08:20:48 -04:00
if ( ! DBA :: insert ( 'advancedcontentfilter_rules' , $fields )) {
throw new HTTPException\ServiceUnavaiableException ( DBA :: errorMessage ());
2018-04-16 22:21:51 -04:00
}
2018-07-20 08:20:48 -04:00
$rule = DBA :: selectFirst ( 'advancedcontentfilter_rules' , [], [ 'id' => DBA :: lastInsertId ()]);
2018-04-16 22:21:51 -04:00
return json_encode ([ 'message' => L10n :: t ( 'Rule successfully added' ), 'rule' => $rule ]);
}
function advancedcontentfilter_put_rules_id ( ServerRequestInterface $request , ResponseInterface $response , $args )
{
if ( ! local_user ()) {
throw new HTTPException\UnauthorizedException ( L10n :: t ( 'You must be logged in to use this method' ));
}
2018-10-17 15:34:15 -04:00
if ( ! BaseModule :: checkFormSecurityToken ()) {
2018-04-16 22:21:51 -04:00
throw new HTTPException\BadRequestException ( L10n :: t ( 'Invalid form security token, please refresh the page.' ));
}
2018-07-20 08:20:48 -04:00
if ( ! DBA :: exists ( 'advancedcontentfilter_rules' , [ 'id' => $args [ 'id' ], 'uid' => local_user ()])) {
2018-04-16 22:21:51 -04:00
throw new HTTPException\NotFoundException ( L10n :: t ( 'Rule doesn\'t exist or doesn\'t belong to you.' ));
}
$data = json_decode ( $request -> getBody (), true );
try {
$fields = advancedcontentfilter_build_fields ( $data );
} catch ( Exception $e ) {
throw new HTTPException\BadRequestException ( $e -> getMessage (), 0 , $e );
}
2018-07-20 08:20:48 -04:00
if ( ! DBA :: update ( 'advancedcontentfilter_rules' , $fields , [ 'id' => $args [ 'id' ]])) {
throw new HTTPException\ServiceUnavaiableException ( DBA :: errorMessage ());
2018-04-16 22:21:51 -04:00
}
return json_encode ([ 'message' => L10n :: t ( 'Rule successfully updated' )]);
}
function advancedcontentfilter_delete_rules_id ( ServerRequestInterface $request , ResponseInterface $response , $args )
{
if ( ! local_user ()) {
throw new HTTPException\UnauthorizedException ( L10n :: t ( 'You must be logged in to use this method' ));
}
2018-10-17 15:34:15 -04:00
if ( ! BaseModule :: checkFormSecurityToken ()) {
2018-04-16 22:21:51 -04:00
throw new HTTPException\BadRequestException ( L10n :: t ( 'Invalid form security token, please refresh the page.' ));
}
2018-07-20 08:20:48 -04:00
if ( ! DBA :: exists ( 'advancedcontentfilter_rules' , [ 'id' => $args [ 'id' ], 'uid' => local_user ()])) {
2018-04-16 22:21:51 -04:00
throw new HTTPException\NotFoundException ( L10n :: t ( 'Rule doesn\'t exist or doesn\'t belong to you.' ));
}
2018-07-20 08:20:48 -04:00
if ( ! DBA :: delete ( 'advancedcontentfilter_rules' , [ 'id' => $args [ 'id' ]])) {
throw new HTTPException\ServiceUnavaiableException ( DBA :: errorMessage ());
2018-04-16 22:21:51 -04:00
}
return json_encode ([ 'message' => L10n :: t ( 'Rule successfully deleted' )]);
}
function advancedcontentfilter_get_variables_guid ( ServerRequestInterface $request , ResponseInterface $response , $args )
{
if ( ! local_user ()) {
throw new HTTPException\UnauthorizedException ( L10n :: t ( 'You must be logged in to use this method' ));
}
if ( ! isset ( $args [ 'guid' ])) {
throw new HTTPException\BadRequestException ( L10n :: t ( 'Missing argument: guid.' ));
}
2018-06-15 18:31:16 -04:00
$condition = [ " `guid` = ? AND (`uid` = ? OR `uid` = 0) " , $args [ 'guid' ], local_user ()];
$params = [ 'order' => [ 'uid' => true ]];
2018-06-17 13:04:23 -04:00
$item = Item :: selectFirstForUser ( local_user (), [], $condition , $params );
2018-04-16 22:21:51 -04:00
2018-07-21 08:46:13 -04:00
if ( ! DBA :: isResult ( $item )) {
2018-04-16 22:21:51 -04:00
throw new HTTPException\NotFoundException ( L10n :: t ( 'Unknown post with guid: %s' , $args [ 'guid' ]));
}
2018-07-19 22:18:02 -04:00
$tags = Term :: populateTagsFromItem ( $item );
2018-04-16 22:21:51 -04:00
$item [ 'tags' ] = $tags [ 'tags' ];
$item [ 'hashtags' ] = $tags [ 'hashtags' ];
$item [ 'mentions' ] = $tags [ 'mentions' ];
$return = [];
foreach ( $item as $key => $value ) {
$return [ str_replace ( '-' , '_' , $key )] = $value ;
}
2018-04-17 20:37:23 -04:00
return json_encode ([ 'variables' => str_replace ( '\\\'' , '\'' , var_export ( $return , true ))]);
2018-06-15 18:31:16 -04:00
}