Source for file KTUploadManager.inc.php

Documentation is available at KTUploadManager.inc.php

  1. <?
  2.  
  3. /**
  4.  *
  5.  * KTUploadManager manages files in the uploaded_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 $userid;
  35.     var $age;
  36.     var $temp_dir;
  37.     var $session;
  38.     
  39.  
  40.     function KTUploadManager()
  41.     {
  42.         $config KTConfig::getSingleton();         
  43.         
  44.         $this->age = $config->get('webservice/uploadExpiry',60);
  45.         $this->temp_dir$config->get('webservice/uploadDirectory');
  46.     
  47.     
  48.     /**
  49.      * Sets the current session.
  50.      *
  51.      * @param KTAPI_Session $session 
  52.      */
  53.     function set_session($session)
  54.     {
  55.         $user &$session->get_user();
  56.         $this->userid=$user->getId();
  57.         $this->session = $session->get_session();        
  58.     }
  59.  
  60.     /**
  61.      * This tells the manager to manage a file that has been uploaded.
  62.      *
  63.      * @param string $filename 
  64.      * @param string $tempfile 
  65.      * @param string $action 
  66.      */
  67.     function uploaded($filename$tempfile$action$relatedid null)
  68.     {
  69.         $filename=basename($filename);
  70.         $now=date('Y-m-d H:i:s');
  71.         $now_str=date('YmdHis');
  72.         
  73.         $tempfile str_replace('/','\\',$tempfile);
  74.         $newtempfile str_replace('\\','/',realpath($this->temp_dir'/' .  $this->userid  . '-'$now_str);
  75.         
  76.         DBUtil::startTransaction();
  77.         $id DBUtil::autoInsert('uploaded_files',
  78.             array(
  79.                 'tempfilename'=>$newtempfile,
  80.                 'filename'=>$filename,
  81.                 'userid'=>$this->userid,
  82.                 'uploaddate'=>$now,
  83.                 'action'=>$action,
  84.                 'related_uploadid'=>$relatedid                
  85.                 ),
  86.                 array('noid'=>true)    
  87.             );
  88.             
  89.         if (PEAR::isError($id))
  90.         {
  91.             DBUtil::rollback();
  92.             return $id;
  93.         }
  94.  
  95.         $result move_uploaded_file($tempfile$newtempfile);
  96.         if ($result == false)
  97.         {
  98.  
  99.             DBUtil::rollback();
  100.             return false;
  101.         }
  102.  
  103.         DBUtil::commit();
  104.  
  105.         return $newtempfile;
  106.     }
  107.     
  108.     /**
  109.      * This is a list of all all managed files.
  110.      *
  111.      * @param string $action 
  112.      */
  113.     function get_uploaded_list($action=null)
  114.     {
  115.         $sql "SELECT idtempfilenamefilenameuseridactionrelated_uploadid FROM uploaded_files WHERE userid=$this->userid";
  116.         if (!is_null($action))
  117.         {
  118.             $sql .= " AND action='$action'";
  119.         }
  120.         $result DBUtil::getResultArray($sql);
  121.         return $result;
  122.     }
  123.     
  124.     function imported_file($action$filename$documentid)
  125.     {
  126.         DBUtil::startTransaction();
  127.         $filename=basename($filename);
  128.         $sql "DELETE FROM uploaded_files WHERE action='$actionAND filename='$filename'";
  129.         $rs DBUtil::runQuery($sql);
  130.         if (PEAR::isError($rs))
  131.         {
  132.             DBUtil::rollback();
  133.             return false;
  134.         }
  135.                 
  136.         $sql "INSERT INTO index_files(document_iduser_idVALUES($documentid$this->userid)";
  137.         DBUtil::runQuery($sql);
  138.         if (PEAR::isError($rs))
  139.         {
  140.             DBUtil::rollback();
  141.             return false;
  142.         }
  143.         
  144.         DBUtil::commit();
  145.         return true;
  146.     }
  147.     
  148.     /**
  149.      * This will remove any temporary files that have not been dealt with in the correct timeframe.
  150.      *
  151.      */
  152.     function cleanup()
  153.     {
  154.         list($year,$mon,$day,$hour$minexplode(':'date('Y:m:d:H:i'));
  155.         $expirydate date('Y-m-d H:i:s'mktime($hour$min $this->age0$mon$day$year));
  156.         
  157.         $sql "SELECT tempfilename FROM uploaded_files WHERE uploaddate<'$expirydate'";
  158.         $rows DBUtil::getResultArray($sql);
  159.         
  160.         foreach($rows as $record)
  161.         {
  162.             $tempfilename=addslashes($record['tempfilename']);
  163.             
  164.             $sql "DELETE FROM uploaded_files WHERE tempfilename='$tempfilename'";
  165.             $rs DBUtil::runQuery($sql);
  166.             if (PEAR::isError($rs))
  167.             {
  168.                 continue;    
  169.             }
  170.             
  171.             @unlink($tempfilename);        
  172.         }
  173.     }
  174. }
  175. ?>

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