PageRenderTime 71ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/includes/functions_calendar.php

https://github.com/MightyGorgon/icy_phoenix
PHP | 1072 lines | 776 code | 130 blank | 166 comment | 134 complexity | b101cf2af8a4289cf745fac08ad8f809 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * @package Icy Phoenix
  5. * @version $Id$
  6. * @copyright (c) 2008 Icy Phoenix
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. /**
  11. *
  12. * @Extra credits for this file
  13. * Ptirhiik (admin@rpgnet-fr.com)
  14. *
  15. */
  16. if (!defined('IN_ICYPHOENIX'))
  17. {
  18. die('Hacking attempt');
  19. }
  20. include_once(IP_ROOT_PATH . './includes/functions_post.' . PHP_EXT);
  21. if (!class_exists('bbcode')) include(IP_ROOT_PATH . 'includes/bbcode.' . PHP_EXT);
  22. if (empty($bbcode)) $bbcode = new bbcode();
  23. function calendar_forum_select($selected_id = '')
  24. {
  25. $forum_list = '<select name="selected_id" onchange="forms[\'f_calendar\'].submit();">' . get_tree_option($selected_id) . '</select>';
  26. return $forum_list;
  27. }
  28. // translate a date for display
  29. function date_dsp($format, $date)
  30. {
  31. global $config, $lang;
  32. static $translate;
  33. if (empty($translate) && $config['default_lang'] != 'english')
  34. {
  35. @reset($lang['datetime']);
  36. while (list($match, $replace) = @each($lang['datetime']))
  37. {
  38. $translate[$match] = $replace;
  39. }
  40. }
  41. return (!empty($translate)) ? strtr(gmdate($format, $date), $translate) : gmdate($format, $date);
  42. }
  43. function get_calendar_title_date($calendar_start, $calendar_duration)
  44. {
  45. global $lang, $images, $config, $user;
  46. global $bbcode;
  47. if (empty($calendar_start)) return '';
  48. // get the component of the date and duration
  49. $year = 0;
  50. $month = 0;
  51. $day = 0;
  52. $hour = 0;
  53. $min = 0;
  54. $d_day = 0;
  55. $d_hour = 0;
  56. $d_min = 0;
  57. if (!empty($calendar_start))
  58. {
  59. $year = intval(gmdate('Y', $calendar_start));
  60. $month = intval(gmdate('m', $calendar_start));
  61. $day = intval(gmdate('d', $calendar_start));
  62. $hour = intval(gmdate('H', $calendar_start));
  63. $min = intval(gmdate('i', $calendar_start));
  64. if (!empty($calendar_duration))
  65. {
  66. $d_dur = intval($calendar_duration);
  67. $d_day = intval($d_dur / 86400);
  68. $d_dur = $d_dur - 86400 * $d_day;
  69. $d_hour = intval($d_dur / 3600);
  70. $d_dur = $d_dur - 3600 * $d_hour;
  71. $d_min = intval($d_dur / 60);
  72. }
  73. }
  74. // quit if no date
  75. if (empty($year) || empty($month) || empty($day)) return '';
  76. // raz duration less than 1 day if no time for event start
  77. if (empty($hour) && empty($min))
  78. {
  79. $d_hour = 0;
  80. $d_min = 0;
  81. }
  82. // add the time to start date if present
  83. $fmt_start = $lang['DATE_FORMAT_CALENDAR'];
  84. if (!empty($hour))
  85. {
  86. $fmt_start = $config['default_dateformat'];
  87. }
  88. // add the time to end date if duration
  89. $fmt_end = $lang['DATE_FORMAT_CALENDAR'];
  90. if (!empty($hour) || !empty($d_hour))
  91. {
  92. $fmt_end = $config['default_dateformat'];
  93. }
  94. // apply it to dates
  95. $date_start = date_dsp($fmt_start, $calendar_start);
  96. $date_end = date_dsp($fmt_end, $calendar_start + $calendar_duration);
  97. // add period to the title
  98. $calendar_icon = '<a href="' . append_sid(IP_ROOT_PATH . 'calendar.' . PHP_EXT . '?start=' . gmdate('Ymd', $calendar_start)). '"><img src="' . $images['icon_calendar'] . '" hspace="3" style="vertical-align: top;" alt="' . $lang['Calendar_event'] . '" /></a>';
  99. if (empty($calendar_duration))
  100. {
  101. $res = sprintf($lang['Calendar_time'], $date_start);
  102. }
  103. else
  104. {
  105. $res = sprintf($lang['Calendar_from_to'], $date_start, $date_end);
  106. }
  107. return $res;
  108. }
  109. function get_calendar_title($calendar_start, $calendar_duration)
  110. {
  111. global $bbcode;
  112. if (empty($calendar_start)) return '';
  113. $calendar_title = get_calendar_title_date($calendar_start, $calendar_duration);
  114. if (empty($calendar_title)) return '';
  115. // send back the full title
  116. $res = '<span class="gensmall"><br />' . $calendar_title . '</span>';
  117. return $res;
  118. }
  119. /*
  120. * Return true if the year is a leap year
  121. * You can also use this one... but it works only for valid UNIX TIMESTAMP... if you want to check year 3245 for example, it won't work...
  122. * $d_isleapyear = gmdate('L', gmmktime(0, 0, 0, $myMonth, 1, $myYear)); // is YYYY a leapyear?
  123. */
  124. function is_leap_year($year)
  125. {
  126. if(($year % 400) == 0)
  127. {
  128. return true;
  129. }
  130. elseif(($year % 100) == 0)
  131. {
  132. return false;
  133. }
  134. elseif(($year % 4) == 0)
  135. {
  136. return true;
  137. }
  138. else
  139. {
  140. return false;
  141. }
  142. }
  143. //------------------------------------------------------------------
  144. // Event management : all events are stored in the array events
  145. // ----------------
  146. // structure of this array :
  147. //
  148. //
  149. // event_id : letter + id : ie u2 = User, user_id=2
  150. //
  151. // event_author_id : id of the author of the event (for topic : topic poster)
  152. // event_author : name of the event author
  153. // event_time : date-time of the event creation
  154. //
  155. // event_last_author_id : for topics : author id of the last reply
  156. // event_last_author : for topics : author name of the last reply
  157. // event_last_time : for topics : date-time of creation of the last reply
  158. //
  159. // event_replies : for topics : number of replies
  160. // event_views : for topics : number of views
  161. // event_type : for topics : topic type
  162. // event_status : for topics : topic status
  163. // event_moved_id : for topics : topic moved id
  164. // event_last_id : for topics : last post id
  165. // event_forum_id : for topics : forum id
  166. //
  167. // event_icon : icon for the event title
  168. // event_title : title of the event
  169. // event_short_title short title of the event (according to the number of char allowed)
  170. // event_message : full message (will be used as the overview flying window)
  171. // event_calendar_time : start date-time of the event
  172. // event_calendar_duration : duration of the event (in seconds)
  173. //
  174. // event_link : link to what should be called when clicking to the link
  175. // event_txt_class : class of CSS used to display the title in the calendar cells
  176. // event_type_icon : icon set to recognize a type of event in the calendar (full HTML <img src="...)
  177. //------------------------------------------------------------------
  178. //
  179. // topics
  180. //
  181. function get_event_topics(&$events, &$number, $start_date, $end_date, $limit = false, $start = 0, $max_limit = -1, $fid = '')
  182. {
  183. global $tree, $template, $lang, $images, $user, $db, $cache, $config, $bbcode;
  184. if (!class_exists('bbcode')) include(IP_ROOT_PATH . 'includes/bbcode.' . PHP_EXT);
  185. if (empty($bbcode)) $bbcode = new bbcode();
  186. // get some parameter
  187. $topic_title_length = isset($config['calendar_title_length']) ? intval($config['calendar_title_length']) : 30;
  188. $topic_text_length = isset($config['calendar_text_length']) ? intval($config['calendar_text_length']) : 200;
  189. if ($max_limit < 0)
  190. {
  191. $max_limit = $config['topics_per_page'];
  192. }
  193. // get the forums authorized (compliency with categories hierarchy v2 mod)
  194. $cat_hierarchy = function_exists(get_auth_keys);
  195. $s_forums_ids = '';
  196. if (!$cat_hierarchy)
  197. {
  198. // standard read
  199. $is_auth = array();
  200. $is_auth = auth(AUTH_ALL, AUTH_LIST_ALL, $user->data);
  201. // forum or cat asked
  202. $is_ask = array();
  203. if (($fid == 'Root') || ($fid == POST_CAT_URL . 0))
  204. {
  205. $fid = '';
  206. }
  207. if (!empty($fid))
  208. {
  209. $type = substr($fid, 0, 1);
  210. $id = intval(substr($fid, 1));
  211. if ($type == POST_CAT_URL)
  212. {
  213. $sql = "SELECT forum_id FROM " . FORUMS_TABLE . " WHERE parent_id = '" . $id . "'";
  214. $result = $db->sql_query($sql);
  215. while ($row = $db->sql_fetchrow($result))
  216. {
  217. $is_ask[$row['forum_id']] = true;
  218. }
  219. $db->sql_freeresult($result);
  220. }
  221. elseif ($type == POST_FORUM_URL)
  222. {
  223. $is_ask[$id] = true;
  224. }
  225. else
  226. {
  227. return;
  228. }
  229. }
  230. // get the list of authorized forums
  231. while (list($forum_id, $forum_auth) = each($is_auth))
  232. {
  233. if ($forum_auth['auth_read'] && (empty($fid) || isset($is_ask[$forum_id])))
  234. {
  235. $s_forum_ids .= (empty($s_forum_ids) ? '' : ', ') . $forum_id;
  236. }
  237. }
  238. }
  239. else
  240. {
  241. if (empty($fid) || ($fid == POST_CAT_URL . 0))
  242. {
  243. $fid = 'Root';
  244. }
  245. // get auth key
  246. $keys = array();
  247. $keys = get_auth_keys($fid, true, -1, -1, 'auth_read');
  248. for ($i = 0; $i < sizeof($keys['id']); $i++)
  249. {
  250. if (($tree['type'][$keys['idx'][$i]] == POST_FORUM_URL) && $tree['auth'][ $keys['id'][$i] ]['auth_read'])
  251. {
  252. $s_forum_ids .= (empty($s_forum_ids) ? '' : ', ') . $tree['id'][$keys['idx'][$i]];
  253. }
  254. }
  255. }
  256. // no forums authed, return
  257. if (empty($s_forum_ids))
  258. {
  259. return;
  260. }
  261. // select topics
  262. $sql_forums_field = '';
  263. $sql_forums_file = '';
  264. $sql_forums_match = '';
  265. if (!$cat_hierarchy)
  266. {
  267. $sql_forums_field = ', f.forum_name';
  268. $sql_forums_file = ', ' . FORUMS_TABLE . ' AS f';
  269. $sql_forums_match = ' AND f.forum_id = t.forum_id';
  270. }
  271. $sql = "SELECT
  272. t.*,
  273. p.poster_id, p.post_username, p.post_text, p.enable_bbcode, p.enable_html, p.enable_smilies,
  274. u.username, u.user_active, u.user_color,
  275. lp.poster_id AS lp_poster_id,
  276. lu.username AS lp_username,
  277. lp.post_username AS lp_post_username,
  278. lp.post_time AS lp_post_time
  279. $sql_forums_field
  280. FROM " . TOPICS_TABLE . " AS t, " . POSTS_TABLE . " AS p, " . USERS_TABLE . " AS u, " . POSTS_TABLE . " AS lp, " . USERS_TABLE . " lu $sql_forums_file
  281. WHERE
  282. t.forum_id IN ($s_forum_ids)
  283. AND p.post_id = t.topic_first_post_id
  284. AND u.user_id = p.poster_id
  285. AND lp.post_id = t.topic_last_post_id
  286. AND lu.user_id = lp.poster_id
  287. AND t.topic_calendar_time < $end_date
  288. AND (t.topic_calendar_time + t.topic_calendar_duration) >= $start_date
  289. AND t.topic_status <> " . TOPIC_MOVED . "
  290. $sql_forums_match
  291. ORDER BY
  292. t.topic_calendar_time, t.topic_calendar_duration DESC, t.topic_last_post_id DESC";
  293. $result = $db->sql_query($sql);
  294. // get the number of occurences
  295. $number = $db->sql_numrows($result);
  296. // if limit per page asked, limit the number of results
  297. if ($limit)
  298. {
  299. $db->sql_freeresult($result);
  300. $sql .= " LIMIT $start, $max_limit";
  301. $result = $db->sql_query($sql);
  302. }
  303. $bbcode->allow_html = ($user->data['user_allowhtml'] && $config['allow_html']) ? 1 : 0;
  304. $bbcode->allow_bbcode = ($user->data['user_allowbbcode'] && $config['allow_bbcode']) ? 1 : 0;
  305. $bbcode->allow_smilies = ($user->data['user_allowsmile'] && $config['allow_smilies']) ? 1 : 0;
  306. // read the items
  307. while ($row = $db->sql_fetchrow($result))
  308. {
  309. // prepare the message
  310. $topic_author_id = $row['poster_id'];
  311. $topic_author = ($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username'];
  312. $topic_time = $row['topic_time'];
  313. $topic_last_author_id = $row['lp_poster_id'];
  314. $topic_last_author = ($row['lp_poster_id'] == ANONYMOUS) ? $row['lp_post_username'] : $row['lp_username'];
  315. $topic_last_time = $row['lp_post_time'];
  316. $topic_views = $row['topic_views'];
  317. $topic_replies = $row['topic_replies'];
  318. $topic_icon = $row['topic_icon'];
  319. $topic_title = $row['topic_title'];
  320. $message = htmlspecialchars($row['post_text']);
  321. $topic_calendar_time = $row['topic_calendar_time'];
  322. $topic_calendar_duration = $row['topic_calendar_duration'];
  323. $topic_link = append_sid(IP_ROOT_PATH . CMS_PAGE_VIEWTOPIC . '?' . POST_TOPIC_URL . '=' . $row['topic_id']);
  324. $topic_title = censor_text($topic_title);
  325. $message = censor_text($message);
  326. $short_title = (strlen($topic_title) > $topic_title_length + 3) ? substr($topic_title, 0, $topic_title_length) . '...' : $topic_title;
  327. // Convert and clean special chars!
  328. $topic_title = htmlspecialchars_clean($topic_title);
  329. $short_title = htmlspecialchars_clean($short_title);
  330. // SMILEYS IN TITLE - BEGIN
  331. if ($config['smilies_topic_title'] && !$lofi)
  332. {
  333. $topic_title = $bbcode->parse_only_smilies($topic_title);
  334. $short_title = $bbcode->parse_only_smilies($short_title);
  335. }
  336. // SMILEYS IN TITLE - END
  337. $dsp_topic_icon = '';
  338. if (function_exists('get_icon_title'))
  339. {
  340. $dsp_topic_icon = get_icon_title($topic_icon, 0, POST_CALENDAR);
  341. }
  342. // parse the message
  343. $message = substr($message, 0, $topic_text_length);
  344. // remove HTML if not allowed
  345. if (!$config['allow_html'] && $row['enable_html'])
  346. {
  347. $message = preg_replace('#(<)([\/]?.*?)(>)#is', "&lt;\\2&gt;", $message);
  348. }
  349. $message = $bbcode->parse($message);
  350. // get the date format
  351. $fmt = $lang['DATE_FORMAT_CALENDAR'];
  352. if (!empty($topic_calendar_duration))
  353. {
  354. $fmt = $config['default_dateformat'];
  355. }
  356. // replace \n with <br />
  357. //$message = preg_replace("/[\n\r]{1,2}/", '<br />', $message);
  358. // build the overview
  359. $sav_tpl = $template->_tpldata;
  360. $det_handler = '_overview_topic_' . $row['topic_id'];
  361. $template->set_filenames(array($det_handler => 'calendar_overview_topic.tpl'));
  362. $nav_desc = '';
  363. if ($cat_hierarchy)
  364. {
  365. $nav_desc = make_cat_nav_tree(POST_FORUM_URL . $row['forum_id'], '', '', 'gensmall');
  366. }
  367. else
  368. {
  369. $nav_desc = '<a href="' . append_sid(IP_ROOT_PATH . CMS_PAGE_VIEWFORUM . '?' . POST_FORUM_URL . '=' . $row['forum_id']) . '" class="gensmall">' . $row['forum_name'] . '</a>';
  370. }
  371. $template->assign_vars(array(
  372. 'L_CALENDAR_EVENT' => $lang['Calendar_event'],
  373. 'L_AUTHOR' => $lang['Author'],
  374. 'L_TOPIC_DATE' => $lang['Date'],
  375. 'L_FORUM' => $lang['Forum'],
  376. 'L_VIEWS' => $lang['Views'],
  377. 'L_REPLIES' => $lang['Replies'],
  378. 'TOPIC_TITLE' => $dsp_topic_icon . '&nbsp;' . $topic_title,
  379. 'CALENDAR_EVENT' => get_calendar_title_date($topic_calendar_time, $topic_calendar_duration),
  380. 'AUTHOR' => $topic_author,
  381. 'TOPIC_DATE' => create_date($user->data['user_dateformat'], $topic_time, $config['board_timezone']),
  382. 'NAV_DESC' => $nav_desc,
  383. 'CALENDAR_MESSAGE' => $message,
  384. 'VIEWS' => $topic_views,
  385. 'REPLIES' => $topic_replies,
  386. )
  387. );
  388. $template->assign_var_from_handle('_calendar_overview', $det_handler);
  389. $message = $template->_tpldata['.'][0]['_calendar_overview'];
  390. $template->_tpldata = $sav_tpl;
  391. // remove \n remaining from the template
  392. $message = preg_replace("/[\n\r]{1,2}/", '', $message);
  393. // store only the new values
  394. $new_row = array();
  395. $new_row['event_id'] = POST_TOPIC_URL . $row['topic_id'];
  396. $new_row['event_author_id'] = $topic_author_id;
  397. $new_row['event_author'] = $topic_author;
  398. $new_row['event_author_active'] = $row['user_active'];
  399. $new_row['event_author_color'] = $row['user_color'];
  400. $new_row['event_time'] = $topic_time;
  401. $new_row['event_last_author_id'] = $topic_last_author_id;
  402. $new_row['event_last_author'] = $topic_last_author;
  403. $new_row['event_last_time'] = $topic_last_time;
  404. $new_row['event_replies'] = $topic_replies;
  405. $new_row['event_views'] = $topic_views;
  406. $new_row['event_type'] = $row['topic_type'];
  407. $new_row['event_status'] = $row['topic_status'];
  408. $new_row['event_moved_id'] = $row['topic_moved_id'];
  409. $new_row['event_last_id'] = $row['topic_last_post_id'];
  410. $new_row['event_forum_id'] = $row['forum_id'];
  411. $new_row['event_forum_name'] = $row['forum_name'];
  412. $new_row['event_icon'] = $topic_icon;
  413. $new_row['event_title'] = $topic_title;
  414. $new_row['event_short_title'] = $short_title;
  415. $new_row['event_message'] = $message;
  416. $new_row['event_calendar_time'] = $topic_calendar_time;
  417. $new_row['event_calendar_duration'] = $topic_calendar_duration;
  418. $new_row['event_link'] = $topic_link;
  419. $new_row['event_birthday'] = false;
  420. $new_row['event_txt_class'] = 'genmed';
  421. $new_row['event_type_icon'] = '<img src="' . $images['icon_tiny_topic'] . '" style="vertical-align: bottom;" alt="" hspace="2" />';
  422. $events[] = $new_row;
  423. }
  424. $db->sql_freeresult($result);
  425. }
  426. /*
  427. * Get birthdays for calendar
  428. */
  429. function get_birthdays(&$events, &$number, $start_date, $end_date, $year = 0, $year_lt = false, $month = 0, $day = 0, $day_end = 0, $limit = 0, $show_inactive = false)
  430. {
  431. global $lang, $images, $db;
  432. $birthdays_list = array();
  433. $birthdays_list = get_birthdays_list($year, $year_lt, $month, $day, $day_end, $limit, false);
  434. // get the number of occurences
  435. $number = sizeof($birthdays_list);
  436. // read users
  437. for ($i = 0; $i < $number; $i++)
  438. {
  439. $user_id = $birthdays_list[$i]['user_id'];
  440. $username = $birthdays_list[$i]['username'];
  441. $user_birthday = realdate($lang['DATE_FORMAT_CALENDAR'], $birthdays_list[$i]['user_birthday']);
  442. // We cannot use colorize_username because this should be just the url... try to parse the color code instead
  443. $username_colorized = colorize_username($birthdays_list[$i]['user_id'], $birthdays_list[$i]['username'], $birthdays_list[$i]['user_color'], $birthdays_list[$i]['user_active'], true);
  444. $username_color = colorize_username($birthdays_list[$i]['user_id'], $birthdays_list[$i]['username'], $birthdays_list[$i]['user_color'], $birthdays_list[$i]['user_active'], false, true);
  445. // Trim last double quote...
  446. $username_color = (substr($username_color, -1) == '"') ? substr($username_color, 0, -1) : '';
  447. $username_link = append_sid(IP_ROOT_PATH . CMS_PAGE_PROFILE . '?mode=viewprofile&amp;' . POST_USERS_URL . '=' . $user_id) . '" ' . $username_color;
  448. $event_day = realdate('d', $birthdays_list[$i]['user_birthday']);
  449. $event_month = realdate('n', $birthdays_list[$i]['user_birthday']);
  450. $event_year2 = realdate('Y', $birthdays_list[$i]['user_birthday']);
  451. $start_month = intval(gmdate('m', $start_date));
  452. $event_year = intval(gmdate('Y', $start_date));
  453. if ($event_month < $start_month)
  454. {
  455. $event_year++;
  456. }
  457. $event_time = gmmktime(0, 0, 0, $event_month, $event_day, $event_year);
  458. $tmp_message = sprintf($lang['birthday'], $username_colorized);
  459. // It is JavaScript... we need to escape slashes
  460. //$message = htmlspecialchars('<table class="forumline"><tr><td class="row1" nowrap="nowrap"><b>' . $lang['birthday_header'] . '<\/b><span class="topiclink"><\/span><hr \/><span class="genmed">' . $tmp_message . '<\/span><\/td><\/tr><\/table>');
  461. $message = htmlspecialchars('<table class="forumline"><tr><td class="row1" nowrap="nowrap"><b>' . $lang['birthday_header'] . '</b><span class="topiclink"></span><hr /><span class="genmed">' . $tmp_message . '</span></td></tr></table>');
  462. $message = preg_replace("/[\n\r]{1,2}/", '', $message);
  463. $new_row = array();
  464. $new_row['event_id'] = POST_USERS_URL . $user_id;
  465. $new_row['event_author_id'] = $user_id;
  466. $new_row['event_author'] = $username;
  467. $new_row['event_time'] = $event_time;
  468. $new_row['event_last_author_id'] = '';
  469. $new_row['event_last_author'] = '';
  470. $new_row['event_last_time'] = '';
  471. $new_row['event_replies'] = '';
  472. $new_row['event_views'] = '';
  473. $new_row['event_type'] = POST_BIRTHDAY;
  474. $new_row['event_status'] = '';
  475. $new_row['event_moved_id'] = '';
  476. $new_row['event_last_id'] = '';
  477. $new_row['event_forum_id'] = '';
  478. $new_row['event_forum_name'] = '';
  479. $new_row['event_icon'] = '';
  480. $new_row['event_title'] = $username;
  481. $new_row['event_short_title'] = $username;
  482. $new_row['event_message'] = $message;
  483. $new_row['event_calendar_time'] = $event_time;
  484. $new_row['event_calendar_duration'] = '';
  485. $new_row['event_link'] = $username_link;
  486. $new_row['event_birthday'] = true;
  487. $new_row['event_txt_class'] = $txt_class;
  488. $new_row['event_type_icon'] = '<img src="' . $images['icon_tiny_profile'] . '" alt="" hspace="2" />';
  489. $events[] = $new_row;
  490. }
  491. }
  492. /*
  493. * Get birthdays for calendar
  494. */
  495. function get_birthdays_list($year = 0, $year_lt = false, $month = 0, $day = 0, $day_end = 0, $limit = 0, $show_inactive = false)
  496. {
  497. global $db, $cache, $config;
  498. $sql_where = '';
  499. if ($year_lt == false)
  500. {
  501. $sql_where .= ($year > 0) ? (' AND u.user_birthday_y = ' . $year) : '';
  502. }
  503. else
  504. {
  505. $sql_where .= ($year > 0) ? (' AND u.user_birthday_y <= ' . $year) : '';
  506. }
  507. if (($month > 0) && ($day_end > 0))
  508. {
  509. $month_start = (int) $month;
  510. $month_end = (int) $month;
  511. if ($day_end < $day)
  512. {
  513. $month_end = ($month_end == 12) ? 1 : ($month_end + 1);
  514. $sql_where .= ' AND (((u.user_birthday_m = ' . $month_start . ') AND (u.user_birthday_d >= ' . $day . ')) OR ((u.user_birthday_m = ' . $month_end . ') AND (u.user_birthday_d <= ' . $day_end . ')))';
  515. }
  516. else
  517. {
  518. $sql_where .= ' AND u.user_birthday_m = ' . $month;
  519. $sql_where .= ' AND u.user_birthday_d >= ' . $day;
  520. $sql_where .= ' AND u.user_birthday_d <= ' . $day_end;
  521. }
  522. }
  523. else
  524. {
  525. $sql_where .= ($month > 0) ? (' AND u.user_birthday_m = ' . $month) : '';
  526. $sql_where .= ($day > 0) ? (' AND u.user_birthday_d = ' . $day) : '';
  527. }
  528. // If WHERE still empty then query only users with a birthday
  529. $sql_where = ($sql_where == '') ? (' AND u.user_birthday <> 999999') : $sql_where;
  530. if ($show_inactive == false)
  531. {
  532. $sql_where .= (' AND user_active = 1');
  533. }
  534. $sql_limit = ($limit > 0) ? ('LIMIT ' . $limit) : '';
  535. // Changed sorting by username_clean instead of username
  536. $sql = "SELECT u.user_id, u.username, u.user_active, u.user_color, u.user_birthday, u.user_birthday_y, u.user_birthday_m, u.user_birthday_d
  537. FROM " . USERS_TABLE . " AS u
  538. WHERE u.user_id <> " . ANONYMOUS . "
  539. " . $sql_where . "
  540. ORDER BY username_clean
  541. " . $sql_limit;
  542. //$result = $db->sql_query($sql, 0, 'birthdays_list_');
  543. $result = $db->sql_query($sql);
  544. $birthdays_list = array();
  545. // read users
  546. while ($row = $db->sql_fetchrow($result))
  547. {
  548. $birthdays_list[] = $row;
  549. }
  550. $db->sql_freeresult($result);
  551. return $birthdays_list;
  552. }
  553. /**
  554. * Get the birthdays list
  555. */
  556. function get_birthdays_list_full()
  557. {
  558. global $db, $cache, $config;
  559. if (($birthdays_list = $cache->get('_birthdays_list_' . $config['board_timezone'])) === false)
  560. {
  561. $time_now = time();
  562. $date_today = create_date('Ymd', $time_now, $config['board_timezone']);
  563. $date_forward = create_date('Ymd', $time_now + ($config['birthday_check_day'] * 86400), $config['board_timezone']);
  564. $b_year = create_date('Y', $time_now, $config['board_timezone']);
  565. $b_month = create_date('n', $time_now, $config['board_timezone']);
  566. $b_day = create_date('j', $time_now, $config['board_timezone']);
  567. $b_day_end = create_date('j', $time_now + ($config['birthday_check_day'] * 86400), $config['board_timezone']);
  568. $b_limit = 0;
  569. $show_inactive = (empty($config['inactive_users_memberlists']) ? false : true);
  570. $birthdays_list['xdays'] = '';
  571. $birthdays_list['today'] = '';
  572. $birthdays_list_sql = get_birthdays_list($b_year, true, $b_month, $b_day, $b_day_end, $b_limit, $show_inactive);
  573. for ($i = 0; $i < sizeof($birthdays_list_sql); $i++)
  574. {
  575. $user_birthday2 = $b_year . ($user_birthday = realdate('md', $birthdays_list_sql[$i]['user_birthday']));
  576. $birthdays_list_sql[$i]['username'] = stripslashes($birthdays_list_sql[$i]['username']);
  577. if ($user_birthday2 < $date_today)
  578. {
  579. // MG: Why???
  580. $user_birthday2 += 10000;
  581. }
  582. $birthday_username_age = colorize_username($birthdays_list_sql[$i]['user_id'], $birthdays_list_sql[$i]['username'], $birthdays_list_sql[$i]['user_color'], $birthdays_list_sql[$i]['user_active']) . ' (' . (intval($b_year) - intval($birthdays_list_sql[$i]['user_birthday_y'])) . ')';
  583. if (($user_birthday2 > $date_today) && ($user_birthday2 <= $date_forward))
  584. {
  585. // users having birthday within the next days
  586. $birthdays_list['xdays'] .= (($birthdays_list['xdays'] == '') ? ' ' : ', ') . $birthday_username_age;
  587. }
  588. elseif ($user_birthday2 == $date_today)
  589. {
  590. //users having birthday today
  591. $birthdays_list['today'] .= (($birthdays_list['today'] == '') ? ' ' : ', ') . $birthday_username_age;
  592. }
  593. }
  594. $current_time = time();
  595. $cache_expiry = create_date_midnight($current_time, $config['board_timezone']) - $current_time + 86400;
  596. $cache->put('_birthdays_list_' . $config['board_timezone'], $birthdays_list, $cache_expiry);
  597. }
  598. return $birthdays_list;
  599. }
  600. function display_calendar($main_template, $nb_days = 0, $start = 0, $fid = '')
  601. {
  602. global $db, $cache, $config, $user, $template, $images, $lang, $bbcode, $tree;
  603. static $handler;
  604. if (empty($handler))
  605. {
  606. $handler = 1;
  607. }
  608. else
  609. {
  610. $handler++;
  611. }
  612. $day_of_week = array(
  613. $lang['datetime']['Sunday'],
  614. $lang['datetime']['Monday'],
  615. $lang['datetime']['Tuesday'],
  616. $lang['datetime']['Wednesday'],
  617. $lang['datetime']['Thursday'],
  618. $lang['datetime']['Friday'],
  619. $lang['datetime']['Saturday'],
  620. );
  621. $months = array(
  622. ' ------------ ',
  623. $lang['datetime']['January'],
  624. $lang['datetime']['February'],
  625. $lang['datetime']['March'],
  626. $lang['datetime']['April'],
  627. $lang['datetime']['May'],
  628. $lang['datetime']['June'],
  629. $lang['datetime']['July'],
  630. $lang['datetime']['August'],
  631. $lang['datetime']['September'],
  632. $lang['datetime']['October'],
  633. $lang['datetime']['November'],
  634. $lang['datetime']['December'],
  635. );
  636. // get some parameter
  637. $first_day_of_week = isset($config['calendar_week_start']) ? intval($config['calendar_week_start']) : 1;
  638. $nb_row_per_cell = isset($config['calendar_nb_row']) ? intval($config['calendar_nb_row']) : 5;
  639. // get the start date - calendar doesn't go before 1971
  640. $cur_date = (empty($start) || (intval(gmdate('Y', $start)) < 1971)) ? cal_date(time(), $config['board_timezone']) : $start;
  641. $cur_date = gmmktime(0, 0, 0, intval(gmdate('m', $cur_date)), intval(gmdate('d', $cur_date)), intval(gmdate('Y', $cur_date)));
  642. $cur_month = 0;
  643. $cur_day = 0;
  644. // the full month is displayed
  645. if (empty($nb_days))
  646. {
  647. // set indicator
  648. $full_month = true;
  649. // set the start day on the start of the month
  650. $start_date = gmmktime(0, 0, 0, intval(gmdate('m', $cur_date)), 01, intval(gmdate('Y', $cur_date)));
  651. // get the day number set as start of the display
  652. $cfg_week_day_start = $first_day_of_week;
  653. // get the number of blank cells
  654. $start_inc = intval(gmdate('w', $start_date)) - $cfg_week_day_start;
  655. if ($start_inc < 0)
  656. {
  657. $start_inc = 7 + $start_inc;
  658. }
  659. // Used to adjust birthdays SQL
  660. $cur_month = intval(gmdate('n', $cur_date));
  661. // get the end date
  662. $year = intval(gmdate('Y', $start_date));
  663. $month = intval(gmdate('m', $start_date)) + 1;
  664. if ($month > 12)
  665. {
  666. $year++;
  667. $month = 1;
  668. }
  669. $end_date = gmmktime(0, 0, 0, $month, 01, $year);
  670. // set the number of cells per line
  671. $nb_cells = 7;
  672. // get the number of rows
  673. $nb_rows = intval(($start_inc + intval(($end_date - $start_date) / 86400)) / $nb_cells) + 1;
  674. }
  675. else
  676. {
  677. // set indicator
  678. $full_month = false;
  679. // set the start date to the day before the date selected
  680. $start_date = gmmktime(0, 0, 0, gmdate('m', $cur_date), gmdate('d', $cur_date) - 1, gmdate('Y', $cur_date));
  681. // get the day number set as start of the week
  682. $cfg_week_day_start = intval(gmdate('w', $start_date));
  683. // get the numbe of blank cells
  684. $start_inc = 0;
  685. // get the end date
  686. $end_date = gmmktime(0, 0, 0, gmdate('m', $start_date), gmdate('d', $start_date) + $nb_days, gmdate('Y', $start_date));
  687. // set the number of cells per line
  688. $nb_cells = $nb_days;
  689. // set the number of rows
  690. $nb_rows = 1;
  691. }
  692. // Ok, let's get the various events :)
  693. $events = array();
  694. $number = 0;
  695. // topics
  696. get_event_topics($events, $number, $start_date, $end_date, false, 0, -1, $fid);
  697. $pages_array = array('calendar.' . PHP_EXT, CMS_PAGE_FORUM, CMS_PAGE_VIEWFORUM);
  698. //$current_page = $_SERVER['SCRIPT_NAME'];
  699. $current_page = basename($_SERVER['SCRIPT_NAME']);
  700. // No limits in calendar
  701. $day_end = 0;
  702. $birthdays_limit = 0;
  703. if ($current_page != 'calendar.' . PHP_EXT)
  704. {
  705. // Limit total birthdays in forum and viewforum... in large could take forever!
  706. $birthdays_limit = 50;
  707. // We are not in calendar, so we can force date to today!!!
  708. $cur_time = time() + (3600 * $config['board_timezone']);
  709. $cur_month = intval(gmdate('n', $cur_time));
  710. $cur_day = intval(gmdate('j', $cur_time));
  711. // Force one week walk forward...
  712. $days_walk_forward = 7;
  713. $day_end = intval(gmdate('j', $cur_time + ($days_walk_forward * 86400)));
  714. }
  715. if (($config['calendar_birthday'] == true) && in_array(strtolower($current_page), $pages_array))
  716. {
  717. // get_birthdays(&$events, &$number, $start_date, $end_date, $year = 0, $year_lt = false, $month = 0, $day = 0, $day_end = 0, $limit = 0, $show_inactive = false)
  718. get_birthdays($events, $number, $start_date, $end_date, 0, false, $cur_month, $cur_day, $day_end, $birthdays_limit, false);
  719. }
  720. /*
  721. for ($i = 0; $i < sizeof($pages_array); $i++)
  722. {
  723. if (strpos(strtolower($current_page), strtolower($pages_array[$i])) !== false)
  724. {
  725. get_birthdays($events, $number, $start_date, $end_date);
  726. }
  727. }
  728. */
  729. // And now display them
  730. // build a list per date
  731. $map = array();
  732. for ($i = 0; $i < sizeof($events); $i++)
  733. {
  734. $event_time = $events[$i]['event_calendar_time'];
  735. // adjust the event period to the start of day
  736. $event_time_end = $event_time + $events[$i]['event_calendar_duration'];
  737. $event_end = gmmktime(0, 0, 0, intval(gmdate('m', $event_time_end)), intval(gmdate('d', $event_time_end)), intval(gmdate('Y', $event_time_end)));
  738. $event_start = gmmktime(0, 0, 0, intval(gmdate('m', $event_time)), intval(gmdate('d', $event_time)), intval(gmdate('Y', $event_time)));
  739. if ($event_start < $start_date)
  740. {
  741. $event_start = $start_date;
  742. }
  743. if ($event_end > $end_date)
  744. {
  745. $event_end = $end_date;
  746. }
  747. // search a free day map offset in the start day
  748. $event_id = $events[$i]['event_id'];
  749. $offset_date = $event_start;
  750. $map_offset = sizeof($map[$event_start]);
  751. $found = false;
  752. for ($k=0; ($k < sizeof($map[$event_start])) && !$found; $k++)
  753. {
  754. if ($map[$event_start][$k] == -1)
  755. {
  756. $found = true;
  757. $map_offset = $k;
  758. }
  759. }
  760. // mark the offset as used for the whole event period
  761. $offset_date = $event_start;
  762. while ($offset_date <= $event_end)
  763. {
  764. for ($l = sizeof($map[$offset_date]); $l <= $map_offset; $l++)
  765. {
  766. $map[$offset_date][$l] = -1;
  767. }
  768. $map[$offset_date][$map_offset] = $i;
  769. $offset_date = gmmktime(0, 0, 0, gmdate('m', $offset_date), gmdate('d', $offset_date) + 1, gmdate('Y', $offset_date));
  770. }
  771. }
  772. // template
  773. $template->set_filenames(array('_calendar_body' . $handler => 'calendar_box.tpl'));
  774. // buid select list for month
  775. $month = intval(gmdate('m', $start_date));
  776. $s_month = '<select name="start_month" onchange="forms[\'f_calendar\'].submit();">';
  777. for ($i = 1; $i < sizeof($months); $i++)
  778. {
  779. $selected = ($month == $i) ? ' selected="selected"' : '';
  780. $s_month .= '<option value="' . $i . '"' . $selected . '>' . $months[$i] . '</option>';
  781. }
  782. $s_month .= '</select>';
  783. // buid select list for year
  784. $year = intval(gmdate('Y', $start_date));
  785. $s_year = '<select name="start_year" onchange="forms[\'f_calendar\'].submit();">';
  786. for ($i=1971; $i < 2070; $i++)
  787. {
  788. $selected = ($year == $i) ? ' selected="selected"' : '';
  789. $s_year .= '<option value="' . $i . '"' . $selected . '>' . $i . '</option>';
  790. }
  791. $s_year .= '</select>';
  792. // build a forum select list
  793. $s_forum_list = '<select name="selected_id" onchange="forms[\'f_calendar\'].submit();">' . get_tree_option($fid) . '</select>';
  794. // header
  795. $config['calendar_display_open'] = false;
  796. $template->assign_vars(array(
  797. 'UP_ARROW' => $images['cal_up_arrow'],
  798. 'DOWN_ARROW' => $images['cal_down_arrow'],
  799. 'UP_ARROW2' => $images['arrow_up'],
  800. 'DOWN_ARROW2' => $images['arrow_down'],
  801. 'TOGGLE_ICON' => ($config['calendar_display_open'] == false) ? $images['cal_up_arrow'] : $images['cal_down_arrow'],
  802. 'TOGGLE_ICON2' => ($config['calendar_display_open'] == false) ? $images['arrow_up'] : $images['arrow_down'],
  803. 'TOGGLE_STATUS' => ($config['calendar_display_open'] == false) ? 'none' : '',
  804. )
  805. );
  806. $prec = (gmdate('Ym', $start_date) > 197101) ? gmdate('Ymd', gmmktime(0, 0, 0, gmdate('m', $start_date) - 1, 01, gmdate('Y', $start_date))) : gmdate('Ymd', $start_date);
  807. $next = gmdate('Ymd', gmmktime(0, 0, 0, gmdate('m', $start_date)+1, 01, gmdate('Y', $start_date)));
  808. $template->assign_block_vars('_calendar_box', array(
  809. 'L_CALENDAR' => '<a href="' . append_sid(IP_ROOT_PATH . 'calendar.' . PHP_EXT . '?start=' . gmdate('Ymd', cal_date(time(), $config['board_timezone']))) . '"><img src="' . $images['icon_calendar'] . '" hspace="3" style="vertical-align: top;" alt="' . $lang['Calendar_event'] . '" /></a>' . $lang['Calendar'],
  810. 'L_CALENDAR_TXT' => $lang['Calendar'],
  811. 'SPAN_ALL' => $nb_cells,
  812. 'S_MONTH' => $s_month,
  813. 'S_YEAR' => $s_year,
  814. 'S_FORUM_LIST' => $s_forum_list,
  815. 'L_GO' => $lang['Go'],
  816. 'ACTION' => append_sid(IP_ROOT_PATH . 'calendar.' . PHP_EXT),
  817. 'U_PREC' => append_sid('calendar.' . PHP_EXT . '?start=' . $prec . '&amp;fid=' . $fid),
  818. 'U_NEXT' => append_sid('calendar.' . PHP_EXT . '?start=' . $next . '&amp;fid=' . $fid),
  819. )
  820. );
  821. if ($full_month)
  822. {
  823. $template->assign_block_vars('_calendar_box.switch_full_month', array());
  824. $offset = $cfg_week_day_start;
  825. for ($j=0; $j < $nb_cells; $j++)
  826. {
  827. if ($offset >= sizeof($day_of_week)) $offset = 0;
  828. $template->assign_block_vars('_calendar_box.switch_full_month._cell', array(
  829. 'WIDTH' => floor(100 / $nb_cells),
  830. 'L_DAY' => $day_of_week[$offset],
  831. )
  832. );
  833. $offset++;
  834. }
  835. }
  836. else
  837. {
  838. $template->assign_block_vars('_calendar_box.switch_full_month_no', array());
  839. }
  840. // display
  841. $offset_date = gmmktime(0, 0, 0, gmdate('m', $start_date), gmdate('d', $start_date) - $start_inc, gmdate('Y', $start_date));
  842. for ($i = 0; $i < $nb_rows; $i++)
  843. {
  844. $template->assign_block_vars('_calendar_box._row', array());
  845. for ($j = 0; $j < $nb_cells; $j++)
  846. {
  847. $span = 1;
  848. // date less than start
  849. if (intval(gmdate('Ymd', $offset_date)) < intval(gmdate('Ymd', $start_date)))
  850. {
  851. // compute the cell to span
  852. $span = $start_inc;
  853. $j = $start_inc - 1;
  854. $offset_date = gmmktime(0, 0, 0, gmdate('m', $start_date), gmdate('d', $start_date) - 1, gmdate('Y', $start_date));
  855. }
  856. // date greater than last
  857. if (intval(gmdate('Ymd', $offset_date)) >= intval(gmdate('Ymd', $end_date)))
  858. {
  859. // compute the cell to span
  860. $span = $nb_cells-$j;
  861. $j = $nb_cells;
  862. }
  863. $format = (intval(gmdate('Ymd', $offset_date)) == intval(gmdate('Ymd', cal_date(time(), $config['board_timezone'])))) ? '<b>%s</b>' : '%s';
  864. $template->assign_block_vars('_calendar_box._row._cell', array(
  865. 'WIDTH' => floor(100 / $nb_cells),
  866. 'SPAN' => $span,
  867. 'DATE' => sprintf($format, date_dsp(($full_month ? '' : 'D ') . $lang['DATE_FORMAT_CALENDAR'], $offset_date)),
  868. 'U_DATE' => append_sid(IP_ROOT_PATH . 'calendar_scheduler.' . PHP_EXT . '?d=' . $offset_date . '&amp;fid=' . $fid),
  869. )
  870. );
  871. // blank cells
  872. if ((intval(gmdate('Ymd', $offset_date)) >= intval(gmdate('Ymd', $start_date))) && (intval(gmdate('Ymd', $offset_date)) < intval(gmdate('Ymd', $end_date))))
  873. {
  874. $template->assign_block_vars('_calendar_box._row._cell.switch_filled', array(
  875. 'EVENT_DATE' => $offset_date,
  876. 'TOGGLE_STATUS' => 'none',
  877. 'TOGGLE_ICON' => $images['arrow_down'],
  878. )
  879. );
  880. // send events
  881. $more = false;
  882. $over = (sizeof($map[$offset_date]) > $nb_row_per_cell);
  883. for ($k = 0; $k < sizeof($map[$offset_date]); $k++)
  884. {
  885. // we are just over the limit
  886. if ($over && ($k == $nb_row_per_cell))
  887. {
  888. $more = true;
  889. $template->assign_block_vars('_calendar_box._row._cell.switch_filled._event._more_header', array());
  890. }
  891. $ind = $map[$offset_date][$k];
  892. $template->assign_block_vars('_calendar_box._row._cell.switch_filled._event', array(
  893. 'U_EVENT' => $events[$ind]['event_link'],
  894. 'EVENT_TYPE' => $events[$ind]['event_type_icon'],
  895. 'EVENT_TITLE' => $events[$ind]['event_short_title'],
  896. 'EVENT_CLASS' => $events[$ind]['event_txt_class'],
  897. //'EVENT_MESSAGE' => str_replace(array('"', '\'', '\\'), array('&quot;', '\\\'', '%5C'), $events[$ind]['event_message']),
  898. 'EVENT_MESSAGE' => str_replace(array('"'), array('&quot;'), addslashes($events[$ind]['event_message'])),
  899. )
  900. );
  901. $flag = ($over && ($k == $nb_row_per_cell-1));
  902. if ($ind > -1)
  903. {
  904. $template->assign_block_vars('_calendar_box._row._cell.switch_filled._event.switch_event', array());
  905. if ($flag)
  906. {
  907. $template->assign_block_vars('_calendar_box._row._cell.switch_filled._event.switch_event._more', array());
  908. }
  909. else
  910. {
  911. $template->assign_block_vars('_calendar_box._row._cell.switch_filled._event.switch_event._more_no', array());
  912. }
  913. }
  914. else
  915. {
  916. $template->assign_block_vars('_calendar_box._row._cell.switch_filled._event.switch_event_no', array());
  917. if ($flag)
  918. {
  919. $template->assign_block_vars('_calendar_box._row._cell.switch_filled._event.switch_event_no._more', array());
  920. }
  921. else
  922. {
  923. $template->assign_block_vars('_calendar_box._row._cell.switch_filled._event.switch_event_no._more_no', array());
  924. }
  925. }
  926. if (($k == sizeof($map[$offset_date])-1) && $more)
  927. {
  928. $template->assign_block_vars('_calendar_box._row._cell.switch_filled._event._more_footer', array());
  929. }
  930. }
  931. }
  932. else
  933. {
  934. $template->assign_block_vars('_calendar_box._row._cell.switch_filled_no', array());
  935. }
  936. $offset_date = gmmktime(0, 0, 0, gmdate('m', $offset_date), gmdate('d', $offset_date) + 1, gmdate('Y', $offset_date));
  937. }
  938. }
  939. // fill the main template
  940. $template->assign_var_from_handle($main_template, '_calendar_body' . $handler);
  941. }
  942. ?>