From 2dae243e99192f8de0b80e13597374a238e02306 Mon Sep 17 00:00:00 2001 From: nuxsmin Date: Wed, 27 Dec 2017 00:32:38 +0100 Subject: [PATCH] * [ADD] Finished access manager refactoring. Work in progress. * [MOD] Public links reworked --- ajax/ajax_getContent.php | 2 +- .../web/Controllers/AccountController.php | 19 +- .../web/Controllers/Helpers/HelperBase.php | 12 +- .../Controllers/Helpers/ItemsGridHelper.php | 12 +- .../web/Controllers/MainController.php | 10 +- .../web/Controllers/PublicLinkController.php | 9 +- .../views/_partials/error-full.inc | 2 +- .../material-blue/views/_partials/error.inc | 2 +- .../views/account/search-header.inc | 2 +- .../views/account/search-index.inc | 2 +- .../views/account/search-rows.inc | 2 +- .../views/account/search-searchbox.inc | 2 +- .../material-blue/views/config/general.inc | 2 +- .../views/grid/datagrid-nav-full.inc | 2 +- .../views/grid/datagrid-rows.inc | 5 +- .../views/grid/datatabs-grid.inc | 2 +- .../views/itemshow/publiclink.inc | 10 +- lib/BaseFunctions.php | 13 +- lib/SP/Bootstrap.php | 2 +- lib/SP/Controller/AccountController.php | 8 +- lib/SP/Controller/AccountSearchController.php | 4 +- lib/SP/Controller/ConfigController.php | 4 +- lib/SP/Controller/ControllerBase.php | 2 +- lib/SP/Controller/EventlogController.php | 4 +- lib/SP/Controller/Grids/Items.php | 6 +- lib/SP/Controller/ItemActionController.php | 16 +- lib/SP/Controller/ItemListController.php | 4 +- lib/SP/Controller/ItemSearchController.php | 2 +- lib/SP/Controller/ItemShowController.php | 2 +- lib/SP/Controller/NoticeShowController.php | 2 +- lib/SP/Controller/NoticesSearchController.php | 4 +- .../Controller/UserPreferencesController.php | 2 +- lib/SP/Core/Crypt/Session.php | 6 - lib/SP/Core/Crypt/Vault.php | 10 +- lib/SP/DataModel/PublicLinkData.php | 281 +++++----- lib/SP/DataModel/PublicLinkListData.php | 126 +---- lib/SP/Forms/PublicLinkForm.php | 10 +- lib/SP/Html/DataGrid/DataGridDataBase.php | 15 +- .../Html/DataGrid/DataGridDataInterface.php | 15 +- lib/SP/Mgmt/PublicLinks/PublicLink.php | 26 +- lib/SP/{Core => Mvc/View}/Template.php | 2 +- .../Services/PublicLink/PublicLinkService.php | 496 +++++++++--------- lib/SP/Util/ErrorUtil.php | 16 +- 43 files changed, 538 insertions(+), 637 deletions(-) rename lib/SP/{Core => Mvc/View}/Template.php (99%) diff --git a/ajax/ajax_getContent.php b/ajax/ajax_getContent.php index fb26eef8..b1e3341d 100644 --- a/ajax/ajax_getContent.php +++ b/ajax/ajax_getContent.php @@ -31,7 +31,7 @@ use SP\Controller\NoticesController; use SP\Controller\UserPreferencesController; use SP\Core\Acl\ActionsInterface; use SP\Core\SessionFactory; -use SP\Core\Template; +use SP\Mvc\View\Template; use SP\Http\Request; use SP\Http\Response; use SP\Util\Util; diff --git a/app/modules/web/Controllers/AccountController.php b/app/modules/web/Controllers/AccountController.php index 403abeaf..392e568b 100644 --- a/app/modules/web/Controllers/AccountController.php +++ b/app/modules/web/Controllers/AccountController.php @@ -28,6 +28,7 @@ use SP\Controller\ControllerBase; use SP\Core\Acl\Acl; use SP\Core\Acl\ActionsInterface; use SP\Core\Crypt\Crypt; +use SP\Core\Crypt\Vault; use SP\Core\Exceptions\SPException; use SP\Core\Exceptions\ValidationException; use SP\Core\SessionUtil; @@ -162,8 +163,8 @@ class AccountController extends ControllerBase implements CrudControllerInterfac $publicLinkService = new PublicLinkService(); $publicLinkData = $publicLinkService->getByHash($hash); - if (time() < $publicLinkData->getDateExpire() - && $publicLinkData->getCountViews() < $publicLinkData->getMaxCountViews() + if (time() < $publicLinkData->getPublicLinkDateExpire() + && $publicLinkData->getPublicLinkCountViews() < $publicLinkData->getPublicLinkMaxCountViews() ) { $publicLinkService->addLinkView($publicLinkData); @@ -180,20 +181,20 @@ class AccountController extends ControllerBase implements CrudControllerInterfac // $NoticeData->setNoticeComponent(__('Cuentas')); // $NoticeData->setNoticeDescription($Message); // $NoticeData->setNoticeType(__('Información')); -// $NoticeData->setNoticeUserId($PublicLink->getUserId()); +// $NoticeData->setNoticeUserId($PublicLink->getPublicLinkUserId()); // // Notice::getItem($NoticeData)->add(); // } $accountService = new AccountService(); - $accountService->incrementViewCounter($publicLinkData->getItemId()); - $accountService->incrementDecryptCounter($publicLinkData->getItemId()); + $accountService->incrementViewCounter($publicLinkData->getPublicLinkItemId()); + $accountService->incrementDecryptCounter($publicLinkData->getPublicLinkItemId()); - $key = $this->configData->getPasswordSalt() . $publicLinkData->getLinkHash(); - $securedKey = Crypt::unlockSecuredKey($publicLinkData->getPassIV(), $key); + /** @var Vault $vault */ + $vault = unserialize($publicLinkData->getPublicLinkData()); /** @var AccountExtData $accountData */ - $accountData = Util::unserialize(AccountExtData::class, Crypt::decrypt($publicLinkData->getData(), $securedKey, $key)); + $accountData = Util::unserialize(AccountExtData::class, $vault->getData(PublicLinkService::getKeyForHash($this->config, $publicLinkData))); $this->view->assign('title', [ @@ -221,7 +222,7 @@ class AccountController extends ControllerBase implements CrudControllerInterfac } catch (\Exception $e) { debugLog($e->getMessage(), true); - ErrorUtil::showErrorInView($this->view, ErrorUtil::ERR_EXCEPTION); + ErrorUtil::showErrorFull($this->view, ErrorUtil::ERR_PAGE_NO_PERMISSION, 'account-link'); } $this->view(); diff --git a/app/modules/web/Controllers/Helpers/HelperBase.php b/app/modules/web/Controllers/Helpers/HelperBase.php index ecdd5798..c930aa2c 100644 --- a/app/modules/web/Controllers/Helpers/HelperBase.php +++ b/app/modules/web/Controllers/Helpers/HelperBase.php @@ -28,8 +28,8 @@ use SP\Config\Config; use SP\Config\ConfigData; use SP\Core\Events\EventDispatcher; use SP\Core\Session\Session; -use SP\Core\Template; use SP\Core\Traits\InjectableTrait; +use SP\Mvc\View\Template; /** * Class HelperBase @@ -41,7 +41,7 @@ abstract class HelperBase use InjectableTrait; /** - * @var Template + * @var \SP\Mvc\View\Template */ protected $view; /** @@ -64,10 +64,10 @@ abstract class HelperBase /** * Constructor * - * @param Template $template - * @param Config $config - * @param Session $session - * @param EventDispatcher $eventDispatcher + * @param \SP\Mvc\View\Template $template + * @param Config $config + * @param Session $session + * @param EventDispatcher $eventDispatcher */ final public function __construct(Template $template, Config $config, Session $session, EventDispatcher $eventDispatcher) { diff --git a/app/modules/web/Controllers/Helpers/ItemsGridHelper.php b/app/modules/web/Controllers/Helpers/ItemsGridHelper.php index 4092046e..8bb7eb8b 100644 --- a/app/modules/web/Controllers/Helpers/ItemsGridHelper.php +++ b/app/modules/web/Controllers/Helpers/ItemsGridHelper.php @@ -940,12 +940,12 @@ class ItemsGridHelper extends HelperBase // Grid Data $GridData = new DataGridData(); $GridData->setDataRowSourceId('publicLink_id'); - $GridData->addDataRowSource('accountName'); - $GridData->addDataRowSource('dateAdd'); - $GridData->addDataRowSource('dateExpire'); - $GridData->addDataRowSource('userLogin'); - $GridData->addDataRowSource('notify'); - $GridData->addDataRowSource('countViews'); + $GridData->addDataRowSource('account_name'); + $GridData->addDataRowSource('getDateAddFormat', true); + $GridData->addDataRowSource('getDateExpireFormat', true); + $GridData->addDataRowSource('user_login'); + $GridData->addDataRowSource('getNotifyString', true); + $GridData->addDataRowSource('getCountViewsString', true); $GridData->setData($data); // Grid diff --git a/app/modules/web/Controllers/MainController.php b/app/modules/web/Controllers/MainController.php index ac1f1b6e..a9b23c42 100644 --- a/app/modules/web/Controllers/MainController.php +++ b/app/modules/web/Controllers/MainController.php @@ -39,7 +39,7 @@ use SP\Core\Plugin\PluginUtil; use SP\Core\SessionFactory; use SP\Core\SessionUtil; use SP\Core\Task; -use SP\Core\Template; +use SP\Mvc\View\Template; use SP\Core\Upgrade\Check; use SP\DataModel\NoticeData; use SP\Html\DataGrid\DataGridAction; @@ -620,14 +620,14 @@ class MainController extends ControllerBase implements ActionsInterface $PublicLink = PublicLink::getItem()->getByHash($hash); if (!$PublicLink - || time() > $PublicLink->getDateExpire() - || $PublicLink->getCountViews() >= $PublicLink->getMaxCountViews() + || time() > $PublicLink->getPublicLinkDateExpire() + || $PublicLink->getPublicLinkCountViews() >= $PublicLink->getPublicLinkMaxCountViews() ) { $this->showError(self::ERR_PAGE_NO_PERMISSION, false); } else { PublicLink::getItem($PublicLink)->addLinkView(); - if ($PublicLink->isNotify()) { + if ($PublicLink->isPublicLinkNotify()) { $Message = new NoticeMessage(); $Message->setTitle(__('Enlace visualizado')); $Message->addDescription(sprintf('%s : %s', __('Cuenta'), $PublicLink->getItemId())); @@ -640,7 +640,7 @@ class MainController extends ControllerBase implements ActionsInterface $NoticeData->setNoticeComponent(__('Cuentas')); $NoticeData->setNoticeDescription($Message); $NoticeData->setNoticeType(__('Información')); - $NoticeData->setNoticeUserId($PublicLink->getUserId()); + $NoticeData->setNoticeUserId($PublicLink->getPublicLinkUserId()); Notice::getItem($NoticeData)->add(); } diff --git a/app/modules/web/Controllers/PublicLinkController.php b/app/modules/web/Controllers/PublicLinkController.php index 68362c4c..3d49325a 100644 --- a/app/modules/web/Controllers/PublicLinkController.php +++ b/app/modules/web/Controllers/PublicLinkController.php @@ -36,6 +36,7 @@ use SP\DataModel\PublicLinkListData; use SP\Forms\PublicLinkForm; use SP\Http\JsonResponse; use SP\Http\Request; +use SP\Mgmt\PublicLinks\PublicLink; use SP\Modules\Web\Controllers\Helpers\ItemsGridHelper; use SP\Modules\Web\Controllers\Traits\ItemTrait; use SP\Modules\Web\Controllers\Traits\JsonTrait; @@ -59,8 +60,6 @@ class PublicLinkController extends ControllerBase implements CrudControllerInter /** * Search action - * - * @throws \SP\Core\Exceptions\InvalidClassException */ public function searchAction() { @@ -116,16 +115,16 @@ class PublicLinkController extends ControllerBase implements CrudControllerInter { $this->view->addTemplate('publiclink', 'itemshow'); - $publicLink = $publicLinkId ? PublicLinkService::mapItemsForList([$this->publicLinkService->getById($publicLinkId)]) : new PublicLinkListData(); + $publicLink = $publicLinkId ? $this->publicLinkService->getById($publicLinkId) : new PublicLinkListData(); - $this->view->assign('publicLink', is_array($publicLink) ? $publicLink[0] : $publicLink); + $this->view->assign('publicLink', $publicLink); $this->view->assign('accounts', AccountUtil::getAccountsForUser($this->session)); $this->view->assign('sk', SessionUtil::getSessionKey(true)); $this->view->assign('nextAction', Acl::getActionRoute(ActionsInterface::ACCESS_MANAGE)); if ($this->view->isView === true) { - $this->view->assign('publicLinkURL', PublicLinkService::getLinkForHash($publicLink[0]->getPublicLinkHash())); + $this->view->assign('publicLinkURL', PublicLinkService::getLinkForHash($publicLink->getPublicLinkHash())); $this->view->assign('disabled', 'disabled'); $this->view->assign('readonly', 'readonly'); } else { diff --git a/app/modules/web/themes/material-blue/views/_partials/error-full.inc b/app/modules/web/themes/material-blue/views/_partials/error-full.inc index 1919440c..91e277da 100644 --- a/app/modules/web/themes/material-blue/views/_partials/error-full.inc +++ b/app/modules/web/themes/material-blue/views/_partials/error-full.inc @@ -1,6 +1,6 @@
- includePartial('error'); ?>
\ No newline at end of file diff --git a/app/modules/web/themes/material-blue/views/_partials/error.inc b/app/modules/web/themes/material-blue/views/_partials/error.inc index 62a56e1c..7d8b9650 100644 --- a/app/modules/web/themes/material-blue/views/_partials/error.inc +++ b/app/modules/web/themes/material-blue/views/_partials/error.inc @@ -24,7 +24,7 @@ /** * @var $icons \SP\Core\UI\ThemeIconsBase - * @var \SP\Core\Template $this + * @var \SP\Mvc\View\Template $this */ ?>
diff --git a/app/modules/web/themes/material-blue/views/account/search-header.inc b/app/modules/web/themes/material-blue/views/account/search-header.inc index fff4248f..6ee5ae3e 100644 --- a/app/modules/web/themes/material-blue/views/account/search-header.inc +++ b/app/modules/web/themes/material-blue/views/account/search-header.inc @@ -2,7 +2,7 @@ /** * @var $data SP\Html\DataGrid\DataGridTab * @var $account SP\Account\AccountsSearchItem - * @var $this SP\Core\Template + * @var $this \SP\Mvc\View\Template */ ?>
diff --git a/app/modules/web/themes/material-blue/views/account/search-index.inc b/app/modules/web/themes/material-blue/views/account/search-index.inc index 81ed167f..137c157d 100644 --- a/app/modules/web/themes/material-blue/views/account/search-index.inc +++ b/app/modules/web/themes/material-blue/views/account/search-index.inc @@ -2,7 +2,7 @@ /** * @var $data SP\Html\DataGrid\DataGridTab * @var $account SP\Account\AccountsSearchItem - * @var $this SP\Core\Template + * @var $this \SP\Mvc\View\Template */ ?> diff --git a/app/modules/web/themes/material-blue/views/account/search-rows.inc b/app/modules/web/themes/material-blue/views/account/search-rows.inc index 804aa06e..6f579c5e 100644 --- a/app/modules/web/themes/material-blue/views/account/search-rows.inc +++ b/app/modules/web/themes/material-blue/views/account/search-rows.inc @@ -2,7 +2,7 @@ /** * @var $data SP\Html\DataGrid\DataGridTab * @var $AccountSearchItem SP\Account\AccountsSearchItem - * @var $this SP\Core\Template + * @var $this \SP\Mvc\View\Template * @var $action \SP\Html\DataGrid\DataGridActionBase * @var $actionMenu \SP\Html\DataGrid\DataGridActionBase * @var $icons \SP\Core\UI\ThemeIconsBase diff --git a/app/modules/web/themes/material-blue/views/account/search-searchbox.inc b/app/modules/web/themes/material-blue/views/account/search-searchbox.inc index 1fda6e92..9cf2e3d2 100644 --- a/app/modules/web/themes/material-blue/views/account/search-searchbox.inc +++ b/app/modules/web/themes/material-blue/views/account/search-searchbox.inc @@ -2,7 +2,7 @@ /** * @var $data SP\Html\DataGrid\DataGridTab * @var $account SP\Account\AccountsSearchItem - * @var $this SP\Core\Template + * @var $this \SP\Mvc\View\Template * @var $icons \SP\Core\UI\ThemeIconsBase */ ?> diff --git a/app/modules/web/themes/material-blue/views/config/general.inc b/app/modules/web/themes/material-blue/views/config/general.inc index 01c9ea52..d38d181e 100644 --- a/app/modules/web/themes/material-blue/views/config/general.inc +++ b/app/modules/web/themes/material-blue/views/config/general.inc @@ -1,7 +1,7 @@
diff --git a/app/modules/web/themes/material-blue/views/grid/datagrid-nav-full.inc b/app/modules/web/themes/material-blue/views/grid/datagrid-nav-full.inc index 35b580e0..f499caef 100644 --- a/app/modules/web/themes/material-blue/views/grid/datagrid-nav-full.inc +++ b/app/modules/web/themes/material-blue/views/grid/datagrid-nav-full.inc @@ -1,7 +1,7 @@
diff --git a/app/modules/web/themes/material-blue/views/grid/datagrid-rows.inc b/app/modules/web/themes/material-blue/views/grid/datagrid-rows.inc index 6ec46042..c19c1000 100644 --- a/app/modules/web/themes/material-blue/views/grid/datagrid-rows.inc +++ b/app/modules/web/themes/material-blue/views/grid/datagrid-rows.inc @@ -1,7 +1,7 @@ {$data->getData()->getDataRowSourceId()}; ?>" data-activetab=""> getData()->getDataRowSources() as $rowSrc): ?> - {$rowSrc} !== '') ? Html::truncate($dataItem->{$rowSrc}, 100 / $numFields) : ' '; // Fix height ?> + {$rowSrc['name']}() : $dataItem->{$rowSrc['name']}; ?> + getData()->getDataRowSourcesWithIcon()) > 0): ?> diff --git a/app/modules/web/themes/material-blue/views/grid/datatabs-grid.inc b/app/modules/web/themes/material-blue/views/grid/datatabs-grid.inc index d3f7cfaa..79cf16b8 100644 --- a/app/modules/web/themes/material-blue/views/grid/datatabs-grid.inc +++ b/app/modules/web/themes/material-blue/views/grid/datatabs-grid.inc @@ -4,7 +4,7 @@ /** * @var $icons SP\Core\UI\ThemeIconsBase * @var $data SP\Html\DataGrid\DataGridTab - * @var $this SP\Core\Template + * @var $this \SP\Mvc\View\Template * @var $action SP\Html\DataGrid\DataGridAction|SP\Html\DataGrid\DataGridActionSearch */ ?> diff --git a/app/modules/web/themes/material-blue/views/itemshow/publiclink.inc b/app/modules/web/themes/material-blue/views/itemshow/publiclink.inc index cd9b238d..1672f45d 100644 --- a/app/modules/web/themes/material-blue/views/itemshow/publiclink.inc +++ b/app/modules/web/themes/material-blue/views/itemshow/publiclink.inc @@ -34,7 +34,7 @@ title=""> getPublicLinkLinkData()->isNotify() ? 'CHECKED' : ''; ?> /> + name="notify" isPublicLinkNotify() ? 'CHECKED' : ''; ?> /> @@ -45,7 +45,7 @@
- getDateAdd(); ?> + getDateAddFormat(); ?> @@ -53,7 +53,7 @@
- getDateExpire(); ?> + getDateExpireFormat(); ?> @@ -69,7 +69,7 @@
- getCountViews(); ?> + getCountViewsString(); ?> @@ -95,7 +95,7 @@
    - getUseInfo() as $useInfo): ?> + getPublicLinkUseInfo() as $useInfo): ?>
  • access_time diff --git a/lib/BaseFunctions.php b/lib/BaseFunctions.php index bba83b10..cc39d132 100644 --- a/lib/BaseFunctions.php +++ b/lib/BaseFunctions.php @@ -54,14 +54,15 @@ function debugLog($data, $printLastCaller = false) function formatTrace($trace) { $btLine = []; - $n = count($trace); + $i = 0; - for ($i = 1; $i <= $n - 1; $i++) { - $class = isset($trace[$i]['class']) ? $trace[$i]['class'] : ''; - $file = isset($trace[$i]['file']) ? $trace[$i]['file'] : ''; - $line = isset($trace[$i]['line']) ? $trace[$i]['line'] : 0; + foreach ($trace as $caller) { + $class = isset($caller['class']) ? $caller['class'] : ''; + $file = isset($caller['file']) ? $caller['file'] : ''; + $line = isset($caller['line']) ? $caller['line'] : 0; - $btLine[] = sprintf('Caller %d: %s\%s (%s:%d)', $i, $class, $trace[$i]['function'], $file, $line); + $btLine[] = sprintf('Caller %d: %s\%s (%s:%d)', $i, $class, $caller['function'], $file, $line); + $i++; } return implode(PHP_EOL, $btLine); diff --git a/lib/SP/Bootstrap.php b/lib/SP/Bootstrap.php index c6cdc85f..b8b96fca 100644 --- a/lib/SP/Bootstrap.php +++ b/lib/SP/Bootstrap.php @@ -44,7 +44,7 @@ use SP\Core\Plugin\PluginUtil; use SP\Core\Session\Session; use SP\Core\SessionFactory; use SP\Core\SessionUtil; -use SP\Core\Template; +use SP\Mvc\View\Template; use SP\Core\Traits\InjectableTrait; use SP\Core\UI\Theme; use SP\Core\Upgrade\Upgrade; diff --git a/lib/SP/Controller/AccountController.php b/lib/SP/Controller/AccountController.php index ed23309b..04e77a14 100644 --- a/lib/SP/Controller/AccountController.php +++ b/lib/SP/Controller/AccountController.php @@ -37,7 +37,7 @@ use SP\Core\Exceptions\SPException; use SP\Core\Init; use SP\Core\SessionFactory; use SP\Core\SessionUtil; -use SP\Core\Template; +use SP\Mvc\View\Template; use SP\DataModel\AccountExtData; use SP\DataModel\CustomFieldData; use SP\DataModel\PublicLinkData; @@ -80,8 +80,8 @@ class AccountController extends ControllerBase implements ActionsInterface /** * Constructor * - * @param Template $template instancia del motor de plantillas - * @param int $accountId int con el id de la cuenta + * @param \SP\Mvc\View\Template $template instancia del motor de plantillas + * @param int $accountId int con el id de la cuenta * @internal param int $lastAction int con la última acción realizada */ public function __construct(Template $template = null, $accountId = null) @@ -138,7 +138,7 @@ class AccountController extends ControllerBase implements ActionsInterface $Account->incrementViewCounter($PublicLinkData->getItemId()); $Account->incrementDecryptCounter($PublicLinkData->getItemId()); - $key = $this->configData->getPasswordSalt() . $PublicLinkData->getLinkHash(); + $key = $this->configData->getPasswordSalt() . $PublicLinkData->getPublicLinkLinkHash(); $securedKey = Crypt::unlockSecuredKey($PublicLinkData->getPassIV(), $key); /** @var AccountExtData $AccountData */ diff --git a/lib/SP/Controller/AccountSearchController.php b/lib/SP/Controller/AccountSearchController.php index 6762f277..f131821b 100644 --- a/lib/SP/Controller/AccountSearchController.php +++ b/lib/SP/Controller/AccountSearchController.php @@ -32,7 +32,7 @@ use SP\Core\Acl\ActionsInterface; use SP\Core\Exceptions\SPException; use SP\Core\SessionFactory; use SP\Core\SessionUtil; -use SP\Core\Template; +use SP\Mvc\View\Template; use SP\Html\DataGrid\DataGrid; use SP\Html\DataGrid\DataGridAction; use SP\Html\DataGrid\DataGridActionSearch; @@ -79,7 +79,7 @@ class AccountSearchController extends ControllerBase implements ActionsInterface /** * Constructor * - * @param $template Template con instancia de plantilla + * @param $template \SP\Mvc\View\Template con instancia de plantilla */ public function __construct(Template $template = null) { diff --git a/lib/SP/Controller/ConfigController.php b/lib/SP/Controller/ConfigController.php index 7940abe4..35fec230 100644 --- a/lib/SP/Controller/ConfigController.php +++ b/lib/SP/Controller/ConfigController.php @@ -40,7 +40,7 @@ use SP\Core\Plugin\PluginUtil; use SP\Core\SessionFactory; use SP\Core\SessionUtil; use SP\Core\Task; -use SP\Core\Template; +use SP\Mvc\View\Template; use SP\Mgmt\Groups\Group; use SP\Mgmt\Profiles\Profile; use SP\Mgmt\Users\User; @@ -67,7 +67,7 @@ class ConfigController extends ControllerBase implements ActionsInterface /** * Constructor * - * @param $template Template con instancia de plantilla + * @param $template \SP\Mvc\View\Template con instancia de plantilla */ public function __construct(Template $template = null) { diff --git a/lib/SP/Controller/ControllerBase.php b/lib/SP/Controller/ControllerBase.php index 1d8bb8b1..b314190b 100644 --- a/lib/SP/Controller/ControllerBase.php +++ b/lib/SP/Controller/ControllerBase.php @@ -32,7 +32,6 @@ use SP\Core\Acl\Acl; use SP\Core\Events\EventDispatcher; use SP\Core\Exceptions\FileNotFoundException; use SP\Core\Session\Session; -use SP\Core\Template; use SP\Core\Traits\InjectableTrait; use SP\Core\UI\Theme; use SP\Core\UI\ThemeIconsBase; @@ -40,6 +39,7 @@ use SP\DataModel\ProfileData; use SP\DataModel\UserData; use SP\Http\JsonResponse; use SP\Http\Request; +use SP\Mvc\View\Template; use SP\Storage\Database; use SP\Util\Checks; use SP\Util\Json; diff --git a/lib/SP/Controller/EventlogController.php b/lib/SP/Controller/EventlogController.php index cf920d8e..3e22640c 100644 --- a/lib/SP/Controller/EventlogController.php +++ b/lib/SP/Controller/EventlogController.php @@ -28,7 +28,7 @@ defined('APP_ROOT') || die(); use SP\Core\Acl\ActionsInterface; use SP\Core\SessionUtil; -use SP\Core\Template; +use SP\Mvc\View\Template; use SP\Html\DataGrid\DataGridActionSearch; use SP\Html\DataGrid\DataGridActionType; use SP\Html\DataGrid\DataGridPager; @@ -56,7 +56,7 @@ class EventlogController extends ControllerBase implements ActionsInterface /** * Constructor * - * @param $template Template con instancia de plantilla + * @param $template \SP\Mvc\View\Template con instancia de plantilla */ public function __construct(Template $template = null) { diff --git a/lib/SP/Controller/Grids/Items.php b/lib/SP/Controller/Grids/Items.php index 0c7b25a9..50675527 100644 --- a/lib/SP/Controller/Grids/Items.php +++ b/lib/SP/Controller/Grids/Items.php @@ -840,11 +840,11 @@ class Items extends GridBase $GridData = new DataGridData(); $GridData->setDataRowSourceId('publicLink_id'); $GridData->addDataRowSource('accountName'); - $GridData->addDataRowSource('dateAdd'); - $GridData->addDataRowSource('dateExpire'); + $GridData->addDataRowSource('publicLink_dateAdd'); + $GridData->addDataRowSource('publicLink_dateExpire'); $GridData->addDataRowSource('userLogin'); $GridData->addDataRowSource('notify'); - $GridData->addDataRowSource('countViews'); + $GridData->addDataRowSource('publicLink_countViews'); // Grid $Grid = new DataGridTab(); diff --git a/lib/SP/Controller/ItemActionController.php b/lib/SP/Controller/ItemActionController.php index 04103658..79ed20b5 100644 --- a/lib/SP/Controller/ItemActionController.php +++ b/lib/SP/Controller/ItemActionController.php @@ -722,8 +722,8 @@ class ItemActionController implements ItemControllerInterface { $PublicLinkData = new PublicLinkData(); $PublicLinkData->setPublicLinkItemId($this->itemId); - $PublicLinkData->setTypeId(PublicLink::TYPE_ACCOUNT); - $PublicLinkData->setNotify(Request::analyze('notify', false, false, true)); + $PublicLinkData->setPublicLinkTypeId(PublicLink::TYPE_ACCOUNT); + $PublicLinkData->setPublicLinkNotify(Request::analyze('notify', false, false, true)); switch ($this->actionId) { case ActionsInterface::PUBLICLINK_CREATE: @@ -732,9 +732,9 @@ class ItemActionController implements ItemControllerInterface $this->LogMessage->setAction(__('Crear Enlace', false)); $this->LogMessage->addDescription(__('Enlace creado', false)); - $this->LogMessage->addDetails(__('Tipo', false), $PublicLinkData->getTypeId()); + $this->LogMessage->addDetails(__('Tipo', false), $PublicLinkData->getPublicLinkTypeId()); $this->LogMessage->addDetails(__('Cuenta', false), AccountUtil::getAccountNameById($PublicLinkData->getItemId())); - $this->LogMessage->addDetails(__('Usuario', false), UserUtil::getUserLoginById($PublicLinkData->getUserId())); + $this->LogMessage->addDetails(__('Usuario', false), UserUtil::getUserLoginById($PublicLinkData->getPublicLinkUserId())); break; case ActionsInterface::PUBLICLINK_REFRESH: $PublicLinkData = PublicLink::getItem()->getById($this->itemId); @@ -742,9 +742,9 @@ class ItemActionController implements ItemControllerInterface $this->LogMessage->setAction(__('Actualizar Enlace', false)); $this->LogMessage->addDescription(__('Enlace actualizado', false)); - $this->LogMessage->addDetails(__('Tipo', false), $PublicLinkData->getTypeId()); + $this->LogMessage->addDetails(__('Tipo', false), $PublicLinkData->getPublicLinkTypeId()); $this->LogMessage->addDetails(__('Cuenta', false), AccountUtil::getAccountNameById($PublicLinkData->getItemId())); - $this->LogMessage->addDetails(__('Usuario', false), UserUtil::getUserLoginById($PublicLinkData->getUserId())); + $this->LogMessage->addDetails(__('Usuario', false), UserUtil::getUserLoginById($PublicLinkData->getPublicLinkUserId())); break; case ActionsInterface::PUBLICLINK_DELETE: if (is_array($this->itemId)) { @@ -757,9 +757,9 @@ class ItemActionController implements ItemControllerInterface PublicLink::getItem()->delete($this->itemId); $this->LogMessage->addDescription(__('Enlace eliminado', false)); - $this->LogMessage->addDetails(__('Tipo', false), $PublicLinkData->getTypeId()); + $this->LogMessage->addDetails(__('Tipo', false), $PublicLinkData->getPublicLinkTypeId()); $this->LogMessage->addDetails(__('Cuenta', false), AccountUtil::getAccountNameById($PublicLinkData->getItemId())); - $this->LogMessage->addDetails(__('Usuario', false), UserUtil::getUserLoginById($PublicLinkData->getUserId())); + $this->LogMessage->addDetails(__('Usuario', false), UserUtil::getUserLoginById($PublicLinkData->getPublicLinkUserId())); } $this->LogMessage->setAction(__('Eliminar Enlace', false)); diff --git a/lib/SP/Controller/ItemListController.php b/lib/SP/Controller/ItemListController.php index ec1dc2c3..00b1af62 100644 --- a/lib/SP/Controller/ItemListController.php +++ b/lib/SP/Controller/ItemListController.php @@ -32,7 +32,7 @@ use SP\Config\Config; use SP\Controller\Grids\Items; use SP\Core\Acl\ActionsInterface; use SP\Core\Exceptions\SPException; -use SP\Core\Template; +use SP\Mvc\View\Template; use SP\Core\Traits\InjectableTrait; use SP\DataModel\ItemSearchData; use SP\Http\Request; @@ -68,7 +68,7 @@ class ItemListController extends GridTabControllerBase implements ActionsInterfa /** * Constructor * - * @param $template Template con instancia de plantilla + * @param $template \SP\Mvc\View\Template con instancia de plantilla */ public function __construct(Template $template = null) { diff --git a/lib/SP/Controller/ItemSearchController.php b/lib/SP/Controller/ItemSearchController.php index 3536dcc7..65cdfa5f 100644 --- a/lib/SP/Controller/ItemSearchController.php +++ b/lib/SP/Controller/ItemSearchController.php @@ -32,7 +32,7 @@ use SP\Config\Config; use SP\Controller\Grids\Items; use SP\Core\Acl\ActionsInterface; use SP\Core\SessionUtil; -use SP\Core\Template; +use SP\Mvc\View\Template; use SP\DataModel\ItemSearchData; use SP\Http\Request; use SP\Mgmt\ApiTokens\ApiTokenSearch; diff --git a/lib/SP/Controller/ItemShowController.php b/lib/SP/Controller/ItemShowController.php index 330b91da..06b6bdd4 100644 --- a/lib/SP/Controller/ItemShowController.php +++ b/lib/SP/Controller/ItemShowController.php @@ -36,7 +36,7 @@ use SP\Core\Exceptions\ItemException; use SP\Core\Plugin\PluginUtil; use SP\Core\SessionFactory; use SP\Core\SessionUtil; -use SP\Core\Template; +use SP\Mvc\View\Template; use SP\DataModel\AccountExtData; use SP\DataModel\ApiTokenData; use SP\DataModel\CategoryData; diff --git a/lib/SP/Controller/NoticeShowController.php b/lib/SP/Controller/NoticeShowController.php index d171a5f1..94eaf0c2 100644 --- a/lib/SP/Controller/NoticeShowController.php +++ b/lib/SP/Controller/NoticeShowController.php @@ -28,7 +28,7 @@ defined('APP_ROOT') || die(); use SP\Core\Acl\ActionsInterface; use SP\Core\SessionUtil; -use SP\Core\Template; +use SP\Mvc\View\Template; use SP\DataModel\NoticeData; use SP\Mgmt\Notices\Notice; use SP\Mgmt\Users\User; diff --git a/lib/SP/Controller/NoticesSearchController.php b/lib/SP/Controller/NoticesSearchController.php index a65cd4dc..26c8370a 100644 --- a/lib/SP/Controller/NoticesSearchController.php +++ b/lib/SP/Controller/NoticesSearchController.php @@ -30,7 +30,7 @@ use SP\Config\Config; use SP\Controller\Grids\Notices; use SP\Core\Acl\ActionsInterface; use SP\Core\SessionUtil; -use SP\Core\Template; +use SP\Mvc\View\Template; use SP\DataModel\ItemSearchData; use SP\Http\Request; use SP\Mgmt\Notices\NoticeSearch; @@ -54,7 +54,7 @@ class NoticesSearchController extends GridItemsSearchController implements Actio /** * Constructor * - * @param $template Template con instancia de plantilla + * @param $template \SP\Mvc\View\Template con instancia de plantilla */ public function __construct(Template $template = null) { diff --git a/lib/SP/Controller/UserPreferencesController.php b/lib/SP/Controller/UserPreferencesController.php index 6ecf237f..a8041abf 100644 --- a/lib/SP/Controller/UserPreferencesController.php +++ b/lib/SP/Controller/UserPreferencesController.php @@ -31,7 +31,7 @@ use SP\Core\Acl\ActionsInterface; use SP\Core\DiFactory; use SP\Core\Language; use SP\Core\SessionUtil; -use SP\Core\Template; +use SP\Mvc\View\Template; use SP\DataModel\UserPreferencesData; use SP\Mgmt\Users\UserPreferences; diff --git a/lib/SP/Core/Crypt/Session.php b/lib/SP/Core/Crypt/Session.php index eace6386..5858eaaa 100644 --- a/lib/SP/Core/Crypt/Session.php +++ b/lib/SP/Core/Crypt/Session.php @@ -38,8 +38,6 @@ class Session * * @return string * @throws \Defuse\Crypto\Exception\CryptoException - * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException - * @throws \Defuse\Crypto\Exception\BadFormatException */ public static function getSessionKey() { @@ -50,8 +48,6 @@ class Session * Guardar la clave maestra en la sesión * * @param $data - * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException - * @throws \Defuse\Crypto\Exception\BadFormatException * @throws \Defuse\Crypto\Exception\CryptoException */ public static function saveSessionKey($data) @@ -63,9 +59,7 @@ class Session /** * Regenerar la clave de sesión * - * @throws \Defuse\Crypto\Exception\BadFormatException * @throws \Defuse\Crypto\Exception\CryptoException - * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException */ public static function reKey() { diff --git a/lib/SP/Core/Crypt/Vault.php b/lib/SP/Core/Crypt/Vault.php index 3fcb8f6c..e4c7cfd9 100644 --- a/lib/SP/Core/Crypt/Vault.php +++ b/lib/SP/Core/Crypt/Vault.php @@ -55,10 +55,8 @@ class Vault * Regenerar la clave de sesión * * @param string $key - * @throws \Defuse\Crypto\Exception\BadFormatException - * @throws \Defuse\Crypto\Exception\CryptoException - * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException * @return Vault + * @throws \Defuse\Crypto\Exception\CryptoException */ public function reKey($key = null) { @@ -78,8 +76,6 @@ class Vault * @param string $key * @return string * @throws \Defuse\Crypto\Exception\CryptoException - * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException - * @throws \Defuse\Crypto\Exception\BadFormatException */ public function getData($key = null) { @@ -102,11 +98,9 @@ class Vault /** * Guardar la clave maestra en la sesión * - * @param $data + * @param mixed $data * @param string $key * @return $this - * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException - * @throws \Defuse\Crypto\Exception\BadFormatException * @throws \Defuse\Crypto\Exception\CryptoException */ public function saveData($data, $key = null) diff --git a/lib/SP/DataModel/PublicLinkData.php b/lib/SP/DataModel/PublicLinkData.php index 0d78cf86..8bf1f60a 100644 --- a/lib/SP/DataModel/PublicLinkData.php +++ b/lib/SP/DataModel/PublicLinkData.php @@ -31,224 +31,235 @@ defined('APP_ROOT') || die(); * * @package SP\DataModel */ -class PublicLinkData extends PublicLinkBaseData +class PublicLinkData extends DataModelBase implements DataModelInterface { /** * @var int */ - protected $itemId = 0; + public $publicLink_id = 0; /** * @var int */ - protected $userId = 0; + public $publicLink_itemId = 0; /** * @var string */ - protected $linkHash = ''; + public $publicLink_hash = ''; /** * @var int */ - protected $typeId = 0; + public $publicLink_userId = 0; + /** + * @var int + */ + public $publicLink_typeId = 0; /** * @var bool */ - protected $notify = false; + public $publicLink_notify = false; /** * @var int */ - protected $dateAdd = 0; + public $publicLink_dateAdd = 0; /** * @var int */ - protected $dateExpire = 0; + public $publicLink_dateExpire = 0; + /** + * @var int + */ + public $publicLink_countViews = 0; + /** + * @var int + */ + public $publicLink_totalCountViews = 0; + /** + * @var int + */ + public $publicLink_maxCountViews = 0; + /** + * @var array|string + */ + public $publicLink_useInfo; /** * @var string */ - protected $pass = ''; - /** - * @var string - */ - protected $passIV = ''; - /** - * @var int - */ - protected $countViews = 0; - /** - * @var int - */ - protected $totalCountViews = 0; - /** - * @var int - */ - protected $maxCountViews = 0; - /** - * @var array - */ - protected $useInfo = []; - /** - * @var string - */ - protected $data; + public $publicLink_data; /** - * @return int + * @return string */ - public function getItemId() + public function getPublicLinkData() { - return $this->itemId; + return $this->publicLink_data; } /** - * @param int $itemId + * @param string $publicLink_data */ - public function setItemId($itemId) + public function setPublicLinkData($publicLink_data) { - $this->itemId = $itemId; - $this->publicLink_itemId = $itemId; + $this->publicLink_data = $publicLink_data; } /** * @return int */ - public function getUserId() + public function getPublicLinkId() { - return $this->userId; + return (int)$this->publicLink_id; } /** - * @param int $userId + * @param int $publicLink_id */ - public function setUserId($userId) + public function setPublicLinkId($publicLink_id) { - $this->userId = $userId; + $this->publicLink_id = (int)$publicLink_id; } /** * @return string */ - public function getLinkHash() + public function getPublicLinkHash() { - return $this->linkHash; + return $this->publicLink_hash; } /** - * @param string $linkHash + * @param string $publicLink_hash */ - public function setLinkHash($linkHash) + public function setPublicLinkHash($publicLink_hash) { - $this->linkHash = $linkHash; + $this->publicLink_hash = $publicLink_hash; } /** * @return int */ - public function getTypeId() + public function getPublicLinkItemId() { - return $this->typeId; + return (int)$this->publicLink_itemId; } /** - * @param int $typeId + * @param int $publicLink_itemId */ - public function setTypeId($typeId) + public function setPublicLinkItemId($publicLink_itemId) { - $this->typeId = $typeId; + $this->publicLink_itemId = (int)$publicLink_itemId; + } + + /** + * @return int + */ + public function getId() + { + return (int)$this->publicLink_id; + } + + /** + * @return string + */ + public function getName() + { + return ''; + } + + /** + * @return int + */ + public function getPublicLinkUserId() + { + return (int)$this->publicLink_userId; + } + + /** + * @param int $publicLink_userId + */ + public function setPublicLinkUserId($publicLink_userId) + { + $this->publicLink_userId = (int)$publicLink_userId; + } + + /** + * @return int + */ + public function getPublicLinkTypeId() + { + return (int)$this->publicLink_typeId; + } + + /** + * @param int $publicLink_typeId + */ + public function setPublicLinkTypeId($publicLink_typeId) + { + $this->publicLink_typeId = (int)$publicLink_typeId; } /** * @return boolean */ - public function isNotify() + public function isPublicLinkNotify() { - return (bool)$this->notify; + return (bool)$this->publicLink_notify; } /** - * @param boolean $notify + * @param boolean $publicLink_notify */ - public function setNotify($notify) + public function setPublicLinkNotify($publicLink_notify) { - $this->notify = $notify; + $this->publicLink_notify = (bool)$publicLink_notify; } /** * @return int */ - public function getDateAdd() + public function getPublicLinkDateAdd() { - return $this->dateAdd; + return (int)$this->publicLink_dateAdd; } /** - * @param int $dateAdd + * @param int $publicLink_dateAdd */ - public function setDateAdd($dateAdd) + public function setPublicLinkDateAdd($publicLink_dateAdd) { - $this->dateAdd = $dateAdd; + $this->publicLink_dateAdd = (int)$publicLink_dateAdd; } /** * @return int */ - public function getDateExpire() + public function getPublicLinkDateExpire() { - return $this->dateExpire; + return (int)$this->publicLink_dateExpire; } /** - * @param int $dateExpire + * @param int $publicLink_dateExpire */ - public function setDateExpire($dateExpire) + public function setPublicLinkDateExpire($publicLink_dateExpire) { - $this->dateExpire = $dateExpire; - } - - /** - * @return string - */ - public function getPass() - { - return $this->pass; - } - - /** - * @param string $pass - */ - public function setPass($pass) - { - $this->pass = $pass; - } - - /** - * @return string - */ - public function getPassIV() - { - return $this->passIV; - } - - /** - * @param string $passIV - */ - public function setPassIV($passIV) - { - $this->passIV = $passIV; + $this->publicLink_dateExpire = (int)$publicLink_dateExpire; } /** * @return int */ - public function getCountViews() + public function getPublicLinkCountViews() { - return $this->countViews; + return (int)$this->publicLink_countViews; } /** - * @param int $countViews + * @param int $publicLink_countViews */ - public function setCountViews($countViews) + public function setPublicLinkCountViews($publicLink_countViews) { - $this->countViews = (int)$countViews; + $this->publicLink_countViews = (int)$publicLink_countViews; } /** @@ -256,71 +267,51 @@ class PublicLinkData extends PublicLinkBaseData */ public function addCountViews() { - return $this->countViews++; + return $this->publicLink_countViews++; } /** * @return int */ - public function getMaxCountViews() + public function getPublicLinkMaxCountViews() { - return $this->maxCountViews; + return (int)$this->publicLink_maxCountViews; } /** - * @param int $maxCountViews + * @param int $publicLink_maxCountViews */ - public function setMaxCountViews($maxCountViews) + public function setPublicLinkMaxCountViews($publicLink_maxCountViews) { - $this->maxCountViews = (int)$maxCountViews; + $this->publicLink_maxCountViews = (int)$publicLink_maxCountViews; } /** * @return array */ - public function getUseInfo() + public function getPublicLinkUseInfo() { - return $this->useInfo; + if (is_string($this->publicLink_useInfo)) { + return unserialize($this->publicLink_useInfo); + } + + return (array)$this->publicLink_useInfo; } /** - * @param array $useInfo + * @param array $publicLink_useInfo */ - public function setUseInfo(array $useInfo) + public function setPublicLinkUseInfo(array $publicLink_useInfo) { - $this->useInfo = $useInfo; - } - - /** - * @param array $useInfo - */ - public function addUseInfo($useInfo) - { - $this->useInfo[] = $useInfo; - } - - /** - * @return string - */ - public function getData() - { - return $this->data; - } - - /** - * @param string $data - */ - public function setData($data) - { - $this->data = $data; + $this->publicLink_useInfo = $publicLink_useInfo; } /** * @return int */ - public function getTotalCountViews() + public function getPublicLinkTotalCountViews() { - return $this->totalCountViews; + return (int)$this->publicLink_totalCountViews; } /** @@ -328,6 +319,6 @@ class PublicLinkData extends PublicLinkBaseData */ public function addTotalCountViews() { - return $this->totalCountViews++; + return $this->publicLink_totalCountViews++; } } \ No newline at end of file diff --git a/lib/SP/DataModel/PublicLinkListData.php b/lib/SP/DataModel/PublicLinkListData.php index be721a38..536f5b8d 100644 --- a/lib/SP/DataModel/PublicLinkListData.php +++ b/lib/SP/DataModel/PublicLinkListData.php @@ -2,8 +2,8 @@ /** * sysPass * - * @author nuxsmin - * @link http://syspass.org + * @author nuxsmin + * @link http://syspass.org * @copyright 2012-2017, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. @@ -24,6 +24,8 @@ namespace SP\DataModel; +use SP\Util\DateUtil; + defined('APP_ROOT') || die(); /** @@ -31,51 +33,27 @@ defined('APP_ROOT') || die(); * * @package SP\DataModel */ -class PublicLinkListData extends PublicLinkBaseData +class PublicLinkListData extends PublicLinkData { /** * @var string */ - public $accountName = ''; + public $user_name; /** * @var string */ - public $userLogin = ''; + public $user_login; /** * @var string */ - public $notify = ''; - /** - * @var string - */ - public $dateAdd = ''; - /** - * @var string - */ - public $dateExpire = ''; - /** - * @var int - */ - public $countViews = 0; - /** - * @var array - */ - public $useInfo = []; + public $account_name; /** * @return string */ - public function getAccountName() + public function getUserName() { - return $this->accountName; - } - - /** - * @param string $accountName - */ - public function setAccountName($accountName) - { - $this->accountName = $accountName; + return $this->user_name; } /** @@ -83,94 +61,46 @@ class PublicLinkListData extends PublicLinkBaseData */ public function getUserLogin() { - return $this->userLogin; - } - - /** - * @param string $userLogin - */ - public function setUserLogin($userLogin) - { - $this->userLogin = $userLogin; + return $this->user_login; } /** * @return string */ - public function getNotify() + public function getAccountName() { - return $this->notify; - } - - /** - * @param string $notify - */ - public function setNotify($notify) - { - $this->notify = $notify; + return $this->account_name; } /** * @return string */ - public function getDateAdd() + public function getNotifyString() { - return $this->dateAdd; + return $this->isPublicLinkNotify() ? __u('ON') : __u('OFF'); } /** - * @param string $dateAdd + * @return false|string */ - public function setDateAdd($dateAdd) + public function getDateAddFormat() { - $this->dateAdd = $dateAdd; + return DateUtil::getDateFromUnix($this->publicLink_dateAdd); + } + + /** + * @return false|string + */ + public function getDateExpireFormat() + { + return DateUtil::getDateFromUnix($this->publicLink_dateExpire); } /** * @return string */ - public function getDateExpire() + public function getCountViewsString() { - return $this->dateExpire; - } - - /** - * @param string $dateExpire - */ - public function setDateExpire($dateExpire) - { - $this->dateExpire = $dateExpire; - } - - /** - * @return int - */ - public function getCountViews() - { - return $this->countViews; - } - - /** - * @param int $countViews - */ - public function setCountViews($countViews) - { - $this->countViews = $countViews; - } - - /** - * @return array - */ - public function getUseInfo() - { - return $this->useInfo; - } - - /** - * @param array $useInfo - */ - public function setUseInfo($useInfo) - { - $this->useInfo = $useInfo; + return sprintf('%d/%d/%d', $this->getPublicLinkCountViews(), $this->getPublicLinkMaxCountViews(), $this->getPublicLinkTotalCountViews()); } } \ No newline at end of file diff --git a/lib/SP/Forms/PublicLinkForm.php b/lib/SP/Forms/PublicLinkForm.php index 7a1145c3..6004d5a6 100644 --- a/lib/SP/Forms/PublicLinkForm.php +++ b/lib/SP/Forms/PublicLinkForm.php @@ -29,6 +29,7 @@ use SP\Core\Exceptions\ValidationException; use SP\DataModel\PublicLinkData; use SP\Http\Request; use SP\Mgmt\PublicLinks\PublicLink; +use SP\Util\Util; /** * Class PublicLinkForm @@ -48,6 +49,7 @@ class PublicLinkForm extends FormBase implements FormInterface * @param $action * @return bool * @throws ValidationException + * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException */ public function validate($action) { @@ -66,14 +68,16 @@ class PublicLinkForm extends FormBase implements FormInterface * Analizar los datos de la petición HTTP * * @return void + * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException */ protected function analyzeRequestData() { $this->PublicLinkData = new PublicLinkData(); $this->PublicLinkData->setPublicLinkId($this->itemId); - $this->PublicLinkData->setTypeId(PublicLink::TYPE_ACCOUNT); - $this->PublicLinkData->setItemId(Request::analyze('accountId', 0)); - $this->PublicLinkData->setNotify(Request::analyze('notify', false, false, true)); + $this->PublicLinkData->setPublicLinkTypeId(PublicLink::TYPE_ACCOUNT); + $this->PublicLinkData->setPublicLinkItemId(Request::analyze('accountId', 0)); + $this->PublicLinkData->setPublicLinkNotify(Request::analyze('notify', false, false, true)); + $this->PublicLinkData->setPublicLinkHash(Util::generateRandomBytes()); } /** diff --git a/lib/SP/Html/DataGrid/DataGridDataBase.php b/lib/SP/Html/DataGrid/DataGridDataBase.php index 3ca3d747..0f85eff7 100644 --- a/lib/SP/Html/DataGrid/DataGridDataBase.php +++ b/lib/SP/Html/DataGrid/DataGridDataBase.php @@ -2,8 +2,8 @@ /** * sysPass * - * @author nuxsmin - * @link http://syspass.org + * @author nuxsmin + * @link http://syspass.org * @copyright 2012-2017, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. @@ -73,11 +73,12 @@ abstract class DataGridDataBase implements DataGridDataInterface } /** - * @param $source string + * @param $source string + * @param bool $isMethod */ - public function addDataRowSource($source) + public function addDataRowSource($source, $isMethod = false) { - $this->_sources[] = $source; + $this->_sources[] = ['name' => $source, 'isMethod' => $isMethod]; } /** @@ -122,9 +123,9 @@ abstract class DataGridDataBase implements DataGridDataInterface } /** - * @param string $source + * @param string $source * @param IconInterface $icon - * @param mixed $value Valor para mostrar el icono + * @param mixed $value Valor para mostrar el icono */ public function addDataRowSourceWithIcon($source, IconInterface $icon, $value = 1) { diff --git a/lib/SP/Html/DataGrid/DataGridDataInterface.php b/lib/SP/Html/DataGrid/DataGridDataInterface.php index bcd07839..13c20672 100644 --- a/lib/SP/Html/DataGrid/DataGridDataInterface.php +++ b/lib/SP/Html/DataGrid/DataGridDataInterface.php @@ -2,8 +2,8 @@ /** * sysPass * - * @author nuxsmin - * @link http://syspass.org + * @author nuxsmin + * @link http://syspass.org * @copyright 2012-2017, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. @@ -38,9 +38,10 @@ interface DataGridDataInterface /** * Establecer los orígenes de datos de la consulta * - * @param $source string + * @param $source string + * @param bool $isMethod */ - public function addDataRowSource($source); + public function addDataRowSource($source, $isMethod = false); /** * Devolver los orígenes de datos de la consulta @@ -80,9 +81,9 @@ interface DataGridDataInterface /** * Establecer los orígenes de datos que se muestran con iconos * - * @param $source string - * @param $icon IconInterface - * @param mixed $value Valor para mostrar el icono + * @param $source string + * @param $icon IconInterface + * @param mixed $value Valor para mostrar el icono * @return */ public function addDataRowSourceWithIcon($source, IconInterface $icon, $value = 1); diff --git a/lib/SP/Mgmt/PublicLinks/PublicLink.php b/lib/SP/Mgmt/PublicLinks/PublicLink.php index 9f71f1f1..5187b988 100644 --- a/lib/SP/Mgmt/PublicLinks/PublicLink.php +++ b/lib/SP/Mgmt/PublicLinks/PublicLink.php @@ -93,7 +93,7 @@ class PublicLink extends PublicLinkBase implements ItemInterface { $query = /** @lang SQL */ 'UPDATE publicLinks - SET publicLink_linkData = ?, + SET publicLink_data = ?, publicLink_hash = ? WHERE publicLink_id = ? LIMIT 1'; @@ -135,7 +135,7 @@ class PublicLink extends PublicLinkBase implements ItemInterface 'INSERT INTO publicLinks SET publicLink_hash = ?, publicLink_itemId = ?, - publicLink_linkData = ?'; + publicLink_data = ?'; $Data = new QueryData(); $Data->setQuery($query); @@ -207,7 +207,7 @@ class PublicLink extends PublicLinkBase implements ItemInterface $query = /** @lang SQL */ 'UPDATE publicLinks - SET publicLink_linkData = ?, + SET publicLink_data = ?, publicLink_hash = ? WHERE publicLink_id = ? LIMIT 1'; @@ -233,7 +233,7 @@ class PublicLink extends PublicLinkBase implements ItemInterface $query = /** @lang SQL */ 'SELECT publicLink_id, publicLink_hash, - publicLink_linkData + publicLink_data FROM publicLinks WHERE publicLink_id = ? LIMIT 1'; $Data = new QueryData(); @@ -261,7 +261,7 @@ class PublicLink extends PublicLinkBase implements ItemInterface public function getAll() { $query = /** @lang SQL */ - 'SELECT publicLink_id, publicLink_hash, publicLink_linkData FROM publicLinks'; + 'SELECT publicLink_id, publicLink_hash, publicLink_data FROM publicLinks'; $Data = new QueryData(); $Data->setMapClassName($this->getDataModel()); @@ -293,14 +293,14 @@ class PublicLink extends PublicLinkBase implements ItemInterface { $PublicLinkListData = new PublicLinkListData(); $PublicLinkListData->setPublicLinkId($PublicLinkData->getPublicLinkId()); - $PublicLinkListData->setPublicLinkHash($PublicLinkData->getLinkHash()); + $PublicLinkListData->setPublicLinkHash($PublicLinkData->getPublicLinkLinkHash()); $PublicLinkListData->setAccountName(AccountUtil::getAccountNameById($PublicLinkData->getItemId())); - $PublicLinkListData->setUserLogin(UserUtil::getUserLoginById($PublicLinkData->getUserId())); - $PublicLinkListData->setNotify($PublicLinkData->isNotify() ? __('ON') : __('OFF')); - $PublicLinkListData->setDateAdd(date('Y-m-d H:i', $PublicLinkData->getDateAdd())); - $PublicLinkListData->setDateExpire(date('Y-m-d H:i', $PublicLinkData->getDateExpire())); - $PublicLinkListData->setCountViews($PublicLinkData->getCountViews() . '/' . $PublicLinkData->getMaxCountViews()); - $PublicLinkListData->setUseInfo($PublicLinkData->getUseInfo()); + $PublicLinkListData->setUserLogin(UserUtil::getUserLoginById($PublicLinkData->getPublicLinkUserId())); + $PublicLinkListData->setNotify($PublicLinkData->isPublicLinkNotify() ? __('ON') : __('OFF')); + $PublicLinkListData->setDateAdd(date('Y-m-d H:i', $PublicLinkData->getPublicLinkDateAdd())); + $PublicLinkListData->setDateExpire(date('Y-m-d H:i', $PublicLinkData->getPublicLinkDateExpire())); + $PublicLinkListData->setCountViews($PublicLinkData->getPublicLinkCountViews() . '/' . $PublicLinkData->getPublicLinkMaxCountViews()); + $PublicLinkListData->setUseInfo($PublicLinkData->getPublicLinkUseInfo()); return $PublicLinkListData; } @@ -332,7 +332,7 @@ class PublicLink extends PublicLinkBase implements ItemInterface $query = /** @lang SQL */ 'SELECT publicLink_id, publicLink_hash, - publicLink_linkData + publicLink_data FROM publicLinks WHERE publicLink_hash = ? LIMIT 1'; $Data = new QueryData(); diff --git a/lib/SP/Core/Template.php b/lib/SP/Mvc/View/Template.php similarity index 99% rename from lib/SP/Core/Template.php rename to lib/SP/Mvc/View/Template.php index 291df925..d6a5b0a0 100644 --- a/lib/SP/Core/Template.php +++ b/lib/SP/Mvc/View/Template.php @@ -22,7 +22,7 @@ * along with sysPass. If not, see . */ -namespace SP\Core; +namespace SP\Mvc\View; defined('APP_ROOT') || die(); diff --git a/lib/SP/Services/PublicLink/PublicLinkService.php b/lib/SP/Services/PublicLink/PublicLinkService.php index 1b63b287..5de7886a 100644 --- a/lib/SP/Services/PublicLink/PublicLinkService.php +++ b/lib/SP/Services/PublicLink/PublicLinkService.php @@ -29,10 +29,9 @@ use SP\Bootstrap; use SP\Config\Config; use SP\Core\Crypt\Crypt; use SP\Core\Crypt\Session as CryptSession; -use SP\Core\Exceptions\InvalidClassException; +use SP\Core\Crypt\Vault; use SP\Core\Exceptions\SPException; use SP\DataModel\ItemSearchData; -use SP\DataModel\PublicLinkBaseData; use SP\DataModel\PublicLinkData; use SP\DataModel\PublicLinkListData; use SP\Http\Request; @@ -70,6 +69,16 @@ class PublicLinkService extends Service implements ServiceItemInterface return Bootstrap::$WEBURI . '/index.php?r=account/viewLink/' . $hash; } + /** + * Generar el hash para el enlace + * + * @return string + */ + protected static function createLinkHash() + { + return hash('sha256', uniqid('sysPassPublicLink', true)); + } + /** * Deletes an item * @@ -101,15 +110,33 @@ class PublicLinkService extends Service implements ServiceItemInterface /** * Returns all the items * - * @return mixed + * @return PublicLinkData[] */ public function getAll() { $query = /** @lang SQL */ - 'SELECT publicLink_id, publicLink_hash, publicLink_linkData FROM publicLinks'; + 'SELECT publicLink_id, + publicLink_itemId, + publicLink_hash, + publicLink_data, + publicLink_userId, + publicLink_typeId, + publicLink_notify, + publicLink_dateAdd, + publicLink_dateExpire, + publicLink_countViews, + publicLink_maxCountViews, + publicLink_totalCountViews, + publicLink_useInfo, + user_name, + user_login, + account_name + FROM publicLinks + INNER JOIN usrData ON user_id = publicLink_userId + INNER JOIN accounts ON account_id = publicLink_itemId'; $Data = new QueryData(); - $Data->setMapClassName(PublicLinkBaseData::class); + $Data->setMapClassName(PublicLinkListData::class); $Data->setQuery($query); return DbWrapper::getResultsArray($Data, $this->db); @@ -119,17 +146,34 @@ class PublicLinkService extends Service implements ServiceItemInterface * Returns all the items for given ids * * @param array $ids - * @return array + * @return PublicLinkData[] */ public function getByIdBatch(array $ids) { $query = /** @lang SQL */ - 'SELECT publicLink_id, - publicLink_hash - FROM publicLinks WHERE publicLink_id IN (' . $this->getParamsFromArray($ids) . ')'; + 'SELECT publicLink_id, + publicLink_itemId, + publicLink_hash, + publicLink_data, + publicLink_userId, + publicLink_typeId, + publicLink_notify, + publicLink_dateAdd, + publicLink_dateExpire, + publicLink_countViews, + publicLink_maxCountViews, + publicLink_totalCountViews, + publicLink_useInfo, + user_name, + user_login, + account_name + FROM publicLinks + INNER JOIN usrData ON user_id = publicLink_userId + INNER JOIN accounts ON account_id = publicLink_itemId + WHERE publicLink_id IN (' . $this->getParamsFromArray($ids) . ')'; $Data = new QueryData(); - $Data->setMapClassName(PublicLinkBaseData::class); + $Data->setMapClassName(PublicLinkListData::class); $Data->setQuery($query); $Data->setParams($ids); @@ -172,110 +216,49 @@ class PublicLinkService extends Service implements ServiceItemInterface * * @param ItemSearchData $SearchData * @return mixed - * @throws InvalidClassException */ public function search(ItemSearchData $SearchData) { $Data = new QueryData(); - $Data->setMapClassName(PublicLinkBaseData::class); - $Data->setSelect('publicLink_id, publicLink_hash, publicLink_linkData'); - $Data->setFrom('publicLinks'); + $Data->setMapClassName(PublicLinkListData::class); + $Data->setSelect('publicLink_id, + publicLink_itemId, + publicLink_hash, + publicLink_data, + publicLink_userId, + publicLink_typeId, + publicLink_notify, + publicLink_dateAdd, + publicLink_dateExpire, + publicLink_countViews, + publicLink_maxCountViews, + publicLink_totalCountViews, + publicLink_useInfo, + user_name, + user_login, + account_name'); + $Data->setFrom('publicLinks INNER JOIN usrData ON user_id = publicLink_userId INNER JOIN accounts ON account_id = publicLink_itemId'); + $Data->setOrder('publicLink_dateExpire DESC'); + + if ($SearchData->getSeachString() !== '') { + $Data->setWhere('user_login LIKE ? OR account_name LIKE ?'); + + $search = '%' . $SearchData->getSeachString() . '%'; + $Data->addParam($search); + $Data->addParam($search); + } + $Data->setLimit('?,?'); $Data->addParam($SearchData->getLimitStart()); $Data->addParam($SearchData->getLimitCount()); DbWrapper::setFullRowCount(); - /** @var PublicLinkListData[] $queryRes */ $queryRes = DbWrapper::getResultsArray($Data, $this->db); - $filters = [ - ['method' => 'getAccountName', 'text' => $SearchData->getSeachString()], - ['method' => 'getUserLogin', 'text' => $SearchData->getSeachString()] - ]; + $queryRes['count'] = $Data->getQueryNumRows(); - $items = self::mapItemsForList($queryRes, $filters); - $items['count'] = $Data->getQueryNumRows(); - - /* - $publicLinks = []; - $publicLinks['count'] = $Data->getQueryNumRows(); - - foreach ($queryRes as $PublicLinkListData) { - $PublicLinkData = Util::castToClass(PublicLinkBaseData::class, $PublicLinkListData->getPublicLinkLinkData()); - - $PublicLinkListData->setAccountName(AccountUtil::getAccountNameById($PublicLinkData->getItemId())); - $PublicLinkListData->setUserLogin(UserUtil::getUserLoginById($PublicLinkData->getUserId())); - $PublicLinkListData->setNotify(__($PublicLinkData->isNotify() ? 'ON' : 'OFF')); - $PublicLinkListData->setDateAdd(date('Y-m-d H:i', $PublicLinkData->getDateAdd())); - $PublicLinkListData->setDateExpire(date('Y-m-d H:i', $PublicLinkData->getDateExpire())); - $PublicLinkListData->setCountViews($PublicLinkData->getCountViews() . '/' . $PublicLinkData->getMaxCountViews()); - $PublicLinkListData->setUseInfo($PublicLinkData->getUseInfo()); - - if ($SearchData->getSeachString() === '' - || mb_stripos($PublicLinkListData->getAccountName(), $SearchData->getSeachString()) !== false - || mb_stripos($PublicLinkListData->getUserLogin(), $SearchData->getSeachString()) !== false - ) { - $publicLinks[] = $PublicLinkListData; - } - } - */ - - return $items; - } - - /** - * Devuelve los datos de un enlace para mostrarlo - * - * @param array $data - * @param array $filters Array of ['method' => , 'text' => ] - * @return PublicLinkListData[] - * @throws InvalidClassException - */ - public static function mapItemsForList(array $data, array $filters = null) - { - $items = []; - - $publicLinkListData = new PublicLinkListData(); - - foreach ($data as $publicLink) { - if (!$publicLink instanceof PublicLinkBaseData) { - throw new InvalidClassException(SPException::SP_ERROR, __u('Error interno')); - } - - /** @var PublicLinkData $publicLinkData */ - $publicLinkData = Util::unserialize(PublicLinkData::class, $publicLink->getPublicLinkLinkData()); - - if ($filters !== null) { - foreach ($filters as $filter) { - if ($filter['text'] !== '' - && method_exists($publicLinkData, $filter['method']) - && mb_stripos($publicLinkData->{$filter['method']}(), $filter['text']) === false - ) { - continue 2; - } - } - } - - $publicLinkData->setPublicLinkId($publicLink->getPublicLinkId()); - - $item = clone $publicLinkListData; - $item->setPublicLinkLinkData($publicLinkData); - $item->setPublicLinkId($publicLinkData->getPublicLinkId()); - $item->setPublicLinkItemId($publicLinkData->getPublicLinkItemId()); - $item->setPublicLinkHash($publicLinkData->getLinkHash()); - $item->setAccountName(AccountUtil::getAccountNameById($publicLinkData->getItemId())); - $item->setUserLogin(UserUtil::getUserLoginById($publicLinkData->getUserId())); - $item->setNotify($publicLinkData->isNotify() ? __u('ON') : __u('OFF')); - $item->setDateAdd(DateUtil::getDateFromUnix($publicLinkData->getDateAdd())); - $item->setDateExpire(DateUtil::getDateFromUnix($publicLinkData->getDateExpire())); - $item->setCountViews(sprintf('%d/%d/%d', $publicLinkData->getCountViews(), $publicLinkData->getMaxCountViews(), $publicLinkData->getTotalCountViews())); - $item->setUseInfo($publicLinkData->getUseInfo()); - - $items[] = $item; - } - - return $items; + return $queryRes; } /** @@ -284,9 +267,7 @@ class PublicLinkService extends Service implements ServiceItemInterface * @param PublicLinkData $itemData * @return int * @throws SPException - * @throws \Defuse\Crypto\Exception\BadFormatException * @throws \Defuse\Crypto\Exception\CryptoException - * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ @@ -296,25 +277,28 @@ class PublicLinkService extends Service implements ServiceItemInterface throw new SPException(SPException::SP_INFO, __u('Enlace ya creado')); } - $itemData->setDateAdd(time()); - $itemData->setUserId($this->session->getUserData()->getUserId()); - $itemData->setMaxCountViews($this->config->getConfigData()->getPublinksMaxViews()); - - self::calcDateExpire($itemData, $this->config); - self::createLinkHash($itemData); - self::setLinkData($itemData, $this->config); - $query = /** @lang SQL */ 'INSERT INTO publicLinks - SET publicLink_hash = ?, - publicLink_itemId = ?, - publicLink_linkData = ?'; + SET publicLink_itemId = ?, + publicLink_hash = ?, + publicLink_data = ?, + publicLink_userId = ?, + publicLink_typeId = ?, + publicLink_notify = ?, + publicLink_dateAdd = UNIX_TIMESTAMP(), + publicLink_dateExpire = ?, + publicLink_maxCountViews = ?'; $Data = new QueryData(); $Data->setQuery($query); - $Data->addParam($itemData->getPublicLinkHash()); $Data->addParam($itemData->getPublicLinkItemId()); - $Data->addParam(serialize($itemData)); + $Data->addParam($itemData->getPublicLinkHash()); + $Data->addParam($this->getSecuredLinkData($itemData->getPublicLinkItemId(), self::getKeyForHash($this->config, $itemData))); + $Data->addParam($this->session->getUserData()->getUserId()); + $Data->addParam($itemData->getPublicLinkTypeId()); + $Data->addParam((int)$itemData->isPublicLinkNotify()); + $Data->addParam(self::calcDateExpire($this->config)); + $Data->addParam($this->config->getConfigData()->getPublinksMaxViews()); $Data->setOnErrorMessage(__u('Error al crear enlace')); DbWrapper::getQuery($Data, $this->db); @@ -343,92 +327,54 @@ class PublicLinkService extends Service implements ServiceItemInterface } /** - * Devolver el tiempo de caducidad del enlace + * Obtener los datos de una cuenta y encriptarlos para el enlace * - * @param PublicLinkData $publicLinkData - * @param Config $config + * @param int $itemId + * @param string $linkKey + * @return Vault + * @throws SPException + * @throws \Defuse\Crypto\Exception\CryptoException */ - protected static function calcDateExpire(PublicLinkData $publicLinkData, Config $config) + protected function getSecuredLinkData($itemId, $linkKey) { - $publicLinkData->setDateExpire(time() + $config->getConfigData()->getPublinksMaxTime()); + // Obtener los datos de la cuenta + $accountService = new AccountService(); + $accountData = $accountService->getDataForLink($itemId); + + // Desencriptar la clave de la cuenta + $key = CryptSession::getSessionKey(); + $securedKey = Crypt::unlockSecuredKey($accountData->getAccountKey(), $key); + $accountData->setAccountPass(Crypt::decrypt($accountData->getAccountPass(), $securedKey, $key)); + $accountData->setAccountKey(null); + + $vault = new Vault(); + return serialize($vault->saveData(serialize($accountData), $linkKey)); } /** - * Generar el hash para el enlace - * + * @param Config $config * @param PublicLinkData $publicLinkData - * @param bool $refresh Si es necesario regenerar el hash * @return string + * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException */ - protected static function createLinkHash(PublicLinkData $publicLinkData, $refresh = false) + public static function getKeyForHash(Config $config, PublicLinkData $publicLinkData = null) { - if ($refresh === true - || $publicLinkData->getLinkHash() === '' - ) { - $hash = hash('sha256', uniqid('sysPassPublicLink', true)); - - $publicLinkData->setPublicLinkHash($hash); - $publicLinkData->setLinkHash($hash); + if (null !== $publicLinkData) { + return $config->getConfigData()->getPasswordSalt() . $publicLinkData->getPublicLinkHash(); } - return $publicLinkData->getLinkHash(); + return $config->getConfigData()->getPasswordSalt() . Util::generateRandomBytes(); } /** - * Obtener los datos de una cuenta y encriptarlos para el enlace + * Devolver el tiempo de caducidad del enlace * - * @param PublicLinkData $publicLinkData - * @param Config $config - * @throws SPException - * @throws \Defuse\Crypto\Exception\BadFormatException - * @throws \Defuse\Crypto\Exception\CryptoException - * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException + * @param Config $config + * @return int */ - protected static function setLinkData(PublicLinkData $publicLinkData, Config $config) + protected static function calcDateExpire(Config $config) { - // Obtener los datos de la cuenta - $accountService = new AccountService(); - $accountData = $accountService->getDataForLink($publicLinkData->getItemId()); - - $key = CryptSession::getSessionKey(); - $securedKey = Crypt::unlockSecuredKey($accountData->getAccountKey(), $key); - $accountData->setAccountPass(Crypt::decrypt($accountData->getAccountPass(), $securedKey, $key)); - $accountData->setAccountKey(null); - - // Encriptar los datos de la cuenta - $linkKey = $config->getConfigData()->getPasswordSalt() . self::createLinkHash($publicLinkData); - $linkSecuredKey = Crypt::makeSecuredKey($linkKey); - - $publicLinkData->setData(Crypt::encrypt(serialize($accountData), $linkSecuredKey, $linkKey)); - $publicLinkData->setPassIV($linkSecuredKey); - } - - /** - * Obtener los datos de una cuenta y encriptarlos para el enlace - * - * @param PublicLinkData $publicLinkData - * @throws SPException - * @throws \Defuse\Crypto\Exception\BadFormatException - * @throws \Defuse\Crypto\Exception\CryptoException - * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException - */ - public function getLinkData(PublicLinkData $publicLinkData) - { - // Obtener los datos de la cuenta - $accountService = new AccountService(); - $accountData = $accountService->getDataForLink($publicLinkData->getItemId()); - - $key = CryptSession::getSessionKey(); - $securedKey = Crypt::unlockSecuredKey($accountData->getAccountKey(), $key); - $accountData->setAccountPass(Crypt::decrypt($accountData->getAccountPass(), $securedKey, $key)); - $accountData->setAccountKey(null); - - // Encriptar los datos de la cuenta - $linkKey = $this->config->getConfigData()->getPasswordSalt() . self::createLinkHash($publicLinkData); - $linkSecuredKey = Crypt::makeSecuredKey($linkKey); - - $publicLinkData->setData(Crypt::encrypt(serialize($accountData), $linkSecuredKey, $linkKey)); - $publicLinkData->setPassIV($linkSecuredKey); + return time() + $config->getConfigData()->getPublinksMaxTime(); } /** @@ -446,45 +392,59 @@ class PublicLinkService extends Service implements ServiceItemInterface * Incrementar el contador de visitas de un enlace * * @param PublicLinkData $publicLinkData - * @return bool + * @return void * @throws \PHPMailer\PHPMailer\Exception * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ public function addLinkView(PublicLinkData $publicLinkData) { - $publicLinkData->addUseInfo(self::getUseInfo($publicLinkData)); - $publicLinkData->addCountViews(); - $publicLinkData->addTotalCountViews(); + $useInfo = $publicLinkData->getPublicLinkUseInfo(); + $useInfo[] = self::getUseInfo($publicLinkData->getPublicLinkHash()); + $publicLinkData->setPublicLinkUseInfo($useInfo); + $query = /** @lang SQL */ + 'UPDATE publicLinks + SET publicLink_countViews = publicLink_countViews + 1, + publicLink_totalCountViews = publicLink_totalCountViews + 1, + publicLink_useInfo = ? + WHERE publicLink_hash = ? LIMIT 1'; + + $Data = new QueryData(); + $Data->setQuery($query); + $Data->addParam(serialize($publicLinkData->getPublicLinkUseInfo())); + $Data->addParam($publicLinkData->getPublicLinkHash()); + $Data->setOnErrorMessage(__u('Error al actualizar enlace')); + + DbWrapper::getQuery($Data, $this->db); + + // FIXME $Log = new Log(); $LogMessage = $Log->getLogMessage(); $LogMessage->setAction(__u('Ver Enlace Público')); $LogMessage->addDescription(__u('Enlace visualizado')); - $LogMessage->addDetails(__u('Tipo'), $publicLinkData->getTypeId()); - $LogMessage->addDetails(__u('Cuenta'), AccountUtil::getAccountNameById($publicLinkData->getItemId())); - $LogMessage->addDetails(__u('Usuario'), UserUtil::getUserLoginById($publicLinkData->getUserId())); + $LogMessage->addDetails(__u('Tipo'), $publicLinkData->getPublicLinkTypeId()); + $LogMessage->addDetails(__u('Cuenta'), AccountUtil::getAccountNameById($publicLinkData->getPublicLinkItemId())); + $LogMessage->addDetails(__u('Usuario'), UserUtil::getUserLoginById($publicLinkData->getPublicLinkUserId())); $Log->writeLog(); - if ($publicLinkData->isNotify()) { + if ($publicLinkData->isPublicLinkNotify()) { Email::sendEmail($LogMessage); } - - return $this->update($publicLinkData); } /** * Actualizar la información de uso * - * @param PublicLinkData $publicLinkData + * @param $hash * @return array */ - protected static function getUseInfo(PublicLinkData $publicLinkData) + protected static function getUseInfo($hash) { return [ 'who' => HttpUtil::getClientAddress(true), 'time' => time(), - 'hash' => $publicLinkData->getLinkHash(), + 'hash' => $hash, 'agent' => Request::getRequestHeaders('HTTP_USER_AGENT'), 'https' => Checks::httpsEnabled() ]; @@ -495,6 +455,8 @@ class PublicLinkService extends Service implements ServiceItemInterface * * @param PublicLinkData $itemData * @return mixed + * @throws SPException + * @throws \Defuse\Crypto\Exception\CryptoException * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ @@ -502,20 +464,24 @@ class PublicLinkService extends Service implements ServiceItemInterface { $query = /** @lang SQL */ 'UPDATE publicLinks - SET publicLink_linkData = ?, - publicLink_hash = ? + SET publicLink_hash = ?, + publicLink_data = ?, + publicLink_notify = ?, + publicLink_dateExpire = ?, + publicLink_maxCountViews = ? WHERE publicLink_id = ? LIMIT 1'; $Data = new QueryData(); $Data->setQuery($query); - $Data->addParam(serialize($itemData)); - $Data->addParam($itemData->getLinkHash()); + $Data->addParam($itemData->getPublicLinkHash()); + $Data->addParam($this->getSecuredLinkData($itemData->getPublicLinkItemId(), self::getKeyForHash($this->config, $itemData))); + $Data->addParam((int)$itemData->isPublicLinkNotify()); + $Data->addParam(self::calcDateExpire($this->config)); + $Data->addParam($this->config->getConfigData()->getPublinksMaxViews()); $Data->addParam($itemData->getPublicLinkId()); $Data->setOnErrorMessage(__u('Error al actualizar enlace')); - DbWrapper::getQuery($Data, $this->db); - - return true; + return DbWrapper::getQuery($Data, $this->db); } /** @@ -524,34 +490,31 @@ class PublicLinkService extends Service implements ServiceItemInterface * @param $id * @return $this * @throws SPException - * @throws \Defuse\Crypto\Exception\BadFormatException * @throws \Defuse\Crypto\Exception\CryptoException - * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ public function refresh($id) { - /** @var PublicLinkData $publicLinkData */ - $publicLinkData = Util::unserialize(PublicLinkData::class, $this->getById($id)->getPublicLinkLinkData()); - $publicLinkData->setCountViews(0); - $publicLinkData->setMaxCountViews($this->config->getConfigData()->getPublinksMaxViews()); - - self::calcDateExpire($publicLinkData, $this->config); - self::createLinkHash($publicLinkData, true); - self::setLinkData($publicLinkData, $this->config); + $publicLinkData = $this->getById($id); + $key = self::getKeyForHash($this->config); $query = /** @lang SQL */ 'UPDATE publicLinks - SET publicLink_linkData = ?, - publicLink_hash = ? + SET publicLink_hash = ?, + publicLink_data = ?, + publicLink_dateExpire = ?, + publicLink_countViews = 0, + publicLink_maxCountViews = ? WHERE publicLink_id = ? LIMIT 1'; $Data = new QueryData(); $Data->setQuery($query); - $Data->addParam(serialize($publicLinkData)); - $Data->addParam($publicLinkData->getPublicLinkHash()); - $Data->addParam($publicLinkData->getPublicLinkId()); + $Data->addParam(self::getHashForKey($key, $this->config)); + $Data->addParam($this->getSecuredLinkData($publicLinkData->getPublicLinkItemId(), $key)); + $Data->addParam(self::calcDateExpire($this->config)); + $Data->addParam($this->config->getConfigData()->getPublinksMaxViews()); + $Data->addParam($id); $Data->setOnErrorMessage(__u('Error al renovar enlace')); DbWrapper::getQuery($Data, $this->db); @@ -563,19 +526,35 @@ class PublicLinkService extends Service implements ServiceItemInterface * Returns the item for given id * * @param int $id - * @return PublicLinkBaseData + * @return PublicLinkData * @throws SPException */ public function getById($id) { $query = /** @lang SQL */ - 'SELECT publicLink_id, - publicLink_hash, - publicLink_linkData - FROM publicLinks WHERE publicLink_id = ? LIMIT 1'; + 'SELECT publicLink_id, + publicLink_itemId, + publicLink_hash, + publicLink_data, + publicLink_userId, + publicLink_typeId, + publicLink_notify, + publicLink_dateAdd, + publicLink_dateExpire, + publicLink_countViews, + publicLink_maxCountViews, + publicLink_totalCountViews, + publicLink_useInfo, + user_name, + user_login, + account_name + FROM publicLinks + INNER JOIN usrData ON user_id = publicLink_userId + INNER JOIN accounts ON account_id = publicLink_itemId + WHERE publicLink_id = ? LIMIT 1'; $Data = new QueryData(); - $Data->setMapClassName(PublicLinkBaseData::class); + $Data->setMapClassName(PublicLinkListData::class); $Data->setQuery($query); $Data->addParam($id); @@ -588,6 +567,18 @@ class PublicLinkService extends Service implements ServiceItemInterface return $queryRes; } + /** + * Returns the hash from a composed key + * + * @param string $key + * @param Config $config + * @return mixed + */ + public static function getHashForKey($key, Config $config) + { + return str_replace($config->getConfigData()->getPasswordSalt(), '', $key); + } + /** * @param $hash string * @return bool|PublicLinkData @@ -596,30 +587,40 @@ class PublicLinkService extends Service implements ServiceItemInterface public function getByHash($hash) { $query = /** @lang SQL */ - 'SELECT publicLink_id, - publicLink_hash, - publicLink_linkData - FROM publicLinks WHERE publicLink_hash = ? LIMIT 1'; + 'SELECT publicLink_id, + publicLink_itemId, + publicLink_hash, + publicLink_data, + publicLink_userId, + publicLink_typeId, + publicLink_notify, + publicLink_dateAdd, + publicLink_dateExpire, + publicLink_countViews, + publicLink_maxCountViews, + publicLink_totalCountViews, + publicLink_useInfo, + user_name, + user_login, + account_name + FROM publicLinks + INNER JOIN usrData ON user_id = publicLink_userId + INNER JOIN accounts ON account_id = publicLink_itemId + WHERE publicLink_hash = ? LIMIT 1'; $Data = new QueryData(); - $Data->setMapClassName(PublicLinkBaseData::class); + $Data->setMapClassName(PublicLinkData::class); $Data->setQuery($query); $Data->addParam($hash); - /** @var PublicLinkBaseData $queryRes */ + /** @var PublicLinkData $queryRes */ $queryRes = DbWrapper::getResults($Data); if ($queryRes === false) { throw new SPException(SPException::SP_ERROR, __u('Error al obtener enlace')); } - /** - * @var $publicLinkData PublicLinkData - */ - $publicLinkData = Util::unserialize(PublicLinkData::class, $queryRes->getPublicLinkLinkData()); - $publicLinkData->setPublicLinkId($queryRes->getPublicLinkId()); - - return $publicLinkData; + return $queryRes; } /** @@ -635,7 +636,7 @@ class PublicLinkService extends Service implements ServiceItemInterface 'SELECT publicLink_id, publicLink_hash FROM publicLinks WHERE publicLink_itemId = ? LIMIT 1'; $Data = new QueryData(); - $Data->setMapClassName(PublicLinkBaseData::class); + $Data->setMapClassName(PublicLinkData::class); $Data->setQuery($query); $Data->addParam($itemId); @@ -648,21 +649,4 @@ class PublicLinkService extends Service implements ServiceItemInterface return $queryRes; } - /** - * Devolver la clave y el IV para el enlace - * - * @param PublicLinkData $publicLinkData - * @param Config $config - * @throws \Defuse\Crypto\Exception\BadFormatException - * @throws \Defuse\Crypto\Exception\CryptoException - * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException - */ - protected function createLinkPass(PublicLinkData $publicLinkData, Config $config) - { - $key = $config->getConfigData()->getPasswordSalt() . self::createLinkHash($publicLinkData); - $securedKey = Crypt::makeSecuredKey($key); - - $publicLinkData->setPass(Crypt::encrypt(CryptSession::getSessionKey(), $securedKey, $key)); - $publicLinkData->setPassIV($securedKey); - } } \ No newline at end of file diff --git a/lib/SP/Util/ErrorUtil.php b/lib/SP/Util/ErrorUtil.php index 3d1d41ea..4bd13945 100644 --- a/lib/SP/Util/ErrorUtil.php +++ b/lib/SP/Util/ErrorUtil.php @@ -25,7 +25,7 @@ namespace SP\Util; use SP\Core\Exceptions\SPException; -use SP\Core\Template; +use SP\Mvc\View\Template; /** * Class ErrorUtil @@ -47,8 +47,8 @@ class ErrorUtil /** * Establecer la plantilla de error con el código indicado. * - * @param Template $view - * @param int $type int con el tipo de error + * @param \SP\Mvc\View\Template $view + * @param int $type int con el tipo de error */ public static function showErrorInViewAndReset(Template $view, $type) { @@ -60,8 +60,8 @@ class ErrorUtil /** * Establecer la plantilla de error con el código indicado. * - * @param Template $view - * @param int $type int con el tipo de error + * @param \SP\Mvc\View\Template $view + * @param int $type int con el tipo de error */ public static function showErrorInView(Template $view, $type) { @@ -118,9 +118,9 @@ class ErrorUtil /** * Establecer la plantilla de error con el código indicado. * - * @param Template $view - * @param int $type int con el tipo de error - * @param string $replace Template replacement + * @param \SP\Mvc\View\Template $view + * @param int $type int con el tipo de error + * @param string $replace Template replacement */ public static function showErrorFull(Template $view, $type, $replace) {