Collection.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. namespace app\common\util;
  3. class Collection {
  4. protected static $url,$config;
  5. /**
  6. * 采集内容
  7. * @param string $url 采集地址
  8. * @param array $config 配置参数
  9. * @param integer $page 分页采集模式
  10. */
  11. public static function get_content($url, $config, $page = 0) {
  12. set_time_limit(300);
  13. static $oldurl = array();
  14. $page = intval($page) ? intval($page) : 0;
  15. if ($html = self::get_html($url, $config)) {
  16. if (empty($page)) {
  17. //获取标题
  18. if ($config['title_rule']) {
  19. if(strpos($config['title_rule'],'[内容]')===false){
  20. $data['title'] = $config['title_rule'];
  21. }
  22. else{
  23. $title_rule = self::replace_sg($config['title_rule']);
  24. $data['title'] = self::replace_item(self::cut_html($html, $title_rule[0], $title_rule[1]), $config['title_html_rule']);
  25. }
  26. }
  27. //获取分类
  28. if ($config['type_rule']) {
  29. if(strpos($config['type_rule'],'[内容]')===false){
  30. $data['type'] = $config['type_rule'];
  31. }
  32. else{
  33. $type_rule = self::replace_sg($config['type_rule']);
  34. $data['type'] = self::replace_item(self::cut_html($html, $type_rule[0], $type_rule[1]), $config['type_html_rule']);
  35. }
  36. }
  37. if (empty($data['time'])) $data['time'] = time();
  38. //对自定义数据进行采集
  39. $config['customize_config'] = json_decode($config['customize_config'],true);
  40. if($config['customize_config']){
  41. foreach ($config['customize_config'] as $k=>$v) {
  42. if (empty($v['rule'])) continue;
  43. if(strpos($v['rule'],'[内容]')===false){
  44. $data[$v['en_name']] = $v['rule'];
  45. }
  46. else{
  47. $rule = self::replace_sg($v['rule']);
  48. $data[$v['en_name']] = self::replace_item(self::cut_html($html, $rule[0], $rule[1]), $v['html_rule']);
  49. }
  50. }
  51. }
  52. }
  53. //获取内容
  54. if ($config['content_rule']) {
  55. if(strpos($config['content_rule'],'[内容]')===false){
  56. $data['content'] = $config['content_rule'];
  57. }
  58. else{
  59. $content_rule = self::replace_sg($config['content_rule']);
  60. $data['content'] = self::replace_item(self::cut_html($html, $content_rule[0], $content_rule[1]), $config['content_html_rule']);
  61. }
  62. }
  63. //处理分页
  64. if (in_array($page, array(0,2)) && !empty($config['content_page_start']) && !empty($config['content_page_end'])) {
  65. $oldurl[] = $url;
  66. $tmp[] = $data['content'];
  67. $page_html = self::cut_html($html, $config['content_page_start'], $config['content_page_end']);
  68. //上下页模式
  69. if ($config['content_page_rule'] == 2 && in_array($page, array(0,2)) && $page_html) {
  70. preg_match_all('/<a [^>]*href=[\'"]?([^>\'" ]*)[\'"]?[^>]*>([^<\/]*)<\/a>/i', $page_html, $out);
  71. if (!empty($out[1]) && !empty($out[2])) {
  72. foreach ($out[2] as $k=>$v) {
  73. if (strpos($v, $config['content_nextpage']) === false) continue;
  74. if ($out[1][$k] == '#') continue;
  75. $out[1][$k] = self::url_check($out[1][$k], $url, $config);
  76. if (in_array($out[1][$k], $oldurl)) continue;
  77. $oldurl[] = $out[1][$k];
  78. $results = self::get_content($out[1][$k], $config, 2);
  79. if (!in_array($results['content'], $tmp)) $tmp[] = $results['content'];
  80. }
  81. }
  82. }
  83. //全部罗列模式
  84. if ($config['content_page_rule'] == 1 && $page == 0 && $page_html) {
  85. preg_match_all('/<a [^>]*href=[\'"]?([^>\'" ]*)[\'"]?/i', $page_html, $out);
  86. if (is_array($out[1]) && !empty($out[1])) {
  87. $out = array_unique($out[1]);
  88. foreach ($out as $k=>$v) {
  89. if ($out[1][$k] == '#') continue;
  90. $v = self::url_check($v, $url, $config);
  91. $results = self::get_content($v, $config, 1);
  92. if (!in_array($results['content'], $tmp) && $results['content']!="") $tmp[] = $results['content'];
  93. }
  94. }
  95. }
  96. $data['content'] = $config['content_page'] == 1 ? implode('[page]', $tmp) : implode('', $tmp);
  97. }
  98. if ($page == 0) {
  99. self::$url = $url;
  100. self::$config = $config;
  101. $data['content'] = preg_replace_callback('/<img[^>]*src=[\'"]?([^>\'"\s]*)[\'"]?[^>]*>/i', array('collection','download_img_callback'), $data['content']);
  102. //下载内容中的图片到本地
  103. if (empty($page) && !empty($data['content']) && $config['down_attachment'] == 1) {
  104. }
  105. }
  106. return $data;
  107. }
  108. }
  109. /**
  110. * 转换图片地址为绝对路径,为下载做准备。
  111. * @param array $out 图片地址
  112. */
  113. protected static function download_img_callback($matches) {
  114. return self::download_img($matches[0], $matches[1]);
  115. }
  116. protected static function download_img($old, $out) {
  117. if (!empty($old) && !empty($out) && strpos($out, '://') === false) {
  118. return str_replace($out, self::url_check($out, self::$url, self::$config), $old);
  119. } else {
  120. return $old;
  121. }
  122. }
  123. /**
  124. * 得到需要采集的网页列表页
  125. * @param array $config 配置参数
  126. * @param integer $num 返回数
  127. */
  128. public static function url_list(&$config, $num = '') {
  129. $url = array();
  130. switch ($config['sourcetype']) {
  131. case '1'://序列化
  132. $num = empty($num) ? $config['pagesize_end'] : $num;
  133. if($num<$config['pagesize_start']) $num=$config['pagesize_start'];
  134. $p=0;
  135. for ($i = $config['pagesize_start']; $i <= $num; $i = $i + $config['par_num']) {
  136. $url[$p] = str_replace('(*)', $i, $config['urlpage']);
  137. $p++;
  138. }
  139. break;
  140. case '2'://多网址
  141. $url = explode("\r\n", $config['urlpage']);
  142. break;
  143. case '3'://单一网址
  144. case '4'://RSS
  145. $url[] = $config['urlpage'];
  146. break;
  147. }
  148. return $url;
  149. }
  150. /**
  151. * 获取文章网址
  152. * @param string $url 采集地址
  153. * @param array $config 配置
  154. */
  155. public static function get_url_lists($url, &$config) {
  156. if ($html = self::get_html($url, $config)) {
  157. if ($config['sourcetype'] == 4) { //RSS
  158. $xml = pc_base::load_sys_class('xml');
  159. $html = $xml->xml_unserialize($html);
  160. if (pc_base::load_config('system', 'charset') == 'gbk') {
  161. $html = array_iconv($html, 'utf-8', 'gbk');
  162. }
  163. $data = array();
  164. if (is_array($html['rss']['channel']['item']))foreach ($html['rss']['channel']['item'] as $k=>$v) {
  165. $data[$k]['url'] = $v['link'];
  166. $data[$k]['title'] = $v['title'];
  167. }
  168. } else {
  169. $html = self::cut_html($html, $config['url_start'], $config['url_end']);
  170. $html = str_replace(array("\r", "\n"), '', $html);
  171. $html = str_replace(array("</a>", "</A>"), "</a>\n", $html);
  172. preg_match_all('/<a ([^>]*)>([^\/a>].*)<\/a>/i', $html, $out);
  173. //$out[1] = array_unique($out[1]);
  174. //$out[2] = array_unique($out[2]);
  175. $data = array();
  176. foreach ($out[1] as $k=>$v) {
  177. if (preg_match('/href=[\'"]?([^\'" ]*)[\'"]?/i', $v, $match_out)) {
  178. if ($config['url_contain']) {
  179. if (strpos($match_out[1], $config['url_contain']) === false) {
  180. continue;
  181. }
  182. }
  183. if ($config['url_except']) {
  184. if (strpos($match_out[1], $config['url_except']) !== false) {
  185. continue;
  186. }
  187. }
  188. $url2 = $match_out[1];
  189. $url2 = self::url_check($url2, $url, $config);
  190. $data[$k]['url'] = $url2;
  191. $data[$k]['title'] = strip_tags($out[2][$k]);
  192. } else {
  193. continue;
  194. }
  195. }
  196. }
  197. return $data;
  198. } else {
  199. return false;
  200. }
  201. }
  202. /**
  203. * 获取远程HTML
  204. * @param string $url 获取地址
  205. * @param array $config 配置
  206. */
  207. protected static function get_html($url, &$config) {
  208. if (!empty($url) && $html = mac_curl_get($url)) {
  209. if ('UTF-8' != $config['sourcecharset'] && $config['sourcetype'] != 4) {
  210. $html = iconv($config['sourcecharset'], 'UTF-8//TRANSLIT//IGNORE', $html);
  211. }
  212. return $html;
  213. } else {
  214. return false;
  215. }
  216. }
  217. /**
  218. *
  219. * HTML切取
  220. * @param string $html 要进入切取的HTML代码
  221. * @param string $start 开始
  222. * @param string $end 结束
  223. */
  224. protected static function cut_html($html, $start, $end) {
  225. if (empty($html)) return false;
  226. $html = str_replace(array("\r", "\n"), "", $html);
  227. $start = str_replace(array("\r", "\n"), "", $start);
  228. $end = str_replace(array("\r", "\n"), "", $end);
  229. $html = explode(trim($start), $html);
  230. if(is_array($html)) $html = explode(trim($end), $html[1]);
  231. return trim($html[0]);
  232. }
  233. /**
  234. * 过滤代码
  235. * @param string $html HTML代码
  236. * @param array $config 过滤配置
  237. */
  238. protected static function replace_item($html, $config) {
  239. if (empty($config)) return $html;
  240. $config = explode("\n", $config);
  241. $patterns = $replace = array();
  242. $p = 0;
  243. foreach ($config as $k=>$v) {
  244. if (empty($v)) continue;
  245. $c = explode('[|]', $v);
  246. $patterns[$k] = '/'.str_replace('/', '\/', $c[0]).'/i';
  247. $replace[$k] = $c[1];
  248. $p = 1;
  249. }
  250. return $p ? @preg_replace($patterns, $replace, $html) : false;
  251. }
  252. /**
  253. * 替换采集内容
  254. * @param $html 采集规则
  255. */
  256. protected static function replace_sg($html) {
  257. $list = explode('[内容]', $html);
  258. if (is_array($list)) foreach ($list as $k=>$v) {
  259. $list[$k] = str_replace(array("\r", "\n"), '', trim($v));
  260. }
  261. return $list;
  262. }
  263. /**
  264. * URL地址检查
  265. * @param string $url 需要检查的URL
  266. * @param string $baseurl 基本URL
  267. * @param array $config 配置信息
  268. */
  269. protected static function url_check($url, $baseurl, $config) {
  270. $urlinfo = parse_url($baseurl);
  271. $baseurl = $urlinfo['scheme'].'://'.$urlinfo['host'].(substr($urlinfo['path'], -1, 1) === '/' ? substr($urlinfo['path'], 0, -1) : str_replace('\\', '/', dirname($urlinfo['path']))).'/';
  272. if (strpos($url, '://') === false) {
  273. if ($url[0] == '/') {
  274. $url = $urlinfo['scheme'].'://'.$urlinfo['host'].$url;
  275. } else {
  276. if ($config['page_base']) {
  277. $url = $config['page_base'].$url;
  278. } else {
  279. $url = $baseurl.$url;
  280. }
  281. }
  282. }
  283. return $url;
  284. }
  285. }