function adrotate_import_ads()
{
    global $wpdb, $current_user, $userdata;
    if (wp_verify_nonce($_POST['adrotate_nonce'], 'adrotate_import')) {
        if (current_user_can('adrotate_ad_manage')) {
            if ($_FILES["adrotate_file"]["error"] == 4) {
                adrotate_return('adrotate-ads', 506, array('view' => 'import'));
                exit;
            } else {
                if ($_FILES["adrotate_file"]["error"] > 0) {
                    adrotate_return('adrotate-ads', 507, array('view' => 'import'));
                    exit;
                } else {
                    if ($_FILES["adrotate_file"]["size"] > 4096000) {
                        adrotate_return('adrotate-ads', 511, array('view' => 'import'));
                        exit;
                    } else {
                        $now = adrotate_now();
                        $ad_fields = array('title', 'bannercode', 'thetime', 'updated', 'author', 'imagetype', 'image', 'tracker', 'responsive', 'type', 'weight', 'sortorder', 'budget', 'crate', 'irate', 'cities', 'countries');
                        if ($_FILES["adrotate_file"]["type"] == "text/xml" or $_FILES["adrotate_file"]["type"] == "application/xml" or $_FILES["adrotate_file"]["type"] == "application/x-xml") {
                            $xml_name = "adrotate_import_" . date_i18n("mdYHi", $now) . ".xml";
                            move_uploaded_file($_FILES["adrotate_file"]["tmp_name"], WP_CONTENT_DIR . "/reports/" . $xml_name);
                            $file = WP_CONTENT_URL . "/reports/" . $xml_name;
                            $xml = simplexml_load_file($file);
                            foreach ($xml->xpath('advert') as $advert) {
                                $ad = array('title' => strip_tags(htmlspecialchars(trim($advert->title, "\t\n "), ENT_QUOTES)), 'bannercode' => htmlspecialchars(trim($advert->bannercode, "\t\n "), ENT_QUOTES), 'thetime' => $now, 'updated' => $now, 'author' => $current_user->user_login, 'imagetype' => strip_tags(trim($advert->imagetype, "\t\n ")), 'image' => strip_tags(trim($advert->image, "\t\n ")), 'tracker' => strip_tags(trim($advert->tracker, "\t\n ")), 'mobile' => strip_tags(trim($advert->mobile, "\t\n ")), 'tablet' => strip_tags(trim($advert->tablet, "\t\n ")), 'responsive' => strip_tags(trim($advert->responsive, "\t\n ")), 'type' => 'import', 'weight' => strip_tags(trim($advert->weight, "\t\n ")), 'sortorder' => 0, 'budget' => strip_tags(trim($advert->budget, "\t\n ")), 'crate' => strip_tags(trim($advert->crate, "\t\n ")), 'irate' => strip_tags(trim($advert->irate, "\t\n ")), 'cities' => serialize(explode(',', strip_tags(trim($advert->cities, "\t\n ")))), 'countries' => serialize(explode(',', strip_tags(trim($advert->countries, "\t\n ")))));
                                $wpdb->insert($wpdb->prefix . "adrotate", $ad);
                                $ad_id = $wpdb->insert_id;
                                $schedule = array('name' => 'Schedule for advert ' . $ad_id, 'starttime' => strip_tags(trim($advert->start, "\t\n ")), 'stoptime' => strip_tags(trim($advert->end, "\t\n ")), 'maxclicks' => 0, 'maximpressions' => 0, 'spread' => 'N', 'dayimpressions' => 0);
                                $wpdb->insert($wpdb->prefix . "adrotate_schedule", $schedule);
                                $schedule_id = $wpdb->insert_id;
                                $linkmeta = array('ad' => $ad_id, 'group' => 0, 'user' => 0, 'schedule' => $schedule_id);
                                $wpdb->insert($wpdb->prefix . "adrotate_linkmeta", $linkmeta);
                                unset($advert, $ad, $ad_id, $schedule, $schedule_id, $linkmeta);
                            }
                        }
                        adrotate_prepare_evaluate_ads(false);
                        // return to dashboard
                        adrotate_return('adrotate-ads', 216);
                        exit;
                    }
                }
            }
        } else {
            adrotate_return('adrotate-ads', 500);
        }
    } else {
        adrotate_nonce_error();
        exit;
    }
}
function adrotate_prepare_color($enddate)
{
    $now = adrotate_now();
    $in2days = $now + 172800;
    $in7days = $now + 604800;
    if ($enddate <= $now) {
        return '#CC2900';
        // red
    } else {
        if ($enddate <= $in2days and $enddate >= $now) {
            return '#F90';
            // orange
        } else {
            if ($enddate <= $in7days and $enddate >= $now) {
                return '#E6B800';
                // yellow
            } else {
                return '#009900';
                // green
            }
        }
    }
}
function adrotate_notifications_dashboard()
{
    global $adrotate_advert_status;
    if (current_user_can('adrotate_ad_manage')) {
        if (!is_array($adrotate_advert_status)) {
            $data = unserialize($adrotate_advert_status);
        } else {
            $data = $adrotate_advert_status;
        }
        if ($data['total'] > 0) {
            if ($data['expired'] > 0 and $data['expiressoon'] == 0 and $data['error'] == 0) {
                echo '<div class="error"><p>' . $data['expired'] . ' ' . __('active ad(s) expired.', 'adrotate') . ' <a href="admin.php?page=adrotate-ads">' . __('Take action now', 'adrotate') . '</a>!</p></div>';
            } else {
                if ($data['expired'] == 0 and $data['expiressoon'] > 0 and $data['error'] == 0) {
                    echo '<div class="error"><p>' . $data['expiressoon'] . ' ' . __('active ad(s) are about to expire.', 'adrotate') . ' <a href="admin.php?page=adrotate-ads">' . __('Check it out', 'adrotate') . '</a>!</p></div>';
                } else {
                    if ($data['expired'] == 0 and $data['expiressoon'] == 0 and $data['error'] > 0) {
                        echo '<div class="error"><p>There are ' . $data['error'] . ' ' . __('active ad(s) with configuration errors.', 'adrotate') . ' <a href="admin.php?page=adrotate-ads">' . __('Solve this', 'adrotate') . '</a>!</p></div>';
                    } else {
                        echo '<div class="error"><p>' . $data['expired'] . ' ' . __('ad(s) expired.', 'adrotate') . ' ' . $data['expiressoon'] . ' ' . __('ad(s) are about to expire.', 'adrotate') . ' There are ' . $data['error'] . ' ' . __('ad(s) with configuration errors.', 'adrotate') . ' <a href="admin.php?page=adrotate-ads">' . __('Fix this as soon as possible', 'adrotate') . '</a>!</p></div>';
                    }
                }
            }
        }
        if (isset($_GET['hide']) and $_GET['hide'] == 1) {
            update_option('adrotate_hide_banner', 1);
        }
        if (isset($_GET['hide']) and $_GET['hide'] == 2) {
            update_option('adrotate_hide_review', 1);
        }
        if (isset($_GET['page'])) {
            $page = $_GET['page'];
        } else {
            $page = '';
        }
        $pro_banner = get_option('adrotate_hide_banner');
        if ($pro_banner != 1 and $pro_banner < adrotate_now() - 604800 and strpos($page, 'adrotate') !== false) {
            echo '<div class="updated" style="padding: 0; margin: 0; border-left: none;">';
            echo '	<div class="adrotate_banner">';
            echo '		<div class="button_div"><a class="button" target="_blank" href="https://ajdg.solutions/products/adrotate-for-wordpress/?add-to-cart=1126?pk_campaign=adrotatefree-upgradebanner">' . __('Buy now', 'adrotate') . '</a></div>';
            echo '		<div class="text">' . __("You've been using <strong>AdRotate</strong> for a while now. Why not upgrade to the <strong>PRO</strong> version", 'adrotate') . '?<br /><span>' . __('Use discount code <b>getadrotatepro</b> for 10% off on any AdRotate license!', 'adrotate') . ' ' . __('Thank you for your purchase!', 'adrotate') . '</span></div>';
            echo '		<a class="close_banner" href="admin.php?page=adrotate-pro&hide=1"><img title="Close" src="' . plugins_url('images/icon-close.png', __FILE__) . '" alt=""/></a>';
            echo '		<div class="icon"><img title="" src="' . plugins_url('images/logo-60x60.png', __FILE__) . '" alt=""/></div>';
            echo '	</div>';
            echo '</div>';
        }
        $review_banner = get_option('adrotate_hide_review');
        if ($review_banner != 1 and $review_banner < adrotate_now() - 2419200 and strpos($page, 'adrotate') !== false) {
            echo '<div class="updated" style="padding: 0; margin: 0; border-left: none;">';
            echo '	<div class="adrotate_banner">';
            echo '		<div class="button_div"><a class="button" target="_blank" href="https://wordpress.org/support/view/plugin-reviews/adrotate?rate=5#postform">Rate AdRotate</a></div>';
            echo '		<div class="text">If you like <strong>AdRotate</strong> please let the world know that you do. Thanks for your support!<br /><span>If you have questions, suggestions or something else that doesn\'t belong in a review, please <a href="https://ajdg.solutions/forums/forum/adrotate-for-wordpress/?pk_campaign=adrotatefree-reviewbanner" target="_blank">get in touch</a>!</span></div>';
            echo '		<a class="close_banner" href="admin.php?page=adrotate&hide=2"><img title="Close" src="' . plugins_url('images/icon-close.png', __FILE__) . '" alt=""/></a>';
            echo '		<div class="icon"><img title="" src="' . plugins_url('images/logo-60x60.png', __FILE__) . '" alt=""/></div>';
            echo '	</div>';
            echo '</div>';
        }
    }
    if (isset($_GET['upgrade']) and $_GET['upgrade'] == 1) {
        adrotate_check_upgrade();
    }
    $adrotate_db_version = get_option("adrotate_db_version");
    $adrotate_version = get_option("adrotate_version");
    if ($adrotate_db_version['current'] < ADROTATE_DB_VERSION or $adrotate_version['current'] < ADROTATE_VERSION) {
        echo '<div class="updated" style="padding: 0; margin: 0; border-left: none;">';
        echo '	<div class="adrotate_banner">';
        echo '		<div class="button_div"><a class="button" href="admin.php?page=adrotate&upgrade=1">Update Now</a></div>';
        echo '		<div class="text">You have almost finished upgrading <strong>AdRotate</strong> to version <strong>' . ADROTATE_DISPLAY . '</strong>!<br /><span>To complete the update click the button on the left. This may take a few seconds to complete!</span></div>';
        echo '		<div class="icon"><img title="" src="' . plugins_url('images/logo-60x60.png', __FILE__) . '" alt=""/></div>';
        echo '	</div>';
        echo '</div>';
    }
}
function adrotate_date_start($what)
{
    $now = adrotate_now();
    $string = gmdate('Y-m-d H:i:s', time());
    preg_match('#([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})#', $string, $matches);
    switch ($what) {
        case 'hour':
            $string_time = gmmktime($matches[4], 0, 0, $matches[2], $matches[3], $matches[1]);
            $result = gmdate('U', $string_time + get_option('gmt_offset') * HOUR_IN_SECONDS);
            break;
        case 'day':
            $timezone = get_option('timezone_string');
            if ($timezone) {
                $server_timezone = date('e');
                date_default_timezone_set($timezone);
                $result = strtotime('00:00:00') + get_option('gmt_offset') * HOUR_IN_SECONDS;
                date_default_timezone_set($server_timezone);
            } else {
                $result = gmdate('U', gmmktime(0, 0, 0, gmdate('n'), gmdate('j')));
            }
            break;
        case 'week':
            $timezone = get_option('timezone_string');
            if ($timezone) {
                $server_timezone = date('e');
                date_default_timezone_set($timezone);
                $result = strtotime('Last Monday', $now) + get_option('gmt_offset') * HOUR_IN_SECONDS;
                date_default_timezone_set($server_timezone);
            } else {
                $result = gmdate('U', gmmktime(0, 0, 0));
            }
            break;
    }
    return $result;
}
Exemple #5
0
function adrotate_manage_schedules()
{
    global $wpdb;
    $now = adrotate_now();
    $today = adrotate_date_start('day');
    $in2days = $now + 172800;
    $in7days = $now + 604800;
    $in84days = $now + 7257600;
    ?>
	<div class="wrap">
		<h1><?php 
    _e('Schedule Management available in AdRotate Pro', 'adrotate');
    ?>
</h1>

		<div class="tablenav">
			<div class="alignleft actions">
				<strong><?php 
    _e('Manage', 'adrotate');
    ?>
</strong> | 
				<strong><?php 
    _e('Add New', 'adrotate');
    ?>
</strong>
			</div>
		</div>

		<h3><?php 
    _e('Manage Schedules', 'adrotate');
    ?>
</h3>
		<p><?php 
    _e('Schedule management and multiple schedules per advert is available in AdRotate Pro.', 'adrotate');
    ?>
 <a href="admin.php?page=adrotate-pro"><?php 
    _e('More information', 'adrotate');
    ?>
</a>.</p>
		
		<?php 
    wp_nonce_field('adrotate_bulk_schedules', 'adrotate_nonce');
    ?>
	
		<div class="tablenav top">
			<div class="alignleft actions">
				<select name="adrotate_action" id="cat" class="postform" disabled>
			        <option value=""><?php 
    _e('Bulk Actions', 'adrotate');
    ?>
</option>
				</select> <input type="submit" id="post-action-submit" name="adrotate_action_submit" value="<?php 
    _e('Go', 'adrotate');
    ?>
" class="button-secondary" disabled />
			</div>	
			<br class="clear" />
		</div>
		
		<table class="widefat" style="margin-top: .5em">
			<thead>
			<tr>
				<td scope="col" class="manage-column column-cb check-column"><input type="checkbox" disabled/></td>
				<th width="4%"><center><?php 
    _e('ID', 'adrotate');
    ?>
</center></th>
				<th width="20%"><?php 
    _e('Start', 'adrotate');
    ?>
 / <?php 
    _e('End', 'adrotate');
    ?>
</th>
		        <th width="4%"><center><?php 
    _e('Ads', 'adrotate');
    ?>
</center></th>
				<th>&nbsp;</th>
		        <th width="15%"><center><?php 
    _e('Max Impressions', 'adrotate');
    ?>
</center></th>
		        <th width="10%"><center><?php 
    _e('Max Clicks', 'adrotate');
    ?>
</center></th>
			</tr>
			</thead>
			<tbody>
		<?php 
    $schedules = $wpdb->get_results("SELECT * FROM `" . $wpdb->prefix . "adrotate_schedule` WHERE `name` != '' ORDER BY `id` ASC;");
    if ($schedules) {
        $class = '';
        foreach ($schedules as $schedule) {
            $schedulesmeta = $wpdb->get_results("SELECT `ad` FROM `" . $wpdb->prefix . "adrotate_linkmeta` WHERE `group` = 0 AND `user` = 0 AND `schedule` = " . $schedule->id . ";");
            if ($schedule->maxclicks == 0) {
                $schedule->maxclicks = '&infin;';
            }
            if ($schedule->maximpressions == 0) {
                $schedule->maximpressions = '&infin;';
            }
            $class != 'alternate' ? $class = 'alternate' : ($class = '');
            if ($schedule->stoptime < $in2days) {
                $class = 'row_urgent';
            }
            if ($schedule->stoptime < $now) {
                $class = 'row_inactive';
            }
            ?>
			    <tr id='adrotateindex' class='<?php 
            echo $class;
            ?>
'>
					<th class="check-column"><input type="checkbox" name="schedulecheck[]" value="" disabled /></th>
					<td><center><?php 
            echo $schedule->id;
            ?>
</center></td>
					<td><?php 
            echo date_i18n("F d, Y H:i", $schedule->starttime);
            ?>
<br /><span style="color: <?php 
            echo adrotate_prepare_color($schedule->stoptime);
            ?>
;"><?php 
            echo date_i18n("F d, Y H:i", $schedule->stoptime);
            ?>
</span></td>
			        <td><center><?php 
            echo count($schedulesmeta);
            ?>
</center></td>
					<td><?php 
            echo stripslashes(html_entity_decode($schedule->name));
            ?>
</td>
			        <td><center><?php 
            echo $schedule->maximpressions;
            ?>
</center></td>
			        <td><center><?php 
            echo $schedule->maxclicks;
            ?>
</center></td>
				</tr>
				<?php 
        }
        ?>
			<?php 
    } else {
        ?>
			<tr id='no-schedules'>
				<th class="check-column">&nbsp;</th>
				<td colspan="7"><em><?php 
        _e('No schedules created yet!', 'adrotate');
        ?>
</em></td>
			</tr>
			<?php 
    }
    ?>
			</tbody>
		</table>
		<center><?php 
    _e('Easily manage your schedules from here with AdRotate Pro.', 'adrotate');
    ?>
 <a href="admin.php?page=adrotate-pro"><?php 
    _e('Upgrade today!', 'adrotate');
    ?>
</a></center>

		<p><center>
			<span style="border: 1px solid #c00; height: 12px; width: 12px; background-color: #ffebe8">&nbsp;&nbsp;&nbsp;&nbsp;</span> <?php 
    _e("Expires soon.", "adrotate");
    ?>
			&nbsp;&nbsp;&nbsp;&nbsp;<span style="border: 1px solid #466f82; height: 12px; width: 12px; background-color: #8dcede">&nbsp;&nbsp;&nbsp;&nbsp;</span> <?php 
    _e("Has expired.", "adrotate");
    ?>
		</center></p>

		<br class="clear" />

		<?php 
    adrotate_credits();
    ?>

		<br class="clear" />
	</div>
<?php 
}
Exemple #6
0
function adrotate_clean_trackerdata() {
	global $wpdb;

	$now = adrotate_now();
	$clicks = $now - 86400;
	$impressions = $now - 3600;

	$wpdb->query("DELETE FROM `{$wpdb->prefix}adrotate_tracker` WHERE (`timer` < ".$clicks." AND `stat` = 'c') OR (`timer` < ".$impressions." AND `stat` = 'i') OR `ipaddress`  = 'unknown' OR `ipaddress`  = '';");
}
function adrotate_renew($id, $howlong = 2592000)
{
    global $wpdb;
    if ($id > 0) {
        $schedule_id = $wpdb->get_var($wpdb->prepare("SELECT `schedule` FROM `" . $wpdb->prefix . "adrotate_linkmeta` WHERE `ad` = %d AND `group` = 0 AND `user` = 0 ORDER BY `id` DESC LIMIT 1;", $id));
        if ($schedule_id > 0) {
            $wpdb->query("UPDATE `" . $wpdb->prefix . "adrotate_schedule` SET `stoptime` = `stoptime` + {$howlong} WHERE `id` = {$schedule_id};");
        } else {
            $now = adrotate_now();
            $stoptime = $now + $howlong;
            $wpdb->insert($wpdb->prefix . 'adrotate_schedule', array('name' => 'Schedule for ad ' . $id, 'starttime' => $now, 'stoptime' => $stoptime, 'maxclicks' => 0, 'maximpressions' => 0));
            $wpdb->insert($wpdb->prefix . 'adrotate_linkmeta', array('ad' => $id, 'group' => 0, 'user' => 0, 'schedule' => $wpdb->insert_id));
        }
    }
}
function adrotate_notifications_dashboard()
{
    global $adrotate_config, $adrotate_advert_status;
    if (current_user_can('adrotate_ad_manage')) {
        $license = get_option('adrotate_activate');
        if ($adrotate_config['dashboard_notifications'] == "Y") {
            if (!is_array($adrotate_advert_status)) {
                $data = unserialize($adrotate_advert_status);
            } else {
                $data = $adrotate_advert_status;
            }
            if ($data['total'] > 0) {
                if ($data['expired'] > 0 and $data['expiressoon'] == 0 and $data['error'] == 0) {
                    echo '<div class="error"><p>' . $data['expired'] . ' ' . __('active ad(s) expired.', 'adrotate-pro') . ' <a href="admin.php?page=adrotate-ads">' . __('Take action now', 'adrotate-pro') . '</a>!</p></div>';
                } else {
                    if ($data['expired'] == 0 and $data['expiressoon'] > 0 and $data['error'] == 0) {
                        echo '<div class="error"><p>' . $data['expiressoon'] . ' ' . __('active ad(s) are about to expire.', 'adrotate-pro') . ' <a href="admin.php?page=adrotate-ads">' . __('Check it out', 'adrotate-pro') . '</a>!</p></div>';
                    } else {
                        if ($data['expired'] == 0 and $data['expiressoon'] == 0 and $data['error'] > 0) {
                            echo '<div class="error"><p>There are ' . $data['error'] . ' ' . __('active ad(s) with configuration errors.', 'adrotate-pro') . ' <a href="admin.php?page=adrotate-ads">' . __('Solve this', 'adrotate-pro') . '</a>!</p></div>';
                        } else {
                            echo '<div class="error"><p>' . $data['expired'] . ' ' . __('ad(s) expired.', 'adrotate-pro') . ' ' . $data['expiressoon'] . ' ' . __('ad(s) are about to expire.', 'adrotate-pro') . ' There are ' . $data['error'] . ' ' . __('ad(s) with configuration errors.', 'adrotate-pro') . ' <a href="admin.php?page=adrotate-ads">' . __('Fix this as soon as possible', 'adrotate-pro') . '</a>!</p></div>';
                        }
                    }
                }
            }
            if ($adrotate_config['enable_geo'] > 2) {
                $lookups = get_option('adrotate_geo_requests');
                if ($license['status'] == 0 and $adrotate_config['enable_geo'] == 5) {
                    echo '<div class="updated"><p>' . __('The AdRotate Geo service can only be used after you activate your license for this website.', 'adrotate-pro') . '</p></div>';
                }
                if ($lookups > 0 and $lookups < 1000) {
                    echo '<div class="updated"><p>' . sprintf(__('You are running out of Geo Lookups for AdRotate. You have less than %d remaining lookups. If this reaches 0, Geo Targeting will stop working.', 'adrotate-pro'), $lookups) . '</p></div>';
                }
                if ($lookups < 1) {
                    echo '<div class="updated"><p>' . __('AdRotate Geo Targeting is no longer working because you have no more lookups. If you think this is untrue, try resaving the Geo Targeting settings.', 'adrotate-pro') . '</p></div>';
                }
            }
        }
        if (isset($_GET['hide']) and $_GET['hide'] == 2) {
            update_option('adrotate_hide_review', 1);
        }
        if (isset($_GET['page'])) {
            $page = $_GET['page'];
        } else {
            $page = '';
        }
        $review_banner = get_option('adrotate_hide_review');
        if ($review_banner != 1 and $review_banner < adrotate_now() - 2419200 and strpos($page, 'adrotate') !== false) {
            echo '<div class="updated" style="padding: 0; margin: 0; border-left: none;">';
            echo '	<div class="adrotate_banner">';
            echo '		<div class="button_div"><a class="button" target="_blank" href="https://wordpress.org/support/view/plugin-reviews/adrotate?rate=5#postform">Rate AdRotate</a></div>';
            echo '		<div class="text">If you like <strong>AdRotate</strong> please let the world know that you do. Thanks for your support!<br /><span>If you have questions, suggestions or something else that doesn\'t belong in a review, please <a href="https://ajdg.solutions/forums/forum/adrotate-for-wordpress/?pk_campaign=adrotatepro-reviewbanner&pk_kwd=forum" target="_blank">get in touch</a>!</span></div>';
            echo '		<div class="icon"><img title="" src="' . plugins_url('images/adrotate-logo-60x60.png', __FILE__) . '" alt=""/></div>';
            echo '		<a class="close_banner" href="admin.php?page=adrotate&hide=2"><img title="Close" src="' . plugins_url('images/icon-close.png', __FILE__) . '" alt=""/></a>';
            echo '	</div>';
            echo '</div>';
        }
        if ($license['firstrun'] == 1) {
            echo '<div class="updated"><p>' . __('Register your copy of AdRotate. This will enable email support and update notifications right from your dashboard.', 'adrotate-pro') . ' <a href="admin.php?page=adrotate-settings" class="button">' . __('Register AdRotate', 'adrotate-pro') . '</a></p></div>';
            update_option('adrotate_activate', array('status' => 0, 'instance' => '', 'activated' => '', 'deactivated' => '', 'type' => '', 'key' => '', 'email' => '', 'version' => '', 'firstrun' => 0));
        }
    }
    if (isset($_GET['upgrade']) and $_GET['upgrade'] == 1) {
        adrotate_check_upgrade();
    }
    $adrotate_db_version = get_option("adrotate_db_version");
    $adrotate_version = get_option("adrotate_version");
    if ($adrotate_db_version['current'] < ADROTATE_DB_VERSION or $adrotate_version['current'] < ADROTATE_VERSION) {
        echo '<div class="updated" style="padding: 0; margin: 0; border-left: none;">';
        echo '	<div class="adrotate_banner">';
        echo '		<div class="button_div"><a class="button" href="admin.php?page=adrotate&upgrade=1">Update Now</a></div>';
        echo '		<div class="text">You have almost finished upgrading <strong>AdRotate</strong> to version <strong>' . ADROTATE_DISPLAY . '</strong>!<br /><span>To complete the update click the button on the left. This may take a few seconds to complete!</span></div>';
        echo '		<div class="icon"><img title="" src="' . plugins_url('images/adrotate-logo-60x60.png', __FILE__) . '" alt=""/></div>';
        echo '	</div>';
        echo '</div>';
    }
}
function adrotate_advertiser()
{
    global $wpdb, $current_user, $adrotate_config, $adrotate_debug;
    get_currentuserinfo();
    $status = $view = $ad_edit_id = $request = $request_id = '';
    if (isset($_GET['status'])) {
        $status = esc_attr($_GET['status']);
    }
    if (isset($_GET['view'])) {
        $view = esc_attr($_GET['view']);
    }
    if (isset($_GET['ad'])) {
        $ad_edit_id = esc_attr($_GET['ad']);
    }
    if (isset($_GET['file'])) {
        $filename = esc_attr($_GET['file']);
    }
    if (isset($_GET['request'])) {
        $request = esc_attr($_GET['request']);
    }
    if (isset($_GET['id'])) {
        $request_id = esc_attr($_GET['id']);
    }
    $now = adrotate_now();
    $today = adrotate_date_start('day');
    $in2days = $now + 172800;
    $in7days = $now + 604800;
    $in84days = $now + 7257600;
    if (isset($_GET['month']) and isset($_GET['year'])) {
        $month = esc_attr($_GET['month']);
        $year = esc_attr($_GET['year']);
    } else {
        $month = date("m");
        $year = date("Y");
    }
    $monthstart = mktime(0, 0, 0, $month, 1, $year);
    $monthend = mktime(0, 0, 0, $month + 1, 0, $year);
    ?>
	<div class="wrap">
	  	<h1><?php 
    _e('Advertiser', 'adrotate-pro');
    ?>
</h1>

		<?php 
    if ($status > 0) {
        adrotate_status($status, array('file' => $filename));
    }
    ?>

		<div class="tablenav">
			<div class="alignleft actions">
				<a class="row-title" href="<?php 
    echo admin_url('/admin.php?page=adrotate-advertiser');
    ?>
"><?php 
    _e('Manage', 'adrotate-pro');
    ?>
</a>
				<?php 
    if ($adrotate_config['enable_editing'] == 'Y') {
        ?>
				 | <a class="row-title" href="<?php 
        echo admin_url('/admin.php?page=adrotate-advertiser&view=addnew');
        ?>
"><?php 
        _e('Add New', 'adrotate-pro');
        ?>
</a> 
				<?php 
    }
    ?>
			</div>
		</div>

		<?php 
    $wpnonceaction = 'adrotate_email_advertiser_' . $request_id;
    if ($view == "") {
        $ads = $wpdb->get_results($wpdb->prepare("SELECT `ad` FROM `{$wpdb->prefix}adrotate_linkmeta` WHERE `group` = 0 AND `user` = %d ORDER BY `ad` ASC;", $current_user->ID));
        if ($ads) {
            $activebanners = $queuebanners = $disabledbanners = false;
            foreach ($ads as $ad) {
                $banner = $wpdb->get_row("SELECT `id`, `title`, `type`, `mobile`, `tablet`, `budget`, `crate`, `irate` FROM `{$wpdb->prefix}adrotate` WHERE (`type` = 'active' OR `type` = '2days' OR `type` = '7days' OR `type` = 'disabled' OR `type` = 'error' OR `type` = 'a_error' OR `type` = 'expired' OR `type` = 'queue' OR `type` = 'reject') AND `id` = '" . $ad->ad . "';");
                // Skip if no ad
                if (!$banner) {
                    continue;
                }
                $starttime = $stoptime = 0;
                $starttime = $wpdb->get_var("SELECT `starttime` FROM `{$wpdb->prefix}adrotate_schedule`, `{$wpdb->prefix}adrotate_linkmeta` WHERE `ad` = '" . $banner->id . "' AND `schedule` = `{$wpdb->prefix}adrotate_schedule`.`id` ORDER BY `starttime` ASC LIMIT 1;");
                $stoptime = $wpdb->get_var("SELECT `stoptime` FROM `{$wpdb->prefix}adrotate_schedule`, `{$wpdb->prefix}adrotate_linkmeta` WHERE `ad` = '" . $banner->id . "' AND `schedule` = `{$wpdb->prefix}adrotate_schedule`.`id` ORDER BY `stoptime` DESC LIMIT 1;");
                $type = $banner->type;
                if ($type == 'active' and $stoptime <= $in7days) {
                    $type = '7days';
                }
                if ($type == 'active' and $stoptime <= $in2days) {
                    $type = '2days';
                }
                if ($type == 'active' and $stoptime <= $now) {
                    $type = 'expired';
                }
                if ($type == 'active' or $type == '2days' or $type == '7days' or $type == 'expired') {
                    $activebanners[$banner->id] = array('id' => $banner->id, 'title' => $banner->title, 'type' => $type, 'mobile' => $banner->mobile, 'tablet' => $banner->tablet, 'firstactive' => $starttime, 'lastactive' => $stoptime, 'budget' => $banner->budget, 'crate' => $banner->crate, 'irate' => $banner->irate);
                }
                if ($type == 'disabled') {
                    $disabledbanners[$banner->id] = array('id' => $banner->id, 'title' => $banner->title, 'type' => $type);
                }
                if ($type == 'queue' or $type == 'reject' or $type == 'error' or $type == 'a_error') {
                    $queuebanners[$banner->id] = array('id' => $banner->id, 'title' => $banner->title, 'type' => $type, 'mobile' => $banner->mobile, 'tablet' => $banner->tablet, 'budget' => $banner->budget, 'crate' => $banner->crate, 'irate' => $banner->irate);
                }
            }
            // Show active ads, if any
            if ($activebanners) {
                include "dashboard/advertiser/main.php";
            }
            // Show disabled ads, if any
            if ($disabledbanners) {
                include "dashboard/advertiser/main-disabled.php";
            }
            // Show queued ads, if any
            if ($queuebanners) {
                include "dashboard/advertiser/main-queue.php";
            }
            if ($adrotate_config['stats'] == 1) {
                // Gather data for summary report
                $summary = adrotate_prepare_advertiser_report($current_user->ID, $activebanners);
                include "dashboard/advertiser/main-summary.php";
            }
        } else {
            ?>
				<table class="widefat" style="margin-top: .5em">
					<thead>
						<tr>
							<th><?php 
            _e('Notice', 'adrotate-pro');
            ?>
</th>
						</tr>
					</thead>
					<tbody>
					    <tr>
							<td><?php 
            _e('No ads for user.', 'adrotate-pro');
            ?>
</td>
						</tr>
					</tbody>
				</table>
				<?php 
        }
    } else {
        if ($view == "addnew" or $view == "edit") {
            include "dashboard/advertiser/edit.php";
        } else {
            if ($view == "report") {
                include "dashboard/advertiser/report.php";
            } else {
                if ($view == "message") {
                    if (wp_verify_nonce($_REQUEST['_wpnonce'], $wpnonceaction)) {
                        include "dashboard/advertiser/message.php";
                    } else {
                        adrotate_nonce_error();
                        exit;
                    }
                }
            }
        }
    }
    ?>
		<br class="clear" />

		<?php 
    adrotate_user_notice();
    ?>

		<br class="clear" />
	</div>
<?php 
}
function adrotate_mail_message()
{
    if (wp_verify_nonce($_POST['adrotate_nonce'], 'adrotate_email_advertiser') or wp_verify_nonce($_POST['adrotate_nonce'], 'adrotate_email_moderator')) {
        $notifications = get_option("adrotate_notifications");
        $id = $_POST['adrotate_id'];
        $request = $_POST['adrotate_request'];
        $author = $_POST['adrotate_username'];
        $useremail = $_POST['adrotate_email'];
        $text = strip_tags(stripslashes(trim($_POST['adrotate_message'], "\t\n ")));
        if (strlen($text) < 1) {
            $text = "";
        }
        $emails = $notifications['notification_email_advertiser'];
        $x = count($emails);
        if ($x == 0) {
            $emails = array(get_option('admin_email'));
        }
        $siteurl = get_option('siteurl');
        $adurl = $siteurl . "/wp-admin/admin.php?page=adrotate-ads&view=edit&ad=" . $id;
        $pluginurl = "https://ajdg.solutions/products/adrotate-for-wordpress/";
        $now = adrotate_now();
        if ($request == "renew") {
            $subject = __('[AdRotate] An advertiser has put in a request for renewal!', 'adrotate-pro');
        }
        if ($request == "remove") {
            $subject = __('[AdRotate] An advertiser wants his ad removed.', 'adrotate-pro');
        }
        if ($request == "other") {
            $subject = __('[AdRotate] An advertiser wrote a comment on his ad!', 'adrotate-pro');
        }
        if ($request == "issue") {
            $subject = __('[AdRotate] An advertiser has a problem!', 'adrotate-pro');
        }
        $message = "<p>Hello,</p>";
        if ($request == "renew") {
            $message .= "<p>{$author} " . __('requests ad', 'adrotate-pro') . " <strong>{$id}</strong> " . __('renewed!', 'adrotate-pro') . "</p>";
        }
        if ($request == "remove") {
            $message .= "<p>{$author} " . __('requests ad', 'adrotate-pro') . " <strong>{$id}</strong> " . __('removed.', 'adrotate-pro') . "</p>";
        }
        if ($request == "other") {
            $message .= "<p>{$author} " . __('has something to say about ad', 'adrotate-pro') . " <strong>{$id}</strong>.</p>";
        }
        if ($request == "issue") {
            $message .= "<p>{$author} " . __('has a problem with AdRotate.', 'adrotate-pro') . "</p>";
        }
        $message .= "<p>" . __('Attached message:', 'adrotate-pro') . " {$text}</p>";
        $message .= "<p>" . __('You can reply to this message to contact', 'adrotate-pro') . " {$author}.<br />";
        if ($request != "issue") {
            $message .= __('Review the ad here:', 'adrotate-pro') . " {$adurl}";
        }
        $message .= "</p>";
        $message .= "<p>" . __('Have a nice day!', 'adrotate-pro') . "<br />";
        $message .= __('Your AdRotate Notifier', 'adrotate-pro') . "<br />";
        $message .= "{$pluginurl}</p>";
        for ($i = 0; $i < $x; $i++) {
            $headers = "Content-Type: text/html; charset=UTF-8" . "\r\n" . "From: {$author} <{$useremail}>" . "\r\n";
            wp_mail($emails[$i], $subject, $message, $headers);
        }
        adrotate_return('adrotate-advertiser', 300);
    } else {
        adrotate_nonce_error();
        exit;
    }
}
function adrotate_advertiser_insert_input()
{
    global $wpdb, $adrotate_config;
    if (wp_verify_nonce($_POST['adrotate_nonce'], 'adrotate_save_ad')) {
        // Mandatory
        $id = $author = $title = $bannercode = '';
        if (isset($_POST['adrotate_id'])) {
            $id = $_POST['adrotate_id'];
        }
        if (isset($_POST['adrotate_username'])) {
            $author = $_POST['adrotate_username'];
        }
        if (isset($_POST['adrotate_title'])) {
            $title = strip_tags(htmlspecialchars(trim($_POST['adrotate_title'], "\t\n "), ENT_QUOTES));
        }
        if (isset($_POST['adrotate_bannercode'])) {
            $bannercode = htmlspecialchars(trim($_POST['adrotate_bannercode'], "\t\n "), ENT_QUOTES);
        }
        $thetime = adrotate_now();
        // Schedule and timeframe variables
        $schedules = $groups = $group_array = '';
        if (isset($_POST['scheduleselect'])) {
            $schedules = $_POST['scheduleselect'];
        }
        if (isset($_POST['groupselect'])) {
            $groups = $_POST['groupselect'];
        }
        // GeoTargeting
        $cities = '';
        $countries = array();
        if (isset($_POST['adrotate_geo_cities'])) {
            $cities = trim($_POST['adrotate_geo_cities'], "\t\n ");
        }
        if (isset($_POST['adrotate_geo_countries'])) {
            $countries = $_POST['adrotate_geo_countries'];
        }
        if (isset($_POST['adrotate_geo_westeurope'])) {
            $countries_westeurope = array('AD', 'AT', 'BE', 'DK', 'FR', 'DE', 'GR', 'IS', 'IE', 'IT', 'LI', 'LU', 'MT', 'MC', 'NL', 'NO', 'PT', 'SM', 'ES', 'SE', 'CH', 'VA', 'GB');
        }
        if (isset($_POST['adrotate_geo_easteurope'])) {
            $countries_easteurope = array('AL', 'AM', 'AZ', 'BY', 'BA', 'BG', 'HR', 'CY', 'CZ', 'EE', 'FI', 'GE', 'HU', 'LV', 'LT', 'MK', 'MD', 'PL', 'RO', 'RS', 'SK', 'SI', 'TR', 'UA');
        }
        if (isset($_POST['adrotate_geo_northamerica'])) {
            $countries_northamerica = array('AG', 'BS', 'BB', 'BZ', 'CA', 'CR', 'CU', 'DM', 'DO', 'SV', 'GD', 'GT', 'HT', 'HN', 'JM', 'MX', 'NI', 'PA', 'KN', 'LC', 'VC', 'TT', 'US');
        }
        if (isset($_POST['adrotate_geo_southamerica'])) {
            $countries_southamerica = array('AR', 'BO', 'BR', 'CL', 'CO', 'EC', 'GY', 'PY', 'PE', 'SR', 'UY', 'VE');
        }
        if (isset($_POST['adrotate_geo_southeastasia'])) {
            $countries_southeastasia = array('AU', 'BN', 'KH', 'TL', 'ID', 'LA', 'MY', 'MM', 'NZ', 'PH', 'SG', 'TH', 'VN');
        }
        // Ad options
        $adrotate_image_current = $type = $weight = '';
        if (isset($_POST['adrotate_image_current'])) {
            $adrotate_image_current = strip_tags(htmlspecialchars(trim($_POST['adrotate_image_current'], "\t\n "), ENT_QUOTES));
        }
        if (isset($_POST['adrotate_type'])) {
            $type = strip_tags(htmlspecialchars(trim($_POST['adrotate_type'], "\t\n "), ENT_QUOTES));
        }
        if (isset($_POST['adrotate_desktop'])) {
            $desktop = strip_tags(trim($_POST['adrotate_desktop'], "\t\n "));
        }
        if (isset($_POST['adrotate_mobile'])) {
            $mobile = strip_tags(trim($_POST['adrotate_mobile'], "\t\n "));
        }
        if (isset($_POST['adrotate_tablet'])) {
            $tablet = strip_tags(trim($_POST['adrotate_tablet'], "\t\n "));
        }
        if (isset($_POST['adrotate_weight'])) {
            $weight = $_POST['adrotate_weight'];
        }
        if (current_user_can('adrotate_advertiser')) {
            if (strlen($title) < 1) {
                $title = 'Ad ' . $id;
            }
            if ($_FILES["adrotate_image"]["size"] > 0) {
                $allowedExts = array("jpg", "jpeg", "gif", "png", "flv", "swf");
                $filename = sanitize_file_name(strtolower($_FILES["adrotate_image"]["name"]));
                $extension = explode(".", $_FILES["adrotate_image"]["name"]);
                $extension = end($extension);
                $image_path = ABSPATH . $adrotate_config['banner_folder'];
                if (($_FILES["adrotate_image"]["type"] == "image/gif" or $_FILES["adrotate_image"]["type"] == "image/jpeg" or $_FILES["adrotate_image"]["type"] == "image/pjpeg" or $_FILES["adrotate_image"]["type"] == "image/jpg" or $_FILES["adrotate_image"]["type"] == "image/png" or $_FILES["adrotate_image"]["type"] == "application/x-shockwave-flash" or $_FILES["adrotate_image"]["type"] == "video/x-flv" or $_FILES["adrotate_image"]["size"] <= 512000) and in_array($extension, $allowedExts)) {
                    if ($_FILES["adrotate_image"]["error"] > 0) {
                        if ($_FILES["adrotate_image"]["error"] == 1 or $_FILES["adrotate_image"]["error"] == 2) {
                            $errorcode = __("File size exceeded.", "adrotate");
                        } else {
                            if ($_FILES["adrotate_image"]["error"] == 3) {
                                $errorcode = __("Upload incomplete.", "adrotate");
                            } else {
                                if ($_FILES["adrotate_image"]["error"] == 4) {
                                    $errorcode = __("No file uploaded.", "adrotate");
                                } else {
                                    if ($_FILES["adrotate_image"]["error"] == 6 or $_FILES["adrotate_image"]["error"] == 7) {
                                        $errorcode = __("Could not write file to server.", "adrotate");
                                    } else {
                                        $errorcode = __("An unknown error occured, contact staff.", "adrotate");
                                    }
                                }
                            }
                        }
                        wp_die("<h3>" . __("Something went wrong!", "adrotate") . "</h3><p>" . __("Go back and try again. If the error persists, contact staff.", "adrotate") . "</p><p style='color: #f00;'>" . $errorcode . "</p>");
                    } else {
                        $image_name = $id . "-" . $author . "-" . $thetime . "-" . $filename;
                        move_uploaded_file($_FILES["adrotate_image"]["tmp_name"], $image_path . $image_name);
                    }
                } else {
                    wp_die("<h3>" . __("Something went wrong!", "adrotate") . "</h3><p>" . __("Go back and try again. If the error persists, contact staff.", "adrotate") . "</p><p style='color: #f00;'>" . __("The file was either too large or not in the right format.", "adrotate") . "</p>");
                }
            } else {
                $image_name = $adrotate_image_current;
            }
            // Force image location
            $image = site_url() . "/%folder%" . $image_name;
            // Determine image settings ($image_field has priority!)
            if (strlen($image_name) > 0) {
                $imagetype = "dropdown";
                $image = site_url() . "/%folder%" . $image_name;
            } else {
                $imagetype = "";
                $image = "";
            }
            // Set desktop value
            if (isset($desktop) and strlen($desktop) != 0) {
                $desktop = 'Y';
            } else {
                $desktop = 'N';
            }
            // Set mobile value
            if (isset($mobile) and strlen($mobile) != 0) {
                $mobile = 'Y';
            } else {
                $mobile = 'N';
            }
            // Set tablet value
            if (isset($tablet) and strlen($tablet) != 0) {
                $tablet = 'Y';
            } else {
                $tablet = 'N';
            }
            // Geo Targeting
            if (strlen($cities) > 0) {
                $cities = explode(",", strtolower($cities));
                foreach ($cities as $key => $value) {
                    $cities_clean[] = trim($value);
                    unset($value);
                }
                unset($cities);
                $cities = serialize($cities_clean);
            }
            $countries = array_merge($countries, $countries_westeurope, $countries_easteurope, $countries_northamerica, $countries_southamerica, $countries_southeastasia);
            $countries = array_unique($countries);
            if (count($countries) == 0) {
                $countries = serialize(array());
            } else {
                foreach ($countries as $key => $value) {
                    $countries_clean[] = trim($value);
                    unset($value);
                }
                unset($countries);
                $countries = serialize($countries_clean);
            }
            // Fetch schedules for the ad
            $schedulemeta = $wpdb->get_results($wpdb->prepare("SELECT `schedule` FROM `" . $wpdb->prefix . "adrotate_linkmeta` WHERE `ad` = %d AND `group` = 0 AND `user` = 0;", $id));
            $schedule_array = array();
            foreach ($schedulemeta as $meta) {
                $schedule_array[] = $meta->schedule;
                unset($meta);
            }
            // Add new schedules to this ad
            if (!is_array($schedules)) {
                $schedules = array();
            }
            $insert = array_diff($schedules, $schedule_array);
            foreach ($insert as &$value) {
                $wpdb->insert($wpdb->prefix . 'adrotate_linkmeta', array('ad' => $id, 'group' => 0, 'user' => 0, 'schedule' => $value));
            }
            unset($insert, $value);
            // Remove schedules from this ad
            $delete = array_diff($schedule_array, $schedules);
            foreach ($delete as &$value) {
                $wpdb->query($wpdb->prepare("DELETE FROM `" . $wpdb->prefix . "adrotate_linkmeta` WHERE `ad` = %d AND `group` = 0 AND `user` = 0 AND `schedule` = %d;", $id, $value));
            }
            unset($delete, $value, $schedulemeta, $schedule_array);
            // Fetch group records for the ad
            $groupmeta = $wpdb->get_results($wpdb->prepare("SELECT `group` FROM `" . $wpdb->prefix . "adrotate_linkmeta` WHERE `ad` = %d AND `user` = 0 AND `schedule` = 0;", $id));
            $group_array = array();
            foreach ($groupmeta as $meta) {
                $group_array[] = $meta->group;
                unset($meta);
            }
            // Add new groups to this ad
            if (!is_array($groups)) {
                $groups = array();
            }
            $insert = array_diff($groups, $group_array);
            foreach ($insert as &$value) {
                $wpdb->insert($wpdb->prefix . 'adrotate_linkmeta', array('ad' => $id, 'group' => $value, 'user' => 0, 'schedule' => 0));
            }
            unset($insert, $value);
            // Remove groups from this ad
            $delete = array_diff($group_array, $groups);
            foreach ($delete as &$value) {
                $wpdb->query($wpdb->prepare("DELETE FROM `" . $wpdb->prefix . "adrotate_linkmeta` WHERE `ad` = %d AND `group` = %d AND `user` = 0 AND `schedule` = 0;", $id, $value));
            }
            unset($delete, $value, $groupmeta, $group_array);
            // Save the ad to the DB
            $wpdb->update($wpdb->prefix . 'adrotate', array('title' => $title, 'bannercode' => $bannercode, 'updated' => $thetime, 'author' => $author, 'imagetype' => $imagetype, 'image' => $image, 'desktop' => $desktop, 'mobile' => $mobile, 'tablet' => $tablet, 'weight' => $weight, 'cities' => $cities, 'countries' => $countries), array('id' => $id));
            // Determine status of ad
            $adstate = adrotate_evaluate_ad($id);
            if ($adstate == 'error' or $adstate == 'expired') {
                $action = 502;
                $active = 'a_error';
            } else {
                $action = 306;
                $active = 'queue';
            }
            $wpdb->update($wpdb->prefix . 'adrotate', array('type' => $active), array('id' => $id));
            if ($action == 306) {
                adrotate_push_notifications('queued', $id);
            }
            // Fetch records for the ad, see if a publisher is set
            $linkmeta = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM `" . $wpdb->prefix . "adrotate_linkmeta` WHERE `ad` = %d AND `group` = 0 AND `user` > 0;", $id));
            $advertiser = wp_get_current_user();
            // Add/update publisher on this ad
            if ($linkmeta == 0 and $advertiser->ID > 0) {
                $wpdb->insert($wpdb->prefix . 'adrotate_linkmeta', array('ad' => $id, 'group' => 0, 'user' => $advertiser->ID, 'schedule' => 0));
            }
            if ($linkmeta == 1 and $advertiser->ID > 0) {
                $wpdb->query($wpdb->prepare("UPDATE `" . $wpdb->prefix . "adrotate_linkmeta` SET `user` = {$advertiser->ID} WHERE `ad` = %d AND `group` = 0 AND `schedule` = 0;", $id));
            }
            adrotate_return('adrotate-advertiser', $action);
            exit;
        } else {
            adrotate_return('adrotate-advertiser', 500);
        }
    } else {
        adrotate_nonce_error();
        exit;
    }
}
function adrotate_renew($id, $howlong = 2592000)
{
    global $wpdb;
    if ($id > 0) {
        $schedule_id = $wpdb->get_var($wpdb->prepare("SELECT `schedule` FROM `{$wpdb->prefix}adrotate_linkmeta` WHERE `ad` = %d AND `group` = 0 AND `user` = 0 ORDER BY `id` DESC LIMIT 1;", $id));
        if ($schedule_id > 0) {
            $schedule = $wpdb->get_row($wpdb->prepare("SELECT * FROM `{$wpdb->prefix}adrotate_schedule` WHERE `id` = %d ORDER BY `id` DESC LIMIT 1;", $schedule_id));
            $stoptime = $schedule->stoptime + $howlong;
            $wpdb->insert($wpdb->prefix . 'adrotate_schedule', array('name' => 'Schedule for ad ' . $id, 'starttime' => $schedule->stoptime, 'stoptime' => $stoptime, 'maxclicks' => $schedule->maxclicks, 'maximpressions' => $schedule->maximpressions, 'spread' => $schedule->spread, 'dayimpressions' => $schedule->dayimpressions, 'daystarttime' => $schedule->daystarttime, 'daystoptime' => $schedule->daystoptime, 'day_mon' => $schedule->day_mon, 'day_tue' => $schedule->day_tue, 'day_wed' => $schedule->day_wed, 'day_thu' => $schedule->day_thu, 'day_fri' => $schedule->day_fri, 'day_sat' => $schedule->day_sat, 'day_sun' => $schedule->day_sun));
        } else {
            $now = adrotate_now();
            $stoptime = $now + $howlong;
            $wpdb->insert($wpdb->prefix . 'adrotate_schedule', array('name' => 'Schedule for ad ' . $id, 'starttime' => $now, 'stoptime' => $stoptime, 'maxclicks' => 0, 'maximpressions' => 0, 'spread' => 'N', 'dayimpressions' => 0, 'daystarttime' => '0000', 'daystoptime' => '0000', 'day_mon' => 'Y', 'day_tue' => 'Y', 'day_wed' => 'Y', 'day_thu' => 'Y', 'day_fri' => 'Y', 'day_sat' => 'Y', 'day_sun' => 'Y'));
        }
        $wpdb->insert($wpdb->prefix . 'adrotate_linkmeta', array('ad' => $id, 'group' => 0, 'user' => 0, 'schedule' => $wpdb->insert_id));
    }
}
    }
}
/*
 *  Send Bulk mails to user request condition check
 */
