<?php
// $Id: pnadmin.php,v 1.23 2005/01/10 15:02:58 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:  Mailer administration display functions
// ----------------------------------------------------------------------

/**
 * @package PostNuke_System_Modules
 * @subpackage Mailer
 * @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 number 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)
 * @author Mark West
 * @return string HTML string
 */
function Mailer_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, 'Mailer::', '::', 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('Mailer');

	// 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('mailer_admin_main.htm');
}

/**
 * This is a standard function to modify the configuration parameters of the
 * module
 * @author Mark West
 * @return string HTML string
 */
function Mailer_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, 'Mailer::', '::', 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('Mailer');

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

    // Default mailer type
    $row = array();
    $pnRender->assign('mailertypes', array(1 => pnVarPrepForDisplay(_MAILERPHPMAIL),
                                     2 => pnVarPrepForDisplay(_MAILERSENDMAIL),
                                     3 => pnVarPrepForDisplay(_MAILERQMAIL),
                                     4 => pnVarPrepForDisplay(_MAILERSMTPMAIL)));
    $pnRender->assign('mailertype', pnModGetVar('Mailer', 'mailertype'));

    // default charset
    $pnRender->assign('charset', pnModGetVar('Mailer', 'charset'));

    // default encoding
    $pnRender->assign('encoding', pnModGetVar('Mailer', 'encoding'));

    // default content type
    $pnRender->assign('contenttype', pnModGetVar('Mailer', 'contenttype'));

    // wordwarap
    $pnRender->assign('wordwrap', pnModGetVar('Mailer', 'wordwrap'));

    // MS Mail header
    $pnRender->assign('msmailheaders', pnModGetVar('Mailer', 'msmailheaders'));

    // sendmail path
    $pnRender->assign('sendmailpath', pnModGetVar('Mailer', 'sendmailpath'));

    // SMTP Server
    $pnRender->assign('smtpserver', pnModGetVar('Mailer', 'smtpserver'));

    // SMTP port
    $pnRender->assign('smtpport', pnModGetVar('Mailer', 'smtpport'));

    // SMTP timeout
    $pnRender->assign('smtptimeout', pnModGetVar('Mailer', 'smtptimeout'));

    // SMTP authentication
    $pnRender->assign('smtpauth', pnModGetVar('Mailer', 'smtpauth'));

    // SMTP username
    $pnRender->assign('smtpusername', pnModGetVar('Mailer', 'smtpusername'));

    // SMTP Password
    $pnRender->assign('smtppassword', pnModGetVar('Mailer', 'smtppassword'));

    // Return the output that has been generated by this function
    return $pnRender->fetch('mailer_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
 * @author Mark West
 * @see Mailer_admin_updateconfig()
 * @param int mailertype Mail transport agent
 * @param string charset default character set of the message
 * @param string encoding default encoding
 * @param string contenttype default mime type for content
 * @param int wordwrap word wrap column
 * @param int msmailheaders include MS mail headers
 * @param string sendmailpath path to sendmail
 * @param int smtpauth enable SMTPAuth
 * @param string smtpserver ip address of SMTP server
 * @param int smtpport port number of SMTP server
 * @param int smtptimeout SMTP timeout
 * @param string smtpusername SMTP username
 * @param string smtppassword SMTP password
 * @return bool true if update successful
 */
function Mailer_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
    $mailertype = pnVarCleanFromInput('mailertype');
    $charset = pnVarCleanFromInput('charset');
    $encoding = pnVarCleanFromInput('encoding');
    $contenttype = pnVarCleanFromInput('contenttype');
    $wordwrap = pnVarCleanFromInput('wordwrap');
    $msmailheaders = pnVarCleanFromInput('msmailheaders');
    $sendmailpath = pnVarCleanFromInput('sendmailpath');
    $smtpauth = pnVarCleanFromInput('smtpauth');
    $smtpserver = pnVarCleanFromInput('smtpserver');
    $smtpport = pnVarCleanFromInput('smtpport');
    $smtptimeout = pnVarCleanFromInput('smtptimeout');
    $smtpusername = pnVarCleanFromInput('smtpusername');
    $smtppassword = pnVarCleanFromInput('smtppassword');

    // 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('Mailer', 'admin', 'main'));
        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($mailertype)) {
        $mailertype = 1;
    }
    pnModSetVar('Mailer', 'mailertype', $mailertype);

    if (empty($charset)) {
        $charset = 'iso-8859-1';
    }
    pnModSetVar('Mailer', 'charset', $charset);

    if (empty($encoding)) {
        $encoding = '8bit';
    }
    pnModSetVar('Mailer', 'encoding', $encoding);

    if (empty($contenttype)) {
        $contenttype = 'text/plain';
    }
    pnModSetVar('Mailer', 'contenttype', $contenttype);

    if (empty($wordwrap)) {
        $wordwrap = 50;
    }
    pnModSetVar('Mailer', 'wordwrap', $wordwrap);

    if (empty($msmailheaders)) {
        $msmailheaders = false;
    }
    pnModSetVar('Mailer', 'msmailheaders', $msmailheaders);

    if (empty($sendmailpath)) {
        $sendmailpath = '/usr/sbin/sendmail';
    }
    pnModSetVar('Mailer', 'sendmailpath', $sendmailpath);

    if (empty($smtpauth)) {
        $smtpauth = 0;
    }
    pnModSetVar('Mailer', 'smtpauth', $smtpauth);

    if (empty($smtpserver)) {
        $smtpserver = 'localhost';
    }
    pnModSetVar('Mailer', 'smtpserver', $smtpserver);

    if (empty($smtpport)) {
        $smtpport = 25;
    }
    pnModSetVar('Mailer', 'smtpport', $smtpport);

    if (!isset($smtptimeout)) {
        $smtptimeout = 10;
    }
    pnModSetVar('Mailer', 'smtptimeout', $smtptimeout);

    if (empty($smtpusername)) {
        $smtpusername = '';
    }
    pnModSetVar('Mailer', 'smtpusername', $smtpusername);

    if (empty($smtppassword)) {
        $smtppassword = '';
    }
    pnModSetVar('Mailer', 'smtppassword', $smtppassword);

	// 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('Mailer', 'admin', 'main'));

    // Return
    return true;
}

