diff --git a/lastRSS.php b/lastRSS.php index 1d72318..4c5d66b 100644 --- a/lastRSS.php +++ b/lastRSS.php @@ -1,7 +1,7 @@ cache_dir = EMLOG_ROOT.'/content/cache/'; -$rssparser->cache_time = $lastRSS_cache_time; +$rssparser->cache_time = $lastRSS_cache_time; $rssparser->items_limit = $lastRSS_item_num > 3 ? 3 : $lastRSS_item_num; $rssparser->CDATA = 'content'; $rssparser->cp = 'UTF-8'; @@ -93,25 +93,26 @@ function updateFeed($id, $url, $title){ } function getTitle($rss_url){ global $rssparser; - $rss = $rssparser->get($rss_url); + $rss = $rssparser->Get($rss_url); return $rss['title']; } function updateLogs(){ global $rssparser, $DB, $lastRSS_is_urlshort, $lastRSS_is_blank; $feeds = getRssFeeds(); while ($item = $DB->fetch_array($feeds)) { - $rss = $rssparser->get($item['url']); - if(!empty($rss['items'])) - foreach ($rss['items'] as $key => $data) { - $rssid = $item['id']; - $hash = md5($data['title']); - $checklog = isLogExists($rssid, $hash); - if ($checklog == 0) { - if (trim($data['title']) != '') { - $link = $lastRSS_is_urlshort ? urlShort($data['link']) : $data['link']; - $target = $lastRSS_is_blank ? ' target="_blank"' : ''; - $log = ''.strip_tags($data['title']).''; - insertLog($rssid, $log ,$hash); + $rss = $rssparser->Get($item['url']); + if(!empty($rss['items'])) { + foreach ($rss['items'] as $key => $data) { + $rssid = $item['id']; + $hash = md5($data['title']); + $checklog = isLogExists($rssid, $hash); + if ($checklog == 0) { + if (trim($data['title']) != '') { + $link = $lastRSS_is_urlshort ? urlShort($data['link']) : $data['link']; + $target = $lastRSS_is_blank ? ' target="_blank"' : ''; + $log = ''.strip_tags($data['title']).''; + insertLog($rssid, $log ,$hash); + } } } } diff --git a/lastRSS_callback.php b/lastRSS_callback.php index 1bc5d7f..ad6f0a1 100644 --- a/lastRSS_callback.php +++ b/lastRSS_callback.php @@ -1,44 +1,45 @@ -query('show tables like "'.DB_PREFIX.'rssfeeds"'); - $is_exist_rsslogs_query = $DB->query('show tables like "'.DB_PREFIX.'rsslogs"'); - $dbcharset = 'utf8'; - $type = 'MYISAM'; - $add = $DB->getMysqlVersion() > '4.1' ? "ENGINE=".$type." DEFAULT CHARSET=".$dbcharset.";":"TYPE=".$type.";"; - $addone = "INSERT INTO ".DB_PREFIX."rssfeeds (url, title) VALUES('http://xiaosong.org/rss.php', '快乐忆站')"; - if($DB->num_rows($is_exist_rssfeeds_query) == 0){ - $sql_rssfeeds = " -CREATE TABLE `".DB_PREFIX."rssfeeds` ( -`id` int(10) NOT NULL AUTO_INCREMENT, -`url` text NOT NULL, -`title` varchar(255) NOT NULL, -PRIMARY KEY (`id`) -)".$add; - $DB->query($sql_rssfeeds); - $DB->query($addone); - - } - if($DB->num_rows($is_exist_rsslogs_query) == 0){ - $sql_rsslogs = " -CREATE TABLE `".DB_PREFIX."rsslogs` ( -`id` int(10) NOT NULL AUTO_INCREMENT, -`rssid` int(10) NOT NULL, -`log` text NOT NULL, -`hash` varchar(64) NOT NULL, -PRIMARY KEY (`id`) -)".$add; - $DB->query($sql_rsslogs); - } -} - -function callback_rm(){ - $DB = MySql::getInstance(); - $feeds = getRssFeeds(); - while ($item = $DB->fetch_array($feeds)) { - deleteFeed($item['id']); - } - $DB->query("DROP TABLE IF EXISTS ".DB_PREFIX."rssfeeds"); - $DB->query("DROP TABLE IF EXISTS ".DB_PREFIX."rsslogs"); -} +query('show tables like "'.DB_PREFIX.'rssfeeds"'); + $is_exist_rsslogs_query = $DB->query('show tables like "'.DB_PREFIX.'rsslogs"'); + $dbcharset = 'utf8'; + $type = 'MYISAM'; + $add = $DB->getMysqlVersion() > '4.1' ? "ENGINE=".$type." DEFAULT CHARSET=".$dbcharset.";":"TYPE=".$type.";"; + $addone = "INSERT INTO ".DB_PREFIX."rssfeeds (url, title) VALUES('http://xiaosong.org/rss.php', '快乐忆站')"; + if($DB->num_rows($is_exist_rssfeeds_query) == 0){ + $sql_rssfeeds = " +CREATE TABLE `".DB_PREFIX."rssfeeds` ( +`id` int(10) NOT NULL AUTO_INCREMENT, +`url` text NOT NULL, +`title` varchar(255) NOT NULL, +PRIMARY KEY (`id`) +)".$add; + $DB->query($sql_rssfeeds); + $DB->query($addone); + + } + if($DB->num_rows($is_exist_rsslogs_query) == 0){ + $sql_rsslogs = " +CREATE TABLE `".DB_PREFIX."rsslogs` ( +`id` int(10) NOT NULL AUTO_INCREMENT, +`rssid` int(10) NOT NULL, +`log` text NOT NULL, +`hash` varchar(64) NOT NULL, +PRIMARY KEY (`id`) +)".$add; + $DB->query($sql_rsslogs); + } +} + +function callback_rm(){ + $DB = MySql::getInstance(); + $feeds = getRssFeeds(); + while ($item = $DB->fetch_array($feeds)) { + deleteFeed($item['id']); + } + $DB->query("DROP TABLE IF EXISTS ".DB_PREFIX."rssfeeds"); + $DB->query("DROP TABLE IF EXISTS ".DB_PREFIX."rsslogs"); +} ?> \ No newline at end of file diff --git a/lastRSS_class.php b/lastRSS_class.php index b948b92..8d4319a 100644 --- a/lastRSS_class.php +++ b/lastRSS_class.php @@ -1,220 +1,246 @@ -cache_dir != '') { - $cache_file = $this->cache_dir . '/rsscache_' . md5($rss_url); - $timedif = @(time() - filemtime($cache_file)); - if ($timedif < $this->cache_time) { - // cached file is fresh enough, return cached array - $result = unserialize(join('', file($cache_file))); - // set 'cached' to 1 only if cached file is correct - if ($result) $result['cached'] = 1; - } else { - // cached file is too old, create new - $result = $this->Parse($rss_url); - $serialized = serialize($result); - if ($f = @fopen($cache_file, 'w')) { - fwrite ($f, $serialized, strlen($serialized)); - fclose($f); - } - if ($result) $result['cached'] = 0; - } - } - // If CACHE DISABLED >> load and parse the file directly - else { - $result = $this->Parse($rss_url); - if ($result) $result['cached'] = 0; - } - // return result - return $result; - } - - // ------------------------------------------------------------------- - // Modification of preg_match(); return trimed field with index 1 - // from 'classic' preg_match() array output - // ------------------------------------------------------------------- - function my_preg_match ($pattern, $subject) { - // start regullar expression - preg_match($pattern, $subject, $out); - - // if there is some result... process it and return it - if(isset($out[1])) { - // Process CDATA (if present) - if ($this->CDATA == 'content') { // Get CDATA content (without CDATA tag) - $out[1] = strtr($out[1], array(''', ']]>'=>'')); - } elseif ($this->CDATA == 'strip') { // Strip CDATA - $out[1] = strtr($out[1], array(''', ']]>'=>'')); - } - - // If code page is set convert character encoding to required - if ($this->cp != '') - //$out[1] = $this->MyConvertEncoding($this->rsscp, $this->cp, $out[1]); - $out[1] = iconv($this->rsscp, $this->cp.'//TRANSLIT', $out[1]); - // Return result - return trim($out[1]); - } else { - // if there is NO result, return empty string - return ''; - } - } - - // ------------------------------------------------------------------- - // Replace HTML entities &something; by real characters - // ------------------------------------------------------------------- - function unhtmlentities ($string) { - // Get HTML entities table - $trans_tbl = get_html_translation_table (HTML_ENTITIES, ENT_QUOTES); - // Flip keys<==>values - $trans_tbl = array_flip ($trans_tbl); - // Add support for ' entity (missing in HTML_ENTITIES) - $trans_tbl += array(''' => "'"); - // Replace entities by values - return strtr ($string, $trans_tbl); - } - - // ------------------------------------------------------------------- - // Parse() is private method used by Get() to load and parse RSS file. - // Don't use Parse() in your scripts - use Get($rss_file) instead. - // ------------------------------------------------------------------- - function Parse ($rss_url) { - // Open and load RSS file - if ($f = @fopen($rss_url, 'r')) { - $rss_content = ''; - while (!feof($f)) { - $rss_content .= fgets($f, 4096); - } - fclose($f); - - // Parse document encoding - $result['encoding'] = $this->my_preg_match("'encoding=[\'\"](.*?)[\'\"]'si", $rss_content); - // if document codepage is specified, use it - if ($result['encoding'] != '') - { $this->rsscp = $result['encoding']; } // This is used in my_preg_match() - // otherwise use the default codepage - else - { $this->rsscp = $this->default_cp; } // This is used in my_preg_match() - - // Parse CHANNEL info - preg_match("'(.*?)'si", $rss_content, $out_channel); - foreach($this->channeltags as $channeltag) - { - $temp = $this->my_preg_match("'<$channeltag.*?>(.*?)'si", $out_channel[1]); - if ($temp != '') $result[$channeltag] = $temp; // Set only if not empty - } - // If date_format is specified and lastBuildDate is valid - if ($this->date_format != '' && ($timestamp = strtotime($result['lastBuildDate'])) !==-1) { - // convert lastBuildDate to specified date format - $result['lastBuildDate'] = date($this->date_format, $timestamp); - } - - // Parse TEXTINPUT info - preg_match("']*[^/])>(.*?)'si", $rss_content, $out_textinfo); - // This a little strange regexp means: - // Look for tag with or without any attributes, but skip truncated version (it's not beggining tag) - if (isset($out_textinfo[2])) { - foreach($this->textinputtags as $textinputtag) { - $temp = $this->my_preg_match("'<$textinputtag.*?>(.*?)'si", $out_textinfo[2]); - if ($temp != '') $result['textinput_'.$textinputtag] = $temp; // Set only if not empty - } - } - // Parse IMAGE info - preg_match("'(.*?)'si", $rss_content, $out_imageinfo); - if (isset($out_imageinfo[1])) { - foreach($this->imagetags as $imagetag) { - $temp = $this->my_preg_match("'<$imagetag.*?>(.*?)'si", $out_imageinfo[1]); - if ($temp != '') $result['image_'.$imagetag] = $temp; // Set only if not empty - } - } - // Parse ITEMS - preg_match_all("'(.*?)'si", $rss_content, $items); - $rss_items = $items[2]; - $i = 0; - $result['items'] = array(); // create array even if there are no items - foreach($rss_items as $rss_item) { - // If number of items is lower then limit: Parse one item - if ($i < $this->items_limit || $this->items_limit == 0) { - foreach($this->itemtags as $itemtag) { - $temp = $this->my_preg_match("'<$itemtag.*?>(.*?)'si", $rss_item); - if ($temp != '') $result['items'][$i][$itemtag] = $temp; // Set only if not empty - } - // Strip HTML tags and other bullshit from DESCRIPTION - if ($this->stripHTML && $result['items'][$i]['description']) - $result['items'][$i]['description'] = strip_tags($this->unhtmlentities(strip_tags($result['items'][$i]['description']))); - // Strip HTML tags and other bullshit from TITLE - if ($this->stripHTML && $result['items'][$i]['title']) - $result['items'][$i]['title'] = strip_tags($this->unhtmlentities(strip_tags($result['items'][$i]['title']))); - // If date_format is specified and pubDate is valid - if ($this->date_format != '' && ($timestamp = strtotime($result['items'][$i]['pubDate'])) !==-1) { - // convert pubDate to specified date format - $result['items'][$i]['pubDate'] = date($this->date_format, $timestamp); - } - // Item counter - $i++; - } - } - - $result['items_count'] = $i; - return $result; - } - else // Error in opening return False - { - return False; - } - } -} - +cache_dir != '') { + $cache_file = $this->cache_dir . '/rsscache_' . md5($rss_url); + $timedif = @(time() - filemtime($cache_file)); + if ($timedif < $this->cache_time) { + // cached file is fresh enough, return cached array + $result = unserialize(join('', file($cache_file))); + // set 'cached' to 1 only if cached file is correct + if ($result) $result['cached'] = 1; + } else { + // cached file is too old, create new + $result = $this->Parse($rss_url); + $serialized = serialize($result); + if ($f = @fopen($cache_file, 'w')) { + fwrite ($f, $serialized, strlen($serialized)); + fclose($f); + } + if ($result) $result['cached'] = 0; + } + } + // If CACHE DISABLED >> load and parse the file directly + else { + $result = $this->Parse($rss_url); + if ($result) $result['cached'] = 0; + } + // return result + return $result; + } + + // ------------------------------------------------------------------- + // Modification of preg_match(); return trimed field with index 1 + // from 'classic' preg_match() array output + // ------------------------------------------------------------------- + function my_preg_match ($pattern, $subject) { + // start regullar expression + preg_match($pattern, $subject, $out); + + // if there is some result... process it and return it + if(isset($out[1])) { + // Process CDATA (if present) + if ($this->CDATA == 'content') { // Get CDATA content (without CDATA tag) + $out[1] = strtr($out[1], array(''', ']]>'=>'')); + } elseif ($this->CDATA == 'strip') { // Strip CDATA + $out[1] = strtr($out[1], array(''', ']]>'=>'')); + } + + // If code page is set convert character encoding to required + if ($this->cp != '') + //$out[1] = $this->MyConvertEncoding($this->rsscp, $this->cp, $out[1]); + $out[1] = iconv($this->rsscp, $this->cp.'//TRANSLIT', $out[1]); + // Return result + return trim($out[1]); + } else { + // if there is NO result, return empty string + return ''; + } + } + + // ------------------------------------------------------------------- + // Replace HTML entities &something; by real characters + // ------------------------------------------------------------------- + function unhtmlentities ($string) { + // Get HTML entities table + $trans_tbl = get_html_translation_table (HTML_ENTITIES, ENT_QUOTES); + // Flip keys<==>values + $trans_tbl = array_flip ($trans_tbl); + // Add support for ' entity (missing in HTML_ENTITIES) + $trans_tbl += array(''' => "'"); + // Replace entities by values + return strtr ($string, $trans_tbl); + } + + // ------------------------------------------------------------------- + // Get remote file or open url + // ------------------------------------------------------------------- + function getRemoteFile ($url){ + $results = ''; + $context = stream_context_create(array( + 'http' => array( + 'timeout' => 3 + ) + )); + if (get_cfg_var('allow_url_fopen')) { + $f = fopen($url, 'r'); + while (!feof($f)) { + $results .= fgets($f, 4096); + } + fclose($f); + } else if (extension_loaded('curl')) { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_TIMEOUT, 5); + curl_setopt($ch, CURLOPT_USERAGENT, 'PHP Curl/emlog Rss订阅插件'); + $results = curl_exec($ch); + curl_close($ch); + } else { + $results = file_get_contents($url, 0, $context); + } + return $results; + } + + // ------------------------------------------------------------------- + // Parse() is private method used by Get() to load and parse RSS file. + // Don't use Parse() in your scripts - use Get($rss_file) instead. + // ------------------------------------------------------------------- + function Parse ($rss_url) { + // Open and load RSS file + $rss_content = $this->getRemoteFile($rss_url); + if (!empty($rss_content)) { + + // Parse document encoding + $result['encoding'] = $this->my_preg_match("'encoding=[\'\"](.*?)[\'\"]'si", $rss_content); + // if document codepage is specified, use it + if ($result['encoding'] != '') + { $this->rsscp = $result['encoding']; } // This is used in my_preg_match() + // otherwise use the default codepage + else + { $this->rsscp = $this->default_cp; } // This is used in my_preg_match() + + // Parse CHANNEL info + preg_match("'(.*?)'si", $rss_content, $out_channel); + foreach($this->channeltags as $channeltag) + { + $temp = $this->my_preg_match("'<$channeltag.*?>(.*?)'si", $out_channel[1]); + if ($temp != '') $result[$channeltag] = $temp; // Set only if not empty + } + // If date_format is specified and lastBuildDate is valid + if ($this->date_format != '' && ($timestamp = strtotime($result['lastBuildDate'])) !==-1) { + // convert lastBuildDate to specified date format + $result['lastBuildDate'] = date($this->date_format, $timestamp); + } + + // Parse TEXTINPUT info + preg_match("']*[^/])>(.*?)'si", $rss_content, $out_textinfo); + // This a little strange regexp means: + // Look for tag with or without any attributes, but skip truncated version (it's not beggining tag) + if (isset($out_textinfo[2])) { + foreach($this->textinputtags as $textinputtag) { + $temp = $this->my_preg_match("'<$textinputtag.*?>(.*?)'si", $out_textinfo[2]); + if ($temp != '') $result['textinput_'.$textinputtag] = $temp; // Set only if not empty + } + } + // Parse IMAGE info + preg_match("'(.*?)'si", $rss_content, $out_imageinfo); + if (isset($out_imageinfo[1])) { + foreach($this->imagetags as $imagetag) { + $temp = $this->my_preg_match("'<$imagetag.*?>(.*?)'si", $out_imageinfo[1]); + if ($temp != '') $result['image_'.$imagetag] = $temp; // Set only if not empty + } + } + // Parse ITEMS + preg_match_all("'(.*?)'si", $rss_content, $items); + $rss_items = $items[2]; + $i = 0; + $result['items'] = array(); // create array even if there are no items + foreach($rss_items as $rss_item) { + // If number of items is lower then limit: Parse one item + if ($i < $this->items_limit || $this->items_limit == 0) { + foreach($this->itemtags as $itemtag) { + $temp = $this->my_preg_match("'<$itemtag.*?>(.*?)'si", $rss_item); + if ($temp != '') $result['items'][$i][$itemtag] = $temp; // Set only if not empty + } + // Strip HTML tags and other bullshit from DESCRIPTION + if ($this->stripHTML && $result['items'][$i]['description']) + $result['items'][$i]['description'] = strip_tags($this->unhtmlentities(strip_tags($result['items'][$i]['description']))); + // Strip HTML tags and other bullshit from TITLE + if ($this->stripHTML && $result['items'][$i]['title']) + $result['items'][$i]['title'] = strip_tags($this->unhtmlentities(strip_tags($result['items'][$i]['title']))); + // If date_format is specified and pubDate is valid + if ($this->date_format != '' && ($timestamp = strtotime($result['items'][$i]['pubDate'])) !==-1) { + // convert pubDate to specified date format + $result['items'][$i]['pubDate'] = date($this->date_format, $timestamp); + } + // Item counter + $i++; + } + } + + $result['items_count'] = $i; + return $result; + } + else // Error in opening return False + { + return False; + } + } +} + ?> \ No newline at end of file diff --git a/lastRSS_setting.php b/lastRSS_setting.php index 3c0145a..5b9fd37 100644 --- a/lastRSS_setting.php +++ b/lastRSS_setting.php @@ -61,6 +61,66 @@ function plugin_setting_view(){ endif; } ?> +"; + $file = EMLOG_ROOT.'/content/plugins/lastRSS/lastRSS_config.php'; + @ $fp = fopen($file, 'wb') OR emMsg('读取文件失败,如果您使用的是Unix/Linux主机,请修改文件/content/plugins/lastRSS/lastRSS_config.php的权限为777。如果您使用的是Windows主机,请联系管理员,将该文件设为everyone可写'); + @ $fw = fwrite($fp,$data) OR emMsg('写入文件失败,如果您使用的是Unix/Linux主机,请修改文件/content/plugins/lastRSS/lastRSS_config.php的权限为777。如果您使用的是Windows主机,请联系管理员,将该文件设为everyone可写'); + fclose($fp); + header("Location:plugin.php?plugin=lastRSS&setting=true"); + } +} +?> -"; - $file = EMLOG_ROOT.'/content/plugins/lastRSS/lastRSS_config.php'; - @ $fp = fopen($file, 'wb') OR emMsg('读取文件失败,如果您使用的是Unix/Linux主机,请修改文件/content/plugins/lastRSS/lastRSS_config.php的权限为777。如果您使用的是Windows主机,请联系管理员,将该文件设为everyone可写'); - @ $fw = fwrite($fp,$data) OR emMsg('写入文件失败,如果您使用的是Unix/Linux主机,请修改文件/content/plugins/lastRSS/lastRSS_config.php的权限为777。如果您使用的是Windows主机,请联系管理员,将该文件设为everyone可写'); - fclose($fp); - header("Location:plugin.php?plugin=lastRSS&setting=true"); -} -?>