Espacio Daycry - Espacio de programación
miércoles, 23 de marzo de 2022
Como enviar passwords de forma segura
El hecho de enviar passwords es un tema bastante delicado, porque ya sea por email, por whatsapp o cualquier otra herramienta de comunicación siempre va a dejar un rastro del contenido.
Read More
Pues bien, se ha desarrollado una nueva herramienta para poder compartir contenido delicado y poderlo visualizar una o varias veces, dependiendo de la configuración que hayamos confirmado.
Por defecto un usuario no registrado tendrá mas limitaciones que un usuario registrado y estas limitaciones son:
- Tiempo de duración es un máximo de 7 días, en cambio, un usuario registrado tendrá hasta 14 días de duración.
- Sólo podrá visualizar el secreto 1 vez, en cambio, un usuario registrado podrá visualizar el secreto tantas veces como quiera.
- Un usuario registrado podrá utilizar el servicio API, para poder integrarlo con otras aplicaciones.
Esta nueva herramienta es:
https://shareyoursecret.es/
sábado, 21 de noviembre de 2020
PHP Basic Auth
function require_auth() {
$AUTH_USER = 'admin';
$AUTH_PASS = 'admin';
header('Cache-Control: no-cache, must-revalidate, max-age=0');
$has_supplied_credentials = !(empty($_SERVER['PHP_AUTH_USER']) && empty($_SERVER['PHP_AUTH_PW']));
$is_not_authenticated = (
!$has_supplied_credentials ||
$_SERVER['PHP_AUTH_USER'] != $AUTH_USER ||
$_SERVER['PHP_AUTH_PW'] != $AUTH_PASS
);
if ($is_not_authenticated) {
header('HTTP/1.1 401 Authorization Required');
header('WWW-Authenticate: Basic realm="Access denied"');
exit;
}
}
jueves, 16 de abril de 2020
API Zoom for Codeigniter 4
No hay comentarios
:
Posted by
daycry
at
11:46
Labels:
api
,
Codeigniter
,
codeigniter 4
,
meetings
,
web
,
zoom
Zoom
Zoom API for Codeigniter 4
Installation via composer
Use the package with composer install
> composer require daycry/zoom
Manual installation
Download this repo and then enable it by editing app/Config/Autoload.php and adding the Daycry\Zoom namespace to the $psr4 array. For example, if you copied it into app/ThirdParty:
$psr4 = [
'Config' => APPPATH . 'Config',
APP_NAMESPACE => APPPATH,
'App' => APPPATH,
'Daycry\Zoom' => APPPATH .'ThirdParty/zoom/src',
];
Configuration
Run command:
> php spark zoom:publish
This command will copy a config file to your app namespace.
Usage Loading Library
$zoom = new \Daycry\Zoom\Zoom();
Usage as a Service
$zoom = \Config\Services::zoom();
Usage as a Helper
In your BaseController - $helpers array, add an element with your helper filename.
protected $helpers = [ 'zoom_helper' ];
And then, you can use the helper
$zoom = zoom_instance();
Authentication
/**
*
* @return AccessTokenInterface
*/
$zoom = new \Daycry\Zoom\Zoom();
$token = $zoom->authentication();
echo "<pre>";
echo json_encode( $token );
echo "</pre>";
Request
/**
* Returns an authenticated PSR-7 request instance.
*
* @param string $method
* @param string $url
* @return RequestInterface
*/
$zoom = new \Daycry\Zoom\Zoom();
$zoom->setAccessToken( $token );
$reponse = $zoom->request( 'GET', 'users' );
echo "<pre>";
var_dump( $reponse );
echo "</pre>";
You can pass extra parametres into the request method.
/**
* Returns an authenticated PSR-7 request instance.
*
* @param string $method
* @param string $url
* @param array $options Any of "headers", "body", and "protocolVersion".
* @param AccessTokenInterface|string $token
* @return RequestInterface
*/
$zoom = new \Daycry\Zoom\Zoom();
$zoom->setAccessToken( $token );
$reponse = $zoom->request( 'GET', 'users', [], $token );
echo "<pre>";
var_dump( $reponse );
echo "</pre>";
Refresh Token
/**
*
* @return AccessTokenInterface
*/
$zoom = new \Daycry\Zoom\Zoom();
$zoom->setAccessToken( $token );
$reponse = $zoom->refreshAccessToken();
echo "<pre>";
var_dump( $reponse );
echo "</pre>";
Example Token to save in your database
{"token_type":"bearer","scope":"dashboard_crc:read:admin","access_token":"xxxxx","refresh_token":"xxxxxx","expires":1586716974}
Sample Code
Install Twig on Codeigniter 4
No hay comentarios
:
Posted by
daycry
at
11:38
Labels:
Codeigniter
,
codeigniter 4
,
HTML
,
templates
,
twig
Twig
Twig for Codeigniter 4
Installation via composer
Use the package with composer install
> composer require daycry/twig
Manual installation
Download this repo and then enable it by editing app/Config/Autoload.php and adding the Daycry\Twig namespace to the $psr4 array. For example, if you copied it into app/ThirdParty:
$psr4 = [
'Config' => APPPATH . 'Config',
APP_NAMESPACE => APPPATH,
'App' => APPPATH,
'Daycry\Twig' => APPPATH .'ThirdParty/twig/src',
];
Configuration
Run command:
> php spark twig:publish
This command will copy a config file to your app namespace. Then you can adjust it to your needs. By default file will be present in
app/Config/Twig.php
.Usage Loading Library
$twig = new \Daycry\Twig\Twig();
$twig->display( 'file.html', [] );
Usage as a Service
$twig = \Config\Services::twig();
$twig->display( 'file.html', [] );
Usage as a Helper
In your BaseController - $helpers array, add an element with your helper filename.
protected $helpers = [ 'twig_helper' ];
And then you can use the helper
$twig = twig_instance();
$twig->display( 'file.html', [] );
Add Globals
$twig = new \Daycry\Twig\Twig();
$session = \Config\Services::session();
$session->set( array( 'name' => 'Daycry' ) );
$twig->addGlobal( 'session', $session );
$twig->display( 'file.html', [] );
File Example
<!DOCTYPE html>
<html lang="es">
<head>
<title>Example</title>
<meta charset="UTF-8">
<meta name="title" content="Example">
<meta name="description" content="Example">
</head>
<body>
<h1>Hi {{ name }}</h1>
{{ dump( session.get( 'name' ) ) }}
</body>
</html>
How Run Tests
vendor\bin\phpunit vendor\daycry\twig\tests
domingo, 26 de mayo de 2019
DateTime PHP
When I search and read code about time and date problems developers have, I noticed that a lot of people still use the old PHP functions like
date()
, time()
or strtotime()
.
What about using DateTime instead?
It’s time to introduce this powerful PHP object.
DateTime
can do all the usual date and time operations you could ask for and even more. Using DateTime
can save a lot of time when you have to do more complex operations on dates.
Now I see you asking: why using DateTime instead of a bunch of PHP date functions?
Here’s why:
- The object
DateTime
is definitely more robust to use. Less bug is always better. - DateTime is an object. You can use composition or inheritance to modify his behavior easily. You need always the same date as input for your unit tests? Use your own dummy
datetime
object which always return the birthday of your dog! - Using one object is more clear for your fellow developer colleagues. Even if they don’t know
DateTime
, they just have to look to the documentation for one single object. It is definitely better than looking for the documentations of three different functions!
DateTime instantiation and formatting
The instantiation is easy: you can pass to the constructor a date correctly formatted, or nothing if you want to use the actual date and time.
Here the list of supported date and time formats you can inject in the constructor.
You can then format the date in order to display it, in two lines of code:
$dateTime = new DateTime('2016-01-01');
echo $dateTime->format('Y-m-d H:i:s');`
Output:
2016-01-01 00:00:00
$dateTime = new DateTime();
echo $dateTime->format('Y-m-d H:i:s');`
Output: whatever the current date is, correctly formatted.
Easy, isn’t it? You can as well precise the timezone you want as a second argument:
$date = new DateTime('2016-05-20', new DateTimeZone('Europe/Berlin'));
DateTime and timestamps
You want to format an unreadable timestamp into a nice and shiny easy-to-read date?
$dateTime = new DateTime();
$dateTime->setTimestamp(1271802325);
echo $dateTime->format('Y-m-d H:i:s');
Output:
2010-04-20 22:25:25
Obviously you can as well output a timestamp if you need to:
$dateTime = new DateTime('2016-05-20');
echo $dateTime->getTimestamp();
Output:
1271802325
Adding or subtracting a chunk of time
What about adding or retrieving a day, a minute, an hour of a date? You only need to use the proper formatting:
$dateTime = new DateTime('2016-01-01');
$dateTime->modify('+1 day');
echo $dateTime->format('Y-m-d H:i:s');`
Output:
2016-01-02 00:00:00
You can as well use the constructor if you work on the current date:
$dateTime = new DateTime('+1d');
echo $dateTime->format('Y-m-d H:i:s');`
Output the current date plus one day.
For the same result you could as well instantiate a DateInterval object but you would have to use an interval specification which is way harder to read.
I never use this last solution personally, simply because I never need to.
Difference between two dates
DateTime
objects can as well be used to compare dates. It’s where DateTime
can be really, really handy.$datetime1 = new DateTime('2009-10-11 12:12:00');
$datetime2 = new DateTime('2009-10-13 10:12:00');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%Y-%m-%d %H:%i:%s');
Output:
00-0-1 22:0:0
In this example we created two
DateTime
objects. They will receive two different dates in their constructors.
In order to compare those two dates we use the method
diff()
of the first DateTime
object with the second DateTime
object as argument.
The
diff()
method will return a new object of type DateInterval
. If you want to format the difference between the two dates, you can use as well the format()
method of the DateInterval
object but be careful: it won’t accept the same formatting as the DateTime::format()
method.
The output indicate that there is 1 day and 22 hours difference between the two dates.
If you only want the seconds, hours or day of interval you can write:
$datetime1 = new DateTime('2009-10-11 12:12:00');
$datetime2 = new DateTime('2009-10-13 10:12:00');
$interval = $datetime1->diff($datetime2);
echo $interval->s;
echo $interval->h;
echo $interval->d;
Output:
0
22
1
Compare DateTime objects
If you need to compare two DateTimes, it’s as simple as that:
<?php
$datetime1 = new DateTime('2009-10-11 12:12:00');
$datetime2 = new DateTime('2009-10-13 10:12:00');
if ($datetime1 > $datetime2) {
echo 'datetime1 greater than datetime2';
}
if ($datetime1 < $datetime2) {
echo 'datetime1 lesser than datetime2';
}
if ($datetime1 == $datetime2) {
echo 'datetime2 is equal than datetime1';
}
Output:
Original font: https://thevaluable.dev/php-datetime-create-compare-format/
datetime1 lesser than datetime2
Suscribirse a:
Entradas
(
Atom
)
Sígueme en las Redes Sociales
Donaciones
Datos personales
Entradas populares
-
En este apartado vamos a explicar como ejercutar archivos PHP a través del terminal de Ubuntu. Lo primero que tendríamos que hacer es inst...
-
En este blog voy a comentar un tema que se utilizan en casi todas las páginas web que existen, y es el tema de la paginación. La paginaci...
-
Este post trata de la integración de la librería PHPExcel en Codeigniter, aunque se podría aplicar a cualquier librería, como por ejemplo mP...
-
Ejemplo para añadir o sumar un número determinado de hora/s, minuto/s, segundo/s a una fecha en php. Con la función strtotime se puede ...
-
Este tema es uno de los temas primordiales sobre el framework Codeigniter, ya que en alguna ocación nos hemos visto obligados a recoger dato...
© Espacio Daycry - Espacio de programación 2013 . Powered by Bootstrap , Blogger templates and RWD Testing Tool