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