Example #1
0
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;
            }
        }
    }
}
Example #2
0
 public static function Rendering($model, $columnsInfo, $filter, $perPage)
 {
     if ($filter != null) {
         $whereCondition = '$model::';
         $temp = [];
         foreach ($columnsInfo as $key => $value) {
             $temp = array_add($temp, $key, collect($value));
         }
         $columnsInfo = $temp;
         $filter = Filter::Interpret($filter, $columnsInfo);
         $operations = $filter['operations'];
         $operands = $filter['operands'];
         function find_key($alias, $columnsInfo)
         {
             foreach ($columnsInfo as $key => $col) {
                 if ($col->contains($alias)) {
                     return $key;
                 }
             }
         }
         function add_condition(&$isFirst, &$whereCondition, $key, $value, $colInfo, $operations, &$i)
         {
             if ($isFirst) {
                 $whereCondition .= "where('" . $key . "','" . $value[0] . "')->";
                 $isFirst = FALSE;
             } elseif ($operations != []) {
                 if ($operations[$i] == '&') {
                     $whereCondition .= "where('" . $key . "','" . $value[0] . "')->";
                     $i++;
                 } elseif ($operations[$i] == '|') {
                     $whereCondition .= "orWhere('" . $key . "','" . $value[0] . "')->";
                     $i++;
                 }
             }
         }
         function add_condition_wherethrough(&$isFirst, &$whereCondition, $key, $value, $colInfo, $operations, &$i)
         {
             $wherethrough = $colInfo['wherethrough'];
             $class = $colInfo['class'];
             $el = $class::where($wherethrough['key'], $value)->get();
             $id = $el->first()->{$wherethrough}['value'];
             if ($isFirst) {
                 $whereCondition .= "where('" . $key . "','" . $id . "')->";
                 $isFirst = FALSE;
             } elseif ($operations != []) {
                 if ($operations[$i] == '&') {
                     $whereCondition .= "where('" . $key . "','" . $id . "')->";
                     $i++;
                 } elseif ($operations[$i] == '|') {
                     $whereCondition .= "orWhere('" . $key . "','" . $id . "')->";
                     $i++;
                 }
             }
         }
         $i = 0;
         $isFirst = TRUE;
         foreach ($operands as $item) {
             $key = find_key(key($item), $columnsInfo);
             if (!$columnsInfo[$key]->has('wherethrough')) {
                 add_condition($isFirst, $whereCondition, $key, array_values($item), $columnsInfo[$key], $operations, $i);
             } else {
                 add_condition_wherethrough($isFirst, $whereCondition, $key, array_values($item), $columnsInfo[$key], $operations, $i);
             }
         }
         //            echo '<div style="direction: ltr">';
         //            \Symfony\Component\VarDumper\VarDumper::dump($operations);
         //            \Symfony\Component\VarDumper\VarDumper::dump($operands);
         //            \Symfony\Component\VarDumper\VarDumper::dump($columnsInfo);
         //            \Symfony\Component\VarDumper\VarDumper::dump($whereCondition);
         //            echo '</div>';
         return eval('return ' . $whereCondition . "paginate({$perPage});");
     } else {
         return $model::paginate($perPage);
     }
 }