<?php
//ini_set( 'error_reporting', E_ALL );
//ini_set( 'display_errors', true );
defined("_VALID_ACCESS") || die('Direct access forbidden'); // - security feature
require_once 'modules/Custom/XeroConnect/xero.php';
require_once 'modules/Custom/XeroConnect/utils.php';
function create_identity_cer(
$countryName, $stateOrProvinceName, $localityName, $organizationName,
$organizationalUnitName, $commonName, $emailAddress, $foafLocation)
{
// Create the DN array for the openssl function calls
if ($countryName)
$dn = array("countryName" => $countryName);
if ($stateOrProvinceName)
{
if ($dn)
$dn = array_merge($dn,
array("stateOrProvinceName" => $stateOrProvinceName));
else
$dn = array("stateOrProvinceName" => $stateOrProvinceName);
}
if ($localityName)
{
if ($dn)
$dn = array_merge($dn, array("localityName" => $localityName));
else
$dn = array("localityName" => $localityName);
}
if ($organizationName)
{
if ($dn)
$dn = array_merge($dn, array("organizationName" => $organizationName));
else
$dn = array("organizationName" => $organizationName);
}
if ($organizationalUnitName)
{
if ($dn)
$dn = array_merge($dn,
array("organizationalUnitName" => $organizationalUnitName));
else
$dn = array("organizationalUnitName" => $organizationalUnitName);
}
if ($commonName)
{
if ($dn)
$dn = array_merge($dn, array("commonName" => $commonName));
else
$dn = array("commonName" => $commonName);
}
if ($emailAddress)
{
if ($dn)
$dn = array_merge($dn, array("emailAddress" => $emailAddress));
else
$dn = array("emailAddress" => $emailAddress);
}
// if the $dn array is NULL at this point set country name to the default of GB
if (!$dn)
$dn = array("countryName" => "GB");
// Setup the contents of the subjectAltName
if ($foafLocation)
$SAN="URI:$foafLocation";
if ($emailAddress)
{
if ($SAN)
$SAN.=",email:$emailAddress";
else
$SAN="email:$emailAddress";
}
// Export the subjectAltName to be picked up by the openssl.cnf file
if ($SAN)
{
putenv("SAN=$SAN");
}
// Create the array to hold the configuration options for the openssl function calls
// You may need to change the slash "\" (for windows) to "/" for linux
$config = array('config'=>dirname(__file__) . '\\openssl.cnf');
if ($SAN)
{
// TODO - This should be more easily configured
//$config = array_merge($config, array('x509_extensions' => 'usr_cert'));
}
// Generate a new private (and public) key pair
$privkey = openssl_pkey_new($config);
if ($privkey==FALSE)
{
// Show any errors that occurred here
while (($e = openssl_error_string()) !== false)
{
echo $e . "\n";
print "<br><br>";
}
}
// Generate a certificate signing request
$csr = openssl_csr_new($dn, $privkey, $config);
if (!$csr)
{
// Show any errors that occurred here
while (($e = openssl_error_string()) !== false)
{
echo $e . "\n";
print "<br><br>";
}
}
// You will usually want to create a self-signed certificate at this
// point until your CA fulfills your request.
// This creates a self-signed cert that is valid for 365 days
$sscert = openssl_csr_sign($csr, null, $privkey, 365, $config);
if ($sscert==FALSE)
{
// Show any errors that occurred here
while (($e = openssl_error_string()) !== false)
{
echo $e . "\n";
print "<br><br>";
}
}
if (openssl_x509_export($sscert, $certout)==false)
{
// Show any errors that occurred here
while (($e = openssl_error_string()) !== false)
{
echo $e . "\n";
print "<br><br>";
}
}
$pkout = "";
if (openssl_pkey_export($privkey, $pkout,"test123", $config)==FALSE)
{
// Show any errors that occurred here
while (($e = openssl_error_string()) !== false)
{
echo $e . "\n";
print "<br><br>";
}
}
// return "test 5";
// if (openssl_sign($certout, $signature, $privkey) == false) {
// print "An error occurred while signing<br/>";
// };
return array("cer" => $certout, "pem" => $pkout, "signature" => null);
}
class Custom_XeroConnect extends Module { // - notice how the class name represents its path
public function body() { // - modules main code
//print (EPESI_LOCAL_DIR );
$local = $this->get_data_dir();
$privkey_path = Custom_XeroConnect::join_paths(EPESI_LOCAL_DIR, $local,'printec_keys.pem');
if (!file_exists($privkey_path)) {
echo "creating new keys";
$this->create_x509_keys();
}
$form = $this->init_module('Libs/QuickForm');
$form->addElement('header', 'title', __('Xero Connect'));
$form->addElement('text','consumer_key',__('Consumer Key'),array('style'=>'width:250px'));
$form->addElement('text','secret_key',__('Consumer Secret'), array('style'=>'width:250px'));
$form->addElement('submit', 'save_btn', __('Save'), $form->get_submit_form_href());
$form->addElement('header', 'subtitle1', __('X509 Certificate'));
$form->addElement('textarea', 'key_txt', null, array('readonly'=>true, 'style'=>'margin-top:5px;height:100px;width:250px'));
$msg = '<div style="width:250px">Copy and paste this certificate to your Xero Dashboard to generate your consumer and secret keys.</div>';
$form->addElement('static', 'static_text_1', '', $msg);
$form->addElement('button', 'cert_btn', __('Generate New Certificate'), $form->get_submit_form_href());
// Set Initial values
$pubkey = $this->get_x509_public_key();
if ($pubkey) {
$form->setDefaults(array('key_txt' => $pubkey));
}
$consumer_key = Variable::get('xc_consumer_key','');
$form->setDefaults(array('consumer_key' => $consumer_key));
$secret_key = Variable::get('xc_secret_key','');
$form->setDefaults(array('secret_key' => $secret_key));
// end set initial values
if ($form->validate()) {
$values = $form->exportValues();
$local = $this->get_data_dir();
if (array_key_exists('save_btn', $values)) {
$consumer_key = $values['consumer_key'];
$secret_key = $values['secret_key'];
Variable::set('xc_consumer_key', $consumer_key);
Variable::set('xc_secret_key', $secret_key);
echo "saved keys";
}
}
if ($consumer_key && $secret_key) {
echo "do stuff here";
$data_dir = Utils::join_paths(EPESI_LOCAL_DIR, $this->get_data_dir());
$obj = new XeroConnect($data_dir, $consumer_key, $secret_key);
//xero($consumer_key, $secret_key)
}
$form->display();
}
private function create_x509_keys() {
//require_once 'modules/Custom/XeroConnect/x509.php';
$foafLocation = "printec_keys";
$countryName = "NZ";
$stateOrProvinceName = "Hauraki";
$localityName = "Waihi";
$organizationName = "Printec";
$organizationalUnitName = "PR";
$commonName = "Printec";
$emailAddress = "glenn@printec.co.nz";
$p12Password = "silver1";
// Create a cer encoded SSL certificate
if ( $data = create_identity_cer(
$countryName, $stateOrProvinceName, $localityName, $organizationName,
$organizationalUnitName, $commonName, $emailAddress,
$foafLocation ) )
{
// // Create files
$local = $this->get_data_dir();
file_put_contents(Utils::join_paths(EPESI_LOCAL_DIR, $local, $foafLocation.".cer"), $data["cer"]);
file_put_contents(Utils::join_paths(EPESI_LOCAL_DIR, $local, $foafLocation.".pem"), $data["pem"]);
// file_put_contents(Custom_XeroConnect::join_paths(EPESI_LOCAL_DIR, $local, $foafLocation."_signature.txt"), $data["signature"]);
}
}
public function get_x509_public_key() {
$pubkey = Utils::join_paths(EPESI_LOCAL_DIR,$this->get_data_dir(),'printec_keys.cer');
if (file_exists($pubkey)) {
if ($contents = file_get_contents($pubkey)) {
return $contents;
}
}
return null;
}
}
?>