Location: PHPKode > projects > CsWebmail > cswebmail-3.10/cswebmail-3.10/templates/gnupg.php
<?
if(VALID_DOCUMENT != 1) die('what?');

if(!$_SESSION['preferences']->getUseGnuPG()){
  location_header($_SERVER['PHP_SELF'].'?action=news');
}

require_once('simple_gpg.php');

$gpg = new SimpleGPG();

if($action == 'gnupg-generate-key'){
  $key = $gpg->getSecretKeyId();
  if($key)
    Notice::set('You have public/private key already.');
  else{
    $password = get_var('password');
    $password_confirm = get_var('password-confirm');
    if($password != $password_confirm){
      Error::set('Passphrase not confirmed.');
      location_header($_SERVER['PHP_SELF'].'?action=gnupg');
    }
    
    if($password == ''){
      Notice::set('Passphrase is empty!');
    }
    
    $ret = $gpg->generateKey($_SESSION['preferences']->getName(),$password);
    
    if($ret['returnval'] == 0)
      Notice::set('Public/private key created successfully.');
    else{
      Error::set($ret['errors']);
    }
  }
  
  location_header($_SERVER['PHP_SELF'].'?action=gnupg');
}
else if($action == 'gnupg-delete-key'){
  $key = get_var('key');
  $secure_key = $gpg->getSecretKeyId();
  if($key == $secure_key){
    Error::set('You can\'t delete your public/private key.');
  }
  else{
    $key_data = $gpg->getKey($key);
    if(isset($key_data->id)){
      $ret = $gpg->deleteKey($key_data);
      
      if($ret['returnval'] === 0){
        list($key_id,$algo) = $gpg->getKeyInfo($key_data);
        Notice::set('Key '.$key_id.' '.
                    h($key_data->get_email_name().' <'.$key_data->get_email_addr().'>').' removed');
      }
      else{
        Error::set('Error occured');
      }
    }
    else
      Error::set('No such key');
  }
  location_header($_SERVER['PHP_SELF'].'?action=gnupg');
}
else if($action == 'gnupg-import'){
  $data = get_var('data');
  $ret = $gpg->importKey($data);
  if($ret['returnval'] === 0 || array_not_empty($ret['newkeys'])){
    Notice::set(implode("\n",$ret['info']));
    
    $key = $gpg->getKey($ret['newkeys'][0]);
    
    if($key->haveSecret){
      $secure_key = $gpg->getSecretKey();

      /*
      if(!is_test_user()){
         Error::set("You can't export private key.<br/>".
                    "Private key ".$key->id." not imported.");
        $gpg->deleteKey($key,'private');
      } else
      */
      if($secure_key && $secure_key->id != $key->id){
        Error::set("You already have private key ".$secure_key->id.".<br/>".
                   "Private key ".$key->id." not imported.");
        $gpg->deleteKey($key,'private');
      }
      else if($key->get_email_addr() != get_current_user_email()){
        Error::set("Email address in secure key is not yours (".$key->get_email_addr().").<br/>".
                   "Private key not imported.");
        $gpg->deleteKey($key,'private');
      }
      else{
        // check password
        $password = get_var('password');
        $ret = $gpg->verifyPassphrase($key,$password);
        if($ret['verified']){
          Notice::set('Private key passphrase verified.');
          // remember password
          Cache::cacheGPGPassphrase($password);
        }
        else{
          Error::set("Password is wrong.<br/>".
                     "Private key not imported.");
          $gpg->deleteKey($key,'private');
        }
        
      }
    }
  }
  else{
    Error::set(implode("\n",$ret['errors']));
  }
  
  location_header($_SERVER['PHP_SELF'].'?action=gnupg');
}
else if($action == 'gnupg-lookup-import'){
  $username = get_var('username');
  if($username == $GLOBALS['MAIL_USER_NAME']){
    Notice::set('That is you!');
  }
  else if($username != ''){
    $ret = $gpg->importLocalPersonKey($username);
    if($ret['returnval'] === 0){
      Notice::set($ret['info']);
    }
    else{
      Error::set($ret['errors']);
    }
  }
  location_header($_SERVER['PHP_SELF'].'?action=gnupg');
}
else if($action == 'gnupg-send-public-key'){
  $key = $gpg->getSecretKeyId();
  if($key){
    $export = $gpg->exportKey($key_data->id);
    $_SESSION['failed_mail_params'] = array('subject'=>'my public key',
                                            'text'=>$export);
    location_header($_SERVER['PHP_SELF'].'?action=compose');
  }
  else
    location_header($_SERVER['PHP_SELF'].'?action=gnupg');
}
else if($action == 'gnupg-verify-passphrase'){
  if($gpg->loadPassphraseFromRequest())
    Notice::set('Private key passphrase accepted.');
  else
    Error::set('Wrong passphrase.');
  $mailbox = get_var('mailbox');
  if($mailbox != ''){
    $mailkey = get_var('mailkey');
    if($mailkey != ''){
      location_header($_SERVER['PHP_SELF'].'?action=read-mail&mailbox='.$mailbox.'&mailkey='.$mailkey);
    }
    else{
      location_header($_SERVER['PHP_SELF'].'?action=show-folder&mailbox='.$mailbox);
    }
  }

  location_header($_SERVER['PHP_SELF'].'?action=gnupg');
}
else{
?>

<table class="content hsides pad">
<? 
  $key = $gpg->getSecretKeyId();
  if($key){
    $key_data = $gpg->getKey($key);
    $sub_key_data = current($key_data->subkeys);
    $export = $gpg->exportKey($key_data->id);
    list($key_id,$key_algo) = $gpg->getKeyInfo($key_data);
?>

  <tr><th colspan="5" class="title">Your Key (public/private)</th></tr>
  <tr><th>User ID</th><th>Key ID</th><th>Type</th><th>Date</th><th></th></tr>
  <tr>
     <td><?=h($key_data->get_email_name().' <'.$key_data->get_email_addr().'>')?></td>
     <td><?=$key_id?></td>
     <td><?=$key_algo?></td>
     <td><?=h($key_data->date)?></td>
     <td>
     <?if(Cache::haveCachedGPGPassphrase()){?> <span class="green">password cached</span>
     <?}else{?>
<div class="notice">
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<input type="hidden" name="action" value="gnupg-verify-passphrase"/>
Private key passphrase not cached<br/>
Enter passphrase: <input type="password" name="password"/>&nbsp;<input value="ok" type="submit"/>
</form>
</div> 
     <?}?> 
     </td>
  </tr>
  <tr><td colspan="5">
  <a onclick="$('#<?='pub_'.$key_data->id?>').toggle();">show export</a>
  <?=action_link('gnupg-send-public-key','send public key')?>
  <div id="<?='pub_'.$key_data->id?>" class="mono small" style="display:none;border:1px solid black;padding:1em;">
  <?=nl2br($export)?>
  </div>
  </td></tr>

<?}else{?>
  <tr><td colspan="5">
  <div class="error">You have no public/private key !</div>
  </td></tr>
  <tr><td colspan="5">
<script type="text/javascript">
function check_generate_form(form){
  var msg='';
  if(form['password'].value != form['password-confirm'].value){
    alert("Passwords are not same.");
    return false;
  }
  if(form['password'].value == '' &&
     !confirm("You don't want a passphrase - this is probably a *bad* idea!\nAre you sure ?")){
    return false;
  }

  return true;
}
</script> 
  <form action="<?=$_SERVER['PHP_SELF']?>" method="post" onsubmit="return check_generate_form(this);">
  <input type="hidden" name="action" value="gnupg-generate-key"/>
  <table>
  <tr>
     <th>Private key passphrase:</th><td><input type="password" name="password"/></td>
  </tr>
  <tr>
    <th>Confirm passphrase:</th><td><input type="password" name="password-confirm"/></td>
  </tr>
  </table>
  <input type="submit" value="Generate it !"/>
  </form>
  </td></tr>   
  <tr><td colspan="5">
  <div class="notice">Be patient, generation process can take 1-3 minutes.</div>
  </td></tr>   
  <tr><td colspan="5">
  Optionally you can export your existent public/private keys using: <br/>
  <code>gpg -armor --export-secret-keys KEY_ID</code>
  </td></tr> 
<?
  }
$public_keys = $gpg->getPublicKeys();
if(array_not_empty($public_keys)){
?>
  <tr><td colspan="5">&nbsp;</td></tr> 
  <tr><th colspan="5" class="title">Other Keys (public)</th></tr>
  <tr><th>User ID</th><th>Key ID</th><th>Type</th><th>Date</th><th></th></tr>
<?
  foreach($public_keys as $key_data){
    $sub_key_data = current($key_data->subkeys);
    list($key_id,$key_algo) = $gpg->getKeyInfo($key_data);
?>
  <tr>
      <td><?=h($key_data->get_email_name().' <'.$key_data->get_email_addr().'>')?></td>
      <td><?=$key_id?></td>
      <td><?=$key_algo?></td>
      <td><?=h($key_data->date)?></td>
      <td><?=action_confirm_link('gnupg-delete-key','delete key','Are you sure ?','key='.urle($key_data->id))?></td>
  </tr>
<?
}}
?>
</table>
<br/> 
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<input type="hidden" name="action" value="gnupg-import"/>
<table class="content hsides w50">
  <tr><th colspan="2" class="title">Import key[s]</th></tr>
  <tr><td>
  Paste key[s] data into text area:<br/>
  <textarea name="data" cols="64" rows="7" class="w100"></textarea>
  </td></tr>

  <tr><td class="notice">
  If data is private key and encrypted by passphrase,<br/>enter key passphrase here:
  <input type="password" name="password"/><br/>
  </td></tr>
  <tr><td><input type="submit" value="import keys"/></td></tr>

</table>
</form>
<br/>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<input type="hidden" name="action" value="gnupg-lookup-import"/>
<table class="content hsides w50">
  <tr><th colspan="2" class="title">Lookup &amp; import public keys in <?=MAILSUFFIX?> domain</th></tr>
  <tr><td class="notice">
  If someone have registered public/private key in <?=VERSION?>,<br/>
  you can imprort his/her public key into your keys ring
  </td></tr>
  <tr><td>
  Person username: <input type="text" name="username"/><b>@<?=MAILSUFFIX?></b>
  </td></tr>
  <tr><td><input type="submit" value="lookup and import"/></td></tr>
</table>
</form>

<p>
<ul>
<li> When you send encrypted email, GnuPG will use public key IFF email addresses are same.
<li> If somebody use <?=VERSION?> and have created public key, his/her public key will
     imported to your keys ring automatically when encrypted email sent.
<li> More info about GnuPG : <a href="http://en.wikipedia.org/wiki/GnuPG">http://en.wikipedia.org/wiki/GnuPG</a>
</ul>
</p>

<? 

}

?>
Return current item: CsWebmail