PX : code

PGP class by Matt DeLong
Download this code


<?php

  
class PGP {

    function 
PGP(){
        
// ** change to correct path
        
putenv("PGPPATH=/usr/home/sites/.pgp\");
    }


    function returnError($msg){
            print \"<br>\\n\";
            print \"<b>ERROR</b><br>\\n\";
            print $msg.\".<br>\\n\";

            exit;
    }
    

    function cleanTxt($string){
        return str_replace(\"\\\"\", \"\'\'\", stripslashes($string));
    }


    function returnPaths(){
        $tmpToken = md5(uniqid(rand()));
        
        // ** change to correct path
        $paths[plainTxt] = \"/usr/home/sites/secure/tmp/\".$tmpToken.\".data\";
        $paths[cryptTxt] = \"/usr/home/sites/secure/tmp/\".$tmpToken.\".data.asc\";        
    
        return $paths;
    }

    
     function sendMsg($to_array, $subj, $msg, $from){
         foreach($to_array as $email => $name){
            $paths = $this->returnPaths();
            
            // write / create the plain text file
            $this->writeFile($paths[plainTxt], $this->cleanTxt($msg));
            
            // create encrypted version
            $this->writeAscFile($paths[plainTxt], $paths[cryptTxt], $email, $name);
            
            // read encrypted version
            $msg = $this->readFile($paths[cryptTxt]);
        
            // send message
            $this->sendEncryptedMsg(\"$name <$email>\", $subj, $msg, $from);
            
            // delete tmp files
            $this->deleteFiles($paths);
        }
     }
 
  
     function writeFile($plainTxt, $msg){
         !$plainTxt ? $this->returnError(\"no \\$plainTxt on call to writeFile()\") : \"\";
         !$msg ? $this->returnError(\"no \\$msg on call to writeFile\") : \"\";

        if(!($fp = @fopen($plainTxt, \"a+\"))){
            $this->returnError(\"unable to create the file located at $plainTxt\");

        } else {
            fputs($fp, $this->cleanTxt($msg));
            @fclose($fp);
        }

     }
     
     
     function writeAscFile($plainTxt, $cryptTxt, $email, $name){
         !$plainTxt ? $this->returnError(\"no \\$plainTxt on call to writeAscFile()\") : \"\";
         !$cryptTxt ? $this->returnError(\"no \\$cryptTxt on call to writeAscFile()\") : \"\";
         !$email ? $this->returnError(\"no \\$email on call to writeAscFile()\") : \"\";
         !$name ? $this->returnError(\"no \\$name on call to writeAscFile()\") : \"\";

        @exec(\"pgp -e $plainTxt \'$name <$email>\'\");
        
        if(!(file_exists($cryptTxt))){
            $this->returnError(\"unable to create file: $cryptTxt\");
        }
     }
     
     
     function readFile($cryptTxt){
         !$cryptTxt ? $this->returnError(\"no \\$cryptTxt on call to readFile()\") : \"\";

        if(!($fp = @fopen($cryptTxt, \"r\"))){
            $this->returnError(\"unable to read file: $cryptTxt\");

        } else {
            $data = fread($fp, filesize($cryptTxt));
            @fclose($fp);

        }
        
        return $data;     
     }

     
     function deleteFiles($paths){
         @unlink($paths[plainTxt]);
         @unlink($paths[cryptTxt]);
     }
     
     
     function sendEncryptedMsg($to, $subj, $msg, $from){         
         @mail($to, $subj, $msg, \"From: $from\\nX-Priority: 1\\nX-MSMail-Priority: High\\nImportance: High\");
     }
 
 }
 
 
?>



// you MUST have a public/private key pair to encrypt & decrypt messages
// you MUST have a public key for each recipient of the email
// you MUST have the PGP command line ebusiness server
// you MUST have PGP client software for each recipient of the email
// http://www.pgp.com
//
// EXAMPLE OF USE
// include(\'pgp.class\');
// 
// $pgp = new PGP;
// 
// # change this to your PGP public key username for your public key ring
// #$to_array = array(\"matt.delong@digisoft77.com\" => \"Matt DeLong\");
// $pgp->sendMsg($to_array, \"PGP TEST MESSAGE\", \"message here\", strtolower($email));
//
//

Comments or questions?
PX is running PHP 5.2.17
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.