File indexing completed on 2024-05-12 05:58:48

0001 <?php
0002 
0003 /**
0004  *  ocs-webserver
0005  *
0006  *  Copyright 2016 by pling GmbH.
0007  *
0008  *    This file is part of ocs-webserver.
0009  *
0010  *    This program is free software: you can redistribute it and/or modify
0011  *    it under the terms of the GNU Affero General Public License as
0012  *    published by the Free Software Foundation, either version 3 of the
0013  *    License, or (at your option) any later version.
0014  *
0015  *    This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
0022  *
0023  */
0024 class Default_Model_Views
0025 {
0026 
0027     const OBJECT_TYPE_PRODUCT = 10;
0028     const OBJECT_TYPE_COLLECTION = 12;
0029     const OBJECT_TYPE_MEMBERPAGE = 20;
0030     const OBJECT_TYPE_LOGIN = 30;
0031     const OBJECT_TYPE_LOGOUT = 32;
0032     const OBJECT_TYPE_DOWNLOAD = 40;
0033     const OBJECT_TYPE_MEDIA_VIDEO = 52;
0034     const OBJECT_TYPE_MEDIA_MUSIC = 54;
0035     const OBJECT_TYPE_MEDIA_BOOK = 56;
0036 
0037     public static function saveViewProduct($product_id)
0038     {
0039         $sql = ("INSERT IGNORE INTO `stat_object_view` (`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)");
0040         self::saveViewObject(self::OBJECT_TYPE_PRODUCT, $product_id, $sql);
0041     }
0042 
0043     protected static function saveViewObject($object_type, $object_id, $sql)
0044     {
0045         $session = new Zend_Session_Namespace();
0046         $view_member_id = Zend_Auth::getInstance()->hasIdentity() ? Zend_Auth::getInstance()->getIdentity()->member_id : null;
0047         $ipClient = Zend_Controller_Front::getInstance()->getRequest()->getClientIp();
0048         $remoteAddress = self::getRemoteAddress($ipClient);
0049         $ipClientv6 = filter_var($remoteAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) ? $remoteAddress : null;
0050         $ipClientv4 = filter_var($remoteAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) ? $remoteAddress : null;
0051         $session_ipv6 = isset($session->stat_ipv6) ? inet_pton($session->stat_ipv6) : null;
0052         $session_ipv4 = isset($session->stat_ipv4) ? inet_pton($session->stat_ipv4) : null;
0053         $session_remote = isset($remoteAddress) ? inet_pton($remoteAddress) : null;
0054         $ip_inet = isset($session_ipv6) ? $session_ipv6 : (isset($session_ipv4) ? $session_ipv4 : $session_remote);
0055         $time = (round(time() / 300)) * 300;
0056         $seen_at = date('Y-m-d H:i:s', $time);
0057 
0058         try {
0059             Zend_Db_Table::getDefaultAdapter()->query($sql, array(
0060                 'seen'        => $seen_at,
0061                 'ip_inet'     => $ip_inet,
0062                 'object_type' => $object_type,
0063                 'product_id'  => $object_id,
0064                 'ipv6'        => $session->stat_ipv6 ? $session->stat_ipv6 : $ipClientv6,
0065                 'ipv4'        => $session->stat_ipv4 ? $session->stat_ipv4 : $ipClientv4,
0066                 'fp'          => $session->stat_fp ? $session->stat_fp : null,
0067                 'ua'          => $_SERVER['HTTP_USER_AGENT'] ? $_SERVER['HTTP_USER_AGENT'] : null,
0068                 'member'      => $view_member_id
0069             ));
0070         } catch (Exception $e) {
0071             Zend_Registry::get('logger')->err(__METHOD__ . ' - ERROR write - ' . print_r($e, true));
0072         }
0073     }
0074 
0075     public static function getRemoteAddress($ipClient)
0076     {
0077         $iplist = explode(',', $ipClient);
0078         foreach ($iplist as $ip) {
0079             if (self::validate_ip($ip)) {
0080                 return $ip;
0081             }
0082         }
0083 
0084         return null;
0085     }
0086 
0087     public static function validate_ip($ip)
0088     {
0089         $filter = FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE;
0090         if (APPLICATION_ENV == 'development') {
0091             $filter = FILTER_FLAG_NO_RES_RANGE;
0092         }
0093         if (filter_var($ip, FILTER_VALIDATE_IP, $filter) === false) {
0094             return false;
0095         }
0096 
0097         return true;
0098     }
0099 
0100     public static function saveViewMemberpage($member_id)
0101     {
0102         $sql = ("INSERT IGNORE INTO `stat_object_view` (`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)");
0103         self::saveViewObject(self::OBJECT_TYPE_MEMBERPAGE, $member_id, $sql);
0104     }
0105 
0106     public static function saveFileDownload($file_id)
0107     {
0108         $sql = ("INSERT IGNORE INTO `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)");
0109         self::saveViewObject(self::OBJECT_TYPE_DOWNLOAD, $file_id, $sql);
0110     }
0111 
0112     public static function saveViewCollection($_projectId)
0113     {
0114         $sql = ("INSERT IGNORE INTO `stat_object_view` (`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)");
0115         self::saveViewObject(self::OBJECT_TYPE_COLLECTION, $_projectId, $sql);
0116     }
0117 
0118     public static function saveViewMusic($object_id)
0119     {
0120         $sql = ("INSERT IGNORE INTO `stat_object_view` (`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)");
0121         self::saveViewObject(self::OBJECT_TYPE_MEDIA_MUSIC, $object_id, $sql);
0122     }
0123 
0124     public static function saveViewVideo($object_id)
0125     {
0126         $sql = ("INSERT IGNORE INTO `stat_object_view` (`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)");
0127         self::saveViewObject(self::OBJECT_TYPE_MEDIA_VIDEO, $object_id, $sql);
0128     }
0129 
0130     public static function saveViewBook($object_id)
0131     {
0132         $sql = ("INSERT IGNORE INTO `stat_object_view` (`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)");
0133         self::saveViewObject(self::OBJECT_TYPE_MEDIA_BOOK, $object_id, $sql);
0134     }
0135 
0136 }