jmyeom

upload via url

Jun 6th, 2011
607
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.42 KB | None | 0 0
  1. ############## Add this in *_post_box.tpl where you want your form to appear
  2.  
  3. <tr>
  4. <td class="postblock">
  5. {t}File URL{/t}</td>
  6. <td>
  7. <input type="text" name="fileurl" size="48" accesskey="h"/>
  8. </td>
  9. </tr>
  10.  
  11. ############## then add this in upload.class.php
  12.  
  13. function HandleUploadURL() {
  14. global $tc_db, $board_class, $is_oekaki, $oekaki;
  15. if (!$is_oekaki) {
  16. if ($board_class->board['type'] == 0 || $board_class->board['type'] == 2 || $board_class->board['type'] == 3) {
  17. $fileurl = isset($_POST['fileurl']) ? $_POST['fileurl'] : '';
  18. if ($fileurl != '') {
  19. $fileurl_size = remote_filesize($fileurl);
  20. if ($fileurl_size <= 0) {
  21. exitWithErrorPage(_gettext('link to image is not valid'));
  22. }
  23. if ($fileurl_size > $board_class->board['maximagesize']) {
  24. exitWithErrorPage(sprintf(_gettext('Please make sure your file is smaller than %dB'), $board_class->board['maximagesize']));
  25. }
  26. $this->file_type = strrchr($fileurl, ".");
  27. if ($this->file_type == "") {
  28. exitWithErrorPage(_gettext("the file extension is incorrect"));
  29. }
  30. if ($this->file_type == '.jpeg') {
  31. /* Fix for the rarely used 4-char format */
  32. $this->file_type = '.jpg';
  33. }
  34. $tmpfname = tempnam(sys_get_temp_dir(), "FOO");
  35.  
  36. $tmp_file = fopen($tmpfname, "w");
  37. $fileurl_read = fopen ($fileurl, "rb");
  38. if ($fileurl_read) {
  39. while(!feof($fileurl_read)) {
  40. fwrite($tmp_file, fread($fileurl_read, 1024 * 8 ), 1024 * 8 );
  41. }
  42. } else {
  43. //closeAll($fileurl_read, $tmp_file, $tmpfname);
  44. exitWithErrorPage(_gettext('link to image is not valid'));
  45. }
  46. fclose($fileurl_read);
  47. $this->file_name = substr(htmlspecialchars(basename($fileurl, $this->file_type), ENT_QUOTES), 0, 50);
  48. $this->file_name = str_replace('.','_',$this->file_name);
  49. $this->original_file_name = $this->file_name;
  50. $this->file_md5 = md5_file($tmpfname);
  51. $exists_thread = checkMd5($this->file_md5, $board_class->board['name'], $board_class->board['id']);
  52. if (is_array($exists_thread)) {
  53. //closeTemp($tmp_file, $tmpfname);
  54. exitWithErrorPage(_gettext('Duplicate file entry detected.'), sprintf(_gettext('Already posted %shere%s.'),'<a href="' . KU_BOARDSPATH . '/' . $board_class->board['name'] . '/res/' . $exists_thread[0] . '.html#' . $exists_thread[1] . '">','</a>'));
  55. }
  56. if (strtolower($this->file_type) == 'svg') {
  57. require_once 'svg.class.php';
  58. $svg = new Svg($tmpfname);
  59. $this->imgWidth = $svg->width;
  60. $this->imgHeight = $svg->height;
  61. } else {
  62. $imageDim = getimagesize($tmpfname);
  63. $this->imgWidth = $imageDim[0];
  64. $this->imgHeight = $imageDim[1];
  65. }
  66. $this->file_type = strtolower($this->file_type);
  67. $this->file_size = $fileurl_size;
  68. $filetype_forcethumb = $tc_db->GetOne("SELECT " . KU_DBPREFIX . "filetypes.force_thumb FROM " . KU_DBPREFIX . "boards, " . KU_DBPREFIX . "filetypes, " . KU_DBPREFIX . "board_filetypes WHERE " . KU_DBPREFIX . "boards.id = " . KU_DBPREFIX . "board_filetypes.boardid AND " . KU_DBPREFIX . "filetypes.id = " . KU_DBPREFIX . "board_filetypes.typeid AND " . KU_DBPREFIX . "boards.name = '" . $board_class->board['name'] . "' and " . KU_DBPREFIX . "filetypes.filetype = '" . substr($this->file_type, 1) . "';");
  69. if ($filetype_forcethumb != '') {
  70. if ($filetype_forcethumb == 0) {
  71. $this->file_name = time() . mt_rand(1, 99);
  72. $this->file_location = KU_BOARDSDIR . $board_class->board['name'] . '/src/' . $this->file_name . $this->file_type;
  73. $this->file_thumb_location = KU_BOARDSDIR . $board_class->board['name'] . '/thumb/' . $this->file_name . 's' . $this->file_type;
  74. $this->file_thumb_cat_location = KU_BOARDSDIR . $board_class->board['name'] . '/thumb/' . $this->file_name . 'c' . $this->file_type;
  75.  
  76. if (!copy($tmpfname, $this->file_location)) {
  77. //closeTemp($tmp_file, $tmpfname);
  78. exitWithErrorPage(sprintf(_gettext('Could not copy uploaded image. --%s-- --%s--'), $tmpfname, $this->file_location));
  79. }
  80. chmod($this->file_location, 0644);
  81.  
  82. if ($fileurl_size == filesize($this->file_location)) {
  83. if ((!$this->isreply && ($this->imgWidth > KU_THUMBWIDTH || $this->imgHeight > KU_THUMBHEIGHT)) || ($this->isreply && ($this->imgWidth > KU_REPLYTHUMBWIDTH || $this->imgHeight > KU_REPLYTHUMBHEIGHT))) {
  84. if (!$this->isreply) {
  85. if (!createThumbnail($this->file_location, $this->file_thumb_location, KU_THUMBWIDTH, KU_THUMBHEIGHT)) {
  86. //closeTemp($tmp_file, $tmpfname);
  87. exitWithErrorPage(_gettext('Could not create thumbnail.'));
  88. }
  89. } else {
  90. if (!createThumbnail($this->file_location, $this->file_thumb_location, KU_REPLYTHUMBWIDTH, KU_REPLYTHUMBHEIGHT)) {
  91. //closeTemp($tmp_file, $tmpfname);
  92. exitWithErrorPage(_gettext('Could not create thumbnail.'));
  93. }
  94. }
  95. } else {
  96. if (!createThumbnail($this->file_location, $this->file_thumb_location, $this->imgWidth, $this->imgHeight)) {
  97. //closeTemp($tmp_file, $tmpfname);
  98. exitWithErrorPage(_gettext('Could not create thumbnail.'));
  99. }
  100. }
  101. if (!createThumbnail($this->file_location, $this->file_thumb_cat_location, KU_CATTHUMBWIDTH, KU_CATTHUMBHEIGHT)) {
  102. //closeTemp($tmp_file, $tmpfname);
  103. exitWithErrorPage(_gettext('Could not create thumbnail.'));
  104. }
  105. $imageDim_thumb = getimagesize($this->file_thumb_location);
  106. $this->imgWidth_thumb = $imageDim_thumb[0];
  107. $this->imgHeight_thumb = $imageDim_thumb[1];
  108. $imageused = true;
  109. } else {
  110. //closeTemp($tmp_file, $tmpfname);
  111. exitWithErrorPage(sprintf(_gettext('File was not fully uploaded. Please go back and try again. %s %s'), $fileurl_size, filesize($this->file_location)));
  112. }
  113. } else {
  114. /* Fetch the mime requirement for this special filetype */
  115. $filetype_required_mime = $tc_db->GetOne("SELECT `mime` FROM `" . KU_DBPREFIX . "filetypes` WHERE `filetype` = " . $tc_db->qstr(substr($this->file_type, 1)));
  116.  
  117. $this->file_name = htmlspecialchars_decode($this->file_name, ENT_QUOTES);
  118. $this->file_name = stripslashes($this->file_name);
  119. $this->file_name = str_replace("\x80", " ", $this->file_name);
  120. $this->file_name = str_replace(' ', '_', $this->file_name);
  121. $this->file_name = str_replace('#', '(number)', $this->file_name);
  122. $this->file_name = str_replace('@', '(at)', $this->file_name);
  123. $this->file_name = str_replace('/', '(fwslash)', $this->file_name);
  124. $this->file_name = str_replace('\\', '(bkslash)', $this->file_name);
  125.  
  126. $this->file_location = KU_BOARDSDIR . $board_class->board['name'] . '/src/' . $this->file_name . $this->file_type;
  127.  
  128. if($this->file_type == '.mp3') {
  129. require_once(KU_ROOTDIR . 'lib/getid3/getid3.php');
  130.  
  131. $getID3 = new getID3;
  132. $getID3->analyze($tmpfname);
  133. if (isset($getID3->info['id3v2']['APIC'][0]['data']) && isset($getID3->info['id3v2']['APIC'][0]['image_mime'])) {
  134. $source_data = $getID3->info['id3v2']['APIC'][0]['data'];
  135. $mime = $getID3->info['id3v2']['APIC'][0]['image_mime'];
  136. }
  137. elseif (isset($getID3->info['id3v2']['PIC'][0]['data']) && isset($getID3->info['id3v2']['PIC'][0]['image_mime'])) {
  138. $source_data = $getID3->info['id3v2']['PIC'][0]['data'];
  139. $mime = $getID3->info['id3v2']['PIC'][0]['image_mime'];
  140. }
  141.  
  142. if($source_data) {
  143. $im = imagecreatefromstring($source_data);
  144. if (preg_match("/png/", $mime)) {
  145. $ext = ".png";
  146. imagepng($im,$this->file_location.".tmp",0,PNG_ALL_FILTERS);
  147. } else if (preg_match("/jpg|jpeg/", $mime)) {
  148. $ext = ".jpg";
  149. imagejpeg($im, $this->file_location.".tmp");
  150. } else if (preg_match("/gif/", $mime)) {
  151. $ext = ".gif";
  152. imagegif($im, $this->file_location.".tmp");
  153. }
  154. $this->file_thumb_location = KU_BOARDSDIR . $board_class->board['name'] . '/thumb/' . $this->file_name .'s'. $ext;
  155. if (!$this->isreply) {
  156. if (!createThumbnail($this->file_location.".tmp", $this->file_thumb_location, KU_THUMBWIDTH, KU_THUMBHEIGHT)) {
  157. //closeTemp($tmp_file, $tmpfname);
  158. exitWithErrorPage(_gettext('Could not create thumbnail.'));
  159. }
  160. } else {
  161. if (!createThumbnail($this->file_location.".tmp", $this->file_thumb_location, KU_REPLYTHUMBWIDTH, KU_REPLYTHUMBHEIGHT)) {
  162. //closeTemp($tmp_file, $tmpfname);
  163. exitWithErrorPage(_gettext('Could not create thumbnail.'));
  164. }
  165. }
  166. $imageDim_thumb = getimagesize($this->file_thumb_location);
  167. $this->imgWidth_thumb = $imageDim_thumb[0];
  168. $this->imgHeight_thumb = $imageDim_thumb[1];
  169. $imageused = true;
  170. unlink($this->file_location.".tmp");
  171. }
  172.  
  173.  
  174. }
  175.  
  176. /* Move the file from the post data to the server */
  177. if (!copy($tmpfname, $this->file_location)) {
  178. //closeTemp($tmp_file, $tmpfname);
  179. exitWithErrorPage(_gettext('Could not copy uploaded image.'));
  180. }
  181.  
  182. /* Check if the filetype provided comes with a MIME restriction */
  183. if ($filetype_required_mime != '') {
  184. /* Check if the MIMEs don't match up */
  185. if (mime_content_type($this->file_location) != $filetype_required_mime) {
  186. //closeTemp($tmp_file, $tmpfname);
  187. /* Delete the file we just uploaded and kill the script */
  188. unlink($this->file_location);
  189. exitWithErrorPage(_gettext('Invalid MIME type for this filetype.'));
  190. }
  191. }
  192.  
  193. /* Make sure the entire file was uploaded */
  194. if ($fileurl_size == filesize($this->file_location)) {
  195. $imageused = true;
  196. } else {
  197. //closeTemp($tmp_file, $tmpfname);
  198. exitWithErrorPage(_gettext('File transfer failure. Please go back and try again.'));
  199. }
  200.  
  201. /* Flag that the file used isn't an internally supported type */
  202. $this->file_is_special = true;
  203. }
  204. } else {
  205. //closeTemp($tmp_file, $tmpfname);
  206. exitWithErrorPage(_gettext('Sorry, that filetype is not allowed on this board.'));
  207. }
  208. //closeTemp($tmp_file, $tmpfname);
  209. }
  210. }
  211. }
  212. }
  213.  
  214.  
  215. ############## then add this to misc.php
  216.  
  217. function remote_filesize($url){
  218. $uh = curl_init();
  219. curl_setopt($uh, CURLOPT_URL, $url);
  220.  
  221. // set NO-BODY to not receive body part
  222. curl_setopt($uh, CURLOPT_NOBODY, TRUE);
  223.  
  224. // set HEADER to be false, we don't need header
  225. curl_setopt($uh, CURLOPT_HEADER, FALSE);
  226.  
  227. curl_exec($uh);
  228.  
  229. // assign filesize into $filesize variable
  230. $filesize = curl_getinfo($uh,CURLINFO_CONTENT_LENGTH_DOWNLOAD);
  231.  
  232. curl_close($uh);
  233.  
  234. if ($filesize <= 0) {
  235. $string = @implode('', file($url));
  236. $filesize = strlen($string);
  237. }
  238. return $filesize;
  239. }
  240.  
  241. ############## then in board.php find this "if"
  242.  
  243. if ((!isset($_POST['nofile']) && $board_class->board['enablenofile'] == 1) || $board_class->board['enablenofile'] == 0) {
  244.  
  245. ############## and change to this
  246.  
  247. if ((!isset($_POST['nofile']) && $board_class->board['enablenofile'] == 1) || $board_class->board['enablenofile'] == 0) {
  248. if (!isset($_POST['fileurl']) || empty($_POST['fileurl']))
  249. $upload_class->HandleUpload();
  250. else
  251. $upload_class->HandleUploadURL();
  252. }
  253.  
  254. ############## always in board.php find this "if"
  255.  
  256. if ($board_class->board['type'] != 1 && (($board_class->board['uploadtype'] == '1' || $board_class->board['uploadtype'] == '2') && $board_class->board['embeds_allowed'] != '')) {
  257.  
  258. ############## and change to this
  259.  
  260. if ($board_class->board['type'] != 1 && (($board_class->board['uploadtype'] == '1' || $board_class->board['uploadtype'] == '2') && $board_class->board['embeds_allowed'] != '')) {
  261. if (isset($_POST['embed'])) {
  262. if ($_POST['embed'] == '') {
  263. if (($board_class->board['uploadtype'] == '1' && $imagefile_name == '') || $board_class->board['uploadtype'] == '2') {
  264. if (isset($_POST['fileurl']) && empty($_POST['fileurl']))
  265. exitWithErrorPage('Please enter an embed ID.');
  266. }
  267. }
  268. } else {
  269. if (isset($_POST['fileurl']) && empty($_POST['fileurl']))
  270. exitWithErrorPage('Please enter an embed ID.');
  271. }
  272. }
  273.  
  274.  
  275. ############## again in board.php find this
  276.  
  277. if ($imagefile_name == '' && !$is_oekaki && ((!isset($_POST['nofile'])&&$board_class->board['enablenofile']==1) || $board_class->board['enablenofile']==0) && ($board_class->board['type'] == 0 || $board_class->board['type'] == 2 || $board_class->board['type'] == 3)) {
  278. if (!isset($_POST['embed']) && $board_class->board['uploadtype'] != 1) {
  279. exitWithErrorPage(_gettext('A file is required for a new thread. If embedding is allowed, either a file or embed ID is required.'));
  280. }
  281. }
  282.  
  283.  
  284. ############## replace with this
  285.  
  286. if ($imagefile_name == '' && !$is_oekaki && ((!isset($_POST['nofile'])&&$board_class->board['enablenofile']==1) || $board_class->board['enablenofile']==0) && ($board_class->board['type'] == 0 || $board_class->board['type'] == 2 || $board_class->board['type'] == 3) && !isset($_POST['fileurl']) && !empty($_POST['fileurl'])) {
  287. if (!isset($_POST['embed']) && $board_class->board['uploadtype'] != 1) {
  288. exitWithErrorPage(_gettext('A file is required for a new thread. If embedding is allowed, either a file or embed ID is required.'));
  289. }
  290. }
Advertisement
Add Comment
Please, Sign In to add comment