Feb 20 2008

perl 把字符串当作文件

Category: 技术ssmax @ 14:10:17

把字符串当作文件

提出问题

现在你手头上有的是字符串,但是你想把它作为一个文件来处理。例如,有一个子函数,它的参数为文件句柄,但是你想这个函数直接作用于你的字符串,而你不想把字符串写入一个文件。

解决方案

用 Perl 5.8 版中的 I/O 标量:

open($fh,"+<",\\$string); # 从 $string 中读取数据

讨论

Perl 的输入输出层支持把数据存入标量及从标量中取出数据。像上面,当你用 <$fh> 语句读取一条记录的时候,你读的是 $string 中所存储数据的下一行。当你用 print 函数向 $string 中写入数据时,你会改变 $string 中现有的数据。你可以把 $fh 传递给一个参数为文件句柄的函数,而那个函数将不会知道它实际在处理字符串而不是文件。

Perl 也能以各种不同的方式来打开字符串,所以,你可以用只读、覆盖、追加等方式打开:

open($fh, "<", \\$string);  # 只读方式打开字符串
open($fh, ">", \\$string);  # 先清空字符串,然后字符串只写
open($fh, "+>", \\$string);  # 先清空字符串,字符串可读可写
open($fh, "+<", \\$string);  # 字符串可读可写,但是写的时候添加在已有内容的后面

这些返回的句柄完全和文件句柄一样,所以所有针对文件的 I/O 操作都能继续用,例如 seek,truncate,sysread 和 friends。


Feb 06 2008

修复wordpress评论数

Category: 技术ssmax @ 15:26:42

以前很多垃圾comment都删掉了,在mysql里面直接删的,没有更新wp_posts表,今天搞了一次

 update wp_posts set comment_count = (select count(*) from wp_comments where comment_post_ID=ID);

 貌似子查询是mysql5才有的吧。。。

 


Jan 26 2008

找到包含php文件的目录

Category: 技术ssmax @ 23:17:03

由于空间的php.ini要求放到所有包含php的子目录里面才行,写了一行命令执行,记录之。。。

find . -type f -iname "*.php" | sed "s/[^\/]*$//" | sort | uniq | xargs -i cp -f php.ini {}

 


Jan 26 2008

关闭wordpress的全角转换

Category: 技术ssmax @ 18:08:07

wordpress在显示的时候会对引号之类的做全角转换,可能是为了防止一些网页代码之类的吧,反正搞到我贴代码的时候很烦,编辑n次都不行,今天一个火起看了一下源代码,在wp-includes/formatting.php 里面,屏蔽掉

 // static strings
 // $curl = str_replace($static_characters, $static_replacements, $curl);
 // regular expressions
 // $curl = preg_replace($dynamic_characters, $dynamic_replacements, $curl);
 

一个静态转换,一个动态正则转换,懒得看是转换了什么,全部屏蔽,出问题再算。。


Jan 26 2008

perl cgi virtual shell console

Category: 技术ssmax @ 17:30:05

这两天在新空间申请ssh shell access,还没有开通,又把cgi 翻了出来应急,参考网上一个php shell的脚本改了一下,主要抄了ajax的部分,嘿嘿,用php的system函数会涉及到占用内存和php执行时间的问题,有很多shell命令都无法正常执行,所以还是用perl cgi来得实际。。。

perl cgi virtual shell console:

 

 

 

#!/usr/bin/perl
use CGI;
my $command = CGI::param('cmd') || '';
my $pwd = CGI::param('pwd') || `pwd`;

