"Fossies" - the Fresh Open Source Software Archive

Member "ampache-5.0.0/src/Module/Application/PrivateMessage/AddMessageAction.php" (31 Aug 2021, 4604 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 "AddMessageAction.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\PrivateMessage;
   26 
   27 use Ampache\Config\ConfigContainerInterface;
   28 use Ampache\Config\ConfigurationKeyEnum;
   29 use Ampache\Repository\Model\ModelFactoryInterface;
   30 use Ampache\Repository\Model\User;
   31 use Ampache\Module\Application\ApplicationActionInterface;
   32 use Ampache\Module\Application\Exception\AccessDeniedException;
   33 use Ampache\Module\Authorization\AccessLevelEnum;
   34 use Ampache\Module\Authorization\GuiGatekeeperInterface;
   35 use Ampache\Module\System\AmpError;
   36 use Ampache\Module\User\PrivateMessage\Exception\PrivateMessageCreationException;
   37 use Ampache\Module\User\PrivateMessage\PrivateMessageCreatorInterface;
   38 use Ampache\Module\Util\UiInterface;
   39 use PHPMailer\PHPMailer\Exception;
   40 use Psr\Http\Message\ResponseInterface;
   41 use Psr\Http\Message\ServerRequestInterface;
   42 
   43 final class AddMessageAction implements ApplicationActionInterface
   44 {
   45     public const REQUEST_KEY = 'add_message';
   46 
   47     private ConfigContainerInterface $configContainer;
   48 
   49     private UiInterface $ui;
   50 
   51     private PrivateMessageCreatorInterface $privateMessageCreator;
   52 
   53     private ModelFactoryInterface $modelFactory;
   54 
   55     public function __construct(
   56         ConfigContainerInterface $configContainer,
   57         UiInterface $ui,
   58         PrivateMessageCreatorInterface $privateMessageCreator,
   59         ModelFactoryInterface $modelFactory
   60     ) {
   61         $this->configContainer       = $configContainer;
   62         $this->ui                    = $ui;
   63         $this->privateMessageCreator = $privateMessageCreator;
   64         $this->modelFactory          = $modelFactory;
   65     }
   66 
   67     public function run(ServerRequestInterface $request, GuiGatekeeperInterface $gatekeeper): ?ResponseInterface
   68     {
   69         if (
   70             $gatekeeper->mayAccess(AccessLevelEnum::TYPE_INTERFACE, AccessLevelEnum::LEVEL_USER) === false ||
   71             $this->configContainer->isFeatureEnabled(ConfigurationKeyEnum::SOCIABLE) === false
   72         ) {
   73             throw new AccessDeniedException('Access Denied: sociable features are not enabled.');
   74         }
   75         if ($this->configContainer->isFeatureEnabled(ConfigurationKeyEnum::DEMO_MODE) === true) {
   76             return null;
   77         }
   78 
   79         $data = $request->getParsedBody();
   80 
   81         $this->ui->showHeader();
   82 
   83         $subject = trim(strip_tags(filter_var($data['subject'] ?? '', FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES)));
   84         $message = trim(strip_tags(filter_var($data['message'] ?? '', FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES)));
   85         $to_user = User::get_from_username($data['to_user'] ?? '');
   86 
   87         if (!$to_user->id) {
   88             AmpError::add('to_user', T_('Unknown user'));
   89         }
   90         if (empty($subject)) {
   91             AmpError::add('subject', T_('Subject is required'));
   92         }
   93         if (AmpError::occurred()) {
   94             $this->ui->show('show_add_pvmsg.inc.php');
   95             $this->ui->showQueryStats();
   96             $this->ui->showFooter();
   97 
   98             return null;
   99         }
  100 
  101         try {
  102             $this->privateMessageCreator->create(
  103                 $to_user,
  104                 $this->modelFactory->createUser($gatekeeper->getUserId()),
  105                 $subject,
  106                 $message
  107             );
  108 
  109             $this->ui->showConfirmation(
  110                 T_('No Problem'),
  111                 T_('Message has been sent'),
  112                 sprintf(
  113                     '%s/browse.php?action=pvmsg',
  114                     $this->configContainer->getWebPath()
  115                 )
  116             );
  117         } catch (PrivateMessageCreationException | Exception $e) {
  118             $this->ui->show('show_add_pvmsg.inc.php');
  119         }
  120 
  121         $this->ui->showQueryStats();
  122         $this->ui->showFooter();
  123 
  124         return null;
  125     }
  126 }