~ overflow ~

Archive for May, 2010

Custom twitter data fetching in php with cache

by z3n on May.28, 2010, under Coding, Tips & Hints

Problem:

I had to fit the twitter posts (“tweets”) on a site using a special layout, without all the regular twitter crapola

Solution:

There’s this old script that return contents as json, although i could simply add it to the javascript, it would slow down the site, everytime a new page was loaded user would need to fetch tweets all over again, so i did a little php function to fetch it cache and add to the output:

function _ld_twitter($profile=_twitter_profile,$tweets=_twitter_tweets) { // v1.0
	// check cache
	$hash=md5(md5($profile).md5($tweets));
	if (_twitter_cache && file_exists(_twitter_cache_path.$hash.".idx") && file_exists(_twitter_cache_path.$hash) && _fs(_twitter_cache_path.$hash.".idx") > getmicrotime())
		return _fs(_twitter_cache_path.$hash);

	// build
	$x=file_get_contents("http://twitter.com/statuses/user_timeline/".$profile.".json?callback=x&count=".$tweets);
	if ($x != "") {
		$x=json_decode(substr($x,2,-2));
		$b=_fs(templates."twitter".file_ext);$r="";$i=count($x)-1;
		foreach ($x as $k => $v)
			$r.=str_replace(array(
				"##WHO##","##IMG##",
				"##WHEN##","##MSG##",
				"##DELAY##","##LAST##"
				),array(
					$v->user->screen_name,$v->user->profile_image_url,
					date(_dt2,strtotime($v->created_at)),not_utf8($v->text),
					duration(getmicrotime()-strtotime($v->created_at),1,0),$k == $i ? " id='last'" : ""
			),$b);
		// do cache
		if (_twitter_cache) {
			_fw(_twitter_cache_path.$hash.".idx",(_twitter_cache_life*60) + getmicrotime(),"w");
			_fw(_twitter_cache_path.$hash,$r,"w");
		}
		// return
		return $r;
	} else {
		return false;
	}
}

// defines -- yes not formatted correctly
"_twitter_profile" => "your_tweeter_profile_name",										// twitter profile
"_twitter_tweets" => 3,															// how many tweets to load
"_twitter_cache_life" => 15,												// twitter cache life in minutes
"_twitter_cache_path" => "lib/cache/",							// twitter cache path
"_twitter_cache" => 1,															// enables / disables twitter cache
"_dt2" => "d M Y",
"templates" => "templates/",
"file_ext" => ".html"

/*
functions (not added here)
those functions are enhanced versions of basic functions, to try this script you can just take :
_fs = file_get_contents
_fw = file_put_contents
duration = a function to return a number of seconds into years, months, days, hours, minutes, seconds notation
*/

If you want to implement it as javascript, you can use the url:
http://twitter.com/statuses/user_timeline/”.$profile.”.json?callback=x&count=”.$tweets

where the callback is the function where the json data will be sent to.

1 Comment :, , , , , more...

IE z-index issues

by z3n on May.28, 2010, under Tips & Hints

Problem:

IE, in all it’s versions, ignores the z-index from non child elements, meaning that if you have a div inside another with z-index it’s fine, however, if there’s an external div (99%) of the cases, it will simply ignore the z-index.

Solution:

There’s no solution for this, but you can do a workaround. Even with IE ignoring the z-indexes it does has an order of placing elements in front of the others which is the order you have them on html, so if you have a header menu that should appear in the top of everything, you can place the html in the footer of the page, with position absolute, to the header position. This might not work for all cases, but helped mine. since the div in question was hidden and would appear when a element at header was clicked.

Leave a Comment :, , , more...

vdi -> vmdk

by z3n on May.28, 2010, under Linux Happyness, Tips & Hints

Problem:

Convert VirtualBox Disk Image (VDI) to VMWare Harddisk Format (VDMK)

Solution:

