<?php

/* echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n"; */
/**
 * resolve ServiceAccount SIP credentials from token
 *
 * @category 	Atmail
 * @package 	Application
 * @copyright 	Copyright (c) 2009-2012 ATMAIL. All rights reserved
 * @license 	See http://atmail.com/license.php for license agreement
 * @author		Allan Wrethman, Atmail (http://atmail.com)
 */

//only allow calls from localhost
if( $_SERVER['REMOTE_ADDR'] != '127.0.0.1' )
{
    header("HTTP/1.0 404 Not Found");
    return;
}

include_once('bootloader.php');

//query db for token.
$dbAdapter = Zend_Registry::get('dbAdapter');
$q = "SELECT * FROM `ServiceAccounts` WHERE MD5(CONCAT(Account,AuthorizationName,`Password`)) = " . $dbAdapter->quote($_REQUEST['token']);

$result = $dbAdapter->query($q)->fetchAll();

if( !is_array($result) || count($result) == 0 )
	exit();
$row = $result[0];
header ("Content-Type:text/xml; charset=UTF-8");
echo '<root registered="true" login="' . $row['UserId'] . '" authenticationName="' . $row['AuthorizationName'] . '" password="' . $row['Password'] . '" outboundProxy="' . $row['SIPServer'] . '" domain="' . $row['Domain'] . '" port="5060" visibleName="' . $row['DisplayName'] . '" contactParams="+g.3gpp2.csi.vt;+g.oma.sip-im;+g.oma.sip-im.device-message;+g.oma.pusheventapp=fs.ua;+g.oma.pusheventapp=vms.ua;+g.oma.pusheventapp=sms.ua" />
';
