ktrss.inc.php

来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· PHP 代码 · 共 749 行 · 第 1/2 页

PHP
749
字号
<?php
/*
 * $Id: KTrss.inc.php 8959 2008-08-03 18:14:30Z kevin_fourie $
 *
 * KnowledgeTree Community Edition
 * Document Management Made Simple
 * Copyright (C) 2008 KnowledgeTree Inc.
 * Portions copyright The Jam Warehouse Software (Pty) Limited
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License version 3 as published by the
 * Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * You can contact KnowledgeTree Inc., PO Box 7775 #87847, San Francisco,
 * California 94120-7775, or email info@knowledgetree.com.
 *
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU General Public License version 3.
 *
 * In accordance with Section 7(b) of the GNU General Public License version 3,
 * these Appropriate Legal Notices must retain the display of the "Powered by
 * KnowledgeTree" logo and retain the original copyright notice. If the display of the
 * logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices
 * must display the words "Powered by KnowledgeTree" and retain the original
 * copyright notice.
 * 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('\\\'','\'',htmlspecialchars($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){
    	$hostPath = KTUtil::kt_url() . '/';

    	$head = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n
    	       <rss version=\"2.0\">\n
    	           <channel>\n
    	               <title>".APP_NAME." RSS</title>\n
    	               <copyright>(c) 2008 KnowledgeTree Inc.</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."</link>\n
        	               <url>".$hostPath."resources/graphics/ktlogo_rss.png</url>\n
    	               </image>\n";


    	$feed = '';
    	foreach($aItems as $aItem){

    	    $aItemHead = $aItem[0][0];
    	    $aItemList = $aItem[1];

	    	if($aItem[0][itemType] == 'folder'){
	    		$sTypeSelect = 'folder.transactions&fFolderId';
	    	}elseif($aItem[0][itemType] == 'document'){
	    		$sTypeSelect = 'document.transactionhistory&fDocumentId';
	    	}


	    	if($aItem[0][0][owner]){
	    	    $owner = $aItem[0][0][owner];
	    	}else{
	    	    $owner = _kt('None');
	    	}

	    	$type = '';
	    	if($aItem[0][0][type]){
	    	    $type = '<tr><td>Document type: '.$aItem[0][0][type]."</td>\n<td></td></tr>\n";
	    	}

	    	if($aItem[0][0][workflow_status]){
	    	    $workflow = $aItem[0][0][workflow_status];
	    	}else{
	    	    $workflow = _kt('No Workflow');
	    	}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?