Hide long category list in prestashop admin

Monday, March 7th, 2011 | Prestashop | Comments Off

This is a very quick fix for people having a very long list of categories inside prestashop.
The problem occurs because in the product edit page from admin you need to scroll a lot to get to the save button each time.

Enough with the talking, here is the piece of code:
open admin/tabs/AdminProducts.php
replace:

                        <td class="col-left">'.$this->l('Catalog:').'</td>
                        <td>
                            <div style="overflow: auto; min-height: 300px; padding-top: 0.6em;" id="categoryList">

with:

                         <td class="col-left"><a onclick="javascript:openCloseLayer(\'categoryList\');return false;" href="#">'.$this->l('Catalog:').'</a></td>
                        <td>
                            <div style="display:none; overflow: auto; min-height: 300px; padding-top: 0.6em;" id="categoryList">

Generate unbreakable passwords from php

Sunday, February 13th, 2011 | Web Security | Comments Off

This is a short post to share my favorite way of generating strong passwords in php.

The advantage over other functions you’ll find over the internet is that it makes sure it uses all the character sets also making sure no character will repeat in the final password.

This outcome of the script is similar with what is generated in cpanel when creating new accounts/emails.

function generatePassword($length=12){
   $validchars = array();
   $validchars[] = "0123456789";
   $validchars[] = "abcdfghjkmnpqrstvwxyz";
   $validchars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   $validchars[] = "_!@#$%&*()-=+/";
 
   $password  = "";
   $counter   = 0;
 
   shuffle($validchars);
   while ($counter < $length) {
	   foreach($validchars as $rand_key => $pool) {
			//every letter is different
			do {
				$actChar = substr($pool, rand(0, strlen($pool)-1), 1);
				if(!strstr($password, $actChar)) break;
			} while (1==1);
			$password .= $actChar;
			$counter++;
	   }
	   reset($validchars);
   }
   return $password;
}
 
echo generatePassword();

clear space after floating divs – includes opera fix

Thursday, February 10th, 2011 | Web development | Comments Off

When writing html code to draw columns you usually use float elements contained inside a div container like here:

<div class="clearfix" style="width: 100%;">
<div style="width: 30%; float: left;">left content</div>
<div style="width: 60%; float: right;">right content</div>
</div>

the problem occurs when bellow this code you want to have a footer for instance. To work in all the browser most of front end developers will clear the space like this:

<div style="clear:both;font-size:1px;">&nbsp;</div>

a better solution is to just use css and don’t worry about additional ugly and useless html code :)
just add the following code inside your stylesheet:

 
.clearfix:after {
    content: '.';
    display: block;
    clear: both;
    visibility: hidden;
    line-height: 0px;
    height: 0px;
}
 
 
.clearfix {
    display: inline-block; 
}
 
html[xmlns] .clearfix {
    display: block;
}
 
* html .clearfix {
    height: 1%;
}
.clearfix { display:block; }

last line is for opera – don’t know why it doesn’t like this clearfix so I made it to like it ;)

Removing css outline from buttons and html elements

Tuesday, February 8th, 2011 | Web development | Comments Off

When designing, one of the first things I do is to define some very common elements with general features like no margin/padding for body.
Similar with the body element I don’t like the outline that appears when clicking a button or a li element from a list.
To do this I write something like this:

*{outline:none;}

however… on firefox the outline still appers on buttons and submit buttons. To fix this just add:

input::-moz-focus-inner, button::-moz-focus-inner { border: 0; }

Extract modified files from GIT

Wednesday, January 26th, 2011 | Web development | Comments Off

Recently I stumbled upon a great script for extracting modified files between two GIT commits.
This is great for website deployments assuming on client side only ftp is accepted.

#!/bin/bash

BOLD=”\033[1m"
_BOLD="\033[22m"
RED="\033[31m"
YELLOW="\033[33m"
GREEN="\033[32m"
RESET="\033[39m"

range=$1
if [ -z "$range" ]; then
echo -e “${BOLD}${RED}You must specify a ‘..‘ argument.${RESET}${_BOLD}”
exit 1
fi

if [ -z "$2" ]; then
target=”$( pwd )/.deployments”
else
target=$( echo “$2″ | sed -e ‘s#/\+$##g’ )
fi

if [ -d "$target" ]; then
echo -ne “Do you wish to remove ‘$target’ first? [Y/n] ”
read prompt
if [ -z "$prompt" ] || [ "$prompt" == "Y" ] || [ "$prompt" == "y" ]; then
echo -e ” ${YELLOW}Purging ‘$target’…${RESET}”
if [ -d "$target" ]; then
rm -Rf “$target”
fi
echo -e ” ${GREEN}Done.${RESET}”
fi
fi

mkdir -p “$target”

LOG=$( git whatchanged -m –oneline “$range” | awk ‘{
if ($1 ~ /^:/) {
print $5 “:” $6
}
}’ | tac )

length=$( echo “$LOG” | wc -l )
manual=”

index=0
for command in $LOG; do
operation=${command:0:1}
filepath=${command:2}
case $operation in
“A” | “M”)
if [ -f "$filepath" ]; then
destination=$( dirname “$target/$filepath” )
filename=$( basename “$filepath” )
mkdir -p “$destination”
cp -f “$filepath” “$target/$filepath”
fi
;;
“D”)
manual=”$manual\n$filepath”
;;
*)
echo -e “${BOLD}${RED}Unknown operation $operation on file $filepath.${RESET}${_BOLD}”
exit 4
;;
esac
let “index++”
echo -ne “\r${YELLOW}Processing ${length} files…${RESET} $( echo “scale=2; ( $index / $length ) * 100.00″ | bc )%”
done
echo

