Skip to content

Commit

Permalink
Merge pull request #80 from philippebeck/dev
Browse files Browse the repository at this point in the history
Release 4.2.0
  • Loading branch information
philippebeck authored Jun 20, 2020
2 parents a6ea746 + 8700847 commit f6645c6
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 52 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "philippebeck/pam",
"version": "4.1.1",
"version": "4.2.0",
"type": "library",
"description": "Php Approachable Microframework",
"keywords": [
Expand Down
8 changes: 6 additions & 2 deletions core/Controller/MainController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Pam\Controller;

use Pam\View\PamExtension;
use Pam\View\GlobalsExtension;
use Pam\View\MainExtension;
use Pam\View\ServiceExtension;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
Expand Down Expand Up @@ -40,7 +42,9 @@ public function __construct()
));

$this->twig->addExtension(new DebugExtension());
$this->twig->addExtension(new PamExtension());
$this->twig->addExtension(new MainExtension());
$this->twig->addExtension(new GlobalsExtension());
$this->twig->addExtension(new ServiceExtension());
}

/**
Expand Down
4 changes: 2 additions & 2 deletions core/Controller/Service/SecurityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace Pam\Controller\Service;

use Pam\Controller\GlobalsController;
use Pam\Controller\MainController;
use ReCaptcha\ReCaptcha;

/**
* Class SecurityManager
* @package Pam\Controller\Service
*/
class SecurityManager extends GlobalsController
class SecurityManager extends MainController
{
/**
* @param string $response
Expand Down
63 changes: 16 additions & 47 deletions core/View/PamExtension.php → core/View/GlobalsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
use Twig\TwigFunction;

/**
* Class PamExtension
* Class GlobalsExtension
* @package Pam\View
*/
class PamExtension extends AbstractExtension
class GlobalsExtension extends AbstractExtension
{
/**
* @var mixed
*/
private $get = null;

/**
* @var array|mixed
*/
Expand All @@ -27,12 +32,13 @@ class PamExtension extends AbstractExtension
private $alert = null;

/**
* PamExtension constructor.
* GlobalsExtension constructor.
*/
public function __construct()
{
$this->session = filter_var_array($_SESSION);
$this->alert = $this->session["alert"];
$this->get = filter_input_array(INPUT_GET);
$this->session = filter_var_array($_SESSION);
$this->alert = $this->session["alert"];

if (isset($this->session["user"])) {
$this->user = $this->session["user"];
Expand All @@ -45,9 +51,7 @@ public function __construct()
public function getFunctions()
{
return array(
new TwigFunction("url", array($this, "url")),
new TwigFunction("cleanString", array($this, "cleanString")),
new TwigFunction("getSessionArray", array($this, "getSessionArray")),
new TwigFunction("getGetVar", array($this, "getGetVar")),
new TwigFunction("hasAlert", array($this, "hasAlert")),
new TwigFunction("getAlertType", array($this, "getAlertType")),
new TwigFunction("getAlertMessage", array($this, "getAlertMessage")),
Expand All @@ -57,47 +61,12 @@ public function getFunctions()
}

/**
* @param string $page
* @param array $params
* @return string
*/
public function url(string $page, array $params = [])
{
$params["access"] = $page;

return "index.php?" . http_build_query($params);
}

/**
* @param string $string
* @return string
*/
public function cleanString(string $string)
{
$string =
str_replace("_", " ",
str_replace(array("ù", "û", "ü"), "u",
str_replace(array("ô", "ö"), "o",
str_replace(array("î", "ï"), "i",
str_replace(array("é", "è", "ê", "ë"), "e",
str_replace(array("ç"), "c",
str_replace(array("à", "â", "ä"), "a", $string)
)
)
)
)
)
);

return ucwords($string);
}

/**
* @return array|mixed
* @param $var
* @return mixed
*/
public function getSessionArray()
public function getGetVar($var)
{
return $this->session;
return $this->get[$var];
}

/**
Expand Down
47 changes: 47 additions & 0 deletions core/View/MainExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Pam\View;

use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

/**
* Class MainExtension
* @package Pam\View
*/
class MainExtension extends AbstractExtension
{
/**
* @return array|TwigFunction[]
*/
public function getFunctions()
{
return array(
new TwigFunction("url", array($this, "url")),
new TwigFunction("redirect", array($this, "redirect"))
);
}

/**
* @param string $page
* @param array $params
* @return string
*/
public function url(string $page, array $params = [])
{
$params["access"] = $page;

return "index.php?" . http_build_query($params);
}

/**
* @param string $page
* @param array $params
*/
public function redirect(string $page, array $params = [])
{
header("Location: " . $this->url($page, $params));

exit;
}
}
66 changes: 66 additions & 0 deletions core/View/ServiceExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Pam\View;

use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

/**
* Class ServiceExtension
* @package Pam\View
*/
class ServiceExtension extends AbstractExtension
{
/**
* @var mixed|null
*/
private $session = null;

/**
* Service Extension constructor.
*/
public function __construct()
{
$this->session = filter_var_array($_SESSION);
}

/**
* @return array|TwigFunction[]
*/
public function getFunctions()
{
return array(
new TwigFunction("cleanString", array($this, "cleanString")),
new TwigFunction("checkIsAdmin", array($this, "checkIsAdmin"))
);
}

/**
* @param string $string
* @return string
*/
public function cleanString(string $string)
{
$string = str_replace("_", " ", $string);

return ucwords($string);
}

public function checkIsAdmin()
{
$isAdmin = false;

if (isset($session["user"]["admin"])) {
if ($this->session["admin"] === true || $this->session["admin"] === 1) {
$isAdmin = true;
}

} elseif (isset($session["user"]["role"])) {
if ($this->session["role"] === 1 || $this->session["role"] === "admin") {
$isAdmin = true;
}
}

return $isAdmin;
}
}

0 comments on commit f6645c6

Please sign in to comment.