Skip to content

Instantly share code, notes, and snippets.

@mrclay
mrclay / crude_autoloading.php
Last active February 2, 2016 19:27
Crude, but tiny single-library PHP autoloading setups
<?php
// In the snippets below, you must replace '/path/to/lib' with the correct path.
// does not support namespaces with underscores
spl_autoload_register(function ($class) {
$file = __DIR__ . '/path/to/lib/' . strtr(ltrim($class, '\\'), '_\\', '//') . '.php';
is_readable($file) && (require $file);
});
// for a particular namespace
@mrclay
mrclay / .htaccess
Created March 7, 2012 15:01
Remove PHP extension from URLs
<IfModule mod_rewrite.c>
RewriteEngine on
# Redirect /index.php (and /index) to /
RewriteCond %{THE_REQUEST} ^GET\ /(.*/)?index(\.php)?(\?.*)?\ HTTP/
RewriteRule ^ http://%{HTTP_HOST}/%1 [L,R=301]
# Redirect /foo.php to /foo
RewriteCond %{THE_REQUEST} ^GET\ /(.*)\.php(\?.*)?\ HTTP/
RewriteRule ^ http://%{HTTP_HOST}/%1 [L,R=301]
@mrclay
mrclay / .htaccess
Created March 9, 2012 00:35
Apache rules to deny any active scripts (for an uploads directory)
<FilesMatch "\.(php[3456]?|pl|py|jsp|asp|html|htm|shtml|sh|cgi)$">
Deny from all
</FilesMatch>
@mrclay
mrclay / loadScript.js
Created March 9, 2012 00:57
Load JS scripts asynchronously w/ callback
function loadScript(src, callback) {
var s = document.createElement("script"),
onEvent = ('onreadystatechange' in s) ? 'onreadystatechange' : 'onload';
s[onEvent] = function () {
if (("loaded,complete").indexOf(this.readyState || "loaded") > -1) {
s[onEvent] = null;
if (callback) {
callback();
}
s.parentNode.removeChild(s);
@mrclay
mrclay / tinymce-export.js
Created March 20, 2012 20:36
Two row TinyMCE configuration for use with Elgg-ufcoe_tinymce
{"gecko_spellcheck":"1","opt_plugins":"lists,autosave,fullscreen,paste,table","opt_theme_advanced_buttons1":"formatselect,bullist,numlist,outdent,indent,blockquote,|,justifyleft,justifycenter,justifyright,justifyfull,|,undo,redo,cleanup,code","opt_theme_advanced_buttons2":"link,unlink,bold,italic,strikethrough,|,fontselect,fontsizeselect,forecolor,backcolor,|,table,image,pastetext,pasteword,fullscreen","opt_theme_advanced_buttons3":""}
@mrclay
mrclay / remote_diff
Created March 20, 2012 20:54
Compare two remote directories
#!/bin/bash
# Compares two remote directories using git diff (for color) by
# first copying all remote contents to local.
#
# USAGE: ./remote_diff path/from/www
TEMP1=~/.tmp_srv1_
TEMP2=~/.tmp_srv2_
@mrclay
mrclay / Elgg-MenuList.php
Last active February 23, 2016 19:04
Manage arrays of Elgg menu items
<?php
namespace UFCOE\Elgg;
/**
* Manage lists of Elgg menu items (during prepare hook) without losing sanity
*/
class MenuList
{
protected $items = array();
@mrclay
mrclay / firstLast.sql
Created April 3, 2012 14:02
MySQL: Extract last and first name(s) from a full "name" field (for Elgg)
SELECT
-- Assumed to be trimmed
name
-- Does name contain multiple words?
,(LOCATE(' ', name) = 0) AS hasMultipleWords
-- Returns the end of the string back until reaches a space.
-- E.g. "John Doe" => "Doe"
-- E.g. "Francis Scott Key" => "Key"
@mrclay
mrclay / alterQueryString.php
Created April 4, 2012 20:56
Alter (or remove) querystring keys from a root-relative URI
<?php
/**
* Alter (or remove) querystring keys from a root-relative URI.
* @param array $values associative array (see http_build_query). Use null value to remove keys
* @param string $uri if not given, $_SERVER['REQUEST_URI'] will be used
* @return string
*/
function alterQueryString($values = array(), $uri = null) {
$parts = parse_url($uri ? $uri : $_SERVER['REQUEST_URI']);
@mrclay
mrclay / CipherSuiteOrderer.java
Created April 8, 2012 21:16
Reorder Java cipher suites
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Enable all the supported cipher suites favoring stronger ciphers