Skip to content

Commit

Permalink
Minor errors are corrected and login template prepared
Browse files Browse the repository at this point in the history
  • Loading branch information
Alxarafe committed Dec 21, 2018
1 parent d687673 commit fd8b21b
Show file tree
Hide file tree
Showing 25 changed files with 1,103 additions and 120 deletions.
31 changes: 31 additions & 0 deletions src/Core/Base/Controller.php
@@ -0,0 +1,31 @@
<?php
/**
* Alxarafe. Development of PHP applications in a flash!
* Copyright (C) 2018 Alxarafe <info@alxarafe.com>
*/
namespace Alxarafe\Base;

use Alxarafe\Base\View;
use Alxarafe\Helpers\Config;
use Alxarafe\Helpers\Skin;

class Controller
{

public $user;

public function __construct()
{
//echo "<p>En constructor de Controller</p>";
}

public function run(array $vars = [])
{
$vars['user'] = Config::$user->getUser();
if (Skin::$view == null) {
Skin::$view = new View($this);
}

Skin::$view->run($vars);
}
}
136 changes: 136 additions & 0 deletions src/Core/Base/View.php
@@ -0,0 +1,136 @@
<?php
/**
* Alxarafe. Development of PHP applications in a flash!
* Copyright (C) 2018 Alxarafe <info@alxarafe.com>
*/
namespace Alxarafe\Base;

use Alxarafe\Helpers\Config;
use Alxarafe\Helpers\Skin;
use Alxarafe\Helpers\Debug;

class View
{

private $vars;
protected $css;
protected $js;

public function __construct($controller = null)
{
Skin::setTemplatesEngine($config['templatesEngine'] ?? 'twig');

$this->vars = [];
$this->vars['ctrl'] = $controller;
$this->vars['view'] = $this;
$this->css = [];
$this->js = [];
$this->addCSS();
$this->addJS();
}

/**
* Check if the resource is in the application's resource folder (for example, in the css or js folders
* of the skin folder). It's a specific file.
*
* If it can not be found, check if it is in the templates folder (for example in the css or
* js folders of the templates folder). It's a common file.
*
* If it is not in either of the two, no route is specified (it will surely give loading error).
*
* @param string $resourceName , is the name of the file (without extension)
* @param string $resourceExtension , is the extension (type) of the resource (js or css)
* @param boolean $relative , set to false for use an absolute path.
*
* @return string the complete path of resource.
*/
protected function addResource(string $resourceName, string $resourceExtension = 'css', $relative = true): string
{
$absPath = $resourceName . '.' . $resourceExtension;
if ($relative) {
$path = Skin::getTemplatesFolder() . $absPath;
Debug::addMessage('messages', "Exists resource '$path'?");
if (file_exists($path)) {
Debug::addMessage('messages', "Using resource $path");
return $path;
}
$path = Config::getVar('commonTemplatesFolder') . $absPath;
Debug::addMessage('messages', "Exists resource '$path'?");
if (file_exists($path)) {
Debug::addMessage('messages', "Using resource $path");
return $path;
}
$path = DEFAULT_TEMPLATES_FOLDER . $absPath;
Debug::addMessage('messages', "Exists resource '$path'?");
if (file_exists($path)) {
Debug::addMessage('messages', "Using resource $path");
return $path;
}
$path = VENDOR_FOLDER . $absPath;
Debug::addMessage('messages', "Exists resource '$path'?");
if (file_exists($path)) {
Debug::addMessage('messages', "Using resource $path");
return $path;
}
}
Debug::addMessage('messages', "Adding absolute resource $absPath");
return $absPath;
}

/**
* addCSS includes the common CSS files to all views templates. Also defines CSS folders templates.
*
* @return void
*/
public function addCSS()
{
$this->css[] = $this->addResource(VENDOR_FOLDER . 'twbs/bootstrap/dist/css/bootstrap.min', 'css', false);
$this->css[] = $this->addResource('css/alxarafe', 'css');
}

/**
* addJS includes the common JS files to all views templates. Also defines JS folders templates.
*
* @return void
*/
public function addJS()
{
$this->js[] = $this->addResource(VENDOR_FOLDER . 'components/jquery/jquery.min', 'js', false);
$this->js[] = $this->addResource(VENDOR_FOLDER . 'twbs/bootstrap/dist/css/bootstrap.min', 'js', false);
$this->js[] = $this->addResource('js/alxarafe', 'js');
}

public function getHeader()
{
return Debug::getRenderHeader();
}

public function getFooter()
{
return Debug::getRenderFooter();
}

public function getVar($name)
{
return isset($this->vars[$name]) ?? [];
}

public function setVar($name, $value)
{
$this->vars[$name] = $value;
}

public function run(array $data = [])
{
if (!Skin::hasTemplate()) {
Skin::setTemplate('default');
}
$this->vars = \array_merge($this->vars, $data);
$this->vars['cssCode'] = $this->css;
$this->vars['jsCode'] = $this->js;
$this->vars['errors'] = Config::getErrors();
echo Skin::render($this->vars);

return true;
}
}
22 changes: 22 additions & 0 deletions src/Core/Controllers/EditConfig.php
@@ -0,0 +1,22 @@
<?php
/**
* Alxarafe. Development of PHP applications in a flash!
* Copyright (C) 2018 Alxarafe <info@alxarafe.com>
*/
namespace Alxarafe\Controllers;

