storageapi.php 9.4 KB

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