Nasrul Hazim Bin Mohamad

CakePHP: Running Shell Script with Cronjob

May
27

Create a Shell Script

Create a ScheduleShell.php in cakephp/app/Console/Command.

<?php
class ScheduleShell extends Shell {
    function main() {
    	$this->out('Hello World');
    }
}
?>

Add a cronjob

sudo crontab -e

I have a cronjob running every 5 minute for ScheduleShell.php

# cronjob run every 5 minute
*/5  *    *    *    * cd /var/www/html/cakephp/app && Console/cake schedule

Ubuntu: LDAP – Install, Configure & Test

May
23

I’m using Ubuntu 12.04

LDAP: Install & Configure

  1. sudo apt-get install slapd ldap-utils
    1. Enter password: [SetY0urP@$$wOrD]
  2. dpkg-reconfigure slapd
    1. Omit OpenLDAP server configuration? No
    2. DNS domain name? domain.com | IP Address
    3. Organization name? My Organization
    4. Administrator password? Use the password during installation | Choose new one
    5. Database backend to use? HDB
    6. Remove the database when slapd is purged? No
    7. Move old database? Yes
    8. Allow LDAPv2 protocol? No
  3. joe /etc/phpldapadmin/config.php (i’m using joe text editor, you may use the default text editor such vi)
    $servers->setValue('server','host','domain.com | IP Address');
    $servers->setValue('server','base',array('dc=domain,dc=com'));
    $servers->setValue('login','bind_id','cn=admin,dc=domain,dc=com');
    $config->custom->appearance['hide_template_warning'] = true;
    

LDAP: Install PHPLDAPAdmin

  1. apt-get install libpam-ldap nscd | pkg-reconfigure ldap-auth-config
    1. LDAP server Uniform Resource Identifier: ldap://**Domain-Name-OR-IP-Address** ( Change the initial string from “ldapi:///” to “ldap://” before inputing your server’s information )
    2. Distinguished name of the search base: ( This should match the value you put in your LDAP server’s/etc/phpldapadmin/config.php file. )
    3. LDAP version to use: 3
    4. Make local root Database admin: Yes
    5. Does the LDAP database require login? No
    6. LDAP account for root: This should also match the value in your /etc/phpldapadmin/config.php
    7. LDAP root account password: LDAP-Root-Password
  2. joe /etc/nsswitch.conf – add ldap before compat for each passwd, group, shadow
  3. joe /etc/pam.d/common-session – add at the bottom: session required pam_mkhomedir.so skel=/etc/skel umask=0022
  4. /etc/init.d/nscd restart
  5. Access to PHPLDAPAdmin: http://domain.com/phpldapadmin

LDAP: Setting up LDAP Groups & Users

  1. Go to http://domain.com/phpldapadmin
  2. Login DN: cn=admin,dc=domain,dc=com
  3. Login Password: Password configured during slapd configuration
  4. To do:
    1. Create an Generic: Organization Units named Users
    2. Create a Generic: Posix Group named groups
    3. Create one or more Generic: User Account (make sure it’s under ou=users)
    4. (Optional) Add users to groups by clicking a group & Add New Attribute. Select memberUid and click on Update Object at the bottom.

LDAP: Test LDAP

ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3); 

$ldapconn = ldap_connect("domain.com")
    or die("Could not connect to LDAP server.");

$person = 'cn=username,ou=users,dc=domain,dc=com';
$password = 'password';

if ($ldapconn) {

    $ldapbind = ldap_bind($ldapconn, $person, $password);

    if ($ldapbind) {

    	$ldaptree = "ou=users,dc=domain,dc=com";
		$justthese = array("ou", "sn", "givenname");

		$result = ldap_search($ldapconn,$ldaptree,"givenName=$person",$justthese) or die ("Error in search query: ".ldap_error($ldapconn));

		$no = ldap_count_entries($ldapconn, $result);
		
		echo 'Count Entries: ' . $no . '<br>';

		$info = ldap_get_entries($ldapconn, $result);
		
		for ($i=0; $i<$info["count"]; $i++) 
		{
			echo $info[$i]['givenname'][0] . ' (' . $info[$i]['mail'][0] . ')';
			echo '<hr>';
		}

    } else {
        echo "LDAP bind ".$user." failed...";
    }

}

Reference: Installing LDAP on Ubuntu 12.04

jQuery: Show Loading…

May
22
$.ajax(
{
	url: 'http://www.url.com/do-something',
	success: function(result) {
		$('#result').html(result);
	},
	beforeSend: function() {
		$("#loading").show();
	},
	complete: function() {
		$("#loading").hide();
	}
}
);

Foundation: Startup

May
01

Download Foundation and Foundation Icon.

<!doctype html>
<html class="no-js" lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Foundation | Welcome</title>
    <link rel="stylesheet" href="css/foundation.css" />
    <link rel="stylesheet" href="css/foundation-icons.css" />
    <link rel="stylesheet" href="css/learn.css" /><!-- Your own set CSS -->
    <script src="js/vendor/modernizr.js"></script>
    <!--[if lte IE 8]><script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
  </head>
  <body>
    <!-- Layout Start -->
    <!-- PUT YOUR LAYOUT, CONTENT HERE -->
    <!-- Layout End -->
    <script src="js/vendor/jquery.js"></script>
    <script src="js/foundation.min.js"></script>
    <script>
      $(document).foundation();
    </script>
  </body>
</html>

Python: Setting up in Windows 7

May
01
Python

Python

  1. Download latest 2.x at https://www.python.org/downloads/
  2. Set Environment path: C:Python27;C:Python27Libsite-packages;C:Python27Scripts;
  3. Install Setuptools: https://pypi.python.org/pypi/setuptools#windows-7-or-graphical-install
    1. Download the script
    2. Open CMD
    3. Go to the script directory
    4. Run the script: python ez_setup.py
    5. Exit once done (Ctrl + Z, then Enter)

Test your installation by open up CMD, and type in python. You should see something similar to the screen shot below.

cmd-python

Following are optional:

  1. Install pip: C:> easy_install pip
  2. Install the followng using pip
    1. Mechanize: C:> pip install mechanize
    2. BeatifulSoup4: C:> pip install beautifulsoup4
    3. CSVKIT: C:> pip install csvkit

References:

  1. http://www.anthonydebarros.com/2011/10/15/setting-up-python-in-windows-7/
  2. http://www.anthonydebarros.com/2011/09/11/csvkit-data-files/