Skip to content

Instantly share code, notes, and snippets.

View alphp's full-sized avatar

Fernando Herrero alphp

View GitHub Profile
@alphp
alphp / AppController.php
Last active February 16, 2018 07:15
Function for list all Controllers and Methods for CakePHP 2.x
<?php
App::uses('Controller', 'Controller');
class AppController extends Controller {
protected function get_controllers () {
$controllers = null;
$app_controller = get_class_methods('AppController');
$controller_classes = App::objects('controller');
foreach ($controller_classes as $controller_class) {
@alphp
alphp / SetColor.php
Created February 23, 2018 22:48
New implementation of SetDrawColor, SetFillColor and SetTextColor for FPDF
<?php
function SetDrawColor() {
$args = func_get_args();
if (count($args) and is_array($args[0])) {
$args = $args[0];
}
switch (count($args)) {
case 1:
$this->DrawColor = sprintf('%.3f G', $args[0] / 100);
@alphp
alphp / Environment.psm1
Created August 27, 2018 04:05
PowerShell module for Get/Set/Remove environment variables in Windows
function Get-Environment {
param (
[parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()]$Name,
[String] $DefaultValue = $null,
[ValidateSet("None", "DoNotExpandEnvironmentNames")] [String] $Options = "None"
)
([Microsoft.Win32.Registry]::LocalMachine.OpenSubKey("SYSTEM\CurrentControlSet\Control\Session Manager\Environment", $true)).GetValue($Name, $DefaultValue, $Options)
}
@alphp
alphp / path_remove.php
Last active August 27, 2018 04:12
Remove directory (PHP)
<?php
function rd ($path) {
if (is_file($path)) {
unlink($path);
} else {
array_map('rd', glob($path . DIRECTORY_SEPARATOR . '*'));
rmdir($path);
}
}
@alphp
alphp / path_check.php
Last active August 27, 2018 04:12
Check if path exists (PHP)
<?php
function path_check ($path, $mode = null) {
if (!is_dir($path)) {
if (!empty($mode)) {
if (is_file($path)) {
unlink($path);
}
return mkdir($path, $mode, true);
}
return false;
@alphp
alphp / path_rename.php
Last active August 27, 2018 04:12
Rename a path (PHP)
<?php
require('path_check.php');
function path_rename ($from, $to) {
if (is_dir($from)) {
path_check($to, 0777);
foreach (glob($from . DIRECTORY_SEPARATOR . '*') as $item) {
path_rename($item, $to . DIRECTORY_SEPARATOR . basename($item));
}
rmdir($from);
@alphp
alphp / nvram-dump.php
Created August 28, 2018 10:47
DD-WRT nvram dump
<?php
class nvram {
public static function dump (string $nvramfile) {
$nvram = file_get_contents($nvramfile);
$header = unpack('a6header/v1record_count', $nvram);
if ($header['header'] !== 'DD-WRT') {
die('nvram format is not compatible);
}
$record_count = $header['record_count'];
@alphp
alphp / ntp.php
Created January 9, 2019 21:45
Query NTP time in PHP
<?php
function ntp_time ($host) {
$msg = hex2bin('e30004fa000100000001000000000000000000000000000000000000000000000000000000000000');
$data = false;
if ($sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)) {
if (@socket_connect($sock, $host, 123)) {
list($xmt['time2'], $xmt['time1']) = explode(' ', microtime());
$xmt['time1'] += 2208988800;
$xmt['time2'] *= 4294967296;
$msg .= pack('N*', $xmt['time1'], $xmt['time2']);
<?php
ini_set('date.timezone', 'Europe/Madrid');
define('HT', "\x09"); // \x09 \t Horizontal Tab
define('LF', "\x0A"); // \x0A \n Line feed
// Calculamos el inicio del DST: último domingo de marzo (2015-03-29 00:00:00 para 2015).
$date10 = strtotime('last sunday of march');
echo strftime('$date10 = strtotime("last sunday of march"); => %Y-%m-%d %H:%M:%S', $date10), LF;
// Añadimos un día a $date10 de la manera correcta (2015-03-30 00:00:00 para 2015).
@alphp
alphp / ServiceShell.php
Created March 5, 2019 20:29
CakePHP 3 service example
<?php
namespace App\Shell;
if (!extension_loaded('win32service')) dl('php_win32service.dll');
use Cake\Core\Configure;
use Cake\Console\Shell;
use Cake\Log\Log;
Log::drop('debug');