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