Wednesday, May 15, 2013

Translating month names in CakePHP (i18n)

I found this, and I think it is worth saving.

Taken from: http://www.bravo-kernel.com/2010/12/using-lc_time-with-cakephp/


USING LC_TIME WITH CAKEPHP

CakePHP supports LC_TIME translations since version 1.3 and since it took me some time to completely figure out the logic behind it I am storing my notes here as a mental reminder to self (again).
First things first… make sure you read  the i18n paragraph on LC_TIME in the Book to get some basic understanding of what we are trying to do here.

Preparing for LC_TIME

For __c(), $this->Time->format() and $this->Time->i18nFormat() to work:
  1. create a file called /app/locale/dut/LC_TIME
  2. on your local Linux workstation open /usr/share/i18n/locales/nl_NL
  3. copy everything between LC_TIME and END LC_TIME to the file created in step 1 and save that file
Note: make sure to add the escape_char and comment_char definitions to your LC_TIME file as well or your setup will not be fully functional (see this page for more info):
1
2
comment_char %
escape_char  /

strftime()

There is no need to use php’s strftime() if you consistently stick to using the CakePHP functions mentioned above. However, if you do need to get strftime() up and running you should add the following line to one of your controllers.
1
setlocale(LC_TIME, 'nl_NL.UTF8');
Please note (and understand) that this will use your server’s locales and NOT your manually created Cake LC_TIME file.

Testing your LC_TIME setup

Add the following lines to one of your views (and make sure the TimeHelper is available):
1
2
3
4
5
6
7
8
9
10
$timestamp = time();
 $timestring = $this->Time->format('Y-m-d H:i:s', $timestamp);
 $months = __c('mon', 5 ,true);
 
 pr("Timestamp = $timestamp");
 pr("Timestring = $timestring");
 pr("strftime() translated = " . strftime("%A %e %B %Y", strtotime($timestring)));
 pr("i18nFormat  translated = " . $this->Time->i18nFormat($timestring, "%A %e %B %Y"));
 pr("Time::format translated = " . $this->Time->format($timestring, '%A %e %B %Y'));
 pr($months);
If your setup is fully operational it should display the following LC_TIME translations:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Timestamp = 1292417839
Timestring = 2010-12-15 13:57:19
strftime() translated = Wednesday 15 December 2010
i18nFormat  translated = woensdag 15 december 2010
Time::format translated = woensdag 15 december 2010
Array
(
    [0] => januari
    [1] => februari
    [2] => maart
    [3] => april
    [4] => mei
    [5] => juni
    [6] => juli
    [7] => augustus
    [8] => september
    [9] => oktober
    [10] => november
    [11] => december
)
Note: you might have spotted that the strftime() translation is not translated. This is intentional since I always stick to Cake methods. See the paragraph on strftime() if you do need to use that function.
Enjoy your time translations ;)

Wednesday, April 10, 2013

cool awk

Awk is quite neat


I recently used it to checkout a bunch of files I modified by mistake.

git checkout * 

did't work because some files we're not in the index and Git complained.

So I did

git st | awk '{if ($2=="modified:") {print $3} }' | xargs git co

which was quite cool...

More on AWK

among infinite features, I just used 'match' to see if a string was inside another...

in particular lines which have an M in the first column... (like to see file in the Megabytes range with du)

du -h  | awk '{ if (match($1, "M")) {print $2} }'

Here's a link which reminded me how to do the 'if's in awk... and other examples:


http://www.thegeekstuff.com/2010/02/awk-conditional-statements/


4 Awk If Statement Examples ( if, if else, if else if, :? )
by SASIKALA on FEBRUARY 17, 2010


This article is part of the on-going Awk Tutorial Examples series. In our earlier awk articles, we discussed about awk print, awk user-defined variables, awk built-in variables, and awk operators.

In this awk tutorial, let us review awk conditional if statements with practical examples.

Awk supports lot of conditional statements to control the flow of the program. Most of the Awk conditional statement syntax are looks like ‘C’ programming language.

Normally conditional statement checks the condition, before performing any action. If the condition is true action(s) are performed. Similarly action can be performed if the condition is false.

Conditional statement starts with the keyword called ‘if’. Awk supports two different kind of if statement.

Awk Simple If statement
Awk If-Else statement
Awk If-ElseIf-Ladder
Awk Simple If Statement

Single Action: Simple If statement is used to check the conditions, if the condition returns true, it performs its corresponding action(s).

Syntax:
if (conditional-expression)
action
if is a keyword
conditional-expression – expression to check conditions
action – any awk statement to perform action.
Multiple Action: If the conditional expression returns true, then action will be performed. If more than one action needs to be performed, the actions should be enclosed in curly braces, separating them into a new line or semicolon as shown below.



Syntax:
if (conditional-expression)
{
action1;
action2;
}
If the condition is true, all the actions enclosed in braces will be performed in the given order. After all the actions are performed it continues to execute the next statements.

Awk If Else Statement

In the above simple awk If statement, there is no set of actions in case if the condition is false. In the awk If Else statement you can give the list of action to perform if the condition is false. If the condition returns true action1 will be performed, if the condition is false action 2 will be performed.