Solely using qemu will generate a broken image, the right solution is:

 VBoxManage --convertSettings internalcommands converttoraw INPUT.vdi OUTPUT.raw && qemu-img convert -O vmdk OUTPUT.raw FINAL.vmdk && rm -f OUTPUT.raw

Source:

commandlinefu

Leave a Comment :, , , , more...

How to scroll on a bash screen

by z3n on May.25, 2010, under Notes, Tips & Hints

Problem:

It’s so cool to have many screens at bash, but i never figured out how to scroll up on them, somehow i never looked after as well.

Solution:

CTRL + A -> ESC -> PAGE UP / PAGE DOWN

Source:

SaltyCrane

Leave a Comment :, , , , more...

Multi threaded php without EXEC

by z3n on May.20, 2010, under Coding, Tips & Hints

Problem:

How to do a multi threaded php execution without using exec or any other less secure method?

Solution:

function _load_thread() { // v1.0
	global $_SESSION,$_POST,$_GET,$_REQUEST,$_COOKIE,$_SERVER;
	if (isset($_SESSION)) {
		if (isset($_SESSION['is_thread']) && $_SESSION['is_thread']) {
			ignore_user_abort(true);
			$_SESSION['is_thread']=0;
			$x=unserialize($_SESSION['thread']);
			$_SESSION['thread']="";
			$_POST=$x['POST'];
			$_GET=$x['GET'];
			$_REQUEST=$x['REQUEST'];
			@ $_SERVER=$x['SERVER'];
			$_COOKIE=$x['COOKIE'];
			define("_is_thread",1);
		}
	}
}
function _save_thread() { // v1.0
	global $_SESSION,$_POST,$_GET,$_REQUEST,$_COOKIE,$_SERVER;
	if (isset($_SESSION)) {
		$_SESSION['is_thread']=1;
		$_SESSION['thread']=serialize(array(
			"POST" => $_POST,
			"GET" => $_GET,
			"REQUEST" => $_REQUEST,
			"SERVER" => $_SERVER,
			"COOKIE" => $_COOKIE
		));
	}
}
function _go_thread() { // v1.0
	global $_SERVER,$post;
	_save_thread();
	$post="thread_sessid=".session_id();
	@ cget(slash_fix(site_full_uri.$_SERVER['PHP_SELF']),"none",1,0,0,1,0,1);
}
function cget($url,$ref="none",$is_post=0,$is_cookie=0,$headers=0,$follow=1,$verbose=0,$timeout=30) { // v1.05-lite
	global $post,$cookie;
	$r=$tail="";
	$ch=curl_init($url);
	curl_setopt($ch,CURLOPT_VERBOSE,$verbose);
	curl_setopt($ch,CURLOPT_HEADER,$headers);
	curl_setopt($ch,CURLOPT_HTTPHEADER,array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; pt-BR; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3 (.NET CLR 3.5.30729)","Accept-Encoding:deflate"));
	curl_setopt($ch,CURLOPT_FOLLOWLOCATION,$follow);
	curl_setopt($ch,CURLOPT_MAXREDIRS,5);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
	curl_setopt($ch,CURLOPT_REFERER,$ref);
	curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,30); // default 30
	curl_setopt($ch,CURLOPT_TIMEOUT,$timeout); // default 30
	if ($is_post) {
		curl_setopt($ch,CURLOPT_POST,1);
		curl_setopt($ch,CURLOPT_POSTFIELDS,$post); // post must be a string, if you set it as a array it will use a non standard multi-part post
		$tail.=" ".$post;
		unset($post);
	}
	if ($is_cookie) {
		curl_setopt($ch,CURLOPT_COOKIE,$cookie);
		$tail.=" ".$cookie;
		unset($cookie);
	}
	$r=curl_exec($ch);
	curl_close($ch);
	return $r;
}

How it works:

