home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / admin / gpg.inc < prev    next >
Encoding:
Text File  |  2003-03-29  |  3.3 KB  |  112 lines

  1. <?php
  2. /////////////////////////////////////////////////////////
  3. //    
  4. //    include/gpg.inc
  5. //
  6. //    (C)Copyright 2003 Ryo Chijiiwa <Ryo@IlohaMail.org>
  7. //
  8. //    This file is part of IlohaMail, and released under GPL.
  9. //    See COPYING, or http://www.fsf.org/copyleft/gpl.html
  10. //
  11. /////////////////////////////////////////////////////////
  12. /********************************************************
  13.     PURPOSE: GPG interface
  14.     COMMENTS:  Based on code contributed by Paul A. Martin
  15.  
  16. ********************************************************/
  17.  
  18. function gpg_list_keys(){
  19.     global $GPG_HOME_STR, $GPG_PATH;
  20.     global $loginID, $host;
  21.     
  22.     $gpg_home = str_replace("%h", $host, str_replace("%u", $loginID, $GPG_HOME_STR));
  23.     $gpgkeys=`"$GPG_PATH" --home="$gpg_home" --list-public-keys`;
  24.     preg_match_all("/pub\s+[\w\/]+\s+[\w-]+\s+([\w ]+).*<([\w@.]+)>/", $gpgkeys, $works);
  25.     
  26.     $result = array();
  27.     for($i=0; $works[1][$i] != ""; $i++){
  28.         $key = $works[1][$i];
  29.         $str = $works[1][$i]." <".$works[2][$i].">";
  30.         $result[$key] = $str;
  31.     }
  32.     
  33.     return $result;
  34. }
  35.  
  36. function gpg_export($person){
  37.     global $loginID, $host;
  38.     global $GPG_HOME_STR, $GPG_PATH;
  39.  
  40.     $person = escapeshellcmd(stripslashes($person));
  41.     $gpg_home = str_replace("%h", $host, str_replace("%u", $loginID, $GPG_HOME_STR));
  42.     $command = $GPG_PATH." --home=".$gpg_home." --export -a \"$person\"";
  43.     $temp = exec($command, $result, $errorno);
  44.     return implode("\n", $result);
  45. }
  46.  
  47. function gpg_encrypt($loginID, $host, $gpgrecp, &$gpgmessage){
  48.     global $GPG_HOME_STR, $GPG_PATH;
  49.     
  50.     $original_message = $gpgmessage;
  51.     
  52.     if($gpgrecp!="noencode")
  53.     {
  54.         //disable command injection
  55.         $gpgmessage = str_replace("`", "\\`", $gpgmessage);
  56.     
  57.         //format home directory path
  58.         $gpg_home = str_replace("%h", $host, str_replace("%u", $loginID, $GPG_HOME_STR));
  59.         $gpg_home = realpath($gpg_home);
  60.         
  61.         //encrypt
  62.         $tempcom = 'echo "'.$gpgmessage.'" | '.$GPG_PATH.' --home='.$gpg_home.' -a --always-trust --batch -e -r "'.$gpgrecp.'"';
  63.         echo $tempcom."<br>\n";
  64.         $oldhome = getEnv("HOME");
  65.         $msg = exec($tempcom, $encrypted, $errorcode);
  66.         echo "msg: $msg <br>\n";
  67.         echo "errorcode: $errorcode <br>\n";
  68.         $gpgmessage = implode("\n", $encrypted);
  69.         echo "New message: <pre>$gpgmessage</pre> <br>\n";
  70.         $gpg_encrypted = true;
  71.         if ($errorcode!=0){
  72.             $gpgmessage = $original_message;
  73.             return false;
  74.         }else{
  75.             return true;
  76.         }
  77.     }
  78.     return false;
  79. }
  80.  
  81. function gpg_decrypt($gpg_passphrase, &$body){
  82.     global $GPG_HOME_STR, $GPG_PATH;
  83.     global $loginID, $host, $user;
  84.  
  85.     //$oldhome = getEnv("HOME");
  86.     //$blah = nl2br($body);
  87.     $original = $body;
  88.     $gpg_home = str_replace("%h", $host, str_replace("%u", $loginID, $GPG_HOME_STR));
  89.     $temp_file = $gpg_home."/$user-gpg.tmp";
  90.     $fp = fopen($temp_file,'w');
  91.     //$fp = fopen("/home/$loginID/.gnupg/blah",'w');
  92.     if ($fp){
  93.         fwrite($fp, $body, strlen($body));
  94.         fclose($fp);
  95.         
  96.         $temp = 'echo "'.escapeshellcmd($gpg_passphrase).'" | '.$GPG_PATH.' --home='.$gpg_home.' -v --batch --passphrase-fd 0 --decrypt '.escapeshellcmd($temp_file);
  97.         $blah = exec($temp, $body, $errorcode);
  98.         
  99.         if ($errorcode==0){
  100.             $body = implode("\n", $body);
  101.             $body = stripslashes($body);
  102.         }else{
  103.             $body = "gpg_decrypt: Decryption failed... (errorno: $errorcode)\n\n".$original;
  104.         }
  105.         unlink($temp_file);
  106.         //unlink("/home/$loginID/.gnupg/$fp");
  107.     }else{
  108.         $body =  "gpg_decrypt: Couldn't open temp file: $temp_file\n\n".$original;
  109.     }
  110. }
  111.  
  112. ?>