use Alxarafe\Base\Controller;
use Alxarafe\Helpers\Config;
use Alxarafe\Helpers\Auth;
use Alxarafe\Helpers\Skin;
use Alxarafe\Helpers\Debug;

class EditConfig extends Controller
{

public function __construct()
{
parent::__construct();
Skin::setTemplate('dbconfig');
}
}
23 changes: 23 additions & 0 deletions src/Core/Controllers/Login.php
@@ -0,0 +1,23 @@
<?php
/**
* Alxarafe. Development of PHP applications in a flash!
* Copyright (C) 2018 Alxarafe <info@alxarafe.com>
*/
namespace Alxarafe\Controllers;

use Alxarafe\Base\Controller;
use Alxarafe\Helpers\Config;
use Alxarafe\Helpers\Auth;
use Alxarafe\Helpers\Skin;
use Alxarafe\Helpers\Debug;
use Alxarafe\Views\Login as LoginView;

class Login extends Controller
{

public function __construct()
{
parent::__construct();
Skin::setView(new LoginView($this));
}
}
68 changes: 47 additions & 21 deletions src/Core/Helpers/Auth.php
Expand Up @@ -6,36 +6,66 @@
namespace Alxarafe\Helpers;

use Alxarafe\Models\Users;
use Alxarafe\Controllers\Login;

class Auth extends Users
{

public function logout()
/**
* TODO:
*/
const COOKIE_EXPIRATION = 0;

private $user = null;

public function __construct()
{
parent::__construct();
$this->getCookieUser();
}

private function getCookieUser()
{
if ($this->user == null) {
if (isset($_COOKIE['user'])) {
$this->user = $_COOKIE['user'];
}
}
}

private function setCookieUser()
{
setcookie('user', $this->user == null ? '' : $this->user, COOKIE_EXPIRATION);
}

private function clearCookieUser()
{
Debug::addMessage('messages', 'Auth::Logout(): ' . (self::$user == null ? 'No había usuario identificado.' : 'El usuario ' . self::$user . ' ha cerrado la sesión'));
Self::$user = null;
setcookie('user', '');
unset($_COOKIE['user']);
}

public function login()
{
(new Login())->run();
}

public function logout()
{
Debug::addMessage('messages', 'Auth::Logout(): ' . ($this->user == null ? 'There was no identified user.' : 'User' . $this->user . ' has successfully logged out'));
$this->user = null;
$this->clearCookieUser();
}

public function setUser($user, $password)
{
if (!database::tableExists($this->tablename)) {
Self::$user = null;
setcookie('user', '');
unset($_COOKIE['user']);
Debug::addMessage('SQL', "La tabla $tablename no existe");
return;
}
//$user = Auth::$userTable->where('username', $user)->get()->toArray();
//$_user = Auth::$userTable->where('username', $user)->get()->toArray();
$_user = Config::$dbEngine->select("SELECT * FROM {$this->tablename} WHERE username='$user';");
if (count($_user) > 0 && md5($password) == $_user[0]['password']) {
Self::$user = $user;
$this->user = $user;
setcookie('user', $user);
Debug::addMessage('SQL', "$user autenticado");
} else {
Self::$user = null;
$this->user = null;
Config::setError('Error al autenticar el usuario. Revise usuario y contraseña.');
setcookie('user', '');
unset($_COOKIE['user']);
if (isset($_user[0])) {
Expand All @@ -44,15 +74,11 @@ public function setUser($user, $password)
Debug::addMessage('SQL', "Comprobado md5:" . md5($password) . ', en fichero no existe usuario ' . $user);
}
}
return $this->user != null;
}

public static function getUser()
public function getUser()
{
if (Self::$user == null) {
if (isset($_COOKIE['user'])) {
Self::$user = $_COOKIE['user'];
}
}
return Self::$user;
return $this->user;
}
}

0 comments on commit fd8b21b

Please sign in to comment.