<?php
/*
Plugin Name: Adsense Injection
Plugin URI: http://wordpress-plugins.biggnuts.com/adsense-injection/
Description: Inserts Adsense into your blog.
Version: 1.0
Author: Dax "The Hammer" Herrera
Author URI: http://www.biggnuts.com
*/

/* UPDATES

*/

function ai_add_options(){
  if(function_exists('add_options_page')){
    add_options_page('Adsense', 'Adsense', 9, basename(__FILE__), 'ai_options_subpanel');
  }
}

switch($_POST['action']){
case 'Save':

  update_option('ai_color_border', $_POST['ai_color_border']);
  update_option('ai_color_link', $_POST['ai_color_link']);
  update_option('ai_color_bg', $_POST['ai_color_bg']);
  update_option('ai_color_text', $_POST['ai_color_text']);
  update_option('ai_color_url', $_POST['ai_color_url']);

  update_option('ai_lra', $_POST['ai_pos']);
  update_option('ai_nads', $_POST['nads']);
  update_option('ai_text_only', false);

  update_option('ai_client', $_POST['ai_client']);
  update_option('ai_channel', $_POST['ai_channel']);
  
  if($_POST['234x60'] == "on")
    update_option('ai_234x60', "checked=on");
  else 
    update_option('ai_234x60', "");

  if($_POST['125x125'] == "on")
    update_option('ai_125x125', "checked=on");
  else 
    update_option('ai_125x125', "");

  if($_POST['180x150'] == "on")
    update_option('ai_180x150', "checked=on");
  else 
    update_option('ai_180x150', "");

  if($_POST['120x240'] == "on")
    update_option('ai_120x240', "checked=on");
  else 
    update_option('ai_120x240', "");

  if($_POST['200x200'] == "on")
    update_option('ai_200x200', "checked=on");
  else 
    update_option('ai_200x200', "");

  if($_POST['300x250'] == "on")
    update_option('ai_300x250', "checked=on");
  else 
    update_option('ai_300x250', "");

  if($_POST['250x250'] == "on")
    update_option('ai_250x250', "checked=on");
  else 
    update_option('ai_250x250', "");

  break;
}

function ai_options_subpanel(){
  $ad_client = get_option('ai_client');
  $ad_channel = get_option('ai_channel');
  $lra = get_option('ai_lra');
  $nads = get_option('ai_nads');
  $text_only = get_option('ai_text_only');

  $ai_color_border = get_option('ai_color_border');
  $ai_color_link = get_option('ai_color_link');
  $ai_color_bg = get_option('ai_color_bg');
  $ai_color_text = get_option('ai_color_text');
  $ai_color_url = get_option('ai_color_url');
?>
<div class="wrap"> 
  <h2><?php _e('Adsense Injection!', 'wpai') ?></h2> 
  <form name="form1" method="post">
	<input type="hidden" name="stage" value="process" />

	<fieldset class="options">
		<legend><?php _e('Options', 'wpai') ?></legend>
		<table width="100%" cellspacing="2" cellpadding="5" class="editform" > 
		  <tr align="left" valign="top">
			<th width="30%" scope="row" style="text-align: left"><?php _e('Adsense ID', 'wpai') ?></th>
			<td><input type="text" name="ai_client" id="ai_client" style="width: 80%;" cols="50" value="<?php echo $ad_client; ?>"></td></tr>
		  <tr align="left" valign="top">
			<th width="30%" scope="row" style="text-align: left"><?php _e('Adsense Channel', 'wpai') ?></th>
			<td><input type="text" name="ai_channel" id="ai_channel" style="width: 80%;" cols="50" value="<?php echo $ad_channel ; ?>"></td></tr>
		  <tr valign="top">
			<th width="30%" scope="row" style="text-align: left"><?php _e('Ad Formats To Randomize', 'wpai') ?></th>

			<td>
			<INPUT TYPE=CHECKBOX NAME="234x60" <?php echo get_option('ai_234x60'); ?>>234x60<BR>
			<INPUT TYPE=CHECKBOX NAME="125x125" <?php echo get_option('ai_125x125'); ?>>125x125<BR>
			<INPUT TYPE=CHECKBOX NAME="180x150" <?php echo get_option('ai_180x150'); ?>>180x150<BR>
			<INPUT TYPE=CHECKBOX NAME="120x240" <?php echo get_option('ai_120x240'); ?>>120x240<BR>
			<INPUT TYPE=CHECKBOX NAME="200x200" <?php echo get_option('ai_200x200'); ?>>200x200<BR>
			<INPUT TYPE=CHECKBOX NAME="250x250" <?php echo get_option('ai_250x250'); ?>>250x250<BR>
			<INPUT TYPE=CHECKBOX NAME="300x250" <?php echo get_option('ai_300x250'); ?>>300x250<BR>
      </td>
      </tr>
      
      <tr align="left" valign="top">
        <th width="30%" scope="row" style="text-align: left"><?php _e('Colors', 'wpai') ?></th>
            <td>
			<table><tr><td>Color Border:</td><td><input type="text" name="ai_color_border" id="ai_color_border" cols="50" value="<?php echo $ai_color_border; ?>"></td></tr>
			<tr><td>Color Link:</td><td><input type="text" name="ai_color_link" id="ai_color_link" cols="50" value="<?php echo $ai_color_link; ?>"></td></tr>
			<tr><td>Color Background:</td><td><input type="text" name="ai_color_bg" id="ai_color_bg" cols="50" value="<?php echo $ai_color_bg; ?>"></td></tr>
			<tr><td>Color Text:</td><td><input type="text" name="ai_color_text" id="ai_color_text" cols="50" value="<?php echo $ai_color_text; ?>"></td></tr>
			<tr><td>Color URL:</td><td><input type="text" name="ai_color_url" id="ai_color_url" cols="50" value="<?php echo $ai_color_url; ?>"></td></tr>
			</tr></table>
			</td></tr>
	
		<tr valign="top">
			<th width="30%" scope="row" style="text-align: left"><?php _e('Number of Ads to Show', 'wpai') ?></th>
      <td>
      <select name='nads'>
      <option value="0" <?php if($nads == 0) echo "SELECTED"; ?> >0
      <option value="1" <?php if($nads == 1) echo "SELECTED"; ?> >1
      <option value="2" <?php if($nads == 2) echo "SELECTED"; ?> >2
      <option value="3" <?php if($nads == 3) echo "SELECTED"; ?> >3
      </select>

      </td> 
    </tr>

    <tr valign="top">
			<th width="30%" scope="row" style="text-align: left"><?php _e('Ad Positioning', 'wpai') ?></th>
      <td>
      <select name='ai_pos'>
      <option value="random" <?php if($lra == 'random') echo "SELECTED"; ?> >random
      <option value="left" <?php if($lra == 'left') echo "SELECTED"; ?> >left
      <option value="right" <?php if($lra == 'right') echo "SELECTED"; ?> >right
      </select></td> 
    <tr>
      <td align=right colspan=5>
      <input type="submit" name="action" value="<?php _e('Save', 'wpai') ?>" />
      </td>
    </tr>

		</table>
	</fieldset>
	 
  </form> 
</div>
<?php 

}