if [ ! -z "$manual" ]; then
echo $( echo -e “$manual” | sed -e ‘s/^\s\+//g’ | sort -u ) > “$target/.delete”
echo -e “[WARN] Please manually delete the files listed in ‘${BOLD}.delete${_BOLD}’”
fi

echo -e “${GREEN}Done.${RESET}”

save it as git-extract and use it as:

git-extract from_old_commit_hash..to_new_commit_hash

All the credits go to: http://blog.angeloff.name/post/1490030701/extract-changed-files-from-git-and-prepare-a-deployment

fix for an iframe injection attack

Tuesday, January 25th, 2011 | Web Security | Comments Off

more and more people, including some of my clients get attack by various versions of iframe injections.

One of them, includes injecting a php file inside the host. after that, by various means, in all the htaccess files it’s injected a code similar with:

AddType application/x-httpd-php .php .phtml .php3 .php4 .php5 .htm .html
php_value auto_prepend_file /path/xxxx_atacking_file_which_has_php_code

now to remove that code from each htaccess file use the following php code:

function r_fix($dir='.') {
	if ($handle = opendir($dir)) {
		while (false !== ($file = readdir($handle))) {
            if (is_dir("$dir/$file")) {
                if ($file != '.' && $file != '..') {
                    r_fix("$dir/$file");
                    //chdir($dir);
                }
            } elseif ($file=='.htaccess'){
				$path = $dir . '/' . $file;
				$contents = file_get_contents($path);
				if(strpos($contents, 'xxxx_atacking_file_which_has_php_code') !== false) {
					$contents = str_replace('AddType application/x-httpd-php .php .phtml .php3 .php4 .php5 .htm .html', '', $contents);
					$contents = str_replace('php_value auto_prepend_file /path/xxxx_atacking_file_which_has_php_code', '', $contents);
					echo $path." <br>\n ";flush();
					file_put_contents($path, $contents);					
				}
			}
		}
		closedir($handle);
	}
}
 
r_fix();

mysql IN function alternative when using mysql field

Monday, December 13th, 2010 | Web development | Comments Off

let’s assume you have a mysql field which is composed from comma separated values

table name: products
field name from table products: category_ids (the comma separated list of categories)

let’s assume that we want to search all products for a certain category. the first idea will be to use the IN mysql function: (I will note {$var} as an external variable)

SELECT * FROM products WHERE {$category_id} IN category_ids

however it won’t work so this statement should be used:
SELECT * FROM products WHERE category_ids REGEXP ‘^{$category_id},|,{$category_id},|,{$category_id}$|^{$category_id}$’

Magento problems: General error: 1005

Friday, November 26th, 2010 | Web development | Comments Off

The exact error I kept getting was:
exception ‘Zend_Db_Statement_Exception’ with message ‘SQLSTATE[HY000]: General error: 1005 Can’t create table ‘xxx catalog_category_flat_store_1.frm’ (errno: 150)’ in xxx/lib/Zend/Db/Statement/Pdo.php:234

The solution is to execute the following statements inside phpMyAdmin:
ALTER TABLE catalog_category_entity ENGINE=INNODB
ALTER TABLE core_store ENGINE=INNODB

Don’t forget the table prefixes if you have!

If you get a similar error but another table please note that this error is related to foreign indexes and occurs most offten when you move the magento database from a server to another.

I am taking the above example, the table catalog_category_flat_store_1. This table has 2 foreign keys one to catalog_category_entity and the other .. you guessed: core_store.

that’s why I executed:
ALTER TABLE catalog_category_entity ENGINE=INNODB
ALTER TABLE core_store ENGINE=INNODB

Solving: mod_fcgid: read data timeout

Saturday, October 23rd, 2010 | Linux | Comments Off

This is starting to be a more and more common problem because the hosting companies are using fcgid more often (which is a great thing as fcgid and suexec combination has a lot of advantages).

solving the read data timeout for mod fcgid is not as hard as it seems it just required some testing: in http.conf – the configuration file for apache or in a file that’s included in http.conf place the following line:

IPCCommTimeout 120 or any number of seconds you find suitable for your server.

error removing addon domain from cpanel

Friday, October 22nd, 2010 | Linux | Comments Off

Recently I was unable to remove a domain from cpanel after firstly I deleted the dns zone for that domain (using whm)

The error was: “There was a problem removing the Addon Domain” and details of the error explained: “Error from park wrapper: Sorry, you do not control the domain exampledomain.com”

I used the following steps to remove the domain:

1. Remove domain.com from /var/cpanel/users/username
2. Run /scripts/updateuserdomains as root user on the server.
3. Remove /var/named/exampledomain.com.db
4. Remove the virtualhost for domain.com on /usr/local/apache/conf/httpd.conf
5. Remove exampledomain.com from /etc/named.conf

Simple as that!