comment.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. // Gasteizko Margolariak API v1 //
  3. //Posible comment target
  4. define('TARGET_PHOTO', 'photo');
  5. define('TARGET_POST', 'post');
  6. define('TARGET_ACTIVITY', 'activity');
  7. //Default target
  8. define('DEF_TARGET', TARGET_ALL);
  9. //$_GET valid parameters
  10. define('GET_CLIENT', 'client');
  11. define('GET_USER', 'user');
  12. define('GET_TARGET', 'target');
  13. define('GET_ID', 'id');
  14. define('GET_PERMALINK', 'permalink');
  15. define('GET_TEXT', 'text');
  16. define('GET_USERNAME', 'username');
  17. /****************************************************
  18. * This function is called from almost everywhere at *
  19. * the beggining of the page. It initializes the *
  20. * session variables, connect to the db, enabling *
  21. * the variable $con for futher use everywhere in *
  22. * the php code, and populates the arrays $user *
  23. * and $permission, with info about the user. *
  24. * *
  25. * @return: (db connection): The connection handler. *
  26. ****************************************************/
  27. function startdb(){
  28. //Include the db configuration file. It's somehow like this
  29. /*
  30. <?php
  31. $host = 'XXXX';
  32. $db_name = 'XXXX';
  33. $username_ro = 'XXXX';
  34. $username_rw = 'XXXX';
  35. $pass_ro = 'XXXX';
  36. $pass_rw = 'XXXX';
  37. ?>
  38. */
  39. include('../../.htpasswd');
  40. //Connect to to database
  41. $con = mysqli_connect($host, $username_rw, $pass_rw, $db_name);
  42. //Set encoding options
  43. mysqli_set_charset($con, 'utf-8');
  44. header('Content-Type: text/html; charset=utf8');
  45. mysqli_query($con, 'SET NAMES utf8;');
  46. //Return the db connection
  47. return $con;
  48. }
  49. //Connect to the database
  50. $con = startdb('rw');
  51. //Get data from URL
  52. $client = mysqli_real_escape_string($con, $_GET[GET_CLIENT]);
  53. $user = mysqli_real_escape_string($con, $_GET[GET_USER]);
  54. $target = strtolower(mysqli_real_escape_string($con, $_GET[GET_TARGET]));
  55. $id = strtolower(mysqli_real_escape_string($con, $_GET[GET_ID]));
  56. $permalink = strtolower(mysqli_real_escape_string($con, $_GET[GET_PERMALINK]));
  57. $ink = strtolower(mysqli_real_escape_string($con, $_GET[GET_PERMALINK]));
  58. $permalink = strtolower(mysqli_real_escape_string($con, $_GET[GET_PERMALINK]));
  59. $username = mysqli_real_escape_string($con, $_GET[GET_USERNAME]);
  60. $text = mysqli_real_escape_string($con, $_GET[GET_TEXT]);
  61. //Validate data
  62. if (strlen($client) < 1){
  63. //Bad request
  64. http_response_code(400);
  65. exit();
  66. }
  67. if (strlen($user) < 1){
  68. $user = '';
  69. }
  70. if (strlen($target) < 1){
  71. //Bad request
  72. http_response_code(400);
  73. exit();
  74. }
  75. if ($target != TARGET_PHOTO && $target != TARGET_POST && $target != TARGET_ACTIVITY){
  76. //Bad request
  77. http_response_code(400);
  78. exit();
  79. }
  80. if (strlen($username) < 1){
  81. //Bad request
  82. http_response_code(400);
  83. exit();
  84. }
  85. //Check id and/or permalink. Several cases:
  86. //1st case: id and permalink empty: Error.
  87. if (strlen($id) < 1 && strlen($permalink) < 1){
  88. //Bad request
  89. http_response_code(400);
  90. exit();
  91. }
  92. //2nd case: Comment for post, permalink and no id.
  93. elseif ($target == TARGET_POST && strlen($id) < 1 && strlen($permalink) >= 1){
  94. //Check if post exists...
  95. $q = mysqli_query($con, "SELECT id, comments FROM post WHERE visible = 1 AND permalink = '$permalink';");
  96. if (mysqli_num_rows($q) == 0){
  97. //Bad request
  98. http_response_code(400);
  99. exit();
  100. }
  101. else{
  102. //... and if it does, check if can be commented.
  103. $r = mysqli_fetch_array($q);
  104. $item_id = $r['id'];
  105. if ($r['comments'] != 1){
  106. //'Forbidden' status code
  107. http_response_code(403);
  108. exit();
  109. }
  110. }
  111. }
  112. //3rd case: Comment for post, id and no permalink.
  113. elseif ($target == TARGET_POST && strlen($id) >= 1 && strlen($permalink) < 1){
  114. //Check if post exists...
  115. $q = mysqli_query($con, "SELECT id, comments FROM post WHERE visible = 1 AND id = $id;");
  116. if (mysqli_num_rows($q) == 0){
  117. //Bad request
  118. http_response_code(400);
  119. exit();
  120. }
  121. else{
  122. //... and if it does, check if can be commented.
  123. $r = mysqli_fetch_array($q);
  124. $item_id = $r['id'];
  125. if ($r['comments'] != 1){
  126. //'Forbidden' status code
  127. http_response_code(403);
  128. exit();
  129. }
  130. }
  131. }
  132. //4th case: Comment for post, permalink and id.
  133. elseif ($target == TARGET_POST && strlen($id) >= 1 && strlen($permalink) >= 1){
  134. //Check if post exists...
  135. $q = mysqli_query($con, "SELECT id, comments FROM post WHERE visible = 1 AND permalink = '$permalink' AND id = $id ;");
  136. if (mysqli_num_rows($q) == 0){
  137. //Bad request
  138. http_response_code(400);
  139. exit();
  140. }
  141. else{
  142. //... and if it does, check if can be commented.
  143. $r = mysqli_fetch_array($q);
  144. $item_id = $r['id'];
  145. if ($r['comments'] != 1){
  146. //'Forbidden' status code
  147. http_response_code(403);
  148. exit();
  149. }
  150. }
  151. }
  152. //5th case: Comment for photo, permalink and no id.
  153. elseif ($target == TARGET_PHOTO && strlen($id) < 1 && strlen($permalink) >= 1){
  154. //Check if photo exists.
  155. $q = mysqli_query($con, "SELECT id FROM photo WHERE approved = 1 AND permalink = '$permalink';");
  156. if (mysqli_num_rows($q) == 0){
  157. //Bad request
  158. http_response_code(400);
  159. exit();
  160. }
  161. else{
  162. $item_id = $r['id'];
  163. }
  164. }
  165. //6th case: Comment for photo, id and no permalink.
  166. elseif ($target == TARGET_PHOTO && strlen($id) >= 1 && strlen($permalink) < 1){
  167. //Check if photo exists.
  168. $q = mysqli_query($con, "SELECT id FROM photo WHERE approved = 1 AND id = $id;");
  169. if (mysqli_num_rows($q) == 0){
  170. //Bad request
  171. http_response_code(400);
  172. exit();
  173. }
  174. else{
  175. $item_id = $r['id'];
  176. }
  177. }
  178. //7th case: Comment for photo, permalink and id.
  179. elseif ($target == TARGET_PHOTO && strlen($id) >= 1 && strlen($permalink) >= 1){
  180. //Check if photo exists.
  181. $q = mysqli_query($con, "SELECT id FROM photo WHERE approved = 1 AND permalink = '$permalink' AND id = $id;");
  182. if (mysqli_num_rows($q) == 0){
  183. //Bad request
  184. http_response_code(400);
  185. exit();
  186. }
  187. else{
  188. $item_id = $r['id'];
  189. }
  190. }
  191. //8nd case: Comment for activity, permalink and no id.
  192. elseif ($target == TARGET_ACTIVITY && strlen($id) < 1 && strlen($permalink) >= 1){
  193. //Check if activity exists...
  194. $q = mysqli_query($con, "SELECT id, comments FROM activity WHERE visible = 1 AND permalink = '$permalink';");
  195. if (mysqli_num_rows($q) == 0){
  196. //Bad request
  197. http_response_code(400);
  198. exit();
  199. }
  200. else{
  201. //... and if it does, check if can be commented.
  202. $r = mysqli_fetch_array($q);
  203. $item_id = $r['id'];
  204. if ($r['comments'] != 1){
  205. //'Forbidden' status code
  206. http_response_code(403);
  207. exit();
  208. }
  209. }
  210. }
  211. //9rd case: Comment for activity, id and no permalink.
  212. elseif ($target == TARGET_ACTIVITY && strlen($id) >= 1 && strlen($permalink) < 1){
  213. //Check if activity exists...
  214. $q = mysqli_query($con, "SELECT id, comments FROM activity WHERE visible = 1 AND id = $id;");
  215. if (mysqli_num_rows($q) == 0){
  216. //Bad request
  217. http_response_code(400);
  218. exit();
  219. }
  220. else{
  221. //... and if it does, check if can be commented.
  222. $r = mysqli_fetch_array($q);
  223. $item_id = $r['id'];
  224. if ($r['comments'] != 1){
  225. //'Forbidden' status code
  226. http_response_code(403);
  227. exit();
  228. }
  229. }
  230. }
  231. //10th case: Comment for activity, permalink and id.
  232. elseif ($target == TARGET_ACTIVITY && strlen($id) >= 1 && strlen($permalink) >= 1){
  233. //Check if activity exists...
  234. $q = mysqli_query($con, "SELECT id, comments FROM activity WHERE visible = 1 AND permalink = '$permalink' AND id = $id ;");
  235. if (mysqli_num_rows($q) == 0){
  236. //Bad request
  237. http_response_code(400);
  238. exit();
  239. }
  240. else{
  241. //... and if it does, check if can be commented.
  242. $r = mysqli_fetch_array($q);
  243. $item_id = $r['id'];
  244. if ($r['comments'] != 1){
  245. //'Forbidden' status code
  246. http_response_code(403);
  247. exit();
  248. }
  249. }
  250. }
  251. //If code gets here, there were no errors. Build query.
  252. $query = "INSERT INTO ";
  253. $section = "";
  254. switch ($target){
  255. case TARGET_POST:
  256. $query = $query . "post_comment (post";
  257. $section = "blog";
  258. break;
  259. case TARGET_PHOTO:
  260. $query = $query . "photo_comment (photo";
  261. $section = "gallery";
  262. break;
  263. case TARGET_ACTIVITY:
  264. $query = $query . "activity_comment (activity";
  265. $section = "activity";
  266. break;
  267. }
  268. $query = $query . ", text, username, app) VALUES ($item_id, \"$text\", \"$username\", \"client\");";
  269. //echo($query);
  270. mysqli_query($con, $query);
  271. mysqli_query($con, "UPDATE version SET version = version + 1 WHERE section = '$section';");
  272. ?>