boardom Forum Index boardom
b2 message board
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Banner Ads

 
Post new topic   Reply to topic    boardom Forum Index -> Hacks
View previous topic :: View next topic  
Author Message
voltaic



Joined: 05 Jan 2004
Posts: 5
Location: The 9. 1. 6.

PostPosted: Mon Jan 05, 2004 9:23 pm    Post subject: Banner Ads Reply with quote

Here's a large but still pretty nice mod (not a hack ) for your b2 blog to have banners. Yeah sometimes they suck but sometimes you may want them. For example, at DIY Games we use them to promote various independent games for free to indie developers. Besides, it won't hurt to have more features for b2!

1. First, you need a new table in the database. You can put this code in the b2install.php file and run it:
Code:
$query = "CREATE TABLE `b2banners`(`ID` int(11) NOT NULL auto_increment, `description`varchar(50) NOT NULL default '', `url` varchar(100) NOT NULL default '', `filename` varchar(50) NOT NULL default '', `orientation` enum('horiz','vert') NOT NULL default 'horiz', `active` int(11) NOT NULL default '0', PRIMARY KEY  (`ID`) )";
$q = mysql_query($query) or mysql_doh("doh, can't create the table \"$tablebanners\" in the database.", $query, mysql_error());

or you can set it up as its own SQL query such as this mySQL table dump:
Code:
CREATE TABLE `b2banners` (
  `ID` int(11) NOT NULL auto_increment,
  `description` varchar(50) NOT NULL default '',
  `url` varchar(100) NOT NULL default '',
  `filename` varchar(50) NOT NULL default '',
  `orientation` enum('horiz','vert') NOT NULL default 'horiz',
  `active` int(11) NOT NULL default '0',
  PRIMARY KEY  (`ID`)
) TYPE=MyISAM;

Only do one of these to create the database table.

2. Second, add this line to b2config.php:
Code:
$tablebanners = 'b2banners';

You should put this near the other ones, such as $tableposts and $tableusers.


3. Third, add this line to b2menutop.txt:
Code:
3   b2banners.php   Banners

This will add the banner admin page to the b2 admin stuff.


4. Fourth, add the banner template function to b2template.functions.php:
Code:
function banner( $orient='horiz' ) {
   global $tablebanners;

   $sql = "select description,url,filename from $tablebanners where orientation='$orient' and active=1 ";
   $res = @mysql_query( $sql );
   $rows = mysql_num_rows( $res );

   $rnd = rand( 1, $rows );
   do {
      $info = mysql_fetch_array( $res );
      $rnd--;
   } while( $rnd );

   $descr = $info[ 'description' ];
   $url = $info[ 'url' ];
   $filename = $info[ 'filename' ];

   $buildbanner  = "<a href=\"$url\" target=\"_blank\" border=0>";
   $buildbanner .= "<img src=\"/images/banners/$filename\" alt=\"$descr\" border=0></a>";

   return $buildbanner;
}

I know Michel says mods shouldn't change the main b2 files, but I wanted this function to be usable from the templates and I didn't want to create another file on my server. Plus this is a pretty large new mod, not a small hack . You can put this in another file and use PHP's include() function if you like. Smile

5. Fifth, open up b2header.php and add the following Javascript function:
Code:
function launchbanner() {
window.open ("b2bannerupload.php", "b2bannerupload", "width=420,height=360,location=0,menubar=0,resizable=1,scrollbars=yes,status=1,toolbar=0");
}

You should place this function right after function launchupload() but before the </script> html tag.

6. Sixth, create a new folder under /images named /banners. This tool will store banner images there instead of the main image folder for ease of use. Smile

7. Seventh, create a new file named b2banners.php in the main b2 directory and put this in it:
Code:
<?php
$title = "Banners";
/* <Banners> */

function add_magic_quotes($array) {
   foreach ($array as $k => $v) {
      if (is_array($v)) {
         $array[$k] = add_magic_quotes($v);
      } else {
         $array[$k] = addslashes($v);
      }
   }
   return $array;
}

if (!get_magic_quotes_gpc()) {
   $HTTP_GET_VARS    = add_magic_quotes($HTTP_GET_VARS);
   $HTTP_POST_VARS   = add_magic_quotes($HTTP_POST_VARS);
   $HTTP_COOKIE_VARS = add_magic_quotes($HTTP_COOKIE_VARS);
}

