• Subscribe to the RSS feed!
  • Subscribe by Email
  • home
  • blog
  • dev
  • Recent Posts

    • Xdebug is full of awesome
    • Creating a chat bot with PHP and Dbus
    • A year in review: 2011
    • Notes on shell scripting
    • Listening to Dbus signals with PHP
    • Configuring 2 monitors with xrandr
    • A quick note on Dojo’s data grids and dojox.data.HtmlStore
    • Communicating with Pidgin from PHP via D-Bus
    • Upgrading to Fedora 16
    • Contributing to Zend Framework 2
  • Recent Comments

    • Creating a chat bot with PHP and Dbus ~ Robert Basic on Communicating with Pidgin from PHP via D-Bus
    • A year in review: 2011 ~ Robert Basic on Announcing Hex
    • Anon on A quick note on Dojo’s data grids and dojox.data.HtmlStore
    • James on Communicating with Pidgin from PHP via D-Bus
    • A Zend Framework 2 EventManager use case ~ Robert Basic « Bookmarks on A Zend Framework 2 EventManager use case
    • Zend_Auth | Kerek egy ég alatt on Login example with Zend_Auth
    • Jowee on A Zend Framework 2 EventManager use case
    • Jurian Sluiman on A Zend Framework 2 EventManager use case
    • Robert on A Zend Framework 2 EventManager use case
    • Jurian Sluiman on A Zend Framework 2 EventManager use case
  • Tags

    about apache ape blog book comic community conference contributing dbus dojo events example facebook framework hack introduction lamp linux me mysql netbeans open source php pidgin plugin pyqt python random registration review script security setup shell signals site svn talk ubuntu web wordpress xdebug zend zend framework
  • Categories

    • Blablabla
    • Development
    • Free time
    • Places on the web
    • Programming
    • Software
  • Archives

    • January 2012
    • December 2011
    • November 2011
    • October 2011
    • September 2011
    • August 2011
    • July 2011
    • May 2011
    • April 2011
    • March 2011
    • January 2011
    • December 2010
    • November 2010
    • October 2010
    • July 2010
    • June 2010
    • April 2010
    • February 2010
    • January 2010
    • December 2009
    • November 2009
    • October 2009
    • August 2009
    • May 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008
  • Find me on

    • DZone
    • Google Code
    • Google Reader
    • Last.fm
    • StumbleUpon
    • Twitter
    • Vimeo
  • Friends and Blogs

    • Andrew Taylor
    • Andy Sowards
    • Bojan Pejić
    • Eran Galperin
    • Graham Smith
    • Jani Hartikainen
    • Jasper Tandy
    • Matthew Turland
    • Matthew Weier O’Phinney
    • Miff
    • Miloš Ćuković
    • Nebojša Radović
    • Nemanja Avramović
    • Nemanja Tobić
    • Nikola Krajačić
    • Nikola Plejić
    • Pádraic Brady
    • Rob Allen
    • Swizec Teller
    • Vladimir Stanković
    • WeAreJustCreative
    • Željko Stevanović
  • I use

    • 960 Grid System
    • jQuery
    • Notepad++
    • Subversion
    • Trac
    • Vim
    • Zend Framework

Archive for October, 2008

Project Euler

by Robert Basic on October 27th, 2008

A few days ago, I found a really great place, full of excellent math and programming problems: Project Euler.

Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems.

I was long looking for this kind of place, to get my brain do some serious thinking :) It’s a great way to improve logic and math/programming skills.

The problems can be solved by using any programming language, so as I just started learning Python, thought that this is a great way to start with it. As I suck more in math than in programming, my solutions are brute-forced, but I always go back to the explanation of the problem (can be viewed only after you gave the right answer to the problem), to understand the math behind the problem.

There are currently 214 problems and I’m on number 6 right now. If you into this kind of stuff, be sure to check it out, it’s a lot of fun :)

Happy hacking!

Tags: euler, project, site.
Categories: Free time, Places on the web, Programming.
Comments: Comments Off.

A Zend_Captcha example

by Robert Basic on October 22nd, 2008

Update: I made an error in the example code, regarding the CAPTCHA image URL. I’m sorry for any troubles caused by this mistake.

Update #2: Here’s an example of using Zend_Captcha without the whole Zend Framework stuff.

