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