PHP一些常用的正则表达式
2007/01/22 18:30 ArthurXF
匹配中文字符的正则表达式: [\u4e00-\u9fa5]
匹配双字节字符(包括汉字在内): [^\x00-\xff]
应用:计算字符串的长度(一个双字节字符长度计2,ASCII字符计1)
String.prototype.len=function(){return this.replace([^\x00-\xff]/g,"aa").length;}
匹配空行的正则表达式: \n[\s| ]*\r
匹配HTML标记的正则表达式: /<(.*)>.*<\/>|<(.*) \/>/
匹配首尾空格的正则表达式: (^\s*)|(\s*$)
应用:javascript中没有像vbscript那样的trim函数,我们就可以利用这个表达式来实现,如下:
利用正则表达式分解和转换IP地址:
下面是利用正则表达式匹配IP地址,并将IP地址转换成对应数值的javascript程序:
不过上面的程序如果不用正则表达式,而直接用split函数来分解可能更简单,程序如下:
var ip="10.100.20.168"
ip=ip.split(".")
alert("IP值是:"+(ip[0]*255*255*255+ip[1]*255*255+ip[2]*255+ip[3]*1))
匹配Email地址的正则表达式: \w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
匹配网址URL的正则表达式: http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
利用正则表达式去除字串中重复的字符的算法程序:
用正则表达式从URL地址中提取文件名的javascript程序,如下结果为page1
利用正则表达式限制网页表单里的文本框输入内容:
用正则表达式限制只能输入中文:
用正则表达式限制只能输入全角字符:
用正则表达式限制只能输入数字:
用正则表达式限制只能输入数字和英文:
匹配双字节字符(包括汉字在内): [^\x00-\xff]
应用:计算字符串的长度(一个双字节字符长度计2,ASCII字符计1)
String.prototype.len=function(){return this.replace([^\x00-\xff]/g,"aa").length;}
匹配空行的正则表达式: \n[\s| ]*\r
匹配HTML标记的正则表达式: /<(.*)>.*<\/>|<(.*) \/>/
匹配首尾空格的正则表达式: (^\s*)|(\s*$)
应用:javascript中没有像vbscript那样的trim函数,我们就可以利用这个表达式来实现,如下:
String.prototype.trim = function() {
return this.replace(/(^\s*)|(\s*$)/g, "");
}
return this.replace(/(^\s*)|(\s*$)/g, "");
}
利用正则表达式分解和转换IP地址:
下面是利用正则表达式匹配IP地址,并将IP地址转换成对应数值的javascript程序:
function IP2V(ip) {
re=/(\d+)\.(\d+)\.(\d+)\.(\d+)/g //匹配IP地址的正则表达式
if(re.test(ip)) {
return RegExp.*Math.pow(255,3))+RegExp.*Math.pow(255,2))+RegExp.*255+RegExp.*1
}
else {
throw new Error("Not a valid IP address!")
}
}
re=/(\d+)\.(\d+)\.(\d+)\.(\d+)/g //匹配IP地址的正则表达式
if(re.test(ip)) {
return RegExp.*Math.pow(255,3))+RegExp.*Math.pow(255,2))+RegExp.*255+RegExp.*1
}
else {
throw new Error("Not a valid IP address!")
}
}
不过上面的程序如果不用正则表达式,而直接用split函数来分解可能更简单,程序如下:
var ip="10.100.20.168"
ip=ip.split(".")
alert("IP值是:"+(ip[0]*255*255*255+ip[1]*255*255+ip[2]*255+ip[3]*1))
匹配Email地址的正则表达式: \w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
匹配网址URL的正则表达式: http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
利用正则表达式去除字串中重复的字符的算法程序:
var s="abacabefgeeii"
var s1=s.replace(/(.).*/g,"")
var re=new RegExp("["+s1+"]","g")
var s2=s.replace(re,"")
alert(s1+s2) //结果为:abcefgi
var s1=s.replace(/(.).*/g,"")
var re=new RegExp("["+s1+"]","g")
var s2=s.replace(re,"")
alert(s1+s2) //结果为:abcefgi
用正则表达式从URL地址中提取文件名的javascript程序,如下结果为page1
s="http://www.9499.net/page1.htm"
s=s.replace(/(.*\/)([^\.]+).*/ig,"")
alert(s)
s=s.replace(/(.*\/)([^\.]+).*/ig,"")
alert(s)
利用正则表达式限制网页表单里的文本框输入内容:
用正则表达式限制只能输入中文:
onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))"
用正则表达式限制只能输入全角字符:
onkeyup="value=value.replace(/[^\uFF00-\uFFFF]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\uFF00-\uFFFF]/g,''))"
用正则表达式限制只能输入数字:
onkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"
用正则表达式限制只能输入数字和英文:
onkeyup="value=value.replace(/[\W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"
跟着ArthurXF学FreeBSD12——find命令用法
2007/01/22 18:21 ArthurXF
在FreeBSD下经常要查找文件所在,所以必须熟练掌握find命令的用法。
find 路径 参数 “表达式或者文件名”
find / -name "php.ini"
从根目录开始查找php.ini文件
find / \! -name "*.c" -print
列出所有不是以.c结尾的文件 Print out a list of all the files whose names do not end in .c.
find / -newer ttt -user wnj -print
列出所有比文件ttt新并且owner是wnj的文件
find / \! \( -newer ttt -user wnj \) -print
列出所有比文件ttt旧并且owner不是是wnj的文件
find / \( -newer ttt -or -user wnj \) -print
列出所有比文件ttt新或者owner是wnj的文件
find / -newerct '1 minute ago' -print
列出所有一分钟之前改变的文件
find / -type f -exec rm {} \;
删除所有文件,除非你疯了
find -L /usr/bin/ -type l -delete
删除/usr/bin目录下所有连接错误的连接符号
find / -name "[a-z].*"
还有几个参数会经常用到
-amin n 表示最后n分钟访问的文件
-atime n 表示最后n小时访问的文件
-cmin n 表示最后n分钟改变状态的文件
-ctime n 表示最后n小时改变状态的文件
-mmin n 表示最后n分钟里修改过的文件
-mtime n 表示最后24*n小时里修改过的文件
-empty 表示空白的文件,或空白的文件目录,或目录中没有子目录的文件夹
-nouser 属于作废用户的文件
-size +nc 长度大于n字节的文件
几个参数之间可以用-and,-or连接,某个参数取反可用!,可以用括号分隔条件
-exec command; 并执行命令
注意:在BSD下所有的字符串要使用引号
现在ArthurXF本人正在搞PHP等技术培训,如果想学习的人可以跟我联系。另外培训的招生简章在这个网址,想了解的可以去看看。加我QQ:29011218交流也可。
PHP培训招生简章
引用
find 路径 参数 “表达式或者文件名”
find / -name "php.ini"
从根目录开始查找php.ini文件
find / \! -name "*.c" -print
列出所有不是以.c结尾的文件 Print out a list of all the files whose names do not end in .c.
find / -newer ttt -user wnj -print
列出所有比文件ttt新并且owner是wnj的文件
find / \! \( -newer ttt -user wnj \) -print
列出所有比文件ttt旧并且owner不是是wnj的文件
find / \( -newer ttt -or -user wnj \) -print
列出所有比文件ttt新或者owner是wnj的文件
find / -newerct '1 minute ago' -print
列出所有一分钟之前改变的文件
find / -type f -exec rm {} \;
删除所有文件,除非你疯了
find -L /usr/bin/ -type l -delete
删除/usr/bin目录下所有连接错误的连接符号
find / -name "[a-z].*"
还有几个参数会经常用到
-amin n 表示最后n分钟访问的文件
-atime n 表示最后n小时访问的文件
-cmin n 表示最后n分钟改变状态的文件
-ctime n 表示最后n小时改变状态的文件
-mmin n 表示最后n分钟里修改过的文件
-mtime n 表示最后24*n小时里修改过的文件
-empty 表示空白的文件,或空白的文件目录,或目录中没有子目录的文件夹
-nouser 属于作废用户的文件
-size +nc 长度大于n字节的文件
几个参数之间可以用-and,-or连接,某个参数取反可用!,可以用括号分隔条件
-exec command; 并执行命令
注意:在BSD下所有的字符串要使用引号
现在ArthurXF本人正在搞PHP等技术培训,如果想学习的人可以跟我联系。另外培训的招生简章在这个网址,想了解的可以去看看。加我QQ:29011218交流也可。
PHP培训招生简章
PHP加速利器eAccelerator的API程序接口
2007/01/22 17:19 ArthurXF
eAccelerator 真是一个好东西(它的前身是truck-mmcache)。
简单来讲它是一套配合PHP(支持PHP5)运作的缓存系统,通过共享内存或磁盘文件方式交换数据。
它被广为使用的是PHP源码和缓存PHP执行的中间码以加速。关于 eA 的安装使用的文章已经很多而且也很详细了,这次我想推荐的是用它辅助程序设计缓存,它提供了一组API如下:
是一个非常便捷而又稳定的本机缓存实现方式,目前这部分设计似乎只支持于共享内存。
1. eaccelerator_put($key, $value, $ttl=0)
将 $value 以 $key 为键名存进缓存(php4下支持对像类型,看源码好像zend2里不支持了),$ttl 是这个缓存的生命周期,单位是秒,省略该参数或指定为 0 表示不限时,直到服务器重启清空为止。
2. eaccelerator_get($key)
根据 $key 从缓存中返回相应的 eaccelerator_put() 存进去的数据,如果这项缓存已经过期或不存在那么返回值是 NULL
3. eaccelerator_rm($key)
根据 $key 移除缓存
4. eaccelerator_gc()
移除清理所有已过期的 key
5. eaccelerator_lock($key)
为 $key 加上锁定操作,以保证多进程多线程操作时数据的同步。需要调用 eaccelerator_unlock($key) 来释放这个锁或等待程序请求结束时自动释放这个锁。
例如:
eaccelerator_lock("count");
eaccelerator_put("count",eaccelerator_get("count")+1));
?>
6. eaccelerator_unlock($key)
根据 $key 释放锁
7. eaccelerator_cache_output($key, $eval_code, $ttl=0)
将 $eval_code 代码的输出缓存 $ttl 秒,($ttl参数同 eacclerator_put)
For Example:
8. eaccelerator_cache_result($key, $eval_code, $ttl=0)
将 $eval_code 代码的执行结果缓存 $ttl 秒,($ttl参数同 eacclerator_put),类似 cache_output
For Example:
9. eaccelerator_cache_page($key, $ttl=0)
将当前整页缓存 $ttl 秒。
For Example:
eaccelerator_cache_page($_SERVER['PHP_SELF'].'?GET='.serialize($_GET),30);
echo time();
phpinfo();
?>
10. eaccelerator_rm_page($key)
删除由 eaccelerator_cache_page() 执行的缓存,参数也是 $key
______________________________________________
(作个简单例子看看它的威力,注意在 cli 模式下可能无效!)
简单来讲它是一套配合PHP(支持PHP5)运作的缓存系统,通过共享内存或磁盘文件方式交换数据。
它被广为使用的是PHP源码和缓存PHP执行的中间码以加速。关于 eA 的安装使用的文章已经很多而且也很详细了,这次我想推荐的是用它辅助程序设计缓存,它提供了一组API如下:
是一个非常便捷而又稳定的本机缓存实现方式,目前这部分设计似乎只支持于共享内存。
1. eaccelerator_put($key, $value, $ttl=0)
将 $value 以 $key 为键名存进缓存(php4下支持对像类型,看源码好像zend2里不支持了),$ttl 是这个缓存的生命周期,单位是秒,省略该参数或指定为 0 表示不限时,直到服务器重启清空为止。
2. eaccelerator_get($key)
根据 $key 从缓存中返回相应的 eaccelerator_put() 存进去的数据,如果这项缓存已经过期或不存在那么返回值是 NULL
3. eaccelerator_rm($key)
根据 $key 移除缓存
4. eaccelerator_gc()
移除清理所有已过期的 key
5. eaccelerator_lock($key)
为 $key 加上锁定操作,以保证多进程多线程操作时数据的同步。需要调用 eaccelerator_unlock($key) 来释放这个锁或等待程序请求结束时自动释放这个锁。
例如:
eaccelerator_lock("count");
eaccelerator_put("count",eaccelerator_get("count")+1));
?>
6. eaccelerator_unlock($key)
根据 $key 释放锁
7. eaccelerator_cache_output($key, $eval_code, $ttl=0)
将 $eval_code 代码的输出缓存 $ttl 秒,($ttl参数同 eacclerator_put)
For Example:
8. eaccelerator_cache_result($key, $eval_code, $ttl=0)
将 $eval_code 代码的执行结果缓存 $ttl 秒,($ttl参数同 eacclerator_put),类似 cache_output
For Example:
9. eaccelerator_cache_page($key, $ttl=0)
将当前整页缓存 $ttl 秒。
For Example:
eaccelerator_cache_page($_SERVER['PHP_SELF'].'?GET='.serialize($_GET),30);
echo time();
phpinfo();
?>
10. eaccelerator_rm_page($key)
删除由 eaccelerator_cache_page() 执行的缓存,参数也是 $key
______________________________________________
(作个简单例子看看它的威力,注意在 cli 模式下可能无效!)
<?php
class test_cache {
var $pro = 'hello';
function test_cache() {
echo "Object Created!<br>\n";
}
function func() {
echo ', the world!';
}
function now($t) {
echo date('Y-m-d H:i:s', $t);
}
}
$tt = eaccelerator_get("test_tt");
if (!$tt)
{
$tt = new test_cache;
eaccelerator_put("test_tt", $tt);
echo "no cached!<br>\n";
}
else {
echo "cached<br>\n";
}
echo $tt->pro;
$tt->func();
$tt->now(time() + 86400);
?>
?php>?php>?php>?php>
class test_cache {
var $pro = 'hello';
function test_cache() {
echo "Object Created!<br>\n";
}
function func() {
echo ', the world!';
}
function now($t) {
echo date('Y-m-d H:i:s', $t);
}
}
$tt = eaccelerator_get("test_tt");
if (!$tt)
{
$tt = new test_cache;
eaccelerator_put("test_tt", $tt);
echo "no cached!<br>\n";
}
else {
echo "cached<br>\n";
}
echo $tt->pro;
$tt->func();
$tt->now(time() + 86400);
?>
PHP生成RSS的类:FeedCreator
2007/01/22 14:41 ArthurXF
这个类是直接使用PHP生成RSS,方便好用,支持目前流行的所有RSS格式。
注意下面的使用条件:
1.$item->data 传进去的值应该是int型的timestamp
>>如果是从mysql里取出来的,即便是整型(事实上也是字符串,在php程序中用会自动转换类型),也要用 intval 强制转换一下。
2.需要设一下文件中的时区
>>时区改这里:define("TIME_ZONE", "+08:00");
3.如果中文页面用的是gb2312或者utf-8需要相应的把默认的encoding改掉
>>编码改这里:var $encoding = "GBK"; 不过事实这里只是一个默认值,调用时可以改的。
<?php
/***************************************************************************
FeedCreator class v1.7.2
originally (c) Kai Blankenhorn
www.bitfolge.de
kaib@bitfolge.de
v1.3 work by Scott Reynen (scott@randomchaos.com) and Kai Blankenhorn
v1.5 OPML support by Dirk Clemens
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
****************************************************************************
Changelog:
v1.7.2 10-11-04
license changed to LGPL
v1.7.1
fixed a syntax bug
fixed left over debug code
v1.7 07-18-04
added HTML and JavaScript feeds (configurable via CSS) (thanks to Pascal Van Hecke)
added HTML descriptions for all feed formats (thanks to Pascal Van Hecke)
added a switch to select an external stylesheet (thanks to Pascal Van Hecke)
changed default content-type to application/xml
added character encoding setting
fixed numerous smaller bugs (thanks to S鰎en Fuhrmann of golem.de)
improved changing ATOM versions handling (thanks to August Trometer)
improved the UniversalFeedCreator's useCached method (thanks to S鰎en Fuhrmann of golem.de)
added charset output in HTTP headers (thanks to S鰎en Fuhrmann of golem.de)
added Slashdot namespace to RSS 1.0 (thanks to S鰎en Fuhrmann of golem.de)
v1.6 05-10-04
added stylesheet to RSS 1.0 feeds
fixed generator comment (thanks Kevin L. Papendick and Tanguy Pruvot)
fixed RFC822 date bug (thanks Tanguy Pruvot)
added TimeZone customization for RFC8601 (thanks Tanguy Pruvot)
fixed Content-type could be empty (thanks Tanguy Pruvot)
fixed author/creator in RSS1.0 (thanks Tanguy Pruvot)
v1.6 beta 02-28-04
added Atom 0.3 support (not all features, though)
improved OPML 1.0 support (hopefully - added more elements)
added support for arbitrary additional elements (use with caution)
code beautification :-)
considered beta due to some internal changes
v1.5.1 01-27-04
fixed some RSS 1.0 glitches (thanks to St閜hane Vanpoperynghe)
fixed some inconsistencies between documentation and code (thanks to Timothy Martin)
v1.5 01-06-04
added support for OPML 1.0
added more documentation
v1.4 11-11-03
optional feed saving and caching
improved documentation
minor improvements
v1.3 10-02-03
renamed to FeedCreator, as it not only creates RSS anymore
added support for mbox
tentative support for echo/necho/atom/pie/???
v1.2 07-20-03
intelligent auto-truncating of RSS 0.91 attributes
don't create some attributes when they're not set
documentation improved
fixed a real and a possible bug with date conversions
code cleanup
v1.1 06-29-03
added images to feeds
now includes most RSS 0.91 attributes
added RSS 2.0 feeds
v1.0 06-24-03
initial release
***************************************************************************/
/*** GENERAL USAGE *********************************************************
include("feedcreator.class.php");
$rss = new UniversalFeedCreator();
$rss->useCached(); // use cached version if age<1 hour
$rss->title = "PHP news";
$rss->description = "daily news from the PHP scripting world";
//optional
$rss->descriptionTruncSize = 500;
$rss->descriptionHtmlSyndicated = true;
$rss->link = "http://www.dailyphp.net/news";
$rss->syndicationURL = "http://www.dailyphp.net/".$_SERVER["PHP_SELF"];
$image = new FeedImage();
$image->title = "dailyphp.net logo";
$image->url = "http://www.dailyphp.net/images/logo.gif";
$image->link = "http://www.dailyphp.net";
$image->description = "Feed provided by dailyphp.net. Click to visit.";
//optional
$image->descriptionTruncSize = 500;
$image->descriptionHtmlSyndicated = true;
$rss->image = $image;
// get your news items from somewhere, e.g. your database:
mysql_select_db($dbHost, $dbUser, $dbPass);
$res = mysql_query("SELECT * FROM news ORDER BY newsdate DESC");
while ($data = mysql_fetch_object($res)) {
$item = new FeedItem();
$item->title = $data->title;
$item->link = $data->url;
$item->description = $data->short;
//optional
item->descriptionTruncSize = 500;
item->descriptionHtmlSyndicated = true;
$item->date = $data->newsdate;
$item->source = "http://www.dailyphp.net";
$item->author = "John Doe";
$rss->addItem($item);
}
// valid format strings are: RSS0.91, RSS1.0, RSS2.0, PIE0.1 (deprecated),
// MBOX, OPML, ATOM, ATOM0.3, HTML, JS
echo $rss->saveFeed("RSS1.0", "news/feed.xml");
***************************************************************************
* A little setup *
**************************************************************************/
// your local timezone, set to "" to disable or for GMT
define("TIME_ZONE","+01:00");
/**
* Version string.
**/
define("FEEDCREATOR_VERSION", "FeedCreator 1.7.2");
/**
* A FeedItem is a part of a FeedCreator feed.
*
* @author Kai Blankenhorn <kaib@bitfolge.de>
* @since 1.3
*/
class FeedItem extends HtmlDescribable {
/**
* Mandatory attributes of an item.
*/
var $title, $description, $link;
/**
* Optional attributes of an item.
*/
var $author, $authorEmail, $image, $category, $comments, $guid, $source, $creator;
/**
* Publishing date of an item. May be in one of the following formats:
*
* RFC 822:
* "Mon, 20 Jan 03 18:05:41 +0400"
* "20 Jan 03 18:05:41 +0000"
*
* ISO 8601:
* "2003-01-20T18:05:41+04:00"
*
* Unix:
* 1043082341
*/
var $date;
/**
* Any additional elements to include as an assiciated array. All $key => $value pairs
* will be included unencoded in the feed item in the form
* <$key>$value</$key>
* Again: No encoding will be used! This means you can invalidate or enhance the feed
* if $value contains markup. This may be abused to embed tags not implemented by
* the FeedCreator class used.
*/
var $additionalElements = Array();
// on hold
// var $source;
}
/**
* An FeedImage may be added to a FeedCreator feed.
* @author Kai Blankenhorn <kaib@bitfolge.de>
* @since 1.3
*/
class FeedImage extends HtmlDescribable {
/**
* Mandatory attributes of an image.
*/
var $title, $url, $link;
/**
* Optional attributes of an image.
*/
var $width, $height, $description;
}
/**
* An HtmlDescribable is an item within a feed that can have a description that may
* include HTML markup.
*/
class HtmlDescribable {
/**
* Indicates whether the description field should be rendered in HTML.
*/
var $descriptionHtmlSyndicated;
/**
* Indicates whether and to how many characters a description should be truncated.
*/
var $descriptionTruncSize;
/**
* Returns a formatted description field, depending on descriptionHtmlSyndicated and
* $descriptionTruncSize properties
* @return string the formatted description
*/
function getDescription() {
$descriptionField = new FeedHtmlField($this->description);
$descriptionField->syndicateHtml = $this->descriptionHtmlSyndicated;
$descriptionField->truncSize = $this->descriptionTruncSize;
return $descriptionField->output();
}
}
/**
* An FeedHtmlField describes and generates
* a feed, item or image html field (probably a description). Output is
* generated based on $truncSize, $syndicateHtml properties.
* @author Pascal Van Hecke <feedcreator.class.php@vanhecke.info>
* @version 1.6
*/
class FeedHtmlField {
/**
* Mandatory attributes of a FeedHtmlField.
*/
var $rawFieldContent;
/**
* Optional attributes of a FeedHtmlField.
*
*/
var $truncSize, $syndicateHtml;
/**
* Creates a new instance of FeedHtmlField.
* @param $string: if given, sets the rawFieldContent property
*/
function FeedHtmlField($parFieldContent) {
if ($parFieldContent) {
$this->rawFieldContent = $parFieldContent;
}
}
/**
* Creates the right output, depending on $truncSize, $syndicateHtml properties.
* @return string the formatted field
*/
function output() {
// when field available and syndicated in html we assume
// - valid html in $rawFieldContent and we enclose in CDATA tags
// - no truncation (truncating risks producing invalid html)
if (!$this->rawFieldContent) {
$result = "";
} elseif ($this->syndicateHtml) {
$result = "<![CDATA[".$this->rawFieldContent."]]>";
} else {
if ($this->truncSize and is_int($this->truncSize)) {
$result = FeedCreator::iTrunc(htmlspecialchars($this->rawFieldContent),$this->truncSize);
} else {
$result = htmlspecialchars($this->rawFieldContent);
}
}
return $result;
}
}
/**
* UniversalFeedCreator lets you choose during runtime which
* format to build.
* For general usage of a feed class, see the FeedCreator class
* below or the example above.
*
* @since 1.3
* @author Kai Blankenhorn <kaib@bitfolge.de>
*/
class UniversalFeedCreator extends FeedCreator {
var $_feed;
function _setFormat($format) {
switch (strtoupper($format)) {
case "2.0":
// fall through
case "RSS2.0":
$this->_feed = new RSSCreator20();
break;
case "1.0":
// fall through
case "RSS1.0":
$this->_feed = new RSSCreator10();
break;
case "0.91":
// fall through
case "RSS0.91":
$this->_feed = new RSSCreator091();
break;
case "PIE0.1":
$this->_feed = new PIECreator01();
break;
case "MBOX":
$this->_feed = new MBOXCreator();
break;
case "OPML":
$this->_feed = new OPMLCreator();
break;
case "ATOM":
// fall through: always the latest ATOM version
case "ATOM0.3":
$this->_feed = new AtomCreator03();
break;
case "HTML":
$this->_feed = new HTMLCreator();
break;
case "JS":
// fall through
case "JAVASCRIPT":
$this->_feed = new JSCreator();
break;
default:
$this->_feed = new RSSCreator091();
break;
}
$vars = get_object_vars($this);
foreach ($vars as $key => $value) {
// prevent overwriting of properties "contentType", "encoding"; do not copy "_feed" itself
if (!in_array($key, array("_feed", "contentType", "encoding"))) {
$this->_feed->{$key} = $this->{$key};
}
}
}
/**
* Creates a syndication feed based on the items previously added.
*
* @see FeedCreator::addItem()
* @param string format format the feed should comply to. Valid values are:
* "PIE0.1", "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM0.3", "HTML", "JS"
* @return string the contents of the feed.
*/
function createFeed($format = "RSS0.91") {
$this->_setFormat($format);
return $this->_feed->createFeed();
}
/**
* Saves this feed as a file on the local disk. After the file is saved, an HTTP redirect
* header may be sent to redirect the use to the newly created file.
* @since 1.4
*
* @param string format format the feed should comply to. Valid values are:
* "PIE0.1" (deprecated), "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM", "ATOM0.3", "HTML", "JS"
* @param string filename optional the filename where a recent version of the feed is saved. If not specified, the filename is $_SERVER["PHP_SELF"] with the extension changed to .xml (see _generateFilename()).
* @param boolean displayContents optional send the content of the file or not. If true, the file will be sent in the body of the response.
*/
function saveFeed($format="RSS0.91", $filename="", $displayContents=true) {
$this->_setFormat($format);
$this->_feed->saveFeed($filename, $displayContents);
}
/**
* Turns on caching and checks if there is a recent version of this feed in the cache.
* If there is, an HTTP redirect header is sent.
* To effectively use caching, you should create the FeedCreator object and call this method
* before anything else, especially before you do the time consuming task to build the feed
* (web fetching, for example).
*
* @param string format format the feed should comply to. Valid values are:
* "PIE0.1" (deprecated), "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM0.3".
* @param filename string optional the filename where a recent version of the feed is saved. If not specified, the filename is $_SERVER["PHP_SELF"] with the extension changed to .xml (see _generateFilename()).
* @param timeout int optional the timeout in seconds before a cached version is refreshed (defaults to 3600 = 1 hour)
*/
function useCached($format="RSS0.91", $filename="", $timeout=3600) {
$this->_setFormat($format);
$this->_feed->useCached($filename, $timeout);
}
}
/**
* FeedCreator is the abstract base implementation for concrete
* implementations that implement a specific format of syndication.
*
* @abstract
* @author Kai Blankenhorn <kaib@bitfolge.de>
* @since 1.4
*/
class FeedCreator extends HtmlDescribable {
/**
* Mandatory attributes of a feed.
*/
var $title, $description, $link;
/**
* Optional attributes of a feed.
*/
var $syndicationURL, $image, $language, $copyright, $pubDate, $lastBuildDate, $editor, $editorEmail, $webmaster, $category, $docs, $ttl, $rating, $skipHours, $skipDays;
/**
* The url of the external xsl stylesheet used to format the naked rss feed.
* Ignored in the output when empty.
*/
var $xslStyleSheet = "";
/**
* @access private
*/
var $items = Array();
/**
* This feed's MIME content type.
* @since 1.4
* @access private
*/
var $contentType = "application/xml";
/**
* This feed's character encoding.
* @since 1.6.1
**/
var $encoding = "ISO-8859-1";
/**
* Any additional elements to include as an assiciated array. All $key => $value pairs
* will be included unencoded in the feed in the form
* <$key>$value</$key>
* Again: No encoding will be used! This means you can invalidate or enhance the feed
* if $value contains markup. This may be abused to embed tags not implemented by
* the FeedCreator class used.
*/
var $additionalElements = Array();
/**
* Adds an FeedItem to the feed.
*
* @param object FeedItem $item The FeedItem to add to the feed.
* @access public
*/
function addItem($item) {
$this->items[] = $item;
}
/**
* Truncates a string to a certain length at the most sensible point.
* First, if there's a '.' character near the end of the string, the string is truncated after this character.
* If there is no '.', the string is truncated after the last ' ' character.
* If the string is truncated, " ..." is appended.
* If the string is already shorter than $length, it is returned unchanged.
*
* @static
* @param string string A string to be truncated.
* @param int length the maximum length the string should be truncated to
* @return string the truncated string
*/
function iTrunc($string, $length) {
if (strlen($string)<=$length) {
return $string;
}
$pos = strrpos($string,".");
if ($pos>=$length-4) {
$string = substr($string,0,$length-4);
$pos = strrpos($string,".");
}
if ($pos>=$length*0.4) {
return substr($string,0,$pos+1)." ...";
}
$pos = strrpos($string," ");
if ($pos>=$length-4) {
$string = substr($string,0,$length-4);
$pos = strrpos($string," ");
}
if ($pos>=$length*0.4) {
return substr($string,0,$pos)." ...";
}
return substr($string,0,$length-4)." ...";
}
/**
* Creates a comment indicating the generator of this feed.
* The format of this comment seems to be recognized by
* Syndic8.com.
*/
function _createGeneratorComment() {
return "<!-- generator=&#92;"".FEEDCREATOR_VERSION."&#92;" -->\n";
}
/**
* Creates a string containing all additional elements specified in
* $additionalElements.
* @param elements array an associative array containing key => value pairs
* @param indentString string a string that will be inserted before every generated line
* @return string the XML tags corresponding to $additionalElements
*/
function _createAdditionalElements($elements, $indentString="") {
$ae = "";
if (is_array($elements)) {
foreach($elements AS $key => $value) {
$ae.= $indentString."<$key>$value</$key>\n";
}
}
return $ae;
}
function _createStylesheetReferences() {
$xml = "";
if ($this->cssStyleSheet) $xml .= "<?xml-stylesheet href=\"".$this->cssStyleSheet."\" type=\"text/css\"?>\n";
if ($this->xslStyleSheet) $xml .= "<?xml-stylesheet href=\"".$this->xslStyleSheet."\" type=\"text/xsl\"?>\n";
return $xml;
}
/**
* Builds the feed's text.
* @abstract
* @return string the feed's complete text
*/
function createFeed() {
}
/**
* Generate a filename for the feed cache file. The result will be $_SERVER["PHP_SELF"] with the extension changed to .xml.
* For example:
*
* echo $_SERVER["PHP_SELF"]."\n";
* echo FeedCreator::_generateFilename();
*
* would produce:
*
* /rss/latestnews.php
* latestnews.xml
*
* @return string the feed cache filename
* @since 1.4
* @access private
*/
function _generateFilename() {
$fileInfo = pathinfo($_SERVER["PHP_SELF"]);
return substr($fileInfo["basename"],0,-(strlen($fileInfo["extension"])+1)).".xml";
}
/**
* @since 1.4
* @access private
*/
function _redirect($filename) {
// attention, heavily-commented-out-area
// maybe use this in addition to file time checking
//Header("Expires: ".date("r",time()+$this->_timeout));
/* no caching at all, doesn't seem to work as good:
Header("Cache-Control: no-cache");
Header("Pragma: no-cache");
*/
// HTTP redirect, some feed readers' simple HTTP implementations don't follow it
//Header("Location: ".$filename);
Header("Content-Type: ".$this->contentType."; charset=".$this->encoding."; filename=".basename($filename));
Header("Content-Disposition: inline; filename=".basename($filename));
readfile($filename, "r");
die();
}
/**
* Turns on caching and checks if there is a recent version of this feed in the cache.
* If there is, an HTTP redirect header is sent.
* To effectively use caching, you should create the FeedCreator object and call this method
* before anything else, especially before you do the time consuming task to build the feed
* (web fetching, for example).
* @since 1.4
* @param filename string optional the filename where a recent version of the feed is saved. If not specified, the filename is $_SERVER["PHP_SELF"] with the extension changed to .xml (see _generateFilename()).
* @param timeout int optional the timeout in seconds before a cached version is refreshed (defaults to 3600 = 1 hour)
*/
function useCached($filename="", $timeout=3600) {
$this->_timeout = $timeout;
if ($filename=="") {
$filename = $this->_generateFilename();
}
if (file_exists($filename) AND (time()-filemtime($filename) < $timeout)) {
$this->_redirect($filename);
}
}
/**
* Saves this feed as a file on the local disk. After the file is saved, a redirect
* header may be sent to redirect the user to the newly created file.
* @since 1.4
*
* @param filename string optional the filename where a recent version of the feed is saved. If not specified, the filename is $_SERVER["PHP_SELF"] with the extension changed to .xml (see _generateFilename()).
* @param redirect boolean optional send an HTTP redirect header or not. If true, the user will be automatically redirected to the created file.
*/
function saveFeed($filename="", $displayContents=true) {
if ($filename=="") {
$filename = $this->_generateFilename();
}
$feedFile = fopen($filename, "w+");
if ($feedFile) {
fputs($feedFile,$this->createFeed());
fclose($feedFile);
if ($displayContents) {
$this->_redirect($filename);
}
} else {
echo "<br /><b>Error creating feed file, please check write permissions.</b><br />";
}
}
}
/**
* FeedDate is an internal class that stores a date for a feed or feed item.
* Usually, you won't need to use this.
*/
class FeedDate {
var $unix;
/**
* Creates a new instance of FeedDate representing a given date.
* Accepts RFC 822, ISO 8601 date formats as well as unix time stamps.
* @param mixed $dateString optional the date this FeedDate will represent. If not specified, the current date and time is used.
*/
function FeedDate($dateString="") {
if ($dateString=="") $dateString = date("r");
if (is_integer($dateString)) {
$this->unix = $dateString;
return;
}
if (preg_match("~(?:(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun),\\s+)?(\\d{1,2})\\s+([a-zA-Z]{3})\\s+(\\d{4})\\s+(\\d{2}):(\\d{2}):(\\d{2})\\s+(.*)~",$dateString,$matches)) {
$months = Array("Jan"=>1,"Feb"=>2,"Mar"=>3,"Apr"=>4,"May"=>5,"Jun"=>6,"Jul"=>7,"Aug"=>8,"Sep"=>9,"Oct"=>10,"Nov"=>11,"Dec"=>12);
$this->unix = mktime($matches[4],$matches[5],$matches[6],$months[$matches[2]],$matches[1],$matches[3]);
if (substr($matches[7],0,1)=='+' OR substr($matches[7],0,1)=='-') {
$tzOffset = (substr($matches[7],0,3) * 60 + substr($matches[7],-2)) * 60;
} else {
if (strlen($matches[7])==1) {
$oneHour = 3600;
$ord = ord($matches[7]);
if ($ord < ord("M")) {
$tzOffset = (ord("A") - $ord - 1) * $oneHour;
} elseif ($ord >= ord("M") AND $matches[7]!="Z") {
$tzOffset = ($ord - ord("M")) * $oneHour;
} elseif ($matches[7]=="Z") {
$tzOffset = 0;
}
}
switch ($matches[7]) {
case "UT":
case "GMT": $tzOffset = 0;
}
}
$this->unix += $tzOffset;
return;
}
if (preg_match("~(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2})(.*)~",$dateString,$matches)) {
$this->unix = mktime($matches[4],$matches[5],$matches[6],$matches[2],$matches[3],$matches[1]);
if (substr($matches[7],0,1)=='+' OR substr($matches[7],0,1)=='-') {
$tzOffset = (substr($matches[7],0,3) * 60 + substr($matches[7],-2)) * 60;
} else {
if ($matches[7]=="Z") {
$tzOffset = 0;
}
}
$this->unix += $tzOffset;
return;
}
$this->unix = 0;
}
/**
* Gets the date stored in this FeedDate as an RFC 822 date.
*
* @return a date in RFC 822 format
*/
function rfc822() {
//return gmdate("r",$this->unix);
$date = gmdate("D, d M Y H:i:s", $this->unix);
if (TIME_ZONE!="") $date .= " ".str_replace(":","",TIME_ZONE);
return $date;
}
/**
* Gets the date stored in this FeedDate as an ISO 8601 date.
*
* @return a date in ISO 8601 format
*/
function iso8601() {
$date = gmdate("Y-m-d\TH:i:sO",$this->unix);
$date = substr($date,0,22) . ':' . substr($date,-2);
if (TIME_ZONE!="") $date = str_replace("+00:00",TIME_ZONE,$date);
return $date;
}
/**
* Gets the date stored in this FeedDate as unix time stamp.
*
* @return a date as a unix time stamp
*/
function unix() {
return $this->unix;
}
}
/**
* RSSCreator10 is a FeedCreator that implements RDF Site Summary (RSS) 1.0.
*
* @see http://www.purl.org/rss/1.0/
* @since 1.3
* @author Kai Blankenhorn <kaib@bitfolge.de>
*/
class RSSCreator10 extends FeedCreator {
/**
* Builds the RSS feed's text. The feed will be compliant to RDF Site Summary (RSS) 1.0.
* The feed will contain all items previously added in the same order.
* @return string the feed's complete text
*/
function createFeed() {
$feed = "<?xml version=\"1.0\" encoding=\"".$this->encoding."\"?>\n";
$feed.= $this->_createGeneratorComment();
if ($this->cssStyleSheet=="") {
$cssStyleSheet = "http://www.w3.org/2000/08/w3c-synd/style.css";
}
$feed.= $this->_createStylesheetReferences();
$feed.= "<rdf:RDF\n";
$feed.= " xmlns=\"http://purl.org/rss/1.0/\"\n";
$feed.= " xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n";
$feed.= " xmlns:slash=\"http://purl.org/rss/1.0/modules/slash/\"\n";
$feed.= " xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n";
$feed.= " <channel rdf:about=\"".$this->syndicationURL."\">\n";
$feed.= " <title>".htmlspecialchars($this->title)."</title>\n";
$feed.= " <description>".htmlspecialchars($this->description)."</description>\n";
$feed.= " <link>".$this->link."</link>\n";
if ($this->image!=null) {
$feed.= " <image rdf:resource=\"".$this->image->url."\" />\n";
}
$now = new FeedDate();
$feed.= " <dc:date>".htmlspecialchars($now->iso8601())."</dc:date>\n";
$feed.= " <items>\n";
$feed.= " <rdf:Seq>\n";
for ($i=0;$i<count($this->items);$i++) {
$feed.= " <rdf:li rdf:resource=\"".htmlspecialchars($this->items[$i]->link)."\"/>\n";
}
$feed.= " </rdf:Seq>\n";
$feed.= " </items>\n";
$feed.= " </channel>\n";
if ($this->image!=null) {
$feed.= " <image rdf:about=\"".$this->image->url."\">\n";
$feed.= " <title>".$this->image->title."</title>\n";
$feed.= " <link>".$this->image->link."</link>\n";
$feed.= " <url>".$this->image->url."</url>\n";
$feed.= " </image>\n";
}
$feed.= $this->_createAdditionalElements($this->additionalElements, " ");
for ($i=0;$i<count($this->items);$i++) {
$feed.= " <item rdf:about=\"".htmlspecialchars($this->items[$i]->link)."\">\n";
//$feed.= " <dc:type>Posting</dc:type>\n";
$feed.= " <dc:format>text/html</dc:format>\n";
if ($this->items[$i]->date!=null) {
$itemDate = new FeedDate($this->items[$i]->date);
$feed.= " <dc:date>".htmlspecialchars($itemDate->iso8601())."</dc:date>\n";
}
if ($this->items[$i]->source!="") {
$feed.= " <dc:source>".htmlspecialchars($this->items[$i]->source)."</dc:source>\n";
}
if ($this->items[$i]->author!="") {
$feed.= " <dc:creator>".htmlspecialchars($this->items[$i]->author)."</dc:creator>\n";
}
$feed.= " <title>".htmlspecialchars(strip_tags(strtr($this->items[$i]->title,"\n\r"," ")))."</title>\n";
$feed.= " <link>".htmlspecialchars($this->items[$i]->link)."</link>\n";
$feed.= " <description>".htmlspecialchars($this->items[$i]->description)."</description>\n";
$feed.= $this->_createAdditionalElements($this->items[$i]->additionalElements, " ");
$feed.= " </item>\n";
}
$feed.= "</rdf:RDF>\n";
return $feed;
}
}
/**
* RSSCreator091 is a FeedCreator that implements RSS 0.91 Spec, revision 3.
*
* @see http://my.netscape.com/publish/formats/rss-spec-0.91.html
* @since 1.3
* @author Kai Blankenhorn <kaib@bitfolge.de>
*/
class RSSCreator091 extends FeedCreator {
/**
* Stores this RSS feed's version number.
* @access private
*/
var $RSSVersion;
function RSSCreator091() {
$this->_setRSSVersion("0.91");
$this->contentType = "application/rss+xml";
}
/**
* Sets this RSS feed's version number.
* @access private
*/
function _setRSSVersion($version) {
$this->RSSVersion = $version;
}
/**
* Builds the RSS feed's text. The feed will be compliant to RDF Site Summary (RSS) 1.0.
* The feed will contain all items previously added in the same order.
* @return string the feed's complete text
*/
function createFeed() {
$feed = "<?xml version=\"1.0\" encoding=\"".$this->encoding."\"?>\n";
$feed.= $this->_createGeneratorComment();
$feed.= $this->_createStylesheetReferences();
$feed.= "<rss version=\"".$this->RSSVersion."\">\n";
$feed.= " <channel>\n";
$feed.= " <title>".FeedCreator::iTrunc(htmlspecialchars($this->title),100)."</title>\n";
$this->descriptionTruncSize = 500;
$feed.= " <description>".$this->getDescription()."</description>\n";
$feed.= " <link>".$this->link."</link>\n";
$now = new FeedDate();
$feed.= " <lastBuildDate>".htmlspecialchars($now->rfc822())."</lastBuildDate>\n";
$feed.= " <generator>".FEEDCREATOR_VERSION."</generator>\n";
if ($this->image!=null) {
$feed.= " <image>\n";
$feed.= " <url>".$this->image->url."</url>\n";
$feed.= " <title>".FeedCreator::iTrunc(htmlspecialchars($this->image->title),100)."</title>\n";
$feed.= " <link>".$this->image->link."</link>\n";
if ($this->image->width!="") {
$feed.= " <width>".$this->image->width."</width>\n";
}
if ($this->image->height!="") {
$feed.= " <height>".$this->image->height."</height>\n";
}
if ($this->image->description!="") {
$feed.= " <description>".$this->image->getDescription()."</description>\n";
}
$feed.= " </image>\n";
}
if ($this->language!="") {
$feed.= " <language>".$this->language."</language>\n";
}
if ($this->copyright!="") {
$feed.= " <copyright>".FeedCreator::iTrunc(htmlspecialchars($this->copyright),100)."</copyright>\n";
}
if ($this->editor!="") {
$feed.= " <managingEditor>".FeedCreator::iTrunc(htmlspecialchars($this->editor),100)."</managingEditor>\n";
}
if ($this->webmaster!="") {
$feed.= " <webMaster>".FeedCreator::iTrunc(htmlspecialchars($this->webmaster),100)."</webMaster>\n";
}
if ($this->pubDate!="") {
$pubDate = new FeedDate($this->pubDate);
$feed.= " <pubDate>".htmlspecialchars($pubDate->rfc822())."</pubDate>\n";
}
if ($this->category!="") {
$feed.= " <category>".htmlspecialchars($this->category)."</category>\n";
}
if ($this->docs!="") {
$feed.= " <docs>".FeedCreator::iTrunc(htmlspecialchars($this->docs),500)."</docs>\n";
}
if ($this->ttl!="") {
$feed.= " <ttl>".htmlspecialchars($this->ttl)."</ttl>\n";
}
if ($this->rating!="") {
$feed.= " <rating>".FeedCreator::iTrunc(htmlspecialchars($this->rating),500)."</rating>\n";
}
if ($this->skipHours!="") {
$feed.= " <skipHours>".htmlspecialchars($this->skipHours)."</skipHours>\n";
}
if ($this->skipDays!="") {
$feed.= " <skipDays>".htmlspecialchars($this->skipDays)."</skipDays>\n";
}
$feed.= $this->_createAdditionalElements($this->additionalElements, " ");
for ($i=0;$i<count($this->items);$i++) {
$feed.= " <item>\n";
$feed.= " <title>".FeedCreator::iTrunc(htmlspecialchars(strip_tags($this->items[$i]->title)),100)."</title>\n";
$feed.= " <link>".htmlspecialchars($this->items[$i]->link)."</link>\n";
$feed.= " <description>".$this->items[$i]->getDescription()."</description>\n";
if ($this->items[$i]->author!="") {
$feed.= " <author>".htmlspecialchars($this->items[$i]->author)."</author>\n";
}
/*
// on hold
if ($this->items[$i]->source!="") {
$feed.= " <source>".htmlspecialchars($this->items[$i]->source)."</source>\n";
}
*/
if ($this->items[$i]->category!="") {
$feed.= " <category>".htmlspecialchars($this->items[$i]->category)."</category>\n";
}
if ($this->items[$i]->comments!="") {
$feed.= " <comments>".htmlspecialchars($this->items[$i]->comments)."</comments>\n";
}
if ($this->items[$i]->date!="") {
$itemDate = new FeedDate($this->items[$i]->date);
$feed.= " <pubDate>".htmlspecialchars($itemDate->rfc822())."</pubDate>\n";
}
if ($this->items[$i]->guid!="") {
$feed.= " <guid>".htmlspecialchars($this->items[$i]->guid)."</guid>\n";
}
$feed.= $this->_createAdditionalElements($this->items[$i]->additionalElements, " ");
$feed.= " </item>\n";
}
$feed.= " </channel>\n";
$feed.= "</rss>\n";
return $feed;
}
}
/**
* RSSCreator20 is a FeedCreator that implements RDF Site Summary (RSS) 2.0.
*
* @see http://backend.userland.com/rss
* @since 1.3
* @author Kai Blankenhorn <kaib@bitfolge.de>
*/
class RSSCreator20 extends RSSCreator091 {
function RSSCreator20() {
parent::_setRSSVersion("2.0");
}
}
/**
* PIECreator01 is a FeedCreator that implements the emerging PIE specification,
* as in http://intertwingly.net/wiki/pie/Syntax.
*
* @deprecated
* @since 1.3
* @author Scott Reynen <scott@randomchaos.com> and Kai Blankenhorn <kaib@bitfolge.de>
*/
class PIECreator01 extends FeedCreator {
function PIECreator01() {
$this->encoding = "utf-8";
}
function createFeed() {
$feed = "<?xml version=\"1.0\" encoding=\"".$this->encoding."\"?>\n";
$feed.= $this->_createStylesheetReferences();
$feed.= "<feed version=\"0.1\" xmlns=\"http://example.com/newformat#\">\n";
$feed.= " <title>".FeedCreator::iTrunc(htmlspecialchars($this->title),100)."</title>\n";
$this->truncSize = 500;
$feed.= " <subtitle>".$this->getDescription()."</subtitle>\n";
$feed.= " <link>".$this->link."</link>\n";
for ($i=0;$i<count($this->items);$i++) {
$feed.= " <entry>\n";
$feed.= " <title>".FeedCreator::iTrunc(htmlspecialchars(strip_tags($this->items[$i]->title)),100)."</title>\n";
$feed.= " <link>".htmlspecialchars($this->items[$i]->link)."</link>\n";
$itemDate = new FeedDate($this->items[$i]->date);
$feed.= " <created>".htmlspecialchars($itemDate->iso8601())."</created>\n";
$feed.= " <issued>".htmlspecialchars($itemDate->iso8601())."</issued>\n";
$feed.= " <modified>".htmlspecialchars($itemDate->iso8601())."</modified>\n";
$feed.= " <id>".htmlspecialchars($this->items[$i]->guid)."</id>\n";
if ($this->items[$i]->author!="") {
$feed.= " <author>\n";
$feed.= " <name>".htmlspecialchars($this->items[$i]->author)."</name>\n";
if ($this->items[$i]->authorEmail!="") {
$feed.= " <email>".$this->items[$i]->authorEmail."</email>\n";
}
$feed.=" </author>\n";
}
$feed.= " <content type=\"text/html\" xml:lang=\"en-us\">\n";
$feed.= " <div xmlns=\"http://www.w3.org/1999/xhtml\">".$this->items[$i]->getDescription()."</div>\n";
$feed.= " </content>\n";
$feed.= " </entry>\n";
}
$feed.= "</feed>\n";
return $feed;
}
}
/**
* AtomCreator03 is a FeedCreator that implements the atom specification,
* as in http://www.intertwingly.net/wiki/pie/FrontPage.
* Please note that just by using AtomCreator03 you won't automatically
* produce valid atom files. For example, you have to specify either an editor
* for the feed or an author for every single feed item.
*
* Some elements have not been implemented yet. These are (incomplete list):
* author URL, item author's email and URL, item contents, alternate links,
* other link content types than text/html. Some of them may be created with
* AtomCreator03::additionalElements.
*
* @see FeedCreator#additionalElements
* @since 1.6
* @author Kai Blankenhorn <kaib@bitfolge.de>, Scott Reynen <scott@randomchaos.com>
*/
class AtomCreator03 extends FeedCreator {
function AtomCreator03() {
$this->contentType = "application/atom+xml";
$this->encoding = "utf-8";
}
function createFeed() {
$feed = "<?xml version=\"1.0\" encoding=\"".$this->encoding."\"?>\n";
$feed.= $this->_createGeneratorComment();
$feed.= $this->_createStylesheetReferences();
$feed.= "<feed version=\"0.3\" xmlns=\"http://purl.org/atom/ns#\"";
if ($this->language!="") {
$feed.= " xml:lang=\"".$this->language."\"";
}
$feed.= ">\n";
$feed.= " <title>".htmlspecialchars($this->title)."</title>\n";
$feed.= " <tagline>".htmlspecialchars($this->description)."</tagline>\n";
$feed.= " <link rel=\"alternate\" type=\"text/html\" href=\"".htmlspecialchars($this->link)."\"/>\n";
$feed.= " <id>".htmlspecialchars($this->link)."</id>\n";
$now = new FeedDate();
$feed.= " <modified>".htmlspecialchars($now->iso8601())."</modified>\n";
if ($this->editor!="") {
$feed.= " <author>\n";
$feed.= " <name>".$this->editor."</name>\n";
if ($this->editorEmail!="") {
$feed.= " <email>".$this->editorEmail."</email>\n";
}
$feed.= " </author>\n";
}
$feed.= " <generator>".FEEDCREATOR_VERSION."</generator>\n";
$feed.= $this->_createAdditionalElements($this->additionalElements, " ");
for ($i=0;$i<count($this->items);$i++) {
$feed.= " <entry>\n";
$feed.= " <title>".htmlspecialchars(strip_tags($this->items[$i]->title))."</title>\n";
$feed.= " <link rel=\"alternate\" type=\"text/html\" href=\"".htmlspecialchars($this->items[$i]->link)."\"/>\n";
if ($this->items[$i]->date=="") {
$this->items[$i]->date = time();
}
$itemDate = new FeedDate($this->items[$i]->date);
$feed.= " <created>".htmlspecialchars($itemDate->iso8601())."</created>\n";
$feed.= " <issued>".htmlspecialchars($itemDate->iso8601())."</issued>\n";
$feed.= " <modified>".htmlspecialchars($itemDate->iso8601())."</modified>\n";
$feed.= " <id>".htmlspecialchars($this->items[$i]->link)."</id>\n";
$feed.= $this->_createAdditionalElements($this->items[$i]->additionalElements, " ");
if ($this->items[$i]->author!="") {
$feed.= " <author>\n";
$feed.= " <name>".htmlspecialchars($this->items[$i]->author)."</name>\n";
$feed.= " </author>\n";
}
if ($this->items[$i]->description!="") {
$feed.= " <summary>".htmlspecialchars($this->items[$i]->description)."</summary>\n";
}
$feed.= " </entry>\n";
}
$feed.= "</feed>\n";
return $feed;
}
}
/**
* MBOXCreator is a FeedCreator that implements the mbox format
* as described in http://www.qmail.org/man/man5/mbox.html
*
* @since 1.3
* @author Kai Blankenhorn <kaib@bitfolge.de>
*/
class MBOXCreator extends FeedCreator {
function MBOXCreator() {
$this->contentType = "text/plain";
$this->encoding = "ISO-8859-15";
}
function qp_enc($input = "", $line_max = 76) {
$hex = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
$lines = preg_split("/(?:\r\n|\r|\n)/", $input);
$eol = "\r\n";
$escape = "=";
$output = "";
while( list(, $line) = each($lines) ) {
//$line = rtrim($line); // remove trailing white space -> no =20\r\n necessary
$linlen = strlen($line);
$newline = "";
for($i = 0; $i < $linlen; $i++) {
$c = substr($line, $i, 1);
$dec = ord($c);
if ( ($dec == 32) && ($i == ($linlen - 1)) ) { // convert space at eol only
$c = "=20";
} elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { // always encode "\t", which is *not* required
$h2 = floor($dec/16); $h1 = floor($dec%16);
$c = $escape.$hex["$h2"].$hex["$h1"];
}
if ( (strlen($newline) + strlen($c)) >= $line_max ) { // CRLF is not counted
$output .= $newline.$escape.$eol; // soft line break; " =\r\n" is okay
$newline = "";
}
$newline .= $c;
} // end of for
$output .= $newline.$eol;
}
return trim($output);
}
/**
* Builds the MBOX contents.
* @return string the feed's complete text
*/
function createFeed() {
for ($i=0;$i<count($this->items);$i++) {
if ($this->items[$i]->author!="") {
$from = $this->items[$i]->author;
} else {
$from = $this->title;
}
$itemDate = new FeedDate($this->items[$i]->date);
$feed.= "From ".strtr(MBOXCreator::qp_enc($from)," ","_")." ".date("D M d H:i:s Y",$itemDate->unix())."\n";
$feed.= "Content-Type: text/plain;\n";
$feed.= " charset=\"".$this->encoding."\"\n";
$feed.= "Content-Transfer-Encoding: quoted-printable\n";
$feed.= "Content-Type: text/plain\n";
$feed.= "From: \"".MBOXCreator::qp_enc($from)."\"\n";
$feed.= "Date: ".$itemDate->rfc822()."\n";
$feed.= "Subject: ".MBOXCreator::qp_enc(FeedCreator::iTrunc($this->items[$i]->title,100))."\n";
$feed.= "\n";
$body = chunk_split(MBOXCreator::qp_enc($this->items[$i]->description));
$feed.= preg_replace("~\nFrom ([^\n]*)(\n?)~","\n>From $1$2\n",$body);
$feed.= "\n";
$feed.= "\n";
}
return $feed;
}
/**
* Generate a filename for the feed cache file. Overridden from FeedCreator to prevent XML data types.
* @return string the feed cache filename
* @since 1.4
* @access private
*/
function _generateFilename() {
$fileInfo = pathinfo($_SERVER["PHP_SELF"]);
return substr($fileInfo["basename"],0,-(strlen($fileInfo["extension"])+1)).".mbox";
}
}
/**
* OPMLCreator is a FeedCreator that implements OPML 1.0.
*
* @see http://opml.scripting.com/spec
* @author Dirk Clemens, Kai Blankenhorn
* @since 1.5
*/
class OPMLCreator extends FeedCreator {
function OPMLCreator() {
$this->encoding = "utf-8";
}
function createFeed() {
$feed = "<?xml version=\"1.0\" encoding=\"".$this->encoding."\"?>\n";
$feed.= $this->_createGeneratorComment();
$feed.= $this->_createStylesheetReferences();
$feed.= "<opml xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n";
$feed.= " <head>\n";
$feed.= " <title>".htmlspecialchars($this->title)."</title>\n";
if ($this->pubDate!="") {
$date = new FeedDate($this->pubDate);
$feed.= " <dateCreated>".$date->rfc822()."</dateCreated>\n";
}
if ($this->lastBuildDate!="") {
$date = new FeedDate($this->lastBuildDate);
$feed.= " <dateModified>".$date->rfc822()."</dateModified>\n";
}
if ($this->editor!="") {
$feed.= " <ownerName>".$this->editor."</ownerName>\n";
}
if ($this->editorEmail!="") {
$feed.= " <ownerEmail>".$this->editorEmail."</ownerEmail>\n";
}
$feed.= " </head>\n";
$feed.= " <body>\n";
for ($i=0;$i<count($this->items);$i++) {
$feed.= " <outline type=\"rss\" ";
$title = htmlspecialchars(strip_tags(strtr($this->items[$i]->title,"\n\r"," ")));
$feed.= " title=\"".$title."\"";
$feed.= " text=\"".$title."\"";
//$feed.= " description=\"".htmlspecialchars($this->items[$i]->description)."\"";
$feed.= " url=\"".htmlspecialchars($this->items[$i]->link)."\"";
$feed.= "/>\n";
}
$feed.= " </body>\n";
$feed.= "</opml>\n";
return $feed;
}
}
/**
* HTMLCreator is a FeedCreator that writes an HTML feed file to a specific
* location, overriding the createFeed method of the parent FeedCreator.
* The HTML produced can be included over http by scripting languages, or serve
* as the source for an IFrame.
* All output by this class is embedded in <div></div> tags to enable formatting
* using CSS.
*
* @author Pascal Van Hecke
* @since 1.7
*/
class HTMLCreator extends FeedCreator {
var $contentType = "text/html";
/**
* Contains HTML to be output at the start of the feed's html representation.
*/
var $header;
/**
* Contains HTML to be output at the end of the feed's html representation.
*/
var $footer ;
/**
* Contains HTML to be output between entries. A separator is only used in
* case of multiple entries.
*/
var $separator;
/**
* Used to prefix the stylenames to make sure they are unique
* and do not clash with stylenames on the users' page.
*/
var $stylePrefix;
/**
* Determines whether the links open in a new window or not.
*/
var $openInNewWindow = true;
var $imageAlign ="right";
/**
* In case of very simple output you may want to get rid of the style tags,
* hence this variable. There's no equivalent on item level, but of course you can
* add strings to it while iterating over the items ($this->stylelessOutput .= ...)
* and when it is non-empty, ONLY the styleless output is printed, the rest is ignored
* in the function createFeed().
*/
var $stylelessOutput ="";
/**
* Writes the HTML.
* @return string the scripts's complete text
*/
function createFeed() {
// if there is styleless output, use the content of this variable and ignore the rest
if ($this->stylelessOutput!="") {
return $this->stylelessOutput;
}
//if no stylePrefix is set, generate it yourself depending on the script name
if ($this->stylePrefix=="") {
$this->stylePrefix = str_replace(".", "_", $this->_generateFilename())."_";
}
//set an openInNewWindow_token_to be inserted or not
if ($this->openInNewWindow) {
$targetInsert = " target='_blank'";
}
// use this array to put the lines in and implode later with "document.write" javascript
$feedArray = array();
if ($this->image!=null) {
$imageStr = "<a href='".$this->image->link."'".$targetInsert.">".
"<img src='".$this->image->url."' border='0' alt='".
FeedCreator::iTrunc(htmlspecialchars($this->image->title),100).
"' align='".$this->imageAlign."' ";
if ($this->image->width) {
$imageStr .=" width='".$this->image->width. "' ";
}
if ($this->image->height) {
$imageStr .=" height='".$this->image->height."' ";
}
$imageStr .="/></a>";
$feedArray[] = $imageStr;
}
if ($this->title) {
$feedArray[] = "<div class='".$this->stylePrefix."title'><a href='".$this->link."' ".$targetInsert." class='".$this->stylePrefix."title'>".
FeedCreator::iTrunc(htmlspecialchars($this->title),100)."</a></div>";
}
if ($this->getDescription()) {
$feedArray[] = "<div class='".$this->stylePrefix."description'>".
str_replace("]]>", "", str_replace("<![CDATA[", "", $this->getDescription())).
"</div>";
}
if ($this->header) {
$feedArray[] = "<div class='".$this->stylePrefix."header'>".$this->header."</div>";
}
for ($i=0;$i<count($this->items);$i++) {
if ($this->separator and $i > 0) {
$feedArray[] = "<div class='".$this->stylePrefix."separator'>".$this->separator."</div>";
}
if ($this->items[$i]->title) {
if ($this->items[$i]->link) {
$feedArray[] =
"<div class='".$this->stylePrefix."item_title'><a href='".$this->items[$i]->link."' class='".$this->stylePrefix.
"item_title'".$targetInsert.">".FeedCreator::iTrunc(htmlspecialchars(strip_tags($this->items[$i]->title)),100).
"</a></div>";
} else {
$feedArray[] =
"<div class='".$this->stylePrefix."item_title'>".
FeedCreator::iTrunc(htmlspecialchars(strip_tags($this->items[$i]->title)),100).
"</div>";
}
}
if ($this->items[$i]->getDescription()) {
$feedArray[] =
"<div class='".$this->stylePrefix."item_description'>".
str_replace("]]>", "", str_replace("<![CDATA[", "", $this->items[$i]->getDescription())).
"</div>";
}
}
if ($this->footer) {
$feedArray[] = "<div class='".$this->stylePrefix."footer'>".$this->footer."</div>";
}
$feed= "".join($feedArray, "\r\n");
return $feed;
}
/**
* Overrrides parent to produce .html extensions
*
* @return string the feed cache filename
* @since 1.4
* @access private
*/
function _generateFilename() {
$fileInfo = pathinfo($_SERVER["PHP_SELF"]);
return substr($fileInfo["basename"],0,-(strlen($fileInfo["extension"])+1)).".html";
}
}
/**
* JSCreator is a class that writes a js file to a specific
* location, overriding the createFeed method of the parent HTMLCreator.
*
* @author Pascal Van Hecke
*/
class JSCreator extends HTMLCreator {
var $contentType = "text/javascript";
/**
* writes the javascript
* @return string the scripts's complete text
*/
function createFeed()
{
$feed = parent::createFeed();
$feedArray = explode("\n",$feed);
$jsFeed = "";
foreach ($feedArray as $value) {
$jsFeed .= "document.write('".trim(addslashes($value))."');\n";
}
return $jsFeed;
}
/**
* Overrrides parent to produce .js extensions
*
* @return string the feed cache filename
* @since 1.4
* @access private
*/
function _generateFilename() {
$fileInfo = pathinfo($_SERVER["PHP_SELF"]);
return substr($fileInfo["basename"],0,-(strlen($fileInfo["extension"])+1)).".js";
}
}
/*** TEST SCRIPT *********************************************************
//include("feedcreator.class.php");
$rss = new UniversalFeedCreator();
$rss->useCached();
$rss->title = "PHP news";
$rss->description = "daily news from the PHP scripting world";
//optional
//$rss->descriptionTruncSize = 500;
//$rss->descriptionHtmlSyndicated = true;
//$rss->xslStyleSheet = "http://feedster.com/rss20.xsl";
$rss->link = "http://www.dailyphp.net/news";
$rss->feedURL = "http://www.dailyphp.net/".$PHP_SELF;
$image = new FeedImage();
$image->title = "dailyphp.net logo";
$image->url = "http://www.dailyphp.net/images/logo.gif";
$image->link = "http://www.dailyphp.net";
$image->description = "Feed provided by dailyphp.net. Click to visit.";
//optional
$image->descriptionTruncSize = 500;
$image->descriptionHtmlSyndicated = true;
$rss->image = $image;
// get your news items from somewhere, e.g. your database:
//mysql_select_db($dbHost, $dbUser, $dbPass);
//$res = mysql_query("SELECT * FROM news ORDER BY newsdate DESC");
//while ($data = mysql_fetch_object($res)) {
$item = new FeedItem();
$item->title = "This is an the test title of an item";
$item->link = "http://localhost/item/";
$item->description = "<b>description in </b>
HTML";
//optional
//item->descriptionTruncSize = 500;
$item->descriptionHtmlSyndicated = true;
$item->date = time();
$item->source = "http://www.dailyphp.net";
$item->author = "John Doe";
$rss->addItem($item);
//}
// valid format strings are: RSS0.91, RSS1.0, RSS2.0, PIE0.1, MBOX, OPML, ATOM0.3, HTML, JS
echo $rss->saveFeed("RSS0.91", "feed.xml");
***************************************************************************/
?>
/***************************************************************************
FeedCreator class v1.7.2
originally (c) Kai Blankenhorn
www.bitfolge.de
kaib@bitfolge.de
v1.3 work by Scott Reynen (scott@randomchaos.com) and Kai Blankenhorn
v1.5 OPML support by Dirk Clemens
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
****************************************************************************
Changelog:
v1.7.2 10-11-04
license changed to LGPL
v1.7.1
fixed a syntax bug
fixed left over debug code
v1.7 07-18-04
added HTML and JavaScript feeds (configurable via CSS) (thanks to Pascal Van Hecke)
added HTML descriptions for all feed formats (thanks to Pascal Van Hecke)
added a switch to select an external stylesheet (thanks to Pascal Van Hecke)
changed default content-type to application/xml
added character encoding setting
fixed numerous smaller bugs (thanks to S鰎en Fuhrmann of golem.de)
improved changing ATOM versions handling (thanks to August Trometer)
improved the UniversalFeedCreator's useCached method (thanks to S鰎en Fuhrmann of golem.de)
added charset output in HTTP headers (thanks to S鰎en Fuhrmann of golem.de)
added Slashdot namespace to RSS 1.0 (thanks to S鰎en Fuhrmann of golem.de)
v1.6 05-10-04
added stylesheet to RSS 1.0 feeds
fixed generator comment (thanks Kevin L. Papendick and Tanguy Pruvot)
fixed RFC822 date bug (thanks Tanguy Pruvot)
added TimeZone customization for RFC8601 (thanks Tanguy Pruvot)
fixed Content-type could be empty (thanks Tanguy Pruvot)
fixed author/creator in RSS1.0 (thanks Tanguy Pruvot)
v1.6 beta 02-28-04
added Atom 0.3 support (not all features, though)
improved OPML 1.0 support (hopefully - added more elements)
added support for arbitrary additional elements (use with caution)
code beautification :-)
considered beta due to some internal changes
v1.5.1 01-27-04
fixed some RSS 1.0 glitches (thanks to St閜hane Vanpoperynghe)
fixed some inconsistencies between documentation and code (thanks to Timothy Martin)
v1.5 01-06-04
added support for OPML 1.0
added more documentation
v1.4 11-11-03
optional feed saving and caching
improved documentation
minor improvements
v1.3 10-02-03
renamed to FeedCreator, as it not only creates RSS anymore
added support for mbox
tentative support for echo/necho/atom/pie/???
v1.2 07-20-03
intelligent auto-truncating of RSS 0.91 attributes
don't create some attributes when they're not set
documentation improved
fixed a real and a possible bug with date conversions
code cleanup
v1.1 06-29-03
added images to feeds
now includes most RSS 0.91 attributes
added RSS 2.0 feeds
v1.0 06-24-03
initial release
***************************************************************************/
/*** GENERAL USAGE *********************************************************
include("feedcreator.class.php");
$rss = new UniversalFeedCreator();
$rss->useCached(); // use cached version if age<1 hour
$rss->title = "PHP news";
$rss->description = "daily news from the PHP scripting world";
//optional
$rss->descriptionTruncSize = 500;
$rss->descriptionHtmlSyndicated = true;
$rss->link = "http://www.dailyphp.net/news";
$rss->syndicationURL = "http://www.dailyphp.net/".$_SERVER["PHP_SELF"];
$image = new FeedImage();
$image->title = "dailyphp.net logo";
$image->url = "http://www.dailyphp.net/images/logo.gif";
$image->link = "http://www.dailyphp.net";
$image->description = "Feed provided by dailyphp.net. Click to visit.";
//optional
$image->descriptionTruncSize = 500;
$image->descriptionHtmlSyndicated = true;
$rss->image = $image;
// get your news items from somewhere, e.g. your database:
mysql_select_db($dbHost, $dbUser, $dbPass);
$res = mysql_query("SELECT * FROM news ORDER BY newsdate DESC");
while ($data = mysql_fetch_object($res)) {
$item = new FeedItem();
$item->title = $data->title;
$item->link = $data->url;
$item->description = $data->short;
//optional
item->descriptionTruncSize = 500;
item->descriptionHtmlSyndicated = true;
$item->date = $data->newsdate;
$item->source = "http://www.dailyphp.net";
$item->author = "John Doe";
$rss->addItem($item);
}
// valid format strings are: RSS0.91, RSS1.0, RSS2.0, PIE0.1 (deprecated),
// MBOX, OPML, ATOM, ATOM0.3, HTML, JS
echo $rss->saveFeed("RSS1.0", "news/feed.xml");
***************************************************************************
* A little setup *
**************************************************************************/
// your local timezone, set to "" to disable or for GMT
define("TIME_ZONE","+01:00");
/**
* Version string.
**/
define("FEEDCREATOR_VERSION", "FeedCreator 1.7.2");
/**
* A FeedItem is a part of a FeedCreator feed.
*
* @author Kai Blankenhorn <kaib@bitfolge.de>
* @since 1.3
*/
class FeedItem extends HtmlDescribable {
/**
* Mandatory attributes of an item.
*/
var $title, $description, $link;
/**
* Optional attributes of an item.
*/
var $author, $authorEmail, $image, $category, $comments, $guid, $source, $creator;
/**
* Publishing date of an item. May be in one of the following formats:
*
* RFC 822:
* "Mon, 20 Jan 03 18:05:41 +0400"
* "20 Jan 03 18:05:41 +0000"
*
* ISO 8601:
* "2003-01-20T18:05:41+04:00"
*
* Unix:
* 1043082341
*/
var $date;
/**
* Any additional elements to include as an assiciated array. All $key => $value pairs
* will be included unencoded in the feed item in the form
* <$key>$value</$key>
* Again: No encoding will be used! This means you can invalidate or enhance the feed
* if $value contains markup. This may be abused to embed tags not implemented by
* the FeedCreator class used.
*/
var $additionalElements = Array();
// on hold
// var $source;
}
/**
* An FeedImage may be added to a FeedCreator feed.
* @author Kai Blankenhorn <kaib@bitfolge.de>
* @since 1.3
*/
class FeedImage extends HtmlDescribable {
/**
* Mandatory attributes of an image.
*/
var $title, $url, $link;
/**
* Optional attributes of an image.
*/
var $width, $height, $description;
}
/**
* An HtmlDescribable is an item within a feed that can have a description that may
* include HTML markup.
*/
class HtmlDescribable {
/**
* Indicates whether the description field should be rendered in HTML.
*/
var $descriptionHtmlSyndicated;
/**
* Indicates whether and to how many characters a description should be truncated.
*/
var $descriptionTruncSize;
/**
* Returns a formatted description field, depending on descriptionHtmlSyndicated and
* $descriptionTruncSize properties
* @return string the formatted description
*/
function getDescription() {
$descriptionField = new FeedHtmlField($this->description);
$descriptionField->syndicateHtml = $this->descriptionHtmlSyndicated;
$descriptionField->truncSize = $this->descriptionTruncSize;
return $descriptionField->output();
}
}
/**
* An FeedHtmlField describes and generates
* a feed, item or image html field (probably a description). Output is
* generated based on $truncSize, $syndicateHtml properties.
* @author Pascal Van Hecke <feedcreator.class.php@vanhecke.info>
* @version 1.6
*/
class FeedHtmlField {
/**
* Mandatory attributes of a FeedHtmlField.
*/
var $rawFieldContent;
/**
* Optional attributes of a FeedHtmlField.
*
*/
var $truncSize, $syndicateHtml;
/**
* Creates a new instance of FeedHtmlField.
* @param $string: if given, sets the rawFieldContent property
*/
function FeedHtmlField($parFieldContent) {
if ($parFieldContent) {
$this->rawFieldContent = $parFieldContent;
}
}
/**
* Creates the right output, depending on $truncSize, $syndicateHtml properties.
* @return string the formatted field
*/
function output() {
// when field available and syndicated in html we assume
// - valid html in $rawFieldContent and we enclose in CDATA tags
// - no truncation (truncating risks producing invalid html)
if (!$this->rawFieldContent) {
$result = "";
} elseif ($this->syndicateHtml) {
$result = "<![CDATA[".$this->rawFieldContent."]]>";
} else {
if ($this->truncSize and is_int($this->truncSize)) {
$result = FeedCreator::iTrunc(htmlspecialchars($this->rawFieldContent),$this->truncSize);
} else {
$result = htmlspecialchars($this->rawFieldContent);
}
}
return $result;
}
}
/**
* UniversalFeedCreator lets you choose during runtime which
* format to build.
* For general usage of a feed class, see the FeedCreator class
* below or the example above.
*
* @since 1.3
* @author Kai Blankenhorn <kaib@bitfolge.de>
*/
class UniversalFeedCreator extends FeedCreator {
var $_feed;
function _setFormat($format) {
switch (strtoupper($format)) {
case "2.0":
// fall through
case "RSS2.0":
$this->_feed = new RSSCreator20();
break;
case "1.0":
// fall through
case "RSS1.0":
$this->_feed = new RSSCreator10();
break;
case "0.91":
// fall through
case "RSS0.91":
$this->_feed = new RSSCreator091();
break;
case "PIE0.1":
$this->_feed = new PIECreator01();
break;
case "MBOX":
$this->_feed = new MBOXCreator();
break;
case "OPML":
$this->_feed = new OPMLCreator();
break;
case "ATOM":
// fall through: always the latest ATOM version
case "ATOM0.3":
$this->_feed = new AtomCreator03();
break;
case "HTML":
$this->_feed = new HTMLCreator();
break;
case "JS":
// fall through
case "JAVASCRIPT":
$this->_feed = new JSCreator();
break;
default:
$this->_feed = new RSSCreator091();
break;
}
$vars = get_object_vars($this);
foreach ($vars as $key => $value) {
// prevent overwriting of properties "contentType", "encoding"; do not copy "_feed" itself
if (!in_array($key, array("_feed", "contentType", "encoding"))) {
$this->_feed->{$key} = $this->{$key};
}
}
}
/**
* Creates a syndication feed based on the items previously added.
*
* @see FeedCreator::addItem()
* @param string format format the feed should comply to. Valid values are:
* "PIE0.1", "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM0.3", "HTML", "JS"
* @return string the contents of the feed.
*/
function createFeed($format = "RSS0.91") {
$this->_setFormat($format);
return $this->_feed->createFeed();
}
/**
* Saves this feed as a file on the local disk. After the file is saved, an HTTP redirect
* header may be sent to redirect the use to the newly created file.
* @since 1.4
*
* @param string format format the feed should comply to. Valid values are:
* "PIE0.1" (deprecated), "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM", "ATOM0.3", "HTML", "JS"
* @param string filename optional the filename where a recent version of the feed is saved. If not specified, the filename is $_SERVER["PHP_SELF"] with the extension changed to .xml (see _generateFilename()).
* @param boolean displayContents optional send the content of the file or not. If true, the file will be sent in the body of the response.
*/
function saveFeed($format="RSS0.91", $filename="", $displayContents=true) {
$this->_setFormat($format);
$this->_feed->saveFeed($filename, $displayContents);
}
/**
* Turns on caching and checks if there is a recent version of this feed in the cache.
* If there is, an HTTP redirect header is sent.
* To effectively use caching, you should create the FeedCreator object and call this method
* before anything else, especially before you do the time consuming task to build the feed
* (web fetching, for example).
*
* @param string format format the feed should comply to. Valid values are:
* "PIE0.1" (deprecated), "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM0.3".
* @param filename string optional the filename where a recent version of the feed is saved. If not specified, the filename is $_SERVER["PHP_SELF"] with the extension changed to .xml (see _generateFilename()).
* @param timeout int optional the timeout in seconds before a cached version is refreshed (defaults to 3600 = 1 hour)
*/
function useCached($format="RSS0.91", $filename="", $timeout=3600) {
$this->_setFormat($format);
$this->_feed->useCached($filename, $timeout);
}
}
/**
* FeedCreator is the abstract base implementation for concrete
* implementations that implement a specific format of syndication.
*
* @abstract
* @author Kai Blankenhorn <kaib@bitfolge.de>
* @since 1.4
*/
class FeedCreator extends HtmlDescribable {
/**
* Mandatory attributes of a feed.
*/
var $title, $description, $link;
/**
* Optional attributes of a feed.
*/
var $syndicationURL, $image, $language, $copyright, $pubDate, $lastBuildDate, $editor, $editorEmail, $webmaster, $category, $docs, $ttl, $rating, $skipHours, $skipDays;
/**
* The url of the external xsl stylesheet used to format the naked rss feed.
* Ignored in the output when empty.
*/
var $xslStyleSheet = "";
/**
* @access private
*/
var $items = Array();
/**
* This feed's MIME content type.
* @since 1.4
* @access private
*/
var $contentType = "application/xml";
/**
* This feed's character encoding.
* @since 1.6.1
**/
var $encoding = "ISO-8859-1";
/**
* Any additional elements to include as an assiciated array. All $key => $value pairs
* will be included unencoded in the feed in the form
* <$key>$value</$key>
* Again: No encoding will be used! This means you can invalidate or enhance the feed
* if $value contains markup. This may be abused to embed tags not implemented by
* the FeedCreator class used.
*/
var $additionalElements = Array();
/**
* Adds an FeedItem to the feed.
*
* @param object FeedItem $item The FeedItem to add to the feed.
* @access public
*/
function addItem($item) {
$this->items[] = $item;
}
/**
* Truncates a string to a certain length at the most sensible point.
* First, if there's a '.' character near the end of the string, the string is truncated after this character.
* If there is no '.', the string is truncated after the last ' ' character.
* If the string is truncated, " ..." is appended.
* If the string is already shorter than $length, it is returned unchanged.
*
* @static
* @param string string A string to be truncated.
* @param int length the maximum length the string should be truncated to
* @return string the truncated string
*/
function iTrunc($string, $length) {
if (strlen($string)<=$length) {
return $string;
}
$pos = strrpos($string,".");
if ($pos>=$length-4) {
$string = substr($string,0,$length-4);
$pos = strrpos($string,".");
}
if ($pos>=$length*0.4) {
return substr($string,0,$pos+1)." ...";
}
$pos = strrpos($string," ");
if ($pos>=$length-4) {
$string = substr($string,0,$length-4);
$pos = strrpos($string," ");
}
if ($pos>=$length*0.4) {
return substr($string,0,$pos)." ...";
}
return substr($string,0,$length-4)." ...";
}
/**
* Creates a comment indicating the generator of this feed.
* The format of this comment seems to be recognized by
* Syndic8.com.
*/
function _createGeneratorComment() {
return "<!-- generator=&#92;"".FEEDCREATOR_VERSION."&#92;" -->\n";
}
/**
* Creates a string containing all additional elements specified in
* $additionalElements.
* @param elements array an associative array containing key => value pairs
* @param indentString string a string that will be inserted before every generated line
* @return string the XML tags corresponding to $additionalElements
*/
function _createAdditionalElements($elements, $indentString="") {
$ae = "";
if (is_array($elements)) {
foreach($elements AS $key => $value) {
$ae.= $indentString."<$key>$value</$key>\n";
}
}
return $ae;
}
function _createStylesheetReferences() {
$xml = "";
if ($this->cssStyleSheet) $xml .= "<?xml-stylesheet href=\"".$this->cssStyleSheet."\" type=\"text/css\"?>\n";
if ($this->xslStyleSheet) $xml .= "<?xml-stylesheet href=\"".$this->xslStyleSheet."\" type=\"text/xsl\"?>\n";
return $xml;
}
/**
* Builds the feed's text.
* @abstract
* @return string the feed's complete text
*/
function createFeed() {
}
/**
* Generate a filename for the feed cache file. The result will be $_SERVER["PHP_SELF"] with the extension changed to .xml.
* For example:
*
* echo $_SERVER["PHP_SELF"]."\n";
* echo FeedCreator::_generateFilename();
*
* would produce:
*
* /rss/latestnews.php
* latestnews.xml
*
* @return string the feed cache filename
* @since 1.4
* @access private
*/
function _generateFilename() {
$fileInfo = pathinfo($_SERVER["PHP_SELF"]);
return substr($fileInfo["basename"],0,-(strlen($fileInfo["extension"])+1)).".xml";
}
/**
* @since 1.4
* @access private
*/
function _redirect($filename) {
// attention, heavily-commented-out-area
// maybe use this in addition to file time checking
//Header("Expires: ".date("r",time()+$this->_timeout));
/* no caching at all, doesn't seem to work as good:
Header("Cache-Control: no-cache");
Header("Pragma: no-cache");
*/
// HTTP redirect, some feed readers' simple HTTP implementations don't follow it
//Header("Location: ".$filename);
Header("Content-Type: ".$this->contentType."; charset=".$this->encoding."; filename=".basename($filename));
Header("Content-Disposition: inline; filename=".basename($filename));
readfile($filename, "r");
die();
}
/**
* Turns on caching and checks if there is a recent version of this feed in the cache.
* If there is, an HTTP redirect header is sent.
* To effectively use caching, you should create the FeedCreator object and call this method
* before anything else, especially before you do the time consuming task to build the feed
* (web fetching, for example).
* @since 1.4
* @param filename string optional the filename where a recent version of the feed is saved. If not specified, the filename is $_SERVER["PHP_SELF"] with the extension changed to .xml (see _generateFilename()).
* @param timeout int optional the timeout in seconds before a cached version is refreshed (defaults to 3600 = 1 hour)
*/
function useCached($filename="", $timeout=3600) {
$this->_timeout = $timeout;
if ($filename=="") {
$filename = $this->_generateFilename();
}
if (file_exists($filename) AND (time()-filemtime($filename) < $timeout)) {
$this->_redirect($filename);
}
}
/**
* Saves this feed as a file on the local disk. After the file is saved, a redirect
* header may be sent to redirect the user to the newly created file.
* @since 1.4
*
* @param filename string optional the filename where a recent version of the feed is saved. If not specified, the filename is $_SERVER["PHP_SELF"] with the extension changed to .xml (see _generateFilename()).
* @param redirect boolean optional send an HTTP redirect header or not. If true, the user will be automatically redirected to the created file.
*/
function saveFeed($filename="", $displayContents=true) {
if ($filename=="") {
$filename = $this->_generateFilename();
}
$feedFile = fopen($filename, "w+");
if ($feedFile) {
fputs($feedFile,$this->createFeed());
fclose($feedFile);
if ($displayContents) {
$this->_redirect($filename);
}
} else {
echo "<br /><b>Error creating feed file, please check write permissions.</b><br />";
}
}
}
/**
* FeedDate is an internal class that stores a date for a feed or feed item.
* Usually, you won't need to use this.
*/
class FeedDate {
var $unix;
/**
* Creates a new instance of FeedDate representing a given date.
* Accepts RFC 822, ISO 8601 date formats as well as unix time stamps.
* @param mixed $dateString optional the date this FeedDate will represent. If not specified, the current date and time is used.
*/
function FeedDate($dateString="") {
if ($dateString=="") $dateString = date("r");
if (is_integer($dateString)) {
$this->unix = $dateString;
return;
}
if (preg_match("~(?:(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun),\\s+)?(\\d{1,2})\\s+([a-zA-Z]{3})\\s+(\\d{4})\\s+(\\d{2}):(\\d{2}):(\\d{2})\\s+(.*)~",$dateString,$matches)) {
$months = Array("Jan"=>1,"Feb"=>2,"Mar"=>3,"Apr"=>4,"May"=>5,"Jun"=>6,"Jul"=>7,"Aug"=>8,"Sep"=>9,"Oct"=>10,"Nov"=>11,"Dec"=>12);
$this->unix = mktime($matches[4],$matches[5],$matches[6],$months[$matches[2]],$matches[1],$matches[3]);
if (substr($matches[7],0,1)=='+' OR substr($matches[7],0,1)=='-') {
$tzOffset = (substr($matches[7],0,3) * 60 + substr($matches[7],-2)) * 60;
} else {
if (strlen($matches[7])==1) {
$oneHour = 3600;
$ord = ord($matches[7]);
if ($ord < ord("M")) {
$tzOffset = (ord("A") - $ord - 1) * $oneHour;
} elseif ($ord >= ord("M") AND $matches[7]!="Z") {
$tzOffset = ($ord - ord("M")) * $oneHour;
} elseif ($matches[7]=="Z") {
$tzOffset = 0;
}
}
switch ($matches[7]) {
case "UT":
case "GMT": $tzOffset = 0;
}
}
$this->unix += $tzOffset;
return;
}
if (preg_match("~(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2})(.*)~",$dateString,$matches)) {
$this->unix = mktime($matches[4],$matches[5],$matches[6],$matches[2],$matches[3],$matches[1]);
if (substr($matches[7],0,1)=='+' OR substr($matches[7],0,1)=='-') {
$tzOffset = (substr($matches[7],0,3) * 60 + substr($matches[7],-2)) * 60;
} else {
if ($matches[7]=="Z") {
$tzOffset = 0;
}
}
$this->unix += $tzOffset;
return;
}
$this->unix = 0;
}
/**
* Gets the date stored in this FeedDate as an RFC 822 date.
*
* @return a date in RFC 822 format
*/
function rfc822() {
//return gmdate("r",$this->unix);
$date = gmdate("D, d M Y H:i:s", $this->unix);
if (TIME_ZONE!="") $date .= " ".str_replace(":","",TIME_ZONE);
return $date;
}
/**
* Gets the date stored in this FeedDate as an ISO 8601 date.
*
* @return a date in ISO 8601 format
*/
function iso8601() {
$date = gmdate("Y-m-d\TH:i:sO",$this->unix);
$date = substr($date,0,22) . ':' . substr($date,-2);
if (TIME_ZONE!="") $date = str_replace("+00:00",TIME_ZONE,$date);
return $date;
}
/**
* Gets the date stored in this FeedDate as unix time stamp.
*
* @return a date as a unix time stamp
*/
function unix() {
return $this->unix;
}
}
/**
* RSSCreator10 is a FeedCreator that implements RDF Site Summary (RSS) 1.0.
*
* @see http://www.purl.org/rss/1.0/
* @since 1.3
* @author Kai Blankenhorn <kaib@bitfolge.de>
*/
class RSSCreator10 extends FeedCreator {
/**
* Builds the RSS feed's text. The feed will be compliant to RDF Site Summary (RSS) 1.0.
* The feed will contain all items previously added in the same order.
* @return string the feed's complete text
*/
function createFeed() {
$feed = "<?xml version=\"1.0\" encoding=\"".$this->encoding."\"?>\n";
$feed.= $this->_createGeneratorComment();
if ($this->cssStyleSheet=="") {
$cssStyleSheet = "http://www.w3.org/2000/08/w3c-synd/style.css";
}
$feed.= $this->_createStylesheetReferences();
$feed.= "<rdf:RDF\n";
$feed.= " xmlns=\"http://purl.org/rss/1.0/\"\n";
$feed.= " xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n";
$feed.= " xmlns:slash=\"http://purl.org/rss/1.0/modules/slash/\"\n";
$feed.= " xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n";
$feed.= " <channel rdf:about=\"".$this->syndicationURL."\">\n";
$feed.= " <title>".htmlspecialchars($this->title)."</title>\n";
$feed.= " <description>".htmlspecialchars($this->description)."</description>\n";
$feed.= " <link>".$this->link."</link>\n";
if ($this->image!=null) {
$feed.= " <image rdf:resource=\"".$this->image->url."\" />\n";
}
$now = new FeedDate();
$feed.= " <dc:date>".htmlspecialchars($now->iso8601())."</dc:date>\n";
$feed.= " <items>\n";
$feed.= " <rdf:Seq>\n";
for ($i=0;$i<count($this->items);$i++) {
$feed.= " <rdf:li rdf:resource=\"".htmlspecialchars($this->items[$i]->link)."\"/>\n";
}
$feed.= " </rdf:Seq>\n";
$feed.= " </items>\n";
$feed.= " </channel>\n";
if ($this->image!=null) {
$feed.= " <image rdf:about=\"".$this->image->url."\">\n";
$feed.= " <title>".$this->image->title."</title>\n";
$feed.= " <link>".$this->image->link."</link>\n";
$feed.= " <url>".$this->image->url."</url>\n";
$feed.= " </image>\n";
}
$feed.= $this->_createAdditionalElements($this->additionalElements, " ");
for ($i=0;$i<count($this->items);$i++) {
$feed.= " <item rdf:about=\"".htmlspecialchars($this->items[$i]->link)."\">\n";
//$feed.= " <dc:type>Posting</dc:type>\n";
$feed.= " <dc:format>text/html</dc:format>\n";
if ($this->items[$i]->date!=null) {
$itemDate = new FeedDate($this->items[$i]->date);
$feed.= " <dc:date>".htmlspecialchars($itemDate->iso8601())."</dc:date>\n";
}
if ($this->items[$i]->source!="") {
$feed.= " <dc:source>".htmlspecialchars($this->items[$i]->source)."</dc:source>\n";
}
if ($this->items[$i]->author!="") {
$feed.= " <dc:creator>".htmlspecialchars($this->items[$i]->author)."</dc:creator>\n";
}
$feed.= " <title>".htmlspecialchars(strip_tags(strtr($this->items[$i]->title,"\n\r"," ")))."</title>\n";
$feed.= " <link>".htmlspecialchars($this->items[$i]->link)."</link>\n";
$feed.= " <description>".htmlspecialchars($this->items[$i]->description)."</description>\n";
$feed.= $this->_createAdditionalElements($this->items[$i]->additionalElements, " ");
$feed.= " </item>\n";
}
$feed.= "</rdf:RDF>\n";
return $feed;
}
}
/**
* RSSCreator091 is a FeedCreator that implements RSS 0.91 Spec, revision 3.
*
* @see http://my.netscape.com/publish/formats/rss-spec-0.91.html
* @since 1.3
* @author Kai Blankenhorn <kaib@bitfolge.de>
*/
class RSSCreator091 extends FeedCreator {
/**
* Stores this RSS feed's version number.
* @access private
*/
var $RSSVersion;
function RSSCreator091() {
$this->_setRSSVersion("0.91");
$this->contentType = "application/rss+xml";
}
/**
* Sets this RSS feed's version number.
* @access private
*/
function _setRSSVersion($version) {
$this->RSSVersion = $version;
}
/**
* Builds the RSS feed's text. The feed will be compliant to RDF Site Summary (RSS) 1.0.
* The feed will contain all items previously added in the same order.
* @return string the feed's complete text
*/
function createFeed() {
$feed = "<?xml version=\"1.0\" encoding=\"".$this->encoding."\"?>\n";
$feed.= $this->_createGeneratorComment();
$feed.= $this->_createStylesheetReferences();
$feed.= "<rss version=\"".$this->RSSVersion."\">\n";
$feed.= " <channel>\n";
$feed.= " <title>".FeedCreator::iTrunc(htmlspecialchars($this->title),100)."</title>\n";
$this->descriptionTruncSize = 500;
$feed.= " <description>".$this->getDescription()."</description>\n";
$feed.= " <link>".$this->link."</link>\n";
$now = new FeedDate();
$feed.= " <lastBuildDate>".htmlspecialchars($now->rfc822())."</lastBuildDate>\n";
$feed.= " <generator>".FEEDCREATOR_VERSION."</generator>\n";
if ($this->image!=null) {
$feed.= " <image>\n";
$feed.= " <url>".$this->image->url."</url>\n";
$feed.= " <title>".FeedCreator::iTrunc(htmlspecialchars($this->image->title),100)."</title>\n";
$feed.= " <link>".$this->image->link."</link>\n";
if ($this->image->width!="") {
$feed.= " <width>".$this->image->width."</width>\n";
}
if ($this->image->height!="") {
$feed.= " <height>".$this->image->height."</height>\n";
}
if ($this->image->description!="") {
$feed.= " <description>".$this->image->getDescription()."</description>\n";
}
$feed.= " </image>\n";
}
if ($this->language!="") {
$feed.= " <language>".$this->language."</language>\n";
}
if ($this->copyright!="") {
$feed.= " <copyright>".FeedCreator::iTrunc(htmlspecialchars($this->copyright),100)."</copyright>\n";
}
if ($this->editor!="") {
$feed.= " <managingEditor>".FeedCreator::iTrunc(htmlspecialchars($this->editor),100)."</managingEditor>\n";
}
if ($this->webmaster!="") {
$feed.= " <webMaster>".FeedCreator::iTrunc(htmlspecialchars($this->webmaster),100)."</webMaster>\n";
}
if ($this->pubDate!="") {
$pubDate = new FeedDate($this->pubDate);
$feed.= " <pubDate>".htmlspecialchars($pubDate->rfc822())."</pubDate>\n";
}
if ($this->category!="") {
$feed.= " <category>".htmlspecialchars($this->category)."</category>\n";
}
if ($this->docs!="") {
$feed.= " <docs>".FeedCreator::iTrunc(htmlspecialchars($this->docs),500)."</docs>\n";
}
if ($this->ttl!="") {
$feed.= " <ttl>".htmlspecialchars($this->ttl)."</ttl>\n";
}
if ($this->rating!="") {
$feed.= " <rating>".FeedCreator::iTrunc(htmlspecialchars($this->rating),500)."</rating>\n";
}
if ($this->skipHours!="") {
$feed.= " <skipHours>".htmlspecialchars($this->skipHours)."</skipHours>\n";
}
if ($this->skipDays!="") {
$feed.= " <skipDays>".htmlspecialchars($this->skipDays)."</skipDays>\n";
}
$feed.= $this->_createAdditionalElements($this->additionalElements, " ");
for ($i=0;$i<count($this->items);$i++) {
$feed.= " <item>\n";
$feed.= " <title>".FeedCreator::iTrunc(htmlspecialchars(strip_tags($this->items[$i]->title)),100)."</title>\n";
$feed.= " <link>".htmlspecialchars($this->items[$i]->link)."</link>\n";
$feed.= " <description>".$this->items[$i]->getDescription()."</description>\n";
if ($this->items[$i]->author!="") {
$feed.= " <author>".htmlspecialchars($this->items[$i]->author)."</author>\n";
}
/*
// on hold
if ($this->items[$i]->source!="") {
$feed.= " <source>".htmlspecialchars($this->items[$i]->source)."</source>\n";
}
*/
if ($this->items[$i]->category!="") {
$feed.= " <category>".htmlspecialchars($this->items[$i]->category)."</category>\n";
}
if ($this->items[$i]->comments!="") {
$feed.= " <comments>".htmlspecialchars($this->items[$i]->comments)."</comments>\n";
}
if ($this->items[$i]->date!="") {
$itemDate = new FeedDate($this->items[$i]->date);
$feed.= " <pubDate>".htmlspecialchars($itemDate->rfc822())."</pubDate>\n";
}
if ($this->items[$i]->guid!="") {
$feed.= " <guid>".htmlspecialchars($this->items[$i]->guid)."</guid>\n";
}
$feed.= $this->_createAdditionalElements($this->items[$i]->additionalElements, " ");
$feed.= " </item>\n";
}
$feed.= " </channel>\n";
$feed.= "</rss>\n";
return $feed;
}
}
/**
* RSSCreator20 is a FeedCreator that implements RDF Site Summary (RSS) 2.0.
*
* @see http://backend.userland.com/rss
* @since 1.3
* @author Kai Blankenhorn <kaib@bitfolge.de>
*/
class RSSCreator20 extends RSSCreator091 {
function RSSCreator20() {
parent::_setRSSVersion("2.0");
}
}
/**
* PIECreator01 is a FeedCreator that implements the emerging PIE specification,
* as in http://intertwingly.net/wiki/pie/Syntax.
*
* @deprecated
* @since 1.3
* @author Scott Reynen <scott@randomchaos.com> and Kai Blankenhorn <kaib@bitfolge.de>
*/
class PIECreator01 extends FeedCreator {
function PIECreator01() {
$this->encoding = "utf-8";
}
function createFeed() {
$feed = "<?xml version=\"1.0\" encoding=\"".$this->encoding."\"?>\n";
$feed.= $this->_createStylesheetReferences();
$feed.= "<feed version=\"0.1\" xmlns=\"http://example.com/newformat#\">\n";
$feed.= " <title>".FeedCreator::iTrunc(htmlspecialchars($this->title),100)."</title>\n";
$this->truncSize = 500;
$feed.= " <subtitle>".$this->getDescription()."</subtitle>\n";
$feed.= " <link>".$this->link."</link>\n";
for ($i=0;$i<count($this->items);$i++) {
$feed.= " <entry>\n";
$feed.= " <title>".FeedCreator::iTrunc(htmlspecialchars(strip_tags($this->items[$i]->title)),100)."</title>\n";
$feed.= " <link>".htmlspecialchars($this->items[$i]->link)."</link>\n";
$itemDate = new FeedDate($this->items[$i]->date);
$feed.= " <created>".htmlspecialchars($itemDate->iso8601())."</created>\n";
$feed.= " <issued>".htmlspecialchars($itemDate->iso8601())."</issued>\n";
$feed.= " <modified>".htmlspecialchars($itemDate->iso8601())."</modified>\n";
$feed.= " <id>".htmlspecialchars($this->items[$i]->guid)."</id>\n";
if ($this->items[$i]->author!="") {
$feed.= " <author>\n";
$feed.= " <name>".htmlspecialchars($this->items[$i]->author)."</name>\n";
if ($this->items[$i]->authorEmail!="") {
$feed.= " <email>".$this->items[$i]->authorEmail."</email>\n";
}
$feed.=" </author>\n";
}
$feed.= " <content type=\"text/html\" xml:lang=\"en-us\">\n";
$feed.= " <div xmlns=\"http://www.w3.org/1999/xhtml\">".$this->items[$i]->getDescription()."</div>\n";
$feed.= " </content>\n";
$feed.= " </entry>\n";
}
$feed.= "</feed>\n";
return $feed;
}
}
/**
* AtomCreator03 is a FeedCreator that implements the atom specification,
* as in http://www.intertwingly.net/wiki/pie/FrontPage.
* Please note that just by using AtomCreator03 you won't automatically
* produce valid atom files. For example, you have to specify either an editor
* for the feed or an author for every single feed item.
*
* Some elements have not been implemented yet. These are (incomplete list):
* author URL, item author's email and URL, item contents, alternate links,
* other link content types than text/html. Some of them may be created with
* AtomCreator03::additionalElements.
*
* @see FeedCreator#additionalElements
* @since 1.6
* @author Kai Blankenhorn <kaib@bitfolge.de>, Scott Reynen <scott@randomchaos.com>
*/
class AtomCreator03 extends FeedCreator {
function AtomCreator03() {
$this->contentType = "application/atom+xml";
$this->encoding = "utf-8";
}
function createFeed() {
$feed = "<?xml version=\"1.0\" encoding=\"".$this->encoding."\"?>\n";
$feed.= $this->_createGeneratorComment();
$feed.= $this->_createStylesheetReferences();
$feed.= "<feed version=\"0.3\" xmlns=\"http://purl.org/atom/ns#\"";
if ($this->language!="") {
$feed.= " xml:lang=\"".$this->language."\"";
}
$feed.= ">\n";
$feed.= " <title>".htmlspecialchars($this->title)."</title>\n";
$feed.= " <tagline>".htmlspecialchars($this->description)."</tagline>\n";
$feed.= " <link rel=\"alternate\" type=\"text/html\" href=\"".htmlspecialchars($this->link)."\"/>\n";
$feed.= " <id>".htmlspecialchars($this->link)."</id>\n";
$now = new FeedDate();
$feed.= " <modified>".htmlspecialchars($now->iso8601())."</modified>\n";
if ($this->editor!="") {
$feed.= " <author>\n";
$feed.= " <name>".$this->editor."</name>\n";
if ($this->editorEmail!="") {
$feed.= " <email>".$this->editorEmail."</email>\n";
}
$feed.= " </author>\n";
}
$feed.= " <generator>".FEEDCREATOR_VERSION."</generator>\n";
$feed.= $this->_createAdditionalElements($this->additionalElements, " ");
for ($i=0;$i<count($this->items);$i++) {
$feed.= " <entry>\n";
$feed.= " <title>".htmlspecialchars(strip_tags($this->items[$i]->title))."</title>\n";
$feed.= " <link rel=\"alternate\" type=\"text/html\" href=\"".htmlspecialchars($this->items[$i]->link)."\"/>\n";
if ($this->items[$i]->date=="") {
$this->items[$i]->date = time();
}
$itemDate = new FeedDate($this->items[$i]->date);
$feed.= " <created>".htmlspecialchars($itemDate->iso8601())."</created>\n";
$feed.= " <issued>".htmlspecialchars($itemDate->iso8601())."</issued>\n";
$feed.= " <modified>".htmlspecialchars($itemDate->iso8601())."</modified>\n";
$feed.= " <id>".htmlspecialchars($this->items[$i]->link)."</id>\n";
$feed.= $this->_createAdditionalElements($this->items[$i]->additionalElements, " ");
if ($this->items[$i]->author!="") {
$feed.= " <author>\n";
$feed.= " <name>".htmlspecialchars($this->items[$i]->author)."</name>\n";
$feed.= " </author>\n";
}
if ($this->items[$i]->description!="") {
$feed.= " <summary>".htmlspecialchars($this->items[$i]->description)."</summary>\n";
}
$feed.= " </entry>\n";
}
$feed.= "</feed>\n";
return $feed;
}
}
/**
* MBOXCreator is a FeedCreator that implements the mbox format
* as described in http://www.qmail.org/man/man5/mbox.html
*
* @since 1.3
* @author Kai Blankenhorn <kaib@bitfolge.de>
*/
class MBOXCreator extends FeedCreator {
function MBOXCreator() {
$this->contentType = "text/plain";
$this->encoding = "ISO-8859-15";
}
function qp_enc($input = "", $line_max = 76) {
$hex = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
$lines = preg_split("/(?:\r\n|\r|\n)/", $input);
$eol = "\r\n";
$escape = "=";
$output = "";
while( list(, $line) = each($lines) ) {
//$line = rtrim($line); // remove trailing white space -> no =20\r\n necessary
$linlen = strlen($line);
$newline = "";
for($i = 0; $i < $linlen; $i++) {
$c = substr($line, $i, 1);
$dec = ord($c);
if ( ($dec == 32) && ($i == ($linlen - 1)) ) { // convert space at eol only
$c = "=20";
} elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { // always encode "\t", which is *not* required
$h2 = floor($dec/16); $h1 = floor($dec%16);
$c = $escape.$hex["$h2"].$hex["$h1"];
}
if ( (strlen($newline) + strlen($c)) >= $line_max ) { // CRLF is not counted
$output .= $newline.$escape.$eol; // soft line break; " =\r\n" is okay
$newline = "";
}
$newline .= $c;
} // end of for
$output .= $newline.$eol;
}
return trim($output);
}
/**
* Builds the MBOX contents.
* @return string the feed's complete text
*/
function createFeed() {
for ($i=0;$i<count($this->items);$i++) {
if ($this->items[$i]->author!="") {
$from = $this->items[$i]->author;
} else {
$from = $this->title;
}
$itemDate = new FeedDate($this->items[$i]->date);
$feed.= "From ".strtr(MBOXCreator::qp_enc($from)," ","_")." ".date("D M d H:i:s Y",$itemDate->unix())."\n";
$feed.= "Content-Type: text/plain;\n";
$feed.= " charset=\"".$this->encoding."\"\n";
$feed.= "Content-Transfer-Encoding: quoted-printable\n";
$feed.= "Content-Type: text/plain\n";
$feed.= "From: \"".MBOXCreator::qp_enc($from)."\"\n";
$feed.= "Date: ".$itemDate->rfc822()."\n";
$feed.= "Subject: ".MBOXCreator::qp_enc(FeedCreator::iTrunc($this->items[$i]->title,100))."\n";
$feed.= "\n";
$body = chunk_split(MBOXCreator::qp_enc($this->items[$i]->description));
$feed.= preg_replace("~\nFrom ([^\n]*)(\n?)~","\n>From $1$2\n",$body);
$feed.= "\n";
$feed.= "\n";
}
return $feed;
}
/**
* Generate a filename for the feed cache file. Overridden from FeedCreator to prevent XML data types.
* @return string the feed cache filename
* @since 1.4
* @access private
*/
function _generateFilename() {
$fileInfo = pathinfo($_SERVER["PHP_SELF"]);
return substr($fileInfo["basename"],0,-(strlen($fileInfo["extension"])+1)).".mbox";
}
}
/**
* OPMLCreator is a FeedCreator that implements OPML 1.0.
*
* @see http://opml.scripting.com/spec
* @author Dirk Clemens, Kai Blankenhorn
* @since 1.5
*/
class OPMLCreator extends FeedCreator {
function OPMLCreator() {
$this->encoding = "utf-8";
}
function createFeed() {
$feed = "<?xml version=\"1.0\" encoding=\"".$this->encoding."\"?>\n";
$feed.= $this->_createGeneratorComment();
$feed.= $this->_createStylesheetReferences();
$feed.= "<opml xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n";
$feed.= " <head>\n";
$feed.= " <title>".htmlspecialchars($this->title)."</title>\n";
if ($this->pubDate!="") {
$date = new FeedDate($this->pubDate);
$feed.= " <dateCreated>".$date->rfc822()."</dateCreated>\n";
}
if ($this->lastBuildDate!="") {
$date = new FeedDate($this->lastBuildDate);
$feed.= " <dateModified>".$date->rfc822()."</dateModified>\n";
}
if ($this->editor!="") {
$feed.= " <ownerName>".$this->editor."</ownerName>\n";
}
if ($this->editorEmail!="") {
$feed.= " <ownerEmail>".$this->editorEmail."</ownerEmail>\n";
}
$feed.= " </head>\n";
$feed.= " <body>\n";
for ($i=0;$i<count($this->items);$i++) {
$feed.= " <outline type=\"rss\" ";
$title = htmlspecialchars(strip_tags(strtr($this->items[$i]->title,"\n\r"," ")));
$feed.= " title=\"".$title."\"";
$feed.= " text=\"".$title."\"";
//$feed.= " description=\"".htmlspecialchars($this->items[$i]->description)."\"";
$feed.= " url=\"".htmlspecialchars($this->items[$i]->link)."\"";
$feed.= "/>\n";
}
$feed.= " </body>\n";
$feed.= "</opml>\n";
return $feed;
}
}
/**
* HTMLCreator is a FeedCreator that writes an HTML feed file to a specific
* location, overriding the createFeed method of the parent FeedCreator.
* The HTML produced can be included over http by scripting languages, or serve
* as the source for an IFrame.
* All output by this class is embedded in <div></div> tags to enable formatting
* using CSS.
*
* @author Pascal Van Hecke
* @since 1.7
*/
class HTMLCreator extends FeedCreator {
var $contentType = "text/html";
/**
* Contains HTML to be output at the start of the feed's html representation.
*/
var $header;
/**
* Contains HTML to be output at the end of the feed's html representation.
*/
var $footer ;
/**
* Contains HTML to be output between entries. A separator is only used in
* case of multiple entries.
*/
var $separator;
/**
* Used to prefix the stylenames to make sure they are unique
* and do not clash with stylenames on the users' page.
*/
var $stylePrefix;
/**
* Determines whether the links open in a new window or not.
*/
var $openInNewWindow = true;
var $imageAlign ="right";
/**
* In case of very simple output you may want to get rid of the style tags,
* hence this variable. There's no equivalent on item level, but of course you can
* add strings to it while iterating over the items ($this->stylelessOutput .= ...)
* and when it is non-empty, ONLY the styleless output is printed, the rest is ignored
* in the function createFeed().
*/
var $stylelessOutput ="";
/**
* Writes the HTML.
* @return string the scripts's complete text
*/
function createFeed() {
// if there is styleless output, use the content of this variable and ignore the rest
if ($this->stylelessOutput!="") {
return $this->stylelessOutput;
}
//if no stylePrefix is set, generate it yourself depending on the script name
if ($this->stylePrefix=="") {
$this->stylePrefix = str_replace(".", "_", $this->_generateFilename())."_";
}
//set an openInNewWindow_token_to be inserted or not
if ($this->openInNewWindow) {
$targetInsert = " target='_blank'";
}
// use this array to put the lines in and implode later with "document.write" javascript
$feedArray = array();
if ($this->image!=null) {
$imageStr = "<a href='".$this->image->link."'".$targetInsert.">".
"<img src='".$this->image->url."' border='0' alt='".
FeedCreator::iTrunc(htmlspecialchars($this->image->title),100).
"' align='".$this->imageAlign."' ";
if ($this->image->width) {
$imageStr .=" width='".$this->image->width. "' ";
}
if ($this->image->height) {
$imageStr .=" height='".$this->image->height."' ";
}
$imageStr .="/></a>";
$feedArray[] = $imageStr;
}
if ($this->title) {
$feedArray[] = "<div class='".$this->stylePrefix."title'><a href='".$this->link."' ".$targetInsert." class='".$this->stylePrefix."title'>".
FeedCreator::iTrunc(htmlspecialchars($this->title),100)."</a></div>";
}
if ($this->getDescription()) {
$feedArray[] = "<div class='".$this->stylePrefix."description'>".
str_replace("]]>", "", str_replace("<![CDATA[", "", $this->getDescription())).
"</div>";
}
if ($this->header) {
$feedArray[] = "<div class='".$this->stylePrefix."header'>".$this->header."</div>";
}
for ($i=0;$i<count($this->items);$i++) {
if ($this->separator and $i > 0) {
$feedArray[] = "<div class='".$this->stylePrefix."separator'>".$this->separator."</div>";
}
if ($this->items[$i]->title) {
if ($this->items[$i]->link) {
$feedArray[] =
"<div class='".$this->stylePrefix."item_title'><a href='".$this->items[$i]->link."' class='".$this->stylePrefix.
"item_title'".$targetInsert.">".FeedCreator::iTrunc(htmlspecialchars(strip_tags($this->items[$i]->title)),100).
"</a></div>";
} else {
$feedArray[] =
"<div class='".$this->stylePrefix."item_title'>".
FeedCreator::iTrunc(htmlspecialchars(strip_tags($this->items[$i]->title)),100).
"</div>";
}
}
if ($this->items[$i]->getDescription()) {
$feedArray[] =
"<div class='".$this->stylePrefix."item_description'>".
str_replace("]]>", "", str_replace("<![CDATA[", "", $this->items[$i]->getDescription())).
"</div>";
}
}
if ($this->footer) {
$feedArray[] = "<div class='".$this->stylePrefix."footer'>".$this->footer."</div>";
}
$feed= "".join($feedArray, "\r\n");
return $feed;
}
/**
* Overrrides parent to produce .html extensions
*
* @return string the feed cache filename
* @since 1.4
* @access private
*/
function _generateFilename() {
$fileInfo = pathinfo($_SERVER["PHP_SELF"]);
return substr($fileInfo["basename"],0,-(strlen($fileInfo["extension"])+1)).".html";
}
}
/**
* JSCreator is a class that writes a js file to a specific
* location, overriding the createFeed method of the parent HTMLCreator.
*
* @author Pascal Van Hecke
*/
class JSCreator extends HTMLCreator {
var $contentType = "text/javascript";
/**
* writes the javascript
* @return string the scripts's complete text
*/
function createFeed()
{
$feed = parent::createFeed();
$feedArray = explode("\n",$feed);
$jsFeed = "";
foreach ($feedArray as $value) {
$jsFeed .= "document.write('".trim(addslashes($value))."');\n";
}
return $jsFeed;
}
/**
* Overrrides parent to produce .js extensions
*
* @return string the feed cache filename
* @since 1.4
* @access private
*/
function _generateFilename() {
$fileInfo = pathinfo($_SERVER["PHP_SELF"]);
return substr($fileInfo["basename"],0,-(strlen($fileInfo["extension"])+1)).".js";
}
}
/*** TEST SCRIPT *********************************************************
//include("feedcreator.class.php");
$rss = new UniversalFeedCreator();
$rss->useCached();
$rss->title = "PHP news";
$rss->description = "daily news from the PHP scripting world";
//optional
//$rss->descriptionTruncSize = 500;
//$rss->descriptionHtmlSyndicated = true;
//$rss->xslStyleSheet = "http://feedster.com/rss20.xsl";
$rss->link = "http://www.dailyphp.net/news";
$rss->feedURL = "http://www.dailyphp.net/".$PHP_SELF;
$image = new FeedImage();
$image->title = "dailyphp.net logo";
$image->url = "http://www.dailyphp.net/images/logo.gif";
$image->link = "http://www.dailyphp.net";
$image->description = "Feed provided by dailyphp.net. Click to visit.";
//optional
$image->descriptionTruncSize = 500;
$image->descriptionHtmlSyndicated = true;
$rss->image = $image;
// get your news items from somewhere, e.g. your database:
//mysql_select_db($dbHost, $dbUser, $dbPass);
//$res = mysql_query("SELECT * FROM news ORDER BY newsdate DESC");
//while ($data = mysql_fetch_object($res)) {
$item = new FeedItem();
$item->title = "This is an the test title of an item";
$item->link = "http://localhost/item/";
$item->description = "<b>description in </b>
HTML";
//optional
//item->descriptionTruncSize = 500;
$item->descriptionHtmlSyndicated = true;
$item->date = time();
$item->source = "http://www.dailyphp.net";
$item->author = "John Doe";
$rss->addItem($item);
//}
// valid format strings are: RSS0.91, RSS1.0, RSS2.0, PIE0.1, MBOX, OPML, ATOM0.3, HTML, JS
echo $rss->saveFeed("RSS0.91", "feed.xml");
***************************************************************************/
?>
注意下面的使用条件:
1.$item->data 传进去的值应该是int型的timestamp
>>如果是从mysql里取出来的,即便是整型(事实上也是字符串,在php程序中用会自动转换类型),也要用 intval 强制转换一下。
2.需要设一下文件中的时区
>>时区改这里:define("TIME_ZONE", "+08:00");
3.如果中文页面用的是gb2312或者utf-8需要相应的把默认的encoding改掉
>>编码改这里:var $encoding = "GBK"; 不过事实这里只是一个默认值,调用时可以改的。
下载文件
企业网站渐渐死去:企业网站的生存之路在何方?
2007/01/22 13:26 ArthurXF
引用
国务院信息化工作办公室发布的《互联网络信息资源数量调查报告》显示:有51.5%的企业网站每天页面访问量在50次以下,并且这个比例还在不断扩大。也就是说,有一半以上的企业网站处于沉睡状态。这组数据无疑向我们传递出这样的信息:企业网站正在渐渐的死去。
企业是一个掌握着巨额资金的群体,企业网站占据了国内网站最多的份额(70%以上),企业网站拥有最迫切的推广需求,但呈现在我们面前的事实却是:经历过2001年的建站狂潮之后,企业网站始终在走着下坡路。本该蓬勃发展的企业网站,春天还没到来就面临夭折,内因何在?
解读中小企业网站的死穴
在谈到企业网站的发展时,企业主感慨良多。尤其是中小型企业主对企业网站日渐失望:"本来以为花大价钱,上马企业网站之后就会有雪片一样的订单,没想到企业网站不但没有带来业务,反而累积了越来越高的各种成本和一大堆烦人的垃圾邮件!"企业网站正在被企业主视为鸡肋,食之无味,弃之可惜。
过分追求华而不实
随便打开一个企业网站,扑面而来的是长达30秒的动画广告,考验着访问者所在地区宽带的普及水平。不少企业主认为,网站是企业个性化的展示舞台,所以希望把首页做得美轮美奂。但用户访问企业网站是为了查看企业信息、寻找商业机会。在网页中放上一个毫无商业意义的动画广告,在0.1秒之内就会被访问者所关闭,即使网站中蕴含着含金量非常高的信息也是枉然。很多企业网站就在这种所谓的"美丽"首页中赶走了潜在的客户。
造成这种现象更深层次的原因来自建站公司,他们为了得到短期的利益,一味的去迎合客户的表面需求,过度追求感官上的刺激。而企业主大多对于网络营销又缺乏深入的理解,久而久之,造成了企业网站越来越华而不实、浏览者不愿访问的局面。
信息折叠,查找麻烦
通常,企业网站都会设置"企业简介"、"企业动态"、"产品和服务"、"联系我们"等很多不同的频道,再将本就不够丰满的企业信息分配到这些孤立的频道中。点击其中的一个频道会发现,每个频道里只有寥寥的几行字或者孤零零的几张图片。在某种程度上说,企业网站相当于企业的"简历",而访问者就是条件苛刻的人事主管。试想一个人事主管翻阅着你20多页的简历,但每页上就只有10个字,会作何反应?对企业网站而言,每个潜在客户每天接触到的企业信息可能多达上百条,如果在您的企业网站上,一分钟内不能让他找到需要的信息,很可能就关掉了访问的网页。于是,花费大量推广费用获得的潜在客户就在企业网站的多级页面前悄然离开。
缺乏互动,无法交流
不少网站都宣称有交互功能。两年前,笔者曾经抱着试一试的态度给几个企业网站留言,到现在还没有收到他们的回复,不知道是企业没有收到邮件,还是没人看到,或者根本就没人去看?企业网站很多时候只是一张冷冰冰的"海报"而已。
单兵作战,不支持全员营销
没有充足的人员管理企业网站是又一个致命缺陷。据CNNIC中心统计,有45%的网站仅有一个人管理,通常这个人还是兼职的,或者是对企业相关业务一无所知的"网管",造成了企业网站完全游离于企业业务之外。
事实上,企业网站必须与企业的日常经营有机的结合在一起。否则,网站就成了点缀。
网络营销应该贯穿企业的每一个环节:招聘部门可以发布招聘信息,销售部门发布产品信息和促销信息,而采购部门则发布采购信息或招标信息,对应这些信息的反馈,也应该回流到各部门相关人员的手中,及时的处理。全员推广才是企业网站发展的重要推动力。
企业网站的没落使得无数对网络营销非常重视的企业主、营销专业人员、业内人士都非常苦恼,于是越来越多的人开始探寻企业网站的解决之道,而企业究竟需要一个什么样的网站自然成为了一个众说纷纭的话题。
后企业网站时代的思考:标准化+门户化+互动性+全员参与企业网站如果想走出困境,真正成为帮助企业推广的有力武器,必须要打通以上所分析的几个死穴,因此企业网站的理想模型必将具备以下四个特性:
1.标准化:企业网站是给潜在客户所建立的,因此客户的需求就是建立企业网站的最高需求。全世界有几亿台电脑,但他们都有着相同结构的键盘,同理,对于客户来说,企业网站也应该具备标准化的布局,让客户能够轻松的找到自己需要的信息,而不是在迷宫中穿梭。
2.门户化:把能表现企业核心价值的信息都罗列在首页,让客户无需东奔西跑,一个页面解决全部困惑,这是企业主和客户共同的需求。
3.互动性:每个潜在的客户都希望与企业的对应人员互动,进行交流。企业网站能否让他们轻易的找到相关人员,并能够通过便捷的方式进行沟通,极大程度的影响了客户的参与程度。
4.全员营销:众人拾柴火焰高,只有当企业中尽可能多的人员参与到网络营销中,才能取得令人满意的效果,尤其是在目前中小企业营销经费有限的情况下。市场人员、销售人员、客服人员、高层管理者分别面向不同层次的准客户,积极的传递出企业的核心价值,这是中小企业网络营销发展的必然趋势。
市场呼唤性价比高的网络营销平台
毫无疑问,标准化、门户化、互动性、全员参与将是今后企业网站的必然发展趋势,而同时企业网站的推广、应用也将进入一个全新时代。
中小企业对网站建设、推广、应用方面除了在功能上有需求之外,性价比也是一个非常重要的评价标准,中小企业无力支付昂贵的费用。
企业是一个掌握着巨额资金的群体,企业网站占据了国内网站最多的份额(70%以上),企业网站拥有最迫切的推广需求,但呈现在我们面前的事实却是:经历过2001年的建站狂潮之后,企业网站始终在走着下坡路。本该蓬勃发展的企业网站,春天还没到来就面临夭折,内因何在?
解读中小企业网站的死穴
在谈到企业网站的发展时,企业主感慨良多。尤其是中小型企业主对企业网站日渐失望:"本来以为花大价钱,上马企业网站之后就会有雪片一样的订单,没想到企业网站不但没有带来业务,反而累积了越来越高的各种成本和一大堆烦人的垃圾邮件!"企业网站正在被企业主视为鸡肋,食之无味,弃之可惜。
过分追求华而不实
随便打开一个企业网站,扑面而来的是长达30秒的动画广告,考验着访问者所在地区宽带的普及水平。不少企业主认为,网站是企业个性化的展示舞台,所以希望把首页做得美轮美奂。但用户访问企业网站是为了查看企业信息、寻找商业机会。在网页中放上一个毫无商业意义的动画广告,在0.1秒之内就会被访问者所关闭,即使网站中蕴含着含金量非常高的信息也是枉然。很多企业网站就在这种所谓的"美丽"首页中赶走了潜在的客户。
造成这种现象更深层次的原因来自建站公司,他们为了得到短期的利益,一味的去迎合客户的表面需求,过度追求感官上的刺激。而企业主大多对于网络营销又缺乏深入的理解,久而久之,造成了企业网站越来越华而不实、浏览者不愿访问的局面。
信息折叠,查找麻烦
通常,企业网站都会设置"企业简介"、"企业动态"、"产品和服务"、"联系我们"等很多不同的频道,再将本就不够丰满的企业信息分配到这些孤立的频道中。点击其中的一个频道会发现,每个频道里只有寥寥的几行字或者孤零零的几张图片。在某种程度上说,企业网站相当于企业的"简历",而访问者就是条件苛刻的人事主管。试想一个人事主管翻阅着你20多页的简历,但每页上就只有10个字,会作何反应?对企业网站而言,每个潜在客户每天接触到的企业信息可能多达上百条,如果在您的企业网站上,一分钟内不能让他找到需要的信息,很可能就关掉了访问的网页。于是,花费大量推广费用获得的潜在客户就在企业网站的多级页面前悄然离开。
缺乏互动,无法交流
不少网站都宣称有交互功能。两年前,笔者曾经抱着试一试的态度给几个企业网站留言,到现在还没有收到他们的回复,不知道是企业没有收到邮件,还是没人看到,或者根本就没人去看?企业网站很多时候只是一张冷冰冰的"海报"而已。
单兵作战,不支持全员营销
没有充足的人员管理企业网站是又一个致命缺陷。据CNNIC中心统计,有45%的网站仅有一个人管理,通常这个人还是兼职的,或者是对企业相关业务一无所知的"网管",造成了企业网站完全游离于企业业务之外。
事实上,企业网站必须与企业的日常经营有机的结合在一起。否则,网站就成了点缀。
网络营销应该贯穿企业的每一个环节:招聘部门可以发布招聘信息,销售部门发布产品信息和促销信息,而采购部门则发布采购信息或招标信息,对应这些信息的反馈,也应该回流到各部门相关人员的手中,及时的处理。全员推广才是企业网站发展的重要推动力。
企业网站的没落使得无数对网络营销非常重视的企业主、营销专业人员、业内人士都非常苦恼,于是越来越多的人开始探寻企业网站的解决之道,而企业究竟需要一个什么样的网站自然成为了一个众说纷纭的话题。
后企业网站时代的思考:标准化+门户化+互动性+全员参与企业网站如果想走出困境,真正成为帮助企业推广的有力武器,必须要打通以上所分析的几个死穴,因此企业网站的理想模型必将具备以下四个特性:
1.标准化:企业网站是给潜在客户所建立的,因此客户的需求就是建立企业网站的最高需求。全世界有几亿台电脑,但他们都有着相同结构的键盘,同理,对于客户来说,企业网站也应该具备标准化的布局,让客户能够轻松的找到自己需要的信息,而不是在迷宫中穿梭。
2.门户化:把能表现企业核心价值的信息都罗列在首页,让客户无需东奔西跑,一个页面解决全部困惑,这是企业主和客户共同的需求。
3.互动性:每个潜在的客户都希望与企业的对应人员互动,进行交流。企业网站能否让他们轻易的找到相关人员,并能够通过便捷的方式进行沟通,极大程度的影响了客户的参与程度。
4.全员营销:众人拾柴火焰高,只有当企业中尽可能多的人员参与到网络营销中,才能取得令人满意的效果,尤其是在目前中小企业营销经费有限的情况下。市场人员、销售人员、客服人员、高层管理者分别面向不同层次的准客户,积极的传递出企业的核心价值,这是中小企业网络营销发展的必然趋势。
市场呼唤性价比高的网络营销平台
毫无疑问,标准化、门户化、互动性、全员参与将是今后企业网站的必然发展趋势,而同时企业网站的推广、应用也将进入一个全新时代。
中小企业对网站建设、推广、应用方面除了在功能上有需求之外,性价比也是一个非常重要的评价标准,中小企业无力支付昂贵的费用。
看了上面摘录的内容,我们可以深深地体会到企业网站的生存空间很小,甚至很多的企业认为企业网站仅仅是公司形象的一部分,并没有多大的用处。或者是认为企业传统的销售渠道已经很好了,没必要再通过网络销售了。
其实这些想法都是错误的,目前全球已经进入了信息化社会,公司内部信息化的管理,信息化的销售和沟通使得企业运转效率大大提高,对社会资源优化合理分配起了很大的促进作用,对于年轻一辈来说,找信息肯定上网找。
那么企业网站如何脱颖而出?如何让更多的客户找到自己?如果能够解决这些问题,就可以大大提高企业网站的寿命,并可以给企业带来更多的利润和更大的知名度。解决了以下的问题,企业网站就可以为企业带来效益:
1. 企业网站不要一味的追求美观:企业必须正视企业网站的功能,而不是一味的追求美观,美观很多时候是牺牲网站实用性来实现,例如全Flash的企业网站,内容更新不便,搜索引擎无法收录内容,失去了免费宣传的最好工具的支持,可想而之,如果不做广告,这样的企业网站基本上可以说没人看,现实中95%的企业不会给企业网站做广告,那么这么一个漂亮美观动感十足的网站只好放着长蜘蛛网了。呵呵。
2.企业网站的功能不可忽视:目前80%的企业网站采用静态html形式制作的,这样的网站如果增添内容或者修改内容,必须有一定的网站专业基础的人才可以操作,可是企业内仅仅为了更改内容而请一个专业人员似乎不是很合算,所以这样的企业网站基本上都是几年不更新,既然没有内容更新,那么又会有多少人天天都来看内容始终不变的网站呢?所以企业网站还是应该采用程序支持的动态网站,企业定时发布一些企业相关的新闻或者产品,技术支持等内容,这样才能提高网站的访问量,而且这种动态网站更新很容易操作,会打字即可。
3.企业网站优化的重要性:目前企业网站在没有做广告的前提下,最大的流量来源就是搜索引擎,类似google或者baidu,那么企业网站如何获得搜索引擎很好的排名就成了一个很重要的课题,也就是SEO,搜索引擎优化,优化分为链接优化、关键词优化、页面代码优化,链接优化就是将动态程序链接生成或者模拟静态链接,这样可以让搜索引擎大范围收录网站内容,关键词优化就是提高页面内容和关键词相关性,使得关键词在搜索引擎的搜索结果排名提高,页面代码优化就是将页面里面的代码整理,尽量减少不必要的多余代码,增加网页打开速度,并减少网页代码下载量,来提高对搜索引擎的友好度。
4.企业网站的扩展性:企业网站不是一成不变的,现在的功能也许适合现在使用,不见得将来也适合,如何灵活扩展功能,还是重建整个网站?从经济,时效各方面考虑,扩展功能性价比远远高于重建,所以企业网站也要选择有很好技术支持的可扩展的建站系统。
5.企业网站的传播性:很多企业网站如闭门造车,造好了也不用,也不去宣传,那么这样的网站很难有流量,企业自然也很难利用网站来提高企业知名度,好多建站系统可以将企业网站的信息通过不同的渠道传播出去。让企业可以不用投入太多的精力在宣传上。
综上所述,一个好的企业网站绝对不仅仅只是有一个漂亮界面,现在很多的建站公司为了追求利润最大化,仅仅给企业做一个网站,而企业网站是否有效应,就一概不管了。网务公司开发网务通商务智能建站系统不仅很好的解决了上面企业网站需求的5点,而且做出了企业网站有效应的承诺保障,网务公司跟踪使用网务通建站系统建立的企业网站流量状态,终身免费为企业网站升级,免去了企业没有技术人员之苦,维护和技术支持均由网务公司负责,网务公司的口号是“只做有效应的网站,为企业服务到底!”
如果对网务公司的网务通商务智能建站系统感兴趣的话,可以去网务通商务智能建站系统查看具体的介绍。做企业网站就要选择好的建站系统和好的建站服务!
现在ArthurXF本人正在搞PHP等技术培训,如果想学习的人可以跟我联系。另外培训的招生简章在这个网址,想了解的可以去看看。也可以联系我QQ:29011218。
PHP培训招生简章