Thursday, February 4, 2010

phpsh - Great tool for commandline PHP

Recently i've found great tool phpsh. It was developed by Facebook programmer Daniel <dancor> Corson. phpsh was written in Python for PHP (irony). It allow you to run PHP scripts in a shell. Yeah, you can say "Just install php5-cli and run php -a". When i have phpsh? No, thanks.

phpsh features i've been searching for:


  • php autocomplete of native and included functions and classes

  • php manual/description of native functions and classes

  • good exception handling (in php -a shell is exiting)

  • Color highlighting for output type

How to install phpsh on Ubuntu Karmic

First of all you need to have git and python installed:

sudo apt-get install git python python-setuptools


Next you need to fetch phpsh git repository on github:

git clone git://github.com/facebook/phpsh.git phpsh


Then build and install phpsh:

cd phpsh/
python2.6 setup.py build
sudo python2.6 setup.py install


During install procedure i've encountered a problem: setuptools was unable to get pysqlite, so i've installed those from Ubuntu repo:

sudo aptitude install python2.6-pysqlite2

and repeated step with install command

First look on phpsh

To see what i can do with phpsh i've used following simple code:

<?php
/**
 * Classs for testing phpsh
 * @author bumz
 *
 */
class Test
{
 /**
  * Variable holds all set foos
  * @var array
  */
 private $_foos = array();
 
 /**
  * General empty constructor
  * @return void
  */
 public function __construct()
 {
  
 }
 
 /**
  * General getter
  *
  * @param string $fooName
  * @return mixed
  * @throws Exception if foo isn't set
  */
 public function __get($fooName)
 {
  if (isset($this->_foos[$fooName])) {
   return $this->_foos[$fooName];
  } else {
   throw new Exception('This foo isn\'t set');
  }
 }
 
 /**
  * General setter
  *
  * @param string $fooName
  * @param mixed $fooValue
  * @return void
  */
 public function __set($fooName, $fooValue)
 {
  $this->_foos[$fooName] = $fooValue;
 }
 
 /**
  * General magic isset function
  *
  * @param string $fooName
  * @return bool
  */
 public function __isset($fooName)
 {
  return isset($this->_foos[$fooName]);
 }
 
 /**
  * General magic unset function
  *
  * @param string $fooName
  * @return bool
  * @throws Exception every time as foo can't be unset
  */
 public function __unset($fooName)
 {
  throw new Exception('You can\'t unset foos');
 }
 
 /**
  * Test of autocomplete
  * @return void
  */
 public function autocomplete()
 {
  echo 'autocomplete works';
 }
}


first of all you need to load phpsh itself and load Test class:

phpsh
phpsh> r '/home/bumz/workspace/test/testsh.php'


Then i've done few simple operations:

php> $class = new Test();

php> $class->foo1 = 'I\'m foo 1';

php> $class->foo2 = 'Foo 2 here';

php> echo $class->foo2;
Foo 2 here
php> echo isset($class->foo2);
1
php> unset($class->foo2);
Uncaught exception: Exception: You can't unset foos

php> $class->autocom
autocommit autocomplete
php> $class->autocomplete();


Now i know i will use this tool.

Friday, January 22, 2010

Firefox 3.6 released, and i want it on Ubuntu Karmic

As web develops very fast (and i take part into it :)), I prefer newest technologies on my workstation under Ubuntu. In Ubuntu main repositories there are no Firefox 3.6 yet, and i believe it will be available in Ubuntu Lucid. To add FF 3.6 to Ubuntu Karmic use

sudo add-apt-repository ppa:ubuntu-mozilla-daily/ppa
sudo apt-get update && sudo apt-get install firefox-3.6

After this you will then find Firefox 3.6 (Namoroka) under Applications > Internet > Namoroka Web Browser.

Wednesday, January 13, 2010

Audio output streaming on Ubuntu linux

Developers also have fun.

