Creating Licences

<?php

/*  This is a sample PHP script to be used as a guide for integrating
 *  the OffByZero Cobalt with your website and create licences
 *  automatically.
 *
 *  You will need to ensure the cURL extension is installed and configured
 *  to work with PHP. On Ubuntu, this is as simple as:
 *    sudo apt-get install php5-curl
 *
 *  To test out the code below simply add the following
 *  PHP code to your website, adjust the "strLicence", "strAuth" and
 *  "strCobaltServerBaseURI" variables, adjust the licence XML string
 *  to include the users submitted values (for name, address, etc.),
 *  and you are done. If you configure appropriate defaults for
 *  feature and product parameters, then you can skip the respective
 *  XML fields in "strLicence".
 *
 *  The product_id and feature_id values can be found by examining the
 *  appropriate tables in the Cobalt UI. The licence-type value
 *  should be one of the following:
 *
 *  PERIODIC = 0
 *  PURCHASE = 1
 *  DEMO = 2
 *  NONE = 3
 */


$strAuth = "username:password";
$strCobaltServerBaseURI = "https://cobalt.offbyzero.com/";

$strLicence = <<< XML
  <licence>
    <allow_ts_citrix>false</allow_ts_citrix>
    <customer-address><![CDATA[123 New Street, Melbourne, VIC 3000, Australia]]></customer-address>
    <customer-company><![CDATA[BigCorp Pty. Ltd.]]></customer-company>
    <customer-company-division><![CDATA[Finance Division]]></customer-company-division>
    <customer-email><![CDATA[jbloggs@customercompany.com]]></customer-email>
    <customer-name><![CDATA[J. Bloggs]]></customer-name>
    <customer-notes><![CDATA[This is a repeat customer.]]></customer-notes>
    <customer-phone><![CDATA[+61 1 2345 6789]]></customer-phone>
    <is-locked>false</is-locked>
    <is-offline>false</is-offline>
    <is-periodic-days>true</is-periodic-days>
    <is-periodic-startup-count>true</is-periodic-startup-count>
    <licence-features>
      <licence-feature>
        <feature-id>1</feature-id>
        <licence-type>0</licence-type>
        <registered-until>2123-01-02T00:00:00Z</registered-until>
        <value><![CDATA[Gold membership]]></value>
      </licence-feature>
      <licence-feature>
        <feature-id>2</feature-id>
        <licence-type>0</licence-type>
        <registered-until>2123-01-02T00:00:00Z</registered-until>
        <value><![CDATA[Gold membership]]></value>
      </licence-feature>
    </licence-features>
    <max-installed-clients>10</max-installed-clients>
    <periodic-days>7</periodic-days>
    <periodic-startup-count>5</periodic-startup-count>
    <product_id>1</product_id>
    <version>5.0.0.0</version>
  </licence>
XML
;

// Licences can be emailed to the user by the server either based on
// the default template or custom fields.
//$strEmail = '<use-template>true</use-template>';
$strEmail = <<< XML
  <custom-email>
    <from>you@company.com</from>
    <to><![CDATA[jbloggs@customercompany.com]]></to>
    <subject>Your new licence</subject>
    <body>Dear Joe, please find attached your new licence</body>
  </custom-email>
XML
;

// Create a new licence.
$xmlRes = SendPost($strCobaltServerBaseURI.'licences.xml', $strLicence, $strAuth);
if ($xmlRes != NULL)
{
  // Determine the new licence_id from the HttpMessage returned.
  $xmlLicence = new SimpleXMLElement($xmlRes);
  $nLicenceId = $xmlLicence->id;
  echo "New licence created (licence_id: ".$nLicenceId.") ";

  // Email the new licence to the user.
  $xmlRes = SendPost($strCobaltServerBaseURI.'licences/'.$nLicenceId.'/send_email.xml', $strEmail, $strAuth);
  if ($xmlRes != NULL) echo "and emailed to user.\nDone.\n";
}

// SendPost() - helper function to send an HTTP POST and check for errors.
//
function SendPost($strUrl, $strRawPostData, $strAuth)
{
  $xmlRes = NULL;
  $urlNew = curl_init();

  curl_setopt($urlNew, CURLOPT_URL, $strUrl);
  curl_setopt($urlNew, CURLOPT_HTTPHEADER, array('Content-Type: application/xml', 'Expect:'));
  curl_setopt($urlNew, CURLOPT_HEADER, 0);
  curl_setopt($urlNew, CURLOPT_POST, 1);
  curl_setopt($urlNew, CURLOPT_POSTFIELDS, $strRawPostData);
  curl_setopt($urlNew, CURLOPT_USERPWD, $strAuth);
  curl_setopt($urlNew, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  curl_setopt($urlNew, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($urlNew, CURLOPT_RETURNTRANSFER,1);
  $xmlRes = curl_exec($urlNew);
  curl_close($urlNew);

  return $xmlRes;
}
?>