1: <?php
2: /**
3: * File based temporary storage place for incoming messages.
4: *
5: * PHP version 5
6: *
7: * @category Kolab
8: * @package Kolab_Filter
9: * @author Gunnar Wrobel <wrobel@pardus.de>
10: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
11: * @link http://pear.horde.org/index.php?package=Kolab_Filter
12: */
13:
14: /**
15: * File based temporary storage place for incoming messages.
16: *
17: * Copyright 2004-2010 Klarälvdalens Datakonsult AB
18: *
19: * See the enclosed file COPYING for license information (LGPL). If you did not
20: * receive this file, see
21: * http://www.horde.org/licenses/lgpl21.
22: *
23: * @category Kolab
24: * @package Kolab_Filter
25: * @author Gunnar Wrobel <wrobel@pardus.de>
26: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
27: * @link http://pear.horde.org/index.php?package=Kolab_Filter
28: */
29: class Horde_Kolab_Filter_Temporary_File
30: implements Horde_Kolab_Filter_Temporary
31: {
32: /**
33: * A temporary buffer file for storing the message.
34: *
35: * @var string
36: */
37: var $_tmpfile;
38:
39: /**
40: * The file handle for the temporary file.
41: *
42: * @var int
43: */
44: var $_tmpfh;
45:
46: /**
47: * Configuration.
48: *
49: * @param Horde_Kolab_Filter_Configuration
50: */
51: private $_config;
52:
53: /**
54: * Constructor.
55: *
56: * @param Horde_Kolab_Filter_Configuration $config The configuration.
57: */
58: public function __construct(
59: Horde_Kolab_Filter_Configuration $config
60: ) {
61: $this->_config = $config;
62: }
63:
64:
65: /**
66: * Creates a buffer for temporary storage of the message.
67: *
68: * @return NULL
69: */
70: public function init()
71: {
72: $conf = $this->_config->getConf();
73:
74: if (isset($conf['kolab']['filter']['tempdir'])) {
75: $tmpdir = $conf['kolab']['filter']['tempdir'];
76: } else {
77: $tmpdir = Horde_Util::getTempDir();
78: }
79:
80: $this->_tmpfile = @tempnam($tmpdir, 'IN.' . get_class($this) . '.');
81: $this->_tmpfh = @fopen($this->_tmpfile, 'w');
82: if (!$this->_tmpfh) {
83: throw new Horde_Kolab_Filter_Exception_IoError(
84: sprintf(
85: "Error: Could not open %s for writing: %s",
86: $this->_tmpfile,
87: $php_errormsg
88: )
89: );
90: }
91:
92: register_shutdown_function(array($this, 'cleanup'));
93: }
94:
95: /**
96: * Return the file handle for writing data.
97: *
98: * @return resource The file handle.
99: */
100: public function getHandle()
101: {
102: return $this->_tmpfh;
103: }
104:
105: /**
106: * Return the file handle for reading data.
107: *
108: * @return resource The file handle.
109: */
110: public function getReadHandle()
111: {
112: return @fopen($this->_tmpfile, 'r');;
113: }
114:
115: /**
116: * A shutdown function for removing the temporary file.
117: *
118: * @return NULL
119: */
120: public function cleanup()
121: {
122: if (@file_exists($this->_tmpfile)) {
123: @unlink($this->_tmpfile);
124: }
125: }
126: }