Archive for the 'Programming' Category

Calculate Google Pagerank with PHP

Monday, March 19th, 2007

This is modified code that I found from the Internet the orginal code that may not work on some server that php has 32bit operation problem and I already tested on my server.

PHP:
  1. <?php
  2. define('GOOGLE_MAGIC', 0xE6359A60);
  3.  
  4. //unsigned shift right
  5. function zeroFill($a, $b)
  6. {
  7.     $z = hexdec(80000000);
  8.         if ($z & $a)
  9.         {
  10.             $a = ($a>>1);
  11.             $a &= (~$z);
  12.             $a |= 0x40000000;
  13.             $a = ($a>>($b-1));
  14.         }
  15.         else
  16.         {
  17.             $a = ($a>>$b);
  18.         }
  19.         return $a;
  20. } 
  21.  
  22. function toInt32(& $x){
  23.   $z = hexdec(80000000);
  24.   $y = (int)$x;
  25.  if($y==-$z&&$x<-$z){
  26.    $y = (int)((-1)*$x);
  27.    $y = (-1)*$y;
  28.   }
  29.   $x = $y;
  30. }
  31.  
  32. function mix($a,$b,$c) {
  33. $a -= $b; $a -= $c; toInt32($a); $a = (int)($a ^ (zeroFill($c,13)));
  34. $b -= $c; $b -= $a; toInt32($b); $b = (int)($b ^ ($a<&lt;8));
  35. $c -= $a; $c -= $b; toInt32($c); $c = (int)($c ^ (zeroFill($b,13)));
  36. $a -= $b; $a -= $c; toInt32($a); $a = (int)($a ^ (zeroFill($c,12)));
  37. $b -= $c; $b -= $a; toInt32($b); $b = (int)($b ^ ($a<&lt;16));
  38. $c -= $a; $c -= $b; toInt32($c); $c = (int)($c ^ (zeroFill($b,5)));
  39. $a -= $b; $a -= $c; toInt32($a); $a = (int)($a ^ (zeroFill($c,3)));
  40. $b -= $c; $b -= $a; toInt32($b); $b = (int)($b ^ ($a<&lt;10));
  41. $c -= $a; $c -= $b; toInt32($c); $c = (int)($c ^ (zeroFill($b,15)));
  42. return array($a,$b,$c);
  43. }
  44.  
  45. function GoogleCH($url, $length=null, $init=GOOGLE_MAGIC) {
  46.     if(is_null($length)) {
  47.         $length = sizeof($url);
  48.     }
  49.     $a = $b = 0x9E3779B9;
  50.     $c = $init;
  51.     $k = 0;
  52.     $len = $length;
  53.     while($len>= 12) {
  54.         $a += ($url[$k+0] +($url[$k+1]<&lt;8) +($url[$k+2]<&lt;16) +($url[$k+3]<&lt;24));
  55.         $b += ($url[$k+4] +($url[$k+5]<&lt;8) +($url[$k+6]<&lt;16) +($url[$k+7]<&lt;24));
  56.         $c += ($url[$k+8] +($url[$k+9]<&lt;8) +($url[$k+10]<&lt;16)+($url[$k+11]<&lt;24));
  57.         $mix = mix($a,$b,$c);
  58.         $a = $mix[0]; $b = $mix[1]; $c = $mix[2];
  59.         $k += 12
  60.         $len -= 12;
  61.     }
  62.  
  63.     $c += $length;
  64.     switch($len)              /* all the case statements fall through */
  65.     {
  66.         case 11: $c+=($url[$k+10]<&lt;24);
  67.         case 10: $c+=($url[$k+9]<&lt;16);
  68.         case 9 : $c+=($url[$k+8]<&lt;8);
  69.           /* the first byte of c is reserved for the length */
  70.         case 8 : $b+=($url[$k+7]<&lt;24);
  71.         case 7 : $b+=($url[$k+6]<&lt;16);
  72.         case 6 : $b+=($url[$k+5]<&lt;8);
  73.         case 5 : $b+=($url[$k+4]);
  74.         case 4 : $a+=($url[$k+3]<&lt;24);
  75.         case 3 : $a+=($url[$k+2]<&lt;16);
  76.         case 2 : $a+=($url[$k+1]<&lt;8);
  77.         case 1 : $a+=($url[$k+0]);
  78.          /* case 0: nothing left to add */
  79.     }
  80.     $mix = mix($a,$b,$c);
  81.     /*-------------------------------------------- report the result */
  82.     return $mix[2];
  83. }
  84.  
  85. //converts a string into an array of integers containing the numeric value of the char
  86. function strord($string) {
  87.     for($i=0;$i<strlen($string);$i++) {
  88.         $result[$i] = ord($string{$i});
  89.     }
  90.     return $result;
  91. }
  92.  
  93.  
  94. // converts an array of 32 bit integers into an array with 8 bit values. Equivalent to (BYTE *)arr32
  95.  
  96. function c32to8bit($arr32) {
  97.     for($i=0;$i<count($arr32);$i++) {
  98.         for ($bitOrder=$i*4;$bitOrder<=$i*4+3;$bitOrder++) {
  99.             $arr8[$bitOrder]=$arr32[$i]&255;
  100.             $arr32[$i]=zeroFill($arr32[$i], 8);
  101.         }     
  102.     }
  103.     return $arr8;
  104. }
  105.  
  106.  
  107. function get_page_rank($url){
  108.     $url = preg_replace('/\?.*$/','?',$url);
  109.     $reqgr = "info:".$url;
  110.     $reqgre = "info:".urlencode($url);     
  111.     $gch = "6".GoogleCH(strord($reqgr));     
  112.         $fsock = fsockopen('toolbarqueries.google.com', 80, $errno, $errstr);
  113.         if ( !$fsock ){   
  114.             echo 'Can not connect to server';
  115.             return -1;
  116.         }
  117.         $base_get = "/search?client=navclient-auto&ch=".$gch."&ie=UTF-8&oe=UTF-8&features=Rank:FVN&q=".$reqgre;
  118.         fputs($fsock, "GET $base_get HTTP/1.1\r\n");
  119.         fputs($fsock, "HOST: toolbarqueries.google.com\r\n");
  120.         fputs($fsock, "User-Agent: Mozilla/4.0 (compatible; GoogleToolbar 2.0.114-big; Windows XP 5.1)\r\n");
  121.         fputs($fsock, "Connection: close\r\n\r\n");
  122.         while(!feof($fsock)){ 
  123.             $res['content'] .= fread($fsock, 1024);
  124.         }
  125.         fclose($fsock);
  126.         if(preg_match('/Rank_.*?:.*?:(\d+)/i', $res['content'], $m)){     
  127.             return $m[1];
  128.         }else{     
  129.             return -1;
  130.         }       
  131. }
  132.  
  133. echo get_page_rank("www.google.com");
  134.  
  135. ?>