add_action('admin_menu', 'ai_add_options');

function ai_install() {

  if(get_option('ai_client') == "") {
    update_option('ai_lra', "random");
    update_option('ai_nads', 3);
    update_option('ai_text_only', false);
  }
}
if (isset($_GET['activate']) && $_GET['activate'] == 'true') {
   add_action('init', 'ai_install');
}

function ai_pickalign($tag){
  if($tag == "left")
    return '<div style="float: left; margin: 16px;">';
  if($tag == "right")
    return '<div style="float: right; margin: 16px;">';
  else
    return ai_pickalign(rand(0,10)<5?"left":"right");
}

function ai_picksize(){
  $sizes = array();
  if(strlen(get_option('ai_234x60')))
    $sizes[] = "234x60";
  if(strlen(get_option('ai_125x125')))
    $sizes[] = "125x125";
  if(strlen(get_option('ai_180x150')))
    $sizes[] = "180x150";
  if(strlen(get_option('ai_120x240')))
    $sizes[] = "120x240";
  if(strlen(get_option('ai_200x200')))
    $sizes[] = "200x200";
  if(strlen(get_option('ai_300x250')))
    $sizes[] = "300x250";
  if(strlen(get_option('ai_250x250')))
    $sizes[] = "250x250";

  return $sizes[rand(0, sizeof($sizes)-1)];
}

function ai_genadcode($size = 0,$color = 0)
{
  if (!$size)
    $size = ai_picksize();
  $width = substr($size, 0, 3);
  $height = substr($size, 4, 3);
    
  $client = get_option('ai_client');
  $channel = get_option('ai_channel');
  
  if ($color)
  {
    $color_border = $color;
    $color_bg = $color;
  }
  else
  {
    $color_border = get_option('ai_color_border');
    $color_bg = get_option('ai_color_bg');
  }

  $color_link = get_option('ai_color_link');
  $color_text = get_option('ai_color_text');
  $color_url = get_option('ai_color_url');
  
  $retstr = '<script type="text/javascript"><!--
google_ad_client = "'.$client.'";
google_alternate_color = "FFFFFF";
google_ad_width = '.$width.';
google_ad_height = '.$height.';
google_ad_format = "'.$size.'_as";
google_ad_type = "text_image";
google_ad_channel ="'.$channel.'";
google_color_border = "'.$color_border.'";
google_color_link = "'.$color_link.'";
google_color_bg = "'.$color_bg.'";
google_color_text = "'.$color_text.'";
google_color_url = "'.$color_url.'";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>';

  return $retstr;
}

$ai_adsused = 0;

function ai_the_content($content){
  // Don't display an ad when we're getting the RSS feed.
  global $feed;
  if ($feed)
    return $content;

  // Otherwise, display an ad.
  global $ai_adsused;

  if($ai_adsused < get_option('ai_nads'))
  {
    $poses = array();
    $lastpos = -1;
    while(strpos($content, "<p>", $lastpos+1) !== false){
      $lastpos = strpos($content, "<p>", $lastpos+1);
      $poses[] = $lastpos;
    }
    
    //cut the doc in half so the ads don't go past the end of the article.  It could still happen, but what the hell
    $half = sizeof($poses)/2;
    while(sizeof($poses) > $half)
      array_pop($poses);
    $pickme = $poses[rand(0, sizeof($poses)-1)];
    
    $replacewith = ai_pickalign(get_option('ai_lra'));
    $replacewith .= ai_genadcode()."</div>";
    
    $content = substr_replace($content, $replacewith."<p>", $pickme, 3);
    $ai_adsused++;
  }

  return $content;
}

add_filter('the_content', 'ai_the_content');

?>
