<?php
/**
 * A class for forcing a secure http request.
 *
 * This class interrupts the controllers execution if the current request is not
 * secure (ssl). It will abort the rest of the execution and redirect to its
 * secure alternative.
 */
class controller_filter_ssl extends controller_filter
{
    ////////////////////////////////////////////////////////////////////////////
    // Construct and destruct functions.

    ////////////////////////////////////////////////////////////////////////////
    // Overload functions.

    ////////////////////////////////////////////////////////////////////////////
    // Interface functions.

    /**
     * Executes the filter (pre action).
     *
     * @return boolean True if the rest of the execution is to be aborted, else
     * false.
     */
    public function pre_execute()
    {
        if (!$this->request->is_secure())
        {
            $schema = 'https';
            $domain = $this->request->SERVER_NAME;
            $port = $this->request->SERVER_PORT;
            $uri = $this->request->REQUEST_URI;
            $get = $this->request->get();
            $to = $schema . '://' . $domain;
            
            if ($port && $port != '80')
            {
                $to .= ':' . $port;
            }
            
            $to .= $uri;
            
            if ($get)
            {
                $to .= '?' . http_build_query($get);
            }
            
            $this->redirect($to);
            return true;
        }
    }

    ////////////////////////////////////////////////////////////////////////////
    // Internal functions.
}
