<?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:users84</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/84" rel="self"/>
  <title>typefreak</title>
  <updated>Fri Dec 14 19:12:37 -0800 2007</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor1148</id>
    <published>2007-12-14T19:12:37-08:00</published>
    <title>[PHP] On Log Referer, Excluding...</title>
    <content type="html">&lt;p&gt;Keep in mind that the 'http_referer' can't be fully trusted, as it is send by the client.
&lt;br /&gt;If you follow the above suggestion (about the db), make sure you escape the information properly.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>typefreak</name>
      <email>openid@typefreak.nl</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/183-log-referer-excluding/refactors/1148" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor695</id>
    <published>2007-11-02T09:47:23-07:00</published>
    <title>[PHP] On Thumbnail generator</title>
    <content type="html">&lt;p&gt;It is called a ternary operator: (scroll down to the 'ternary operator' heading.)
&lt;br /&gt;&lt;a href="http://nl2.php.net/operators.comparison" target="_blank"&gt;http://nl2.php.net/operators.comparison&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In short:
&lt;br /&gt;if the part before the ? is true, then the part between ? and : is executed. otherwise, the part after : is executed.&lt;/p&gt;

&lt;p&gt;The line you copied is functional equivilant to:  &lt;/p&gt;

&lt;pre&gt;if ( isset($_REQUEST['width']) ) {
    $maxwidth = $_REQUEST['width'];
}
else {
    $maxwidth = 160;
}&lt;/pre&gt;</content>
    <author>
      <name>typefreak</name>
      <email>openid@typefreak.nl</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/122-thumbnail-generator/refactors/695" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor682</id>
    <published>2007-11-01T12:17:17-07:00</published>
    <title>[PHP] On Thumbnail generator</title>
    <content type="html">&lt;p&gt;Personally, I would rather use $_POST or $_GET then $_REQUEST&lt;/p&gt;

&lt;p&gt;Either way, you're not even trying to prevent notices. Bad idea. Especially if you're trying to send headers after that.&lt;/p&gt;

&lt;p&gt;The statement ($getimg[2] &amp;lt; 1 &amp;amp;&amp;amp; $getimg[2] &amp;gt; 3) alway returns false, as a number can't be smaller then 1, and bigger then 3 at the same time.&lt;/p&gt;

&lt;p&gt;The use of the while-loop doensn't make any sense to me.&lt;/p&gt;

&lt;p&gt;Function names are supposed to be case-sensitive, better use lowercase where appropriate.&lt;/p&gt;

&lt;p&gt;$imgA and $imgB aren't really describing names. Rather use $smallimg and $bigimg instead.&lt;/p&gt;

&lt;pre&gt;&amp;lt;?
// requiered argument
if ( empty($_REQUEST[&amp;quot;pic&amp;quot;]) ) {
    die('You haven\'t specified a picture');
}
$pic = $_REQUEST[&amp;quot;pic&amp;quot;];

// optional parameters
$maxwidth = (isset($_REQUEST['width']) ? $_REQUEST['width'] : 160);
$maxheight = (isset($_REQUEST['height']) ? $_REQUEST['height'] : 120);

$getimg = getimagesize($pic);
// 0. width
// 1. height
// 2. Type: 1=GIF, 2=JPEG, 3=PNG
// 3. size html tags (e.g. 'width=&amp;quot;111&amp;quot; height=&amp;quot;24&amp;quot;')

// preventing from accessing non gfx files.
if ( $getimg[2] &amp;lt; 1 || $getimg[2] &amp;gt; 3 || $getimg[2] == &amp;quot;&amp;quot; ) {
    die(&amp;quot;No valid gfx-source.&amp;quot;);
}

$oldwidth = $getimg[0];
$oldheight = $getimg[1];

