1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13: class Hylax_Storage {
14:
15: 16: 17: 18: 19:
20: var $_params = array();
21:
22: 23: 24:
25: var $_vfs;
26:
27: 28: 29: 30: 31: 32:
33: function Hylax_Storage($params)
34: {
35: $this->_params = $params;
36: $this->_vfs = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Vfs')->create();
37: }
38:
39: function saveFaxData($data, $type = '.ps')
40: {
41:
42: $fax_id = $this->newFaxId();
43: if (is_a($fax_id, 'PEAR_Error')) {
44: return $fax_id;
45: }
46:
47:
48: $path = Hylax::getVFSPath($fax_id);
49: $file = $fax_id . $type;
50: try {
51: $this->_vfs->writeData($path, $file, $data, true);
52: } catch (Horde_Vfs_Exception $e) {
53: Horde::logMessage('Could not save fax file to VFS: ' . $e->getMessage(), 'ERR');
54: throw $e;
55: }
56: return $fax_id;
57: }
58:
59: function createFax($info, $fix_owner = false)
60: {
61: 62:
63: if (!isset($info['fax_number'])) {
64: $info['fax_number'] = '';
65: }
66:
67:
68: $info['fax_folder'] = ($info['fax_type']) ? 'outbox' : 'inbox';
69:
70:
71: if (empty($info['fax_created'])) {
72: $info['fax_created'] = time();
73: }
74:
75: $data = $this->getFaxData($info['fax_id']);
76: if (is_a($data, 'PEAR_Error')) {
77: Horde::logMessage('Could not get fax data: ' . $data->getMessage(), 'ERR');
78: return $data;
79: }
80:
81:
82: require_once HYLAX_BASE . '/lib/Image.php';
83: $image = new Hylax_Image();
84: $image->loadData($data);
85: if (empty($info['fax_pages'])) {
86: $info['fax_pages'] = $image->getNumPages();
87: }
88:
89:
90: $result = $this->_createFax($info);
91: if (is_a($result, 'PEAR_Error')) {
92: return $result;
93: }
94: global $conf;
95: foreach ($conf['fax']['notify'] as $rec) {
96: mail($rec, _("New fax from: ") . $info['fax_number'], '',
97: 'From: '. $conf['fax']['notifyfrom']);
98: }
99: return true;
100: }
101:
102: function getFaxData($fax_id)
103: {
104: $path = Hylax::getVFSPath($fax_id);
105: $file = $fax_id . '.ps';
106: try {
107: return $this->_vfs->read($path, $file);
108: } catch (Horde_Vfs_Exception $e) {
109: Horde::logMessage(sprintf("%s '%s/%s'.", $e->getMessage(), $path, $file), 'ERR');
110: throw $e;
111: }
112: }
113:
114: function listFaxes($folder)
115: {
116: return $this->_listFaxes($folder);
117: }
118:
119: function send($fax_id, $number)
120: {
121: global $hylax;
122:
123: $this->_setFaxNumber($fax_id, $number);
124:
125: $data = $this->getFaxData($fax_id);
126:
127: $job_id = $hylax->gateway->send($number, $data);
128:
129: $this->_setJobId($fax_id, $job_id);
130: }
131:
132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143:
144: function &factory($driver, $params = array())
145: {
146: $driver = basename($driver);
147: include_once dirname(__FILE__) . '/Storage/' . $driver . '.php';
148: $class = 'Hylax_Storage_' . $driver;
149: if (class_exists($class)) {
150: $storage = new $class($params);
151: return $storage;
152: }
153:
154: throw new Horde_Exception(sprintf(_("No such backend \"%s\" found"), $driver));
155: }
156:
157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175:
176: function &singleton($driver, $params = array())
177: {
178: static $instances;
179:
180: if (!isset($instances)) {
181: $instances = array();
182: }
183:
184: $signature = serialize(array($driver, $params));
185: if (!isset($instances[$signature])) {
186: $instances[$signature] = &Hylax_Storage::factory($driver, $params);
187: }
188:
189: return $instances[$signature];
190: }
191:
192: }
193: