wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
class.Obfuscator.php
Go to the documentation of this file.
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2014 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of any of the following licenses
7  * at your choice:
8  *
9  * - GNU Lesser General Public License (LGPL)
10  * http://www.gnu.org/licenses/lgpl.html
11  * - Eclipse Public License (EPL)
12  * http://www.eclipse.org/org/documents/epl-v10.php
13  *
14  * See the license.txt file distributed with this work for
15  * additional information.
16  *
17  * $Id: class.Obfuscator.php 1462 2014-02-04 23:52:27Z iherwig $
18  */
19 require_once(BASE."wcmf/lib/util/class.SessionData.php");
20 
21 /**
22  * @class Obfuscator
23  * @ingroup Util
24  * @brief This class allows to obfuscate strings. By passing an objuscated string
25  * to the method Obfuscator::unveil() the orginal string is returned.
26  * This is especially useful, if you want to place a secret string inside a client view
27  * as a parameter and want to get the original string back as the request is processed.
28  *
29  * @author ingo herwig <ingo@wemove.com>
30  */
32 {
33  // session name constants
34  var $VALUES_VARNAME = 'Obfuscator.values';
35 
36  /**
37  * Returns an instance of the class.
38  * @return A reference to the only instance of the Singleton object
39  */
40  function &getInstance()
41  {
42  global $gObfuscator;
43 
44  if ($gObfuscator == null)
45  $gObfuscator = new Obfuscator();
46 
47  return $gObfuscator;
48  }
49  /**
50  * Get an obfuscated string
51  * @note This method maybe called staticly
52  * @param str The original sring
53  * @return The obfuscated string
54  */
55  function obfuscate($str)
56  {
57  if (strlen($str) == 0)
58  return '';
59 
60  $obfuscator = &Obfuscator::getInstance();
61  $session = &SessionData::getInstance();
62  $obfuscator->ensureStorage();
63 
64  // create and store the value
65  $obfuscated = md5($str);
66  $values = $session->get($obfuscator->VALUES_VARNAME);
67  $values[$obfuscated] = $str;
68  $session->set($obfuscator->VALUES_VARNAME, $values);
69 
70  return $obfuscated;
71  }
72  /**
73  * Get an unveiled string
74  * @note This method maybe called staticly
75  * @param str The obfuscated sring
76  * @return The original string or an empty string if it does not exist
77  */
78  function unveil($str)
79  {
80  $obfuscator = &Obfuscator::getInstance();
81  $session = &SessionData::getInstance();
82  $obfuscator->ensureStorage();
83 
84  $values = $session->get($obfuscator->VALUES_VARNAME);
85  $unveiled = $values[$str];
86  return $unveiled;
87  }
88  /**
89  * Ensure that the session storage for the values is initialized
90  */
91  function ensureStorage()
92  {
93  $session = &SessionData::getInstance();
94  if (!$session->exist($obfuscator->VALUES_VARNAME))
95  {
96  $values = array();
97  $session->set($obfuscator->VALUES_VARNAME, $values);
98  }
99  }
100 }
This class allows to obfuscate strings. By passing an objuscated string to the method Obfuscator::unv...