"Fossies" - the Fresh Open Source Software Archive

Member "ampache-5.0.0/src/Module/Application/Admin/Access/AddHostAction.php" (31 Aug 2021, 4829 Bytes) of package /linux/www/ampache-5.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "AddHostAction.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 /*
    3  * vim:set softtabstop=4 shiftwidth=4 expandtab:
    4  *
    5  * LICENSE: GNU Affero General Public License, version 3 (AGPL-3.0-or-later)
    6  * Copyright 2001 - 2020 Ampache.org
    7  *
    8  * This program is free software: you can redistribute it and/or modify
    9  * it under the terms of the GNU Affero General Public License as published by
   10  * the Free Software Foundation, either version 3 of the License, or
   11  * (at your option) any later version.
   12  *
   13  * This program is distributed in the hope that it will be useful,
   14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16  * GNU Affero General Public License for more details.
   17  *
   18  * You should have received a copy of the GNU Affero General Public License
   19  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
   20  *
   21  */
   22 
   23 declare(strict_types=0);
   24 
   25 namespace Ampache\Module\Application\Admin\Access;
   26 
   27 use Ampache\Config\ConfigContainerInterface;
   28 use Ampache\Module\Application\ApplicationActionInterface;
   29 use Ampache\Module\Application\Exception\AccessDeniedException;
   30 use Ampache\Module\Authorization\AccessLevelEnum;
   31 use Ampache\Module\Authorization\AccessListManagerInterface;
   32 use Ampache\Module\Authorization\Exception\InvalidEndIpException;
   33 use Ampache\Module\Authorization\Exception\InvalidIpRangeException;
   34 use Ampache\Module\Authorization\Exception\InvalidStartIpException;
   35 use Ampache\Module\Authorization\GuiGatekeeperInterface;
   36 use Ampache\Module\System\AmpError;
   37 use Ampache\Module\System\Core;
   38 use Ampache\Module\System\LegacyLogger;
   39 use Ampache\Module\Util\UiInterface;
   40 use PHPUnit\Framework\MockObject\DuplicateMethodException;
   41 use Psr\Http\Message\ResponseInterface;
   42 use Psr\Http\Message\ServerRequestInterface;
   43 use Psr\Log\LoggerInterface;
   44 
   45 final class AddHostAction implements ApplicationActionInterface
   46 {
   47     public const REQUEST_KEY = 'add_host';
   48 
   49     private UiInterface $ui;
   50 
   51     private ConfigContainerInterface $configContainer;
   52 
   53     private AccessListManagerInterface $accessListManager;
   54 
   55     private LoggerInterface $logger;
   56 
   57     public function __construct(
   58         UiInterface $ui,
   59         ConfigContainerInterface $configContainer,
   60         AccessListManagerInterface $accessListManager,
   61         LoggerInterface $logger
   62     ) {
   63         $this->ui                = $ui;
   64         $this->configContainer   = $configContainer;
   65         $this->accessListManager = $accessListManager;
   66         $this->logger            = $logger;
   67     }
   68 
   69     public function run(ServerRequestInterface $request, GuiGatekeeperInterface $gatekeeper): ?ResponseInterface
   70     {
   71         // Make sure we've got a valid form submission
   72         if (
   73             $gatekeeper->mayAccess(AccessLevelEnum::TYPE_INTERFACE, AccessLevelEnum::LEVEL_ADMIN) === false ||
   74             !Core::form_verify('add_acl')
   75         ) {
   76             throw new AccessDeniedException();
   77         }
   78 
   79         $this->ui->showHeader();
   80 
   81         $data = $request->getParsedBody();
   82 
   83         $startIp = $data['start'] ?? '';
   84         $endIp   = $data['end'] ?? '';
   85 
   86         try {
   87             $this->accessListManager->create(
   88                 $startIp,
   89                 $endIp,
   90                 $data['name'] ?? '',
   91                 (int) ($data['user'] ?: -1),
   92                 (int) $data['level'] ?? 0,
   93                 $data['type'] ?? '',
   94                 $data['addtype'] ?? ''
   95             );
   96         } catch (InvalidIpRangeException $e) {
   97             AmpError::add('start', T_('IP Address version mismatch'));
   98             AmpError::add('end', T_('IP Address version mismatch'));
   99         } catch (InvalidStartIpException $e) {
  100             AmpError::add('start', T_('An Invalid IPv4 / IPv6 Address was entered'));
  101         } catch (InvalidEndIpException $e) {
  102             AmpError::add('end', T_('An Invalid IPv4 / IPv6 Address was entered'));
  103         } catch (DuplicateMethodException $e) {
  104             $this->logger->critical(
  105                 'Error: An ACL entry equal to the created one already exists. Not adding duplicate: ' . $startIp . ' - ' . $endIp,
  106                 [LegacyLogger::CONTEXT_TYPE => __CLASS__]
  107             );
  108 
  109             AmpError::add('general', T_('Duplicate ACL entry defined'));
  110         }
  111 
  112         if (!AmpError::occurred()) {
  113             $this->ui->showConfirmation(
  114                 T_('No Problem'),
  115                 T_('Your new Access Control List(s) have been created'),
  116                 sprintf(
  117                     '%s/admin/access.php',
  118                     $this->configContainer->getWebPath()
  119                 )
  120             );
  121         } else {
  122             $this->ui->show(
  123                 'show_add_access.inc.php',
  124                 ['action' => 'show_add_' . Core::get_post('type')]
  125             );
  126         }
  127 
  128         $this->ui->showQueryStats();
  129         $this->ui->showFooter();
  130 
  131         return null;
  132     }
  133 }