<?php
    header("content-type: text/xml");
    date_default_timezone_set('America/New_York');

$top = '3';
$ezdate = date("F j Y");
$timeStamp = date("U");
$Udate =  date("U");

// Check the Key before creating Alert Record

$thekey = $_REQUEST['thekey']; // Get the Key from the command line
  if ($thekey <> 'Buck-a-roo') {
  exit;
  }

// Get Message Details from the Session Variables from SMS Gateway

$MsgSid = $_REQUEST['MessageSid'];
$AccountSid=$_REQUEST['AccountSid'];
$MsgFrom=$_REQUEST['From'];
$MsgTo=$_REQUEST['To'];
$MsgBody=$_REQUEST['Body'];

// Strip the Plus Sign off the Phone Number for  User ID field.

list ($Nothing, $UserID) = explode("+", $MsgFrom);

// Log Message Transactions (json)
// Add support for up to ten media attachments to the message. 1-26-25 MF

$json = '{"UserID":"' . $UserID
    . '","TimeStamp":"' . $timeStamp
    . '","EZdate":"' . $ezdate
    . '","UNIXepoch":"' . $Udate

    . '","MessagegSid":"' .  $_REQUEST['MessageSid']
    . '","AccountSid":"' . $_REQUEST['AccountSid']
    . '","MsgFrom":"' . $_REQUEST['From']
    . '","MsgTo":"' . $_REQUEST['To']

	. '","$FromCity":"' . $_REQUEST['FromCity']
	. '","$FromState":"' . $_REQUEST['FromState']
	. '","$FromZip":"' . $_REQUEST['FromZip']
	. '","$FromCountry":"' . $_REQUEST['FromCountry']

	. '","$ToCity":"' . $_REQUEST['ToCity']
	. '","$ToState":"' . $_REQUEST['ToState']
	. '","$ToZip":"' . $_REQUEST['ToZip']
	. '","$ToCountry":"' . $_REQUEST['ToCountry']

	. '","$Body":"' . $_REQUEST['Body']

	. '","$NumMedia":"' . $_REQUEST['NumMedia']
	. '","$MediaUrl0":"' . $_REQUEST['MediaUrl0']
	. '","$MediaContentType0":"' . $_REQUEST['MediaContentType0']

    . '"}' . "\n";


// All Alerts  Log Record

$filename =  'alerts';
$logfile = fopen( $filename . '.json' , 'a');
fputs($logfile,$json);
fclose($logfile);

// LINES Log 10-21-23 MF

$filename0 = $_REQUEST['From'];
$userfile = fopen('./LINES/' . $filename0 . '.json' , 'a');
fputs($userfile,$json);
fclose($userfile);

// "View" the NUPicture  (7-25-24 MF)

$filename1 =  './NUfrom/'. $_REQUEST['From'];
$pictfile = fopen( $filename1 . '.json' , 'w');
fputs($pictfile,$json);
fclose($pictfile);


// Copy Reel to Local Drive

// Get the Media Attachment 2-24-26 MF --------------------

$url = $_REQUEST['MediaUrl0'];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec ($ch);
curl_close ($ch);

// Is it a photo or a reel ?
$Media=$_REQUEST['MediaContentType0'];

switch ($Media) {
  case "image/jpeg":

//    echo "Your media is a photo";
//    Photos to the bit bucket 2-24-26 MF
        $thePhoto =  "PHOTOS/". $timeStamp . $_REQUEST['From'] .".jpeg";
        $fp = fopen($thePhoto,'w');
        fwrite($fp, $rawdata);
        fclose($fp);

    break;

  case "video/3gpp":
  //  echo "Your reel is from an iphone";

	// 4-2-26 MF
//	$theReel =  "REELS/". $_REQUEST['From'].'@' . $timeStamp . ".3gpp";
	$theReel =  "REELS/". $timeStamp . $_REQUEST['From'] .".3gpp";
	$fp = fopen($theReel,'w');
	fwrite($fp, $rawdata);
	fclose($fp);

// Reels Log Record ( 7-23-26 MF )

$filename2 =  './3gpp';
$wkfile = fopen( $filename2 . '.json' , 'a');
fputs($wkfile,$json);
fclose($wkfile);


    break;
  case "video/mp4":
//    echo "Your reel is a mp4";

	// 4-2-26 MF
	// $theReel =  "REELS/".$_REQUEST['From'].'@' . $timeStamp . ".mp4";
	$theReel =  "REELS/". $timeStamp . $_REQUEST['From'] .".mp4";

$fp = fopen($theReel,'w');
fwrite($fp, $rawdata);
fclose($fp);

// Reels Log Record ( 7-23-26 MF )

$filename2 =  './mp4';
$wkfile = fopen( $filename2 . '.json' , 'a');
fputs($wkfile,$json);
fclose($wkfile);


    break;


  default:
//    echo "Snarl Out";
return;
}



// End Photos or Reels 7-7-25 MF

exit;
?>
