Skip to content

Instantly share code, notes, and snippets.

View ezimuel's full-sized avatar
🇮🇹
Working remotely, since 2008

Enrico Zimuel ezimuel

🇮🇹
Working remotely, since 2008
View GitHub Profile
@ezimuel
ezimuel / sign.sh
Created March 14, 2016 15:50
Sign and verify a file using OpenSSL command line tool. It exports the digital signature in Base64 format.
#!/bin/bash
# Sign a file with a private key using OpenSSL
# Encode the signature in Base64 format
#
# Usage: sign <file> <private_key>
#
# NOTE: to generate a public/private key use the following commands:
#
# openssl genrsa -aes128 -passout pass:<passphrase> -out private.pem 2048
# openssl rsa -in private.pem -passin pass:<passphrase> -pubout -out public.pem
@ezimuel
ezimuel / static.php
Created July 17, 2018 16:30
Swoole HTTP static file server example
<?php
// Simple HTTP static file server using Swoole
$host = $argv[1] ?? '127.0.0.1';
$port = $argv[2] ?? 9501;
$http = new swoole_http_server($host, $port);
// The usage of enable_static_handler seems to produce errors
// $http->set([
@ezimuel
ezimuel / decrypt.php
Created April 26, 2018 13:07
Decrypt a file in PHP form an encrypted file with OpenSSL CLI
<?php
/**
* Decrypt a file generated with the command line:
* openssl enc -aes-256-cbc -in file-to-encrypt -out encrypted-file -k password
*
* To decrypt:
* php decrypt.php encrypted-file password decrypted-file
*
* NOTE: this script has been tested with OpenSSL v.1.1, for old version
* please check if you need to use MD5 instead of SHA256 in EVP_BytesToKey()
@ezimuel
ezimuel / install.sh
Created April 19, 2016 11:59
Verify and install composer from bash command line
#!/bin/bash
# Verify and install composer from https://getcomposer.org/installer
me=`basename "$0"`
if [[ $# -eq 0 ]] ; then
echo "Usage: $me <hash>"
echo 'where <hash> is the hash value of the installer to verify'
exit 1
fi
@ezimuel
ezimuel / gist:9135151
Created February 21, 2014 14:24
Tesing SimpleXML and DOMDocument to prevent XXE attacks on XML
<?php
// The libxml entity loader is disabled by default
// even setting the libxml_disable_entity_loader to false doesn't works!
//
// @see http://uk3.php.net/manual/en/function.libxml-disable-entity-loader.php
// @see http://stackoverflow.com/a/10213239
$dir = __DIR__;
$content = 'This is a remote content!';
file_put_contents('content.txt', $content);
@ezimuel
ezimuel / benchmark.md
Created September 23, 2020 11:50
Benchmarking PHP 7.4.10 vs. PHP 8.0.0 beta4

Benchmarking PHP 7.4.10 vs. PHP 8.0.0 beta4

I executed the Zend\bench.php file using PHP 7.4.10 and PHP 8 beta4.

I used a CPU Intel i9-8950HK at 2.90GHz with 32GB RAM running Ubuntu 18.04.4 LTS.

I compiled PHP 8 beta4 using the official php.net source and installed in /opt/php/php8. I used the following options:

./configure --prefix=/opt/php/php8 --enable-opcache --with-zlib --enable-zip --enable-json --enable-sockets --without-pear
@ezimuel
ezimuel / benchmark.md
Created July 15, 2020 09:29
Benchmarking PHP 8 alpha2

Benchmarking PHP 7.4.7 vs. PHP 8 alpha2

I executed the Zend\bench.php file using PHP 7.4.7 and PHP 8 alpha2.

I used a CPU Intel i9-8950HK at 2.90GHz with 32GB RAM running Ubuntu 18.04.4 LTS.

I compiled PHP 8 alpha2 using the official php.net source and installed in /opt/php/php8. I used the following configure options:

./configure --prefix=/opt/php/php8 --enable-opcache --with-zlib --enable-zip --enable-json --enable-sockets --without-pear
@ezimuel
ezimuel / api.php
Last active June 12, 2020 15:11
Simple API for overriding functions and method of classes
<?php
// APIs for overriding functions and methods of classes
override_class_function(string $class, string $method, Closure $callback);
override_function(string $function, Closure $callback);
call_orginal_function(array $params);
// Example of usage override_function()
override_function('curl_exec', function(){
$params = func_get_args();
echo "--- BEFORE ---";
@ezimuel
ezimuel / server.php
Created May 31, 2018 14:11
Simple Hello World! HTTP server in PHP with Swoole
<?php
$http = new swoole_http_server("127.0.0.1", 9501);
$http->on("start", function ($server) {
var_dump($server);
echo "Swoole http server is started at http://127.0.0.1:9501\n";
});
$http->on("request", function ($request, $response) {
var_dump($request);
@ezimuel
ezimuel / bench_PHP7_results.md
Last active September 17, 2019 12:10
Benchmarking execution times of PHP from 7.1 to 7.4

Benchmarking execution time of PHP from 7.1 to 7.4

In this experiment, I tested the execution time of the script Zend\bench.php available in the PHP socurce code. I tested the latest PHP versions available, as follows:

  • PHP 7.1.32
  • PHP 7.2.22
  • PHP 7.3.9
  • PHP 7.4RC1