$b2varstoreset = array('action','standalone','cat');
for ($i=0; $i<count($b2varstoreset); $i += 1) {
   $b2var = $b2varstoreset[$i];
   if (!isset($$b2var)) {
      if (empty($HTTP_POST_VARS["$b2var"])) {
         if (empty($HTTP_GET_VARS["$b2var"])) {
            $$b2var = '';
         } else {
            $$b2var = $HTTP_GET_VARS["$b2var"];
         }
      } else {
         $$b2var = $HTTP_POST_VARS["$b2var"];
      }
   }
}

switch($action) {

case "addban":

   $standalone = 1;
   require_once("./b2header.php");

   if ($user_level < 3)
   die ("Cheatin' uh ?");

   $ban_descr=addslashes($HTTP_POST_VARS["ban_descr"]);
   $ban_url=$HTTP_POST_VARS["ban_url"];
   $ban_filename=$HTTP_POST_VARS["ban_filename"];
   $ban_orient=$HTTP_POST_VARS["ban_orient"];

   $query ="INSERT INTO $tablebanners (description,url,filename,orientation,active) VALUES ";
   $query.="('$ban_descr','$ban_url','$ban_filename','$ban_orient',1)";
   $result=mysql_query($query) or die("Couldn't add banner <b>$ban_descr</b>");

   header("Location: b2banners.php");

break;

case "Delete":

   $standalone = 1;
   require_once("./b2header.php");

   if ($user_level < 3)
   die ("Cheatin' uh ?");

   $ban_id=$HTTP_POST_VARS["ban_id"];

   $query="SELECT filename,description from $tablebanners where ID=$ban_id";
   $result=mysql_query($query) or die("Couldn't delete banner #<b>$ban_id</b>".mysql_error());
   $result_a=mysql_fetch_array( $result );
   $ban_fname=$result_a['filename'];
   $ban_descr=$result_a['description'];

   if( unlink( $fileupload_realpath."/banners/".$ban_fname ) ) {
      $query="DELETE FROM $tablebanners WHERE ID=$ban_id";
      $result=mysql_query($query) or die("Couldn't delete banner <b>$ban_descr</b>".mysql_error());
   } else {
      die( "Couldn't delete filename <b>$ban_fname</b>" );
   }
   header("Location: b2banners.php");

break;

case "Edit":

   require_once ("./b2header.php");

   $ban_id=$HTTP_POST_VARS["ban_id"];
   $query="SELECT * from $tablebanners where ID=$ban_id";
   $result=mysql_query($query) or die("Couldn't get banner #<b>$ban_id</b>".mysql_error());
   $result_a=mysql_fetch_array( $result );
   $ban_descr=addslashes( $result_a['description'] );
   $ban_url=$result_a['url'];
   $ban_fname=$result_a['filename'];
   $ban_orient=$result_a['orientation'];
   $ban_active=$result_a['active'];

   echo $blankline;
   echo $tabletop;
   ?>

   <p><b>Edit</b> banner settings:<br />
   </p>
   <p>
   <form name="renameban" action="b2banners.php" method="post">
      <b>Banner information:</b><br />
      <input type="hidden" name="action" value="editedban" />
      <input type="hidden" name="ban_id" value="<?php echo $ban_id ?>" />
      Description: <input type="text" size=\"50\" name="ban_descr" value="<?php echo $ban_descr ?>" /><br /><br />
      URL/homepage: <input type="text" size=\"50\" name="ban_url" value="<?php echo $ban_url ?>" /><br /><br />
      <?php
         if($ban_orient=='horiz')$bhoriz="checked";
         else $bhoriz=" ";
         if($ban_orient=='vert')$bvert="checked";
         else $bvert=" ";
         if($ban_active==1)$bactive="checked";
      ?>
      Orientation:
         <input type="radio" name="ban_orient" value="horiz" <?php echo $bhoriz; ?>> Horizontal  
         <input type="radio" name="ban_orient" value="vert" <?php echo $bvert; ?>> Vertical<br /><br />
      Currently active: <input type="checkbox" name="ban_active" <?php echo $bactive; ?>> <br /><br />
      <input type="submit" name="submit" value="Edit it !" class="search" />
   </form>

   <?php
   echo $tablebottom;

break;

case "editedban":

   $standalone = 1;
   require_once("./b2header.php");

   if ($user_level < 3)
   die ("Cheatin' uh ?");

   $ban_id=$HTTP_POST_VARS["ban_id"];
   $ban_descr=addslashes($HTTP_POST_VARS["ban_descr"]);
   $ban_url=$HTTP_POST_VARS["ban_url"];
   $ban_filename=$HTTP_POST_VARS["ban_filename"];
   $ban_orient=$HTTP_POST_VARS["ban_orient"];

   $ban_active=0;
   if( $HTTP_POST_VARS["ban_active"] == "on" )
      $ban_active=1;

   $query ="UPDATE $tablebanners SET description='$ban_descr',url='$ban_url', ";
   $query.="orientation='$ban_orient',active=$ban_active WHERE ID=$ban_id";

   $result=mysql_query($query) or die("Couldn't edit banner <b>$ban_descr</b>: ".mysql_error().$query);

   header("Location: b2banners.php");

break;

default:

   $standalone=0;
   require_once ("./b2header.php");
   if ($user_level < 3) die("You have no right to edit these banners.<br>");

   echo $blankline;
      echo $tabletop;
   ?>

   <table width="" cellpadding="5" cellspacing="0">
   <form></form>
   <tr>
   <td>
   <form name="bans" method="post">
   <b>Manage</b> banner ads:<br />

   <p>
   <b>Add</b> a banner:<br />
      <form name="addban" action="b2banners.php" method="post">
         <input type="hidden" name="action" value="addban" />
         Description: <input type="text" name="ban_descr" /><br /><br />
         URL/homepage: <input type="text" name="ban_url" /><br /><br />
         Image filename: <input type="text" name="ban_filename" />
            <input type="button" value="upload image" onclick="launchbanner();" class="search" /><br /><br />
         Orientation:
            <input type="radio" name="ban_orient" value="horiz" selected> Horizontal  
            <input type="radio" name="ban_orient" value="vert"> Vertical<br /><br />
         <input type="submit" name="submit" value="Add it !" class="search" /></form></td></tr></table>
   </p>

   <?php
   echo $tablebottom;
   echo "<br>\n";
   echo $tabletop;
   $query="SELECT * FROM $tablebanners where orientation='horiz' ORDER BY ID";
   $result=mysql_query($query);
   while($row = @mysql_fetch_object($result)) {
      echo "<p><form name=\"editban\" action=\"b2banners.php\" method=\"post\">";
      echo "<input type=\"hidden\" name=\"ban_id\" value=\"".$row->ID."\">\n";
      echo "<img src=\"./images/banners/".$row->filename."\"><br />";
      echo $row->description." located at <a href=\"".$row->url."\" target=\"_blank\">".$row->url."</a><br />";
      echo "<b>";
      if( !($row->active) ) echo "IN";
      echo "ACTIVE</b><br />\n";

      echo "<input type=\"submit\" name=\"action\" value=\"Edit\" class=\"search\" />         ";
      echo "<input type=\"submit\" name=\"action\" value=\"Delete\" class=\"search\" onclick=\"return confirm('You are about to permanently delete the banner \'".$row->description."\'\\n  \'Cancel\' to stop, \'OK\' to delete.')\" />\n";
      echo "</form><hr></p>\n";
   }
   echo $tablebottom;

   echo "<br>\n";
   echo $tabletop;
   $query="SELECT * FROM $tablebanners where orientation='vert' ORDER BY ID";
   $result=mysql_query($query);
   while($row = @mysql_fetch_object($result)) {
      echo "<p><form name=\"editban\" action=\"b2banners.php\" method=\"post\">";
      echo "<input type=\"hidden\" name=\"ban_id\" value=\"".$row->ID."\">\n";
      echo "<img src=\"./images/banners/".$row->filename."\"><br />";
      echo $row->description." located at <a href=\"".$row->url."\" target=\"_blank\">".$row->url."</a><br />";
      echo "<b>";
      if( !($row->active) ) echo "IN";
      echo "ACTIVE</b><br />\n";

      echo "<input type=\"submit\" name=\"action\" value=\"Edit\" class=\"search\" />         ";
      echo "<input type=\"submit\" name=\"action\" value=\"Delete\" class=\"search\" onclick=\"return confirm('You are about to permanently delete the banner \'".$row->description."\'\\n  \'Cancel\' to stop, \'OK\' to delete.')\" />\n";
      echo "</form><hr></p>\n";
   }
   echo $tablebottom;
?>

<br />

   <?php
break;
}

