ktwsapi.inc.php 22.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
<?

/**
 *
 * This is the object model for the KnowledgeTree WebService.
 *
 * The contents of this file are subject to the KnowledgeTree Public
 * License Version 1.1 ("License"); You may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.knowledgetree.com/KPL
 * 
 * Software distributed under the License is distributed on an "AS IS"
 * basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 * 
 * The Original Code is: KnowledgeTree Open Source
 * 
 * The Initial Developer of the Original Code is The Jam Warehouse Software
 * (Pty) Ltd, trading as KnowledgeTree.
 * Portions created by The Jam Warehouse Software (Pty) Ltd are Copyright
 * (C) 2007 The Jam Warehouse Software (Pty) Ltd;
 * All Rights Reserved.
 *
 */

// TODO: comment
// TODO: php4 compatability

require_once('ktwsapi_cfg.inc.php');

ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . KT_PEAR_DIR);

require_once('SOAP/Client.php');

define('KTWSAPI_ERR_SESSION_IN_USE','There is a session already active.');
define('KTWSAPI_ERR_SESSION_NOT_STARTED','An active session has not been started.');

class KTWSAPI_FolderItem
{
	var $ktapi;
	
	function _upload_file($filename, $action, $document_id=null)
	{
		if (!extension_loaded('curl'))
		{
			return new PEAR_Error('CURL library not included.');
		}
		
		if (!is_file($filename) || !is_readable($filename))
		{
			return new PEAR_Error('Could not access file to upload.');			
		}
		
		if (is_null($document_id))
		{
			$uploadname="upload_document";
		}
		else 
		{
			$uploadname="upload_$document_id";
			
		}
		
		$session_id = $this->ktapi->session;
		
		$ch = curl_init();
		$fp = fopen ($filename, 'r');		 
		curl_setopt($ch, CURLOPT_URL, KTUploadURL);
		curl_setopt($ch, CURLOPT_POST, 1);
		
		$post_fields = array(
			'session_id'=>$session_id,
			'action'=>$action,
			'document_id'=>$document_id,
			$uploadname=>'@' . $filename
		
		);
		
		$str = serialize($post_fields);
		
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);		
		$response = curl_exec ($ch);
		curl_close ($ch);
		
		if ($response == '')
		{
			return new PEAR_Error('No response from server.');
		}
		
		$fields=explode('&',$response);
		$status_code=-1;
		$msg='*not set*';
		$upload_status='';
		

		foreach($fields as $field)
		{
			list($fieldname, $value) = explode('=', $field);
			
			$$fieldname = $value;
		}
		
		if ($status_code == 0)
		{
			$upload_status= unserialize(urldecode($upload_status));
			
			if ($upload_status[$uploadname]['size'] == filesize($filename))
			{
				return $upload_status[$uploadname]['tmp_name'];
			}
		}
		return new PEAR_Error('Could not upload file.');
		
	}

	function _download_file($url, $localpath, $filename)
	{
		$localfilename = $localpath . '/' . $filename;
		
		$fp = fopen($localfilename,'wb');
		if ($fp == null)
		{
			return new PEAR_Error('Could not create local file');
		}
		
	    $ch = curl_init();
           
    	curl_setopt($ch, CURLOPT_FILE, $fp);
	    curl_setopt($ch, CURLOPT_HEADER, 0);
    	curl_setopt($ch, CURLOPT_URL, $url); 
		
    	$response = curl_exec($ch);
    	curl_close($ch);
    	
    	fclose($fp);
    	
    	return $response;		
	}
	
}

class KTWSAPI_Folder extends KTWSAPI_FolderItem
{
	var $folder_name;
	var $parent_id;
	var $full_path;
	var $folderid;
	
	function KTWSAPI_Folder($ktapi, $kt_folder_detail)
	{
		$this->ktapi = $ktapi;
		$this->folderid = $kt_folder_detail->id+0;
		$this->folder_name = $kt_folder_detail->folder_name;
		$this->parent_id = $kt_folder_detail->parent_id+0;
		$this->full_path = $kt_folder_detail->full_path;		
	}
	
