"Fossies" - the Fresh Open Source Software Archive

Member "ampache-5.0.0/src/Repository/AccessRepository.php" (31 Aug 2021, 4481 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 "AccessRepository.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=1);
   24 
   25 namespace Ampache\Repository;
   26 
   27 use Ampache\Module\System\Dba;
   28 
   29 final class AccessRepository implements AccessRepositoryInterface
   30 {
   31     /**
   32      * Returns a full listing of all access rules on this server
   33      * @return int[]
   34      */
   35     public function getAccessLists(): array
   36     {
   37         $sql = 'SELECT `id` FROM `access_list`';
   38 
   39         $db_results = Dba::read($sql);
   40         $results    = array();
   41         while ($row = Dba::fetch_assoc($db_results)) {
   42             $results[] = (int) $row['id'];
   43         }
   44 
   45         return $results;
   46     }
   47 
   48     /**
   49      * Searches for certain ip and config. Returns true if a match was found
   50      */
   51     public function findByIp(
   52         string $userIp,
   53         int $level,
   54         string $type,
   55         ?int $userId
   56     ): bool {
   57         $sql = 'SELECT `id` FROM `access_list` WHERE `start` <= ? AND `end` >= ? AND `level` >= ? AND `type` = ?';
   58 
   59         $params  = array(inet_pton($userIp), inet_pton($userIp), $level, $type);
   60 
   61         if ($userId !== null && $userId != -1) {
   62             $sql .= " AND `user` IN(?, '-1')";
   63             $params[] = $userId;
   64         } else {
   65             $sql .= " AND `user` = '-1'";
   66         }
   67 
   68         return Dba::num_rows(Dba::read($sql, $params)) > 0;
   69     }
   70 
   71     /**
   72      * deletes the specified access_list entry
   73      */
   74     public function delete(int $accessId): void
   75     {
   76         Dba::write("DELETE FROM `access_list` WHERE `id` = ?;", [$accessId]);
   77     }
   78 
   79     /**
   80      * This sees if the ACL that we've specified already exists in order to
   81      * prevent duplicates. The name is ignored.
   82      */
   83     public function exists(
   84         string $inAddrStart,
   85         string $inAddrEnd,
   86         string $type,
   87         int $userId
   88     ): bool {
   89         $db_results = Dba::read(
   90             'SELECT * FROM `access_list` WHERE `start` = ? AND `end` = ? AND `type` = ? AND `user` = ?',
   91             [$inAddrStart, $inAddrEnd, $type, $userId]
   92         );
   93 
   94         return Dba::num_rows($db_results) > 0;
   95     }
   96 
   97     /**
   98      * Creates a new acl item
   99      *
  100      * @param string $startIp The startip in in-addr notation
  101      * @param string $endIp The end ip in in-addr notation
  102      * @param string $name Name of the acl
  103      * @param integer $userId Designated user id (or -1 if none)
  104      * @param integer $level Access level
  105      * @param string $type Access type
  106      */
  107     public function create(
  108         string $startIp,
  109         string $endIp,
  110         string $name,
  111         int $userId,
  112         int $level,
  113         string $type
  114     ): void {
  115         Dba::write(
  116             'INSERT INTO `access_list` (`name`, `level`, `start`, `end`, `user`, `type`) VALUES (?, ?, ?, ?, ?, ?)',
  117             [$name, $level, $startIp, $endIp, $userId, $type]
  118         );
  119     }
  120 
  121     /**
  122      * Updates the data of a certain acl item
  123      *
  124      * @param integer $accessId Id of an existing acl item
  125      * @param string $startIp The startip in in-addr notation
  126      * @param string $endIp The end ip in in-addr notation
  127      * @param string $name Name of the acl
  128      * @param integer $userId Designated user id (or -1 if none)
  129      * @param integer $level Access level
  130      * @param string $type Access type
  131      */
  132     public function update(
  133         int $accessId,
  134         string $startIp,
  135         string $endIp,
  136         string $name,
  137         int $userId,
  138         int $level,
  139         string $type
  140     ): void {
  141         Dba::write(
  142             'UPDATE `access_list` SET `start` = ?, `end` = ?, `level` = ?, `user` = ?, `name` = ?, `type` = ? WHERE `id` = ?',
  143             [$startIp, $endIp, $level, $userId, $name, $type, $accessId]
  144         );
  145     }
  146 }