';
$accountCreated = createSubAccount($_GET['email']);
//echo 'Account created: ' . $accountCreated . '
';
$grantAccess = grantAccountAccess($accountCreated,$createdSite);
//echo 'Account granted access.
';
$sso_link = generateSSOLink($createdSite,$accountCreated);
header("Location: " . $sso_link);
}
//if a template was note selected, display template selection
else {
displayTemplates();
}
//Functions below
function generateSSOLink($siteName,$account) {
//Set editor custom domain --
$editor_url = 'example.mobilewebsiteserver.com';
//Set SSO Parameters
$dm_sig_site = $siteName;
$dm_sig_user = $account;
$dm_sig_partner_key = '73a3a4';
$dm_sig_timestamp = date_timestamp_get(date_create());
$secret_key = '440268d25d84e6718ebe9ecd82e7f1ba';
//Concatenate sso strings so it can be encrypted
$dm_sig_string = $secret_key.'user='.$dm_sig_user.'timestamp='.$dm_sig_timestamp.'site='.$dm_sig_site.'partner_key='.$dm_sig_partner_key;
//Encrypt values
$dm_sig = hash_hmac('sha1', $dm_sig_string, $secret_key);
//Create SSO link
$sso_link = 'http://' . $editor_url.'/home/site/'.$dm_sig_site.'?dm_sig_partner_key='.$dm_sig_partner_key.'&dm_sig_timestamp='.$dm_sig_timestamp.'&dm_sig_user='.$dm_sig_user.'&dm_sig_site='.$dm_sig_site.'&dm_sig='.$dm_sig;
//return SSO link
return $sso_link;
}
function grantAccountAccess($email,$siteName) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
//format URL to grant access to email and sitename passed
curl_setopt($ch, CURLOPT_URL, 'https://api.dudamobile.com/api/accounts/grant-access/'.$email.'/sites/'.$siteName);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, API_USER.':'.API_PASS);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
//execute cURL call and get template data
$output = curl_exec($ch);
curl_close($ch);
return true;
}
function createSubAccount($emailToCreate) {
$data = '{"account_name":"'.$emailToCreate.'"}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, 'https://api.dudamobile.com/api/accounts/create');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, API_USER.':'.API_PASS);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
//execute cURL call and get template data
$output = curl_exec($ch);
//Check to see if response was successful, if not output error and exit
if(curl_getinfo($ch,CURLINFO_HTTP_CODE) == 204) {
curl_close($ch);
return $emailToCreate;
} else {
curl_close($ch);
die('Account creation failed, error: '. $output . '
');
}
}
function createSite($tempalte_id,$original_url) {
//create array with data
if($original_url) {
$data = array("template_id"=>$_GET['template_id'],"url"=>$original_url);
} else {
$data = array("template_id"=>$_GET['template_id']);
}
//turn data into json to pass via cURL
$data = json_encode($data);
//Set cURL parameters
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, 'https://api.dudamobile.com/api/sites/multiscreen/create');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, API_USER.':'.API_PASS);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
//execute cURL call and get template data
$output = curl_exec($ch);
//check for errors in cURL
if(curl_errno($ch)) {
die('Curl error: ' . curl_error($ch));
}
$output = json_decode($output);
return $output->site_name;
}
function displayTemplates() {
//Set parameters to make cURL call to Duda
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, 'https://api.dudamobile.com/api/sites/multiscreen/templates');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, API_USER.':'.API_PASS);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
//execute cURL call and get template data
$output = curl_exec($ch);
//check for errors in cURL
if(curl_errno($ch)) {
die('Curl error: ' . curl_error($ch));
}
$output = json_decode($output);
curl_close($ch);
//Loop through all templates and display all the available templates in a table
echo '
| Template Name | Template Image | Template Id |
';
foreach($output as $template) {
echo '';
echo '| ' . $template->template_name . ' | ';
echo ' ' . ' | ';
echo ' | ';
echo '
';
}
echo '
';
}
?>