1<?php
2/** OLD Kolab f/b trigger code. Some parts mights still need to be salvaged into
3    the new code. Remove segments that have been verified to replace the old
4    code. */
5
6/**
7 * This decorator triggers a URL following certain actions on the folder.
8 *
9 * PHP version 5
10 *
11 * @category Kolab
12 * @package  Kolab_Storage
13 * @author   Gunnar Wrobel <wrobel@pardus.de>
14 * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
15 */
16
17/**
18 * This decorator triggers a URL following certain actions on the folder.
19 *
20 * Copyright 2008-2010 Klarälvdalens Datakonsult AB
21 *
22 * See the enclosed file COPYING for license information (LGPL). If you
23 * did not receive this file, see http://www.horde.org/licenses/lgpl21.
24 *
25 * @author  Gunnar Wrobel <wrobel@pardus.de>
26 * @package Kolab_Storage
27 */
28class Horde_Kolab_Storage_Folder_Decorator_Trigger
29extends Horde_Kolab_Storage_Folder_Decorator_Base
30{
31    /**
32     * An output for log messages.
33     *
34     * @var Horde_Log_Logger
35     */
36    private $_logger;
37
38    /**
39     * Constructor
40     *
41     * @param Horde_Kolab_Storage_Folder $folder The folder to be decorated.
42     * @param Horde_Log_Logger           $logger The logger.
43     */
44    public function __construct(Horde_Kolab_Storage_Folder $folder,
45                                Horde_Log_Logger $logger)
46    {
47        $this->_logger = $logger;
48        parent::__construct($folder);
49    }
50
51    /**
52     * Saves the folder.
53     *
54     * @param array $attributes An array of folder attributes. You can
55     *                          set any attribute but there are a few
56     *                          special ones like 'type', 'default',
57     *                          'owner' and 'desc'.
58     *
59     * @return NULL
60     */
61    public function save($attributes = null)
62    {
63        /**
64         * Trigger the old folder on an empty IMAP folder after renaming a folder!
65         */
66        try {
67            $this->_connection->create($this->name);
68            $this->_connection->setAnnotation(self::ANNOT_FOLDER_TYPE,
69                                              $this->_type,
70                                              $this->name);
71            $this->trigger($this->name);
72            $this->_connection->delete($this->name);
73        } catch (Exception $e) {
74            Horde::log(sprintf('Failed handling the dummy folder: %s!', $e->getMessage()), 'ERR');
75        }
76
77        /** Finally trigger the folder after saving.*/
78        try {
79            $this->trigger();
80        } catch (Horde_Kolab_Storage_Exception $e) {
81            Horde::log(sprintf('Failed triggering folder %s! Error was: %s', $this->name, $e->getMessage()), 'ERR');
82        }
83    }
84
85    /**
86     * Delete the specified message from this folder.
87     *
88     * @param  string  $id      IMAP id of the message to be deleted.
89     * @param  boolean $trigger Should the folder be triggered?
90     *
91     * @return NULL
92     */
93    public function deleteMessage($id, $trigger = true)
94    {
95        $this->_folder->deleteMessage($id, $trigger);
96
97        if ($trigger) {
98            try {
99                $result = $this->trigger();
100            } catch (Horde_Kolab_Storage_Exception $e) {
101                Horde::log(sprintf('Failed triggering folder %s! Error was: %s', $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