sitemap.inc

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

INC
676
字号
<?php
/**
 * $Id: SiteMap.inc 8387 2008-04-22 16:36:04Z kevin_fourie $
 *
 * Maintains (page, access) access map, as well as (section, page) map.
 *
 * 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): ______________________________________
 */

require_once(KT_LIB_DIR . '/security/Permission.inc');
// define access constants
define("None", 1);
define("Guest", 2);
define("User", 3);
define("UnitAdmin", 4);
define("SysAdmin", 5);

class SiteMap {

    /**
     * The underlying site map storage array
     */
    var $aSiteMap;
	var $aSectionColours;

    /**
     * Whether to use the database to store the sitemap or not
     */
    var $bUseDB;

     /**
     * Whether to the user accessing the page is a Sysadmin
     */
    var $isSysAdmin;

     /**
     * * Whether to the user accessing the page is a Unitadmin
     */
    var $isUnitAdmin;

    /**
     * Constructs a new SiteMap
     * If the db is not being used, the array is initialised.
     *
     * @param boolean whether to use the database to store the sitemap or not
     */
    function SiteMap($bUseDB) {
        $this->bUseDB = $bUseDB;
        if (!$this->bUseDB) {
            $this->aSiteMap = array();
        }
		$this->aSectionColours = array();
    }

    /**
     * Sets the database flag
     *
     * @param boolean whether to use the database to store the sitemap or not
     */
    function setUseDB($bUseDB) {
        $this->bUseDB = $bUseDB;
    }

    /**
     * Returns the database flag
     */
    function getUseDB() {
        return $this->bUseDB;
    }


    /**
     * Adds a site page mapping entry.
     *
     * @param string  the controller action
     * @param string  the corresponding page for this action
     * @param string  the section this page falls under
     * @param int     the minimum access needed to access this page
     * @param string  description of the page for link presentation
     * @param boolean whether this action is enabled or not
     */
    function addPage($sAction, $sPage, $sSectionName, $sRequiredAccess, $sLinkText, $bEnabled = true, $iOrder = 0) {
        if (!$this->bUseDB) {
            $this->aSiteMap[$sSectionName][$sRequiredAccess][$sAction] = array ("page" => $sPage,
                    "description" => $sLinkText,
                    "enabled" => (($bEnabled) ? "1" : "0"),
                    "default" => "0",
                    "order" => $iOrder);
        }
    }

	function addSectionColour($sSectionName, $sHtmlElement, $sColour) {
		$this->aSectionColours[$sSectionName][$sHtmlElement] = $sColour;
	}

	function getSectionColour($sSectionName, $sHtmlElement) {
		return $this->aSectionColours[$sSectionName][$sHtmlElement];
	}

    /**
     * Adds a site page mapping entry- the default page for the section.
     *
     * @param string the controller action
     * @param string the corresponding page for this action
     * @param string the section this page falls under
     * @param int    the minimum access needed to access this page
     * @param string description of the page for link presentation
     */
    function addDefaultPage($sAction, $sPage, $sSectionName, $sRequiredAccess, $sLinkText, $bEnabled = true, $iOrder = 0) {
        if (!$this->bUseDB) {
            $this->aSiteMap[$sSectionName][$sRequiredAccess][$sAction] = array ("page" => $sPage,
                    "description" => $sLinkText,
                    "enabled" => (($bEnabled) ? "1" : "0"),
                    "default" => "1",
                    "order" => $iOrder);
        }
    }

    /**
     * Returns true if the user has the necessary rights to access
     * a sitemap entry
     *
     * @param int the required access (defined above class)
     * @return boolean true if the user has the access, else false.
     */
    function hasPermission($requiredAccess) {
        return true;
    }

    /**
     * Returns controller links for a section.
     * Checks whether to use the db or not and calls the appropriate method
     *
     * @param string the section to return links for
     */
    function getSectionLinks($sSectionName, $bSortLinks = true) {
        if ($this->bUseDB) {
            return $this->getSectionLinksDB($sSectionName, $bSortLinks);
        } else {
            return $this->getSectionLinksArray($sSectionName, $bSortLinks);
        }
    }

    /**
     * Returns controller links for a section (uses the db)
     *
     * @param string the section to return links for
     */
    function getSectionLinksDB($sSectionName, $bSortLinks) {
        global $default, $lang_err_database;
        $iFolderID = $_REQUEST['fFolderID'];
        $sql = $default->db;
        // lookup sectionID
        $sectionID = lookupID($default->site_sections_table, "name", $sSectionName);
        if ($sectionID) {
            // initialise result array
            $results = array("descriptions" => array(), "links" => array());

            $sQuery = "SELECT link_text, action, access_id FROM $default->sitemap_table WHERE " ./*ok*/
                "section_id = ? AND is_enabled = ? AND is_default=0";
            if ($bSortLinks) {
                $sQuery .= " ORDER BY link_text ASC";
            }
            $aParams = array($sectionID, true);

            if ($sql->query(array($sQuery, $aParams))) {
                while ($sql->next_record()) {
                    // check permissions
                  $default->log->info("SiteMap:: getSectionLinksDB calling hasPermission with access:  " . $sql->f("access_id"));
                    if ($this->hasPermission($sql->f("access_id"))) {
                        // add this array to the resultset array if there is link text
                        if (strlen($sql->f("link_text")) > 0) {
                            $results["descriptions"][] = $sql->f("link_text");
                            // if fFolderID is set and fFolderID is in the page string
                            // append folderID to the controller link
                            $results["links"][] = isset($iFolderID) ? generateControllerLink($sql->f("action"), "fFolderID=$iFolderID") : generateControllerLink($sql->f("action"), "");
                        }
                    }
                }
                // now check if we have anything in the results array before returning it
                if (count($results) > 0) {
                    return $results;
                } else {
                    return false;
                }
            } else {
                $_SESSION["errorMessage"] = $lang_err_database;
                return false;
            }
        } else {
            $_SESSION["errorMessage"] = "No such section name ($sSectionName) in the sitemap";
            return false;
        }
    }

