<?php
ob_start();
// Last Modified by DPT on June/1/05 to add provision for updating maximum amount limits for merchants/affiliates/admin
// Last Modified by DPT on August/1/05 to add provision for setting flatrate/percentage for click/lead/sale rate

include '../includes/session.php';
include '../includes/constants.php';
include '../includes/functions.php';
include_once '../includes/allstripslashes.php';

$partners = new partners;
$partners->connection($host, $user, $pass, $db);

$url = "index.php?Act=setpayments";

switch ($action) {

    // Modified on 16-JUNE-06
    case 'Modify Impression':
        $imprate = trim($_POST['imprate']);

        if (is_numeric($imprate)) {
            $filename = "../includes/constants.php";

            if (file_exists($filename)) {
                $contents = file($filename, FILE_IGNORE_NEW_LINES);
                $n = count($contents);

                for ($i = 0; $i < $n; $i++) {
                    $line = trim($contents[$i]);
                    if (strpos($line, '$const_imp_rate') === 0) {
                        $contents[$i] = '$const_imp_rate = "' . $imprate . '";';
                        break;
                    }
                }

                file_put_contents($filename, implode("\n", $contents));
                $msg = "Impression Rate Changed";
            } else {
                $msg = "Error: Configuration file not found.";
            }
        } else {
            $msg = "Error: Invalid entry";
        }
        break;
    // End Modify

      case 'Modify Normal Amount':
        $normal = trim($_POST['normal']);

        if (is_numeric($normal)) {
            $filename = "../includes/constants.php";

            if (file_exists($filename)) {
                $contents = file($filename, FILE_IGNORE_NEW_LINES);
                $n = count($contents);

                for ($i = 0; $i < $n; $i++) {
                    $line = trim($contents[$i]);
                    if (strpos($line, '$normal_user') === 0) {
                        $contents[$i] = '$normal_user = "' . $normal . '";';
                        break;
                    }
                }

                file_put_contents($filename, implode("\n", $contents));
                $msg = "Normal Users Subscription Rate Changed";
            } else {
                $msg = "Error: Configuration file not found.";
            }
        } else {
            $msg = "Error: Invalid entry";
        }
        break;

    case 'Modify Advanced Amount':
        $advanced = trim($_POST['advanced']);

        if (is_numeric($advanced)) {
            $filename = "../includes/constants.php";

            if (file_exists($filename)) {
                $contents = file($filename, FILE_IGNORE_NEW_LINES);
                $n = count($contents);

                for ($i = 0; $i < $n; $i++) {
                    $line = trim($contents[$i]);
                    if (strpos($line, '$advanced_user') === 0) {
                        $contents[$i] = '$advanced_user = "' . $advanced . '";';
                        break;
                    }
                }

                file_put_contents($filename, implode("\n", $contents));
                $msg = "Advanced Users Subscription Rate Changed";
            } else {
                $msg = "Error: Configuration file not found.";
            }
        } else {
            $msg = "Error: Invalid entry";
        }
        break;

       case 'Modify Minimum Amount':
        $minimum_amount1 = trim($_POST['minimumamount']);

        if (is_numeric($minimum_amount1)) {
            $filename = "../includes/constants.php";

            if (file_exists($filename)) {
                $contents = file($filename, FILE_IGNORE_NEW_LINES);
                $n = count($contents);

                for ($i = 0; $i < $n; $i++) {
                    $line = trim($contents[$i]);
                    if (strpos($line, '$minimum_amount') === 0) {
                        $contents[$i] = '$minimum_amount = "' . $minimum_amount1 . '";';
                        break;
                    }
                }

                file_put_contents($filename, implode("\n", $contents));
                $msg = "Minimum Merchant Amount Changed";
            } else {
                $msg = "Error: Configuration file not found.";
            }
        } else {
            $msg = "Error: Invalid entry";
        }
        break;

    case 'Modify Click Amount':
        $click = trim($_POST['click']);
        $click_type = $_POST['rad_click_type'];

        if (is_numeric($click)) {
            $filename = "../includes/constants.php";

            if (file_exists($filename)) {
                $contents = file($filename, FILE_IGNORE_NEW_LINES);
                $n = count($contents);

                for ($i = 0; $i < $n; $i++) {
                    $line = trim($contents[$i]);

                    if (strpos($line, '$admin_clickrate =') === 0) {
                        $contents[$i] = '$admin_clickrate = "' . $click . '";';
                    }

                    if (strpos($line, '$admin_clickrate_type =') === 0) {
                        $contents[$i] = '$admin_clickrate_type = "' . $click_type . '";';
                    }
                }

                file_put_contents($filename, implode("\n", $contents));
                $msg = "Click Rate Changed";
            } else {
                $msg = "Error: Configuration file not found.";
            }
        } else {
            $msg = "Error: Invalid entry";
        }
        break;

    case 'Modify Lead Amount':
        $lead = trim($_POST['lead']);
        $lead_type = $_POST['rad_lead_type'];

        if (is_numeric($lead)) {
            $filename = "../includes/constants.php";

            if (file_exists($filename)) {
                $contents = file($filename, FILE_IGNORE_NEW_LINES);
                $n = count($contents);

                for ($i = 0; $i < $n; $i++) {
                    $line = trim($contents[$i]);

                    if (strpos($line, '$admin_leadrate =') === 0) {
                        $contents[$i] = '$admin_leadrate = "' . $lead . '";';
                    }

                    if (strpos($line, '$admin_leadrate_type =') === 0) {
                        $contents[$i] = '$admin_leadrate_type = "' . $lead_type . '";';
                    }
                }

                file_put_contents($filename, implode("\n", $contents));
                $msg = "Lead Rate Changed";
            } else {
                $msg = "Error: Configuration file not found.";
            }
        } else {
            $msg = "Error: Invalid entry";
        }
        break;

        case 'Minimum Withdraw Amount':
        $withdraw = trim($_POST['withdraw']);

        if (is_numeric($withdraw)) {
            $filename = "../includes/constants.php";

            if (file_exists($filename)) {
                $contents = file($filename, FILE_IGNORE_NEW_LINES);
                $n = count($contents);

                for ($i = 0; $i < $n; $i++) {
                    if (strpos(trim($contents[$i]), '$minimum_withdraw =') === 0) {
                        $contents[$i] = '$minimum_withdraw = "' . $withdraw . '";';
                        break;
                    }
                }

                file_put_contents($filename, implode("\n", $contents));
                $msg = "Minimum Withdraw Amount Changed";
            } else {
                $msg = "Error: Configuration file not found.";
            }
        } else {
            $msg = "Error: Invalid entry";
        }
        break;

    case 'Modify Sale Amount':
        $sale = trim($_POST['sale']);
        $sale_type = $_POST['rad_sale_type'];

        if (is_numeric($sale)) {
            $filename = "../includes/constants.php";

            if (file_exists($filename)) {
                $contents = file($filename, FILE_IGNORE_NEW_LINES);
                $n = count($contents);

                for ($i = 0; $i < $n; $i++) {
                    $line = trim($contents[$i]);

                    if (strpos($line, '$admin_salerate =') === 0) {
                        $contents[$i] = '$admin_salerate = "' . $sale . '";';
                    }

                    if (strpos($line, '$admin_salerate_type =') === 0) {
                        $contents[$i] = '$admin_salerate_type = "' . $sale_type . '";';
                    }
                }

                file_put_contents($filename, implode("\n", $contents));
                $msg = "Sale Rate Changed";
            } else {
                $msg = "Error: Configuration file not found.";
            }
        } else {
            $msg = "Error: Invalid entry";
        }
        break;

    case 'Program Type':
        $program_type_value = trim($_POST['programtype']);

        if (is_numeric($program_type_value)) {
            $filename = "../includes/constants.php";

            if (file_exists($filename)) {
                $contents = file($filename, FILE_IGNORE_NEW_LINES);

                // Update program_type
                for ($i = 0; $i < count($contents); $i++) {
                    if (strpos(trim($contents[$i]), '$program_type =') === 0) {
                        $contents[$i] = '$program_type = "' . $program_type_value . '";';
                        break;
                    }
                }

                file_put_contents($filename, implode("\n", $contents));

                // If program type is 2 (recurring), update program_value
                if ($program_type_value == 2) {
                    $recur_value = intval(trim($_POST['recur_value']));
                    $recur_period = trim($_POST['recur_period']);
                    $value = $recur_value . " " . $recur_period;

                    $contents = file($filename, FILE_IGNORE_NEW_LINES);

                    for ($i = 0; $i < count($contents); $i++) {
                        if (strpos(trim($contents[$i]), '$program_value =') === 0) {
                            $contents[$i] = '$program_value = "' . $value . '";';
                            break;
                        }
                    }

                    file_put_contents($filename, implode("\n", $contents));
                }

                $msg = "Program Type Changed";
            } else {
                $msg = "Error: Configuration file not found.";
            }
        } else {
            $msg = "Error: Invalid entry";
        }
        break;

        case 'Membership Type':
        $membership_type_value = trim($_POST['membershiptype']);

        if (is_numeric($membership_type_value)) {
            $filename = "../includes/constants.php";

            if (file_exists($filename)) {
                $contents = file($filename, FILE_IGNORE_NEW_LINES);

                for ($i = 0; $i < count($contents); $i++) {
                    if (strpos(trim($contents[$i]), '$membership_type =') === 0) {
                        $contents[$i] = '$membership_type = "' . $membership_type_value . '";';
                        break;
                    }
                }

                file_put_contents($filename, implode("\n", $contents));

                // If Membership is recurring (type 2), update membership_value
                if ($membership_type_value == 2) {
                    $recur_value = intval(trim($_POST['recurMem_value']));
                    $recur_period = trim($_POST['recurMem_period']);
                    $value = $recur_value . ' ' . $recur_period;

                    $contents = file($filename, FILE_IGNORE_NEW_LINES);
                    for ($i = 0; $i < count($contents); $i++) {
                        if (strpos(trim($contents[$i]), '$membership_value =') === 0) {
                            $contents[$i] = '$membership_value = "' . $value . '";';
                            break;
                        }
                    }

                    file_put_contents($filename, implode("\n", $contents));
                }

                $msg = "Membership Type Changed";
            } else {
                $msg = "Error: Configuration file not found.";
            }
        } else {
            $msg = "Error: Invalid entry";
        }
        break;

    case 'Program Fee':
        $program_fee_value = trim($_POST['program']);

        if (is_numeric($program_fee_value)) {
            $filename = "../includes/constants.php";

            if (file_exists($filename)) {
                $contents = file($filename, FILE_IGNORE_NEW_LINES);

                for ($i = 0; $i < count($contents); $i++) {
                    if (strpos(trim($contents[$i]), '$program_fee =') === 0) {
                        $contents[$i] = '$program_fee = "' . $program_fee_value . '";';
                        break;
                    }
                }

                file_put_contents($filename, implode("\n", $contents));
                $msg = "Program Fee Changed";
            } else {
                $msg = "Error: Configuration file not found.";
            }
        } else {
            $msg = "Error: Invalid entry";
        }
        break;
  
  	    //---------Added By DPT on June/1/05--------------
    // Update maximum amount limit for merchants
    case 'Modify For Merchants':
        $amount = trim($_POST['maximum_merchant']);

        if (is_numeric($amount)) {
            $filename = "../includes/constants.php";

            if (file_exists($filename)) {
                $contents = file($filename, FILE_IGNORE_NEW_LINES);

                for ($i = 0; $i < count($contents); $i++) {
                    if (strpos(trim($contents[$i]), '$merchant_maximum_amount =') === 0) {
                        $contents[$i] = '$merchant_maximum_amount = "' . $amount . '";';
                        break;
                    }
                }

                file_put_contents($filename, implode("\n", $contents));

                // Process the HTML message (not saved but sanitized here)
                $content = stripslashes($_POST['txta_merchant_message']);
                $content = str_replace(["<?php", "<?", "?>"], "", $content);

                $msg = "Maximum amount limit for merchant is updated";
            } else {
                $msg = "Error: Configuration file not found.";
            }
        } else {
            $msg = "Error: Invalid Entry";
        }
        break;

    // Update maximum amount limit for affiliates
    case 'Modify For Affiliates':
        $amount = trim($_POST['maximum_affiliate']);

        if (is_numeric($amount)) {
            $filename = "../includes/constants.php";

            if (file_exists($filename)) {
                $contents = file($filename, FILE_IGNORE_NEW_LINES);

                for ($i = 0; $i < count($contents); $i++) {
                    if (strpos(trim($contents[$i]), '$affiliate_maximum_amount =') === 0) {
                        $contents[$i] = '$affiliate_maximum_amount = "' . $amount . '";';
                        break;
                    }
                }

                file_put_contents($filename, implode("\n", $contents));

                // Process the HTML message (not saved but sanitized here)
                $content = stripslashes($_POST['txta_affiliate_message']);
                $content = str_replace(["<?php", "<?", "?>"], "", $content);

                $msg = "Maximum amount limit for affiliate is updated";
            } else {
                $msg = "Error: Configuration file not found.";
            }
        } else {
            $msg = "Error: Invalid Entry";
        }
        break;
			} 
			else $msg 			= "Error: Invalid Entry";
			break;  
			
	//Update maximum amount limit for admin			
    case 'Modify For Admin':
    $amount = trim($_POST['maximum_admin']);

    if (is_numeric($amount)) {
        // Read constants file
        $filename = "../includes/constants.php";
        $fd = fopen($filename, "r");
        $contents = fread($fd, filesize($filename));
        fclose($fd);

        // Update line with admin_maximum_amount
        $conts = explode("\n", $contents);
        $n = count($conts);
        for ($i = 0; $i < $n; $i++) {
            $tmp = explode("=", $conts[$i]);
            $tmp1 = trim($tmp[0]);
            if ($tmp1 == "$" . "admin_maximum_amount") {
                $conts[$i] = str_replace($admin_maximum_amount, $amount, $conts[$i]);
                continue;
            }
        }

        // Write back to constants file
        $fd = fopen($filename, "w");
        $cont1 = implode("\n", $conts);
        fwrite($fd, $cont1);
        fclose($fd);

        // Sanitize admin message input
        $content = isset($_POST['txta_admin_message']) ? stripslashes($_POST['txta_admin_message']) : '';
        $content = str_replace(["<?php", "<?", "?>"], "", $content);

        $msg = "Maximum amount limit for admin is updated";
    } else {
        $msg = "Error: Invalid Entry";
    }
    break;
  									
		//---------End of Addition by DPT--------------

  }

  header("Location:$url&msg=$msg");
  exit;

?>