↓ Archives ↓

Posts Tagged → php

Two patches for Lithium (li3)

First, added method _toBoolean to Lithium\data\source\database\adapter\MySql:

    protected function _toBoolean($value) {
        return (int) parent::_toBoolean($value);
    }

Now Lithium treat tinyint(1) in MySQL as boolean type, but when you insert/update with a field has been defined as tinyint(1), Lithium will try insert boolean type, then an SQL error will throws.

The second patch is for Lithium\data\source\Database. Try this:

class User extends Lithium\data\Model {}

$user = User::create();
$user->name = 'Leechael';
$user->email = 'blah@blah.blah';
$user->blah = 'Field that NOT exists in schema';
$user->save();

Than you will got an SQL error, said unknown column `blah`. The solution is add a few lines in method Database::create() and Database::update(). First one, go to line 190:

while (list($field, $value) = each($data['fields'])) { // This line is what we looking for!
    // Insert following block!
    if (!isset($schema[$field])) {
        continue;
    }
// other codes ....

Second, go to line line 274 (after previous patched):

while (list($field, $value) = each($data['fields'])) { // You looking for this!
    if (!isset($schema[$field])) {
        continue;
    }
// other codes ....

Done.

Setup a helper for Lithium (a.k.a. li3)

Save your helper class under app/extensions/helper/, like Navigator.php, than calls $this->Navigator in your template.

It’s fucking easy but leaks document. Hope this helps you.

PHP Snippet: ip4_in_range

ip 范围检测函数,老生常谈了,贴一下自己造的轮子:

function ip4_in_range ($ip, $start, $end = null) {
    if (func_num_args() === 2) {
        if (strpos($start, '*') !== false) {
            $end = str_replace('*', '255', $start);
            $start = str_replace('*', 0, $start);
        } elseif (strpos($start, '/') !== false) {
            $ip_dec = ip2long($ip);
            list($range, $netmask) = explode('/', $start);
            $netmask_dec = ~ (pow(2, (32 - intval($netmask))) - 1);
            $range_dec = ip2long($range);
            return (($ip_dec & $netmask_dec) === ($range_dec & $netmask_dec));
        } else {
            trigger_error('ip4_in_range: Parameter $start maybe in invalid format.');
            return false;
        }
    }
    extract(array_map(function ($ip) {
        return (float) sprintf("%u", ip2long($ip));
    }, compact('ip', 'start', 'end')));
    if ($start > $end) {
        list($start, $end) = array($end, $start);
    }
    return ($ip >= $start && $ip <= $end);
}

只简单的做了几个小测试,都通过了。用法:

  • ip4_in_range('10.0.1.13', '10.0.1.0/24');
  • ip4_in_range('10.0.1.13', '10.0.1.*');
  • ip4_in_range('10.0.1.13', '10.0.1.10', '10.0.1.20');

z3n: web based checklist

early Sreenshot of z3n

z3n 是什么呢?

z3n 是一个可以用作 checklist / to-do list 的小工具。它需要 PHP 5.3.0 或以上版本才能运行。对于 Windows 用户来说,下载一个 WAMP5,再进行一些简单的设置即可运行。这个小工具并不建议于公共空间中运行——因为这是仅是一个小工具,没有任何对校验用户身份的代码。

z3n 的安装十分简单,在 MySQL 中创建一个数据库,然后在导入 z3n.init.sql 进行数据库的初始化;这些操作你都可以通过 PHPMyAdmin 完成。然后把 core 目录下的 config.example.php 重命名为 config.php,然后填写相应项进行设置。对于本地运行、例如利用 WAMP5 简单快速部署 z3n 的朋友,只需设置 Z3N_MYSQL_USERNAME,Z3N_MYSQL_PASSWORD,Z3N_MYSQL_SCHEMATA 这三个值就可以了。如果你觉得页面显示 Leechael 的这样的文字很碍眼,你可以把 Z3N_USERNAME 更改为你想要的字眼,例如 “Simpson” 或者 “Mr. Potatohead”;中文也是可以的,只要记得该文件是使用 UTF-8 编码格式保存即可。另一个设置是 Z3N_BACKGROUND_IMAGE,我在压缩包里面放了四张背景图片,根据喜好把图片名字替换 Z3N_BACKGROUND_IMAGE 的值即可。

输入条目时,可以通过按下 enter 保存;如果按下的是 ctrl + enter,则是在保存当前条目的同时创建一个新的条目,以保证你整个输入流程是连续的。

Z3N 是基于 BSD 协议发布,请随意使用。下载地址:http://yan-yan.info/files/z3n.tar.gz

Z3N 的原型是 @Livid 旧作 Project-Babel 中的 zen,一个很好用的小工具,但我们并不需要 Project-Babel 完整的功能,所以, Z3N 诞生了。

这是一个利用空余时间制作的小东西,没有进行严谨的调试,如果你在使用中出现莫名其妙的情况,请留言告知。

对了,你需要一个多人可协作版本的 Z3N 吗? ;-)