collection.class.php 9.8 KB

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