if (isset($_POST['adrotate_email_nonce']) && wp_verify_nonce($_POST['adrotate_email_nonce'], 'adrotate_email_ad_active')) {
    if (isset($_POST['adrotate_action']) && $_POST['adrotate_action'] != '' && isset($_POST['bannercheck']) && isset($_POST['dsp_report_gen_month']) && $_POST['dsp_report_gen_month'] != '') {
        $month = $_POST['dsp_report_gen_month'];
        $dsp_email1 = new Ad_Rotate_Email_Addon_Send_Email();
        foreach ($_POST['bannercheck'] as $ad_id) {
            $dsp_email1->send_email($ad_id, $month);
        }
    }
}
global $wpdb, $current_user, $userdata, $adrotate_config, $adrotate_debug;
$now = adrotate_now();
$today = adrotate_date_start('day');
$in2days = $now + 172800;
$in7days = $now + 604800;
$in84days = $now + 7257600;
if (isset($_GET['month']) and isset($_GET['year'])) {
    $month = esc_attr($_GET['month']);
    $year = esc_attr($_GET['year']);
} else {
    $month = date("m");
    $year = date("Y");
}
$monthstart = mktime(0, 0, 0, $month, 1, $year);
$monthend = mktime(0, 0, 0, $month + 1, 0, $year);
?>
	<div class="wrap">
