Guest User

Untitled

a guest
May 17th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.02 KB | None | 0 0
  1. <?php
  2.  
  3. include_once("Zend/Feed/Rss.php");
  4.  
  5. class News {
  6.  
  7. // when set to 1, script will echo out queries and other data
  8. var $debug = 0;
  9.  
  10. // array of feeds available - maps to `news_feeds` in database
  11. private $feeds = array();
  12.  
  13. // array of articles returned after NEWS::loadArticles() is called
  14. var $articles = array();
  15.  
  16. // used for paging - number of records returned and total records available
  17. // after a query is performed using NEWS::loadArticles()
  18. var $num_records = 0;
  19. var $total_records = 0;
  20.  
  21. // used in front-end to determine the title of the category specified
  22. // i.e. $_REQUEST['category'] = 'css-and-xhtml'
  23. // -> after NEWS::loadArticles() is called, NEWS::category_title = 'CSS and XHTML'
  24. var $category_title = '';
  25.  
  26. function __construct () {
  27.  
  28. $this->loadFeeds();
  29.  
  30. }
  31.  
  32. function loadFeeds () {
  33.  
  34. $this->feeds = array();
  35.  
  36. $sql = "SELECT * FROM `news_feeds` ORDER BY `title` ASC";
  37. $query = mysql_query ($sql) or die(mysql_error());
  38.  
  39. while ($row = mysql_fetch_assoc($query)) {
  40.  
  41. $this->feeds[] = $this->cleanRow($row);
  42.  
  43. }
  44.  
  45. }
  46.  
  47. // populates news object with articles matching a query
  48. // $arr is a hash specifying the parameters to search
  49. function loadArticles ($arr, $limit = '', $offset = '') {
  50.  
  51. // reset object
  52. $this->articles = array();
  53. $this->num_records = 0;
  54. $this->total_records = 0;
  55. $this->category_title = '';
  56.  
  57. // sanitize array before building query
  58. if (is_array($arr)) {
  59.  
  60. foreach ($arr as $key=>$value) {
  61. $arr[$key] = mysql_real_escape_string($value);
  62. }
  63.  
  64. }
  65.  
  66. if ($arr['category']) {
  67.  
  68. // get all categories and attempt to match against one passed in $arr
  69. $sql = "SELECT * FROM `news_categories`";
  70. $query = mysql_query ($sql);
  71.  
  72. while ($row = mysql_fetch_assoc($query)) {
  73.  
  74. // format title, i.e. CSS and XHTML becomes css-and-xhtml
  75. $category = $this->rewriteFormat(stripslashes($row['title']));
  76.  
  77. if ($arr['category'] == $category) {
  78.  
  79. // match found - use category_id in query and set the object's category_title
  80. $where[] = "t2.`category_id`='" .$row['id']. "'";
  81. $this->category_title = stripslashes($row['title']);
  82.  
  83. }
  84.  
  85. }
  86.  
  87. }
  88.  
  89. // article published on given year
  90. if ($arr['year']) {
  91.  
  92. $where[] = "YEAR(t1.`date`)='" .$arr['year']. "'";
  93.  
  94. }
  95.  
  96. // use 3 letter month for clarity - find articles published on given month
  97. if ($arr['month']) {
  98.  
  99. $months = array('jan'=>1, 'feb'=>2, 'mar'=>3, 'apr'=>4,
  100. 'may'=>5, 'jun'=>6, 'jul'=>7, 'aug'=>8,
  101. 'sep'=>9, 'oct'=>10, 'nov'=>11, 'dec'=>12);
  102.  
  103. $where[] = "MONTH(t1.`date`)='" .$months[$arr['month']]. "'";
  104.  
  105. }
  106.  
  107. // find articles published on given day of month
  108. if ($arr['day']) {
  109.  
  110. $where[] = "DAY(t1.`date`)='" .$arr['day']. "'";
  111.  
  112. }
  113.  
  114. // scan title of article - not in use currently
  115. if ($arr['title']) {
  116.  
  117. $where[] = "t1.`rewrite`='" .$arr['title']. "'";
  118.  
  119. }
  120.  
  121. // match query against article title
  122. if ($arr['query']) {
  123.  
  124. $where[] = "t1.`title` LIKE '%" .$arr['query']. "%'";
  125.  
  126. }
  127.  
  128. // build WHERE portion of query
  129. if ($where) {
  130.  
  131. $where = " WHERE (" .implode(") AND (", $where). ")";
  132.  
  133. } else {
  134.  
  135. $where = "";
  136.  
  137. }
  138.  
  139. // the query
  140. $sql = "SELECT SQL_CALC_FOUND_ROWS t1.*, t3.title AS `feed`, t3.url AS `feed_url`
  141. FROM `news_articles` AS t1
  142.  
  143. INNER JOIN `news_articles_categories_rel` AS t2
  144. ON t1.id = t2.article_id
  145.  
  146. INNER JOIN `news_feeds` AS t3
  147. ON t1.feed_id = t3.id
  148.  
  149. " .$where. "
  150.  
  151. GROUP BY t1.id
  152.  
  153. ORDER BY t1.date DESC, t1.title ASC";
  154.  
  155. // allow for paging
  156. if ($limit) {
  157. $sql .= "
  158. LIMIT " .$limit;
  159. }
  160.  
  161. if ($offset) {
  162. $sql .= "
  163. OFFSET " .$offset;
  164. }
  165.  
  166. $this->debug($sql);
  167.  
  168. $query = mysql_query ($sql);
  169.  
  170. // results found -> populate object
  171. if (mysql_num_rows($query)) {
  172.  
  173. $this->num_records = mysql_num_rows($query);
  174.  
  175. $this->total_records = mysql_result(mysql_query("SELECT FOUND_ROWS()"), 0);
  176.  
  177. while ($row = mysql_fetch_assoc($query)) {
  178.  
  179. $this->articles[] = $this->cleanRow($row);
  180.  
  181. }
  182.  
  183. }
  184.  
  185. }
  186.  
  187. function downloadArticles ($strip_html = true) {
  188.  
  189. // cron function - this populates the database
  190.  
  191. // loop through the feeds and figure out what categories they feed into
  192. // and what filter is applied to the articles before insertion into that
  193. // category
  194. foreach ($this->feeds as $feed) {
  195.  
  196. // go ahead and download the feed now
  197. $channel = new Zend_Feed_Rss($feed['url']);
  198.  
  199. $this->debug("Processing feed: " .$channel->title. " (id: " .$feed['id']. ")");
  200.  
  201. // loop through each feed item
  202. foreach ($channel as $item) {
  203.  
  204. // clean up the feed data for insertion into database
  205. // some titles have text in-between brackets -- get rid of it
  206. $title = trim(mysql_real_escape_string(preg_replace("/\[.*\]/", "", $item->title)));
  207. $content = $item->description;
  208.  
  209. // remove img, embed, input, script, small tags
  210. if ($strip_html) {
  211. $content = preg_replace('!(<img[^>]*>|<embed[^>]*>|<input[^>]*>|<script[^>]*>|<small[^>]*>|</img>|</embed>|</input>|</script>|</small>)?!', '', $content);
  212. }
  213.  
  214. $content = mysql_real_escape_string($content);
  215. $link = mysql_real_escape_string($item->link);
  216. $date = date("Y-m-d H:i:s", strtotime($item->pubDate));
  217.  
  218. $this->debug("Processing article: " .$item->title);
  219.  
  220. // does the article already exist in the database?
  221. $sql = "SELECT * FROM `news_articles` WHERE `title`='" .$title. "'";
  222. $query = mysql_query ($sql);
  223.  
  224. if (mysql_num_rows($query) == 0) {
  225.  
  226. $this->debug('<span style="color: #080;">Article does not already exist in database!</span>');
  227.  
  228. // unset the categories array -> this array tells what categories to insert
  229. // the article into
  230. $categories = array();
  231.  
  232.  
  233. $sql = "SELECT * FROM `news_feeds_categories_rel`
  234. WHERE `feed_id`='" .$feed['id']. "'";
  235.  
  236. $query = mysql_query ($sql);
  237.  
  238. // figure out what categories to insert article into and check against filters if exist
  239. while ($row = mysql_fetch_assoc($query)) {
  240.  
  241. // relevant data here is
  242. // category_id and filter
  243. $row = $this->cleanRow($row);
  244.  
  245. $pass = true;
  246.  
  247. // filter exists, check against
  248. if ($row['filter_id'] > 0) {
  249.  
  250. $this->debug('<span style="color: #777;">Checking against filter: '.$row['filter_id']. '</span>');
  251.  
  252. // get list of keywords
  253. $sql = "SELECT * FROM `news_filters_keywords` WHERE `filter_id`='" .$row['filter_id']. "'";
  254. $keyword_query = mysql_query ($sql);
  255.  
  256. if (mysql_num_rows($keyword_query)) {
  257.  
  258. $pass = false;
  259.  
  260. while ($keyword_row = mysql_fetch_assoc($keyword_query)) {
  261.  
  262. $keyword = stripslashes($keyword_row['keyword']);
  263.  
  264. if (preg_match('/\b' .$keyword. '\b/i', $title) || preg_match('/\b' .$keyword. '\b/i', $content)) {
  265.  
  266. $this->debug('<span style="color: #080;">Match found on keyword: '.$keyword. '</span>');
  267. $pass = true;
  268.  
  269. }
  270.  
  271. }
  272.  
  273. }
  274.  
  275. }
  276.  
  277. if ($pass == true) {
  278.  
  279. // article will be inserted into the category
  280. $categories[] = $row['category_id'];
  281.  
  282. }
  283.  
  284. }
  285.  
  286. // inserted into 1 or more categories, so add to database
  287. if (count($categories)) {
  288.  
  289. $this->debug('<span style="color: #080;">Inserting article to database</span>');
  290.  
  291. $sql = "INSERT INTO `news_articles`
  292. SET
  293. `feed_id`='" .$feed['id']. "',
  294. `title`='" .$title. "',
  295. `content`='" .$content. "',
  296. `link`='" .$link. "',
  297. `date`='" .$date. "',
  298. `rewrite`='" .$this->rewriteFormat(stripslashes($title)). "'";
  299. $query = mysql_query ($sql) or die(mysql_error());
  300.  
  301. $article_id = mysql_insert_id();
  302.  
  303. foreach ($categories as $category_id) {
  304.  
  305. $this->debug('<span style="color: #080;">Inserting article to category: ' .$category_id. '</span>');
  306.  
  307. $sql = "INSERT INTO `news_articles_categories_rel`
  308. SET
  309. `article_id`='" .$article_id. "',
  310. `category_id`='" .$category_id. "'";
  311. $query = mysql_query ($sql) or die(mysql_error());
  312.  
  313. }
  314.  
  315. $inserts++;
  316.  
  317. }
  318.  
  319. }
  320.  
  321. }
  322.  
  323. }
  324.  
  325. $this->debug('<strong>DONE</strong> Inserted: ' .$inserts. ' articles');
  326.  
  327. return true;
  328.  
  329. }
  330.  
  331. function deleteOldArticles () {
  332.  
  333. // delete articles a year old
  334. $cutoff = mktime(0, 0, 0, date("n"), date("j"), date("Y") - 1);
  335.  
  336. $sql = "DELETE FROM `news_articles` WHERE UNIX_TIMESTAMP(`date`) < " .$cutoff;
  337. $query = mysql_query ($sql);
  338.  
  339. }
  340.  
  341. function getCategories () {
  342.  
  343. $sql = "SELECT t1.*, COUNT(t2.`id`) AS `count`
  344. FROM `news_categories` AS t1
  345. LEFT JOIN `news_articles_categories_rel` AS t2
  346. ON t1.id = t2.category_id
  347. GROUP BY t1.id
  348. ORDER BY t1.`title` ASC
  349. ";
  350. $query = mysql_query ($sql);
  351.  
  352. while ($row = mysql_fetch_assoc($query)) {
  353.  
  354. $categories[] = $this->cleanRow($row);
  355.  
  356. }
  357.  
  358. return $categories;
  359.  
  360. }
  361.  
  362. function getFilters () {
  363.  
  364. $sql = "SELECT t1.*, COUNT(t2.`filter_id`) AS `count`
  365. FROM `news_filters` AS t1
  366. INNER JOIN `news_filters_keywords` AS t2
  367. ON t1.id = t2.filter_id
  368. GROUP BY t1.id
  369. ORDER BY t1.`title` ASC";
  370. $query = mysql_query ($sql);
  371.  
  372. while ($row = mysql_fetch_assoc($query)) {
  373.  
  374. $filters[] = $this->cleanRow($row);
  375.  
  376. }
  377.  
  378. return $filters;
  379.  
  380. }
  381.  
  382. function getFeeds () {
  383.  
  384. return $this->feeds;
  385.  
  386. }
  387.  
  388. function cleanRow ($row) {
  389.  
  390. // remove slashes from array
  391. foreach ($row as $key=>$value) {
  392. $row[$key] = stripslashes($value);
  393. }
  394. return $row;
  395. }
  396.  
  397. function rewriteFormat ($str) {
  398.  
  399. // clean up string and make it URL-friendly
  400. $str = strtolower(str_replace(" ", "-", $str));
  401. $str = preg_replace('/[^A-Za-z0-9-]/i', '', $str);
  402.  
  403. return $str;
  404.  
  405. }
  406.  
  407. function debug ($s) {
  408.  
  409. if (!$this->debug) return;
  410.  
  411. echo '<pre>';
  412.  
  413. if (is_array($s)) {
  414. print_r($s);
  415. } else {
  416. echo $s;
  417. }
  418.  
  419. echo '</pre>';
  420.  
  421. }
  422.  
  423. }
  424.  
  425. ?>
Add Comment
Please, Sign In to add comment