dbVersion) ) $this->dbVersion = $dbVersion; if ( is_null($this->codeVersion) ) $this->codeVersion = $codeVersion; } global $wpdb; $this->adsTable = $wpdb->prefix."AdMangler_ads"; $this->settingsTable = $wpdb->prefix."AdMangler_settings"; $this->usersTable = $wpdb->prefix."AdMangler_users"; $this->positionsTable = $wpdb->prefix."AdMangler_positions"; add_action( 'init', function () { wp_enqueue_script("jquery"); //wp_enqueue_script('jquery.validate', '/' . PLUGINDIR . '/admangler/js/jquery.validate.min.js'); }); // Generate the Admin Menu if ( is_admin() ) { add_action('admin_init', array($this, "activate") ); //register_activation_hook(__FILE__, array($this, "activate")); add_action('admin_menu', array($this, 'admin_menu')); wp_enqueue_script('admanglertooltip', '/' . PLUGINDIR . '/admangler/js/tooltip.js'); // init process for registering our button add_action('init', function () { //Abort early if the user will never see TinyMCE if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') && get_user_option('rich_editing') == 'true') return; //Add a callback to regiser our tinymce plugin //This callback registers our plug-in add_filter("mce_external_plugins", function ($plugin_array) { global $PLUGINDIR; $plugin_array['wpse72394_button'] = '/' . PLUGINDIR . '/admangler/js/shortcode.js'; return $plugin_array; }); // Add a callback to add our button to the TinyMCE toolbar //This callback adds our button to the toolbar add_filter('mce_buttons', function ($buttons) { //Add the button ID to the $button array $buttons[] = "wpse72394_button"; return $buttons; }); }); } add_filter('the_content', array($this, 'filter_the_content')); add_shortcode('AdMangler', array($this, 'short_code_helper')); add_shortcode('admangler', array($this, 'short_code_helper')); add_action('init', array($this, 'register_widgets'), 1); } // End function AdMangler function admin_menu() { add_menu_page('AdMangler Settings', 'AdMangler', 9, __FILE__, array($this, 'create_admin_page'), '/'. PLUGINDIR . '/admangler/images/logo.gif'); //add_submenu_page(__FILE__, 'AdMangler Settings', 'Settings', 9, 'settings', array($this, 'create_admin_page')); add_submenu_page(__FILE__, 'AdMangler Settings', 'Banners', 9, 'banners', array($this, 'create_admin_page')); } // End function admin_menu function activate() { ob_start(); global $wpdb; // Installed plugin database table version $db_version = get_option('AdMangler_db_version'); if ( false === $db_version ) { $db_version = '0.0.0'; } $code_version = get_option('AdMangler_code_version'); if ( false === $code_version ) $code_version = '0.0.0'; if ( version_compare( $code_version, $this->codeVersion, '<') ) { if (get_option('AdMangler_code_version')) { update_option('AdMangler_code_version', $this->codeVersion); $action = 'update'; } else { add_option('AdMangler_code_version', $this->codeVersion); $action = 'install'; } $this->send_statistics($action); } // If the database has changed, update the structure while preserving data if ( version_compare( $db_version, $this->dbVersion, '<') ) { // Plugin database table version $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) );"; 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', $this->dbVersion); } else { add_option('AdMangler_db_version', $this->dbVersion); } } //file_put_contents(dirname(__FILE__)."/log.txt", var_export($this, true), FILE_APPEND); return true; } // End function activate function confirm_registration() { 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 confirm_registration() function create_admin_page() { 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 create_admin_page function filter_the_content($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->get_ads($banner->width, $banner->height), $content); } $content = str_replace('[AdMangler:panel]', $this->panel(), $content); return $content; } // End function filter_the_content function format_ad($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 format_ad function get_ad_by_id($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->format_ad($row); if ($return) return $str; else echo $str; } // End function get_ad_by_id function get_ad($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->format_ad($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->format_ad($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->format_ad($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->format_ad($banner); if ($return) return $str; else echo $str; exit(1); } $str = ""; if ($return) return $str; else echo $str; } function get_ads($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->format_ad($banner); if ($return) return $str; else echo $str; } // End function get_ads 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 register_widgets() { register_widget('AdManglerWidget'); // This adds the Widget to the backend } function reset_password() { 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 reset_password() function short_code_helper($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->get_ad($atts); else if (isset($atts['type']) && 0 == strcmp($atts['type'], "panel")) return $this->panel(); else return ""; } function set_confirmation_key() { 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 set_confirmation_key() function set_db_version( $version = "0.0.0" ) { $this->dbVersion = $version; } function set_code_version( $version = "0.0.0" ) { $this->codeVersion = $version; } function send_statistics($action) { if (in_array ('curl', get_loaded_extensions())) { try { $api = new WebAPI('publicapi', 'publicapi'); $phone = new SimpleXMLElement(''); $phone->addChild('action', 'stats'); $phone->addChild('application', 'AdMangler'); $phone->addChild('version', $this->codeVersion); $phone->addChild('database', $this->dbVersion); $phone->addChild('status', $action); $phone->addChild('domain', urlencode($_SERVER['SERVER_NAME'])); $api->add_request($phone); $api->request(); } catch (Exception $e) { /* Fail quitely */ } } else { $temp = ''; // Do Nothing for now } } function valid_confirmation_key() { 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 valid_confirmation_key() } // End class AdMangler ?>