comment.php 12 KB

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