When you want to send a long php job to background, assuming you have this very same job into the same php user is on but delimited by _is_thread define, you just need to call _go_thread(); , it will save ALL vars into the session_id, do a cURL call the PHP_SELF (you need to define site_full_uri as the full address of the request, like http://192.168.1.65/) this cURL have a timeout of 1s on propouse so the caller script is able to send the reply to the user fast, while the real job is done in background.

Leave a Comment :, , , more...

Pixel by pixel html table to build an image

by z3n on May.20, 2010, under lol

I was looking around for curl timeout issues and found this movie of a guy doing pixel by pixel html table to build an anime

Leave a Comment :, , , , more...

When MySQL Query Optimizing don’t work

by z3n on May.13, 2010, under Coding, Tips & Hints

Problem:

A supposed plain query like:

select
	pacote.*,
	users.id as uid,users.usr,
	group_concat(content.nm separator '|') as fnm,
	group_concat(historico.mid separator '|') as fmid
from
	pacote
	join historico on pacote.id=historico.pid
	join content on historico.mid=content.id
	join users on users.id=historico.uid
group by historico.pid,pacote.id
order by pacote.id desc

takes 15s to run, only because of the order by, assuming that each of the involved tables have at least 100,000 entries and pacote.id is the primary key.

Solution:

I’ve searched a lot but there’s no way to optimize this, limits will not help due the `order by`, having this query un-merged in many others will get worst results, having it without join will get about the same processing time (a bit slower in long run).

So, the rest of options were quite bad:
1. Remove crap rows out the tables
2. Get new hardware?!

Although pacote.added will not match pacote.id 100%, it will match 99.9%, so why not use the bare sorting (without order by mysql will output the rows in primary key order) ?

This would require a little coding to have it right:

So we have the same query as above WITHOUT order by:

select
	pacote.*,
	users.id as uid,users.usr,
	group_concat(content.nm separator '|') as fnm,
	group_concat(historico.mid separator '|') as fmid
from
	pacote
	join historico on pacote.id=historico.pid
	join content on historico.mid=content.id
	join users on users.id=historico.uid
group by historico.pid,pacote.id

(0.1s avg)

now on php:

// some vars
define("_ipp",20); // items per page
$x['page']=0; // current page

// do the query (assuming that $sql is the query above)
$res=mysql_db_query($db,$sql);

// build an array with ALL results
for ($rez=array();$r=mysql_fetch_array($res,MYSQL_ASSOC);$rez[]=$r) { }

// cut the array from the end (note minus signal)
$rez=array_slice($rez,-(intval($x['page']+1)*_ipp),_ipp);
krsort($rez); // invert array, simulating order by's DESC

foreach ($rez as $val) {
    // do things
}

(4.25s avg)

So the php solution is far faster than the mysql order by, although it’s not accurate as, it really solved the issue.

Leave a Comment :, , , , more...

Copy a Table into another

by z3n on May.12, 2010, under Tips & Hints

Problem:

How to copy a table into another, like a fast backup of a single table.

Solution:

MySQL:

create table <destination> select * from <source>

MSSQL:

select * into <destination> From <source>

Notes:

Although primary keys are preserved, the primary key column will not be automatically set on the destination table, so watch out before start inserting content on the destination.

Leave a Comment :, , , , more...

.htaccess to redirect non www to www

by z3n on May.12, 2010, under Tips & Hints

Since i keep forgetting this i’m posting here:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www\.|$) [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

This kind of redirect helps spiders knowing where you site really is, usually if you don’t have those and your non www domain is the same of your www you may get penalized for having a duplicate site. With this 301 redirect this will not happen.

Leave a Comment :, , , more...

Find dependencies of a rpm with yum

by z3n on May.08, 2010, under Coding, Linux Happyness, Tips & Hints

Problem:

While installing virtualbox on my server i found out that it had a hell of deps, which i can’t seem to find.

Solution:

Search around i found out that yum is able to find and install deps off a rpm, like this:

yum localinstall --nogpgcheck VirtualBox-2.1.2_41885_fedora9-1.i386.rpm
Leave a Comment :, , , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!