PageRenderTime 24ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/legacy/uploads/uploader.php

http://novaboard.googlecode.com/
PHP | 399 lines | 296 code | 70 blank | 33 comment | 14 complexity | fa758861ce82c315d2a9fce4e39961fc MD5 | raw file
Possible License(s): AGPL-3.0
  1. <?php
  2. ob_start("ob_gzhandler");
  3. /*
  4. +--------------------------------------------------------------------------
  5. | NovaBoard
  6. | ========================================
  7. | By The NovaBoard team
  8. | Released under the Artistic License 2.0
  9. | http://www.novaboard.net
  10. | ======================================== |+--------------------------------------------------------------------------
  11. | uploader.php - script that does the uploading of files
  12. */
  13. define("NOVA_RUN", 1);
  14. // Best to include the config file..
  15. include "../includes/config.php";
  16. $my_address="http://".$_SERVER['HTTP_HOST']."".$_SERVER['PHP_SELF'];
  17. $nova_domain = str_replace('/uploads/uploader.php', '', $my_address); // returns http://myforum.com/forum style address
  18. global $db_prefix;
  19. include "../scripts/php/functions.php";
  20. $nova_name=$_COOKIE['nova_name'];
  21. $nova_password=$_COOKIE['nova_password'];
  22. $nova_name=escape_string($nova_name);
  23. $nova_password=escape_string($nova_password);
  24. // if the user is logged in, let's get their ID, and if that doesn't work, return an error....
  25. $query211 = "select ID, BANNED from {$db_prefix}members WHERE name='$nova_name' AND password='$nova_password'" ;
  26. $result211 = mysql_query($query211) or die("Query failed");
  27. $id_count = mysql_num_rows($result211);
  28. while ($results211 = mysql_fetch_array($result211)){
  29. $my_id = $results211['ID'];
  30. $banned = $results211['BANNED'];
  31. }
  32. if ($my_id < '1' OR $banned=='1' OR !isset($nova_name) OR $id_count!='1'){
  33. echo "You can't upload files without being logged in.";
  34. exit();
  35. }
  36. else{
  37. $query211 = "select THEME, ATTACH_IMG_SIZE, ATTACH_AVATAR_SIZE from {$db_prefix}settings" ;
  38. $result211 = mysql_query($query211) or die("Query failed") ;
  39. while ($results211 = mysql_fetch_array($result211)){
  40. $theme = $results211['THEME'];
  41. $attach_img_size = $results211['ATTACH_IMG_SIZE'];
  42. $attach_avatar_size = $results211['ATTACH_AVATAR_SIZE'];
  43. }
  44. echo "<html style='overflow: hidden; border: none;'>";
  45. echo "<head>";
  46. if ($theme!='novaboard_default'){
  47. echo "<link rel='stylesheet' href='$nova_domain/themes/novaboard_default/stylesheet.css' type='text/css'>";
  48. }
  49. echo "<link rel='stylesheet' href='$nova_domain/themes/$theme/stylesheet.css' type='text/css'>";
  50. echo "</head>";
  51. echo "<body style='overflow: hidden; margin-top: 0px;'>";
  52. echo "<div class='upload'>";
  53. // Right.. are they allowed to upload this file type?
  54. // Give it some values for max size for files...
  55. $attachtype = $_POST['attachtype'];
  56. $attachtype = escape_string($attachtype);
  57. if ($attachtype=='attachments'){
  58. $size=$attach_img_size;
  59. }
  60. elseif($attachtype=='avatar'){
  61. $size=$attach_avatar_size;
  62. }
  63. else{
  64. echo "Sorry, wrong attachment area.";
  65. exit();
  66. }
  67. $contenttype = $_FILES['uploadedfile']['type'];
  68. $file=$_FILES['uploadedfile']['name'];
  69. $parts = explode(".",$file);
  70. $ext = $parts[count($parts)-1];
  71. $contenttype = strtolower($ext);
  72. if ($contenttype=='zip'){
  73. if ($attachtype=='avatar'){
  74. $allowed="0";
  75. }
  76. else{
  77. $allowed="1";
  78. }
  79. $image="no";
  80. $findme = '.php.';
  81. $pos = strpos($file, $findme);
  82. if ($pos=== false){
  83. $allowed="1";
  84. }
  85. else{
  86. $allowed="0";
  87. }
  88. }
  89. elseif ($contenttype=='rar'){
  90. if ($attachtype=='avatar'){
  91. $allowed="0";
  92. }
  93. else{
  94. $allowed="1";
  95. }
  96. $image="no";
  97. $findme = '.php.';
  98. $pos = strpos($file, $findme);
  99. if ($pos=== false){
  100. $allowed="1";
  101. }
  102. else{
  103. $allowed="0";
  104. }
  105. }
  106. elseif ($contenttype=='png'){
  107. $findme = '.php.';
  108. $pos = strpos($file, $findme);
  109. if ($pos=== false){
  110. $allowed="1";
  111. }
  112. else{
  113. $allowed="0";
  114. }
  115. $image="yes";
  116. }
  117. elseif ($contenttype=='jpg'){
  118. $findme = '.php.';
  119. $pos = strpos($file, $findme);
  120. if ($pos=== false){
  121. $allowed="1";
  122. }
  123. else{
  124. $allowed="0";
  125. }
  126. $image="yes";
  127. }
  128. elseif ($contenttype=='jpeg'){
  129. $findme = '.php.';
  130. $pos = strpos($file, $findme);
  131. if ($pos=== false){
  132. $allowed="1";
  133. }
  134. else{
  135. $allowed="0";
  136. }
  137. $image="yes";
  138. }
  139. elseif ($contenttype=='gif'){
  140. $findme = '.php.';
  141. $pos = strpos($file, $findme);
  142. if ($pos=== false){
  143. $allowed="1";
  144. }
  145. else{
  146. $allowed="0";
  147. }
  148. $image="yes";
  149. }
  150. else{
  151. $allowed="0";
  152. }
  153. if ($allowed=='0'){
  154. exit("<br /><br />You are not allowed to upload files with this extension. Please upload a file with a valid extension");
  155. }
  156. else{
  157. // Where the file is going to be placed
  158. $target_path = "$attachtype/";
  159. $thumb_target = $target_path;
  160. // Y'know, we should rename the file so that it doesn't
  161. // clash with a file that is already uploaded.
  162. $file_name = $_FILES['uploadedfile']['name'];
  163. $file_name = strtolower($file_name);
  164. // remove apostrophe or Windows will take a fit
  165. $file_name = str_replace("'", "", $file_name);
  166. $memberid=$my_id;
  167. $hash=$_POST['hash'];
  168. $hash=escape_string($hash);
  169. $file_time = time();
  170. $new_file_name="$memberid-$file_time.$contenttype";
  171. /* Add the original filename to our target path.
  172. Result is "uploads/filename.extension" */
  173. $target_path = $target_path . basename( $new_file_name);
  174. $_FILES['uploadedfile']['tmp_name'];
  175. if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
  176. if ($image=='yes'){
  177. /*
  178. * let's start by identifying the image type. No doubt the more efficient way
  179. * is to use string functions but who cares?
  180. */
  181. $target_path=$thumb_target;
  182. $parts = explode('.', $new_file_name);
  183. $ext = $parts[count($parts)-1];
  184. $thumb_name = array_slice($parts,0,count($parts)-1);
  185. $ext = strtolower($ext);
  186. switch($ext)
  187. {
  188. case "jpg";
  189. $src_img=ImageCreateFromJpeg("$target_path/$new_file_name");
  190. $thumb_name = join(".",$thumb_name) . ".jpg";
  191. $thumb_name="t_$thumb_name";
  192. break;
  193. case "jpeg";
  194. $src_img=ImageCreateFromJpeg("$target_path/$new_file_name");
  195. $thumb_name = join(".",$thumb_name) . ".jpeg";
  196. $thumb_name="t_$thumb_name";
  197. break;
  198. case "gif":
  199. $src_img=ImageCreateFromGif("$target_path/$new_file_name");
  200. $thumb_name = join(".",$thumb_name) . ".gif";
  201. $thumb_name="t_$thumb_name";
  202. break;
  203. case "png":
  204. $src_img=ImageCreateFromPng("$target_path/$new_file_name");
  205. imagesavealpha($src_img,true) ;
  206. $thumb_name = join(".",$thumb_name) . ".png";
  207. $thumb_name="t_$thumb_name";
  208. break;
  209. }
  210. /* get it's height and width */
  211. $imgSx = imagesx($src_img);
  212. $imgSy = imagesy($src_img);
  213. if($imgSy != 0)
  214. {
  215. /*
  216. * lets calculate the aspect ratio and the height
  217. * and width for the scaled image.
  218. */
  219. $ratio = $imgSx/$imgSy;
  220. if (($imgSx - $size) <= '0'){
  221. $new_imgSx = $imgSx;
  222. $new_imgSy = $imgSy;
  223. }
  224. elseif($ratio > 1)
  225. {
  226. $new_imgSx = $size;
  227. $new_imgSy = $size/$ratio;
  228. }
  229. else
  230. {
  231. $new_imgSx = (float) $size * $ratio;
  232. $new_imgSy = $size;
  233. }
  234. $dst_img=imagecreatetruecolor($new_imgSx,$new_imgSy);
  235. imagealphablending($dst_img, false);
  236. /* create the scaled instance */
  237. ImageCopyResampled($dst_img,$src_img,0,0,0,0,$new_imgSx,$new_imgSy,$imgSx,$imgSy);
  238. imagesavealpha($dst_img, true);
  239. /* write the damned thing to disk */
  240. if($ext == "jpg" OR $ext=="jpeg")
  241. {
  242. imageJpeg($dst_img,"$target_path/$thumb_name");
  243. }
  244. elseif($ext == "gif")
  245. {
  246. imagecolortransparent($dst_img, black);
  247. imageGif($dst_img,"$target_path/$thumb_name");
  248. }
  249. else
  250. {
  251. imagePng($dst_img,"$target_path/$thumb_name");
  252. }
  253. }
  254. imagedestroy($src_img);
  255. imagedestroy($dst_img);
  256. }
  257. // Sneakily tell them the name of the original file so they can't try
  258. // and hotlink it...
  259. if ($attachtype=='avatar'){
  260. // delete previous avatar info in database
  261. // and delete the avatar itself
  262. $query2121 = "select FILENAME from {$db_prefix}attachments WHERE POSTID='0' AND MEMBER='$memberid'" ;
  263. $result2121 = mysql_query($query2121) or die("uploader.php - Error in query: $query2121") ;
  264. while ($results2121 = mysql_fetch_array($result2121)){
  265. $filename = $results2121['FILENAME'];
  266. foreach (glob("avatar/$filename") as $filename_original) {
  267. unlink($filename_original);
  268. }
  269. foreach (glob("avatar/t_$filename") as $filename_thumb) {
  270. unlink($filename_thumb);
  271. }
  272. mysql_query("DELETE FROM {$db_prefix}attachments WHERE filename ='$filename'");
  273. }
  274. }
  275. // Get the filesize...
  276. $filesize=$_FILES['uploadedfile']['size'];
  277. // And add the info to the database...
  278. mysql_query("INSERT INTO {$db_prefix}attachments (filename, filesize, original_filename, hash, member) VALUES ('$new_file_name', '$filesize', '$file_name', '$hash', '$my_id')");
  279. if ($attachtype=='avatar'){
  280. if (strpos($new_file_name, ".gif")!==false){
  281. $avatar="$new_file_name";
  282. }
  283. else{
  284. $avatar="t_$new_file_name";
  285. }
  286. $avatar="uploads/avatar/$avatar";
  287. mysql_query("UPDATE {$db_prefix}members SET avatar='$avatar', remote_avatar='0' WHERE id = '$memberid'");
  288. }
  289. // MESSAGE IN HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  290. header("HTTP/1.0 200 OK");
  291. header("Location: $nova_domain/uploads/upload.php?topicid=$topicid&attachtype=$attachtype&member=$my_id&hash=$hash");
  292. exit;
  293. } else{
  294. echo "There was an error uploading the file, please try again!";
  295. }
  296. }
  297. echo "</div>";
  298. echo "</body>";
  299. echo "</html>";
  300. mysql_close();
  301. ob_flush();
  302. }
  303. ?>