Archive for the ‘Development’ category

How to – layout with fixed sidebar

March 13th, 2010

Today I was working on a page layout where a page width had to be fluid, while the sidebar had to be with fixed width. So here is my solution:

The layout looked like this in the beginning:

Layout

» Read more: How to – layout with fixed sidebar

Problem with mysql_real_escape_string when Sanitize your data.

February 28th, 2010

When building applications is always smart to check and clean the user input. This is a must when you building a website or public application.

I create always an instance of Sanitize class in my AppController and then using it in all of my controllers like this
» Read more: Problem with mysql_real_escape_string when Sanitize your data.

CakePHP Excel Helper

November 21st, 2009

This is a helper for building multi-sheets Excel documents in CakePHP. The hepler was inspired from Yuen Ying’s blog post, but it’s extended to support multiple sheets.
» Read more: CakePHP Excel Helper

How to secure selectively admin location

November 15th, 2009

Securing the admin part of the site is always tricky, especially for open source software CMS. A very good practice is to protect your admin directory with an extra .htaccess, because most of the damages has been done from exploits of the applications rather than brute force cracking.

There are many articles how to set up .htaccess authentication on Apache web server, but here I will explain how to set an extra password request only for Internet users while the users from Local Network should spare the extra password.

The scenario:
When the user in in the local network the administration part need to be accessible with the CMS default authentication, while if the user access the Administration from Internet, an extra password prompt will be shown.

1. Create your password file

# htpasswd -c /your_secret_location/.htpassword user
New password:
Re-type new password:
Adding password for user user

If you writing in an existing file don’t use the option -c

2. Setting the .htaccess
Depending of the server set up you can do this in your httpd.conf, or in apache2 style – in sites-available directory. So, let’s say we have a file in /etc/apache/sites-available called site.com which holds the record of the domain

<VirtualHost *:80>
        ServerAdmin email@site.com
        ServerName  site.com
        ...
        <Location /admin>
                 AuthType Basic
                 AuthName intranet
                 Satisfy any
                 Allow from 192.168
                 Order allow,deny
                 AuthUserFile /your_secret_location/.htpasswd
                 Require valid-user
        </Location>
        ...
</VirtualHost>

Here is the tricky part: The directive “Satisfy any”. By default the directive is set to all so it’s like AND:
If (you are in local net AND you are valid user) {access the location}

while Satisfy any is like OR
If (you are in local net OR you are valid user) {access the location}

More on this topic: Satisfy directive

Adding notification when new order arrives. Magento

October 28th, 2009

It’s very common practice the administrators to get notification mail when a new order is placed on the shop. Here is how to activate this feature in Magento for free (there is paid Admin Notification Module as well):

Log in into admin area, then go to System->Configuration menu. Scroll down to Sales section and click on the Sales Emails and select the first section Orders. Enter your admin e-mail in the field “Send Order Email Copy To”.

And here comes the tricky one :) Select the option “Separate email” from the “Send Order Email Copy Method” field. I don’t know why, but if it’s selected “Bcc”, the e-mail to the admin wasn’t send.

It’s possible that this is fixed in the versions above 1.2.x, but I haven’t tested.

hope this helps someone.

Web based CakePHP Project generation tool

October 23rd, 2009

Yesterday I found a tool which could be handy for rookie CakePHP developers. Atlas is a web code generator based in CakePHP. As a glance it’s a web alternative of CakePHP bake console which is a nice idea indeed.

My first impression was that tool is like diamond in the mud and I was wondering how I didn’t hear about this tool till now, but after installing I realized that it’s not so good as it looked like.

Why?

The first problem occurs in the generation of my first project – I’ve got a error message in Spanish which I couldn’t understand. I had to use Google translate for it, but after the translation, the message still doesn’t say much – “Failed to generate project skeleton”.