print ("Content-Type: text/plain;\n");
print ("Cache-Control: no-cache\n\n");
#print "change dir: ".`cd $pwd 2>&1`."\n";
#print "now dir: ".`pwd`."\n";
#print "command: ".$command."\n\n";
if($command ne '') {
print `cd $pwd; $command 2>&1`;
}
else {
$~ = "MYFORMAT";
write;
format MYFORMAT =
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>PHP AJAX Shell</title>
<script type="text/javascript" language="javascript">
var CommHis=new Array();
var HisP;
function doReq(_1,_2,_3){var HR=false;
if(window.XMLHttpRequest){HR=new XMLHttpRequest();
if(HR.overrideMimeType){HR.overrideMimeType("text/xml");}}
else{if(window.ActiveXObject){
try{HR=new ActiveXObject("Msxml2.XMLHTTP");}
catch(e){try{HR=new ActiveXObject("Microsoft.XMLHTTP");}
catch(e){}}}}
if(!HR){return false;}
HR.onreadystatechange=function(){if(HR.readyState==4){
if(HR.status==200){if(_3){eval(_2+"(HR.responseXML)");}
else{eval(_2+"(HR.responseText)");}}}};
HR.open("GET",_1,true);HR.send(null);}
function pR(rS){var _6=document.getElementById("outt");
_6.value+=document.getElementById("cmd").value;
_6.value+="\n";
_6.value+=rS;
_6.value+="\n:->";
_6.scrollTop=_6.scrollHeight;
/*
var _7=rS.split("\n\n");
var _8=document.getElementById("cmd").value;
_6.appendChild(document.createTextNode(_8));
_6.appendChild(document.createElement("br"));
for(var _9 in _7){var _a=document.createElement("pre");
_a.style.display="inline";
line=document.createTextNode(_7[_9]);
_a.appendChild(line);_6.appendChild(_a);
_6.appendChild(document.createElement("br"));}
_6.appendChild(document.createTextNode(":-> "));
_6.scrollTop=_6.scrollHeight;
*/
document.getElementById("cmd").value="";}
function keyE(_b){switch(_b.keyCode){
case 13:
var _c=document.getElementById("cmd").value;
if(_c){CommHis[CommHis.length]=_c;
HisP=CommHis.length;
var _d=document.location.href+"?cmd="+escape(_c)+"&pwd="+escape(document.getElementById("pwd").value);
doReq(_d,"pR");}
break;
case 38:
if(HisP>0){HisP--;
document.getElementById("cmd").value=CommHis[HisP];}
break;
case 40:
if(HisP<CommHis.length-1){HisP++;
document.getElementById("cmd").value=CommHis[HisP];}
break;
default:
break;}}
</script></head>
<body style="font-family:courier">
<form onsubmit="return false">
<textarea id="outt" cols="100" rows="30" style="background-color: #000; color: #3F0;" readonly="readonly">:-&gt;</textarea><br/>
Command: <input tabindex="1" onkeyup="keyE(event)" style="color:#FFF;background:#333;width:100%;" id="cmd" type="text" /><br/>
.
print('WorkDir: <input tabindex="1" style="color:#FFF;background:#333;width:100%;" id="pwd" type="text" value="'.$pwd.'"/>'."\n");
print("</form></body></html>\n");
}


Jan 22 2008

NTLM代理的使用

Category: 技术ssmax @ 15:41:20

目前在Windows平台下代理服务器软件产品主要有:Microsoft Proxy,Microsoft ISA,Netscape Proxy、WinProxy、WinGate Pro、winRoute、SyGate以及CCProxy等等。

  由于目前HTTP应用最广泛,因为http代理的实现也是最重要的。http代理又有两种认证方式: Proxy Basic认证和Proxy NTLM认证。

  Proxy Basic认证一般用于 不太重要的场合,因为Basic认证时直接把用户名密码变一下形就从网络上传过去了,随便sniffer一下就能把这个 数据截获从而得到 密码。

 Proxy NTLM认证用于正式场合,它的认证方式与Windows远程登录的认证方式是一样的,需要三次握手传递信息,不在认证的过程中传递密码,而且,每次认证传递的信息都不一样。

 Proxy NTLM认证 涉及到 复杂的密码算法及公钥机制,所以很多软件都没有实现通过这种方式使用代理,然而不少的公司内网都是用NTLM认证的代理限制员工上网,下面介绍了如何把NTLM的代理转变为本机普通HTTP代理:

需要安装PYTHON,然后下载 NTLM authorization Proxy Server

这个是Python写的,所以必须先安装Python运行环境。下载了NTLM APS以后,直接解压,不要管那个bat文件,直接双击main.py即可运行。不过在运行之前必须先配置一下:

打开server.cfg,修改以下参数:
PARENT_PROXY:代理的IP
PARENT_PROXY_PORT:代理的端口
NT_DOMAIN:你的域
USER:用户名
PASSWORD:密码

