<?php

require $_SERVER['DOCUMENT_ROOT'] . '/includes/includeMeBlank.php';
$netID = getenv("SERVICENOWUSER");
$password = getenv("SERVICENOWPASSWORD");
$query = $_REQUEST['query'];
$url = getenv("SERVICENOWSTAGEURL") . "/sys_user_group_list.do?WSDL";
if ($env == 2) {
    $url = getenv("SERVICENOWURL") . "/sys_user_group_list.do?WSDL";
}
$ServiceNowClient = new SoapClient($url, array('trace' => 1, 'exceptions' => true, 'cache_wsdl' => WSDL_CACHE_MEMORY, 'compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP, 'login' => $netID, 'password' => $password));
$result = $ServiceNowClient->getRecords(array('__encoded_query' => $query));
if (isset($result->getRecordsResult)) {
    if (is_array($result->getRecordsResult)) {
        echo json_encode(array('status' => 'OK', 'data' => $result->getRecordsResult));
    } else {
        echo json_encode(array('status' => 'OK', 'data' => array($result->getRecordsResult)));
    }
} else {
    echo json_encode(array('status' => 'FAIL', 'error' => 'No records found.'));
}
            mail(getenv("HIGHPRIORITYTEXT"), 'P1 Alert', $incident->number . ": \n" . $incident->short_description, 'From: ' . getenv("OPSEMAILUSERNAME") . ' <' . getenv("OPSEMAILADDRESS") . '>' . "\r\n");
        }
    }
}
// DECRYPT PASSWORD
$passy = $_GET['p'];
$password = passDecrypt($passy);
// ESTABLISH CONNECTION
try {
    $soapurl = getenv("SERVICENOWSTAGEURL") . "/incident.do?WSDL";
    if ($env == 2) {
        $soapurl = getenv("SERVICENOWURL") . "/incident.do?WSDL";
    }
    $incidentClient = new SoapClient($soapurl, array('trace' => 1, 'exceptions' => true, 'cache_wsdl' => WSDL_CACHE_MEMORY, 'compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP, 'login' => $netID, 'password' => $password));
    // GET HIGH PRIORITY TICKETS FROM THE PAST 12 HOURS
    $resultSet = $incidentClient->getRecords(array('__encoded_query' => 'priority<=2^active=true^ORclosed_at>javascript:gs.hoursAgo(12)'));
    // IF A HIGH PRIORITY TICKET EXISTS
    if (isset($resultSet->getRecordsResult)) {
        // CREATE TABLE AND HEADERS
        echo '<table id="hiPriTable" class="sortable"><tr><th>Pri.</th><th>Ticket #</th><th>Description</th><th>State</th></tr>';
        if (is_array($resultSet->getRecordsResult)) {
            foreach ($resultSet->getRecordsResult as $incident) {
                printHiPriTicket($incident);
            }
        } else {
            printHiPriTicket($resultSet->getRecordsResult);
        }
        echo '</table>';
    } else {
        echo '<br /><div style="width: 100%; text-align: center; font-weight: bold; margin-bottom: 8px; font-size: 90%;"><table style="margin-left: auto; margin-right: auto;"><tr><th style="height: 2em; vertical-align: middle;">  There are currently no high priority tickets.  </th></tr></table></div>';
    }