Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.91 KB | None | 0 0
  1. remove_action('wp_ajax_pinc-upload-pin', 'pinc_upload_pin');
  2.  
  3. function pinc_upload_pin_new(){
  4. check_ajax_referer('upload_pin', 'ajax-nonce');
  5.  
  6. do_action('pinc_before_upload_pin', $_POST);
  7.  
  8. $minWidth = 2;
  9. $minHeight = 2;
  10.  
  11. $minWidth = apply_filters('pinc_minwidth', $minWidth);
  12. $minHeight = apply_filters('pinc_minheight', $minHeight);
  13.  
  14. require_once(ABSPATH . 'wp-admin/includes/image.php');
  15. require_once(ABSPATH . 'wp-admin/includes/file.php');
  16. require_once(ABSPATH . 'wp-admin/includes/media.php');
  17.  
  18. if ($_POST['mode'] == 'computer') {
  19. if ($_FILES) {
  20. foreach ($_FILES as $file => $array) {
  21. $imageTypes = array (
  22. 1, //IMAGETYPE_GIF
  23. 2, //IMAGETYPE_JPEG
  24. 3 //IMAGETYPE_PNG
  25. );
  26.  
  27. $imageinfo = getimagesize($_FILES[$file]['tmp_name']);
  28. $width = @$imageinfo[0];
  29. $height = @$imageinfo[1];
  30. $type = @$imageinfo[2];
  31. $mime = @$imageinfo['mime'];
  32.  
  33. if (!in_array($type, $imageTypes)) {
  34. @unlink($_FILES[$file]['tmp_name']);
  35. echo 'error';
  36. die();
  37. }
  38.  
  39. if ($width < $minWidth || $height < $minWidth) {
  40. @unlink($_FILES[$file]['tmp_name']);
  41. echo 'errorsize';
  42. die();
  43. }
  44.  
  45. if($mime != 'image/gif' && $mime != 'image/jpeg' && $mime != 'image/png') {
  46. @unlink($_FILES[$file]['tmp_name']);
  47. echo 'error';
  48. die();
  49. }
  50.  
  51. switch($type) {
  52. case 1:
  53. $ext = '.gif';
  54.  
  55. //check if is animated gif
  56. $frames = 0;
  57. if(($fh = @fopen($_FILES[$file]['tmp_name'], 'rb')) && $error != 'error') {
  58. while(!feof($fh) && $frames < 2) {
  59. $chunk = fread($fh, 1024 * 100); //read 100kb at a time
  60. $frames += preg_match_all('#x00x21xF9x04.{4}x00(x2C|x21)#s', $chunk, $matches);
  61. }
  62. }
  63. fclose($fh);
  64.  
  65. break;
  66. case 2:
  67. $ext = '.jpg';
  68. break;
  69. case 3:
  70. $ext = '.png';
  71. break;
  72. }
  73. $transliterationTable = array('á' => 'a', 'Á' => 'A', 'à' => 'a', 'À' => 'A', 'ă' => 'a', 'Ă' => 'A', 'â' => 'a', 'Â' => 'A', 'å' => 'a', 'Å' => 'A', 'ã' => 'a', 'Ã' => 'A', 'ą' => 'a', 'Ą' => 'A', 'ā' => 'a', 'Ā' => 'A', 'ä' => 'ae', 'Ä' => 'AE', 'æ' => 'ae', 'Æ' => 'AE', 'ḃ' => 'b', 'Ḃ' => 'B', 'ć' => 'c', 'Ć' => 'C', 'ĉ' => 'c', 'Ĉ' => 'C', 'č' => 'c', 'Č' => 'C', 'ċ' => 'c', 'Ċ' => 'C', 'ç' => 'c', 'Ç' => 'C', 'ď' => 'd', 'Ď' => 'D', 'ḋ' => 'd', 'Ḋ' => 'D', 'đ' => 'd', 'Đ' => 'D', 'ð' => 'dh', 'Ð' => 'Dh', 'é' => 'e', 'É' => 'E', 'è' => 'e', 'È' => 'E', 'ĕ' => 'e', 'Ĕ' => 'E', 'ê' => 'e', 'Ê' => 'E', 'ě' => 'e', 'Ě' => 'E', 'ë' => 'e', 'Ë' => 'E', 'ė' => 'e', 'Ė' => 'E', 'ę' => 'e', 'Ę' => 'E', 'ē' => 'e', 'Ē' => 'E', 'ḟ' => 'f', 'Ḟ' => 'F', 'ƒ' => 'f', 'Ƒ' => 'F', 'ğ' => 'g', 'Ğ' => 'G', 'ĝ' => 'g', 'Ĝ' => 'G', 'ġ' => 'g', 'Ġ' => 'G', 'ģ' => 'g', 'Ģ' => 'G', 'ĥ' => 'h', 'Ĥ' => 'H', 'ħ' => 'h', 'Ħ' => 'H', 'í' => 'i', 'Í' => 'I', 'ì' => 'i', 'Ì' => 'I', 'î' => 'i', 'Î' => 'I', 'ï' => 'i', 'Ï' => 'I', 'ĩ' => 'i', 'Ĩ' => 'I', 'į' => 'i', 'Į' => 'I', 'ī' => 'i', 'Ī' => 'I', 'ĵ' => 'j', 'Ĵ' => 'J', 'ķ' => 'k', 'Ķ' => 'K', 'ĺ' => 'l', 'Ĺ' => 'L', 'ľ' => 'l', 'Ľ' => 'L', 'ļ' => 'l', 'Ļ' => 'L', 'ł' => 'l', 'Ł' => 'L', 'ṁ' => 'm', 'Ṁ' => 'M', 'ń' => 'n', 'Ń' => 'N', 'ň' => 'n', 'Ň' => 'N', 'ñ' => 'n', 'Ñ' => 'N', 'ņ' => 'n', 'Ņ' => 'N', 'ó' => 'o', 'Ó' => 'O', 'ò' => 'o', 'Ò' => 'O', 'ô' => 'o', 'Ô' => 'O', 'ő' => 'o', 'Ő' => 'O', 'õ' => 'o', 'Õ' => 'O', 'ø' => 'oe', 'Ø' => 'OE', 'ō' => 'o', 'Ō' => 'O', 'ơ' => 'o', 'Ơ' => 'O', 'ö' => 'oe', 'Ö' => 'OE', 'ṗ' => 'p', 'Ṗ' => 'P', 'ŕ' => 'r', 'Ŕ' => 'R', 'ř' => 'r', 'Ř' => 'R', 'ŗ' => 'r', 'Ŗ' => 'R', 'ś' => 's', 'Ś' => 'S', 'ŝ' => 's', 'Ŝ' => 'S', 'š' => 's', 'Š' => 'S', 'ṡ' => 's', 'Ṡ' => 'S', 'ş' => 's', 'Ş' => 'S', 'ș' => 's', 'Ș' => 'S', 'ß' => 'SS', 'ť' => 't', 'Ť' => 'T', 'ṫ' => 't', 'Ṫ' => 'T', 'ţ' => 't', 'Ţ' => 'T', 'ț' => 't', 'Ț' => 'T', 'ŧ' => 't', 'Ŧ' => 'T', 'ú' => 'u', 'Ú' => 'U', 'ù' => 'u', 'Ù' => 'U', 'ŭ' => 'u', 'Ŭ' => 'U', 'û' => 'u', 'Û' => 'U', 'ů' => 'u', 'Ů' => 'U', 'ű' => 'u', 'Ű' => 'U', 'ũ' => 'u', 'Ũ' => 'U', 'ų' => 'u', 'Ų' => 'U', 'ū' => 'u', 'Ū' => 'U', 'ư' => 'u', 'Ư' => 'U', 'ü' => 'ue', 'Ü' => 'UE', 'ẃ' => 'w', 'Ẃ' => 'W', 'ẁ' => 'w', 'Ẁ' => 'W', 'ŵ' => 'w', 'Ŵ' => 'W', 'ẅ' => 'w', 'Ẅ' => 'W', 'ý' => 'y', 'Ý' => 'Y', 'ỳ' => 'y', 'Ỳ' => 'Y', 'ŷ' => 'y', 'Ŷ' => 'Y', 'ÿ' => 'y', 'Ÿ' => 'Y', 'ź' => 'z', 'Ź' => 'Z', 'ž' => 'z', 'Ž' => 'Z', 'ż' => 'z', 'Ż' => 'Z', 'þ' => 'th', 'Þ' => 'Th', 'µ' => 'u', 'а' => 'a', 'А' => 'a', 'б' => 'b', 'Б' => 'b', 'в' => 'v', 'В' => 'v', 'г' => 'g', 'Г' => 'g', 'д' => 'd', 'Д' => 'd', 'е' => 'e', 'Е' => 'E', 'ё' => 'e', 'Ё' => 'E', 'ж' => 'zh', 'Ж' => 'zh', 'з' => 'z', 'З' => 'z', 'и' => 'i', 'И' => 'i', 'й' => 'j', 'Й' => 'j', 'к' => 'k', 'К' => 'k', 'л' => 'l', 'Л' => 'l', 'м' => 'm', 'М' => 'm', 'н' => 'n', 'Н' => 'n', 'о' => 'o', 'О' => 'o', 'п' => 'p', 'П' => 'p', 'р' => 'r', 'Р' => 'r', 'с' => 's', 'С' => 's', 'т' => 't', 'Т' => 't', 'у' => 'u', 'У' => 'u', 'ф' => 'f', 'Ф' => 'f', 'х' => 'h', 'Х' => 'h', 'ц' => 'c', 'Ц' => 'c', 'ч' => 'ch', 'Ч' => 'ch', 'ш' => 'sh', 'Ш' => 'sh', 'щ' => 'sch', 'Щ' => 'sch', 'ъ' => '', 'Ъ' => '', 'ы' => 'y', 'Ы' => 'y', 'ь' => '', 'Ь' => '', 'э' => 'e', 'Э' => 'e', 'ю' => 'ju', 'Ю' => 'ju', 'я' => 'ja', 'Я' => 'ja');
  74.  
  75. $fname = $_FILES[$file]['name'];
  76. $fname = str_replace(array_keys($transliterationTable), array_values($transliterationTable), $fname);
  77. $filename = time() . str_shuffle('pcl48');
  78. $original_filename = preg_replace('/[^(x20|x61-x7A)]*/', '', strtolower(str_ireplace($ext, '', $fname))); //preg_replace('/[^(x48-x7A)]*/' strips non-utf character. Ref: http://www.ssec.wisc.edu/~tomw/java/unicode.html#x0000
  79. $_FILES[$file]['name'] = strtolower(substr($original_filename, 0, 100)) . '-' . $filename . $ext;
  80.  
  81. $attach_id = media_handle_upload($file, $post_id);
  82.  
  83. if (is_wp_error($attach_id)) {
  84. @unlink($_FILES[$file]['tmp_name']);
  85. echo 'error';
  86. die();
  87. } else {
  88. if ($frames > 1) {
  89. update_post_meta($attach_id, 'a_gif', 'yes');
  90. }
  91. }
  92. }
  93. }
  94.  
  95. update_post_meta($attach_id, 'pinc_unattached', 'yes');
  96.  
  97. $return = array();
  98.  
  99. $thumbnail = wp_get_attachment_image_src($attach_id, 'medium');
  100. $return['thumbnail'] = $thumbnail[0];
  101. $return['id'] = $attach_id;
  102.  
  103. do_action('pinc_after_upload_pin_computer', $attach_id);
  104. echo json_encode($return);
  105. } else if ($_POST['mode'] == 'web') {
  106. $url = esc_url_raw($_POST['pin_upload_web']);
  107.  
  108. if (function_exists('curl_init')) {
  109. $ch = curl_init();
  110. curl_setopt($ch, CURLOPT_URL, $url);
  111. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  112. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  113. $image = curl_exec($ch);
  114. curl_close($ch);
  115. } elseif (ini_get('allow_url_fopen')) {
  116. $image = file_get_contents($url, false, $context);
  117. }
  118.  
  119. if (!$image) {
  120. echo 'error';
  121. die();
  122. }
  123.  
  124. $filename = time() . str_shuffle('pcl48');
  125. $file_array['tmp_name'] = WP_CONTENT_DIR . "/" . $filename . '.tmp';
  126. $filetmp = file_put_contents($file_array['tmp_name'], $image);
  127.  
  128. if (!$filetmp) {
  129. @unlink($file_array['tmp_name']);
  130. echo 'error';
  131. die();
  132. }
  133.  
  134. $imageTypes = array (
  135. 1, //IMAGETYPE_GIF
  136. 2, //IMAGETYPE_JPEG
  137. 3 //IMAGETYPE_PNG
  138. );
  139.  
  140. $imageinfo = getimagesize($file_array['tmp_name']);
  141. $width = @$imageinfo[0];
  142. $height = @$imageinfo[1];
  143. $type = @$imageinfo[2];
  144. $mime = @$imageinfo['mime'];
  145.  
  146. if (!in_array ($type, $imageTypes)) {
  147. @unlink($file_array['tmp_name']);
  148. echo 'error';
  149. die();
  150. }
  151.  
  152. if ($width < $minWidth || $height < $minWidth) {
  153. @unlink($file_array['tmp_name']);
  154. echo 'errorsize';
  155. die();
  156. }
  157.  
  158. if($mime != 'image/gif' && $mime != 'image/jpeg' && $mime != 'image/png') {
  159. @unlink($file_array['tmp_name']);
  160. echo 'error';
  161. die();
  162. }
  163.  
  164. switch($type) {
  165. case 1:
  166. $ext = '.gif';
  167.  
  168. //check if is animated gif
  169. $frame = 0;
  170. if(($fh = @fopen($file_array['tmp_name'], 'rb')) && $error != 'error') {
  171. while(!feof($fh) && $frames < 2) {
  172. $chunk = fread($fh, 1024 * 100); //read 100kb at a time
  173. $frames += preg_match_all('#x00x21xF9x04.{4}x00(x2C|x21)#s', $chunk, $matches);
  174. }
  175. }
  176. fclose($fh);
  177.  
  178. break;
  179. case 2:
  180. $ext = '.jpg';
  181. break;
  182. case 3:
  183. $ext = '.png';
  184. break;
  185. }
  186. $original_filename = preg_replace('/[^(x20|x61-x7A)]*/', '', strtolower(str_ireplace($ext, '', basename($url)))); //preg_replace('/[^(x48-x7A)]*/' strips non-utf character. Ref: http://www.ssec.wisc.edu/~tomw/java/unicode.html#x0000
  187. $file_array['name'] = strtolower(substr($original_filename, 0, 100)) . '-' . $filename . $ext;
  188.  
  189. $attach_id = media_handle_sideload($file_array, $post_id);
  190.  
  191. if (is_wp_error($attach_id)) {
  192. @unlink($file_array['tmp_name']);
  193. echo 'error';
  194. die();
  195. } else {
  196. if ($frames > 1) {
  197. update_post_meta($attach_id, 'a_gif', 'yes');
  198. }
  199. }
  200.  
  201. update_post_meta($attach_id, 'pinc_unattached', 'yes');
  202.  
  203. $return = array();
  204. $thumbnail = wp_get_attachment_image_src($attach_id, 'medium');
  205. $return['thumbnail'] = $thumbnail[0];
  206. $return['id'] = $attach_id;
  207.  
  208. do_action('pinc_after_upload_pin_web', $attach_id);
  209. echo json_encode($return);
  210. }
  211. exit;
  212. }
  213. add_action('wp_ajax_pinc-upload-pin', 'pinc_upload_pin_new');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement