<?php
/*
 * Copyright 2008-2010 GuardTime AS
 *
 * This file is part of the GuardTime PHP SDK.
 *
 * 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.
 */

/**
 * @package tsp
 */

/**
 * Represents the result of full verification procedure, possibly including
 * interaction with the online verification service.
 *
 * @package tsp
 */
class GTVerificationResultHttp extends GTVerifificationResultBase {

    /******************************************* STATUS CODES *******************************************/

    /**
     * Reserved as alias for <samp>PKIStatusInfo status = granted (0)</samp>
     * from {@link http://tools.ietf.org/html/rfc3161#section-2.4.2 RFC 3161}.
     *
     * @see getStatusCode, hasStatus
     */
    const TIMESTAMP_GRANTED = 1;

    /**
     * Reserved as alias for <samp>PKIStatusInfo status = grantedWithMods (1)</samp>
     * from {@link http://tools.ietf.org/html/rfc3161#section-2.4.2 RFC 3161}.
     *
     * @see getStatusCode, hasStatus
     */
    const TIMESTAMP_GRANTED_WITH_MODS = 2;

    /**
     * Reserved as alias for <samp>PKIStatusInfo status = rejection (2)</samp>
     * from {@link http://tools.ietf.org/html/rfc3161#section-2.4.2 RFC 3161}.
     *
     * @see getStatusCode, hasStatus
     */
    const TIMESTAMP_REJECTED = 4;

    /**
     * Reserved as alias for <samp>PKIStatusInfo status = waiting (3)</samp>
     * from {@link http://tools.ietf.org/html/rfc3161#section-2.4.2 RFC 3161}.
     *
     * @see getStatusCode, hasStatus
     */
    const TIMESTAMP_WAITING = 8;

    /**
     * Reserved as alias for <samp>PKIStatusInfo status = revocationWarning (4)</samp>
     * from {@link http://tools.ietf.org/html/rfc3161#section-2.4.2 RFC 3161}.
     *
     * @see getStatusCode, hasStatus
     */
    const REVOCATION_WARNING = 16;

    /**
     * Reserved as alias for <samp>PKIStatusInfo status = revocationNotification (5)</samp>
     * from {@link http://tools.ietf.org/html/rfc3161#section-2.4.2 RFC 3161}.
     *
     * @see getStatusCode, hasStatus
     */
    const REVOCATION_NOTIFICATION = 32;

    /**
     * Timestamp extension was attempted during verification.
     *
     * @see getStatusCode, hasStatus
     */
    const EXTENSION_ATTEMPTED = 128;

    /**
     * Timestamp was successfully extended during verification.
     *
     * @see getStatusCode, hasStatus
     */
    const TIMESTAMP_EXTENDED = 256;

    /******************************************* ERROR CODES *******************************************/

    /**
     * Reserved as alias for <samp>PKIStatusInfo fail = badAlg (0)</samp>
     * from {@link http://tools.ietf.org/html/rfc3161#section-2.4.2 RFC 3161}.
     *
     * @see getErrorCode, hasError
     */
    const INVALID_ALGORITHM_FAILURE = 1;

    /**
     * Reserved as alias for <samp>PKIStatusInfo fail = badRequest (2)</samp>
     * from {@link http://tools.ietf.org/html/rfc3161#section-2.4.2 RFC 3161}.
     *
     * @see getErrorCode, hasError
     */
    const  INVALID_REQUEST_FAILURE = 2;

    /**
     * Reserved as alias for <samp>PKIStatusInfo fail = badDataFormat (5)</samp>
     * from {@link http://tools.ietf.org/html/rfc3161#section-2.4.2 RFC 3161}.
     *
     * @see getErrorCode, hasError
     */
    const INVALID_DATA_FORMAT_FAILURE = 4;

    /**
     * Reserved as alias for <samp>PKIStatusInfo fail = timeNotAvailalble (14)</samp>
     * from {@link http://tools.ietf.org/html/rfc3161#section-2.4.2 RFC 3161}.
     *
     * @see getErrorCode, hasError
     */
    const TIME_NOT_AVAILBLE_FAILURE = 8;

    /**
     * Reserved as alias for <samp>PKIStatusInfo fail = unacceptedPolicy (15)</samp>
     * from {@link http://tools.ietf.org/html/rfc3161#section-2.4.2 RFC 3161}.
     *
     * @see getErrorCode, hasError
     */
    const UNACCEPTED_POLICY_FAILURE = 16;

    /**
     * Reserved as alias for <samp>PKIStatusInfo fail = unacceptedExtension (16)</samp>
     * from {@link http://tools.ietf.org/html/rfc3161#section-2.4.2 RFC 3161}.
     *
     * @see getErrorCode, hasError
     */
    const UNACCEPTED_EXTENSION_FAILURE = 32;

    /**
     * Reserved as alias for <samp>PKIStatusInfo fail = addInfoNotAvailalble (17)</samp>
     * from {@link http://tools.ietf.org/html/rfc3161#section-2.4.2 RFC 3161}.
     *
     * @see getErrorCode, hasError
     */
    const ADDITIONAL_INFO_NOT_AVAILABLE_FAILURE = 64;

    /**
     * Reserved as alias for <samp>PKIStatusInfo fail = systemFailure (25)</samp>
     * from {@link http://tools.ietf.org/html/rfc3161#section-2.4.2 RFC 3161}.
     *
     * @see getErrorCode, hasError
     */
    const SYSTEM_FAILURE = 128;

    /**
     * Timestamp is too new to be extended.
     *
     * @see getErrorCode, hasError
     */
    const TIMESTAMP_TOO_NEW_FAILURE = 256;

    /**
     * Timestamp is too old to be extended.
     *
     * @see getErrorCode, hasError
     */
    const TIMESTAMP_TOO_OLD_FAILURE = 512;

    /**
     * Service response has invalid format.
     *
     * @see getErrorCode, hasError
     */
    const RESPONSE_FORMAT_FAILURE = 2048;

    /**
     * Service is unreachable, possibly network error or malformed URL.
     *
     * @see getErrorCode, hasError
     */
    const SERVICE_UNREACHABLE_FAILURE = 8192;

    private $gtResult = null;

    /**
     * Sets the timestamp verification result.
     *
     * @param  GTVerificationResult $gtResult timestamp verification result
     * @return void
     */
    public function setGtResult(GTVerificationResult $gtResult) {
        $this->gtResult = $gtResult;
    }

    /**
     * Gets the timestamp verification result.
     *
     * @return GTVerificationResult timestamp verification result
     */
    public function getGtResult() {
        return $this->gtResult;
    }

    /**
     * Checks if this verification result is valid.
     *
     * @return bool true if verification was successful
     */
    public function isValid() {

        $errorCode = $this->getErrorCode();

        if ($errorCode == self::NO_FAILURES ||
            $errorCode == self::TIMESTAMP_TOO_NEW_FAILURE ||
            $errorCode == self::TIMESTAMP_TOO_OLD_FAILURE ||
            $errorCode == self::SERVICE_UNREACHABLE_FAILURE) {

            if ($this->gtResult === null) {
                return false;

            } else {
                return $this->gtResult->isValid();

            }

        } else {

            return false;
        }

    }

}

?>
