Skip to content
SpencerChang edited this page Nov 18, 2016 · 1 revision
'/asset-files/files/solutions/', 'thumbnail' => '/asset-files/images/solutions/scaled/', ]; public function __construct() { $servers = DB::connection('vsc') ->table('servers') ->select('id', 'address', 'address_map', 'web_doc_base') ->get(); foreach ($servers as $server) { $this->base[(int) $server->id] = $server->web_doc_base; $this->servers[$server->address] = (int) $server->id; foreach (explode(',', $server->address_map) as $index) { $this->servers[$index] = (int) $server->id; } } } public function getUrl($loc, $lang) { $urls = DB::connection('vsc') ->table('country') ->select('live_url', 'stage_url') ->where('locale_cc', $loc) ->where('lang_cc', $lang) ->first(); $result = new \stdClass(); foreach ($urls as $key => $url) { $parse = parse_url($url); $result->$key = $parse['host']; } return $result; } public function getFolder($loc, $lang, $mode = 'live_url', $sub = false) { $root = $this->base[$this->servers[$this->getUrl($loc, $lang)->$mode]]; $country = DB::connection('vsc') ->table('country') ->select('web_doc_dir', 'country_base_url') ->where('locale_cc', $loc) ->where('lang_cc', $lang) ->first(); return $sub ? $root.$country->web_doc_dir.$country->country_base_url : $root.$country->web_doc_dir; } public function isSameServer($remote) { $local = strtolower($_SERVER['SERVER_NAME']); if ($this->servers[$local] == $this->servers[$remote]) { return true; } return false; } public function getCountry() { $result = []; $data = DB::connection('vsc') ->table('country') ->select('locale_cc', 'lang_cc', 'english_name') ->where('status', 1) ->get(); foreach ($data as $value) { $index = $value->locale_cc.'_'.$value->lang_cc; $result[$index] = $value->english_name; } return $result; } public function getDBName($loc, $lang) { return DB::connection('vsc') ->table('country') ->where('locale_cc', $loc) ->where('lang_cc', $lang) ->pluck('db'); } public function isFieldExists($dbName, $field, $value) { $count = DB::connection($dbName)->table('solutions')->where($field, $value)->count(); return $count > 0 ? true : false; } public function isTitleExists($value) { $count = DB::table('solutions')->where('title', $value)->count(); return $count > 0 ? true : false; } public function getData($id) { return DB::table('solutions')->where('id', $id)->first(); } public function dbCopy($id, $loc, $lang, $mode = 'live_url', $override = false) { $remote = $this->getUrl($loc, $lang)->$mode; // local if ($this->isSameServer($remote)) { $data = DB::table('solutions')->where('id', $id)->first(); $dbName = $this->getDBName($loc, $lang); if ($override && $this->isFieldExists($dbName, 'title', $data->title)) { unset($data->id); return DB::connection($dbName)->table('solutions')->where('title', $data->title)->update((array) $data); } else { if ($this->isFieldExists($dbName, 'id', $id)) { unset($data->id); } $data->title = $data->title.'(Copy-'.date("Y-m-d").')'; return DB::connection($dbName)->table('solutions')->insertGetId((array) $data); } // remote } else { } return false; } public function mirrorFile($loc, $lang, $src, $mode = 'live_url', $override = false, $sub = false) { $remote = $this->getUrl($loc, $lang)->$mode; // local if ($this->isSameServer($remote)) { $dst = $this->getFolder($loc, $lang, $mode, $sub).str_replace($_SERVER['DOCUMENT_ROOT'], '', $src); $dir = pathinfo($dst, PATHINFO_DIRNAME); if (!is_dir($dir)) { mkdir($dir, 0775, true); } if (false === strpos($src, $_SERVER['DOCUMENT_ROOT'])) { $src = $_SERVER['DOCUMENT_ROOT'].$src; } if (is_dir($src)) { return $this->copyDirectory($src, $dst); } elseif (is_file($src)) { if ($override) { @unlink($dst); } return copy($src, $dst); } // remote } else { } return false; } public function extractFilesInHtml($html) { $result = []; $dom = new \DOMDocument(); if (@$dom->loadHTML($html)) { $imgs = $dom->getElementsByTagName('img'); foreach ($imgs as $img) { $result[] = $img->getAttribute('src'); } } return $result; } public function extractDataInHtml_old($html, $detail = false, $select = []) { $result = []; $dom = new \DOMDocument(); if (@$dom->loadHTML($html)) { $xpath = new \DOMXPath($dom); $list = $xpath->query("//*[contains(@class, 'docCaption')]"); foreach ($list as $key => $item) { $index = $key + 1; if (!empty($select) && !in_array($index, $select)) { continue; } $title = trim($item->textContent); $view = $xpath->query("//*[@id='resources']/div[2]/div[$index]/div[2]/a[2]")->item(0); if (!empty($view) && !$this->isTitleExists($title)) { $product = $xpath->query("//*[@id='resources']/div[2]/div[$index]/div[1]/p/span[1]")->item(0); $file = $xpath->query("//*[@id='resources']/div[2]/div[$index]/div[1]/p/span[2]")->item(0); $business = $xpath->query("//*[@id='resources']/div[2]/div[$index]/div[1]/p/span[3]")->item(0); $result[$index]['title'] = $title; $result[$index]['view'] = $view->attributes->item(0)->value; $result[$index]['product_type'] = $this->getType('product_type', trim($product->textContent)); $result[$index]['file_type'] = $this->getType('file_type', trim($file->textContent)); $result[$index]['business_type'] = $this->getType('business_type', trim($business->textContent)); if ($detail) { $pdf = $xpath->query("//*[@id='resources']/div[2]/div[$index]/div[2]/a[1]")->item(0); $img = $xpath->query("//*[@id='resources']/div[2]/div[$index]/div[1]/a/img")->item(0); $content = $this->getHtmlByClass($this->getHtmlByURL($view->attributes->item(0)->value), 'std'); //$content = str_replace('http://www.viewsonic.com/media/wysiwyg/', '/asset-files/images/solutions/', $content); $result[$index]['pdf'] = $pdf->attributes->item(0)->value; $result[$index]['thumbnail'] = $img->attributes->item(0)->value; $result[$index]['snapshot'] = $img->attributes->item(0)->value; $result[$index]['content'] = $content; unset($result[$index]['view']); } } } } return $result; } public function extractDataInHtml($html ='', $detail = false, $select = []) { $menu_show = [ 'Business Type' => [ 'Business' => 'business', 'Education' => 'education', 'Healthcare' => 'healthcare', 'Home' => 'home', 'Public Display' => 'public_display' ], 'Product Type' => [ 'Commercial Display' => 'commercial_display', 'Desktop Virtualization' => 'desktop_virtualization', 'Projectors' => 'projectors', 'Monitors' => 'monitors' ], 'File Type' => [ 'Solution Briefs' => 'solution_briefs', 'Case Studies' => 'case_studies', 'White Papers' => 'white_papers', 'Videos' => 'videos' ] ]; $menus = [ 'solution_business_type' => [ 'business', 'education', 'healthcare', 'home', 'public_display' ], 'solution_product_type' => [ 'commercial_display', 'desktop_virtualization', 'projectors', 'monitors' ], 'solution_file_type' => [ 'solution_briefs', 'case_studies', 'white_papers', 'videos' ] ]; // $menus_type = [ // 'solution_business_type' => 'business_type', // 'solution_product_type' => 'product_type', // 'solution_file_type' => 'file_type' // ] $url = 'http://www.viewsonic.com/us/solution'; $result = []; $dom = new \DOMDocument(); foreach($menus as $main_menu => $menu){ foreach($menu as $sub_menu){ $url = $url.'?'.$main_menu.'='.$sub_menu; $html = $this->getHtmlByURL($url); if (@$dom->loadHTML($html)) { $xpath = new \DOMXPath($dom); $pages = $xpath->query("//a[contains(@class, 'last')]"); $last_page = $pages->item(0)->textContent; for($page =1;$page<=$last_page;$i++){ if($page%2==1){ $url = $url.'&page='.$page; $html = $this->getHtmlByURL($url); if (@$dom->loadHTML($html)) { $xpath = new \DOMXPath($dom); $list = $xpath->query("//*[contains(@class, 'vs-product-grid-content')]"); foreach ($list as $key => $item) { // $index = $key + 1; if (!empty($select) && !in_array($index, $select)) { continue; } $title = trim($item->textContent); $view = $item->getElementsByTagName("a")->item(0)->getAttribute("href"); $id = $item->getElementsByTagName("a")->item(0)->getAttribute("rel"); if (!empty($view) && !$this->isTitleExists($title)) { $result[$id]['title'] = $title; $result[$id]['view'] = $view; switch ($main_menu) { case 'solution_business_type': $result[$id]['business_type'] = $this->getType($main_menu, $sub_menu); break; case 'solution_product_type': $result[$id]['product_type'] = $this->getType($main_menu, $sub_menu); break; case 'solution_file_type': $result[$id]['file_type'] = $this->getType($main_menu, $sub_menu); break; } if ($detail) { $img = $item->getElementsByTagName("img")->item(0)->getAttribute("src"); $content = $this->getHtmlByClass($this->getHtmlByURL($view), 'solution-page-wrapper'); $result[$id]['pdf'] = $id; $result[$id]['pdf_form_key'] = $xpath->query("//*[contains(@id, 'solution-download-form')]/input[2]")->item(0)->getAttribute("value"); $result[$id]['thumbnail'] = $img; $result[$id]['snapshot'] = $img; $result[$id]['content'] = $content; $result[$id]['show_date'] = $item->getElementsByTagName("span")->item(1)->textContent; } } } } } } } return $result; } } return $result; } public function insertGetId($data) { return DB::table('solutions')->insertGetId($data); } public function downloadFile($url, $type = "") { set_time_limit(0); ini_set('memory_limit', '2048M'); if (isset($this->filePath[$type])) { $filePath = $_SERVER['DOCUMENT_ROOT'].$this->filePath[$type].pathinfo($url, PATHINFO_BASENAME); } else { $filePath = $_SERVER['DOCUMENT_ROOT'].str_replace('http://www.viewsonic.com/media/wysiwyg/', '/asset-files/images/solutions/', $url); } $dir = pathinfo($filePath, PATHINFO_DIRNAME); if (!is_dir($dir)) { mkdir($dir, 0775, true); } $file = file_get_contents($url); $download = file_put_contents($filePath, $file); return $download > 0 ? $this->filePath[$type].pathinfo($url, PATHINFO_BASENAME) : false; } public function getType($type, $value) { $filter = [ 'solution_business_type' => ['', 'business', 'education', 'healthcare', 'home', 'public_display'], 'solution_product_type' => ['', 'commercial_display', 'desktop_virtualization', 'projectors', 'monitors'], 'solution_file_type' => ['', 'solution_briefs', 'case_studies', 'white_papers','videos'] ]; return array_search($value, $filter[$type]); } public function getHtmlByClass($html, $className) { $dom = new \DOMDocument(); if (@$dom->loadHTML($html)) { $xpath = new \DOMXPath($dom); $content = $xpath->query("//*[contains(@class, '$className')]")->item(0); return $dom->savehtml($content); } return false; } public function getHtmlByURL($url) { $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $ret = curl_exec($ch); curl_close($ch); return $ret; } }
Clone this wiki locally