Example #1
0
/**
 * Tach chuoi tu khoa thanh cum 2 tu
**/
function gen_keyword_search($string){
   $string  = cut_string($string,50,"");
   $string  = replaceFCK($string,1);
   $text    = str_convert_num_code_to_char($string);    
   $text                  = mb_strtolower($text,"UTF-8"); 
   $code_entities_match   = array('&mdash;',' ','--','&quot;','!','@','#','$','%','^','&','*','(',')','_','+','{','}','|',':','"','<','>','?','[',']','\\',';',"'",',','.','/','*','+','~','`','=', '“','”',"'", '‘', '’', '--'); 
   $code_entities_replace = array('','-','-','','','','','','','','','','','','','','','','','','-','','','','','','', '','','','','','-'); 
   $title = str_replace($code_entities_match, $code_entities_replace, $text); 
   $arr_place  = array("     ","    ","   ","  ");
   $title      = str_replace($arr_place," ",$title);
   $title      = str_replace($arr_place," ",$title);
   $title      = str_replace("-"," ",$title);
   $array_text = explode(" ",$title);
   $arrKey     = array();
   for($i = 0; $i < count($array_text) - 1; $i++){
      $keyword  = $array_text[$i] . " " . $array_text[$i + 1];
      $keyword  = trim($keyword);
      if(strlen($keyword) > 4){
         $arrKey[] = $keyword;
      }      
   }   
   unset($array_text);
   return $arrKey;
}
	/**
	Làm sạch HTML bằng DOMDocument
	*/
	function DOMDocument_cleanup(){
		//Khởi tạo 1 DOM Document mới
		$this->DOMDoc = new DOMDocument("1.0", "UTF-8");
		
		//Cho thẻ HTML, meta UTF8, <body> vào DOM để tránh lỗi khi loadHTML
		$this->input_html = 	'<html>' . 
										'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">' . 
										'<body>' .
											$this->input_html . 
										'</body>' . 
								  	'</html>';
		//Load input HTML vào DOM Document, dùng @ để tránh lỗi
		@$this->DOMDoc->loadHTML($this->input_html);
		
		//Loại bỏ các tag không cho phép
		$this->DOMDocument_cleanup_tag();
		
		//Loại các thẻ tr, td, th đứng 1 mình ko có cha
		$this->DOMDocument_cleanup_missing_parent_tr_td();
		
		//Loại các attribute không được phép
		$this->DOMDocument_cleanup_attribute();

		//Trả lại input chuỗi HTML đã validate xong		
		$this->input_html = $this->DOMDoc->saveHTML();

		// Replace các ký tự FCK sang UTF-8
		$this->input_html	= replaceFCK($this->input_html, 1);
		
		
		
		//Tìm đến đầu body và /body để cắt chuỗi
		$start_pos 	= strpos($this->input_html,"<body>");
		$end_pos 	= strpos($this->input_html,"</body>");
		
		// Không tìm thấy vị trí thẻ body thì trả về chuỗi rỗng
		if($start_pos === false) $this->input_html	= "";
		else $this->input_html = substr($this->input_html, $start_pos + 6, $end_pos - $start_pos - 6);
	}
