File indexing completed on 2024-12-22 05:33:08

0001 <?php
0002 
0003 /**
0004  * ocs-fileserver
0005  *
0006  * Copyright 2016 by pling GmbH.
0007  *
0008  * This file is part of ocs-fileserver.
0009  *
0010  * ocs-fileserver is free software: you can redistribute it and/or modify
0011  * it under the terms of the GNU Affero General Public License as published by
0012  * the Free Software Foundation, either version 3 of the License, or
0013  * (at your option) any later version.
0014  *
0015  * ocs-fileserver is distributed in the hope that it will be useful,
0016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0018  * GNU Affero General Public License for more details.
0019  *
0020  * You should have received a copy of the GNU Affero General Public License
0021  * along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
0022  **/
0023 
0024 require_once 'models/table_profiles.php';
0025 require_once 'models/table_collections.php';
0026 require_once 'models/table_collections_downloaded.php';
0027 require_once 'models/table_files.php';
0028 require_once 'models/table_files_downloaded.php';
0029 require_once 'models/table_files_downloaded_all.php';
0030 require_once 'models/table_files_downloaded_unique.php';
0031 require_once 'models/table_favorites.php';
0032 require_once 'models/table_media.php';
0033 require_once 'models/table_media_artists.php';
0034 require_once 'models/table_media_albums.php';
0035 require_once 'models/table_media_played.php';
0036 
0037 class ModelContainer
0038 {
0039 
0040     public $profiles = null;
0041     public $collections = null;
0042     public $collections_downloaded = null;
0043     public $files = null;
0044     public $files_downloaded = null;
0045     public $files_downloaded_all = null;
0046     public $files_downloaded_unique = null;
0047     public $favorites = null;
0048     public $media = null;
0049     public $media_artists = null;
0050     public $media_albums = null;
0051     public $media_played = null;
0052     protected $_db = null;
0053     protected $_config = array('createTables' => true);
0054 
0055     public function __construct(Flooer_Db &$db, array $config = null)
0056     {
0057         $this->_db =& $db;
0058         if ($config) {
0059             $this->_config = $config + $this->_config;
0060         }
0061 
0062         if ($this->_config['createTables']) {
0063             $this->createTables();
0064         }
0065 
0066         $this->profiles = new table_profiles($this->_db);
0067         $this->collections = new table_collections($this->_db);
0068         $this->collections_downloaded = new table_collections_downloaded($this->_db);
0069         $this->files = new table_files($this->_db);
0070         $this->files_downloaded = new table_files_downloaded($this->_db);
0071         $this->files_downloaded_all = new table_files_downloaded_all($this->_db);
0072         $this->files_downloaded_unique = new table_files_downloaded_unique($this->_db);
0073         $this->favorites = new table_favorites($this->_db);
0074         $this->media = new table_media($this->_db);
0075         $this->media_artists = new table_media_artists($this->_db);
0076         $this->media_albums = new table_media_albums($this->_db);
0077         $this->media_played = new table_media_played($this->_db);
0078     }
0079 
0080     public function createTables()
0081     {
0082         $idDifinition = 'INTEGER NOT NULL PRIMARY KEY';
0083         $timestampDifinition = 'CHAR(19)';
0084         switch ($this->_db->getAttribute(Flooer_Db::ATTR_DRIVER_NAME)) {
0085             case 'mysql':
0086                 //$idDifinition = 'INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY';
0087                 $timestampDifinition = 'DATETIME';
0088                 break;
0089             case 'pgsql':
0090                 //$idDifinition = 'SERIAL PRIMARY KEY';
0091                 $timestampDifinition = 'TIMESTAMP';
0092                 break;
0093             case 'sqlite':
0094                 //$idDifinition = 'INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT';
0095                 $timestampDifinition = 'CHAR(19)';
0096                 break;
0097         }
0098 
0099         if (!isset($this->_db->profiles)) {
0100             $this->_db->profiles = array('id'          => $idDifinition,
0101                                          'active'      => 'INTEGER(1) NOT NULL DEFAULT 1',
0102                                          'client_id'   => 'INTEGER NOT NULL',
0103                                          'owner_id'    => 'VARCHAR(255) NOT NULL',
0104                                          'name'        => 'VARCHAR(255) NOT NULL',
0105                                          'email'       => 'VARCHAR(255)',
0106                                          'homepage'    => 'VARCHAR(255)',
0107                                          'image'       => 'VARCHAR(255)',
0108                                          'description' => 'TEXT');
0109         }
0110 
0111         if (!isset($this->_db->collections)) {
0112             $this->_db->collections = array('id'                   => $idDifinition,
0113                                             'active'               => 'INTEGER(1) NOT NULL DEFAULT 1',
0114                                             'client_id'            => 'INTEGER NOT NULL',
0115                                             'owner_id'             => 'VARCHAR(255) NOT NULL',
0116                                             'name'                 => 'VARCHAR(255) NOT NULL',
0117                                             'files'                => 'INTEGER NOT NULL',
0118                                             'size'                 => 'BIGINT UNSIGNED NOT NULL',
0119                                             'title'                => 'VARCHAR(200)',
0120                                             'description'          => 'TEXT',
0121                                             'category'             => 'VARCHAR(64)',
0122                                             'tags'                 => 'TEXT',
0123                                             'version'              => 'VARCHAR(64)',
0124                                             'content_id'           => 'VARCHAR(255)',
0125                                             'content_page'         => 'VARCHAR(255)',
0126                                             'downloaded_timestamp' => $timestampDifinition,
0127                                             'downloaded_ip'        => 'VARCHAR(39)',
0128                                             'downloaded_count'     => 'INTEGER',
0129                                             'created_timestamp'    => $timestampDifinition,
0130                                             'created_ip'           => 'VARCHAR(39)',
0131                                             'updated_timestamp'    => $timestampDifinition,
0132                                             'updated_ip'           => 'VARCHAR(39)');
0133         }
0134 
0135         if (!isset($this->_db->collections_downloaded)) {
0136             $this->_db->collections_downloaded = array('id'                   => $idDifinition,
0137                                                        'client_id'            => 'INTEGER NOT NULL',
0138                                                        'owner_id'             => 'VARCHAR(255) NOT NULL',
0139                                                        'collection_id'        => 'INTEGER NOT NULL',
0140                                                        'user_id'              => 'VARCHAR(255)',
0141                                                        'referer'              => 'VARCHAR(255)',
0142                                                        'downloaded_timestamp' => $timestampDifinition,
0143                                                        'downloaded_ip'        => 'VARCHAR(39)');
0144         }
0145 
0146         if (!isset($this->_db->files)) {
0147             $this->_db->files = array('id'                   => $idDifinition,
0148                                       'origin_id'            => 'INTEGER NOT NULL',
0149                                       'active'               => 'INTEGER(1) NOT NULL DEFAULT 1',
0150                                       'client_id'            => 'INTEGER NOT NULL',
0151                                       'owner_id'             => 'VARCHAR(255) NOT NULL',
0152                                       'collection_id'        => 'INTEGER NOT NULL',
0153                                       'name'                 => 'VARCHAR(255) NOT NULL',
0154                                       'type'                 => 'VARCHAR(255) NOT NULL',
0155                                       'size'                 => 'BIGINT UNSIGNED NOT NULL',
0156                                       'md5sum'               => 'VARCHAR(200)',
0157                                       'title'                => 'VARCHAR(200)',
0158                                       'description'          => 'TEXT',
0159                                       'category'             => 'VARCHAR(64)',
0160                                       'tags'                 => 'TEXT',
0161                                       'version'              => 'VARCHAR(64)',
0162                                       'ocs_compatible'       => 'INTEGER(1) NOT NULL DEFAULT 1',
0163                                       'content_id'           => 'VARCHAR(255)',
0164                                       'content_page'         => 'VARCHAR(255)',
0165                                       'downloaded_timestamp' => $timestampDifinition,
0166                                       'downloaded_ip'        => 'VARCHAR(39)',
0167                                       'downloaded_count'     => 'INTEGER',
0168                                       'created_timestamp'    => $timestampDifinition,
0169                                       'created_ip'           => 'VARCHAR(39)',
0170                                       'updated_timestamp'    => $timestampDifinition,
0171                                       'updated_ip'           => 'VARCHAR(39)');
0172         }
0173 
0174         if (!isset($this->_db->files_downloaded)) {
0175             $this->_db->files_downloaded = array('id'                   => $idDifinition,
0176                                                  'client_id'            => 'INTEGER NOT NULL',
0177                                                  'owner_id'             => 'VARCHAR(255) NOT NULL',
0178                                                  'collection_id'        => 'INTEGER NOT NULL',
0179                                                  'file_id'              => 'INTEGER NOT NULL',
0180                                                  'user_id'              => 'VARCHAR(255)',
0181                                                  'referer'              => 'VARCHAR(255)',
0182                                                  'downloaded_timestamp' => $timestampDifinition,
0183                                                  'downloaded_ip'        => 'VARCHAR(39)');
0184         }
0185 
0186         if (!isset($this->_db->files_downloaded_all)) {
0187             $this->_db->files_downloaded_all = array('id'                   => $idDifinition,
0188                                                      'client_id'            => 'INTEGER NOT NULL',
0189                                                      'owner_id'             => 'VARCHAR(255) NOT NULL',
0190                                                      'collection_id'        => 'INTEGER NOT NULL',
0191                                                      'file_id'              => 'INTEGER NOT NULL',
0192                                                      'user_id'              => 'VARCHAR(255)',
0193                                                      'referer'              => 'VARCHAR(255)',
0194                                                      'downloaded_timestamp' => $timestampDifinition,
0195                                                      'downloaded_ip'        => 'VARCHAR(39)',
0196                                                      'source'               => 'VARCHAR(39)',
0197                                                      'link_type'            => 'VARCHAR(39)',
0198                                                      'user_agent'           => 'VARCHAR(255)',
0199                                                      'app_id'               => 'INT',
0200                                                      'fingerprint'          => 'VARCHAR(255)',
0201 
0202             );
0203         }
0204 
0205         if (!isset($this->_db->files_downloaded_unique)) {
0206             $this->_db->files_downloaded_unique = array('id'                   => $idDifinition,
0207                                                         'client_id'            => 'INTEGER NOT NULL',
0208                                                         'owner_id'             => 'VARCHAR(255) NOT NULL',
0209                                                         'collection_id'        => 'INTEGER NOT NULL',
0210                                                         'file_id'              => 'INTEGER NOT NULL',
0211                                                         'user_id'              => 'VARCHAR(255)',
0212                                                         'referer'              => 'VARCHAR(255)',
0213                                                         'downloaded_timestamp' => $timestampDifinition,
0214                                                         'downloaded_ip'        => 'VARCHAR(39)');
0215         }
0216 
0217         if (!isset($this->_db->favorites)) {
0218             $this->_db->favorites = array('id'            => $idDifinition,
0219                                           'client_id'     => 'INTEGER NOT NULL',
0220                                           'user_id'       => 'VARCHAR(255) NOT NULL',
0221                                           'owner_id'      => 'VARCHAR(255) NOT NULL',
0222                                           'collection_id' => 'INTEGER',
0223                                           'file_id'       => 'INTEGER');
0224         }
0225 
0226         if (!isset($this->_db->media)) {
0227             $this->_db->media = array('id'               => $idDifinition,
0228                                       'client_id'        => 'INTEGER NOT NULL',
0229                                       'owner_id'         => 'VARCHAR(255) NOT NULL',
0230                                       'collection_id'    => 'INTEGER NOT NULL',
0231                                       'file_id'          => 'INTEGER NOT NULL',
0232                                       'artist_id'        => 'INTEGER NOT NULL',
0233                                       'album_id'         => 'INTEGER NOT NULL',
0234                                       'title'            => 'VARCHAR(255)',
0235                                       'genre'            => 'VARCHAR(64)',
0236                                       'track'            => 'VARCHAR(5)',
0237                                       'creationdate'     => 'INTEGER(4)',
0238                                       'bitrate'          => 'INTEGER',
0239                                       'playtime_seconds' => 'INTEGER',
0240                                       'playtime_string'  => 'VARCHAR(8)',
0241                                       'played_timestamp' => $timestampDifinition,
0242                                       'played_ip'        => 'VARCHAR(39)',
0243                                       'played_count'     => 'INTEGER');
0244         }
0245 
0246         if (!isset($this->_db->media_artists)) {
0247             $this->_db->media_artists = array('id'        => $idDifinition,
0248                                               'client_id' => 'INTEGER NOT NULL',
0249                                               'name'      => 'VARCHAR(255) NOT NULL');
0250         }
0251 
0252         if (!isset($this->_db->media_albums)) {
0253             $this->_db->media_albums = array('id'        => $idDifinition,
0254                                              'client_id' => 'INTEGER NOT NULL',
0255                                              'name'      => 'VARCHAR(255) NOT NULL');
0256         }
0257 
0258         if (!isset($this->_db->media_played)) {
0259             $this->_db->media_played = array('id'               => $idDifinition,
0260                                              'client_id'        => 'INTEGER NOT NULL',
0261                                              'owner_id'         => 'VARCHAR(255) NOT NULL',
0262                                              'collection_id'    => 'INTEGER NOT NULL',
0263                                              'file_id'          => 'INTEGER NOT NULL',
0264                                              'media_id'         => 'INTEGER NOT NULL',
0265                                              'user_id'          => 'VARCHAR(255)',
0266                                              'played_timestamp' => $timestampDifinition,
0267                                              'played_ip'        => 'VARCHAR(39)');
0268         }
0269     }
0270 
0271     public function getDb()
0272     {
0273         return $this->_db;
0274     }
0275 
0276 }