93 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			93 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
global $db;
 | 
						|
 | 
						|
/// Send Error To Client
 | 
						|
/** @return error message from server to client
 | 
						|
 | 
						|
    Function calls exit to terminate.
 | 
						|
 | 
						|
    Message format is json:
 | 
						|
    @code
 | 
						|
    {
 | 
						|
      success: false,
 | 
						|
      txt:     'error message string';
 | 
						|
    }
 | 
						|
    @endcode */
 | 
						|
function error($txt) {
 | 
						|
  error_log("**** ERROR: ".$txt);
 | 
						|
  if (isset($db) && is_object($db)) {
 | 
						|
    error_log("**** DATABASE ERROR: ".$db->error);
 | 
						|
  }
 | 
						|
  echo json_encode(array('success' => false, 'txt' => $txt));
 | 
						|
  exit;
 | 
						|
}
 | 
						|
 | 
						|
/// Send Success To Client
 | 
						|
/** @return success message from server to client
 | 
						|
 | 
						|
    Function calls exit to terminate.
 | 
						|
 | 
						|
    Message format is json:
 | 
						|
    @code
 | 
						|
    {
 | 
						|
      success: true,
 | 
						|
      txt:     'success message string';
 | 
						|
    }
 | 
						|
    @endcode */
 | 
						|
function success($txt) {
 | 
						|
  echo json_encode(array('success' => true, 'txt' => $txt));
 | 
						|
  exit;
 | 
						|
}
 | 
						|
 | 
						|
function getoption($name, $default) {
 | 
						|
  if (!isset($OPTION[$name])) {
 | 
						|
    $q = $db->query("select value from options where name='$name';");
 | 
						|
    if ($q->num_rows==1) {
 | 
						|
      $OPTION[$name]=$q->fetch_row()[0];
 | 
						|
    } else {
 | 
						|
      if (isset($_SERVER[$name]))
 | 
						|
        $OPTION[$name]=$_SERVER[$name];
 | 
						|
      else
 | 
						|
        $OPTION[$name]=$default;
 | 
						|
      $q = $db->query("insert into options (name, value) values ('$name', '$value');");
 | 
						|
    }
 | 
						|
  }
 | 
						|
  return $OPTION[$name];
 | 
						|
}
 | 
						|
 | 
						|
/// Create user safechat as server's identity
 | 
						|
/** Server has reserved username @c safechat */
 | 
						|
function createSafechatUser() {
 | 
						|
  $q = $db->query("select pubkey from user where name='safechat';");
 | 
						|
  if ($q->num_rows!=1 && $user=="safechat") {  
 | 
						|
    $KEY_LENGTH=getoption("KEYLEN", "4096");
 | 
						|
    $SAFECHAT_NAME=getoption("NAME", "Safe Chat");
 | 
						|
    $SAFECHAT_COMMENT=getoption("COMMENT", "https://safechat.ch");
 | 
						|
    $SAFECHAT_EMAIL=getoption("EMAIL", "server@safechat.ch");
 | 
						|
    $PASSWORD=getoption("PASSWORD", "s3Cr37");
 | 
						|
    $create_key_cmd=<<<EOT
 | 
						|
      gpg -v -v --gen-key --batch <<EOF
 | 
						|
Key-Type: RSA
 | 
						|
Key-Length: 4096
 | 
						|
Subkey-Type: RSA
 | 
						|
Subkey-Length: 4096
 | 
						|
Name-Real: ${SAFECHAT_NAME}
 | 
						|
Name-Comment: ${SAFECHAT_COMMENT}
 | 
						|
Name-Email: ${SAFECHAT_EMAIL}
 | 
						|
Expire-Date: 0
 | 
						|
Passphrase: ${PASSWORD}
 | 
						|
%echo generating key for ${SAFECHAT_NAME} ...
 | 
						|
%commit
 | 
						|
%echo done.
 | 
						|
EOF
 | 
						|
EOT;
 | 
						|
    system($create_key_cmd);
 | 
						|
    gnupg_setarmor($pgp, 1);
 | 
						|
    $export = gnupg_export($pgp, ${SAFECHAT_NAME});
 | 
						|
    $q = $db->query("insert into user (name, pubkey) values ('safechat', '$export');");
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
?>
 |