Syntax:
if (conditional-expression)
action1
else
action2
Awk also has conditional operator i.e ternary operator ( ?: ) whose feature is similar to the awk If Else Statement. If the conditional-expression is true, action1 will be performed and if the conditional-expression is false action2 will be performed.

Syntax:

conditional-expression ? action1 : action2 ;
Awk If Else If ladder

if(conditional-expression1)
action1;
else if(conditional-expression2)
action2;
else if(conditional-expression3)
action3;
.
.
else
action n;
If the conditional-expression1 is true then action1 will be performed.
If the conditional-expression1 is false then conditional-expression2 will be checked, if its true, action2 will be performed and goes on like this. Last else part will be performed if none of the conditional-expression is true.
Now let us create the sample input file which has the student marks.

$cat student-marks
Jones 2143 78 84 77
Gondrol 2321 56 58 45
RinRao 2122 38 37
Edwin 2537 87 97 95
Dayan 2415 30 47
1. Awk If Example: Check all the marks are exist

$ awk '{
if ($3 =="" || $4 == "" || $5 == "")
print "Some score for the student",$1,"is missing";'
}' student-marks
Some score for the student RinRao is missing
Some score for the student Dayan is missing
$3, $4 and $5 are test scores of the student. If test score is equal to empty, it throws the message. || operator is to check any one of marks is not exist, it should alert.

2. Awk If Else Example: Generate Pass/Fail Report based on Student marks in each subject

$ awk '{
if ($3 >=35 && $4 >= 35 && $5 >= 35)
print $0,"=>","Pass";
else
print $0,"=>","Fail";
}' student-marks
Jones 2143 78 84 77 => Pass
Gondrol 2321 56 58 45 => Pass
RinRao 2122 38 37 => Fail
Edwin 2537 87 97 95 => Pass
Dayan 2415 30 47 => Fail
The condition for Pass is all the test score mark should be greater than or equal to 35. So all the test scores are checked if greater than 35, then it prints the whole line and string “Pass”, else i.e even if any one of the test score doesn’t meet the condition, it prints the whole line and prints the string “Fail”.

3. Awk If Else If Example: Find the average and grade for every student

$ cat grade.awk
{
total=$3+$4+$5;
avg=total/3;
if ( avg >= 90 ) grade="A";
else if ( avg >= 80) grade ="B";
else if (avg >= 70) grade ="C";
else grade="D";

print $0,"=>",grade;
}
$ awk -f grade.awk student-marks
Jones 2143 78 84 77 => C
Gondrol 2321 56 58 45 => D
RinRao 2122 38 37 => D
Edwin 2537 87 97 95 => A
Dayan 2415 30 47 => D
In the above awk script, the variable called ‘avg’ has the average of the three test scores. If the average is greater than or equal to 90, then grade is A, or if the average is greater than or equal to 80 then grade is B, if the average is greater than or equal to 70, then the grade is C. Or else the grade is D.

4. Awk Ternary ( ?: ) Example: Concatenate every 3 lines of input with a comma.

$ awk 'ORS=NR%3?",":"\n"' student-marks
Jones 2143 78 84 77,Gondrol 2321 56 58 45,RinRao 2122 38 37
Edwin 2537 87 97 95,Dayan 2415 30 47,
We discussed about awk ORS built-in variable earlier. This variable gets appended after every line that gets output. In this example, it gets changed on every 3rd line from a comma to a newline. For lines 1, 2 it’s a comma, for line 3 it’s a newline, for lines 4, 5 it’s a comma, for line 6 a newline, etc.

Thursday, March 14, 2013

counting connections to my server and limit connections by IP

Today I was informed by Amazon that I was being hacked. : - o

I learned to count the connection that where being made to my server with a very pretty command:

sudo netstat -anp |grep 'tcp\|udp' | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n

There were some 'mean' Indonesians and Slovakians opening a lot of connection to my server, so the output of the command looked like:

sudo netstat -anp |grep 'tcp\|udp' | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n
...

     54 64.90.61.239
     62 77.55.119.209
     63 75.119.200.56
    134 112.78.2.183
    182 173.233.65.108


Having Apache allowing 300 connections (directive MaxClients in apache2.conf), and ServerLimit 500 (ServerLimit directive), this connections from weird countries I shouldn't have visitors from rapidly collapsed my site and it was down

iptables to the rescue:

I found this simple tutorial: http://www.cyberciti.biz/faq/iptables-connection-limits-howto/ to configure IPTABLES, and this other one to learn a bit more about the command: https://help.ubuntu.com/community/IptablesHowTo

In the end, it was enough to do

sudo iptables -A INPUT -p tcp --syn --dport 80 -m connlimit --connlimit-above 20 -j REJECT --reject-with tcp-rese

and then

sudo sh -c "iptables-save > /etc/iptables.rules"

to save the configuration into a file that is loaded at boot. Watch out dont overwrite the /etc/iptables.rules if you have any.

After this commands my site was up again, and I had learned a lot.

