<?php
// $Id: pnadmin.php,v 1.16 2005/01/10 15:29:57 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: Mark West
// Purpose of file:  RSS administration display functions
// ----------------------------------------------------------------------

/**
 * @package PostNuke_Content_Modules
 * @subpackage RSS
 * @license http://www.gnu.org/copyleft/gpl.html
*/

/**
 * the main administration function
 * This function is the default function, and is called whenever the
 * module is initiated without defining arguments.  As such it can
 * be used for a url of things, but most commonly it either just
 * shows the module menu and returns or calls whatever the module
 * designer feels should be the default function (often this is the
 * view() function)
 */
function RSS_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, 'RSS::Item', '::', 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('RSS');

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

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

/**
 * add new item
 * This is a standard function that is called whenever an administrator
 * wishes to create a new module item
 */
function RSS_admin_new()
{
    // Security check - important to do this as early as possible to avoid
    // potential security holes or just too much wasted processing
    if (!pnSecAuthAction(0, 'RSS::Item', '::', ACCESS_ADD)) {
        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('RSS');

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

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

/**
 * This is a standard function that is called with the results of the
 * form supplied by RSS_admin_new() to create a new item
 * @param 'feedname' the name of the item to be created
 * @param 'url' the url of the item to be created
 */
function RSS_admin_create($args)
{
    // 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($feedname,
         $url) = pnVarCleanFromInput('feedname',
                                     'url');

    // 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
    // pnVarCleanFromInput().
    extract($args);

    // Confirm authorisation code.  This checks that the form had a valid
    // authorisation code attached to it.  If it did not then the function will
    // proceed no further as it is possible that this is an attempt at sending
    // in false data to the system
    if (!pnSecConfirmAuthKey()) {
        pnSessionSetVar('errormsg', _BADAUTHKEY);
        pnRedirect(pnModURL('RSS', 'admin', 'view'));
        return true;
    }

    // Notable by its absence there is no security check here.  This is because
    // the security check is carried out within the API function and as such we
    // do not duplicate the work here

    // The API function is called.  Note that the name of the API function and
    // the name of this function are identical, this helps a lot when
    // programming more complex modules.  The arguments to the function are
    // passed in as their own arguments array
    $fid = pnModAPIFunc('RSS',
                        'admin',
                        'create',
                        array('feedname' => $feedname,
                              'url' => $url));

    // The return value of the function is checked here, and if the function
    // suceeded then an appropriate message is posted.  Note that if the
    // function did not succeed then the API function should have already
    // posted a failure message so no action is required
    if ($fid != false) {
        // Success
        pnSessionSetVar('statusmsg', _RSSCREATED);
    }

    // This function generated no output, and so now it is complete we redirect
    // the user to an appropriate page for them to carry on their work
    pnRedirect(pnModURL('RSS', 'admin', 'view'));

    // Return
    return true;
}

/**
 * modify an item
 * This is a standard function that is called whenever an administrator
 * wishes to modify a current module item
 * @param 'fid' the id of the item to be modified
 */
function RSS_admin_modify($args)
{
    // 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($fid,
         $objecfid)= pnVarCleanFromInput('fid',
                                         'objecfid');

    // 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
    // pnVarCleanFromInput().
    extract($args);

    // At this stage we check to see if we have been passed $objecfid, the
    // generic item identifier.  This could have been passed in by a hook or
    // through some other function calling this as part of a larger module, but
    // if it exists it overrides $fid
    //
    // Note that this module couuld just use $objecfid everywhere to avoid all
    // of this munging of variables, but then the resultant code is less
    // descriptive, especially where multiple objects are being used.  The
    // decision of which of these ways to go is up to the module developer
    if (!empty($objecfid)) {
        $fid = $objecfid;
    }                       

    // The user API function is called.  This takes the item ID which we
    // obtained from the input and gets us the information on the appropriate
    // item.  If the item does not exist we post an appropriate message and
    // return
    $item = pnModAPIFunc('RSS',
                         'user',
                         'get',
                         array('fid' => $fid));

    if ($item == false) {
        return pnVarPrepHTMLDisplay(_RSSNOSUCHITEM);
    }

    // Security check - important to do this as early as possible to avoid
    // potential security holes or just too much wasted processing.  However,
    // in this case we had to wait until we could obtain the item name to
    // complete the instance information so this is the first chance we get to
    // do the check
    if (!pnSecAuthAction(0, 'RSS::Item', "$item[feedname]::$fid", 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('RSS');

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

    // Add a hidden variable for the item id.  This needs to be passed on to
    // the update function so that it knows which item for which item to carry
    // out the update
    $pnRender->assign('fid', $fid);

    // Name
    $pnRender->assign('feedname', $item['feedname']);

    // url
    $pnRender->assign('url', $item['url']);

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

/**
 * This is a standard function that is called with the results of the
 * form supplied by RSS_admin_modify() to update a current item
 * @param 'fid' the id of the item to be updated
 * @param 'feedname' the name of the item to be updated
 * @param 'url' the url of the item to be updated
 */
function RSS_admin_update($args)
{
    // 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($fid,
         $objectid,
         $feedname,
         $url) = pnVarCleanFromInput('fid',
                                     'objectid',
                                     'feedname',
                                     'url');

    // User 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
    // pnVarCleanFromInput().
    extract($args);
                            
    // At this stage we check to see if we have been passed $objecfid, the
    // generic item identifier.  This could have been passed in by a hook or
    // through some other function calling this as part of a larger module, but
    // if it exists it overrides $fid
    //
    // Note that this module couuld just use $objecfid everywhere to avoid all
    // of this munging of variables, but then the resultant code is less
    // descriptive, especially where multiple objects are being used.  The
    // decision of which of these ways to go is up to the module developer
    if (!empty($objectid)) {
        $fid = $objectid;
    }                       

    // Confirm authorisation code.  This checks that the form had a valid
    // authorisation code attached to it.  If it did not then the function will
    // proceed no further as it is possible that this is an attempt at sending
    // in false data to the system
    if (!pnSecConfirmAuthKey()) {
        pnSessionSetVar('errormsg', _BADAUTHKEY);
        pnRedirect(pnModURL('RSS', 'admin', 'view'));
        return true;
    }

    // Notable by its absence there is no security check here.  This is because
    // the security check is carried out within the API function and as such we
    // do not duplicate the work here

    // The API function is called.  Note that the name of the API function and
    // the name of this function are identical, this helps a lot when
    // programming more complex modules.  The arguments to the function are
    // passed in as their own arguments array.
    //
    // The return value of the function is checked here, and if the function
    // suceeded then an appropriate message is posted.  Note that if the
    // function did not succeed then the API function should have already
    // posted a failure message so no action is required
    if(pnModAPIFunc('RSS',
                    'admin',
                    'update',
                    array('fid' => $fid,
                          'feedname' => $feedname,
                          'url' => $url))) {
        // Success
        pnSessionSetVar('statusmsg', _RSSUPDATED);
    }

    // This function generated no output, and so now it is complete we redirect
    // the user to an appropriate page for them to carry on their work
    pnRedirect(pnModURL('RSS', 'admin', 'view'));

    // Return
    return true;
}

/**
 * delete item
 * This is a standard function that is called whenever an administrator
 * wishes to delete a current module item.  Note that this function is
 * the equivalent of both of the modify() and update() functions above as
 * it both creates a form and processes its output.  This is fine for
 * simpler functions, but for more complex operations such as creation and
 * modification it is generally easier to separate them into separate
 * functions.  There is no requirement in the PostNuke MDG to do one or the
 * other, so either or both can be used as seen appropriate by the module
 * developer
 * @param 'fid' the id of the item to be deleted
 * @param 'confirmation' confirmation that this item can be deleted
 */
function RSS_admin_delete($args)
{
    // 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($fid,
         $objecfid,
         $confirmation) = pnVarCleanFromInput('fid',
                                              'objecfid',
                                              'confirmation');


    // User 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
    // pnVarCleanFromInput().
    extract($args);

     // At this stage we check to see if we have been passed $objecfid, the
     // generic item identifier.  This could have been passed in by a hook or
     // through some other function calling this as part of a larger module, but
     // if it exists it overrides $fid
     //
     // Note that this module couuld just use $objecfid everywhere to avoid all
     // of this munging of variables, but then the resultant code is less
     // descriptive, especially where multiple objects are being used.  The
     // decision of which of these ways to go is up to the module developer
     if (!empty($objecfid)) {
         $fid = $objecfid;
     }                     

    // The user API function is called.  This takes the item ID which we
    // obtained from the input and gets us the information on the appropriate
    // item.  If the item does not exist we post an appropriate message and
    // return
    $item = pnModAPIFunc('RSS',
                         'user',
                         'get',
                         array('fid' => $fid));

    if ($item == false) {
        return pnVarPrepHTMLDisplay(_RSSNOSUCHITEM);
    }

    // Security check - important to do this as early as possible to avoid
    // potential security holes or just too much wasted processing.  However,
    // in this case we had to wait until we could obtain the item name to
    // complete the instance information so this is the first chance we get to
    // do the check
    if (!pnSecAuthAction(0, 'RSS::Item', "$item[name]::$fid", ACCESS_DELETE)) {
        return pnVarPrepHTMLDisplay(_MODULENOAUTH);
    }

    // Check for confirmation. 
    if (empty($confirmation)) {
        // No confirmation yet - display a suitable form to obtain confirmation
        // of this action from the user

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

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

        // Add a hidden variable for the item id.  This needs to be passed on to
        // the update function so that it knows which item for which item to carry
        // out the update
        $pnRender->assign('fid', $fid);

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

    // If we get here it means that the user has confirmed the action

    // Confirm authorisation code.  This checks that the form had a valid
    // authorisation code attached to it.  If it did not then the function will
    // proceed no further as it is possible that this is an attempt at sending
    // in false data to the system
    if (!pnSecConfirmAuthKey()) {
        pnSessionSetVar('errormsg', _BADAUTHKEY);
        pnRedirect(pnModURL('RSS', 'admin', 'view'));
        return true;
    }

    // The API function is called.  Note that the name of the API function and
    // the name of this function are identical, this helps a lot when
    // programming more complex modules.  The arguments to the function are
    // passed in as their own arguments array.
    //
    // The return value of the function is checked here, and if the function
    // suceeded then an appropriate message is posted.  Note that if the
    // function did not succeed then the API function should have already
    // posted a failure message so no action is required
    if (pnModAPIFunc('RSS',
                     'admin',
                     'delete',
                     array('fid' => $fid))) {
        // Success
        pnSessionSetVar('statusmsg', _RSSDELETED);
    }

    // This function generated no output, and so now it is complete we redirect
    // the user to an appropriate page for them to carry on their work
    pnRedirect(pnModURL('RSS', 'admin', 'view'));
    
    // Return
    return true;
}

/**
 * view items
 */
function RSS_admin_view()
{
    // 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
    $startnum = pnVarCleanFromInput('startnum');

    // Security check - important to do this as early as possible to avoid
    // potential security holes or just too much wasted processing.  However,
    // in this case we had to wait until we could obtain the item name to
    // complete the instance information so this is the first chance we get to
    // do the check
    if (!pnSecAuthAction(0, 'RSS::', '::', ACCESS_EDIT)) {
        return pnVarPrepHTMLDisplay(_MODULENOAUTH);
    }

    // The user API function is called.  This takes the url of items
    // required and the first url in the list of all items, which we
    // obtained from the input and gets us the information on the appropriate
    // items.
    $items = pnModAPIFunc('RSS',
                          'user',
                          'getall',
                          array('startnum' => $startnum,
                                'numitems' => pnModGetVar('RSS',
                                                          'itemsperpage')));

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

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

    $rssitems = array();
    foreach ($items as $item) {

        if (pnSecAuthAction(0, 'RSS::', "$item[feedname]::$item[fid]", ACCESS_READ)) {
    
            // Options for the item.  Note that each item has the appropriate
            // levels of authentication checked to ensure that it is suitable
            // for display

            $options = array();
            if (pnSecAuthAction(0, 'RSS::', "$item[feedname]::$item[fid]", ACCESS_EDIT)) {
                $options[] = array('url' => pnModURL('RSS', 'admin', 'modify', array('fid' => $item['fid'])),
                                   'title' => _EDIT);
                if (pnSecAuthAction(0, 'RSS::', "$item[feedname]::$item[fid]", ACCESS_DELETE)) {
                    $options[] = array('url' => pnModURL('RSS', 'admin', 'delete', array('fid' => $item['fid'])),
                                       'title' => _DELETE);
                }
            }

            // Name and url.  Note that unlike the user function we do not
            // censor the text that is being displayed.  This is so the
            // administrator can see the text as exists in the database rather
            // than the munged output version
            $rssitems[] = array('name' => $item['feedname'],
			                    'url' => $item['url'],
								'options' => $options);
        }
    }
	$pnRender->assign('rssitems', $rssitems);

    // Assign the values for the smarty plugin to produce a pager in case of there
    // being many items to display.
    //
    // Note that this function includes another user API function.  The
    // function returns a simple count of the total number of items in the item
    // table so that the pager function can do its job properly
	$pnRender->assign('pager', array('numitems' => pnModAPIFunc('RSS', 'user', 'countitems'),
	                           'itemsperpage' => pnModGetVar('RSS', 'itemsperpage')));

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


/**
 * This is a standard function to modify the configuration parameters of the
 * module
 */
function RSS_admin_modifyconfig()
{
    // Security check - important to do this as early as possible to avoid
    // potential security holes or just too much wasted processing
    if (!pnSecAuthAction(0, 'RSS::', '::', ACCESS_ADMIN)) {
        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('RSS');

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

    // Bold
    $pnRender->assign('bold', pnModGetVar('RSS', 'bold'));

    // open items in a new browser
    $pnRender->assign('openinnewwindow', pnModGetVar('RSS', 'openinnewwindow'));

    // number of items to display per page
    $pnRender->assign('itemsperpage', pnModGetVar('RSS', 'itemsperpage'));

    // cache directory for the parser
    $pnRender->assign('cachedirectory', pnModGetVar('RSS', 'cachedirectory'));

    // cache interval for the parser
    $pnRender->assign('cacheinterval', pnModGetVar('RSS', 'cacheinterval'));
    
    // Return the output that has been generated by this function
    return $pnRender->fetch('rss_admin_modifyconfig.htm');
}

/**
 * This is a standard function to update the configuration parameters of the
 * module given the information passed back by the modification form
 */
function RSS_admin_updateconfig()
{
    // 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($bold, 
         $openinnewwindow,
	     $itemsperpage,
		 $cacheinterval,
		 $cachedirectory) = pnVarCleanFromInput('bold',
                                                'openinnewwindow', 
		                                        'itemsperpage',
							                    'cacheinterval',
												'cachedirectory');

    // Confirm authorisation code.  This checks that the form had a valid
    // authorisation code attached to it.  If it did not then the function will
    // proceed no further as it is possible that this is an attempt at sending
    // in false data to the system
    if (!pnSecConfirmAuthKey()) {
        pnSessionSetVar('errormsg', _BADAUTHKEY);
        pnRedirect(pnModURL('RSS', 'admin', 'view'));
        return true;
    }

    // Update module variables.  Note that depending on the HTML structure used
    // to obtain the information from the user it is possible that the values
    // might be unset, so it is important to check them all and assign them
    // default values if required
    if (empty($bold)) {
        $bold = 0;
    }
    pnModSetVar('RSS', 'bold', $bold);

    if (empty($openinnewwindow)) {
        $openinnewwindow = 0;
    }
    pnModSetVar('RSS', 'openinnewwindow', $openinnewwindow);

    if (empty($itemsperpage)) {
        $itemsperpage = 10;
    }
    pnModSetVar('RSS', 'itemsperpage', $itemsperpage);

    if (empty($cacheinterval)) {
        $cacheinterval = 180;
    }
    pnModSetVar('RSS', 'cacheinterval', $cacheinterval);

    if (empty($cachedirectory)) {
        $cachedirectory = 'onyxrssparser';
    }
    pnModSetVar('RSS', 'cachedirectory', $cachedirectory);

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

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

    // This function generated no output, and so now it is complete we redirect
    // the user to an appropriate page for them to carry on their work
    pnRedirect(pnModURL('RSS', 'admin', 'view'));

    // Return
    return true;
}

?>