1: <?php
2: /**
3: * Kronolith_Calendar_Remote defines an API for single external WebDAV or
4: * CalDAV calendars.
5: *
6: * Copyright 2010-2012 Horde LLC (http://www.horde.org/)
7: *
8: * See the enclosed file COPYING for license information (GPL). If you
9: * did not receive this file, see http://www.horde.org/licenses/gpl.
10: *
11: * @author Jan Schneider <jan@horde.org>
12: * @package Kronolith
13: */
14: class Kronolith_Calendar_Remote extends Kronolith_Calendar
15: {
16: /**
17: * The URL of this calendar.
18: *
19: * @var string
20: */
21: protected $_url;
22:
23: /**
24: * The name of this calendar.
25: *
26: * @var string
27: */
28: protected $_name;
29:
30: /**
31: * The description of this calendar.
32: *
33: * @var string
34: */
35: protected $_desc = '';
36:
37: /**
38: * The HTTP user name for this calendar.
39: *
40: * @var string
41: */
42: protected $_user;
43:
44: /**
45: * The HTTP password for this calendar.
46: *
47: * @var string
48: */
49: protected $_password;
50:
51: /**
52: * The color of this calendar.
53: *
54: * @var string
55: */
56: protected $_color;
57:
58: /**
59: * Constructor.
60: *
61: * @param array $params A hash with any parameters that this calendar
62: * might need.
63: * Required parameters:
64: * - share: The share of this calendar.
65: */
66: public function __construct($params = array())
67: {
68: if (!isset($params['url'])) {
69: throw new BadMethodCallException('url parameter is missing');
70: }
71: if (!isset($params['name'])) {
72: throw new BadMethodCallException('name parameter is missing');
73: }
74: $key = $GLOBALS['registry']->getAuthCredential('password');
75: if ($key) {
76: $secret = $GLOBALS['injector']->getInstance('Horde_Secret');
77: if (!empty($params['user'])) {
78: $params['user'] = $secret->read($key, base64_decode($params['user']));
79: }
80: if (!empty($params['password'])) {
81: $params['password'] = $secret->read($key, base64_decode($params['password']));
82: }
83: }
84: parent::__construct($params);
85: }
86:
87: /**
88: * Returns the name of this calendar.
89: *
90: * @return string This calendar's name.
91: */
92: public function name()
93: {
94: return $this->_name;
95: }
96:
97: /**
98: * Returns the description of this calendar.
99: *
100: * @return string This calendar's description.
101: */
102: public function description()
103: {
104: return $this->_desc;
105: }
106:
107: /**
108: * Returns the background color for this calendar.
109: *
110: * @return string A HTML color code.
111: */
112: public function background()
113: {
114: return empty($this->_color) ? parent::background() : $this->_color;
115: }
116:
117: /**
118: * Encapsulates permissions checking.
119: *
120: * @param integer $permission The permission to check for.
121: * @param string $user The user to check permissions for. Defaults
122: * to the current user.
123: * @param string $creator An event creator, to check for creator
124: * permissions.
125: *
126: * @return boolean Whether the user has the permission on this calendar.
127: */
128: public function hasPermission($permission, $user = null, $creator = null)
129: {
130: return (boolean)(Kronolith::getDriver('Ical', $this->_url)->getPermission() & $permission);
131: }
132:
133: /**
134: * Whether this calendar is supposed to be displayed in lists.
135: *
136: * @return boolean True if this calendar should be displayed.
137: */
138: public function display()
139: {
140: return in_array($this->_url, $GLOBALS['display_remote_calendars']);
141: }
142:
143: /**
144: * Returns the URL of this calendar.
145: *
146: * @return string This calendar's URL.
147: */
148: public function url()
149: {
150: return $this->_url;
151: }
152:
153: /**
154: * Returns the authentication credentials for this calendar.
155: *
156: * @return array This calendar's credentials.
157: */
158: public function credentials()
159: {
160: if (!empty($this->_user)) {
161: return array('user' => $this->_user, 'password' => $this->_password);
162: }
163: return array();
164: }
165:
166: /**
167: * Returns a hash representing this calendar.
168: *
169: * @return array A simple hash.
170: */
171: public function toHash()
172: {
173: return array_merge(
174: parent::toHash(),
175: array('show' => in_array($this->_url, $GLOBALS['display_remote_calendars']),
176: 'edit' => $this->hasPermission(Horde_Perms::EDIT)),
177: $this->credentials()
178: );
179: }
180: }
181: