<?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:users919</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/919" rel="self"/>
  <title>Chris Dean</title>
  <updated>Sat Nov 15 23:09:11 -0800 2008</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor72436</id>
    <published>2008-11-15T23:09:11-08:00</published>
    <title>[PHP] On remove http from url string</title>
    <content type="html">&lt;p&gt;I'd go with fain182's solution (above) as it is faster (as it doesn't have to load the regex engine) but would strtolower the url first or use str_ireplace() if these urls are coming from user input. &lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Chris Dean</name>
      <email>chris@christopher-dean.co.uk</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/598-remove-http-from-url-string/refactors/72436" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor56388</id>
    <published>2008-10-28T07:57:13-07:00</published>
    <title>[PHP] On Array To Tablerows</title>
    <content type="html">&lt;p&gt;Love the one liner, but at the same time omg. &lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Chris Dean</name>
      <email>chris@christopher-dean.co.uk</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/559-array-to-tablerows/refactors/56388" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor56387</id>
    <published>2008-10-28T07:48:56-07:00</published>
    <title>[PHP] On Generate a numbered file name</title>
    <content type="html">&lt;p&gt;If the only reason for the number is to ensure the file doesn't exist why not use a unix timestamp? 
&lt;br /&gt;For most intents and purposes it will always be unique and it will increment itself.
&lt;br /&gt;Also I don't se a need to use sprintf for this...
&lt;br /&gt;As a result you can strip out all this checking code and just have:&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;pre&gt;&amp;lt;?php
function unique_file($prefix, $suffix)
{ 
   return $prefix.$suffix.time();
}
?&amp;gt;&lt;/pre&gt;</content>
    <author>
      <name>Chris Dean</name>
      <email>chris@christopher-dean.co.uk</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/555-generate-a-numbered-file-name/refactors/56387" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor56386</id>
    <published>2008-10-28T07:34:45-07:00</published>
    <title>[PHP] On Mortgage Calculator</title>
    <content type="html">&lt;p&gt;Maybe you should post this under the java category?&lt;/p&gt;

&lt;p&gt;That said I'd expect it to work pretty much as is, just with a change to how you reference the variables php uses $varname, not sure how java does this...&lt;/p&gt;

&lt;p&gt;Hope that helps&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Chris Dean</name>
      <email>chris@christopher-dean.co.uk</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/505-mortgage-calculator/refactors/56386" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor15383</id>
    <published>2008-08-20T16:20:58-07:00</published>
    <title>[PHP] On Universal File Download Class</title>
    <content type="html">&lt;p&gt;@Ishkur&lt;/p&gt;

&lt;p&gt;I try to avoid back-ticks as (for me at least) they obfuscate the code a little bit. &lt;/p&gt;

&lt;p&gt;That said, it may give a small performance boost - I don't know as I've not done any such tests, but maybe I will if I get 5 mins :)&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Chris Dean</name>
      <email>chris@christopher-dean.co.uk</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/440-universal-file-download-class/refactors/15383" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code440</id>
    <published>2008-08-16T11:21:49-07:00</published>
    <updated>2009-01-13T16:49:35-08:00</updated>
    <title>[PHP] Universal File Download Class</title>
    <content type="html">&lt;p&gt;After finding different hosting companies having wildly different policies when it comes to what's enabled and what isn't in PHP, I'm trying to build a generic file download class which will work in almost all situations.&lt;/p&gt;

&lt;p&gt;What I'd like to know is if any of the code below can be made more efficient or robust and also if anyone has a function for achieving the equivalent wget call in windows.&lt;/p&gt;

