<commit_before><?php
/**
 * Source file for the auth component
 * 
 * Ntentan Framework
 * Copyright (c) 2010-2012 <NAME>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
 * 
 * @category Components
 * @author <NAME> <<EMAIL>>
 * @copyright 2010-2012 <NAME>
 * @license MIT
 */

namespace ntentan\controllers\components\auth;

use ntentan\Ntentan;
use \ntentan\controllers\components\Component;

/**
 * The class for the authentication component. This class provides an entry point
 * through which the various authentication methods could be used. It also acts
 * as a watch dog of some sort which prevents un authenticated users from 
 * acessing sections of the site they are not entitled to. Finally this class
 * provides a role mechanism so that the various users of the system can be limited
 * to what they have access to based on thei attached role.
 * 
 * @author <NAME> <<EMAIL>>
 */
class AuthComponent extends Component
{
    /**
     * 
     */
    const REDIRECT      = 'redirect';
    
    /**
     * 
     */
    const CALL_FUNCTION = 'call_function';
    
    /**
     * 
     * @var string
     */
    const DO_NOTHING    = 'do_nothing';

    protected $parameters;
    private $authMethodInstance;

    public function __construct($parameters = array())
    {
        parent::__construct();
        $this->parameters = $parameters;
        $this->authenticated = $_SESSION['logged_in'];
    }
    
    private function getParameter($parameter, $default = null)
    {
        if(isset($this->parameters[$parameter]))
        {
            return $this->parameters[$parameter];
        }
        else
        {
            return $default;
        }
    }

    public function init()
    {
        foreach($this->getParameter('excluded_routes', array()) as $excludedRoute)
        {
            if(preg_match("/$excludedRoute/i", Ntentan::$route) > 0)
            {
                return;
            }
        }

        if($_SESSION["logged_in"] !== true)
        {
            $this->set('app_name', Ntentan::$appName);
            $this->set('title', "Login");
            $this->login();
        }
    }

    public function redirectToLogin()
    {
        $this->set("login_message", $this->authMethodInstance->getMessage());
        $this->set("login_status", false);
        $loginRoute = $this->getParameter('login_route', $this->controller->route . "/login");
        if(Ntentan::$route != $loginRoute && Ntentan::$requestedRoute != $loginRoute)
        {
            Ntentan::redirect(
                $loginRoute .
                (
                    Ntentan::$requestedRoute == ""
                    ? "" :
                    (Ntentan::$requestedRoute == $logoutRoute ? "" : "?redirect=" . urlencode(Ntentan::$requestedRoute))
                )
            );
        }
    }
    
    private function performSuccessOperation()
    {
        $this->authenticated = true;
        switch($this->getParameter('on_success', self::REDIRECT))
        {
            case self::REDIRECT:
                Ntentan::redirect($this->getParameter('redirect_route', '/'));
                break;

            case self::CALL_FUNCTION:
                $decomposed = explode("::", $this->getParameter('success_function'));
                $className = $decomposed[0];
                $methodName = $decomposed[1];
                $method = new \ReflectionMethod($className, $methodName);
                $method->invoke(null, $this->controller);
                break;

            default:
                $this->set('login_status', true);
        }        
    }
    
    private function performFailureOperation()
    {
        switch($this->getParameter('on_failure', self::REDIRECT))
        {
            case self::CALL_FUNCTION:
                $decomposed = explode("::", $this->getParameter('failure_function'));
                $className = $decomposed[0];
                $methodName = $decomposed[1];
                $method = new \ReflectionMethod($className, $methodName);
                $method->invoke(null, $this->controller);
                break;

            case self::REDIRECT:
                $this->redirectToLogin();
                break;

            default:
                $this->set('login_status', false);
                break;
        }        
    }
    
