Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 54365abf1d | |||
| 758abcaf48 |
@@ -1,5 +1,3 @@
|
|||||||
# mdesouza
|
# mdesouza
|
||||||
|
|
||||||
Utils classes
|
Utils classes by mdesouza3@gmail.com
|
||||||
|
|
||||||
Release-v1.0.6
|
|
||||||
@@ -1,57 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Mdesouza\Utils;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @file
|
|
||||||
* Implementation of cookies.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class implementation for cookies.
|
|
||||||
*/
|
|
||||||
class Cookie {
|
|
||||||
/**
|
|
||||||
* Content of $_COOKIE array.
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
private $cookie = [];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor.
|
|
||||||
*/
|
|
||||||
public function __construct() {
|
|
||||||
$this->cookie = $_COOKIE;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if a cookie exists.
|
|
||||||
*/
|
|
||||||
public function cookieExists($name) {
|
|
||||||
return isset($this->cookie[$name]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get value of a cookie.
|
|
||||||
*/
|
|
||||||
public function getCookie($name) : string {
|
|
||||||
return $this->cookieExists($name) ? $this->cookie[$name] : "";
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set a cookie.
|
|
||||||
*/
|
|
||||||
public function setCookie(array $options) {
|
|
||||||
setcookie(
|
|
||||||
$options['name'],
|
|
||||||
$options['value'],
|
|
||||||
$options['expires'],
|
|
||||||
$options['path'],
|
|
||||||
$options['domain'],
|
|
||||||
$options['secure'],
|
|
||||||
$options['httponly']
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Mdesouza\Utils;
|
namespace Mdesouza\Utils\DB;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @file
|
* @file
|
||||||
|
|||||||
@@ -1,186 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Mdesouza\Utils;
|
|
||||||
|
|
||||||
use Twig\Extension\AbstractExtension;
|
|
||||||
use Twig\TwigFilter;
|
|
||||||
use Twig\TwigFunction;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A class to extend Twig Filters.
|
|
||||||
*/
|
|
||||||
class MDTwigExtension extends AbstractExtension {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Regular expression for a brazilian phone number.
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
private $phoneRegex = '/^([\d]{2})([\d]{1})?([\d]{4})([\d]{4})$/';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Regular expression for a Brazilian zip code.
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
private $cepRegex = '/^([\d]{5})([\d]{3})?$/';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Regular expression for CPF.
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
private $cpfRegex = '/^([\d]{3})([\d]{3})([\d]{3})([\d]{2})$/';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Regular expression for CNPJ.
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
private $cnpjRegex = '/^([\d]{2})([\d]{3})([\d]{3})([\d]{4})([\d]{2})$/';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the available filters for this extension.
|
|
||||||
*/
|
|
||||||
public function getFilters() {
|
|
||||||
return [
|
|
||||||
new TwigFilter('phone', [$this, 'formatPhone']),
|
|
||||||
new TwigFilter('cep', [$this, 'formatCEP']),
|
|
||||||
new TwigFilter('cpf', [$this, 'formatCPF']),
|
|
||||||
new TwigFilter('cnpj', [$this, 'formatCNPJ']),
|
|
||||||
new TwigFilter('real', [$this, 'formatReal']),
|
|
||||||
new TwigFilter('unicode', [$this, 'returnUnicode']),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the availeble functions for this extension.
|
|
||||||
*/
|
|
||||||
public function getFunctions() {
|
|
||||||
return [
|
|
||||||
new TwigFunction('version', [$this, 'getVersionNumber']),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get version of the software from db.
|
|
||||||
*
|
|
||||||
* @param string $currentVersion
|
|
||||||
* The current version.
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
* The latest version, according to the db.
|
|
||||||
*/
|
|
||||||
public function getVersionNumber($currentVersion) {
|
|
||||||
if ($_SESSION['devel'] == 1) {
|
|
||||||
$version = exec('git rev-parse --abbrev-ref HEAD');
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$version = $_SESSION['version'];
|
|
||||||
}
|
|
||||||
return $version;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Format a phone number - Brazilian phone number.
|
|
||||||
*
|
|
||||||
* @param string $number
|
|
||||||
* The number to be formated.
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
* The formatted number.
|
|
||||||
*/
|
|
||||||
public function formatPhone(string $number): string {
|
|
||||||
return preg_replace($this->phoneRegex, '($1) $2 $3-$4', $number);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Format a brazilian zip code.
|
|
||||||
*
|
|
||||||
* @param string $cep
|
|
||||||
* The value to be formatted.
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
* The formatted value.
|
|
||||||
*/
|
|
||||||
public function formatCep(string $cep): string {
|
|
||||||
return preg_replace($this->cepRegex, '$1-$2', $cep);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Format a CPF.
|
|
||||||
*
|
|
||||||
* @param string $cpf
|
|
||||||
* The value to be formatted.
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
* The formatted value (will mask part of the CPF).
|
|
||||||
*/
|
|
||||||
public function formatCpf(string $cpf): string {
|
|
||||||
return preg_replace($this->cpfRegex, '$1.$2.$3-$4', $cpf);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Format CNPJ.
|
|
||||||
*
|
|
||||||
* @param string $cnpj
|
|
||||||
* The value to be formated.
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
* The formatted value
|
|
||||||
*/
|
|
||||||
public function formatCnpj(string $cnpj): string {
|
|
||||||
return preg_replace($this->cnpjRegex, '$1.$2.$3/$4-$5', $cnpj);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Format number as Brazilian currency.
|
|
||||||
*
|
|
||||||
* @param string $number
|
|
||||||
* The number to be formatted.
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
* The formatted number.
|
|
||||||
*/
|
|
||||||
public function formatReal(string $number): string {
|
|
||||||
$a = new \NumberFormatter("pt-BR", \NumberFormatter::DECIMAL);
|
|
||||||
$a->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, 2);
|
|
||||||
return $a->format((float) $number);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return a converted unicode to HTML.
|
|
||||||
*
|
|
||||||
* @param string $value
|
|
||||||
* The number to be formatted.
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
* The formatted value.
|
|
||||||
*/
|
|
||||||
public function returnUnicode(string $value): string {
|
|
||||||
$value = str_replace("u0", "\\u0", $value);
|
|
||||||
$unicode = preg_replace_callback('/\\\\u([0-9a-fA-F]{4})/', function ($match) {
|
|
||||||
$encodings = [
|
|
||||||
"UCS-2",
|
|
||||||
"UCS-2BE",
|
|
||||||
"UTF-16",
|
|
||||||
"UTF-16BE",
|
|
||||||
"ISO-8859-1",
|
|
||||||
"ISO-8859-4",
|
|
||||||
"ISO-8859-9",
|
|
||||||
"ISO-8859-10",
|
|
||||||
"ISO-8859-14",
|
|
||||||
"ISO-8859-15",
|
|
||||||
"byte2be",
|
|
||||||
"HTML-ENTITIES",
|
|
||||||
"8bit",
|
|
||||||
"Windows-1252",
|
|
||||||
];
|
|
||||||
|
|
||||||
return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', $encodings);
|
|
||||||
}, str_replace(['\"', '\/', '"', '\n'], ['"', '/', '\'', "\n"], $value));
|
|
||||||
|
|
||||||
return $unicode;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
+3
-1
@@ -1,12 +1,14 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Mdesouza\Utils;
|
namespace Mdesouza\Utils\DB;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @file
|
* @file
|
||||||
* A database class around the PDO object.
|
* A database class around the PDO object.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
include_once 'db.inc.php';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A mysql class around the PDO object.
|
* A mysql class around the PDO object.
|
||||||
*/
|
*/
|
||||||
|
|||||||
+8
-37
@@ -21,12 +21,10 @@ class TwigRender {
|
|||||||
*/
|
*/
|
||||||
private $template;
|
private $template;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Templates paths.
|
* Templates paths
|
||||||
*
|
*/
|
||||||
* @var string
|
private $templates_path;
|
||||||
*/
|
|
||||||
private $templatesPath;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Template data.
|
* Template data.
|
||||||
@@ -38,9 +36,9 @@ class TwigRender {
|
|||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
*/
|
*/
|
||||||
public function __construct(string $template, string $templates_path, array $data = []) {
|
public function __construct(string $template, array $templates_path, array $data = []) {
|
||||||
$this->template = $template;
|
$this->template = $template;
|
||||||
$this->templatesPath = $templates_path;
|
$this->templates_path = $templates_path;
|
||||||
$this->data = $data;
|
$this->data = $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,16 +47,13 @@ class TwigRender {
|
|||||||
*/
|
*/
|
||||||
public function render() : string {
|
public function render() : string {
|
||||||
|
|
||||||
$this->data['host'] = $_SERVER['HTTP_HOST'];
|
$data['host'] = $_SERVER['HTTP_HOST'];
|
||||||
$html = "";
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$loader = new FilesystemLoader([]);
|
$loader = new FilesystemLoader($this->templates_path);
|
||||||
$loader = $this->addPaths($loader);
|
|
||||||
$twig = new Environment($loader, ['debug' => TRUE]);
|
$twig = new Environment($loader, ['debug' => TRUE]);
|
||||||
$twig->addExtension(new DebugExtension());
|
$twig->addExtension(new DebugExtension());
|
||||||
$twig->addExtension(new MFDESTwigExtension());
|
$twig->addExtension(new MFDESTwigExtension());
|
||||||
$twig->addGlobal('session', $_SESSION);
|
|
||||||
$html = $twig->render($this->template, ['data' => $this->data]);
|
$html = $twig->render($this->template, ['data' => $this->data]);
|
||||||
}
|
}
|
||||||
catch (LoaderError $e) {
|
catch (LoaderError $e) {
|
||||||
@@ -77,28 +72,4 @@ class TwigRender {
|
|||||||
return $html;
|
return $html;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Recursively add all directories under the templates base path.
|
|
||||||
*/
|
|
||||||
private function addPaths(FilesystemLoader $loader) : FilesystemLoader {
|
|
||||||
|
|
||||||
// Add base path.
|
|
||||||
$loader->addPath($this->templatesPath);
|
|
||||||
|
|
||||||
// Create a recursive iterator to find all subdirectories.
|
|
||||||
$iterator = new \RecursiveIteratorIterator(
|
|
||||||
new \RecursiveDirectoryIterator($this->templatesPath, \FilesystemIterator::SKIP_DOTS),
|
|
||||||
\RecursiveIteratorIterator::SELF_FIRST
|
|
||||||
);
|
|
||||||
|
|
||||||
// Add each sub-dir under the base path.
|
|
||||||
foreach ($iterator as $info) {
|
|
||||||
if ($info->isDir()) {
|
|
||||||
$loader->addPath($info->getPathname());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $loader;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
-195
@@ -1,195 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Mdesouza\Utils;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Show variables and exit or continue.
|
|
||||||
*/
|
|
||||||
class Utils {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Print vars and exit.
|
|
||||||
*/
|
|
||||||
public static function printAndExit($data, $trace = TRUE) {
|
|
||||||
$bt = debug_backtrace();
|
|
||||||
echo "<pre>";
|
|
||||||
if (TRUE == $trace) {
|
|
||||||
echo "Trace: " . PHP_EOL;
|
|
||||||
foreach ($bt as $atrace) {
|
|
||||||
echo "$atrace[file]:$atrace[line]" . PHP_EOL;
|
|
||||||
}
|
|
||||||
echo PHP_EOL;
|
|
||||||
}
|
|
||||||
|
|
||||||
print_r($data);
|
|
||||||
echo "</pre>";
|
|
||||||
die("");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Print vars and continue.
|
|
||||||
*/
|
|
||||||
public static function printAndContinue($data, $trace = TRUE) {
|
|
||||||
$bt = debug_backtrace();
|
|
||||||
echo "<pre>";
|
|
||||||
if (TRUE == $trace) {
|
|
||||||
echo "Trace: " . PHP_EOL;
|
|
||||||
foreach ($bt as $atrace) {
|
|
||||||
echo "$atrace[file]:$atrace[line]" . PHP_EOL;
|
|
||||||
}
|
|
||||||
echo PHP_EOL;
|
|
||||||
}
|
|
||||||
|
|
||||||
print_r($data);
|
|
||||||
echo "</pre>";
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert date to yyyy-mm-dd format.
|
|
||||||
*
|
|
||||||
* @param string $input
|
|
||||||
* Date to be converted.
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
* Converted date.
|
|
||||||
*/
|
|
||||||
public static function dateToMysql($input): string {
|
|
||||||
if ($input == "") {
|
|
||||||
return "0000-00-00";
|
|
||||||
}
|
|
||||||
|
|
||||||
return date("Y-m-d", strtotime($input));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Validate an email address.
|
|
||||||
*
|
|
||||||
* @param string $email
|
|
||||||
* Email to be validted.
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
* TRUE if the email is valid; FALSE otherwise.
|
|
||||||
*/
|
|
||||||
public static function validateEmail($email) : bool {
|
|
||||||
if ("" != $email) {
|
|
||||||
$email = filter_var((string) $email, FILTER_VALIDATE_EMAIL);
|
|
||||||
if (FALSE == $email) {
|
|
||||||
return FALSE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return TRUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Trim all items inside an array.
|
|
||||||
*
|
|
||||||
* @param array $array
|
|
||||||
* Array to be trimmed.
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
* The trimmed array.
|
|
||||||
*/
|
|
||||||
public static function trimAll($array) : array {
|
|
||||||
$array_r = [];
|
|
||||||
foreach ($array as $key => $value) {
|
|
||||||
$value = str_replace("\"", "", $value);
|
|
||||||
$value = preg_replace('/(\s)+/', '$1', $value);
|
|
||||||
$value = trim($value);
|
|
||||||
if (is_numeric($key)) {
|
|
||||||
if (trim($value != "")) {
|
|
||||||
$array_r[] = $value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$array_r[$key] = $value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $array_r;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove code/html/javascript, double quotes and other invalid inputs.
|
|
||||||
*
|
|
||||||
* @param string $input
|
|
||||||
* String to be sanitized.
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
* Sanitized string.
|
|
||||||
*/
|
|
||||||
public static function sanitizeInput($input): string {
|
|
||||||
$input = strip_tags($input);
|
|
||||||
$input = str_replace('"', '', $input);
|
|
||||||
return $input;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove bad characters from currency strings.
|
|
||||||
*
|
|
||||||
* @param string $input
|
|
||||||
* String to sanitize.
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
* Sanitized string.
|
|
||||||
*/
|
|
||||||
public static function sanitizeCurrency($input): string {
|
|
||||||
$input = str_replace([".", '$'], "", $input);
|
|
||||||
$input = str_replace(",", ".", $input);
|
|
||||||
if ($input == "") {
|
|
||||||
$input = "0.00";
|
|
||||||
}
|
|
||||||
|
|
||||||
return $input;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove bad characters from number strings.
|
|
||||||
*
|
|
||||||
* @param string $input
|
|
||||||
* String to sanitize.
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
* Sanitized string.
|
|
||||||
*/
|
|
||||||
public static function sanitizeNumber($input): string {
|
|
||||||
$input = str_replace(",", ".", $input);
|
|
||||||
if ($input == "") {
|
|
||||||
$input = "0";
|
|
||||||
}
|
|
||||||
|
|
||||||
return $input;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Multi dimension array search.
|
|
||||||
*
|
|
||||||
* @param string $needle
|
|
||||||
* The value being searched.
|
|
||||||
* @param array $haystack
|
|
||||||
* The array being searched.
|
|
||||||
* @param mixed $column
|
|
||||||
* The column being searched.
|
|
||||||
*
|
|
||||||
* @return mixed
|
|
||||||
* The index of the item, if found, or FALSE.
|
|
||||||
*/
|
|
||||||
public static function mdaSearch($needle, $haystack, $column): mixed {
|
|
||||||
$idx = '';
|
|
||||||
$found = FALSE;
|
|
||||||
|
|
||||||
foreach ($haystack as $key => $value) {
|
|
||||||
if (isset($value[$column]) && $value[$column] == $needle) {
|
|
||||||
$idx = $key;
|
|
||||||
$found = TRUE;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($found) {
|
|
||||||
return $idx;
|
|
||||||
}
|
|
||||||
|
|
||||||
return FALSE;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user