File indexing completed on 2024-04-21 16:36:32

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_ocs_downloads extends BaseModel
0025 {
0026 
0027     protected $ocsDbConfig;
0028     protected $dbName;
0029 
0030     public function __construct(&$db)
0031     {
0032         parent::__construct($db, $db->getTableConfig());
0033         $this->setName('stat_object_download');
0034         $this->setPrimaryInsert(true);
0035     }
0036 
0037     public function __set($key, $value)
0038     {
0039         $value = $this->_convertArrayToObject($value);
0040         parent::__set($key, $value);
0041     }
0042 
0043     public function save($value)
0044     {
0045         $ipRemoteV6 = filter_var($this->_getIp(), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) ? $this->_getIp() : null;
0046         $ipRemoteV4 = filter_var($this->_getIp(), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) ? $this->_getIp() : null;
0047 
0048         $ipClientv6 = filter_var($value['ip'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) ? $value['ip'] : $ipRemoteV6;
0049         $ipClientv4 = filter_var($value['ip'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) ? $value['ip'] : $ipRemoteV4;
0050 
0051         $sql = ("INSERT IGNORE INTO `{$this->dbName}`.`stat_object_download` (`seen_at`, `ip_inet`, `object_type`, `object_id`, `ipv4`, `ipv6`, `fingerprint`, `user_agent`, `member_id_viewer`) VALUES (:seen, :ip_inet, :object_type, :product_id, :ipv4, :ipv6, :fp, :ua, :member)");
0052         $ip_inet = isset($value['ip']) ? $value['ip'] : $this->_getIp();
0053         $time = (round(time() / 300)) * 300;
0054         $seen_at = date('Y-m-d H:i:s', $time);
0055 
0056         $this->_db->addStatementLog($sql);
0057         $stmt = $this->getDb()->prepare($sql);
0058         $ret = $stmt->execute(array(
0059             'seen'        => $seen_at,
0060             'ip_inet'     => inet_pton($ip_inet),
0061             'object_type' => 40,
0062             'product_id'  => $value['file_id'],
0063             'ipv6'        => $ipClientv6,
0064             'ipv4'        => $ipClientv4,
0065             'fp'          => $value['fp'] ? $value['fp'] : null,
0066             'ua'          => $_SERVER['HTTP_USER_AGENT'] ? $_SERVER['HTTP_USER_AGENT'] : null,
0067             'member'      => isset($value['u']) ? $value['u'] : null
0068         ));
0069         if (false == $ret) {
0070             error_log(__FUNCTION__ . ' - ' . $stmt->errorInfo());
0071         }
0072         $stmt->closeCursor();
0073         $stmt = null;
0074     }
0075 
0076     /**
0077      * @return mixed
0078      */
0079     public function getOcsDbConfig()
0080     {
0081         return $this->ocsDbConfig;
0082     }
0083 
0084     /**
0085      * @param mixed $ocsDbConfig
0086      */
0087     public function setOcsDbConfig($ocsDbConfig)
0088     {
0089         $this->ocsDbConfig = $ocsDbConfig;
0090         $this->dbName = $this->ocsDbConfig['ocsDbConfig']['database'];
0091     }
0092 
0093 }