	function get($ktapi, $folderid)
	{
		assert(!is_null($ktapi));
		assert(is_a($ktapi, 'KTWSAPI'));
		assert(is_numeric($folderid));
		
		$folderid += 0;
		
		$kt_folder_detail = $ktapi->soapclient->get_folder_detail($ktapi->session, $folderid);
		if (SOAP_Client::isError($kt_folder_detail))
		{
			return $kt_folder_detail;
		}
		
		if ($kt_folder_detail->status_code != 0)
		{
			return new PEAR_Error($kt_folder_detail->message);
		}
		
		return new KTWSAPI_Folder($ktapi, $kt_folder_detail);		
	}

	function get_parent_folder_id()
	{
		return $this->parent_id;
	}
	
	function get_folder_name()
	{
		return $this->folder_name;
	}	
	
	function get_folderid()
	{
		return $this->folderid;
	}	

	/**
	 * 
	 *
	 * @param string $foldername
	 * @return KTWSAPI_Folder
	 */
	function get_folder_by_name($foldername)
	{
		$path = $this->full_path . '/' . $foldername;
		if (substr($path,0,13) == '/Root Folder/')
		{
			$path = substr($path,13);
		}
		if (substr($path,0,12) == 'Root Folder/')
		{
			$path = substr($path,12);
		}
		
		$kt_folder_detail = $this->ktapi->soapclient->get_folder_detail_by_name($this->ktapi->session, $path);
		
		if (SOAP_Client::isError($kt_folder_detail))
		{
			return $kt_folder_detail;
		}
		
		if ($kt_folder_detail->status_code != 0)
		{
			return new PEAR_Error($kt_folder_detail->message);
		}
		
		return new KTWSAPI_Folder($this->ktapi, $kt_folder_detail);			
	}
		
	function get_full_path()
	{
		return $this->full_path;
	}	
	
	function get_listing($depth=1, $what='DF')
	{
		$kt_folder_contents = $this->ktapi->soapclient->get_folder_contents($this->ktapi->session, $this->folderid, $depth+0, $what);
		if (SOAP_Client::isError($kt_folder_contents))
		{
			return $kt_folder_contents;
		}
		
		if ($kt_folder_contents->status_code != 0)
		{
			return new PEAR_Error($kt_folder_contents->message);
		}
		
		return $kt_folder_contents->items;
		
	}
	
	function get_document_by_name($title)
	{
		$path = $this->full_path . '/' . $title;		
		if (substr($path,0,13) == '/Root Folder/')
		{
			$path = substr($path,13);
		}
		if (substr($path,0,12) == 'Root Folder/')
		{
			$path = substr($path,12);
		}
				
		$kt_document_detail = $this->ktapi->soapclient->get_document_detail_by_name($this->ktapi->session, $path, 'T');
		
		if (SOAP_Client::isError($kt_document_detail))
		{
			return $kt_document_detail;
		}
		
		if ($kt_document_detail->status_code != 0)
		{
			return new PEAR_Error($kt_document_detail->message);
		}
		
		return new KTWSAPI_Document($this->ktapi, $kt_document_detail);
	}

	function get_document_by_filename($filename)
	{
		$path = $this->full_path . '/' . $filename;		
		if (substr($path,0,13) == '/Root Folder/')
		{
			$path = substr($path,13);
		}
		if (substr($path,0,12) == 'Root Folder/')
		{
			$path = substr($path,12);
		}
				
		$kt_document_detail = $this->ktapi->soapclient->get_document_detail_by_name($this->ktapi->session, $path, 'F');
		
		if (SOAP_Client::isError($kt_document_detail))
		{
			return $kt_document_detail;
		}
		
		if ($kt_document_detail->status_code != 0)
		{
			return new PEAR_Error($kt_document_detail->message);
		}
		
		return new KTWSAPI_Document($this->ktapi, $kt_document_detail);
	}
	
	
	/**
	 * 
	 *
	 * @param unknown_type $foldername
	 * @return KTWSAPI_Folder
	 */
	function add_folder($foldername)
	{
		$kt_folder_detail = $this->ktapi->soapclient->create_folder($this->ktapi->session, $this->folderid, $foldername);
		if (SOAP_Client::isError($kt_folder_detail))
		{
			return $kt_folder_detail;
		}
		
		if ($kt_folder_detail->status_code != 0)
		{
			return new PEAR_Error($kt_folder_detail->message);
		}
		
		return new KTWSAPI_Folder($this->ktapi, $kt_folder_detail);
	}
	
