PageRenderTime 68ms CodeModel.GetById 37ms RepoModel.GetById 1ms app.codeStats 0ms

/tng_add_edit_user_code.php

https://github.com/neskie/Stewardship-Portal
PHP | 260 lines | 178 code | 26 blank | 56 comment | 21 complexity | 9fdccc8f14099450a61c1989648b4898 MD5 | raw file
  1. <?php
  2. /*---------------------------------------------------------------
  3. author: alim karim
  4. date: May 14, 2007
  5. file: tng_add_edit_user.php
  6. desc: webpage to add users or reset passwords for
  7. existing users
  8. ---------------------------------------------------------------*/
  9. include_once('classes/class_login.php');
  10. include_once('classes/class_dbconn.php');
  11. session_start();
  12. //unset($_SESSION['user_list']);
  13. //return;
  14. // form is being loaded first time or
  15. // it is being loaded through ajax
  16. if(isset($_SESSION['obj_login'])){
  17. global $user_list;
  18. global $obj_list;
  19. global $xslt_user;
  20. if(isset($_POST['ajax_action'])){
  21. switch($_POST['ajax_action']){
  22. // query for all users in
  23. // the db
  24. case "get_users":
  25. get_user_list($user_list);
  26. $xml = generate_object_list_xml($user_list, "");
  27. echo $xml;
  28. break;
  29. // the caller wishes get details
  30. // about a user
  31. case "get_user_details":
  32. $uid = $_POST['ajax_uid'];
  33. $xml = get_user_details($uid);
  34. echo $xml;
  35. break;
  36. // the caller wishes to update
  37. // attributes associated with
  38. // a user
  39. case "update_user":
  40. $uid = $_POST['ajax_uid'];
  41. $fname = $_POST['ajax_fname'];
  42. $lname = $_POST['ajax_lname'];
  43. $email = $_POST['ajax_email'];
  44. $active = $_POST['ajax_active'];
  45. $new_passwd = "";
  46. if(isset($_POST['ajax_newpasswd']))
  47. $new_passwd = $_POST['ajax_newpasswd'];
  48. update_user($uid, $new_passwd, $fname, $lname, $email, $active);
  49. break;
  50. // the caller wishes to add a new
  51. // user to the db
  52. case "add_user":
  53. $uname = $_POST['ajax_uname'];
  54. $passwd = $_POST['ajax_passwd'];
  55. $fname = $_POST['ajax_fname'];
  56. $lname = $_POST['ajax_lname'];
  57. $email = $_POST['ajax_email'];
  58. $active = $_POST['ajax_active'];
  59. add_user($uname, $passwd, $fname, $lname, $email, $active);
  60. // regenerate the user list
  61. // and send back the new list
  62. // as xml
  63. get_user_list($user_list);
  64. $xml = generate_object_list_xml($user_list, "");
  65. echo $xml;
  66. break;
  67. }
  68. }
  69. }
  70. ///
  71. /// get_user_list()
  72. /// get the user list from the
  73. /// database
  74. ///
  75. function get_user_list(&$user_list){
  76. $user_list = array();
  77. $sql_str = "SELECT "
  78. . "uid, "
  79. . "uname "
  80. . "FROM "
  81. . "tng_user ";
  82. $dbconn =& new DBConn();
  83. $dbconn->connect();
  84. $result = pg_query($dbconn->conn, $sql_str);
  85. if(!$result){
  86. echo "An error occurred while executing the query " . pg_last_error($dbconn->conn);
  87. $dbconn->disconnect();
  88. return NULL;
  89. }
  90. $n_users = pg_num_rows($result);
  91. // populate user_list array
  92. // as name-value pairs
  93. // i.e. the user name is the name, and the
  94. // user id is the value
  95. for($i = 0; $i < $n_users; $i++)
  96. $user_list[pg_fetch_result($result, $i, 1)] = pg_fetch_result($result, $i, 0);
  97. $dbconn->disconnect();
  98. }
  99. ///
  100. /// generate_object_list_xml()
  101. /// produce xml representing a list
  102. /// of objects(users, forms, layers, etc)
  103. /// limited by $prefix (if any)
  104. /// provided by the user.
  105. /// note that $obj_list is a name-value pair
  106. /// array, where the name of the object is the key
  107. /// and the id of the object is the value
  108. ///
  109. function generate_object_list_xml($obj_list, $prefix){
  110. $xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
  111. . "<objects>";
  112. $n_objects = count($obj_list);
  113. $obj_names = array_keys($obj_list);
  114. for($i = 0; $i < $n_objects; $i++){
  115. if($prefix == ""
  116. || substr($obj_names[$i], 0, strlen($prefix)) == $prefix
  117. ){
  118. $xml .= "<object>"
  119. . "<id>"
  120. . $obj_list[$obj_names[$i]]
  121. . "</id>"
  122. . "<name>"
  123. . $obj_names[$i]
  124. . "</name>"
  125. . "</object>\n";
  126. }
  127. }
  128. $xml .= "</objects>";
  129. return $xml;
  130. }
  131. ///
  132. /// get_user_details()
  133. /// get user attributes
  134. ///
  135. function get_user_details($uid){
  136. $sql_str = "SELECT "
  137. . "uname, "
  138. . "fname, "
  139. . "lname, "
  140. . "email, "
  141. . "active "
  142. . "FROM "
  143. . "tng_user "
  144. . "WHERE "
  145. . "uid = " . $uid;
  146. $dbconn =& new DBConn();
  147. $dbconn->connect();
  148. $result = pg_query($dbconn->conn, $sql_str);
  149. if(!$result){
  150. echo "An error occurred while executing the query "
  151. . pg_last_error($dbconn->conn) . "\n"
  152. . $sql_str;
  153. $dbconn->disconnect();
  154. return NULL;
  155. }
  156. $xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
  157. . "<user>"
  158. . "<uname>" . pg_fetch_result($result, 0, 'uname') . "</uname>"
  159. . "<fname>" . pg_fetch_result($result, 0, 'fname') . "</fname>"
  160. . "<lname>" . pg_fetch_result($result, 0, 'lname') . "</lname>"
  161. . "<email>" . pg_fetch_result($result, 0, 'email') . "</email>";
  162. if(pg_fetch_result($result, 0, 'active') == "t")
  163. $xml .= "<active>true</active>";
  164. else
  165. $xml .= "<active>false</active>";
  166. $xml .= "</user>";
  167. $dbconn->disconnect();
  168. return $xml;
  169. }
  170. ///
  171. /// update_user()
  172. /// update user attributes
  173. ///
  174. function update_user($uid, $new_passwd = "", $fname, $lname, $email, $active){
  175. $sql_str = "UPDATE "
  176. . "tng_user "
  177. . "SET "
  178. . "fname = '" . $fname . "', "
  179. . "lname = '" . $lname . "', "
  180. . "email = '" . $email . "', "
  181. . "active = '" . $active . "' ";
  182. if($new_passwd != ""){
  183. $md5_pass = md5($new_passwd);
  184. $sql_str .= ", passwd = '" . $md5_pass . "' ";
  185. }
  186. $sql_str .= "WHERE "
  187. . "uid = " . $uid;
  188. $dbconn =& new DBConn();
  189. $dbconn->connect();
  190. $result = pg_query($dbconn->conn, $sql_str);
  191. if(!$result){
  192. echo "An error occurred while executing the query " . pg_last_error($dbconn->conn);
  193. $dbconn->disconnect();
  194. return NULL;
  195. }
  196. $dbconn->disconnect();
  197. }
  198. ///
  199. /// add_user()
  200. /// create a user record in the
  201. /// tng_user table
  202. ///
  203. function add_user($uname, $passwd, $fname, $lname, $email, $active){
  204. $md5_passwd = md5($passwd);
  205. $sql_str = "INSERT INTO tng_user "
  206. . "("
  207. . "uname, "
  208. . "passwd, "
  209. . "fname, "
  210. . "lname, "
  211. . "email, "
  212. . "active "
  213. . ") "
  214. . "VALUES "
  215. . "("
  216. . "'" . $uname . "', "
  217. . "'" . $md5_passwd . "', "
  218. . "'" . $fname . "', "
  219. . "'" . $lname . "', "
  220. . "'" . $email . "', "
  221. . "'" . $active . "' "
  222. . ")";
  223. $dbconn =& new DBConn();
  224. $dbconn->connect();
  225. $result = pg_query($dbconn->conn, $sql_str);
  226. if(!$result){
  227. echo "An error occurred while executing the query " . pg_last_error($dbconn->conn);
  228. $dbconn->disconnect();
  229. return NULL;
  230. }
  231. $dbconn->disconnect();
  232. }
  233. ?>