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

/php/projusers.php

https://gitlab.com/JoshJ/AGORA
PHP | 231 lines | 175 code | 15 blank | 41 comment | 20 complexity | 176d49326ba903f7cfd8f819e91b860a MD5 | raw file
  1. <?php
  2. /**
  3. AGORA - an interactive and web-based argument mapping tool that stimulates reasoning,
  4. reflection, critique, deliberation, and creativity in individual argument construction
  5. and in collaborative or adversarial settings.
  6. Copyright (C) 2011 Georgia Institute of Technology
  7. This program is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU Affero General Public License as
  9. published by the Free Software Foundation, either version 3 of the
  10. License, or (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU Affero General Public License for more details.
  15. You should have received a copy of the GNU Affero General Public License
  16. along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. List of variables for project creation:
  20. uid: ID number for the user
  21. pass_hash: The hash of the user's password
  22. projID: The ID of the project.
  23. userID: The ID of the user to be added or removed.
  24. action: "add" or "remove". Pretty self-explanatory, really.
  25. THIS IS NOT THE CODE FOR A USER JOINING THE PROJECT WITH THE PASSWORD!
  26. **/
  27. require 'configure.php';
  28. require 'errorcodes.php';
  29. require 'establish_link.php';
  30. require 'utilfuncs.php';
  31. /**
  32. * Convenience function.
  33. * Selects the last auto-generated ID (AUTO_INCREMENT) from the Database.
  34. * See the following for the function that this uses:
  35. * http://php.net/manual/en/function.mysql-insert-id.php
  36. */
  37. function getLastInsert($linkID)
  38. {
  39. $query = "SELECT LAST_INSERT_ID()";
  40. $resultID = mysql_query($query, $linkID);
  41. $row = mysql_fetch_assoc($resultID);
  42. return $row['LAST_INSERT_ID()'];
  43. }
  44. function addUser($otheruserID, $projID, $userID, $level, $pass_hash, $output){
  45. $output->addAttribute("ID", $projID);
  46. global $dbName, $version;
  47. header("Content-type: text/xml");
  48. $xmlstr = "<?xml version='1.0'?>\n<project version='$version'></project>";
  49. $output = new SimpleXMLElement($xmlstr);
  50. $linkID= establishLink();
  51. if(!$linkID){
  52. badDBLink($output);
  53. return $output;
  54. }
  55. $status=mysql_select_db($dbName, $linkID);
  56. if(!$status){
  57. databaseNotFound($output);
  58. return $output;
  59. }
  60. if(!checkLogin($userID, $pass_hash, $linkID)){
  61. print $userID;
  62. print $pass_hash;
  63. print $dbName;
  64. incorrectLogin($output);
  65. return $output;
  66. }
  67. //Basic boilerplate is done.
  68. if(!checkForAdmin($projID, $userID, $linkID)){
  69. notProjectAdmin($output);
  70. return $output;
  71. }
  72. $query = "INSERT INTO projusers (proj_id, user_id, user_level) VALUES ($projID, $otheruserID, $level)";
  73. $success = mysql_query($query, $linkID);
  74. if($success){
  75. $otheruser=$output->addChild("user");
  76. $otheruser->addAttribute("ID", $otheruserID);
  77. $otheruser->addAttribute("added", true);
  78. return $output;
  79. }else{
  80. $otheruser=$output->addChild("user");
  81. $otheruser->addAttribute("ID", $otheruserID);
  82. $otheruser->addAttribute("added", false);
  83. updateFailed($output, $query);
  84. //Note that the UNIQUE pu_combo field ensures that a user can't be added to a project twice.
  85. return $output;
  86. }
  87. return $output;
  88. }
  89. function removeUser($otheruserID, $projID, $userID, $pass_hash, $output){
  90. $output->addAttribute("ID", $projID);
  91. global $dbName, $version;
  92. header("Content-type: text/xml");
  93. $xmlstr = "<?xml version='1.0'?>\n<project version='$version'></project>";
  94. $output = new SimpleXMLElement($xmlstr);
  95. $linkID= establishLink();
  96. if(!$linkID){
  97. badDBLink($output);
  98. return $output;
  99. }
  100. $status=mysql_select_db($dbName, $linkID);
  101. if(!$status){
  102. databaseNotFound($output);
  103. return $output;
  104. }
  105. if(!checkLogin($userID, $pass_hash, $linkID)){
  106. incorrectLogin($output);
  107. return $output;
  108. }
  109. //Basic boilerplate is done.
  110. if(!checkForAdmin($projID, $userID, $linkID)){
  111. notProjectAdmin($output);
  112. return $output;
  113. }
  114. mysql_query("START TRANSACTION");
  115. $query = "DELETE FROM projusers WHERE proj_id=$projID AND user_id=$otheruserID";
  116. $success = mysql_query($query, $linkID);
  117. if($success){
  118. //Remove all of this user's maps from the project and make a new project for them.
  119. $query = "INSERT INTO projects (user_id, title, password, is_hostile) VALUES
  120. ($otheruserID, 'Automatically created project', NULL, 1)";
  121. $status = mysql_query($query, $linkID);
  122. $newID = getLastInsert($linkID);
  123. if(!$status){
  124. mysql_query("ROLLBACK");
  125. rolledBack($output);
  126. return $output;
  127. }
  128. $uquery = "UPDATE maps SET proj_id=$newID WHERE user_id=$otheruserID AND proj_id=$projID";
  129. $status = mysql_query($uquery, $linkID);
  130. if(!$status){
  131. mysql_query("ROLLBACK");
  132. rolledBack($output);
  133. return $output;
  134. }
  135. $otheruser=$output->addChild("user");
  136. $otheruser->addAttribute("ID", $otheruserID);
  137. $otheruser->addAttribute("removed", true);
  138. mysql_query("COMMIT");
  139. return $output;
  140. }else{
  141. $otheruser=$output->addChild("user");
  142. $otheruser->addAttribute("ID", $otheruserID);
  143. $otheruser->addAttribute("removed", false);
  144. updateFailed($output, $query);
  145. mysql_query("ROLLBACK");
  146. rolledBack($output);
  147. return $output;
  148. }
  149. return $output;
  150. }
  151. function modifyUser($otheruserID, $projID, $userID, $level, $pass_hash, $output){
  152. $output->addAttribute("ID", $projID);
  153. global $dbName, $version;
  154. header("Content-type: text/xml");
  155. $xmlstr = "<?xml version='1.0'?>\n<project version='$version'></project>";
  156. $output = new SimpleXMLElement($xmlstr);
  157. $linkID= establishLink();
  158. if(!$linkID){
  159. badDBLink($output);
  160. return $output;
  161. }
  162. $status=mysql_select_db($dbName, $linkID);
  163. if(!$status){
  164. databaseNotFound($output);
  165. return $output;
  166. }
  167. if(!checkLogin($userID, $pass_hash, $linkID)){
  168. incorrectLogin($output);
  169. return $output;
  170. }
  171. //Basic boilerplate is done.
  172. if(!checkForAdmin($projID, $userID, $linkID)){
  173. notProjectAdmin($output);
  174. return $output;
  175. }
  176. $query = "UPDATE projusers SET user_level=$level WHERE proj_id=$projID AND user_id=$otheruserID";
  177. $success = mysql_query($query, $linkID);
  178. if($success){
  179. $otheruser=$output->addChild("user");
  180. $otheruser->addAttribute("ID", $otheruserID);
  181. $otheruser->addAttribute("modified", true);
  182. return $output;
  183. }else{
  184. $otheruser=$output->addChild("user");
  185. $otheruser->addAttribute("ID", $otheruserID);
  186. $otheruser->addAttribute("modified", false);
  187. updateFailed($output, $query);
  188. return $output;
  189. }
  190. return $output;
  191. }
  192. $userID = mysql_real_escape_string($_REQUEST['uid']);
  193. $pass_hash = mysql_real_escape_string($_REQUEST['pass_hash']);
  194. $projID = mysql_real_escape_string($_REQUEST['projID']);
  195. $otheruserID = mysql_real_escape_string($_REQUEST['otheruserID']);
  196. $level = mysql_real_escape_string($_REQUEST['level']);
  197. $action = $_REQUEST['action'];
  198. header("Content-type: text/xml");
  199. $xmlstr = "<?xml version='1.0'?>\n<project version='$version'></project>";
  200. $output = new SimpleXMLElement($xmlstr);
  201. if($action=="add"){
  202. $output=addUser($otheruserID, $projID, $userID, $level, $pass_hash, $output);
  203. }else if($action=="remove"){
  204. $output=removeUser($otheruserID, $projID, $userID, $pass_hash, $output);
  205. }else if($action=="modify"){
  206. $output=modifyUser($otheruserID, $projID, $userID, $level, $pass_hash, $output);
  207. }else{
  208. meaninglessQueryVariables($output, "The 'action' variable must be set to either add or remove.");
  209. }
  210. print $output->asXML();
  211. ?>