<?php
// File: $Id: admin.php,v 1.3 2002/10/31 19:55:05 tanis Exp $ $Name:  $
// ----------------------------------------------------------------------
// Banner Modules 1.0

// Copyright (C) 2003 by the Domenico Pasquini(webmaster@vivituscia.com)

// http://www.vivituscia.com/

// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
// POST-NUKE Content Management System
// Copyright (C) 2001 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:
// Purpose of file:
// ----------------------------------------------------------------------

if ( !eregi( "admin.php", $PHP_SELF ) )
{
  die ("Access Denied");
}

$ModName = $module;
modules_get_language();
modules_get_manual();


function getRegionString( $regionid )
{
  $retstr = "Unknown";
  switch ( $regionid )
  {
  case 0:
    $retstr = "Everywhere";
    break;
  case 1:
    $retstr = "Denver Metro/Boulder";
    break;
  case 2:
    $retstr = "Northern Colorado";
    break;
  }
  return $retstr;
}



/**
 * Banners Administration Functions
 */

function BannersAdmin()
{
  include 'header.php';
  $bgcolor2 = $GLOBALS['bgcolor2'];
  list($clientname) = pnVarCleanFromInput('clientname');

  list($dbconn) = pnDBGetConn();
  $pntable = pnDBGetTables();

  GraphicAdmin();
  OpenTable();
  echo "<center><font class=\"pn-title\"><b>"._BANNERSADMIN."</b></font></center>";
  CloseTable();

  /* Add Banner */
  if ( pnSecAuthAction(0, 'Banners::Banner', '::', ACCESS_ADD) )
  {
    $column = $pntable['bannerclient_column'];
    $result = $dbconn->Execute("SELECT $column[cid], $column[name]
                                FROM $pntable[bannerclient]");
    if ( !$result->EOF )
    {
      OpenTable();
      echo "<font class=\"pn-title\"><b>"._ADDNEWBANNER."</b></font><br><br>"
          ."<b>0</b> &nbsp;Don't Show<br>"
          ."<b>1</b> &nbsp;Top Banner<br>"
          ."<b>3</b> &nbsp;Left Side<br>"
          ."<b>4</b> &nbsp;Right Side - animated<br>"
          ."<b>5</b> &nbsp;Right Side - static<br>"
          ."<b>6</b> &nbsp;Calendar (790 x 150)<br>"
          ."<b>7</b> &nbsp;Resource Listing (610 x 150)<br>"
          ."<br>"
          ."<form action=\"admin.php\" method=\"post\">";

      // todo: hook this up
      echo 'Region:&nbsp;&nbsp;';
      echo '<select name="event_region">';
      echo '  <option value="0" selected>Everywhere</option>';
      echo '  <option value="1" >Denver Metro/Boulder</option>';
      echo '  <option value="2" >Northern Colorado</option>';
      echo '</select>';
      echo '<br />';

      echo ""._BANTYPE.": <input type=\"text\" name=\"type\" size=\"2\" maxlength=\"2\" value=\"5\"><b> ATTENTION !! : if you use Banner Modules insert type=0 for standard banners </b><br>";
      echo ""._IMAGEURL.": <input type=\"text\" name=\"imageurl\" size=\"50\" maxlength=\"250\" value=\"/images/banners/\"><br>";
      echo ""._CLICKURL.": <input type=\"text\" name=\"clickurl\" size=\"50\" maxlength=\"250\"><br>";
      echo "<br /><br />";

      echo ""._CLIENTNAME . ":&nbsp;&nbsp;"
           ."<select name=\"cid\">";
      while ( list($cid, $name) = $result->fields )
      {
        echo "<option value=\"$cid\">". pnVarPrepForDisplay($name) . "</option>";
        $result->MoveNext();
      }
      echo "</select><br>";
      echo ""._PURCHASEDIMPRESSIONS.": <input type=\"text\" name=\"imptotal\" size=\"12\" maxlength=\"11\"> 0 = "._UNLIMITED."<br>";
      echo '<br />';

      echo " FOR BANNER IN FLASH : Width <input type=\"text\" name=\"width\" size=\"12\" maxlength=\"4\"> Height <input type=\"text\" name=\"height\" size=\"12\" maxlength=\"4\"><br><br>";
      echo ""._MODULE." :";
      echo "<select name=\"title\">";

      echo "<option value=\"ALL\">"._ALL."</option>";

      list($dbconn) = pnDBGetConn();
      $pntable = pnDBGetTables();
      $result = $dbconn->Execute("SELECT pn_name
                                  FROM $pntable[modules] ORDER BY pn_name ASC");

      while ( list($title) = $result->fields )
      {
        echo "<option value=\"$title\">$title</option>";
        $result->MoveNext();
      }

      echo "</select><br><br>"
          ."<input type=\"hidden\" name=\"module\" value=\"".$GLOBALS['module']."\">"
          ."<input type=\"hidden\" name=\"op\" value=\"BannersAdd\">"
          ."<input type=\"hidden\" name=\"authid\" value=\"" . pnSecGenAuthKey() . "\">"
          ."<input type=\"submit\" value=\""._ADDBANNER."\">"
          ."</form>";
      CloseTable();
    }
  }

  /* Check if Banners variable is active, if not then print a message */
  if (pnConfigGetVar('banners') == 0)
  {
    OpenTable();
    echo "<center><br><i><b>"._IMPORTANTNOTE."</b></i><br><br>"
        .""._BANNERSNOTACTIVE."<br>"
        .""._TOACTIVATE."<br><br></center>";
    CloseTable();
  }

  // Banners List
  echo "<a name=\"top\"></a>";
  if (pnSecAuthAction(0, 'Banners::Banner', '::', ACCESS_READ))
  {
    OpenTable();
    echo "<center><font class=\"pn-title\"><b>"._ACTIVEBANNERS."</b></font></center><br>"
        ."<table width=100% border=0>"
        ."<tr>"
        ."<td bgcolor=\"$bgcolor2\" align=\"center\"><b>" . _ID            . "</b></td>"
        ."<td bgcolor=\"$bgcolor2\" align=\"center\"><b>" . _CLIENTNAME    . "</b></td>"
        ."<td bgcolor=\"$bgcolor2\" align=\"center\"><b>" . _IMPRESSIONS   . "</b></td>"
        ."<td bgcolor=\"$bgcolor2\" align=\"center\"><b>" . _IMPLEFT       . "</b></td>"
        ."<td bgcolor=\"$bgcolor2\" align=\"center\"><b>" . _CLICKS        . "</b></td>"
        ."<td bgcolor=\"$bgcolor2\" align=\"center\"><b>" . _CLICKSPERCENT . "</b></td>"
        ."<td bgcolor=\"$bgcolor2\" align=\"center\"><b>" . _TYPE          . "</b></td>"
        ."<td bgcolor=\"$bgcolor2\" align=\"center\"><b>" . "Region"       . "</b></td>"
        ."<td bgcolor=\"$bgcolor2\" align=\"center\"><b>" . _MODULE        . "</b></td>"
        ."<td bgcolor=\"$bgcolor2\" align=\"center\"><b>Image URL</b></td>"
        ."<td bgcolor=\"$bgcolor2\" align=\"center\"><b>"._FUNCTIONS."</b></td>"
        ."</tr>";

    $column = $pntable['banner_column'];
    $column2 = $pntable['bannerclient_column'];
    $result = $dbconn->Execute( "SELECT pn_bid, pn_cid, pn_imptotal, pn_impmade, pn_clicks, pn_date, pn_type, pn_title, pn_imageurl, pn_regionid
                                 FROM $pntable[banner]
                                 ORDER BY $column[type] desc, pn_regionid asc, $column[imageurl] asc" );
//                                 ORDER BY $column[bid]" );

    $currtype = -1;
    $currcount = 0;
    while( list($bid, $cid, $imptotal, $impmade, $clicks, $date, $type, $title, $imgurl, $regionid) = $result->fields )
    {
      if ( $currtype != $type )
      {
        if ( $currtype != -1 )
        {
          echo '<tr><td colspan="10">&nbsp;Count: ' . $currcount . '</td></td></tr>';
          echo '<tr><td colspan="10">&nbsp;</td></td></tr>';
        }
        $currtype = $type;
        $currcount = 0;
      }
      $currcount++;

      $result->MoveNext();
      $result2 = $dbconn->Execute( "SELECT pn_cid, pn_name FROM $pntable[bannerclient] WHERE pn_cid='$cid'" );

      list($pn_cid, $clientname) = $result2->fields;

      // jgm - Get and use $clientname
      if ( !isset( $clientname ) )
      {
        $clientname = '';
      }

      if ( pnSecAuthAction(0, 'Banners::Banner', "$clientname::$bid", ACCESS_READ) )
      {
        if ( $impmade==0 )
        {
          $percent = 0;
        }
        else
        {
          $percent = substr(100 * $clicks / $impmade, 0, 5);
        }
        if ( $imptotal == 0 )
        {
          $left = _UNLIMITED;
        }
        else
        {
          $left = $imptotal-$impmade;
        }
        echo "<tr>"
          ."<td bgcolor=\"$bgcolor2\" align=center>$bid</td>"
          ."<td bgcolor=\"$bgcolor2\" align=center>$clientname</td>"
          ."<td bgcolor=\"$bgcolor2\" align=center>$impmade</td>"
          ."<td bgcolor=\"$bgcolor2\" align=center>$left</td>"
          ."<td bgcolor=\"$bgcolor2\" align=center>$clicks</td>"
          ."<td bgcolor=\"$bgcolor2\" align=center>$percent%</td>"
          ."<td bgcolor=\"$bgcolor2\" align=center>$type</td>"
          ."<td bgcolor=\"$bgcolor2\" align=center>" . getRegionString( $regionid ) . "</td>"
          ."<td bgcolor=\"$bgcolor2\" align=center>$title</td>"
          ."<td bgcolor=\"$bgcolor2\" style=\"padding-left:5px;padding-right:5px;\" align=left>$imgurl</td>";

        if (pnSecAuthAction(0, 'Banners::Banner', "$clientname::$bid", ACCESS_EDIT))
        {
          echo "<td bgcolor=\"$bgcolor2\" align=center><a href=\"admin.php?module="
               .$GLOBALS['module']."&amp;op=BannerEdit&amp;bid=$bid\">"._EDIT."</a>";

          if (pnSecAuthAction(0, 'Banners::Banner', "$clientname::$bid", ACCESS_DELETE))
          {
            echo " | <a href=\"admin.php?module=".$GLOBALS['module']
                 ."&amp;op=BannerDelete&amp;bid=$bid&amp;ok=0\">"._DELETE."</a></td>";
          }
          else
          {
            echo "</td>";
          }
        }
        else
        {
          echo "<td bgcolor=\"$bgcolor2\">&nbsp;</td>";
        }
        echo "</tr>";
      }
    }

    if ( $currtype != -1 )
    {
      echo '<tr><td colspan="10">&nbsp;Count: ' . $currcount . '</td></td></tr>';
      echo '<tr><td colspan="10">&nbsp;</td></td></tr>';
    }

    echo "</table>";
    CloseTable();
  }

  /* Finished Banners List */
  if (pnSecAuthAction(0, 'Banners::Banner', '::', ACCESS_READ))
  {
    OpenTable();
    echo "<center><font class=\"pn-title\"><b>"._FINISHEDBANNERS."</b></font></center><br>"
        ."<table width=\"100%\" border=\"0\"><tr>"
        ."<td bgcolor=\"$bgcolor2\" align=\"center\"><b>"._ID."</b></td>"
        ."<td bgcolor=\"$bgcolor2\" align=\"center\"><b>"._IMP."</b></td>"
        ."<td bgcolor=\"$bgcolor2\" align=\"center\"><b>"._CLICKS."</b></td>"
        ."<td bgcolor=\"$bgcolor2\" align=\"center\"><b>"._CLICKSPERCENT."</b></td>"
        ."<td bgcolor=\"$bgcolor2\" align=\"center\"><b>"._DATESTARTED."</b></td>"
        ."<td bgcolor=\"$bgcolor2\" align=\"center\"><b>"._DATEENDED."</b></td>"
        ."<td bgcolor=\"$bgcolor2\" align=\"center\"><b>"._CLIENTNAME."</b></td>"
        ."<td bgcolor=\"$bgcolor2\" align=\"center\"><b>"._FUNCTIONS."</b></td><tr>";
    $column = $pntable['bannerfinish_column'];
    $column2 = $pntable['bannerclient_column'];
    $result = $dbconn->Execute("SELECT $column[bid],
                                       $column[cid],
                                       $column[impressions],
                                       $column[clicks],
                                       $column[datestart],
                                       $column[dateend],
                                       $column2[name]
                                FROM $pntable[bannerfinish],
                                     $pntable[bannerclient]
                                WHERE $column[cid] = ".pnVarPrepForStore($column2['cid'])."
                                ORDER BY $column[bid]");

    while( list($bid, $cid, $impressions, $clicks, $datestart, $dateend, $name) = $result->fields )
    {
      $result->MoveNext();
      // jgm - get and use clientname
      if ( pnSecAuthAction( 0, 'Banners::Banner', "$clientname::$bid", ACCESS_READ ) )
      {
        $percent = substr(100 * $clicks / $impressions, 0, 5);
        echo "<tr>"
            ."<td bgcolor=\"$bgcolor2\" align=\"center\">" . pnVarPrepForDisplay($bid) . "</td>"
            ."<td bgcolor=\"$bgcolor2\" align=\"center\">" . pnVarPrepForDisplay($impressions) . "</td>"
            ."<td bgcolor=\"$bgcolor2\" align=\"center\">" . pnVarPrepForDisplay($clicks) . "</td>"
            ."<td bgcolor=\"$bgcolor2\" align=\"center\">" . pnVarPrepForDisplay($percent) . "%</td>"
            ."<td bgcolor=\"$bgcolor2\" align=\"center\">" . pnVarPrepForDisplay($datestart) . "</td>"
            ."<td bgcolor=\"$bgcolor2\" align=\"center\">" . pnVarPrepForDisplay($dateend) . "</td>"
            ."<td bgcolor=\"$bgcolor2\" align=\"center\">" . pnVarPrepForDisplay($name) . "</td>";

        if (pnSecAuthAction(0, 'Banners::Banner', "$clientname::$bid", ACCESS_DELETE))
        {
          echo "<td bgcolor=\"$bgcolor2\" align=\"center\"><a href=\"admin.php?module="
               .$GLOBALS['module']."&amp;op=BannerFinishDelete&amp;bid=$bid&amp;authid=" . pnSecGenAuthKey() . "\">"._DELETE."</a></td>";
        }
        else
        {
          echo "<td bgcolor=\"$bgcolor2\">&nbsp;</td>";
        }
        echo "</tr>";
      }
    }
    echo "</table>";
    CloseTable();
  }

  /* Clients List */
  if (pnSecAuthAction(0, 'Banners::Client', '::', ACCESS_READ))
  {
    OpenTable();
    echo "<center><font class=\"pn-title\"><b>"._ADVERTISINGCLIENTS."</b></font></center><br>"
        ."<table width=\"100%\" border=\"0\"><tr>"
        ."<td bgcolor=\"$bgcolor2\" align=\"center\"><b>"._ID."</b></td>"
        ."<td bgcolor=\"$bgcolor2\" align=\"center\"><b>"._CLIENTNAME."</b></td>"
        ."<td bgcolor=\"$bgcolor2\" align=\"center\"><b>"._ACTIVEBANNERS2."</b></td>"
        ."<td bgcolor=\"$bgcolor2\" align=\"center\"><b>"._CONTACTNAME."</b></td>"
        ."<td bgcolor=\"$bgcolor2\" align=\"center\"><b>"._CONTACTEMAIL."</b></td>"
        ."<td bgcolor=\"$bgcolor2\" align=\"center\"><b>"._FUNCTIONS."</b></td><tr>";
    $column = $pntable['bannerclient_column'];
    $result = $dbconn->Execute("SELECT $column[cid],
                                       $column[name],
                                       $column[contact],
                                       $column[email]
                                FROM $pntable[bannerclient]
                                ORDER BY $column[cid]");

    while (list($cid, $name, $contact, $email) = $result->fields)
    {
      $result2 = $dbconn->Execute("SELECT COUNT(*)
                                   FROM $pntable[banner]
                                   WHERE {$pntable['banner_column']['cid']}=".pnVarPrepForStore($cid)."");
      list($numrows) = $result2->fields;

      echo "<tr>"
          ."<td bgcolor=\"$bgcolor2\" align=\"center\">" . pnVarPrepForDisplay($cid) . "</td>"
          ."<td bgcolor=\"$bgcolor2\" align=\"center\">" . pnVarPrepForDisplay($name) . "</td>"
          ."<td bgcolor=\"$bgcolor2\" align=\"center\">" . pnVarPrepForDisplay($numrows) . "</td>"
          ."<td bgcolor=\"$bgcolor2\" align=\"center\">" . pnVarPrepForDisplay($contact) . "</td>"
          ."<td bgcolor=\"$bgcolor2\" align=\"center\">" . pnVarPrepForDisplay($email) . "</td>";

      if (pnSecAuthAction(0, 'Banners::Client', "$name::$cid", ACCESS_EDIT))
      {
        echo "<td bgcolor=\"$bgcolor2\" align=\"center\"><a href=\"admin.php?module="
             .$GLOBALS['module']."&amp;op=BannerClientEdit&amp;cid=$cid\">"._EDIT."</a>";
        if (pnSecAuthAction(0, 'Banners::Client', "$name::$cid", ACCESS_DELETE))
        {
          echo " | <a href=\"admin.php?module=".$GLOBALS['module']
              ."&amp;op=BannerClientDelete&amp;cid=$cid\">"._DELETE."</a></td></tr>";
        }
        else
        {
          echo "</td></tr>";
        }
      }
      else
      {
        echo "<td bgcolor=\"$bgcolor2\">&nbsp;</td>";
      }
      $result->MoveNext();
    }
    echo "</table>";
    CloseTable();
  }

  /* Add Banner */
  if ( pnSecAuthAction(0, 'Banners::Banner', '::', ACCESS_ADD) )
  {
    $column = $pntable['bannerclient_column'];
    $result = $dbconn->Execute("SELECT $column[cid], $column[name]
                                FROM $pntable[bannerclient]");
    if ( !$result->EOF )
    {
      OpenTable();
      echo "<font class=\"pn-title\"><b>"._ADDNEWBANNER."</b></font><br><br>"
          ."<b>0</b> &nbsp;Don't Show<br>"
          ."<b>1</b> &nbsp;Top Banner<br>"
          ."<b>3</b> &nbsp;Left Side<br>"
          ."<b>4</b> &nbsp;Right Side - animated<br>"
          ."<b>5</b> &nbsp;Right Side - static<br>"
          ."<b>6</b> &nbsp;Calendar (790 x 150)<br>"
          ."<b>7</b> &nbsp;Resource Listing (610 x 150)<br>"
          ."<br>"
          ."<form action=\"admin.php\" method=\"post\">"
          .""._CLIENTNAME . ":&nbsp;&nbsp;"
          ."<select name=\"cid\">";

      while ( list($cid, $name) = $result->fields )
      {
        echo "<option value=\"$cid\">". pnVarPrepForDisplay($name) . "</option>";
        $result->MoveNext();
      }

      echo "</select><br>";

//      $regions = array();
//      array_push( $regions, array( 'value' => 0, 'selected' => ( 0 == $event_region ? 'selected' : ''), 'name'=>'Everywhere' ) );
//      array_push( $regions, array( 'value' => 1, 'selected' => ( 1 == $event_region ? 'selected' : ''), 'name'=>'Denver Metro/Boulder' ) );
//      array_push( $regions, array( 'value' => 2, 'selected' => ( 2 == $event_region ? 'selected' : ''), 'name'=>'Northern Colorado' ) );
//      $tpl->assign('regions',$regions);

      echo ""._PURCHASEDIMPRESSIONS.": <input type=\"text\" name=\"imptotal\" size=\"12\" maxlength=\"11\"> 0 = "._UNLIMITED."<br>";
      echo '<br /><br />';

      // todo: hook this up
      echo 'Region:&nbsp;&nbsp;';
      echo '<select name="event_region">';
      echo '  <option value="0" selected>Everywhere</option>';
      echo '  <option value="1" >Denver Metro/Boulder</option>';
      echo '  <option value="2" >Northern Colorado</option>';
      echo '</select>';
      echo '<br />';

      echo ""._BANTYPE.": <input type=\"text\" name=\"type\" size=\"2\" maxlength=\"2\" value=\"5\"><b> ATTENTION !! : if you use Banner Modules insert type=0 for standard banners </b><br>";
      echo ""._IMAGEURL.": <input type=\"text\" name=\"imageurl\" size=\"50\" maxlength=\"250\" value=\"/images/banners/\"><br>";
      echo ""._CLICKURL.": <input type=\"text\" name=\"clickurl\" size=\"50\" maxlength=\"250\"><br>";
      echo "<br /><br />";
      echo " FOR BANNER IN FLASH : Width <input type=\"text\" name=\"width\" size=\"12\" maxlength=\"4\"> Height <input type=\"text\" name=\"height\" size=\"12\" maxlength=\"4\"><br><br>";
      echo ""._MODULE." :";
      echo "<select name=\"title\">";

      echo "<option value=\"ALL\">"._ALL."</option>";

      list($dbconn) = pnDBGetConn();
      $pntable = pnDBGetTables();
      $result = $dbconn->Execute("SELECT pn_name
                                  FROM $pntable[modules] ORDER BY pn_name ASC");

      while ( list($title) = $result->fields )
      {
        echo "<option value=\"$title\">$title</option>";
        $result->MoveNext();
      }

      echo "</select><br><br>"
          ."<input type=\"hidden\" name=\"module\" value=\"".$GLOBALS['module']."\">"
          ."<input type=\"hidden\" name=\"op\" value=\"BannersAdd\">"
          ."<input type=\"hidden\" name=\"authid\" value=\"" . pnSecGenAuthKey() . "\">"
          ."<input type=\"submit\" value=\""._ADDBANNER."\">"
          ."</form>";
      CloseTable();
    }
  }

  /* Add Client */
  if (pnSecAuthAction(0, 'Banners::Client', '::', ACCESS_ADD))
  {
    OpenTable();
    echo"<font class=\"pn-title\"><b>"._ADDCLIENT."</b><br><br>
        <form action=\"admin.php\" method=\"post\">
        "._CLIENTNAME.": <input type=\"text\" name=\"name\" size=\"30\" maxlength=\"60\"><br>
        "._CONTACTNAME.": <input type=\"text\" name=\"contact\" size=\"30\" maxlength=\"60\"><br>
        "._CONTACTEMAIL.": <input type=\"text\" name=\"email\" size=\"30\" maxlength=\"60\"><br>
        "._CLIENTLOGIN.": <input type=\"text\" name=\"login\" size=\"12\" maxlength=\"10\"><br>
        "._CLIENTPASSWD.": <input type=\"text\" name=\"passwd\" size=\"12\" maxlength=\"10\"><br><br>
        "._EXTRAINFO.":<br><textarea name=\"extrainfo\" cols=\"60\" rows=\"10\"></textarea><br>"
        ."<input type=\"hidden\" name=\"module\" value=\"".$GLOBALS['module']."\">"
        ."<input type=\"hidden\" name=\"op\" value=\"BannerAddClient\">"
        ."<input type=\"hidden\" name=\"authid\" value=\"" . pnSecGenAuthKey() . "\">"
        ."<input type=\"submit\" value=\""._ADDCLIENT2."\">"
        ."</form>";
    CloseTable();
  }

  // Access Banner Settings
  OpenTable();
  echo "<font class=\"pn-title\"><b>"._BANNERSCONF."</b></font><br /><br />";
  echo "<a href=\"admin.php?module=".$GLOBALS['module']."&amp;op=getConfig\">"._BANNERSCONF."</a>";
  CloseTable();
  include ("footer.php");
}

function BannersAdd()
{
  list($dbconn) = pnDBGetConn();
  $pntable = pnDBGetTables();

  list($name,
       $cid,
       $type,
       $imptotal,
       $imageurl,
       $clickurl,
       $clientname,
       $title,
       $width,
       $height,
       $regionid ) = pnVarCleanFromInput('name',
                                         'cid',
                                         'type',
                                         'imptotal',
                                         'imageurl',
                                         'clickurl',
                                         'clientname',
                                         'title',
                                         'width',
                                         'height',
                                         'event_region');

  // jgm - get and use clientname
  if ( !isset( $clientname ) )
  {
    $clientname = '';
  }
  if ( !( pnSecAuthAction( 0, 'Banners::Banner', "$clientname::", ACCESS_ADD ) ) )
  {
    include 'header.php';
    echo _BANNERSADDBANNERNOAUTH;
    include 'footer.php';
    exit;
  }

  if ( !pnSecConfirmAuthKey() )
  {
    include 'header.php';
    echo _BADAUTHKEY;
    include 'footer.php';
    exit;
  }

  $column = $pntable['banner_column'];
  $result = $dbconn->Execute("INSERT INTO $pntable[banner]
                              ($column[bid], $column[cid], $column[type], $column[imptotal],
                              $column[impmade], $column[clicks], $column[imageurl],
                              $column[clickurl], $column[date], pn_title, pn_width, pn_height, $column[regionid])
                              VALUES (NULL, '".pnVarPrepForStore($cid)."', '".pnVarPrepForStore($type)."', '".pnVarPrepForStore($imptotal)."', '1', '0',
                              '".pnVarPrepForStore($imageurl)."', '".pnVarPrepForStore($clickurl)."', now(), '$title', '$width', '$height', '$regionid')");
  if ( $dbconn->ErrorNo() <> 0 )
  {
    error_log("Error: " . $dbconn->ErrorMsg());
  }
  pnRedirect('admin.php?module='.$GLOBALS['module'].'&op=BannersAdmin');
}

function BannerAddClient()
{
  list($dbconn) = pnDBGetConn();
  $pntable = pnDBGetTables();

  list($name,
       $contact,
       $email,
       $login,
       $passwd,
       $extrainfo) = pnVarCleanFromInput('name',
                                         'contact',
                                         'email',
                                         'login',
                                         'passwd',
                                         'extrainfo');

  if (!(pnSecAuthAction(0, 'Banners::Client', '::', ACCESS_ADD)))
  {
    include 'header.php';
    echo _BANNERSADDCLIENTNOAUTH;
    include 'footer.php';
    exit;
  }

  if (!pnSecConfirmAuthKey())
  {
    include 'header.php';
    echo _BADAUTHKEY;
    include 'footer.php';
    exit;
  }

  $column = $pntable['bannerclient_column'];
  $result = $dbconn->Execute("INSERT INTO $pntable[bannerclient]
                              ($column[cid], $column[name], $column[contact],
                              $column[email], $column[login], $column[passwd],
                              $column[extrainfo])
                              VALUES (NULL, '".pnVarPrepForStore($name)."', '".pnVarPrepForStore($contact)."', '".pnVarPrepForStore($email)."', '".pnVarPrepForStore($login)."',
                              '".pnVarPrepForStore($passwd)."', '".pnVarPrepForStore($extrainfo)."')");
  if ( $dbconn->ErrorNo() <> 0 )
  {
    error_log("Error: " . $dbconn->ErrorMsg());
  }
  pnRedirect('admin.php?module='.$GLOBALS['module'].'&op=BannersAdmin');
}

function BannerFinishDelete()
{
  $bid = pnVarCleanFromInput('bid');

  list($dbconn) = pnDBGetConn();
  $pntable = pnDBGetTables();

  $bannercolumn = &$pntable['banner_column'];
  $bannerclientcolumn =  &$pntable['bannerclient_column'];

  $result = $dbconn->Execute("SELECT $bannerclientcolumn[name]
                              FROM $pntable[banner], $pntable[bannerclient]
                              WHERE $bannercolumn[bid] = 1
                              AND $bannercolumn[cid] = ".pnVarPrepForStore($bannerclientcolumn['cid'])."");
  list($clientname) = $result->fields;
  $result->Close();

  if ( !pnSecAuthAction( 0, 'Banners::Banner', "$clientname::$bid", ACCESS_DELETE ) )
  {
    include 'header.php';
    echo _BANNERSDELBANNERNOAUTH;
    include 'footer.php';
    return;
  }

  if ( !pnSecConfirmAuthKey() )
  {
    include 'header.php';
    echo _BADAUTHKEY;
    include 'footer.php';
    exit;
  }

  $result = $dbconn->Execute("DELETE FROM $pntable[bannerfinish]
                              WHERE {$pntable[bannerfinish_column][bid]}=".pnVarPrepForStore($bid)."");
  if ( $dbconn->ErrorNo() <> 0 )
  {
    error_log("Error: " . $dbconn->ErrorMsg());
  }

  pnRedirect('admin.php?module='.$GLOBALS['module'].'&op=BannersAdmin');
}

function BannerDelete()
{
  list($bid,
       $ok) = pnVarCleanFromInput('bid',
                                  'ok');
  if ( !isset( $ok ) )
  {
    $ok = 0;
  }

  list($dbconn) = pnDBGetConn();
  $pntable = pnDBGetTables();

  $bannercolumn = &$pntable['banner_column'];
  $bannerclientcolumn =  &$pntable['bannerclient_column'];

  $result = $dbconn->Execute("SELECT $bannerclientcolumn[name]
                              FROM $pntable[banner], $pntable[bannerclient]
                              WHERE $bannercolumn[bid] = 1
                              AND $bannercolumn[cid] = ".pnVarPrepForStore($bannerclientcolumn['cid'])."");
  list($clientname) = $result->fields;
  $result->Close();

  if ( !pnSecAuthAction( 0, 'Banners::Banner', "$clientname::$bid", ACCESS_DELETE ) )
  {
    include 'header.php';
    echo _BANNERSDELBANNERNOAUTH;
    include 'footer.php';
    return;
  }

  if ( $ok == 1 )
  {
    if ( !pnSecConfirmAuthKey() )
    {
      include 'header.php';
      echo _BADAUTHKEY;
      include 'footer.php';
      exit;
    }

    $column = $pntable['banner_column'];
    $result = $dbconn->Execute("DELETE FROM $pntable[banner]
                                WHERE {$pntable['banner_column']['bid']}='".pnVarPrepForStore($bid)."'");
    if( $dbconn->ErrorNo() <> 0 )
    {
      error_log("Error: " . $dbconn->ErrorMsg());
    }

    pnRedirect('admin.php?module='.$GLOBALS['module'].'&op=BannersAdmin');
  }
  else
  {
    include("header.php");
    $bgcolor2 = $GLOBALS['bgcolor2'];
    GraphicAdmin();
    OpenTable();
    echo "<center><font class=\"pn-title\"><b>"._BANNERSADMIN."</b></font></center>";
    CloseTable();
    $column = $pntable['banner_column'];
    $column2 = $pntable['bannerclient_column'];
    $result = $dbconn->Execute("SELECT $column[cid], $column[imptotal],
                                       $column[impmade], $column[clicks],
                                       $column[imageurl], $column[clickurl], pn_title, pn_type, pn_width, pn_height,
                                       $column2[name]
                                FROM $pntable[banner], $pntable[bannerclient]
                                WHERE $column[bid]=".pnVarPrepForStore($bid)."
                                AND $column[cid] = ".pnVarPrepForStore($column2['cid'])."");
    list($cid, $imptotal, $impmade, $clicks, $imageurl, $clickurl, $title, $type, $width, $height, $name) = $result->fields;
    OpenTable();

    echo "<center><b>"._DELETEBANNER."</b><br><br>";
    //-------------- code modified for Banner Modules 1.0 ----------------//

    if (strpos($imageurl,"swf")==TRUE)
    {
      echo "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"\n";
      echo "codebase=\"http://active.macromedia.com/flash4/cabs/swflash.cab#version=4,0,0,0\"\n";
      echo "id=\"bannerflash\" width=\"$width\" height=\"$height\">\n";
      echo " <param name=\"movie\" value=\"$imageurl\">\n";
      echo " <param name=\"quality\" value=\"high\">\n";
      echo " <param name=\"bgcolor\" value=\"#FFFFFF\">\n";
      echo " <embed name=\"bannerflash\" src=\"$imageurl\" quality=\"high\" bgcolor=\"#FFFFFF\"\n";
      echo "   width=\"$width\" height=\"$height\"\n";
      echo "   type=\"application/x-shockwave-flash\"\n";
      echo "   pluginspage=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\">\n";
      echo " </embed>\n";
      echo "</object>\n";
      echo "</a></center>";
    }
    elseif ( strpos( $imageurl, "htm" ) == TRUE )
    {
      include $imageurl;
    }
    else
    {
      echo "<a href=\"$clickurl\"><img src=\"$imageurl\" border=\"1\" alt=\"\"></a><br>";
    }

    echo "<center><a href=\"$clickurl\">$clickurl</center></a><br><br>"
        ."<table width=\"100%\" border=\"0\"><tr>"
        ."<td bgcolor=\"$bgcolor2\" align=\"center\"><b>"._ID."<b></td>"
        ."<td bgcolor=\"$bgcolor2\" align=\"center\"><b>"._IMPRESSIONS."<b></td>"
        ."<td bgcolor=\"$bgcolor2\" align=\"center\"><b>"._IMPLEFT."<b></td>"
        ."<td bgcolor=\"$bgcolor2\" align=\"center\"><b>"._CLICKS."<b></td>"
        ."<td bgcolor=\"$bgcolor2\" align=\"center\"><b>"._CLICKSPERCENT."<b></td>"
        ."<td bgcolor=\"$bgcolor2\" align=\"center\"><b>"._TYPE."<b></td>"
        ."<td bgcolor=\"$bgcolor2\" align=\"center\"><b>"._MODULE."<b></td>"
        ."<td bgcolor=\"$bgcolor2\" align=\"center\"><b>"._CLIENTNAME."<b></td><tr>";

    $percent = substr( 100 * $clicks / $impmade, 0, 5 );

    if ( $imptotal == 0 )
    {
      $left = _UNLIMITED;
    }
    else
    {
      $left = $imptotal - $impmade;
    }
    echo "<td bgcolor=\"$bgcolor2\" align=\"center\">" . pnVarPrepForDisplay($bid) . "</td>"
         ."<td bgcolor=\"$bgcolor2\" align=\"center\">" . pnVarPrepForDisplay($impmade) . "</td>"
         ."<td bgcolor=\"$bgcolor2\" align=\"center\">" . pnVarPrepForDisplay($left) . "</td>"
         ."<td bgcolor=\"$bgcolor2\" align=\"center\">" . pnVarPrepForDisplay($clicks) . "</td>"
         ."<td bgcolor=\"$bgcolor2\" align=\"center\">" . pnVarPrepForDisplay($percent) . "%</td>"
         ."<td bgcolor=\"$bgcolor2\" align=\"center\">$type</td>"
         ."<td bgcolor=\"$bgcolor2\" align=\"center\">$title</td>"
         ."<td bgcolor=\"$bgcolor2\" align=\"center\">" . pnVarPrepForDisplay($name) . "</td><tr>";

    echo "</td></tr></table><br>"
        ."<center>"._SURETODELBANNER."<br><br>"
        ."[ <a href=\"admin.php?module=".$GLOBALS['module']."&amp;op=BannersAdmin\">"
        ._NO."</a> | <a href=\"admin.php?module=".$GLOBALS['module']."&amp;op=BannerDelete&amp;bid=$bid&amp;ok=1&amp;authid=" . pnSecGenAuthKey() . "\">"
        ._YES."</a> ]</center><br><br>";
    CloseTable();
    include("footer.php");
  }
}

function BannerEdit($bid)
{
  list($dbconn) = pnDBGetConn();
  $pntable = pnDBGetTables();
  include("header.php");
  GraphicAdmin();
  OpenTable();
  echo "<center><font class=\"pn-title\"><b>"._BANNERSADMIN."</b></font></center>";
  CloseTable();

  $column = $pntable['banner_column'];
  $column2 = $pntable['bannerclient_column'];
  $result = $dbconn->Execute( "SELECT pn_cid, pn_imptotal,
                                      pn_impmade, pn_clicks, pn_imageurl,
                                      pn_clickurl, pn_title, pn_type, pn_width, pn_height, pn_regionid
                               FROM $pntable[banner]
                               WHERE $column[bid]=".pnVarPrepForStore($bid) . "" );

  list($cid, $imptotal, $impmade, $clicks, $imageurl, $clickurl, $title, $type, $width, $height, $region_id) = $result->fields;

  $result = $dbconn->Execute( "SELECT pn_name
                               FROM $pntable[bannerclient]
                               WHERE pn_cid='$cid'" );

  list($name) = $result->fields;

  if ( !pnSecAuthAction( 0, 'Banners::Banner', "$name::$bid", ACCESS_EDIT ) )
  {
    echo _BANNERSEDITBANNERNOAUTH;
    include 'footer.php';
    return;
  }
  OpenTable();
  echo "<center><font class=\"pn-title\"><b>"._EDITBANNER."</b></font></center><br><br>";

  //-------------- code modified for Banner Modules 1.0 ----------------//
  if ( strpos( $imageurl, "swf" ) == TRUE)
  {
    echo "<center>";
    echo "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"\n";
    echo "codebase=\"http://active.macromedia.com/flash4/cabs/swflash.cab#version=4,0,0,0\"\n";
    echo "id=\"bannerflash\" width=\"$width\" height=\"$height\">\n";
    echo " <param name=\"movie\" value=\"$imageurl\">\n";
    echo " <param name=\"quality\" value=\"high\">\n";
    echo " <param name=\"bgcolor\" value=\"#FFFFFF\">\n";
    echo " <embed name=\"bannerflash\" src=\"$imageurl\" quality=\"high\" bgcolor=\"#FFFFFF\"\n";
    echo "   width=\"$width\" height=\"$height\"\n";
    echo "   type=\"application/x-shockwave-flash\"\n";
    echo "   pluginspage=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\">\n";
    echo " </embed>\n";
    echo "</object>\n";
    echo "</a></center>";
  }
  elseif ( strpos( $imageurl, "htm" ) == TRUE )
  {
    include $imageurl;
  }
  else
  {
    // echo "<img src=\"$imageurl\" border=\"1\" alt=\"\"><br><br>";
    echo "<a href=\"$clickurl\" target=\"_blank\"><img src=\"$imageurl\" border=\"1\" alt=\"\"></a><br><br>";
  }

  //-------------- end code modified for Banner Modules 1.0 ----------------//
  echo "<form action=\"admin.php\" method=\"post\">"
      .""._CLIENTNAME.": "
      ."<select name=\"cid\">";
  echo "<option value=\"$cid\" selected>".pnVarPrepForDisplay($name)."</option>";
  $column = $pntable['banner_column'];
  $result = $dbconn->Execute("SELECT $column2[cid], $column2[name] from
                         $pntable[bannerclient]");

  while ( list($ccid, $name) = $result->fields )
  {
    $result->MoveNext();
    if( $cid != $ccid )
    {
      echo "<option value=\"$ccid\">" . pnVarPrepForDisplay($name) . "</option>";
    }
  }

  echo "</select><br>";
  if ( $imptotal == 0 )
  {
    $impressions = _UNLIMITED;
  }
  else
  {
    $impressions = $imptotal;
  }

  echo "<br />"
    ."<b>0</b> &nbsp;Don't Show<br>"
    ."<b>1</b> &nbsp;Top Banner<br>"
    ."<b>3</b> &nbsp;Left Side<br>"
    ."<b>4</b> &nbsp;Right Side - animated<br>"
    ."<b>5</b> &nbsp;Right Side - static<br>"
    ."<b>6</b> &nbsp;Calendar (790 x 150)<br>"
    ."<b>7</b> &nbsp;Resource Listing (610 x 150)<br>"
    ."<br>";

  echo "<br>"._ADDIMPRESSIONS.": <input type=\"text\" name=\"impadded\" size=\"12\" maxlength=\"11\"> "._PURCHASED.": <b>" . pnVarPrepForDisplay($impressions) . "</b> "._MADE.": <b>" . pnVarPrepForDisplay($impmade) . "</b><br>";

  echo 'Region:&nbsp;&nbsp;';
  echo '<select name="event_region">';
  echo '  <option value="0" ' . ( $region_id == 0 ? "selected" : "" ) . '>Everywhere</option>';
  echo '  <option value="1" ' . ( $region_id == 1 ? "selected" : "" ) .  '>Denver Metro/Boulder</option>';
  echo '  <option value="2" ' . ( $region_id == 2 ? "selected" : "" ) . '>Northern Colorado</option>';
  echo '</select>';
  echo '<br />';

  echo ""._BANTYPE.": <input type=\"text\" name=\"type\" size=\"2\" maxlength=\"2\" value=\"$type\"><br>"
  .""._IMAGEURL.": <input type=\"text\" name=\"imageurl\" size=\"50\" maxlength=\"255\" value=\"$imageurl\"><br>"
  .""._CLICKURL.": <input type=\"text\" name=\"clickurl\" size=\"50\" maxlength=\"255\" value=\"$clickurl\"><br>"
  ." FOR BANNER IN FLASH : Width <input type=\"text\" name=\"width\" size=\"12\" maxlength=\"4\" value=\"$width\"> Height <input type=\"text\" name=\"height\" size=\"12\" maxlength=\"4\" value=\"$height\"><br>";

  echo ""._MODULE." :"
       ." <select name=\"title\">";

  echo "<option value=\"ALL\">"._ALL."</option>";
  echo "<option value=\"$title\" selected>$title</option>";
  list($dbconn) = pnDBGetConn();

  $pntable = pnDBGetTables();
  $result = $dbconn->Execute("SELECT pn_name
                              FROM $pntable[modules]
                              ORDER BY pn_name ASC");

  while ( list($title) = $result->fields )
  {
    echo "<option value=\"$title\">$title</option>";
    $result->MoveNext();
  }

  echo "</select><br><br>"
      ."<input type=\"hidden\" name=\"bid\" value=\"$bid\">"
      ."<input type=\"hidden\" name=\"imptotal\" value=\"$imptotal\">"
      ."<input type=\"hidden\" name=\"module\" value=\"".$GLOBALS['module']."\">"
      ."<input type=\"hidden\" name=\"op\" value=\"BannerChange\">"
      ."<input type=\"hidden\" name=\"authid\" value=\"" . pnSecGenAuthKey() . "\">"
      ."<input type=\"submit\" value=\""._SAVECHANGES."\">"
      ."</form>";

  CloseTable();
  include("footer.php");
}

function BannerChange()
{
  list($bid,
       $cid,
       $type,
       $imptotal,
       $impadded,
       $imageurl,
       $clickurl,
       $title,
       $width,
       $height,
       $regionid ) = pnVarCleanFromInput('bid',
                                         'cid',
                                         'type',
                                         'imptotal',
                                         'impadded',
                                         'imageurl',
                                         'clickurl',
                                         'title',
                                         'width',
                                         'height',
                                         'event_region');

  list($dbconn) = pnDBGetConn();
  $pntable = pnDBGetTables();

  $imp = $imptotal+$impadded;

  $bannercolumn = &$pntable['banner_column'];
  $bannerclientcolumn =  &$pntable['bannerclient_column'];

  $result = $dbconn->Execute("SELECT $bannerclientcolumn[name]
                              FROM $pntable[banner], $pntable[bannerclient]
                              WHERE $bannercolumn[bid] = 1
                              AND $bannercolumn[cid] = ".pnVarPrepForStore($bannerclientcolumn['cid'])."");
  list($clientname) = $result->fields;
  $result->Close();

  if ( !pnSecAuthAction( 0, 'Banners::Banner', "$clientname::$bid", ACCESS_EDIT ) )
  {
    include 'header.php';
    echo _BANNERSEDITBANNERNOAUTH;
    include 'footer.php';
    return;
  }

  if ( !pnSecConfirmAuthKey() )
  {
    include 'header.php';
    echo _BADAUTHKEY;
    include 'footer.php';
    exit;
  }

  $result = $dbconn->Execute("UPDATE $pntable[banner]
                              SET $bannercolumn[cid]='".pnVarPrepForStore($cid)."',
                                  $bannercolumn[type]='".pnVarPrepForStore($type)."',
                                  $bannercolumn[imptotal]='".pnVarPrepForStore($imp)."',
                                  $bannercolumn[imageurl]='".pnVarPrepForStore($imageurl)."',
                                  $bannercolumn[clickurl]='".pnVarPrepForStore($clickurl)."',
                                  pn_title='$title',
                                  pn_width='$width',
                                  pn_height='$height',
                                  $bannercolumn[regionid]='$regionid'
                              WHERE $bannercolumn[bid]=".pnVarPrepForStore($bid)."");
  if ( $dbconn->ErrorNo()<>0 )
  {
    error_log("Error: " . $dbconn->ErrorMsg());
  }

  pnRedirect('admin.php?module='.$GLOBALS['module'].'&op=BannersAdmin');
}

function BannerClientDelete()
{
  list($cid,
       $ok) = pnVarCleanFromInput('cid',
                                  'ok');
  if ( !isset( $ok ) )
  {
    $ok = 0;
  }

  list($dbconn) = pnDBGetConn();
  $pntable = pnDBGetTables();

  $bannerclientcolumn =  &$pntable['bannerclient_column'];

  $result = $dbconn->Execute("SELECT $bannerclientcolumn[name]
                              FROM $pntable[bannerclient]
                              WHERE $bannerclientcolumn[cid] = ".pnVarPrepForStore($cid)."");
  list($clientname) = $result->fields;
  $result->Close();

  if ( !pnSecAuthAction( 0, 'Banners::Client', "$clientname::$cid", ACCESS_DELETE ) )
  {
    include 'header.php';
    echo _BANNERSDELCLIENTNOAUTH;
    include 'footer.php';
    return;
  }

  if ( $ok == 1 )
  {
    if ( !pnSecConfirmAuthKey() )
    {
      include 'header.php';
      echo _BADAUTHKEY;
      include 'footer.php';
      exit;
    }

    $result = $dbconn->Execute("DELETE FROM $pntable[banner]
                                WHERE {$pntable['banner_column']['cid']}='".pnVarPrepForStore($cid)."'");

    if ( $dbconn->ErrorNo() <> 0 )
    {
      error_log("Error: " . $dbconn->ErrorMsg());
    }

    $result = $dbconn->Execute("DELETE FROM $pntable[bannerclient]
                                WHERE {$pntable['bannerclient_column']['cid']}='".pnVarPrepForStore($cid)."'");

    if ( $dbconn->ErrorNo() <> 0 )
    {
      error_log("Error: " . $dbconn->ErrorMsg());
    }

    pnRedirect('admin.php?module='.$GLOBALS['module'].'&op=BannersAdmin');
  }
  else
  {
    include("header.php");
    GraphicAdmin();
    OpenTable();
    echo "<center><font class=\"pn-title\"><b>"._BANNERSADMIN."</b></font></center>";
    CloseTable();

    $column = $pntable['bannerclient_column'];
    $result = $dbconn->Execute("SELECT $column[cid], $column[name]
                                FROM $pntable[bannerclient]
                                WHERE $column[cid]=".pnVarPrepForStore($cid)."");

    list($cid, $name) = $result->fields;
    OpenTable();
    echo "<center><b>"._DELETECLIENT.": $name</b><br><br>"._SURETODELCLIENT."<br><br>";
    $column = $pntable['banner_column'];
    $result = $dbconn->Execute("SELECT $column[imageurl], $column[clickurl]
                                FROM $pntable[banner]
                                WHERE $column[cid]=".pnVarPrepForStore($cid)."");
    if ( $result->EOF )
    {
      echo ""._CLIENTWITHOUTBANNERS."<br><br>";
    }
    else
    {
      echo "<b>"._WARNING."!!!</b><br>"._DELCLIENTHASBANNERS.":<br><br>";
    }

    while ( list($imageurl, $clickurl) = $result->fields )
    {
      echo "<a href=\"$clickurl\"><img src=\"$imageurl\" border=\"1\" alt=\"\"></a><br>
            <a href=\"$clickurl\">$clickurl</a><br><br>";
      $result->MoveNext();
    }

    echo "" . _SURETODELCLIENT . "<br><br>[ <a href=\"admin.php?module=".$GLOBALS['module']."&amp;op=BannersAdmin\">"
         ._NO."</a> | <a href=\"admin.php?module=".$GLOBALS['module']."&amp;op=BannerClientDelete&amp;cid=$cid&amp;ok=1&amp;authid=" . pnSecGenAuthKey() . "\">"
         ._YES."</a> ]</center><br><br>";
    CloseTable();
    include("footer.php");
  }
}

function BannerClientEdit()
{
  list($cid,
       $clientname) = pnVarCleanFromInput('cid',
                                          'clientname');

  list($dbconn) = pnDBGetConn();
  $pntable = pnDBGetTables();

  include("header.php");

  GraphicAdmin();
  OpenTable();
  echo "<center><font class=\"pn-title\"><b>"._BANNERSADMIN."</b></font></center>";
  CloseTable();

  $column = $pntable['bannerclient_column'];
  $result = $dbconn->Execute("SELECT $column[name],
                                     $column[contact],
                                     $column[email],
                                     $column[login],
                                     $column[passwd],
                                     $column[extrainfo]
                              FROM $pntable[bannerclient]
                              WHERE $column[cid]=".pnVarPrepForStore($cid)."");
  list($name, $contact, $email, $login, $passwd, $extrainfo) = $result->fields;

  if( !isset( $clientname ) )
  {
    $clientname = '';
  }

  if ( !pnSecAuthAction( 0, 'Banners::Client', "$clientname::$cid", ACCESS_EDIT ) )
  {
    echo _BANNERSEDITCLIENTNOAUTH;
    include 'footer.php';
    return;
  }

  OpenTable();
  echo "<center><font class=\"pn-title\"><b>"._EDITCLIENT."</b></font></center><br><br>"
       ."<form action=\"admin.php\" method=\"post\">"
       .""._CLIENTNAME.": <input type=\"text\" name=\"name\" value=\"$name\" size=\"30\" maxlength=\"60\"><br>"
       .""._CONTACTNAME.": <input type=\"text\" name=\"contact\" value=\"$contact\" size=\"30\" maxlength=\"60\"><br>"
       .""._CONTACTEMAIL.": <input type=\"text\" name=\"email\" size=30 maxlength=\"60\" value=\"$email\"><br>"
       .""._CLIENTLOGIN.": <input type=\"text\" name=\"login\" size=12 maxlength=\"10\" value=\"$login\"><br>"
       .""._CLIENTPASSWD.": <input type=\"text\" name=\"passwd\" size=12 maxlength=\"10\" value=\"$passwd\"><br><br>"
       .""._EXTRAINFO."<br><textarea name=\"extrainfo\" cols=\"60\" rows=\"10\">$extrainfo</textarea><br>"
       ."<input type=\"hidden\" name=\"cid\" value=\"$cid\">"
       ."<input type=\"hidden\" name=\"module\" value=\"".$GLOBALS['module']."\">"
       ."<input type=\"hidden\" name=\"op\" value=\"BannerClientChange\">"
       ."<input type=\"hidden\" name=\"authid\" value=\"" . pnSecGenAuthKey() . "\">"
       ."<input type=\"submit\" value=\""._SAVECHANGES."\">"
       ."</form>";
  CloseTable();

  include 'footer.php';
}

function BannerClientChange()
{
  list($cid,
       $name,
       $contact,
       $email,
       $extrainfo,
       $login,
       $passwd) = pnVarCleanFromInput('cid',
                                      'name',
                                      'contact',
                                      'email',
                                      'extrainfo',
                                      'login',
                                      'passwd');

  list($dbconn) = pnDBGetConn();
  $pntable = pnDBGetTables();

  $column = $pntable['bannerclient_column'];

  // NB - authorisation is against *OLD* client name
  $bannerclientcolumn =  &$pntable['bannerclient_column'];

  $result = $dbconn->Execute("SELECT $bannerclientcolumn[name]
                            FROM $pntable[bannerclient]
                            WHERE $bannerclientcolumn[cid] = ".pnVarPrepForStore($cid)."");
  list($clientname) = $result->fields;
  $result->Close();

  if ( !pnSecAuthAction( 0, 'Banners::Client', "$clientname::$cid", ACCESS_EDIT ) )
  {
    include 'header.php';
    echo _BANNERSEDITCLIENTNOAUTH;
    include 'footer.php';
    return;
  }

  if ( !pnSecConfirmAuthKey() )
  {
    include 'header.php';
    echo _BADAUTHKEY;
    include 'footer.php';
    exit;
  }

  $result = $dbconn->Execute("UPDATE $pntable[bannerclient]
                              SET $column[name]='".pnVarPrepForStore($name)."', $column[contact]='".pnVarPrepForStore($contact)."',
                                  $column[email]='".pnVarPrepForStore($email)."', $column[extrainfo]='".pnVarPrepForStore($extrainfo)."',
                                  $column[login]='".pnVarPrepForStore($login)."', $column[passwd]='".pnVarPrepForStore($passwd)."'
                              WHERE $column[cid]=".pnVarPrepForStore($cid)."");

  if( $dbconn->ErrorNo() <> 0 )
  {
    error_log("Error: " . $dbconn->ErrorMsg());
  }

  pnRedirect('admin.php?module='.$GLOBALS['module'].'&op=BannersAdmin');
}

function banners_admin_getConfig()
{
  include 'header.php';
  $bgcolor2 = $GLOBALS["bgcolor2"];

  // prepare vars
  $sel_banners['0'] = '';
  $sel_banners['1'] = '';
  $sel_banners['2'] = '';
  $sel_banners[pnConfigGetVar('banners')] = ' checked';

  GraphicAdmin();
  OpenTable();
  print '<center><font size="3" class="pn-title">'._BANNERSCONF.'</b></font></center><br />'
       .'<form action="admin.php" method="post">'
       .'<table border="0"><tr><td class="pn-normal">'
       ._ACTBANNERS.'</td><td class="pn-normal">'
       ."<input type=\"radio\" name=\"xbanners\" value=\"1\" class=\"pn-normal\" ".$sel_banners['1'].">"._YES.' &nbsp;'
       ."<input type=\"radio\" name=\"xbanners\" value=\"0\" class=\"pn-normal\" ".$sel_banners['0'].">"._NO.' &nbsp;'
       ."<input type=\"radio\" name=\"xbanners\" value=\"2\" class=\"pn-normal\" ".$sel_banners['2']."> Banner Modules"
       .'</td></tr><tr><td class="pn-normal">'
       ._YOURIP.':</td><td>'
       ."<input type=\"text\" name=\"xmyIP\" value=\"".pnConfigGetVar('myIP')."\" size=\"30\" class=\"pn-normal\">"
       .'</td></tr></table>'
       ."<input type=\"hidden\" name=\"module\" value=\"".$GLOBALS['module']."\">"
       ."<input type=\"hidden\" name=\"op\" value=\"setConfig\">"
       ."<input type=\"hidden\" name=\"authid\" value=\"" . pnSecGenAuthKey() . "\">"
       ."<input type=\"submit\" value=\""._SUBMIT."\">"
       ."</form>";

  CloseTable();
  include 'footer.php';
}

function banners_admin_setConfig($var)
{
  if ( !pnSecConfirmAuthKey() )
  {
    include 'header.php';
    echo _BADAUTHKEY;
    include 'footer.php';
    exit;
  }

  // Escape some characters in these variables.
  // hehe, I like doing this, much cleaner :-)
  $fixvars = array();

  // todo: make FixConfigQuotes global / replace with other function
  foreach ( $fixvars as $v )
  {
    // $var[$v] = FixConfigQuotes($var[$v]);
  }

  // Set any numerical variables that havn't been set, to 0. i.e. paranoia check :-)
  $fixvars = array();

  foreach ( $fixvars as $v )
  {
    if ( empty( $var[$v] ) )
    {
      $var[$v] = 0;
    }
  }

  // all variables starting with x are the config vars.
  while ( list($key, $val) = each ($var) )
  {
    if ( substr( $key, 0, 1 ) == 'x' )
    {
      pnConfigSetVar(substr($key, 1), $val);
    }
  }
  pnRedirect('admin.php');
}

function banners_admin_main($var)
{
  list($op,
       $bid) = pnVarCleanFromInput('op',
                                   'bid');

  extract($var);

  if ( !( pnSecAuthAction( 0, 'Banners::Banner', '::', ACCESS_READ ) ) )
  {
    include 'header.php';
    echo _BANNERSNOAUTH;
    include 'footer.php';
  }
  else
  {
    switch( $op )
    {
      case "BannersAdmin":
        BannersAdmin();
        break;

      case "BannersAdd":
        BannersAdd();
        break;

      case "BannerAddClient":
        BannerAddClient();
        break;

      case "BannerFinishDelete":
        BannerFinishDelete();
        break;

      case "BannerDelete":
        BannerDelete();
        break;

      case "BannerEdit":
        BannerEdit($bid);
        break;

      case "BannerChange":
        BannerChange();
        break;

      case "BannerClientDelete":
        BannerClientDelete();
        break;

      case "BannerClientEdit":
        BannerClientEdit();
        break;

      case "BannerClientChange":
        BannerClientChange();
        break;

      case "getConfig":
         banners_admin_getConfig();
         break;

      case "setConfig":
         banners_admin_setConfig( $var );
         break;

      default:
         BannersAdmin();
         break;
    }
  }
}
?>