// If original is smaller then or equal to new image
if ( $oldwidth &amp;lt;= $maxwidth &amp;amp;&amp;amp; $oldheight &amp;lt;= $maxheight ) {
    switch ($getimg[2]) {
        case 1:
            // Gif image
            $img = imagecreatefromgif($pic);
            header(&amp;quot;Content-Type: image/gif&amp;quot;);
            imagegif($img);
            break;
        case 2:
            // Jpeg image
            $img = imagecreatefromjpeg($pic);
            header(&amp;quot;Content-Type: image/jpeg&amp;quot;);
            imagejpeg($img);
            break;
        case 3:
            // Png image
            $img = imagecreatefrompng($pic);
            header(&amp;quot;Content-Type: image/png&amp;quot;);
            imagepng($img);
            break;
    }
}
else {
    // Image should be rezised
    $widthdif = $oldwidth / $maxwidth;
    $heightdif = $oldheight / $maxheight;
    $maxdif = max($widthdif, $heightdif);
    $newwidth = floor($oldwidth / $maxdif);
    $newheight = floor($oldheight / $maxdif);

    $smallimg = imagecreatetruecolor($newwidth,$newheight);    //alte gdlib --&amp;gt; imagecreate()
    switch ($getimg[2]) {
        case 1:
            // Gif image
            $bigimg = imagecreatefromgif($pic);    
            imagecopyresized($smallimg, $bigimg, 0,0, 0,0, $newwidth, $newheight, $oldwidth, $oldheight);        
            header(&amp;quot;Content-Type: image/gif&amp;quot;);
            imagegif($smallimg);
            break;
        case 2:
            // Jpeg image
            $bigimg = imagecreatefromjpeg($pic);
            imagecopyresized($smallimg, $bigimg, 0,0, 0,0, $newwidth, $newheight, $oldwidth, $oldheight);
            header(&amp;quot;Content-Type: image/jpeg&amp;quot;);
            imagejpeg($smallimg);
            break;
        case 3:
            // Png image
            $imgB = imagecreatefrompng($pic);
            imagecopyresized($smallimg, $bigimg, 0,0, 0,0, $newwidth, $newheight, $oldwidth, $oldheight);
            header(&amp;quot;Content-Type: image/png&amp;quot;);
            imagepng($smallimg);
            break;
    }
}
?&amp;gt;&lt;/pre&gt;</content>
    <author>
      <name>typefreak</name>
      <email>openid@typefreak.nl</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/122-thumbnail-generator/refactors/682" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor616</id>
    <published>2007-10-28T16:12:04-07:00</published>
    <title>[PHP] On Robots Reader</title>
    <content type="html">&lt;p&gt;I'm not currently refactoring, but I have a few comments:&lt;/p&gt;

