1: <?php
2: /**
3: * This decorator triggers a URL following certain actions on the folder.
4: *
5: * PHP version 5
6: *
7: * @category Kolab
8: * @package Kolab_Storage
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_Storage
12: */
13:
14: /**
15: * This decorator triggers a URL following certain actions on the folder.
16: *
17: * Copyright 2008-2010 Klarälvdalens Datakonsult AB
18: *
19: * See the enclosed file COPYING for license information (LGPL). If you
20: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
21: *
22: * @author Gunnar Wrobel <wrobel@pardus.de>
23: * @package Kolab_Storage
24: */
25: class Horde_Kolab_Storage_Folder_Decorator_Trigger
26: extends Horde_Kolab_Storage_Folder_Decorator_Base
27: {
28: /**
29: * An output for log messages.
30: *
31: * @var Horde_Log_Logger
32: */
33: private $_logger;
34:
35: /**
36: * Constructor
37: *
38: * @param Horde_Kolab_Storage_Folder $folder The folder to be decorated.
39: * @param Horde_Log_Logger $logger The logger.
40: */
41: public function __construct(Horde_Kolab_Storage_Folder $folder,
42: Horde_Log_Logger $logger)
43: {
44: $this->_logger = $logger;
45: parent::__construct($folder);
46: }
47:
48: /**
49: * Saves the folder.
50: *
51: * @param array $attributes An array of folder attributes. You can
52: * set any attribute but there are a few
53: * special ones like 'type', 'default',
54: * 'owner' and 'desc'.
55: *
56: * @return NULL
57: */
58: public function save($attributes = null)
59: {
60: /**
61: * Trigger the old folder on an empty IMAP folder after renaming a folder!
62: */
63: try {
64: $this->_connection->create($this->name);
65: $this->_connection->setAnnotation(self::ANNOT_FOLDER_TYPE,
66: $this->_type,
67: $this->name);
68: $this->trigger($this->name);
69: $this->_connection->delete($this->name);
70: } catch (Exception $e) {
71: Horde::logMessage(sprintf('Failed handling the dummy folder: %s!',
72: $e->getMessage()), 'ERR');
73: }
74:
75: /** Finally trigger the folder after saving.*/
76: try {
77: $this->trigger();
78: } catch (Horde_Kolab_Storage_Exception $e) {
79: Horde::logMessage(sprintf('Failed triggering folder %s! Error was: %s',
80: $this->name, $e->getMessage()), 'ERR');
81: }
82: }
83:
84: /**
85: * Delete the specified message from this folder.
86: *
87: * @param string $id IMAP id of the message to be deleted.
88: * @param boolean $trigger Should the folder be triggered?
89: *
90: * @return NULL
91: */
92: public function deleteMessage($id, $trigger = true)
93: {
94: $this->_folder->deleteMessage($id, $trigger);
95:
96: if ($trigger) {
97: try {
98: $result = $this->trigger();
99: } catch (Horde_Kolab_Storage_Exception $e) {
100: Horde::logMessage(sprintf('Failed triggering folder %s! Error was: %s',
101: $this->name, $result->getMessage()), 'ERR');
102: }
103: }
104: }
105:
106: /**
107: * Move the specified message to the specified folder.
108: *
109: * @param string $id IMAP id of the message to be moved.
110: * @param string $folder Name of the receiving folder.
111: *
112: * @return NULL
113: */
114: public function moveMessage($id, $folder)
115: {
116: $this->_folder->moveMessage($id, $folder);
117:
118: //@todo: shouldn't we trigger both folders here?
119:
120: $result = $this->trigger();
121: }
122:
123: /**
124: * Move the specified message to the specified share.
125: *
126: * @param string $id IMAP id of the message to be moved.
127: * @param string $share Name of the receiving share.
128: *
129: * @return NULL
130: */
131: public function moveMessageToShare($id, $share)
132: {
133: $this->_folder->moveMessageToShare($id, $share);
134:
135: //@todo: shouldn't we trigger both folders here?
136: $result = $this->trigger();
137: }
138:
139: /**
140: * Save an object in this folder.
141: *
142: * @param array $object The array that holds the data of the object.
143: * @param int $data_version The format handler version.
144: * @param string $object_type The type of the kolab object.
145: * @param string $id The IMAP id of the old object if it
146: * existed before
147: * @param array $old_object The array that holds the current data of the
148: * object.
149: *
150: * @return NULL
151: */
152: public function saveObject(&$object, $data_version, $object_type, $id = null,
153: &$old_object = null)
154: {
155: $this->_folder->saveObject($object, $data_version, $object_type, $id, $old_object);
156: $this->trigger();
157: }
158:
159: /**
160: * Set the ACL of this folder.
161: *
162: * @param $user The user for whom the ACL should be set.
163: * @param $acl The new ACL value.
164: *
165: * @return NULL
166: */
167: public function setAcl($user, $acl)
168: {
169: $this->_folder->setAcl($user, $acl);
170: $this->trigger();
171: }
172:
173: /**
174: * Delete the ACL for a user on this folder.
175: *
176: * @param $user The user for whom the ACL should be deleted.
177: *
178: * @return NULL
179: */
180: public function deleteAcl($user)
181: {
182: $this->_folder->deleteAcl($user);
183: $this->trigger();
184: }
185:
186: /**
187: * Triggers any required updates after changes within the
188: * folder. This is currently only required for handling free/busy
189: * information with Kolab.
190: *
191: * @param string $name Name of the folder that should be triggered.
192: *
193: * @return boolean|PEAR_Error True if successfull.
194: */
195: private function trigger($name = null)
196: {
197: $type = $this->getType();
198: if (is_a($type, 'PEAR_Error')) {
199: return $type;
200: }
201:
202: $owner = $this->getOwner();
203: if (is_a($owner, 'PEAR_Error')) {
204: return $owner;
205: }
206:
207: $subpath = $this->getSubpath($name);
208: if (is_a($subpath, 'PEAR_Error')) {
209: return $subpath;
210: }
211:
212: switch($type) {
213: case 'event':
214: $session = &Horde_Kolab_Session_Singleton::singleton();
215: $url = sprintf('%s/trigger/%s/%s.pfb',
216: $session->freebusy_server, $owner, $subpath);
217: break;
218: default:
219: return true;
220: }
221:
222: $result = $this->triggerUrl($url);
223: if (is_a($result, 'PEAR_Error')) {
224: return PEAR::raiseError(sprintf(Horde_Kolab_Storage_Translation::t("Failed triggering folder %s. Error was: %s"),
225: $this->name, $result->getMessage()));
226: }
227: return $result;
228: }
229:
230: /**
231: * Triggers a URL.
232: *
233: * @param string $url The URL to be triggered.
234: *
235: * @return boolean|PEAR_Error True if successfull.
236: */
237: private function triggerUrl($url)
238: {
239: global $conf;
240:
241: if (!empty($conf['kolab']['no_triggering'])) {
242: return true;
243: }
244:
245: $options['method'] = 'GET';
246: $options['timeout'] = 5;
247: $options['allowRedirects'] = true;
248:
249: if (isset($conf['http']['proxy']) && !empty($conf['http']['proxy']['proxy_host'])) {
250: $options = array_merge($options, $conf['http']['proxy']);
251: }
252:
253: $http = new HTTP_Request($url, $options);
254: $http->setBasicAuth($GLOBALS['registry']->getAuth(), $GLOBALS['registry']->getAuthCredential('password'));
255: @$http->sendRequest();
256: if ($http->getResponseCode() != 200) {
257: return PEAR::raiseError(sprintf(Horde_Kolab_Storage_Translation::t("Unable to trigger URL %s. Response: %s"),
258: $url, $http->getResponseCode()));
259: }
260: return true;
261: }
262:
263: }
264: