KTrss.inc.php 24.8 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
<?php
/*
 * The contents of this file are subject to the KnowledgeTree Public
 * License Version 1.1.2 ("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.
 *
 * All copies of the Covered Code must include on each user interface screen:
 *    (i) the "Powered by KnowledgeTree" logo and
 *    (ii) the KnowledgeTree copyright notice
 * in the same form as they appear in the distribution.  See the License for
 * requirements.
 *
 * 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.
 * Contributor( s): ______________________________________
 *
 */

// boilerplate.
require_once(KT_LIB_DIR . "/templating/templating.inc.php");
require_once(KT_LIB_DIR . "/templating/kt3template.inc.php");
require_once(KT_LIB_DIR . "/dispatcher.inc.php");
require_once(KT_LIB_DIR . "/util/ktutil.inc");
require_once(KT_LIB_DIR . "/database/dbutil.inc");

// document related includes
require_once(KT_LIB_DIR . "/documentmanagement/Document.inc");
require_once(KT_LIB_DIR . "/documentmanagement/DocumentType.inc");
require_once(KT_LIB_DIR . "/documentmanagement/DocumentFieldLink.inc");
require_once(KT_LIB_DIR . "/documentmanagement/documentmetadataversion.inc.php");
require_once(KT_LIB_DIR . "/documentmanagement/documentcontentversion.inc.php");
require_once(KT_LIB_DIR . "/metadata/fieldset.inc.php");
require_once(KT_LIB_DIR . "/security/Permission.inc");

require_once(KT_LIB_DIR . "/actions/documentaction.inc.php");
require_once(KT_LIB_DIR . "/browse/browseutil.inc.php");

class KTrss{
    // Gets a listing of external feeds for user
    function getExternalFeedsList($iUserId){
    	$sQuery = "SELECT id, url, title FROM plugin_rss WHERE user_id = ?";
        $aParams = array($iUserId);
        $aFeeds = DBUtil::getResultArray(array($sQuery, $aParams));

        if (PEAR::isError($aFeeds)) {
            // XXX: log error
            return false;
        }
        if ($aFeeds) {
            return $aFeeds;
        }
    }

    // Gets full listing of data of documents and folders subscribed to
    function getInternalFeed($iUserId){
    	$documents = KTrss::getDocuments($iUserId);
    	$folders = KTrss::getFolders($iUserId);

    	if (is_null($documents)) $documents=array();
    	if (is_null($folders)) $folders=array();

    	$response = '';
    	$aFullList = kt_array_merge($documents,$folders );
    	if(!empty($aFullList)){
    		$internalFeed = KTrss::arrayToXML($aFullList);
    		$response = rss2arrayBlock($internalFeed);
    	}
    	return $response;
    }

    // Get the data for the document or folder
    function getExternalInternalFeed($sFeed, $iUserId){
        $aRss = array();
        $pos = strpos($sFeed, 'docId');

        if($pos === false){
            $pos = strpos($sFeed, 'folderId');
            $folderId = substr($sFeed, $pos+9);
            $aRss[] = KTrss::getOneFolder($folderId);
        }else{
            $docId = substr($sFeed, $pos+6);
            $aRss[] = KTrss::getOneDocument($docId, $iUserId);
        }

    	if($aRss){
    		$internalFeed = KTrss::arrayToXML($aRss);
    		$response = rss2arrayBlock($internalFeed);
    	}
    	return $response;
    }

    // Get list of document subscriptions
    function getDocumentList($iUserId){
    	$sQuery = "SELECT document_id as id FROM document_subscriptions WHERE user_id = ?";
        $aParams = array($iUserId);
        $aDocumentList = DBUtil::getResultArrayKey(array($sQuery, $aParams), 'id');

        if (PEAR::isError($aDocumentList)) {
            // XXX: log error
            return false;
        }
        if($aDocumentList){
            return $aDocumentList;
        }
    }

    // Get list of folder subscriptions
    function getFolderList($iUserId){
        $sQuery = "SELECT folder_id as id FROM folder_subscriptions WHERE user_id = ?";
        $aParams = array($iUserId);
        $aFolderList = DBUtil::getResultArrayKey(array($sQuery, $aParams), 'id');

        if (PEAR::isError($aFolderList)) {
            // XXX: log error
            return false;
        }
        if ($aFolderList) {
            return $aFolderList;
        }
    }

