PageRenderTime 51ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/func/posts.php

https://github.com/stormeus/Kusaba-Z
PHP | 255 lines | 171 code | 28 blank | 56 comment | 50 complexity | 9672228bafc24d7f2ecfbee944f51cfb MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Display the embedded video
  4. *
  5. * @param array $post Post data
  6. * @return string Embedded video
  7. */
  8. function embeddedVideoBox($post) {
  9. global $tc_db;
  10. $results = $tc_db->GetAll("SELECT HIGH_PRIORITY * FROM `" . KU_DBPREFIX . "embeds`");
  11. $output = '<span style="float: left;">' . "\n";
  12. foreach ($results as $line) {
  13. if ($post['file_type'] == $line['filetype']) {
  14. $code = $line['code'];
  15. /*$width = $line['width'];
  16. $height = $line['height'];*/
  17. $replace = array('SET_HEIGHT', 'SET_WIDTH', 'EMBED_ID');
  18. $trueval = array($line['height'], $line['width'], $post['file']);
  19. $code = str_replace($replace, $trueval, $code);
  20. $output .= $code;
  21. }
  22. }
  23. $output .= '</span>&nbsp;' . "\n";
  24. return $output;
  25. }
  26. /**
  27. * Check if the supplied md5 file hash is currently recorded inside of the database, attached to a non-deleted post
  28. */
  29. function checkMd5($md5, $board, $boardid) {
  30. global $tc_db;
  31. $matches = $tc_db->GetAll("SELECT `id`, `parentid` FROM `".KU_DBPREFIX."posts` WHERE `boardid` = " . $boardid . " AND `IS_DELETED` = 0 AND `file_md5` = ".$tc_db->qstr($md5)." LIMIT 1");
  32. if (count($matches) > 0) {
  33. $real_parentid = ($matches[0][1] == 0) ? $matches[0][0] : $matches[0][1];
  34. return array($real_parentid, $matches[0][0]);
  35. }
  36. return false;
  37. }
  38. /* Image handling */
  39. /**
  40. * Create a thumbnail
  41. *
  42. * @param string $name File to be thumbnailed
  43. * @param string $filename Path to place the thumbnail
  44. * @param integer $new_w Maximum width
  45. * @param integer $new_h Maximum height
  46. * @return boolean Success/fail
  47. */
  48. function createThumbnail($name, $filename, $new_w, $new_h) {
  49. if (KU_THUMBMETHOD == 'imagemagick') {
  50. $convert = 'convert ' . escapeshellarg($name);
  51. if (!KU_ANIMATEDTHUMBS) {
  52. $convert .= '[0] ';
  53. }
  54. $convert .= ' -resize ' . $new_w . 'x' . $new_h . ' -quality ';
  55. if (substr($filename, 0, -3) != 'gif') {
  56. $convert .= '70';
  57. } else {
  58. $convert .= '90';
  59. }
  60. $convert .= ' ' . escapeshellarg($filename);
  61. exec($convert);
  62. if (is_file($filename)) {
  63. return true;
  64. } else {
  65. return false;
  66. }
  67. } elseif (KU_THUMBMETHOD == 'gd') {
  68. $system=explode(".", $filename);
  69. $system = array_reverse($system);
  70. if (preg_match("/jpg|jpeg/", $system[0])) {
  71. $src_img=imagecreatefromjpeg($name);
  72. } else if (preg_match("/png/", $system[0])) {
  73. $src_img=imagecreatefrompng($name);
  74. } else if (preg_match("/gif/", $system[0])) {
  75. $src_img=imagecreatefromgif($name);
  76. } else {
  77. return false;
  78. }
  79. if (!$src_img) {
  80. exitWithErrorPage(_gettext('Unable to read uploaded file during thumbnailing.'), _gettext('A common cause for this is an incorrect extension when the file is actually of a different type.'));
  81. }
  82. $old_x = imageSX($src_img);
  83. $old_y = imageSY($src_img);
  84. if ($old_x > $old_y) {
  85. $percent = $new_w / $old_x;
  86. } else {
  87. $percent = $new_h / $old_y;
  88. }
  89. $thumb_w = round($old_x * $percent);
  90. $thumb_h = round($old_y * $percent);
  91. $dst_img = ImageCreateTrueColor($thumb_w, $thumb_h);
  92. fastImageCopyResampled($dst_img, $src_img, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_x, $old_y, $system);
  93. if (preg_match("/png/", $system[0])) {
  94. if (!imagepng($dst_img,$filename,0,PNG_ALL_FILTERS) ) {
  95. echo 'unable to imagepng.';
  96. return false;
  97. }
  98. } else if (preg_match("/jpg|jpeg/", $system[0])) {
  99. if (!imagejpeg($dst_img, $filename, 70)) {
  100. echo 'unable to imagejpg.';
  101. return false;
  102. }
  103. } else if (preg_match("/gif/", $system[0])) {
  104. if (!imagegif($dst_img, $filename)) {
  105. echo 'unable to imagegif.';
  106. return false;
  107. }
  108. }
  109. imagedestroy($dst_img);
  110. imagedestroy($src_img);
  111. return true;
  112. }
  113. return false;
  114. }
  115. /* Author: Tim Eckel - Date: 12/17/04 - Project: FreeRingers.net - Freely distributable. */
  116. /**
  117. * Faster method than only calling imagecopyresampled()
  118. *
  119. * @return boolean Success/fail
  120. */
  121. function fastImageCopyResampled(&$dst_image, &$src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h, $system, $quality = 3) {
  122. /*
  123. Optional "quality" parameter (defaults is 3). Fractional values are allowed, for example 1.5.
  124. 1 = Up to 600 times faster. Poor results, just uses imagecopyresized but removes black edges.
  125. 2 = Up to 95 times faster. Images may appear too sharp, some people may prefer it.
  126. 3 = Up to 60 times faster. Will give high quality smooth results very close to imagecopyresampled.
  127. 4 = Up to 25 times faster. Almost identical to imagecopyresampled for most images.
  128. 5 = No speedup. Just uses imagecopyresampled, highest quality but no advantage over imagecopyresampled.
  129. */
  130. if (empty($src_image) || empty($dst_image) || $quality <= 0) { return false; }
  131. if (preg_match("/png/", $system[0]) || preg_match("/gif/", $system[0])) {
  132. $colorcount = imagecolorstotal($src_image);
  133. if ($colorcount <= 256 && $colorcount != 0) {
  134. imagetruecolortopalette($dst_image,true,$colorcount);
  135. imagepalettecopy($dst_image,$src_image);
  136. $transparentcolor = imagecolortransparent($src_image);
  137. imagefill($dst_image,0,0,$transparentcolor);
  138. imagecolortransparent($dst_image,$transparentcolor);
  139. }
  140. else {
  141. imageAlphaBlending($dst_image, false);
  142. imageSaveAlpha($dst_image, true); //If the image has Alpha blending, lets save it
  143. }
  144. }
  145. if ($quality < 5 && (($dst_w * $quality) < $src_w || ($dst_h * $quality) < $src_h)) {
  146. $temp = imagecreatetruecolor ($dst_w * $quality + 1, $dst_h * $quality + 1);
  147. if (preg_match("/png/", $system[0])) {
  148. $background = imagecolorallocate($temp, 0, 0, 0);
  149. ImageColorTransparent($temp, $background); // make the new temp image all transparent
  150. imagealphablending($temp, false); // turn off the alpha blending to keep the alpha channel
  151. }
  152. imagecopyresized ($temp, $src_image, 0, 0, $src_x, $src_y, $dst_w * $quality + 1, $dst_h * $quality + 1, $src_w, $src_h);
  153. imagecopyresampled ($dst_image, $temp, $dst_x, $dst_y, 0, 0, $dst_w, $dst_h, $dst_w * $quality, $dst_h * $quality);
  154. imagedestroy ($temp);
  155. }
  156. else imagecopyresampled ($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
  157. return true;
  158. }
  159. /*
  160. Link validator
  161. Will use cURL to attempt to visit a webpage, and then return based upon how the
  162. request was handled. Used for embedded videos to validate the ID is existant.
  163. Thanks phadeguy - http://www.zend.com/codex.php?id=1256&single=1
  164. expects a link url as string
  165. returns an array of three elements:
  166. return_array[0] = HTTP version
  167. return_array[1] = Returned error number (200, 404, etc)
  168. return_array[2] = Returned error text ("OK", "File Not Found", etc) */
  169. function check_link($link) {
  170. $main = array();
  171. $ch = curl_init();
  172. curl_setopt ($ch, CURLOPT_URL, $link);
  173. curl_setopt ($ch, CURLOPT_HEADER, 1);
  174. curl_setopt ($ch, CURLOPT_NOBODY, 1);
  175. // curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
  176. curl_setopt ($ch, CURLOPT_TIMEOUT, 10);
  177. curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
  178. ob_start();
  179. curl_exec ($ch);
  180. $stuff = ob_get_contents();
  181. ob_end_clean();
  182. curl_close ($ch);
  183. $parts = split("n",$stuff,2);
  184. $main = split(" ",$parts[0],3);
  185. return $main;
  186. }
  187. /**
  188. * Trim the threads to the page limit and delete posts which are older than limited
  189. */
  190. function TrimToPageLimit($board) {
  191. global $tc_db;
  192. if ($board['maxage'] != 0) {
  193. // If the maximum thread age setting is not zero (do not delete old threads), find posts which are older than the limit, and delete them
  194. $results = $tc_db->GetAll("SELECT `id`, `timestamp` FROM `".KU_DBPREFIX."posts` WHERE `boardid` = " . $board['id'] . " AND `IS_DELETED` = 0 AND `parentid` = 0 AND `stickied` = 0 AND ((`timestamp` + " . ($board['maxage']*3600) . ") < " . time() . ")");
  195. foreach($results AS $line) {
  196. // If it is older than the limit
  197. $post_class = new Post($line['id'], $board['name'], $board['id']);
  198. $post_class->Delete(true);
  199. }
  200. }
  201. if ($board['maxpages'] != 0) {
  202. // If the maximum pages setting is not zero (do not limit pages), find posts which are over the limit, and delete them
  203. $results = $tc_db->GetAll("SELECT `id`, `stickied` FROM `".KU_DBPREFIX."posts` WHERE `boardid` = " . $board['id'] . " AND `IS_DELETED` = 0 AND `parentid` = 0");
  204. $results_count = count($results);
  205. if (calculatenumpages($board['type'], $results_count) >= $board['maxpages']) {
  206. $board['maxthreads'] = ($board['maxpages'] * KU_THREADS);
  207. $numthreadsover = ($results_count - $board['maxthreads']);
  208. if ($numthreadsover > 0) {
  209. $resultspost = $tc_db->GetAll("SELECT `id`, `stickied` FROM `".KU_DBPREFIX."posts` WHERE `boardid` = " . $board['id'] . " AND `IS_DELETED` = 0 AND `parentid` = 0 AND `stickied` = 0 ORDER BY `bumped` ASC LIMIT " . $numthreadsover);
  210. foreach($resultspost AS $linepost) {
  211. $post_class = new Post($linepost['id'], $board['name'], $board['id']);
  212. $post_class->Delete(true);
  213. }
  214. }
  215. }
  216. }
  217. // If the thread was marked for deletion more than two hours ago, delete it
  218. $results = $tc_db->GetAll("SELECT `id` FROM `".KU_DBPREFIX."posts` WHERE `boardid` = " . $board['id'] . " AND `IS_DELETED` = 0 AND `parentid` = 0 AND `stickied` = 0 AND `deleted_timestamp` > 0 AND (`deleted_timestamp` <= " . time() . ")");
  219. foreach($results AS $line) {
  220. // If it is older than the limit
  221. $post_class = new Post($line['id'], $board['name'], $board['id']);
  222. $post_class->Delete(true);
  223. }
  224. }
  225. ?>