<?php
        set_time_limit(0);
        ini_set('memory_limit', '512M');
        ini_set('default_charset', 'utf-8');

        require_once 'lib/Goutte/goutte.phar';

use Goutte\Client;

$client = new Client();
        $crawler = $client->request('GET', 'http://lolzbook.com/');

        if ($_SERVER['HTTP_HOST'] == 'localhost') {
            $host = "localhost";
            $db = "wordpress";
            $user = "root";
            $pass = "";
        } else {
            $host = "localhost";
            $db = "wpwp";
            $user = "wpwp";
            $pass = "A7AqmCMc";
        }

        $conn = new PDO("mysql:host=$host;dbname=$db", $user, $pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
//categories
        $temp_categories = $crawler->filter('header ul li.cat-item')->each(function ($node, $i) {
                    return $node->text();
                });
        $temp_link = $crawler->filter('header ul li.cat-item')->each(function ($node, $i) {
                    return $node->selectLink($node->text())->link()->getUri();
                });
                
//categories links
        $categories = array();
        foreach ($temp_categories as $key => $value) {
            //$url = 'http://lolzbook.com/category/' . $value . '/';
            array_push($categories, array('name' => $value, 'link' => $temp_link[$key]));
        }
        echo '<pre>';
        print_r($categories);
        echo '</pre>';
        echo '<br>============================<br>';
        
        foreach ($categories as $key => $category) {

            if ((isset($_REQUEST['cate']) && $key >= $_REQUEST['cate'] - 1) || !isset($_REQUEST['cate'])) {
                //$cate_no = $_REQUEST['cate'];
                //unset($_REQUEST['cate']);

                $crawler = $client->request('GET', $category['link']);
                $pages = $crawler->filter('nav.post-nav ul li')->eq(4)->text();
                $folder_name = preg_replace('/ +/', '-', strtolower($category['name']));              
                
                $folder_url = '../wp-content/uploads/' . $folder_name;
                if (!file_exists($folder_url)) {
                    mkdir($folder_url,0777, true);
                }

                $conn = new PDO("mysql:host=$host;dbname=$db", $user, $pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
                $stmt = $conn->prepare('SELECT * FROM wp_terms Left JOIN wp_term_taxonomy
ON wp_terms.term_id = wp_term_taxonomy.term_id WHERE wp_terms.name = :name');
                $stmt->bindValue(":name", $category['name']);
                $stmt->execute();
                $result = $stmt->fetchAll(PDO::FETCH_ASSOC);

                if (count($result) == 0) {
                    //insert term record
                    $insertsql = "INSERT INTO wp_terms (name, slug, term_group) VALUES (:name, :slug, :term_group)";
                    $query = $conn->prepare($insertsql);
                    $query->bindValue(":name", $category['name']);
                    $query->bindValue(":slug", $folder_name);
                    $query->bindValue(":term_group", 0);
                    $query->execute();
                    //term id
                    $term_id = $conn->lastInsertId();

                    //insert taxonomy record
                    $insertsql = "INSERT INTO wp_term_taxonomy (term_id, taxonomy, description, parent, count) VALUES (:term_id, :taxonomy, :description, :parent ,:count)";
                    $query = $conn->prepare($insertsql);
                    $query->bindValue(":term_id", $term_id);
                    $query->bindValue(":taxonomy", 'category');
                    $query->bindValue(":description", '');
                    $query->bindValue(":parent", 0);
                    $query->bindValue(":count", 0);
                    $query->execute();
                    //taxonomy id
                    $taxonomy_id = $conn->lastInsertId();
                } else {
                    if (isset($result[0]['term_taxonomy_id']))
                        $taxonomy_id = $result[0]['term_taxonomy_id'];
                }

                echo '<br>Category Name = ' . $category['name'] . '<br>';
                echo '<br>===============<br>';

                if (isset($_REQUEST['page']) && $_REQUEST['page'] != 0) {
                    $page_no = $_REQUEST['page'];
                    unset($_REQUEST['page']);
                } else {
                    $page_no = 1;
                }
                
                for ($index = $page_no; $index <= $pages; $index++) {
                    $page_url = $category['link'] . '/page/' . $index . '/';
                    $crawler = $client->request('GET', $page_url);

                    $posts = $crawler->filter('article.post')->each(function ($node, $i) {
                                global $taxonomy_id;
                                global $folder_name;
                                global $index;
                                //return $node->text();
                                try {
                                    $title = $node->filter('header.entry-header a')->text();
                                    $imge = $node->filter('div.entry-content img')->attr('src');
                                    echo $title . '<br>';
                                    echo $imge . '<br>';
                                    //move image
                                    $image_url = getImage($imge, $folder_name);

                                    if ($image_url != False) {

                                        $image_name = filename_from_uri($image_url);


                                        if ($_SERVER['HTTP_HOST'] == 'localhost') {
                                            $host = "localhost";
                                            $db = "wordpress";
                                            $user = "root";
                                            $pass = "";
                                        } else {
                                            $host = "localhost";
                                            $db = "wpwp";
                                            $user = "wpwp";
                                            $pass = "A7AqmCMc";
                                        }

                                        try {
                                            $conn = new PDO("mysql:host=$host;dbname=$db", $user, $pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
                                            $stmt = $conn->prepare('SELECT * FROM wp_posts WHERE post_title = :title');
                                            $stmt->bindValue(":title", $title);
                                            $stmt->execute();
                                            $result = $stmt->fetchAll();
                                            if (count($result) == 0) {
                                                $content = '<img src="http://' . $_SERVER['HTTP_HOST'] . '/wp-content/uploads/' . $folder_name . '/' . $image_name . '" alt="Untitled" class="size-large" />';

                                                //insert post record
                                                $insertsql = "INSERT INTO wp_posts (post_author, post_date, post_date_gmt, post_title, post_content, post_status,comment_status,ping_status,post_name, post_modified, post_modified_gmt, post_type) VALUES (:post_author, :post_date, :post_date_gmt, :post_title, :post_content, :post_status, :comment_status, :ping_status, :post_name, :post_modified, :post_modified_gmt, :post_type)";
                                                $query = $conn->prepare($insertsql);
                                                $query->bindValue(":post_author", 1);
                                                $query->bindValue(":post_date", date('Y-m-d g:i:s'));
                                                $query->bindValue(":post_date_gmt", date('Y-m-d g:i:s'));
                                                $query->bindValue(":post_title", $title);
                                                $query->bindValue(":post_content", $content);
                                                $query->bindValue(":post_status", "publish");
                                                $query->bindValue(":comment_status", "open");
                                                $query->bindValue(":ping_status", "open");
                                                $query->bindValue(":post_name", clean($title));
                                                $query->bindValue(":post_modified", date('Y-m-d g:i:s'));
                                                $query->bindValue(":post_modified_gmt", date('Y-m-d g:i:s'));
                                                $query->bindValue(":post_type", "post");
                                                $query->execute();

                                                //post id
                                                $post_id = $conn->lastInsertId();

                                                $image_site = getimagesize($image_url);
                                                //insert image post record
                                                $insertsql = "INSERT INTO wp_posts (post_author, post_date, post_date_gmt, post_title, post_status,comment_status,ping_status,post_name, post_modified, post_modified_gmt, post_type, post_mime_type) VALUES (:post_author, :post_date, :post_date_gmt, :post_title, :post_status, :comment_status, :ping_status, :post_name, :post_modified, :post_modified_gmt, :post_type, :post_mime_type)";
                                                $query = $conn->prepare($insertsql);
                                                $query->bindValue(":post_author", 1);
                                                $query->bindValue(":post_date", date('Y-m-d g:i:s'));
                                                $query->bindValue(":post_date_gmt", date('Y-m-d g:i:s'));
                                                $query->bindValue(":post_title", $title . " image");
                                                $query->bindValue(":post_status", "inherit");
                                                $query->bindValue(":comment_status", "open");
                                                $query->bindValue(":ping_status", "open");
                                                $query->bindValue(":post_name", clean($title . " image"));
                                                $query->bindValue(":post_modified", date('Y-m-d g:i:s'));
                                                $query->bindValue(":post_modified_gmt", date('Y-m-d g:i:s'));
                                                $query->bindValue(":post_type", "attachment");
                                                $query->bindValue(":post_mime_type", $image_site['mime']);
                                                $query->execute();
                                                //image post id
                                                $image_post_id = $conn->lastInsertId();

                                                //insert image post meta record
                                                $insertsql = "INSERT INTO wp_postmeta (post_id, meta_key, meta_value) VALUES (:post_id, :meta_key, :meta_value)";
                                                $query = $conn->prepare($insertsql);
                                                $query->bindValue(":post_id", $image_post_id);
                                                $query->bindValue(":meta_key", "_wp_attached_file");
                                                $query->bindValue(":meta_value", $image_name);
                                                $query->execute();

                                                $image_meta = array(
                                                    'width' => $image_site[0],
                                                    'height' => $image_site[1],
                                                    'file' => $image_name,
                                                    'sizes' => array(
                                                        'thumbnail' => array(
                                                            'file' => $image_name,
                                                            'width' => $image_site[0],
                                                            'height' => $image_site[1],
                                                            'mime-type' => $image_site['mime']
                                                        ),
                                                        'medium' => array(
                                                            'file' => $image_name,
                                                            'width' => $image_site[0],
                                                            'height' => $image_site[1],
                                                            'mime-type' => $image_site['mime']
                                                        ),
                                                        'large' => array(
                                                            'file' => $image_name,
                                                            'width' => $image_site[0],
                                                            'height' => $image_site[1],
                                                            'mime-type' => $image_site['mime']
                                                        ),
                                                        'post_thumbnail' => array(
                                                            'file' => $image_name,
                                                            'width' => $image_site[0],
                                                            'height' => $image_site[1],
                                                            'mime-type' => $image_site['mime']
                                                        )
                                                    ),
                                                    'image_meta' => array(
                                                        'aperture' => 0,
                                                        'credit' => '',
                                                        'camera' => '',
                                                        'caption' => '',
                                                        'created_timestamp' => 0,
                                                        'copyright' => '',
                                                        'focal_length' => 0,
                                                        'iso' => 0,
                                                        'shutter_speed' => 0,
                                                        'title' => ''
                                                    )
                                                );
                                                //insert image post meta record
                                                $insertsql = "INSERT INTO wp_postmeta (post_id, meta_key, meta_value) VALUES (:post_id, :meta_key, :meta_value)";
                                                $query = $conn->prepare($insertsql);
                                                $query->bindValue(":post_id", $image_post_id);
                                                $query->bindValue(":meta_key", "_wp_attachment_metadata");
                                                $query->bindValue(":meta_value", serialize($image_meta));
                                                $query->execute();
//                            //insert post meta record
//                            $insertsql = "INSERT INTO wp_postmeta (post_id, meta_key, meta_value) VALUES (:post_id, :meta_key, :meta_value)";
//                            $query = $conn->prepare($insertsql);
//                            $query->bindValue(":post_id", $post_id);
//                            $query->bindValue(":meta_key", "_thumbnail_id");
//                            $query->bindValue(":meta_value", $image_post_id);
//                            $query->execute();
                                                //insert post category relationship record
                                                $insertsql = "INSERT INTO wp_term_relationships (object_id, term_taxonomy_id, term_order) VALUES (:object_id, :term_taxonomy_id, :term_order)";
                                                $query = $conn->prepare($insertsql);
                                                $query->bindValue(":object_id", $post_id);
                                                $query->bindValue(":term_taxonomy_id", $taxonomy_id);
                                                $query->bindValue(":term_order", 0);
                                                $query->execute();
                                                echo '<br>Page No = ' . $index . '<br>';
                                                echo '<br>------------------<br>';
                                            }
                                        } catch (PDOException $e) {
                                            echo 'ERROR: ' . $e->getMessage();
                                        }
                                    }//end of if
                                } catch (Exception $e) {
                                    echo 'ERROR: ' . $e->getMessage();
                                }
                            });
                }
                die();
            }
        }

		function clean($string) {
		   $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
		   return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
		}		
		
        function getImage($get_image_url, $new_folder_name) {
            //echo filename_from_uri($url);
            //echo $url;
            $parts = explode('/', $get_image_url);
            $filename = array_pop($parts);

//    $ch = curl_init($url);
//    $fp = fopen('/uploads/'.$filename, 'wb');
//    curl_setopt($ch, CURLOPT_FILE, $fp);
//    curl_setopt($ch, CURLOPT_HEADER, 0);
//    curl_exec($ch);
//    curl_close($ch);
//    fclose($fp);

            $img = '../wp-content/uploads/' . $new_folder_name . '/' . $filename;
            $file = @file_get_contents($get_image_url);
            if (false === $file) {
                return False;
            } else {
                if (!file_put_contents($img, $file)) {
                    return False;
                } else {
                    return $img;
                }
            }
        }

        function filename_from_uri($url) {
            if ($url != '') {
                $parts = explode('/', $url);
                return array_pop($parts);
            } else {
                return '';
            }
        }
