Skip to content

Instantly share code, notes, and snippets.

@lcherone
lcherone / c9php7.sh
Created December 24, 2017 08:09
Cloud9 upgrade PHP 5 to 7
#!/bin/bash
# add ppa and update
sudo add-apt-repository ppa:ondrej/php -y
sudo apt-get update
# install deps
sudo apt-get install -y php7.0-{dev,curl,gd,intl,mcrypt,json,mysql,opcache,bcmath,mbstring,soap,xml}
sudo apt-get install -y libapache2-mod-php7.0
@lcherone
lcherone / recursive_menu_function.md
Created December 24, 2017 05:20
nested recursive menu function
@lcherone
lcherone / hash.php
Created December 14, 2017 19:44
Basic encode an int into a short hash.
<?php
/**
* Encode an int into a short hash.
*
* @param int $id
* @param string|int $seed - Lock alphabet shuffle, or leave blank for random
* @return string
*/
function id_encode(int $id, $seed = null) {
$alphabet = str_split('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890');
@lcherone
lcherone / bug.php
Last active December 13, 2017 11:31
Addition precedence post increment bug.
<?php
$a = 0; echo $a+$a+$a++; //0
$a = 1; echo $a+$a+$a++; //3
$a = 2; echo $a+$a+$a++; //6
$a = 3; echo $a+$a+$a++; //9
echo PHP_EOL;
$a = 0; echo $a+$a++; //1 <-- should be 0
$a = 1; echo $a+$a++; //3 <-- should be 2
@lcherone
lcherone / guidv4.php
Last active December 7, 2017 19:30
guidv4 with random_bytes()
<?php
function guidv4()
{
if (function_exists('random_bytes') === true) {
$bytes = random_bytes(16);
} elseif (function_exists('openssl_random_pseudo_bytes') === true) {
$bytes = openssl_random_pseudo_bytes(16);
} elseif (function_exists('mcrypt_create_iv') === true) {
$bytes = mcrypt_create_iv(16, MCRYPT_DEV_URANDOM);
} elseif (function_exists('com_create_guid') === true) {
@lcherone
lcherone / code.php
Last active November 22, 2017 13:19
Asymmetric encryption using PHP (the alice and bob story)
<?php
// define an example, our people, messages and their keys
$people = [
'alice' => [
'keys' => gen_keys(),
'msg' => 'Hi Bob, I\'m sending you a private message'
],
'bob' => [
'keys' => gen_keys(),
@lcherone
lcherone / c9-lamp-prep.sh
Created October 10, 2017 17:25
c9-lamp-prep.sh
#!/bin/bash
#
# Cloud9 Prep & LAMP Server
# Apache2 (rewrite, headers enabled), PHP7, MariaDB, Git, Composer, nodejs & npm
#
# Set envioroment, for cloud-init
set -e
export DEBIAN_FRONTEND=noninteractive
@lcherone
lcherone / Fibonacci.php
Created October 1, 2017 18:07
Fibonacci sequence generator class, a play with PHP7 features.
<?php
declare(strict_types=1);
/**
* Fibonacci sequence generator, a play with PHP7 features.
* @author Lawrence Cherone <cherone.co.uk>
*/
class Fibonacci {
private $sequence = [];
@lcherone
lcherone / xor.php
Last active October 1, 2017 12:24
xor
<?php
$xor = function ($str, $key = 192) {
for ($i = 0; $i < strlen($str); $i++) {
$str[$i] = chr(ord($str[$i]) ^ $key);
}
return $str;
};
$enc = $xor("xorme");
$dec = $xor($enc);
@lcherone
lcherone / phone-codes.txt
Last active September 23, 2017 10:51
Mobile *# hidden device codes
Android
*#06# - IMEI
*#0*# - Sensor Tests
*#9900# - SysDump
*#0808# - USBSettings
*#0011# - ServiceMode
*#0228# - BatteryStatus
*#1234# - Version
*#2663# - Firmware version