here is the tutorial I followed (iptables is pretty standard, so its not necessary to reproduce it here):

Iptables Limits Connections Per IP

by on February 7, 2010 · 29 comments· last updated at February 9, 2010
How do I restrict the number of connections used by a single IP address to my server for port 80 and 25 using iptables? You need to use the connlimit modules which allows you to restrict the number of parallel TCP connections to a server per client IP address (or address block).
This is useful to protect your server or vps box against flooding, spamming or content scraping.

Syntax

The syntax is as follows:
/sbin/iptables -A INPUT -p tcp --syn --dport $port -m connlimit --connlimit-above N -j REJECT --reject-with tcp-reset
# save the changes see iptables-save man page, the following is redhat and friends specific command
service iptables save

Example: Limit SSH Connections Per IP / Host

Only allow 3 ssg connections per client host:
/sbin/iptables  -A INPUT -p tcp --syn --dport 22 -m connlimit --connlimit-above 3 -j REJECT
# save the changes see iptables-save man page, the following is redhat and friends specific command
service iptables save

Example: Limit HTTP Connections Per IP / Host

Only allow 20 http connections per IP (MaxClients is set to 60 in httpd.conf):
WARNING! Please note that large proxy servers may legitimately create a large number of connections to your server. You can skip those ips using ! syntax
/sbin/iptables -A INPUT -p tcp --syn --dport 80 -m connlimit --connlimit-above 20 -j REJECT --reject-with tcp-reset
# save the changes see iptables-save man page, the following is redhat and friends specific command
service iptables save
Skip proxy server IP 1.2.3.4 from this kind of limitations:
/sbin/iptables -A INPUT -p tcp --syn --dport 80 -d ! 1.2.3.4 -m connlimit --connlimit-above 20 -j REJECT --reject-with tcp-reset

Example: Class C Limitations

In this example, limit the parallel http requests to 20 per class C sized network (24 bit netmask)
/sbin/iptables  -A INPUT -p tcp --syn --dport 80 -m connlimit --connlimit-above 20 --connlimit-mask 24 -j REJECT --reject-with tcp-reset
# save the changes see iptables-save man page
service iptables save

Example: Limit Connections Per Second

The following example will drop incoming connections if IP make more than 10 connection attempts to port 80 within 100 seconds (add rules to your iptables shell script)
#!/bin/bash
IPT=/sbin/iptables
# Max connection in seconds
SECONDS=100
# Max connections per IP
BLOCKCOUNT=10
# ....
# ..
# default action can be DROP or REJECT
DACTION="DROP"
$IPT -A INPUT -p tcp --dport 80 -i eth0 -m state --state NEW -m recent --set
$IPT -A INPUT -p tcp --dport 80 -i eth0 -m state --state NEW -m recent --update --seconds ${SECONDS} --hitcount ${BLOCKCOUNT} -j ${DACTION}
# ....
# ..

How Do I Test My Firewall Working?

Use the following shell script to connect to your web server hosted at 202.1.2.3:
#!/bin/bash
ip="202.1.2.3"
port="80"
for i in {1..100}
do
  # do nothing just connect and exit
  echo "exit" | nc ${ip} ${port};
done
 

References:

Wednesday, March 6, 2013

for loop inline bash style AND while ... loop

So simple, yet I always forget.

Here's how to do a for loop in inline bash:

for x in {1..30}; do foo1; foo2; done

Also how to do an inline while loop in bash:

while true; do foo; sleep 2; done

copy hidden files when using 'cp'

To copy hidden files when using 'cp' don't specify the files being copied..


Don't specify the files:
cp -r /etc/skel /home/user

http://superuser.com/questions/61611/how-to-copy-with-cp-to-include-hidden-files-and-hidden-directories-and-their-con

Sunday, March 3, 2013

cakephp 1.3 migration to 2.3.1

I decided to migrate one of my apps (one of the easiest) from cakephp 1.3 to 2.3.1

Although the cakaphp upgrade shell is magnificent upgrading my code, it doesn't to all (obviously).

Problems I encountered:


  • upgrading plugins
    • I was lucky I think all my plugins had a 2.x branch
  • One to manually upgrade webroot/index.php file
  • One has to manually merge Config/core.php file with your old one
  • Im 1.3 I used $this->view = 'MyView'  in app_controller to use my own view class. This changed to $this->viewClass
  • I also had to update my legacy code.
    • That is to change some things from 1.2 style to 1.3 style... which was already compatble with 2.3.1 !
      • that included basically changing
        • $this->Javascript->link to $this->Html->script
        • $this->Javascript->codeBlock to $this->Html->scriptBlock
        • $this->Javascript->object to $this->JsBaseEngine->object
        • this was easily done with find && sed:
find . -type f -exec sed -i -e "s/Javascript->link/Html->script/g" {} \;
find . -type f -exec sed -i -e "s/Javascript->codeBlock/Html->scriptBlock/g" {} \;
find . -type f -exec sed -i -e "s/Javascript->link/Html->script/g" {} \;

cancel script completely on ctrl-c

I found this question interesting: basically how to cancel completely a script and all child processes : You do this by creating a subro...