<?php
// $Id: pnadmin.php,v 1.23.2.1 2006/01/12 12:50:29 markwest Exp $
// ----------------------------------------------------------------------
// PostNuke Content Management System
// Copyright (C) 2002 by the PostNuke Development Team.
// http://www.postnuke.com/
// ----------------------------------------------------------------------
// Based on:
// PHP-NUKE Web Portal System - http://phpnuke.org/
// Thatware - http://thatware.org/
// ----------------------------------------------------------------------
// LICENSE
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// 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.
//
// To read the license please visit http://www.gnu.org/copyleft/gpl.html
// ----------------------------------------------------------------------
// Original Author of file: Brian Bain
// Purpose of file:  Censor administration display functions
// ----------------------------------------------------------------------

/**
 * @package PostNuke_System_Modules
 * @subpackage Censor
 * @license http://www.gnu.org/copyleft/gpl.html
*/
/**
 * Censor Module main admin function
 * @author Brian Bain
 * @return string HTML output string
 */
function Censor_admin_main()
{
    // Security check - important to do this as early as possible to avoid
    // potential security holes or just too much wasted processing.  For the
    // main function we want to check that the user has at least edit privilege
    // for some item within this component, or else they won't be able to do
    // anything and so we refuse access altogether.  The lowest level of access
    // for administration depends on the particular module, but it is generally
    // either 'edit' or 'delete'
    if (!pnSecAuthAction(0, 'Censor::', '::', ACCESS_EDIT)) {
        return pnVarPrepHTMLDisplay(_MODULENOAUTH);
    }

    // Create output object - this object will store all of our output so that
    // we can return it easily when required
	$pnRender =& new pnRender('Censor');

	// As Admin output changes often, we do not want caching.
	$pnRender->caching = false;

    // Add menu to output - it helps if all of the module pages have a standard
    // menu at their head to aid in navigation
    $pnRender->assign('main', Censor_admin_modify(array()));

    // Return the output that has been generated by this function
    return $pnRender->fetch('censor_admin_main.htm');

}

/**
 * Modify censor configuration
 * @author Brian Bain
 * @param int censormode censor on or off
 * @param string censorlist list of censored words
 * @return string HTML output string
 */
function Censor_admin_modify($args)
{
    // Security check - important to do this as early as possible to avoid
    // potential security holes or just too much wasted processing
    if (!pnSecAuthAction(0, 'Censor::', ":CensorList:", ACCESS_ADMIN)) {
        return pnVarPrepHTMLDisplay(_MODULENOAUTH);
    }

    // Get parameters from whatever input we need.  All arguments to this
    // function should be obtained from pnVarCleanFromInput(), getting them
    // from other places such as the environment is not allowed, as that makes
    // assumptions that will not hold in future versions of PostNuke
    list($censormode,
         $censorlist)= pnVarCleanFromInput('censormode',
                                           'censorlist');

    // Admin functions of this type can be called by other modules.  If this
    // happens then the calling module will be able to pass in arguments to
    // this function through the $args parameter.  Hence we extract these
    // arguments *after* we have obtained any form-based input through
    extract($args);

    // Create output object - this object will store all of our output so that
    // we can return it easily when required
	$pnRender =& new pnRender('Censor');

	// As Admin output changes often, we do not want caching.
	$pnRender->caching = false;

    // censor mode
    $pnRender->assign('censormode', pnConfigGetVar('CensorMode'));

	// censor list
    $pnRender->assign('censorlist', implode("\n",pnConfigGetVar('CensorList')));

	// censor replace
    $pnRender->assign('censorreplace', pnConfigGetVar('CensorReplace'));

    // Return the output that has been generated by this function
    return $pnRender->fetch('censor_admin_modify.htm');

}

/**
 * Update censor configuration
 * @author Brian Bain
 * @param int censormode censor on or off
 * @param string censorlist list of censored words
 * @param censorreplace string to replace censored word with
 * @return bool true if successful, false otherwise
 */
function Censor_admin_update($args)
{
    // Security check - important to do this as early as possible to avoid
    // potential security holes or just too much wasted processing
    if (!pnSecAuthAction(0, 'Censor::', ":CensorList:", ACCESS_ADMIN)) {
        return pnVarPrepHTMLDisplay(_MODULENOAUTH);
    }

    list($censormode,
         $censorlist,
         $censorreplace) = pnVarCleanFromInput('censormode',
                                              'censorlist',
                                              'censorreplace'
                                                           );
    extract($args);
    
    if(empty($censormode)){
        $censormode = 0;
    }else{
        $censormode = 1;
    }
    
    if (!pnSecConfirmAuthKey()) {
        pnSessionSetVar('errormsg', _BADAUTHKEY);
        pnRedirect(pnModURL('Censor', 'admin', 'main'));
        return true;
    }

    $censorlist = preg_split ("/[\s,]+/", $censorlist);
    if(pnModAPIFunc('Censor',
                    'admin',
                    'update',
                    array(
                          'censormode' => $censormode,
                          'censorlist' => $censorlist,
                          'censorreplace' => $censorreplace
                                                      ))) {

        pnSessionSetVar('statusmsg', _CENSORUPDATED);
    }

	// the module configuration has been updated successfuly
	pnSessionSetVar('statusmsg', _CONFIGUPDATED);

    // Let any other modules know that the modules configuration has been updated
    pnModCallHooks('module','updateconfig','Censor', array('module' => 'Censor'));

    pnRedirect(pnModURL('Censor', 'admin', 'main'));

    return true;
}

?>