PHP

浏览模式: 标准 | 列表 | 图文
如何在微信里关闭窗口

如何在微信里关闭窗口

众所周知,微信里的网页就是一个webview。但是它是在程序里面,所以原生的window.close()是无效的。那么我们该怎么办?其实很简单就一句代码:

WeixinJSBridge.call('closeWindow');

你可以在你的链接或者button上加一句就OK了:

<a href="###" onclick="WeixinJSBridge.call('closeWindow');">CLOSE </a>

或者

<a href="javascript:WeixinJSBridge.call('closeWindow');">CLOSE</a>

试试看。窗口是不是关掉了?

如何在终端输出带颜色的字体?

如何在终端输出带颜色的字体?

终端显示颜色,在以前的想法当中,都是因为有了.profile的配色方案。而我一般也都是 采用默认的(snakevil是写过一个bash带颜色的方案的。我觉得太花哨了就没有使用)

为什么突然间又想到这个?是因为在使用PHP输出LOG的时候,千篇一率,从屏幕中找关键字很累,所以就想着,是不是用PHP也可以输出这种带颜色的关键字?当然,这是因为我正好看到了一个PHP是这么输出的,它就是laraval,它的工具(laraval.phar)在命令行的输出就是有不同颜色的,它给了我指引,意思就是,这个想法是可以实现的。

