PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/replies/fx.php

https://github.com/ntulip/TwitApps
PHP | 181 lines | 122 code | 21 blank | 38 comment | 18 complexity | 15a03f7608005e7623964cf705edba11 MD5 | raw file
Possible License(s): MIT, LGPL-2.1
  1. <?php
  2. ini_set('precision', 20);
  3. define('ROOTDIR', dirname(__FILE__).'/');
  4. define('CODEDIR', ROOTDIR.'code/');
  5. define('TPLDIR', ROOTDIR.'tpl/');
  6. require ROOTDIR.'../common/shared.php';
  7. require ROOTDIR.'../common/twitter.class.php';
  8. require ROOTDIR.'../common/phpmailer/class.phpmailer.php';
  9. require CODEDIR.'queue.class.php';
  10. require CODEDIR.'user.class.php';
  11. date_default_timezone_set('Europe/London');
  12. ShowErrors(true);
  13. require ROOTDIR.'config.php';
  14. if (file_exists(ROOTDIR.'config_dev.php')) require ROOTDIR.'config_dev.php';
  15. Twitter::Config($_twitter);
  16. function & GetDB()
  17. {
  18. static $_db = false;
  19. if ($_db === false)
  20. {
  21. $_db = mysql_connect('localhost', 'ta_replies', 'sdfoihsdukbsdkvhsldivhgsdlig') or die('DB gone? <!-- '.mysql_error().' -->');
  22. mysql_select_db('twitapps_replies', $_db) or die('DB dead? <!-- '.mysql_error().' -->');
  23. mysql_query('SET NAMES utf8', $_db);
  24. }
  25. return $_db;
  26. }
  27. /**
  28. * Output the layout header and set up the footer
  29. * @param string $title
  30. * @param string $section
  31. */
  32. function Layout($title, $section = '', $data = array())
  33. {
  34. // Expire the page immediately
  35. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
  36. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  37. // always modified
  38. header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
  39. header("Cache-Control: post-check=0, pre-check=0", false);
  40. header("Pragma: no-cache"); // HTTP/1.0
  41. // Now output the page
  42. $GLOBALS['layoutdata'] = $data;
  43. $GLOBALS['layoutdata']['title'] = $title;
  44. $GLOBALS['layoutdata']['section'] = $section;
  45. register_shutdown_function('Footer');
  46. TPL('header', $GLOBALS['layoutdata']);
  47. }
  48. /**
  49. * Output the layout footer
  50. */
  51. function Footer()
  52. {
  53. TPL('footer', $GLOBALS['layoutdata']);
  54. }
  55. /**
  56. * Turn error display on or off
  57. *
  58. * @param bool $show
  59. */
  60. function ShowErrors($show)
  61. {
  62. error_reporting(E_ALL);
  63. ini_set('display_errors', ($show ? 1 : 0));
  64. }
  65. /**
  66. * Turn off output buffering
  67. */
  68. function NoBuffers()
  69. {
  70. while (@ob_end_clean());
  71. }
  72. /**
  73. * "Execute" a template
  74. *
  75. * @param string $filename
  76. * @param array $data
  77. * @return string
  78. */
  79. function TPL($____filename, $____data = array(), $____return = false)
  80. {
  81. $____filename = TPLDIR.$____filename.'.tpl.php';
  82. if ($____return) ob_start();
  83. extract($____data);
  84. include $____filename;
  85. if ($____return)
  86. {
  87. $retval = ob_get_contents();
  88. ob_end_clean();
  89. return $retval;
  90. }
  91. }
  92. /**
  93. * Simple function to split the URL by /, trim it and return the array.
  94. * Example: /blog/2007/01/14/example-post
  95. * ParseURL() => array('blog', '2007', '01', '14', 'example-post')
  96. * ParseURL(1) => array('2007', '01', '14', 'example-post')
  97. * ParseURL(4) => array('example-post')
  98. *
  99. * @param string $url The URL to parse
  100. * @param integer $skip The number of items to drop from the start
  101. * @return array
  102. */
  103. function ParseURL($url = false, $skip = 0)
  104. {
  105. if ($url === false) $url = $_SERVER['REQUEST_URI'];
  106. $bits = explode('?', $url);
  107. $bits = explode('/', array_shift($bits));
  108. while (count($bits) > 0 and strlen($bits[0]) == 0)
  109. array_shift($bits);
  110. while (count($bits) > 0 and strlen($bits[count($bits)-1]) == 0)
  111. array_pop($bits);
  112. while ($skip-- > 0)
  113. array_shift($bits);
  114. return $bits;
  115. }
  116. function Authenticate($realm = '', $users = false)
  117. {
  118. if (empty($realm)) $realm = 'Restricted area';
  119. // user => password
  120. if ($users === false) $users = array('admin' => 'password');
  121. if (!isset($_SERVER['PHP_AUTH_USER']))
  122. {
  123. header('WWW-Authenticate: Basic realm="'.$realm.'"');
  124. header('HTTP/1.0 401 Unauthorized');
  125. die('<h1>Unauthorised</h1><p>You are not authorised to view this page</p>');
  126. }
  127. else
  128. {
  129. if (!isset($users[$_SERVER['PHP_AUTH_USER']]) or $users[$_SERVER['PHP_AUTH_USER']] != $_SERVER['PHP_AUTH_PW'])
  130. die('<h1>Unauthorised</h1><p>You are not authorised to view this page: Invalid credentials</p>');
  131. }
  132. }
  133. function InDays($stamp)
  134. {
  135. $retval = date('F jS, Y @ h:ia', $stamp);
  136. $diff = time() - $stamp;
  137. if ($diff <= 50)
  138. $retval = 'less than a minute ago';
  139. elseif ($diff < 70)
  140. $retval = 'about a minute ago';
  141. elseif ($diff < 180)
  142. $retval = 'a few minutes ago';
  143. elseif ($diff < 290)
  144. $retval = 'less than 5 minutes ago';
  145. elseif ($diff < 3300)
  146. $retval = 'about '.floor($diff / 60).' minutes ago';
  147. elseif ($diff < 3900)
  148. $retval = 'about an hour ago';
  149. elseif ($diff < 82800)
  150. $retval = 'about '.ceil($diff / 3600).' hours ago';
  151. else
  152. {
  153. $days = floor($diff / 86400);
  154. if ($days < 2)
  155. $retval = 'yesterday';
  156. else
  157. $retval = $days.' days ago';
  158. }
  159. return $retval;
  160. }