PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/include/inc.php

https://github.com/aaronpk/GraphThis
PHP | 93 lines | 68 code | 22 blank | 3 comment | 19 complexity | 9c01bee8b1b0f36988e2fa91ff53e09a MD5 | raw file
  1. <?php
  2. ini_set('error_reporting', E_ALL);
  3. date_default_timezone_set('UTC');
  4. if(!file_exists(dirname(__FILE__) . '/config.php'))
  5. {
  6. die('Setup not complete: Copy config.template.php to config.php and modify the configuration settings to match your environment.');
  7. }
  8. require_once(dirname(__FILE__) . '/config.php');
  9. if(!array_key_exists('SHELL', $_SERVER)) {
  10. session_set_cookie_params(365*24*60*60);
  11. session_start();
  12. }
  13. $START_TIME = microtime(TRUE);
  14. require_once('functions.php');
  15. require_once('Model.php');
  16. require_once('Model/User.php');
  17. require_once('Model/Graph.php');
  18. require_once('Model/GraphSeries.php');
  19. require_once('Model/GraphData.php');
  20. require_once('Model/GraphDataNote.php');
  21. require_once('Model/GraphString.php');
  22. $headerTags = array();
  23. $user = FALSE;
  24. if(!array_key_exists('argv', $_SERVER)) {
  25. // Don't do this stuff for cli scripts
  26. if(get('login_token') && session('noLoginRedirect') != 1) {
  27. $user = Model_User::createFromLoginToken(get('login_token'));
  28. if($user) {
  29. $_SESSION['userID'] = $user->id;
  30. Model_User::update(array('lastLoginAt'=>date('Y-m-d H:i:s')), $user->id);
  31. // Redirect to the page they were trying to get to
  32. $redirect = trim(str_replace('login_token=' . get('login_token'), '', $_SERVER['REQUEST_URI']), '?');
  33. if($redirect != '/login') {
  34. header('Location: ' . $redirect);
  35. die();
  36. }
  37. }
  38. } else {
  39. if(session('userID')) {
  40. $user = new Model_User(session('userID'));
  41. }
  42. }
  43. // Redirect to the login page if they're not logged in now
  44. if(($user == FALSE || $user->id == FALSE) && !in_array($_SERVER['SCRIPT_NAME'], array('/login.php', '/index.php', '/help.php'))) {
  45. header('Location: /login');
  46. die();
  47. }
  48. }
  49. if(($user && $user->id != FALSE)) {
  50. $timezone = new DateTimeZone($user->timezone);
  51. date_default_timezone_set($user->timezone);
  52. }
  53. function formatText($text) {
  54. $text = nl2br($text);
  55. $expressions[] = '|(https?://[^\s]+)|';
  56. $replacements[] = '<a href="$1">$1</a>';
  57. $expressions[] = '/(?<![a-z0-9_])@([a-z0-9_]+)/i';
  58. $replacements[] = '<a href="http://twitter.com/$1">@$1</a>';
  59. $expressions[] = '|([a-z0-9_\.\+\-]+@[a-z0-9_\.\+\-]+\.[a-z0-9\.]{2,4})|i';
  60. $replacements[] = '<a href="mailto:$1">$1</a>';
  61. $text = preg_replace($expressions, $replacements, $text);
  62. return $text;
  63. }
  64. function uuidSecure()
  65. {
  66. return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  67. mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
  68. mt_rand( 0, 0x0fff ) | 0x4000,
  69. mt_rand( 0, 0x3fff ) | 0x8000,
  70. mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ) );
  71. }