<?php
class controller_upload extends controller
{
    ////////////////////////////////////////////////////////////////////////////
    // Construct and destruct functions.
    
    protected function initialize()
    {
        unset($this->filter['user']);
    }
    
    ////////////////////////////////////////////////////////////////////////////
    // Overload functions.

    ////////////////////////////////////////////////////////////////////////////
    // Interface functions.
    
    ////////////////////////////////////////////////////////////////////////////
    // Internal functions.
    
    protected function execute_upload()
    {
        if ($this->request->is_post())
        {
            $form = new form(include $this->directory . 'config/sign.php');
            
            if (!$form->is_valid($this->request->post()))
            {
                $this->session->flash('form', $form);
                $this->redirect('upload');
                return true;
            }
            
            $username = setting::get('username', 'admin');
            $username = $username['value'];
            $password = setting::get('password', 'admin');
            $password = $password['value'];
            
            if ($username !== $form->user || $password !== $form->password)
            {
                $form->set_error('password');
                $this->session->flash('form', $form);
                $this->redirect('upload');
                return true;
            }
            
            $this->session->logged = true;
            $this->redirect('home');
        }
        else
        {
            if (isset($this->session->logged) && $this->session->logged)
            {
                $this->redirect('home');
                return true;
            }
            
            if (isset($this->session->form))
            {
                $form = $this->session->form;
            }
            else
            {
                $form = new form(include $this->directory . 'config/sign.php');
            }
            
            $data['form'] = $form;
            $this->render('upload.html', $data);
        }
    }
    
    protected function execute_signed_widget()
    {
        if (isset($this->session->logged) && $this->session->logged)
        {
            $body = '<a href="upload/sign-out">Sign out</a>';
        }
        else
        {
            $body = '<a href="">Sign in</a>';
        }
        
        $this->response->set($body);
    }
    
    protected function execute_sign_out()
    {
        unset($this->session->logged);
        $this->redirect('');
    }
}