实在是很简单吧。这样就搞定了。运行main.py以后就在本地生成了一个普通HTTP代理,端口是5865。


Jan 20 2008

eMule个人数据备份

Category: 技术ssmax @ 17:41:02

eMule个人数据备份:

换eMule时可以使用网友绿色西瓜的批处理工具“04XEmuleBack”对Config文件夹进行备份,以便于转移。

NeoMule4.25的clients.met[信用文件,记载他人信用]已使用官方版本的格式,但是格式刚换新所以可能会有点问题:
NeoMule互copy时必须clients.met、clients2.met都copy才行(已写入上述批处理工具),否则可能会出错
NeoMule拷贝到Xtreme或官方eMule时,可以将NeoMule的clients.met先拷贝到MorphXT,打开MorphXT再关掉以转换其格式,再把MorphXT的这个clients.met拷贝到Xtreme或官方eMule

另注:其他版本copy到NeoMule均正常

备份文件列表:

cryptkey.dat 安全认证
preferences.dat userhash
clients.met 信用文件
clients2.met NeoMule信用文件
emfriends.met 好友列表
known.met 已知文件信息[包括hash,上传数等]
known2_64.met AICH hash相关,存放文件的hash值
preferences.ini EM的配置文件,同时存放旧版的统计数据[0.43以前]
Category.ini 下载分类
fileinfo.ini 文件注释
statistics.ini 新版的统计数据[0.43及以后的版本]
shareddir.dat 共享的目录
staticservers.dat 静态服务器列表
server.met 服务器列表
preferencesKad.dat Kad网络上的userhash


Jan 18 2008

hibernate 的 one to zero or one

Category: 技术ssmax @ 18:44:35

hibernate能支持这种方式,一对零或一,但是不能做到lazy,今天搞cdr搞了一天,锁死在这个问题上面了,暂时无解

 cdr的表已经400多w数据了,而且字段有nn个,这次新加的属性也不是每条都用到,所以是一个one to zero or one的关系,但是hibernate怎么搞都不能做到lazy,这样如果搜索很多cdr的时候就造成join表或者select过多。。。很郁闷。到官方论坛找了一下,貌似这个问题暂时无解。。。摘录一点评论:

from http://forum.hibernate.org/viewtopic.php?t=949458

1、

The problem is that sometimes we really need a lazy “one-to-zero-or-one” relationship. If we would change to a lazy one-to-one we would have to use the constrained attribute in ‘one-to-one’ element which forces a not null attribute. But sometimes a person doesn’t have a note.The use of “many-to-one” mapping that we did fits perfectly to our needs. It gets a proxy when we don’t use eager fetching. It gets the right note when we use eager fetching, but it gets a proxy when there is no note associated to the person. So it works almost perfectly, except that when we force eager fetching in null notes, Hibernate hydrates person objects with a proxy and not with a null reference.I know that I could add a FK in note table and solve my problem, but why should I do that if I can use the same person PK? If Hibernate would return null when there is no note it would be just right. I just would like that the following mapping should work as stated above. I think that when we get a proxy when an eager fetching is used is an inconsistent result. In our case, we are using a workaround to detect and fix this situation, but we wanted to report it as a way to help other people who may face the same problem and maybe to help Hibernate become even better.Many-to-one mapping:

<many-to-one name=”note” class=”Note” column=”id”
                   unique=”true” insert=”false” update=”false”
                   cascade=”all”/>

Our problem is not so rare. We found other people in this forum facing the same lazy “one-to-zero-or-one” problem, but the solutions were “non-lazy one-to-one mapping”, “lazy not-null one-to-one mapping using constrained=”true”” or “many-to-one mapping using a foreign key (FK)”. We are suggesting one more solution to the problem: “many-to-one mapping using the same primary key (PK)”. I hope that can help someone else.
 

2、

Hibernate cannot replace a proxy with a null, this is not possible in Java wo bytecode enhancement.
So wo does hibernate know whether it needs to create a proxy or set null? The only solution is to load the second row. Check the wiki comunity area, there is a page dedicated to the non one-to-one lazyness

