示例#1
0
function bsaAddNewAd()
{
    $plugin_id = 'bsa_pro_plugin_';
    if ($_SERVER["REQUEST_METHOD"] == "POST" && $_POST["bsaProAction"] == 'updateAd') {
        if (isset($_SESSION['bsa_ad_' . $_GET['ad_id']])) {
            unset($_SESSION['bsa_ad_' . $_GET['ad_id']]);
            // Reset cache
        }
        // validate form
        foreach (explode(',', str_replace('desc', 'description', $_POST['inputs_required'])) as $input) {
            $error = FALSE;
            if ($input == 'img') {
                if ($_FILES['img']["name"] == '') {
                    $error = FALSE;
                    // img not required for updateAd Action
                }
            } else {
                if ($_POST[$input] == '') {
                    $error = TRUE;
                }
            }
            if ($error == TRUE) {
                echo '
				<div class="updated settings-error">
					<p><strong>Ad not saved.</strong> The ' . str_replace(',', ', ', str_replace('desc', 'description', $_POST['inputs_required'])) . ' fields are required!</p>
				</div>';
                return;
            }
        }
        if ($_POST["buyer_email"] != '') {
            // if isset img
            $uploadName = strtolower($_FILES["img"]["name"]);
            if ($uploadName) {
                $allowedExts = array("gif", "jpeg", "jpg", "png");
                $temp = explode(".", $uploadName);
                $extension = end($temp);
                $fileName = NULL;
                if (($_FILES["img"]["type"] == "image/gif" || $_FILES["img"]["type"] == "image/jpeg" || $_FILES["img"]["type"] == "image/jpg" || $_FILES["img"]["type"] == "image/pjpeg" || $_FILES["img"]["type"] == "image/x-png" || $_FILES["img"]["type"] == "image/png") && $_FILES["img"]["error"] == 0 && in_array($extension, $allowedExts)) {
                    $fileName = time() . '-' . $uploadName;
                    $path = bsa_upload_url('basedir') . $fileName;
                    $thumbLoc = $_FILES["img"]["tmp_name"];
                    list($width, $height) = getimagesize($thumbLoc);
                    $maxSize = get_option($plugin_id . 'thumb_size');
                    $maxWidth = get_option($plugin_id . 'thumb_w');
                    $maxHeight = get_option($plugin_id . 'thumb_h');
                    if ($_FILES["img"]["size"] > $maxSize * 1024 or $width > $maxWidth or $height > $maxHeight) {
                        echo '
						<div class="updated settings-error">
							<p><strong>Ad not saved.</strong> Images was too high.</p>
						</div>';
                        return;
                    } else {
                        // save img
                        move_uploaded_file($thumbLoc, $path);
                    }
                } else {
                    echo '
					<div class="updated settings-error">
						<p><strong>Ad not saved.</strong> Type of image invalid.</p>
					</div>
					';
                    return;
                }
            } else {
                $fileName = NULL;
            }
            $limit = bsa_ad($_GET['ad_id'], 'ad_limit');
            if (isset($_POST["increase_limit"]) && $_POST["increase_limit"] != '') {
                if ($_POST["increase_limit"] > 0 || $_POST["increase_limit"] < 0) {
                    // increase / decrease limit
                    if (bsa_ad($_GET['ad_id'], 'ad_model') == 'cpd') {
                        $time = time();
                        $increase = $_POST["increase_limit"] * 24 * 60 * 60;
                        $diff = $limit - $time;
                        $increase_limit = $diff <= 0 ? $time + $increase : $limit + $increase;
                    } else {
                        $increase_limit = $limit + $_POST["increase_limit"];
                    }
                } else {
                    $increase_limit = bsa_ad($_GET['ad_id'], 'ad_limit');
                }
            } else {
                $increase_limit = null;
            }
            //			var_dump(stripslashes( wp_filter_post_kses( addslashes( $_POST["html"] ) ) ));
            if (bsa_role() == 'user') {
                $status = get_option('bsa_pro_plugin_auto_accept') == 'no' ? 'pending' : null;
                $increase_limit = null;
            } else {
                $status = null;
            }
            $capping = $_POST["capping"] > 0 ? number_format($_POST["capping"], 0, '', '') : 0;
            $model = new BSA_PRO_Model();
            $model->updateAd($_GET['ad_id'], $_POST["buyer_email"], $_POST["title"], $_POST["description"], $_POST["url"], $fileName, stripslashes($_POST["html"]), $capping, $increase_limit, $status);
            unset($_SESSION['bsa_ad_' . $_GET['ad_id']]);
            // Reset cache
            echo '
						<div class="updated settings-error">
							<p><strong>Success!</strong> Ad saved.</p>
						</div>';
        } else {
            echo '
			<div class="updated settings-error">
				<p><strong>Ad not saved.</strong> The buyer email field is required!</p>
			</div>';
        }
    } elseif ($_SERVER["REQUEST_METHOD"] == "POST" && $_POST["bsaProAction"] == 'addNewAd') {
        // validate form
        foreach (explode(',', str_replace('desc', 'description', $_POST['inputs_required'])) as $input) {
            $error = FALSE;
            if ($input == 'img') {
                if ($_FILES['img']["name"] == '') {
                    $error = TRUE;
                }
            } else {
                if ($_POST[$input] == '') {
                    $error = TRUE;
                }
            }
            if ($error == TRUE) {
                echo '
				<div class="updated settings-error">
					<p><strong>Ad not saved.</strong> The ' . str_replace(',', ', ', str_replace('desc', 'description', $_POST['inputs_required'])) . ' fields are required!</p>
				</div>';
                return;
            }
        }
        if (isset($_POST["buyer_email"]) && $_POST["buyer_email"] != '' && isset($_POST["space_id"]) && $_POST["space_id"] != '' && isset($_POST["ad_model"]) && $_POST["ad_model"] != '' && isset($_POST["ad_limit_" . $_POST["ad_model"]]) && $_POST["ad_limit_" . $_POST["ad_model"]] != '') {
            // if isset img
            if ($_FILES['img']["name"]) {
                $allowedExts = array("gif", "jpeg", "jpg", "png", "GIF", "JPEG", "JPG", "PNG");
                $temp = explode(".", $_FILES["img"]["name"]);
                $extension = end($temp);
                $fileName = NULL;
                if (($_FILES["img"]["type"] == "image/gif" || $_FILES["img"]["type"] == "image/jpeg" || $_FILES["img"]["type"] == "image/jpg" || $_FILES["img"]["type"] == "image/pjpeg" || $_FILES["img"]["type"] == "image/x-png" || $_FILES["img"]["type"] == "image/png") && $_FILES["img"]["error"] == 0 && in_array($extension, $allowedExts)) {
                    $fileName = time() . '-' . $_FILES["img"]["name"];
                    $path = bsa_upload_url('basedir') . $fileName;
                    $thumbLoc = $_FILES["img"]["tmp_name"];
                    list($width, $height) = getimagesize($thumbLoc);
                    $maxSize = get_option($plugin_id . 'thumb_size');
                    $maxWidth = get_option($plugin_id . 'thumb_w');
                    $maxHeight = get_option($plugin_id . 'thumb_h');
                    if ($_FILES["img"]["size"] > $maxSize * 1024 or $width > $maxWidth or $height > $maxHeight) {
                        echo '
						<div class="updated settings-error">
							<p><strong>Ad not saved.</strong> Images was too high.</p>
						</div>';
                        return;
                    } else {
                        // save img
                        move_uploaded_file($thumbLoc, $path);
                    }
                } else {
                    echo '
					<div class="updated settings-error">
						<p><strong>Ad not saved.</strong> Type of image invalid.</p>
					</div>
					';
                    return;
                }
            } else {
                $fileName = '';
            }
            // set limit for cpd - change days to timestamp
            if ($_POST["ad_model"] == 'cpd') {
                $ad_limit = time() + $_POST["ad_limit_" . $_POST["ad_model"]] * 24 * 60 * 60;
            } else {
                $ad_limit = $_POST["ad_limit_" . $_POST["ad_model"]];
            }
            $model = new BSA_PRO_Model();
            if (bsa_role() == 'user') {
                $status = get_option('bsa_pro_plugin_auto_accept') == 'no' ? 'pending' : 'active';
            } else {
                $status = 'active';
            }
            $capping = $_POST["capping"] > 0 ? number_format($_POST["capping"], 0, '', '') : 0;
            $model->addNewAd(NULL, $_POST["space_id"], $_POST["buyer_email"], $_POST["title"], $_POST["description"], $_POST["url"], $fileName, stripslashes($_POST["html"]), $capping, $_POST["ad_model"], $ad_limit, 0.0, 2, $status);
            // paid 2 - Added via Admin Panel
            $_SESSION['bsa_ad_status'] = 'ad_added';
            echo '
						<div class="updated settings-error">
							<p><strong>Success!</strong> Ad saved.</p>
						</div>';
        } else {
            echo '
			<div class="updated settings-error">
				<p><strong>Ad not saved.</strong> The buyer email, space id, billing model fields are required!</p>
			</div>';
        }
    }
}