    /**
     * Returns controller links for a section (uses the array)
     *
     * @param string the section to return links for
     */
    function getSectionLinksArray($sSectionName, $bSortLinks) {
        global $default;
        $iFolderID = $_REQUEST['fFolderID'];

        // check if the section exists
        if (is_array($this->aSiteMap[$sSectionName])) {
            // initialise result array
            $results = array("descriptions" => array(), "links" => array());

            // need to loop through all (access, page) arrays in this section
            foreach ($this->aSiteMap[$sSectionName] as $requiredAccess => $pages) {
            	//$default->log->info("SECTION:$sSectionName; access=" . arrayToString($requiredAccess) . ";pages=" . arrayToString($pages));
            	$default->log->info("SiteMap:: getSectionLinksArray calling hasPermission with requiredAccess:  " . $requiredAccess);
                if ($this->hasPermission($requiredAccess)) {

                    foreach ($pages as $action => $pageDetail) {
                        // add this array to the resultset array if there is link text and it is enabled
                        // but not if it is the default page

                        if ((strlen($pages[$action]["description"]) > 0) &&
                                ($pages[$action]["enabled"]) && (!$pages[$action]["default"])) {
                            if (!$bSortLinks) {
	                            $results["descriptions"][$pages[$action]["order"]] = $pages[$action]["description"];

	                            $results["links"][$pages[$action]["order"]] = isset($iFolderID) ? generateControllerLink($action, "fFolderID=$iFolderID") : generateControllerLink($action, "");
                            } else {
	                            $results["descriptions"][] = $pages[$action]["description"];

	                            $results["links"][] = isset($iFolderID) ? generateControllerLink($action, "fFolderID=$iFolderID") : generateControllerLink($action, "");
                            }
                        }
                    }
                }
            }
            // now check if we have anything in the results array before returning it
            if (count($results) > 0) {
            	if ($bSortLinks) {
	            	// alpha sort the results
	            	sort($results["descriptions"], SORT_ASC);
	            	sort($results["links"], SORT_ASC);
					reset ($results);
            	}
                return $results;
            } else {
                return false;
            }
        } else {
            $_SESSION["errorMessage"] = "No such section name ($sSectionName) in the sitemap";
            return false;
        }
    }

    /**
     * Returns the link text for a page
     *
     * @param string the action to lookup the link text for
     * @return string the link text
     */
    function getPageLinkText($sAction) {
    	global $default;
    	//$sAction = $this->getActionFromPage($sPage);
    	$default->log->info("getPageLinkText action=$sAction");
        // for each section
        foreach ($this->aSiteMap as $section => $valArr) {
            // for each group, page array combination
            foreach ($valArr as $requiredAccess => $pageArr) {
                // now loop through pages until we find the right one
                foreach ($pageArr as $ackshin => $page) {
                    if ($ackshin == $sAction) {
                    	$default->log->info("loop:$ackshin; test:$sAction");
                    	$default->log->info("SiteMap:: getPageLinkText calling hasPermission with access:  " . $requiredAccess);
                        if ($this->hasPermission($requiredAccess)) {
                            return $page["description"];
                        }
                    }
                }
            }
        }
    }

    /**
     * Returns the page mapped to the (action, groupName) pair.
     * Checks whether to use the db or not and calls the appropriate method
     *
     * @param string the action to lookup pages for
     * @return string the page to redirect to, or false if the user doesn't have access to the page
     */
    function getPage($action) {
        if ($this->bUseDB) {
            return $this->getPageDB($action);
        } else {
            return $this->getPageArray($action);
        }
    }

    /**
     * Returns the page mapped to the (action, groupName) pair. (uses the db)
     *
     * @param string the action to lookup pages for
     * @return string the page to redirect to, or false if the user doesn't have access to the page
     */
    function getPageDB($action) {
        global $default, $lang_err_database;
        $sql = $default->db;
        // lookup the page and access_id from the sitemap
        if ($sql->query(array("SELECT page, access_id FROM $default->sitemap_table WHERE action = ?", $action))) {/*ok*/
            if ($sql->next_record()) {
                // check permissions

⌨️ 快捷键说明

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