Usually developers who are listening to music listen it in their headphones, as not all people in office like music they are listening to. After couple of month all music on hard drive becomes annoying. And then internet radio becomes helpful. But if more and more programmers listen to radio through internet it would lag a lot. For web developers it's more annoying than music on HD. And they must listen to it. I have couple of co-workers who like to listen to one internet radio station. The big problem that it's going through Adobe Flash interface and i can't re-stream it. I've googled a lot to find how to broadcast audio stream through LAN. First option i've got is a PulseAudio Ubuntu native driver. But output on other machines was like on old cassette audio player. Music sometimes accelerates. So that was not a solution. Mix of other broadcasting tools does not help. And i remembered one of my classmates had broadcasted video through VLC.

Solution

To stream output channel you must have to install:

jackd
vlc
vlc-plugin-jack


Also you need to set up PulseAudio to play through Jack. Please refer this manual
After you have setup PulseAudio to play through jack and checked it's working you need to start qjackctl and start server if you are didn't set to start it automatically. Then start VLC with command:


vlc -vvv 'jack://channels=2:ports=.*' --sout '#transcode{acodec=mp3,ab=128,channels=2,samplerate=44100}:std{access=http,mux=raw,dst=192.168.1.3:8088}'
:sout=#transcode{acodec=mp3,ab=128,channels=2,samplerate=44100}:std{access=http,mux=raw,dst=0.0.0.0:8088}

Please note you need to set your IP address instead of 192.168.1.3
if you want vlc to start without an UI change vlc to cvlc

After this you can hear any audio on other machines by typing

mplayer http://192.168.1.3:8088


Finally you can write shell script to load all in one shot without interfaces

#!/bin/bash
pulseaudio -k
/usr/bin/jackd -t2000 -dalsa -dhw:0 -r48000 -p1024 -n2&
sleep 15
pulseaudio -DnF ~/.pulse/pulsejack.pa
cvlc -vvv 'jack://channels=2:ports=.*' --sout '#transcode{acodec=mp3,ab=128,channels=2,samplerate=44100}:std{access=http,mux=raw,dst=192.168.1.3:8088}' && pulseaudio -k && killall jackd; pulse-session

Please note you need to set your IP address instead of 192.168.1.3

Good music for you

Friday, December 18, 2009

PHPProxifier. PHP Proxy script.

Intro:

Recently team i'm currently working in received a task to make a proxy script that supports GET, POST, PUT, DELETE HTTP methods. All scripts we've found in network aren't supporting PUT and DELETE methods.
IMPORTANT this script has no fully-featured demo, sample etc. If you are don't know PHP please read PHP manuals before.

Idea:

  • Proxy should be as simple as possible.
  • Proxy should proxy one domain.
Proxy is a script that receives headers, data and pass it to proxied host. Then returns returned headers and data back

Steps of development:

First of all i remember my experience with Snoopy PHP library. It has really good functionality, but old code and code-style, and as we're have agile project with PHPUnderControl that checks it, we've decided that rewriting Snoopy library will consume a lot of time. Anyway i've rewritten it :) and soon it will be available.

Our project based on CodeIgniter has no such things also, but i remembered i saw REST service in Zend Framework and research/work begins.

First of all we don't want to use all of Zend Framework and i stripped out only classes we need:
  • Zend_Controller_Request*
  • Zend_Http_Client*
  • Zend_Http_Cookie
  • Zend_Http_CookieJar
  • Zend_Http_Response
  • Zend_Uri*
  • Zend_Validate_Hostname*
  • Zend_Validate_Ip

Second step was writing a proxy script. You need have php-cURL installed to use this script from scratch. All you need to run it in example mode: download from it's GIT repository

git@github.com:biozshock/PHPProxifier.git

if you are have what to proxy through this script please change $proxyingUrl variable. To enter in debug mode that shows headers, server variables, etc. you need to change $debug variable.

Third thing: to demonstrate feature is working i write script client.php to check all things are working nice. Also this script shows how to implement a page that will call proxy script. To use it you need to change $uri variable to point script to server.php script.
As i don't want to write fully-featured page with all HTTP methods as selector i've just commented out methods you are need to call to get responce.

Conclusion

If you are familiar with PHP and do not know Zend Framework you will easily read code and find out it's simple. If no - please go and read other PHP manuals please.