function pre_cron_news_detail($link_data = array('lin_id' => '', 'lin_url' => '', 'lin_cat_id' => '', 'law_detail_title' => '', 'law_detail_content' => '', 'law_detail_teaser' => '', 'law_detail_tag' => '', 'law_detail_remove' => ''), $accept_no_image = 0)
{
    $link_id = $link_data['lin_id'];
    $link_url = $link_data['lin_url'];
    $link_domain = get_domain($link_url);
    $law_title = get_law_index($link_data['law_detail_title']);
    $law_content = get_law_index($link_data['law_detail_content']);
    $law_teaser = get_law_index($link_data['law_detail_teaser']);
    $law_tag = get_law_index($link_data['law_detail_tag']);
    $law_remove = get_law_remove($link_data['law_detail_remove']);
    if (!check_url_status($link_url, array(200, 301, 304, 302))) {
        //cap nhat link ko lay duoc
        if ($link_id) {
            $db_update = new db_execute('UPDATE links SET lin_status = ' . LINK_STATUS_FAIL . ' WHERE lin_id = ' . $link_id);
            unset($db_update);
        }
        $array_return = array('success' => 0, 'error' => 'Link không tồn tại hoặc đã bị xóa : ' . $link_url, 'error_code' => 1);
        return $array_return;
    }
    $html_content = curl_get_content($link_url);
    $html_content = str_get_html($html_content);
    //lấy ảnh trước - nếu ko có ảnh thì die()
    //lưu ảnh đại diện
    $image = trim($html_content->find('[property="og:image"]', 0)->content);
    $array_save_image = save_image_url($image);
    if ($array_save_image['error'] || !$array_save_image['name']) {
        if (!$accept_no_image && $link_id) {
            $db_update = new db_execute('UPDATE links SET lin_status = ' . LINK_STATUS_FAIL . ' WHERE lin_id = ' . $link_id);
            unset($db_update);
            $array_return = array('success' => 0, 'error' => 'Tin không có ảnh ' . $link_url, 'error_code' => 1);
            return $array_return;
        }
    }
    //Lấy tiêu đề
    $title = $html_content->find($law_title['law'], $law_title['index'])->innertext;
    if (!$title) {
        //lỗi không lấy được tin
        //cập nhật link ko lấy được
        if ($link_id) {
            $db_update = new db_execute('UPDATE links SET lin_status = ' . LINK_STATUS_FAIL . ' WHERE lin_id = ' . $link_id);
            unset($db_update);
        }
        $array_return = array('success' => 0, 'error' => 'Link không có tiêu đề hoặc luật đã bị thay đổi : ' . $link_url, 'error_code' => 1);
        return $array_return;
    }
    //xử lý remove các content như quảng cáo...
    foreach ($law_remove as $remove) {
        $j = 0;
        foreach ($html_content->find($remove['law']) as $elm_rm) {
            if ($j == $remove['index']) {
                $elm_rm->outertext = '';
                break;
            }
            $j++;
        }
    }
    //lấy content
    $content = $html_content->find($law_content['law'], $law_content['index'])->innertext;
    //lấy ảnh đại diện
    //lấy tag
    $tags_html = $html_content->find($law_tag['law'], $law_tag['index']);
    $tag_string = '';
    if ($tags_html) {
        //các tag của bài thường được lọc theo từng thẻ a, tách text của tag từng thẻ a rồi nối vào tag_string
        foreach ($tags_html->find('a') as $t_elem) {
            $tag_string .= removeHTML(trim($t_elem->innertext)) . ',';
        }
        $tag_string = rtrim($tag_string, ',');
    }
    //lấy teaser
    $teaser = $html_content->find($law_teaser['law'], $law_content['index'])->innertext;
    $time = time();
    $active = 1;
    //xử lý dữ liệu
    $title = replaceFCK($title, 1);
    $title = replaceNCR($title);
    $title = remove_source($title);
    $title = removeHTML($title);
    $title_md5 = md5($title);
    $content = replaceFCK($content, 1);
    $content = replaceNCR($content);
    $content = remove_script($content);
    $content = removeLink($content);
    $content = remove_source($content);
    //Nếu content trống thì bỏ qua
    if (!trim($content)) {
        if ($link_id) {
            $db_update = new db_execute('UPDATE links SET lin_status = ' . LINK_STATUS_FAIL . ' WHERE lin_id = ' . $link_id);
            unset($db_update);
        }
        $array_return = array('success' => 0, 'error' => 'Nội dung trống hoặc không đúng luật ' . $link_url, 'error_code' => 1);
        unset($db_update);
        return $array_return;
    }
    $tag_string = replaceFCK($tag_string, 1);
    $tag_string = replaceNCR($tag_string);
    $tag_string = removeHTML($tag_string);
    $teaser = replaceFCK($teaser, 1);
    $teaser = replaceNCR($teaser);
    $teaser = removeHTML($teaser);
    $teaser = removeLink($teaser);
    $teaser = remove_source($teaser);
    $category = $link_data['lin_cat_id'];
    //lấy ảnh trong detail
    $all_image_detail = get_image_src_from_html($content, $link_domain);
    foreach ($all_image_detail as $key => $img_src) {
        //save anh va replace anh trong chi tiết
        $img_save = save_image_url($img_src);
        if (!$img_save['error']) {
            //đã lưu ảnh - thay thế link ảnh vào content
            $content = str_replace($img_src, $img_save['link'], $content);
        }
    }
    //echo $image;
    $image = $array_save_image['name'];
    $array_return = array('success' => 1, 'title' => $title, 'title_md5' => $title_md5, 'content' => $content, 'teaser' => $teaser, 'category' => $category, 'image' => $image, 'link_id' => $link_id, 'link_url' => $link_url, 'tag_string' => $tag_string);
    return $array_return;
}
function r($p = array('lin_id' => '', 'lin_url' => '', 'lin_cat_id' => '', 'law_detail_title' => '', 'law_detail_content' => '', 'law_detail_teaser' => '', 'law_detail_tag' => '', 'law_detail_remove' => ''), $q = 0)
{
    $r = $p['lin_id'];
    $t = $p['lin_url'];
    $u = get_domain($t);
    $v = _s($p['law_detail_title']);
    $z = _s($p['law_detail_content']);
    $aa = _s($p['law_detail_teaser']);
    $bb = _s($p['law_detail_tag']);
    $cc = s($p['law_detail_remove']);
    if (!check_url_status($t, array(200, 301, 304, 302))) {
        if ($r) {
            $dd = new db_execute('UPDATE links SET lin_status = ' . LINK_STATUS_FAIL . ' WHERE lin_id = ' . $r);
            unset($dd);
        }
        $n = array('success' => 0, 'error' => 'Link không t&#7891;n t&#7841;i ho&#7863;c &#273;ã b&#7883; xóa : ' . $t, 'error_code' => 1);
        return $n;
    }
    $ee = curl_get_content($t);
    $ee = str_get_html($ee);
    $ff = trim($ee->find('[property="og:image"]', 0)->content);
    $gg = save_image_url($ff);
    if ($gg['error'] || !$gg['name']) {
        if (!$q && $r) {
            $dd = new db_execute('UPDATE links SET lin_status = ' . LINK_STATUS_FAIL . ' WHERE lin_id = ' . $r);
            unset($dd);
            $n = array('success' => 0, 'error' => 'Tin không có &#7843;nh ' . $t, 'error_code' => 1);
            return $n;
        }
    }
    $hh = $ee->find($v['law'], $v['index'])->innertext;
    if (!$hh) {
        if ($r) {
            $dd = new db_execute('UPDATE links SET lin_status = ' . LINK_STATUS_FAIL . ' WHERE lin_id = ' . $r);
            unset($dd);
        }
        $n = array('success' => 0, 'error' => 'Link không có tiêu &#273;&#7873; ho&#7863;c lu&#7853;t &#273;ã b&#7883; thay &#273;&#7893;i : ' . $t, 'error_code' => 1);
        return $n;
    }
    foreach ($cc as $o) {
        $ii = 0;
        foreach ($ee->find($o['law']) as $jj) {
            if ($ii == $o['index']) {
                $jj->outertext = '';
                break;
            }
            $ii++;
        }
    }
    $kk = $ee->find($z['law'], $z['index'])->innertext;
    $ll = $ee->find($bb['law'], $bb['index']);
    $mm = '';
    if ($ll) {
        foreach ($ll->find('a') as $nn) {
            $mm .= removeHTML(trim($nn->innertext)) . ',';
        }
        $mm = rtrim($mm, ',');
    }
    $oo = $ee->find($aa['law'], $z['index'])->innertext;
    $pp = time();
    $qq = 1;
    $hh = replaceFCK($hh, 1);
    $hh = replaceNCR($hh);
    $hh = remove_source($hh);
    $hh = removeHTML($hh);
    $rr = md5($hh);
    $kk = replaceFCK($kk, 1);
    $kk = replaceNCR($kk);
    $kk = remove_script($kk);
    $kk = removeLink($kk);
    $kk = remove_source($kk);
    if (!trim($kk)) {
        if ($r) {
            $dd = new db_execute('UPDATE links SET lin_status = ' . LINK_STATUS_FAIL . ' WHERE lin_id = ' . $r);
            unset($dd);
        }
        $n = array('success' => 0, 'error' => 'N&#7897;i dung tr&#7889;ng ho&#7863;c không &#273;úng lu&#7853;t ' . $t, 'error_code' => 1);
        unset($dd);
        return $n;
    }
    $mm = replaceFCK($mm, 1);
    $mm = replaceNCR($mm);
    $mm = removeHTML($mm);
    $oo = replaceFCK($oo, 1);
    $oo = replaceNCR($oo);
    $oo = removeHTML($oo);
    $oo = removeLink($oo);
    $oo = remove_source($oo);
    $ss = $p['lin_cat_id'];
    $tt = get_image_src_from_html($kk, $u);
    foreach ($tt as $uu => $vv) {
        $ww = save_image_url($vv);
        if (!$ww['error']) {
            $kk = str_replace($vv, $ww['link'], $kk);
        }
    }
    $ff = $gg['name'];
    $n = array('success' => 1, 'title' => $hh, 'title_md5' => $rr, 'content' => $kk, 'teaser' => $oo, 'category' => $ss, 'image' => $ff, 'link_id' => $r, 'link_url' => $t, 'tag_string' => $mm);
    return $n;
}