Skip to content

Instantly share code, notes, and snippets.

View CMCDragonkai's full-sized avatar
🚀
Lightspeed

CMCDragonkai

🚀
Lightspeed
View GitHub Profile
@CMCDragonkai
CMCDragonkai / NativeSession.php
Created October 10, 2013 03:44
PHP: Session Handler Abstraction. Requires 5.4.12 or higher.
<?php
/**
* NativeSession implements the native PHP session handler to handle the server side sessions for the currently
* authenticated user. When sessions are not available, such as when they are disabled, this class just fallsback
* onto memory implementation of $_SESSION, which will only be persisted for the lifetime of the PHP process or
* request/response. So this will work even when you're not using cookies and trying to be RESTful. This implementation
* will not work in PHP daemons such as Ratchet. This is because $_SESSION is a global variable for the entire process.
* Therefore all the session operations like login and logout would operate globally. There would need to be a more
* fine grained session implementation such as Symfony Sessions.
@CMCDragonkai
CMCDragonkai / Cookies.php
Created October 16, 2013 15:32
PHP: Cookie Manager Class
<?php
class Cookies{
protected $options;
//setup some configuration
public function __construct(array $options){
$this->options = $options;
@CMCDragonkai
CMCDragonkai / createConditionalIntervalFunction.js
Created October 27, 2013 18:13
JS: createConditionalIntervalFunction - Creates a function that repeats its own execution repeatedly or until a condition evaluates to true. Useful for asynchronously checking some condition before running something else.
var createConditionalIntervalFunction = function(condition, callback, interval, repeat = true){
var intervalFunc = function(){
setTimeout(function(){
//if repeat is true, this intervalFunc will be repeated continually
//if repeat is false, this intervalFunc will only be repeated until the condition() is true
if(condition()){
callback();
@CMCDragonkai
CMCDragonkai / migrate.php
Created November 8, 2013 21:25
PHP: Codeigniter Migrate Controller
<?php
class Migrate extends CI_Controller {
public function __construct(){
parent::__construct();
if(!$this->input->is_cli_request()){
exit;
@CMCDragonkai
CMCDragonkai / parseBooleanStyle.js
Created November 9, 2013 20:06
JS: Parse mixed type values into booleans. Like 'true' to true.
/**
* Parses mixed type values into booleans. This is the same function as filter_var in PHP using boolean validation
* @param {Mixed} value
* @param {Boolean} nullOnFailure = false
* @return {Boolean|Null}
*/
var parseBooleanStyle = function(value, nullOnFailure = false){
switch(value){
case true:
case 'true':
@CMCDragonkai
CMCDragonkai / wp-config.php
Last active November 5, 2017 03:30
Wordpress: Automatic Url and Content Dir/Url Detection for Wordpress Composer Installs. Add to the top of the wp-config.php script.
<?php
/**
* Automatic Url + Content Dir/Url Detection for Wordpress
*/
$document_root = rtrim(str_replace(array('/', '\\'), '/', $_SERVER['DOCUMENT_ROOT']), '/');
$root_dir = str_replace(array('/', '\\'), '/', __DIR__);
$wp_dir = str_replace(array('/', '\\'), '/', __DIR__ . '/wp');
$wp_content_dir = str_replace(array('/', '\\'), '/', __DIR__ . '/wp-content');
@CMCDragonkai
CMCDragonkai / Secrets.php
Last active July 14, 2019 07:33
PHP: Secrets Loader - Place Secrets.php in the root of the project. Loads files within ./secrets/*.php adapting $secrets[] to $_ENV['secrets'][].
<?php
/**
* Loads secrets into the application. Setup secrets via $secrets[], get secrets via $_ENV['secrets'][]
*/
class Secrets{
public static function load(){
$secrets_path = __DIR__ . '/secrets';
@CMCDragonkai
CMCDragonkai / UniqueFilename.php
Last active December 31, 2015 17:19
PHP: Unique Filenames using Gaufrette
<?php
//works for Gaufrette, but the same principle exists for other filesystem code
do{
$name = uniqid('folder', true);
if(!$this->filesystem->has($name)) break;
}while(true);
@CMCDragonkai
CMCDragonkai / AsyncAnchor.Directive.js
Last active December 31, 2015 22:49
JS: AngularJS Asynchronous Anchor Scroll - There are 3 ways of doing hash scrolling in AngularJS, put a hash into the URL via links, put a hash onto the URL via the $location API, or using $anchorScroll. All three ways rely on the URL having the hash component. The problem comes when the resource you want to scroll is loaded asynchronously, that…
app.directive('asyncAnchor', [
'$location',
'$anchorScroll',
'$timeout',
function($location, $anchorScroll, $timeout){
return {
link: function(scope, element, attributes){
var id = attributes.asyncAnchor || attributes.id || attributes.name;
var delay = attributes.asyncAnchorDelay;
@CMCDragonkai
CMCDragonkai / MY_Security.php
Created January 6, 2014 07:07
PHP: Codeigniter CSRF functionality does not support putting the CSRF token in the HTTP headers for the purposes of the double submit cookie method. It also only runs the CSRF check on POST and not on PUT or DELETE. This drop in MY_Security.php makes sure CSRF runs on POST, PUT or DELETE and checks the HTTP headers for X-XSRF-TOKEN recommended b…
<?php
class MY_Security extends CI_Security{
//overriding the normal csrf_verify, this gets automatically called in the Input library's constructor
//verifying on POST and PUT and DELETE
public function csrf_verify(){
$request_method = strtoupper($_SERVER['REQUEST_METHOD']);