<commit_before><?php
  /**
   * Copyright 2017 BookStore
   *
   * 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.
   */

  //Import the user model and dto
  use \Model\UserModel;
  use \Model\UserDTO;

  /**
   * The Welcome Controller.
   *
   * A basic Login and SignUp page
   *
   * @package  app
   * @extends  Controller
   */
  class Controller_Welcome extends Controller {
    /**
     * The index page
     * @access  public
     * @return  Response
     */
    public function action_index() {
	  //check if the user is logged
      if(!$user = Session::get('userInfo')){
		//if not, load the loggin function 
        $view = View::forge('welcome/index');
        $view->cities = UserModel::getCities();
        return $view;
      }

	  //if yes go to the profile page
      Response::redirect('profile','location');
    }

	/**
	 * Register a new user in the database
	 * @access post
	 * @return Response
	 */
    public function post_registerUser() {
	  //get the user data
      $user = new UserDTO();
      $user->setEmail(Input::post('emailsignup'));
      $user->setName(Input::post('namesignup'));
      $user->setCity(Input::post('citysignup'));
      $user->setAddress(Input::post('addresssignup'));
	  
	  //check that both passwords are the same
      $password1 = Input::post('passwordsignup');
      $password2 = Input::post('passwordsignup_<PASSWORD>');
      if(!($password1 == $password2)){
        echo '<script language="javascript">';
        echo 'alert("Sorry, passwords does not match")';
        echo '</script>';
        Response::redirect('/#toregister', 'refresh');
      }
	  
	  //try to register the user in the database
      try{
        if (UserModel::registerUser($user, $password1) == 1) {
		  //if works login the new user
		  Session::create();
          Session::set('userInfo', $user);
		  echo '<script language="javascript">';
		  echo 'alert("Congratulations, you have a new account")';
		  echo '</script>';
          Response::redirect('profile', 'location');
		} else {
		  //if not print an error message
		  echo '<script language="javascript">';
          echo 'alert("Sorry, there was a problem. Please try again later")';
          echo '</script>';
          Response::redirect('/#toregister', 'refresh');
		}
      }catch(Exception $e){
		//if something fail, print an error message
        echo '<script language="javascript">';
        echo 'alert("Sorry, email already exists")';
        echo '</script>';
        Response::redirect('/#toregister', 'refresh');
      }
    }

	/**
	 * Login an user in the store
	 * @access post
	 * @return Response
	 */
    public function post_checkUser() {
	  //get the user email and password
      $email      = Input::post('emaillogin');
      $password   = Input::post('passwordlogin');
	  
	  //check if the user credentials are correct
      $userResult = UserModel::getUser($email, $password);
      if ($userResult == null) {
		//if not print an error message
        $view = View::forge('welcome/index');
        echo '<script language="javascript">';
        echo 'alert("Sorry, wrong user and/or password")';
        echo '</script>';
        Response::redirect('/#toregister', 'refresh');
      } else {
		//if they are login the user
        Session::create();
        Session::set('userInfo', $userResult);
        Response::redirect('profile', 'location');
      }
    }

    /**
     * The 404 action for the application.
     * @access  public
     * @return  Response
     */
    public function action_404() {
      return Response::forge(Presenter::forge('welcome/404'), 404);
    }
  }
?><commit_msg>Changing the welcome controller to use the Auth Fuel package<commit_after><?php
  /**
   * Copyright 2017 BookStore
   *
   * 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.
   */

  //Import the user model and dto
  use \Model\UserModel;
  use \Model\UserDTO;

  /**
   * The Welcome Controller.
   *
   * A basic Login and SignUp page
   *
   * @package  app
   * @extends  Controller
   */
  class Controller_Welcome extends Controller {
    /**
     * The index page
     * @access  public
     * @return  Response
     */
    public function action_index() {
	    //check if the user is logged
      if (\Auth::check()) {
        //if yes go to the profile page
        Response::redirect('profile','location');
      } else {
        //if not, load the loggin page
        $view = View::forge('welcome/index');
        $view->cities = UserModel::getCities();
        return $view;
      }
    }

	  /**
	   * Register a new user in the database
	   * @access post
	   * @return Response
	   */
    public function post_registerUser() {
	    //get the user data
      $user = new UserDTO();
      $user->setEmail(Input::post('emailsignup'));
      $user->setName(Input::post('namesignup'));
      $user->setCity(Input::post('citysignup'));
      $user->setAddress(Input::post('addresssignup'));
	  
	    //check that both passwords are the same
      $password1 = Input::post('passwordsignup');
      $password2 = Input::post('passwordsignup_confirm');
      if ($password1 != $password2) {
        echo '<script language="javascript">"Sorry, passwords does not match");</script>';
        Response::redirect('/#toregister', 'refresh');
      }
	  
	    //try to register the user in the database
      try {
        if (UserModel::registerUser($user, $password1)) {
		    //if works login the new user
		    \Auth::remember_me($user->getId());
		    echo '<script language="javascript">alert("Congratulations, you have a new account");</script>';
        Response::redirect('profile', 'location');
		    } else {
		      //if not print an error message
		      echo '<script language="javascript">Sorry, email already exists");</script>';
          Response::redirect('/#toregister', 'refresh');
		    }
      } catch(Exception $e) {
		    //if something fail, print an error message
		    echo '<script language="javascript">alert("Sorry, there was a problem. Please try again later");</script>';
        Response::redirect('/#toregister', 'refresh');
      }
    }

	  /**
	   * Login an user in the store
	   * @access post
	   * @return Response
	   */
    public function post_checkUser() {
	    //get the user email and password
      $email      = Input::post('emaillogin');
      $password   = Input::post('passwordlogin');
	  
	    //check if the user credentials are correct
      $userID = UserModel::loginUser($email, $password);
      if ($userID == null) {
		    //if not print an error message
        $view = View::forge('welcome/index');
        echo '<script language="javascript">alert("Sorry, wrong user and/or password");</script>';
        Response::redirect('/#toregister', 'refresh');
      } else {
		    //if they are login the user
		    \Auth::remember_me($userID);
        Response::redirect('profile', 'location');
      }
    }

    /**
     * The 404 action for the application.
     * @access  public
     * @return  Response
     */
    public function action_404() {
      return Response::forge(Presenter::forge('welcome/404'), 404);
    }
  }
?>
{'fraction_non_alphanumeric': 0.1354996934396076, 'fraction_numerical': 0.006253832004904966, 'mean_word_length': 2.6459544032185964, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6123810', 'n_tokens_mistral': 2368, 'n_tokens_neox': 2230, 'n_words': 714}