3、 The use of not-found=”ignore” results in a non-lazy property. So lazy “one-to-zero-or-one” in Hibernate is not possible yet. We can use non-lazy “one-to-zero-or-one” or a lazy one-to-one association. 4、hibernate teamHi guys,Just a small comment – there is *no* problem in using polymorpishm with respect to associations in hibernate. The issue you have is about representing NULL, something that is more related to the kind of datamodelling people tend to do and not OO. The users I see having problem with polymorphic associations are mostly those who want to downcast their objects which again is something that is arguable OO.And note, that just because there *can* be a association defined between two objects, doesnt mean it *has* to be physically represented in your object model.In any case, all is possible now with H3.1, so go buy your bigger machines 😉

补充:

下班前终于想到一个临时方法搞定。。。贴到hibernate。。。用one to many 来实现,会好看一点,呵呵

i think i have this problem today , i don’t know whether hibernate team has resolved this problem or not after two years…(the last post is 2005…)and i think a temp solution:
use one-to-many instead , i think one to many includes one to zero or one or many….
and for one to zero or one , wirte a bean method to handle the Set/List and get the real one value….
<set name=”notes” table=”Note” inverse=”true” lazy=”true”>
<key column=”owner”></key>
<one-to-many class=”Note”/>
</set>

and you have a
Set getNotes() in the module

add a method:

Note getNote() {
if(notes == null) return null;
else if(notes.size()>0)return notes.iterator().next();
else return null;
}


Jan 11 2008

邮件格式互转

Category: 技术ssmax @ 11:23:30

最近我的foxmail好像出了点问题,回复邮件的时候经常cpu占用100%,就想换个客户端玩玩,但是公司的邮件都放在foxmail下面,要导出来才行,foxmail自己好像没有提供批量导出的功能,找到了不少办法,记录一下:

如果想用DreamMail的话,DreamMail里面直接就提供了导入foxmail box邮箱的方法,所以转用dreammail的就不用看了,嘿嘿

 国际上面流行的格式一般是eml和mbox,eml是分开一封封邮件的,mbox就是合成到一个文件,而foxmail自己的box格式本来就和mbox相似,找了一下好像有人写了个perl直接把box转换到mbox,我就懒得研究了。。大家可以google一下。。

1、把foxmail的box邮箱导出到eml的方法:

使用月影foxmail邮件转换工具,批量导出成eml文件,不过导出之前最好用fomail的压缩功能删掉已经没用的邮件。

2、eml和mbox方式互转:

windows下面用一只叫做 IMAPSize 的工具就可以了,里面有个eml2mboxs的功能,可以自动搜索子目录来转换,不用一个个来,也有eml2mbox、mbox2eml的互转。

linux下面到google搜索一个eml2mbox的ruby或者perl脚本就好了,上面那个IMAPSize的地方也有一个ruby脚本。

3、mbox与thunderbird:

雷鸟好像直接就是用mbox格式保存邮件的,新版完全不需要任何导入工具,直接把mbox文件copy到邮箱目录,然后在重开雷鸟,就可以找到那些邮件了。


Jan 09 2008

resin 3.1 里面 ognl list 问题

Category: 技术ssmax @ 10:58:45

I use <s:select /> tag and find it doen’t work under resin 3.1.x;but it works well under tomcat 5.5.x and tomcat 6.0.x. The sample from struts-2.0.9\docs\docs\select.html, please try the code below under resin 3.1.x:

<s:select label=”Months”
       name=”months”
       headerKey=”-1″ headerValue=”Select Month”
       list=”#{’01’:’Jan’, ’02’:’Feb’}”
       value=”selectedMonth”
       required=”true”
/>

it fails.

#{} won’t be working in the future , see

http://java.sun.com/products/jsp/reference/techart/unifiedEL.html

use the alternative OGNL map syntax:

list=”#@java.util.HashMap@{‘Jan’:’01’, ‘Feb’:’02’, ‘Mar’:’03’}”

http://www.ognl.org/2.6.9/Documentation/html/LanguageGuide/collectionConstruction.html#mapConstruction

所有tag里面自定义的list都要新的方式,要不resin新版就显示不了的了


« Previous PageNext Page »