function geodir_expire_check()
{
    global $wpdb, $plugin_prefix;
    $current_date = date('Y-m-d');
    $geodir_postypes = geodir_get_posttypes();
    $upload_dir = wp_upload_dir();
    $upload_basedir = $upload_dir['basedir'];
    if (get_option('geodir_listing_expiry')) {
        foreach ($geodir_postypes as $post) {
            $table = $plugin_prefix . $post . '_detail';
            if (get_option('geodir_listing_preexpiry_notice_disable')) {
                $number_of_grace_days = get_option('geodir_listing_preexpiry_notice_days');
                if ($number_of_grace_days == '') {
                    $number_of_grace_days = 1;
                }
                $today = date('Y-m-d', strtotime(date('Y-m-d') . "+" . (int) $number_of_grace_days . " days"));
                $strtoday = $wpdb->get_var("SELECT UNIX_TIMESTAMP( STR_TO_DATE( '" . $today . "','%Y-%m-%d'))");
                $postid_str = $wpdb->get_results($wpdb->prepare("SELECT p.ID, p.post_author, p.post_title from " . $table . " detail, " . $wpdb->posts . " p WHERE p.ID=detail.post_id AND detail.expire_date!='Never' AND detail.expire_date!='' AND detail.expire_notification='false' AND unix_timestamp(detail.expire_date)<=%s", array($strtoday)));
                if (!empty($postid_str)) {
                    foreach ($postid_str as $postid_str_obj) {
                        geodir_payment_clientEmail($postid_str_obj->ID, $postid_str_obj->post_author, 'expiration');
                        $wpdb->query($wpdb->prepare("update " . $table . " set expire_notification='true' where post_id=%d", array($postid_str_obj->ID)));
                    }
                }
            }
            $strcurrent = $wpdb->get_var("SELECT UNIX_TIMESTAMP( STR_TO_DATE( '" . $current_date . "','%Y-%m-%d'))");
            $postid_str = $wpdb->get_results($wpdb->prepare("SELECT p.ID, p.post_author, p.post_title, detail.package_id from " . $table . " detail, " . $wpdb->posts . " p WHERE p.ID=detail.post_id AND detail.expire_date!='Never' AND detail.expire_date!='' AND unix_timestamp(detail.expire_date)<=%s", array($strcurrent)));
            if (!empty($postid_str)) {
                foreach ($postid_str as $postid_str_obj) {
                    $post_id = $postid_str_obj->ID;
                    $package_id = $postid_str_obj->package_id;
                    $old_package_info = geodir_get_package_info($package_id);
                    $old_image_limit = empty($old_package_info->image_limit) ? 0 : $old_package_info->image_limit;
                    $old_cat_limit = empty($old_package_info->cat_limit) ? 0 : $old_package_info->cat_limit;
                    $downgrade_pkg = $old_package_info->downgrade_pkg;
                    $package_info = (int) $downgrade_pkg > 0 ? geodir_get_package_info($downgrade_pkg) : array();
                    if ((int) $downgrade_pkg > 0 && $downgrade_pkg != '' && !empty($package_info)) {
                        $featured = $package_info->is_featured;
                        $image_limit = empty($package_info->image_limit) ? 0 : $package_info->image_limit;
                        $cat_limit = empty($package_info->cat_limit) ? 0 : $package_info->cat_limit;
                        $days = $package_info->days;
                        $exclude_cats = $package_info->cat;
                        if ($cat_limit != 0 && $cat_limit < $old_cat_limit) {
                            $terms = wp_get_post_terms($post_id, $post . 'category', array("fields" => "all"));
                            $term_ids = array();
                            foreach ($terms as $termsObj) {
                                if ($termsObj->parent == 0) {
                                    $term_ids[] = $termsObj->term_id;
                                }
                            }
                            $cat_arr = array_slice($term_ids, 0, $cat_limit);
                            $term_ids = implode(",", $cat_arr);
                            wp_set_object_terms($post_id, $cat_arr, $post . 'category');
                            $post_default_category = geodir_get_post_meta($post_id, 'default_category');
                            if ($post_default_category != '' && !in_array($post_default_category, $cat_arr)) {
                                $post_default_category = $cat_arr[0];
                                geodir_save_post_meta($post_id, 'default_category', $post_default_category);
                            }
                            geodir_set_postcat_structure($post_id, $post . 'category', $post_default_category, '');
                        }
                        $post_images = $wpdb->get_results($wpdb->prepare("SELECT * FROM " . GEODIR_ATTACHMENT_TABLE . " WHERE `post_id`=%d order by menu_order asc", array($post_id)));
                        $count_post_images = count($post_images);
                        if ($image_limit != 0 && $image_limit < $old_image_limit && $count_post_images > $image_limit) {
                            $post_images_arr = array_slice($post_images, $image_limit, $image_limit);
                            foreach ($post_images_arr as $perimage) {
                                if (file_exists($upload_basedir . $perimage->file)) {
                                    unlink($upload_basedir . $perimage->file);
                                }
                                $wpdb->query($wpdb->prepare("DELETE FROM " . GEODIR_ATTACHMENT_TABLE . " WHERE ID=%d", array($perimage->ID)));
                            }
                        }
                        $expire_date = 'Never';
                        if ((int) $days > 0 && $days != '') {
                            $expire_date = date('Y-m-d', strtotime(date('Y-m-d') . "+" . (int) $days . " days"));
                        }
                        geodir_save_post_meta($post_id, 'is_featured', $featured);
                        geodir_save_post_meta($post_id, 'package_id', $downgrade_pkg);
                        geodir_save_post_meta($post_id, 'paid_amount', '');
                        geodir_save_post_meta($post_id, 'paymentmethod', '');
                        geodir_save_post_meta($post_id, 'alive_days', $days);
                        geodir_save_post_meta($post_id, 'expire_date', $expire_date);
                        geodir_save_post_meta($post_id, 'expire_notification', 'false');
                        $post_info = get_post($post_id);
                        if (!empty($post_info) && isset($post_info->post_status) && $post_info->post_status != 'publish') {
                            $update_post = array();
                            $update_post['post_status'] = 'publish';
                            $update_post['ID'] = $post_id;
                            wp_update_post($update_post);
                        }
                    } else {
                        $post_info = get_post($post_id);
                        $post_ex_status = get_option('geodir_listing_ex_status');
                        if (!empty($post_info) && isset($post_info->post_status) && $post_info->post_status != $post_ex_status) {
                            $expire_post = array();
                            $expire_post['post_status'] = $post_ex_status;
                            $expire_post['ID'] = $post_id;
                            wp_update_post($expire_post);
                            // update post expiry status
                        }
                    }
                }
            }
        }
    }
}
function geodir_ipn_handler_2co()
{
    global $Cart, $General;
    $req = '';
    foreach ($_POST as $field => $value) {
        $ipnData["{$field}"] = $value;
        $req .= "&{$field}={$value}";
    }
    $postid = intval($ipnData['x_invoice_num']);
    $pnref = $ipnData['x_trans_id'];
    $amount = geodir_get_currency_sym() . doubleval($ipnData['x_amount']);
    $result = intval($ipnData['x_response_code']);
    $respmsg = $ipnData['x_response_reason_text'];
    $customer_email = $ipnData['x_email'];
    $customer_name = $ipnData['x_first_name'];
    $fromEmail = get_option('site_email');
    $fromEmailName = get_site_emailName();
    $subject = "Acknowledge for Place Listing ID #{$postid} payment";
    // get current post status
    $current_post_status = get_post_status($postid);
    if ($result == '1') {
        /* Valid IPN transaction.*/
        $post_default_status = geodir_new_post_default_status();
        if ($post_default_status == '') {
            $post_default_status = 'publish';
        }
        set_property_status($postid, $post_default_status);
        $productinfosql = $wpdb->prepare("select ID,post_title,guid,post_author from {$wpdb->posts} where ID = %d", array($postid));
        $productinfo = $wpdb->get_results($productinfosql);
        foreach ($productinfo as $productinfoObj) {
            $post_title = '<a href="' . $productinfoObj->guid . '">' . $productinfoObj->post_title . '</a>';
            $aid = $productinfoObj->post_author;
            $userInfo = get_author_info($aid);
            $to_name = $userInfo->user_nicename;
            $to_email = $userInfo->user_email;
        }
        $message = __('<p>payment for Place Listing ID #' . $postid . ' confirmation.<br></p><p><b>You may find the details below:</b></p><p>----</p><p>Place Listing Id : ' . $postid . '</p><p>Place Listing Title : ' . $post_title . '</p><p>User Name : ' . $to_name . '</p><p>User Email : ' . $to_email . '</p><p>Paid Amount :       ' . $amount . '</p><p>Transaction ID :       ' . $pnref . '</p><p>Result Code : ' . $result . '</p><p>Response Message : ' . $respmsg . '</p><p>----</p><br><br><p>Thank you.</p>', GEODIRPAYMENT_TEXTDOMAIN);
        $subject = get_option('post_payment_success_admin_email_subject');
        if (!$subject) {
            $subject = __("Place Listing Submitted and Payment Success Confirmation Email", GEODIRPAYMENT_TEXTDOMAIN);
        }
        $content = get_option('post_payment_success_admin_email_content');
        $store_name = get_option('blogname');
        $fromEmail = get_option('site_email');
        $search_array = array('[#to_name#]', '[#information_details#]', '[#site_name#]');
        $replace_array = array($fromEmail, $message, $store_name);
        /*$message = str_replace($search_array,$replace_array,$content);*/
        geodir_payment_adminEmail($postid, $aid, 'payment_success', $message);
        /* email to admin*/
        geodir_payment_clientEmail($postid, $aid, 'payment_success', $message);
        /* email to client*/
        /*@wp_mail($fromEmail,$subject,$message,$headerarr);*/
        /* email to admin*/
        // Extend expire date start
        $invoice_info = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . INVOICE_TABLE . " WHERE post_id = %d AND is_current=%s", array($postid, '1')));
        $invoice_id = $invoice_info->id;
        $invoice_package_id = '';
        if (!empty($invoice_info) && isset($invoice_info->package_id)) {
            $invoice_package_id = $invoice_info->package_id;
            $invoice_alive_days = $invoice_info->alive_days;
            $invoice_status = $invoice_info->status;
        }
        $geodir_post_info = geodir_get_post_info($postid);
        if (!empty($geodir_post_info)) {
            $post_package_id = $geodir_post_info->package_id;
            $post_expire_date = $geodir_post_info->expire_date;
            if (!empty($invoice_package_id) && $invoice_alive_days > 0 && $invoice_package_id == $post_package_id && strtolower($post_expire_date) != 'never' && strtotime($post_expire_date) >= strtotime(date('Y-m-d')) && $current_post_status == 'publish') {
                $alive_days = (int) ($geodir_post_info->alive_days + $invoice_alive_days);
                $expire_date = date('Y-m-d', strtotime($post_expire_date . "+" . $invoice_alive_days . " days"));
            } else {
                $alive_days = (int) $geodir_post_info->alive_days;
                if (strtolower($post_expire_date) != 'never' && strtotime($post_expire_date) < strtotime(date('Y-m-d'))) {
                    $alive_days = $invoice_alive_days;
                }
                $expire_date = $alive_days > 0 ? date('Y-m-d', strtotime(date('Y-m-d') . "+" . $alive_days . " days")) : 'Never';
            }
            geodir_save_post_meta($postid, 'alive_days', $alive_days);
            geodir_save_post_meta($postid, 'expire_date', $expire_date);
        }
        // Extend expire date start	end
        /*############ SET THE INVOICE STATUS START ############*/
        // update invoice statuse and transactio details
        $transaction_details = $message;
        geodir_update_invoice_status($invoice_id, 'Paid');
        geodir_update_invoice_transaction_details($invoice_id, $transaction_details);
        /*############ SET THE INVOICE STATUS END ############*/
        if ($ct_on && file_exists($child_dir . '/library/includes/success.php')) {
            include_once $child_dir . '/library/includes/success.php';
        } else {
            include_once TEMPLATEPATH . '/library/includes/success.php';
        }
        exit;
        return true;
    } else {
        if ($result != '1') {
            $message = __('<p>payment for Place Listing ID #' . $postid . ' incompleted.<br>because of ' . $respmsg . '</p><p><b>You may find the details below:</b></p><p>----</p><p>Place Listing Id : ' . $postid . '</p><p>Place Listing Title : ' . $post_title . '</p><p>User Name : ' . $to_name . '</p><p>User Email : ' . $to_email . '</p><p>Paid Amount :       ' . $amount . '</p><p>Transaction ID :       ' . $pnref . '</p><p>Result Code : ' . $result . '</p><p>Response Message : ' . $respmsg . '</p><p>----</p><br><br><p>Thank you.</p>', GEODIRPAYMENT_TEXTDOMAIN);
            $subject = get_option('post_payment_success_client_email_subject');
            if (!$subject) {
                $subject = __("Place Listing Submitted and Payment Success Confirmation Email", GEODIRPAYMENT_TEXTDOMAIN);
            }
            $content = get_option('post_payment_success_client_email_content');
            $store_name = get_option('blogname');
            $search_array = array('[#to_name#]', '[#information_details#]', '[#site_name#]');
            $replace_array = array($to_name, $message, $store_name);
            /*$message = str_replace($search_array,$replace_array,$content);*/
            geodir_payment_adminEmail($postid, $aid, 'payment_success', $message);
            /* email to admin*/
            geodir_payment_clientEmail($postid, $aid, 'payment_success', $message);
            /* email to client*/
            /*@wp_mail($to_email,$subject,$message,$headerarr);*/
            /* email to client*/
            /*############ SET THE INVOICE STATUS START ############*/
            global $wpdb;
            $invoice_info = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . INVOICE_TABLE . " WHERE post_id = %d AND is_current=%s", array($postid, '1')));
            $invoice_id = $invoice_info->id;
            // update invoice statuse and transactio details
            $transaction_details = $message;
            geodir_update_invoice_status($invoice_id, 'Unpaid');
            geodir_update_invoice_transaction_details($invoice_id, $transaction_details);
            /*############ SET THE INVOICE STATUS END ############*/
            if ($ct_on && file_exists($child_dir . '/library/includes/success.php')) {
                include_once $child_dir . '/library/includes/success.php';
            } else {
                include_once TEMPLATEPATH . '/library/includes/success.php';
            }
            exit;
            return false;
        }
    }
}