PHP Group accused of security incompetence

Thursday, February 22nd, 2007

PHP developer Stefan Esser has said he will go ahead with plans to disclose dozens of security flaws in PHP in March, hitting back at criticism that the “Month of PHP bugs” project is nothing more than dangerous, self-serving publicity. The problem isn’t irresponsible disclosure, but the sluggishness of the PHP team in fixing serious problems, Esser contended. He has first-hand experience with the PHP security process having created both the Hardened-PHP Project and the PHP Security Response Team, which he left acrimoniously in December.

Original post by Forum of Incident Response and Security Teams - Daily Security News

March To Be Month of PHP Bugs

Wednesday, February 21st, 2007

“Stefan Esser is the founder of both the Hardened-PHP Project and the PHP Security Response Team (which he recently left). During an interview with SecurityFocus he announced the upcoming Month of PHP bugs initiative in March.”

Original post by Forum of Incident Response and Security Teams - Daily Security News

Fix 404 errors on Ultimate Tag Warrior

Monday, February 19th, 2007

I uses Ultimate Tag Warrior version 3.14159265 (February 4th, 2007) with WordPress 2.1, it work great but there have some problem or bug that when you try to open /tag it will show 404 errors. I check and try to make the page /tag or /tag/ show all tags with function of Ultimate Tag Warrior. Here is small changed.

PHP:
  1. function ultimate_tag_templates() {
  2.     if ($_GET["archive"] == "tag") {
  3.         include(TEMPLATEPATH . '/tag_all.php');
  4.         exit;
  5.     } else  if (get_query_var("tag") != "") {
  6.         ultimate_get_posts();
  7.  
  8.         if (is_feed()) {
  9.  
  10.             return;
  11.         }
  12.         if (file_exists(TEMPLATEPATH . "/tag.php")) {
  13.             if ( $_GET["feed"] == '') {
  14.                 include(TEMPLATEPATH . '/tag.php');
  15.                 exit;
  16.             }
  17.         } else {
  18.     //    include(TEMPLATEPATH . '/index.php');
  19.         }
  20.     }
  21. }

This function will process when you query for tag but you will see if you request with empty value of tag it won’t process anything and let Wordpress process this and finally it give 404 errors

I just made some changes to handle when user query with empty tag or open at /tag or /tag/

PHP:
  1. function ultimate_tag_templates() {
  2.     if ($_GET["archive"] == "tag") {
  3.         include(TEMPLATEPATH . '/tag_all.php');
  4.         exit;
  5.     } else  if (get_query_var("tag") != "") {
  6.         ultimate_get_posts();
  7.  
  8.         if (is_feed()) {
  9.  
  10.             return;
  11.         }
  12.         if (file_exists(TEMPLATEPATH . "/tag.php")) {
  13.             if ( $_GET["feed"] == '') {
  14.                 include(TEMPLATEPATH . '/tag.php');
  15.                 exit;
  16.             }
  17.         } else {
  18.     //    include(TEMPLATEPATH . '/index.php');
  19.         } else    {  
  20.         if ( (eregi('^/tag$', $_SERVER['REQUEST_URI']) ) ||
  21. (eregi("^/tag/$", $_SERVER['REQUEST_URI'])) ) {  
  22.         include(TEMPLATEPATH . '/tags.php');
  23.         exit;
  24.         }
  25.     }
  26. }

You may need to have file tags.php in your theme directory

Here is example of tags.php

PHP:
  1. <?php get_header(); ?>
  2.  
  3. <div class="primary">
  4.  
  5.     <?php UTW_ShowWeightedTagSetAlphabetical("coloredsizedtagcloud","",0) ?>
  6.  
  7. </div>
  8.  
  9. <?php get_sidebar(); ?>
  10.  
  11. <?php get_footer(); ?>

You also can change function or parameters to display tag in the page as you want. Please see all detail here UltimateTagWarrior help

Change category name without 404 errors

Friday, February 16th, 2007

Today I try to change category name from anti-virus to virus but using old permalink /%category%/%postname%.html and i found later that there have 404 errors. I try to looking around on search engine but could not find any mod_rewrite to fix this problem. Finally I look into source code of Wordpress 2.1 and change the code below:-

/wp-includes/category.php

Search for line 168

function get_category_by_path($category_path, $full_match = true, $output = OBJECT) {

Add this code after global $wpdb;

$category_path = str_replace('old-category', 'new-category', $category_path);

For above I changed my /wp-includes/category.php to

......
function get_category_by_path($category_path, $full_match = true, $output = OBJECT) {
	global $wpdb;
	$category_path = str_replace('anti-virus', 'virus', $category_path);
......