	function delete($reason)
	{
		// TODO: check why no transaction in folder_transactions
		$kt_response = $this->ktapi->soapclient->delete_folder($this->ktapi->session, $this->folderid, $reason);
		if (SOAP_Client::isError($kt_response))
		{
			return $kt_response;
		}
		
		if ($kt_response->status_code != 0)
		{
			return new PEAR_Error($kt_response->message);
		}
		
		return true;
	}
	
	function rename($newname)
	{
		$kt_response = $this->ktapi->soapclient->rename_folder($this->ktapi->session, $this->folderid, $newname);
		if (SOAP_Client::isError($kt_response))
		{
			return $kt_response;
		}
		
		if ($kt_response->status_code != 0)
		{
			return new PEAR_Error($kt_response->message);
		}
		
		return true;
	}
	
	/**
	 * Enter description here...
	 *
	 * @param KTWSAPI_Folder $ktwsapi_target_folder
	 * @param string $reason
	 */
	function move($ktwsapi_target_folder, $reason='')
	{
		assert(!is_null($ktwsapi_target_folder));
		assert(is_a($ktwsapi_target_folder,'KTWSAPI_Folder'));
		
		$kt_response = $this->ktapi->soapclient->move_folder($this->ktapi->session, $this->folderid,$ktwsapi_target_folder->get_folderid(), $newname);
		if (SOAP_Client::isError($kt_response))
		{
			return $kt_response;
		}
		
		if ($kt_response->status_code != 0)
		{
			return new PEAR_Error($kt_response->message);
		}
		
		return true;
	}

	/**
	 * Enter description here...
	 *
	 * @param KTWSAPI_Folder $ktwsapi_target_folder
	 * @param string $reason
	 * @return unknown
	 */
	function copy($ktwsapi_target_folder, $reason='')
	{
		assert(!is_null($ktwsapi_target_folder));
		assert(is_a($ktwsapi_target_folder,'KTWSAPI_Folder'));
		
		$targetid=$ktwsapi_target_folder->get_folderid();
		
		$kt_response = $this->ktapi->soapclient->copy_folder($this->ktapi->session, $this->folderid,$targetid, $reason);
		if (SOAP_Client::isError($kt_response))
		{
			return $kt_response;
		}
		
		if ($kt_response->status_code != 0)
		{
			return new PEAR_Error($kt_response->message);
		}
		
		return true;
	}	
	
	function add_document($filename, $title=null, $documenttype=null)
	{
		if (empty($title))
		{
			$title=basename($filename);	
		}		
		$basename=basename($filename);	
		
		if (empty($documenttype))
		{
			$documenttype='Default';
		}
		
		// First step - upload file
		$tempfilename = $this->_upload_file($filename,'A');
		if (PEAR::isError($tempfilename))
		{
			return new PEAR_Error($tempfilename->message);
		}

		// Second step - move file into KT
		$kt_document_detail = $this->ktapi->soapclient->add_document($this->ktapi->session, $this->folderid, $title, $basename, $documenttype, $tempfilename );
		if (SOAP_Client::isError($kt_document_detail))
		{
			return $kt_document_detail;
		}

		if ($kt_document_detail->status_code != 0)
		{
			return new PEAR_Error($kt_document_detail->message);
		}
		
		return new KTWSAPI_Document($this->ktapi, $kt_document_detail);
	}
}

class KTWSAPI_Document extends KTWSAPI_FolderItem 
{
	var $document_id;
	var $title;
	var $document_type;
	var $version;
	var $filename;
	var $created_date;
	var $created_by;
	var $updated_date;
	var $updated_by;
	var $folder_id;
	var $workflow;
	var $workflow_state;
	var $checkout_by;
	var $full_path;
	
	function KTWSAPI_Document($ktapi, $kt_document_detail)
	{
		$this->ktapi=$ktapi;
		$this->document_id = $kt_document_detail->document_id;
		$this->title = $kt_document_detail->title;
		$this->document_type = $kt_document_detail->document_type;
		$this->version = $kt_document_detail->version;
		$this->filename = $kt_document_detail->filename;
		$this->created_date = $kt_document_detail->created_date;
		$this->created_by = $kt_document_detail->created_by;
		$this->updated_date = $kt_document_detail->updated_date;
		$this->updated_by = $kt_document_detail->updated_by;
		$this->folder_id = $kt_document_detail->folder_id;
		$this->workflow = $kt_document_detail->workflow;
		$this->workflow_state = $kt_document_detail->workflow_state;
		$this->checkout_by = $kt_document_detail->checkout_by;
		$this->full_path = $kt_document_detail->full_path;
	}
	
