comment.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. * This function selects the language for the *
  50. * comment if it has not been provided. It tries to *
  51. * detect the language cookie. *
  52. * *
  53. * @return: (string): Two letter language code or *
  54. * null. *
  55. ****************************************************/
  56. function detect_language(){
  57. //Try to read cookie.
  58. header('Cache-control: private');
  59. if (isSet($_COOKIE['lang'])){
  60. $lang = $_COOKIE['lang'];
  61. if ($lang == 'es' || $lang == 'en' || $lang == 'eu'){
  62. return $lang;
  63. }
  64. else{
  65. return null;
  66. }
  67. }
  68. }
  69. /*****************************************************
  70. * Gets information about the comment from the get *
  71. * paameters and the browser info. *
  72. * *
  73. * @params: *
  74. * con: (MySQL server connection) Db connector. *
  75. * get: (string array) Contains the GET *
  76. * parameters. *
  77. * @return: (string array): Array with the keys *
  78. * 'client', 'user', 'target', 'id', *
  79. * 'permalink', 'username', 'text', 'lang' *
  80. * and 'status'. 'status' will contain a *
  81. * 4XX status code if some parameter is *
  82. * missing, invalid, or the comment can't *
  83. * be posted. *
  84. *****************************************************/
  85. function get_comment_info($con, $get){
  86. $comment = array();
  87. //Get data from URL
  88. $comment["client"] = mysqli_real_escape_string($con, $get[GET_CLIENT]);
  89. $comment["user"] = mysqli_real_escape_string($con, $get[GET_USER]);
  90. $comment["target"] = strtolower(mysqli_real_escape_string($con, $get[GET_TARGET]));
  91. $comment["id"] = strtolower(mysqli_real_escape_string($con, $get[GET_ID]));
  92. $comment["permalink"] = strtolower(mysqli_real_escape_string($con, $get[GET_PERMALINK]));
  93. $comment["username"] = mysqli_real_escape_string($con, $get[GET_USERNAME]);
  94. $comment["text"] = mysqli_real_escape_string($con, $get[GET_TEXT]);
  95. if(isset($get[GET_LANG])){
  96. $comment["lang"] = mysqli_real_escape_string($con, $get[GET_LANG]);
  97. }
  98. else{
  99. $comment["lang"] = null;
  100. }
  101. $comment["status"] = 204; // No content status code: No error.
  102. //Validate data
  103. if (strlen($comment["client"]) < 1){
  104. $comment["status"] = 400; // Bad request status code.
  105. }
  106. if (strlen($comment["user"]) < 1){
  107. $comment["user"] = '';
  108. }
  109. if (strlen($comment["target"]) < 1){
  110. $comment["status"] = 400; // Bad request status code.
  111. }
  112. if ($comment["target"] != TARGET_PHOTO && $comment["target"] != TARGET_POST && $comment["target"] != TARGET_ACTIVITY){
  113. $comment["status"] = 400; // Bad request status code.
  114. }
  115. if (strlen($comment["username"]) < 1){
  116. $comment["status"] = 400; // Bad request status code.
  117. }
  118. if (strlen($comment["lang"]) == null){
  119. strlen($comment["lang"]) = detect_language()
  120. }
  121. //Check id and/or permalink. Several cases:
  122. //1st case: id and permalink empty: Error.
  123. if (strlen($comment["id"]) < 1 && strlen($comment["permalink"]) < 1){
  124. $comment["status"] = 400; // Bad request status code.
  125. }
  126. //2nd case: Comment for post, permalink and no id.
  127. elseif ($comment["target"] == TARGET_POST && strlen($comment["id"]) < 1 && strlen($comment["permalink"]) >= 1){
  128. //Check if post exists...
  129. $q = mysqli_query($con, "SELECT id, comments FROM post WHERE visible = 1 AND permalink = '$comment[permalink]';");
  130. if (mysqli_num_rows($q) == 0){
  131. $comment["status"] = 400; // Bad request status code.
  132. }
  133. else{
  134. //... and if it does, check if can be commented.
  135. $r = mysqli_fetch_array($q);
  136. $item_id = $r['id'];
  137. if ($r['comments'] != 1){
  138. $comment["status"] = 403; // Forbidden status code.
  139. }
  140. }
  141. }
  142. //3rd case: Comment for post, id and no permalink.
  143. elseif ($comment["target"] == TARGET_POST && strlen($comment["id"]) >= 1 && strlen($comment["permalink"]) < 1){
  144. //Check if post exists...
  145. $q = mysqli_query($con, "SELECT id, comments FROM post WHERE visible = 1 AND id = $comment[id];");
  146. if (mysqli_num_rows($q) == 0){
  147. $comment["status"] = 400; // Bad request status code.
  148. }
  149. else{
  150. //... and if it does, check if can be commented.
  151. $r = mysqli_fetch_array($q);
  152. $item_id = $r['id'];
  153. if ($r['comments'] != 1){
  154. $comment["status"] = 403; // Forbidden status code.
  155. }
  156. }
  157. }
  158. //4th case: Comment for post, permalink and id.
  159. elseif ($comment["target"] == TARGET_POST && strlen($comment["id"]) >= 1 && strlen($comment["permalink"]) >= 1){
  160. //Check if post exists...
  161. $q = mysqli_query($con, "SELECT id, comments FROM post WHERE visible = 1 AND permalink = '$comment[permalink]' AND id = $comment[id] ;");
  162. if (mysqli_num_rows($q) == 0){
  163. $comment["status"] = 400; // Bad request status code.
  164. }
  165. else{
  166. //... and if it does, check if can be commented.
  167. $r = mysqli_fetch_array($q);
  168. $item_id = $r['id'];
  169. if ($r['comments'] != 1){
  170. $comment["status"] = 403; // Forbidden status code.
  171. }
  172. }
  173. }
  174. //5th case: Comment for photo, permalink and no id.
  175. elseif ($comment["target"] == TARGET_PHOTO && strlen($comment["id"]) < 1 && strlen($comment["permalink"]) >= 1){
  176. //Check if photo exists.
  177. $q = mysqli_query($con, "SELECT id FROM photo WHERE approved = 1 AND permalink = '$comment[permalink]';");
  178. if (mysqli_num_rows($q) == 0){
  179. $comment["status"] = 400; // Bad request status code.
  180. }
  181. else{
  182. $item_id = $r['id'];
  183. }
  184. }
  185. //6th case: Comment for photo, id and no permalink.
  186. elseif ($comment["target"] == TARGET_PHOTO && strlen($comment["id"]) >= 1 && strlen($comment["permalink"]) < 1){
  187. //Check if photo exists.
  188. $q = mysqli_query($con, "SELECT id FROM photo WHERE approved = 1 AND id = $comment[id];");
  189. if (mysqli_num_rows($q) == 0){
  190. $comment["status"] = 400; // Bad request status code.
  191. }
  192. else{
  193. $item_id = $r['id'];
  194. }
  195. }
  196. //7th case: Comment for photo, permalink and id.
  197. elseif ($comment["target"] == TARGET_PHOTO && strlen($comment["id"]) >= 1 && strlen($comment["permalink"]) >= 1){
  198. //Check if photo exists.
  199. $q = mysqli_query($con, "SELECT id FROM photo WHERE approved = 1 AND permalink = '$comment[permalink]' AND id = $comment[id];");
  200. if (mysqli_num_rows($q) == 0){
  201. $comment["status"] = 400; // Bad request status code.
  202. }
  203. else{
  204. $item_id = $r['id'];
  205. }
  206. }
  207. //8th case: Comment for activity, permalink and no id.
  208. elseif ($comment["target"] == TARGET_ACTIVITY && strlen($comment["id"]) < 1 && strlen($comment["permalink"]) >= 1){
  209. //Check if activity exists...
  210. $q = mysqli_query($con, "SELECT id, comments FROM activity WHERE visible = 1 AND permalink = '$comment[permalink]';");
  211. if (mysqli_num_rows($q) == 0){
  212. $comment["status"] = 400; // Bad request status code.
  213. }
  214. else{
  215. //... and if it does, check if can be commented.
  216. $r = mysqli_fetch_array($q);
  217. $item_id = $r['id'];
  218. if ($r['comments'] != 1){
  219. $comment["status"] = 403; // Forbidden status code.
  220. }
  221. }
  222. }
  223. //9th case: Comment for activity, id and no permalink.
  224. elseif ($comment["target"] == TARGET_ACTIVITY && strlen($comment["id"]) >= 1 && strlen($comment["permalink"]) < 1){
  225. //Check if activity exists...
  226. $q = mysqli_query($con, "SELECT id, comments FROM activity WHERE visible = 1 AND id = $comment[id];");
  227. if (mysqli_num_rows($q) == 0){
  228. $comment["status"] = 400; // Bad request status code.
  229. }
  230. else{
  231. //... and if it does, check if can be commented.
  232. $r = mysqli_fetch_array($q);
  233. $item_id = $r['id'];
  234. if ($r['comments'] != 1){
  235. $comment["status"] = 403; // Forbidden status code.
  236. }
  237. }
  238. }
  239. //10th case: Comment for activity, permalink and id.
  240. elseif ($comment["target"] == TARGET_ACTIVITY && strlen($comment["id"]) >= 1 && strlen($comment["permalink"]) >= 1){
  241. //Check if activity exists...
  242. $q = mysqli_query($con, "SELECT id, comments FROM activity WHERE visible = 1 AND permalink = '$comment[permalink]' AND id = $comment[id] ;");
  243. if (mysqli_num_rows($q) == 0){
  244. $comment["status"] = 400; // Bad request status code.
  245. }
  246. else{
  247. //... and if it does, check if can be commented.
  248. $r = mysqli_fetch_array($q);
  249. $item_id = $r['id'];
  250. if ($r['comments'] != 1){
  251. $comment["status"] = 403; // Forbidden status code.
  252. }
  253. }
  254. }
  255. }
  256. /*****************************************************
  257. * Inserts the comment into the database. *
  258. * *
  259. * @params: *
  260. * con: (MySQL server connection) Db connector. *
  261. * comment: (string array) Array with the keys *
  262. * 'client', 'user', 'target', 'id', *
  263. * 'permalink', 'username', 'text', *
  264. * 'lang' and 'status'. *
  265. *****************************************************/
  266. function insert_comment($con, $comment){
  267. $query = "INSERT INTO ";
  268. switch ($comment["target"]){
  269. case TARGET_POST:
  270. $query = $query . "post_comment (post";
  271. break;
  272. case TARGET_PHOTO:
  273. $query = $query . "photo_comment (photo";
  274. break;
  275. case TARGET_ACTIVITY:
  276. $query = $query . "activity_comment (activity";
  277. break;
  278. }
  279. if ($comment["lang"] == null){
  280. $lang = "'$comment[lang]'";
  281. }
  282. else{
  283. $lang = "null";
  284. }
  285. $query = $query . ", text, username, app, app_user, lang) VALUES ($comment[id], '$comment[text]', '$comment[username]', '$comment[client]', '$comment[user]', $lang);";
  286. mysqli_query($con, $query);
  287. }
  288. //Connect to the database
  289. $con = startdb('rw');
  290. $comment = get_comment_info($con, $_GET);
  291. if ($comment["status"] >= 400){ // 4XX or 5XX are errors.
  292. http_response_code($comment["status"]);
  293. exit(-1);
  294. }
  295. insert_comment($con, $comment);
  296. http_response_code(204);
  297. ?>