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

/wp-content/plugins/wp-e-commerce/wpsc-includes/misc.functions.php

https://gitlab.com/endomorphosis/reservationtelco
PHP | 796 lines | 573 code | 119 blank | 104 comment | 171 complexity | 0238141e4587fc7bb0d86167671cf8e7 MD5 | raw file
  1. <?php
  2. /**
  3. * WP eCommerce misc functions
  4. *
  5. * These are the WPSC miscellaneous functions
  6. *
  7. * @package wp-e-commerce
  8. * @since 3.7
  9. */
  10. /**
  11. * WPSC get state by id function, gets either state code or state name depending on param
  12. *
  13. * @since 3.7
  14. * $param int $id the id for the region
  15. * @param string $return_value either 'name' or 'code' depending on what you want returned
  16. */
  17. function wpsc_get_state_by_id($id, $return_value){
  18. global $wpdb;
  19. $sql = "SELECT `".$return_value."` FROM `".WPSC_TABLE_REGION_TAX."` WHERE `id`=".$id;
  20. $value = $wpdb->get_var($sql);
  21. return $value;
  22. }
  23. /**
  24. * WPSC add new user function, validates and adds a new user, for the
  25. *
  26. * @since 3.7
  27. *
  28. * @param string $user_login The user's username.
  29. * @param string $password The user's password.
  30. * @param string $user_email The user's email (optional).
  31. * @return int The new user's ID.
  32. */
  33. function wpsc_add_new_user($user_login,$user_pass, $user_email) {
  34. require_once(ABSPATH . WPINC . '/registration.php');
  35. $errors = new WP_Error();
  36. $user_login = sanitize_user( $user_login );
  37. $user_email = apply_filters( 'user_registration_email', $user_email );
  38. // Check the username
  39. if ( $user_login == '' ) {
  40. $errors->add('empty_username', __('<strong>ERROR</strong>: Please enter a username.'));
  41. } elseif ( !validate_username( $user_login ) ) {
  42. $errors->add('invalid_username', __('<strong>ERROR</strong>: This username is invalid. Please enter a valid username.'));
  43. $user_login = '';
  44. } elseif ( username_exists( $user_login ) ) {
  45. $errors->add('username_exists', __('<strong>ERROR</strong>: This username is already registered, please choose another one.'));
  46. }
  47. // Check the e-mail address
  48. if ($user_email == '') {
  49. $errors->add('empty_email', __('<strong>ERROR</strong>: Please type your e-mail address.'));
  50. } elseif ( !is_email( $user_email ) ) {
  51. $errors->add('invalid_email', __('<strong>ERROR</strong>: The email address isn&#8217;t correct.'));
  52. $user_email = '';
  53. } elseif ( email_exists( $user_email ) ) {
  54. $errors->add('email_exists', __('<strong>ERROR</strong>: This email is already registered, please choose another one.'));
  55. }
  56. if ( $errors->get_error_code() ) {
  57. return $errors;
  58. }
  59. $user_id = wp_create_user( $user_login, $user_pass, $user_email );
  60. if ( !$user_id ) {
  61. $errors->add('registerfail', sprintf(__('<strong>ERROR</strong>: Couldn&#8217;t register you... please contact the <a href="mailto:%s">webmaster</a> !'), get_option('admin_email')));
  62. return $errors;
  63. }
  64. $credentials = array('user_login' => $user_login, 'user_password' => $user_pass, 'remember' => true);
  65. $user = wp_signon($credentials);
  66. return $user;
  67. //wp_new_user_notification($user_id, $user_pass);
  68. }
  69. /**
  70. * WPSC product has variations function
  71. * @since 3.7
  72. * @param int product id
  73. * @return bool true or false
  74. */
  75. function wpsc_product_has_variations($product_id) {
  76. global $wpdb;
  77. if($product_id > 0) {
  78. $variation_count = $wpdb->get_var("SELECT COUNT(`id`) FROM `".WPSC_TABLE_VARIATION_ASSOC."` WHERE `type` IN('product') AND `associated_id` IN('{$product_id}')");
  79. if($variation_count > 0) {
  80. return true;
  81. }
  82. }
  83. return false;
  84. }
  85. function wpsc_post_title_seo($title) {
  86. global $wpdb, $page_id, $wp_query;
  87. $new_title = wpsc_obtain_the_title();
  88. if($new_title != '') {
  89. $title = $new_title;
  90. }
  91. return stripslashes($title);
  92. }
  93. add_filter('single_post_title','wpsc_post_title_seo');
  94. /**
  95. * WPSC canonical URL function
  96. * Needs a recent version
  97. * @since 3.7
  98. * @param int product id
  99. * @return bool true or false
  100. */
  101. function wpsc_change_canonical_url($url) {
  102. global $wpdb, $wpsc_query, $post;
  103. // Only change the URL is we're viewing a WP e-Commerce page
  104. if(stristr($post->post_content,'[productspage]')) {
  105. if (isset($wpsc_query->query_vars['product_url_name'])) {
  106. $product_url_name = $wpsc_query->query_vars['product_url_name'];
  107. } else {
  108. $product_url_name = '';
  109. }
  110. // Viewing a single product page
  111. if ($product_url_name != '') {
  112. if(!is_numeric($_GET['product_id'])) {
  113. $product_id = $wpdb->get_var($wpdb->prepare("SELECT product_id FROM ".WPSC_TABLE_PRODUCTMETA." WHERE meta_key = 'url_name' AND meta_value = %s ORDER BY product_id DESC LIMIT 1", $product_url_name));
  114. } else {
  115. $product_id = absint($_GET['product_id']);
  116. }
  117. if($product_id > 0){
  118. $url = wpsc_product_url($product_id);
  119. }else{
  120. $url = get_option('product_list_url');
  121. }
  122. // Viewing a category page
  123. } elseif (absint($wpsc_query->query_vars['category_id']) > 0) {
  124. $url = wpsc_category_url(absint($wpsc_query->query_vars['category_id']));
  125. if ( $wpsc_query->query_vars['page'] > 1 ) {
  126. if ( get_option( 'permalink_structure' ) ) {
  127. $url .= "page/{$wpsc_query->query_vars['page']}/";
  128. } else {
  129. $url .= "&page_number={$wpsc_query->query_vars['page']}";
  130. $url = html_entity_decode( $url );
  131. }
  132. }
  133. }
  134. }
  135. return $url;
  136. }
  137. add_filter('aioseop_canonical_url', 'wpsc_change_canonical_url');
  138. function wpsc_insert_canonical_url() {
  139. $wpsc_url = wpsc_change_canonical_url(null);
  140. // exit($wpsc_url);
  141. echo "<link rel='canonical' href='$wpsc_url' />\n";
  142. }
  143. function wpsc_canonical_url() {
  144. $wpsc_url = wpsc_change_canonical_url(null);
  145. if(($wpsc_url != null) && ((count($aioseop_options) <= 1) || (($aioseop_options['aiosp_can'] != '1' && $aioseop_options['aiosp_can'] != 'on'))) ) {
  146. remove_action( 'wp_head', 'rel_canonical' );
  147. add_action( 'wp_head', 'wpsc_insert_canonical_url');
  148. }
  149. }
  150. add_action( 'template_redirect', 'wpsc_canonical_url' );
  151. // check for all in one SEO pack and the is_static_front_page function
  152. if(is_callable(array("All_in_One_SEO_Pack", 'is_static_front_page'))) {
  153. function wpsc_change_aioseop_home_title($title) {
  154. global $aiosp, $aioseop_options;
  155. if((get_class($aiosp) == 'All_in_One_SEO_Pack') && $aiosp->is_static_front_page()) {
  156. $aiosp_home_title = $aiosp->internationalize($aioseop_options['aiosp_home_title']);
  157. $new_title = wpsc_obtain_the_title();
  158. if($new_title != '') {
  159. $title = str_replace($aiosp_home_title, $new_title, $title);
  160. }
  161. }
  162. return $title;
  163. }
  164. add_filter('aioseop_home_page_title', 'wpsc_change_aioseop_home_title');
  165. //add_filter('aioseop_title_page', 'wpsc_change_aioseop_home_title');
  166. }
  167. function wpsc_set_aioseop_description($data) {
  168. $replacement_data = wpsc_obtain_the_description();
  169. if($replacement_data != '') {
  170. $data = $replacement_data;
  171. }
  172. return $data;
  173. }
  174. add_filter('aioseop_description', 'wpsc_set_aioseop_description');
  175. function wpsc_set_aioseop_keywords($data) {
  176. global $wpdb, $wp_query, $wpsc_title_data, $aioseop_options;
  177. if(isset($wp_query->query_vars['product_url_name'])) {
  178. $product_name = $wp_query->query_vars['product_url_name'];
  179. $product_id = $wpdb->get_var("SELECT `product_id` FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE `meta_key` IN ( 'url_name' ) AND `meta_value` IN ( '{$wp_query->query_vars['product_url_name']}' ) ORDER BY `id` DESC LIMIT 1");
  180. $replacement_data = '';
  181. $replacement_data_array = array();
  182. if($aioseop_options['aiosp_use_categories']) {
  183. $category_list = $wpdb->get_col("SELECT `categories`.`name` FROM `".WPSC_TABLE_ITEM_CATEGORY_ASSOC."` AS `assoc` , `".WPSC_TABLE_PRODUCT_CATEGORIES."` AS `categories` WHERE `assoc`.`product_id` IN ('{$product_id}') AND `assoc`.`category_id` = `categories`.`id` AND `categories`.`active` IN('1')");
  184. $replacement_data_array += $category_list;
  185. }
  186. $replacement_data_array += wp_get_object_terms($product_id, 'product_tag', array('fields' => 'names'));
  187. $replacement_data .= implode(",", $replacement_data_array);
  188. if($replacement_data != '') {
  189. $data = strtolower($replacement_data);
  190. }
  191. }
  192. return $data;
  193. }
  194. add_filter('aioseop_keywords', 'wpsc_set_aioseop_keywords');
  195. /**
  196. * wpsc_populate_also_bought_list function, runs on checking out, populates the also bought list.
  197. */
  198. function wpsc_populate_also_bought_list() {
  199. global $wpdb, $wpsc_cart, $wpsc_coupons;
  200. //exit("<pre>".print_r($variations,true)."</pre>");
  201. $new_also_bought_data = array();
  202. foreach($wpsc_cart->cart_items as $outer_cart_item) {
  203. $new_also_bought_data[$outer_cart_item->product_id] = array();
  204. foreach($wpsc_cart->cart_items as $inner_cart_item) {
  205. if($outer_cart_item->product_id != $inner_cart_item->product_id) {
  206. $new_also_bought_data[$outer_cart_item->product_id][$inner_cart_item->product_id] = $inner_cart_item->quantity;
  207. } else {
  208. continue;
  209. }
  210. }
  211. }
  212. $insert_statement_parts = array();
  213. foreach($new_also_bought_data as $new_also_bought_id => $new_also_bought_row) {
  214. $new_other_ids = array_keys($new_also_bought_row);
  215. $also_bought_data = $wpdb->get_results("SELECT `id`, `associated_product`, `quantity` FROM `".WPSC_TABLE_ALSO_BOUGHT."` WHERE `selected_product` IN('$new_also_bought_id') AND `associated_product` IN('".implode("','", $new_other_ids)."')", ARRAY_A);
  216. $altered_new_also_bought_row = $new_also_bought_row;
  217. foreach((array)$also_bought_data as $also_bought_row) {
  218. $quantity = $new_also_bought_row[$also_bought_row['associated_product']] + $also_bought_row['quantity'];
  219. unset($altered_new_also_bought_row[$also_bought_row['associated_product']]);
  220. $wpdb->query("UPDATE `".WPSC_TABLE_ALSO_BOUGHT."` SET `quantity` = {$quantity} WHERE `id` = '{$also_bought_row['id']}' LIMIT 1;");
  221. }
  222. if(count($altered_new_also_bought_row) > 0 ) {
  223. foreach($altered_new_also_bought_row as $associated_product => $quantity) {
  224. $insert_statement_parts[] = "(".absint($new_also_bought_id).",".absint($associated_product).",".absint($quantity).")";
  225. }
  226. }
  227. }
  228. if(count($insert_statement_parts) > 0) {
  229. $insert_statement = "INSERT INTO `".WPSC_TABLE_ALSO_BOUGHT."` (`selected_product`, `associated_product`, `quantity`) VALUES ".implode(",\n ", $insert_statement_parts);
  230. $wpdb->query($insert_statement);
  231. //echo $insert_statement;
  232. }
  233. }
  234. function add_product_meta($product_id, $key, $value, $unique = false, $custom = false) {
  235. global $wpdb, $post_meta_cache, $blog_id;
  236. $product_id = (int)$product_id;
  237. if($product_id > 0) {
  238. if(($unique == true) && $wpdb->get_var("SELECT meta_key FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE meta_key = '$key' AND product_id = '$product_id'")) {
  239. return false;
  240. }
  241. if(!is_string($value)) {
  242. $value = maybe_serialize($value);
  243. } else {
  244. $value = $wpdb->escape($value);
  245. }
  246. if(!$wpdb->get_var("SELECT meta_key FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE meta_key = '$key' AND product_id = '$product_id'")) {
  247. $custom = (int)$custom;
  248. $wpdb->query("INSERT INTO `".WPSC_TABLE_PRODUCTMETA."` (product_id,meta_key,meta_value, custom) VALUES ('$product_id','$key','$value', '$custom')");
  249. } else {
  250. $wpdb->query("UPDATE `".WPSC_TABLE_PRODUCTMETA."` SET meta_value = '$value' WHERE meta_key = '$key' AND product_id = '$product_id'");
  251. }
  252. return true;
  253. }
  254. return false;
  255. }
  256. function delete_product_meta($product_id, $key, $value = '') {
  257. global $wpdb, $post_meta_cache, $blog_id;
  258. $product_id = (int)$product_id;
  259. if($product_id > 0) {
  260. if ( empty($value) ) {
  261. $meta_id = $wpdb->get_var("SELECT id FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE product_id = '$product_id' AND meta_key = '$key'");
  262. if(is_numeric($meta_id) && ($meta_id > 0)) {
  263. $wpdb->query("DELETE FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE product_id = '$product_id' AND meta_key = '$key'");
  264. }
  265. } else {
  266. $meta_id = $wpdb->get_var("SELECT id FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE product_id = '$product_id' AND meta_key = '$key' AND meta_value = '$value'");
  267. if(is_numeric($meta_id) && ($meta_id > 0)) {
  268. $wpdb->query("DELETE FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE product_id = '$product_id' AND meta_key = '$key' AND meta_value = '$value'");
  269. }
  270. }
  271. }
  272. return true;
  273. }
  274. function get_product_meta($product_id, $key, $single = false) {
  275. global $wpdb, $post_meta_cache, $blog_id;
  276. $product_id = (int)$product_id;
  277. if($product_id > 0) {
  278. $meta_id = $wpdb->get_var("SELECT `id` FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE `meta_key` IN('$key') AND `product_id` = '$product_id' LIMIT 1");
  279. //exit($meta_id);
  280. if(is_numeric($meta_id) && ($meta_id > 0)) {
  281. if($single != false) {
  282. $meta_values = maybe_unserialize($wpdb->get_var("SELECT `meta_value` FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE `meta_key` IN('$key') AND `product_id` = '$product_id' LIMIT 1"));
  283. } else {
  284. $meta_values = $wpdb->get_col("SELECT `meta_value` FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE `meta_key` IN('$key') AND `product_id` = '$product_id'");
  285. $meta_values = array_map('maybe_unserialize', $meta_values);
  286. }
  287. }
  288. } else {
  289. $meta_values = false;
  290. }
  291. if (is_array($meta_values) && (count($meta_values) == 1)) {
  292. return array_pop($meta_values);
  293. } else {
  294. return $meta_values;
  295. }
  296. }
  297. function update_product_meta($product_id, $key, $value, $prev_value = '') {
  298. global $wpdb, $blog_id;
  299. $product_id = (int)$product_id;
  300. if($product_id > 0) {
  301. if(!is_string($value)) {
  302. $value = $wpdb->escape(maybe_serialize($value));
  303. } else {
  304. $value = $wpdb->escape($value);
  305. }
  306. if(!empty($prev_value)) {
  307. $prev_value = $wpdb->escape(maybe_serialize($prev_value));
  308. }
  309. if($wpdb->get_var("SELECT meta_key FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE `meta_key` IN('$key') AND product_id = '$product_id'")) {
  310. if (empty($prev_value)) {
  311. $wpdb->query("UPDATE `".WPSC_TABLE_PRODUCTMETA."` SET `meta_value` = '$value' WHERE `meta_key` IN('$key') AND product_id = '$product_id'");
  312. } else {
  313. $wpdb->query("UPDATE `".WPSC_TABLE_PRODUCTMETA."` SET `meta_value` = '$value' WHERE `meta_key` IN('$key') AND product_id = '$product_id' AND meta_value = '$prev_value'");
  314. }
  315. } else {
  316. $wpdb->query("INSERT INTO `".WPSC_TABLE_PRODUCTMETA."` (product_id,meta_key,meta_value) VALUES ('$product_id','$key','$value')");
  317. }
  318. return true;
  319. }
  320. }
  321. function wpsc_get_country($country_code) {
  322. global $wpdb;
  323. $country = $wpdb->get_var("SELECT `country` FROM `".WPSC_TABLE_CURRENCY_LIST."` WHERE `isocode` IN ('".$country_code."') LIMIT 1");
  324. return $country;
  325. }
  326. function wpsc_get_region($region_code) {
  327. global $wpdb;
  328. $region = $wpdb->get_var("SELECT `name` FROM `".WPSC_TABLE_REGION_TAX."` WHERE `id` IN('$region_code')");
  329. return $region;
  330. }
  331. function nzshpcrt_display_preview_image() {
  332. global $wpdb;
  333. if(($_GET['wpsc_request_image'] == 'true') || is_numeric($_GET['productid']) || is_numeric($_GET['image_id'])|| isset($_GET['image_name'])) {
  334. if(function_exists("getimagesize")) {
  335. if(is_numeric($_GET['productid'])) {
  336. $product_id = (int)$_GET['productid'];
  337. $image_data = $wpdb->get_var("SELECT `image` FROM `".WPSC_TABLE_PRODUCT_LIST."` WHERE `id`='{$product_id}' LIMIT 1");
  338. if(is_numeric($image_data)) {
  339. $image = $wpdb->get_var("SELECT `image` FROM `".WPSC_TABLE_PRODUCT_IMAGES."` WHERE `id` = '{$image_data}' LIMIT 1");
  340. $imagepath = WPSC_IMAGE_DIR . $image;
  341. } else {
  342. $imagepath = WPSC_IMAGE_DIR . $imagedata['image'];
  343. }
  344. } else if($_GET['image_id']) {
  345. $image_id = (int)$_GET['image_id'];
  346. $results = $wpdb->get_row("SELECT `image`,`product_id` FROM `".WPSC_TABLE_PRODUCT_IMAGES."` WHERE `id` = '{$image_id}' LIMIT 1");
  347. $image = $results->image;
  348. $pid = $results->product_id;
  349. $thumbnail_info = $wpdb->get_row("SELECT `thumbnail_state`,`image` FROM `".WPSC_TABLE_PRODUCT_LIST."` WHERE `id` = '{$pid}' LIMIT 1");
  350. $thumbnail_state = $thumbnail_info->thumbnail_state;
  351. $thumbnail_image = $thumbnail_info->image;
  352. if (($thumbnail_state == 3) && ($image_id == $thumbnail_image)) {
  353. $imagepath = WPSC_THUMBNAIL_DIR . $image;
  354. } else {
  355. $imagepath = WPSC_IMAGE_DIR . $image;
  356. }
  357. } else if( $_GET['image_name']) {
  358. $image = basename($_GET['image_name']);
  359. $imagepath = WPSC_USER_UPLOADS_DIR . $image;
  360. } else if( $_GET['category_id']) {
  361. $category_id = absint($_GET['category_id']);
  362. $image = $wpdb->get_var("SELECT `image` FROM `".WPSC_TABLE_PRODUCT_CATEGORIES."` WHERE `id` = '{$category_id}' LIMIT 1");
  363. if($image != '') {
  364. $imagepath = WPSC_CATEGORY_DIR.$image;
  365. }
  366. }
  367. if(!is_file($imagepath)) {
  368. $imagepath = WPSC_FILE_PATH."/images/no-image-uploaded.gif";
  369. }
  370. $image_size = @getimagesize($imagepath);
  371. if(is_numeric($_GET['height']) && is_numeric($_GET['width'])) {
  372. $height = (int)$_GET['height'];
  373. $width = (int)$_GET['width'];
  374. } else {
  375. $width = $image_size[0];
  376. $height = $image_size[1];
  377. }
  378. if(!(($height > 0) && ($height <= 1024) && ($width > 0) && ($width <= 1024))) {
  379. $width = $image_size[0];
  380. $height = $image_size[1];
  381. }
  382. if($product_id > 0) {
  383. $cache_filename = basename("product_{$product_id}_{$height}x{$width}");
  384. } else if ($category_id > 0 ) {
  385. $cache_filename = basename("category_{$category_id}_{$height}x{$width}");
  386. } else {
  387. $cache_filename = basename("product_img_{$image_id}_{$height}x{$width}");
  388. }
  389. //echo "<pre>".print_r($_GET, true)."</pre>";
  390. //exit($cache_filename);
  391. $imagetype = @getimagesize($imagepath);
  392. $use_cache = false;
  393. switch($imagetype[2]) {
  394. case IMAGETYPE_JPEG:
  395. $extension = ".jpg";
  396. break;
  397. case IMAGETYPE_GIF:
  398. $extension = ".gif";
  399. break;
  400. case IMAGETYPE_PNG:
  401. $extension = ".png";
  402. break;
  403. }
  404. if(file_exists(WPSC_CACHE_DIR.$cache_filename.$extension)) {
  405. $original_modification_time = filemtime($imagepath);
  406. $cache_modification_time = filemtime(WPSC_CACHE_DIR.$cache_filename.$extension);
  407. if($original_modification_time < $cache_modification_time) {
  408. $use_cache = true;
  409. }
  410. }
  411. if($use_cache === true ) {
  412. $cache_url = WPSC_CACHE_URL;
  413. if(is_ssl()) {
  414. $cache_url = str_replace("http://", "https://", $cache_url);
  415. }
  416. header("Location: ".$cache_url.$cache_filename.$extension);
  417. exit('');
  418. } else {
  419. switch($imagetype[2]) {
  420. case IMAGETYPE_JPEG:
  421. //$extension = ".jpg";
  422. $src_img = imagecreatefromjpeg($imagepath);
  423. $pass_imgtype = true;
  424. break;
  425. case IMAGETYPE_GIF:
  426. //$extension = ".gif";
  427. $src_img = imagecreatefromgif($imagepath);
  428. $pass_imgtype = true;
  429. break;
  430. case IMAGETYPE_PNG:
  431. //$extension = ".png";
  432. $src_img = imagecreatefrompng($imagepath);
  433. $pass_imgtype = true;
  434. break;
  435. default:
  436. $pass_imgtype = false;
  437. break;
  438. }
  439. if($pass_imgtype === true) {
  440. $source_w = imagesx($src_img);
  441. $source_h = imagesy($src_img);
  442. //Temp dimensions to crop image properly
  443. $temp_w = $width;
  444. $temp_h = $height;
  445. // select our scaling method
  446. $scaling_method = 'cropping';
  447. //list($source_h, $source_w) = array($source_w, $source_h);
  448. // set both offsets to zero
  449. $offset_x = $offset_y = 0;
  450. // Here are the scaling methods, non-cropping causes black lines in tall images, but doesnt crop images.
  451. switch($scaling_method) {
  452. case 'cropping':
  453. // if the image is wider than it is high and at least as wide as the target width.
  454. if (($source_h <= $source_w)) {
  455. if ($height < $width ) {
  456. $temp_h = ($width / $source_w) * $source_h;
  457. } else {
  458. $temp_w = ($height / $source_h) * $source_w;
  459. }
  460. } else {
  461. $temp_h = ($width / $source_w) * $source_h;
  462. }
  463. break;
  464. case 'non-cropping':
  465. default:
  466. if ($height < $width ) {
  467. $temp_h = ($width / $source_w) * $source_h;
  468. } else {
  469. $temp_w = ($height / $source_h) * $source_w;
  470. }
  471. break;
  472. }
  473. // Create temp resized image
  474. $temp_img = ImageCreateTrueColor( $temp_w, $temp_h );
  475. $bgcolor = ImageColorAllocate( $temp_img, 255, 255, 255 );
  476. ImageFilledRectangle( $temp_img, 0, 0, $temp_w, $temp_h, $bgcolor );
  477. ImageAlphaBlending( $temp_img, TRUE );
  478. ImageCopyResampled( $temp_img, $src_img, 0, 0, 0, 0, $temp_w, $temp_h, $source_w, $source_h );
  479. $dst_img = ImageCreateTrueColor($width,$height);
  480. $bgcolor = ImageColorAllocate( $dst_img, 255, 255, 255 );
  481. ImageFilledRectangle( $dst_img, 0, 0, $width, $height, $bgcolor );
  482. ImageAlphaBlending($dst_img, TRUE );
  483. if (($imagetype[2]==IMAGETYPE_PNG) ||($imagetype[2]==IMAGETYPE_GIF)){
  484. //imagecolortransparent($dst_img, $bgcolor);
  485. }
  486. // X & Y Offset to crop image properly
  487. if($temp_w < $width) {
  488. $w1 = ($width/2) - ($temp_w/2);
  489. } else if($temp_w == $width) {
  490. $w1 = 0;
  491. } else {
  492. $w1 = ($width/2) - ($temp_w/2);
  493. }
  494. if($temp_h < $height) {
  495. $h1 = ($height/2) - ($temp_h/2);
  496. } else if($temp_h == $height) {
  497. $h1 = 0;
  498. } else {
  499. $h1 = ($height/2) - ($temp_h/2);
  500. }
  501. switch($scaling_method) {
  502. case 'cropping':
  503. ImageCopy( $dst_img, $temp_img, $w1, $h1, 0, 0, $temp_w, $temp_h );
  504. break;
  505. case 'non-cropping':
  506. default:
  507. ImageCopy( $dst_img, $temp_img, 0, 0, 0, 0, $temp_w, $temp_h );
  508. break;
  509. }
  510. ImageAlphaBlending($dst_img, false);
  511. switch($imagetype[2]) {
  512. case IMAGETYPE_JPEG:
  513. header("Content-type: image/jpeg");
  514. ImagePNG($dst_img);
  515. ImagePNG($dst_img, WPSC_CACHE_DIR.$cache_filename.".jpg");
  516. @ chmod( WPSC_CACHE_DIR.$cache_filename.".jpg", 0775 );
  517. break;
  518. case IMAGETYPE_GIF:
  519. header("Content-type: image/gif");
  520. ImagePNG($dst_img);
  521. ImagePNG($dst_img, WPSC_CACHE_DIR.$cache_filename.".gif");
  522. @ chmod( WPSC_CACHE_DIR.$cache_filename.".gif", 0775 );
  523. break;
  524. case IMAGETYPE_PNG:
  525. header("Content-type: image/png");
  526. ImagePNG($dst_img);
  527. ImagePNG($dst_img, WPSC_CACHE_DIR.$cache_filename.".png");
  528. @ chmod( WPSC_CACHE_DIR.$cache_filename.".png", 0775 );
  529. break;
  530. default:
  531. $pass_imgtype = false;
  532. break;
  533. }
  534. exit();
  535. }
  536. }
  537. }
  538. }
  539. }
  540. add_action('init', 'nzshpcrt_display_preview_image');
  541. //function added to preserve backwards compatibility
  542. function nzshpcrt_listdir($dirname){
  543. return wpsc_list_dir($dirname);
  544. }
  545. function wpsc_list_dir($dirname) {
  546. /*
  547. lists the provided directory, was nzshpcrt_listdir
  548. */
  549. $dir = @opendir($dirname);
  550. $num = 0;
  551. while(($file = @readdir($dir)) !== false) {
  552. //filter out the dots and any backup files, dont be tempted to correct the "spelling mistake", its to filter out a previous spelling mistake.
  553. if(($file != "..") && ($file != ".") && !stristr($file, "~") && !stristr($file, "Chekcout") && !( strpos($file, ".") === 0 )) {
  554. $dirlist[$num] = $file;
  555. $num++;
  556. }
  557. }
  558. if($dirlist == null) {
  559. $dirlist[0] = "paypal.php";
  560. $dirlist[1] = "testmode.php";
  561. }
  562. return $dirlist;
  563. }
  564. /**
  565. * wpsc_recursive_copy function, copied from here and renamed: http://nz.php.net/copy
  566. * Why doesn't PHP have one of these built in?
  567. */
  568. function wpsc_recursive_copy($src,$dst) {
  569. $dir = opendir($src);
  570. @mkdir($dst);
  571. while(false !== ( $file = readdir($dir)) ) {
  572. if (( $file != '.' ) && ( $file != '..' )) {
  573. if ( is_dir($src . '/' . $file) ) {
  574. wpsc_recursive_copy($src . '/' . $file,$dst . '/' . $file);
  575. }
  576. else {
  577. @ copy($src . '/' . $file,$dst . '/' . $file);
  578. }
  579. }
  580. }
  581. closedir($dir);
  582. }
  583. /**
  584. * wpsc_replace_reply_address function,
  585. * Replace the email address for the purchase receipts
  586. */
  587. function wpsc_replace_reply_address($input) {
  588. $output = get_option('return_email');
  589. if($output == '') {
  590. $output = $input;
  591. }
  592. return $output;
  593. }
  594. /**
  595. * wpsc_replace_reply_address function,
  596. * Replace the email address for the purchase receipts
  597. */
  598. function wpsc_replace_reply_name($input) {
  599. $output = get_option('return_name');
  600. if($output == '') {
  601. $output = $input;
  602. }
  603. return $output;
  604. }
  605. /**
  606. * wpsc_object_to_array, recusively converts an object to an array, for usage with SOAP code
  607. * Copied from here, then modified:
  608. * http://www.phpro.org/examples/Convert-Object-To-Array-With-PHP.html
  609. */
  610. function wpsc_object_to_array( $object ) {
  611. if( !is_object( $object ) && !is_array( $object ) ) {
  612. return $object;
  613. } else if( is_object( $object ) ) {
  614. $object = get_object_vars( $object );
  615. }
  616. return array_map( 'wpsc_object_to_array', $object );
  617. }
  618. function wpsc_readfile_chunked($filename, $retbytes = true) {
  619. $chunksize = 1 * (1024 * 1024); // how many bytes per chunk
  620. $buffer = '';
  621. $cnt = 0;
  622. $handle = fopen($filename, 'rb');
  623. if($handle === false) {
  624. return false;
  625. }
  626. while (!feof($handle)) {
  627. $buffer = fread($handle, $chunksize);
  628. echo $buffer;
  629. ob_flush();
  630. flush();
  631. if($retbytes) {
  632. $cnt += strlen($buffer);
  633. }
  634. }
  635. $status = fclose($handle);
  636. if($retbytes && $status) {
  637. return $cnt; // return num. bytes delivered like readfile() does.
  638. }
  639. return $status;
  640. }
  641. /**
  642. * wpsc_clear_stock_claims, clears the stock claims, runs using wp-cron
  643. */
  644. function wpsc_clear_stock_claims( ) {
  645. global $wpdb;
  646. ///wp_mail('thomas.howard@gmail.com', 'test hourly cron', 'wpsc_clear_stock_claims ran');
  647. /// Delete the old claims on stock
  648. $old_claimed_stock_timestamp = mktime((date('H') - 1), date('i'), date('s'), date('m'), date('d'), date('Y'));
  649. $old_claimed_stock_datetime = date("Y-m-d H:i:s", $old_claimed_stock_timestamp);
  650. $wpdb->query("DELETE FROM `".WPSC_TABLE_CLAIMED_STOCK."` WHERE `last_activity` < '{$old_claimed_stock_datetime}' AND `cart_submitted` IN ('0')");
  651. }
  652. add_action('wpsc_daily_cron_tasks', 'wpsc_clear_stock_claims');
  653. /**
  654. * Description Check PHP version to Compare
  655. * @access public
  656. *
  657. * @param string of version to compare
  658. * @return boolean true or false
  659. */
  660. function phpMinV($v)
  661. {
  662. $phpV = PHP_VERSION;
  663. if ($phpV[0] >= $v[0]) {
  664. if (empty($v[2]) || $v[2] == '*') {
  665. return true;
  666. } elseif ($phpV[2] >= $v[2]) {
  667. if (empty($v[4]) || $v[4] == '*' || $phpV[4] >= $v[4]) {
  668. return true;
  669. }
  670. }
  671. }
  672. return false;
  673. }
  674. ?>