Update #3: There was an unintentional error in the captchaAction() method, Adam warned me about it in the comments. The error is fixed now. Thanks Adam.

OK, this was a bit tricky and I found no examples about it, so I thought to blog it. I’ll just show a quick example how to implement Zend_Captcha into a Zend_Form, may be useful for someone. There are several CAPTCHA types in ZF, like the Image, Figlet and Dumb. I use Image.

First of all, we’ll use sessions, so we need to change the bootstrap file a little:

// Put this line somewhere after the Zend_Loader::registerAutoload(); line
Zend_Session::start();

We need to start the session to use it, putting it close to the top will assure that there will be no “Headers already sent by…” errors caused by a wrongly placed session start.

Next we need a folder which has a 777 permission on it (Windows users, you can skip this… Or start using GNU/Linux) where we will put our captcha images for a while… This folder must be in the public folder somewhere. So create one.

How does this work? When a captcha is generated, it generates a unique ID (e.g. 539e517b0c0f4e32ef634dae92f07f77) and the word on the image. That unique ID is used for the file name of the image and for the session namespace (the namespace is like: Zend_Form_Captcha_uniqueId), so it knows which image belongs to which session. Also, the generated word is placed inside it’s own session. That ID is placed on the form in a hidden field, so when the submission is received, we can access the ID and recreate the correct session namespace and access the data in it: the word on the image.

Awesome. Now, to the fun part. I use the Zend_Form_Element_Captcha class, so no additional fooling around is needed to put the captcha in the form. Here’s the code:

public function indexAction()
{
// Our form object...
$form = new Zend_Form();
// And here's our captcha object...
$captcha = new Zend_Form_Element_Captcha(
        'captcha', // This is the name of the input field
        array('label' => 'Write the chars to the field',
        'captcha' => array( // Here comes the magic...
        // First the type...
        'captcha' => 'Image',
        // Length of the word...
        'wordLen' => 6,
        // Captcha timeout, 5 mins
        'timeout' => 300,
        // What font to use...
        'font' => '/path/to/font/FontName.ttf',
        // Where to put the image
        'imgDir' => '/var/www/project/public/captcha/',
        // URL to the images
        // This was bogus, here's how it should be... Sorry again :S
        'imgUrl' => 'http://project.com/captcha/',
)));
// Add the captcha element to the form...
$form->setAction('/index/captcha/')
        ->setMethod('post')
        // Add the captcha to the form...
        ->addElement($captcha)
        ->addElement('submit','Submit')
// Pass the form to the view...
$this->view->form = $form;
}

On the other side, it goes something like this:

public function captchaAction()
{
  $request = $this->getRequest();
  // Get out from the $_POST array the captcha part...
  $captcha = $request->getPost('captcha');
  // Actually it's an array, so both the ID and the submitted word
  // is in it with the corresponding keys
  // So here's the ID...
  $captchaId = $captcha['id'];
  // And here's the user submitted word...
  $captchaInput = $captcha['input'];
  // We are accessing the session with the corresponding namespace
  // Try overwriting this, hah!
  $captchaSession = new Zend_Session_Namespace('Zend_Form_Captcha_'.$captchaId);
  // To access what's inside the session, we need the Iterator
  // So we get one...
  $captchaIterator = $captchaSession->getIterator();
  // And here's the correct word which is on the image...

  $captchaWord = $captchaIterator['word']
  // Now just compare them...
  if($captchaInput == $captchaWord)
  {
  // OK
  }
  else
  {
  // NOK
  }
}

Easy, ain’t it?

Happy hacking :)

Tip: Using a monospace or a serif font for the words on the image (like FreeMono.ttf found by default on Ubuntu), makes the word quite unreadable — with the FreeMono.ttf about 8 out of 10 is UNreadable — so use a sans-serif font.

Tags: captcha, example, framework, php, zend.
Categories: Development, Programming, Software.
Comments: 35.

Starting with Zend Framework – part 2

by Robert Basic on October 20th, 2008

This post is the second part of my introductory text on Zend Framework, Starting with Zend Framework. This time I cover the basics about controllers, actions, view scripts and view helpers. On request routing and the Front Controller I will write one (or more) big post(s), so this part won’t be explained now. I will also skip explaining the models; they deserve their own post :)

