]> git.mxchange.org Git - friendica-addons.git/blobdiff - advancedcontentfilter/advancedcontentfilter.php
Array instead of a string
[friendica-addons.git] / advancedcontentfilter / advancedcontentfilter.php
index 37463d5d056441ce3aaf9679475faecf01a23251..dbbfb2e2ec67747137fb65e376b7406fb901f945 100644 (file)
  */
 
 use Friendica\App;
+use Friendica\BaseModule;
 use Friendica\Content\Text\Markdown;
-use Friendica\Core\Addon;
 use Friendica\Core\Cache;
+use Friendica\Core\Hook;
 use Friendica\Core\L10n;
-use Friendica\Core\System;
+use Friendica\Core\Logger;
+use Friendica\Core\Renderer;
 use Friendica\Database\DBA;
-use Friendica\Database\DBM;
 use Friendica\Database\DBStructure;
 use Friendica\Model\Item;
 use Friendica\Model\Term;
@@ -51,29 +52,24 @@ use Psr\Http\Message\ResponseInterface;
 use Psr\Http\Message\ServerRequestInterface;
 use Symfony\Component\ExpressionLanguage;
 
-require_once 'boot.php';
-require_once 'include/conversation.php';
-require_once 'include/dba.php';
-require_once 'include/security.php';
-
 require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
 
-function advancedcontentfilter_install()
+function advancedcontentfilter_install(App $a)
 {
-       Addon::registerHook('dbstructure_definition'     , __FILE__, 'advancedcontentfilter_dbstructure_definition');
-       Addon::registerHook('prepare_body_content_filter', __FILE__, 'advancedcontentfilter_prepare_body_content_filter');
-       Addon::registerHook('addon_settings'             , __FILE__, 'advancedcontentfilter_addon_settings');
+       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');
 
-       DBStructure::update(false, true);
+       DBStructure::update($a->getBasePath(), false, true);
 
-       logger("installed advancedcontentfilter");
+       Logger::log("installed advancedcontentfilter");
 }
 
 function advancedcontentfilter_uninstall()
 {
-       Addon::unregisterHook('dbstructure_definition'     , __FILE__, 'advancedcontentfilter_dbstructure_definition');
-       Addon::unregisterHook('prepare_body_content_filter', __FILE__, 'advancedcontentfilter_prepare_body_content_filter');
-       Addon::unregisterHook('addon_settings'             , __FILE__, 'advancedcontentfilter_addon_settings');
+       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');
 }
 
 /*
@@ -91,7 +87,7 @@ function advancedcontentfilter_dbstructure_definition(App $a, &$database)
                        "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" => NULL_DATE, "comment" => "Creation date"],
+                       "created"    => ["type" => "datetime"    , "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => "Creation date"],
                ],
                "indexes" => [
                        "PRIMARY" => ["id"],
@@ -134,7 +130,8 @@ function advancedcontentfilter_prepare_body_content_filter(App $a, &$hook_data)
                                        $rule['serialized']
                                );
 
-                               $found = (bool) $expressionLanguage->evaluate($serializedParsedExpression, $vars);
+                               // The error suppression operator is used because of potentially broken user-supplied regular expressions
+                               $found = (bool) @$expressionLanguage->evaluate($serializedParsedExpression, $vars);
                        } catch (Exception $e) {
                                $found = false;
                        }
@@ -171,7 +168,7 @@ function advancedcontentfilter_module() {}
 
 function advancedcontentfilter_init(App $a)
 {
-       if ($a->argv[1] == 'api') {
+       if ($a->argc > 1 && $a->argv[1] == 'api') {
                $slim = new \Slim\App();
 
                require __DIR__ . '/src/middlewares.php';
@@ -189,7 +186,7 @@ function advancedcontentfilter_content(App $a)
                return Login::form('/' . implode('/', $a->argv));
        }
 
-       if ($a->argc > 0 && $a->argv[1] == 'help') {
+       if ($a->argc > 1 && $a->argv[1] == 'help') {
                $lang = $a->user['language'];
 
                $default_dir = 'addon/advancedcontentfilter/doc/';
@@ -207,34 +204,34 @@ function advancedcontentfilter_content(App $a)
 
                return $html;
        } else {
-               $t = get_markup_template('settings.tpl', 'addon/advancedcontentfilter/');
-               return replace_macros($t, [
+               $t = Renderer::getMarkupTemplate('settings.tpl', 'addon/advancedcontentfilter/');
+               return Renderer::replaceMacros($t, [
+                       '$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'),
+                       ],
                        '$current_theme' => $a->getCurrentTheme(),
-                       '$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'),
-                       '$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>.'),
-                       '$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'),
-                       '$examples' => L10n::t('<p>Examples:</p><ul><li><pre>author_link == \'https://friendica.mrpetovan.com/profile/hypolite\'</pre></li><li>tags</li></ul>'),
-                       '$cancel' => L10n::t('Cancel'),
                        '$rules' => advancedcontentfilter_get_rules(),
-                       '$baseurl' => System::baseUrl(true),
-                       '$form_security_token' => get_form_security_token()
+                       '$form_security_token' => BaseModule::getFormSecurityToken()
                ]);
        }
 }
@@ -322,7 +319,7 @@ function advancedcontentfilter_post_rules(ServerRequestInterface $request)
                throw new HTTPException\UnauthorizedException(L10n::t('You must be logged in to use this method'));
        }
 
-       if (!check_form_security_token()) {
+       if (!BaseModule::checkFormSecurityToken()) {
                throw new HTTPException\BadRequestException(L10n::t('Invalid form security token, please refresh the page.'));
        }
 
@@ -356,7 +353,7 @@ function advancedcontentfilter_put_rules_id(ServerRequestInterface $request, Res
                throw new HTTPException\UnauthorizedException(L10n::t('You must be logged in to use this method'));
        }
 
-       if (!check_form_security_token()) {
+       if (!BaseModule::checkFormSecurityToken()) {
                throw new HTTPException\BadRequestException(L10n::t('Invalid form security token, please refresh the page.'));
        }
 
@@ -385,7 +382,7 @@ function advancedcontentfilter_delete_rules_id(ServerRequestInterface $request,
                throw new HTTPException\UnauthorizedException(L10n::t('You must be logged in to use this method'));
        }
 
-       if (!check_form_security_token()) {
+       if (!BaseModule::checkFormSecurityToken()) {
                throw new HTTPException\BadRequestException(L10n::t('Invalid form security token, please refresh the page.'));
        }
 
@@ -414,7 +411,7 @@ function advancedcontentfilter_get_variables_guid(ServerRequestInterface $reques
        $params = ['order' => ['uid' => true]];
        $item = Item::selectFirstForUser(local_user(), [], $condition, $params);
 
-       if (!DBM::is_result($item)) {
+       if (!DBA::isResult($item)) {
                throw new HTTPException\NotFoundException(L10n::t('Unknown post with guid: %s', $args['guid']));
        }