File indexing completed on 2025-02-16 05:25:42
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 class table_files extends BaseModel 0025 { 0026 0027 protected $_columns = ''; 0028 0029 protected $_join = ''; 0030 0031 public function __construct(&$db) 0032 { 0033 parent::__construct($db, $db->getTableConfig()); 0034 $this->setName('files'); 0035 $this->setPrimaryInsert(true); 0036 0037 $prefix = $this->getPrefix(); 0038 0039 $this->_columns = "{$prefix}files.id AS id," . "{$prefix}files.origin_id AS origin_id," . "{$prefix}files.active AS active," . "{$prefix}files.client_id AS client_id," . "{$prefix}files.owner_id AS owner_id," . "{$prefix}profiles.id AS profile_id," . "{$prefix}profiles.name AS profile_name," . "{$prefix}files.collection_id AS collection_id," . "{$prefix}collections.active AS collection_active," . "{$prefix}collections.title AS collection_title," . "{$prefix}collections.category AS collection_category," . "{$prefix}collections.tags AS collection_tags," . "{$prefix}collections.version AS collection_version," . "{$prefix}collections.content_id AS collection_content_id," . "{$prefix}collections.content_page AS collection_content_page," . "{$prefix}files.name AS name," . "{$prefix}files.type AS type," . "{$prefix}files.size AS size," . "{$prefix}files.md5sum AS md5sum," . "{$prefix}files.title AS title," . "{$prefix}files.description AS description," . "{$prefix}files.category AS category," . "{$prefix}files.tags AS tags," . "{$prefix}files.version AS version," . "{$prefix}files.ocs_compatible AS ocs_compatible," . "{$prefix}files.content_id AS content_id," . "{$prefix}files.content_page AS content_page," . "{$prefix}files.downloaded_timestamp AS downloaded_timestamp," . "{$prefix}files.downloaded_count AS downloaded_count," . "{$prefix}files.downloaded_count AS downloaded_timeperiod_count," . "{$prefix}files.created_timestamp AS created_timestamp," . "{$prefix}files.updated_timestamp AS updated_timestamp"; 0040 0041 $this->_join = "LEFT OUTER JOIN {$prefix}profiles" . " ON {$prefix}profiles.client_id = {$prefix}files.client_id" . " AND {$prefix}profiles.owner_id = {$prefix}files.owner_id" . " LEFT OUTER JOIN {$prefix}collections" . " ON {$prefix}collections.id = {$prefix}files.collection_id"; 0042 } 0043 0044 public function __set($key, $value) 0045 { 0046 $value = $this->_convertArrayToObject($value); 0047 unset($value->id, $value->created_timestamp, $value->created_ip, $value->updated_timestamp, $value->updated_ip); 0048 $value->updated_timestamp = $this->_getTimestamp(); 0049 $value->updated_ip = $this->_getIp(); 0050 if (!isset($this->$key)) { 0051 $value->downloaded_timestamp = $value->updated_timestamp; 0052 $value->downloaded_ip = $value->updated_ip; 0053 //$value->downloaded_count = 0; 0054 $value->downloaded_count = $value->downloaded_count; // for hive files importing (Deprecated) 0055 $value->created_timestamp = $value->updated_timestamp; 0056 $value->created_ip = $value->updated_ip; 0057 } 0058 parent::__set($key, $value); 0059 } 0060 0061 public function getFiles($originId = null, 0062 $status = 'active', 0063 $clientId = null, 0064 $ownerId = null, 0065 $collectionId = null, 0066 $collectionStatus = 'active', 0067 $collectionCategory = null, 0068 $collectionTags = null, 0069 $collectionContentId = null, 0070 $types = null, 0071 $category = null, 0072 $tags = null, 0073 $ocsCompatibility = 'all', 0074 $contentId = null, 0075 $search = null, 0076 $ids = null, 0077 array $favoriteIds = null, 0078 $downloadedTimeperiodBegin = null, 0079 $downloadedTimeperiodEnd = null, 0080 $sort = 'name', 0081 $perpage = 20, 0082 $page = 1) 0083 { 0084 $prefix = $this->getPrefix(); 0085 $name = $this->getName(); 0086 $columns = $this->getColumns(); 0087 0088 $statementOption = ''; 0089 $where = array(); 0090 $values = array(); 0091 $order = "{$prefix}files.name ASC"; 0092 $offset = 0; 0093 0094 if ($originId) { 0095 $where[] = "{$prefix}files.origin_id = :origin_id"; 0096 $values[':origin_id'] = $originId; 0097 } 0098 if ($status != 'all') { 0099 $active = 1; 0100 if ($status == 'inactive') { 0101 $active = 0; 0102 } 0103 $where[] = "{$prefix}files.active = :active"; 0104 $values[':active'] = $active; 0105 } 0106 if ($clientId) { 0107 $where[] = "{$prefix}files.client_id = :client_id"; 0108 $values[':client_id'] = $clientId; 0109 } 0110 if ($ownerId) { 0111 $where[] = "{$prefix}files.owner_id = :owner_id"; 0112 $values[':owner_id'] = $ownerId; 0113 } 0114 if ($collectionId) { 0115 $where[] = "{$prefix}files.collection_id = :collection_id"; 0116 $values[':collection_id'] = $collectionId; 0117 } 0118 if ($collectionStatus != 'all') { 0119 $collectionActive = 1; 0120 if ($collectionStatus == 'inactive') { 0121 $collectionActive = 0; 0122 } 0123 $where[] = "{$prefix}collections.active = :collection_active"; 0124 $values[':collection_active'] = $collectionActive; 0125 } 0126 if ($collectionCategory !== null && $collectionCategory !== '') { 0127 $where[] = "{$prefix}collections.category = :collection_category"; 0128 $values[':collection_category'] = $collectionCategory; 0129 } 0130 if ($collectionTags !== null && $collectionTags !== '') { 0131 foreach (explode(',', $collectionTags) as $tag) { 0132 $tag = trim($tag); 0133 if ($tag) { 0134 $where[] = "({$prefix}collections.tags = " . $this->getDb()->quote($tag) 0135 . " OR {$prefix}collections.tags LIKE " . $this->getDb()->quote("$tag,%") 0136 . " OR {$prefix}collections.tags LIKE " . $this->getDb()->quote("%,$tag,%") 0137 . " OR {$prefix}collections.tags LIKE " . $this->getDb()->quote("%,$tag") . ')'; 0138 } 0139 } 0140 } 0141 if ($collectionContentId !== null && $collectionContentId !== '') { 0142 $where[] = "{$prefix}collections.content_id = :collection_content_id"; 0143 $values[':collection_content_id'] = $collectionContentId; 0144 } 0145 if ($types) { 0146 $_types = array(); 0147 foreach (explode(',', $types) as $type) { 0148 $type = trim($type); 0149 if ($type) { 0150 $_types[] = $this->getDb()->quote($type); 0151 } 0152 } 0153 if ($_types) { 0154 $where[] = "{$prefix}files.type IN (" . implode(',', $_types) . ')'; 0155 } 0156 } 0157 if ($category !== null && $category !== '') { 0158 $where[] = "{$prefix}files.category = :category"; 0159 $values[':category'] = $category; 0160 } 0161 if ($tags !== null && $tags !== '') { 0162 foreach (explode(',', $tags) as $tag) { 0163 $tag = trim($tag); 0164 if ($tag) { 0165 $where[] = "({$prefix}files.tags = " . $this->getDb()->quote($tag) 0166 . " OR {$prefix}files.tags LIKE " . $this->getDb()->quote("$tag,%") 0167 . " OR {$prefix}files.tags LIKE " . $this->getDb()->quote("%,$tag,%") 0168 . " OR {$prefix}files.tags LIKE " . $this->getDb()->quote("%,$tag") . ')'; 0169 } 0170 } 0171 } 0172 if ($ocsCompatibility != 'all') { 0173 $ocsCompatible = null; 0174 if ($ocsCompatibility == 'compatible') { 0175 $ocsCompatible = 1; 0176 } else { 0177 if ($ocsCompatibility == 'incompatible') { 0178 $ocsCompatible = 0; 0179 } 0180 } 0181 if ($ocsCompatible !== null) { 0182 $where[] = "{$prefix}files.ocs_compatible = :ocs_compatible"; 0183 $values[':ocs_compatible'] = $ocsCompatible; 0184 } 0185 } 0186 if ($contentId !== null && $contentId !== '') { 0187 $where[] = "{$prefix}files.content_id = :content_id"; 0188 $values[':content_id'] = $contentId; 0189 } 0190 if ($search) { 0191 $isSearchable = false; 0192 foreach (explode(' ', $search) as $keyword) { 0193 if ($keyword && strlen($keyword) > 2) { 0194 $keyword = $this->getDb()->quote("%$keyword%"); 0195 $where[] = "({$prefix}files.name LIKE $keyword" . " OR {$prefix}files.title LIKE $keyword" . " OR {$prefix}files.description LIKE $keyword)"; 0196 $isSearchable = true; 0197 } 0198 } 0199 if (!$isSearchable) { 0200 return null; 0201 } 0202 } 0203 if ($ids) { 0204 $_ids = array(); 0205 foreach (explode(',', $ids) as $id) { 0206 $id = trim($id); 0207 if ($id) { 0208 $_ids[] = $this->getDb()->quote($id); 0209 } 0210 } 0211 if ($_ids) { 0212 $where[] = "{$prefix}files.id IN (" . implode(',', $_ids) . ')'; 0213 } 0214 } 0215 if (!empty($favoriteIds['ownerIds']) || !empty($favoriteIds['collectionIds']) || !empty($favoriteIds['fileIds'])) { 0216 $where[] = $this->_convertFavoriteIdsToStatement($favoriteIds, array('ownerId' => "{$prefix}files.owner_id", 0217 'collectionId' => "{$prefix}files.collection_id", 0218 'fileId' => "{$prefix}files.id")); 0219 } 0220 0221 if ($where) { 0222 $statementOption = 'WHERE ' . implode(' AND ', $where); 0223 } 0224 0225 if ($sort == 'newest') { 0226 $order = "{$prefix}files.id DESC"; 0227 } else { 0228 if ($sort == 'recent') { 0229 $order = "{$prefix}files.downloaded_timestamp DESC"; 0230 } else { 0231 if ($sort == 'frequent') { 0232 $order = "{$prefix}files.downloaded_count DESC"; 0233 } 0234 } 0235 } 0236 0237 if ($page > 1) { 0238 $offset = ($page - 1) * $perpage; 0239 } 0240 0241 $files = null; 0242 $pagination = null; 0243 0244 if ($downloadedTimeperiodBegin || $downloadedTimeperiodEnd) { 0245 $_downloadedTimeperiodBegin = $this->_getTimestamp(0); 0246 if ($downloadedTimeperiodBegin) { 0247 $_downloadedTimeperiodBegin = $downloadedTimeperiodBegin; 0248 } 0249 $_downloadedTimeperiodBegin = $this->getDb()->quote($_downloadedTimeperiodBegin); 0250 0251 $_downloadedTimeperiodEnd = $this->_getTimestamp(); 0252 if ($downloadedTimeperiodEnd) { 0253 $_downloadedTimeperiodEnd = $downloadedTimeperiodEnd; 0254 } 0255 $_downloadedTimeperiodEnd = $this->getDb()->quote($_downloadedTimeperiodEnd); 0256 0257 $_from = '(' . " SELECT {$prefix}files_downloaded.file_id AS file_id," . " COUNT({$prefix}files_downloaded.file_id) AS count" . " FROM {$prefix}files_downloaded" . " WHERE {$prefix}files_downloaded.downloaded_timestamp" . " BETWEEN {$_downloadedTimeperiodBegin} AND {$_downloadedTimeperiodEnd}" . " GROUP BY {$prefix}files_downloaded.file_id" . ') AS downloaded_timeperiod'; 0258 0259 $_join = "LEFT OUTER JOIN {$prefix}files" . " ON {$prefix}files.id = downloaded_timeperiod.file_id" . ' ' . $this->_join; 0260 0261 $_columns = str_replace("{$prefix}files.downloaded_count AS downloaded_timeperiod_count", 'downloaded_timeperiod.count AS downloaded_timeperiod_count', $this->_columns); 0262 0263 if ($sort == 'frequent') { 0264 $order = 'downloaded_timeperiod.count DESC'; 0265 } 0266 0267 $this->setPrefix(''); 0268 $this->setName($_from); 0269 $this->setColumns($_columns); 0270 0271 $files = $this->fetchRowset($_join . ' ' . $statementOption . " ORDER BY $order LIMIT $perpage OFFSET $offset", $values); 0272 0273 $this->setPrefix($prefix); 0274 $this->setName($name); 0275 $this->setColumns($columns); 0276 0277 if (!$files) { 0278 return null; 0279 } 0280 0281 $this->setPrefix(''); 0282 $this->setName($_from); 0283 $this->setColumns($_columns); 0284 0285 $pagination = Flooer_Utility_Pagination::paginate($this->count($_join . ' ' . $statementOption, $values), $perpage, $page); 0286 0287 $this->setPrefix($prefix); 0288 $this->setName($name); 0289 $this->setColumns($columns); 0290 } else { 0291 $this->setColumns($this->_columns); 0292 $files = $this->fetchRowset($this->_join . ' ' . $statementOption . " ORDER BY $order LIMIT $perpage OFFSET $offset", $values); 0293 $this->setColumns($columns); 0294 0295 if (!$files) { 0296 return null; 0297 } 0298 0299 $this->setColumns($this->_columns); 0300 $pagination = Flooer_Utility_Pagination::paginate($this->count($this->_join . ' ' . $statementOption, $values), $perpage, $page); 0301 $this->setColumns($columns); 0302 } 0303 0304 return array('files' => $files, 0305 'pagination' => $pagination); 0306 } 0307 0308 public function getFile($id) 0309 { 0310 $prefix = $this->getPrefix(); 0311 $columns = $this->getColumns(); 0312 0313 $this->setColumns($this->_columns); 0314 $file = $this->fetchRow($this->_join . " WHERE {$prefix}files.id = :id" . ' LIMIT 1', array(':id' => $id)); 0315 $this->setColumns($columns); 0316 0317 if ($file) { 0318 return $file; 0319 } 0320 0321 return null; 0322 } 0323 0324 public function getFileId($id, $of = 'self') 0325 { 0326 $file = $this->$id; 0327 if ($file) { 0328 if ($of == 'self') { 0329 return $file->id; 0330 } else { 0331 if ($of == 'origin') { 0332 return $file->origin_id; 0333 } else { 0334 if ($of == 'latest') { 0335 $latestFile = $this->fetchRow('WHERE origin_id = :origin_id' . ' ORDER BY id DESC LIMIT 1', array(':origin_id' => $file->origin_id)); 0336 0337 return $latestFile->id; 0338 } 0339 } 0340 } 0341 } 0342 0343 return null; 0344 } 0345 0346 public function updateDownloadedStatus($id) 0347 { 0348 if (isset($this->$id)) { 0349 parent::__set($id, array('downloaded_timestamp' => $this->_getTimestamp(), 0350 'downloaded_ip' => $this->_getIp(), 0351 'downloaded_count' => $this->$id->downloaded_count + 1)); 0352 } 0353 } 0354 0355 public function deleteByCollectionId($collectionId) 0356 { 0357 $primary = $this->getPrimary(); 0358 $this->setPrimary('collection_id'); 0359 unset($this->$collectionId); 0360 $this->setPrimary($primary); 0361 } 0362 0363 }