	function get($ktapi, $documentid, $loadinfo=true)	
	{
		assert(!is_null($ktapi));
		assert(is_a($ktapi, 'KTWSAPI'));
		assert(is_numeric($documentid));
		
		if ($loadinfo)
		{
			$kt_document_detail = $ktapi->soapclient->get_document_detail($ktapi->session, $documentid);
			if (SOAP_Client::isError($kt_document_detail))
			{
				return $kt_document_detail;
			}

			if ($kt_document_detail->status_code != 0)
			{
				return new PEAR_Error($kt_document_detail->message);
			}			
		}
		else 
		{
			$kt_document_detail = array(
				'document_id'=>$documentid,			
			);
		}
		
		return new KTWSAPI_Document($ktapi, $kt_document_detail);		
	}
	

	
	function checkin($filename, $reason, $major_update )
	{
		$basename=basename($filename);		 
		
		$tempfilename = $this->_upload_file($filename,'C', $this->document_id);
		if (PEAR::isError($tempfilename))
		{
			return new PEAR_Error($tempfilename->message);
		}
		
		$kt_response = $this->ktapi->soapclient->checkin_document($this->ktapi->session, $this->document_id, $basename, $reason, $tempfilename, $major_update );
		if (SOAP_Client::isError($kt_response))
		{
			return $kt_response;
		}

		if ($kt_response->status_code != 0)
		{
			return new PEAR_Error($kt_response->message);
		}
		
		return true;    
	}
	
	function checkout($reason, $localpath=null)
	{		
		if (is_null($localpath))
		{
			$localpath = $this->ktapi->get_download_path();
		}
		
		if (!is_dir($localpath))
		{
			return new PEAR_Error('local path does not exist');
		}
		if (!is_writable($localpath))
		{
			return new PEAR_Error('local path is not writable');
		}
		
		$kt_response = $this->ktapi->soapclient->checkout_document($this->ktapi->session, $this->document_id, $reason);
		if (SOAP_Client::isError($kt_response))
		{
			return $kt_response;
		}

		if ($kt_response->status_code != 0)
		{
			return new PEAR_Error($kt_response->message);
		}
		
		$url = $kt_response->message;
		
		$response = $this->_download_file($url, $localpath, $this->filename);
		if (PEAR::isError($response))
		{
			return new PEAR_Error($kt_response->message);
		}    	
		
		return true;  
	}
	
	function undo_checkout($reason)
	{
		$kt_response = $this->ktapi->soapclient->undo_document_checkout($this->ktapi->session, $this->document_id, $reason);
		if (SOAP_Client::isError($kt_response))
		{
			return $kt_response;
		}

		if ($kt_response->status_code != 0)
		{
			return new PEAR_Error($kt_response->message);
		}
		
		return true;
	}

	function download($version=null, $localpath=null)
	{		
		if (is_null($localpath))
		{
			$localpath = $this->ktapi->get_download_path();
		}
		
		if (!is_dir($localpath))
		{
			return new PEAR_Error('local path does not exist');
		}
		if (!is_writable($localpath))
		{
			return new PEAR_Error('local path is not writable');
		}
		
		$kt_response = $this->ktapi->soapclient->download_document($this->ktapi->session, $this->document_id);
		if (SOAP_Client::isError($kt_response))
		{
			return $kt_response;
		}

		if ($kt_response->status_code != 0)
		{
			return new PEAR_Error($kt_response->message);
		}
		
		$url = $kt_response->message;
		
		$response = $this->_download_file($url, $localpath, $this->filename);
		if (PEAR::isError($response))
		{
			return new PEAR_Error($kt_response->message);
		}    	
		
		return true;  
	}
	
	function delete($reason)
	{
		$kt_response = $this->ktapi->soapclient->delete_document($this->ktapi->session, $this->document_id, $reason);
		if (SOAP_Client::isError($kt_response))
		{
			return $kt_response;
		}

		if ($kt_response->status_code != 0)
		{
			return new PEAR_Error($kt_response->message);
		}
		
		return true;
	}	
	
	function change_owner($username, $reason)
	{
		$kt_response = $this->ktapi->soapclient->change_document_owner($this->ktapi->session, $this->document_id, $username, $reason);
		if (SOAP_Client::isError($kt_response))
		{
			return $kt_response;
		}

		if ($kt_response->status_code != 0)
		{
			return new PEAR_Error($kt_response->message);
		}
		
		return true;
	}	
	
