]> git.mxchange.org Git - friendica.git/blob - mod/cal.php
Merge pull request #2624 from annando/1606-contact-id
[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(&$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                 if((intval($profile['page-flags']) == PAGE_COMMUNITY) || (intval($profile['page-flags']) == PAGE_PRVGROUP))
44                         $account_type = t('Forum');
45                 else
46                         $account_type = "";
47
48                 $tpl = get_markup_template("vcard-widget.tpl");
49
50                 $vcard_widget .= replace_macros($tpl, array(
51                         '$name' => $profile['name'],
52                         '$photo' => $profile['photo'],
53                         '$addr' => (($profile['addr'] != "") ? $profile['addr'] : ""),
54                         '$account_type' => $account_type,
55                         '$pdesc' => (($profile['pdesc'] != "") ? $profile['pdesc'] : ""),
56                 ));
57
58                 $cal_widget = widget_events();
59
60                 if(! x($a->page,'aside'))
61                         $a->page['aside'] = '';
62
63                 $a->page['aside'] .= $vcard_widget;
64                 $a->page['aside'] .= $cal_widget;
65         }
66
67         return;
68 }
69
70 function cal_content(&$a) {
71         nav_set_selected('events');
72
73         $editselect = 'none';
74         if( feature_enabled(local_user(), 'richtext') )
75                 $editselect = 'textareas';
76
77         // First day of the week (0 = Sunday)
78         $firstDay = get_pconfig(local_user(),'system','first_day_of_week');
79         if ($firstDay === false) $firstDay=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' => $a->get_baseurl(),
87                 '$module_url' => '/cal/' . $a->data['user']['nickname'],
88                 '$modparams' => 2,
89                 '$i18n' => $i18n,
90                 '$editselect' => $editselect
91         ));
92
93         $etpl = get_markup_template('event_end.tpl');
94         $a->page['end'] .= replace_macros($etpl,array(
95                 '$baseurl' => $a->get_baseurl(),
96                 '$editselect' => $editselect
97         ));
98
99         $o ="";
100
101         $mode = 'view';
102         $y = 0;
103         $m = 0;
104         $ignored = ((x($_REQUEST,'ignored')) ? intval($_REQUEST['ignored']) : 0);
105
106         if($a->argc == 4) {
107                 if($a->argv[2] == 'export') {
108                         $mode = 'export';
109                         $format = $a->argv[3];
110                 }
111         }
112
113         //
114         // Setup permissions structures
115         //
116
117         $contact = null;
118         $remote_contact = false;
119         $contact_id = 0;
120
121         $owner_uid = $a->data['user']['uid'];
122         $nick = $a->data['user']['nickname'];
123
124         if(is_array($_SESSION['remote'])) {
125                 foreach($_SESSION['remote'] as $v) {
126                         if($v['uid'] == $a->profile['profile_uid']) {
127                                 $contact_id = $v['cid'];
128                                 break;
129                         }
130                 }
131         }
132         if($contact_id) {
133                 $groups = init_groups_visitor($contact_id);
134                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
135                         intval($contact_id),
136                         intval($a->profile['profile_uid'])
137                 );
138                 if(count($r)) {
139                         $contact = $r[0];
140                         $remote_contact = true;
141                 }
142         }
143         if(! $remote_contact) {
144                 if(local_user()) {
145                         $contact_id = $_SESSION['cid'];
146                         $contact = $a->contact;
147                 }
148         }
149         $is_owner = ((local_user()) && (local_user() == $a->profile['profile_uid']) ? true : false);
150
151         if($a->profile['hidewall'] && (! $is_owner) && (! $remote_contact)) {
152                 notice( t('Access to this profile has been restricted.') . EOL);
153                 return;
154         }
155
156         $sql_extra = item_permissions_sql($owner_uid,$remote_contact,$groups);
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 = date("Y-m-d h:i:s", $_GET['start']);
202                         if (x($_GET,'end'))     $finish = date("Y-m-d h:i:s", $_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(count($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] = $a->get_baseurl() . '/' . $a->cmd . '#link-' . $j;
236                         }
237                 }
238
239
240                 $events=array();
241
242                 // transform the event in a usable array
243                 if(count($r))
244                         $r = sort_by_date($r);
245                         $events = process_events($r);
246
247                 if ($a->argv[2] === 'json'){
248                         echo json_encode($events); killme();
249                 }
250
251                 // links: array('href', 'text', 'extra css classes', 'title')
252                 if (x($_GET,'id')){
253                         $tpl =  get_markup_template("event.tpl");
254                 } else {
255 //                      if (get_config('experimentals','new_calendar')==1){
256                                 $tpl = get_markup_template("events-js.tpl");
257 //                      } else {
258 //                              $tpl = get_markup_template("events.tpl");
259 //                      }
260                 }
261
262                 // Get rid of dashes in key names, Smarty3 can't handle them
263                 foreach($events as $key => $event) {
264                         $event_item = array();
265                         foreach($event['item'] as $k => $v) {
266                                 $k = str_replace('-','_',$k);
267                                 $event_item[$k] = $v;
268                         }
269                         $events[$key]['item'] = $event_item;
270                 }
271
272                 $o = replace_macros($tpl, array(
273                         '$baseurl'      => $a->get_baseurl(),
274                         '$tabs'         => $tabs,
275                         '$title'        => t('Events'),
276                         '$view'         => t('View'),
277                         '$previus'      => array($a->get_baseurl()."/events/$prevyear/$prevmonth",t('Previous'),'',''),
278                         '$next'         => array($a->get_baseurl()."/events/$nextyear/$nextmonth",t('Next'),'',''),
279                         '$calendar' => cal($y,$m,$links, ' eventcal'),
280
281                         '$events'       => $events,
282
283                         "today" => t("today"),
284                         "month" => t("month"),
285                         "week" => t("week"),
286                         "day" => t("day"),
287
288
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                 if(! (feature_enabled($owner_uid, "export_calendar"))) {
303                         notice( t('Permission denied.') . EOL);
304                         return;
305                 }
306
307                 // Get the export data by uid
308                 $evexport = event_export($owner_uid, $format);
309
310                 if ($evexport["success"] == false ) {
311                         if($evexport["content"])
312                                 notice( t('This calendar format is not supported') );
313                         else
314                                 notice( t('No exportable data found'));
315
316                         return;
317                 }
318
319                 // If nothing went wrong we can echo the export content
320                 if ($evexport["success"] == true ) {
321                         header('Content-type: text/calendar');
322                         header('content-disposition: attachment; filename="' . t('calendar') . '-' . $nick . '.' . $evexport["extension"] . '"' );
323                         echo $evexport["content"];
324                         killme();
325                 }
326
327                 return;
328         }
329 }