/* </Banners> */
include($b2inc."/b2footer.php"); ?>

This is the admin page for banner stuff.

8. Eighth, create a new file named b2bannerupload.php in the main b2 directory and put this in it:
Code:
<?php
/* b2 Banner Upload - original hack by shockingbird.com */
/* modified by VOLTMAN! to do banners  */

$standalone="1";
require_once("./b2header.php");

if ($user_level < 3) //Checks to see if user has logged in
die ("Cheatin' uh ?");

if (!$use_fileupload) //Checks if file upload is enabled in the config
die ("The admin disabled this function");

?><html>
<head>
<title>b2 > upload banner images</title>
<link rel="stylesheet" href="<?php echo $b2inc; ?>/b2.css" type="text/css">
<?php if ($use_spellchecker) { ?>
<script type="text/javascript" language="javascript" src="<?php echo $spch_url; ?>"></script><?php } ?>
<style type="text/css">
<!--
body {
   background-image: url('<?php
if ($is_gecko || $is_macIE) {
?>b2-img/bgbookmarklet1.gif<?php
} else {
?>b2-img/bgbookmarklet3.gif<?php
}
?>');
   background-repeat: no-repeat;
   margin: 30px;
}
<?php
if (!$is_NS4) {
?>
textarea,input,select {
   background-color: white;
/*<?php if ($is_gecko || $is_macIE) { ?>
   background-image: url('b2-img/bgbookmarklet.png');
<?php } elseif ($is_winIE) { ?>
   background-color: #cccccc;
   filter: alpha(opacity:80);
<?php } ?>
*/  border-width: 1px;
   border-color: #cccccc;
   border-style: solid;
   padding: 2px;
   margin: 1px;
}
<?php if (!$is_gecko) { ?>
.checkbox {
   border-width: 0px;
   border-color: transparent;
   border-style: solid;
   padding: 0px;
   margin: 0px;
}
.uploadform {
   background-color: white;
<?php if ($is_winIE) { ?>
   filter: alpha(opacity:100);
<?php } ?>
   border-width: 1px;
   border-color: #333333;
   border-style: solid;
   padding: 2px;
   margin: 1px;
   width: 265px;
   height: 24px;
}
<?php } ?>
<?php
}
?>
-->
</style>
<script type="text/javascript">
<!-- // idocs.com's popup tutorial rules !
function targetopener(blah, closeme, closeonly) {
   if (! (window.focus && window.opener))return true;
   window.opener.focus();
   if (! closeonly)window.opener.document.post.content.value += blah;
   if (closeme)window.close();
   return false;
}
//-->
</script>
</head>
<body>

<table align="center" width="100%" height="100%" cellpadding="15" cellspacing="0" border="1" style="border-width: 1px; border-color: #cccccc;">
   <tbody>
   <tr>
   <td valign="top" style="background-color: transparent; <?php if ($is_gecko || $is_macIE) { ?>background-image: url('b2-img/bgbookmarklet.png');<?php } elseif ($is_winIE) { ?>background-color: #cccccc; filter: alpha(opacity:60);<?php } ?>;">
<?php

if (!$HTTP_POST_VARS["submit"]) {
   $i = explode(" ",$fileupload_allowedtypes);
   $i = implode(", ",array_slice($i, 1, count($i)-2));
   ?>
   <p><strong>File upload</strong></p>
   <p>You can upload files of type:<br /><em><?php echo $i ?></em></p>
   <p>The maximum size of the file should be:<br /><em><?php echo $fileupload_maxk ?> KB</em></p>
   <form action="b2bannerupload.php" method="post" enctype="multipart/form-data">
   <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $fileupload_maxk*1024 ?>" />
   <input type="file" name="img1" size="30" class="uploadform" />
   <br /><br />
   <input type="submit" name="submit" value="upload !" class="search" />
   </form>
   </td>
   </tr>
   </tbody>
</table>
</body>
</html><?php die();
}

?>

<?php  //Makes sure they choose a file

//print_r($HTTP_POST_FILES);
//die();

if (!empty($HTTP_POST_VARS)) { //$img1_name != "") {

   $imgalt = (isset($HTTP_POST_VARS['imgalt'])) ? $HTTP_POST_VARS['imgalt'] : $imgalt;

   $img1_name = (strlen($imgalt)) ? $HTTP_POST_VARS['imgalt'] : $HTTP_POST_FILES['img1']['name'];
   $img1_type = (strlen($imgalt)) ? $HTTP_POST_VARS['img1_type'] : $HTTP_POST_FILES['img1']['type'];

   $imgtype = explode(".",$img1_name);
   $imgtype = " ".$imgtype[count($imgtype)-1]." ";

   if (!ereg(strtolower($imgtype), strtolower($fileupload_allowedtypes))) {
       die("File $img1_name of type $imgtype is not allowed.");
   }

   if (strlen($imgalt)) {
      $pathtofile = $fileupload_realpath."/banners/".$imgalt;
      $img1 = $HTTP_POST_VARS['img1'];
   } else {
      $pathtofile = $fileupload_realpath."/banners/".$img1_name;
      $img1 = $HTTP_POST_FILES['img1']['tmp_name'];
   }

   // makes sure not to upload duplicates, rename duplicates
   $i = 1;
   $pathtofile2 = $pathtofile;
   $tmppathtofile = $pathtofile2;
   $img2_name = $img1_name;

   while (file_exists($pathtofile2)) {
       $pos = strpos($tmppathtofile, '.'.trim($imgtype));
       $pathtofile_start = substr($tmppathtofile, 0, $pos);
       $pathtofile2 = $pathtofile_start.'_'.zeroise($i++, 2).'.'.trim($imgtype);
       $img2_name = explode('/', $pathtofile2);
       $img2_name = $img2_name[count($img2_name)-1];
   }

   if (file_exists($pathtofile) && !strlen($imgalt)) {
      $i = explode(" ",$fileupload_allowedtypes);
      $i = implode(", ",array_slice($i, 1, count($i)-2));
      move_uploaded_file($img1, $pathtofile2)
       or die("Couldn't Upload Your File to $pathtofile2.");
   
   // duplicate-renaming function contributed by Gary Lawrence Murphy
   ?>
   <p><strong>Duplicate File?</strong></p>
   <p><b><em>The filename '<?php echo $img1_name; ?>' already exists!</em></b></p>
   <p> filename '<?php echo $img1; ?>' moved to '<?php echo "$pathtofile2 - $img2_name"; ?>'</p>
   <p>Confirm or rename:</p>
   <form action="b2bannerupload.php" method="post" enctype="multipart/form-data">
   <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $fileupload_maxk*1024 ?>" />
   <input type="hidden" name="img1_type" value="<?php echo $img1_type;?>" />
   <input type="hidden" name="img1_name" value="<?php echo $img2_name;?>" />
   <input type="hidden" name="img1" value="<?php echo $pathtofile2;?>" />
   Alternate name:<br /><input type="text" name="imgalt" size="30" class="uploadform" value="<?php echo $img2_name;?>" /><br />
   <br />
   <input type="submit" name="submit" value="confirm !" class="search" />
   </form>
   </td>
   </tr>
   </tbody>
</table>
</body>
</html><?php die();

   }

   if (!strlen($imgalt)) {
      move_uploaded_file($img1, $pathtofile) //Path to your images directory, chmod the dir to 777
       or die("Couldn't Upload Your File to $pathtofile.");
   } else {
      rename($img1, $pathtofile)
      or die("Couldn't Upload Your File to $pathtofile.");
   }

}


if ( ereg('image/',$img1_type)) {
   $piece_of_code = "$img1_name";
} else {
   $piece_of_code = "$img1_name";
};

?>

<p><strong>File uploaded !</strong></p>
<p>Your file <b><?php echo "$img1_name"; ?></b> was uploaded successfully !</p>
<p>Here's the code to display it:</p>
<p><form>
<!--<textarea cols="25" rows="3" wrap="virtual"><?php echo "<img src="$fileupload_url/$img1_name" border="0" alt="" />"; ?></textarea>-->
<input type="text" name="imgpath" value="<?php echo $piece_of_code; ?>" size="38" style="padding: 5px; margin: 2px;" /><br />
<?php /*
<input type="button" name="close" value="Add the code to your post !" class="search" onClick="targetopener('<?php echo $piece_of_code; ?>')" style="margin: 2px;" />
*/ ?>
</form>
</p>
<p><strong>Image Details</strong>: <br />
name:
<?php echo "$img1_name"; ?>
<br />
size:
<?php echo round($img1_size/1024,2); ?> KB
<br />
type:
<?php echo "$img1_type"; ?>
</p>
<p align="right">
<form>
<input type="button" name="close" value="Close this window" class="search" onClick="window.close()" />
</form>
</p>
</td>
</tr>
</tbody>
</table>

</body>

This is very similar to the regular upload popup but makes sure that the images go in the proper banner directory.

9. Ninth and finally, upload some banner ads! You can use the admin tool to upload, delete, set in/active, change the URL, etc. In your templates, to show a banner simply call the banner() function like so:
Code:
<?php echo banner( "horiz" ); ?>

To display a horizontal banner or
Code:
<?php echo banner( "vert" ); ?>

if you want vertical banners on your site.

Reply with any comments or bugs you find so I can improve the banner mod! Notice: This code is released under the terms of the GNU General Public License for use by anyone for anything. Do with it as you see fit. I especially welcome Michel to include it with future versions of b2 if he likes.
Back to top
View user's profile Send private message Visit poster's website
voltaic



Joined: 05 Jan 2004
Posts: 5
Location: The 9. 1. 6.

PostPosted: Mon Jan 05, 2004 9:39 pm    Post subject: Reply with quote

For those interested, here's a screenshot of what the banner admin page looks like with some example horizontal banners:

Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    boardom Forum Index -> Hacks All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB 2 © 2001, 2002 phpBB Group