Source for file ktwsapi.inc.php

Documentation is available at ktwsapi.inc.php

  1. <?
  2.  
  3. /**
  4.  *
  5.  * This is the object model for the KnowledgeTree WebService.
  6.  * @license http://www.knowledgetree.com/KPL KnowledgeTree Public License Version 1.1
  7.  * @package KTWSAPI
  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. // TODO: add more validation based on information already returned. this can prevent all validation being done server side and minimise a little traffic possibly...
  34. // TODO: caching so that requests don't have to be returned.
  35. // TODO: possibly add a subscriber model, where updates can be propogated through to a cache/model on the client side.
  36.  
  37. require_once('ktwsapi_cfg.inc.php');
  38.  
  39. ini_set('include_path'ini_get('include_path'PATH_SEPARATOR KT_PEAR_DIR);
  40.  
  41. require_once('SOAP/Client.php');
  42.  
  43. define('KTWSAPI_ERR_SESSION_IN_USE','There is a session already active.');
  44. define('KTWSAPI_ERR_SESSION_NOT_STARTED','An active session has not been started.');
  45.  
  46. {
  47.     var $ktapi;
  48.     
  49.     var $parent_id;
  50.     
  51.     /**
  52.      * Upload a file to KT. Returns a temp filename on the server.
  53.      *
  54.      * @param string $filename 
  55.      * @param string $action 
  56.      * @param int $document_id 
  57.      * @return string 
  58.      */
  59.     function _upload_file($filename$action$document_id=null)
  60.     {
  61.         if (!extension_loaded('curl'))
  62.         {
  63.             return new PEAR_Error('CURL library not included.');
  64.         }
  65.         
  66.         if (!is_file($filename|| !is_readable($filename))
  67.         {
  68.             return new PEAR_Error('Could not access file to upload.');            
  69.         }
  70.         
  71.         if (is_null($document_id))
  72.         {
  73.             $uploadname="upload_document";
  74.         }
  75.         else 
  76.         {
  77.             $uploadname="upload_$document_id";
  78.             
  79.         }
  80.         
  81.         $session_id $this->ktapi->session;
  82.         
  83.         $ch curl_init();
  84.         $fp fopen ($filename'r');         
  85.         curl_setopt($chCURLOPT_URLKTUploadURL);
  86.         curl_setopt($chCURLOPT_POST1);
  87.         
  88.         $post_fields array(
  89.             'session_id'=>$session_id,
  90.             'action'=>$action,
  91.             'document_id'=>$document_id,
  92.             $uploadname=>'@' $filename
  93.         
  94.         );
  95.         
  96.         $str serialize($post_fields);
  97.         
  98.         curl_setopt($chCURLOPT_POSTFIELDS$post_fields);
  99.         curl_setopt($chCURLOPT_RETURNTRANSFER,1);        
  100.         $response curl_exec ($ch);
  101.         curl_close ($ch);
  102.         
  103.         if ($response == '')
  104.         {
  105.             return new PEAR_Error('No response from server.');
  106.         }
  107.         
  108.         $fields=explode('&',$response);
  109.         $status_code=-1;
  110.         $msg='*not set*';
  111.         $upload_status='';
  112.         
  113.  
  114.         foreach($fields as $field)
  115.         {
  116.             list($fieldname$valueexplode('='$field);
  117.             
  118.             $$fieldname $value;
  119.         }
  120.         
  121.         if ($status_code == 0)
  122.         {
  123.             $upload_statusunserialize(urldecode($upload_status));
  124.             
  125.             if ($upload_status[$uploadname]['size'== filesize($filename))
  126.             {
  127.                 return $upload_status[$uploadname]['tmp_name'];
  128.             }
  129.         }
  130.         return new PEAR_Error('Could not upload file.');
  131.         
  132.     }
  133.  
  134.     /**
  135.      * Downlaods a file from KT.
  136.      *
  137.      * @param string $url 
  138.      * @param string $localpath 
  139.      * @param string $filename 
  140.      * @return boolean 
  141.      */
  142.     function _download_file($url$localpath$filename)
  143.     {
  144.         $localfilename $localpath '/' $filename;
  145.         
  146.         $fp fopen($localfilename,'wb');
  147.         if ($fp == null)
  148.         {
  149.             return new PEAR_Error('Could not create local file');
  150.         }
  151.         
  152.         $ch curl_init();
  153.            
  154.         curl_setopt($chCURLOPT_FILE$fp);
  155.         curl_setopt($chCURLOPT_HEADER0);
  156.         curl_setopt($chCURLOPT_URL$url)
  157.         
  158.         $response curl_exec($ch);
  159.         curl_close($ch);
  160.         
  161.         fclose($fp);
  162.         
  163.         return $response;        
  164.     }
  165.     
  166.     /**
  167.      * Returns a reference to the parent folder.
  168.      *
  169.      * @return KTWSAPI_Folder 
  170.      */
  171.     function &get_parent_folder()
  172.     {
  173.         $parent &KTAPI::get_folder_by_id($this->parent_id);
  174.         return $parent;
  175.     }
  176.     
  177. }
  178.  
  179. {
  180.     var $folder_name;
  181.     var $full_path;
  182.     var $folder_id;
  183.     
  184.     /**
  185.      * Constructor
  186.      *
  187.      * @param KTWSAPI $ktapi 
  188.      * @param kt_folder_detail $kt_folder_detail 
  189.      * @return KTWSAPI_Folder 
  190.      */
  191.     function KTWSAPI_Folder(&$ktapi$kt_folder_detail)
  192.     {
  193.         $this->ktapi = &$ktapi;
  194.         $this->folder_id = $kt_folder_detail->id+0;
  195.         $this->folder_name = $kt_folder_detail->folder_name;
  196.         $this->parent_id = $kt_folder_detail->parent_id+0;
  197.         $this->full_path = $kt_folder_detail->full_path;        
  198.     }
  199.     
  200.     /**
  201.      * Returns a reference to a KTWSAPI_Folder
  202.      *
  203.      * @param KTWSAPI $ktapi 
  204.      * @param int $folderid 
  205.      * @return KTWSAPI_Folder 
  206.      */
  207.     function &get(&$ktapi$folderid)
  208.     {
  209.         assert(!is_null($ktapi));
  210.         assert(is_a($ktapi'KTWSAPI'));
  211.         assert(is_numeric($folderid));
  212.         
  213.         $folderid += 0;
  214.         
  215.         $kt_folder_detail $ktapi->soapclient->get_folder_detail($ktapi->session$folderid);
  216.         if (SOAP_Client::isError($kt_folder_detail))
  217.         {
  218.             return $kt_folder_detail;
  219.         }
  220.         
  221.         if ($kt_folder_detail->status_code != 0)
  222.         {
  223.             return new PEAR_Error($kt_folder_detail->message);
  224.         }
  225.         
  226.         $folder new KTWSAPI_Folder($ktapi$kt_folder_detail);        
  227.         
  228.         return $folder;
  229.     }
  230.  
  231.     /**
  232.      * Returnss the parent folder id.
  233.      *
  234.      * @return int 
  235.      */
  236.     function get_parent_folder_id()
  237.     {
  238.         return $this->parent_id;
  239.     }
  240.     
  241.     /**
  242.      * Returns the folder name.
  243.      *
  244.      * @return string 
  245.      */
  246.     function get_folder_name()
  247.     {
  248.         return $this->folder_name;
  249.     }    
  250.     
  251.     /**
  252.      * Returns the current folder id.
  253.      *
  254.      * @return int 
  255.      */
  256.     function get_folderid()
  257.     {
  258.         return $this->folder_id;
  259.     }    
  260.  
  261.     /**
  262.      * Returns the foldre based on foldername.
  263.      *
  264.      * @param string $foldername 
  265.      * @return KTWSAPI_Folder 
  266.      */
  267.     function &get_folder_by_name($foldername)
  268.     {
  269.         $path $this->full_path . '/' $foldername;
  270.         if (substr($path,0,13== '/Root Folder/')
  271.         {
  272.             $path substr($path,13);
  273.         }
  274.         if (substr($path,0,12== 'Root Folder/')
  275.         {
  276.             $path substr($path,12);
  277.         }
  278.         
  279.         $kt_folder_detail $this->ktapi->soapclient->get_folder_detail_by_name($this->ktapi->session$path);
  280.         
  281.         if (SOAP_Client::isError($kt_folder_detail))
  282.         {
  283.             return $kt_folder_detail;
  284.         }
  285.         
  286.         if ($kt_folder_detail->status_code != 0)
  287.         {
  288.             return new PEAR_Error($kt_folder_detail->message);
  289.         }
  290.         
  291.         $foldernew KTWSAPI_Folder($this->ktapi$kt_folder_detail);    
  292.         return $folder;        
  293.     }
  294.         
  295.     /**
  296.      * Returns the full folder path.
  297.      *
  298.      * @return string 
  299.      */
  300.     function get_full_path()
  301.     {
  302.         return $this->full_path;
  303.     }    
  304.     
  305.     /**
  306.      * Returns the contents of a folder.
  307.      *
  308.      * @param int $depth 
  309.      * @param string $what 
  310.      * @return kt_folder_items 
  311.      */
  312.     function get_listing($depth=1$what='DF')
  313.     {
  314.         $kt_folder_contents $this->ktapi->soapclient->get_folder_contents($this->ktapi->session$this->folder_id$depth+0$what);
  315.         if (SOAP_Client::isError($kt_folder_contents))
  316.         {
  317.             return $kt_folder_contents;
  318.         }
  319.         
  320.         if ($kt_folder_contents->status_code != 0)
  321.         {
  322.             return new PEAR_Error($kt_folder_contents->message);
  323.         }
  324.         
  325.         return $kt_folder_contents->items;        
  326.     }
  327.     
  328.     /**
  329.      * Returns a document based on title.
  330.      *
  331.      * @param string $title 
  332.      * @return KTWSAPI_Document 
  333.      */
  334.     function &get_document_by_name($title)
  335.     {
  336.         $path $this->full_path . '/' $title;        
  337.         if (substr($path,0,13== '/Root Folder/')
  338.         {
  339.             $path substr($path,13);
  340.         }
  341.         if (substr($path,0,12== 'Root Folder/')
  342.         {
  343.             $path substr($path,12);
  344.         }
  345.                 
  346.         $kt_document_detail $this->ktapi->soapclient->get_document_detail_by_name($this->ktapi->session$path'T');
  347.         
  348.         if (SOAP_Client::isError($kt_document_detail))
  349.         {
  350.             return $kt_document_detail;
  351.         }
  352.         
  353.         if ($kt_document_detail->status_code != 0)
  354.         {
  355.             return new PEAR_Error($kt_document_detail->message);
  356.         }
  357.         
  358.         $document new KTWSAPI_Document($this->ktapi$kt_document_detail);
  359.         
  360.         return $document;
  361.     }
  362.  
  363.     /**
  364.      * Returns a document based on filename.
  365.      *
  366.      * @param string $filename 
  367.      * @return KTWSAPI_Document 
  368.      */
  369.     function &get_document_by_filename($filename)
  370.     {
  371.         $path $this->full_path . '/' $filename;        
  372.         if (substr($path,0,13== '/Root Folder/')
  373.         {
  374.             $path substr($path,13);
  375.         }
  376.         if (substr($path,0,12== 'Root Folder/')
  377.         {
  378.             $path substr($path,12);
  379.         }
  380.                 
  381.         $kt_document_detail $this->ktapi->soapclient->get_document_detail_by_name($this->ktapi->session$path'F');
  382.         
  383.         if (SOAP_Client::isError($kt_document_detail))
  384.         {
  385.             return $kt_document_detail;
  386.         }
  387.         
  388.         if ($kt_document_detail->status_code != 0)
  389.         {
  390.             return new PEAR_Error($kt_document_detail->message);
  391.         }
  392.         
  393.         $document new KTWSAPI_Document($this->ktapi$kt_document_detail);
  394.         return $document;
  395.     }
  396.     
  397.     
  398.     /**
  399.      * Adds a sub folder.
  400.      *
  401.      * @param string $foldername 
  402.      * @return KTWSAPI_Folder 
  403.      */
  404.     function &add_folder($foldername)
  405.     {
  406.         $kt_folder_detail $this->ktapi->soapclient->create_folder($this->ktapi->session$this->folder_id$foldername);
  407.         if (SOAP_Client::isError($kt_folder_detail))
  408.         {
  409.             return $kt_folder_detail;
  410.         }
  411.         
  412.         if ($kt_folder_detail->status_code != 0)
  413.         {
  414.             return new PEAR_Error($kt_folder_detail->message);
  415.         }
  416.         
  417.         $folder &new KTWSAPI_Folder($this->ktapi$kt_folder_detail);
  418.         
  419.         return $folder;
  420.     }
  421.     
  422.     /**
  423.      * Deletes the current folder.
  424.      *
  425.      * @param string $reason 
  426.      * @return true 
  427.      */
  428.     function delete($reason)
  429.     {
  430.         // TODO: check why no transaction in folder_transactions
  431.         $kt_response $this->ktapi->soapclient->delete_folder($this->ktapi->session$this->folder_id$reason);
  432.         if (SOAP_Client::isError($kt_response))
  433.         {
  434.             return $kt_response;
  435.         }
  436.         
  437.         if ($kt_response->status_code != 0)
  438.         {
  439.             return new PEAR_Error($kt_response->message);
  440.         }
  441.         
  442.         return true;
  443.     }
  444.     
  445.     /**
  446.      * Renames the current folder.
  447.      *
  448.      * @param string $newname 
  449.      * @return true 
  450.      */
  451.     function rename($newname)
  452.     {
  453.         $kt_response $this->ktapi->soapclient->rename_folder($this->ktapi->session$this->folder_id$newname);
  454.         if (SOAP_Client::isError($kt_response))
  455.         {
  456.             return $kt_response;
  457.         }
  458.         
  459.         if ($kt_response->status_code != 0)
  460.         {
  461.             return new PEAR_Error($kt_response->message);
  462.         }
  463.         
  464.         return true;
  465.     }
  466.     
  467.     /**
  468.      * Moves a folder to another location.
  469.      *
  470.      * @param KTWSAPI_Folder $ktwsapi_target_folder 
  471.      * @param string $reason 
  472.      * @return true 
  473.      */
  474.     function move(&$ktwsapi_target_folder$reason='')
  475.     {
  476.         assert(!is_null($ktwsapi_target_folder));
  477.         assert(is_a($ktwsapi_target_folder,'KTWSAPI_Folder'));
  478.         
  479.         $kt_response $this->ktapi->soapclient->move_folder($this->ktapi->session$this->folder_id,$ktwsapi_target_folder->get_folderid());
  480.         if (SOAP_Client::isError($kt_response))
  481.         {
  482.             return $kt_response;
  483.         }
  484.         
  485.         if ($kt_response->status_code != 0)
  486.         {
  487.             return new PEAR_Error($kt_response->message);
  488.         }
  489.         
  490.         return true;
  491.     }
  492.  
  493.     /**
  494.      * Copies a folder to another location
  495.      *
  496.      * @param KTWSAPI_Folder $ktwsapi_target_folder 
  497.      * @param string $reason 
  498.      * @return true 
  499.      */
  500.     function copy(&$ktwsapi_target_folder$reason='')
  501.     {
  502.         assert(!is_null($ktwsapi_target_folder));
  503.         assert(is_a($ktwsapi_target_folder,'KTWSAPI_Folder'));
  504.         
  505.         $targetid=$ktwsapi_target_folder->get_folderid();
  506.         
  507.         $kt_response $this->ktapi->soapclient->copy_folder($this->ktapi->session$this->folder_id,$targetid$reason);
  508.         if (SOAP_Client::isError($kt_response))
  509.         {
  510.             return $kt_response;
  511.         }
  512.         
  513.         if ($kt_response->status_code != 0)
  514.         {
  515.             return new PEAR_Error($kt_response->message);
  516.         }
  517.         
  518.         return true;
  519.     }    
  520.     
  521.     /**
  522.      * Adds a document to the current folder.
  523.      *
  524.      * @param string $filename 
  525.      * @param string $title 
  526.      * @param string $documenttype 
  527.      * @return KTWSAPI_Document 
  528.      */
  529.     function &add_document($filename$title=null$documenttype=null)
  530.     {
  531.         if (empty($title))
  532.         {
  533.             $title=basename($filename);    
  534.         }        
  535.         $basename=basename($filename);    
  536.         
  537.         if (empty($documenttype))
  538.         {
  539.             $documenttype='Default';
  540.         }
  541.         
  542.         // First step - upload file
  543.         $tempfilename $this->_upload_file($filename,'A');
  544.         if (PEAR::isError($tempfilename))
  545.         {
  546.             return new PEAR_Error($tempfilename->message);
  547.         }
  548.  
  549.         // Second step - move file into KT
  550.         $kt_document_detail $this->ktapi->soapclient->add_document($this->ktapi->session$this->folder_id$title$basename$documenttype$tempfilename );
  551.         if (SOAP_Client::isError($kt_document_detail))
  552.         {
  553.             return $kt_document_detail;
  554.         }
  555.  
  556.         if ($kt_document_detail->status_code != 0)
  557.         {
  558.             return new PEAR_Error($kt_document_detail->message);
  559.         }
  560.         
  561.         $document new KTWSAPI_Document($this->ktapi$kt_document_detail);
  562.         return $document;
  563.     }
  564. }
  565.  
  566. {
  567.     var $document_id;
  568.     var $title;
  569.     var $document_type;
  570.     var $version;
  571.