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

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 class DlController extends Local_Controller_Action_DomainSwitch
0024 {
0025 
0026     public function indexAction()
0027     {
0028         $this->_helper->layout->disableLayout();
0029 
0030         $file_id = $this->getParam('file_id');
0031         $file_type = $this->getParam('file_type');
0032         $file_name = $this->getParam('file_name');
0033         $file_size = $this->getParam('file_size');
0034         $projectId = $this->getParam('project_id');
0035         $linkType = "download";
0036         if ($this->hasParam('link_type')) {
0037             $linkType = $this->getParam('link_type');
0038         }
0039         $isExternal = $this->getParam('is_external');
0040         $externalLink = $this->getParam('external_link');
0041 
0042         $hasTorrent = $this->getParam('has_torrent');
0043 
0044         $modelProduct = new Default_Model_Project();
0045         $productInfo = $modelProduct->fetchProductInfo($projectId);
0046 
0047         $collectionID = $productInfo->ppload_collection_id;
0048 
0049         $sModel = new Default_Model_Section();
0050         $section = $sModel->fetchSectionForCategory($productInfo->project_category_id);
0051         $info = new Default_Model_Info();
0052         $supporter = $info->getRandomSupporterForSection($section['section_id']);
0053 
0054         $this->view->section_id = $section['section_id'];
0055 
0056         $this->view->link_type = $linkType;
0057         $this->view->file_name = $file_name;
0058         $this->view->file_size = $file_size;
0059         $this->view->file_size_human = $this->humanFileSize($file_size);
0060         $this->view->project_title = $productInfo->title;
0061         $this->view->project_owner = $productInfo->username;
0062         $this->view->project_id = $projectId;
0063         $this->view->is_external = $isExternal;
0064         $this->view->external_link = $externalLink;
0065         $this->view->supporter = $supporter;
0066         $this->view->has_torrent = ($hasTorrent == "1");
0067         $this->view->file_id = $file_id;
0068 
0069         $memberId = $this->_authMember->member_id;
0070 
0071         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
0072 
0073             $payload = array('id' => $file_id, 'u' => $memberId, 'lt' => $linkType);
0074             $url = Default_Model_PpLoad::createDownloadUrlJwt($collectionID, $file_name, $payload);
0075 
0076             if ($linkType == 'install') {
0077                 $helperCatXdgType = new Default_View_Helper_CatXdgType();
0078                 $xdgType = $helperCatXdgType->catXdgType($productInfo->project_category_id);
0079 
0080                 $url = 'ocs://install'
0081                        . '?url=' . urlencode($url)
0082                        . '&type=' . urlencode($xdgType)
0083                        . '&filename=' . urldecode($file_name);
0084             }
0085 
0086             $this->view->url = $url;
0087 
0088             // save to member_download_history            
0089             if (isset($file_id) && isset($projectId)) {
0090 
0091                 $server_info = '';
0092 
0093                 foreach ($_SERVER as $key => $value) {
0094                     if ($value) {
0095                         $server_info = $server_info . $key . ': ' . $value . ' ';
0096                     }
0097                 }
0098 
0099                 // handle cookie
0100                 $config = Zend_Registry::get('config');
0101                 $cookieName = $config->settings->session->auth->anonymous;
0102                 $storedInCookie = isset($_COOKIE[$cookieName]) ? $_COOKIE[$cookieName] : null;
0103                 if (!$storedInCookie) {
0104                     $remember_me_seconds = $config->settings->session->remember_me->cookie_lifetime;
0105                     $cookieExpire = time() + $remember_me_seconds;
0106                     $hash = hash('sha512', PPLOAD_DOWNLOAD_SECRET . $collectionID . (time() + 3600));
0107                     $storedInCookie = $hash;
0108                     setcookie($cookieName, $hash, $cookieExpire, '/');
0109                 }
0110 
0111                 $data = array(
0112                     'project_id'           => $projectId,
0113                     'member_id'            => $memberId,
0114                     'anonymous_cookie'     => $storedInCookie,
0115                     'file_id'              => $file_id,
0116                     'file_type'            => $file_type,
0117                     'file_name'            => $file_name,
0118                     'file_size'            => $file_size,
0119                     'downloaded_ip'        => $this->getRealIpAddr(),
0120                     'HTTP_X_FORWARDED_FOR' => isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : null,
0121                     'HTTP_X_FORWARDED'     => isset($_SERVER['HTTP_X_FORWARDED']) ? $_SERVER['HTTP_X_FORWARDED'] : null,
0122                     'HTTP_CLIENT_IP'       => isset($_SERVER['HTTP_CLIENT_IP']) ? $_SERVER['HTTP_CLIENT_IP'] : null,
0123                     'HTTP_FORWARDED_FOR'   => isset($_SERVER['HTTP_FORWARDED_FOR']) ? $_SERVER['HTTP_FORWARDED_FOR'] : null,
0124                     'HTTP_FORWARDED'       => isset($_SERVER['HTTP_FORWARDED']) ? $_SERVER['HTTP_FORWARDED'] : null,
0125                     'REMOTE_ADDR'          => $_SERVER['REMOTE_ADDR'],
0126                     'server_info'          => $server_info
0127                 );
0128 
0129                 $memberDlHistory = new Default_Model_DbTable_MemberDownloadHistory();
0130                 $memberDlHistory->createRow($data)->save();
0131             }
0132         }
0133     }
0134 
0135     /**
0136      * @param int $bytes
0137      * @return string|null
0138      */
0139     public function humanFileSize($bytes)
0140     {
0141         if (!empty($bytes)) {
0142             $size = round($bytes / 1048576, 2);
0143             if ($size == 0.0) {
0144                 return '0.01 MB';
0145             } else {
0146                 return $size . ' MB';
0147             }
0148         } else {
0149             return null;
0150         }
0151     }
0152 
0153     public function getRealIpAddr()
0154     {
0155         if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
0156         {
0157             $ip = $_SERVER['HTTP_CLIENT_IP'];
0158         } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
0159         {
0160             $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
0161         } else {
0162             $ip = $_SERVER['REMOTE_ADDR'];
0163         }
0164 
0165         return $ip;
0166     }
0167 
0168     /**
0169      * @return mixed|null
0170      */
0171     protected function getReferer()
0172     {
0173         $referer = null;
0174         if (!empty($_SERVER['HTTP_REFERER'])) {
0175             $referer = $_SERVER['HTTP_REFERER'];
0176         }
0177 
0178         return $referer;
0179     }
0180 
0181     /**
0182      * @param int $bytes
0183      * @param int $precision
0184      * @return string
0185      */
0186     protected function formatBytes($bytes, $precision = 2)
0187     {
0188         $units = array('B', 'KB', 'MB', 'GB', 'TB');
0189 
0190         $bytes = max($bytes, 0);
0191         $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
0192         $pow = min($pow, count($units) - 1);
0193 
0194         // Uncomment one of the following alternatives
0195         // $bytes /= pow(1024, $pow);
0196         // $bytes /= (1 << (10 * $pow));
0197 
0198         return round($bytes, $precision) . ' ' . $units[$pow];
0199     }
0200 
0201 }