Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.4 updates #8

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion AkismetHandler.inc.php → AkismetHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
* @ingroup plugins_generic_akismet
* @brief Handles controller requests for Akismet plugin.
*/
namespace APP\plugins\generic\akismet;

import('classes.handler.Handler');
use APP\handler\Handler;
use PKP\core\JSONMessage;
use PKP\security\Validation;
use PKP\plugins\PluginRegistry;

class AkismetHandler extends Handler {

Expand Down
93 changes: 41 additions & 52 deletions AkismetPlugin.inc.php → AkismetPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,26 @@
*
* @brief Akismet plugin class
*/
namespace APP\plugins\generic\akismet;

use PKP\plugins\GenericPlugin;
use PKP\config\Config;
use PKP\plugins\Hook;
use PKP\linkAction\LinkAction;
use PKP\linkAction\request\AjaxModal;
use PKP\core\JSONMessage;
use PKP\db\DAORegistry;
use PKP\linkAction\request\RemoteActionConfirmationModal;
use PKP\session\SessionManager;
use PKP\security\Validation;
use PKP\core\PKPApplication;
use Exception;
use APP\facades\Repo;
use APP\core\Application;
use APP\template\TemplateManager;
use APP\notification\NotificationManager;
use APP\plugins\generic\akismet\AkismetSettingsForm as AkismetSettingsForm;

import('lib.pkp.classes.plugins.GenericPlugin');
class AkismetPlugin extends GenericPlugin {

/**
Expand All @@ -34,15 +52,15 @@ function register($category, $path, $mainContextId = NULL) {
if ($success && $this->getEnabled()) {

// Enable Akismet anti-spam check of new registrations
HookRegistry::register('registrationform::validate', array(&$this, 'checkAkismet'));
HookRegistry::register('registrationform::execute', array(&$this, 'storeAkismetData'));
HookRegistry::register('userdao::getAdditionalFieldNames', array(&$this, 'addAkismetSetting'));
Hook::add('registrationform::validate', array(&$this, 'checkAkismet'));
Hook::add('registrationform::execute', array(&$this, 'storeAkismetData'));
Hook::add('userdao::getAdditionalFieldNames', array(&$this, 'addAkismetSetting'));
// Add a link to mark a missed spam user
HookRegistry::register('TemplateManager::fetch', array($this, 'templateFetchCallback'));
Hook::add('TemplateManager::fetch', array($this, 'templateFetchCallback'));
// Add a handler to process a missed spam user
HookRegistry::register('LoadComponentHandler', array($this, 'callbackLoadHandler'));
Hook::add('LoadComponentHandler', array($this, 'callbackLoadHandler'));
// Register callback to add text to registration page
HookRegistry::register('TemplateManager::display', array($this, 'handleTemplateDisplay'));
Hook::add('TemplateManager::display', array($this, 'handleTemplateDisplay'));
}
return $success;
}
Expand Down Expand Up @@ -94,7 +112,6 @@ function getDescription() {
*/
function getActions($request, $verb) {
$router = $request->getRouter();
import('lib.pkp.classes.linkAction.request.AjaxModal');
// Must be site administrator to access the settings option
return array_merge(
$this->getEnabled() && Validation::isSiteAdmin() ? array(
Expand All @@ -112,17 +129,14 @@ function getActions($request, $verb) {
*/
function manage($args, $request) {
$user = $request->getUser();
import('classes.notification.NotificationManager');
$notificationManager = new NotificationManager();
switch ($request->getUserVar('verb')) {
case 'settings':
if (!Validation::isSiteAdmin()) {
return new JSONMessage(false);
}
$templateMgr = TemplateManager::getManager();
$templateMgr->register_function('plugin_url', array(&$this, 'smartyPluginUrl'));

$this->import('AkismetSettingsForm');
$templateMgr =& TemplateManager::getManager($request);
$templateMgr->registerPlugin('function', 'plugin_url', array(&$this, 'smartyPluginUrl'));
$form = new AkismetSettingsForm($this);
if ($request->getUserVar('save')) {
$form->readInputData();
Expand Down Expand Up @@ -153,7 +167,7 @@ function checkAkismet($hookName, $args) {
$locales = $context->getSupportedFormLocaleNames();
}
// The Akismet API key is required
$apikey = $this->getSetting(CONTEXT_SITE, 'akismetKey');
$apikey = $this->getSetting(PKPApplication::CONTEXT_SITE, 'akismetKey');
if (empty($apikey)) {
return false;
}
Expand Down Expand Up @@ -214,8 +228,8 @@ function checkAkismet($hookName, $args) {
* @return array
*/
function getAkismetData($userId) {
$userdao = DAORegistry::getDAO('UserDAO');
$user = $userdao->getById($userId);
$userDAO = Repo::user()->dao;
$user = $userDAO->get($userId);
if (isset($user)) {
return $user->getData($this->getName()."::".$this->dataUserSetting);
}
Expand All @@ -227,11 +241,11 @@ function getAkismetData($userId) {
* @param $userId int User ID
*/
function unsetAkismetData($userId) {
$userdao = DAORegistry::getDAO('UserDAO');
$user = $userdao->getById($userId);
$userDAO = Repo::user()->dao;
$user = $userDAO->get($userId);
if (isset($user)) {
$user->setData($this->getName()."::".$this->dataUserSetting, '');
$userdao->updateObject($user);
Repo::user()->dao->update($user);
}
}

Expand All @@ -249,20 +263,17 @@ function storeAkismetData($hookName, $args) {
$sessionManager = SessionManager::getManager();
$session = $sessionManager->getUserSession();
$data = $session->getSessionVar($this->getName()."::".$this->dataUserSetting);
// Prior to 3.1.2 the user was passed as an argument
$user = $args[1];
// For 3.1.2 and 3.1.2-1, we need a hack:
if (!$user) {
$form = $args[0];
$username = $form->getData('username');
$userDao = DAORegistry::getDAO('UserDAO');
$userDAO = Repo::user()->dao;
$settingName = $this->getName()."::".$this->dataUserSetting;
$session->unsetSessionVar($this->getName()."::".$settingName);
// On shutdown, persist the Akismet setting to the new user account. (https://github.com/pkp/pkp-lib/issues/4601)
register_shutdown_function(function() use ($username, $userDao, $settingName, $data) {
$user = $userDao->getByUsername($username);
$user = $userDAO->getByUsername($username);
$user->setData($settingName, $data);
$userDao->updateObject($user);
$userDao->update($user);
});
}
break;
Expand Down Expand Up @@ -304,13 +315,7 @@ public function templateFetchCallback($hookName, $params) {
if ($resourceName === 'controllers/grid/gridRow.tpl') {
$templateMgr = $params[0];
// fetch the gridrow from the template
if (method_exists($templateMgr, 'getTemplateVars')) {
// Smarty 3
$row = $templateMgr->getTemplateVars('row');
} else {
// Smarty 2
$row = $templateMgr->get_template_vars('row');
}
$row = $templateMgr->getTemplateVars('row');
$data = $row ? $row->getData() : array();
// Is this a User grid?
if (is_a($data, 'User')) {
Expand All @@ -321,7 +326,6 @@ public function templateFetchCallback($hookName, $params) {
$user = $request->getUser();
// Is data present, and is the user able to administer this row?
if ($row->hasActions() && isset($akismetData) && !empty($akismetData) && Validation::canAdminister($userid, $user->getId())) {
import('lib.pkp.classes.linkAction.request.RemoteActionConfirmationModal');
$row->addAction(new LinkAction(
'flagAsSpam',
new RemoteActionConfirmationModal(
Expand All @@ -346,14 +350,8 @@ public function templateFetchCallback($hookName, $params) {
function handleTemplateDisplay($hookName, $args) {
$templateMgr = $args[0];
$template = $args[1];
if ($template === 'frontend/pages/userRegister.tpl' && $this->getSetting(CONTEXT_SITE, 'akismetPrivacyNotice')) {
if (method_exists($templateMgr, 'register_outputfilter')) {
// 3.1.1 and earlier (Smarty 2)
$templateMgr->register_outputfilter(array($this, 'registrationFilter'));
} else {
// 3.1.2 and later (Smarty 3)
$templateMgr->registerFilter('output', array($this, 'registrationFilter'));
}
if ($template === 'frontend/pages/userRegister.tpl' && $this->getSetting(PKPApplication::CONTEXT_SITE, 'akismetPrivacyNotice')) {
$templateMgr->registerFilter('output', array($this, 'registrationFilter'));
}
return false;
}
Expand All @@ -372,13 +370,7 @@ function registrationFilter($output, $templateMgr) {
$newOutput .= '<div id="akismetprivacy">'.__('plugins.generic.akismet.privacyNotice').'</div>';
$newOutput .= substr($output, $offset+strlen($match));
$output = $newOutput;
if (method_exists($templateMgr, 'unregister_outputfilter')) {
// 3.1.1 and earlier (Smarty 2)
$templateMgr->unregister_outputfilter('registrationFilter');
} else {
// 3.1.2 and later (Smarty 3)
$templateMgr->unregisterFilter('output', array($this, 'registrationFilter'));
}
$templateMgr->unregisterFilter('output', array($this, 'registrationFilter'));
}
return $output;
}
Expand Down Expand Up @@ -408,7 +400,7 @@ function _sendPayload($data, $flag = false) {
return false;
}
}
$akismetKey = $this->getSetting(CONTEXT_SITE, 'akismetKey');
$akismetKey = $this->getSetting(PKPApplication::CONTEXT_SITE, 'akismetKey');
if (empty($akismetKey)) {
return false;
}
Expand Down Expand Up @@ -441,15 +433,12 @@ function _sendPayload($data, $flag = false) {
*/
function getTemplatePath($inCore = false) {
$templatePath = parent::getTemplatePath($inCore);
// OJS 3.1.2 and later include the 'templates' directory, but no trailing slash
$templateDir = 'templates';
if (strlen($templatePath) >= strlen($templateDir)) {
if (substr_compare($templatePath, $templateDir, strlen($templatePath) - strlen($templateDir), strlen($templateDir)) === 0) {
return $templatePath;
}
}
// OJS 3.1.1 and earlier includes a trailing slash to the plugin path
return $templatePath . $templateDir . DIRECTORY_SEPARATOR;
}

}
19 changes: 11 additions & 8 deletions AkismetSettingsForm.inc.php → AkismetSettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
*
* @brief Form for site admins to modify Akismet plugin settings
*/


import('lib.pkp.classes.form.Form');
namespace APP\plugins\generic\akismet;
use PKP\form\Form;
use PKP\form\validation\FormValidator;
use PKP\form\validation\FormValidatorPost;
use PKP\core\PKPApplication;
use APP\template\TemplateManager;

class AkismetSettingsForm extends Form {

Expand All @@ -29,7 +32,7 @@ function __construct($plugin) {

parent::__construct(method_exists($plugin, 'getTemplateResource') ? $plugin->getTemplateResource('settingsForm.tpl') : $plugin->getTemplatePath() . 'settingsForm.tpl');

$this->addCheck(new FormValidator($this, 'akismetKey', FORM_VALIDATOR_REQUIRED_VALUE, 'plugins.generic.akismet.manager.settings.akismetKeyRequired'));
$this->addCheck(new FormValidator($this, 'akismetKey', FormValidator::FORM_VALIDATOR_REQUIRED_VALUE, 'plugins.generic.akismet.manager.settings.akismetKeyRequired'));
$this->addCheck(new FormValidatorPost($this));
}

Expand All @@ -39,8 +42,8 @@ function __construct($plugin) {
function initData() {
$plugin = $this->_plugin;

$this->setData('akismetKey', $plugin->getSetting(CONTEXT_SITE, 'akismetKey'));
$this->setData('akismetPrivacyNotice', $plugin->getSetting(CONTEXT_SITE, 'akismetPrivacyNotice'));
$this->setData('akismetKey', $plugin->getSetting(PKPApplication::CONTEXT_SITE, 'akismetKey'));
$this->setData('akismetPrivacyNotice', $plugin->getSetting(PKPApplication::CONTEXT_SITE, 'akismetPrivacyNotice'));
}

/**
Expand All @@ -55,8 +58,8 @@ function readInputData() {
*/
function execute(...$functionArgs) {
$plugin = $this->_plugin;
$plugin->updateSetting(CONTEXT_SITE, 'akismetKey', $this->getData('akismetKey'), 'string');
$plugin->updateSetting(CONTEXT_SITE, 'akismetPrivacyNotice', $this->getData('akismetPrivacyNotice'), 'bool');
$plugin->updateSetting(PKPApplication::CONTEXT_SITE, 'akismetKey', $this->getData('akismetKey'), 'string');
$plugin->updateSetting(PKPApplication::CONTEXT_SITE, 'akismetPrivacyNotice', $this->getData('akismetPrivacyNotice'), 'bool');
}

/**
Expand Down
20 changes: 0 additions & 20 deletions index.php

This file was deleted.

File renamed without changes.
File renamed without changes.
8 changes: 6 additions & 2 deletions markSpamUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
*
* @brief CLI tool for marking a user as missed spam by username within Akismet.
*/
namespace APP\plugins\generic\akismet;
use PKP\db\DAORegistry;
use PKP\plugins\PluginRegistry;
use PKP\cliTool\CommandLineTool;
use APP\facades\Repo;

require(dirname(dirname(dirname(dirname(__FILE__)))) . '/tools/bootstrap.inc.php');

Expand Down Expand Up @@ -49,8 +54,7 @@ function usage() {
*/
function execute() {
$plugin = PluginRegistry::getPlugin('generic', 'akismetplugin');
$userDao = DAORegistry::getDAO('UserDAO');

$userDAO = Repo::user()->dao;
$user = $userDao->getByUsername($this->username);

$userId = isset($user) ? $user->getId() : null;
Expand Down
2 changes: 1 addition & 1 deletion version.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<version>
<application>akismet</application>
<type>plugins.generic</type>
<release>1.2.3.2</release>
<release>1.3.0.0</release>
<date>2024-07-09</date>
<lazy-load>1</lazy-load>
<class>AkismetPlugin</class>
Expand Down