| Server IP : 195.134.90.114 / Your IP : 216.73.216.86 Web Server : Apache/2.4.58 System : Linux nepub 6.8.0-88-generic #89-Ubuntu SMP PREEMPT_DYNAMIC Sat Oct 11 01:02:46 UTC 2025 x86_64 User : www-data ( 33) PHP Version : 8.2.30 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/html/public_html/lib/pkp/classes/user/form/ |
Upload File : |
<?php
/**
* @file classes/user/form/APIProfileForm.inc.php
*
* Copyright (c) 2014-2021 Simon Fraser University
* Copyright (c) 2003-2021 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class APIProfileForm
* @ingroup user_form
*
* @brief Form to edit user's API key settings.
*/
use \Firebase\JWT\JWT;
import('lib.pkp.classes.user.form.BaseProfileForm');
class APIProfileForm extends BaseProfileForm {
/**
* Constructor.
* @param $user User
*/
public function __construct($user) {
parent::__construct('user/apiProfileForm.tpl', $user);
}
/**
* @copydoc Form::initData()
*/
public function initData() {
$user = $this->getUser();
$this->setData('apiKeyEnabled', (bool) $user->getData('apiKeyEnabled'));
}
/**
* Assign form data to user-submitted data.
*/
public function readInputData() {
parent::readInputData();
$this->readUserVars(array(
'apiKeyEnabled', 'generateApiKey',
));
}
/**
* Fetch the form to edit user's API key settings.
* @return string JSON-encoded form contents.
* @see BaseProfileForm::fetch
*/
public function fetch($request, $template = null, $display = false) {
$user = $request->getUser();
$secret = Config::getVar('security', 'api_key_secret', '');
if ($secret === '') {
$notificationManager = new NotificationManager();
$notificationManager->createTrivialNotification(
$user->getId(), NOTIFICATION_TYPE_WARNING, array(
'contents' => __('user.apiKey.secretRequired'),
));
} elseif ($user->getData('apiKey')) {
$templateMgr = TemplateManager::getManager($request);
$templateMgr->assign(array(
'apiKey' => JWT::encode($user->getData('apiKey'), $secret, 'HS256'),
));
}
return parent::fetch($request, $template, $display);
}
/**
* @copydoc Form::execute()
*/
function execute(...$functionArgs) {
$request = Application::get()->getRequest();
$user = $request->getUser();
$apiKeyEnabled = (bool) $this->getData('apiKeyEnabled');
$user->setData('apiKeyEnabled', $apiKeyEnabled);
// remove api key if exists
if (!$apiKeyEnabled) {
$user->setData('apiKeyEnabled', null);
}
// generate api key
if ($apiKeyEnabled && !is_null($this->getData('generateApiKey'))) {
$secret = Config::getVar('security', 'api_key_secret', '');
if ($secret) {
$user->setData('apiKey', sha1(time()));
}
}
parent::execute(...$functionArgs);
}
}