<?php
/*************************************************************
* This script is developed by Arturs Sosins aka ar2rsawseen, http://webcodingeasy.com
* Fee free to distribute and modify code, but keep reference to its creator
*
* Pop under class allows you to implement pop under advertising to your website.
* You can customize probability of ads to appear, time for how long to show advertisement and much more.
* Language and template files included to provide full outlook customization
*
* For more information, examples and online documentation visit:
* http://webcodingeasy.com/PHP-classes/Implement-pop-under-advertising-to-your-site-using-pop-under-class
**************************************************************/
class pop_under
{
//timer in seconds
private $timer = 10;
//show exit if true, else wait whole time
private $show_exit = true;
//probability of showing pop under in percents
private $probability = 10;
//callback function
private $callback = "";
//url array
private $urls = array();
//period how often to show popunder ads to the same user in seconds
//if sessions are used instead of cookies, then max time depends on how long session is maintained
//using cookies period of showing ad will be more precise
//default value 1 day
private $period = 86400;
//mark people who viewed ad and don't show any ads to them for a period of time set in period property
//if false, advertisments will be showed only based on probability
private $use_mark = true;
//error storage
private $errors = array();
//language array
private $lang = array
(
"add_to_title" => "Advertisment",
"seconds_left" => "seconds left",
"redirecting" => "Redirecting",
"waiting" => "Waiting for page to load",
"skip" => "Continue to page"
);
function __construct(){
//If no session exists, start new
if(session_id()=='')
{
session_start();
}
if(file_exists("./language.php"))
{
//including language file
include("./language.php");
}
if(isset($lang) && is_array($lang))
{
$arr = array_diff(array_keys($this->lang), array_keys($lang));
if(empty($arr))
{
$this->lang = $lang;
}
else
{
$this->errors[] = "Incorrect language array. Default language array will be used";
}
}
else
{
$this->errors[] = "No language array provided. Default language array will be used";
}
}
//get errors fro debugging
public function get_errors(){
if(!empty($this->errors))
{
return $this->errors;
}
else
{
return false;
}
}
//set timer for how long to show the advertisment
public function set_timer($sec){
if($sec > 0)
{
$this->timer = $sec;
}
else
{
$this->errors[] = "Timer can not be negative. Previous or default value will be used";
}
}
//set probability in from 1 to 100 in percent to show advertisment
//20 percent is a probability of showing advertisment every 5th pageload
public function set_probability($percent){
if($percent > 1 && $percent <= 100)
{
$this->probability = $percent;
}
else
{
$this->errors[] = "Porbability can be between 1 and 100 percents. Previous or default value will be used";
}
}
//bool if show skip advertisment option
public function set_exit($bool){
if($bool)
{
$this->show_exit = true;
}
else
{
$this->show_exit = false;
}
}
//bool to mark people who saw advertisment or show only based on propability
public function use_mark($bool){
if($bool)
{
$this->use_mark = true;
}
else
{
$this->use_mark = false;
}
}
//set period of time to not show advertisment to marked users
public function set_period($sec){
if($sec > 0)
{
$this->period = $sec;
}
else
{
$this->errors[] = "Period of time can not be negative. Previous or default value will be used";
}
}
//mark user who saw advertisment
private function mark(){
$now = time();
$period = $now + $this->period;
if($this->use_mark)
{
if(!isset($_COOKIE['pop_under']) || $_COOKIE['pop_under'] < $now)
{
setcookie('pop_under', $period, $period);
}
}
}
//check if visitor is marked
private function is_marked(){
$now = time();
if($this->use_mark)
{
if(isset($_COOKIE['pop_under']) && $_COOKIE['pop_under'] > $now)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
//add url to show wit unique identifier (optional)
//unique identifiers are used to pass as parameter to callback function, to notify that this advertisments with this identifier was shown
//if no unique identifier provided, then one will be created
//if existing identifier will be provided, then url will be over writen
public function add_url($src, $title = "", $id = ""){
if($id == "")
{
$size = sizeof($this->urls);
$this->urls[$size]['src'] = $src;
$this->urls[$size]['title'] = $title;
}
else
{
$this->urls[$id]['src'] = $src;
$this->urls[$id]['title'] = $title;
}
}
//set callback function to notify which advertisment was shown to visitors
/*callback function will be called with these parameters
name_of_function($id, $skip)
where:
name_of_function is name of function provided as callback function
$id - unique identifier of advertisment provided when adding url of advertisments
$skip - bool if visitor clicked to skip ads, false - if visitor watched whole ad
*/
public function set_callback($func_name){
if(is_callable($func_name))
{
$this->callback = $func_name;
}
else
{
$this->errors[] = "Function with specified name: $func_name does not exist";
}
}
//gets users edited templates or uses default one
private function get_template(){
$constants = array
(
"[@title]" => "<title>".$this->urls[$_SESSION['pop_under_returned']]['title'].' '.$this->lang['add_to_title']."</title>",
"[@script]" => '<script type="text/ecmascript">
// <![CDATA[
function skip_page()
{
var attachform = document.getElementById("seconds");
var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("action", "");
var inp = document.createElement("input");
inp.setAttribute("type", "hidden");
inp.setAttribute("value", "true");
inp.setAttribute("name", "pop_under_skip");
form.appendChild(inp);
attachform.appendChild(form);
form.submit();
}
var seconds = "'.($this->timer).'";
function redirect()
{
window.location = "'.$_SERVER['REQUEST_URI'].'";
}
function update ()
{
if(seconds > 0)
{
seconds--;
document.getElementById("seconds").innerHTML = seconds + " '.$this->lang['seconds_left'].'";
setTimeout("update();", 1000);
}
else
{
document.getElementById("seconds").innerHTML = "'.$this->lang['redirecting'].'";
setTimeout("redirect();", 1000);
}
}
window.onload = function ()
{
setTimeout("update();", 1000);
}
// ]]>
</script>',
"[@popunder_text]" => $this->urls[$_SESSION['pop_under_returned']]['title'],
"[@waiting]" => $this->lang['waiting'],
"[@popunder_src]" => $this->urls[$_SESSION['pop_under_returned']]['src'],
"[@request_uri]" => $_SERVER['REQUEST_URI'],
"[@skip]" => $this->lang['skip'],
"[@exit]" => "<a href='".$_SERVER['REQUEST_URI']."' onclick='skip_page(); return false'>".$this->lang['skip']."</a>"
);
if(!$this->show_exit)
{
$constants['[@exit]'] = "";
}
if(file_exists("./popunder_template.php"))
{
$template = file_get_contents("./popunder_template.php");
}
else
{
$template = "<html>
<head>
[@title]
[@script]
</head>
<body>
<table style='height:80px; width:100%; text-align: center; border: 0;'>
<tr>
<td style='width: 70%'>
<h3>[@popunder_text]</h3>
</td>
<td style='width: 30%'>
<p id='seconds'>[@waiting]</p>
<p>[@exit]</p>
</td>
</tr>
</table>
<iframe name='content' src='[@popunder_src]' width='100%' height='90%'/>
<noframes>
<p><a href='[@request_uri]'>[@skip]</a></p>
</noframes>
</body>
</html>";
}
foreach($constants as $const => $value)
{
$template = str_replace($const, $value, $template);
}
return $template;
}
//show advertisments
public function show_ads(){
$now = time();
if(isset($_POST['pop_under_skip']))
{
$skip = true;
$_SESSION['pop_under_time'] = 0;
}
else
{
$skip = false;
}
if(!empty($this->urls))
{
if(isset($_SESSION['pop_under_returned']) && $_SESSION['pop_under_time'] < $now)
{
$this->mark();
if($this->callback != '')
{
$function = $this->callback;
$function($_SESSION['pop_under_returned'], $skip);
}
unset($_SESSION['pop_under_returned']);
unset($_SESSION['pop_under_time']);
}
else if(!$this->is_marked())
{
$max = ceil(100/$this->probability);
$rand = rand(1, $max);
if(isset($_SESSION['pop_under_time']) && $_SESSION['pop_under_time'] >= $now)
{
$_SESSION['pop_under_time'] = time() + $this->timer;
echo $this->get_template();
exit();
}
else if($rand == $max)
{
$key = array_rand($this->urls);
$_SESSION['pop_under_returned'] = $key;
$_SESSION['pop_under_time'] = time() + $this->timer;
echo $this->get_template();
exit();
}
}
}
else
{
$this->errors[] = "No urls to show provided";
}
}
}
?>