2018-04-17 02:21:51 +00: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 19:34:15 +00:00
use Friendica\BaseModule ;
2018-07-20 02:18:02 +00:00
use Friendica\Content\Text\Markdown ;
2018-12-26 07:28:16 +00:00
use Friendica\Core\Hook ;
2018-10-29 23:40:18 +00:00
use Friendica\Core\Logger ;
2018-10-31 14:55:15 +00:00
use Friendica\Core\Renderer ;
2018-07-20 12:20:48 +00:00
use Friendica\Database\DBA ;
2018-04-17 02:21:51 +00:00
use Friendica\Database\DBStructure ;
2020-01-10 19:05:27 +00:00
use Friendica\DI ;
2018-07-20 02:18:02 +00:00
use Friendica\Model\Item ;
2021-01-17 00:00:32 +00:00
use Friendica\Model\Post ;
2020-05-05 22:47:43 +00:00
use Friendica\Model\Tag ;
2021-08-08 17:10:04 +00:00
use Friendica\Model\User ;
2019-12-30 02:55:37 +00:00
use Friendica\Module\Security\Login ;
2018-04-17 02:21:51 +00:00
use Friendica\Network\HTTPException ;
2018-07-20 02:18:02 +00:00
use Friendica\Util\DateTimeFormat ;
2018-04-17 02:21:51 +00: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' ;
2023-01-14 02:16:09 +00:00
function advancedcontentfilter_install ()
2018-04-17 02:21:51 +00:00
{
2019-12-23 00:36:32 +00:00
Hook :: register ( 'dbstructure_definition' , __FILE__ , 'advancedcontentfilter_dbstructure_definition' );
2018-12-26 07:28:16 +00:00
Hook :: register ( 'prepare_body_content_filter' , __FILE__ , 'advancedcontentfilter_prepare_body_content_filter' );
Hook :: register ( 'addon_settings' , __FILE__ , 'advancedcontentfilter_addon_settings' );
2018-04-17 02:21:51 +00:00
2019-12-23 00:36:32 +00:00
Hook :: add ( 'dbstructure_definition' , __FILE__ , 'advancedcontentfilter_dbstructure_definition' );
2021-01-30 13:32:43 +00:00
DBStructure :: performUpdate ();
2018-04-17 02:21:51 +00:00
2022-06-23 04:04:05 +00:00
Logger :: notice ( 'installed advancedcontentfilter' );
2018-04-17 02:21:51 +00:00
}
/*
* Hooks
*/
2023-01-14 02:16:09 +00:00
function advancedcontentfilter_dbstructure_definition ( & $database )
2018-04-17 02:21:51 +00:00
{
2022-06-23 04:04:05 +00:00
$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' ],
'created' => [ 'type' => 'datetime' , 'not null' => '1' , 'default' => DBA :: NULL_DATETIME , 'comment' => 'Creation date' ],
2018-04-17 02:21:51 +00:00
],
2022-06-23 04:04:05 +00:00
'indexes' => [
'PRIMARY' => [ 'id' ],
'uid_active' => [ 'uid' , 'active' ],
2018-04-17 02:21:51 +00:00
]
];
}
2022-05-11 13:00:19 +00:00
/**
* @ param array $item Prepared by either Model\Item :: prepareBody or advancedcontentfilter_prepare_item_row
* @ return array
*/
2021-04-11 09:03:14 +00:00
function advancedcontentfilter_get_filter_fields ( array $item )
{
$vars = [];
// Convert the language JSON text into a filterable format
if ( ! empty ( $item [ 'language' ]) && ( $languages = json_decode ( $item [ 'language' ], true ))) {
foreach ( $languages as $key => $value ) {
$vars [ 'language_' . strtolower ( $key )] = $value ;
}
}
foreach ( $item as $key => $value ) {
$vars [ str_replace ( '-' , '_' , $key )] = $value ;
}
ksort ( $vars );
return $vars ;
}
2023-01-14 02:16:09 +00:00
function advancedcontentfilter_prepare_body_content_filter ( & $hook_data )
2018-04-17 02:21:51 +00:00
{
static $expressionLanguage ;
if ( is_null ( $expressionLanguage )) {
$expressionLanguage = new ExpressionLanguage\ExpressionLanguage ();
}
2022-10-20 21:51:49 +00:00
if ( ! DI :: userSession () -> getLocalUserId ()) {
2018-04-17 02:21:51 +00:00
return ;
}
2021-04-11 09:03:14 +00:00
$vars = advancedcontentfilter_get_filter_fields ( $hook_data [ 'item' ]);
2018-04-17 02:21:51 +00:00
2022-10-20 21:51:49 +00:00
$rules = DI :: cache () -> get ( 'rules_' . DI :: userSession () -> getLocalUserId ());
2018-04-17 02:21:51 +00:00
if ( ! isset ( $rules )) {
2018-07-21 02:16:16 +00:00
$rules = DBA :: toArray ( DBA :: select (
2018-04-17 02:21:51 +00:00
'advancedcontentfilter_rules' ,
[ 'name' , 'expression' , 'serialized' ],
2022-10-20 21:51:49 +00:00
[ 'uid' => DI :: userSession () -> getLocalUserId (), 'active' => true ]
2018-04-17 02:21:51 +00:00
));
2020-01-10 19:05:27 +00:00
2022-10-20 21:51:49 +00:00
DI :: cache () -> set ( 'rules_' . DI :: userSession () -> getLocalUserId (), $rules );
2018-04-17 02:21:51 +00:00
}
2018-05-01 12:39:15 +00:00
if ( $rules ) {
foreach ( $rules as $rule ) {
try {
$serializedParsedExpression = new ExpressionLanguage\SerializedParsedExpression (
$rule [ 'expression' ],
$rule [ 'serialized' ]
);
2018-08-23 00:27:00 +00:00
// The error suppression operator is used because of potentially broken user-supplied regular expressions
$found = ( bool ) @ $expressionLanguage -> evaluate ( $serializedParsedExpression , $vars );
2018-05-01 12:39:15 +00:00
} catch ( Exception $e ) {
$found = false ;
}
if ( $found ) {
2020-01-18 19:52:33 +00:00
$hook_data [ 'filter_reasons' ][] = DI :: l10n () -> t ( 'Filtered by rule: %s' , $rule [ 'name' ]);
2018-05-01 12:39:15 +00:00
break ;
}
2018-04-17 02:21:51 +00:00
}
}
}
2023-01-14 02:16:09 +00:00
function advancedcontentfilter_addon_settings ( array & $data )
2018-04-17 02:21:51 +00:00
{
2022-10-20 21:51:49 +00:00
if ( ! DI :: userSession () -> getLocalUserId ()) {
2018-04-17 02:21:51 +00:00
return ;
}
2021-11-20 09:56:55 +00:00
$data = [
'addon' => 'advancedcontentfilter' ,
'title' => DI :: l10n () -> t ( 'Advanced Content Filter' ),
'href' => 'advancedcontentfilter' ,
];
2018-04-17 02:21:51 +00:00
}
/*
* Module
*/
2022-06-24 21:27:58 +00:00
/**
* This is a statement rather than an actual function definition . The simple
* existence of this method is checked to figure out if the addon offers a
* module .
*/
2018-04-17 02:21:51 +00:00
function advancedcontentfilter_module () {}
2023-01-14 02:16:09 +00:00
function advancedcontentfilter_init ()
2018-04-17 02:21:51 +00:00
{
2021-07-25 12:29:43 +00:00
if ( DI :: args () -> getArgc () > 1 && DI :: args () -> getArgv ()[ 1 ] == 'api' ) {
2024-01-12 05:08:24 +00:00
$slim = \Slim\Factory\AppFactory :: create ();
2018-04-17 02:21:51 +00:00
2024-11-24 15:36:37 +00:00
/**
* The routing middleware should be added before the ErrorMiddleware
* Otherwise exceptions thrown from it will not be handled
*/
$slim -> addRoutingMiddleware ();
$slim -> addErrorMiddleware ( true , true , true , DI :: logger ());
// register routes
$slim -> group ( '/advancedcontentfilter/api' , function ( \Slim\Routing\RouteCollectorProxy $app ) {
$app -> group ( '/rules' , function ( \Slim\Routing\RouteCollectorProxy $app ) {
$app -> get ( '' , 'advancedcontentfilter_get_rules' );
$app -> post ( '' , 'advancedcontentfilter_post_rules' );
$app -> get ( '/{id}' , 'advancedcontentfilter_get_rules_id' );
$app -> put ( '/{id}' , 'advancedcontentfilter_put_rules_id' );
$app -> delete ( '/{id}' , 'advancedcontentfilter_delete_rules_id' );
});
$app -> group ( '/variables' , function ( \Slim\Routing\RouteCollectorProxy $app ) {
$app -> get ( '/{guid}' , 'advancedcontentfilter_get_variables_guid' );
});
});
2018-04-17 02:21:51 +00:00
$slim -> run ();
exit ;
}
}
2023-01-14 02:16:09 +00:00
function advancedcontentfilter_content ()
2018-04-17 02:21:51 +00:00
{
2022-10-20 21:51:49 +00:00
if ( ! DI :: userSession () -> getLocalUserId ()) {
2021-07-25 12:29:43 +00:00
return Login :: form ( '/' . implode ( '/' , DI :: args () -> getArgv ()));
2018-04-17 02:21:51 +00:00
}
2021-07-25 12:29:43 +00:00
if ( DI :: args () -> getArgc () > 1 && DI :: args () -> getArgv ()[ 1 ] == 'help' ) {
2022-10-20 21:51:49 +00:00
$user = User :: getById ( DI :: userSession () -> getLocalUserId ());
2021-08-08 17:10:04 +00:00
$lang = $user [ 'language' ];
2018-04-17 02:21:51 +00:00
$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-20 02:18:02 +00:00
$html = Markdown :: convert ( $content , false );
2018-04-17 02:21:51 +00:00
$html = str_replace ( 'code>' , 'key>' , $html );
return $html ;
} else {
2018-10-31 14:55:15 +00:00
$t = Renderer :: getMarkupTemplate ( 'settings.tpl' , 'addon/advancedcontentfilter/' );
return Renderer :: replaceMacros ( $t , [
2018-08-05 12:26:48 +00:00
'$messages' => [
2020-01-18 19:52:33 +00:00
'backtosettings' => DI :: l10n () -> t ( 'Back to Addon Settings' ),
'title' => DI :: l10n () -> t ( 'Advanced Content Filter' ),
'add_a_rule' => DI :: l10n () -> t ( 'Add a Rule' ),
'help' => DI :: l10n () -> t ( 'Help' ),
'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.' ),
'your_rules' => DI :: l10n () -> t ( 'Your rules' ),
'no_rules' => DI :: l10n () -> t ( 'You have no rules yet! Start adding one by clicking on the button above next to the title.' ),
'disabled' => DI :: l10n () -> t ( 'Disabled' ),
'enabled' => DI :: l10n () -> t ( 'Enabled' ),
'disable_this_rule' => DI :: l10n () -> t ( 'Disable this rule' ),
'enable_this_rule' => DI :: l10n () -> t ( 'Enable this rule' ),
'edit_this_rule' => DI :: l10n () -> t ( 'Edit this rule' ),
'edit_the_rule' => DI :: l10n () -> t ( 'Edit the rule' ),
'save_this_rule' => DI :: l10n () -> t ( 'Save this rule' ),
'delete_this_rule' => DI :: l10n () -> t ( 'Delete this rule' ),
'rule' => DI :: l10n () -> t ( 'Rule' ),
'close' => DI :: l10n () -> t ( 'Close' ),
'addtitle' => DI :: l10n () -> t ( 'Add new rule' ),
'rule_name' => DI :: l10n () -> t ( 'Rule Name' ),
'rule_expression' => DI :: l10n () -> t ( 'Rule Expression' ),
'cancel' => DI :: l10n () -> t ( 'Cancel' ),
2018-08-05 12:26:48 +00:00
],
2023-01-14 02:16:09 +00:00
'$current_theme' => DI :: app () -> getCurrentTheme (),
2024-03-04 06:16:49 +00:00
'$rules' => DBA :: toArray ( DBA :: select ( 'advancedcontentfilter_rules' , [], [ 'uid' => DI :: userSession () -> getLocalUserId ()])),
2018-10-17 19:34:15 +00:00
'$form_security_token' => BaseModule :: getFormSecurityToken ()
2018-04-17 02:21:51 +00:00
]);
}
}
/*
* Common functions
*/
function advancedcontentfilter_build_fields ( $data )
{
$fields = [];
if ( ! empty ( $data [ 'name' ])) {
$fields [ 'name' ] = $data [ 'name' ];
}
if ( ! empty ( $data [ 'expression' ])) {
2022-05-11 13:00:19 +00:00
// Using a dummy item to validate the field existence
2022-10-20 21:51:49 +00:00
$condition = [ " (`uid` = ? OR `uid` = 0) " , DI :: userSession () -> getLocalUserId ()];
2022-05-11 13:00:19 +00:00
$params = [ 'order' => [ 'uid' => true ]];
2022-10-20 21:51:49 +00:00
$item_row = Post :: selectFirstForUser ( DI :: userSession () -> getLocalUserId (), [], $condition , $params );
2018-04-17 02:21:51 +00:00
2022-05-11 13:00:19 +00:00
if ( ! DBA :: isResult ( $item_row )) {
throw new HTTPException\NotFoundException ( DI :: l10n () -> t ( 'This addon requires this node having at least one post' ));
}
2018-04-17 02:21:51 +00:00
2022-05-11 13:00:19 +00:00
$expressionLanguage = new ExpressionLanguage\ExpressionLanguage ();
$parsedExpression = $expressionLanguage -> parse (
$data [ 'expression' ],
array_keys ( advancedcontentfilter_get_filter_fields ( advancedcontentfilter_prepare_item_row ( $item_row )))
);
2018-04-17 02:21:51 +00:00
$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
*/
2024-03-04 06:16:49 +00:00
function advancedcontentfilter_get_rules ( ServerRequestInterface $request , ResponseInterface $response ) : ResponseInterface
2018-04-17 02:21:51 +00:00
{
2022-10-20 21:51:49 +00:00
if ( ! DI :: userSession () -> getLocalUserId ()) {
2020-01-18 19:52:33 +00:00
throw new HTTPException\UnauthorizedException ( DI :: l10n () -> t ( 'You must be logged in to use this method' ));
2018-04-17 02:21:51 +00:00
}
2022-10-20 21:51:49 +00:00
$rules = DBA :: toArray ( DBA :: select ( 'advancedcontentfilter_rules' , [], [ 'uid' => DI :: userSession () -> getLocalUserId ()]));
2018-04-17 02:21:51 +00:00
2024-01-12 05:08:24 +00:00
$response -> getBody () -> write ( json_encode ( $rules ));
return $response -> withHeader ( 'Content-Type' , 'application/json' );
2018-04-17 02:21:51 +00:00
}
function advancedcontentfilter_get_rules_id ( ServerRequestInterface $request , ResponseInterface $response , $args )
{
2022-10-20 21:51:49 +00:00
if ( ! DI :: userSession () -> getLocalUserId ()) {
2020-01-18 19:52:33 +00:00
throw new HTTPException\UnauthorizedException ( DI :: l10n () -> t ( 'You must be logged in to use this method' ));
2018-04-17 02:21:51 +00:00
}
2022-10-20 21:51:49 +00:00
$rule = DBA :: selectFirst ( 'advancedcontentfilter_rules' , [], [ 'id' => $args [ 'id' ], 'uid' => DI :: userSession () -> getLocalUserId ()]);
2018-04-17 02:21:51 +00:00
2024-01-12 05:08:24 +00:00
$response -> getBody () -> write ( json_encode ( $rule ));
return $response -> withHeader ( 'Content-Type' , 'application/json' );
2018-04-17 02:21:51 +00:00
}
2024-01-12 05:08:24 +00:00
function advancedcontentfilter_post_rules ( ServerRequestInterface $request , ResponseInterface $response )
2018-04-17 02:21:51 +00:00
{
2022-10-20 21:51:49 +00:00
if ( ! DI :: userSession () -> getLocalUserId ()) {
2020-01-18 19:52:33 +00:00
throw new HTTPException\UnauthorizedException ( DI :: l10n () -> t ( 'You must be logged in to use this method' ));
2018-04-17 02:21:51 +00:00
}
2018-10-17 19:34:15 +00:00
if ( ! BaseModule :: checkFormSecurityToken ()) {
2020-01-18 19:52:33 +00:00
throw new HTTPException\BadRequestException ( DI :: l10n () -> t ( 'Invalid form security token, please refresh the page.' ));
2018-04-17 02:21:51 +00:00
}
$data = json_decode ( $request -> getBody (), true );
try {
$fields = advancedcontentfilter_build_fields ( $data );
} catch ( Exception $e ) {
2020-03-10 22:44:27 +00:00
throw new HTTPException\BadRequestException ( $e -> getMessage (), $e );
2018-04-17 02:21:51 +00:00
}
if ( empty ( $fields [ 'name' ]) || empty ( $fields [ 'expression' ])) {
2020-01-18 19:52:33 +00:00
throw new HTTPException\BadRequestException ( DI :: l10n () -> t ( 'The rule name and expression are required.' ));
2018-04-17 02:21:51 +00:00
}
2022-10-20 21:51:49 +00:00
$fields [ 'uid' ] = DI :: userSession () -> getLocalUserId ();
2018-07-20 02:18:02 +00:00
$fields [ 'created' ] = DateTimeFormat :: utcNow ();
2018-04-17 02:21:51 +00:00
2018-07-20 12:20:48 +00:00
if ( ! DBA :: insert ( 'advancedcontentfilter_rules' , $fields )) {
2019-11-24 19:20:57 +00:00
throw new HTTPException\ServiceUnavailableException ( DBA :: errorMessage ());
2018-04-17 02:21:51 +00:00
}
2018-07-20 12:20:48 +00:00
$rule = DBA :: selectFirst ( 'advancedcontentfilter_rules' , [], [ 'id' => DBA :: lastInsertId ()]);
2018-04-17 02:21:51 +00:00
2022-10-20 21:51:49 +00:00
DI :: cache () -> delete ( 'rules_' . DI :: userSession () -> getLocalUserId ());
2021-08-16 10:10:34 +00:00
2024-01-12 05:08:24 +00:00
$response -> getBody () -> write ( json_encode ([ 'message' => DI :: l10n () -> t ( 'Rule successfully added' ), 'rule' => $rule ]));
return $response -> withHeader ( 'Content-Type' , 'application/json' );
2018-04-17 02:21:51 +00:00
}
function advancedcontentfilter_put_rules_id ( ServerRequestInterface $request , ResponseInterface $response , $args )
{
2022-10-20 21:51:49 +00:00
if ( ! DI :: userSession () -> getLocalUserId ()) {
2020-01-18 19:52:33 +00:00
throw new HTTPException\UnauthorizedException ( DI :: l10n () -> t ( 'You must be logged in to use this method' ));
2018-04-17 02:21:51 +00:00
}
2018-10-17 19:34:15 +00:00
if ( ! BaseModule :: checkFormSecurityToken ()) {
2020-01-18 19:52:33 +00:00
throw new HTTPException\BadRequestException ( DI :: l10n () -> t ( 'Invalid form security token, please refresh the page.' ));
2018-04-17 02:21:51 +00:00
}
2022-10-20 21:51:49 +00:00
if ( ! DBA :: exists ( 'advancedcontentfilter_rules' , [ 'id' => $args [ 'id' ], 'uid' => DI :: userSession () -> getLocalUserId ()])) {
2020-01-18 19:52:33 +00:00
throw new HTTPException\NotFoundException ( DI :: l10n () -> t ( 'Rule doesn\'t exist or doesn\'t belong to you.' ));
2018-04-17 02:21:51 +00:00
}
$data = json_decode ( $request -> getBody (), true );
try {
$fields = advancedcontentfilter_build_fields ( $data );
} catch ( Exception $e ) {
2020-03-10 22:44:27 +00:00
throw new HTTPException\BadRequestException ( $e -> getMessage (), $e );
2018-04-17 02:21:51 +00:00
}
2018-07-20 12:20:48 +00:00
if ( ! DBA :: update ( 'advancedcontentfilter_rules' , $fields , [ 'id' => $args [ 'id' ]])) {
2020-01-19 15:29:54 +00:00
throw new HTTPException\ServiceUnavailableException ( DBA :: errorMessage ());
2018-04-17 02:21:51 +00:00
}
2022-10-20 21:51:49 +00:00
DI :: cache () -> delete ( 'rules_' . DI :: userSession () -> getLocalUserId ());
2021-08-16 10:10:34 +00:00
2024-01-12 05:08:24 +00:00
$response -> getBody () -> write ( json_encode ([ 'message' => DI :: l10n () -> t ( 'Rule successfully updated' )]));
return $response -> withHeader ( 'Content-Type' , 'application/json' );
2018-04-17 02:21:51 +00:00
}
function advancedcontentfilter_delete_rules_id ( ServerRequestInterface $request , ResponseInterface $response , $args )
{
2022-10-20 21:51:49 +00:00
if ( ! DI :: userSession () -> getLocalUserId ()) {
2020-01-18 19:52:33 +00:00
throw new HTTPException\UnauthorizedException ( DI :: l10n () -> t ( 'You must be logged in to use this method' ));
2018-04-17 02:21:51 +00:00
}
2018-10-17 19:34:15 +00:00
if ( ! BaseModule :: checkFormSecurityToken ()) {
2020-01-18 19:52:33 +00:00
throw new HTTPException\BadRequestException ( DI :: l10n () -> t ( 'Invalid form security token, please refresh the page.' ));
2018-04-17 02:21:51 +00:00
}
2022-10-20 21:51:49 +00:00
if ( ! DBA :: exists ( 'advancedcontentfilter_rules' , [ 'id' => $args [ 'id' ], 'uid' => DI :: userSession () -> getLocalUserId ()])) {
2020-01-18 19:52:33 +00:00
throw new HTTPException\NotFoundException ( DI :: l10n () -> t ( 'Rule doesn\'t exist or doesn\'t belong to you.' ));
2018-04-17 02:21:51 +00:00
}
2018-07-20 12:20:48 +00:00
if ( ! DBA :: delete ( 'advancedcontentfilter_rules' , [ 'id' => $args [ 'id' ]])) {
2020-01-19 15:29:54 +00:00
throw new HTTPException\ServiceUnavailableException ( DBA :: errorMessage ());
2018-04-17 02:21:51 +00:00
}
2022-10-20 21:51:49 +00:00
DI :: cache () -> delete ( 'rules_' . DI :: userSession () -> getLocalUserId ());
2021-08-16 10:10:34 +00:00
2024-01-12 05:08:24 +00:00
$response -> getBody () -> write ( json_encode ([ 'message' => DI :: l10n () -> t ( 'Rule successfully deleted' )]));
return $response -> withHeader ( 'Content-Type' , 'application/json' );
2018-04-17 02:21:51 +00:00
}
function advancedcontentfilter_get_variables_guid ( ServerRequestInterface $request , ResponseInterface $response , $args )
{
2022-10-20 21:51:49 +00:00
if ( ! DI :: userSession () -> getLocalUserId ()) {
2020-01-18 19:52:33 +00:00
throw new HTTPException\UnauthorizedException ( DI :: l10n () -> t ( 'You must be logged in to use this method' ));
2018-04-17 02:21:51 +00:00
}
if ( ! isset ( $args [ 'guid' ])) {
2020-01-18 19:52:33 +00:00
throw new HTTPException\BadRequestException ( DI :: l10n () -> t ( 'Missing argument: guid.' ));
2018-04-17 02:21:51 +00:00
}
2022-10-20 21:51:49 +00:00
$condition = [ " `guid` = ? AND (`uid` = ? OR `uid` = 0) " , $args [ 'guid' ], DI :: userSession () -> getLocalUserId ()];
2018-06-15 22:31:16 +00:00
$params = [ 'order' => [ 'uid' => true ]];
2022-10-20 21:51:49 +00:00
$item_row = Post :: selectFirstForUser ( DI :: userSession () -> getLocalUserId (), [], $condition , $params );
2018-04-17 02:21:51 +00:00
2022-05-11 13:00:19 +00:00
if ( ! DBA :: isResult ( $item_row )) {
2020-01-18 19:52:33 +00:00
throw new HTTPException\NotFoundException ( DI :: l10n () -> t ( 'Unknown post with guid: %s' , $args [ 'guid' ]));
2018-04-17 02:21:51 +00:00
}
2022-05-11 13:00:19 +00:00
$return = advancedcontentfilter_get_filter_fields ( advancedcontentfilter_prepare_item_row ( $item_row ));
2018-04-17 02:21:51 +00:00
2024-01-12 05:08:24 +00:00
$response -> getBody () -> write ( json_encode ([ 'variables' => str_replace ( '\\\'' , '\'' , var_export ( $return , true ))]));
return $response -> withHeader ( 'Content-Type' , 'application/json' );
2022-05-11 13:00:19 +00:00
}
2018-04-17 02:21:51 +00:00
2022-05-11 13:00:19 +00:00
/**
* This mimimcs the processing performed in Model\Item :: prepareBody
*
* @ param array $item_row
* @ return array
* @ throws HTTPException\InternalServerErrorException
* @ throws ImagickException
*/
function advancedcontentfilter_prepare_item_row ( array $item_row ) : array
{
$tags = Tag :: populateFromItem ( $item_row );
2021-08-16 09:29:17 +00:00
2022-05-11 13:00:19 +00:00
$item_row [ 'tags' ] = $tags [ 'tags' ];
$item_row [ 'hashtags' ] = $tags [ 'hashtags' ];
$item_row [ 'mentions' ] = $tags [ 'mentions' ];
2023-11-14 21:25:06 +00:00
$item_row [ 'attachments' ] = DI :: postMediaRepository () -> splitAttachments ( $item_row [ 'uri-id' ]);
2022-05-11 13:00:19 +00:00
return $item_row ;
2018-06-15 22:31:16 +00:00
}