OK。找资料,知道在终端中用指定的字符来做为背景色和字体色(http://blog.csdn.net/acmee/article/details/6613060)

文中这样介绍:

一、shell下的实现方法

先来讲在shell下,如何实现。用echo命令就可以实现,参看以下例子:

echo -e "\033[32mHello, world!"

当你在终端里敲下这条命令后,是不是发现系统用绿色输出了"Hello,world!",不止如此,连之后的命令提示符都变成了绿色?不要着急,听我继续说。echo命令-e选项的作用是激活终端对反斜线转义符(即\)的解释。引号内\033用于引导非常规字符序列,在这里的作用就是引导设置输出属性,后边的[32m就是将前景色设置为绿色,字母m表示设置的属性类别,数字代表属性值。设置可以单独使用,例如:

echo -e "\033[0m"

这行命令的作用是恢复属性为默认值,也就是说0m设置项用于恢复默认值。现在你的终端是不是又一切正常了?

理解了这些,剩下的就简单了。用这种命令,除了设置文本前景色,还可以设置很多属性。下边列出其他的设置项:

--------------------------------------------------------------------------

\033[0m 关闭所有属性
\033[1m 设置高亮度
\033[4m 下划线
\033[5m 闪烁
\033[7m 反显
\033[8m 消隐
\033[30m 至 \33[37m 设置前景色
\033[40m 至 \33[47m 设置背景色
\033[nA 光标上移n行
\033[nB 光标下移n行
\033[nC 光标右移n行
\033[nD 光标左移n行
\033[y;xH设置光标位置
\033[2J 清屏
\033[K 清除从光标到行尾的内容
\033[s 保存光标位置
\033[u 恢复光标位置
\033[?25l 隐藏光标
\033[?25h 显示光标

--------------------------------------------------------------------------

各数字所代表的颜色如下:

字背景颜色范围:40----49
40:黑
41:深红
42:绿
43:黄色
44:蓝色
45:紫色
46:深绿
47:白色

字颜色:30-----------39
30:黑
31:红
32:绿
33:黄
34:蓝色
35:紫色
36:深绿
37:白色

另外,同类的多种设置项可以组合在一起,中间用分号(;)隔开。如下:

echo -e "\033[20;1H\033[1;4;32mHello,world\033[0m"

这行命令首先\033[20;1H将光标移动到终端第20行第1列,之后的\033[1;4;32m将文本属性设置为高亮、带下划线且颜色为绿色,然后输出Hello,world;最后\033[0m将终端属性恢复为默认值,这样就不会看到连命令完成后的命令提示符也变了样儿了。

通过以上各种命令的组合就可以实现对终端输出地复杂控制。

二、如何在C编程中实现?

理解了以上在Shell中的实现方法,关于在C中如何实现就很简单了。可以说只需要用printf函数代替上边的echo -e就OK了。参见下例:

int color = 32;

printf("\033[20;1H\033[1;4;%dmHello, world.\033[0m", color);

这个例子类似上边shell中最后那个例子,只是这里颜色值通过变量color来指定(当然,也可以直接指定)。

三、联想

看到这里你可能会想,是不是在其他编程语言里也可以用类似的方法实现对终端输出的控制呢?答案是肯定的!比如在python中,可以如下输出:

color=32

print “\033[20;1H\033[1;4;%dHello, world.\033[0m"%color

这个例子的效果跟上边C的例子是相同的。

其实在看到这一段之前,snakevil在自己的github的项目(https://github.com/snakevil/bashrc.x)中也介绍过,其实我相对还是喜欢ubuntu的默认配色,snakevil的这个配色我是真心不是特别喜欢。。

但究竟怎么用PHP输出呢?在用PHP输出之前,找了一下网络,发现已经有有用perl实现过了。那么说实在的。如果没有使用到一些特别的函数,其实php和perl实在是太象了,所以,可以直接参考(http://perldoc.perl.org/Term/ANSIColor.html),这里的代码,除了那个类外,都还是可以复刻的。于是,再随便找了点,果然还是有现成的PHP代码的(http://www.if-not-true-then-false.com/2010/php-class-for-coloring-php-command-line-cli-scripts-output-php-output-colorizing-using-bash-shell-colors/)

好吧。这个URL太长了。还是直接贴代码吧:

<?php

class Colors {
private $foreground_colors = array();
private $background_colors = array();

public function __construct() {
// Set up shell colors
$this->foreground_colors['black'] = '0;30';
$this->foreground_colors['dark_gray'] = '1;30';
$this->foreground_colors['blue'] = '0;34';
$this->foreground_colors['light_blue'] = '1;34';
$this->foreground_colors['green'] = '0;32';
$this->foreground_colors['light_green'] = '1;32';
$this->foreground_colors['cyan'] = '0;36';
$this->foreground_colors['light_cyan'] = '1;36';
$this->foreground_colors['red'] = '0;31';
$this->foreground_colors['light_red'] = '1;31';
$this->foreground_colors['purple'] = '0;35';
$this->foreground_colors['light_purple'] = '1;35';
$this->foreground_colors['brown'] = '0;33';
$this->foreground_colors['yellow'] = '1;33';
$this->foreground_colors['light_gray'] = '0;37';
$this->foreground_colors['white'] = '1;37';

$this->background_colors['black'] = '40';
$this->background_colors['red'] = '41';
$this->background_colors['green'] = '42';
$this->background_colors['yellow'] = '43';
$this->background_colors['blue'] = '44';
$this->background_colors['magenta'] = '45';
$this->background_colors['cyan'] = '46';
$this->background_colors['light_gray'] = '47';
}

// Returns colored string
public function getColoredString($string, $foreground_color = null, $background_color = null) {
$colored_string = "";

// Check if given foreground color found
if (isset($this->foreground_colors[$foreground_color])) {
$colored_string .= "\033[" . $this->foreground_colors[$foreground_color] . "m";
}
// Check if given background color found
if (isset($this->background_colors[$background_color])) {
$colored_string .= "\033[" . $this->background_colors[$background_color] . "m";
}

// Add string and end coloring
$colored_string .= $string . "\033[0m";

return $colored_string;
}

// Returns all foreground color names
public function getForegroundColors() {
return array_keys($this->foreground_colors);
}

// Returns all background color names
public function getBackgroundColors() {
return array_keys($this->background_colors);
}
}

用法也比较简单:

<?php

// Create new Colors class
$colors = new Colors();

// Test some basic printing with Colors class
echo $colors->getColoredString("Testing Colors class, this is purple string on yellow background.", "purple", "yellow") . "\n";
echo $colors->getColoredString("Testing Colors class, this is blue string on light gray background.", "blue", "light_gray") . "\n";
echo $colors->getColoredString("Testing Colors class, this is red string on black background.", "red", "black") . "\n";
echo $colors->getColoredString("Testing Colors class, this is cyan string on green background.", "cyan", "green") . "\n";
echo $colors->getColoredString("Testing Colors class, this is cyan string on default background.", "cyan") . "\n";
echo $colors->getColoredString("Testing Colors class, this is default string on cyan background.", null, "cyan") . "\n";

当然,如果你觉得这个代码太麻烦,还有一个简单的方法:

function colorize($text, $status) {
$out = "";
switch($status) {
case "SUCCESS":
$out = "[42m"; //Green background
break;
case "FAILURE":
$out = "[41m"; //Red background
break;
case "WARNING":
$out = "[43m"; //Yellow background
break;
case "NOTE":
$out = "[44m"; //Blue background
break;
default:
throw new Exception("Invalid status: " . $status);
}
return chr(27) . "$out" . "$text" . chr(27) . "[0m";
}

echo colorize("Your command was successfully executed...", "SUCCESS");

四种颜色也够了。不过。。。NOTE的blue background。。。如果你的字符还是黑色的,真心看不到字符串了。

至此,介绍完毕,你可以试试(我已经用在项目中了)

转一个别人写的所谓加解密函数

转一个别人写的所谓加解密函数

这段代码不是挺复杂,其实如果你细看是可以看得到discuz中的authcode的影子的。如果你有兴趣,你可以看看:

/*加密函数内部调用函数*/
function keyED($txt,$encrypt_key) {
$encrypt_key = md5($encrypt_key);
$ctr=0;
$tmp = "";
for ($i=0;$i<strlen($txt);$i++) {
if ($ctr==strlen($encrypt_key)) $ctr=0;
$tmp.= substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1);
$ctr++;
}
return $tmp;
}

/*发送邮件中连接地址的加密函数*/
function inner_DYEncrypt( $encryptstr ){
return urlencode(inner_DYEncrypt_subfun($encryptstr));
}

function inner_DYEncrypt_subfun($encryptstr){
srand((double)microtime()*1000000);
$encrypt_key = md5(rand(0,32000));
$ctr=0; $tmpstr = "";
for ($i=0;$i<strlen($encryptstr);$i++){
if ($ctr==strlen($encrypt_key)) $ctr=0;
$tmpstr.= substr($encrypt_key,$ctr,1) .
(substr($encryptstr,$i,1) ^ substr($encrypt_key,$ctr,1));
$ctr++;
}
$returninfo = base64_encode(keyED($tmpstr,ENCRYPTKEY));
if (strrpos($returninfo,"/") or strrpos($returninfo,'') or strrpos($returninfo,'+'))
return inner_DYEncrypt_subfun( $encryptstr );
return $returninfo;

}

/*发送邮件中连接地址的解密函数*/
function inner_DYDecrypt( $decryptstr ){
$decryptstr = urldecode($decryptstr);
$decryptstr = keyED(base64_decode($decryptstr),ENCRYPTKEY);
$tmpstr = "";
for ($i=0;$i<strlen($decryptstr);$i++){
$md5 = substr($decryptstr,$i,1);
$i++;
$tmpstr.= (substr($decryptstr,$i,1) ^ $md5);
}
return $tmpstr;
}

/*演示*/
$key = "rdid=5135"; //待加密的字符串
echo "待加密的字符串:".$key."";
$key = inner_DYEncrypt($key);
echo "加密后的字符串:".$key."";
echo "解密后的字符串:".inner_DYDecrypt($key);

?>

发完这个贴子的时候突然发现。我好久没有写博客了。倒不是不坚持,而是发现我实在没有什么东西好写了。一直吃老本,还能写什么?

http post : Content-Type

http post : Content-Type

今天在使用http post(curl)获取一个数据的时候发现了问题,死活拿不到数据。获取永远为空。

场景是这样的:我向微信发起一个请求,QQ请求我的服务器,由我请求接口服务器。返回结果
但问题是,如果由微信直接请求接口服务器。一切正常。反而因为我做了一次中间层后。取不到数据了。
一点一点排查:
1、接口服务器的TransferMode是chunked。OK。我CURL换成1.0请求,结果还是空
2、换file_get_contents。。。一样是空。
3、排查微信请求的头。在我的服务器上把$_SERVER变量打印出来。结果。。意外的发现:Content-Type: text/xml,居然是这个?那我换成这个请求接口服务器试一下呢?居然真的成功了
遇到问题,果然是要一个个的排查啊。对方是java的服务器。可能会httprawbody做了一个强验证。所以。。。不象我们PHP可以拿到数据。先simplexml_load_string,不行?再json_decode,还不行?再用其他处理。。。
[转]老王:如何安全的include文件

[转]老王:如何安全的include文件

大多数人会将一个单独的php文件当成配置文件,早些年的配置文件大多类似这样:

<?php
$config['a']=1;
$config['b']=2;

这样include后。可以使用$config变量了,但慢慢的,却越来越发现这样不太好,于是就有了这种

<?php
return array(
'a'=>1,'b'=>2
);

这时候只要写$config = include('config.php'),就相当于和上面一样了。然后老王讲的就是指第二种情况。

原文在:http://huoding.com/2014/02/25/329

他举的例子是:

<?php

$debug = false;
// ...
$config = include 'config.php';
// ...
if ($debug) {
phpinfo();
}

如果config.php里万一写了$debug=true;那么就会造成phpinfo()被输出了,与实际预料就有了偏差了。那么怎么安全的include呢?

老王说:

$config = call_user_func(function() {
return include 'config.php';
});

这样就利用局部变量把里面的一些可能污染外部的变量控制在匿名函数的作用域里。

然后我自己也测试了一下,确实。即使用$GLOBALS,也没有影响,而单独的php文件,如果你不是function ,也不能直接global $debug;这样是语法错误的。

所以。。。。如果为了安全,你还是和老王学一下吧。。。

magento 后台为链接加上key

magento 后台为链接加上key

这年头,什么都不可靠。所幸,我还不需要用黄瓜。

在使用magento的时候,由于要使用ajax,但我又不想将这些链接加入到菜单里(加到菜单的链接会自动加入key参数),所以一直都是出现了自动跳转的问题。在这里面我要说一下。如果你使用原生的prototype.js,你用ajax的post功能时,即使不带key参数,也是可以正常处理的。如果是jquery..即使你带了key参数。也不能post,只能get。。。。苦逼
那么,如果在URL上加入key参数呢。大家都知道key参数是magento后台用来与前台分开的,里面带入了安全性的处理。可是怎么加上这个key呢?搜索了一下google居然没有一个文章是介绍这玩意的。当然也可能我的关键字不正确。
求人不如求已,打开代码,搜索key,很快就发现了Mage::getSingleton("adminhtml_url")这个玩意,里面有一个getUrl。。哇塞,可以直接生成带key的URL。看了一下。直接使用在程序里,确实生成了URL。并且可以通过浏览器访问。
记录一下
微信菜单的实现代码

微信菜单的实现代码

不多说,上代码。有几个要注意的(这段代码是我从我的系统里剥出来的。写了很久了,只是最近有人一直在问,我想,这也不是什么特别的代码,还是开源吧。)

1、请求微信菜单需要token,所以要事先生成。这个token其实可以用不少时间的,看官方接口(token调用生成,每天是有次数的,所以节约着用,能存本地就存本地吧,记录下过期时间即可)
2、子菜单的数组顺序就是微信的子菜单:从上至下的顺序 。这个不要搞错
3、子菜单是有数量限制的
4、主菜单是有字数限制的。不能超过4个中文字。
上代码吧:
<?php
/**
* @category menu.php
* @author gouki <gouki.xiao@gmail.com>
* @created 2013-10-16 15:14
* @since
*/
$menudata = array(
'button' => array(
array(
'name' => '菜单一',
'sub_button' => array(
array(
'type' => 'click',
'name' => '最上方子菜单1',
'key' => 'R:4:1',
),
array(
'type' => 'click',
'name' => '下方菜单',
'key' => 'R:3:1',
),
)
),
array(
'name' => '菜单2',
'sub_button' => array(
array(
'type' => 'click',
'name' => '第一个',
'key' => 'R:2:1',
),
array(
'type' => 'click',
'name' => '第二个',
'key' => 'R:8:1',
),
array(
'type' => 'click',
'name' => '第三个',
'key' => 'R:9:1',
),
array(
'type' => 'click',
'name' => '第四个',
'key' => 'R:10:1',
),
)
),
array(
'type' => 'click',
'name' => '菜单3',
'key' => 'M:7',
),
)
);
$menujson = json_encode($menudata, JSON_UNESCAPED_UNICODE);
define('APPKEY', ''); //这里是APPKEY
define('SECRET', ''); //SECRET ....
$getTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
$getTokenUrl = sprintf($getTokenUrl, APPKEY, SECRET);
$result = json_decode(file_get_contents($getTokenUrl), true);
$token = '';
if (isset($result['access_token'])) {
$token = $result['access_token'];
}
if (!$token) {
throw new Exception('token can not empty');
}

$createMenuUrl = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token=' . $token;
$opts = array(
'http' => array(
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencodedContent-Length: " . strlen($menujson) . "" . "Connection: keep-alive" . "Keep-Alive: " . 300 . "",
'content' => $menujson,
'timeout' => 120,
),
);

$context = stream_context_create($opts);
$response = json_decode(file_get_contents($createMenuUrl, false, $context),true);

if(!$response['errcode'] ){
echo "success";
}else{
echo $response['errmsg'];
}

echo "<pre>";
print_r($response);
echo "</pre>";
记得。如果返回成功,你又看不到效果,可以将公众号先取消关注,再加为关注,立刻就能看到效果了。
否则,你要等24小时左右 才能看到效果。
顺便,我也可以承接微信开发(请不要咨询我微信开发怎么做,网上教程很多。。。)
ltc监控脚本

ltc监控脚本

最近BTC和LTC很火啊。。但我又没有时间关注它,所以就写了个脚本,利用macosx 的notification来提醒。其实有很多办法,比如chrome的桌面提醒等。windows的messmage,邮件都OK

我这个只是最简单的版本,花了5分钟写完。。用了一些技术:
1、php
2、crontab
3、terminal-notifier (这个是ruby的,在mac下面安装也很简单,直接brew install terminal-notifier 就OK了)
上代码:
if (count($argv) == 1) {
echo "\nUse age:\n\n";
echo "php okcoin.php --ltc=100 --btc=500 \n\n";
echo "--ltc 代表参数,ltc和btc不分顺序\n\n";
exit;
}
array_shift($argv);
$params = array();
foreach ($argv as $val) {
if (strncasecmp($val, "--", 2) === 0) {
$p = explode("=", substr($val, 2));
if (!isset($p[1])) {
continue;
}
$params[trim($p[0])] = trim($p[1]);
}
}
if(!$params){
exit("参数不正确");
}
$api = "https://www.okcoin.com/ticker.do?random=" . rand(1, 9);
$data = @json_decode(file_get_contents($api), true);

$warning = array(
'btc' => 5000,
'ltc' => 150,
);

if ($data) { //证明是正常数据
$btcLast = $data['btcLast'];
$ltcLast = $data['ltcLast'];
if (isset($params['btc']) && $btcLast <= $params['btc']) {
exec("terminal-notifier -message '关注一下BTC,价格在指定价位附近了' -title 'BTC 警告'");
}
if (isset($params['ltc']) && $ltcLast <= $params['ltc']) {
exec("terminal-notifier -message '关注一下LTC,价格在指定价位附近了' -title 'LTC 警告'");
}
}
exit;
自用,只是够用就好
Magento 中为FORM添加自定义的判断

Magento 中为FORM添加自定义的判断

搜索了一些网上的教程,发现网上对于自定义的判断,都是说加个字段或者基于prototype来进行判断就可以了。什么require-text代表必填 之类的。这段时间在对magento做处理的时候发现这样的判断好痛苦(好象,我是指基于产品的FORM的扩展),如果是自己做二次开发,自己写表单,当然这样就OK了

翻看了一下程序,我开始看到资料就是指require-text之类的扩展,那我想,我只要能够加入一个class属性,我就可以基于这个class属性进行判断了,不是吗?

于是我找到了模版的layout,重新定义了layout,表示我可以引入一个指定的js文件,或者引入了代码。

然后,我找到了dispatchEvent,发现有一个事件,但我看了下,在这个事件里我几乎没有什么事情可以做【不过,基于这个事件,我把SKU,日期等做成了自动生成,只是他对表单的验证并没有什么帮助】

找了半天,最后发现数据库有一个backend_model,原来可以为这些程序指定事件,发现SKU有一个事情,是判断 sku的长度不能超过N个字符的,于是对着这个model进行了测试,发现果然OK。添加了几个model,然后update一下数据库,一切就完成了

Magento 中 后台 tag save的时候的BUG

Magento 中 后台 tag save的时候的BUG

magento的后台中在tag进行保存的时候做了一次处理,它原先的代码是:

if (isset($postData['tag_assigned_products']) ) {

意思是如果form提交的时候,有tag_assigned_products字段的话,会进行处理。然而,如果你当时的tag是inactive的时候,在tag of administrator的列表 中是看不到有tag的。这个时候你点保存。那么原来那些tagof administrator的tag就会全部被删除 了。怎么办?

只要你点击那个状态条,看到列表的时候,这个字段就有值了。这时候只有两种处理方式:

1、刷新页面重做处理

2、修改程序。

所以我改成了:

if (isset($postData['tag_assigned_products']) && $postData['tag_assigned_products']) {

OK,收工