PageRenderTime 62ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

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

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