    // Get data for all documents subscribed to
    function getDocuments($iUserId){
    	$aDList = KTrss::getDocumentList($iUserId);
    	if($aDList){
	    	foreach($aDList as $document_id){
		        $document = KTrss::getOneDocument($document_id, $iUserId);
		        if($document){
		        	$aDocuments[] = $document;
		        }
	    	}
    	}
    	if (PEAR::isError($aDocuments)) {
            // XXX: log error
            return false;
        }
        if ($aDocuments) {
            return $aDocuments;
        }
    }

    // Get data for all folders subscribed to
    function getFolders($iUserId){
    	$aFList = KTrss::getFolderList($iUserId);

    	if($aFList){
	    	foreach($aFList as $folder_id){
		        $folder = KTrss::getOneFolder($folder_id);
		        if($folder){
		        	$aFolders[] = $folder;
		        }
	    	}
    	}

    	if (PEAR::isError($aFolders)) {
            // XXX: log error
            return false;
        }
        if ($aFolders){
            return $aFolders;
        }
    }

    function getChildrenFolderTransactions($iParentFolderId, $depth = '1'){
        $aParams = array($iParentFolderId);

        if($depth == '1'){
            // Get direct child folder id's
            $sQuery = "SELECT id FROM folders WHERE parent_id = ?";
        }else{
            // Get all child folders
            if($iParentFolderId == 1){
                $sQuery = "SELECT id FROM folders WHERE parent_folder_ids LIKE '?' OR parent_folder_ids LIKE '?,%'";
            }
	    	$sQuery = "SELECT id FROM folders WHERE parent_folder_ids LIKE '%,?' OR parent_folder_ids LIKE '%,?,%'";
	    	$aParams[] = $iParentFolderId;
    	}

        $aFolderList = DBUtil::getResultArrayKey(array($sQuery, $aParams), 'id');

        if (PEAR::isError($aFolderList)) {
            // XXX: log error
            return false;
        }
        return $aFolderList;
    }

    function getChildrenDocumentTransactions($iParentFolderId, $depth = '1'){
    	$aParams = array($iParentFolderId);

        if($depth == '1'){
            // Get direct child document id's
            $sQuery = "SELECT id FROM documents WHERE folder_id = ?";
        }else{
            // Get all documents in child folders
            if($iParentFolderId == 1){
                $sQuery = "SELECT id FROM documents WHERE parent_folder_ids LIKE '?' OR parent_folder_ids LIKE '?,%'";
            }
	    	$sQuery = "SELECT id FROM documents WHERE parent_folder_ids LIKE '%,?' OR parent_folder_ids LIKE '%,?,%'";
	    	$aParams[] = $iParentFolderId;
    	}

        $aDocumentList = DBUtil::getResultArrayKey(array($sQuery, $aParams), 'id');

        if (PEAR::isError($aDocumentList)) {
            // XXX: log error
            return false;
        }

        if ($aDocumentList) {
            $aDocumentTransactions = KTrss::getDocumentTransactions($aDocumentList);
        }
        if ($aDocumentTransactions){
            return $aDocumentTransactions;
        }
    }

    // get information on document
    function getOneDocument($iDocumentId, $iUserId){
        $aDData = KTrss::getDocumentData($iUserId, $iDocumentId);
        $aDTransactions = KTrss::getDocumentTransactions(array($iDocumentId));

        if($aDData){
        	$aDData['itemType'] = 'document';

    		// create mime info
			$aMimeInfo = KTrss::getMimeTypeInfo($iUserId, $iDocumentId);
			$aDData['mimeTypeFName'] = $aMimeInfo['typeFName'];
			$aDData['mimeTypeIcon'] = $aMimeInfo['typeIcon'];

        	$aDocument[] = $aDData;
        	$aDocument[] = $aDTransactions;
        }
    	if (PEAR::isError($aDData)) {
            return false;
        }
        if ($aDocument){
            return $aDocument;
        }
    }

