月度归档:2012年08月

WordPress: modify the upload list of allowed file types

When you attempt to upload a file in WordPress that is not in the default list of acceptable file types, you will receive the following error: File type does not meet security guidelines. Try another. While there’s no admin-based tool for editing list of allowed file types, it’s not at all difficult to add your own or remove any existing.
Upload filetypes are checked by the function wp_check_filetype in wp-includes/functions.php. But we will add new file types to the file functions.php in our template due to upgrade WordPress.
Open your theme’s functions.php file and add this line somewhere between the <?php and ?>:
 

add_filter('upload_mimes', 'custom_upload_mimes');
function custom_upload_mimes ( $existing_mimes=array() ) {
// add your extension to the array
$existing_mimes['deb'] = 'application/x-deb';
// add as many as you like
// removing existing file types
unset( $existing_mimes['exe'] );
// add as many as you like
// and return the new full result
return $existing_mimes;
}

In this case I allowed upload .deb files and banned upload .exe files. List of MIME Types can be found here, or use google, if your file type is not in the list (e.g. deb or rpm file type).

IIS7 WordPress 中文 URL 解决方案

1、
使用插件 IIS Chinese Tag Permalink
2、
用 WordPress 写中文博客时 URL 中经常会包含中文字符,一般出现在 Tag 和分类的 URL 中。而 IIS7 对包含中文的 URL 支持有问题。这样的情况下访问如「http://sheng00.com/tag/软件」这样的 URL 就会发生 404 错误。
像 Godaddy 的 Windows 主机正是使用 IIS7 作为 Web 服务器的,在此空间上使用 WordPress 就会出现以上问题,虽然可以通过修改程序文件或者修改 Tag 和分类的别名为英文的方法来解决,但非常不实用。
下面介绍一种解决方法,只需要增加一个 PHP 文件,并修改一下 web.config 文件中的 Rewrite 规则即可,不需要修改程序文件。
新建一个“chineseurl.php”文件,内容如下:

<?php
if (isset($_SERVER['HTTP_X_ORIGINAL_URL'])) {
// IIS Mod-Rewrite
$_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_ORIGINAL_URL'];
} else if (isset($_SERVER['HTTP_X_REWRITE_URL'])) {
// IIS Isapi_Rewrite
$_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_REWRITE_URL'];
} else {
// Use ORIG_PATH_INFO if there is no PATH_INFO
(!isset($_SERVER['PATH_INFO']) && isset($_SERVER['ORIG_PATH_INFO'])) && ($_SERVER['PATH_INFO'] = $_SERVER['ORIG_PATH_INFO']);
// Some IIS + PHP configurations puts the script-name in the path-info (No need to append it twice)
if (isset($_SERVER['PATH_INFO'])) {
($_SERVER['PATH_INFO'] == $_SERVER['SCRIPT_NAME']) ? ($_SERVER['REQUEST_URI'] = $_SERVER['PATH_INFO']) : ($_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] . $_SERVER['PATH_INFO']);
}
// Append the query string if it exists and isn't null
(isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])) && ($_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING']);
}
require("index.php");
?>

修改 web.config 文件内容如下:

?<?xml version="1.0" encoding="UTF-8"?>
<configuration>
 <system.webServer>
 <rewrite>
 <rules>
 <rule name="cnUrl" stopProcessing="true">
 <match url="^(tag|category)/(.*)$" />
 <action type="Rewrite" url="cnurl.php" />
 </rule>
 <rule name="WordPress" patternSyntax="Wildcard">
 <match url="*" />
 <conditions>
 <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
 <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
 </conditions>
 <action type="Rewrite" url="index.php" />
 </rule>
 </rules>
 </rewrite>
 </system.webServer>
</configuration>

以上两个文件都存放在 WordPress 的安装目录。
如果 WordPress 不是装在根目录,则需要更改“<match url=”^(tag|category)/(.*)$” />”为“<match url=”^安装目录/(tag|category)/(.*)$” />”。如果在固定链接设置里把默认的标签前缀和分类目录前缀(tag 和 category)改了,则更改这句里的对应内容即可。
此文适用于 WordPress 2.9,2.9以下版本应该也可以适用,但笔者没有测试过。

js验证代码

1.JS判断是否是邮编

<script>
function isPostalCode(s)
{
//var patrn=/^[a-zA-Z0-9]{3,12}$/;
var patrn=/^[a-zA-Z0-9 ]{3,12}$/;
if (!patrn.exec(s)) return false
return true
}
var a = "315400";
alert(a+"是否合法!");
alert(isPostalCode(a));
a = "ab";
alert(a+"是否合法!");
alert(isPostalCode(a));
</script>

