Nolan's Blog

a PHP JS UI APP developer

PHP

Java new byte[16] same as str_repeat(“\0”, 16) in PHP

Java new byte[16] same as str_repeat(“\0”, 16) in PHP!



class CryptAES
{
    protected $cipher = MCRYPT_RIJNDAEL_128;
    protected $mode   = MCRYPT_MODE_CBC;
    protected $pad_method = NULL;
    protected $secret_key = '';
    protected $iv = '';

    public function set_cipher($cipher)
    {
        $this -> cipher = $cipher;
    }

    public function set_mode($mode)
    {
        $this -> mode = $mode;
    }

    public function set_iv($iv)
    {
        $this -> iv = $iv;
    }

    public function set_key($key)
    {
        $this -> secret_key = $key;
    }

    public function require_pkcs5()
    {
        $this -> pad_method = 'pkcs5';
    }

    protected function pad_or_unpad($str, $ext)
    {
        if ( is_null($this -> pad_method) )
        {
            return $str;
        }
        else
        {
            $func_name = __CLASS__ . '::' . $this->pad_method . '_' . $ext . 'pad';
            if ( is_callable($func_name) )
            {
                $size = mcrypt_get_block_size($this->cipher, $this->mode);
                return call_user_func($func_name, $str, $size);
            }
        }
        return $str;
    }

    protected function pad($str)
    {
        return $this->pad_or_unpad($str, '');
    }

    protected function unpad($str)
    {
        return $this->pad_or_unpad($str, 'un');
    }

    public function encrypt($str)
    {
        $str = $this -> pad($str);
        $td = mcrypt_module_open($this->cipher, '', $this->mode, '');

        if ( empty($this->iv) )
        {
            //$iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
            $iv = str_repeat("\0", mcrypt_enc_get_iv_size($td));                // < << like here
        }
        else
        {
            $iv = $this->iv;
        }

        mcrypt_generic_init($td, $this->secret_key, $iv);
        $cyper_text = mcrypt_generic($td, $str);
        $rt = base64_encode($cyper_text);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);

        return $rt;
    }

    public function decrypt($str)
    {
        $td = mcrypt_module_open($this->cipher, '', $this->mode, '');

        if ( empty($this->iv) )
        {
            //$iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
            $iv = str_repeat("\0", mcrypt_enc_get_iv_size($td));
        }
        else
        {
            $iv = $this->iv;
        }

        mcrypt_generic_init($td, $this->secret_key, $iv);
        $decrypted_text = mdecrypt_generic($td, base64_decode($str));
        $rt = $decrypted_text;
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);

        return $this -> unpad($rt);
    }

    public static function pkcs5_pad($text, $blocksize)
    {
        $pad = $blocksize - (strlen($text) % $blocksize);
        $text .= str_repeat(chr($pad), $pad);
        return $text;
    }

    public static function pkcs5_unpad($text)
    {
        $pad = ord($text{strlen($text) - 1});
        if ($pad > strlen($text)) return false;
        if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return false;
        return substr($text, 0, -1 * $pad);
    }

}

继续阅读》

, , ,

PHP Socket with multiple clients


error_reporting(E_ALL);

/* Allow the script to hang around waiting for connections. */
set_time_limit(0);

/* Turn on implicit output flushing so we see what we're getting as it comes in. */
ob_implicit_flush();

$address = '127.0.0.1';

$port = 10000;

// create a streaming socket, of type TCP/IP
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1);

socket_bind($sock, $address, $port);

socket_listen($sock);

// create a list of all the clients that will be connected to us..
// add the listening socket to this list
$clients = array($sock);

while (true)
{
    // create a copy, so $clients doesn't get modified by socket_select()
    $read = $clients;
    $write = null;
    $except = null;

    // get a list of all the clients that have data to be read from
    // if there are no clients with data, go to next iteration
    if (socket_select($read, $write, $except, 0) < 1)
        continue;

    // check if there is a client trying to connect
    if (in_array($sock, $read))
    {
        $clients[] = $newsock = socket_accept($sock);

        socket_write($newsock, "There are ".(count($clients) - 1)." client(s) connected to the server\n");

        socket_getpeername($newsock, $ip, $port);
        echo "New client connected: {$ip}\n";

        $key = array_search($sock, $read);
        unset($read[$key]);
    }

    // loop through all the clients that have data to read from
    foreach ($read as $read_sock)
    {
        // read until newline or 1024 bytes
        // socket_read while show errors when the client is disconnected, so silence the error messages
        $data = @socket_read($read_sock, 4096, PHP_BINARY_READ);

        // check if the client is disconnected
        if ($data === false)
        {
            // remove client for $clients array
            $key = array_search($read_sock, $clients);
            unset($clients[$key]);
            echo "client disconnected.\n";
            continue;
        }

        $data = trim($data);

        if (!empty($data))
        {
            echo " send {$data}\n";

            // do sth..

            // send some message to listening socket
            socket_write($read_sock, $send_data);

            // send this to all the clients in the $clients array (except the first one, which is a listening socket)
            foreach ($clients as $send_sock)
            {
                if ($send_sock == $sock)
                    continue;

                socket_write($send_sock, $data);

            } // end of broadcast foreach
        }

    } // end of reading foreach
}

