"Fossies" - the Fresh Open Source Software Archive

Member "ampache-5.0.0/src/Module/Application/Stream/AbstractStreamAction.php" (31 Aug 2021, 4931 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 "AbstractStreamAction.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\Stream;
   26 
   27 use Ampache\Config\ConfigContainerInterface;
   28 use Ampache\Config\ConfigurationKeyEnum;
   29 use Ampache\Repository\Model\User;
   30 use Ampache\Module\Application\ApplicationActionInterface;
   31 use Ampache\Module\Application\Exception\AccessDeniedException;
   32 use Ampache\Module\Application\Exception\ApplicationException;
   33 use Ampache\Module\Authorization\AccessLevelEnum;
   34 use Ampache\Module\Authorization\GuiGatekeeperInterface;
   35 use Ampache\Module\Playback\Stream;
   36 use Ampache\Module\Playback\Stream_Playlist;
   37 use Ampache\Module\System\Core;
   38 use Ampache\Module\System\LegacyLogger;
   39 use Ampache\Module\System\Session;
   40 use Psr\Http\Message\ResponseInterface;
   41 use Psr\Log\LoggerInterface;
   42 
   43 abstract class AbstractStreamAction implements ApplicationActionInterface
   44 {
   45     private LoggerInterface $logger;
   46 
   47     private ConfigContainerInterface $configContainer;
   48 
   49     protected function __construct(
   50         LoggerInterface $logger,
   51         ConfigContainerInterface $configContainer
   52     ) {
   53         $this->logger          = $logger;
   54         $this->configContainer = $configContainer;
   55     }
   56 
   57     /**
   58      * @throws ApplicationException
   59      */
   60     protected function preCheck(
   61         GuiGatekeeperInterface $gatekeeper
   62     ): bool {
   63         if (!defined('NO_SESSION')) {
   64             /* If we are running a demo, quick while you still can! */
   65             if (
   66                 $this->configContainer->isFeatureEnabled(ConfigurationKeyEnum::DEMO_MODE) === true ||
   67                 (
   68                     $this->configContainer->isAuthenticationEnabled() &&
   69                     $gatekeeper->mayAccess(AccessLevelEnum::TYPE_INTERFACE, AccessLevelEnum::LEVEL_USER) === false
   70                 )
   71             ) {
   72                 throw new AccessDeniedException();
   73             }
   74         }
   75 
   76         return true;
   77     }
   78 
   79     /**
   80      * @throws ApplicationException
   81      */
   82     protected function stream(
   83         array $mediaIds,
   84         array $urls,
   85         string $streamType = ''
   86     ): ?ResponseInterface {
   87         if ($streamType == 'stream') {
   88             $streamType = $this->configContainer->get(ConfigurationKeyEnum::PLAYLIST_TYPE);
   89         }
   90 
   91         $this->logger->debug(
   92             'Stream Type: ' . $streamType . ' Media IDs: ' . json_encode($mediaIds),
   93             [LegacyLogger::CONTEXT_TYPE => __CLASS__]
   94         );
   95         if ($mediaIds !== [] || $urls !== []) {
   96             if ($streamType != 'democratic') {
   97                 if (!User::stream_control($mediaIds)) {
   98                     $this->logger->warning(
   99                         'Stream control failed for user ' . Core::get_global('user')->username,
  100                         [LegacyLogger::CONTEXT_TYPE => __CLASS__]
  101                     );
  102                     throw new AccessDeniedException();
  103                 }
  104             }
  105 
  106             if (Core::get_global('user')->id > -1) {
  107                 Session::update_username(Stream::get_session(), Core::get_global('user')->username);
  108             }
  109 
  110             $playlist = new Stream_Playlist();
  111 
  112             // don't do this if nothing is there
  113             if ($mediaIds !== []) {
  114                 $this->logger->debug(
  115                     sprintf('Stream Type: %s Media Count: %d', $streamType, count($mediaIds)),
  116                     [LegacyLogger::CONTEXT_TYPE => __CLASS__]
  117                 );
  118                 $playlist->add($mediaIds);
  119             }
  120             if (isset($urls)) {
  121                 $this->logger->debug(
  122                     sprintf('Stream Type: %s Loading URL: %s', $streamType, $urls[0]),
  123                     [LegacyLogger::CONTEXT_TYPE => __CLASS__]
  124                 );
  125                 $playlist->add_urls($urls);
  126             }
  127 
  128             // Depending on the stream type, will either generate a redirect or actually do the streaming.
  129             $playlist->generate_playlist($streamType, false);
  130         } else {
  131             $this->logger->debug(
  132                 'No item. Ignoring...',
  133                 [LegacyLogger::CONTEXT_TYPE => __CLASS__]
  134             );
  135         }
  136 
  137         return null;
  138     }
  139 }