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