PageRenderTime 40ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/authorisation.php

http://airscore.googlecode.com/
PHP | 252 lines | 241 code | 6 blank | 5 comment | 29 complexity | c181de760e36b6f7cd3caf25ab2886a7 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. define('BINDIR', '%CGIBIN%');
  3. define('FILEDIR', '%TRACKDIR%');
  4. function redirect($loc)
  5. {
  6. echo "<script language=\"JavaScript\" type=\"text/javascript\">";
  7. echo "<!--\n";
  8. echo "window.location.href=\"$loc\";";
  9. echo "//-->\n";
  10. echo "</script>\n";
  11. }
  12. function db_connect()
  13. {
  14. $link = mysql_connect('localhost', 'xc', 'x323c')
  15. or die('Could not connect: ' . mysql_error());
  16. mysql_select_db('xcdb') or die('Could not select database');
  17. return $link;
  18. }
  19. function is_admin($what,$usePk,$comPk)
  20. {
  21. $query = "select useLevel from tblCompAuth where usePk=$usePk and comPk in ($comPk,-1)";
  22. $result = mysql_query($query) or die('Admin check failed: ' . mysql_error());
  23. while($row = mysql_fetch_array($result))
  24. {
  25. $level = $row['useLevel'];
  26. if ($what == $level)
  27. {
  28. return 1;
  29. }
  30. }
  31. return 0;
  32. }
  33. function check_admin($what,$usePk,$comPk)
  34. {
  35. if (is_admin($what,$usePk,$comPk))
  36. {
  37. return 1;
  38. }
  39. echo "You are unauthorised to perform that action ($what $usePk $comPk).<br>";
  40. //redirect('unauthorised.php');
  41. return 0;
  42. }
  43. function check_auth($region)
  44. {
  45. $link = db_connect();
  46. // FIX: check time/region/IP stuff too ...
  47. // but for no just validate the session
  48. if (!array_key_exists('XCauth', $_COOKIE))
  49. {
  50. return 0;
  51. }
  52. $magic = addslashes($_COOKIE['XCauth']);
  53. $query = "select usePk from tblUserSession where useSession='$magic'";
  54. $result = mysql_query($query) or die('Query failed: ' . mysql_error());
  55. $usePk = 0;
  56. if (mysql_num_rows($result) > 0)
  57. {
  58. $usePk = mysql_result($result,0);
  59. }
  60. mysql_close($link);
  61. if (!$usePk)
  62. {
  63. return 0;
  64. }
  65. return $usePk;
  66. }
  67. function auth($region)
  68. {
  69. $usePk = 0;
  70. $browser = addslashes($_SERVER['HTTP_USER_AGENT']);
  71. if ((strpos($browser, "MSIE 6.0; Windows") || strpos($browser, "MSIE 7.0; Windows")) && strpos($browser, "Opera") == FALSE)
  72. {
  73. redirect("better_browsers.php");
  74. exit;
  75. }
  76. $usePk = check_auth($region);
  77. if ($usePk == 0)
  78. {
  79. // auth fails - redirect to login.
  80. echo "<b><i>Authorisation Failed</i></b>";
  81. redirect("login.php?message=Authorisation%20Failed:$magic");
  82. exit;
  83. }
  84. return $usePk;
  85. }
  86. function menubar($comPk)
  87. {
  88. if ($comPk != 0)
  89. {
  90. $link = db_connect();
  91. $query = "select comName,comType from tblCompetition where comPk=$comPk";
  92. $result = mysql_query($query) or die('Query failed: ' . mysql_error());
  93. if (mysql_num_rows($result) > 0)
  94. {
  95. $comName = mysql_result($result,0,0);
  96. $comType = mysql_result($result,0,1);
  97. }
  98. else
  99. {
  100. $comName = 'Unknown Competition';
  101. $comType = 'OLC';
  102. }
  103. mysql_close($link);
  104. echo "<div id=\"vhead\"><h1>$comName</h1></div>";
  105. }
  106. else
  107. {
  108. echo "<div id=\"vhead\"><h1>Skyhigh Cup</h1></div>";
  109. $comPk = 5;
  110. }
  111. echo "<div id=\"menu\">";
  112. echo "<ul>";
  113. if (check_auth("system"))
  114. {
  115. if ($comPk > 0)
  116. {
  117. echo "<li><a href=\"competition.php?comPk=$comPk\">Admin</a></li>";
  118. }
  119. else
  120. {
  121. echo "<li><a href=\"comp_admin.php\">Admin</a></li>";
  122. }
  123. }
  124. if ($comType == 'OLC')
  125. {
  126. echo "<li><a href=\"index.php\">Home</a></li>";
  127. echo "<li><a href=\"top_scores.php?comPk=$comPk\">Top Scores</a></li>";
  128. echo "<li><a href=\"top.php?comPk=$comPk\">Top Tracks</a></li>";
  129. echo "<li><a href=\"recent.php?comPk=$comPk\">Recent Tracks</a></li>";
  130. }
  131. else
  132. {
  133. echo "<li><a href=\"compview.php?comPk=$comPk\">Home</a></li>";
  134. echo "<li><a href=\"comp_result.php?comPk=$comPk\">Scores</a></li>";
  135. echo "<li><a href=\"recent.php?comPk=$comPk\">Recent Tracks</a></li>";
  136. }
  137. echo "<li><a href=\"submit_track.php?comPk=$comPk\">Submit Track</a></li>";
  138. echo "</ul>";
  139. echo "</div>\n";
  140. }
  141. function adminbar($comPk)
  142. {
  143. echo "<div id=\"menu\">";
  144. echo "<ul>";
  145. if (check_auth("system"))
  146. {
  147. echo "<li><a href=\"login.php?logout=1\">Logout</a></li>";
  148. }
  149. else
  150. {
  151. echo "<li><a href=\"login.php\">Login</a></li>";
  152. }
  153. echo "<li><a href=\"comp_admin.php\">Competition</a></li>";
  154. if ($comPk > 0)
  155. {
  156. $link = db_connect();
  157. $query = "select comName,comType,comEntryRestrict from tblCompetition where comPk=$comPk";
  158. $result = mysql_query($query) or die('Query failed: ' . mysql_error());
  159. if (mysql_num_rows($result) > 0)
  160. {
  161. $comName = mysql_result($result,0,0);
  162. $comType = mysql_result($result,0,1);
  163. $comEntryRestrict = mysql_result($result,0,2);
  164. }
  165. else
  166. {
  167. $comName = 'Unknown Competition';
  168. $comType = 'OLC';
  169. }
  170. mysql_close($link);
  171. echo "<li><a href=\"comp_result.php?comPk=$comPk\">Scores</a></li>";
  172. echo "<li><a href=\"track_admin.php?comPk=$comPk\">Track</a></li>";
  173. echo "<li><a href=\"team_admin.php?comPk=$comPk\">Teams</a></li>";
  174. echo "<li><a href=\"pilot_admin.php?comPk=$comPk\">Pilots</a></li>";
  175. if ($comEntryRestrict == 'registered')
  176. {
  177. echo "<li><a href=\"registration.php?comPk=$comPk\">Registration</a></li>";
  178. }
  179. }
  180. else
  181. {
  182. echo "<li><a href=\"track_admin.php\">Track</a></li>";
  183. echo "<li><a href=\"pilot_admin.php\">Pilots</a></li>";
  184. }
  185. echo "<li><a href=\"region_admin.php\">Waypoints</a></li>";
  186. echo "<li><a href=\"airspace_admin.php\">Airspace</a></li>";
  187. echo "</ul>";
  188. echo "</div>";
  189. }
  190. function output_select($name,$selected,$options)
  191. {
  192. echo "<select name=\"$name\">";
  193. foreach ($options as $key => $value)
  194. {
  195. if (is_int($key))
  196. {
  197. $key = $value;
  198. }
  199. if ($selected == $value)
  200. {
  201. echo "<option value=\"$value\" selected>$key</option>\n";
  202. }
  203. else
  204. {
  205. echo "<option value=\"$value\">$key</option>\n";
  206. }
  207. }
  208. echo "</select>\n";
  209. }
  210. function waypoint_select($link,$tasPk,$name,$selected)
  211. {
  212. $query="select distinct RW.* from tblTask T, tblRegion R, tblRegionWaypoint RW where T.tasPk=$tasPk and RW.regPk=R.regPk and R.regPk=T.regPk order by RW.rwpName";
  213. $result = mysql_query($query) or die('Waypoint select failed: ' . mysql_error());
  214. $waypoints = array();
  215. while($row = mysql_fetch_array($result))
  216. {
  217. $rwpPk = $row['rwpPk'];
  218. $rname = $row['rwpName'];
  219. $waypoints[$rname] = $rwpPk;
  220. }
  221. //ksort($waypoints);
  222. output_select($name,$selected,$waypoints);
  223. }
  224. function reqival($key)
  225. {
  226. if (array_key_exists($key, $_REQUEST))
  227. {
  228. return intval($_REQUEST[$key]);
  229. }
  230. else
  231. {
  232. return 0;
  233. }
  234. }
  235. function reqsval($key)
  236. {
  237. if (array_key_exists($key, $_REQUEST))
  238. {
  239. return addslashes($_REQUEST[$key]);
  240. }
  241. else
  242. {
  243. return '';
  244. }
  245. }
  246. ?>