<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users482</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/482" rel="self"/>
  <title>hubfactor</title>
  <updated>Mon Jan 19 00:56:07 -0800 2009</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code708</id>
    <published>2009-01-19T00:56:07-08:00</published>
    <updated>2010-11-08T02:32:11-08:00</updated>
    <title>[PHP] innerHTML of a DOMElement</title>
    <content type="html">&lt;p&gt;The equivalent of the non-standard DOM property 'innerHTML'.&lt;/p&gt;

&lt;pre&gt;&amp;lt;?php
function innerHTML($node){
  $doc = new DOMDocument();
  foreach ($node-&amp;gt;childNodes as $child)
    $doc-&amp;gt;appendChild($doc-&amp;gt;importNode($child, true));
    
  return $doc-&amp;gt;saveHTML();
}&lt;/pre&gt;</content>
    <author>
      <name>hubfactor</name>
      <email>sites@hubmed.org</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/708-innerhtml-of-a-domelement" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code700</id>
    <published>2009-01-07T16:25:13-08:00</published>
    <updated>2009-07-18T15:52:48-07:00</updated>
    <title>[PHP] Parse HTTP Accept headers</title>
    <content type="html">&lt;p&gt;a) define a list of MIME types and their corresponding local formats.
&lt;br /&gt;b) parse the HTTP Accept headers and pick the available format with the highest q value.&lt;/p&gt;

&lt;p&gt;The explode parameter might not be enough - can there be spaces and other characters, not just ',' and ';q='?&lt;/p&gt;

&lt;pre&gt;&amp;lt;?php
function parse_accept_headers($default = 'html'){
  $formats = array(
    'text/html' =&amp;gt; 'html',
    'application/xhtml+xml' =&amp;gt; 0,
    'application/xml' =&amp;gt; 0,
    '*/*' =&amp;gt; 'html',
    );

  $accept = array();
  foreach (explode(',', $_SERVER['HTTP_ACCEPT']) as $header){
    $parts = explode(';q=', $header);
    if (count($parts) === 1)
      $parts[1] = 1;
    $accept[$parts[0]] = $parts[1];
  }

  arsort($accept);
  $accept[] = $default;

  foreach ($accept as $format =&amp;gt; $q)
    if (array_key_exists($format, $formats) &amp;amp;&amp;amp; $formats[$format])
      break;
  
  return array($format, $formats[$format]);
}&lt;/pre&gt;</content>
    <author>
      <name>hubfactor</name>
      <email>sites@hubmed.org</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/700-parse-http-accept-headers" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code555</id>
    <published>2008-10-23T13:53:24-07:00</published>
    <updated>2009-01-16T14:57:24-08:00</updated>
    <title>[PHP] Generate a numbered file name</title>
    <content type="html">&lt;p&gt;Create a file name from the given prefix and suffix, incorporating an incremented number to ensure that the file doesn't already exist.&lt;/p&gt;

&lt;pre&gt;&amp;lt;?php
function unique_file($prefix, $suffix){
  $file = sprintf('%s.%s', $prefix, $suffix);
  if (file_exists($file))
    do 
      $file = sprintf('%s.%d.%s', $prefix, ++$i, $suffix);
    while (file_exists($file));

  return $file;
}&lt;/pre&gt;</content>
    <author>
      <name>hubfactor</name>
      <email>sites@hubmed.org</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/555-generate-a-numbered-file-name" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor11739</id>
    <published>2008-06-25T11:26:14-07:00</published>
    <title>[PHP] On Moving my xml parsing into a function.</title>
    <content type="html">

&lt;pre&gt;&amp;lt;?php
function page_xml($page){
  $xml = array(
    'home' =&amp;gt; 'cyloop_mobile.xml',
    'artist' =&amp;gt; 'cyloop_mobile.xml',
    'people' =&amp;gt; 'cyloop_mobile.xml',
    'news' =&amp;gt; 'cyloop_mobile.xml'
    );
    
  return simplexml_load_file('http://cm.cyloop.com/feeds/drupal/' . $xml[$page]);
}

