add.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. $http_host = $_SERVER['HTTP_HOST'];
  3. include("../../functions.php");
  4. $con = startdb('rw');
  5. if (!checkSession($con)){
  6. header("Location: /index.php");
  7. exit (-1);
  8. }
  9. else{
  10. // Get post data
  11. $title_es = mysqli_real_escape_string($con, $_POST['title_es']);
  12. $title_eu = mysqli_real_escape_string($con, $_POST['title_eu']);
  13. $title_en = mysqli_real_escape_string($con, $_POST['title_en']);
  14. $text_es = mysqli_real_escape_string($con, $_POST['text_es']);
  15. $text_eu = mysqli_real_escape_string($con, $_POST['text_eu']);
  16. $text_en = mysqli_real_escape_string($con, $_POST['text_en']);
  17. $image = array();
  18. $image[0] = $_FILES['image_0'];
  19. $image[1] = $_FILES['image_1'];
  20. $image[2] = $_FILES['image_2'];
  21. $image[3] = $_FILES['image_3'];
  22. $comments = mysqli_real_escape_string($con, $_POST['comments']);
  23. $visible = mysqli_real_escape_string($con, $_POST['visible']);
  24. $admin = mysqli_real_escape_string($con, $_POST['admin']);
  25. $schedule = mysqli_real_escape_string($con, $_POST['schedule']);
  26. $city = mysqli_real_escape_string($con, $_POST['city']);
  27. $day = mysqli_real_escape_string($con, $_POST['day']);
  28. $month = mysqli_real_escape_string($con, $_POST['month']);
  29. $year = mysqli_real_escape_string($con, $_POST['year']);
  30. $price = mysqli_real_escape_string($con, $_POST['price']);
  31. $inscription = mysqli_real_escape_string($con, $_POST['inscription']);
  32. $people = mysqli_real_escape_string($con, $_POST['people']);
  33. //Check spanish title and generate permalink
  34. if ($title_es == null){
  35. exit();
  36. }
  37. else{
  38. $permalink = permalink($title_es);
  39. $i = 2;
  40. $ok = false;
  41. $tmppermalink = $permalink;
  42. while ($ok == false){
  43. $query = mysqli_query($con, "SELECT id FROM activity WHERE permalink = '$tmppermalink';");
  44. if (mysqli_num_rows($query) > 0){
  45. $tmppermalink = $permalink . "-" . $i;
  46. $i ++;
  47. }
  48. else{
  49. $permalink = $tmppermalink;
  50. $ok = true;
  51. }
  52. }
  53. }
  54. //Check city
  55. if ($city == ''){
  56. $city = 'Vitoria-Gasteiz';
  57. }
  58. //Check booleans and assign default values if not
  59. if ($comments == 'off'){
  60. $comments = 0;
  61. }
  62. else{
  63. $comments = 1;
  64. }
  65. if ($visible == 'off'){
  66. $visible = 0;
  67. }
  68. else{
  69. $visible = 1;
  70. }
  71. if ($admin == 'on'){
  72. $admin = 1;
  73. }
  74. else{
  75. $admin = 0;
  76. }
  77. if ($inscription == 'off'){
  78. $inscription = 0;
  79. }
  80. else{
  81. $inscription = 1;
  82. }
  83. //TODO: Check valid date
  84. if ($day == '' || $month == '' || $year == ''){
  85. exit();
  86. }
  87. $date = "str_to_date('$year-$month-$day', '%Y-%m-%d')";
  88. //Assign numeric inputs, defaulting to 0 if not numeric
  89. $price = intval($price);
  90. $people = intval($people);
  91. // If no translations, same text in all languages
  92. if (strlen($text_eu) == 0){
  93. $text_eu = $text_es;
  94. $title_eu = $title_es;
  95. }
  96. if (strlen($text_en) == 0){
  97. $text_en = $text_es;
  98. $title_en = $title_es;
  99. }
  100. //Get user
  101. if ($admin){
  102. $user = 0;
  103. }
  104. else{
  105. $user = $_SESSION["id"];
  106. }
  107. //Insert into database and get id
  108. mysqli_query($con, "INSERT INTO activity (permalink, date, title_es, title_eu, title_en, text_es, text_eu, text_en, price, inscription, max_people, user, visible, comments, city) VALUES('$permalink', $date, '$title_es', '$title_eu', '$title_en', '$text_es', '$text_eu', '$text_en', $price, $inscription, $people, $user, $visible, $comments, '$city');");
  109. $q = mysqli_query($con, "SELECT id FROM activity WHERE permalink = '$permalink' ORDER BY dtime DESC LIMIT 1;");
  110. $r = mysqli_fetch_array($q);
  111. $id = $r['id'];
  112. //Process images
  113. $file_idx = 0;
  114. $img_idx = 0;
  115. while ($file_idx < 4 && $img_idx < 4){
  116. if (file_exists($image[$file_idx]['tmp_name']) > 0){
  117. //Convert to jpg
  118. $im = new imagick($image[$file_idx]['tmp_name']);
  119. $im->setImageFormat('jpg');
  120. $im->writeImage("../../../www/img/actividades/$permalink-n$img_idx.jpg");
  121. $im->resizeImage(800, 800, Imagick::FILTER_POINT, false);
  122. $im->writeImage("../../../www/img/actividades/view/$permalink-n$img_idx.jpg");
  123. $im->resizeImage(600, 600, Imagick::FILTER_POINT, false);
  124. $im->writeImage("../../../www/img/actividades/preview/$permalink-n$img_idx.jpg");
  125. $im->resizeImage(340, 340, Imagick::FILTER_POINT, false);
  126. $im->writeImage("../../../www/img/actividades/miniature/$permalink-n$img_idx.jpg");
  127. $im->resizeImage(180, 180, Imagick::FILTER_POINT, false);
  128. $im->writeImage("../../../www/img/actividades/thumb/$permalink-n$img_idx.jpg");
  129. $im->clear();
  130. $im->destroy();
  131. //Database
  132. mysqli_query($con, "INSERT INTO activity_image (activity, image, idx) VALUES ($id, '$permalink-n$img_idx.jpg', $img_idx);");
  133. $img_idx ++;
  134. }
  135. $file_idx ++;
  136. }
  137. //Edit schedule if requires, return if not
  138. if ($schedule == 'on'){
  139. header("Location: /actividades/add/itinerary.php?id=$id");
  140. }
  141. else{
  142. header("Location: /actividades/");
  143. }
  144. }
  145. ?>