PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/ales/anekdotru.php

http://cartonbank.googlecode.com/
PHP | 393 lines | 267 code | 85 blank | 41 comment | 14 complexity | f7648d37c45a3428a430a0883f2406db MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, LGPL-2.1, AGPL-1.0, LGPL-3.0
  1. <?php
  2. //
  3. // send top 7($howmanyemails?) rating images to anekdot.ru
  4. //
  5. // configuration
  6. $howmanyrows = 100; // how many rows to select from database
  7. $howmanyemails = 8; // how many images to send (plus ignored artists)
  8. $mailto = "igor.aleshin@gmail.com"; // destination email box
  9. //$mailto = "cartoonbank.ru@gmail.com"; // destination email box
  10. include("config.php");
  11. $link = mysql_connect($mysql_hostname, $mysql_user, $mysql_password);
  12. mysql_set_charset('utf8',$link);
  13. $sql = "SELECT post as ID,
  14. wp_product_list.image as image,
  15. wp_product_list.name AS title,
  16. wp_product_brands.name AS author,
  17. COUNT(*) AS votes,
  18. SUM(wp_fsr_user.points) AS points,
  19. AVG(wp_fsr_user.points)*SQRT(COUNT(*)) AS average,
  20. wp_product_files.idhash,
  21. wp_product_list.brand AS brand,
  22. wp_product_files.mimetype
  23. FROM wp_fsr_user, wp_fsr_post, wp_product_list, wp_product_brands, wp_product_files
  24. WHERE wp_fsr_user.post = wp_product_list.id
  25. AND wp_fsr_user.post = wp_fsr_post.ID
  26. AND wp_product_list.file = wp_product_files.id
  27. AND wp_product_list.brand = wp_product_brands.id
  28. AND wp_product_list.active = 1
  29. AND wp_product_list.visible = 1
  30. AND wp_fsr_post.anekdotru_date is NULL
  31. GROUP BY 1
  32. ORDER BY 7 DESC, 5 DESC
  33. LIMIT ".$howmanyrows;
  34. $result = mysql_query("$sql");
  35. if (!$result) {die('Invalid query: ' . mysql_error());}
  36. //
  37. $count=mysql_num_rows($result);
  38. //
  39. $arrAuthors = array('?????????? ???????');
  40. while($row=mysql_fetch_array($result))
  41. {
  42. $ID = $row['ID'];
  43. $image = $row['image'];
  44. $title = $row['title'];
  45. $author = $row['author'];
  46. $brand = $row['brand'];
  47. $votes = $row['votes'];
  48. $points = $row['points'];
  49. $average = $row['average'];
  50. $vote_date = $row['vote_date'];
  51. $idhash = $row['idhash'];
  52. $mimetype = $row['mimetype'];
  53. $extension = '';
  54. // get extension of the source file
  55. $fileType = strtolower(substr($mimetype, strlen($mimetype)-3));
  56. switch($fileType) {
  57. case('gif'):
  58. $extension = 'gif';
  59. break;
  60. case('png'):
  61. $extension = 'png';
  62. break;
  63. default:
  64. $extension = 'jpg';
  65. }
  66. // echo the name of the image
  67. if (!in_array($author, $arrAuthors))
  68. {
  69. echo "<br /><br /><font color='#FF00FF'><b>".count($arrAuthors).":</b> </font>".$ID." <img src='http://cartoonbank.ru/wp-content/plugins/wp-shopping-cart/images/".$image."' width='40'> <b>&quot;".$title."&quot;</b> ".$author." ????: ".$average."<br />";
  70. array_push($arrAuthors, $author);
  71. //image
  72. $filename = "/home/www/cb3/wp-content/plugins/wp-shopping-cart/product_images/".$image;
  73. //resize the image
  74. $chwidth=500;
  75. $chheight=500;
  76. $thatdir='';
  77. $ifolder='';
  78. $file = $filename.'.'.$extension;
  79. $idhash_path = "/home/www/cb3/wp-content/plugins/wp-shopping-cart/files/".$idhash;
  80. $product_images='';
  81. $slidename = $author.'_'.$idhash.'.'.$extension;
  82. $thumb='';
  83. $resample_quality=100;
  84. //create resized image
  85. al_create_resized_file($chwidth, $chheight, $thatdir, $ifolder, $file, $idhash_path, $product_images, $slidename, $thumb, $resample_quality = '100');
  86. // Add logo
  87. $export_dir = "/home/www/cb3/ales/";
  88. if(file_exists($export_dir.$slidename))
  89. {
  90. wtrmark($export_dir.$slidename,$wm,$author.' Š cartoonbank.ru');
  91. //echo "\n\r>>>> watermarked";
  92. }
  93. //send email
  94. // To send HTML mail, the Content-type header must be set
  95. $headers = 'MIME-Version: 1.0' . "\r\n";
  96. $headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
  97. $headers .= 'From: CartoonBank Robot <cartoonbank.ru@gmail.com>' . "\r\n";
  98. //email content
  99. $content = "?????: ".$author."\n\r";
  100. $content .= "????????: ".$title."\n\r";
  101. $content .= "??????: http://cartoonbank.ru/?page_id=29&brand=".$brand."\n\r";
  102. $content .= "??? ?????? ?? ???????? ??????: <a href='http://cartoonbank.ru/?page_id=29&brand=".$brand."'>".$author."</a>\n\r";
  103. $my_file = $slidename;
  104. $my_path = $_SERVER['DOCUMENT_ROOT']."ales/";
  105. //echo "<br />my_path: ".$my_path."<br />";
  106. $my_name = "cartoonbank";
  107. $my_mail = "cartoonbank.ru@gmail.com";
  108. $my_replyto = "cartoonbank.ru@gmail.com";
  109. $my_subject = "?????????? ?????????? ??? ????????.?? ?? ???????????.??";
  110. $my_message = $content;
  111. //send email 2
  112. mail_attachment($my_file, $my_path, $mailto, $my_mail, $my_name, $my_replyto, $my_subject, $my_message);
  113. $count=$count-1;
  114. if(file_exists($export_dir.$slidename))
  115. {
  116. unlink($export_dir.$slidename);
  117. //echo "\n\r>>>> slide removed";
  118. }
  119. // Mark image as sent to the Anekdot.ru
  120. $update_sql = "update wp_fsr_post set anekdotru_date='".date("d.m.y H:m:s")."' where ID=".$ID;
  121. $res = mysql_query($update_sql);
  122. if (!$res) {die('<br />'.$update_sql.'<br />Invalid delete query: ' . mysql_error());}
  123. }
  124. if (count($arrAuthors) >= $howmanyemails)
  125. {
  126. pokazh($arrAuthors);
  127. exit;
  128. }
  129. }
  130. function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
  131. $file = $path.$filename;
  132. $file_size = filesize($file);
  133. $handle = fopen($file, "r");
  134. $content = fread($handle, $file_size);
  135. fclose($handle);
  136. $content = chunk_split(base64_encode($content));
  137. $uid = md5(uniqid(time()));
  138. $name = basename($file);
  139. $header = "From: ".$from_name." <".$from_mail.">\r\n";
  140. $header .= "Reply-To: ".$replyto."\r\n";
  141. $header .= "MIME-Version: 1.0\r\n";
  142. $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
  143. $header .= "This is a multi-part message in MIME format.\r\n";
  144. $header .= "--".$uid."\r\n";
  145. $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
  146. $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
  147. $header .= $message."\r\n\r\n";
  148. $header .= "--".$uid."\r\n";
  149. $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
  150. $header .= "Content-Transfer-Encoding: base64\r\n";
  151. $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
  152. $header .= $content."\r\n\r\n";
  153. $header .= "--".$uid."--";
  154. if (mail($mailto, $subject, "", $header)) {
  155. echo "mail send ... OK"; // or use booleans here
  156. } else {
  157. echo "mail send ... ERROR!";
  158. }
  159. }
  160. function wtrmark($sourcefile, $watermarkfile, $text) {
  161. $logopath = "/home/www/cb3/img/cb-logo-300.png";
  162. $logofile_id = imagecreatefrompng($logopath);
  163. imageAlphaBlending($logofile_id, true);
  164. imageSaveAlpha($logofile_id, true);
  165. $fileType = strtolower(substr($sourcefile, strlen($sourcefile)-3));
  166. switch($fileType) {
  167. case('gif'):
  168. $sourcefile_id = imagecreatefromgif($sourcefile);
  169. break;
  170. case('png'):
  171. $sourcefile_id = imagecreatefrompng($sourcefile);
  172. break;
  173. default:
  174. $sourcefile_id = imagecreatefromjpeg($sourcefile);
  175. }
  176. imageAlphaBlending($sourcefile_id, true);
  177. imageSaveAlpha($sourcefile_id, true);
  178. //Get the sizes of both pix
  179. $sourcefile_width=imageSX($sourcefile_id);
  180. $sourcefile_height=imageSY($sourcefile_id);
  181. $logo_width=imageSX($logofile_id);
  182. $logo_height=imageSY($logofile_id);
  183. $dest_x_logo = $sourcefile_width - $logo_width - 4;
  184. $dest_y_logo = $sourcefile_height - $logo_height - 8;
  185. // if a gif, we have to upsample it to a truecolor image
  186. if($fileType == 'gif') {
  187. // create an empty truecolor container
  188. $tempimage = imagecreatetruecolor($sourcefile_width,$sourcefile_height);
  189. // copy the 8-bit gif into the truecolor image
  190. imagecopy($tempimage, $sourcefile_id, 0, 0, 0, 0,
  191. $sourcefile_width, $sourcefile_height);
  192. // copy the source_id int
  193. $sourcefile_id = $tempimage;
  194. }
  195. // create an empty truecolor container
  196. $tempimage = imagecreatetruecolor($sourcefile_width+20,$sourcefile_height);
  197. $bgColor = imagecolorallocate($tempimage, 255,255,255);
  198. imagefill($tempimage , 0,0 , $bgColor);
  199. // copy the 8-bit gif into the truecolor image
  200. imagecopy($tempimage, $sourcefile_id, 0, 0, 0, 0,
  201. $sourcefile_width, $sourcefile_height);
  202. // copy the source_id int
  203. $sourcefile_id = $tempimage;
  204. //text
  205. $black = ImageColorAllocate($sourcefile_id, 200, 200, 200);
  206. $white = ImageColorAllocate($sourcefile_id, 255, 255, 255);
  207. //The canvas's (0,0) position is the upper left corner
  208. //So this is how far down and to the right the text should start
  209. $start_x = $sourcefile_width;
  210. $start_y = $sourcefile_height;
  211. // write text
  212. Imagettftext($sourcefile_id, 10, 90, $sourcefile_width+11, $sourcefile_height, $black, '/home/www/cb3/ales/arial.ttf', $text);
  213. $opacity_logo = 30;
  214. ImageCopyMerge($sourcefile_id, $logofile_id, $dest_x_logo, $dest_y_logo, 0, 0, $logo_width, $logo_height, $opacity_logo);
  215. //Create a jpeg out of the modified picture
  216. switch($fileType) {
  217. // remember we don't need gif any more, so we use only png or jpeg.
  218. // See the upsaple code immediately above to see how we handle gifs
  219. case('png'):
  220. imagepng ($sourcefile_id,$sourcefile);
  221. break;
  222. default:
  223. imagejpeg ($sourcefile_id,$sourcefile);
  224. }
  225. imagedestroy($sourcefile_id);
  226. imagedestroy($logofile_id);
  227. }
  228. function al_create_resized_file($chwidth, $chheight, $thatdir, $ifolder, $file, $idhash_path, $product_images, $slidename, $thumb, $resample_quality = '100') {
  229. // Default thumbs creation
  230. $img_location = $file;
  231. $export_dir = "/home/www/cb3/ales/";
  232. // Creating a resource image
  233. $path = pathinfo($img_location);
  234. switch(strtolower($path["extension"])){
  235. case "pjpeg":
  236. case "jpeg":
  237. case "jpg":
  238. try
  239. {
  240. $img = imagecreatefromjpeg($idhash_path);
  241. break;
  242. }
  243. catch(Exception $e)
  244. {
  245. echo ("\n" .$idhash_path. " bad file");
  246. echo ("\n exception: " .$e);
  247. break;
  248. }
  249. case "gif":
  250. $img = imagecreatefromgif($idhash_path);
  251. break;
  252. case "png":
  253. $img = imagecreatefrompng($idhash_path);
  254. break;
  255. default:
  256. break;
  257. }
  258. $xratio = $chheight/(imagesx($img));
  259. $yratio = $chwidth/(imagesy($img));
  260. if($xratio < 1 || $yratio < 1)
  261. {
  262. if($xratio < $yratio)
  263. $resized = imagecreatetruecolor($chwidth,floor(imagesy($img)*$xratio));
  264. else
  265. $resized = imagecreatetruecolor(floor(imagesx($img)*$yratio), $chheight);
  266. imagecopyresampled($resized, $img, 0, 0, 0, 0, imagesx($resized)+1,imagesy($resized)+1,imagesx($img),imagesy($img));
  267. //echo "\n ..strtolower(path[extension]):".strtolower($path["extension"]);
  268. switch(strtolower($path["extension"])){
  269. case "jpeg":
  270. case "pjpeg":
  271. case "jpg":
  272. //echo "\n\r Jpg: copy resized image to ".$export_dir.$slidename;
  273. imagejpeg($resized, $export_dir.$slidename, $resample_quality);
  274. break;
  275. case "gif":
  276. //echo "\n\r Gif: copy resized image to ".$export_dir.$slidename;
  277. imagegif($resized, $export_dir.$slidename);
  278. break;
  279. case "png":
  280. imagepng($resized, $export_dir.$slidename);
  281. break;
  282. default:
  283. break;
  284. }
  285. imagedestroy($resized);
  286. imagedestroy($img);
  287. }
  288. else
  289. {
  290. echo "\n !!!!shit";
  291. switch(strtolower($path["extension"])){
  292. case "jpeg":
  293. case "jpg":
  294. imagejpeg($img, $export_dir.$slidename, $resample_quality);
  295. break;
  296. case "gif":
  297. imagegif($img, $export_dir.$slidename);
  298. break;
  299. case "png":
  300. imagepng($img, $export_dir.$slidename);
  301. break;
  302. default:
  303. break;
  304. }
  305. imagedestroy($img);
  306. }
  307. }
  308. ?>