storageapi.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. // Storage API
  3. // PHP Backend
  4. // CBlue SPRL, Jean-Karim Bockstael, <jeankarim@cblue.be>
  5. require_once '../inc/global.inc.php';
  6. // variable cleaning...
  7. foreach (["svkey", "svvalue"] as $key) {
  8. $_REQUEST[$key] = Database::escape_string($_REQUEST[$key]);
  9. }
  10. foreach (["svuser", "svcourse", "svsco", "svlength", "svasc"] as $key) {
  11. $_REQUEST[$key] = intval($_REQUEST[$key]);
  12. }
  13. switch ($_REQUEST['action']) {
  14. case "get":
  15. print storage_get($_REQUEST['svuser'], $_REQUEST['svcourse'], $_REQUEST['svsco'], $_REQUEST['svkey']);
  16. break;
  17. case "set":
  18. if (storage_can_set($_REQUEST['svuser'])) {
  19. echo storage_set($_REQUEST['svuser'], $_REQUEST['svcourse'], $_REQUEST['svsco'], $_REQUEST['svkey'], $_REQUEST['svvalue']);
  20. }
  21. break;
  22. case "getall":
  23. print storage_getall($_REQUEST['svuser'], $_REQUEST['svcourse'], $_REQUEST['svsco']);
  24. break;
  25. case "stackpush":
  26. if (storage_can_set($_REQUEST['svuser'])) {
  27. echo storage_stack_push($_REQUEST['svuser'], $_REQUEST['svcourse'], $_REQUEST['svsco'], $_REQUEST['svkey'], $_REQUEST['svvalue']);
  28. }
  29. break;
  30. case "stackpop":
  31. if (storage_can_set($_REQUEST['svuser'])) {
  32. echo storage_stack_pop($_REQUEST['svuser'], $_REQUEST['svcourse'], $_REQUEST['svsco'], $_REQUEST['svkey']);
  33. }
  34. break;
  35. case "stacklength":
  36. print storage_stack_length($_REQUEST['svuser'], $_REQUEST['svcourse'], $_REQUEST['svsco'], $_REQUEST['svkey']);
  37. break;
  38. case "stackclear":
  39. if (storage_can_set($_REQUEST['svuser'])) {
  40. echo storage_stack_clear($_REQUEST['svuser'], $_REQUEST['svcourse'], $_REQUEST['svsco'], $_REQUEST['svkey']);
  41. }
  42. break;
  43. case "stackgetall":
  44. if (storage_can_set($_REQUEST['svuser'])) {
  45. echo storage_stack_getall($_REQUEST['svuser'], $_REQUEST['svcourse'], $_REQUEST['svsco'], $_REQUEST['svkey']);
  46. }
  47. break;
  48. case "getposition":
  49. print storage_get_position($_REQUEST['svuser'], $_REQUEST['svcourse'], $_REQUEST['svsco'], $_REQUEST['svkey'], $_REQUEST['svasc']);
  50. break;
  51. case "getleaders":
  52. print storage_get_leaders($_REQUEST['svuser'], $_REQUEST['svcourse'], $_REQUEST['svsco'], $_REQUEST['svkey'], $_REQUEST['svasc'], $_REQUEST['svlength']);
  53. break;
  54. case "usersgetall":
  55. // security issue
  56. print "NOT allowed, security issue, see sources";
  57. // print storage_get_all_users();
  58. break;
  59. default:
  60. // Do nothing
  61. }
  62. function storage_can_set($sv_user)
  63. {
  64. // platform admin can change any user's stored values, other users can only change their own values
  65. $allowed = ((api_is_platform_admin()) || ($sv_user == api_get_user_id()));
  66. if (!$allowed) {
  67. echo "ERROR : Not allowed";
  68. }
  69. return $allowed;
  70. }
  71. function storage_get($sv_user, $sv_course, $sv_sco, $sv_key)
  72. {
  73. $sql = "select sv_value
  74. from ".Database::get_main_table(TABLE_TRACK_STORED_VALUES)."
  75. where user_id= '$sv_user'
  76. and sco_id = '$sv_sco'
  77. and course_id = '$sv_course'
  78. and sv_key = '$sv_key'";
  79. $res = Database::query($sql);
  80. if (Database::num_rows($res) > 0) {
  81. $row = Database::fetch_assoc($res);
  82. if (get_magic_quotes_gpc()) {
  83. return stripslashes($row['sv_value']);
  84. } else {
  85. return $row['sv_value'];
  86. }
  87. } else {
  88. return null;
  89. }
  90. }
  91. function storage_get_leaders($sv_user, $sv_course, $sv_sco, $sv_key, $sv_asc, $sv_length)
  92. {
  93. // get leaders
  94. $sql_leaders = "select u.user_id, firstname, lastname, email, username, sv_value as value
  95. from ".Database::get_main_table(TABLE_TRACK_STORED_VALUES)." sv,
  96. ".Database::get_main_table(TABLE_MAIN_USER)." u
  97. where u.user_id=sv.user_id
  98. and sco_id = '$sv_sco'
  99. and course_id = '$sv_course'
  100. and sv_key = '$sv_key'
  101. order by sv_value ".($sv_asc ? "ASC" : "DESC")." limit $sv_length";
  102. // $sql_data = "select sv.user_id as user_id, sv_key as variable, sv_value as value
  103. // from ".Database::get_main_table(TABLE_TRACK_STORED_VALUES)." sv
  104. // where sv.user_id in (select u2.user_id from ($sql_leaders) u2)
  105. // and sco_id = '$sv_sco'
  106. // and course_id = '$sv_course'";
  107. // $resData = Database::query($sql_data);
  108. // $data = Array();
  109. // while($row = Database::fetch_assoc($resData))
  110. // $data[] = $row; // fetching all data
  111. //
  112. $resLeaders = Database::query($sql_leaders);
  113. $result = [];
  114. while ($row = Database::fetch_assoc($resLeaders)) {
  115. $row["values"] = [];
  116. // foreach($data as $dataRow) {
  117. // if ($dataRow["user_id"] = $row["user_id"])
  118. // $row["values"][$dataRow["variable"]] = $dataRow["value"];
  119. // }
  120. $result[] = $row;
  121. }
  122. return json_encode($result);
  123. }
  124. function storage_get_position($sv_user, $sv_course, $sv_sco, $sv_key, $sv_asc, $sv_length)
  125. {
  126. $sql = "select count(list.user_id) as position
  127. from ".Database::get_main_table(TABLE_TRACK_STORED_VALUES)." search,
  128. ".Database::get_main_table(TABLE_TRACK_STORED_VALUES)." list
  129. where search.user_id= '$sv_user'
  130. and search.sco_id = '$sv_sco'
  131. and search.course_id = '$sv_course'
  132. and search.sv_key = '$sv_key'
  133. and list.sv_value ".($sv_asc ? "<=" : ">=")." search.sv_value
  134. and list.sco_id = search.sco_id
  135. and list.course_id = search.course_id
  136. and list.sv_key = search.sv_key
  137. order by list.sv_value";
  138. $res = Database::query($sql);
  139. if (Database::num_rows($res) > 0) {
  140. $row = Database::fetch_assoc($res);
  141. return $row['position'];
  142. } else {
  143. return null;
  144. }
  145. }
  146. function storage_set($sv_user, $sv_course, $sv_sco, $sv_key, $sv_value)
  147. {
  148. $sv_value = Database::escape_string($sv_value);
  149. $sql = "replace into ".Database::get_main_table(TABLE_TRACK_STORED_VALUES)."
  150. (user_id, sco_id, course_id, sv_key, sv_value)
  151. values
  152. ('$sv_user','$sv_sco','$sv_course','$sv_key','$sv_value')";
  153. $res = Database::query($sql);
  154. return Database::affected_rows($res);
  155. }
  156. function storage_getall($sv_user, $sv_course, $sv_sco)
  157. {
  158. $sql = "select sv_key, sv_value
  159. from ".Database::get_main_table(TABLE_TRACK_STORED_VALUES)."
  160. where user_id= '$sv_user'
  161. and sco_id = '$sv_sco'
  162. and course_id = '$sv_course'";
  163. $res = Database::query($sql);
  164. $data = [];
  165. while ($row = Database::fetch_assoc($res)) {
  166. if (get_magic_quotes_gpc()) {
  167. $row['sv_value'] = stripslashes($row['sv_value']);
  168. }
  169. $data[] = $row;
  170. }
  171. return json_encode($data);
  172. }
  173. function storage_stack_push($sv_user, $sv_course, $sv_sco, $sv_key, $sv_value)
  174. {
  175. $sv_value = Database::escape_string($sv_value);
  176. Database::query("start transaction");
  177. $sqlorder = "select ifnull((select max(stack_order)
  178. from ".Database::get_main_table(TABLE_TRACK_STORED_VALUES_STACK)."
  179. where user_id= '$sv_user'
  180. and sco_id='$sv_sco'
  181. and course_id='$sv_course'
  182. and sv_key='$sv_key'
  183. ), 0) as stack_order";
  184. $resorder = Database::query($sqlorder);
  185. $row = Database::fetch_assoc($resorder);
  186. $stack_order = (1 + $row['stack_order']);
  187. $sqlinsert = "insert into ".Database::get_main_table(TABLE_TRACK_STORED_VALUES_STACK)."
  188. (user_id, sco_id, course_id, sv_key, stack_order, sv_value)
  189. values
  190. ('$sv_user', '$sv_sco', '$sv_course', '$sv_key', '$stack_order', '$sv_value')";
  191. $resinsert = Database::query($sqlinsert);
  192. if ($resorder && $resinsert) {
  193. Database::query("commit");
  194. return 1;
  195. } else {
  196. Database::query("rollback");
  197. return 0;
  198. }
  199. }
  200. function storage_stack_pop($sv_user, $sv_course, $sv_sco, $sv_key)
  201. {
  202. Database::query("start transaction");
  203. $sqlselect = "select sv_value, stack_order
  204. from ".Database::get_main_table(TABLE_TRACK_STORED_VALUES_STACK)."
  205. where user_id= '$sv_user'
  206. and sco_id='$sv_sco'
  207. and course_id='$sv_course'
  208. and sv_key='$sv_key'
  209. order by stack_order desc
  210. limit 1";
  211. $resselect = Database::query($sqlselect);
  212. $rowselect = Database::fetch_assoc($resselect);
  213. $stack_order = $rowselect['stack_order'];
  214. $sqldelete = "delete
  215. from ".Database::get_main_table(TABLE_TRACK_STORED_VALUES_STACK)."
  216. where user_id= '$sv_user'
  217. and sco_id='$sv_sco'
  218. and course_id='$sv_course'
  219. and sv_key='$sv_key'
  220. and stack_order='$stack_order'";
  221. $resdelete = Database::query($sqldelete);
  222. if ($resselect && $resdelete) {
  223. Database::query("commit");
  224. if (get_magic_quotes_gpc()) {
  225. return stripslashes($rowselect['sv_value']);
  226. } else {
  227. return $rowselect['sv_value'];
  228. }
  229. } else {
  230. Database::query("rollback");
  231. return null;
  232. }
  233. }
  234. function storage_stack_length($sv_user, $sv_course, $sv_sco, $sv_key)
  235. {
  236. $sql = "select count(*) as length
  237. from ".Database::get_main_table(TABLE_TRACK_STORED_VALUES_STACK)."
  238. where user_id= '$sv_user'
  239. and sco_id='$sv_sco'
  240. and course_id='$sv_course'
  241. and sv_key='$sv_key'";
  242. $res = Database::query($sql);
  243. $row = Database::fetch_assoc($res);
  244. return $row['length'];
  245. }
  246. function storage_stack_clear($sv_user, $sv_course, $sv_sco, $sv_key)
  247. {
  248. $sql = "delete
  249. from ".Database::get_main_table(TABLE_TRACK_STORED_VALUES_STACK)."
  250. where user_id= '$sv_user'
  251. and sco_id='$sv_sco'
  252. and course_id='$sv_course'
  253. and sv_key='$sv_key'";
  254. $res = Database::query($sql);
  255. return Database::num_rows($res);
  256. }
  257. function storage_stack_getall($sv_user, $sv_course, $sv_sco, $sv_key)
  258. {
  259. $sql = "select stack_order as stack_order, sv_value as value
  260. from ".Database::get_main_table(TABLE_TRACK_STORED_VALUES_STACK)."
  261. where user_id= '$sv_user'
  262. and sco_id='$sv_sco'
  263. and course_id='$sv_course'
  264. and sv_key='$sv_key'";
  265. $res = Database::query($sql);
  266. $results = [];
  267. while ($row = Database::fetch_assoc($res)) {
  268. if (get_magic_quotes_gpc()) {
  269. $row['value'] = stripslashes($row['value']);
  270. }
  271. $results[] = $row;
  272. }
  273. return json_encode($results);
  274. }
  275. function storage_get_all_users()
  276. {
  277. $sql = "select user_id, username, firstname, lastname
  278. from ".Database::get_main_table(TABLE_MAIN_USER)."
  279. order by user_id asc";
  280. $res = Database::query($sql);
  281. $results = [];
  282. while ($row = Database::fetch_assoc($res)) {
  283. $results[] = $row;
  284. }
  285. return json_encode($results);
  286. }