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

/ales/upload/savefiles.php

http://cartonbank.googlecode.com/
PHP | 574 lines | 396 code | 128 blank | 50 comment | 60 complexity | 4abe92f9a3e12dd752d1412700f226a3 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, LGPL-2.1, AGPL-1.0, LGPL-3.0
  1. <?php
  2. include ('/home/www/cb3/wp-includes/functions.php');
  3. //include ('/home/www/cb3/wp-includes/load.php');
  4. //include ('/home/www/cb3/wp-includes/wp-db.php');
  5. include("/home/www/cb/ales/config.php"); //todo
  6. $link = mysql_connect($mysql_hostname, $mysql_user, $mysql_password);
  7. mysql_set_charset('utf8',$link);
  8. //savefiles
  9. if (isset($_FILES) && isset($_POST)){
  10. foreach ($_FILES as $key => $_file) {
  11. //foreach ($_file as $vkey => $vvalue) {
  12. $fileid = savefiles($_file);
  13. //fill_product_list($fileid);
  14. //}
  15. }
  16. }
  17. // files
  18. if (isset($_FILES))
  19. $result = $_FILES;
  20. else
  21. $result = "no files received";
  22. fww($result);
  23. // forms
  24. if (isset($_POST))
  25. $result = $_POST;
  26. else
  27. $result = "no form fields received";
  28. fw($result);
  29. function fww($text)
  30. {
  31. $fp = fopen('_kloplog.txt', 'a') or die('Could not open file!');
  32. fwrite($fp, "\n====FILES====\n") or die('Could not write to file');
  33. foreach ($text as $key => $value) {
  34. foreach ($value as $vkey => $vvalue) {
  35. $toFile = " $vkey = $vvalue \n";
  36. fwrite($fp, $toFile) or die('Could not write to file');
  37. }
  38. fwrite($fp, "\n") or die('Could not write to file');
  39. }
  40. fclose($fp);
  41. }
  42. function fw($text)
  43. {
  44. $fp = fopen('_kloplog.txt', 'a') or die('Could not open file!');
  45. fwrite($fp, "-----POST----\n") or die('Could not write to file');
  46. foreach ($text as $key => $value) {
  47. $toFile = " $key = $value \n";
  48. fwrite($fp, $toFile) or die('Could not write to file');
  49. }
  50. fclose($fp);
  51. }
  52. function fw1($text)
  53. {
  54. $fp = fopen('_kloplog.txt', 'a') or die('Could not open file!');
  55. fwrite($fp, "\n-----POST[carname]----\n") or die('Could not write to file');
  56. fwrite($fp, $text) or die('Could not write to file');
  57. fclose($fp);
  58. }
  59. function savefiles($_file){
  60. // add product
  61. if(isset($_POST['submit_action']) && $_POST['submit_action'] == 'add') {
  62. if($_file != null) {
  63. $basepath = str_replace("/ales/upload", "" , getcwd());
  64. $imagedir = $basepath."/wp-content/plugins/wp-shopping-cart/images/";
  65. $product_images = $basepath."/wp-content/plugins/wp-shopping-cart/product_images/";
  66. $filedir = $basepath."/wp-content/plugins/wp-shopping-cart/files/";
  67. //upload_and_resize_and_watermark_images();
  68. $t = $_FILES['my-pic'];
  69. /* read data (binary) */
  70. $ifp = fopen( $t['tmp_name'], "rb" );
  71. $imageData = fread( $ifp, filesize( $t['tmp_name'] ) );
  72. fclose( $ifp );
  73. /* encode & write data (binary) */
  74. $ifp = fopen( $t['tmp_name'], "wb" );
  75. fwrite( $ifp, base64_decode( $imageData ) );
  76. fclose( $ifp );
  77. //transliterate file
  78. $_FILES['my-pic']['name'] = rus2translit($_FILES['my-pic']['name']);
  79. //rename the file
  80. $_FILES['my-pic']['name'] = uniqid('', true).$_FILES['my-pic']['name'];
  81. //ales default upload
  82. if(!is_dir($product_images))
  83. {
  84. mkdir($product_images);
  85. }
  86. if(function_exists("getimagesize"))
  87. {
  88. $height = 140;
  89. $width = 140;
  90. copy($_FILES['my-pic']['tmp_name'], $product_images.$_FILES['my-pic']['name']);
  91. copy($_FILES['my-pic']['tmp_name'], $imagedir.$_FILES['my-pic']['name']);
  92. chmod($product_images.$_FILES['my-pic']['name'], 0666);
  93. $imgsize = getimagesize($product_images.$_FILES['my-pic']['name']);
  94. $file_w = $imgsize[0];
  95. $file_h = $imgsize[1];
  96. //ales here we replace slides to that from LG
  97. $chwidth = 600; //get_option('lg_pictwidth'); // crop size
  98. $chheight = 600; //get_option('lg_pictheight'); // crop size
  99. $thatdir = $product_images; //destination dir
  100. $ifolder = ''; //subfolder for artist
  101. $file = $_FILES['my-pic']['name']; //
  102. $resample_quality = 100; //image quality
  103. ales_create_cropped_file($chwidth, $chheight, $thatdir, $ifolder, $file, $resample_quality);
  104. $wm = $basepath."/img/watermark.png";
  105. wtrmark($thatdir.$file,$wm);
  106. // ales here we replace thumbs to that from LG
  107. $chwidth = $width; // crop size
  108. $chheight = $height; // crop size
  109. $thatdir = $imagedir; //destination dir
  110. al_create_cropped_file($chwidth, $chheight, $thatdir, $ifolder, $file, $resample_quality);
  111. $image = $_FILES['my-pic']['name'];
  112. /// ales
  113. }
  114. else {
  115. move_uploaded_file($_FILES['my-pic']['tmp_name'], ($imagedir.$_FILES['my-pic']['name']));
  116. $image = $_FILES['my-pic']['name'];
  117. }
  118. ///ales
  119. $timestamp = time();
  120. $insert_sql = "INSERT INTO `wp_product_files` ( `id` , `filename` , `mimetype` , `idhash` , `date` , `width`, `height`) VALUES ( '' , '', '', '', '$timestamp', '', '');";
  121. if (!($result = mysql_query($insert_sql))) {die('Invalid query: ' . mysql_error());}
  122. $sql = "SELECT `id` FROM `wp_product_files` WHERE `date` = '$timestamp'";
  123. if (!($result = mysql_query($sql))) {die('Invalid query: ' . mysql_error());}
  124. $fileid = mysql_fetch_row($result);
  125. $fileid = $fileid[0];
  126. $idhash = sha1($fileid);
  127. $mimetype = $_FILES['my-pic']['type'];
  128. $splitname = explode(".",$_FILES['my-pic']['name']);
  129. $splitname = array_reverse($splitname);
  130. $filename = $_FILES['my-pic']['name'];
  131. if(move_uploaded_file($_FILES['my-pic']['tmp_name'],($filedir.$idhash)))
  132. {
  133. $update_sql = "UPDATE `wp_product_files` SET `filename` = '".$filename."', `mimetype` = '$mimetype', `idhash` = '$idhash', `width` = '$file_w', `height` = '$file_h' WHERE `id` = '$fileid' LIMIT 1;";
  134. if (!($result = mysql_query($update_sql))) {die('Invalid query: ' . mysql_error());}
  135. //$wpdb->query("UPDATE `wp_product_files` SET `filename` = '".$filename."', `mimetype` = '$mimetype', `idhash` = '$idhash', `width` = '$file_w', `height` = '$file_h' WHERE `id` = '$fileid' LIMIT 1");
  136. }
  137. $file = $fileid;
  138. }
  139. }
  140. // add line to productlist table:
  141. $l1_price = 250;
  142. $l2_price = 500;
  143. $l3_price = 2500;
  144. $not_for_sale = 0;
  145. $display_frontpage = 1;
  146. $visible = 1;
  147. $user_brand = 8; //todo
  148. $image = $filename;
  149. if (isset($_POST['colored']) && $_POST['colored']=='on'){
  150. $colored = 1;
  151. }
  152. else {
  153. $colored = 0;
  154. }
  155. if (isset($_POST['carcategory']) && is_numeric($_POST['carcategory'])){
  156. $category_id = $_POST['carcategory'];
  157. }
  158. else {
  159. $category_id = 5;//cartoon
  160. }
  161. if (isset($_POST['tema']) && $_POST['tema']=='undefined'){
  162. $temadnya = 0;
  163. }
  164. else {
  165. $temadnya = 1;
  166. }
  167. if (isset($_POST['brand']) && is_numeric($_POST['brand']))
  168. {$_brand = mysql_real_escape_string($_POST['brand']);}
  169. else {$_brand = trim($user_brand);}
  170. $insertsql = "INSERT INTO `wp_product_list` ( `id`, `name`, `description`, `additional_description`, `file` , `image` , `category`, `brand`, `display_frontpage`, `visible`, `approved`, `color`, `not_for_sale`, `l1_price`, `l2_price`, `l3_price`) VALUES ('', '".removeCrLf(htmlspecialchars($_POST['carname']))."', '".removeCrLf(htmlspecialchars($_POST['cardescription']))."', '".correct_comma(removeCrLf(htmlspecialchars($_POST['cartags'])))."','".$fileid."', '".$image."', '".$category_id."', '".$_brand."', '$display_frontpage', '$visible', NULL, '$colored', '$not_for_sale', $l1_price, $l2_price, $l3_price);";
  171. if (!($result = mysql_query($insertsql))) {die('Invalid query: ' . mysql_error());}
  172. $new_id = mysql_insert_id();
  173. // add to purgatory
  174. $sql_purgery = "insert into al_editors_votes (image_id, up, down) values ('".$new_id."','0','0')";
  175. if (!($result = mysql_query($sql_purgery))) {die('Invalid query: ' . mysql_error());}
  176. // insert temadnya
  177. if ($temadnya == '1') // insert category 777
  178. {
  179. $sql_temadnya = "insert into `wp_item_category_associations` (product_id, category_id) values ('".$new_id."','777')";
  180. if (!($result = mysql_query($sql_temadnya))) {die('Invalid query: ' . mysql_error());}
  181. }
  182. return $fileid;
  183. }
  184. function ales_create_cropped_file($chwidth, $chheight, $thatdir, $ifolder, $file, $resample_quality = '100') {
  185. $img_location = $thatdir.$file;
  186. // Getting width ([0]) and height ([1]) maybe add options
  187. $size_bits = getimagesize($img_location);
  188. // Creating a resource image
  189. $path = pathinfo($img_location);
  190. switch(strtolower($path["extension"])){
  191. case "jpeg":
  192. case "jpg":
  193. $img = imagecreatefromjpeg($img_location);
  194. break;
  195. case "gif":
  196. $img = imagecreatefromgif($img_location);
  197. break;
  198. case "png":
  199. $img = imagecreatefrompng($img_location);
  200. break;
  201. default:
  202. break;
  203. }
  204. if($size_bits[0] > $chwidth || $size_bits[1] > $chheight) {
  205. // Resize the image
  206. $resized = imagecreatetruecolor($chwidth, $chheight);
  207. $o_width = $size_bits[0];
  208. $o_height = $size_bits[1];
  209. // if the image is more wide than high
  210. if($o_width > $o_height) {
  211. // landscape image
  212. $out_width = $o_height;
  213. $out_height = $o_height;
  214. $cutoff = round(($o_width - $o_height) / 2);
  215. $out_left = $cutoff;
  216. $out_top = 0;
  217. } else {
  218. $cutoff = round(($o_height - $o_width) / 2);
  219. $out_width = $o_width;
  220. $out_height = $o_width;
  221. $out_left = 0;
  222. $out_top = $cutoff;
  223. }
  224. // Resampling the image
  225. imagecopyresampled ($resized, $img, 0, 0, $out_left, $out_top, $chwidth, $chheight, $out_width, $out_height);
  226. if (is_writable($thatdir.$ifolder)){
  227. switch(strtolower($path["extension"])){
  228. case "jpeg":
  229. case "jpg":
  230. imagejpeg($resized, $thatdir.$ifolder.'/'.$file, $resample_quality);
  231. break;
  232. case "gif":
  233. imagegif($resized, $thatdir.$ifolder.'/'.$file);
  234. break;
  235. case "png":
  236. imagepng($resized, $thatdir.$ifolder.'/'.$file);
  237. break;
  238. default:
  239. break;
  240. }
  241. } else {
  242. echo "<div class='error'><b>WARNING:</b> Unable to create $ifolder inside $thatdir. <br />";
  243. echo "Check your permissions.</div><br />";
  244. }
  245. imagedestroy($resized);
  246. } else {
  247. switch(strtolower($path["extension"])){
  248. case "jpeg":
  249. case "jpg":
  250. imagejpeg($img, $thatdir.$ifolder.'/'.$file, $resample_quality);
  251. break;
  252. case "gif":
  253. imagegif($img, $thatdir.$ifolder.'/'.$file);
  254. break;
  255. case "png":
  256. imagepng($img, $thatdir.$ifolder.'/'.$file);
  257. break;
  258. default:
  259. break;
  260. }
  261. }
  262. }
  263. function wtrmark($sourcefile, $watermarkfile) {
  264. $logopath = "/home/www/cb3/img/cb-logo-300.png";
  265. $logofile_id = imagecreatefrompng($logopath);
  266. imageAlphaBlending($logofile_id, true);
  267. imageSaveAlpha($logofile_id, true);
  268. $fileType = strtolower(substr($sourcefile, strlen($sourcefile)-3));
  269. switch($fileType) {
  270. case('gif'):
  271. $sourcefile_id = imagecreatefromgif($sourcefile);
  272. break;
  273. case('png'):
  274. $sourcefile_id = imagecreatefrompng($sourcefile);
  275. break;
  276. default:
  277. $sourcefile_id = imagecreatefromjpeg($sourcefile);
  278. }
  279. imageAlphaBlending($sourcefile_id, true);
  280. imageSaveAlpha($sourcefile_id, true);
  281. //Get the sizes of both pix
  282. $sourcefile_width=imageSX($sourcefile_id);
  283. $sourcefile_height=imageSY($sourcefile_id);
  284. $logo_width=imageSX($logofile_id);
  285. $logo_height=imageSY($logofile_id);
  286. $dest_x_logo = $sourcefile_width - $logo_width - 4;
  287. $dest_y_logo = $sourcefile_height - $logo_height - 8;
  288. // if a gif, we have to upsample it to a truecolor image
  289. if($fileType == 'gif') {
  290. // create an empty truecolor container
  291. $tempimage = imagecreatetruecolor($sourcefile_width,$sourcefile_height);
  292. // copy the 8-bit gif into the truecolor image
  293. imagecopy($tempimage, $sourcefile_id, 0, 0, 0, 0,
  294. $sourcefile_width, $sourcefile_height);
  295. // copy the source_id int
  296. $sourcefile_id = $tempimage;
  297. }
  298. // create an empty truecolor container
  299. $tempimage = imagecreatetruecolor($sourcefile_width+20,$sourcefile_height);
  300. $bgColor = imagecolorallocate($tempimage, 255,255,255);
  301. imagefill($tempimage , 0,0 , $bgColor);
  302. // copy the 8-bit gif into the truecolor image
  303. imagecopy($tempimage, $sourcefile_id, 0, 0, 0, 0,
  304. $sourcefile_width, $sourcefile_height);
  305. // copy the source_id int
  306. $sourcefile_id = $tempimage;
  307. //text
  308. $black = ImageColorAllocate($sourcefile_id, 200, 200, 200);
  309. $white = ImageColorAllocate($sourcefile_id, 255, 255, 255);
  310. //The canvas's (0,0) position is the upper left corner
  311. //So this is how far down and to the right the text should start
  312. $start_x = $sourcefile_width;
  313. $start_y = $sourcefile_height;
  314. // write text
  315. Imagettftext($sourcefile_id, 10, 90, $sourcefile_width+12, $sourcefile_height-4, $black, '/home/www/cb3/ales/arial.ttf', $text);
  316. $opacity_logo = 5;
  317. ImageCopyMerge($sourcefile_id, $logofile_id, $dest_x_logo, $dest_y_logo, 0, 0, $logo_width, $logo_height, $opacity_logo);
  318. //Create a jpeg out of the modified picture
  319. switch('jpg') {
  320. // remember we don't need gif any more, so we use only png or jpeg.
  321. // See the upsaple code immediately above to see how we handle gifs
  322. case('png'):
  323. imagepng ($sourcefile_id,$sourcefile);
  324. break;
  325. default:
  326. imagejpeg ($sourcefile_id,$sourcefile);
  327. }
  328. imagedestroy($sourcefile_id);
  329. imagedestroy($logofile_id);
  330. }
  331. function al_create_cropped_file($chwidth, $chheight, $thatdir, $ifolder, $file, $resample_quality = '100') {
  332. $img_location = $thatdir.$file;
  333. // Getting width ([0]) and height ([1]) maybe add options
  334. $size_bits = getimagesize($img_location);
  335. // Creating a resource image
  336. $path = pathinfo($img_location);
  337. switch(strtolower($path["extension"])){
  338. case "jpeg":
  339. case "jpg":
  340. $img = imagecreatefromjpeg($img_location);
  341. break;
  342. case "gif":
  343. $img = imagecreatefromgif($img_location);
  344. break;
  345. case "png":
  346. $img = imagecreatefrompng($img_location);
  347. break;
  348. default:
  349. break;
  350. }
  351. if($size_bits[0] > $chwidth || $size_bits[1] > $chheight) {
  352. // Resize the image
  353. $resized = imagecreatetruecolor($chwidth, $chheight);
  354. $o_width = $size_bits[0];
  355. $o_height = $size_bits[1];
  356. // if the image is more wide than high
  357. if($o_width > $o_height) {
  358. // landscape image
  359. $out_width = $o_height;
  360. $out_height = $o_height;
  361. $cutoff = round(($o_width - $o_height) / 2);
  362. $out_left = $cutoff;
  363. $out_top = 0;
  364. } else {
  365. $cutoff = round(($o_height - $o_width) / 2);
  366. $out_width = $o_width;
  367. $out_height = $o_width;
  368. $out_left = 0;
  369. $out_top = $cutoff;
  370. }
  371. // Resampling the image
  372. imagecopyresampled ($resized, $img, 0, 0, $out_left, $out_top, $chwidth, $chheight, $out_width, $out_height);
  373. if (is_writable($thatdir.$ifolder)){
  374. switch(strtolower($path["extension"])){
  375. case "jpeg":
  376. case "jpg":
  377. imagejpeg($resized, $thatdir.$ifolder.'/'.$file, $resample_quality);
  378. break;
  379. case "gif":
  380. imagegif($resized, $thatdir.$ifolder.'/'.$file);
  381. break;
  382. case "png":
  383. imagepng($resized, $thatdir.$ifolder.'/'.$file);
  384. break;
  385. default:
  386. break;
  387. }
  388. } else {
  389. echo "<div class='error'><b>WARNING:</b> Unable to create $ifolder inside $thatdir. <br />";
  390. echo "Check your permissions.</div><br />";
  391. }
  392. imagedestroy($resized);
  393. } else {
  394. switch(strtolower($path["extension"])){
  395. case "jpeg":
  396. case "jpg":
  397. imagejpeg($img, $thatdir.$ifolder.'/'.$file, $resample_quality);
  398. break;
  399. case "gif":
  400. imagegif($img, $thatdir.$ifolder.'/'.$file);
  401. break;
  402. case "png":
  403. imagepng($img, $thatdir.$ifolder.'/'.$file);
  404. break;
  405. default:
  406. break;
  407. }
  408. }
  409. }
  410. function fill_product_list ($fileid) {
  411. $l1_price = 250;
  412. $l2_price = 500;
  413. $l3_price = 2500;
  414. $not_for_sale = 0;
  415. $display_frontpage = 1;
  416. $visible = 1;
  417. $user_brand = 8; //todo
  418. $image = '';
  419. if (isset($_POST['colored']) && $_POST['colored']=='on'){
  420. $colored = 1;
  421. }
  422. else {
  423. $colored=0;
  424. }
  425. if (isset($_POST['carcategory']) && is_numeric($_POST['carcategory'])){
  426. $category_id = $_POST['carcategory'];
  427. }
  428. else {
  429. $category_id=5;//cartoon
  430. }
  431. if (isset($_POST['brand']) && is_numeric($_POST['brand']))
  432. {$_brand = mysql_real_escape_string($_POST['brand']);}
  433. else {$_brand = trim($user_brand);}
  434. $insertsql = "INSERT INTO `wp_product_list` ( `id`, `name`, `description`, `additional_description`, `file` , `image` , `category`, `brand`, `display_frontpage`, `visible`, `approved`, `color`, `not_for_sale`, `l1_price`, `l2_price`, `l3_price`) VALUES ('', '".removeCrLf(htmlspecialchars($_POST['carname']))."', '".removeCrLf(htmlspecialchars($_POST['cardescription']))."', '".correct_comma(removeCrLf(htmlspecialchars($_POST['cartags'])))."','".$fileid."', '".$image."', '".$category_id."', '".$_brand."', '$display_frontpage', '$visible', NULL, '$colored', '$not_for_sale', $l1_price, $l2_price, $l3_price);";
  435. if (!($result = mysql_query($insertsql))) {die('Invalid query: ' . mysql_error());}
  436. $new_id = mysql_insert_id();
  437. $sql_purgery = "insert into al_editors_votes (image_id, up, down) values ('".$new_id."','0','0')";
  438. if (!($result = mysql_query($sql_purgery))) {die('Invalid query: ' . mysql_error());}
  439. }
  440. function correct_comma($string)
  441. {
  442. $string = str_replace(",", ", " , $string);
  443. $string = str_replace(", ", ", " , $string);
  444. return $string;
  445. }
  446. ?>