<?php
include("template.php");

//$siteName = $_GET['siteName'] . " RESPONSE";
$siteName = $_POST['siteName'] . " RESPONSE";

?>

 <body>
        <header id='header'>
                <?php echo $siteName; ?>
        </header>
        <section id='hyperlinks' class='leftlinks'>
        <?php
        include("mid_template.php");
         printSideLinks($currentPage);
        ?>
        </section>
        <section id='container'>
<?php
//START WRITING PHP CODE HERE

//Variables
$phone= $_GET['phone'];
$first= $_GET['first'];
$last= $_GET['last'];
$upload= $_POST['upload'];
$account="AC061dfdeaac04466783e164e3efb4c72e";
$token="AUbce970ab2c4841da8e8b902de1f5e05a";

if(isset($upload) and $upload == 'true') {
    upload();
    multiUpdate();
} else {
    singleUpdate();
}

function singleUpdate()
{
//If last name is Empty then use first name as Business Name
    if (!isset($last)) {
        global $phone, $first, $last, $account, $token;
        $business = $first;
        $site = '"https://storage.opencnam.com/v1/phone/+1' . $phone . '?account_sid=' . $account . '&auth_token=' . $token . '" -v -d "business=' . $business . '&language=eng&class=BUS"';
    } else {
        $site = '"https://storage.opencnam.com/v1/phone/+1' . $phone . '?account_sid=' . $account . '&auth_token=' . $token . '" -v -d "first_name=' . $first . '&last_name=' . $last . '&language=eng&class=MOB"';
    }

    echo ' This is the '. $site . '</br>';
    echo 'Updating phone 1' . $phone . ':</br>';
    echo '</br>';
    echo 'THIS CHANGE WILL REQUIRE UPTO 48 HOURS TO PROPAGATE TO ALL OF OUR SYSTEMS:</br>';

//Since this is not an option echo is the best bet.
    echo '</br>';
    echo '###################################################################</br>';
//$site='"https://storage.opencnam.com/v1/phone/+1'.$phone.'?account_sid='.$account.'&auth_token='.$token.'" -v -d "first_name='.$first.'&last_name='.$last.'&language=eng&class=BUS"';
    $output = shell_exec('curl ' . $site);
    echo "<pre>$output</pre>";
    echo '###################################################################</br>';
    echo '</br>';
}

function multiUpdate() {
    echo '</br>';
    //echo 'THIS CHANGE WILL REQUIRE UPTO 48 HOURS TO PROPAGATE TO ALL OF OUR SYSTEMS:</br>';
    echo 'AS OF RIGHT NOW I AM WORKING WITH OPENCNAM ON UPLOADING THE FILES TO THEM, FOR RIGHT NOW EMAIL JOLI.MARTINEZ@FUSIONCONNECT.COM ONCE FILE IS UPLOADED:</br>';
    echo 'THIS IS JUST TEMPORARY UNTIL WE WORK OUT THE ISSUES WITH OPENCNAM</BR>';
}

function upload() {
    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    $uploadOk = 1;
    $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

    // Check if file already exists
    if (file_exists($target_file)) {
        echo "Sorry, file already exists.";
        echo "</br>";
        $uploadOk = 0;
    }
    // Check file size
    if ($_FILES["fileToUpload"]["size"] > 500000) {
        echo "Sorry, your file is too large.";
        echo "</br>";
        $uploadOk = 0;
    }
    // Check if $uploadOk is set to 0 by an error
    if ($uploadOk == 0) {
        echo "Sorry, your file was not uploaded.";
        echo "</br>";
    // if everything is ok, try to upload file
    } else {
        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
            echo "The file \"". basename( $_FILES["fileToUpload"]["name"]). "\" has been uploaded.";
            echo "</br>";
        } else {
            echo "Sorry, there was an error uploading your file.";
            echo "</br>";
        }
    }
}
?>
            <!--  </section>     -->
        </section>
 <?php
    include("footer.php");
?>