/**
 * This function displays a form to sent a test mail
 * @author Mark West
 * @return string HTML string
 */
function Mailer_admin_testconfig()
{
    // Security check - important to do this as early as possible to avoid
    // potential security holes or just too much wasted processing
    if (!pnSecAuthAction(0, 'Mailer::', '::', 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('Mailer');

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

    // from name
    $pnRender->assign('fromname', pnConfigGetVar('sitename'));

    // from address
	$pnRender->assign('fromaddress', pnConfigGetVar('adminmail'));

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

}

/**
 * This function processes the results of the test form
 * @author Mark West
 * @param string args['toname '] name to the recipient
 * @param string args['toaddress'] the address of the recipient
 * @param string args['subject'] message subject
 * @param string args['body'] message body
 * @param int args['html'] HTML flag
 * @return bool true
 */
function Mailer_admin_sendmessage($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($toname,
         $toaddress,
		 $subject,
         $body,
		 $pnmail,
		 $html) = pnVarCleanFromInput('toname',
                                      'toaddress',
									  'subject',
                                      'body',
									  'pnmail',
									  'html');

    // 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('Mailer', 'admin', 'main'));
        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
	if ($pnmail) {
	    $from = pnConfigGetVar('adminmail');
	    $result = pnMail($toaddress, $subject, $body, "From: $from\nX-Mailer: PHP/" . phpversion(), $html);
	} else {
        $result = pnModAPIFunc('Mailer',
                               'user',
                               'sendmessage',
                               array('toname' => $toname,
				    				 'toaddress' => $toaddress,
					     			 'subject' => $subject,
                                     'body' => $body,
                                     'html' => $html));
    }

    // 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 ($result === true) {
        // Success
        pnSessionSetVar('statusmsg', _MAILERMESSAGESENT);
    } elseif ($result === false) {
        // Failiure
        pnSessionSetVar('errormsg', _MAILERMESSAGENOTSENT);
	} else {
        // Failiure with error
        pnSessionSetVar('errormsg', _MAILERMESSAGENOTSENT . ' - ' . $result);
	}

    // 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('Mailer', 'admin', 'main'));

    // Return
    return true;

}

?>