From 04ddb284da1a8c4663b60e18a8535de41f968edc Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sun, 5 Aug 2018 13:42:53 +0200 Subject: [PATCH 1/4] Revert "Merge pull request #682 from MrPetovan/bug/5341-advancedcontentfilter-honors-csp" This reverts commit e79d73843778f4df19b3e7e85a71b2d38d80e973, reversing changes made to 20e42c09b7a77b4da850c32e13717f081f50ea8a. --- .../advancedcontentfilter.js | 122 +++++++++ .../advancedcontentfilter.php | 2 +- advancedcontentfilter/templates/settings.tpl | 231 +++++++----------- advancedcontentfilter/templates/vue_dom.tpl | 96 -------- 4 files changed, 213 insertions(+), 238 deletions(-) create mode 100644 advancedcontentfilter/advancedcontentfilter.js delete mode 100644 advancedcontentfilter/templates/vue_dom.tpl diff --git a/advancedcontentfilter/advancedcontentfilter.js b/advancedcontentfilter/advancedcontentfilter.js new file mode 100644 index 000000000..67a2b8d24 --- /dev/null +++ b/advancedcontentfilter/advancedcontentfilter.js @@ -0,0 +1,122 @@ +$.ajaxSetup({headers: {'X-CSRF-Token': document.querySelector('#csrf').getAttribute('value')}}); + +$.extend({ + ajaxJSON: function(method, url, data) { + return $.ajax({ + type: method.toUpperCase(), + url: url, + data: JSON.stringify(data), + contentType: 'application/json; charset=utf-8', + dataType: 'json' + }); + } +}); + +new Vue({ + el: '#rules', + + data: { + showModal: false, + errorMessage: '', + editedIndex: null, + rule: {id: '', name: '', expression: '', created: ''}, + rules: existingRules || [], + itemUrl: '', + itemJson: '' + }, + + watch: { + showModal: function () { + if (this.showModal) { + $(this.$refs.vuemodal).modal('show'); + } else { + $(this.$refs.vuemodal).modal('hide'); + } + } + }, + + methods: { + resetForm: function() { + this.rule = {id: '', name: '', expression: '', created: ''}; + this.showModal = false; + this.editedIndex = null; + }, + + addRule: function () { + if (this.rule.name.trim()) { + this.errorMessage = ''; + + var self = this; + $.ajaxJSON('post', '/advancedcontentfilter/api/rules', this.rule) + .then(function (responseJSON) { + self.rules.push(responseJSON.rule); + self.resetForm(); + }, function (response) { + self.errorMessage = response.responseJSON.message; + }); + } + }, + + editRule: function (rule) { + this.editedIndex = this.rules.indexOf(rule); + this.rule = Object.assign({}, rule); + this.showModal = true; + }, + + saveRule: function (rule) { + this.errorMessage = ''; + + var self = this; + $.ajaxJSON('put', '/advancedcontentfilter/api/rules/' + rule.id, rule) + .then(function () { + self.rules[self.editedIndex] = rule; + self.resetForm(); + }, function (response) { + self.errorMessage = response.responseJSON.message; + }); + }, + + toggleActive: function (rule) { + var previousValue = this.rules[this.rules.indexOf(rule)].active; + var newValue = Math.abs(parseInt(rule.active) - 1); + + this.rules[this.rules.indexOf(rule)].active = newValue; + + var self = this; + $.ajaxJSON('put', '/advancedcontentfilter/api/rules/' + rule.id, {'active': newValue}) + .fail(function (response) { + self.rules[self.rules.indexOf(rule)].active = previousValue; + console.log(response.responseJSON.message); + }); + }, + + deleteRule: function (rule) { + if (confirm('Are you sure you want to delete this rule?')) { + var self = this; + $.ajaxJSON('delete', '/advancedcontentfilter/api/rules/' + rule.id) + .then(function () { + self.rules.splice(self.rules.indexOf(rule), 1); + }, function (response) { + console.log(response.responseJSON.message); + }); + } + }, + + showVariables: function () { + var urlParts = this.itemUrl.split('/'); + var guid = urlParts[urlParts.length - 1]; + + this.itemJson = ''; + + var self = this; + $.ajaxJSON('get', '/advancedcontentfilter/api/variables/' + guid) + .then(function (responseJSON) { + self.itemJson = responseJSON.variables; + }, function (response) { + self.itemJson = response.responseJSON.message; + }); + + return false; + } + } +}); \ No newline at end of file diff --git a/advancedcontentfilter/advancedcontentfilter.php b/advancedcontentfilter/advancedcontentfilter.php index 2d1308e7a..34d47425e 100644 --- a/advancedcontentfilter/advancedcontentfilter.php +++ b/advancedcontentfilter/advancedcontentfilter.php @@ -213,7 +213,7 @@ function advancedcontentfilter_content(App $a) '$title' => L10n::t('Advanced Content Filter'), '$add_a_rule' => L10n::t('Add a Rule'), '$help' => L10n::t('Help'), - '$advanced_content_filter_intro' => addslashes(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.')), + '$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 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'), diff --git a/advancedcontentfilter/templates/settings.tpl b/advancedcontentfilter/templates/settings.tpl index b12df73c2..813dc9199 100644 --- a/advancedcontentfilter/templates/settings.tpl +++ b/advancedcontentfilter/templates/settings.tpl @@ -1,149 +1,98 @@
-
+
+

🔙 {{$backtosettings}}

+

+ {{$title}} + + + + +

+
{{$advanced_content_filter_intro}}
+

+ {{$your_rules}} + +

+
+ {{$no_rules}} +
+ +
    +
  • +

    + + + + + +

    +

    + {{$rule}} #{{ rule.id }}: {{ rule.name }} +

    +
    {{ rule.expression }}
    +
  • +
+ + + +
+
+ Show post variables +
+ + +
+ +
+
+
+{{ itemJson }}
+		
+
- +
diff --git a/advancedcontentfilter/templates/vue_dom.tpl b/advancedcontentfilter/templates/vue_dom.tpl deleted file mode 100644 index 24538b86d..000000000 --- a/advancedcontentfilter/templates/vue_dom.tpl +++ /dev/null @@ -1,96 +0,0 @@ - -
-

🔙 ##$backtosettings##

-

- ##$title## - - - - -

-
##$advanced_content_filter_intro##
-

- ##$your_rules## - -

-
- ##$no_rules## -
- - - - - -
-
- Show post variables -
- - -
- -
-
-
-{{ itemJson }}
-	
-
From d014fa3e28472fc6f0f7375904f182ec124fbd98 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sun, 5 Aug 2018 14:26:24 +0200 Subject: [PATCH 2/4] [advancedcontentfilter] Fix notice messages --- advancedcontentfilter/advancedcontentfilter.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/advancedcontentfilter/advancedcontentfilter.php b/advancedcontentfilter/advancedcontentfilter.php index 34d47425e..3012b3120 100644 --- a/advancedcontentfilter/advancedcontentfilter.php +++ b/advancedcontentfilter/advancedcontentfilter.php @@ -170,7 +170,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'; @@ -188,7 +188,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/'; From 6e07bf3d298cd10dd032d5247a01d408ebdabf45 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sun, 5 Aug 2018 14:26:48 +0200 Subject: [PATCH 3/4] [advancedcontentfilter] Move Vue template from DOM to compiled render functions (redux) --- .../advancedcontentfilter.js | 18 +++- .../advancedcontentfilter.php | 46 ++++----- advancedcontentfilter/templates/settings.tpl | 98 +++---------------- 3 files changed, 48 insertions(+), 114 deletions(-) diff --git a/advancedcontentfilter/advancedcontentfilter.js b/advancedcontentfilter/advancedcontentfilter.js index 67a2b8d24..e3c2da540 100644 --- a/advancedcontentfilter/advancedcontentfilter.js +++ b/advancedcontentfilter/advancedcontentfilter.js @@ -1,4 +1,4 @@ -$.ajaxSetup({headers: {'X-CSRF-Token': document.querySelector('#csrf').getAttribute('value')}}); +$.ajaxSetup({headers: {'X-CSRF-Token': csrfToken}}); $.extend({ ajaxJSON: function(method, url, data) { @@ -22,7 +22,9 @@ new Vue({ rule: {id: '', name: '', expression: '', created: ''}, rules: existingRules || [], itemUrl: '', - itemJson: '' + itemJson: '', + currentTheme: currentTheme, + messages: messages, }, watch: { @@ -118,5 +120,13 @@ new Vue({ return false; } - } -}); \ No newline at end of file + }, + + // These render functions have been compiled from templates/advancedcontentfilter.vue, check this file out for instructions + render: function () { + with(this){return _c('div',{attrs:{"id":"rules"}},[_c('p',[_c('a',{attrs:{"href":"settings/addon"}},[_v("🔙 "+_s(messages.backtosettings))])]),_c('h1',[_v(_s(messages.title)+" "),_c('a',{staticClass:"btn btn-default btn-sm",attrs:{"href":"advancedcontentfilter/help","title":messages.help}},[_c('i',{staticClass:"fa fa-question fa-2x",attrs:{"aria-hidden":"true"}})])]),_c('div',[_v(_s(messages.intro))]),_c('h2',[_v(_s(messages.your_rules)+" "),_c('button',{staticClass:"btn btn-primary btn-sm",attrs:{"title":messages.add_a_rule},on:{"click":function($event){showModal = true}}},[_c('i',{staticClass:"fa fa-plus fa-2x",attrs:{"aria-hidden":"true"}})])]),(rules.length === 0)?_c('div',{},[_v(_s(messages.no_rules))]):_e(),_c('ul',{staticClass:"list-group"},_l((rules),function(rule){return _c('li',{staticClass:"list-group-item"},[_c('p',{staticClass:"pull-right"},[(parseInt(rule.active))?_c('button',{staticClass:"btn btn-xs btn-primary",attrs:{"type":"button","aria-label":messages.disable_this_rule,"title":messages.disable_this_rule},on:{"click":function($event){toggleActive(rule)}}},[_c('i',{staticClass:"fa fa-toggle-on",attrs:{"aria-hidden":"true"}}),_v(" "+_s(messages.enabled))]):_c('button',{staticClass:"btn btn-xs btn-default",attrs:{"type":"button","aria-label":messages.enable_this_rule,"title":messages.enable_this_rule},on:{"click":function($event){toggleActive(rule)}}},[_c('i',{staticClass:"fa fa-toggle-off",attrs:{"aria-hidden":"true"}}),_v(" "+_s(messages.disabled))]),_v(" "),_c('button',{staticClass:"btn btn-xs btn-primary",attrs:{"type":"button","aria-label":messages.edit_this_rule,"title":messages.edit_this_rule},on:{"click":function($event){editRule(rule)}}},[_c('i',{staticClass:"fa fa-pencil",attrs:{"aria-hidden":"true"}})]),_v(" "),_c('button',{staticClass:"btn btn-xs btn-default",attrs:{"type":"button","aria-label":messages.delete_this_rule,"title":messages.delete_this_rule},on:{"click":function($event){deleteRule(rule)}}},[_c('i',{staticClass:"fa fa-trash-o",attrs:{"aria-hidden":"true"}})])]),_c('h3',{staticClass:"list-group-item-heading"},[_v(_s(messages.rule)+" #"+_s(rule.id)+": "+_s(rule.name))]),(rule.expression)?_c('pre',{staticClass:"list-group-item-text"},[_v(_s(rule.expression))]):_e()])})),_c('div',{ref:"vuemodal",staticClass:"modal fade",attrs:{"tabindex":"-1","role":"dialog"}},[_c('div',{staticClass:"modal-dialog",attrs:{"role":"document"}},[_c('div',{staticClass:"modal-content"},[_c('div',{staticClass:"modal-header"},[(currentTheme === 'frio')?_c('button',{staticClass:"close",attrs:{"type":"button","data-dismiss":"modal","aria-label":messages.close},on:{"click":function($event){showModal = false}}},[_c('span',{attrs:{"aria-hidden":"true"}},[_v("×")])]):_e(),(rule.id)?_c('h3',[_v(_s(messages.edit_the_rule)+" \""+_s(rule.name)+"\"")]):_e(),(!rule.id)?_c('h3',[_v(_s(messages.add_a_rule))]):_e()]),_c('div',{staticClass:"modal-body"},[_c('form',[(errorMessage)?_c('div',{staticClass:"alert alert-danger",attrs:{"role":"alert"}},[_v(_s(errorMessage))]):_e(),_c('div',{staticClass:"form-group"},[_c('input',{directives:[{name:"model",rawName:"v-model",value:(rule.name),expression:"rule.name"}],staticClass:"form-control",attrs:{"placeholder":messages.rule_name},domProps:{"value":(rule.name)},on:{"input":function($event){if($event.target.composing)return;$set(rule, "name", $event.target.value)}}})]),_c('div',{staticClass:"form-group"},[_c('input',{directives:[{name:"model",rawName:"v-model",value:(rule.expression),expression:"rule.expression"}],staticClass:"form-control",attrs:{"placeholder":messages.rule_expression},domProps:{"value":(rule.expression)},on:{"input":function($event){if($event.target.composing)return;$set(rule, "expression", $event.target.value)}}})])])]),_c('div',{staticClass:"modal-footer"},[_c('button',{staticClass:"btn btn-default",attrs:{"type":"button","data-dismiss":"modal","aria-label":"Close"},on:{"click":function($event){resetForm()}}},[_v(_s(messages.cancel))]),(rule.id)?_c('button',{staticClass:"btn btn-primary",attrs:{"slot":"button","type":"button"},on:{"click":function($event){saveRule(rule)}},slot:"button"},[_v(_s(messages.save_this_rule))]):_e(),(!rule.id)?_c('button',{staticClass:"btn btn-primary",attrs:{"slot":"button","type":"button"},on:{"click":function($event){addRule()}},slot:"button"},[_v(_s(messages.add_a_rule))]):_e()])])])]),_c('form',{staticClass:"form-inline",on:{"submit":function($event){$event.preventDefault();showVariables()}}},[_c('fieldset',[_c('legend',[_v("Show post variables")]),_c('div',{staticClass:"form-group",staticStyle:{"width":"50%"}},[_c('label',{staticClass:"sr-only",attrs:{"for":"itemUrl"}},[_v("Post URL or item guid")]),_c('input',{directives:[{name:"model",rawName:"v-model",value:(itemUrl),expression:"itemUrl"}],staticClass:"form-control",staticStyle:{"width":"100%"},attrs:{"id":"itemUrl","placeholder":"Post URL or item guid"},domProps:{"value":(itemUrl)},on:{"input":function($event){if($event.target.composing)return;itemUrl=$event.target.value}}})]),_c('button',{staticClass:"btn btn-primary",attrs:{"type":"submit"}},[_v("Show Variables")])])]),_c('pre',{},[_v(_s(itemJson))])])} + }, + + staticRenderFns: [ + ], +}); diff --git a/advancedcontentfilter/advancedcontentfilter.php b/advancedcontentfilter/advancedcontentfilter.php index 3012b3120..55a80a802 100644 --- a/advancedcontentfilter/advancedcontentfilter.php +++ b/advancedcontentfilter/advancedcontentfilter.php @@ -208,31 +208,31 @@ function advancedcontentfilter_content(App $a) } else { $t = get_markup_template('settings.tpl', 'addon/advancedcontentfilter/'); return replace_macros($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 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'), - '$examples' => L10n::t('

Examples:

  • author_link == \'https://friendica.mrpetovan.com/profile/hypolite\'
  • tags
'), - '$cancel' => L10n::t('Cancel'), '$rules' => advancedcontentfilter_get_rules(), - '$baseurl' => System::baseUrl(true), '$form_security_token' => get_form_security_token() ]); } diff --git a/advancedcontentfilter/templates/settings.tpl b/advancedcontentfilter/templates/settings.tpl index 813dc9199..a04baf7de 100644 --- a/advancedcontentfilter/templates/settings.tpl +++ b/advancedcontentfilter/templates/settings.tpl @@ -1,96 +1,20 @@
-
-

🔙 {{$backtosettings}}

-

- {{$title}} +
- - - -

-
{{$advanced_content_filter_intro}}
-

- {{$your_rules}} - -

-
- {{$no_rules}} -
+ + var currentTheme = "{{$current_theme}}"; + From 7947909c9cb6e75892bda1f485d6b57a1c7d376e Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sun, 5 Aug 2018 14:27:12 +0200 Subject: [PATCH 4/4] [advancedcontentfilter] Add new support DOM file (redux) --- .../templates/advancedcontentfilter.vue | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 advancedcontentfilter/templates/advancedcontentfilter.vue diff --git a/advancedcontentfilter/templates/advancedcontentfilter.vue b/advancedcontentfilter/templates/advancedcontentfilter.vue new file mode 100644 index 000000000..fded67a9d --- /dev/null +++ b/advancedcontentfilter/templates/advancedcontentfilter.vue @@ -0,0 +1,96 @@ + +
+

🔙 {{ messages.backtosettings }}

+

+ {{ messages.title }} +   + + + +

+
{{ messages.intro }}
+

+ {{ messages.your_rules }} +   + +

+
+ {{ messages.no_rules }} +
+ +
    +
  • +

    + + +   + +   + +

    +

    + {{ messages.rule }} #{{ rule.id }}: {{ rule.name }} +

    +
    {{ rule.expression }}
    +
  • +
+ + + +
+
+ Show post variables +
+ + +
+ +
+
+
+{{ itemJson }}
+	
+