&lt;p&gt;1: You don't check for user agent * (Only for $my_user_agent)
&lt;br /&gt;2: You don't check for allow lines (sometimes a exception for disallowed pages is given in 'allow: ' lines)
&lt;br /&gt;3: At the end of the main function, you're using $forbidden a bit strange: (You want a boolean answer, so use true/false. And in this case, as the function is robots_allowed(), I would rather call the variable $allowed instead of $forbidden.)
&lt;br /&gt;4: Why is this line?
&lt;br /&gt;$disallow_line=str_replace(&amp;quot;/&amp;quot;, &amp;quot;&amp;quot; ,$disallow_line);
&lt;br /&gt;What if a site has
&lt;br /&gt;Disallow: /info/secret in its list?
&lt;br /&gt;Currently, you'r checking if the requested url contains infosecret, instead of info/secret
&lt;br /&gt;5: (related to 4), When checking url's, It isn't wise to use '/' as the delimiter, as the url itself can contain these caracters. Better use # instead.
&lt;br /&gt;6: In Read_Content(), if fopen fails, you'll probably get a notice at the return, because $contents isn't set. (Please, don't suppress, but solve)&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>typefreak</name>
      <email>openid@typefreak.nl</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/116-robots-reader/refactors/616" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor552</id>
    <published>2007-10-26T13:40:23-07:00</published>
    <title>[PHP] On image upload script</title>
    <content type="html">&lt;p&gt;A bit of rewriting:&lt;/p&gt;

&lt;p&gt;Comment behinde lines is telling what i've changed&lt;/p&gt;

&lt;p&gt;Why are you using the database? Is there anything else using it? Just storing the name is useless, you can also use 'file_exists' or simmilar functions.&lt;/p&gt;

&lt;pre&gt;&amp;lt;?php
// check to see if image has been uploaded

if (empty($_FILES['image']) OR $_FILES['image']['error'] != UPLOAD_ERR_OK) {
        die ('&amp;lt;strong&amp;gt;Invalid image uploaded.  Please go back and try again.&amp;lt;/strong&amp;gt;');
}    else {
    $uploaded_image = $_FILES['image']['tmp_name'];
    $uploaded_image_name = $_FILES['image']['name']; // Added name var
}    


//function to view any type of image
function open_image ($file, $name) { // Request another paramater, containing original filename
        // Get extension
        $extension = strtolower(strrchr($name, '.')); // 2 lines to 1, using the new parameter

        switch($extension) {
                case '.jpg':
                case '.jpeg': // .jpg and .jpeg can be treated the same
                        $im = @imagecreatefromjpeg($file);
                        $image_type = &amp;quot;.jpeg&amp;quot;;
                        break;
                case '.gif':
                        $im = @imagecreatefromgif($file);
                        $image_type = &amp;quot;.gif&amp;quot;;
                        break;                        
                case '.png':
                        $im = @imagecreatefrompng($file); // Create from png instead of gif (whe have png, don't we?)
                        $image_type = &amp;quot;.png&amp;quot;;
                        break;
                default:
                        $im = false;
                        break;
        }
        return $im;
    }
        
        

        
        
// Load image
$image = open_image($uploaded_image, $uploaded_image_name); // Also pass second parameter
if ($image === false) { die ('that file type is not allowed'); } // die if theres no image


// get original image width and height
$width = imagesx($image);
$height = imagesy($image);

// resized image width
$resize_width = 1020;
$resize_height = intval($height * ($resize_width/$width)); // Make sure $resize_height is an int

// thumbnail image width
$thumb_width = 150;
$thumb_height = intval($height * ($thumb_width/$width)); // Make sure $thumb_height is an int

// Resample resized image
$image_resized = imagecreatetruecolor($resize_width, $resize_height);
imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $resize_width, $resize_height, $width, $height);

// Resample thumbnail image
$image_resized = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
imagecopyresampled($image_thumbnail, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

///////////////////////////////// check image name and rename it ///////////////////////////////
$db_safe_name = mysql_real_escape_string($uploaded_image_name); // Better safe then sorry
  $result_check = mysql_query(&amp;quot;SELECT photo_file_name FROM album WHERE photo_file_name='{$db_safe_name}'&amp;quot;) or die(mysql_error()); // You sure you want to show that error?

$number = mysql_num_rows($result_check); // Why use a while if you can see if there are results directly?
if ( $number &amp;gt; 0 ) { // Perhaps checking again after renaming, the new name might already by in use.
    $ext = explode($uploaded_image_name);
    $ext = array_pop($ext);
    $uploaded_image_name = str_replace(&amp;quot;.&amp;quot; . $ext, &amp;quot;&amp;quot;, $uploaded_image_name);
    $uploaded_image_name = $uploaded_image_name . &amp;quot;1.&amp;quot;. $ext;
}
  
  
/////////////////////////////////////// insert to directory ///////////////////////////////

  $filedir = '/upload/user_album/'; // the directory for the original image
  $thumbdir = '/upload/user_album/thumb/'; // the directory for the thumbnail image
  $prod_img = $filedir.$uploaded_image_name;
  $prod_img_thumb = $thumbdir.$uploaded_image_name;
         



    if( !empty($HTTP_POST_FILES['song_image']['tmp_name']) )
        {
        $ini_val = ( @phpversion() &amp;gt;= '4.0.0' ) ? 'ini_get' : 'get_cfg_var';

        if ( @$ini_val('open_basedir') != '' )
        {
            if ( @phpversion() &amp;lt; '4.0.3' )
            {
            message_die(GENERAL_ERROR, 'open_basedir is set and your PHP version does not allow move_uploaded_file&amp;lt;br /&amp;gt;&amp;lt;br /&amp;gt;Please contact your server admin', '', __LINE__, __FILE__);
            }

            $move_file = 'move_uploaded_file';
        }
        else
        {
            $move_file = 'copy';
        }
}

        
        // moved resized image into directory
        $move_file($uploaded_image, $prod_img);
        @chmod($prod_img, 0777);
        
        // moved thumb image into directory
        $move_file($uploaded_image, $prod_img_thumb);
        @chmod($prod_img_thumb, 0777);
        
        }
        
