Overview

Packages

  • Hylax
  • None

Classes

  • Hylax
  • Hylax_Driver
  • Hylax_Driver_hylafax
  • Hylax_Driver_spandsp
  • Hylax_Image
  • Hylax_SQL_Attributes
  • Hylax_Storage
  • Hylax_Storage_sql
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Hylax_Storage Class
  4:  *
  5:  * Copyright 2003-2012 Horde LLC (http://www.horde.org/)
  6:  *
  7:  * See the enclosed file COPYING for license information (GPL). If you
  8:  * did not receive this file, see http://www.horde.org/licenses/gpl.
  9:  *
 10:  * @author  Marko Djukic <marko@oblo.com>
 11:  * @package Hylax
 12:  */
 13: class Hylax_Storage {
 14: 
 15:     /**
 16:      * A hash containing any parameters for the current storage driver.
 17:      *
 18:      * @var array
 19:      */
 20:     var $_params = array();
 21: 
 22:     /**
 23:      * @var VFS
 24:      */
 25:     var $_vfs;
 26: 
 27:     /**
 28:      * Constructor
 29:      *
 30:      * @param array $params  Any parameters needed for this storage driver.
 31:      * @throws Horde_Exception
 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:         /* Get the next ID. */
 42:         $fax_id = $this->newFaxId();
 43:         if (is_a($fax_id, 'PEAR_Error')) {
 44:             return $fax_id;
 45:         }
 46: 
 47:         /* Save data to VFS backend. */
 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:         /* In case this is just a fax creation without yet a number assigned
 62:          * create an empty number. */
 63:         if (!isset($info['fax_number'])) {
 64:             $info['fax_number'] = '';
 65:         }
 66: 
 67:         /* Set the folder. */
 68:         $info['fax_folder'] = ($info['fax_type']) ? 'outbox' : 'inbox';
 69: 
 70:         /* Set timestamp. */
 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:         /* Create a fax image object. */
 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:         /* Save to backend. */
 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:      * Attempts to return a concrete Hylax_Storage instance based on $driver.
134:      *
135:      * @param string $driver  The type of concrete Hylax_Storage subclass to
136:      *                        return.
137:      * @param array $params   A hash containing any additional configuration or
138:      *                        connection parameters a subclass might need.
139:      *
140:      * @return Hylax_Storage  The newly created concrete Hylax_Storage
141:      *                        instance, or false on error.
142:      * @throws Horde_Exception
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:      * Attempts to return a reference to a concrete Hylax_Storage instance
159:      * based on $driver.
160:      *
161:      * It will only create a new instance if no Hylax_Storage instance with the
162:      * same parameters currently exists.
163:      *
164:      * This should be used if multiple storage sources are required.
165:      *
166:      * This method must be invoked as: $var = &Hylax_Storage::singleton()
167:      *
168:      * @param string $driver  The type of concrete Hylax_Storage subclass to
169:      *                        return.
170:      * @param array $params   A hash containing any additional configuration or
171:      *                        connection parameters a subclass might need.
172:      *
173:      * @return mixed  The created concrete Hylax_Storage instance, or false on
174:      *                error.
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: 
API documentation generated by ApiGen