add.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. //Check spanish title and generate permalink
  26. if ($title_es == null)
  27. exit();
  28. else{
  29. $permalink = permalink($title_es);
  30. $i = 2;
  31. $ok = false;
  32. $tmppermalink = $permalink;
  33. while ($ok == false){
  34. $query = mysqli_query($con, "SELECT id FROM post WHERE permalink = '$tmppermalink';");
  35. if (mysqli_num_rows($query) > 0){
  36. $tmppermalink = $permalink . "-" . $i;
  37. $i ++;
  38. }
  39. else{
  40. $permalink = $tmppermalink;
  41. $ok = true;
  42. }
  43. }
  44. }
  45. //Check titles with no text
  46. if (strlen($title_eu) > 0 && strlen($text_eu) < 1)
  47. exit();
  48. if (strlen($title_en) > 0 && strlen($text_en) < 1)
  49. exit();
  50. //Check text with no titles
  51. if (strlen($text_eu) > 0 && strlen($title_eu) < 1)
  52. exit();
  53. if (strlen($text_en) > 0 && strlen($title_en) < 1)
  54. exit();
  55. //Check booleans and assign default values if not
  56. if ($comments == 'off')
  57. $comments = 0;
  58. else
  59. $comments = 1;
  60. if ($visible == 'off')
  61. $visible = 0;
  62. else
  63. $visible = 1;
  64. if ($admin == 'on')
  65. $admin = 1;
  66. else
  67. $admin = 0;
  68. // If no translations, same text in all languages
  69. if (strlen($text_eu) == 0){
  70. $text_eu = $text_es;
  71. $title_eu = $title_es;
  72. }
  73. if (strlen($text_en) == 0){
  74. $text_en = $text_es;
  75. $title_en = $title_es;
  76. }
  77. //Get user
  78. if ($admin)
  79. $user = 0;
  80. else{
  81. //TODO
  82. $user = 0;
  83. }
  84. //Insert into database and get id
  85. mysqli_query($con, "INSERT INTO post (permalink, title_es, title_eu, title_en, text_es, text_eu, text_en, user, visible, comments) VALUES ('$permalink', '$title_es', '$title_eu', '$title_en', '$text_es', '$text_eu', '$text_en', $user, $visible, $comments);");
  86. $res_post = mysqli_query($con, "SELECT id FROM post WHERE permalink = '$permalink' ORDER BY dtime DESC LIMIT 1;");
  87. $row_post = mysqli_fetch_array($res_post);
  88. $post_id = $row_post['id'];
  89. //Process images
  90. $file_idx = 0;
  91. $img_idx = 0;
  92. while ($file_idx < 4 && $img_idx < 4){
  93. if (file_exists($image[$file_idx]['tmp_name']) > 0){
  94. //Convert to jpg
  95. $im = new imagick($image[$file_idx]['tmp_name']);
  96. $im->setImageFormat('jpg');
  97. $im->writeImage("../../../www/img/blog/$permalink-n$img_idx.jpg");
  98. $im->resizeImage(800, 800, Imagick::FILTER_POINT, false);
  99. $im->writeImage("../../../www/img/blog/view/$permalink-n$img_idx.jpg");
  100. $im->resizeImage(600, 600, Imagick::FILTER_POINT, false);
  101. $im->writeImage("../../../www/img/blog/preview/$permalink-n$img_idx.jpg");
  102. $im->resizeImage(340, 340, Imagick::FILTER_POINT, false);
  103. $im->writeImage("../../../www/img/blog/miniature/$permalink-n$img_idx.jpg");
  104. $im->resizeImage(180, 180, Imagick::FILTER_POINT, false);
  105. $im->writeImage("../../../www/img/blog/thumb/$permalink-n$img_idx.jpg");
  106. $im->clear();
  107. $im->destroy();
  108. //Database
  109. mysqli_query($con, "INSERT INTO post_image (post, image, idx) VALUES ($post_id, '$permalink-n$img_idx.jpg', $img_idx);");
  110. $img_idx ++;
  111. }
  112. $file_idx ++;
  113. }
  114. version();
  115. header("Location: /blog/");
  116. }
  117. ?>