adsTable = $wpdb->prefix."AdMangler_ads";
$this->settingsTable = $wpdb->prefix."AdMangler_settings";
$this->usersTable = $wpdb->prefix."AdMangler_users";
$this->positionsTable = $wpdb->prefix."AdMangler_positions";
} // End function AdMangler
function AdminMenu()
{
add_menu_page('AdMangler Settings', 'Admangler', 9, __FILE__, array($this, 'CreateAdminPage'), '/'. PLUGINDIR . '/admangler/images/logo.gif');
//add_submenu_page(__FILE__, 'AdMangler Settings', 'Settings', 9, 'settings', array($this, 'CreateAdminPage'));
add_submenu_page(__FILE__, 'AdMangler Settings', 'Banners', 9, 'banners', array($this, 'CreateAdminPage'));
} // End function AdminMenu
function Activate()
{
global $wpdb;
// Plugin database table version
$db_version = "0.0.7"; // You must increment this if we change the database other wise leave it alone
$sql[] = "CREATE TABLE ".$wpdb->prefix."AdMangler_ads (
id INT(11) NOT NULL AUTO_INCREMENT,
advertiser VARCHAR(256) COLLATE utf8_bin NOT NULL DEFAULT 'admin',
width INT(11) NOT NULL,
height INT(11) NOT NULL,
active BOOL NOT NULL DEFAULT 0,
approved BOOL NOT NULL DEFAULT 0,
base BOOL NOT NULL DEFAULT 0,
type VARCHAR(5) COLLATE utf8_bin NOT NULL DEFAULT 'image',
code TEXT COLLATE utf8_bin,
href VARCHAR(256) character set utf8 collate utf8_bin NOT NULL default 'http://www.webternals.com/projects/admangler/',
src VARCHAR(256) NOT NULL default 'http://www.webternals.com/images/no-image.png',
UNIQUE KEY id (id)
);";
$sql[] = "CREATE TABLE ".$wpdb->prefix."AdMangler_positions (
ad_ID INT(11) NOT NULL,
page_ID INT(11) NOT NULL,
page_exclusive INT(1) NOT NULL DEFAULT 0,
custom_slot INT(1) NOT NULL DEFAULT 0,
slot INT(11) NOT NULL DEFAULT 0,
slot_exclusive INT(1) NOT NULL DEFAULT 0
);";
$sql[] = "CREATE TABLE ".$wpdb->prefix."AdMangler_settings (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(256) COLLATE utf8_bin NOT NULL,
value VARCHAR(256) COLLATE utf8_bin NOT NULL,
UNIQUE KEY id (id),
PRIMARY KEY name (name)
);";
$sql[] = "CREATE TABLE ".$wpdb->prefix."AdMangler_users (
id INT(11) NOT NULL AUTO_INCREMENT,
username VARCHAR(256) COLLATE utf8_bin NOT NULL,
password VARCHAR(256) COLLATE utf8_bin NOT NULL,
email VARCHAR(256) COLLATE utf8_bin NOT NULL,
credits FLOAT(10,2),
active BOOL DEFAULT 0,
confirm VARCHAR(256) COLLATE utf8_bin,
UNIQUE KEY id (id),
PRIMARY KEY username (username)
);";
// Installed plugin database table version
$installed_ver = get_option('AdMangler_db_version');
// If the database has changed, update the structure while preserving data
if (empty($installed_ver) || $db_version != $installed_ver)
{
require_once ABSPATH . "wp-admin/includes/upgrade.php";
foreach($sql as $temp)
dbDelta($temp);
if (get_option('AdMangler_db_version'))
update_option('AdMangler_db_version', $db_version);
else
add_option('AdMangler_db_version', $db_version);
}
self::SendStatistics();
return true;
} // End function Activate
function ConfirmRegistration()
{
global $wpdb;
$sql = "UPDATE $this->usersTable SET active=1 WHERE confirm=\"".$_GET['key']."\"";
if ($wpdb->query($wpdb->prepare($sql)))
return true;
return false;
} // End function ConfirmRegistration()
function CreateAdminPage()
{
echo "
AdMangler Admin
";
switch($_GET['page'])
{
case 'settings':
include_once "forms/settings.php";
break;
case 'banners':
include_once "forms/banners.php";
break;
default:
include_once "forms/dashboard.php";
break;
}
echo "";
} // End function CreateAdminPage
function FilterTheContent($content)
{
global $wpdb;
$sql = "SELECT a.width, b.height FROM ".$wpdb->prefix."AdMangler_ads as a, ".$wpdb->prefix."AdMangler_ads as b ";
$sql .= "WHERE a.height = b.height GROUP by b.height, a.width";
$results = $wpdb->get_results($sql);
foreach ($results as $banner)
{
$content = str_replace("[AdMangler:".$banner->width."x".$banner->height."]", $this->GetAds($banner->width, $banner->height), $content);
}
$content = str_replace('[AdMangler:Panel]', $this->Panel(), $content);
return $content;
} // End function FilterTheContent
function FormatAd($banner)
{
switch($banner->type)
{
case 'html':
$code = stripslashes($banner->code);
break;
case 'image':
$code = "width}x{$banner->height}\" style=\"width:{$banner->width}px;height:{$banner->height}px;\">
href}\">
src}\" /> ";
}
return $code;
} //End function FormatAd
function GetAdById($id, $return)
{
global $wpdb;
$sql = "SELECT type,code,href,src,width,height FROM $this->adsTable WHERE id=".intval($id);
$row = $wpdb->get_row($sql);
$str = $this->FormatAd($row);
if ($return) return $str; else echo $str;
} // End function GetAdById
function GetAd($options = array('width'=>null,'height'=>null,'pageID'=>null,'position'=>null,'return'=>true))
{
global $wpdb;
if (is_object($options))
{
$width = $options->width;
$height = $options->height;
$pageID = $options->pageID;
$position = $options->position;
$pageID = (empty($pageID)) ? get_the_ID() : $pageID;
$pageID = (is_home()) ? -1 : $pageID;
$position = (empty($position)) ? 0 : $position;
$return = (isset($options->return)) ? $options->return : true;
}
if (is_array($options))
{
$width = $options['width'];
$height = $options['height'];
$pageID = $options['pageID'];
$position = $options['position'];
$pageID = (empty($pageID)) ? get_the_ID() : $pageID;
$pageID = (is_home()) ? -1 : $pageID;
$position = (empty($position)) ? 0 : $position;
$return = (isset($options['return'])) ? $options['return'] : true;
}
foreach (range(0, 3) as $num)
{
//if (!is_array($this->banners[$width."x".$height][$num]))
$this->banners[$width."x".$height][$num] = array();
}
$sql1 = "SELECT * FROM {$this->adsTable} as ads
JOIN {$this->positionsTable} as pos ON ads.id = pos.ad_id
WHERE
(ads.width=$width AND ads.height=$height AND ads.active AND ads.approved AND NOT ads.base)
AND
(pos.page_ID=$pageID AND pos.page_exclusive AND pos.custom_slot AND pos.slot=$position AND pos.slot_exclusive)
ORDER BY RAND()";
$sql2 = "SELECT * FROM {$this->adsTable} as ads
JOIN {$this->positionsTable} as pos ON ads.id = pos.ad_id
WHERE
(ads.width=$width AND ads.height=$height AND ads.active AND ads.approved AND ads.base)
AND
(pos.page_ID=$pageID AND pos.page_exclusive AND pos.custom_slot AND pos.slot=$position AND pos.slot_exclusive)
ORDER BY RAND()";
$results1 = $wpdb->get_results($sql1);
$results2 = $wpdb->get_results($sql2);
if ($results1)
$this->banners[$width."x".$height][0] = $results1;
else if ($results2)
$this->banners[$width."x".$height][0] = $results2;
if (!empty($this->banners[$width."x".$height][0]))
{
$banner = array_shift($this->banners[$width."x".$height][0]);
array_push($this->banners[$width."x".$height][0], $banner);
$str = $this->FormatAd($banner);
if ($return) return $str; else echo $str;
exit(1);
}
$sql1 = "SELECT * FROM {$this->adsTable} as ads
JOIN {$this->positionsTable} as pos ON ads.id = pos.ad_id
WHERE
(ads.width=$width AND ads.height=$height AND ads.active AND ads.approved AND NOT ads.base)
AND
(pos.page_ID=$pageID AND ((pos.page_exclusive AND pos.custom_slot AND pos.slot=$position) OR (pos.page_exclusive AND NOT pos.custom_slot)))
ORDER BY RAND()";
$sql2 = "SELECT * FROM {$this->adsTable} as ads
JOIN {$this->positionsTable} as pos ON ads.id = pos.ad_id
WHERE
(ads.width=$width AND ads.height=$height AND ads.active AND ads.approved AND ads.base)
AND
(pos.page_ID=$pageID AND ((pos.page_exclusive AND pos.custom_slot AND pos.slot=$position) OR (pos.page_exclusive AND NOT pos.custom_slot)))
ORDER BY RAND()";
$results1 = $wpdb->get_results($sql1);
$results2 = $wpdb->get_results($sql2);
if ($results1)
$this->banners[$width."x".$height][1] = $results1;
else if ($results2)
$this->banners[$width."x".$height][1] = $results2;
if (!empty($this->banners[$width."x".$height][1]))
{
$banner = array_shift($this->banners[$width."x".$height][1]);
array_push($this->banners[$width."x".$height][1], $banner);
$str = $this->FormatAd($banner);
if ($return) return $str; else echo $str;
exit(1);
}
$sql1 = "SELECT * FROM {$this->adsTable} as ads
JOIN {$this->positionsTable} as pos ON ads.id = pos.ad_id
WHERE
(ads.width=$width AND ads.height=$height AND ads.active AND ads.approved AND NOT ads.base)
AND
((pos.page_ID=$pageID OR pos.page_ID=0) AND pos.custom_slot AND pos.slot=$position AND pos.slot_exclusive)
ORDER BY RAND()";
$sql2 = "SELECT * FROM {$this->adsTable} as ads
JOIN {$this->positionsTable} as pos ON ads.id = pos.ad_id
WHERE
(ads.width=$width AND ads.height=$height AND ads.active AND ads.approved AND ads.base)
AND
((pos.page_ID=$pageID OR pos.page_ID=0) AND pos.custom_slot AND pos.slot=$position AND pos.slot_exclusive)
ORDER BY RAND();";
$results1 = $wpdb->get_results($sql1);
$results2 = $wpdb->get_results($sql2);
if ($results1)
$this->banners[$width."x".$height][2] = $results1;
else if ($results2)
$this->banners[$width."x".$height][2] = $results2;
if (!empty($this->banners[$width."x".$height][2]))
{
$banner = array_shift($this->banners[$width."x".$height][2]);
array_push($this->banners[$width."x".$height][2], $banner);
$str = $this->FormatAd($banner);
if ($return) return $str; else echo $str;
exit(1);
}
$sql1 = "SELECT * FROM {$this->adsTable} as ads
LEFT JOIN {$this->positionsTable} as pos ON pos.ad_id = ads.id
WHERE
(ads.width=$width AND ads.height=$height AND ads.active AND ads.approved AND NOT ads.base)
AND
(((pos.page_ID=$pageID OR pos.page_ID=0) AND NOT pos.custom_slot) OR pos.page_ID IS NULL)
ORDER BY RAND()";
$sql2 = "SELECT * FROM {$this->adsTable} as ads
LEFT JOIN {$this->positionsTable} as pos ON pos.ad_id = ads.id
WHERE
(ads.width=$width AND ads.height=$height AND ads.active AND ads.approved AND ads.base)
AND
(((pos.page_ID=$pageID OR pos.page_ID=0) AND NOT pos.custom_slot) OR pos.page_ID IS NULL)
ORDER BY RAND()";
$results1 = $wpdb->get_results($sql1);
$results2 = $wpdb->get_results($sql2);
if ($results1)
$this->banners[$width."x".$height][3] = $results1;
else if ($results2)
$this->banners[$width."x".$height][3] = $results2;
if (!empty($this->banners[$width."x".$height][3]))
{
$banner = array_shift($this->banners[$width."x".$height][3]);
array_push($this->banners[$width."x".$height][3], $banner);
$str = $this->FormatAd($banner);
if ($return) return $str; else echo $str;
exit(1);
}
$str = "";
if ($return) return $str; else echo $str;
}
function GetAds($width=468, $height=60, $return=true)
{
global $wpdb;
if (!is_array($this->banners[$width."x".$height]))
$this->banners[$width."x".$height] = array();
if (empty($this->banners[$width."x".$height]))
{
$sql = "SELECT type,code,href,src,width,height FROM $this->adsTable WHERE width=$width and height=$height and active and approved and NOT base ORDER BY RAND()";
$results = $wpdb->get_results($sql);
if ($results)
$this->banners[$width."x".$height] = $results;
$sql = "SELECT type,code,href,src,width,height FROM $this->adsTable WHERE width=$width and height=$height and active and approved and base ORDER BY RAND()";
$results = $wpdb->get_results($sql);
if ($results)
{
$this->banners[$width."x".$height] = array_merge($this->banners[$width."x".$height], $results);
}
}
$str = "";
$banner = array_shift($this->banners[$width."x".$height]);
array_push($this->banners[$width."x".$height], $banner);
$str = $this->FormatAd($banner);
if ($return) return $str; else echo $str;
} // End function GetAds
function Login()
{
global $wpdb;
$sql = "SELECT id FROM $this->usersTable WHERE username='".$_POST['username']."' AND password='".sha1($_POST['password'])."' AND active";
$login = $wpdb->get_row($sql);
if (1 == $wpdb->num_rows)
{
$_SESSION['AdMangler']['loggedin'] = true;
$_SESSION['AdMangler']['username'] = $_POST['username'];
$_SESSION['AdMangler']['password'] = $_POST['password'];
return true;
}
return false;
} // End function Login()
function Logout()
{
unset($_SESSION['AdMangler']);
return true;
} // End function Logout
function Panel()
{
$action = (isset($_GET['action'])) ? $_GET['action'] : 'login';
ob_start();
include_once "panel/$action.php";
$contents = ob_get_contents();
ob_end_clean();
return $contents;
} // End PublicForm
function Register()
{
global $wpdb;
$sql = "SELECT id FROM $this->usersTable WHERE username=\"".$_POST['username']."\"";
if($wpdb->query($wpdb->prepare($sql)))
{
if (0 == $wpdb->num_rows)
{
$sql = "
INSERT INTO
$this->usersTable
(username, password, credits, email, active)
VALUES
(\"".$_POST['username']."\", \"".sha1($_POST['password'])."\", 0, \"".$_POST['email']."\", 0)
";
if($wpdb->query($wpdb->prepare($sql)))
return true;
}
}
return false;
} // End function Login()
function RegisterWidgets()
{
register_widget('AdManglerWidget'); // This adds the Widget to the backend
}
function ResetPassword()
{
global $wpdb;
$sql = "UPDATE $this->usersTable SET password = \"".sha1($_POST['password'])."\" WHERE confirm=\"".$_GET['key']."\"";
if ($wpdb->query($wpdb->prepare($sql)))
return true;
return false;
} // End function ResetPassword()
function ShortCodeHandler($atts, $content=null, $code="")
{
// $atts ::= array of attributes
// $content ::= text within enclosing form of shortcode element
// $code ::= the shortcode found, when == callback name
// examples: [my-shortcode]
// [my-shortcode/]
// [my-shortcode foo='bar']
// [my-shortcode foo='bar'/]
// [my-shortcode]content[/my-shortcode]
// [my-shortcode foo='bar']content[/my-shortcode]
if (!isset($atts['type']))
return $this->GetAd($atts);
else if (isset($atts['type']) && 0 == strcmp($atts['type'], "Panel"))
return $this->Panel();
else
return "";
}
function SetConfirmationKey()
{
global $wpdb;
srand(time());
$key = md5(rand(1000,10000). $_GET['username'] .rand(10000,100000));
$sql = "UPDATE $this->usersTable SET confirm = \"$key\" WHERE username=\"".$_POST['username']."\"";
if ($wpdb->query($wpdb->prepare($sql)))
{
$sql = "SELECT email FROM $this->usersTable WHERE username=\"".$_POST['username']."\"";
if ($row = $wpdb->get_row($wpdb->prepare($sql)))
return array("key" => $key, "email" => $row->email);
}
return false;
} // End function SetConfirmationKey()
function SendStatistics($url=null)
{
if (in_array ('curl', get_loaded_extensions()))
{
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => true, // don't return headers
CURLOPT_FOLLOWLOCATION => false, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "AdMangler Phone Home", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_POST => true, // i am sending post data
CURLOPT_POSTFIELDS => "application=AdMangler&version=0.0.9.2.Alpha&action=install&domain=".urlencode($_SERVER['SERVER_NAME']), // this are my post vars
CURLOPT_SSL_VERIFYHOST => false, // don't verify ssl
CURLOPT_SSL_VERIFYPEER => false, //
CURLOPT_VERBOSE => false, //
CURLOPT_MUTE => true
);
$ch = curl_init('http://www.webternals.com/custom/statistics.php');
curl_setopt_array($ch,$options);
$content = curl_exec($ch);
$err = curl_errno($ch);
$errmsg = curl_error($ch) ;
$header = curl_getinfo($ch);
curl_close($ch);
}
else
{
$temp = ''; // Do Nothing for now
}
}
function ValidConfirmationKey()
{
global $wpdb;
$sql = "SELECT * FROM $this->usersTable WHERE confirm=\"".$_GET['key']."\"";
if ($row = $wpdb->get_row($wpdb->prepare($sql)))
return true;
return false;
} // End function ValidConfirmationKey()
} // End class AdMangler
?>