Source for file KTDownloadManager.inc.php

Documentation is available at KTDownloadManager.inc.php

  1. <?
  2.  
  3. /**
  4.  *
  5.  * KTDownloadManager manages files in the download_files table.
  6.  * @license http://www.knowledgetree.com/KPL KnowledgeTree Public License Version 1.1
  7.  * @package KTWEBSERVICE
  8.  */
  9.  
  10. /*
  11.  *
  12.  * The contents of this file are subject to the KnowledgeTree Public
  13.  * License Version 1.1 ("License"); You may not use this file except in
  14.  * compliance with the License. You may obtain a copy of the License at
  15.  * http://www.knowledgetree.com/KPL
  16.  * 
  17.  * Software distributed under the License is distributed on an "AS IS"
  18.  * basis,
  19.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  20.  * for the specific language governing rights and limitations under the
  21.  * License.
  22.  * 
  23.  * The Original Code is: KnowledgeTree Open Source
  24.  * 
  25.  * The Initial Developer of the Original Code is The Jam Warehouse Software
  26.  * (Pty) Ltd, trading as KnowledgeTree.
  27.  * Portions created by The Jam Warehouse Software (Pty) Ltd are Copyright
  28.  * (C) 2007 The Jam Warehouse Software (Pty) Ltd;
  29.  * All Rights Reserved.
  30.  *
  31.  */
  32.  
  33. {
  34.     var $session;     
  35.     var $age;
  36.     var $download_url;
  37.     var $random;
  38.     
  39.     /**
  40.      * Constructor for the download manager.
  41.      *
  42.      * @param KTAPI_Session $session 
  43.      * @return KTDownloadManager 
  44.      */
  45.     function KTDownloadManager()
  46.     {        
  47.         $config &KTConfig::getSingleton();         
  48.         
  49.         $this->age = $config->get('webservice/downloadExpiry',5);
  50.         $this->download_url = $config->get('webservice/downloadUrl');
  51.         $this->random=$config->get('webservice/randomKeyText','jhsdf8q1jkjpoiudfs7sd3ds1');
  52.     }
  53.  
  54.     /**
  55.      * Sets the current session.
  56.      *
  57.      * @param string $session 
  58.      */
  59.     function set_session($session)
  60.     {
  61.         $this->session = $session;        
  62.     }    
  63.     
  64.     /**
  65.      * This returns
  66.      *
  67.      * @access public
  68.      * @param KTAPI_Document $document 
  69.      * @return string 
  70.      */
  71.     function allow_download($document$content_version=null)
  72.     {
  73.         assert(!is_null($document));
  74.         assert(is_a($document'KTAPI_Document'));
  75.         
  76.         $hash sha1("$document->documentid $this->session $this->random");
  77.         
  78.         
  79.         $id DBUtil::autoInsert('download_files',
  80.             array(
  81.                 'document_id'=>$document->documentid,
  82.                 'session'=>$this->session,
  83.                 'download_date'=>date('Y-m-d H:i:s'),
  84.                 'hash'=>$hash        
  85.                 ),
  86.                 array('noid'=>true)
  87.             );
  88.             
  89.         return $this->build_url($hash$document->documentid );
  90.     }
  91.     
  92.     /**
  93.      * This returns the url used to download a document.
  94.      *
  95.      * @param string $hash 
  96.      * @param int $documentid 
  97.      * @param int $userid 
  98.      * @return string 
  99.      */    
  100.     function build_url($hash$documentid )
  101.     {
  102.         return $this->download_url . "?code=$hash&d=$documentid&u=$this->session";
  103.     }
  104.     
  105.     
  106.     /**
  107.      * This starts a download.
  108.      *
  109.      * @access public
  110.      */
  111.     function download($document_id$hash$version null)
  112.     {
  113.         $sql "SELECT 1 FROM download_files WHERE hash='$hashAND session='$this->sessionAND document_id=$document_id";
  114.         $rows DBUtil::getResultArray($sql);
  115.         if (PEAR::isError($rows))
  116.         {
  117.             return $rows;
  118.         }
  119.         
  120.         if (count($rows== 0)
  121.         {
  122.             return new PEAR_Error('Invalid session.');
  123.         }
  124.         
  125.         $storage =KTStorageManagerUtil::getSingleton();
  126.         
  127.         
  128.         $ktapi &new KTAPI();
  129.         $res $ktapi->get_active_session($this->session);
  130.         if (PEAR::isError($res))
  131.         {
  132.             return $res;
  133.         }
  134.         
  135.         $document $ktapi->get_document_by_id($document_id);
  136.         if (PEAR::isError($document))
  137.         {
  138.             return $document;
  139.         }
  140.         
  141.         if (!empty($version)) 
  142.         {
  143.             $version KTDocumentContentVersion::get($version);
  144.             
  145.             $res $storage->downloadVersion($document->document$version);
  146.         
  147.         else 
  148.         {
  149.             $res $storage->download($document->document);
  150.         }
  151.         if (PEAR::isError($res))
  152.         {
  153.             return $res;
  154.         }
  155.         
  156.         $sql "DELETE FROM download_files WHERE hash='$hashAND session='$this->sessionAND document_id=$document_id";
  157.         $result DBUtil::runQuery($sql);
  158.         
  159.         return true;        
  160.     }
  161.     
  162.     /**
  163.      * This will remove any temporary files that have not been dealt with in the correct timeframe.
  164.      * 
  165.      * @access public
  166.      */
  167.     function cleanup()
  168.     {
  169.         list($year,$mon,$day,$hour$minexplode(':'date('Y:m:d:H:i'));
  170.         $expirydate date('Y-m-d H:i:s'mktime($hour$min $this->age0$mon$day$year));
  171.         $sql "DELETE FROM download_files WHERE download_date<'$expirydate'";    
  172.         DBUtil::runQuery($sql);    
  173.     }    
  174. }
  175. ?>

Documentation generated on Sun, 22 Apr 2007 02:30:54 +0200 by phpDocumentor 1.3.2