PageRenderTime 27ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/tfsbox.class.php

http://tfsbox.googlecode.com/
PHP | 332 lines | 243 code | 63 blank | 26 comment | 58 complexity | 1a9a4bed1dbcb2f25d28c6bcb8ae38ff MD5 | raw file
  1. <?php
  2. if (substr(phpversion(), 0, 1) < 5)
  3. exit ("<b>*** PHP 5 or higher is required.</b>");
  4. // PHP 6
  5. if (!function_exists('get_magic_quotes_gpc'))
  6. {
  7. function get_magic_quotes_gpc()
  8. {
  9. return false;
  10. }
  11. }
  12. function stripslashes_deep($value)
  13. {
  14. if (is_array($value))
  15. $value = array_map('stripslashes_deep', $value);
  16. else if (!empty($value) && is_string($value))
  17. $value = stripslashes($value);
  18. return $value;
  19. }
  20. if(get_magic_quotes_gpc())
  21. {
  22. stripslashes_deep($_GET);
  23. stripslashes_deep($_POST);
  24. stripslashes_deep($_REQUEST);
  25. stripslashes_deep($_COOKIE);
  26. stripslashes_deep($_SESSION);
  27. }
  28. function gen_salt($randnum)
  29. {
  30. for ($i = 0; $i < 20; $i++)
  31. $randnum .= mt_rand(0, 60);
  32. return md5($randnum);
  33. }
  34. require_once "tfsbox_debugger.class.php";
  35. require_once "tfsbox_config.class.php";
  36. require_once "tfsbox_database.class.php";
  37. require_once "tfsbox_session.class.php";
  38. require_once "tfsbox_comments_view.class.php";
  39. class TFsBOX extends TFsBOXDebugger
  40. {
  41. const Name = "TrueFusion sBOX v4 (Beta 3)";
  42. const Version = "3.9.6";
  43. const EmailRegex = "#^[\w\d-]+@[\w\d-]+\.[\w\.]{2,5}$#";
  44. const IpRegex = "#^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$#";
  45. const NameRegex = "#^[a-zA-Z][\w\d]+$#";
  46. const SiteRegex = "#^http://[\w\d\.]+/$#";
  47. const USERNAME = "admin"; ///< User name for flatfile mode (change if you want).
  48. const PASSWORD = ""; ///< Password for flatfile mode; leaving it empty means no one can log in.
  49. static $Config;
  50. static $Database;
  51. static $Session;
  52. static $View;
  53. function __construct($config_log = null,
  54. $shouts_log = null,
  55. $ip_log = null,
  56. $url_log = null,
  57. $wordfilters_log = null,
  58. $config_obj = null,
  59. $db_obj = null,
  60. $sess_obj = null,
  61. $view_obj = null)
  62. {
  63. if (file_exists("VERSION") && is_writable("VERSION"))
  64. file_put_contents("VERSION", TFsBOX::Version);
  65. date_default_timezone_set("Etc/Universal");
  66. define("TFsBOX_SITE", "http://".$_SERVER['SERVER_NAME']."/");
  67. // Config Log
  68. if ($config_log)
  69. define("TFsBOX_CONFIG_FILE", realpath($config_log));
  70. else
  71. define("TFsBOX_CONFIG_FILE", dirname(__FILE__).'/logs/config.log');
  72. // Shouts Log
  73. if ($shouts_log)
  74. define("TFsBOX_SHOUTS_FILE", realpath($shouts_log));
  75. else
  76. define("TFsBOX_SHOUTS_FILE", dirname(__FILE__).'/logs/shouts.log');
  77. // IP Log
  78. if ($ip_log)
  79. define("TFsBOX_IP_FILE", realpath($ip_log));
  80. else
  81. define("TFsBOX_IP_FILE", dirname(__FILE__).'/logs/ipban.log');
  82. // URL Log
  83. if ($url_log)
  84. define("TFsBOX_URL_FILE", realpath($url_log));
  85. else
  86. define("TFsBOX_URL_FILE", dirname(__FILE__).'/logs/urlban.log');
  87. // Word Filters Log
  88. if ($wordfilters_log)
  89. define("TFsBOX_WORDFILTERS_FILE", realpath($wordfilters_log));
  90. else
  91. define("TFsBOX_WORDFILTERS_FILE", dirname(__FILE__).'/logs/wordfilter.log');
  92. // Initial setup
  93. if (is_subclass_of($config_obj, 'TFsBOXConfig'))
  94. self::$Config = new $config_obj(TFsBOX_CONFIG_FILE);
  95. else
  96. self::$Config = new TFsBOXConfig(TFsBOX_CONFIG_FILE);
  97. // Must come after $Config.
  98. if (is_subclass_of($db_obj, 'TFsBOXDatabase'))
  99. self::$Database = new $db_obj;
  100. else
  101. self::$Database = new TFsBOXDatabase;
  102. // Must come after $Database.
  103. if (is_subclass_of($sess_obj, 'TFsBOXSession'))
  104. self::$Session = new $sess_obj;
  105. else
  106. self::$Session = new TFsBOXSession;
  107. // Must come after $Session.
  108. if (is_subclass_of($view_obj, 'TFsBOXViewInterface'))
  109. self::$View = new $view_obj;
  110. else
  111. self::$View = new TFsBOXShoutsView;
  112. }
  113. static protected function filterAttributes($match)
  114. {
  115. $tmp = explode(" ", $match[1]);
  116. $elem = $tmp[0];
  117. unset($tmp[0]); // simplest way to remove element
  118. $allowed_attrs = array("href", "src", "style", "alt", "target");
  119. foreach ($tmp as $attr)
  120. {
  121. $attr = explode("=", $attr);
  122. // If a non-allowed attribute is found,
  123. // or if a javascript command is found,
  124. // return just the element.
  125. if (!in_array($attr[0], $allowed_attrs)
  126. || preg_match("#^\s*\"?\s*javascript\:#", strtolower($attr[1])))
  127. return "<$elem>";
  128. }
  129. return $match[0];
  130. }
  131. static function filterShout($shout)
  132. {
  133. $img = "";
  134. if (self::$Config->value("allow_images", "no") == "yes")
  135. $img .= "<img>";
  136. $shout = strip_tags($shout, "<a><span><b><i><u><s><sup><sub>$img");
  137. $shout = preg_replace_callback("#<([^<]+)>#i", "TFsBOX::filterAttributes", $shout);
  138. $shout = nl2br($shout);
  139. foreach (self::$Database->wordFilters() as $filter)
  140. $shout = preg_replace("#\b".preg_quote($filter->word)."\b#", $filter->filter, $shout);
  141. return $shout;
  142. }
  143. static function pathInfo()
  144. {
  145. static $path_info = NULL;
  146. if ($path_info === NULL)
  147. $path_info = str_replace($_SERVER['SCRIPT_NAME'], '', $_SERVER['PATH_INFO']);
  148. return $path_info;
  149. }
  150. function parsePathInfo()
  151. {
  152. if (TFsBOX::pathInfo() == "/tfsbox/shouts")
  153. define("TFsBOX_SHOUTS_ONLY", 1);
  154. // Report shout
  155. if (preg_match("#^/tfsbox/report/\d+$#", TFsBOX::pathInfo()))
  156. {
  157. $id = (int) TFsBOXSession::$PathInfo[count(TFsBOXSession::$PathInfo)-1];
  158. self::$Database->report($id);
  159. TFsBOX::redirect($_SERVER['SCRIPT_NAME']);
  160. }
  161. // Clear report
  162. if (preg_match("#^/tfsbox/reports/clear/\d+$#", TFsBOX::pathInfo()))
  163. {
  164. $id = (int) TFsBOXSession::$PathInfo[count(TFsBOXSession::$PathInfo)-1];
  165. self::$Database->clearReport($id);
  166. TFsBOX::redirect($_SERVER['SCRIPT_NAME']);
  167. }
  168. // Edit shout
  169. if (preg_match("#^/tfsbox/shouts/edit#", TFsBOX::pathInfo()))
  170. {
  171. if ($_POST['tfsbox_shout_id'] > -1)
  172. {
  173. // Assume user has requested to edit shout
  174. if (empty($_POST['tfsbox_shout']))
  175. {
  176. // Retrieve shout
  177. echo htmlentities(self::$Database->shout($_POST['tfsbox_shout_id']));
  178. exit(); // Prevent other data from rendering
  179. }
  180. // Assume user has finished editing shout
  181. else
  182. self::$Database->editShout($_POST['tfsbox_shout_id']);
  183. TFsBOX::redirect($_SERVER['SCRIPT_NAME']);
  184. }
  185. }
  186. // Add shout (else-if to avoid duplicate entries)
  187. else if (self::$Session->hasShoutReady()
  188. && !self::$Session->hasErrors())
  189. self::$Database->addShout();
  190. // Delete shout
  191. if (preg_match("#^/tfsbox/shouts/delete/\d+#", TFsBOX::pathInfo()))
  192. {
  193. $id = (int) TFsBOXSession::$PathInfo[count(TFsBOXSession::$PathInfo)-1];
  194. self::$Database->deleteShout($id);
  195. TFsBOX::redirect($_SERVER['SCRIPT_NAME']);
  196. }
  197. // Ban IP
  198. if (preg_match("#^/tfsbox/ban/ip/\d+#", TFsBOX::pathInfo()))
  199. {
  200. $ip = long2ip(TFsBOXSession::$PathInfo[count(TFsBOXSession::$PathInfo)-1]);
  201. if (self::$Database->banIp($ip))
  202. TFsBOX::redirect($_SERVER['SCRIPT_NAME']);
  203. else TFsBOXSession::storeError("There was a problem banning the IP address <tt>$ip</tt>.");
  204. }
  205. // Ban URL
  206. if (TFsBOX::pathInfo() == "/tfsbox/ban/url")
  207. {
  208. if (self::$Database->banUrl(rawurldecode($_SERVER['QUERY_STRING'])))
  209. TFsBOX::redirect($_SERVER['SCRIPT_NAME']);
  210. else TFsBOXSession::storeError("There was a problem banning the URL <tt>". $_SERVER['QUERY_STRING'] ."</tt>");
  211. }
  212. // Subscribe
  213. if (preg_match("#^/tfsbox/subscribe/#", TFsBOX::pathInfo()))
  214. {
  215. $sub = TFsBOXSession::$PathInfo[count(TFsBOXSession::$PathInfo)-1];
  216. $user_id = self::$Session->userId();
  217. if ($user_id)
  218. {
  219. if (self::$Database->subscribe($user_id, $sub))
  220. TFsBOX::redirect($_SERVER['SCRIPT_NAME']);
  221. else
  222. TFsBOXSession::storeError("There was a problem subscribing to <tt>$sub</tt>.");
  223. }
  224. }
  225. // Log out
  226. if (TFsBOX::pathInfo() == "/tfsbox/logout")
  227. self::$Session->endSession();
  228. }
  229. static function passwordHash($username, $password)
  230. {
  231. return sha1(md5($username).md5($password));
  232. }
  233. static function redirect($location)
  234. {
  235. header('Location: '. $location);
  236. exit(); // Necessary?
  237. }
  238. function renderShoutBox($show_title_bar = true)
  239. {
  240. if ($show_title_bar)
  241. TFsBOXAbstractView::renderShoutBoxTitleBar();
  242. $form_position = self::$Session->formPosition();
  243. TFsBOXAbstractView::renderErrors((!self::$Session->hasErrors()));
  244. TFsBOXAbstractView::renderNotices((!self::$Session->hasNotices()));
  245. echo "<div id=\"tfsbox_body\">\n";
  246. // echo "<h2 style=\"text-align: center;\">This represents the latest source code from the <a href=\"http://code.google.com/p/tfsbox/source/checkout\">SVN trunk.</a></h2>\n";
  247. if (self::$Database->allowedToShout(false, false))
  248. {
  249. if ($form_position == "top"
  250. || $form_position == "left")
  251. self::$View->renderShoutForm();
  252. }
  253. echo "<div id=\"tfsbox_content\">\n";
  254. self::$View->renderShouts(true, true, false);
  255. echo "</div>\n";
  256. if (self::$Database->allowedToShout(false, false))
  257. {
  258. if ($form_position == "bottom"
  259. || $form_position == "right")
  260. self::$View->renderShoutForm();
  261. }
  262. echo "</div>\n";
  263. }
  264. function setView(TFsBOXViewInterface $view)
  265. {
  266. self::$View = $view;
  267. }
  268. }
  269. ?>