2.JS判断电话传真号码是否有效

<script>
function isTel(s)
{
var patrn=/^[+]{0,1}(d){1,3}[ ]?([-]?((d)|[ ]){1,12})+$/;
if (!patrn.exec(s)) return false
return true
}
var a = "0574-56158888";
alert(a+"是否合法!");
alert(isTel(a));
a = "123456a";
alert(a+"是否合法!");
alert(isTel(a));
</script>

3.js正则判断:只能输入5-20个以字母开头、可带数字、“_”、“.”的字符串

<script>
function isRegisterUserName(s)
{
var patrn=/^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){4,19}$/;
if (!patrn.exec(s)) return false
return true
}
var a = "abc";
alert(a+"是否合法!");
alert(isRegisterUserName(a));
a = "abcdef";
alert(a+"是否合法!");
alert(isRegisterUserName(a));
</script>

4.判断身份证号码的JS函数

<script type="text/javascript">
var aCity={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"}
function cidInfo(sId)
{
var iSum=0
var info=""
if(!/^d{17}(d|x)$/i.test(sId))
{
return false;
}
sId=sId.replace(/x$/i,"a");
if(aCity[parseInt(sId.substr(0,2))]==null)
{
return false;
}
sBirthday=sId.substr(6,4)+"-"+Number(sId.substr(10,2))+"-"+Number(sId.substr(12,2));
var d=new Date(sBirthday.replace(/-/g,"/"))
if(sBirthday!=(d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate()))
{
return false;
}
for(var i = 17;i>=0;i --)
{
iSum += (Math.pow(2,i) % 11) * parseInt(sId.charAt(17 - i),11)
}
if(iSum%11!=1)
{
return false;
}
return true;
}
alert(cidInfo("3432423432423432"));
</script>

5.验证日期是否合法的JS函数

<script type="text/javascript">
function isDateFormat(txt){ //是否为合法的日期格式:YYYY-MM-DD
if(txt==null || txt == ""){return false;}
else{
var regex = /[0-9]{1,4}-[0-9]{1,2}-[0-9]{1,2}/;
if( regex.test(txt) ){
var noArr = txt.split("-");
var year = eval(noArr[0]);
var month = eval(noArr[1]);
var day = eval(noArr[2]);
if ( year < 1 || month < 1 || month > 12 || day < 1 || day > 31) {return false;}
if ((month == 4 || month == 6 || month == 9 || month == 11) && day > 30){return false;}
if (month == 2){
if ((year % 4 != 0) && day > 29) { return false;}
if (year % 4 == 0){
if(year % 100 == 0 && year % 400 != 0 && day > 29){return false;}
else if (day > 28){ return false;}
}
}
return true;
}else{return false;}
}
}
alert("2008-05-11是日期吗?答:"+isDateFormat("2008-05-11"));
</script>

6.验证手机号码固定电话号码的JS函数

<script type="text/javascript">
function isPhoneNum(txt){ //检测是否为电话号码(固定电话或手机)
return isPhone(txt)||isMobile(txt);
}
function isPhone(txt){//检测是否为固定电话号码
if(txt == null || txt == ""){return false;}
else{
var regex = /[0-9]{1}[0-9]{2,3}-[1-9]{1}[0-9]{5,8}/;
return regex.test(txt);
}
}
function isMobile(txt){//检测是否为手机号码
if(txt == null || txt == ""){return false;}
else{
var regex = /13[0-9]{9}/;
return regex.test(txt);
}
}
alert(isPhone("0574-56157573"));
alert(isMobile("0574-56157574"));
alert(isMobile("13956122541"));
</script>

7.控制文本框输入长度的例子

<script>
function test()
{
if(a.value.length>10){
alert("不能超过10个字符");
a.focus();
}
else
top.location.href="http://www.baidu.com";
}
</script>
<input type="text" name=a><input type="button" value="测试" onclick="test();">

8.JS判断中文的正则表达式

<script>
function strChinese(str){
var pattern = /[^u4E00-u9FA5]/;
return !pattern.test(str);
}
alert(strChinese("中文"))
alert(strChinese("abc"))
alert(strChinese("中文Abc"))
</script>

9.验证email邮箱地址的js

<script type="text/javascript">
function isEmail(str){
res = /^[0-9a-zA-Z_-.]+@[0-9a-zA-Z_-]+(.[0-9a-zA-Z_-]+)*$/;
var re = new RegExp(res);
return !(str.match(re) == null);
}
alert(isEmail("abc.com"));
alert(isEmail(""));
</script>