PageRenderTime 50ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/HistorySite/editor/util.php

http://oregon-caspages.googlecode.com/
PHP | 516 lines | 357 code | 119 blank | 40 comment | 63 complexity | 62568e2956c12c442e1b4b63aa90cbaa MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-3.0
  1. <?php
  2. include 'editor_conf.php';
  3. function connect_db(){
  4. //echo 'connecting to db<br>';
  5. $host="localhost";
  6. $db_user="phpmyadmin";
  7. $db_password="password";
  8. $database="history";
  9. $mysql_link = mysql_connect($host, $db_user, $db_password) or die(mysql_error());
  10. mysql_select_db($database) or die(mysql_error());
  11. //echo 'connected db<br>';
  12. return $mysql_link;
  13. }
  14. function userLogin($username, $password){
  15. //echo 'logging in<br>';
  16. $mysql_link = connect_db();
  17. //echo 'pass = ' . $password . '<br>';
  18. $password = md5($password);
  19. //echo $password;
  20. $result=mysql_query("SELECT * FROM user_info WHERE username='$username'
  21. AND password='$password'");
  22. if(mysql_num_rows($result)!='0'){
  23. //echo 'passed logging in test<br>';
  24. $uniqueID = rand(0,1000000);
  25. $IP = $_SERVER['REMOTE_ADDR'];
  26. mysql_query("UPDATE user_info SET unique_ID = '$uniqueID', IP = '$IP'
  27. WHERE username = '$username'");
  28. setcookie("engl_username", $username, time()+3600);
  29. setcookie("engl_uniqueID", $uniqueID, time()+3600);
  30. if(mysql_num_rows($result)!='0'){
  31. $user_info = mysql_fetch_array(mysql_query("select permission from user_info where username='$username'"));
  32. mysql_close($mysql_link);
  33. return $user_info['permission'];
  34. }
  35. else
  36. return false;
  37. }
  38. /*
  39. while($row = mysql_fetch_array($result)){
  40. echo $row['username'];
  41. echo '<br>';
  42. echo $row['password'];
  43. echo '<br>';
  44. }
  45. */
  46. mysql_close($mysql_link);
  47. return false;
  48. }
  49. function checkUser(){
  50. //echo 'start check user <br>';
  51. // if(isset($_COOKIE['engl_username']) && isset($_COOKIE['engl_uniqueID'])){
  52. $link = connect_db();
  53. $username = $_COOKIE['engl_username'];
  54. $uniqueID = $_COOKIE['engl_uniqueID'];
  55. $IP = $_SERVER['REMOTE_ADDR'];
  56. $result=mysql_query("select * from user_info where username='$username'
  57. and unique_ID='$uniqueID' and IP = '$IP'");
  58. /*
  59. while($row = mysql_fetch_array($result)){
  60. echo $username . ' = ' . $row['username'];
  61. echo '<br>';
  62. echo $uniqueID . ' = ' . $row['unique_ID'];
  63. echo '<br>';
  64. echo $IP . ' = ' . $row['IP'];
  65. echo '<br>';
  66. }
  67. */
  68. if(mysql_num_rows($result)!='0'){
  69. $user_info = mysql_fetch_array(mysql_query("select permission from user_info where username='$username'"));
  70. return $user_info['permission'];
  71. }
  72. else
  73. return false;
  74. /*}
  75. else{
  76. echo 'here';
  77. return false;
  78. }*/
  79. }
  80. function checkStanding(){
  81. // if(isset($_COOKIE['engl_username']) && isset($_COOKIE['engl_uniqueID'])){
  82. $link = connect_db();
  83. $username = $_COOKIE['engl_username'];
  84. $uniqueID = $_COOKIE['engl_uniqueID'];
  85. $IP = $_SERVER['REMOTE_ADDR'];
  86. $result=mysql_query("select * from user_info where username='$username'
  87. and unique_ID='$uniqueID' and IP = '$IP'");
  88. if(mysql_num_rows($result)!='0'){
  89. $user_info = mysql_fetch_array(mysql_query("select standing from user_info where username='$username'"));
  90. return $user_info['standing'];
  91. }
  92. else
  93. return false;
  94. /*}
  95. else
  96. return false;*/
  97. }
  98. function checkIntranetPermit(){
  99. if(checkStanding() != false){
  100. return in_array(checkStanding(),$intranetList);
  101. }
  102. }
  103. function deleteCookie(){
  104. setcookie("engl_username",'',time() - 3600);
  105. setcookie("engl_uniqueID",'',time() - 3600);
  106. }
  107. function savedata($basepath, $facultypath, $data, $permission, $val){
  108. $fac_addr_list=array($facultypath . $_COOKIE['engl_username'] . '/',
  109. $facultypath . $_COOKIE['engl_username'] . '/sub_nav/');
  110. if($permission != 'Admin'){
  111. $tok = strtok($val, '/');
  112. if($permission == 'Graduate Advisor'){
  113. if($tok != 'graduate'){
  114. $error = 1; header('location:login.html'); exit;
  115. }
  116. }
  117. if($permission == 'Undergraduate Advisor'){
  118. if($tok != 'undergraduate'){
  119. $error = 1; header('location:login.html'); exit;
  120. }
  121. }
  122. if($permission == 'Instructor'){
  123. if($val != 'people/faculty/'.$_COOKIE['engl_username'].'/'){
  124. $error = 1; header('location:login.html'); exit;
  125. }
  126. }
  127. if($permission == false){
  128. $error = 1; header('location:login.html'); exit;
  129. }
  130. }
  131. $html=stripslashes($data);
  132. // save the content
  133. $val = $_GET['addr'];
  134. chdir($basepath);
  135. $addr = $val . "content.html";
  136. $fh = fopen($addr, 'w');
  137. fwrite($fh, $html);
  138. fclose($fh);
  139. }
  140. function change_pass($user_name, $new_pass, $confirm_pass){
  141. if($new_pass == $confirm_pass){
  142. $link = connect_db();
  143. $new_pass = md5($new_pass);
  144. $user_list[$user_name] = $new_pass;
  145. mysql_query("UPDATE user_info SET password = '$new_pass'
  146. WHERE username = '$username'");
  147. mysql_close($link);
  148. echo '<body bgcolor="#D8E2E2">';
  149. echo "password has been changed<br>";
  150. }
  151. else{ echo 'An error occured during the process'; }
  152. }
  153. function create_user($new_username, $new_password, $lastname, $firstname, $email, $permission, $standing, $phonenb, $office, $officehr, $title, $field){
  154. $index_page = '<?php
  155. include "../../../editor_conf.php";
  156. include $basepath."page_top2.php";
  157. ?>
  158. <div id="global_nav">
  159. <?php include $basepath."global_nav.php";?>
  160. </div>
  161. <div id="main_content">
  162. <?php echo file_get_contents("content.html");?>
  163. </div>
  164. <div id="sub_nav">
  165. <?php echo file_get_contents("sub_nav/content.html");?>
  166. </div>
  167. <div id="closing">
  168. <span></span>
  169. </div>
  170. </div>
  171. </body>
  172. </html>';
  173. $link = connect_db();
  174. $result=mysql_query("SELECT * FROM user_info WHERE username='$new_username'");
  175. if(mysql_num_rows($result)=='0' && $new_username != ''){
  176. $password = md5($new_password);
  177. mysql_query("INSERT INTO user_info (username, password, last_name, first_name, email, permission, standing, phonenb, office, officehr, title, field)
  178. VALUES ('$new_username', '$password', '$lastname', '$firstname', '$email', '$permission', '$standing', '$phonenb', '$office', '$officehr', '$title', '$field')");
  179. mysql_close($link);
  180. $user_dir = '../faculty/profiles/' . $new_username;
  181. $user_sub = $user_dir . '/sub_nav';
  182. $user_file = '../files/' . $new_username;
  183. if(!is_dir($user_dir)){
  184. mkdir($user_dir,0755);
  185. //mkdir($user_sub,0755);
  186. $fh = fopen($user_dir . '/index.php', 'w');
  187. fwrite($fh, $index_page);
  188. fclose($fh);
  189. echo "<br>created " . $new_username . '<br>';
  190. }
  191. }else{echo '<br> existing username ' . $new_username . '<br>';}
  192. }
  193. function deleteUser($username){
  194. $link = connect_db();
  195. $result=mysql_query("SELECT * FROM user_info WHERE username='$username'");
  196. if(mysql_num_rows($result)=='1' && $username != ''){
  197. mysql_query("DELETE FROM user_info WHERE username='$username'");
  198. mysql_close($link);
  199. $user_dir = '../faculty/profiles/' . $username;
  200. $user_sub = $user_dir . '/sub_nav';
  201. $userInfo = getUserInfo($username);
  202. $userEmail = $userInfo['email'];
  203. $tok = strtok($userEmail,'@');
  204. $user_file = '../files/' . $tok .'/';
  205. if(is_dir($user_dir)){
  206. $index_path = $user_dir . '/index.php';
  207. $content_path = $user_dir . '/content.html';
  208. unlink($index_path);
  209. unlink($content_path);
  210. rmdir($user_dir);
  211. //rmdir($user_sub);
  212. }
  213. if(is_dir($user_file)){
  214. rm_recursive($user_file);
  215. }
  216. }
  217. }
  218. function listusers(){
  219. $link = connect_db();
  220. $result = mysql_query("SELECT * FROM user_info");
  221. mysql_close($link);
  222. $list = null;
  223. while($row = mysql_fetch_array($result)){
  224. if($row['permission'] != 'Admin'){
  225. $list[$row['username']] = $row['username']; // this is not the right way, but for now this hack works.
  226. }
  227. }
  228. if($list != null){
  229. asort($list);
  230. return $list;
  231. }
  232. else
  233. return '0';
  234. }
  235. function listAdmin(){
  236. $link = connect_db();
  237. $result = mysql_query("SELECT * FROM user_info");
  238. mysql_close($link);
  239. $list = null;
  240. while($row = mysql_fetch_array($result)){
  241. if($row['permission'] == 'Admin'){
  242. $list[$row['username']] = $row['username']; // this is not the right way, but for now this hack works.
  243. }
  244. }
  245. if($list != null){
  246. return $list;
  247. }
  248. else
  249. return '0';
  250. }
  251. function returnData(){
  252. $link = connect_db();
  253. $result = mysql_query("SELECT * FROM historydata");
  254. mysql_close($link);
  255. return $result;
  256. }
  257. function getUserData($username){
  258. $link = connect_db();
  259. $result = mysql_query("SELECT * FROM user_info WHERE username='$username'");
  260. $row = mysql_fetch_array($result);
  261. mysql_close($link);
  262. return $row;
  263. }
  264. function changeUserData($username, $new_password, $lastname, $firstname, $email, $permission, $standing, $phonenb, $office, $officehr,$field,$title,$interest){
  265. $link = connect_db();
  266. if($new_password == 'donotchange'){
  267. mysql_query("UPDATE user_info SET
  268. last_name='$lastname', first_name='$firstname', email='$email',
  269. permission='$permission', standing='$standing', phonenb='$phonenb',
  270. office='$office', officehr='$officehr', field='$field', title='$title', interest='$interest' WHERE username='$username'");
  271. }
  272. else{
  273. //echo $new_password .'<br>';
  274. //echo md5($new_password);
  275. $new_password = md5($new_password);
  276. mysql_query("UPDATE user_info SET password = '$new_password',
  277. last_name='$lastname', first_name='$firstname', email='$email',
  278. permission='$permission', standing='$standing', phonenb='$phonenb',
  279. office='$office', officehr='$officehr', field='$field', title='$title', interest='$interest' WHERE username='$username'");
  280. }
  281. mysql_close($link);
  282. }
  283. function pullDownMenu($items, $data){
  284. $tag = '';
  285. foreach($items as $item){
  286. if($data==$item)
  287. $tag .= '<option value="'.$item.'" selected="yes">'.$item.'</option>';
  288. else
  289. $tag .= '<option value="'.$item.'">'.$item.'</option>';
  290. }
  291. return $tag;
  292. }
  293. function checkBox($items, $str){
  294. $datas = stringToArray($str, ',');
  295. $tag = '';
  296. if(empty($datas)){
  297. $datas = array('empty');
  298. }
  299. $list;
  300. foreach($items as $item){
  301. foreach($datas as $data){
  302. if($item==$data){
  303. $list[$item]=1;
  304. }
  305. elseif(!isset($list[$item])){
  306. $list[$item]=0;
  307. }
  308. }
  309. }
  310. foreach($list as $item => $value){
  311. if($value == 1){
  312. $tag .= '<input type="checkbox" name="'.$item.'" CHECKED >'.$item.'</input><br>';
  313. }
  314. else{
  315. $tag .= '<input type="checkbox" name="'.$item.'">'.$item.'</input><br>';
  316. }
  317. }
  318. return $tag;
  319. }
  320. function stringToArray($str,$sp){
  321. $tok = strtok($str, $sp);
  322. $arr = Array();
  323. while ($tok !== false) {
  324. array_push($arr,$tok);
  325. $tok = strtok($sp);
  326. }
  327. return $arr;
  328. }
  329. function CBItemsToString($CBItems, $post){
  330. $str = '';
  331. foreach($CBItems as $item){
  332. if(isset($post[$item])){
  333. $str .= $item . ',';
  334. }
  335. }
  336. if($str != ''){
  337. $str = substr($str,0,-1);
  338. }
  339. return $str;
  340. }
  341. function updateFacultyPath($filepath,$user){
  342. if(isset($_COOKIE['engl_username'])){
  343. $userInfo = getUserInfo($_COOKIE['engl_username']);
  344. $userEmail = $userInfo['email'];
  345. $tok = strtok($userEmail,'@');
  346. switch($user){
  347. case 'Admin':
  348. break;
  349. case 'Graduate Advisor':
  350. break;
  351. case 'Undergraduate Advisor':
  352. break;
  353. case 'Homepage':
  354. break;
  355. case 'News':
  356. break;
  357. case 'Instructor':
  358. $filepath .= $tok .'/';
  359. break;
  360. default:
  361. return false;
  362. }
  363. return $filepath;
  364. }
  365. else
  366. return false;
  367. }
  368. function getUserInfo($username){
  369. $link = connect_db();
  370. $result=mysql_query("select * from user_info where username='$username'");
  371. $user_info = mysql_fetch_array(mysql_query("select * from user_info where username='$username'"));
  372. mysql_close($link);
  373. return $user_info;
  374. }
  375. function rm_recursive($filepath)
  376. {
  377. if (is_dir($filepath) && !is_link($filepath))
  378. {
  379. if ($dh = opendir($filepath))
  380. {
  381. while (($sf = readdir($dh)) !== false)
  382. {
  383. if ($sf == '.' || $sf == '..')
  384. {
  385. continue;
  386. }
  387. if (!rm_recursive($filepath.'/'.$sf))
  388. {
  389. throw new Exception($filepath.'/'.$sf.' could not be deleted.');
  390. }
  391. }
  392. closedir($dh);
  393. }
  394. return rmdir($filepath);
  395. }
  396. return unlink($filepath);
  397. }