	/**
	 * Enter description here...
	 *
	 * @param KTWSAPI_Folder $folder 
	 * @param unknown_type $reason
	 * @param unknown_type $newtitle
	 * @param unknown_type $newfilename
	 */
	
	function copy($folder,$reason,$newtitle='',$newfilename='')
	{		
		$folder_id = $folder->folderid;
		$kt_response = $this->ktapi->soapclient->copy_document($this->ktapi->session, $this->document_id, $folder_id, $reason, $newtitle, $newfilename);
		if (SOAP_Client::isError($kt_response))
		{
			return $kt_response;
		}

		if ($kt_response->status_code != 0)
		{
			return new PEAR_Error($kt_response->message);
		}
		
		return true;
	}
	function move($folder,$reason,$newtitle='',$newfilename='')
	{		
		$folder_id = $folder->folderid;
		$kt_response = $this->ktapi->soapclient->move_document($this->ktapi->session, $this->document_id, $folder_id, $reason, $newtitle, $newfilename);
		if (SOAP_Client::isError($kt_response))
		{
			return $kt_response;
		}

		if ($kt_response->status_code != 0)
		{
			return new PEAR_Error($kt_response->message);
		}
		
		return true;
	}

	function change_document_type($documenttype)
	{
		$kt_response = $this->ktapi->soapclient->change_document_type($this->ktapi->session, $this->document_id, $documenttype);
		if (SOAP_Client::isError($kt_response))
		{
			return $kt_response;
		}

		if ($kt_response->status_code != 0)
		{
			return new PEAR_Error($kt_response->message);
		}
		
		return true;
	}
	
	function rename_title( $newtitle)
	{
		$kt_response = $this->ktapi->soapclient->rename_document_title($this->ktapi->session, $this->document_id, $newtitle);
		if (SOAP_Client::isError($kt_response))
		{
			return $kt_response;
		}

		if ($kt_response->status_code != 0)
		{
			return new PEAR_Error($kt_response->message);
		}
		
		return true;
	}
	function rename_filename( $newfilename)
	{
		$kt_response = $this->ktapi->soapclient->rename_document_filename($this->ktapi->session, $this->document_id, $newfilename);
		if (SOAP_Client::isError($kt_response))
		{
			return $kt_response;
		}

		if ($kt_response->status_code != 0)
		{
			return new PEAR_Error($kt_response->message);
		}
		
		return true;
	}
	
	function start_workflow($workflow)
    {
		$kt_response = $this->ktapi->soapclient->start_document_workflow($this->ktapi->session, $this->document_id, $workflow);
		if (SOAP_Client::isError($kt_response))
		{
			return $kt_response;
		}

		if ($kt_response->status_code != 0)
		{
			return new PEAR_Error($kt_response->message);
		}
		
		return true;
    }

    function delete_document_workflow()
    {
		$kt_response = $this->ktapi->soapclient->delete_document_workflow($this->ktapi->session, $this->document_id);
		if (SOAP_Client::isError($kt_response))
		{
			return $kt_response;
		}

		if ($kt_response->status_code != 0)
		{
			return new PEAR_Error($kt_response->message);
		}
		
		return true;
    }
    
    function perform_document_workflow_transition($session_id,$document_id,$transition,$reason)
    {
		$kt_response = $this->ktapi->soapclient->delete_document_workflow($this->ktapi->session, $this->document_id, $transition, $reason);
		if (SOAP_Client::isError($kt_response))
		{
			return $kt_response;
		}

		if ($kt_response->status_code != 0)
		{
			return new PEAR_Error($kt_response->message);
		}
		
		return true;
    }
    
    function get_metadata()
    {
		$kt_metadata_response = $this->ktapi->soapclient->get_document_metadata($this->ktapi->session, $this->document_id );
		if (SOAP_Client::isError($kt_metadata_response))
		{
			return $kt_metadata_response;
		}

		if ($kt_metadata_response->status_code != 0)
		{
			return new PEAR_Error($kt_metadata_response->message);
		}
		
		return $kt_metadata_response;
    }

