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