// close the listening socket
socket_close($sock);

继续阅读》

,

CentOS 6.5 PHP Update to 5.4 5.5 5.6

A best rpm:
rpm -Uvh http://mirror.webtatic.com/yum/el6/latest.rpm

yum install php(54w/55w/56w) php55w-bcmath php55w-cli php55w-common php55w-devel php55w-fpm php55w-gd php55w-imap php55w-ldap php55w-mbstring php55w-mcrypt php55w-mysql php55w-odbc php55w-pdo php55w-pear php55w-pecl-igbinary php55w-xml php55w-xmlrpc php55w-opcache php55w-intl php55w-pecl-memcache

继续阅读》

Use php curl to send/get a https request with pem/p12/cert

Sometimes we must use cert file to visit site or api.
If you only have .p12 file, please use openssl to rebuild a pem file.


$ch = curl_init();

curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_URL, 'https://api.xxxxx.com:443/');
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch,CURLOPT_SSLCERT, '/common/client.pem');
curl_setopt($ch,CURLOPT_CAINFO, '/common/server.cer');

curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($params));
$data = curl_exec($ch);
curl_close($ch);

var_dump($data);

继续阅读》

, , ,

My New WordPress Plugin – wp_upload_rename

My New WordPress Plugin – wp_upload_rename

Plugin : https://wordpress.org/plugins/wp-upload-rename/
Github : https://github.com/page7/wp_upload_rename

wp_upload_rename

Sometimes we forgot change our file to a available name. So the plugin can help you auto change your upload file.
I will add a filetype option of setting in next version.
And a rename popup before the file upload if you change setting in future, default value is post_name.

继续阅读》

, , ,

PHP 与C# Soap通信(Web Services)

(╯-_-)╯╧╧
一上来掀桌要闹哪样…嘛,其实跟SOAP无关, 而是C#
C# 提供的 Web Services 方案坑死了多少非 C# 的程序员不得而知.. 只是觉得这个好麻烦啊好麻烦…
基础知识请自行百科.

(还有很多百度的大神们, 注意你们的文章是否是针对Web Services的, 它与SOAP是完全不一样啊!

简单地说, 一个客户端的 Web Client 要请求 Web Service 仅仅是通过 XML+SOAP 去完成通信.
但在开发的时候, 就需要另一个东西对该 Web Service 的功能/参数等等进行约束.
这就是 WSDL. WSDL 通常是由一个 UDDI 发布端进行更新或维护.
(至于WSDL变更后是否会验证, 就请各位自行测试了.
也就是说, 作为PHP端, 既要作为一个 服务管理者 又要做一个 数据提供者. 以上.

继续阅读》

, , ,

PHP 一个蹲墙角的函数 error_log

其实应该昨天一起发的..(嘛~ 今天这一贴全当混过去吧.. ♪(^∇^*)
认识的同学应该知道这货干啥用的, 不认识的也应该去翻手册了吧?

那功能就不多说了.主要是讨论一下,为什么很多人没有使用它…

例如 CI/YII等等log.class都会用以下代码


flock($fp, LOCK_EX);
fwrite($fp, $message);
flock($fp, LOCK_UN);
fclose($fp);

嘛,这样写是给文件一文件锁,防止多进程修改.那 error_log 是否有这个功能呢?

情况这里 PHP_bug#40897
看到了么?早在5.2的时候就已经给予修复.所以大家大可放心使用.
所以经常关注升级说明是很有必要的呀! 但是升级说明我倒是没找到…(啊咧咧…

继续阅读》

, , , ,

PHP 关于异常和错误的解释

我想很多PHPer跟我一样,都在混乱的使用die throw 甚至是 exit 来解决流程中的异常

在Pt-framework的开发过程中,需要借鉴很多的框架.嘛,大家对错误异常的处理基本也都百家齐放的状态.
为什么会出现这样的问题? 可能是php对于错误的处理文档相当至少的原因吧.

1.首先让我们看一下原有错误处理函数 set_error_handler

该函数功能是自定义错误函数.通过定义的函数来处理错误等级包括: E_ERROR,E_WARNING,E_NOTICE,E_STRICT,E_USER_ERROR,E_USER_WARNING…等
有了错误级别,然后做什么呢? log/debug/trace 之类..

嘛,那这样看好像很简单.如何触发呢?

继续阅读》

, , , , , , ,

Powered by WordPress.org.