Automatic Signup for ACRCloud Trial
 Page :   [ 1 ]    [ 2 ]    [ 3 ]    [ 4 ]    [ 5 ]  

 Process.php looks like this.  This is called from the .forward file to process any incoming email to see if it's one we are interested in.

 

#!/usr/bin/php -q
<?php 

error_reporting(0);

$stdin = fopen('php://stdin', 'r');
$file = "";
while (!feof($stdin)) {
	$file .= fread($stdin, 1024);
}
fclose($stdin);

include __DIR__ . '/inc/config.php';

$process = Process::initWithMail($file);

if (!$process){
	die();
}

if (strpos($process->getFrom(), 'acrcloud.com') !== false){
	
	$service = new ACRCloud();
	
	$ok = $service->processMail($process);
	
	if ($ok){
		
		$service->finish();
		
	}
	
}

?>

First this creates an object called "Process" which i stubbed together to do the initial parse of the email.

If the mail gets processed OK (comes from the right place, has a verification link etc.) then it calls the "finish" method on the ACRCloud object.

 

Here is the "Process" class:

 

<?php 

class Process{
	
	public $headers;
	public $message;
	public $from;
	public $subject;
	public $to;
	
	public function setHeaders($val){

		if (is_array($val)){
			$this->headers = $val;
			if (isset($val['from'])){
				$this->from = $val['from'];
			}
			
			if (isset($val['to'])){
				$this->to = $val['to'];
			}
			
			if (isset($val['subject'])){
				$this->subject = $val['subject'];
			}
		
		}

	}
	
	public function setMessage($val){
	
		$this->message = $val;
	
	}
	
	public function getHeaders(){
	
		return $this->headers;
	
	}	

	public function getMessage(){
	
		return $this->message;
	
	}
	
	public function getFrom(){
	
		return $this->from;
	
	}
	
	public function getSubject(){
	
		return $this->subject;
	
	}
	
	public function getTo(){
	
		return $this->to;
	
	}

	public function __construct($zendmail){
		
		$this->from = '';
		$this->subject = '';
		$this->message = '';
		$this->headers = array();
		
		$foundPart = false;
		
		$this->setHeaders($zendmail->getHeaders());
		
		$content = '';
		
		$content = explode("\n", $zendmail->getContent());
		
		
		foreach (new RecursiveIteratorIterator($zendmail) as $part) {
			try {
				if (strtok($part->contentType, ';') == 'text/plain') {
					$foundPart = $part;
					break;
				}
			} catch (Zend_Mail_Exception $e) {
				// ignore
			}
		}
		if ($foundPart) {

			$content = trim(utf8_encode(quoted_printable_decode(strip_tags($foundPart)))) ;

		}
				
		$gotFirst = false;
		$gotLast = false;
				
		$str = '';

		if (!strlen($str)){
			$str = $zendmail->getContent();
		}			
			
		$content = $str;
			
		$this->setMessage($content);

	}
	
	
	public static function getAllowed(){
		
		$allowed = array();
		$allowed[] = 'acrcloud.com';
		
		return $allowed;
		
	}
	
	
	public static function initWithMail($mail){
		
		$zendmail = self::processMail($mail);
		if($zendmail !== false){
	
			$process = new Process($zendmail);
			return $process;
		}else{
			return false;
		}

	}
	

	public static function processMail($mail){
		
		$allowed = self::getAllowed();

		$z = new Zend_Mail_Message(array('file' => $mail));
		
		$headers = $z->getHeaders();
	
		if (!isset($headers['subject'])){
			$headers['subject'] = '';
		}
		
		$ok = false;
		
		foreach ($allowed as $akey => $address){
			$address = strtolower(trim($address));
		
			$pos = strpos($address, '@');
		
			if ($pos !== false){
				if (strlen($address)){
					$pos = strpos(strtolower($headers['from']), trim($address));
					if ($pos !== false){
						$ok = true;
						break;
					}
				}
			} else {
				$parts = explode('@', $headers['from']);
		
		
				if (is_array($parts)){
					if (count($parts) == 2){
		
						$parts[1] = str_replace(">", "", $parts[1]);
						$parts[1] = str_replace("<", "", $parts[1]);
							
						if (strtolower(trim($parts[1])) == $address){
							$ok = true;
							break;
						}
					}
				}
		
			}
		
		}
		
		if ($ok){
			
			return $z;

		}
		
		return false;
		
	}
	
	
}

?>

 

 The finish method in the "ACRCloud" object (see previous page) does a lot of things.  It sends back the verification link.  It then logs in to ACRCloud.com and finishes off the sign up process (they ask for some extra info like address, country, phone etc.).

Once the verification and extra info are submitted it then sends us to the user console.  From there it creates a "project" with some random word and numbers.  This then returns some JSON with the access_key and access_secret.  All this info is then stored in the database (results field) and the "finished" and "success" flags are set for this record in the "account" table of our database.

Here's an example of the JSON data returned when creating a project:

I just store what is needed from this.

{
    "uid": 2222,
    "access_key": "22222222222222222222222222222222",
    "access_secret": "22222222222222222222222222222222",
    "service_type": "AVR",
    "state": "valid",
    "name": "myProjectName",
    "region": "us-west-2",
    "hour_limit": 0,
    "day_limit": 10000,
    "total_limit": 0,
    "res_mode": 3,
    "audio_type": {
        "id": 2,
        "name": "Line-in"
    },
    "version": 1,
    "external_id": "none",
    "updated_at": "2016-10-14 10:24:32",
    "created_at": "2016-10-14 10:24:32",
    "id": 1111,
    "type": "service\n.type_AVR"
}

 

 Next page talks a bit about how to use this data.

 

(Page 4 of 5)