File indexing completed on 2024-06-23 05:51:18

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 Backend_Commands_CreateTorrent implements Local_Queue_CommandInterface
0024 {
0025     /**
0026      * Configuration for HTTP-Client
0027      *
0028      * @var array
0029      */
0030     protected $_config = array(
0031         'maxredirects' => 0,
0032         'timeout'      => 21600
0033     );
0034     
0035     protected $file;
0036     
0037     /**
0038      * PHP 5 allows developers to declare constructor methods for classes.
0039      * Classes which have a constructor method call this method on each newly-created object,
0040      * so it is suitable for any initialization that the object may need before it is used.
0041      *
0042      * Note: Parent constructors are not called implicitly if the child class defines a constructor.
0043      * In order to run a parent constructor, a call to parent::__construct() within the child constructor is required.
0044      *
0045      * param [ mixed $args [, $... ]]
0046      *
0047      * @param int    $collectionId
0048      * @param int $fileId
0049      *
0050      * @link http://php.net/manual/en/language.oop5.decon.php
0051      */
0052     public function __construct($file)
0053     {
0054         $this->file = $file;
0055         
0056     }
0057 
0058     public function doCommand()
0059     {
0060 
0061         return $this->callCreateTorrent($this->file);
0062     }
0063 
0064     protected function callCreateTorrent($file)
0065     {
0066         
0067         
0068         $link = null;
0069         $isExternLink = false;
0070         if($file->tags)
0071         {
0072             $tags = explode(',', $file->tags);        
0073             foreach ($tags as $t) {
0074                $tagStr =  explode('##',$t);
0075                if(sizeof($tagStr)==2 && $tagStr[0]=='link')
0076                {
0077                 $link = $tagStr[1];
0078                }
0079             }
0080         }
0081 
0082         
0083         if($link)
0084         {
0085             $isExternLink = true;
0086         }
0087         
0088         if(!$isExternLink) {
0089         
0090             $log = Zend_Registry::get('logger');
0091             $log->debug('**********' . __CLASS__ . '::' . __FUNCTION__ . '**********' . "\n");
0092 
0093             $result = $this->createExternalTorrent($file->id);
0094 
0095             if(!empty($result) && $result != 'Error') {
0096                 //Done, set has_torrent in table ppload_files
0097             } else {
0098                 $log->debug("Error on Creating Torrent! Result: ".$result);
0099                 return false;
0100             }
0101         } else {
0102             return false;   
0103         }
0104         
0105         return true;
0106     }
0107     
0108     public function createExternalTorrent($fileId)
0109     {
0110         $httpClient = $this->getHttpClient();
0111         
0112         $config = Zend_Registry::get('config');
0113         $torrenturl = $config->torrent->media->createurl . "?id=".$fileId;
0114         
0115         $uri = $this->generateUri($torrenturl);
0116 
0117         $httpClient->setUri($uri);
0118         $response = $this->retrieveBody($httpClient);
0119         if (false === $response) {
0120             Zend_Registry::get('logger')->err(__METHOD__ . " - Error while creating torrent: " . $uri
0121                 . ".\n Server replay was: " . $httpClient->getLastResponse()->getStatus() . ". " . $httpClient->getLastResponse()
0122                                                                                                               ->getMessage()
0123                 . PHP_EOL)
0124             ;
0125 
0126             return false;
0127         }
0128         
0129         Zend_Registry::get('logger')->debug(__METHOD__ . ' Result: ' . print_r($response, true));
0130 
0131         return $response;
0132     }
0133     
0134     /**
0135      * @return Zend_Http_Client
0136      * @throws Zend_Http_Client_Exception
0137      */
0138     public function getHttpClient()
0139     {
0140         $httpClient = new Zend_Http_Client();
0141         $httpClient->setConfig($this->_config);
0142 
0143         return $httpClient;
0144     }
0145 
0146     /**
0147      * @param $url
0148      *
0149      * @return Zend_Uri
0150      * @throws Zend_Uri_Exception
0151      */
0152     protected function generateUri($url)
0153     {
0154         $uri = Zend_Uri::factory($url);
0155 
0156         return $uri;
0157     }
0158 
0159     /**
0160      * @param Zend_Http_Client $httpClient
0161      *
0162      * @return bool
0163      * @throws Zend_Http_Client_Exception
0164      */
0165     public function retrieveBody($httpClient)
0166     {
0167         $response = $httpClient->request();
0168 
0169         if ($response->isError()) {
0170             return false;
0171         } else {
0172             return $response->getBody();
0173         }
0174     }
0175     
0176     
0177 
0178 }