Skip to content

Instantly share code, notes, and snippets.

View tobsn's full-sized avatar

Tobsn tobsn

  • US/EU/SEA
View GitHub Profile
@tobsn
tobsn / oneLineReddit.php
Created February 5, 2010 00:49
reddit clone in one line
<?/*shell: touch s;chmod 777 s;*/$w=array('type','text','value','name','input');function c($x,$v,$s=''){return $x->createElement($v,$s);}function a($x,$v){return $x->appendChild($v);}function s($x,$v,$s=''){return $x->setAttribute($v,$s);}$g=$_GET;$p=$_POST;if($p['l']&&$p['t']){file_put_contents('s',implode('§§',array(0,time(),strip_tags($p['l']),strip_tags($p['t'])))."\n",FILE_APPEND);}$d=new DOMDocument();$h=c($d,'head');a($h,c($d,'title','r'));$m=c($d,'html');a($m,$h);$b=c($d,'body');$f=c($d,'form');s($f,'method','post');$i=c($d,$w[4]);s($i,$w[0],$w[1]);s($i,$w[3],'t');s($i,$w[2],'title');a($f,$i);$i=c($d,$w[4]);s($i,$w[0],$w[1]);s($i,$w[3],'l');s($i,$w[2],'link');a($f,$i);$i=c($d,$w[4]);s($i,$w[0],'submit');a($f,$i);a($b,$f);$z=file_get_contents('s');if($z){$u=c($d,'ul');$z=explode("\n",$z);$z=array_reverse($z,true);foreach($z as $i => $y){if(!$y){continue;}$y=explode('§§',$y);$l=c($d,'li');$a=c($d,'a','^');s($a,'href','?u='.$i);a($l,$a);a($l,$d->createTextNode(' '.$y[0].' '));$a=c($d,'a','v');s($a,'href'
@tobsn
tobsn / return image with text formatted to fit within given width.php
Created June 1, 2010 17:00
really ugly code to generate text within an transparent imagine of a given width without running over the borders (only pixel fonts are supported because of the fixed width)
function formattextimg( $text, $width = 960, $color = array( 0, 0, 0 ), $font = 2 ) {
$width = ( ( isset( $width ) && is_numeric( $width ) ) ? ( ( $width >= 100 ) ? (int)$width : 100 ) : 960 );
$text = str_replace( array( '<b>', '</b>', '<strong>', '</strong>' ), '|', $text );
$text_b = preg_split( '/\|/', $text, -1, PREG_SPLIT_OFFSET_CAPTURE );
foreach($text_b as $k => $tb){if($k%2!=0){$textbold[($tb[1]-1)]=$tb[0];}}
$text = str_replace('|','',$text);
for( $i = 0; $i < strlen( $text ); $i++ ) {
if( $string_c >= ( $width - ( (int)(imagefontwidth( $font ) / 2 ) ) ) ) {
$space = strrpos( $string, ' ' );
$string_sub = substr( $string, 0, $space );
@tobsn
tobsn / jquery.loader.js
Created December 10, 2010 19:50
there are many problems when you act as third party that offers a javascript file and you want to use jquery - this solved all of my problems. the little function checks if jquery is already embedded, includes it and waits until its loaded if its missing
(function(j,q,u,e,r,y,R,o,x){try{o=jQuery;if(o&&(!R||(R&&o.fn.jquery==R))){x=true}}catch(er){}if(!x||(R&&o.fn.jquery!=R)){(q=j.createElement(q)).type='text/javascript';if(r){q.async=true}q.src='//ajax.googleapis.com/ajax/libs/jquery/'+(R||1)+'/jquery.min.js';u=j.getElementsByTagName(u)[0];q.onload=q.onreadystatechange=(function(){if(!e&&(!this.readyState||this.readyState=='loaded'||this.readyState=='complete')){e=true;x=jQuery;jQuery.noConflict(true)(function(){y(x)});q.onload=q.onreadystatechange=null;u.removeChild(q)}});u.appendChild(q)}else{y(o)}})(document,'script','head',false,false,(function($){$(function(){
/* code goes here */
console.log($.fn.jquery);
})}));
// readable:
(function (j, q, u, e, r, y, R, o, x ) {
// IE8 undefined crash fix
try {
@tobsn
tobsn / BlitzView.php
Created December 16, 2010 01:58
blitz template engine view class for slim framework
<?php
// extended blitz class for better block handling (load assoc arrays correctly, one level)
class xBlitz extends Blitz{function xblock($k,$a){foreach($a as $v){$this->block('/'.$k,$v,true);}}}
class BlitzView extends View {
private $blitzEnvironment = null;
public function render( $template ) {
$env = $this->getEnvironment( $template );
@tobsn
tobsn / loader.plugin.php
Created December 17, 2010 01:25
controller loader for slim framework
<?php
Slim::hook('slim.before.dispatch', function () {
list( $type, $controller_name, $controller_action ) = array_merge( explode( '.', Slim::router()->current()->getName() ), array( null,null,null ) );
if( $type == 'controller' ) {
$file = './controllers/'.$controller_name.'.controller.class.php';
if( !is_readable( $file ) ) { return false; }
require_once( './controllers/base.controller.class.php' );
require_once( $file );
$class = $controller_name.'Controller';
db.coll.group({
key:{domain:true},
reduce:function(obj,prev){
prev.items.push(obj);
},
initial:{items:[]}
});
$list_pattern = 'SELECT
%5$s AS zip,
%6$s AS name,
SQRT( (
( %2$d - %8$s ) *
( %2$d - %8$s )
) + (
( %1$d - %7$s ) *
( %1$d - %7$s )
) ) AS distance
@tobsn
tobsn / gist:910241
Created April 8, 2011 16:42
Sizzle Closure Compiled - Sizzle() becomes A()
var l=null;
(function(){function z(a,b){for(var c,d=[],f="",e=b.nodeType?[b]:b;c=j.match.c.exec(a);)f+=c[0],a=a.replace(j.match.c,"");a=j.b[a]?a+"*":a;c=0;for(var h=e.length;c<h;c++)i(a,e[c],d);return i.filter(f,d)}function n(a,b){a=Array.prototype.slice.call(a,0);if(b)return b.push.apply(b,a),b;return a}function F(a,b){return"\\"+(b-0+1)}function i(a,b,c,d){var c=c||[],f=b=b||document;if(b.nodeType!==1&&b.nodeType!==9)return[];if(!a||typeof a!=="string")return c;var e,h,g,m,t,o=!0,p=i.i(b),k=[],q=a;do if(x.exec(""),
e=x.exec(q))if(q=e[3],k.push(e[1]),e[2]){m=e[3];break}while(e);if(k.length>1&&G.exec(a))if(k.length===2&&j.b[k[0]])h=z(k[0]+k[1],b);else for(h=j.b[k[0]]?[b]:i(k.shift(),b);k.length;)a=k.shift(),j.b[a]&&(a+=k.shift()),h=z(a,h);else if(!d&&k.length>1&&b.nodeType===9&&!p&&j.match.a.test(k[0])&&!j.match.a.test(k[k.length-1])&&(e=i.find(k.shift(),b,p),b=e.d?i.filter(e.d,e.set)[0]:e.set[0]),b){e=d?{d:k.pop(),set:n(d)}:i.find(k.pop(),k.length===1&&(k[0]==="~"||k[0]==="+")&&b.parentNode?b.parentNode:b,p)
@tobsn
tobsn / log.js
Created May 3, 2011 12:09
node.js pipe for lighttpd logs to mongodb
#!/usr/local/bin/node
var fs = require('fs'),
useragent = require('/www/utils/node_modules/useragent'),
zoneinfo = require('/www/utils/node_modules/zoneinfo'),
TZDate = zoneinfo.TZDate,
countrycodes = require('/www/utils/node_modules/zoneinfo/lib/countrycodes');
mongo = require('/www/utils/node_modules/mongoq'),
db = mongo('mongodb:\/\/localhost/logs'),
access = db.collection( 'access' );
@tobsn
tobsn / pixel.php
Created May 6, 2011 01:25
one transparent or colored gif pixel
<?php
header( 'Content-type: image/gif' );
header( 'Expires: Wed, 11 Nov 1998 11:11:11 GMT' );
header( 'Cache-Control: no-cache' );
header( 'Cache-Control: must-revalidate' );
if( !empty( $_GET['c'] ) ) {
$rgb = preg_replace( '/[^0-9a-z]/i', '', $_GET['c'] );
die( sprintf(