LAMP How To – Open Source At Work

Only Passion Matters

php curl proxy settings

It can be daunting to get proxy to work in curl. If it is not working for you, try changing the auth to ntlm.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ‘http://yoururl.com’);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_PROXY , “your_proxy_ip:proxy_port”);
curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_NTLM);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
curl_setopt($ch,CURLOPT_PROXYUSERPWD,”user:pass”);

curl_exec ($ch);

if(curl_errno($ch))
{
echo ‘Curl error: ‘ . curl_error($ch);
exit;
}

curl_close ($ch);

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • StumbleUpon
  • Technorati
  • LinkedIn
  • MySpace
  • Slashdot
  • TwitThis
  • Yahoo! Buzz

Preserving HTML in XML

Characters like “<” and “&” are illegal in XML elements. “<” will generate an error because the parser interprets it as the start of a new element. “&” will generate an error because the parser interprets it as the start of an character entity. Some text, like JavaScript code, contains a lot of “<” or “&” characters. To avoid errors, we need to parse the code using CDATA (Character Data) like so:

<!CDATA[
xxxxxxxxxxxxxxx
]]>

This is different from commenting. Commenting skips the whole section entirely, like so:

<!–
xxxxxxxxxx
–>

which is just like HTML comments.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • StumbleUpon
  • Technorati
  • LinkedIn
  • MySpace
  • Slashdot
  • TwitThis
  • Yahoo! Buzz

Eclipse Proxy Fix

If you are behind a proxy server, you may get annoying proxy popups after installing eclipse. The trick is to manually setup the proxies in

windows->preferences->general->networking

This works for galileo, the version that I am using.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • StumbleUpon
  • Technorati
  • LinkedIn
  • MySpace
  • Slashdot
  • TwitThis
  • Yahoo! Buzz

Advance SVN post-commit hook script for System Administrators

Everyone knows how cool svn post-commit feature is. Instead of using the basic post-commit script provided by svn, we can do alot more with a bit of server side scripting. Here, I like to share a simple script that I wrote to automate the process of updating different server system files using svn post-commit. It works for me… it might work for you as well. There are of course alot of areas that needs improvement.

The first thing to do is to have the svn repo setup with the server name as the top dir and all sub directories mirror exactly the same way as the system dir. I have 2 servers here, ares.stag and zeus.dev for example:

server_config
|-- ares.stag
|   `-- home
|       `-- data
|               `-- vhost.ares.conf
|-- zeus.dev
|   |-- mutt
|   |   `-- muttrc
|   |-- vim
|   |   `-- vimrc
|   `-- xen
|   |-- var
|       `-- named
|           `-- chroot
|               |-- etc
|               |   `-- named.conf
|               `-- var
|                   `-- named
|                       |-- 115.2.10.in-addr.arpa
|                       |-- linux.dev.db
|                       |-- linux.live.db
|                       `-- linux.stag.db

Everytime I want to edit my stag dns for example, I dont need to manually do it in the server, I just need to edit linux.stag.db in my own desktop, then “svn commit”. The commit script is responsible to then put the files that I committed to the right place for me.

#!/bin/sh

# - This is a more advanced svn post-commit script.
# - This is currently used to rollover server config.
# - The script attempts to copy files from different server based on the
# current svn dir hierarchy.
# - ssh keys from user@{current_server} to root@{external_server} must exists.
# - The file in the server must exists else this script will fail.
# - It rolls over modified files, it doesn't delete file.
#
# author: Bernard Peh
# date: 16 April 2010
#
# version 1.0
# script created.

REPOS="$1"
REV="$2"

# define your admin for email alert
EMAIL="youremail@youremail.com"

# define log. Leave it as default if you want
TMP=/tmp/$$
LOG=/tmp/$$_log
USERLOG=/tmp/$$_userlog

# output svn details into log
svn log -v --xml -r$REV file://$REPOS > $LOG

# full path to command
WALL=/usr/bin/wall
SVN=/usr/bin/svn
SED=/bin/sed
AWK=/bin/awk
GREP=/bin/grep
CAT=/bin/cat
SSH=/usr/bin/ssh

# only copy committed files that are new or modified
$CAT $LOG | $GREP ' action=' | while read x; do SERVER=`echo $x | $AWK -F/ '{print $2}'`; RES=`echo $x | $SED 's/action=\"\(.*\)\">\/'$SERVER'\(.*\)<\/path>/\1 \2/'`; PATH=`echo $RES | $GREP '^\(A\|M\)'`; PATH=`if [ ! -z "$PATH" ]; then echo $PATH | $AWK '{print $2}'; fi;`; $SVN cat file://${REPOS}/${SERVER}${PATH} 2>/dev/null | $SSH root@$SERVER "cat - > $PATH" 2>> $USERLOG; if [ !$? ]; then echo "Successfully updated ${SERVER}:${PATH}" >> $USERLOG; fi; done

# mail to admin
$CAT "$USERLOG" | mail -s "$REPOS updated to rev $REV" $EMAIL

# clean up
rm -rf $TMP
rm -rf $LOG
rm -rf $USERLOG

# all good now exit
exit 0;
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • StumbleUpon
  • Technorati
  • LinkedIn
  • MySpace
  • Slashdot
  • TwitThis
  • Yahoo! Buzz

SVN over HTTP

Objective

I want to be able to browse the svn dir from a website like http://zeus.dev/svn.php and at the same time, be able to checkout from the repository via http://zeus.dev/svn/projectname

How

First of all, install mod_dav_svn

Then in the apache config, I am using vhost for example

<VirtualHost *:80>
  DocumentRoot /home/data/zeus.dev/apps/
  ServerName zeus.dev
  # this is for svn
  <Location /svn.php>
    RewriteEngine on
    RewriteRule svn.php/([^/\.]+) /svn/$1 [L]
  </Location>
  <Location /svn>
    DAV svn
    # SVNPath /home/data/svn/
    SVNParentPath /home/data/svn
    # Limit write permission to list of valid users.
    # Require SSL connection for password protection.
    # SSLRequireSSL
    AuthType Basic
    AuthName "SVN repository"
    AuthUserFile /etc/httpd/conf.d/subversion.passwd
    Require valid-user
  </Location>
</VirtualHost>

In my document root, /home/data/zeus.dev/apps, I have a svn.php file soft-linked to /home/data/svn (the place where all svn repository are). As you can see from above, I also use a basic authentication mechanism for anyone who wishes to checkout from the repo.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • StumbleUpon
  • Technorati
  • LinkedIn
  • MySpace
  • Slashdot
  • TwitThis
  • Yahoo! Buzz