add.php 6.4 KB

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