/*
 * Copyright 2015 AT&T
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
session_start();
require_once __DIR__ . '/common.php';
require_once __DIR__ . '/../lib/IMMN/IMMNService.php';
use Att\Api\IMMN\IMMNService;
$arr = null;
try {
    envinit();
    $immnSrvc = new IMMNService(getFqdn(), getSessionToken());
    $immnSrvc->createMessageIndex();
    $arr = array('success' => true, 'text' => 'Message index created.');
} catch (Exception $e) {
    $arr = array('success' => false, 'text' => $e->getMessage());
}
echo json_encode($arr);
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/*
 * Copyright 2015 AT&T
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
session_start();
require_once __DIR__ . '/common.php';
require_once __DIR__ . '/../lib/AAB/AABService.php';
use Att\Api\AAB\AABService;
$arr = null;
try {
    envinit();
    $aabService = new AABService(getFqdn(), getSessionToken());
    $contactId = $_POST['deleteContactId'];
    $aabService->deleteContact($contactId);
    $arr = array('success' => true, 'text' => 'Successfully deleted contact.');
} catch (Exception $e) {
    $arr = array('success' => false, 'text' => $e->getMessage());
}
echo json_encode($arr);
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
    if (isset($_POST['areaCode']) && $_POST['areaCode'] != "") {
        $optArgs->setAreaCode($_POST['areaCode']);
    }
    if (isset($_POST['country']) && $_POST['country'] != "") {
        $optArgs->setCountry($_POST['country']);
    }
    if (isset($_POST['latitude']) && $_POST['latitude'] != "") {
        $optArgs->setLatitude($_POST['latitude']);
    }
    if (isset($_POST['longitude']) && $_POST['longitude'] != "") {
        $optArgs->setLongitude($_POST['longitude']);
    }
    return $optArgs;
}
$arr = null;
try {
    envinit();
    $adsService = new ADSService(getFqdn(), getFileToken());
    $category = $_POST['category'];
    $optArgs = parseArgs();
    $result = $adsService->getAdvertisement($category, $userAgent, $udid, $optArgs);
    if ($result === null) {
        $arr = array('success' => true, 'text' => 'No Ads were returned');
    } else {
        $arr = array('success' => true, 'tables' => array(array('caption' => 'Ads Response:', 'headers' => array('Type', 'ClickUrl'), 'values' => array(array($result->getAdsType(), $result->getClickUrl())))));
    }
} catch (Exception $e) {
    $arr = array('success' => false, 'text' => $e->getMessage());
}
echo json_encode($arr);
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
session_start();
require_once __DIR__ . '/common.php';
require_once __DIR__ . '/../lib/Util/Util.php';
require_once __DIR__ . '/../lib/DC/DCService.php';
use Att\Api\Util\Util;
use Att\Api\DC\DCService;
$arr = null;
try {
    envinit();
    $dcService = new DCService(getFqdn(), getFileToken());
    $response = $dcService->getDeviceInformation();
    $dcaps = $response->getCapabilities();
    $headers = array('TypeAllocationCode', 'Name', 'Vendor', 'Model', 'FirmwareVersion', 'UaProf', 'MmsCapable', 'AssistedGps', 'LocationTechnology', 'DeviceBrowser', 'WapPushCapable');
    $valuesEntry = array($response->getTypeAllocationCode(), $dcaps->getName(), $dcaps->getVendor(), $dcaps->getModel(), $dcaps->getFirmwareVersion(), $dcaps->getUaProf(), $dcaps->isMmsCapable() ? 'Y' : 'N', $dcaps->isAssistedGps() ? 'Y' : 'N', $dcaps->getLocationTechnology(), $dcaps->getDeviceBrowser(), $dcaps->isWapPushCapable() ? 'Y' : 'N');
    $arr = array('success' => true, 'tables' => array(array('caption' => 'Device Capabilities', 'headers' => $headers, 'values' => array($valuesEntry))));
} catch (Exception $e) {
    $arr = array('success' => false, 'text' => $e->getMessage());
}
echo json_encode($arr);
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
try {
    if (isset($_SESSION['subscriptionId'])) {
        throw new Exception('You must first delete your existing subscription.');
    }
    $events = array();
    if (isset($_REQUEST['subscriptionText'])) {
        $events[] = 'TEXT';
    }
    if (isset($_REQUEST['subscriptionMms'])) {
        $events[] = 'MMS';
    }
    if (count($events) == 0) {
        throw new Exception("You must select at least one of Text or MMS");
    }
    envinit();
    $webhooksSrvc = new WebhooksService(getFqdn(), getSessionToken());
    $callbackData = $_REQUEST['callbackData'];
    if ($callbackData == '') {
        $callbackData = null;
    }
    $args = new CreateSubscriptionArgs(CHANNEL_ID, $events, $callbackData, EXPIRES_IN);
    $response = $webhooksSrvc->createNotificationSubscription($args);
    $subscription = $response->getSubscription();
    $subscriptionId = $subscription->getSubscriptionId();
    $_SESSION['subscriptionId'] = $subscriptionId;
    $_SESSION['subscriptionExpiry'] = EXPIRES_IN + time();
    $arr = array('success' => true, 'text' => 'Subscription created.');
} catch (Exception $e) {
    $arr = array('success' => false, 'text' => $e->getMessage());
}
echo json_encode($arr);
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
session_start();
require_once __DIR__ . '/common.php';
require_once __DIR__ . '/../config.php';
require_once __DIR__ . '/../lib/Webhooks/WebhooksService.php';
use Att\Api\Webhooks\WebhooksService;
$arr = null;
try {
    if (!isset($_SESSION['subscriptionId'])) {
        throw new Exception('You must first create a subscription.');
    }
    $subscriptionId = $_SESSION['subscriptionId'];
    envinit();
    $token = getFileTokenByScope(NOTIFICATION_SCOPE);
    $webhooksSrvc = new WebhooksService(getFqdn(), $token);
    $webhooksSrvc->deleteNotificationSubscription(CHANNEL_ID, $subscriptionId);
    unset($_SESSION['subscriptionId']);
    $arr = array('success' => true, 'text' => 'Subscription deleted.');
} catch (Exception $e) {
    $arr = array('success' => false, 'text' => $e->getMessage());
}
echo json_encode($arr);
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
session_start();
require_once __DIR__ . '/common.php';
require_once __DIR__ . '/../lib/Util/Util.php';
require_once __DIR__ . '/../lib/MMS/MMSService.php';
use Att\Api\Util\Util;
use Att\Api\MMS\MMSService;
$arr = null;
try {
    envinit();
    $mmsService = new MMSService(getFqdn(), getFileToken());
    $msgId = $_POST['msgId'];
    $response = $mmsService->getMMSStatus($msgId);
    $deliveryInfoList = $response->getDeliveryInfoList();
    $values = array();
    foreach ($deliveryInfoList as $deliveryInfo) {
        $values[] = array($deliveryInfo->getId(), $deliveryInfo->getAddress(), $deliveryInfo->getDeliveryStatus());
    }
    $arr = array('success' => true, 'text' => 'ResourceURL: ' . $response->getResourceUrl(), 'tables' => array(array('caption' => 'Status:', 'headers' => array('MessageId', 'Address', 'DeliveryStatus'), 'values' => $values)));
} catch (Exception $e) {
    $arr = array('success' => false, 'text' => $e->getMessage());
}
echo json_encode($arr);
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
session_start();
require_once __DIR__ . '/common.php';
require_once __DIR__ . '/../lib/Util/Util.php';
require_once __DIR__ . '/../lib/SMS/SMSService.php';
use Att\Api\Util\Util;
use Att\Api\SMS\SMSService;
$arr = null;
try {
    envinit();
    $smsService = new SMSService(getFqdn(), getFileToken());
    $addr = Util::convertAddresses($_POST['address']);
    $addr = count($addr) == 1 ? $addr[0] : $addr;
    $msg = $_POST['message'];
    $notifyDeliveryStatus = isset($_POST['deliveryNotificationStatus']);
    $response = $smsService->sendSMS($addr, $msg, $notifyDeliveryStatus);
    $resourceUrl = $response->getResourceUrl();
    $resourceUrl = $resourceUrl == null ? '-' : $resourceUrl;
    $arr = array('success' => true, 'tables' => array(array('caption' => 'Message sent successfully', 'headers' => array('Message ID', 'Resource URL'), 'values' => array(array($response->getMessageId(), $resourceUrl)))));
} catch (Exception $e) {
    $arr = array('success' => false, 'text' => $e->getMessage());
}
echo json_encode($arr);
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/** 
 * Gets an access token that will be cached using the user's session. 
 *
 * This method works by first trying to load the token from the user's 
 * session, and, if a saved OAuth token isn't found, this method will send 
 * an API request.
 *
 * @return OAuthToken OAuth token that can be used for authorization
 * @throws OAuthException if API request was not successful or if 
 *                        there was a session issue  
 */
function getSessionToken()
{
    // Try loading token from session
    $token = isset($_SESSION['token']) ? unserialize($_SESSION['token']) : null;
    // No token or token is expired... send token request
    if (!$token || $token->isAccessTokenExpired()) {
        // check for error/error description in request params
        if (isset($_REQUEST['error'])) {
            $error = $_REQUEST['error'];
            $errorDesc = '';
            if (isset($_REQUEST['error_description'])) {
                $errorDesc = $_REQUEST['error_description'];
            }
            throw new OAuthException($error, $errorDesc);
        }
        $code = null;
        // check for code in request params
        if (isset($_REQUEST['code'])) {
            $code = new OAuthCode($_REQUEST['code']);
        } else {
            $error = 'Invalid state';
            $errrorDesc = 'No code found in parameter';
            throw new OAuthException($error, $errrorDesc);
        }
        $fqdn = getFqdn();
        $tokenSrvc = new OAuthTokenService($fqdn, CLIENT_ID, CLIENT_SECRET);
        $token = $tokenSrvc->getTokenUsingCode($code);
        $_SESSION['token'] = serialize($token);
    }
    return $token;
}