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