If anyone is into writing a guest-post on models, let me know!

The Controllers

The Controllers are the heart of every MVC based application. They control the execution of the application, what to do with the data, what to show the user, what to write to the database, etc. The Controllers that you will write all the time, are called Action Controllers. These Controllers subclass the Zend_Controller_Action abstract class. Every application module must have a default Controller, which will be accessed if no specific Controller is requested. The default name for this default Controller is Index. Examples of the IndexController and FooController:

<?php

// The IndexController class must be placed in the controllers folder
// and saved as IndexController.php
class IndexController extends Zend_Controller_Action
{
    public function init()
    {
    }

    public function indexAction()
    {
    }
}

// The FooController class must be placed in the controllers folder
// and saved as FooController.php
class FooController extends Zend_Controller_Action
{
    public function init()
    {
    }

    public function indexAction()
    {
    }

    public function barAction()
    {
    }

    public function someRandomFunctionDoingSomeFunkyStuff()
    {
    }
}

Continue reading this post…

Tags: example, framework, introduction, php, zend.
Categories: Development, Programming, Software.
Comments: 5.

Ubuntu as a dev machine

by Robert Basic on October 15th, 2008

This post is more of a note to myself, ’cause I keep forgetting all these Linux commands, and spend hours setting up stuff right…

I’m installing Ubuntu 8.04 on VirtualBox, with windows xp as the host machine. I must do it this way, because my wireless card is having some problems with Linux, something with the drivers. The possible solution includes kernel compiling — thanks, but no thanks.

Anyway… The installation itself is no trouble, so I’ll skip that. I always keep the apt-cache from previous installations, sparing hours of updating the system… On the host I have a folder that I share between the host OS and the client OS and first I need to reach that folder, to get from it the apt-cache.

First, need to install the Guest Additions. In Virtualbox go to Devices —> Install Guest Additions. In the console run:

sudo /media/cdrom/VBoxLinuxAdditions.run

After it’s finished, we need to mount the shared folder:

sudo mount -t vboxsf name_of_the_sharing_folder /path/to/mount_point

Now, for me, this command shows some error. Here’s what I have to do:

sudo modprobe vboxfs
sudo mount -t vboxsf name_of_the_sharing_folder /path/to/mount_point

Something with some modules not being loaded into the kernel, not bothered with it really… Now I can copy the apt-cache to where it needs to be:

sudo cp -r /path/to/mount_point/apt-cache /var/cache/apt/archives

Now do the system update. If the system update includes a kernel update, you’ll have to install Guest Additions once more…

Next installing the LAMP:

sudo apt-get install apache2
sudo apt-get install php5 libapache2-mod-php5
sudo /etc/init.d/apache2 restart
sudo apt-get install mysql-server
sudo apt-get install libapache2-mod-auth-mysql php5-mysql phpmyadmin
sudo /etc/init.d/apache2 restart
sudo a2enmod rewrite
sudo /etc/init.d/apache2 restart

That should do it. But hey! mod_rewrite still doesn’t work!

sudo gvim /etc/apache2/sites-available/default

And change AllowOverride None to AllowOverride All.

There. I have a basic LAMP on Ubuntu under VirtualBox. I made a few snapshots of the VirtualBox image, in case I trash it (which probably will happen soon), so I don’t need to reinstall over again.

Now, I’m of to setup SVN…

Tags: apache, lamp, linux, mysql, php, setup, ubuntu, virtualbox.
Categories: Development, Software.
Comments: 5.

Optimizing MySQL and a Zend_Db_Profiler example

by Robert Basic on October 13th, 2008

Last night I came across on a post on TechFounder, about using Zend_Db_Profiler and a good example with real data on optimizing MySQL queries. For “geeks” who SQL speak fluently this will probably be no new stuff, but for great number of web developers (me included!) this will probably come in handy.

Cheers!

Tags: framework, mysql, optimizing, profiler, zend.
Categories: Development, Places on the web.
Comments: 2.
12 » Last
Robert Basic © 2008 — 2012
Design & graphics by: Livia Radvanski — Lady L.
Coded by: Robert Basic
Home page last updated on November 30th, 2009.
Frameworks used: Zend Framework, jQuery, 960 Grid System
Blog is powered by Wordpress
Subscribe: Entries — RSS & Comments — RSS