4 * Web Service functionality to get events.
5 * Uses XML (but not SOAP at this point since that would be
6 * overkill and require extra packages to install).
9 * Client apps must use the same authentication as the web browser.
10 * If WebCalendar is setup to use web-based authentication, then
11 * the login.php found in this directory should be used to obtain
16 // Load include files.
17 $basedir = ".."; // points to the base WebCalendar directory relative to
18 // current working directory
19 $includedir = "../includes";
21 include "$includedir/config.php";
22 include "$includedir/php-dbi.php";
23 include "$includedir/functions.php";
24 include "$includedir/$user_inc";
25 include "$includedir/validate.php";
26 include "$includedir/connect.php";
27 load_global_settings ();
28 load_user_preferences ();
29 include "$includedir/site_extras.php";
31 include "$includedir/translate.php";
33 $debug = false; // set to true to print debug info...
35 //Header ( "Content-type: text/xml" );
36 Header ( "Content-type: text/plain" );
38 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
41 // If login is public user, make sure public can view others...
42 if ( $login == "__public__" && $login != $user ) {
43 if ( $public_access_others != 'Y' ) {
44 echo "<error>" . translate("Not authorized") . "</error>\n";
48 echo "<!-- Allowing public user to view other user's calendar -->\n";
51 if ( empty ( $user ) )
54 // If viewing different user then yourself...
55 if ( $login != $user ) {
56 if ( $allow_view_other != 'Y' ) {
57 echo "<error>" . translate("Not authorized") . "</error>\n";
61 echo "<!-- Allowing user to view other user's calendar -->\n";
64 if ( empty ( $startdate ) )
65 $startdate = date ( "Ymd" );
66 if ( empty ( $enddate ) )
67 $enddate = $startdate;
69 // Now read events all the repeating events (for all users)
70 $repeated_events = query_events ( $user, true,
71 "AND (webcal_entry_repeats.cal_end > $startdate OR " .
72 "webcal_entry_repeats.cal_end IS NULL) " );
74 // Read non-repeating events (for all users)
76 echo "Checking for events for $user from date $startdate to date $enddate\n";
77 $events = read_events ( $user, $startdate, $enddate );
79 echo "Found " . count ( $events ) . " events in time range.\n";
83 function escapeXml ( $str )
85 return ( str_replace ( "<", "<", str_replace ( ">", ">", $str ) ) );
88 // Send a single event
89 function print_event_xml ( $id, $event_date ) {
90 global $site_extras, $debug,
91 $server_url, $application_name;
92 global $EXTRA_TEXT, $EXTRA_MULTILINETEXT, $EXTRA_URL, $EXTRA_DATE,
93 $EXTRA_EMAIL, $EXTRA_USER, $EXTRA_REMINDER, $LANGUAGE;
95 $pri[1] = translate("Low");
96 $pri[2] = translate("Medium");
97 $pri[3] = translate("High");
99 // get participants first...
101 $sql = "SELECT cal_login FROM webcal_entry_user " .
102 "WHERE cal_id = $id AND cal_status IN ('A','W') " .
103 "ORDER BY cal_login";
104 $res = dbi_query ( $sql );
105 $participants = array ();
106 $num_participants = 0;
108 while ( $row = dbi_fetch_row ( $res ) ) {
109 $participants[$num_participants++] = $row[0];
113 // get external participants
114 $ext_participants = array ();
115 $num_ext_participants = 0;
116 if ( ! empty ( $allow_external_users ) && $allow_external_users == "Y" &&
117 ! empty ( $external_reminders ) && $external_reminders == "Y" ) {
118 $sql = "SELECT cal_fullname, cal_email FROM webcal_entry_ext_user " .
119 "WHERE cal_id = $id AND cal_email IS NOT NULL " .
120 "ORDER BY cal_fullname";
121 $res = dbi_query ( $sql );
123 while ( $row = dbi_fetch_row ( $res ) ) {
124 $ext_participants[$num_ext_participants] = $row[0];
125 $ext_participants_email[$num_ext_participants++] = $row[1];
130 if ( ! $num_participants && ! $num_ext_participants ) {
132 echo "No participants found for event id: $id\n";
139 "SELECT cal_create_by, cal_date, cal_time, cal_mod_date, " .
140 "cal_mod_time, cal_duration, cal_priority, cal_type, cal_access, " .
141 "cal_name, cal_description FROM webcal_entry WHERE cal_id = $id" );
143 echo "Db error: could not find event id $id.\n";
148 if ( ! ( $row = dbi_fetch_row ( $res ) ) ) {
149 echo "Error: could not find event id $id in database.\n";
153 $create_by = $row[0];
155 $description = $row[10];
158 echo " <id>$id</id>\n";
159 echo " <name>" . escapeXml ( $name ) . "</name>\n";
160 if ( ! empty ( $server_url ) ) {
161 if ( substr ( $server_url, -1, 1 ) == "/" ) {
162 echo " <url>" . $server_url . "view_entry.php?id=" . $id . "</url>\n";
164 echo " <url>" . $server_url . "/view_entry.php?id=" . $id . "</url>\n";
167 echo " <description>" . escapeXml ( $description ) . "</description>\n";
168 echo " <dateFormatted>" . date_to_str ( $event_date ) . "</dateFormatted>\n";
169 echo " <date>" . $event_date . "</date>\n";
170 if ( $row[2] >= 0 ) {
171 echo " <time>" . sprintf ( "%04d", $row[2] / 100 ) . "</time>\n";
172 echo " <timeFormatted>" . display_time ( $row[2] ) . "</timeFormatted>\n";
175 echo " <duration>" . $row[5] . "</duration>\n";
176 if ( ! $disable_priority_field )
177 echo " <priority>" . $pri[$row[6]] . "</priority>\n";
178 if ( ! $disable_access_field )
180 ( $row[8] == "P" ? translate("Public") : translate("Confidential") ) .
182 if ( ! strlen ( $single_user_login ) )
183 echo " <createdBy>" . $row[0] . "</createdBy>\n";
184 echo " <updateDate>" . date_to_str ( $row[3] ) . "</updateDate>\n";
185 echo " <updateTime>" . display_time ( $row[4] ) . "</updateTime>\n";
188 $extras = get_site_extra_fields ( $id );
189 echo " <siteExtras>\n";
190 for ( $i = 0; $i < count ( $site_extras ); $i++ ) {
191 $extra_name = $site_extras[$i][0];
192 $extra_descr = $site_extras[$i][1];
193 $extra_type = $site_extras[$i][2];
194 if ( $extras[$extra_name]['cal_name'] != "" ) {
195 $tag = preg_replace ( "/[^A-Za-z0-9]+/", "", translate ( $extra_descr ) );
196 $tag = strtolower ( $tag );
197 $tagname = str_replace ( '"', '', $extra_name );
198 echo " <siteExtra>\n";
199 echo " <number>$i</number>\n";
200 echo " <name>" . escapeXml ( $extra_name ) . "</name>\n";
201 echo " <description>" . escapeXml ( $extra_descr ) . "</description>\n";
202 echo " <type>" . $extra_type . "</type>\n";
204 if ( $extra_type == $EXTRA_DATE ) {
205 //echo date_to_str ( $extras[$extra_name]['cal_date'] );
206 echo $extras[$extra_name]['cal_date'];
207 } else if ( $extra_type == $EXTRA_MULTILINETEXT ) {
208 echo escapeXml ( $extras[$extra_name]['cal_data'] );
209 } else if ( $extra_type == $EXTRA_REMINDER ) {
210 echo ( $extras[$extra_name]['cal_remind'] > 0 ?
211 translate("Yes") : translate("No") );
213 // default method for $EXTRA_URL, $EXTRA_TEXT, etc...
214 echo escapeXml ( $extras[$extra_name]['cal_data'] );
216 echo "</value>\n </siteExtra>\n";
219 echo " </siteExtras>\n";
220 if ( $single_user != "Y" && ! $disable_participants_field ) {
221 echo " <participants>\n";
222 for ( $i = 0; $i < count ( $participants ); $i++ ) {
223 echo " <participant>" . $participants[$i] .
226 for ( $i = 0; $i < count ( $ext_participants ); $i++ ) {
227 echo " <participant>" . $ext_participants[$i] .
230 echo " </participants>\n";
237 // Process an event for a single day. Check to see if it has
238 // a reminder, when it needs to be sent and when the last time it
240 function process_event ( $id, $name, $event_date, $event_time ) {
244 printf ( "Event %d: \"%s\" at %s on %s \n",
245 $id, $name, $event_time, $event_date );
247 print_event_xml ( $id, $event_date );
251 echo "<!-- events for user \"$user\", login \"$login\" -->\n";
252 echo "<!-- date range: $startdate - $enddate -->\n";
254 $startyear = substr ( $startdate, 0, 4 );
255 $startmonth = substr ( $startdate, 4, 2 );
256 $startday = substr ( $startdate, 6, 2 );
257 $endyear = substr ( $enddate, 0, 4 );
258 $endmonth = substr ( $enddate, 4, 2 );
259 $endday = substr ( $enddate, 6, 2 );
261 $starttime = mktime ( 3, 0, 0, $startmonth, $startday, $startyear );
262 $endtime = mktime ( 3, 0, 0, $endmonth, $endday, $endyear );
264 for ( $d = $starttime; $d <= $endtime; $d += $ONE_DAY ) {
265 $completed_ids = array ();
266 $date = date ( "Ymd", $d );
267 //echo "Date: $date\n";
268 // Get non-repeating events for this date.
269 // An event will be included one time for each participant.
270 $ev = get_entries ( $user, $date );
271 // Keep track of duplicates
272 $completed_ids = array ( );
273 for ( $i = 0; $i < count ( $ev ); $i++ ) {
274 $id = $ev[$i]['cal_id'];
275 if ( ! empty ( $completed_ids[$id] ) )
277 $completed_ids[$id] = 1;
278 process_event ( $id, $ev[$i]['cal_name'], $date, $ev[$i]['cal_time'] );
280 $rep = get_repeating_entries ( $user, $date );
281 for ( $i = 0; $i < count ( $rep ); $i++ ) {
282 $id = $rep[$i]['cal_id'];
283 if ( ! empty ( $completed_ids[$id] ) )
285 $completed_ids[$id] = 1;
286 process_event ( $id, $rep[$i]['cal_name'], $date, $rep[$i]['cal_time'] );