2022-01-20 09:58:26 +01:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* Este archivo es parte de Nabu.
|
|
|
|
*
|
|
|
|
* Nabu es software libre: puedes redistribuirlo y/o modificarlo
|
|
|
|
* bajo los términos de la Licencia Pública General de GNU Affero publicada por
|
|
|
|
* la Free Software Foundation, ya sea la versión 3 de la Licencia, o
|
|
|
|
* (a su elección) cualquier versión posterior.
|
|
|
|
*
|
|
|
|
* Nabu se distribuye con la esperanza de que sea de utilidad,
|
|
|
|
* pero SIN NINGUNA GARANTÍA; incluso sin la garantía implícita de
|
|
|
|
* COMERCIABILIDAD o APTITUD PARA UN PROPÓSITO PARTICULAR. Consulte la
|
|
|
|
* Licencia Pública General de GNU Affero para obtener más detalles.
|
|
|
|
*
|
|
|
|
* Debería haber recibido una copia de la Licencia Pública General de GNU Affero
|
|
|
|
* junto con este programa. De lo contrario, consulte <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
defined('NABU') || exit();
|
|
|
|
|
|
|
|
// Colección de herramientas propias para Nabu.
|
|
|
|
class utils {
|
|
|
|
// Redirecciona a una ruta y termina la ejecución de todos los scripts de PHP.
|
|
|
|
static public function redirect(string $route) {
|
|
|
|
header('Location: ' . $route);
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Redirecciona a una ruta si existe una sesión de usuario.
|
|
|
|
static public function session_exists(string $route) {
|
|
|
|
if (isset($_SESSION['user']))
|
|
|
|
self::redirect($route);
|
|
|
|
}
|
|
|
|
|
|
|
|
// @return un string escapado para HTML.
|
|
|
|
static public function escape(string $str) {
|
|
|
|
return htmlentities($str, ENT_COMPAT | ENT_HTML5, 'UTF-8');
|
|
|
|
}
|
|
|
|
|
|
|
|
// @return la fecha actual.
|
|
|
|
static public function current_date() {
|
|
|
|
return date('Y-m-d H:i:s');
|
|
|
|
}
|
2022-01-20 11:04:57 +01:00
|
|
|
|
|
|
|
// Redirecciona a una ruta si no existe una sesión de usuario.
|
2022-01-20 12:01:25 +01:00
|
|
|
static public function check_session(string $route) {
|
2022-01-20 11:04:57 +01:00
|
|
|
if (empty($_SESSION['user']))
|
|
|
|
self::redirect($route);
|
|
|
|
}
|
2022-01-20 12:01:25 +01:00
|
|
|
|
|
|
|
// Genera la URL de un artículo.
|
|
|
|
public static function url_slug(string $title) {
|
|
|
|
return date('Ymd') . '-' . preg_replace('/[^A-Za-z0-9-]+/', '-', strtolower($title));
|
|
|
|
}
|
2022-01-22 23:56:34 +01:00
|
|
|
|
|
|
|
// @return la URL completa de una imagen.
|
|
|
|
public static function url_image(string $category, $filename) {
|
|
|
|
$url = NABU_DEFAULT[$category];
|
|
|
|
|
|
|
|
if (!empty($filename))
|
2022-01-24 08:27:56 +01:00
|
|
|
if (file_exists(NABU_DIRECTORY['storage-' . $category . 's'] . '/' . $filename))
|
2022-01-22 23:56:34 +01:00
|
|
|
$url = NABU_DIRECTORY[$category . 's'] . '/' . $filename;
|
|
|
|
|
|
|
|
return $url;
|
|
|
|
}
|
2022-01-24 08:27:56 +01:00
|
|
|
|
2022-01-24 08:53:02 +01:00
|
|
|
// Elimina una imagen.
|
|
|
|
public static function remove_image(string $category, $filename) {
|
|
|
|
if (!empty($filename)) {
|
|
|
|
$path = NABU_DIRECTORY['storage-' . $category . 's'] . '/' . $filename;
|
|
|
|
|
|
|
|
if (file_exists($path))
|
|
|
|
unlink($path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-24 08:27:56 +01:00
|
|
|
// @return el nombre de una imagen y reemplaza una imagen por otra.
|
|
|
|
public static function update_image(string $model, string $category, $original, array $replacement) {
|
|
|
|
$extension = explode('/', $replacement['type'])[1];
|
|
|
|
|
|
|
|
if ($extension == 'svg+xml')
|
|
|
|
$extension = 'svg';
|
|
|
|
|
|
|
|
require_once 'models/' . $model . '.php';
|
|
|
|
|
|
|
|
$Model = new $model();
|
|
|
|
|
|
|
|
// Valida si el nombre de la imagen es único.
|
|
|
|
do
|
|
|
|
$filename = bin2hex(random_bytes(32)) . '.' . $extension;
|
|
|
|
while(!empty($Model -> find_image($category, $filename)));
|
|
|
|
|
|
|
|
unset($Model);
|
|
|
|
|
2022-01-24 08:53:02 +01:00
|
|
|
$destination = NABU_DIRECTORY['storage-' . $category . 's'] . '/' . $filename;
|
2022-01-24 08:27:56 +01:00
|
|
|
|
2022-01-24 22:28:48 +01:00
|
|
|
// Mueve la imagen subida a la carpeta de almacenamiento.
|
2022-01-24 08:27:56 +01:00
|
|
|
if (move_uploaded_file($replacement['tmp_name'], $destination)) {
|
2022-01-24 08:53:02 +01:00
|
|
|
self::remove_image($category, $original);
|
2022-01-24 08:27:56 +01:00
|
|
|
|
|
|
|
return $filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2022-01-31 03:58:17 +01:00
|
|
|
|
|
|
|
// @return un array asociativo con la gestión de páginas de resultados.
|
|
|
|
public static function pagination(int $total, int $limit, string $route) {
|
|
|
|
// Página de resultado inicial.
|
|
|
|
$page = 1;
|
|
|
|
|
|
|
|
// Calcula el número total de páginas de resultados.
|
|
|
|
$results = ceil($total/$limit);
|
|
|
|
|
|
|
|
if (isset($_GET['page']) && is_numeric($_GET['page']))
|
|
|
|
$page = $_GET['page'];
|
|
|
|
|
|
|
|
if ($page < 1)
|
|
|
|
self::redirect($route);
|
|
|
|
|
|
|
|
if ($results < 1)
|
|
|
|
$results = 1;
|
|
|
|
|
|
|
|
if ($page > $results)
|
|
|
|
self::redirect($route . '&page=' . $results);
|
|
|
|
|
|
|
|
// Calcula el número de resultados acumulados por paginación.
|
|
|
|
$pagination = array(
|
|
|
|
'page' => $page,
|
|
|
|
'accumulation' => ($page - 1) * $limit
|
|
|
|
);
|
|
|
|
|
|
|
|
return $pagination;
|
|
|
|
}
|
2022-02-16 17:40:38 +01:00
|
|
|
|
|
|
|
// @return un string de búsqueda validado.
|
|
|
|
public static function validate_search(string $view, int $max) {
|
2022-02-16 17:46:30 +01:00
|
|
|
$search = array('query' => '', 'view' => $view);
|
2022-02-16 17:40:38 +01:00
|
|
|
|
|
|
|
// Selecciona si se realiza una búsqueda por el método POST o GET.
|
|
|
|
if (!empty($_POST['q'])) {
|
|
|
|
csrf::validate($_POST['csrf']);
|
|
|
|
|
|
|
|
$form = $_POST;
|
|
|
|
}
|
|
|
|
elseif (!empty($_GET['q']))
|
|
|
|
$form = $_GET;
|
|
|
|
|
|
|
|
if (!empty($form)) {
|
|
|
|
$validations = new validations($view);
|
|
|
|
|
|
|
|
// Valida el string de búsqueda.
|
|
|
|
$data = $validations -> validate($form, array(
|
|
|
|
array('field' => 'q', 'trim_all' => true, 'min_length' => 0, 'max_length' => $max)
|
|
|
|
));
|
|
|
|
|
|
|
|
$search['query'] = $data['q'];
|
2022-02-16 17:46:30 +01:00
|
|
|
$search['view'] = $view . '&q=' . urlencode($search['query']);
|
2022-02-16 17:40:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Redirecciona a una búsqueda por el método GET si se realiza una búsqueda por el método POST.
|
|
|
|
if (!empty($_POST['q']))
|
2022-02-16 17:46:30 +01:00
|
|
|
utils::redirect($search['view']);
|
2022-02-16 17:40:38 +01:00
|
|
|
|
|
|
|
return $search;
|
|
|
|
}
|
2022-01-20 09:58:26 +01:00
|
|
|
}
|