    // get information for folder
    function getOneFolder($iFolderId){
    	$aFolder = array();
    	$aFData = KTrss::getFolderData($iFolderId);

    	if (PEAR::isError($aFData)) {
            return false;
        }

    	// Get child folder ids
    	$aFolderIds = KTrss::getChildrenFolderTransactions($iFolderId);

    	// Get folder transactions
    	$aFolderIds[] = $iFolderId;
    	$aFTransactions = KTrss::getFolderTransactions($aFolderIds);

    	if(PEAR::isError($aFTransactions)){
    	    return false;
    	}

    	// Get child document transactions
    	$aDocTransactions = KTrss::getChildrenDocumentTransactions($iFolderId);

    	if(!empty($aDocTransactions)){
            $aFTransactions = array_merge($aFTransactions, $aDocTransactions);

            // Sort the child folder and document transactions by date and reduce to 4
            $code = 'if (strtotime($a[datetime]) == strtotime($b[datetime])){
    	        return 0;
    	    }
    	    return (strtotime($a[datetime]) > strtotime($b[datetime])) ? -1 : 1;';

    		$compare = create_function('$a,$b', $code);

            usort($aFTransactions, $compare);
            $aFTransactions = array_slice($aFTransactions, 0, 4);
    	}

        if($aFData){
        	$aFData['itemType'] = 'folder';

    		// create mime info
			$aFData['mimeTypeFName'] = 'Folder';
			$aFData['mimeTypeIcon'] = KTrss::getFolderIcon();

        	$aFolder[] = $aFData;
        	$aFolder[] = $aFTransactions;
        }
        if ($aFolder){
            return $aFolder;
        }
    }

    function rss_sanitize($str, $do_amp=true)
    {

        $result = str_replace("\\\"","\"",str_replace('\\\'','\'',htmlentities($str,ENT_NOQUOTES, 'UTF-8')));
        if ($do_amp)
        {
            $result = str_replace('&','&amp;',$result);
        }
        return $result;
    }

    // Takes in an array as a parameter and returns rss2.0 compatible xml
    function arrayToXML($aItems){
    	// Build path to host
    	$aPath = explode('/', trim($_SERVER['PHP_SELF']));
    	global $default;
    	if(count($aPath) > 2){
    		for($i = 0; $i < count($aPath)-1; $i++){
    			$sSuffix .= $aPath[$i];
    		}
    		$sSuffix = $aPath[1]."/";
    	}else{
    		$sSuffix = '';
    	}
    	$hostPath = "http" . ($default->sslEnabled ? "s" : "") . "://".$_SERVER['HTTP_HOST']."/".$sSuffix;
    	$feed = "<?xml version=\"1.0\"?>\n";
    	$feed .= "<rss version=\"2.0\">\n".
    			 "<channel>\n" .
	    			"<title>".APP_NAME." RSS</title>\n" .
	    			"<copyright>(c) 2007 The Jam Warehouse Software (Pty) Ltd. All Rights Reserved</copyright>\n" .
	    			"<link>".$hostPath."</link>\n" .
	    			"<description>KT-RSS</description>\n" .
	    			"<image>\n".
					"<title>".APP_NAME." RSS</title>\n".
					"<width>140</width>\n".
					"<height>28</height>".
					"<link>".$hostPath."knowledgeTree/</link>\n".
					"<url>".$hostPath."resources/graphics/ktlogo_rss.png</url>\n".
					"</image>\n";
	    foreach($aItems as $aItems){
	    	if($aItems[0][itemType] == 'folder'){
	    		$sTypeSelect = 'folder.transactions&amp;fFolderId';
	    	}elseif($aItems[0][itemType] == 'document'){
	    		$sTypeSelect = 'document.transactionhistory&amp;fDocumentId';
	    	}
	    	$feed .= "<item>\n" .
	    	         	"<title>".htmlentities(KTrss::rss_sanitize($aItems[0][0][name],false), ENT_QUOTES, 'UTF-8')."</title>\n" .
	    	         	"<link>".$hostPath."action.php?kt_path_info=ktcore.actions.".$sTypeSelect."=".$aItems[0][0]['id']."</link>\n" .
	    	         	"<description>\n" .
	    	         	"&lt;table border='0' width='90%'&gt;\n".
			 				"&lt;tr&gt;\n".
								"&lt;td width='5%' height='16px'&gt;" .
									"&lt;a href='".$hostPath."action.php?kt_path_info=ktcore.actions.".$sTypeSelect."=".$aItems[0][0][id]."' &gt;&lt;img src='".$aItems[0][mimeTypeIcon]."' align='left' height='16px' width='16px' alt='' border='0' /&gt;&lt;/a&gt;" .
								"&lt;/td&gt;\n".
								"&lt;td align='left'&gt; ".$aItems[0][mimeTypeFName]."&lt;/td&gt;\n".
							"&lt;/tr&gt;\n".
							"&lt;tr&gt;\n".
								"&lt;td colspan='2'&gt;\n".
									ucfirst($aItems[0]['itemType'])." Information (ID: ".$aItems[0][0][id].")&lt;/&gt;\n".
									"&lt;hr&gt;\n".
									"&lt;table width='95%'&gt;\n".
										"&lt;tr&gt;\n".
											"&lt;td&gt;Filename: ".KTrss::rss_sanitize($aItems[0][0][filename])."&lt;/td&gt;\n".
											"&lt;td&gt;\n".
										"&lt;/tr&gt;\n".
										"&lt;tr&gt;\n".
											"&lt;td&gt;Author: ".$aItems[0][0][author]."&lt;/td&gt;\n".
											"&lt;td&gt;\n".
										"&lt;/tr&gt;\n".
										"&lt;tr&gt;\n".
											"&lt;td&gt;Owner: ";if($aItems[0][0][owner]){$feed .= $aItems[0][0][owner];}else{$feed .= "None";}
											$feed .= "&lt;/td&gt;\n".
											"&lt;td&gt;&lt;/td&gt;\n".
										"&lt;/tr&gt;\n".
										"&lt;tr&gt;\n";if($aItems[0][0][type]){
											$feed .= "&lt;td&gt;Document type: ".$aItems[0][0][type]."&lt;/td&gt;\n".
											"&lt;td&gt;&lt;/td&gt;\n";}
										$feed .= "&lt;/tr&gt;\n".
										"&lt;tr&gt;\n".
											"&lt;td&gt;Workflow status: ";if($aItems[0][0][workflow_status]){$feed .= $aItems[0][0][workflow_status];}else{$feed .= "No Workflow";}
											$feed .= "&lt;/td&gt;\n".
											"&lt;td&gt;&lt;/td&gt;\n".
										"&lt;/tr&gt;\n".
									"&lt;/table&gt;&lt;br&gt;\n".
									"Transaction Summary (Last 4)\n".
									"&lt;hr&gt;\n".
									"&lt;table width='100%'&gt;\n";
										foreach($aItems[1] as $item){
										$feed .= "&lt;tr&gt;\n".
											"&lt;td&gt;".$item[type]." name:&lt;/td&gt;\n".
											"&lt;td&gt;".KTrss::rss_sanitize($item[name] )."&lt;/td&gt;\n".
										"&lt;/tr&gt;\n".
										"&lt;tr&gt;\n".
											"&lt;td&gt;Path:&lt;/td&gt;\n".
											"&lt;td&gt;".KTrss::rss_sanitize($item[fullpath] )."&lt;/td&gt;\n".
										"&lt;/tr&gt;\n".
										"&lt;tr&gt;\n".
											"&lt;td&gt;Transaction:&lt;/td&gt;\n".
											"&lt;td&gt;".$item[transaction_name]."&lt;/td&gt;\n".
										"&lt;/tr&gt;\n".
										"&lt;tr&gt;\n".
											"&lt;td&gt;Comment:&lt;/td&gt;\n".
											"&lt;td&gt;".KTrss::rss_sanitize($item[comment] )."&lt;/td&gt;\n".
										"&lt;/tr&gt;\n".
										"&lt;tr&gt;\n";if($item[version]){
											$feed .= "&lt;td&gt;Version:&lt;/td&gt;\n".
											"&lt;td&gt;".$item[version]."&lt;/td&gt;\n";}
										$feed .= "&lt;/tr&gt;\n".
										"&lt;tr&gt;\n".
											"&lt;td&gt;Date:&lt;/td&gt;\n".
											"&lt;td&gt;".$item[datetime]."&lt;/td&gt;\n".
										"&lt;/tr&gt;\n".
										"&lt;tr&gt;\n".
											"&lt;td&gt;User:&lt;/td&gt;\n".
											"&lt;td&gt;".$item[user_name]."&lt;/td&gt;\n".
										"&lt;/tr&gt;\n".
										"&lt;tr&gt;\n".
											"&lt;td colspan='2'&gt;&lt;hr width='100' align='left'&gt;&lt;/td&gt;\n".
										"&lt;/tr&gt;\n";}
								$feed .= "&lt;/table&gt;\n".
								"&lt;/td&gt;\n".
							"&lt;/tr&gt;\n".
						"&lt;/table&gt;".
						"</description>\n".
	    			 "</item>\n";
	    }
	    $feed .= "</channel>\n" .
	    		 "</rss>\n";

	   return $feed;
    }

    // Takes in an array as a parameter and returns rss2.0 compatible xml
    function errorToXML($sError){
    	// Build path to host
    	$aPath = explode('/', trim($_SERVER['PHP_SELF']));
    	global $default;
    	$hostPath = "http" . ($default->sslEnabled ? "s" : "") . "://".$_SERVER['HTTP_HOST']."/".$aPath[1]."/";
    	$feed = "<?xml version=\"1.0\"?>\n";
    	$feed .= "<rss version=\"2.0\">\n".
    			 "<channel>\n" .
	    			"<title>".APP_NAME." RSS</title>\n" .
	    			"<copyright>(c) 2007 The Jam Warehouse Software (Pty) Ltd. All Rights Reserved</copyright>\n" .
	    			"<link>".$hostPath."</link>\n" .
	    			"<description>KT-RSS</description>\n" .
	    			"<image>\n".
					"<title>".APP_NAME." RSS</title>\n".
					"<width>140</width>\n".
					"<height>28</height>".
					"<link>".$hostPath."knowledgeTree/</link>\n".
					"<url>".$hostPath."resources/graphics/ktlogo_rss.png</url>\n".
					"</image>\n";
    	$feed .= "<item>\n".
    	         	"<title>Feed load error</title>\n" .
    	         	"<description>".$sError."</description>\n".
    			 "</item>\n";
	    $feed .= "</channel>\n" .
	    		 "</rss>\n";

	   return $feed;
    }

    // Delete feed function
    function deleteFeed($iFeedId){
    	$res = DBUtil::autoDelete('plugin_rss', $iFeedId);
    }

    // Get title for external feed
    function getExternalFeedTitle($iFeedId){
    	$sQuery = "SELECT title FROM plugin_rss WHERE id = ?";
        $aParams = array($iFeedId);
        $sFeedTitle = DBUtil::getOneResultKey(array($sQuery, $aParams), 'title');

        if (PEAR::isError($sFeedTitle)) {
            // XXX: log error
            return false;
        }
        if ($sFeedTitle) {
            return $sFeedTitle;
        }
    }

    // Get url for external feed
    function getExternalFeedUrl($iFeedId){
    	$sQuery = "SELECT url FROM plugin_rss WHERE id = ?";
        $aParams = array($iFeedId);
        $sFeedUrl = DBUtil::getOneResultKey(array($sQuery, $aParams), 'url');

        if (PEAR::isError($sFeedUrl)) {
            // XXX: log error
            return false;
        }
        if ($sFeedUrl) {
            return $sFeedUrl;
        }
    }

    // Update external feed data
    function updateFeed($iFeedId, $sFeedTitle, $sFeedUrl){
    	$sQuery = "UPDATE plugin_rss SET title=?, url=? WHERE id=?";
        $aParams = array($sFeedTitle, $sFeedUrl, $iFeedId);
        $res = DBUtil::runQuery(array($sQuery, $aParams));

        return $res;
    }

    // Create new external feed
    function createFeed($sFeedTitle, $sFeedUrl, $iUserId){
        $aParams = array(
        'user_id' => $iUserId,
        'url' => $sFeedUrl,
        'title' => $sFeedTitle,
        );
        $res = DBUtil::autoInsert('plugin_rss', $aParams);

        return $res;
    }

    // Function to validate that a user has permissions for a specific document
    function validateDocumentPermissions($iUserId, $iDocumentId){
		// check if user id is in session. If not, set it
		if(!isset($_SESSION["userID"])){
			$_SESSION['userID'] = $iUserId;
		}
		// get document object
		$oDocument =& Document::get($iDocumentId);
		if (PEAR::isError($oDocument)) {
            return false;
        }

		// check permissions for document
		if(Permission::userHasDocumentReadPermission($oDocument)){
		    return true;
		}else{
			return false;
		}
	}

	// Function to validate that a user has permissions for a specific folder
	function validateFolderPermissions($iUserId, $iFolderId){
		// check if user id is in session. If not, set it
		if(!isset($_SESSION["userID"])){
			$_SESSION['userID'] = $iUserId;
		}
		// get folder object
		$oFolder = Folder::get($iFolderId);
		if (PEAR::isError($oFolder)) {
            return false;
        }

		// check permissions for folder
		if(Permission::userHasFolderReadPermission($oFolder)){
		    return true;
		}else{
			return false;
		}
	}

	// get icon link for rss
	function getRssLinkIcon(){
    	// built server path
        global $default;
    	$sHostPath = "http" . ($default->sslEnabled ? "s" : "") . "://".$_SERVER['HTTP_HOST']."/".$GLOBALS['KTRootUrl']."/";

        // create image
        $icon = "<img src='".$sHostPath."resources/graphics/rss.gif' alt='RSS' border=0/>";

        return $icon;
    }

    // get rss link for a document/folder
    function getRssLink($iItemId, $sItemType){
        $item = strToLower($sItemType);
        if($item == 'folder'){
        	$sItemParameter = '?folderId';
        }else if($item == 'document'){
        	$sItemParameter = '?docId';
        }

        // built server path
        global $default;
        $sHostPath = "http" . ($default->sslEnabled ? "s" : "") . "://" . $_SERVER['HTTP_HOST'];

        // build link
    	$sLink = $sHostPath.KTBrowseUtil::buildBaseUrl('rss').$sItemParameter.'='.$iItemId;

    	return $sLink;
    }

    // get rss icon link
    function getImageLink($iItemId, $sItemType){
    	return "<a href='".KTrss::getRssLink($iItemId, $sItemType)."' target='_blank'>".KTrss::getRssLinkIcon()."</a>";
    }

    // get the mime type id for a document
    function getDocumentMimeTypeId($iUserId, $iDocumentId){
		if(!isset($_SESSION["userID"])){
			$_SESSION['userID'] = $iUserId;
		}
		// get document object
		$oDocument =& Document::get($iDocumentId);

		$docMime = $oDocument->getMimeTypeID();
		return $docMime;
	}

	// get mime information for a document
    function getMimeTypeInfo($iUserId, $iDocumentId){
        global $default;
    	$mimeinfo['typeId'] = KTrss::getDocumentMimeTypeId($iUserId, $iDocumentId); // mime type id
		$mimeinfo['typeName'] = KTMime::getMimeTypeName($mimeinfo['typeId']); // mime type name
		$mimeinfo['typeFName'] = KTMime::getFriendlyNameForString($mimeinfo['typeName']); // mime type friendly name
		$mimeinfo['typeIcon'] = "http" . ($default->sslEnabled ? "s" : "") . "://".$_SERVER['HTTP_HOST']."/".$GLOBALS['KTRootUrl']."/resources/mimetypes/".KTMime::getIconPath($mimeinfo['typeId']).".png"; //icon path

		return $mimeinfo;
    }

    // get the default folder icon
    function getFolderIcon(){
    	global $default;
    	return $mimeinfo['typeIcon'] = "http" . ($default->sslEnabled ? "s" : "") . "://".$_SERVER['HTTP_HOST']."/".$GLOBALS['KTRootUrl']."/thirdparty/icon-theme/16x16/mimetypes/x-directory-normal.png"; //icon path
    }

    // get a document information
    function getDocumentData($iUserId, $iDocumentId){
    	if(!isset($_SESSION["userID"])){
			$_SESSION['userID'] = $iUserId;
		}
		// get document object
		$oDocument =& Document::get($iDocumentId);

		$cv = $oDocument->getContentVersionId();
		$mv = $oDocument->getMetadataVersionId();

		$sQuery = "SELECT dcv.document_id AS id, dmver.name AS name, dcv.filename AS filename, c.name AS author, o.name AS owner, dtl.name AS type, dwfs.name AS workflow_status " .
				"FROM documents AS d LEFT JOIN document_content_version AS dcv ON d.id = dcv.document_id " .
				"LEFT JOIN users AS o ON d.owner_id = o.id " .
				"LEFT JOIN users AS c ON d.creator_id = c.id " .
				"LEFT JOIN document_metadata_version AS dmv ON d.id = dmv.document_id " .
				"LEFT JOIN document_types_lookup AS dtl ON dmv.document_type_id = dtl.id " .
				"LEFT JOIN document_metadata_version AS dmver ON d.id = dmver.document_id " .
				"LEFT JOIN workflow_states AS dwfs ON dmver.workflow_state_id = dwfs.id " .
				"WHERE d.id = ? " .
				"AND dmver.id = ? " .
				"AND dcv.id = ? " .
				"LIMIT 1";

		$aParams = array($iDocumentId, $mv, $cv);
        $aDocumentData = DBUtil::getResultArray(array($sQuery, $aParams));
        if($aDocumentData){
			return $aDocumentData;
        }
    }

    // get a folder information
    function getFolderData($iFolderId){
		$sQuery = "SELECT f.id AS id, f.name AS name, f.name AS filename, c.name AS author, o.name AS owner, f.description AS description " .
				"FROM folders AS f " .
				"LEFT JOIN users AS o ON f.owner_id = o.id " .
				"LEFT JOIN users AS c ON f.creator_id = c.id " .
				"WHERE f.id = ? " .
				"LIMIT 1";

		$aParams = array($iFolderId);
        $aFolderData = DBUtil::getResultArray(array($sQuery, $aParams));
        if($aFolderData){
			return $aFolderData;
        }
    }

    // get a listing of the latest 3 transactions for a document
    function getDocumentTransactions($aDocumentIds){
        $sDocumentIds = implode(', ', $aDocumentIds);

    	$sQuery = "SELECT DT.datetime AS datetime, 'Document' AS type, DMV.name, D.full_path AS fullpath,
    	   DTT.name AS transaction_name, U.name AS user_name, DT.version AS version, DT.comment AS comment
    	   FROM document_transactions AS DT
    	   INNER JOIN users AS U ON DT.user_id = U.id
    	   INNER JOIN document_transaction_types_lookup AS DTT ON DTT.namespace = DT.transaction_namespace
    	   LEFT JOIN documents AS D ON DT.document_id = D.id
    	   LEFT JOIN document_metadata_version AS DMV ON D.metadata_version_id = DMV.id
    	   WHERE DT.document_id IN ($sDocumentIds)
    	   ORDER BY DT.datetime DESC
    	   LIMIT 4";

    	$aDocumentTransactions = DBUtil::getResultArray($sQuery);
    	if(!PEAR::isError($aDocumentTransactions)){
            return $aDocumentTransactions;
    	}
    }

    // Get a listing of the latest transactions for a folder and its child folders
    function getFolderTransactions($aFolderIds){
        $sFolderIds = implode(', ', $aFolderIds);

    	$sQuery = "SELECT FT.datetime AS datetime, 'Folder' AS type, F.name, F.full_path AS fullpath,
    	   DTT.name AS transaction_name, U.name AS user_name, FT.comment AS comment
    	   FROM folder_transactions AS FT LEFT JOIN users AS U ON FT.user_id = U.id
    	   LEFT JOIN document_transaction_types_lookup AS DTT ON DTT.namespace = FT.transaction_namespace
    	   LEFT JOIN folders AS F ON FT.folder_id = F.id
    	   WHERE FT.folder_id IN ($sFolderIds)
    	   ORDER BY FT.datetime DESC
    	   LIMIT 4";

    	$aFolderTransactions = DBUtil::getResultArray($sQuery);
		return $aFolderTransactions;
    }
}
?>