Skip to content

Commit

Permalink
PHP SESSION Prefix and IP check, engine constants and user_constants …
Browse files Browse the repository at this point in the history
…as a function for better memory usage, add SQLite 3 driver, new loader for aditional database method, new REST HTPP method check, updated template engines: Mustache 2.13, Smarty 3.1.34, Twig 1.42.5, Twig 2.12.5, add Twig 3 support to PHP 7.2+, define Dwoo template as deprecated, Caches and captcha bugfix in PHP 5.4.x
  • Loading branch information
oliveira131 committed Apr 4, 2020
1 parent 488ac37 commit f16eefd
Show file tree
Hide file tree
Showing 1,372 changed files with 68,496 additions and 33,957 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ All routes are a mapping class with extends the primary Controller class. In a s
| DEFAULT_ROUTE | string | Define the default route to assume with initial page. Default is *common/home*. |
| CUSTOM_DB_CONFIG | string | Custom SQL to load application configs in database. |
| NOT_FOUND | string | Custom route to not found page. Default is error/not_found. |
| SESSION_PREFIX | string | Custom session cookie name prefix. Default is SESS. |

## Outputs and renders

Expand Down Expand Up @@ -209,15 +210,15 @@ For functions inside the controller that are different from index, for automatic
- TPL (basic template with PHP and HTML);
- [Twig](https://twig.symfony.com);
- [Mustache](https://mustache.github.io);
- [Dwoo](http://dwoo.org);
- [Smarty](https://www.smarty.net).
- [Smarty](https://www.smarty.net);
- [Dwoo](http://dwoo.org) (Deprecated, will be removed soon in further versions).

To use a determined template engine, just create a file with name of engine in extension, for sample, if you like to use a Twig, the template file is **demo.twig**, if desire the Mustache template, use **demo.mustache** extension file.
The ExacTI Phacil Framework allows to use various template engines in the same project.

## Easy made functions

This framework is very focused in agile, security and reusable code in PHP. Contains a very utile functions we see in the next section.
This framework is very focused in agile, security and reusable code in PHP. Contains a very utils functions we see in the next section.

### Database

Expand Down Expand Up @@ -271,6 +272,7 @@ To get these rows:
| postgre | PostgreSQL | Driver for connect to PostgreSQL databases. |
| sqlsrv | MS SQL Server | Connect a Microsoft SQL Server database with sqlsrv PHP extension. |
| sqlsrvpdo | MS SQL Server | Connect to Microsoft SQL Server using PDO driver. |
| sqlite3_db | SQLite 3 | Use the SQLite 3 database |


### Cache
Expand Down Expand Up @@ -388,7 +390,7 @@ This load is simple and registry to object of origin.
public function otherData() {
$this->load->database('mpdo', 'localhost', 'root', 'imtheking', 'nameDatabase');

$sql = $this->nameDatabase->query("SELECT * FROM mundo");
$sql = $this->db->nameDatabase->query("SELECT * FROM mundo");

return $sql->rows;
}
Expand Down Expand Up @@ -441,11 +443,14 @@ To create a model, put in the models folder a directory and file with the code.

To use a magic request system, you just need to call a `$this->request` method. For sample, to obtain a POST value, use `$this->request->post['field']` to get the post value with security.
For a $_SERVER predefined variables, use `$this->request->server['VALUE']` and `$this->request->get[key]` for $_GET values.
For REST services you can use `$this->request->method` to check if is a POST, GET, PUT, DELETE or other HTTP method. You can use the auxiliary functions to determinate the HTTP method, like `$this->request->isPUT()` to return *true* or *false* for PUT method. Also others methods have your functions, like `$this->request->isPOST()`, `$this->request->isGET()`, `$this->request->isHEAD()`, `$this->request->isCONNECT()`, `$this->request->isOPTIONS()`, `$this->request->isTRACE()`, `$this->request->isPATCH()` or `$this->request->isDELETE()`. All are equivalent to "is" function in request, like this sample: `$this->request->is('PUT')` return true or false for PUT HTTP method.
The advantages to use this requests instead the predefined variables of PHP are more the more security, upgradable and unicode values.

### Sessions

Sessions is a little different method, you can define and use with `$this->session->data['name']`.
All sessions called with Phacil are secure for HTTPS and IP verify, in other words, we made the session cookie stolen more difficulty, to have less chance of session hijacking.
You can define the session prefix name with constant `SESSION_PREFIX` in your config file. For default, the prefix is "SESS".

## Special controller parameters

Expand Down
4 changes: 3 additions & 1 deletion system/caches/caches.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
final class Caches {
private $expire = 3600;

public $dirCache = DIR_CACHE."caches/";
public $dirCache = "caches/";

public function __construct() {
$this->dirCache = DIR_CACHE."caches/";

if (!file_exists($this->dirCache)) {
mkdir($this->dirCache, 0760, true);
}
Expand Down
4 changes: 3 additions & 1 deletion system/captcha/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ class Captcha {
protected $perturbation = 0.90;
protected $noise_level = 1;
protected $background = 'black';
public $fonts = __DIR__."/fonts/*/*.ttf";
public $fonts = "/fonts/*/*.ttf";
public $pos = 'ABCDEFGHJKLMNOPQRSTUWVXZ0123456789abcdefhijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUWVXZ0123456789';

function __construct($width = NULL, $height = NULL, $numChar = 6, $background = 'black') {

$this->fonts = __DIR__."/fonts/*/*.ttf";

if(!extension_loaded('gd')){
throw new \Exception("The captcha function requires GD extension on PHP!");
}
Expand Down
1 change: 0 additions & 1 deletion system/database/database/dbmysqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,3 @@ public function __destruct() {
$this->connection->close();
}
}
?>
61 changes: 61 additions & 0 deletions system/database/database/sqlite3_db.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

final class Sqlite3_db {
private $connection;

public function __construct($hostname, $username = null, $password = null, $database, $port = '3306', $charset = 'utf8mb4')
{
$this->connection = new SQLite3($hostname.$database, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE, $password);

if (!$this->connection) {
throw new \Exception('Error: ' . $this->connection->lastErrorMsg() . '<br />Error No: ' . $this->connection->lastErrorCode());
}
}

public function query($sql){
//$query = $this->connection->query($sql);

if ($stm = $this->connection->prepare($sql)) {

$query = $stm->execute();

if (!$query instanceof \SQLite3Result || $query->numColumns() == 0)
return true;


$data = [];
while ($row = $query->fetchArray(SQLITE3_ASSOC)) {
$data[] = $row;
}
$result = new \stdClass();
$result->num_rows = (!empty($data)) ? count($data) : 0;
$result->row = isset($data[0]) ? $data[0] : array();
$result->rows = $data;
$query->finalize();
return $result;

} else {
throw new \Exception('Error: ' . $this->connection->lastErrorMsg() . '<br />Error No: ' . $this->connection->lastErrorCode() . '<br />' . $sql);
}

}

public function escape($value) {
return $this->connection->escapeString($value);
}

public function countAffected() {
return $this->connection->changes();
}
public function getLastId() {
return $this->connection->lastInsertRowID();
}

public function isConnected() {
return ($this->connection) ? true : false;
}

public function __destruct() {
$this->connection->close();
}
}
6 changes: 5 additions & 1 deletion system/database/library/db.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,9 @@ private function Cache($sql) {
return $this->driver->query($sql);
}
}

public function createSubBase($nome, $object) {

$this->$nome = $object;
}
}
?>
2 changes: 1 addition & 1 deletion system/engine/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.4.3
1.5.0
22 changes: 14 additions & 8 deletions system/engine/controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ abstract class Controller {
protected $twig = array();
protected $error = array();
protected $output;
public $templateTypes = ["tpl", "twig", "mustache", "smarty", "dwoo"];

public function __construct($registry) {
$this->registry = $registry;
Expand Down Expand Up @@ -62,8 +63,6 @@ protected function render() {
if($this->template === NULL) {
$pegRout = explode("/", $this->request->get['route']);

$this->templateTypes = array("tpl", "twig", "dwoo", "mustache", "smarty");

$thema = ($this->config->get("config_template") != NULL) ? $this->config->get("config_template") : "default";

foreach($this->templateTypes as $extensionTemplate) {
Expand Down Expand Up @@ -110,27 +109,32 @@ protected function render() {
'cache' => DIR_CACHE."twig/",
'debug' => (defined('DEBUG')) ? DEBUG : false
);
$loader = new Twig_Loader_Filesystem(DIR_TEMPLATE);
$twig = new Twig_Environment($loader, $config);
$TwigLoaderFilesystem = constant('TwigLoaderFilesystem');
$Twig_Environment = constant('TwigEnvironment');
$Twig_SimpleFilter = constant('TwigSimpleFilter');
$Twig_Extension_Debug = constant('TwigExtensionDebug');

$loader = new $TwigLoaderFilesystem (DIR_TEMPLATE);
$twig = new $Twig_Environment($loader, $config);

if($config['debug']) {
$twig->addExtension(new Twig_Extension_Debug());
$twig->addExtension(new $Twig_Extension_Debug());
}

$twig->addExtension(new transExtension());

$twig->addFilter(new Twig_SimpleFilter('translate', function ($str) {
$twig->addFilter(new $Twig_SimpleFilter('translate', function ($str) {
// do something different from the built-in date filter
return traduzir($str);
}));

$twig->addFilter(new Twig_SimpleFilter('config', function ($str) {
$twig->addFilter(new $Twig_SimpleFilter('config', function ($str) {
// do something different from the built-in date filter
return $this->config->get($str);
}));

foreach ($this->twig as $key => $item) {
$twig->addFilter(new Twig_SimpleFilter($key, $item));
$twig->addFilter(new $Twig_SimpleFilter($key, $item));
}

$template = $twig->load($this->template);
Expand All @@ -141,6 +145,8 @@ protected function render() {
case 'dwoo':
require_once(DIR_SYSTEM."templateEngines/Dwoo/autoload.php");

@trigger_error(sprintf("Dwoo template engine is deprecated since Phacil 1.5.0 and will be removed in further versions. Migration to Smarty (most similarity) or other template engine is recommended."), E_USER_DEPRECATED);

$dwoo = new Dwoo\Core();

// Configure directories
Expand Down
16 changes: 13 additions & 3 deletions system/engine/loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,21 @@ public function controller($control) {
public function database($driver, $hostname, $username, $password, $database, $port = NULL, $charset = NULL) {
$file = DIR_SYSTEM . 'database/database/' . $driver . '.php';
$class = ($driver);

$replace = [
'/' => '_',
'.' => '_'
];

$database_name = str_replace(array_keys($replace), array_values($replace), preg_replace('/[^a-zA-Z0-9]/', '', $database));


if (file_exists($file)) {
include_once($file);

$this->registry->set(str_replace('/', '_', $database), new $class($hostname, $username, $password, $database));
//include_once($file);

$this->db->createSubBase($database_name, new DB($driver, $hostname, $username, $password, $database));

return $database_name;
} else {
trigger_error('Error: Could not load database ' . $driver . '!');
exit();
Expand Down
3 changes: 1 addition & 2 deletions system/engine/log.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
final class Log {
private $filename;

public function __construct($filename) {
public function __construct($filename = "error.log") {
$this->filename = fopen(DIR_LOGS . $filename, 'a');
}

Expand All @@ -14,4 +14,3 @@ public function __destruct() {
fclose($this->filename);
}
}
?>
1 change: 0 additions & 1 deletion system/engine/registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@ public function has($key) {
return isset($this->data[$key]);
}
}
?>
102 changes: 67 additions & 35 deletions system/request/autoload.php
Original file line number Diff line number Diff line change
@@ -1,39 +1,71 @@
<?php
final class Request {
public $get = array();
public $post = array();
public $cookie = array();
public $files = array();
public $server = array();

public function __construct() {
$_GET = $this->clean($_GET);
$_POST = $this->clean($_POST);
$_REQUEST = $this->clean($_REQUEST);
$_COOKIE = $this->clean($_COOKIE);
$_FILES = $this->clean($_FILES);
$_SERVER = $this->clean($_SERVER);

$this->get = $_GET;
$this->post = $_POST;
$this->request = $_REQUEST;
$this->cookie = $_COOKIE;
$this->files = $_FILES;
$this->server = $_SERVER;
}

public function clean($data) {
if (is_array($data)) {
foreach ($data as $key => $value) {
unset($data[$key]);

$data[$this->clean($key)] = $this->clean($value);
}
} else {
$data = htmlspecialchars($data, ENT_COMPAT);
}
public $get = array();
public $post = array();
public $cookie = array();
public $files = array();
public $server = array();
public $method;

return $data;
}
public function __construct() {
$_GET = $this->clean($_GET);
$_POST = $this->clean($_POST);
$_REQUEST = $this->clean($_REQUEST);
$_COOKIE = $this->clean($_COOKIE);
$_FILES = $this->clean($_FILES);
$_SERVER = $this->clean($_SERVER);

$this->get = $_GET;
$this->post = $_POST;
$this->request = $_REQUEST;
$this->cookie = $_COOKIE;
$this->files = $_FILES;
$this->server = $_SERVER;
$this->method = (isset($this->server['REQUEST_METHOD'])) ? $this->clean($this->server['REQUEST_METHOD']) : false;
}

public function clean($data) {
if (is_array($data)) {
foreach ($data as $key => $value) {
unset($data[$key]);

$data[$this->clean($key)] = $this->clean($value);
}
} else {
$data = htmlspecialchars($data, ENT_COMPAT);
}

return $data;
}

public function isPOST() {
return $this->is('POST');
}
public function isGET() {
return $this->is('GET');
}
public function isHEAD() {
return $this->is('HEAD');
}
public function isPUT() {
return $this->is('PUT');
}
public function isDELETE() {
return $this->is('DELETE');
}
public function isCONNECT() {
return $this->is('CONNECT') ;
}
public function isOPTIONS() {
return $this->is('OPTIONS') ;
}
public function isTRACE() {
return $this->is('TRACE');
}
public function isPATCH() {
return $this->is('PATCH');
}
public function is($method){
return ($this->method == $method) ? true : false;
}
}
?>
Loading

0 comments on commit f16eefd

Please sign in to comment.