title: App works in php 5.3 but not 5.4+
body: <p>We're running a server app to activate our software, the web app and database itself works fine in php 5.4+ but the activation within the software causes an internal server error if we're running anything higher than php 5.3 (test with 5.4, 5.5 and 5.6) - i'd like to install 5.6 to be future proof and up to date on the server.</p>
  
  <p>I guess the problem lies within the activation php file, can anyone spot any code here which wouldn' tbe compatible with php54+</p>
  
  <pre><code>&lt;?PHP
  
  ini_set('display_errors',1);
  error_reporting(E_ALL);
  
  require 'includes/master.inc.php';
  
  $post = trim(file_get_contents('php://input'));
  //$post = base64_decode($post);
  //$dict = json_decode($post);
  
  $a = new Activation();
  $a-&gt;app_id        = 373; // $dict-&gt;app_id;
  $a-&gt;name          = $_REQUEST["nameOrEmail"]; //$dict-&gt;email;
  $a-&gt;serial_number = $_REQUEST["activationCode"]; //$dict-&gt;serial;
  $a-&gt;guid        = $_REQUEST["guid"];
  $a-&gt;dt            = dater();
  $a-&gt;ip            = $_REQUEST["ip_overwrite"];
  if($a-&gt;ip == "")  $a-&gt;ip = $_SERVER['REMOTE_ADDR'];
  
  if($a-&gt;serial_number == null || $a-&gt;guid == null) {
      header("HTTP/1.0 400 Activation Code or more arguments required");
      exit();
  }
  $a-&gt;insert();
  
  $app = new Application($a-&gt;app_id);
  if(!$app-&gt;ok()) {
      header("HTTP/1.0 412 Invalid Application");
      exit();
  }
  $o = new Order();
  $o-&gt;select($a-&gt;serial_number, 'serial_number');
  if(!$o-&gt;ok()) {
      header("HTTP/1.0 406 Invalid activation code");
      exit();
  }
  
  // Because we die before the activation is updated with the found order id,
  // this has the added benefit of highlighting the activation as "fraudulent"
  // in the activations list. It's not fraudulent obviously, but it does let
  // us quickly see if deactivated licenses are still being used.
  if($o-&gt;deactivated == 1) {
      header("HTTP/1.0 409 Activation Code is disabled.");
      exit();
  }
  
  if($o-&gt;license_name != null &amp;&amp; $o-&gt;license_name != $a-&gt;guid)  {
      header("HTTP/1.0 410 Activation Code has been already claimed.");
      exit();
  }
  $a-&gt;order_id = $o-&gt;id;
  $a-&gt;update();
  
  //$o-&gt;downloadLicense();
  
  
  //header("Cache-Control: public");
  //header("Cache-Control: no-store, no-cache, must-revalidate");
  //header("Cache-Control: post-check=0, pre-check=0", false);
  //header("Pragma: no-cache");
  //header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  //header("Content-Type: application/x-download"); // Stupid fix for Safari not honoring content-disposition
  //header("Content-Length: " . strlen($this-&gt;order-&gt;license));
  //header("Content-Disposition: attachment; filename={$this-&gt;application-&gt;license_filename}");
  //header("Content-Transfer-Encoding: binary");
  //echo $this-&gt;order-&gt;license;
  //exit;
  
  /**
    * hex2bin
    * Converts a hexadecimal string to binary
    * @param string Hex string
    * @return string Binary string
    */
  function hex2bin($hex) {
  if (strlen($hex) % 2)
      $hex = "0".$hex;
  $bin = '';
  for ($i = 0; $i &lt; strlen($hex); $i += 2) { 
      $bin .= chr(hexdec(substr($hex, $i, 2))); 
  }
  
     return $bin; 
  } 
  
  /**
    * dec2hex
    * Converts a decimal string to a hexadecimal string
    * @param string Decimal string
    * @return string Hex string
    */
  function dec2hex($number)
  {
  $hexvalues = array('0','1','2','3','4','5','6','7',
                     '8','9','A','B','C','D','E','F');
  $hexval = '';
  while($number != '0')
  {
      $hexval = $hexvalues[bcmod($number,'16')].$hexval;
      $number = bcdiv($number,'16',0);
  }
  return $hexval;
  }
  
  /**
    * hex2dec
    * Converts a hexadecimal string to decimal string
    * @param string Hex string
    * @return string Decimal string
    */
  function hex2dec($number)
  {
  $decvalues = array('0' =&gt;  '0', '1' =&gt;  '1', '2' =&gt; '2',
                     '3' =&gt;  '3', '4' =&gt;  '4', '5' =&gt; '5',
                     '6' =&gt;  '6', '7' =&gt;  '7', '8' =&gt; '8',
                     '9' =&gt;  '9', 'A' =&gt; '10', 'B' =&gt; '11',
                     'C' =&gt; '12', 'D' =&gt; '13', 'E' =&gt; '14',
                     'F' =&gt; '15', 'a' =&gt; '10', 'b' =&gt; '11',
                     'c' =&gt; '12', 'd' =&gt; '13', 'e' =&gt; '14',
                     'f' =&gt; '15');
  $decval = '0';
  
  $number = array_pop(explode("0x", $number, 2));
  
  $number = strrev($number);
  for($i = 0; $i &lt; strlen($number); $i++)
  {
          $decval = bcadd(bcmul(bcpow('16',$i,0),$decvalues[$number{$i}]), $decval);
  }
  return $decval;
  }
  
  /**
    * powmod
    * Raise a number to a power mod n
    * This could probably be made faster with some Montgomery trickery, but it's just fallback for now
    * @param string Decimal string to be raised
    * @param string Decimal string of the power to raise to
    * @param string Decimal string the modulus
    * @return string Decimal string
    */
  function powmod($num, $pow, $mod)
  {
  if (function_exists('bcpowmod')) {
      // bcpowmod is only available under PHP5
      return bcpowmod($num, $pow, $mod);
  }
  
  // emulate bcpowmod
  $result = '1';
  do {
      if (!bccomp(bcmod($pow, '2'), '1')) {
          $result = bcmod(bcmul($result, $num), $mod);
      }
      $num = bcmod(bcpow($num, '2'), $mod);
      $pow = bcdiv($pow, '2');
  } while (bccomp($pow, '0'));
  return $result;
  }
  
  /**
    * getSignature
    * Get the base64 signature of a dictionary
    * @param array Associative array (i.e. dictionary) of key-value pairs
    * @param string Hexadecimal string of public key
    * @param string Hexadecimal string the private key
    * @return string Base64 encoded signature
    */
  function getSignature($dict, $key, $privKey)
  {
  // Sort keys alphabetically
  uksort($dict, "strcasecmp");
  
  // Concatenate all values
  $total = '';
  foreach ($dict as $value)
      $total .= $value;
  
  // Get the hash
  $hash = sha1(utf8_encode($total));
  
  // OpenSSL-compatible PKCS1 Padding
  // 128 bytes - 20 bytes hash - 3 bytes extra padding = 105 bytes '0xff'
  $paddedHash = '0001';
  for ($i = 0; $i &lt; 105; $i++)
  {
      $paddedHash .= 'ff';
  }
  $paddedHash .= '00'.$hash;
  
  $decryptedSig = hex2dec($paddedHash);
  
  // Encrypt into a signature
  $sig = powmod($decryptedSig, hex2dec($privKey), hex2dec($key));
  $sig = base64_encode(hex2bin(dec2hex($sig)));
  return $sig;
  }
  
  $activationResponse =  getSignature(array("activationCode"=&gt;$a-&gt;serial_number, "guid"=&gt;$a-     &gt;guid), "BE6749000DA18D2D21F1D09455DB1E6C354E0B",    "7EEF860009165E1E164517CBDDF6D66A6F5D176C03883EC3E7BB643D84B0E17A41E68A57AE09670A943042F50AE9E41633E  E64A88639DA4691C3A11778839AAEB");
  echo $activationResponse;
  
   $o-&gt;license_name = $a-&gt;guid;
   $o-&gt;license = $activationResponse;
  $o-&gt;update();
  
  exit();
  </code></pre>
  
  <p>Here is the hex error from log</p>
  
  <pre><code>2014/12/02 13:05:22 [error] 22848#0: *280 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Cannot redeclare hex2bin() in /var/www/html/aurora/aurora-inapp.php on line 85" while reading response header from upstream, client: 141.101.00.22, server: test.com, request: "POST /aurora/aurora-inapp.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php-fpm.sock:", host: "test.com"
  </code></pre>