function fetch_web($schemas, $test = null) { global $_user, $_channels; $updated_file = array(); foreach ($schemas as $k => $v) { if (!$v->channel && $v->url == '' || $v->keywords == '') { continue; } if ($v->channel) { $url_arr = $_channels[$v->channel]['url']; } else { $url_arr = explode(' ', $v->url); } foreach ($url_arr as $url_key => $url) { $url = mkurl($v->channel, $url_key, $url, $v->url); $filename = md5($url) . '.tmp'; if (in_array($filename, $updated_file)) { $html = file_get_contents(CACHE_PATH . $filename); } else { $file = ''; $context = $v->channel ? mkfp_context($url_key) : null; $html = file_get_contents($url, false, $context); $html = format_script($html); file_put_contents(CACHE_PATH . $filename, $html); $updated_file[] = $filename; } //处理关键词 $keywords = $v->keywords; $encoding = mb_detect_encoding($html, array('ASCII', 'GB2312', 'GBK', 'UTF-8')); if ($encoding == 'EUC-CN') { $encoding = 'GB2312'; $keywords = iconv('UTF-8', 'GBK', $keywords); } elseif ($encoding == 'CP936') { $keywords = iconv('UTF-8', $encoding, $keywords); } $keywords = explode(' ', $keywords); $first_key = array_shift($keywords); $lpos = 0; $rpos = NULL; $len = strlen($first_key); for ($lpos = strpos($html, $first_key, $lpos); $lpos !== false; $lpos = strpos($html, $first_key, $lpos + $len)) { $rpos = find_key($html, $keywords, $lpos + $len); if ($rpos) { $html_len = strlen($html); //得到关键词上下文 for ($j = 1; $j < 6 && $lpos !== false; $j++) { $lpos = strrpos($html, '</', $lpos - $html_len - 2); } if (!$lpos) { $lpos = 0; } for ($i = 1; $i < 5; $i++) { $rpos = strpos($html, '</', $rpos + 2); } if (!$rpos) { $rpos = $html_len; } $result = substr($html, $lpos, $rpos - $lpos); break; } } $result = trim(strip_tags($result, '<a>')); //确定包含数字 if ($v->max_num != '' || $v->min_num != '') { $result = find_num($result, $v->max_num, $v->min_num); } if ($result) { $result = str_replace(array("\n", "\r", "\t", " "), '', $result); if ($encoding != 'UTF-8') { $result = substr($result, 0, 400); $result = iconv('GBK', 'UTF-8', $result); } else { $result = mb_substr($result, 0, 400, 'utf8'); } $website = isset($_channels[$v->channel]['website'][$url_key]) ? $_channels[$v->channel]['website'][$url_key] : $url; $content = $result . "<a href='{$website}' target='_blank'>查看</a>"; if ($test) { return $content; } else { $_user = user::get_one(array('user_id' => $v->user_id)); schema::update(array('status' => 'off', 'schema_id' => $v->schema_id)); msg::add(array('schema_id' => $v->schema_id, 'title' => $v->title, 'content' => $content, 'status' => 'new')); $email = filter_var($_user->email, FILTER_VALIDATE_EMAIL); if ($_user->email_notify == 'on' && $email) { mail::send_cron($email, $v->title, $content); } if ($_user->app_notify == 'on' && $_user->baidu_uid) { push::push_message($_user->baidu_uid, $v->title, $result); } } $result = NULL; break; } } } }
<?php if ($_view == 'reactivate_schema') { schema::update(array('schema_id' => $_GET['schema_id'], 'status' => 'on')); } include TEMPLATES_PATH . 'msg.html';
<?php $template = 'schema.html'; if ($_view == 'edit') { $template = 'schema_edit.html'; if ($_POST['submit_schema_activate']) { $_POST['status'] = 'on'; $_POST['submit_schema'] = 1; } if ($_POST['submit_schema']) { if (is_numeric($_POST['schema_id'])) { schema::update($_POST); $template = 'schema.html'; } else { schema::add($_POST); $template = 'schema.html'; } } } elseif ($_view == 'test') { $keywords = str_replace('^^', ' ', $_REQUEST['keywords']); $schema = array('channel' => $_REQUEST['channel'], 'url' => $_REQUEST['url'], 'keywords' => $keywords); $result = fetch_web(array((object) $schema), true); echo $result; exit; } elseif ($_view == 'del') { schema::delete($_GET); } include TEMPLATES_PATH . $template;