Skip to content

Instantly share code, notes, and snippets.

Neil McGann nmcgann

  • UK
Block or report user

Report or block nmcgann

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@nmcgann
nmcgann / pub-sub.js
Created Feb 10, 2017
Simple JS Pub-Sub implementation. Synchronous or Asynchronous.
View pub-sub.js
/*
* Simple Publish / subscribe events routine.
*
* Used to extend an object with the subscribe and publish methods.
* The return value of subscribe has a remove method that can be used
* to unsubscribe.
*
* Modifies the object directly.
*/
var $$ = $$ || {}; //This is the utility functions object used elsewhere.
@nmcgann
nmcgann / phpzip.php
Created May 7, 2017
PHP Command line zip utility. Simply zips a directory structure into an archive. No bells or whistles.
View phpzip.php
<?php
/*
* Command line zip equivalent - very simple version that just zips a directory structure completely.
* No options!
* Useful when it is needed to build e.g. a Wordpress plugin zip archive, but the dev environment
* doesn't have zip - i.e. MINGW64 as bundled with Git - has unzip, but not zip.
*/
date_default_timezone_set('UTC');
error_reporting (E_ALL);
@nmcgann
nmcgann / wp-db-find-and-replace.sh
Created Apr 13, 2017
Wordpress Database search and replace domain including serialized data.
View wp-db-find-and-replace.sh
#!/bin/bash
#wp-db-find-and-replace.sh
#
#Script to search and replace on wordpress db in .sql format e.g. to change domain url
#Works with serialized data as it recognises the php serialized format
#writes a file output
#Based on version of:
#http://wojnowski.net.pl/main/index/updating-wordpress-database-urls-part-2
#(this doesn't work as-is, but the principles are good once it is fixed)
@nmcgann
nmcgann / mysql_backup.class.php
Last active Mar 17, 2017
PHP Class to dump a Mysql/Maria database to a string. Includes views, triggers, procedures and functions. (Improved!)
View mysql_backup.class.php
<?php
/*
* PHP backup class. Based on an old script updated to use mysqli_ routines.
* Extended and bugfixed - now handles NULL's, doesn't quote numbers, handles utf-8,
* creates multiple inserts and breaks to a new one after a defined number, creates gzip
* or zip files, generates foriegn key disable and table lock/unlock statements.
* Handles Views, Triggers, Procedures and Functions.
* Optionally removes DEFINER from views/procs/funcs and DB names from views.
*
* Thanks to Phil Rodgers (https://github.com/placeposition) for spotting the field type
@nmcgann
nmcgann / get_host_url.php
Last active Mar 14, 2017
PHP function to automatically get a host url from $_SERVER variables to enable creation of absolute links etc. (Apache)
View get_host_url.php
<?php
//e.g.semi-colon separated define list of all allowable hosts (does not need to show sub-domains - works automatically)
//$valid_hosts = 'first-host.com;second-host.com';
//create absolute url for page address - quite tricky to make this work on linux / windows and shared hosting and also
//php built-in web server.
//Constructed URL ends up in a trailing slash with the $target parameter appended.
//defaults to not checking a valid host list unless one is supplied
function get_host_url($target = '', $valid_hosts = '*'){
// Get HTTP/HTTPS (the possible values for this vary from server to server)
@nmcgann
nmcgann / lightsail_install_memcache.sh
Created Mar 14, 2017
bash script to install memcache on AWS lightsail LAMP stack (Mar 2017)
View lightsail_install_memcache.sh
#!/bin/bash
#install memcache on AWS lightsail bitnami
#memcache (no "d") is handy for cross platform testing as it will work on windows
#WAMP etc. where the memcached version does not appear to have a windows port)
#(lightsail lamp has the build environment gcc etc. already installed)
#memcache version
VERSION='3.0.8'
cd /root/bitnami
@nmcgann
nmcgann / vultr_prov.sh
Created Mar 12, 2017
Provision and fix the Vultr LAMP stack (Centos 6). Adds PhpMyAdmin and fixes the broken .htaccess files due to fcgi config (as of March 2017)
View vultr_prov.sh
#!/bin/bash
#Provision the vultr Centos6 LAMP stack as it is a bit broken as standard
#update and fetch missing things
yum update -y
yum install -y php56u-bcmath php56u-imap php56u-intl php56u-pecl-memcache
#disable opcache for development
perl -pi -e 's/^opcache.enable=1/opcache.enable=0/' /etc/php.d/10-opcache.ini
@nmcgann
nmcgann / IMAP_access.php
Created Feb 15, 2017
IMAP access class to decode imap body, fetch and assemble all the parts and attachments. Used as an utility class for processing a whole mailbox.
View IMAP_access.php
<?php
/**
* IMAP_access. Utility class for processing a mailbox for applications that
* want to grab the body text and do something with it e.g. create a new
* todo list item. Enough clues in there that it can be expanded to do
* a lot more if required - e.g. also grab file attachments.
*
* Based on several part-working tutorials (fixed), the PHP manual examples
* and testing on real email samples from Gmail, Thunderbird and MS outlook 2010.
* (flatten_parts is the tricky bit and was from here:
@nmcgann
nmcgann / pdo_mysql_session.php
Last active Feb 10, 2017
PHP Mysql session handling class using PDO. Includes row locking on reads to prevent ajax race condition issues. Needs php >= 5.4.
View pdo_mysql_session.php
<?php
/**
* Mysql DB Session class with PDO
*
* Uses row locking to handle concurrent ajax access
*
* Requires a PDO db connection to be handed to the session constructor.
*
* Table:
*
@nmcgann
nmcgann / async-scriptloader.js
Created Feb 10, 2017
Simple Asynchronous JS script loader. Not clever or modern - just works.
View async-scriptloader.js
//js async loader
var $$ = $$ || {};
$$.asyncScriptLoader = function (url) {
//get currently loaded scripts
var allScripts = document.getElementsByTagName('script'),
alreadyLoaded = false;
function onLoad(){}; //url is parameter
function onError(){}; //url is parameter
You can’t perform that action at this time.