1: <?php
2: /**
3: * The Horde_Crypt:: class provides an API for various cryptographic
4: * systems used by Horde applications.
5: *
6: * Copyright 2002-2012 Horde LLC (http://www.horde.org/)
7: *
8: * See the enclosed file COPYING for license information (LGPL). If you
9: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
10: *
11: * @author Michael Slusarz <slusarz@horde.org>
12: * @category Horde
13: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
14: * @package Crypt
15: */
16: class Horde_Crypt
17: {
18: /**
19: * Configuration parameters.
20: *
21: * @var array
22: */
23: protected $_params = array();
24:
25: /**
26: * The temporary directory to use.
27: *
28: * @var string
29: */
30: protected $_tempdir;
31:
32: /**
33: * Attempts to return a concrete Horde_Crypt instance based on $driver.
34: *
35: * @param string $driver Either a driver name, or the full class name to
36: * use (class must extend Horde_Crypt).
37: * @param array $params A hash containing any additional configuration
38: * or parameters a subclass might need.
39: *
40: * @return Horde_Crypt The newly created concrete instance.
41: * @throws Horde_Crypt_Exception
42: */
43: static public function factory($driver, $params = array())
44: {
45: /* Return a base Horde_Crypt object if no driver is specified. */
46: if (empty($driver) || (strcasecmp($driver, 'none') == 0)) {
47: return new Horde_Crypt();
48: }
49:
50: /* Base drivers (in Crypt/ directory). */
51: $class = __CLASS__ . '_' . ucfirst(basename($driver));
52: if (class_exists($class)) {
53: return new $class($params);
54: }
55:
56: /* Explicit class name, */
57: $class = $driver;
58: if (class_exists($class)) {
59: return new $class($params);
60: }
61:
62: throw new Horde_Crypt_Exception(__CLASS__ . ': Class definition of ' . $driver . ' not found.');
63: }
64:
65: /**
66: * Constructor.
67: *
68: * @param array $params Configuration parameters:
69: * <pre>
70: * email_charset - (string) The default email charset.
71: * DEFAULT: NONE
72: * temp - (string) [REQUIRED] Location of temporary directory.
73: * </pre>
74: *
75: * @throws InvalidArgumentException
76: */
77: public function __construct(array $params = array())
78: {
79: if (empty($params['temp'])) {
80: throw new InvalidArgumentException('A temporary directory must be provided.');
81: }
82:
83: $this->_tempdir = Horde_Util::createTempDir(true, $params['temp']);
84:
85: $this->_params = array_merge(array(
86: 'email_charset' => null,
87: ), $params);
88: }
89:
90: /**
91: * Encrypt the requested data.
92: * This method should be provided by all classes that extend Horde_Crypt.
93: *
94: * @param string $data The data to encrypt.
95: * @param array $params An array of arguments needed to encrypt the data.
96: *
97: * @return array The encrypted data.
98: */
99: public function encrypt($data, $params = array())
100: {
101: return $data;
102: }
103:
104: /**
105: * Decrypt the requested data.
106: * This method should be provided by all classes that extend Horde_Crypt.
107: *
108: * @param string $data The data to decrypt.
109: * @param array $params An array of arguments needed to decrypt the data.
110: *
111: * @return array The decrypted data.
112: * @throws Horde_Crypt_Exception
113: */
114: public function decrypt($data, $params = array())
115: {
116: return $data;
117: }
118:
119: /**
120: * Create a temporary file that will be deleted at the end of this
121: * process.
122: *
123: * @param string $descrip Description string to use in filename.
124: * @param boolean $delete Delete the file automatically?
125: *
126: * @return string Filename of a temporary file.
127: */
128: protected function _createTempFile($descrip = 'horde-crypt',
129: $delete = true)
130: {
131: return Horde_Util::getTempFile($descrip, $delete, $this->_tempdir, true);
132: }
133:
134: }
135: