Short review for Nexus One + Verison Mifi

I’m using Nexus One without SIM card, and Mifi from Verison in a couple of weeks. I used to use iPhone 3G in Tokyo (sadly, it was SIM locked, I can’t use it in the US), comparing two but it’s hard to say which one is better. Mostly I use default applications like browser, email and maps. Nexus One’s maps is awesome. Especially just I was surprised its car navigation. Email app has a feature that iPhone doesn’t have, is adding a star label. For facebook and twitter, I prefer to use their mobile sites than native applications. Software keyboard is not great, many times I missed type. The shape, weight and feeling of touch are very good. Battery life is not bad. I don’t use it as a phone, at least I can use it a whole day. Monitor is beautiful. No crash, no freeze. It’s a small issue, but I don’t understand the meaning of projecting button. It seems like a Macbook’s sleep lamp, but I’ve never used it as a button/cursor.

Mifi provides wifi access point everywhere. It works for Nexus One and any laptop. I often work at coffee shop, so it’s pretty useful. A bit unstable when is in car/train (connect laptop directly it works well) or using for three machines. One thing I should mention is that 5GB/month is not enough for a heavy user. My usage of this month is already exceeded its limit, I’m a bit worry about extra charge. ($0.05/MB) A nice Verison guy said that never heard about someone who is charged overuse, but it seems I’ll be the one. I think 5GB limitation is pretty enough for usual customers, but for me, like use browsing, skype video chat, ssh, remote desktop, download some applications and also pandora, almost over 10 hours per every day, it’s not. Comparing to in Tokyo, Internet connection is extremely expensive and slow.

Posted in Think | Leave a comment

git post-receive-email with php using GMail SMTP

Hosting SMTP myself is a bit high cost. It’s a good solution to use GMail SMTP server.

I changed post-receive-email a bit.

send_mail()
{
#       if [ -n "$envelopesender" ]; then
#               /usr/sbin/sendmail -t -f "$envelopesender"
#       else
#               /usr/sbin/sendmail -t
#       fi
        /path/to/bin/sendmail.php
}

This file supposed to be on git remote server.
% /usr/local/git/my-repository/hooks/post-receive

Here is sendmail.php:

#!/usr/bin/php -d include_path=/usr/share/pear
<?php
//  % pear install Mail_mimeDecode
require_once "Mail.php";
require_once "Mail/mimeDecode.php";
 
$from = "Agent Smith <bot@sample.com>";
$to   = "Git <your-gmail-account@gmail.com>";
 
$original = file_get_contents('php://stdin'); // will receive data via hooks/post-receive
$decoder = new Mail_mimeDecode($original);
$mail = $decoder->decode(array('include_bodies' => true, 'decode_bodies' => true, 'decode_headers' => true));
 
$host = "tls://smtp.gmail.com";
$port = 465;
$username = "your-gmail-address@gmail.com";// Google Apps also works
$password = "***your password***";
 
$headers = array(
    'From' => $from,
    'To' => $to,
    'Subject' => $mail->headers['subject']);
$smtp = Mail::factory('smtp',
    array ('host' => $host,
    'port' => $port,
    'auth' => true,
    'username' => $username,
    'password' => $password));
 
$mail = $smtp->send($to, $headers, $mail->body);
 
if (PEAR::isError($mail)) {
    echo $mail->getMessage(), PHP_EOL;
} else {
    echo 'Message successfully sent!', PHP_EOL;
}

Then you will receive an email from GMail when someone do ‘git push’. It’s working on CentOS 5.3 + PHP5.2.10 + git 1.6.4.4.

Posted in Uncategorized | Leave a comment

Get error message “G is null” or “a is null” on jQuery

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    var obj = null;
    // ...maybe there are complexed lines...
    $.each(obj, function(){
    });
});
</script>

Will get an error message “a is null”. When using jquery.min.js, it might be hard to recognize what kind of error it is. On version 1.3.2 the message says “G is null”.

Posted in Tweak | Tagged , | Leave a comment

Bracket auto completion in vim for PHPer

I used to use this setting in .vimrc:

inoremap \( (
inoremap ( ()<LEFT>

But escape bracket every time for

second\(first();

is a little bit stressful. (typed “first()”, then move to the head of the line, typing “second”, back slash and bracket)

Here is an improved script:

function! CompleteDetachedBracket()
    let line = line('.')
    let col = col('.')
    let c = getline(".")[col(".")]
    execute "normal \<RIGHT>%"
    if c == "$"
        execute "normal 2wi)"
    elseif line != line('.') || col != col('.')
        execute "normal a)"
    else
        execute "normal a)"
        startinsert
    endif
endfunction
inoremap ( (<ESC>:call CompleteDetachedBracket()<CR>

This is the first time to code vim script for me. I was guessing vim script is a weird language, so have avoided long time. But at least coding small one is not so difficult than I thought. Documentation is very organized. There are many sample codes in your .vim/plugin directory. Not like a GUI programming, but still like a Tetris. It maybe just because of the first time, I think it’s pretty fun.

Posted in Create | Tagged | Leave a comment

Convert multiple object to array in PHP

For simple object, just use cast syntax should be fine.

$array = (array)$object;

However, this way doesn’t work for complexed object (or array+object combination) like this:

array(2) {
  [0]=>
  object(stdClass)#1 (1) {
    ["id"]=>
    int(0)
  }
  [1]=>
  object(stdClass)#2 (1) {
    ["id"]=>
    int(1)
  }
}

Then, simply this one liner works well.

$multipleArray = array_map('get_object_vars', $complexedObject);

Since I’m getting use more json_decode(), I get “PHP Fatal error: Cannot use object of type stdClass as array” sometimes. Actually in this context, to use the second parameter of json_decode is the simplest way.

$it_will_be_array = json_decode($json, true);
Posted in Tweak | Tagged | Leave a comment

Complete working sample for Services_Twitter OAuth in PHP PEAR

% pear install --alldeps Services_Twitter-alpha

You can get some sample codes for Services_Twitter with OAuth from php doc, but it’s not a complete code. I rewrite some parts.

<?php
if (isset($_GET['clear'])) { // sometimes you need to clear session, just access ?clear=1
    session_start();
    $_SESSION = array();
    session_destroy();
    die('session has been cleared.');
}
 
 
define('CONSUMER_KEY', '***');
define('CONSUMER_SECRET', '***');
 
 
require_once 'Services/Twitter.php';
require_once 'HTTP/OAuth/Consumer.php';
 
try {
    session_start();
 
    if (isset($_SESSION['token2'], $_SESSION['token_secret2'])) {
        $twitter = new Services_Twitter;
        $oauth = new HTTP_OAuth_Consumer(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['token2'], $_SESSION['token_secret2']);
        $twitter->setOAuth($oauth);
        print_r($twitter->statuses->home_timeline());
 
    } else if (isset($_SESSION['token'], $_SESSION['token_secret'], $_GET['oauth_verifier'])) {
        $twitter = new Services_Twitter;
        $oauth = new HTTP_OAuth_Consumer(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['token'], $_SESSION['token_secret']);
        $oauth->getAccessToken('http://twitter.com/oauth/access_token', $_GET['oauth_verifier']);
        $twitter->setOAuth($oauth);
 
        $_SESSION['token2'] = $oauth->getToken();
        $_SESSION['token_secret2'] = $oauth->getTokenSecret();
 
        $msg = $twitter->statuses->update("I'm coding with PEAR right now!");
        print_r($msg);
 
    } else {
        $oauth = new HTTP_OAuth_Consumer(CONSUMER_KEY, CONSUMER_SECRET);
        $oauth->getRequestToken('http://twitter.com/oauth/request_token', 'http://zuzara.com/twitter_oauth.php'); // callback URL should be the same as that you registered at http://twitter.com/apps/new
 
        $_SESSION['token']        = $oauth->getToken();
        $_SESSION['token_secret'] = $oauth->getTokenSecret();
 
        $url = $oauth->getAuthorizeUrl('http://twitter.com/oauth/authorize');
 
        http_redirect($url); // function from pecl_http
    }
 
} catch (Exception $e) {
    echo $e->getMessage();
}
Posted in Tweak | Tagged , , , | Leave a comment

Compiling mongo php driver on pecl for MAMP

MAMP 1.8.2 includes pecl but actually it doesn’t work. When trying to “pecl install mongo”, there will be an error.
grep: /Applications/MAMP/bin/php5/include/php/main/php.h: No such file or directory

So, I downloaded source tar ball of PHP 5.2.10 (same version as MAMP’s PHP) and configure && make && make install. Then $prefix/include directory will be created. Just copy them into /Application/MAMP/bin/php5/. It might be better to set prefix like this “./configure –prefix=/opt/apps/php-5.2.10″

Now pecl works well. You can get version 1.0.2 right now.

BTW, there is a link for a compiled .so file (http://osdir.com/ml/mongodb-user/2009-11/msg00463.html) but it’s old (1.0.0+) that means “$connection->database” syntax doesn’t work well.
Notice: Undefined property: Mongo::$database in /Users/nobu/Sites/mongo.php on line 4

Posted in Tweak | Tagged , , | 1 Comment

Load remote CSS file dynamically with jQuery

These codes are for dynamic loading for remote CSS. It’s easy to load all CSS files from the beginning or just append link element (ref. Switch stylesheets with jQuery). But it rarely needs to load a file dynamically and wait until CSS file has been loaded completely.

test.html:

<html>
<head>
<title>css.html</title>
<script src="/jquery-1.3.2.js"></script>
<script>
$(document).ready(function()
{
     // if it's a local file just using $.get() should be fine
    $.getJSON('http://remote-server/css.php?callback=?', function(data){
        //$('head').append('<style>');
        //$('head > style:last').html(data); // it doesn't work on IE
        $('body').append('<style>' + data + '</style>');
        //  edit dom tree. these elements need new properties of stylesheet
        $('body').append('<div id="something-special"><h1>Hello World!</h1></div>');
    });
});
</script>
</head>
<body>
</body>
</html>

css.php:

<?php
ob_start();
?>
 
div#something-special h1 {
    color: red;
}
 
<?php
sleep(10); // it's for test
if (isset($_GET['callback'])) {
    $buf = ob_get_contents();
    ob_clean();
    echo filter_var($_GET['callback'], FILTER_SANITIZE_STRING), '(', json_encode($buf), ')';
}
Posted in Tweak | Tagged , , | Leave a comment

Catch Up On The News Mashup For Social Media

“Catch Up On The News” is a mashup for social news media such as digg, tweetmeme, techmeme and delicious. It is showing top 20 popular articles from yesterday, last week or whenever you can specify.

Do you have an experience that after finishing a vacation, would like to read a week’s news all at one time? This is what you’re looking for.
catchup_screenshot

Posted in Create | Tagged , | 1 Comment

Call a method of object with string in JavaScript

var call_user_func = function(obj, method) {
    return eval('obj.' + method).apply(obj, Array.prototype.slice.call(arguments, 2));
}
var test1 = {
    method1: function(arg1, arg2, arg3) {
        return alert(arg1 + arg2 + arg3);
    }
}
call_user_func(test1, 'method1', 1, 2, 3);

It works on Firefox3, Safari4, IE6.

Posted in Uncategorized | Leave a comment
  • About

    I'm Nobu, a web developer living in Mountain View, CA. Feel free to comment on any posts. I'm also tweeting on twitter.

  • Archives

  • Recent Posts