<?php
/*
+--------------------------------------------------------------------+
- | CiviCRM version 4.5 |
+ | CiviCRM version 4.6 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2014 |
+--------------------------------------------------------------------+
/**
* Instantiate a signature-processor
*
- * @param $secret string, private
- * @param $paramNames array, fields which should be part of the signature
+ * @param string $secret
+ * private.
+ * @param array $paramNames
+ * Array, fields which should be part of the signature.
*/
- function __construct($secret, $paramNames) {
+ public function __construct($secret, $paramNames) {
sort($paramNames); // ensure consistent serialization of payloads
$this->secret = $secret;
$this->paramNames = $paramNames;
/**
* Generate a signature for a set of key-value pairs
*
- * @param $params array, key-value pairs
- * @param $salt string, the salt (if known) or NULL (for auto-generated)
+ * @param array $params
+ * Array, key-value pairs.
+ * @param string $salt
+ * the salt (if known) or NULL (for auto-generated).
* @return string, the full public token representing the signature
*/
- function sign($params, $salt = NULL) {
+ public function sign($params, $salt = NULL) {
$message = array();
$message['secret'] = $this->secret;
$message['payload'] = array();
if (empty($salt)) {
$message['salt'] = $this->createSalt();
- } else {
- $message['salt'] = $salt;
+ }
+ else {
+ $message['salt'] = $salt;
}
// recall: paramNames is pre-sorted for stability
foreach ($this->paramNames as $paramName) {
if (is_numeric($params[$paramName])) {
$params[$paramName] = (string) $params[$paramName];
}
- } else { // $paramName is not included or ===NULL
- $params[$paramName] = '';
}
- $message['payload'][$paramName] = $params[$paramName];
+ else {// $paramName is not included or ===NULL
+ $params[$paramName] = '';
}
+ $message['payload'][$paramName] = $params[$paramName];
+ }
$token = $message['salt'] . $this->signDelim . md5(serialize($message));
return $token;
}
/**
* Determine whether a token represents a proper signature for $params
*
- * @param $token string, the full public token representing the signature
- * @param $params array, key-value pairs
+ * @param string $token
+ * the full public token representing the signature.
+ * @param array $params
+ * Array, key-value pairs.
*
* @throws Exception
* @return bool, TRUE iff all $paramNames for the submitted validate($params) and the original sign($params)
*/
- function validate($token, $params) {
+ public function validate($token, $params) {
list ($salt, $signature) = explode($this->signDelim, $token);
if (strlen($salt) != self::SALT_LEN) {
throw new Exception("Invalid salt [$token]=[$salt][$signature]");
return ($token == $newToken);
}
- function createSalt() {
+ /**
+ * @return string
+ */
+ public function createSalt() {
// It would be more secure to generate a new value but liable to run this
// many times on certain admin pages; so instead we'll re-use the hash.
return $this->defaultSalt;