<?php

session_start();
include 'Services/Twilio.php';
include 'config.php';
include 'company-directory-map.php';
$error = false;
if (isset($_REQUEST['Digits'])) {
    $digits = $_REQUEST['Digits'];
} else {
    $digits = '';
}
if (strlen($digits)) {
    $result = getPhoneNumberByDigits($digits);
    if ($result != false) {
        $number = getPhoneNumberByExtension($result['extension']);
        $r = new Services_Twilio_Twiml();
        $r->say($result['name'] . "'s extension is " . $result['extension'] . " Connecting you now");
        $r->dial($number);
        header("Content-Type:text/xml");
        print $r;
        exit;
    } else {
        $error = true;
    }
}
$r = new Services_Twilio_Twiml();
if ($error) {
    $r->say("No match found for {$digits}");
}
$g = $r->gather();
include 'Services/Twilio.php';
include 'config.php';
include 'company-directory-map.php';
$first = true;
if (isset($_REQUEST['Digits'])) {
    $digits = $_REQUEST['Digits'];
    if ($digits == '*') {
        header("Location: company-directory-lookup?Digits=" . $digits);
        exit;
    }
} else {
    $digits = '';
}
if (strlen($digits)) {
    $first = false;
    $phone_number = getPhoneNumberByExtension($digits);
    if ($phone_number != null) {
        $r = new Services_Twilio_Twiml();
        $r->say("Thank you, dialing now");
        $r->dial($phone_number);
        header("Content-Type:text/xml");
        print $r;
        exit;
    }
}
$r = new Services_Twilio_Twiml();
$g = $r->gather();
if ($first) {
    $g->say("Thank you for calling Example Incorporated.");
} else {
    $g->say('I\'m sorry, we could not find the extension ' . $_REQUEST['Digits']);