&lt;p&gt;I should just mention that this class has been cleaned of some application specific code, so if there's any disjointed bits I apologise - I have tried to deal with any discontinuities before posting but there may be something i've missed (this is also the reason for the bunch of if's in get_file() - the main version follows a few other processes/checks before attempting one of the download functions).&lt;/p&gt;

&lt;p&gt;Thanks&lt;/p&gt;

&lt;p&gt;Chris&lt;/p&gt;

&lt;pre&gt;&amp;lt;?php

class RequestFile
{
    public $StatusLogOn = FALSE;    //turn logging on/off (TRUE/FALSE)
    
    //tries different methods for retrieving a url as 
    //each hosting company tends to screw this up in one way or another
    //it logs the successful one in the db for use next time
    public function get_file($url)
    {
        //attempt the standard file_get_contents 
        $data = $this-&amp;gt;cc_file_get_contents($url); 
        
        //if that didn't work try curl
        if(!$data)
        {
            $data = $this-&amp;gt;cc_curl_file($url);             
        }
        
        //if that didn't work try a socket
        if(!$data)
        {
            $data = $this-&amp;gt;cc_socket_file($url);         
        }
        
        //if all else fails we can try wget (assuming we're on linux)
        if(!$data)
        {
            $data = $this-&amp;gt;cc_wget_file($url);             
        }
        
        return $data;
    }
    
    //minor modification to file_get_contents to log if it worked or not
    //note this or (fopen) will not work if allow_url_fopen = 0 in php.ini
    public function cc_file_get_contents($url)
    {
        if($data = file_get_contents($url))
        {
            $this-&amp;gt;log_status(date(&amp;quot;H:i:s d-m-Y&amp;quot;).' SUCCESS: Data Has Been Downloaded Using file_get_contents');
            return $data;
        }
        else
        {    
            if (ini_get('allow_url_fopen') != '1') 
            {
               $msg = &amp;quot;fopen wrappers are disabled&amp;quot;;
            }
                    
            $this-&amp;gt;log_status(date(&amp;quot;H:i:s d-m-Y&amp;quot;).' FAIL: '.$msg);
            
            return FALSE;    
        }
    }
    
    //try and use curl
    public function cc_curl_file($url)
    {
        // make sure curl is installed
        if (function_exists('curl_init')) 
        {
            $ch = curl_init();                                                //initialize a new curl resource    
            curl_setopt($ch, CURLOPT_URL, $url);                            //set the url to fetch
            curl_setopt($ch, CURLOPT_HEADER, 0);                            //don't give me the headers just the content
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);   //return the value instead of printing the response to browser
            curl_setopt($ch, CURLOPT_USERAGENT, $this-&amp;gt;rand_user_agent());    //use a user agent to mimic a browser    
            $data = curl_exec($ch);                                            //run the command
            curl_close($ch);                              //remember to always close the session and free all resources
            
            $this-&amp;gt;log_status(date(&amp;quot;H:i:s d-m-Y&amp;quot;).' SUCCESS: Data Has Been Downloaded Using CURL');
            return $data;
        } 
        else
        {
            $this-&amp;gt;log_status(date(&amp;quot;H:i:s d-m-Y&amp;quot;).' FAIL: CURL is not installed');
            return FALSE;
        }        
    }
    
    //try and use sockets - this code is untested
    public function cc_socket_file($url)
    {
        $parsedUrl = parse_url($url);    //get the host name and url path
    
        $host = $parsedUrl['host'];
    
        if(isset($parsedUrl['path'])) 
        {
            $path = $parsedUrl['path'];
        } 
        else 
        {
            $path = '/';    //the url is pointing to the host like http://www.mysite.com
        }
        
        if (isset($parsedUrl['query'])) 
        {
            $path .= '?'.$parsedUrl['query'];
        }
        
        if (isset($parsedUrl['port'])) 
        {
            $port = $parsedUrl['port'];
        } 
        else 
        {
            $port = '80';    //most sites use port 80
        }
        
        $timeout = 10;
        $response = '';
        
        $fp = @fsockopen($host, '80', $errno, $errstr, $timeout);    //connect to the remote server
        
        if(!$fp) 
        {
            $this-&amp;gt;log_status(date(&amp;quot;H:i:s d-m-Y&amp;quot;).' FAIL: Socket Failed Cannot Retrieve '.$url);
            return FALSE;
        } 
        else 
        {
            //send the necessary headers to get the file
            fputs($fp,    &amp;quot;GET $path HTTP/1.0\r\n&amp;quot; .
                        &amp;quot;Host: $host\r\n&amp;quot; .
                        &amp;quot;User-Agent: &amp;quot;.$this-&amp;gt;rand_user_agent().&amp;quot;\r\n&amp;quot; .
                        &amp;quot;Accept: */*\r\n&amp;quot; .
                        &amp;quot;Accept-Language: en-us,en;q=0.5\r\n&amp;quot; .
                        &amp;quot;Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n&amp;quot; .
                        &amp;quot;Keep-Alive: 300\r\n&amp;quot; .
                        &amp;quot;Connection: keep-alive\r\n&amp;quot; .
                        &amp;quot;Referer: http://$host\r\n\r\n&amp;quot;);
        
            //retrieve the response from the remote server
            while($line = fread($fp, 4096)) 
            {
                $response .= $line;
            }
            
            fclose($fp);
            
            //strip the headers
            $pos = strpos($response, &amp;quot;\r\n\r\n&amp;quot;);
            $response = substr($response, $pos + 4);
        }
        
        $this-&amp;gt;log_status(date(&amp;quot;H:i:s d-m-Y&amp;quot;).' SUCCESS: Data Has Been Downloaded Via A Socket Connection');
        return $response;    
    }
    
    //try and use wget if on linux
    public function cc_wget_file($url)
    {
        $cmd = &amp;quot;wget '&amp;quot;.$url.&amp;quot;'&amp;quot;;
        exec($cmd);
    
        if($data = file_get_contents(&amp;quot;broadcast&amp;quot;))    //the file gets saved as file simply called broadcast
        {
            $this-&amp;gt;log_status(date(&amp;quot;H:i:s d-m-Y&amp;quot;).' SUCCESS: File Downloaded Using wget');
            return $data;
        }
        else
        {
            $this-&amp;gt;log_status(date(&amp;quot;H:i:s d-m-Y&amp;quot;).' FAILED: Data Could not be retrieved using wget and file_get_contents');
            return FALSE;
        }        
    }
    
    //select an random user agent from the db
    public function rand_user_agent()
    {
        $sql = &amp;quot;SELECT * FROM cc_user_agents ORDER BY rand()&amp;quot;;    
        
        //code to get a record from the database goes here
        
        if($row === FALSE || !$row[0])
        {
            $this-&amp;gt;log_status(date(&amp;quot;H:i:s d-m-Y&amp;quot;).' FAIL: Could Not Retrieve Random User Agent - Using Default');
            return &amp;quot;Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3&amp;quot;;
        }
        else
        {
            return $row[0];
        }
    }
    
    //log status to a file
    public function log_status($msg)
    {
        if($this-&amp;gt;StatusLogOn === TRUE)
        {
            $path = &amp;quot;RequestFile.status.log&amp;quot;;
            $data = file_get_contents($path);
            $data .= &amp;quot;\n&amp;quot;.$msg;
            file_put_contents($data, $path);
        }
        
        return TRUE;
    }
}

?&amp;gt;&lt;/pre&gt;</content>
    <author>
      <name>Chris Dean</name>
      <email>chris@christopher-dean.co.uk</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/440-universal-file-download-class" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor15012</id>
    <published>2008-08-14T07:20:20-07:00</published>
    <title>[PHP] On MySQL original PHP 5 wrapper class</title>
    <content type="html">&lt;p&gt;I've had a play with doctrine and to me, although it has loads of great functionality, it's a bit of a bloated monster.&lt;/p&gt;

&lt;p&gt;If you're just doing basic ORM, running simple operations, then something like you have presented is more suitable, in my opinion.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Chris Dean</name>
      <email>chris@christopher-dean.co.uk</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/416-mysql-original-php-5-wrapper-class/refactors/15012" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor15009</id>
    <published>2008-08-14T07:00:53-07:00</published>
    <title>[PHP] On Resize image to canvas</title>
    <content type="html">&lt;p&gt;I don't see any real issues with the way this works, though it all applies to GD lib - unless you're doing a check to see if GD lib's available elsewhere maybe you'd want to include something like that?&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Chris Dean</name>
      <email>chris@christopher-dean.co.uk</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/433-resize-image-to-canvas/refactors/15009" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor14546</id>
    <published>2008-08-06T07:52:54-07:00</published>
    <title>[PHP] On Adding seconds to a MySQL timestamp</title>
    <content type="html">&lt;p&gt;Personally I'd use strtotime() for this - I've not tested it but it's probably quicker&lt;/p&gt;

&lt;pre&gt;function addSecondsToTimestamp ($_timestamp, $_amount) {
    $time = strtotime($_timestamp) + $_amount;
    return date('Y-m-d H:i:s', $time);
}
&lt;/pre&gt;</content>
    <author>
      <name>Chris Dean</name>
      <email>chris@christopher-dean.co.uk</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/363-adding-seconds-to-a-mysql-timestamp/refactors/14546" rel="alternate"/>
  </entry>
</feed>

