aRo` automating your e-commerce

This blog has been neglected due to the success of Solidshops.com
27Mar/10

paypal ipn postback on a custom port.

When we were developping the paypal IPN script for our ecommerce project solidshops.com, we ran into a strange limition.

Apparently paypal does not allow you to post back to a url with a custom port. Only http(80) and https(443) are allowed.

We solved it by using the paypal ipn proxy below. You should put this script on a different server and it will post all the IPN data to your application on port 8080.

The $url should be changed to the webapplication(on custom port) where the paypal data is processed.

PHP:
  1. $url = 'http://www.yourdomain.com:8080/ipnscript.php';
  2.        
  3. $fields = $_POST;
  4. if (count ( $fields )> 0) {
  5.            
  6.     //url-ify the data for the POST
  7.     foreach ( $fields as $key => $value ) {
  8.         $fields_string .= $key . '=' . $value . '&';
  9.     }
  10.     rtrim ( $fields_string, '&' );
  11.            
  12.     //open connection
  13.     $ch = curl_init ();
  14.            
  15.     //set the url, number of POST vars, POST data
  16.     curl_setopt ( $ch, CURLOPT_URL, $url );
  17.     curl_setopt ( $ch, CURLOPT_POST, count ( $fields ) );
  18.     curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields_string );
  19.            
  20.     //execute post
  21.     $result = curl_exec ( $ch );
  22.            
  23.     //close connection
  24.     curl_close ( $ch );
  25. }

Popularity: 2% [?]

Filed under: php No Comments
20Mar/08

phpHackChecker: sends you a report of all changed files on your server

This php script will send you a mail with all the files that have been changed within an interval you specify.

I created this script because someone hacked into my shared hosting account and added some hidden spammy link into my templates. And since most shared hosting accounts do not have any kind of logfiles available, it is almost impossible for you to find out how the hacker got in. I could even be, the hacker got acces to your files by hacking another shared hosting account on that server.

With this script you will at least be notified when someone has changed the files on your server.

settings:

PHP:
  1. $from="name@domain.com";
  2. $to="name@domain.com";
  3. $subject="changes on webhosting";
  4. $domain = "yourdomain.com";
  5.  
  6. $settings = array( array(
  7.                 wwwname => 'htdocs1',
  8.                 wwwdirectory => '/mounted-storage/home/website1.com'
  9.             ),
  10.             array(
  11.                 wwwname => 'htdocs2',
  12.                 wwwdirectory => '/mounted-storage/home/website2.com'
  13.             )
  14.          );

functions

PHP:
  1. function url_remove_lastslash($url){   
  2.     if (substr($url,strlen($url)-1,1)=="/"){
  3.         return substr($url,0,strlen($url)-1);
  4.     }else{
  5.         return $url;
  6.     }
  7. }
  8.  
  9. $resultsarray();
  10. function getfiles($path_start) {
  11.     global $results;
  12.     $dir = @opendir($path_start);
  13.     while (false!=($file = @readdir($dir))) {
  14.         if ($file != "." && $file != "..") {
  15.        
  16.             $file_path = url_remove_lastslash($path_start) . "/"$file;
  17.             if(is_dir($file_path)){
  18.                 getfiles ($file_path);
  19.             }
  20.             $date_mod = filemtime($file_path); //get last modified date of file
  21.             if(trim($date_mod) <> ""){
  22.                            
  23.                 $date_mod = date("Y-m-d", $date_mod);         
  24.                 $yr = substr($date_mod,0,4); //display 'new' image if item not-older than 7days
  25.                 $mnth = substr($date_mod,5,2);
  26.                 $dy = substr($date_mod,8,2);
  27.                 $newday = date("Y-m-d", mktime(0,0,0,$mnth,$dy + 2,$yr));
  28.                 $now = date("Y-m-d");
  29.                 //echo $yr .$mnth . $dy."<br>";
  30.                
  31.                     if (($now <= $newday)  ) {
  32.                         //echo $file_path . " " . $date_mod  . " " . $now  . " " . $newday. $yr .$mnth . $dy."<br>";
  33.                         $results[$file_path][0] = $file_path ;
  34.                         $results[$file_path][1]$date_mod;
  35.                     }
  36.             }
  37.  
  38.         }
  39.  
  40.     }
  41. }

check all hosting accounts & create mailbody

PHP:
  1. foreach ($settings as $setting){
  2.     $resultsarray();
  3.     $date_change = "";
  4.     $filename= "";
  5.    
  6.     getfiles($setting['wwwdirectory']);
  7.    
  8.     $mail_body .=  "<b>".$setting['wwwname']."</b><br/>";
  9.    
  10.     //sort by change date
  11.     foreach ($results as $key => $row) {
  12.         $date_change[$key]  = $row[0];
  13.         $filename[$key] = $row[1];
  14.        
  15.     }
  16.    
  17.     //array_multisort($filename, SORT_DESC, $date_change, SORT_ASC, $results);
  18.    
  19.     foreach ($results as $result){
  20.         $mail_body .=  $result[1] . ": " . str_replace($setting['wwwdirectory'],"",$result[0])."<br/>";
  21.     }
  22.    
  23. }

mail the changed files

PHP:
  1. $headers="";
  2.    $headers .= "X-Sender:  $from <$from>\n"; //
  3.    $headers .="From: $from <$from>\n";
  4.    $headers .= "Reply-To: $from <$from>\n";
  5.    $headers .= "Date: ".date("r")."\n";
  6.    $headers .= "Message-ID: <".date("YmdHis")."$from>\n";
  7.    $headers .= "Subject: $subject\n";
  8.    $headers .= "Return-Path: $from <$from>\n";
  9.    $headers .= "Delivered-to: $from <$from>\n";
  10.    $headers .= "MIME-Version: 1.0\n";
  11.    $headers .= "Content-type: text/html;charset=ISO-8859-9\n";
  12.    $headers .= "X-MSMail-Priority: Normal\n";
  13.    $headers .= "X-Mailer: Mail send from ".$domain."!\n";
  14.  
  15.    //mail to customer
  16.    if(mail($to,$subject,$mail_body,$headers)){
  17.      echo "job executed succesfully";
  18.    }else{
  19.         echo "error executing job";
  20.    }

You can download a copy of phpHackChecker here. Change the file extension to ".php".

Popularity: 9% [?]

Filed under: php, tools No Comments
17Feb/08

capture the encoding from the main XML file tag

To capture the encoding from the XML tag of the file. We need to read the first line, and pass it to the function to capture the encoding value.

The first line looks like this:

< ?xml version="1.0" encoding="utf-8"? >

The PHP code:

PHP:
  1. function readfirstline($file){
  2.    $fp = @fopen($file, "r");
  3.    $firstline = fgets($fp);
  4.    fclose($fp);
  5.    return $firstline;
  6. } 
  7. function get_attr(  $line ,$attr) {
  8.   $start = strpos($line,"$attr");
  9.   $linesubstr($line,$start);
  10.   $arr_enc = split("\"",$line);
  11.  
  12.   return $arr_enc [1];
  13. }
  14.  
  15. $path = "home/user/feeds/id.xml";
  16. $line= readfirstline($path);
  17.        
  18. echo get_attr(  $line ,"encoding");

Popularity: 4% [?]

Filed under: php, xml No Comments
1Feb/08

hot or not wordpress plugin

This wordpress plugins adds a radio button to the comment form so users can tell if a post is hot or not. Depending on the value that is chosen, another css class is added to comment .

  1. Download the plugin
  2. Download the Hot Or Not plugin here
    Unzip this file into the /wp- content/plugins/ folder.

  3. Adding the radionbutton group to the comment form.
  4. This radiobutton has to be added in the < form > tag of the comments. You can find this somewere at the bottom of your "template/comments.php" file. It looks something like this.

    HTML:
    1. <form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform">

    You have to paste the code below in that form.

    HTML:
    1.   <input name="hon_group" type="radio" /> Yes
    2.   <input name="hon_group" CHECKED="true" type="radio" /> no
    3.  </p>

  5. Dynamically add the class to the comment
  6. In the beginning of that same file, there is a php foreach statement to print all comments.

    HTML:
    1. <?php foreach ($comments as $comment) ?>

    A bit further in the file you have to add the hotornot class to the listitem:

    HTML:
    1. li id="comment-<?php comment_ID() ?>" class="<?php print $comment->css ?>">

  7. Activate the plugin
  8. Active the plugin in the admin "Plugins" screen in wordpress.

  9. Change the css style of your comments
  10. You can do this in the template/style.css file.

    .vote_hot {style.css (line 692)
    background-color:#CDEEAB;
    border-top:1px dotted #006600;
    color:#006600;
    }

    .vote_not {style.css (line 699)
    background-color:#FFE0E0;
    border-top:1px dotted #F5857E;
    color:#AB0000;
    }

Popularity: 28% [?]

Filed under: php, software 7 Comments
13Nov/07

chmod under safemode

When you would try to change the filepermission with a php script on your hosting, you would probably run into an "Operation not permitted" error.

This is because save mode is enabled. To bypass this limitation you could login unter ftp and change the file permission from there.

PHP:
  1. //settings
  2. $folders = array("/feeds/","/sitemap/");
  3. $ftp_details['ftp_user_name'] = "username";
  4. $ftp_details['ftp_user_pass'] = "pw";
  5. $ftp_details['ftp_root'] = '/defaultfolder/';
  6. $ftp_details['ftp_server'] = 'ftp.domain.com';
  7.  
  8. //function to login and change by FTP
  9. function chmod_custom($path, $mod, $ftp_details)
  10. {
  11. // extract ftp details (array keys as variable names)
  12. extract ($ftp_details);
  13.  
  14. // set up basic connection
  15. $conn_id = ftp_connect($ftp_server);
  16.  
  17. // login with username and password
  18. $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
  19.  
  20. // try to chmod $path directory
  21. if (ftp_site($conn_id, 'CHMOD '.$mod.' '.$ftp_root.$path) !== false) {
  22. $success=TRUE;
  23. }
  24. else {
  25. $success=FALSE;
  26. }
  27.  
  28. // close the connection
  29. ftp_close($conn_id);
  30. return $success;
  31. }
  32.  
  33. //the actual transaction
  34. foreach($folders as $folder){
  35. chmod_custom($folder, "0777", $ftp_details);
  36.  
  37. }

I found this solution on php.net.

Popularity: 100% [?]

Filed under: php No Comments