$xml = page_xml('home');

foreach ($xml-&amp;gt;xpath('item/title') as $artist)
  printf('&amp;lt;li&amp;gt;%s&amp;lt;/li&amp;gt;', htmlspecialchars($artist));&lt;/pre&gt;</content>
    <author>
      <name>hubfactor</name>
      <email>sites@hubmed.org</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/309-moving-my-xml-parsing-into-a-function/refactors/11739" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor4848</id>
    <published>2008-04-11T21:30:12-07:00</published>
    <title>[PHP] On Content Page Include</title>
    <content type="html">

&lt;pre&gt;&amp;lt;?php
$id = $_GET['id'] ? $_GET['id'] : 'news'; // default = 'news'
$file = &amp;quot;includes/$id.php&amp;quot;;
if (strpos($id, '/') !== FALSE || !file_exists($file)) die('Naughty!'); // no directory traversal or missing files allowed
include $file;
&lt;/pre&gt;</content>
    <author>
      <name>hubfactor</name>
      <email>sites@hubmed.org</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/42-content-page-include/refactors/4848" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor4847</id>
    <published>2008-04-11T21:13:36-07:00</published>
    <title>[JavaScript] On Random URL</title>
    <content type="html">&lt;p&gt;Back to Javascript...&lt;/p&gt;

&lt;pre&gt;&amp;lt;script type=&amp;quot;text/javascript&amp;quot; id=&amp;quot;target&amp;quot;&amp;gt;
var urls = [[&amp;quot;url1&amp;quot;, &amp;quot;name1&amp;quot;], [&amp;quot;url2&amp;quot;, &amp;quot;name2&amp;quot;], [&amp;quot;url3&amp;quot;, &amp;quot;name3&amp;quot;]];

var link = urls[Math.floor(Math.random() * urls.length)];

var a = document.createElement(&amp;quot;a&amp;quot;);
a.href = link[0];
a.textContent = link[1];

var li = document.createElement(&amp;quot;li&amp;quot;);
li.appendChild(a);

var target = document.getElementById(&amp;quot;target&amp;quot;);
target.parentNode.insertBefore(li, target);
&amp;lt;/script&amp;gt;&lt;/pre&gt;</content>
    <author>
      <name>hubfactor</name>
      <email>sites@hubmed.org</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/117-random-url/refactors/4847" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code280</id>
    <published>2008-04-11T20:48:15-07:00</published>
    <updated>2008-04-11T20:53:42-07:00</updated>
    <title>[PHP] Download all audio enclosures from an RSS feed</title>
    <content type="html">&lt;p&gt;Given an RSS feed and an output directory, this should download all the audio files from the feed into that directory.&lt;/p&gt;

&lt;pre&gt;&amp;lt;?php
if (count($argv) != 3)
	die(&amp;quot;Usage: $argv[0] &amp;lt;RSS feed URL&amp;gt; &amp;lt;output dir&amp;gt;\n&amp;quot;);	

$rss = simplexml_load_file($argv[1]);
$rss-&amp;gt;registerXPathNamespace('media', 'http://search.yahoo.com/mrss/');

