]> git.mxchange.org Git - friendica.git/blob - mod/cal.php
Issue-#3873
[friendica.git] / mod / cal.php
1 <?php
2 /**
3  * @file mod/cal.php
4  * @brief The calendar module
5  *      This calendar is for profile visitors and contains only the events
6  *      of the profile owner
7  */
8
9 use Friendica\App;
10 use Friendica\Core\Config;
11 use Friendica\Core\PConfig;
12 use Friendica\Core\System;
13
14 require_once('include/event.php');
15 require_once('include/redir.php');
16
17 function cal_init(App $a) {
18         if($a->argc > 1)
19                 auto_redir($a, $a->argv[1]);
20
21         if((Config::get('system','block_public')) && (! local_user()) && (! remote_user())) {
22                 return;
23         }
24
25         nav_set_selected('events');
26
27         $o = '';
28
29         if($a->argc > 1) {
30                 $nick = $a->argv[1];
31                 $user = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 LIMIT 1",
32                         dbesc($nick)
33                 );
34
35                 if(! count($user))
36                         return;
37
38                 $a->data['user'] = $user[0];
39                 $a->profile_uid = $user[0]['uid'];
40
41                 // if it's a json request abort here becaus we don't
42                 // need the widget data
43                 if ($a->argv[2] === 'json')
44                         return;
45
46                 $profile = get_profiledata_by_nick($nick, $a->profile_uid);
47
48                 $account_type = account_type($profile);
49
50                 $tpl = get_markup_template("vcard-widget.tpl");
51
52                 $vcard_widget .= replace_macros($tpl, array(
53                         '$name' => $profile['name'],
54                         '$photo' => $profile['photo'],
55                         '$addr' => (($profile['addr'] != "") ? $profile['addr'] : ""),
56                         '$account_type' => $account_type,
57                         '$pdesc' => (($profile['pdesc'] != "") ? $profile['pdesc'] : ""),
58                 ));
59
60                 $cal_widget = widget_events();
61
62                 if(! x($a->page,'aside'))
63                         $a->page['aside'] = '';
64
65                 $a->page['aside'] .= $vcard_widget;
66                 $a->page['aside'] .= $cal_widget;
67         }
68
69         return;
70 }
71
72 function cal_content(App $a) {
73         nav_set_selected('events');
74
75         // First day of the week (0 = Sunday)
76         $firstDay = PConfig::get(local_user(),'system','first_day_of_week');
77         if ($firstDay === false) $firstDay=0;
78
79         // get the translation strings for the callendar
80         $i18n = get_event_strings();
81
82         $htpl = get_markup_template('event_head.tpl');
83         $a->page['htmlhead'] .= replace_macros($htpl,array(
84                 '$baseurl' => System::baseUrl(),
85                 '$module_url' => '/cal/' . $a->data['user']['nickname'],
86                 '$modparams' => 2,
87                 '$i18n' => $i18n,
88         ));
89
90         $etpl = get_markup_template('event_end.tpl');
91         $a->page['end'] .= replace_macros($etpl,array(
92                 '$baseurl' => System::baseUrl(),
93         ));
94
95         $o ="";
96
97         $mode = 'view';
98         $y = 0;
99         $m = 0;
100         $ignored = ((x($_REQUEST,'ignored')) ? intval($_REQUEST['ignored']) : 0);
101
102         if($a->argc == 4) {
103                 if($a->argv[2] == 'export') {
104                         $mode = 'export';
105                         $format = $a->argv[3];
106                 }
107         }
108
109         //
110         // Setup permissions structures
111         //
112
113         $contact = null;
114         $remote_contact = false;
115         $contact_id = 0;
116
117         $owner_uid = $a->data['user']['uid'];
118         $nick = $a->data['user']['nickname'];
119
120         if(is_array($_SESSION['remote'])) {
121                 foreach($_SESSION['remote'] as $v) {
122                         if($v['uid'] == $a->profile['profile_uid']) {
123                                 $contact_id = $v['cid'];
124                                 break;
125                         }
126                 }
127         }
128         if($contact_id) {
129                 $groups = init_groups_visitor($contact_id);
130                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
131                         intval($contact_id),
132                         intval($a->profile['profile_uid'])
133                 );
134                 if (dbm::is_result($r)) {
135                         $contact = $r[0];
136                         $remote_contact = true;
137                 }
138         }
139         if(! $remote_contact) {
140                 if(local_user()) {
141                         $contact_id = $_SESSION['cid'];
142                         $contact = $a->contact;
143                 }
144         }
145         $is_owner = ((local_user()) && (local_user() == $a->profile['profile_uid']) ? true : false);
146
147         if($a->profile['hidewall'] && (! $is_owner) && (! $remote_contact)) {
148                 notice( t('Access to this profile has been restricted.') . EOL);
149                 return;
150         }
151
152         // get the permissions
153         $sql_perms = item_permissions_sql($owner_uid,$remote_contact,$groups);
154         // we only want to have the events of the profile owner
155         $sql_extra = " AND `event`.`cid` = 0 " . $sql_perms;
156
157         // get the tab navigation bar
158         $tabs .= profile_tabs($a,false, $a->data['user']['nickname']);
159
160         // The view mode part is similiar to /mod/events.php
161         if($mode == 'view') {
162
163
164                 $thisyear = datetime_convert('UTC',date_default_timezone_get(),'now','Y');
165                 $thismonth = datetime_convert('UTC',date_default_timezone_get(),'now','m');
166                 if(! $y)
167                         $y = intval($thisyear);
168                 if(! $m)
169                         $m = intval($thismonth);
170
171                 // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
172                 // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
173
174                 if($y < 1901)
175                         $y = 1900;
176                 if($y > 2099)
177                         $y = 2100;
178
179                 $nextyear = $y;
180                 $nextmonth = $m + 1;
181                 if($nextmonth > 12) {
182                                 $nextmonth = 1;
183                         $nextyear ++;
184                 }
185
186                 $prevyear = $y;
187                 if($m > 1)
188                         $prevmonth = $m - 1;
189                 else {
190                         $prevmonth = 12;
191                         $prevyear --;
192                 }
193
194                 $dim    = get_dim($y,$m);
195                 $start  = sprintf('%d-%d-%d %d:%d:%d',$y,$m,1,0,0,0);
196                 $finish = sprintf('%d-%d-%d %d:%d:%d',$y,$m,$dim,23,59,59);
197
198
199                 if ($a->argv[2] === 'json'){
200                         if (x($_GET,'start'))   $start = $_GET['start'];
201                         if (x($_GET,'end'))     $finish = $_GET['end'];
202                 }
203
204                 $start  = datetime_convert('UTC','UTC',$start);
205                 $finish = datetime_convert('UTC','UTC',$finish);
206
207                 $adjust_start = datetime_convert('UTC', date_default_timezone_get(), $start);
208                 $adjust_finish = datetime_convert('UTC', date_default_timezone_get(), $finish);
209
210                 // put the event parametes in an array so we can better transmit them
211                 $event_params = array(
212                         'event_id' => (x($_GET,'id') ? $_GET["id"] : 0),
213                         'start' => $start,
214                         'finish' => $finish,
215                         'adjust_start' => $adjust_start,
216                         'adjust_finish' => $adjust_finish,
217                         'ignored' => $ignored,
218                 );
219
220                 // get events by id or by date
221                 if (x($_GET,'id')){
222                         $r = event_by_id($owner_uid, $event_params, $sql_extra);
223                 } else {
224                         $r = events_by_date($owner_uid, $event_params, $sql_extra);
225                 }
226
227                 $links = array();
228
229                 if (dbm::is_result($r)) {
230                         $r = sort_by_date($r);
231                         foreach ($r as $rr) {
232                                 $j = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['start'], 'j') : datetime_convert('UTC','UTC',$rr['start'],'j'));
233                                 if (! x($links,$j)) {
234                                         $links[$j] = System::baseUrl() . '/' . $a->cmd . '#link-' . $j;
235                                 }
236                         }
237                 }
238
239
240                 $events=array();
241
242                 // transform the event in a usable array
243                 if (dbm::is_result($r))
244                         $r = sort_by_date($r);
245                         $events = process_events($r);
246
247                 if ($a->argv[2] === 'json'){
248                         echo json_encode($events); killme();
249                 }
250
251                 // links: array('href', 'text', 'extra css classes', 'title')
252                 if (x($_GET,'id')){
253                         $tpl =  get_markup_template("event.tpl");
254                 } else {
255 //                      if (Config::get('experimentals','new_calendar')==1){
256                                 $tpl = get_markup_template("events_js.tpl");
257 //                      } else {
258 //                              $tpl = get_markup_template("events.tpl");
259 //                      }
260                 }
261
262                 // Get rid of dashes in key names, Smarty3 can't handle them
263                 foreach($events as $key => $event) {
264                         $event_item = array();
265                         foreach($event['item'] as $k => $v) {
266                                 $k = str_replace('-','_',$k);
267                                 $event_item[$k] = $v;
268                         }
269                         $events[$key]['item'] = $event_item;
270                 }
271
272                 $o = replace_macros($tpl, array(
273                         '$baseurl'      => System::baseUrl(),
274                         '$tabs'         => $tabs,
275                         '$title'        => t('Events'),
276                         '$view'         => t('View'),
277                         '$previous'     => array(System::baseUrl()."/events/$prevyear/$prevmonth", t('Previous'),'',''),
278                         '$next'         => array(System::baseUrl()."/events/$nextyear/$nextmonth", t('Next'),'',''),
279                         '$calendar' => cal($y,$m,$links, ' eventcal'),
280
281                         '$events'       => $events,
282
283                         "today" => t("today"),
284                         "month" => t("month"),
285                         "week" => t("week"),
286                         "day" => t("day"),
287                         "list" => t("list"),
288                 ));
289
290                 if (x($_GET,'id')){ echo $o; killme(); }
291
292                 return $o;
293         }
294
295         if($mode == 'export') {
296                 if(! (intval($owner_uid))) {
297                         notice( t('User not found'));
298                         return;
299                 }
300
301                 // Test permissions
302                 // Respect the export feature setting for all other /cal pages if it's not the own profile
303                 if( ((local_user() !== intval($owner_uid))) && ! feature_enabled($owner_uid, "export_calendar")) {
304                         notice( t('Permission denied.') . EOL);
305                         goaway('cal/' . $nick);
306                 }
307
308                 // Get the export data by uid
309                 $evexport = event_export($owner_uid, $format);
310
311                 if (!$evexport["success"]) {
312                         if($evexport["content"])
313                                 notice( t('This calendar format is not supported') );
314                         else
315                                 notice( t('No exportable data found'));
316
317                         // If it the own calendar return to the events page
318                         // otherwise to the profile calendar page
319                         if (local_user() === intval($owner_uid))
320                                 $return_path = "events";
321                         else
322                                 $returnpath = "cal/".$nick;
323
324                         goaway($return_path);
325                 }
326
327                 // If nothing went wrong we can echo the export content
328                 if ($evexport["success"]) {
329                         header('Content-type: text/calendar');
330                         header('content-disposition: attachment; filename="' . t('calendar') . '-' . $nick . '.' . $evexport["extension"] . '"' );
331                         echo $evexport["content"];
332                         killme();
333                 }
334
335                 return;
336         }
337 }