PageRenderTime 56ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/WebCalendar-1.2.5/includes/classes/WebCalendar.class

#
Unknown | 991 lines | 881 code | 110 blank | 0 comment | 0 complexity | 040b000f76eae44068364748d44bb507 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /* Declares the WebCalendar class.
  3. *
  4. * @author Adam Roben <adam.roben@gmail.com>
  5. * @copyright Craig Knudsen, <cknudsen@cknudsen.com>, http://www.k5n.us/cknudsen
  6. * @license http://www.gnu.org/licenses/gpl.html GNU GPL
  7. * @version $Id: WebCalendar.class,v 1.108.2.15 2011/07/12 19:25:12 rjones6061 Exp $
  8. * @package WebCalendar
  9. */
  10. /* The WebCalendar.
  11. *
  12. * Right now this class's functionality is limited to initialization routines.
  13. *
  14. * @todo Get rid of all the global variables.
  15. * @todo Organize initialization steps more logically.
  16. */
  17. class WebCalendar {
  18. /* Filename of the page the user is viewing.
  19. *
  20. * @var string
  21. *
  22. * @access private
  23. */
  24. var $_filename;
  25. /* WebCalendar install directory.
  26. *
  27. * @var string
  28. *
  29. * @access private
  30. */
  31. var $_directory;
  32. /* A map from filenames to initialization phases.
  33. *
  34. * This array holds the initialization steps for each page. Steps are
  35. * separated into phases, and listed in the order they should be executed,
  36. * and are the names of the WebCalendar::methods that should be called,
  37. * without the `_Init' prefix.
  38. *
  39. * @var array
  40. *
  41. * @access private
  42. *
  43. * @todo Make it possible to distinguish between files in different directories
  44. * (e.g. login.php and ws/login.php).
  45. */
  46. var $_filePhaseMap =
  47. array ( '/^(about|nulogin|login|login-app|register|controlpanel|upcoming)\.php$/' =>
  48. array (
  49. array ( 'Config', 'PHPDBI', 'Functions' ),
  50. array ( 'User', 'Connect' ) ),
  51. '/^(ajax|css_cacher|js_cacher|icalclient|freebusy|publish|rss|rss_unapproved|rss_activity_log|get_reminders|get_events|ws)\.php$/' =>
  52. array (
  53. array ( 'Config', 'PHPDBI', 'Functions' ),
  54. array ( 'User', 'Validate', 'Connect', 'SiteExtras', 'Access' ) ),
  55. '/^convert_passwords\.php$/' =>
  56. array (
  57. array ( 'Config', 'PHPDBI' ),
  58. array () ),
  59. '/^send_reminders|reload_remotes\.php$/' =>
  60. array (
  61. array ( 'Config', 'PHPDBI', 'Functions' ),
  62. array ( 'User', 'SiteExtras' ) ),
  63. /* This is for files which have called include('includes/init.php'). */
  64. '/^init\.php$/' =>
  65. array (
  66. array ( 'InitFirstPhase', 'Config', 'PHPDBI', 'Functions' ),
  67. array ( 'User', 'Validate', 'Connect', 'SiteExtras', 'Access', 'InitSecondPhase' ) )
  68. );
  69. /* WebCalendar constructor.
  70. *
  71. * @param string $path Full path of file being viewed.
  72. *
  73. * @return WebCalendar New WebCalendar object.
  74. *
  75. * @access public
  76. */
  77. function WebCalendar ( $path ) {
  78. $this->_filename = basename ( $path );
  79. $this->_directory = dirname ( __FILE__ ) . '/../../';
  80. // Define a value to prevent direct access to files.
  81. define ( '_ISVALID', 1 );
  82. }
  83. // cek: This function is used by some other apps that I have developed
  84. // but have not released.
  85. function addExternalPage ( $pattern, $initArray ) {
  86. $this->_filePhaseMap[$pattern] = $initArray;
  87. }
  88. /* First part of initializations from includes/init.php.
  89. *
  90. * @access private
  91. */
  92. function _initInitFirstPhase () {
  93. global $DMW, $HTTP_GET_VARS, $HTTP_POST_VARS, $PHP_SELF, $SCRIPT, $self,
  94. $special, $user_inc;
  95. // Make sure another app in the same domain doesn't have a 'user' cookie.
  96. if ( empty ( $HTTP_GET_VARS ) )
  97. $HTTP_GET_VARS = $_GET;
  98. if ( empty ( $HTTP_POST_VARS ) )
  99. $HTTP_POST_VARS = $_POST;
  100. if ( ! empty ( $HTTP_GET_VARS ) && empty ( $HTTP_GET_VARS['user'] ) && !
  101. empty ( $HTTP_POST_VARS ) && empty ( $HTTP_POST_VARS['user'] ) &&
  102. isset ( $GLOBALS['user'] ) )
  103. unset ( $GLOBALS['user'] );
  104. // Get script name.
  105. $self = $_SERVER['PHP_SELF'];
  106. if ( empty ( $self ) )
  107. $self = $PHP_SELF;
  108. preg_match ( '/\/(\w+\.php)/', $self, $match );
  109. $SCRIPT = $match[1];
  110. // Several files need a no-cache header and some of the same code.
  111. $special = array ( 'month.php', 'day.php', 'week.php',
  112. 'week_details.php', 'year.php', 'minical.php' );
  113. $DMW = in_array ( $SCRIPT, $special );
  114. // Security precaution. Don't allow <script> to be included in
  115. // a URL in any way.
  116. if ( preg_match ( '/\s*script/i', $_SERVER['QUERY_STRING'] ) ) {
  117. // No need to have a graceful exit for this since it should only
  118. // happen to malicioius crapweasels.
  119. echo "<html><body><h2>User Error</h2><p>Bite me.</p></html>\n";
  120. exit;
  121. }
  122. // Unset some variables that shouldn't be set.
  123. unset ( $user_inc );
  124. }
  125. /* Second part of initializations from includes/init.php.
  126. *
  127. * @access private
  128. */
  129. function _initInitSecondPhase () {
  130. global $ALLOW_VIEW_OTHER, $can_add, $cat_id, $CATEGORIES_ENABLED,
  131. $CATEGORY_VIEW, $caturl, $date, $DMW, $friendly, $override, $fullname, $GROUPS_ENABLED,
  132. $hour, $id, $is_admin, $is_assistant, $is_nonuser, $is_nonuser_admin, $login, $minute, $month,
  133. $NONUSER_ENABLED, $nonusers, $ovrd, $PUBLIC_ACCESS, $PUBLIC_ACCESS_CAN_ADD,
  134. $PUBLIC_ACCESS_FULLNAME, $PUBLIC_ACCESS_OTHERS, $readonly, $u_url, $user,
  135. $user_fullname, $USER_SEES_ONLY_HIS_GROUPS, $userlist, $valid_user, $year;
  136. load_global_settings ();
  137. $this->setLanguage ();
  138. $user = user_case(getValue ( 'user', '[A-Za-z0-9_\.=@,\-]*', true ));
  139. if ( empty ( $ovrd ) )
  140. load_user_preferences ();
  141. // Error-check some commonly used form variable names.
  142. $cat_id = getValue ( 'cat_id', '[\-0-9,]+' );
  143. $date = getValue ( 'date', '[0-9]+' );
  144. $friendly = getValue ( 'friendly', '[01]' );
  145. $override = getValue ( 'override', '[01]' );
  146. $hour = getValue ( 'hour', '[0-9]+' );
  147. $id = getValue ( 'id', '[0-9]+', true );
  148. $minute = getValue ( 'minute', '[0-9]+' );
  149. $month = getValue ( 'month', '[0-9]+' );
  150. $year = getValue ( 'year', '[0-9]+' );
  151. if ( empty ( $PUBLIC_ACCESS ) )
  152. $PUBLIC_ACCESS = 'N';
  153. // Initialize access settings ($user_access string)
  154. // and make sure user is allowed to view the current page.
  155. access_init ();
  156. if ( ! access_can_view_page () ) {
  157. $user_BGCOLOR = get_pref_setting ( $login, 'BGCOLOR' );
  158. echo '<html>
  159. <head>
  160. <title>' . generate_application_name () . ' ' . translate ( 'Error' ) . '</title>
  161. </head>
  162. <body bgcolor="' . $user_BGCOLOR . '">
  163. ' . print_not_auth ( true ) . '
  164. </body>
  165. </html>';
  166. exit;
  167. }
  168. $can_add = false;
  169. // Load if $SCRIPT is in $special array:
  170. if ( $DMW ) {
  171. // Tell the browser not to cache.
  172. // send_no_cache_header ();
  173. if ( $ALLOW_VIEW_OTHER != 'Y' && ! $is_admin && ! $is_assistant )
  174. $user = '';
  175. $can_add = ( $readonly == 'N' || $is_admin == 'Y' );
  176. if ( $PUBLIC_ACCESS == 'Y' && $login == '__public__' ) {
  177. if ( $PUBLIC_ACCESS_CAN_ADD != 'Y' )
  178. $can_add = false;
  179. if ( $PUBLIC_ACCESS_OTHERS != 'Y' )
  180. $user = ''; // Security precaution.
  181. }
  182. if (!$is_admin && !$is_assistant && !$is_nonuser_admin) {
  183. if ($is_nonuser)
  184. $can_add = false;
  185. else if (!empty($user) && $user != $login && $user != '__public__')
  186. $can_add = false;
  187. }
  188. if ( $GROUPS_ENABLED == 'Y' && $USER_SEES_ONLY_HIS_GROUPS == 'Y' && ! $is_admin ) {
  189. $userlist = get_my_users ();
  190. $valid_user = false;
  191. if ( ! empty ( $NONUSER_ENABLED ) && $NONUSER_ENABLED == 'Y' ) {
  192. $nonusers = get_my_nonusers ( $login, true );
  193. $userlist = array_merge ( $nonusers, $userlist );
  194. }
  195. for ( $i = 0; $i < count ( $userlist ); $i++ ) {
  196. if ( $user == $userlist[$i]['cal_login'] )
  197. $valid_user = true;
  198. }
  199. if ( ! $valid_user )
  200. $user = ''; // Security precaution.
  201. }
  202. if ( ! empty ( $user ) ) {
  203. $u_url = 'user=' . $user . '&amp;';
  204. if (!user_load_variables ( $user, 'user_' ))
  205. nonuser_load_variables($user, 'user_');
  206. if ( $user == '__public__' )
  207. $user_fullname = translate ( $PUBLIC_ACCESS_FULLNAME );
  208. } else {
  209. $u_url = '';
  210. $user_fullname = $fullname;
  211. if ( $login == '__public__' )
  212. $user_fullname = translate ( $PUBLIC_ACCESS_FULLNAME );
  213. }
  214. set_today ( $date );
  215. remember_this_view ();
  216. if ( $CATEGORIES_ENABLED == 'Y' ) {
  217. if ( ! empty ( $cat_id ) ) {
  218. } elseif ( ! empty ( $CATEGORY_VIEW ) && ! isset ( $_GET['cat_id'] ) )
  219. $cat_id = $CATEGORY_VIEW;
  220. else
  221. $cat_id = '';
  222. } else
  223. $cat_id = '';
  224. $caturl = ( empty ( $cat_id ) ? '' : '&amp;cat_id=' . $cat_id );
  225. }
  226. }
  227. /* Initializations from includes/assert.php.
  228. *
  229. * @access private
  230. */
  231. function _initAssert () {
  232. // Initialize assert options.
  233. assert_options ( ASSERT_CALLBACK, 'assert_handler' );
  234. assert_options ( ASSERT_ACTIVE, 1 );
  235. }
  236. /* Initializations from includes/config.php.
  237. *
  238. * @access private
  239. */
  240. function _initConfig () {
  241. do_config ( $this->absolutePath ( 'includes/settings.php' ) );
  242. }
  243. /* Initializations from includes/dbi4php.php.
  244. *
  245. * @access private
  246. */
  247. function _initPHPDBI () {
  248. global $phpdbiVerbose;
  249. // Enable the following to show the actual database error in the browser.
  250. // It is more secure to not show this info, so this should only be turned
  251. // on for debugging purposes.
  252. if ( ! isset ( $phpdbiVerbose ) )
  253. $phpdbiVerbose = false;
  254. }
  255. /* Initializations from includes/functions.php.
  256. *
  257. * @access private
  258. */
  259. function _initFunctions () {
  260. global $byday_names, $byday_values, $days_per_month, $db_login, $db_password,
  261. $ldays_per_month,
  262. $offsets, $PHP_SELF, $settings, $weekday_names;
  263. /**#@+
  264. * Used for activity log.
  265. */
  266. define ( 'LOG_APPROVE', 'A' );
  267. define ( 'LOG_APPROVE_J', 'P' );
  268. define ( 'LOG_APPROVE_T', 'H' );
  269. define ( 'LOG_ATTACHMENT', 'T' );
  270. define ( 'LOG_COMMENT', 'M' );
  271. define ( 'LOG_CREATE', 'C' );
  272. define ( 'LOG_CREATE_J', 'I' );
  273. define ( 'LOG_CREATE_T', 'G' );
  274. define ( 'LOG_DELETE', 'D' );
  275. define ( 'LOG_DELETE_J', 'V' );
  276. define ( 'LOG_DELETE_T', 'L' );
  277. define ( 'LOG_LOGIN_FAILURE', 'x' );
  278. define ( 'LOG_NEWUSER_EMAIL', 'E' );
  279. define ( 'LOG_NEWUSER_FULL', 'F' );
  280. define ( 'LOG_NOTIFICATION', 'N' );
  281. define ( 'LOG_REJECT', 'X' );
  282. define ( 'LOG_REJECT_J', 'Q' );
  283. define ( 'LOG_REJECT_T', 'J' );
  284. define ( 'LOG_REMINDER', 'R' );
  285. define ( 'LOG_UPDATE', 'U' );
  286. define ( 'LOG_UPDATE_J', 'S' );
  287. define ( 'LOG_UPDATE_T', 'K' );
  288. define ( 'LOG_USER_ADD', 'a' );
  289. define ( 'LOG_USER_DELETE', 'd' );
  290. define ( 'LOG_USER_UPDATE', 'u' );
  291. /**#@-*/
  292. /* Number of seconds in:
  293. */
  294. define ( 'ONE_HOUR', 3600 );
  295. define ( 'ONE_DAY', 86400 );
  296. define ( 'ONE_WEEK', 604800 );
  297. /* Arrays containing the number of days in each month
  298. * in a leap year and a non-leap year.
  299. *
  300. * @global array $ldays_per_month
  301. * @global array $days_per_month
  302. */
  303. $ldays_per_month =
  304. $days_per_month = array ( 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
  305. $ldays_per_month[2] = 29;
  306. /* Array containing the short names for the days of the week.
  307. *
  308. * @global array $weekday_names
  309. */
  310. $weekday_names = array ( 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' );
  311. /* Array containing the BYDAY names for the days of the week.
  312. *
  313. * @global array $byday_name
  314. */
  315. $byday_names = array ( 'SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA' );
  316. /* Array containing the number value of the days of the week.
  317. *
  318. * @global array $days_per_week
  319. */
  320. $days_of_week = array_flip ( $weekday_names );
  321. /* Array containing the number value of the ical ByDay abbreviations.
  322. *
  323. * @global array $byday_values
  324. */
  325. $byday_values = array_flip ( $byday_names );
  326. /* Pull out cookies and place them in global variables */
  327. if ( ! empty ( $_COOKIE['webcalendar_session'] ) )
  328. $GLOBALS['webcalendar_session'] = $_COOKIE['webcalendar_session'];
  329. if ( ! empty ( $_COOKIE['webcalendar_login'] ) )
  330. $GLOBALS['webcalendar_login'] = $_COOKIE['webcalendar_login'];
  331. if ( ! empty ( $_COOKIE['webcalendar_last_view'] ) )
  332. $GLOBALS['webcalendar_last_view'] = $_COOKIE['webcalendar_last_view'];
  333. if ( ! empty ( $_COOKIE['webcalendar_csscache'] ) )
  334. $GLOBALS['webcalendar_csscache'] = $_COOKIE['webcalendar_csscache'];
  335. // Don't allow a user to put "login=XXX" in the URL
  336. // if they are not coming from the login.php page.
  337. if ( empty ( $PHP_SELF ) && ! empty ( $_SERVER['PHP_SELF'] ) )
  338. $PHP_SELF = $_SERVER['PHP_SELF']; // Backward compatibility.
  339. if ( empty ( $PHP_SELF ) )
  340. $PHP_SELF = ''; // This happens when running send_reminders.php from CL.
  341. if ( ! strstr ( $PHP_SELF, 'login.php' ) && ! empty ( $GLOBALS['login'] ) )
  342. $GLOBALS['login'] = '';
  343. // Define an array to use to jumble up the key: $offsets
  344. // We define a unique key to scramble the cookie we generate.
  345. // We use the admin install password that the user set to make
  346. // the salt unique for each WebCalendar install.
  347. $salt = ( ! empty ( $settings ) && ! empty ( $settings['install_password'] )
  348. ? $settings['install_password'] : md5 ( $db_login ) );
  349. $salt_len = strlen ( $salt );
  350. $salt2 = md5 ( empty ( $db_password ) ? 'oogabooga' : $db_password );
  351. $salt2_len = strlen ( $salt2 );
  352. $offsets = array ();
  353. for ( $i = 0; $i < $salt_len || $i < $salt2_len; $i++ ) {
  354. $offsets[$i] = 0;
  355. if ( $i < $salt_len )
  356. $offsets[$i] += ord ( substr ( $salt, $i, 1 ) );
  357. if ( $i < $salt2_len )
  358. $offsets[$i] += ord ( substr ( $salt2, $i, 1 ) );
  359. $offsets[$i] %= 128;
  360. }
  361. }
  362. /* Initializations from includes/user*.php.
  363. *
  364. * This is a placeholder for now. We are letting includes/user*.php handle
  365. * its own initialization.
  366. *
  367. * @access private
  368. *
  369. * @todo Make an Authentication interface class and create a subclass for
  370. * each user*.php page.
  371. */
  372. function _initUser () {
  373. }
  374. /* Initializations from includes/validate.php.
  375. *
  376. * @access private
  377. */
  378. function _initValidate () {
  379. global $c, $cryptpw, $db_database, $db_host, $db_login, $db_password,
  380. $encoded_login, $HTTP_ENV_VARS, $HTTP_SERVER_VARS, $is_nonuser, $login,
  381. $login_return_path, $PHP_AUTH_USER, $REMOTE_USER, $SCRIPT,
  382. $session_not_found, $settings, $single_user, $single_user_login,
  383. $use_http_auth, $user_inc, $validate_redirect, $webcalendar_session;
  384. /* If WebCalendar is configured to use http authentication, then we can
  385. * use _initValidate (). If we are not using http auth, icalclient.php will
  386. * create its own http auth since an iCal client cannot login via a
  387. * web-based login. Publish.php does need to validate if not http_auth.
  388. */
  389. if ( ! $use_http_auth &&
  390. ( $this->_filename == 'css_cacher.php' ||
  391. $this->_filename == 'icalclient.php' ||
  392. $this->_filename == 'rss_unapproved.php' ||
  393. $this->_filename == 'rss_activity_log.php' ||
  394. $this->_filename == 'js_cacher.php' ||
  395. $this->_filename == 'publish.php' ) ) {
  396. return;
  397. }
  398. $is_nonuser = $session_not_found = $validate_redirect = false;
  399. // Catch-all for getting the username when using HTTP-authentication.
  400. if ( $use_http_auth ) {
  401. if ( empty ( $PHP_AUTH_USER ) ) {
  402. if ( ! empty ( $_SERVER ) && isset ( $_SERVER['PHP_AUTH_USER'] ) )
  403. $PHP_AUTH_USER = $_SERVER['PHP_AUTH_USER'];
  404. else
  405. if ( ! empty ( $HTTP_SERVER_VARS ) &&
  406. isset ( $HTTP_SERVER_VARS['PHP_AUTH_USER'] ) )
  407. $PHP_AUTH_USER = $HTTP_SERVER_VARS['PHP_AUTH_USER'];
  408. else
  409. if ( isset ( $REMOTE_USER ) )
  410. $PHP_AUTH_USER = $REMOTE_USER;
  411. else
  412. if ( ! empty ( $_ENV ) && isset ( $_ENV['REMOTE_USER'] ) )
  413. $PHP_AUTH_USER = $_ENV['REMOTE_USER'];
  414. else
  415. if ( ! empty ( $HTTP_ENV_VARS ) && isset ( $HTTP_ENV_VARS['REMOTE_USER'] ) )
  416. $PHP_AUTH_USER = $HTTP_ENV_VARS['REMOTE_USER'];
  417. else
  418. if ( @getenv ( 'REMOTE_USER' ) )
  419. $PHP_AUTH_USER = getenv ( 'REMOTE_USER' );
  420. else
  421. if ( isset ( $AUTH_USER ) )
  422. $PHP_AUTH_USER = $AUTH_USER;
  423. else
  424. if ( ! empty ( $_ENV ) && isset ( $_ENV['AUTH_USER'] ) )
  425. $PHP_AUTH_USER = $_ENV['AUTH_USER'];
  426. else
  427. if ( ! empty ( $HTTP_ENV_VARS ) && isset ( $HTTP_ENV_VARS['AUTH_USER'] ) )
  428. $PHP_AUTH_USER = $HTTP_ENV_VARS['AUTH_USER'];
  429. else
  430. if ( @getenv ( 'AUTH_USER' ) )
  431. $PHP_AUTH_USER = getenv ( 'AUTH_USER' );
  432. }
  433. }
  434. if ( $single_user == 'Y' )
  435. $login = $single_user_login;
  436. else {
  437. if ( $use_http_auth ) {
  438. // HTTP server did validation for us....
  439. if ( empty ( $PHP_AUTH_USER ) )
  440. $session_not_found = true;
  441. else
  442. $login = $PHP_AUTH_USER;
  443. } else
  444. if ( substr ( $user_inc, 0, 9 ) == 'user-app-' ) {
  445. // Make sure we are connected to the database for session check.
  446. $c = @dbi_connect ( $db_host, $db_login, $db_password, $db_database );
  447. if ( ! $c )
  448. die_miserable_death ( 'Error connecting to database:<blockquote>'
  449. . dbi_error () . '</blockquote>' );
  450. // Use another application's authentication.
  451. if ( $login != user_logged_in () )
  452. $session_not_found = true;
  453. } else {
  454. @session_start ();
  455. if ( ! empty ( $_SESSION['webcal_login'] ) )
  456. $login = $_SESSION['webcal_login'];
  457. if ( ! empty ( $_SESSION['webcalendar_session'] ) )
  458. $webcalendar_session = $_SESSION['webcalendar_session'];
  459. if ( empty ( $login ) && empty ( $webcalendar_session ) )
  460. $session_not_found = true;
  461. else
  462. if ( empty ( $_SESSION['webcal_login'] ) &&
  463. // Check for cookie...
  464. ! empty ( $webcalendar_session ) ) {
  465. $encoded_login = $webcalendar_session;
  466. if ( empty ( $encoded_login ) )
  467. // Invalid session cookie.
  468. $session_not_found = true;
  469. else {
  470. $login_pw = explode( '|', decode_string ( $encoded_login ) );
  471. $login = $login_pw[0];
  472. $cryptpw = $login_pw[1];
  473. // Security fix. Don't allow certain types of characters in
  474. // the login. WebCalendar does not escape the login name in
  475. // SQL requests. So, if the user were able to set the login
  476. // name to be "x';drop table u;",
  477. // they may be able to affect the database.
  478. // NOTE: we also changed the cookie encoding from WebCalendar 1.0.X
  479. // to WebCalendar 1.1.X+, so this causes a bad cookie error.
  480. if ( ! empty ( $login ) && $login != addslashes ( $login ) ) {
  481. // The following deletes the bad cookie. So, the user just needs
  482. // to reload.
  483. SetCookie ( 'webcalendar_session', '', 0 );
  484. die_miserable_death ( 'Illegal characters in login <tt>'
  485. . htmlentities ( $login ) . '</tt>' .
  486. "Press browser reload to clear bad cookie." );
  487. }
  488. // Make sure we are connected to the database for password check.
  489. $c = @dbi_connect ( $db_host, $db_login, $db_password, $db_database );
  490. if ( ! $c )
  491. die_miserable_death ( 'Error connecting to database:<blockquote>'
  492. . dbi_error () . '</blockquote>' );
  493. doDbSanityCheck ();
  494. if ( $cryptpw == 'nonuser' ) {
  495. if ( ! nonuser_load_variables ( $login, 'nutemp_' ) )
  496. // No such nonuser cal.
  497. die_miserable_death ( 'Invalid nonuser calendar.' );
  498. if ( empty ( $GLOBALS['nutemp_is_public'] ) ||
  499. $GLOBALS['nutemp_is_public'] != 'Y' )
  500. die_miserable_death ( 'Nonuser calendar is not public.' );
  501. $is_nonuser = true;
  502. } else
  503. if ( ! user_valid_crypt ( $login, $cryptpw ) )
  504. do_redirect ( 'login.php' . ( empty ( $login_return_path )
  505. ? '' : '?return_path=' . $login_return_path ) );
  506. @session_start ();
  507. $_SESSION['webcal_login'] = $login;
  508. $_SESSION['webcalendar_session'] = $webcalendar_session;
  509. }
  510. }
  511. }
  512. }
  513. // If login is not case-sensitive, map it to lowercase now.
  514. $login = user_case($login);
  515. }
  516. /* Initializations from includes/connect.php.
  517. *
  518. * @access private
  519. */
  520. function _initConnect () {
  521. global $c, $db_database, $db_host, $db_login, $db_password, $firstname,
  522. $fullname, $is_admin, $is_nonuser, $LANGUAGE, $lastname, $login,
  523. $login_email, $login_firstname, $login_fullname, $login_is_admin,
  524. $login_lastname, $login_login, $login_url, $not_auth, $PHP_AUTH_USER,
  525. $PHP_SELF, $PROGRAM_VERSION, $pub_acc_enabled, $PUBLIC_ACCESS_CAN_ADD,
  526. $readonly, $SCRIPT, $session_not_found, $single_user, $single_user_login,
  527. $use_http_auth, $user_email, $user_inc;
  528. // db settings are in config.php.
  529. // Establish a database connection.
  530. // This may have happened in validate.php, depending on settings.
  531. // If not, do it now.
  532. if ( empty ( $c ) ) {
  533. $c = dbi_connect ( $db_host, $db_login, $db_password, $db_database );
  534. if ( ! $c )
  535. die_miserable_death ( 'Error connecting to database:<blockquote>'
  536. . dbi_error () . '</blockquote>' );
  537. // Do a sanity check on the database,
  538. // making sure we can at least access the webcal_config table.
  539. if ( function_exists ( 'doDbSanityCheck' ) )
  540. doDbSanityCheck ();
  541. // Check the current installation version.
  542. // Redirect user to install page if it is different from stored value.
  543. // This will prevent running WebCalendar until UPGRADING.html has been
  544. // read and required upgrade actions completed.
  545. $rows = dbi_get_cached_rows ( 'SELECT cal_value FROM webcal_config
  546. WHERE cal_setting = \'WEBCAL_PROGRAM_VERSION\'' );
  547. if ( $rows ) {
  548. $row = $rows[0];
  549. if ( $row[0] != $PROGRAM_VERSION ) {
  550. // &amp; does not work here...leave it as &
  551. header ( 'Location: install/index.php?action=mismatch&version='
  552. . $row[0] );
  553. exit;}
  554. }
  555. }
  556. // If we are in single user mode,
  557. // make sure that the login selected is a valid login.
  558. if ( $single_user == 'Y' ) {
  559. if ( empty ( $single_user_login ) )
  560. die_miserable_death ( 'You have not defined <tt>single_user_login</tt> '
  561. . 'in <tt>includes/settings.php</tt>' );
  562. $res = dbi_execute ( 'SELECT COUNT( * ) FROM webcal_user
  563. WHERE cal_login = ?', array ( $single_user_login ) );
  564. if ( ! $res ) {
  565. echo 'Database error: ' . dbi_error ();
  566. exit;
  567. }
  568. $row = dbi_fetch_row ( $res );
  569. if ( $row[0] == 0 ) {
  570. // User specified as single_user_login does not exist.
  571. if ( ! dbi_execute ( 'INSERT INTO webcal_user ( cal_login, cal_passwd,
  572. cal_is_admin ) VALUES ( ?, ?, ? )',
  573. array ( $single_user_login, md5 ( $single_user_login ), 'Y' ) ) )
  574. die_miserable_death ( 'User <tt>' . $single_user_login
  575. . '</tt> does not exist in <tt>webcal_user</tt> table and we were '
  576. . 'not able to add it for you:<br /><blockquote>' . dbi_error ()
  577. . '</blockquote>' );
  578. // User was added... should we tell them?
  579. }
  580. dbi_free_result ( $res );
  581. }
  582. // Global settings have not been loaded yet, so check for public_access now.
  583. $rows = dbi_get_cached_rows ( 'SELECT cal_value FROM webcal_config
  584. WHERE cal_setting = \'PUBLIC_ACCESS\'' );
  585. if ( $rows ) {
  586. $row = $rows[0];
  587. }
  588. $pub_acc_enabled = ( ! empty ( $row ) && $row[0] == 'Y' );
  589. if ( $pub_acc_enabled ) {
  590. $rows = dbi_get_cached_rows ( 'SELECT cal_value FROM webcal_config
  591. WHERE cal_setting = \'PUBLIC_ACCESS_CAN_ADD\'' );
  592. if ( $rows && $row == $rows[0] )
  593. $PUBLIC_ACCESS_CAN_ADD = $row[0];
  594. }
  595. if ( empty ( $PHP_SELF ) )
  596. $PHP_SELF = $_SERVER['PHP_SELF'];
  597. if ( empty ( $login_url ) )
  598. $login_url = 'login.php';
  599. $login_url .= ( strstr ( $login_url, '?' ) ? '&amp;' : '?' )
  600. . ( empty ( $login_return_path ) ? '' : 'return_path='
  601. . $login_return_path );
  602. // If sent here from an email and not logged in,
  603. //save URI and redirect to login.
  604. $em = getGetValue ( 'em' );
  605. $view_via_email = false;
  606. if ( ! empty ( $em ) && empty ( $login ) ) {
  607. remember_this_view ();
  608. $view_via_email = true;
  609. }
  610. if ( empty ( $session_not_found ) )
  611. $session_not_found = false;
  612. if ( ! $view_via_email && $pub_acc_enabled && ! empty ( $session_not_found ) ) {
  613. $firstname = $lastname = $user_email = '';
  614. $fullname = 'Public Access'; // Will be translated after translation is loaded.
  615. $is_admin = false;
  616. $login = '__public__';
  617. } else
  618. if ( $view_via_email || ( ! $pub_acc_enabled && $session_not_found
  619. && ! $use_http_auth ) ) {
  620. if ( substr ( $user_inc, 0, 9 ) == 'user-app-' )
  621. app_login_screen ( clean_whitespace ( $SCRIPT ) );
  622. else {
  623. do_redirect ( $login_url );
  624. exit;
  625. }
  626. }
  627. $is_nonuser = false;
  628. if ( empty ( $login ) && $use_http_auth ) {
  629. if ( strstr ( $PHP_SELF, "login.php" ) ) {
  630. // Ignore since login.php will redirect to index.php.
  631. } else
  632. send_http_login ();
  633. } else
  634. if ( ! empty ( $login ) ) {
  635. // They are already logged in ($login is set in validate.php).
  636. if ( strstr ( $PHP_SELF, 'login.php' ) ) {
  637. // Ignore since login.php will redirect to index.php.
  638. } else
  639. if ( $login == '__public__' ) {
  640. $firstname = $lastname = $user_email = '';
  641. $fullname = 'Public Access';
  642. $is_admin = false;
  643. } else {
  644. user_load_variables ( $login, 'login_' );
  645. if ( ! empty ( $login_login ) ) {
  646. $firstname = $login_firstname;
  647. $lastname = $login_lastname;
  648. $fullname = $login_fullname;
  649. $is_admin = ( $login_is_admin == 'Y' );
  650. $is_nonuser = ( ! empty ( $GLOBALS['login_is_nonuser'] ) &&
  651. $GLOBALS['login_is_nonuser'] );
  652. $user_email = $login_email;
  653. } else {
  654. // Invalid login.
  655. if ( $use_http_auth ) {
  656. if ($pub_acc_enabled) {
  657. $login = '__public__';
  658. $firstname = $lastname = $user_email = '';
  659. $fullname = 'Public Access';
  660. $is_admin = false;
  661. } else
  662. send_http_login ();
  663. } else
  664. // This shouldn't happen since login should be validated in validate.php.
  665. // If it does happen, it means we received an invalid login cookie.
  666. do_redirect ( $login_url . '&amp;error=Invalid+session+found.' );
  667. }
  668. }
  669. }
  670. // If they are accessing using the public login, restrict them from using
  671. // certain pages.
  672. $not_auth = false;
  673. if ( ! empty ( $login ) && $login == '__public__' || $is_nonuser ) {
  674. if ( strstr ( $PHP_SELF, 'views.php' ) ||
  675. strstr ( $PHP_SELF, 'views_edit_handler.php' ) ||
  676. strstr ( $PHP_SELF, 'category.php' ) ||
  677. strstr ( $PHP_SELF, 'category_handler.php' ) ||
  678. strstr ( $PHP_SELF, 'activity_log.php' ) ||
  679. strstr ( $PHP_SELF, 'admin.php' ) ||
  680. strstr ( $PHP_SELF, 'adminhome.php' ) ||
  681. strstr ( $PHP_SELF, 'admin_handler.php' ) ||
  682. strstr ( $PHP_SELF, 'groups.php' ) ||
  683. strstr ( $PHP_SELF, 'group_edit_handler.php' ) ||
  684. strstr ( $PHP_SELF, 'pref.php' ) ||
  685. strstr ( $PHP_SELF, 'pref_handler.php' ) ||
  686. strstr ( $PHP_SELF, 'edit_remotes.php' ) ||
  687. strstr ( $PHP_SELF, 'edit_remotes_handler.php' ) ||
  688. strstr ( $PHP_SELF, 'edit_user.php' ) ||
  689. strstr ( $PHP_SELF, 'edit_user_handler.php' ) ||
  690. strstr ( $PHP_SELF, 'approve_entry.php' ) ||
  691. strstr ( $PHP_SELF, 'reject_entry.php' ) ||
  692. strstr ( $PHP_SELF, 'del_entry.php' ) ||
  693. strstr ( $PHP_SELF, 'set_entry_cat.php' ) ||
  694. strstr ( $PHP_SELF, 'list_unapproved.php' ) ||
  695. strstr ( $PHP_SELF, 'layers.php' ) ||
  696. strstr ( $PHP_SELF, 'layer_toggle.php' ) ||
  697. strstr ( $PHP_SELF, 'import.php' ) ||
  698. strstr ( $PHP_SELF, 'import_handler.php' ) ||
  699. strstr ( $PHP_SELF, 'edit_template.php' ) ) {
  700. $not_auth = true;
  701. }
  702. }
  703. if ( ! empty ( $login ) && ( empty ( $is_admin ) || ! $is_admin ) ) {
  704. if ( strstr ( $PHP_SELF, 'admin.php' ) ||
  705. strstr ( $PHP_SELF, 'admin_handler.php' ) ||
  706. strstr ( $PHP_SELF, 'groups.php' ) ||
  707. strstr ( $PHP_SELF, 'group_edit.php' ) ||
  708. strstr ( $PHP_SELF, 'group_edit_handler.php' ) ||
  709. strstr ( $PHP_SELF, 'activity_log.php' ) ) {
  710. $not_auth = true;
  711. }
  712. }
  713. // restrict access if calendar is read-only
  714. if ( $readonly == 'Y' ) {
  715. //if ( strstr ( $PHP_SELF, 'activity_log.php' ) ||
  716. if ( strstr ( $PHP_SELF, 'adminhome.php' ) ||
  717. strstr ( $PHP_SELF, 'admin.php' ) ||
  718. strstr ( $PHP_SELF, 'approve_entry.php' ) ||
  719. strstr ( $PHP_SELF, 'category_handler.php' ) ||
  720. strstr ( $PHP_SELF, 'category.php' ) ||
  721. strstr ( $PHP_SELF, 'del_entry.php' ) ||
  722. strstr ( $PHP_SELF, 'edit_report_handler.php' ) ||
  723. strstr ( $PHP_SELF, 'edit_report.php' ) ||
  724. strstr ( $PHP_SELF, 'edit_template.php' ) ||
  725. strstr ( $PHP_SELF, 'edit_user_handler.php' ) ||
  726. strstr ( $PHP_SELF, 'edit_user.php' ) ||
  727. strstr ( $PHP_SELF, 'group_edit_handler.php' ) ||
  728. strstr ( $PHP_SELF, 'groups.php' ) ||
  729. strstr ( $PHP_SELF, 'import_handler.php' ) ||
  730. strstr ( $PHP_SELF, 'import_handler.php' ) ||
  731. strstr ( $PHP_SELF, 'import.php' ) ||
  732. strstr ( $PHP_SELF, 'layers.php' ) ||
  733. strstr ( $PHP_SELF, 'layer_toggle.php' ) ||
  734. strstr ( $PHP_SELF, 'list_unapproved.php' ) ||
  735. strstr ( $PHP_SELF, 'pref_handler.php' ) ||
  736. strstr ( $PHP_SELF, 'pref.php' ) ||
  737. strstr ( $PHP_SELF, 'pref_handler.php' ) ||
  738. strstr ( $PHP_SELF, 'purge.php' ) ||
  739. strstr ( $PHP_SELF, 'register.php' ) ||
  740. strstr ( $PHP_SELF, 'reject_entry.php' ) ||
  741. strstr ( $PHP_SELF, 'set_entry_cat.php' ) ||
  742. strstr ( $PHP_SELF, 'users.php' ) ||
  743. strstr ( $PHP_SELF, 'views_edit_handler.php' ) ||
  744. strstr ( $PHP_SELF, 'views.php' ) ) {
  745. $not_auth = true;
  746. }
  747. }
  748. // An attempt will be made to translate
  749. if ( $not_auth ) {
  750. load_user_preferences ();
  751. $error = ( function_exists ( 'translate' )
  752. ? translate ( 'You are not authorized.' ) : 'You are not authorized.' );
  753. die_miserable_death ( $error );
  754. }
  755. }
  756. /* Initializations from includes/site-extras.php.
  757. *
  758. * This is a placeholder for now.
  759. *
  760. * @access private
  761. *
  762. * @todo Figure out what should go here.
  763. */
  764. function _initSiteExtras () {
  765. }
  766. /* Initializations from includes/access.php.
  767. *
  768. * @access private
  769. */
  770. function _initAccess () {
  771. global $access_other_cals;
  772. // Global variable used to cache permissions
  773. $access_other_cals = array ();
  774. }
  775. /* Initializations from includes/translate.php.
  776. *
  777. * @access private
  778. */
  779. function _initTranslate () {
  780. global $lang, $lang_file, $LANGUAGE, $PUBLIC_ACCESS_FULLNAME,
  781. $translation_loaded, $enable_mbstring;
  782. if ( empty ( $LANGUAGE ) )
  783. $LANGUAGE = 'English-US'; // Default
  784. // If set to use browser settings,
  785. // use the user's language preferences from their browser.
  786. $lang = $LANGUAGE;
  787. if ( $LANGUAGE == 'Browser-defined' || $LANGUAGE == 'none' ) {
  788. $lang = get_browser_language ();
  789. if ( $lang == 'none' )
  790. $lang = '';
  791. }
  792. if ( strlen ( $lang ) == 0 || $lang == 'none' )
  793. $lang = 'English-US'; // Default
  794. $lang_file = 'translations/' . $lang . '.txt';
  795. if (extension_loaded('mbstring')) {
  796. $mb_lang = strtok($lang, '-');
  797. if (@mb_language($mb_lang) && mb_internal_encoding(translate('charset'))) {
  798. $enable_mbstring = true;
  799. } else {
  800. $enable_mbstring = false;
  801. }
  802. }
  803. $translation_loaded = false;
  804. $PUBLIC_ACCESS_FULLNAME = 'Public Access'; // default
  805. }
  806. /* Gets the initialization phases for the page being viewed.
  807. *
  808. * @return array Array of initialization phases.
  809. *
  810. * @access private
  811. */
  812. function _getPhases () {
  813. global $user_inc;
  814. foreach ( $this->_filePhaseMap as $pattern => $phases ) {
  815. if ( preg_match ( $pattern, $this->_filename ) !== 0 )
  816. return $phases;
  817. }
  818. die_miserable_death ( '_getPhases: could not find \'' . $this->_filename
  819. . '\' in _filePhaseMap.' );
  820. }
  821. /* Gets the initialization steps for the current page and phase.
  822. *
  823. * @param int $phase Initialization phase number
  824. *
  825. * @return array Array of initialization steps.
  826. *
  827. * @access private
  828. */
  829. function _getSteps ( $phase ) {
  830. $phases = $this->_getPhases ();
  831. return $phases[$phase - 1];
  832. }
  833. /* Performs initialization steps.
  834. *
  835. * @param int $phase Which step of initialization should we perform?
  836. *
  837. * @access private
  838. */
  839. function _doInit ( $phase ) {
  840. $steps = $this->_getSteps ( $phase );
  841. foreach ( $steps as $step ) {
  842. $function = "_init$step";
  843. $this->$function ();
  844. }
  845. }
  846. /* Begins initialization of WebCalendar.
  847. *
  848. * @param string $path Full path of page being viewed
  849. *
  850. * @access public
  851. */
  852. function initializeFirstPhase () {
  853. $this->_doInit ( 1 );
  854. }
  855. /* Continues initialization of WebCalendar.
  856. *
  857. * @param string $path Full path of page being viewed
  858. *
  859. * @access public
  860. */
  861. function initializeSecondPhase () {
  862. $this->_doInit ( 2 );
  863. }
  864. /* Sets the translation language.
  865. *
  866. * @access public
  867. */
  868. function setLanguage () {
  869. $this->_initTranslate ();
  870. }
  871. /* Construct an absolute path.
  872. *
  873. * @param string $path The path relative to the WebCalendar install directory
  874. *
  875. * @return string The absolute path
  876. */
  877. function absolutePath ( $path ) {
  878. return $this->_directory . $path;
  879. }
  880. }
  881. ?>