$media = $rss-&amp;gt;xpath(&amp;quot;//item/media:content[@type='audio/mpeg']&amp;quot;);
if (empty($media)){
	$media = $rss-&amp;gt;xpath(&amp;quot;//item/enclosure[@type='audio/mpeg']&amp;quot;);
	if (empty($media))
		die(&amp;quot;No media files found\n&amp;quot;);
}

$curl = curl_multi_init();
$connections = array();
$files = array();

foreach ($media as $enclosure){
  $url = (string) $enclosure['url'];
  $md5 = md5($url);
  
  print &amp;quot;$url\n&amp;quot;;
  
  $file = $argv[2] . &amp;quot;/$md5.mp3&amp;quot;;
  if (file_exists($file)) continue;
  
  $files[$md5] = fopen($file, 'w');
  
  $connections[$md5] = curl_init();
  curl_setopt_array($connections[$md5], array(
    CURLOPT_URL =&amp;gt; $url,
    CURLOPT_FOLLOWLOCATION =&amp;gt; TRUE,
    CURLOPT_MAXREDIRS =&amp;gt; 3,
    CURLOPT_FILE =&amp;gt; $files[$md5],
  	));
  	
  curl_multi_add_handle($curl, $connections[$md5]);
}

print &amp;quot;Downloading...&amp;quot;;

$running = NULL;
do {
   curl_multi_exec($curl, $running);
   sleep(1);
} while ($running &amp;gt; 0);

foreach ($connections as $md5 =&amp;gt; $connection){
  if ($data = curl_multi_getcontent($connection))
    file_put_contents($files[$md5], $data);
    
  curl_multi_remove_handle($curl, $connection);
  fclose($files[$md5]);
}

curl_multi_close($curl);

print &amp;quot;finished.\n&amp;quot;;
&lt;/pre&gt;</content>
    <author>
      <name>hubfactor</name>
      <email>sites@hubmed.org</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/280-download-all-audio-enclosures-from-an-rss-feed" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code269</id>
    <published>2008-03-30T20:25:27-07:00</published>
    <updated>2008-04-06T19:20:56-07:00</updated>
    <title>[PHP] Trim an image vertically</title>
    <content type="html">&lt;p&gt;Originally from &lt;a href="http://zavaboy.com/2007/10/06/trim_an_image_using_php_and_gd" target="_blank"&gt;http://zavaboy.com/2007/10/06/trim_an_image_using_php_and_gd&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Could do with a way to detect the background colour automatically (from the bottom-left corner pixel of the image?).&lt;/p&gt;

&lt;pre&gt;&amp;lt;?php

function imagetrim(&amp;amp;$im){
  $bg = imagecolorallocate($im, 255, 255, 255); // white
    
  $width = imagesx($im);
  $height = imagesy($im);

  foreach (range($height - 1, 0) as $y)
    foreach (range($width - 1, 0) as $x)
      if (imagecolorat($im, $x, $y) != $bg)
        break 2;

  $im2 = imagecreatetruecolor($width, $y);
  $bg2 = imagecolorallocate($im2, 255, 255, 255); // white
  imagefill($im2, 0, 0, $bg2);
  imagecopy($im2, $im, 0, 0, 0, 0, $width, $y);

  $im = $im2;
}&lt;/pre&gt;</content>
    <author>
      <name>hubfactor</name>
      <email>sites@hubmed.org</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/269-trim-an-image-vertically" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor3737</id>
    <published>2008-03-05T17:58:49-08:00</published>
    <title>[PHP] On Parse del.icio.us RSS feed</title>
    <content type="html">&lt;p&gt;SimpleXML isn't an RDF parser, so that wouldn't be better.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>hubfactor</name>
      <email>sites@hubmed.org</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/221-parse-del-icio-us-rss-feed/refactors/3737" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor3609</id>
    <published>2008-03-02T16:08:17-08:00</published>
    <title>[JavaScript] On Zoom all image files from small to medium, medium to large etc..</title>
    <content type="html">&lt;p&gt;I hadn't realised that HTMLCollection arrays had properties other than the selected elements. I guess it's better to iterate the old way then:&lt;/p&gt;

&lt;pre&gt;javascript:
(function(){
  var imgs = document.getElementsByTagName(&amp;quot;img&amp;quot;);
  for (var i = 0; i &amp;lt; imgs.length; i++) {
    var img = imgs[i];
    if (img.className == &amp;quot;icon&amp;quot;) continue;
    img.src = img.src.replace(/size=(small|medium)/, &amp;quot;size=large&amp;quot;);
    img.style.width = &amp;quot;75px&amp;quot;;
    img.style.height = &amp;quot;75px&amp;quot;;
  }
}
)()&lt;/pre&gt;</content>
    <author>
      <name>hubfactor</name>
      <email>sites@hubmed.org</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/244-zoom-all-image-files-from-small-to-medium-medium-to-large-etc/refactors/3609" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor3485</id>
    <published>2008-02-28T19:26:32-08:00</published>
    <title>[JavaScript] On Zoom all image files from small to medium, medium to large etc..</title>
    <content type="html">

&lt;pre&gt;javascript:
(function(){
  var imgs = document.getElementsByTagName(&amp;quot;img&amp;quot;);
  for each (var img in imgs) {
    if (img.className == &amp;quot;icon&amp;quot;) continue;
    img.src = img.src.replace(/size=medium/, &amp;quot;size=large&amp;quot;);
    img.style.width = &amp;quot;75px&amp;quot;;
    img.style.height = &amp;quot;75px&amp;quot;;
  }
}
)()
&lt;/pre&gt;</content>
    <author>
      <name>hubfactor</name>
      <email>sites@hubmed.org</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/244-zoom-all-image-files-from-small-to-medium-medium-to-large-etc/refactors/3485" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor2597</id>
    <published>2008-02-21T17:58:21-08:00</published>
    <title>[JavaScript] On Mathtex bookmarklet</title>
    <content type="html">&lt;p&gt;Ignoring the second version, which should be using document.createElement and document.appendChild&lt;/p&gt;

&lt;pre&gt;javascript:
(function(){
  var tex=prompt(&amp;quot;TeX input:&amp;quot;,&amp;quot;&amp;quot;);
  if(tex){
    var w=window.open(&amp;quot;http://www.forkosh.dreamhost.com/mathtex.cgi?&amp;quot;+encodeURIComponent(tex),&amp;quot;latexbox&amp;quot;,&amp;quot;width=300,height=200&amp;quot;);
    w.focus();
  }
}
)();&lt;/pre&gt;</content>
    <author>
      <name>hubfactor</name>
      <email>sites@hubmed.org</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/234-mathtex-bookmaklet/refactors/2597" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor2188</id>
    <published>2008-02-08T11:52:54-08:00</published>
    <title>[PHP] On Removing Parameters from an URL</title>
    <content type="html">

&lt;pre&gt;&amp;lt;?php

$url = 'http://www.example.com/url.php?t=4&amp;amp;p=5&amp;amp;sid=ddf175fd02216cbe9fab9a4b528d7185&amp;amp;hilite=2c23tra#link';
$remove = array('sid','t');

$parsed = parse_url($url);
parse_str($parsed['query'], $params);

$parsed['params'] = array_diff_key($params, array_fill_keys($remove, 0));
print_r($parsed);&lt;/pre&gt;</content>
    <author>
      <name>hubfactor</name>
      <email>sites@hubmed.org</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/228-removing-parameters-from-an-url/refactors/2188" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor2167</id>
    <published>2008-02-07T18:22:32-08:00</published>
    <title>[JavaScript] On Beautify JS Date to how recently the event occured.</title>
    <content type="html">&lt;p&gt;Hope the date's not in the future...&lt;/p&gt;

&lt;pre&gt;Date.prototype.when = function(){
  var diff = (new Date() - this) / 1e3;
  var u = [ &amp;quot;second&amp;quot;, &amp;quot;minute&amp;quot;, &amp;quot;hour&amp;quot;, &amp;quot;day&amp;quot;, &amp;quot;week&amp;quot;, &amp;quot;month&amp;quot;, &amp;quot;year&amp;quot; ];
  var s = [ 1, 60, 60, 24, 7, 4.333, 12, 1e9 ]; // 1e9 = hack for speed

  for (var i in s)
    if ((diff /= s[i]) &amp;lt; 1)
      return ~~ (diff *= s[i]) + &amp;quot; &amp;quot; + u[i-1] + (diff &amp;gt; 1 ? &amp;quot;s&amp;quot; : &amp;quot;&amp;quot;);
}&lt;/pre&gt;</content>
    <author>
      <name>hubfactor</name>
      <email>sites@hubmed.org</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/37-beautify-js-date-to-how-recently-the-event-occured/refactors/2167" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code226</id>
    <published>2008-02-01T12:40:24-08:00</published>
    <updated>2009-06-23T21:55:46-07:00</updated>
    <title>[JavaScript] Recursively dump an object</title>
    <content type="html">&lt;p&gt;Given an object, iterate through it and dump the properties.&lt;/p&gt;

&lt;pre&gt;function odump(object, depth, max){
  depth = depth || 0;
  max = max || 2;

  if (depth &amp;gt; max)
    return false;

  var indent = &amp;quot;&amp;quot;;
  for (var i = 0; i &amp;lt; depth; i++)
    indent += &amp;quot;  &amp;quot;;

  var output = &amp;quot;&amp;quot;;  
  for (var key in object){
    output += &amp;quot;\n&amp;quot; + indent + key + &amp;quot;: &amp;quot;;
    switch (typeof object[key]){
      case &amp;quot;object&amp;quot;: output += odump(object[key], depth + 1, max); break;
      case &amp;quot;function&amp;quot;: output += &amp;quot;function&amp;quot;; break;
      default: output += object[key]; break;        
    }
  }
  return output;
}&lt;/pre&gt;</content>
    <author>
      <name>hubfactor</name>
      <email>sites@hubmed.org</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/226-recursively-dump-an-object" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code221</id>
    <published>2008-01-28T16:08:27-08:00</published>
    <updated>2008-03-09T17:29:18-07:00</updated>
    <title>[PHP] Parse del.icio.us RSS feed</title>
    <content type="html">&lt;p&gt;Uses ARC RDF parser from &lt;a href="http://arc.semsol.org/" target="_blank"&gt;http://arc.semsol.org/&lt;/a&gt;&lt;/p&gt;

&lt;pre&gt;&amp;lt;?php

require_once(&amp;quot;arc/ARC2.php&amp;quot;); // http://arc.semsol.org/

$url = 'http://www.google.com/';  // URL to investigate

$ns_rss = 'http://purl.org/rss/1.0/';
$ns_dc = 'http://purl.org/dc/elements/1.1/';
$ns_rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';

$md5_url = md5($url);
$rss_url = 'http://del.icio.us/rss/url/' . $md5_url;
$html_url = 'http://del.icio.us/url/' . $md5_url;

$rdf = file_get_contents($rss_url);

$parser = ARC2::getRDFXMLParser();
$parser-&amp;gt;parse($rss_url, $rdf);
$root = $parser-&amp;gt;getSimpleIndex();

$channel = $root[&amp;quot;$html_url&amp;quot;];
$items_id = $channel[$ns_rss . 'items'][0];
$items = $root[&amp;quot;$items_id&amp;quot;];

$output = array();
foreach ($items as $key =&amp;gt; $triple){
  $item = $root[&amp;quot;$triple[0]&amp;quot;]; // pick out the individual item by ID
  
  if ($item[$ns_rdf . 'type'][0] == $ns_rss . 'item'){ // if it's an &amp;quot;rss item&amp;quot;
    $output[] = array(
      'creator' =&amp;gt; $item[$ns_dc . 'creator'][0],
      'tags' =&amp;gt; $item[$ns_dc . 'subject'][0],
      'description' =&amp;gt; $item[$ns_rss . 'description'][0],
      );
  }
}
print_r($output);&lt;/pre&gt;</content>
    <author>
      <name>hubfactor</name>
      <email>sites@hubmed.org</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/221-parse-del-icio-us-rss-feed" rel="alternate"/>
  </entry>
</feed>

