Merge pull request #14928 from lcdservices/dev-core-1158
[civicrm-core.git] / CRM / Core / Smarty / plugins / function.crmSigner.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC
16 * $Id$
17 *
18 */
19
20 /**
21 * Generate a secure signature
22 *
23 * {code}
24 * {crmSigner var=mySig extra=123}
25 * var urlParams = ts={$mySig.ts}&extra={$mySig.extra}&sig={$mySig.signature}
26 * {endcode}
27 *
28 * @param array $params
29 * Array with keys:
30 * - var: string, a smarty variable to generate
31 * - ts: int, the current time (if omitted, autogenerated)
32 * - any other vars are put into the signature (sorted)
33 * @param $smarty
34 */
35 function smarty_function_crmSigner($params, &$smarty) {
36 $var = $params['var'];
37 unset($params['var']);
38 $params['ts'] = CRM_Utils_Time::getTimeRaw();
39
40 $fields = array_keys($params);
41 sort($fields);
42
43 $signer = new CRM_Utils_Signer(CRM_Core_Key::privateKey(), $fields);
44 $params['signature'] = $signer->sign($params);
45 $smarty->assign($var, $params);
46 }