<?php
// File: $Id: pnBanners.php,v 1.2 2002/11/30 12:28:57 magicx Exp $
// ----------------------------------------------------------------------
// POST-NUKE Content Management System
// Copyright (C) 2001 by the Post-Nuke 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: Francisco Burzi
// Purpose of file: Display banners
// ----------------------------------------------------------------------

/**
 * Function to display banners in all pages
 */
function pnBannerDisplayMulti( & $dbconn, $type, & $pntable )
{
  $MAX_BANNERS = 8;
  $regionID = getRegionIDCcookie();

  $column = &$pntable['banner_column'];

  // get animated gifs
  $sql = "SELECT pn_bid, pn_imageurl, pn_width, pn_height FROM $pntable[banner] WHERE pn_type='$type'";
  if ( $regionID > 0 )
  {
    $sql .= " AND ( pn_regionid = $regionID or pn_regionid = 0 ) ";
  }
  $bresult2 = $dbconn->Execute( $sql );
  $animatedbannerarray = array();
  $currBanner = 0;
  while ( list($bid, $imageurl, $width, $height) = $bresult2->fields )
  {
    $bresult2->MoveNext();
    $animatedbannerarray[$currBanner] = array( $bid, $imageurl, $width, $height );
    ++$currBanner;
  }
  $bresult2->Close();

  // get static gifs
  $sql = "SELECT pn_bid, pn_imageurl, pn_width, pn_height FROM $pntable[banner] WHERE pn_type='5'";
  if ( $regionID > 0 )
  {
    $sql .= " AND ( pn_regionid = $regionID or pn_regionid = 0 ) ";
  }
  $bresult2 = $dbconn->Execute( $sql );
  $staticbannerarray = array();
  $currBanner = 0;
  while ( list($bid, $imageurl, $width, $height) = $bresult2->fields )
  {
    $bresult2->MoveNext();
    $staticbannerarray[$currBanner] = array( $bid, $imageurl, $width, $height );
    ++$currBanner;
  }
  $bresult2->Close();

  shuffle($staticbannerarray);
  shuffle($animatedbannerarray);

//  $bannerarray = array();

  $myIP = pnConfigGetVar('myIP');
  $myhost = getenv("REMOTE_ADDR");

//  $maxBanners = min( count($bannerarray), $MAX_BANNERS );
  $maxBanners = $MAX_BANNERS;
  $content = "";

  $staticbannermax   = count($staticbannerarray);
  $animatedbannermax = count($animatedbannerarray);

  $staticbannercount   = 0;
  $animatedbannercount = 0;

  for ( $i = 0; $i < $maxBanners ; $i++ )
  {
    $foundAd = false;
    if ( $i % 2 == 0 )
    {
      if ( $animatedbannercount < $animatedbannermax )
      {
        $foundAd = true;
        list($bid, $imageurl, $width, $height) = $animatedbannerarray[$animatedbannercount];
        ++$animatedbannercount;
      }
    }
    else
    {
      if ( $staticbannercount < $staticbannermax )
      {
        $foundAd = true;
        list($bid, $imageurl, $width, $height) = $staticbannerarray[$staticbannercount];
        ++$staticbannercount;
      }
    }

    if ( $foundAd )
    {
      if ($myIP == $myhost)
      {
        // do nothing
      }
      else
      {
        $dbconn->Execute("UPDATE $pntable[banner]
                          SET $column[impmade]=$column[impmade]+1
                          WHERE $column[bid]=".pnVarPrepForStore($bid)."");
      }

      $content .= "<center><a href=\"banners.php?op=click&amp;bid=$bid\" target=\"_blank\">";
      $content .= "<img src=\"$imageurl\" border=\"0\" alt=\""._CLICK."\"></a></center>";
      if ( $i < $maxBanners - 1 )
      {
        $content .= "<br>";
      }
    }
  }
  return $content;
}

function pnBannerDisplay($type=0)
{
  // test on config settings
  if (pnConfigGetVar('banners') == 0) return '&nbsp;';

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

  if ( $type == 4 )
  {
    return pnBannerDisplayMulti( & $dbconn, $type, & $pntable );
  }

  $regionID = getRegionIDCcookie();
  $column = &$pntable['banner_column'];
  if ( pnConfigGetVar('banners') != 2 )
  {
    $sql = "SELECT count(*) AS count FROM $pntable[banner] WHERE $column[type] = $type";
    if ( $regionID > 0 )
    {
      $sql .= " AND ( pn_regionid = $regionID or pn_regionid = 0 ) ";
    }
    $bresult = $dbconn->Execute( $sql );
  }
  else
  {
    $mymodname=$GLOBALS['name'];
    $sql = "SELECT count(*) AS count FROM $pntable[banner] WHERE $column[type] = $type AND pn_title='$mymodname' OR $column[type] = $type AND pn_title='ALL'";
    if ( $regionID > 0 )
    {
      $sql .= " AND ( pn_regionid = $regionID or pn_regionid = 0 ) ";
    }
    $bresult = $dbconn->Execute( $sql);
  }
  list($numrows) = $bresult->fields;
  $bresult->Close();

  //todo: where it was

  // Get a random banner if exist any.
  if ( $numrows > 1 )
  {
    $numrows = $numrows-1;
    mt_srand((double)microtime()*1000000);
    $bannum = mt_rand(0, $numrows);
  }
  else
  {
    $bannum = 0;
  }

  $column = &$pntable['banner_column'];

  if (pnConfigGetVar('banners') != 2)
  {
    $sql = "SELECT pn_bid, pn_imageurl, pn_width, pn_height FROM $pntable[banner] WHERE pn_type='$type'";
    if ( $regionID > 0 )
    {
      $sql .= " AND ( pn_regionid = $regionID or pn_regionid = 0 ) ";
    }
    $sql .= " LIMIT $bannum,1";
    $bresult2 = $dbconn->Execute( $sql );
  }
  else
  {
    $sql = "SELECT pn_bid, pn_imageurl, pn_width, pn_height FROM $pntable[banner] WHERE pn_type='$type' AND pn_title='$mymodname' OR pn_type='$type' AND pn_title='ALL'";
    if ( $regionID > 0 )
    {
      $sql .= " AND ( pn_regionid = $regionID or pn_regionid = 0 ) ";
    }
    $sql .= " LIMIT $bannum,1";
    $bresult2 = $dbconn->Execute( $sql );
  }
  list($bid, $imageurl, $width, $height) = $bresult2->fields;

  // we no longer need this, free the resources
  $bresult2->Close();

  $myIP = pnConfigGetVar('myIP');
  $myhost = getenv("REMOTE_ADDR");
  if ($myIP == $myhost)
  {
    // do nothing
  }
  else
  {
    $dbconn->Execute("UPDATE $pntable[banner]
                      SET $column[impmade]=$column[impmade]+1
                      WHERE $column[bid]=".pnVarPrepForStore($bid)."");
  }

  if ($numrows > 0)
  {
    $aborrar = $dbconn->Execute("SELECT $column[cid],$column[imptotal],
                                        $column[impmade], $column[clicks],
                                        $column[date]
                                 FROM $pntable[banner]
                                 WHERE $column[bid]=".pnVarPrepForStore($bid)."");
    list($cid, $imptotal, $impmade, $clicks, $date) = $aborrar->fields;
    $aborrar->Close();

    // Check if this impression is the last one and print the banner
    if ($imptotal == $impmade)
    {
      $column = &$pntable['bannerfinish_column'];
      $dbconn->Execute("INSERT INTO $pntable[bannerfinish]
                        ( $column[bid], $column[cid], $column[impressions], $column[clicks], $column[datestart], $column[dateend] )
                        VALUES (NULL, '".pnVarPrepForStore($cid)."', '".pnVarPrepForStore($impmade)."', '".pnVarPrepForStore($clicks)."', '".pnVarPrepForStore($date)."', now())");
      $dbconn->Execute("DELETE FROM $pntable[banner] WHERE $column[bid]=".pnVarPrepForStore($bid)."");
    }

    if ( $type==1 or $type==2 or $type==0 or $type==6 or $type==7 )
    {
      $content = "<a href=\"banners.php?op=click&amp;bid=$bid\" target=\"_blank\">";

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

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



function pnFamilyResourceBannerDisplay($type, $secid, $artid )
{
  // test on config settings
  if (pnConfigGetVar('banners') == 0) return '&nbsp;';

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

  if ( $type == 4 )
  {
    return pnBannerDisplayMulti( & $dbconn, $type, & $pntable );
  }

  $regionID = getRegionIDCcookie();
  $column = &$pntable['banner_column'];


  if ( pnConfigGetVar('banners') != 2 )
  {
    $sql = "SELECT count(*) AS count FROM $pntable[banner] WHERE $column[type] = $type";
    $sql .= " and pn_bid <> 271";
    if ( $regionID > 0 )
    {
      $sql .= " AND ( pn_regionid = $regionID or pn_regionid = 0 ) ";
    }
    $bresult = $dbconn->Execute( $sql );
  }
  else
  {
    $mymodname=$GLOBALS['name'];
    $sql = "SELECT count(*) AS count FROM $pntable[banner] WHERE $column[type] = $type AND pn_title='$mymodname' OR $column[type] = $type AND pn_title='ALL'";
    if ( $regionID > 0 )
    {
      $sql .= " AND ( pn_regionid = $regionID or pn_regionid = 0 ) ";
    }
    $bresult = $dbconn->Execute( $sql);
  }

  list($numrows) = $bresult->fields;
  $bresult->Close();

  //todo: where it was

  // Get a random banner if exist any.
  if ( $numrows > 1 )
  {
    $numrows = $numrows-1;
    mt_srand((double)microtime()*1000000);
    $bannum = mt_rand(0, $numrows);
  }
  else
  {
    $bannum = 0;
  }

  $column = &$pntable['banner_column'];

  if ( $artid == 69 ) // todo: hack: Birthday Parties :: Great Party Locations
  {
    $sql = "SELECT pn_bid, pn_imageurl, pn_width, pn_height FROM $pntable[banner] WHERE pn_bid=271";
    $bresult2 = $dbconn->Execute( $sql );
  }
  else if ( $artid == 244 && $regionID == 2 )
  {
    $sql = "SELECT pn_bid, pn_imageurl, pn_width, pn_height FROM $pntable[banner] WHERE pn_bid=273";
    $bresult2 = $dbconn->Execute( $sql );
  }
  else if ( $artid == 4 && $regionID == 2 )
  {
    $sql = "SELECT pn_bid, pn_imageurl, pn_width, pn_height FROM $pntable[banner] WHERE pn_bid=274";
    $bresult2 = $dbconn->Execute( $sql );
  }
  else if ( $artid == 194 && $regionID == 2 )
  {
    $sql = "SELECT pn_bid, pn_imageurl, pn_width, pn_height FROM $pntable[banner] WHERE pn_bid=275";
    $bresult2 = $dbconn->Execute( $sql );
  }
  else
  {
    if (pnConfigGetVar('banners') != 2)
    {
      $sql = "SELECT pn_bid, pn_imageurl, pn_width, pn_height FROM $pntable[banner] WHERE pn_type='$type'";
      $sql .= " and pn_bid <> 271 and pn_bid <> 273 and pn_bid <> 274 and pn_bid <> 275";
      if ( $regionID > 0 )
      {
        $sql .= " AND ( pn_regionid = $regionID or pn_regionid = 0 ) ";
      }
      $sql .= " LIMIT $bannum,1";
      $bresult2 = $dbconn->Execute( $sql );
    }
    else
    {
      $sql = "SELECT pn_bid, pn_imageurl, pn_width, pn_height FROM $pntable[banner] WHERE pn_type='$type' AND pn_title='$mymodname' OR pn_type='$type' AND pn_title='ALL'";
      $sql .= " and pn_bid <> 271 and pn_bid <> 273 and pn_bid <> 274 and pn_bid <> 275";
      if ( $regionID > 0 )
      {
        $sql .= " AND ( pn_regionid = $regionID or pn_regionid = 0 ) ";
      }
      $sql .= " LIMIT $bannum,1";
      $bresult2 = $dbconn->Execute( $sql );
    }
  }
  list($bid, $imageurl, $width, $height) = $bresult2->fields;

  // we no longer need this, free the resources
  $bresult2->Close();

  $myIP = pnConfigGetVar('myIP');
  $myhost = getenv("REMOTE_ADDR");
  if ($myIP == $myhost)
  {
    // do nothing
  }
  else
  {
    $dbconn->Execute("UPDATE $pntable[banner]
                      SET $column[impmade]=$column[impmade]+1
                      WHERE $column[bid]=".pnVarPrepForStore($bid)."");
  }

  if ($numrows > 0)
  {
    $aborrar = $dbconn->Execute("SELECT $column[cid],$column[imptotal],
                                        $column[impmade], $column[clicks],
                                        $column[date]
                                 FROM $pntable[banner]
                                 WHERE $column[bid]=".pnVarPrepForStore($bid)."");
    list($cid, $imptotal, $impmade, $clicks, $date) = $aborrar->fields;
    $aborrar->Close();

    // Check if this impression is the last one and print the banner
    if ($imptotal == $impmade)
    {
      $column = &$pntable['bannerfinish_column'];
      $dbconn->Execute("INSERT INTO $pntable[bannerfinish]
                        ( $column[bid], $column[cid], $column[impressions], $column[clicks], $column[datestart], $column[dateend] )
                        VALUES (NULL, '".pnVarPrepForStore($cid)."', '".pnVarPrepForStore($impmade)."', '".pnVarPrepForStore($clicks)."', '".pnVarPrepForStore($date)."', now())");
      $dbconn->Execute("DELETE FROM $pntable[banner] WHERE $column[bid]=".pnVarPrepForStore($bid)."");
    }

    if ( $type==1 or $type==2 or $type==0 or $type==6 or $type==7 )
    {
      $content = "<a href=\"banners.php?op=click&amp;bid=$bid\" target=\"_blank\">";

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

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

?>