function adrotate_load_adverts($user_id)
{
    global $wpdb;
    $now = adrotate_now();
    $today = adrotate_date_start('day');
    $in2days = $now + 172800;
    $in7days = $now + 604800;
    $in84days = $now + 7257600;
    $ads = $wpdb->get_results($wpdb->prepare("SELECT `ad` FROM `" . $wpdb->prefix . "adrotate_linkmeta` WHERE `group` = 0 AND `user` = %d ORDER BY `ad` ASC;", $user_id));
    if ($ads) {
        $adverts = array('active' => array(), 'disabled' => array(), 'queued' => array());
        foreach ($ads as $ad) {
            $banner = $wpdb->get_row("SELECT `id`, `title`, `type`, `tracker` FROM `" . $wpdb->prefix . "adrotate` WHERE (`type` = 'active' OR `type` = '2days' OR `type` = '7days' OR `type` = 'disabled' OR `type` = 'error' OR `type` = 'a_error' OR `type` = 'expired' OR `type` = 'queue' OR `type` = 'reject') AND `id` = '" . $ad->ad . "';");
            // Skip if no ad
            if (!$banner) {
                continue;
            }
            $starttime = $stoptime = 0;
            $starttime = $wpdb->get_var("SELECT `starttime` FROM `" . $wpdb->prefix . "adrotate_schedule`, `" . $wpdb->prefix . "adrotate_linkmeta` WHERE `ad` = '" . $banner->id . "' AND `schedule` = `" . $wpdb->prefix . "adrotate_schedule`.`id` ORDER BY `starttime` ASC LIMIT 1;");
            $stoptime = $wpdb->get_var("SELECT `stoptime` FROM `" . $wpdb->prefix . "adrotate_schedule`, `" . $wpdb->prefix . "adrotate_linkmeta` WHERE `ad` = '" . $banner->id . "' AND `schedule` = `" . $wpdb->prefix . "adrotate_schedule`.`id` ORDER BY `stoptime` DESC LIMIT 1;");
            $type = $banner->type;
            if ($type == 'active' and $stoptime <= $in7days) {
                $type = '7days';
            }
            if ($type == 'active' and $stoptime <= $in2days) {
                $type = '2days';
            }
            if ($type == 'active' and $stoptime <= $now) {
                $type = 'expired';
            }
            if ($type == 'active' or $type == '2days' or $type == '7days' or $type == 'expired') {
                $adverts['active'][$banner->id] = array('id' => $banner->id, 'title' => stripslashes(html_entity_decode($banner->title)), 'type' => $type, 'tracker' => $banner->tracker, 'firstactive' => $starttime, 'lastactive' => $stoptime);
            }
            if ($type == 'disabled') {
                $adverts['disabled'][$banner->id] = array('id' => $banner->id, 'title' => stripslashes(html_entity_decode($banner->title)), 'type' => $type);
            }
            if ($type == 'queue' or $type == 'reject' or $type == 'error' or $type == 'a_error') {
                $adverts['queued'][$banner->id] = array('id' => $banner->id, 'title' => stripslashes(html_entity_decode($banner->title)), 'type' => $type);
            }
        }
        return $adverts;
    }
}