After debugging the code figuring out why this error means, I found that the application uses backslash as directory separator (\) ?!? and of course this lead to directories and files with strange names like /var/www/ctest\app\controllers directories where ctest\app\controllers was the full name of the directory :) . I am on Ubuntu Desktop machine where backslash is not a directory separator.

Pros and Cons about this tool:

Pros:

  • Very nice interface – really big point for Atlas
  • It should work properly on windows environment and based on the features list and screenshots it quite intuitive.
  • I like web based development everything is in your web browser, so another big plus.

Cons

  • Not working as expected – the backslash problem described above
  • If you are not Spanish speaking person – you are in the middle of nowhere when you see error like this: Error: no se ha podido generar el esqueleto del proyecto
  • No documentation at all. I had to look in the code in order to get idea why I cannot create a project – Not clear labels (or at least no hints). Probably a hint like: Directory (enter a full path to the empty directory under htdocs) or similar could help a lot.

The conclusion:
Atlas looking like promising tool, still it’s far from the final version, but it’s worth to try it in Windows environment. :) I definitely will follow that project in the future, so hopefully I could use it one day :)

How to use aggregation SQL functions with CakePHP

September 10th, 2009

Have you ever wondering how to find MAX, MIN or AVG of a database table column in CakePHP way? Well I haven’t since yesterday. The solution is quite simple and obvious.

Lets say you want to show the average views per post (in the example we have the views field in every post).
The SQL should be something like this:

SELECT AVG(views) FROM posts

Instead of using query() function you can use the CakePHP way:

$this->Post->find('first', array('fields'=>array('AVG(views) as avg_views')));

Hope this will help someone.

JSON and CakePHP

July 27th, 2009

Last few articles in this blog are only for stupid problems and unfortunately this one will be the same.

I had to use JSON to update some chained select boxes. I am using one jQuery plugin for this. The problem is that if you code, most likely your debug setting in the core.php file is set to 1 (or 2). This way the developer can see errors from CakePHP.

When the level is set to 1, at the end of the page, there is an comment, displaying the processing time for the page, but when you use JSON, this comment causes troubles (probably, because JSON doesn’t accept this type of comments) and the JS fail to process the response.

In order to check JSON, I needed to disable set debug level to 0 and after this to reset it again to 1 (for debugging purposes in other parts of the application).

So, the conclusion: bear in mind, that JSON doesn’t like HTML comments.

Hope this helps.

linxspy.com – track your links

July 17th, 2009

Today I want to present you a small service which I wrote for a week /including the design/. :)

I’ve wrote it, because few times I’ve exchanged some links with strangers and few weeks after this, my link was disappeared from their sites while their was on mine. It is a bit unfair isn’t it? I know that link exchange is not promoted from Google, but people still do it. Do you? :)

The service is called Linxspy.com and in general it is a simple interface, where you can define your links and partner’s websites. On a regular basis /at this moment every week/, the service will check the partner site and will look for your link. If everything is ok and link is there, no action will be taken, but if the link is missing, you will be notified by mail.

It should be very usefull for web masters which exchanging or buying links with unknown or not well known partners.

Basically if everything is cool with your partners, you should forget about Linxspy, because it will bother you only for troubles. :)

A little bit for implementation: As I said it was build for a week and as you probably guess it’s CakePHP app. On this project I’ve used Console Shell interface for triggering the weekly check.

Here you could find more information on about the service page

Here are some screenshots from Linxspy:

Hope it would be useful for you.

CakePHP ajaxed pagination and sort

May 16th, 2009

Here I will describe how I did an Ajaxed pagination for one of my projects using jQuery and Livequery Plugin. Currently I am working on a huge project which, hopefully, will feed me with some ideas for blog posts and this is one of them :) . So:

1. Loading the javascript libs

Loading the jQuery and Livequery was done by Autoloader helper, but if you don’t use it just include jQuery and Livequery in your layout with following code

<?php
echo $javascript->link('jquery.min');
echo $javascript->link('plugins/jquery.livequery');
?>

» Read more: CakePHP ajaxed pagination and sort