    function update_metadata($metadata)
    {
		$kt_response = $this->ktapi->soapclient->update_document_metadata($this->ktapi->session, $this->document_id, $metadata);
		if (SOAP_Client::isError($kt_response))
		{
			return $kt_response;
		}

		if ($kt_response->status_code != 0)
		{
			return new PEAR_Error($kt_response->message);
		}
		
		return $kt_response;
    }    
    
    function get_transaction_history()
	{
		$kt_document_transaction_history_response = $this->ktapi->soapclient->get_document_transaction_history($this->ktapi->session, $this->document_id);
		if (SOAP_Client::isError($kt_document_transaction_history_response))
		{
			return $kt_document_transaction_history_response;
		}

		if ($kt_document_transaction_history_response->status_code != 0)
		{
			return new PEAR_Error($kt_document_transaction_history_response->message);
		}
		
		return $kt_document_transaction_history_response->history;
	}
	
    function get_version_history()
	{
		$kt_document_version_history_response = $this->ktapi->soapclient->get_document_version_history($this->ktapi->session, $this->document_id);
		if (SOAP_Client::isError($kt_document_version_history_response))
		{
			return $kt_document_version_history_response;
		}

		if ($kt_document_version_history_response->status_code != 0)
		{
			return new PEAR_Error($kt_document_version_history_response->message);
		}
		
		return $kt_document_version_history_response->history;
	}    
}

class KTWSAPI
{
	var $wsdl;
	/**
	 * Enter description here...
	 *
	 * @var SOAP_Client
	 */
	var $soapclient;
	var $timeout;
	var $session;
	var $download_path;
	
	function KTWSAPI($wsdl, $timeout=30)
	{
		$this->wsdl = new SOAP_WSDL($wsdl);
		$this->timeout = $timeout;
		$this->soapclient = $this->wsdl->getProxy();
		$this->soapclient->setOpt('timeout', $this->timeout);
		$this->download_path =  'c:/temp';		
	}
	
	function get_download_path()
	{
		return $this->download_path;
	}
	
	function set_download_path($download_path)
	{
		if (!is_dir($download_path))
		{
			return new PEAR_Error('local path is not writable');
		}
		
		if (!is_writable($download_path))
		{
			return new PEAR_Error('local path is not writable');
		}		
		$this->download_path = $download_path;
	}
	
	function start_anonymous_session($ip=null)
	{
		return $this->start_session('anonymous','',$ip);
	}
	
	function start_session($username, $password, $ip=null)
	{
		if (!is_null($this->session))
		{
			return new PEAR_Error(KTWSAPI_ERR_SESSION_IN_USE);
		}
		$kt_response = $this->soapclient->login($username, $password, $ip);
		if (SOAP_Client::isError($kt_response))
		{
			return $kt_response;
		}
		
		if ($kt_response->status_code == 0)
		{
			$this->session = $kt_response->message;
		}
		else 
		{
			return new PEAR_Error($kt_response->message);
		}	
		
		return $this->session;
	}
	
	function active_session($session, $ip=null)
	{
		if (!is_null($this->session))
		{
			return new PEAR_Error(KTWSAPI_ERR_SESSION_IN_USE);
		}		
		$this->session = $session;
		
		return $session;
	}
	
	function logout()
	{
		if (is_null($this->session))
		{
			return new PEAR_Error(KTWSAPI_ERR_SESSION_NOT_STARTED);
		}		
		
		$kt_response = $this->soapclient->logout($this->session);
		
		if ($kt_response->status_code != 0)
		{
			return new PEAR_Error($kt_response->message);
		}
		$this->session = null;

		return true;
	}
	
	/**
	 * 
	 *
	 * @return KTWSAPI_Folder
	 */
	function get_root_folder()
	{
		return $this->get_folder_by_id(1);
	}
	
	/**
	 * 
	 *
	 * @return KTWSAPI_Folder
	 */	
	function get_folder_by_id($folderid)
	{
		if (is_null($this->session))
		{
			return new PEAR_Error('A session is not active');			
		}
		
		$folder = KTWSAPI_Folder::get($this, $folderid);
				
		return $folder;
	}	
	
	/**
	 * Enter description here...
	 *
	 * @param int $documentid
	 * @return KTWSAPI_Document
	 */
	function get_document_by_id($documentid)
	{
		if (is_null($this->session))
		{
			return new PEAR_Error('A session is not active');			
		}
		
		$document = KTWSAPI_Document::get($this, $documentid);
				
		return $document;
	}	
}

?>