/////////////////////////////////////// insert to database///////////////////////////////
        $db_safe_name = mysql_real_escape_string($uploaded_image_name);
        mysql_query(&amp;quot;INSERT INTO album ('photo_file_name') VALUES('$db_safe_name') &amp;quot;) or die(mysql_error());&lt;/pre&gt;</content>
    <author>
      <name>typefreak</name>
      <email>openid@typefreak.nl</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/109-image-upload-script/refactors/552" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor551</id>
    <published>2007-10-26T13:21:59-07:00</published>
    <title>[PHP] On image upload script</title>
    <content type="html">&lt;p&gt;The wrong filetype is easy to understand: You're using $_FILES['image']['tmp_name'] to check the extension, while you should use $_FILES['image']['name'] For that purpose.
&lt;br /&gt;&lt;a href="http://nl2.php.net/manual/nl/features.file-upload.php" target="_blank"&gt;http://nl2.php.net/manual/nl/features.file-upload.php&lt;/a&gt;&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>typefreak</name>
      <email>openid@typefreak.nl</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/109-image-upload-script/refactors/551" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor420</id>
    <published>2007-10-14T10:00:35-07:00</published>
    <title>[PHP] On making user html input secure</title>
    <content type="html">&lt;p&gt;Overdoing it.
&lt;br /&gt;After the lines of code I posted (including the $_POST = secure_input($_POST); line), all $_POST['xxx'] are secured. You don't need to call the function for each var again.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>typefreak</name>
      <email>openid@typefreak.nl</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/80-making-user-input-secure/refactors/420" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor415</id>
    <published>2007-10-13T15:42:01-07:00</published>
    <title>[PHP] On making user html input secure</title>
    <content type="html">&lt;p&gt;I don't know why you're using the str_replace function, but if you need it, put in on the line containing
&lt;br /&gt;  return htmlspecialchars(trim($v));&lt;/p&gt;

&lt;p&gt;Using this method, you can also automatically handle/secure any submitted array, regardless of its dimensions.&lt;/p&gt;

&lt;pre&gt;function secure_input($v) {
    if ( is_array($v) ) {
        return array_map('secure_input', $v);
    }
    else {
        return htmlspecialchars(trim($v));
    }
}
$_POST = secure_input($_POST);&lt;/pre&gt;</content>
    <author>
      <name>typefreak</name>
      <email>openid@typefreak.nl</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/80-making-user-input-secure/refactors/415" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor239</id>
    <published>2007-10-03T03:47:26-07:00</published>
    <title>[PHP] On makeSafe Content Filter</title>
    <content type="html">&lt;p&gt;if ($escape==true) equals if ($escape)&lt;/p&gt;

