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

/web/trunk/forum/common.php

#
PHP | 421 lines | 381 code | 21 blank | 19 comment | 2 complexity | 0420603986b15f0d479b2d33b64b0347 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. if ( defined( "_COMMON_PHP" ) ) return;
  3. define("_COMMON_PHP", 1 );
  4. // These variables may be altered as needed:
  5. // location where settings are stored
  6. $settings_dir="/home/groups/e/ex/exult/.phorum"; // no ending slash
  7. // If you have dynamic vars for GET and POST to pass on:
  8. // AddGetPostVars("dummy", $dummy);
  9. //////////////////////////////////////////////////////////////////////////////////////////
  10. // End of normally user-defined variables
  11. //////////////////////////////////////////////////////////////////////////////////////////
  12. // See the FAQ on what this does. Normally not important.
  13. // **TODO: make this a define and figure out where we really need it.
  14. $cutoff = 800;
  15. $phorumver="3.3.2c";
  16. // all available db-files
  17. $dbtypes = array(
  18. 'mysql' => "MySQL",
  19. 'postgresql65' => "PostgreSQL 6.5 or newer",
  20. 'postgresql' => "PostgreSQL (older than 6.5)"
  21. );
  22. // handle configs that have register_globals turned off.
  23. // we use $PHP_SELF as the test since it should always be there.
  24. // We might need to consider not using globals soon.
  25. if(!isset($PHP_SELF)) {
  26. include ("./include/register_globals.php");
  27. }
  28. // *** Some Defines ***
  29. // security
  30. define("SEC_NONE", 0);
  31. define("SEC_OPTIONAL", 1);
  32. define("SEC_POST", 2);
  33. define("SEC_ALL", 3);
  34. // signature
  35. define("PHORUM_SIG_MARKER", "[%sig%]");
  36. // thread flags
  37. define("FLG_FROZEN", 1);
  38. define("FLG_MODERATED", 2); //not yet implemented
  39. define("FLG_UNMODERATED", 4); //not yet implemented
  40. define("FLG_KEEPONTOP", 8); //not yet implemented
  41. // **TODO: move all this into the admin
  42. $GetVars="";
  43. $PostVars="";
  44. function AddGetPostVars($var, $value){
  45. global $GetVars;
  46. global $PostVars;
  47. $var=urlencode($var);
  48. $value=urlencode($value);
  49. $GetVars.="&";
  50. $GetVars.="$var=$value";
  51. $PostVars.="<input type=\"hidden\" name=\"$var\" value=\"$value\">\n";
  52. }
  53. function AddPostVar($var, $value){
  54. AddGetPostVars($var, $value);
  55. }
  56. function AddGetVar($var, $value){
  57. AddGetPostVars($var, $value);
  58. }
  59. // **TODO: switch to get_html_translation_table
  60. function undo_htmlspecialchars($string){
  61. $string = str_replace("&amp;", "&", $string);
  62. $string = str_replace("&quot;", "\"", $string);
  63. $string = str_replace("&lt;", "<", $string);
  64. $string = str_replace("&gt;", ">", $string);
  65. return $string;
  66. }
  67. function htmlencode($string){
  68. $ret_string="";
  69. $len=strlen($string);
  70. for($x=0;$x<$len;$x++){
  71. $ord=ord($string[$x]);
  72. $ret_string .= "&#$ord;";
  73. }
  74. return $ret_string;
  75. }
  76. function my_nl2br($str){
  77. return str_replace("><br />", ">", nl2br($str));
  78. }
  79. function bgcolor($color){
  80. return ($color!="") ? " bgcolor=\"".$color."\"" : "";
  81. }
  82. // **TODO: replace with wordwrap soon. Will require some changes to the calls.
  83. function textwrap ($String, $breaksAt = 78, $breakStr = "\n", $padStr="") {
  84. $newString="";
  85. $lines=explode($breakStr, $String);
  86. $cnt=count($lines);
  87. for($x=0;$x<$cnt;$x++){
  88. if(strlen($lines[$x])>$breaksAt){
  89. $str=$lines[$x];
  90. while(strlen($str)>$breaksAt){
  91. $pos=strrpos(chop(substr($str, 0, $breaksAt)), " ");
  92. if ($pos == false) {
  93. break;
  94. }
  95. $newString.=$padStr.substr($str, 0, $pos).$breakStr;
  96. $str=trim(substr($str, $pos));
  97. }
  98. $newString.=$padStr.$str.$breakStr;
  99. }
  100. else{
  101. $newString.=$padStr.$lines[$x].$breakStr;
  102. }
  103. }
  104. return $newString;
  105. } // end textwrap()
  106. // **TODO: replace with a better function that optionally checks the MX record
  107. function is_email($email){
  108. $ret=false;
  109. if(function_exists("preg_match") && preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/i", $email)){
  110. $ret=true;
  111. }
  112. elseif(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$", $email)){
  113. $ret=true;
  114. }
  115. return $ret;
  116. }
  117. // passed to array_walk in read.php and list.php
  118. // **TODO: replace using array_flip
  119. function explode_haveread($var){
  120. global $haveread;
  121. $haveread[$var]=true;
  122. }
  123. // these two function would be better served as a class.
  124. function addnav(&$var, $text, $url){
  125. $var[$text]=$url;
  126. }
  127. function getnav($var, $splitter="&nbsp;&nbsp;|&nbsp;&nbsp;", $usefont=true){
  128. global $default_nav_font_color, $ForumNavFontColor;
  129. if(isset($ForumNavFontColor)){
  130. $color=$ForumNavFontColor;
  131. }
  132. else{
  133. $color=$default_nav_font_color;
  134. }
  135. $menu=array();
  136. while(list($text, $url)=each($var)){
  137. if($usefont) $text="<FONT color='$color' class=\"PhorumNav\">$text</font>";
  138. $menu[]="<a href=\"$url\">$text</a>";
  139. }
  140. $nav=implode($splitter, $menu);
  141. if($usefont)
  142. $nav="<FONT color='$color' class=\"PhorumNav\">&nbsp;".$nav."&nbsp;</font>";
  143. return $nav;
  144. }
  145. // These functions exist in PHP 4.0.3 and up.
  146. // **TODO: This will go away when we move to PHP4 only.
  147. if(!function_exists("is_uploaded_file")){
  148. function is_uploaded_file($filename) {
  149. $ret=false;
  150. if(dirname($filename)==dirname(tempnam(get_cfg_var("upload_tmp_dir"), ''))){
  151. $ret=true;
  152. }
  153. return $ret;
  154. }
  155. function move_uploaded_file($old_filename, $new_filename) {
  156. $ret=false;
  157. if(is_uploaded_file($old_filename) && rename($old_filename,$new_filename)) {
  158. $ret=true;
  159. }
  160. return $ret;
  161. }
  162. }
  163. function phorum_login_user($sessid, $userid=0){
  164. global $DB, $q, $pho_main;
  165. if(!isset($_COOKIE["phorum_auth"])){
  166. AddGetPostVars("phorum_auth", "$sessid");
  167. }
  168. // **TODO: We should make this time configurable
  169. SetCookie("phorum_auth", "$sessid", time()+86400*365);
  170. if($userid){
  171. $SQL="update $pho_main"."_auth set sess_id='$sessid' where id=$userid";
  172. $q->query($DB, $SQL);
  173. }
  174. }
  175. function phorum_get_file_name($type)
  176. {
  177. global $PHORUM;
  178. settype($PHORUM["ForumConfigSuffix"], "string");
  179. switch($type){
  180. case "css":
  181. $file="phorum.css";
  182. $custom="phorum_$PHORUM[ForumConfigSuffix].css";
  183. break;
  184. case "header":
  185. $file="$PHORUM[include]/header.php";
  186. $custom="$PHORUM[include]/header_$PHORUM[ForumConfigSuffix].php";
  187. break;
  188. case "footer":
  189. $file="$PHORUM[include]/footer.php";
  190. $custom="$PHORUM[include]/footer_$PHORUM[ForumConfigSuffix].php";
  191. break;
  192. }
  193. return (file_exists($custom)) ? $custom : $file;
  194. }
  195. function phorum_check_login($user, $pass)
  196. {
  197. global $q, $DB, $PHORUM;
  198. if(!get_magic_quotes_gpc()) $user=addslashes($user);
  199. $md5_pass=md5($pass);
  200. $id=0;
  201. $SQL="Select id from $PHORUM[auth_table] where username='$user' and password='$md5_pass'";
  202. $q->query($DB, $SQL);
  203. if($q->numrows()==0 && function_exists("crypt")){
  204. // check for old crypt system
  205. $crypt_pass=crypt($pass, substr($pass, 0, CRYPT_SALT_LENGTH));
  206. $SQL="Select id from $PHORUM[auth_table] where username='$user' and password='$crypt_pass'";
  207. $q->query($DB, $SQL);
  208. if($q->numrows()>0){
  209. // update password to md5.
  210. $SQL="Update $PHORUM[auth_table] set password='$md5_pass' where username='$user'";
  211. $q->query($DB, $SQL);
  212. }
  213. }
  214. if($q->numrows()>0){
  215. $id=$q->field("id", 0);
  216. }
  217. return $id;
  218. }
  219. function phorum_session_id($username, $password)
  220. {
  221. return md5($username.$password.microtime());
  222. }
  223. // variable initialization function
  224. // **TODO: need to scrap this function and just use settype()
  225. function initvar($varname, $value=''){
  226. global $$varname;
  227. if(!isset($$varname))
  228. $$varname=$value;
  229. return $$varname;
  230. }
  231. // set a sensible error level for including some stuff:
  232. $old_err_level = error_reporting (E_ERROR | E_WARNING | E_PARSE);
  233. // go ahead and unset/check these to evade hack attempts.
  234. unset($phorum_user);
  235. unset($PHORUM);
  236. settype($f, "integer");
  237. settype($num, "integer");
  238. $num = (empty($num)) ? $f : $num;
  239. $f = (empty($f)) ? $num : $f;
  240. // include forums.php
  241. // the most important variables
  242. $PHORUM["settings"]="$settings_dir/forums.php";
  243. $PHORUM["settings_backup"]="$settings_dir/forums.bak.php";
  244. if(!file_exists($PHORUM["settings"])){
  245. echo "<html><head><title>Phorum Error</title></head><body>Phorum could not load the settings file ($PHORUM[settings]).<br />If you are just installing Phorum, please go to the admin to complete the install. Otherwise, see the faq for other reasons you could see this message.</body></html>";
  246. exit();
  247. }
  248. include ($PHORUM["settings"]);
  249. // set some PHORUM vars
  250. $PHORUM["auth_table"]=$PHORUM["main_table"]."_auth";
  251. $PHORUM["mod_table"]=$PHORUM["main_table"]."_moderators";
  252. $PHORUM["settings_dir"]=$settings_dir;
  253. $PHORUM["include"]="./include";
  254. // **TODO: remove legacy code
  255. $include_path=$PHORUM["include"];
  256. $pho_main=$PHORUM['main_table'];
  257. // include abstraction layer and check if its defined
  258. if(!defined("PHORUM_ADMIN") && (empty($PHORUM["dbtype"]) || !file_exists("./db/$PHORUM[dbtype].php"))){
  259. echo "<html><head><title>Phorum Error</title></head><body>Something is wrong. You need to edit common.php and select a database.</body></html>";
  260. exit();
  261. }
  262. include ("./db/$dbtype.php");
  263. // create database classes
  264. $DB = new db();
  265. // check if database is already configured or if we are in the admin
  266. if ( defined( "_DB_LAYER" ) && $PHORUM["DatabaseName"]!=''){
  267. // this code below has to be this way for some weird reason. Otherwise\n";
  268. // connecting on a different port won't work.\n";
  269. $DB->open($PHORUM["DatabaseName"], implode(':', explode(':', $PHORUM["DatabaseServer"])), $PHORUM["DatabaseUser"], $PHORUM["DatabasePassword"]);
  270. } elseif(!defined("PHORUM_ADMIN")) {
  271. echo "<html><head><title>Phorum Error</title></head><body>You need to go to the admin and fix your database settings.</body></html>";
  272. exit();
  273. }
  274. //dummy query for generic operations
  275. $q = new query($DB);
  276. if(!is_object($q)){
  277. echo "<html><head><title>Phorum Error</title></head><body>Unkown error creating $q.</body></html>";
  278. exit();
  279. }
  280. if(!empty($f)){
  281. if(file_exists("$PHORUM[settings_dir]/$f.php")){
  282. include "$PHORUM[settings_dir]/$f.php";
  283. if($ForumLang!=""){
  284. include ("./".$ForumLang);
  285. } else {
  286. include ("./".$default_lang);
  287. }
  288. }
  289. else{
  290. header("Location: $forum_url/$forum_page.$ext");
  291. exit();
  292. }
  293. }
  294. else {
  295. include ("./".$default_lang);
  296. include ($include_path."/blankset.php");
  297. }
  298. if(!$PHORUM["started"] && !defined("PHORUM_ADMIN")){
  299. Header("Location: $forum_url/$down_page.$ext");
  300. exit();
  301. }
  302. if(!defined("PHORUM_ADMIN") && $DB->connect_id){
  303. // check security
  304. if($ForumFolder==1 || $f==0){
  305. $SQL="Select max(security) as sec from $pho_main";
  306. $q->query($DB, $SQL);
  307. $max_sec=$q->field("sec", 0);
  308. }
  309. if(($ForumSecurity!=SEC_NONE || (($ForumFolder==1 || $f==0) && $max_sec>0)) && isset($phorum_auth)){
  310. $SQL="Select * from $PHORUM[auth_table] where sess_id='$phorum_auth'";
  311. $q->query($DB, $SQL);
  312. $phorum_user=$q->getrow();
  313. if(isset($phorum_user["id"])){
  314. $SQL="Select forum_id from $PHORUM[mod_table] where (forum_id=$f or forum_id=0) and user_id=$phorum_user[id]";
  315. $q->query($DB, $SQL);
  316. $phorum_user["moderator"] = ($q->numrows()>0) ? true : false;
  317. if(!isset($_COOKIE["phorum_auth"])){
  318. AddGetPostVars("phorum_auth", "$phorum_auth");
  319. }
  320. }
  321. }
  322. if(!isset($phorum_user["id"]) && isset($phorum_auth)) unset($phorum_auth);
  323. if($ForumSecurity==SEC_ALL && empty($phorum_auth)){
  324. header("Location: $forum_url/login.$ext?target=".urlencode($REQUEST_URI));
  325. exit();
  326. }
  327. // load plugins
  328. unset($plugins);
  329. $plugins = array(
  330. "read_body" => array(),
  331. "read_header" => array()
  332. );
  333. if(isset($PHORUM["plugins"])){
  334. $dir = opendir("./plugin/");
  335. while($plugindirname = readdir($dir)) {
  336. if($plugindirname[0] != "." && @file_exists("./plugin/$plugindirname/plugin.php") && !empty($PHORUM["plugins"][$plugindirname])){
  337. include("./plugin/$plugindirname/plugin.php");
  338. }
  339. }
  340. }
  341. }
  342. // set the error level back to what it was.
  343. error_reporting ($old_err_level);
  344. // work-around SourceForge automatically sending an Expires header
  345. // two days in the future
  346. Header("Expires: Sat, 01 Jan 2000 00:00:00 GMT");
  347. ?>