    public function login()
    {
        Ntentan::addIncludePath(Ntentan::getFilePath('lib/controllers/components/auth/methods'));
        $authenticatorClass = __NAMESPACE__ . '\\methods\\' . Ntentan::camelize($this->getParameter('auth_method', 'http_request'));
        $this->authMethodInstance = new $authenticatorClass();
        $this->authMethodInstance->setPasswordCryptFunction(
            $this->getParameter(
                'password_crypt', 
                function($password, $storedPassword){ 
                    return md5($password) == $storedPassword; 
                }
            )
        );<commit_msg>Finding another way to deal with redirected functions<commit_after><?php
/**
 * Source file for the auth component
 * 
 * Ntentan Framework
 * Copyright (c) 2010-2012 <NAME>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
 * 
 * @category Components
 * @author <NAME> <<EMAIL>>
 * @copyright 2010-2012 <NAME>
 * @license MIT
 */

namespace ntentan\controllers\components\auth;

use ntentan\Ntentan;
use \ntentan\controllers\components\Component;

/**
 * The class for the authentication component. This class provides an entry point
 * through which the various authentication methods could be used. It also acts
 * as a watch dog of some sort which prevents un authenticated users from 
 * acessing sections of the site they are not entitled to. Finally this class
 * provides a role mechanism so that the various users of the system can be limited
 * to what they have access to based on thei attached role.
 * 
 * @author <NAME> <<EMAIL>>
 */
class AuthComponent extends Component
{
    /**
     * 
     */
    const REDIRECT = 'redirect';
    
    /**
     * 
     */
    const CALL_FUNCTION = 'call_function';
    
    /**
     * 
     * @var string
     */
    const DO_NOTHING = 'do_nothing';

    protected $parameters;
    private $authMethodInstance;

    public function __construct($parameters = array())
    {
        parent::__construct();
        $this->parameters = $parameters;
        $this->authenticated = $_SESSION['logged_in'];
    }
    
    private function getParameter($parameter, $default = null)
    {
        if(isset($this->parameters[$parameter]))
        {
            return $this->parameters[$parameter];
        }
        else
        {
            return $default;
        }
    }

    public function init()
    {
        foreach($this->getParameter('excluded_routes', array()) as $excludedRoute)
        {
            if(preg_match("/$excludedRoute/i", Ntentan::$route) > 0)
            {
                return;
            }
        }

        if($_SESSION["logged_in"] !== true)
        {
            $this->set('app_name', Ntentan::$appName);
            $this->set('title', "Login");
            $this->login();
        }
    }

    public function redirectToLogin()
    {
        $this->set("login_message", $this->authMethodInstance->getMessage());
        $this->set("login_status", false);
        $loginRoute = $this->getParameter('login_route', $this->controller->route . "/login");
        if(Ntentan::$route != $loginRoute && Ntentan::$requestedRoute != $loginRoute)
        {
            Ntentan::redirect(
                $loginRoute .
                (
                    Ntentan::$requestedRoute == ""
                    ? "" :
                    (Ntentan::$requestedRoute == $logoutRoute ? "" : "?redirect=" . urlencode(Ntentan::$requestedRoute))
                )
            );
        }
    }
    
    private function performSuccessOperation()
    {
        $this->authenticated = true;
        switch($this->getParameter('on_success', self::REDIRECT))
        {
            case self::REDIRECT:
                Ntentan::redirect($this->getParameter('redirect_route', '/'));
                break;

            case self::CALL_FUNCTION:
                $function = $this->getParameter('success_function');
                $function();
                break;

            default:
                $this->set('login_status', true);
        }        
    }
    
    private function performFailureOperation()
    {
        switch($this->getParameter('on_failure', self::REDIRECT))
        {
            case self::CALL_FUNCTION:
                $function = $this->getParameter('failure_function');
                $function();
                break;

            case self::REDIRECT:
                $this->redirectToLogin();
                break;

            default:
                $this->set('login_status', false);
                break;
        }        
    }
    
    public function login()
    {
        Ntentan::addIncludePath(Ntentan::getFilePath('lib/controllers/components/auth/methods'));
        $authenticatorClass = __NAMESPACE__ . '\\methods\\' . Ntentan::camelize($this->getParameter('auth_method', 'http_request'));
        $this->authMethodInstance = new $authenticatorClass();
        $this->authMethodInstance->setPasswordCryptFunction(
            $this->getParameter(
                'password_crypt', 
                function($password, $storedPassword){ 
                    return md5($password) == $storedPassword; 
                }
            )
        );
{'fraction_non_alphanumeric': 0.11245674740484429, 'fraction_numerical': 0.0034602076124567475, 'mean_word_length': 2.193646408839779, 'pattern_counts': {'":': 2, '<': 21, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6264850', 'n_tokens_mistral': 3124, 'n_tokens_neox': 2964, 'n_words': 843}