Obfuscator.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2015 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
11 namespace wcmf\lib\util;
12 
14 
15 /**
16  * Obfuscator allows to obfuscate strings. By passing an objuscated string
17  * to the method Obfuscator::unveil() the orginal string is returned.
18  * This is especially useful, if you want to place a secret string inside a client view
19  * as a parameter and want to get the original string back as the request is processed.
20  *
21  * @author ingo herwig <ingo@wemove.com>
22  */
23 class Obfuscator {
24 
25  // session name constants
26  private static $VALUES_VARNAME = 'Obfuscator.values';
27 
28  private $_session = null;
29 
30  /**
31  * Constructor
32  * @param $session
33  */
34  public function __construct(Session $session) {
35  $this->_session = $session;
36  }
37 
38  /**
39  * Get an obfuscated string
40  * @param $str The original sring
41  * @return The obfuscated string
42  */
43  public function obfuscate($str) {
44  if (strlen($str) == 0) {
45  return '';
46  }
47  $this->ensureStorage();
48 
49  // create and store the value
50  $obfuscated = md5($str);
51  $values = $this->_session->get(self::$VALUES_VARNAME);
52  $values[$obfuscated] = $str;
53  $this->_session->set(self::$VALUES_VARNAME, $values);
54 
55  return $obfuscated;
56  }
57 
58  /**
59  * Get an unveiled string
60  * @param $str The obfuscated sring
61  * @return The original string or an empty string if it does not exist
62  */
63  public function unveil($str) {
64  $this->ensureStorage();
65 
66  $values = $this->_session->get(self::$VALUES_VARNAME);
67  if (isset($values[$str])) {
68  return $values[$str];
69  }
70  else {
71  return $str;
72  }
73  }
74 
75  /**
76  * Ensure that the session storage for the values is initialized
77  */
78  private function ensureStorage() {
79  if (!$this->_session->exist(self::$VALUES_VARNAME)) {
80  $values = array();
81  $this->_session->set(self::$VALUES_VARNAME, $values);
82  }
83  }
84 }
85 ?>
Utility classes.
Definition: namespaces.php:97
obfuscate($str)
Get an obfuscated string.
Definition: Obfuscator.php:43
Session is the interface for session implementations and defines access to session variables...
Definition: Session.php:21
__construct(Session $session)
Constructor.
Definition: Obfuscator.php:34
Obfuscator allows to obfuscate strings.
Definition: Obfuscator.php:23
unveil($str)
Get an unveiled string.
Definition: Obfuscator.php:63