&lt;p&gt;What if the input uses &amp;quot;\n&amp;quot; instead of &amp;quot;\r\n&amp;quot; ? (won't be replaced)&lt;/p&gt;

&lt;p&gt;Also, if you use htmlentities, &amp;lt; and &amp;gt; are translated to &amp;amp;lt; and &amp;amp;gt;, so strip-tags won't be of any use. (As far As I know)&lt;/p&gt;

&lt;pre&gt;&amp;lt;?php
function makeSafe($variable, $escape=true) {
  if (get_magic_quotes_gpc()) { 
    $variable = stripslashes($variable); 
  }

  $variable = htmlentities($variable, ENT_QUOTES);
  $variable = str_replace(&amp;quot;\r\n&amp;quot;, &amp;quot;&amp;quot;, $variable);

  if ($escape) {
    $variable = mysql_escape_string(trim($variable));
  }
  return $variable;
}
?&amp;gt;&lt;/pre&gt;</content>
    <author>
      <name>typefreak</name>
      <email>openid@typefreak.nl</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/44-makesafe-content-filter/refactors/239" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor168</id>
    <published>2007-10-01T13:56:14-07:00</published>
    <title>[JavaScript] On Beautify JS Date to how recently the event occured.</title>
    <content type="html">

&lt;pre&gt;/***
* Beautify date to how recent the event occurred compared to now.
* Some examples:  1 second, 4 hours, 10 days, 1 year.
*	Example:
*   var oneFiftyMin = new Date(new Date().getTime() - 60000 * 150);
*   alert(oneFiftyMin.when()); // will display &amp;quot;2 hours&amp;quot;
*
*  Handy for things like &amp;quot;Item posted &amp;quot; + someDate.when() + &amp;quot; ago.&amp;quot;
*/
Date.prototype.when = function() {

	var diff = new Date().getTime() - this.getTime();
	var when; // our return value

	//TODO:  what if the time is in the future? 
	//if (diff &amp;lt; 0) throw new Error (&amp;quot;Date is in future, check timezone?&amp;quot;;

	//one or more of these will be non-zero, but we only care about the biggest one (in scale of time)
  if (diff &amp;gt; (2592000000 * 12) ) {
    when = Math.floor(diff/(2592000000 * 12)) + &amp;quot; Year&amp;quot;;
  }
  else if (diff &amp;gt; 2592000000) {
    when = Math.floor(diff/2592000000) + &amp;quot; Month&amp;quot;;
  }
  else if (diff &amp;gt; 86400000) {
    when = Math.floor(diff/86400000) + &amp;quot; Day&amp;quot;;
  }
  else if (diff &amp;gt; 3600000) {
    when = Math.floor(diff/3600000) + &amp;quot; Hour&amp;quot;;
  }
  else if (diff &amp;gt; 60000) {
    when = Math.floor(diff/60000) + &amp;quot; Minute&amp;quot;;
  }
  else if (diff &amp;gt; 1000) {
    when = Math.floor(diff/1000) + &amp;quot; Second&amp;quot;;
  }

  //add plural if necessary
  return (0 == when.indexOf(&amp;quot;1 &amp;quot;)) ? when : when + &amp;quot;s&amp;quot;;
}&lt;/pre&gt;</content>
    <author>
      <name>typefreak</name>
      <email>openid@typefreak.nl</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/37-beautify-js-date-to-how-recently-the-event-occured/refactors/168" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor146</id>
    <published>2007-10-01T05:35:36-07:00</published>
    <title>[Java] On Get a random number within a given interval</title>
    <content type="html">&lt;p&gt;Why do you return a 'long', if the range is only specified by ints? The result always fits in an integer.
&lt;br /&gt;This is more or less the standard method for generating a random number. The only (usefull) change I can think of is:&lt;/p&gt;

&lt;pre&gt;public static int randomNumber(int min, int max) {
	return min + (int)(Math.random() * (max - min));
}&lt;/pre&gt;</content>
    <author>
      <name>typefreak</name>
      <email>openid@typefreak.nl</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/35-get-a-random-number-within-a-given-interval/refactors/146" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor137</id>
    <published>2007-09-30T14:05:36-07:00</published>
    <title>[Ruby] On compound interest</title>
    <content type="html">&lt;p&gt;Why don't you use a loop?
&lt;br /&gt;(Untested)&lt;/p&gt;

&lt;pre&gt;cap_growth = 5
v += (@property.current_value / 100 * cap_growth)

for i in (0..7)
  puts v
  v +=  v / 100 * cap_growth
end&lt;/pre&gt;</content>
    <author>
      <name>typefreak</name>
      <email>openid@typefreak.nl</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/34-compound-interest/refactors/137" rel="alternate"/>
  </entry>
</feed>

