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 class BaseModel extends Flooer_Db_Table
0025 {
0026 
0027     /**
0028      * Generates an ID number for primary key
0029      *
0030      * This method generating a unique ID number based on unix time.
0031      * And the maximum value of the ID has affected by database
0032      * column data type.
0033      *
0034      * Case of INT Signed:
0035      * Max value = 2147483647 (19 Jan 2038 03:14:07 UTC)
0036      * Case of INT Unsigned:
0037      * Max value = 4294967295 (07 Feb 2106 06:28:15 UTC)
0038      * Case of BIGINT Signed:
0039      * Max value = 9223372036854775807 (After 3000 billion years)
0040      */
0041     public function generateId()
0042     {
0043         $id = time() + mt_rand(1, 1000);
0044         while (isset($this->$id)) {
0045             $id = time() + mt_rand(1, 1000);
0046         }
0047 
0048         return $id;
0049     }
0050 
0051     public function generateNewId()
0052     {
0053         $result = $this->_db->query("SELECT UUID_SHORT();");
0054         $res = $result->fetchAll();
0055 
0056         return $res[0]['UUID_SHORT()'];
0057     }
0058 
0059     protected function _getTimestamp($time = null)
0060     {
0061         if ($time === null) {
0062             $time = time();
0063         }
0064 
0065         return date('Y-m-d H:i:s', $time);
0066     }
0067 
0068     protected function _getIp()
0069     {
0070         $ip = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']) : $_SERVER['REMOTE_ADDR'];
0071 
0072         if (is_array($ip)) {
0073             return mb_strimwidth($ip[0],0,39);
0074         }
0075 
0076         return mb_strimwidth($ip,0,39);
0077     }
0078 
0079     protected function _getReferer()
0080     {
0081         if (isset($_SERVER['HTTP_X_FORWARDED_REFERRER'])) {
0082             return mb_strimwidth($_SERVER['HTTP_X_FORWARDED_REFERRER'],0,255);
0083         }
0084         if (!empty($_SERVER['HTTP_REFERER'])) {
0085             return mb_strimwidth($_SERVER['HTTP_REFERER'],0,255);
0086         }
0087 
0088         return null;
0089     }
0090 
0091     protected function _convertArrayToObject($values)
0092     {
0093         if (is_array($values)) {
0094             $values = (object)$values;
0095         }
0096         if (is_object($values)) {
0097             foreach ($values as &$value) {
0098                 $value = $this->_convertArrayToObject($value);
0099             }
0100         }
0101 
0102         return $values;
0103     }
0104 
0105     protected function _convertObjectToArray($values)
0106     {
0107         if (is_object($values)) {
0108             $values = (array)$values;
0109         }
0110         if (is_array($values)) {
0111             foreach ($values as &$value) {
0112                 $value = $this->_convertObjectToArray($value);
0113             }
0114         }
0115 
0116         return $values;
0117     }
0118 
0119     protected function _convertFavoriteIdsToStatement(array $favoriteIds, array $columns)
0120     {
0121         $whereOr = array();
0122 
0123         if (!empty($favoriteIds['ownerIds']) && !empty($columns['ownerId'])) {
0124             $ownerIds = array();
0125             foreach ($favoriteIds['ownerIds'] as $id) {
0126                 $ownerIds[] = $this->getDb()->quote($id);
0127             }
0128             $whereOr[] = "{$columns['ownerId']} IN (" . implode(',', $ownerIds) . ")";
0129         }
0130 
0131         if (!empty($favoriteIds['collectionIds']) && !empty($columns['collectionId'])) {
0132             $collectionIds = array();
0133             foreach ($favoriteIds['collectionIds'] as $id) {
0134                 $collectionIds[] = $this->getDb()->quote($id);
0135             }
0136             $whereOr[] = "{$columns['collectionId']} IN (" . implode(',', $collectionIds) . ")";
0137         }
0138 
0139         if (!empty($favoriteIds['fileIds']) && !empty($columns['fileId'])) {
0140             $fileIds = array();
0141             foreach ($favoriteIds['fileIds'] as $id) {
0142                 $fileIds[] = $this->getDb()->quote($id);
0143             }
0144             $whereOr[] = "{$columns['fileId']} IN (" . implode(',', $fileIds) . ")";
0145         }
0146 
0147         if ($whereOr) {
0148             return '(' . implode(' OR ', $whereOr) . ')';
0149         }
0150 
0151         return '';
0152     }
0153 
0154 }