Friday 27 June 2014

PHP: Get user list from the Active Directory

/**
 * Get a list of users from Active Directory.
 */
$ldap_password = 'PASSWORD';
$ldap_username = 'USERNAME@DOMAIN';
$ldap_connection = ldap_connect(HOSTNAME);
if (FALSE === $ldap_connection){
    // Uh-oh, something is wrong...
}

// We have to set this option for the version of Active Directory we are using.
ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3) or die('Unable to set LDAP protocol version');
ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, 0); // We need this for doing an LDAP search.

if (TRUE === ldap_bind($ldap_connection, $ldap_username, $ldap_password)){
    $ldap_base_dn = 'DC=XXXX,DC=XXXX';
    $search_filter = '(&(objectCategory=person)(samaccountname=*))';
    $attributes = array();
    $attributes[] = 'givenname';
    $attributes[] = 'mail';
    $attributes[] = 'samaccountname';
    $attributes[] = 'sn';
    $result = ldap_search($ldap_connection, $ldap_base_dn, $search_filter, $attributes);
    if (FALSE !== $result){
        $entries = ldap_get_entries($ldap_connection, $result);
        for ($x=0; $x<$entries['count']; $x++){
            if (!empty($entries[$x]['givenname'][0]) &&
                 !empty($entries[$x]['mail'][0]) &&
                 !empty($entries[$x]['samaccountname'][0]) &&
                 !empty($entries[$x]['sn'][0]) &&
                 'Shop' !== $entries[$x]['sn'][0] &&
                 'Account' !== $entries[$x]['sn'][0]){
                $ad_users[strtoupper(trim($entries[$x]['samaccountname'][0]))] = array('email' => strtolower(trim($entries[$x]['mail'][0])),'first_name' => trim($entries[$x]['givenname'][0]),'last_name' => trim($entries[$x]['sn'][0]));
            }
        }
    }
    ldap_unbind($ldap_connection); // Clean up after ourselves.
}

$message .= "Retrieved ". count($ad_users) ." Active Directory users\n";

Monday 16 June 2014

PHP: Generate PDF from php data retrieve from mysql

<?php

require('fpdf.php');
$fullname = "";
echo $contractno = $_REQUEST['contractno'];
echo $sname = $_REQUEST['sname'];
echo $fname = $_REQUEST['fname'];
echo $mi = $_REQUEST['mi'];
echo $dob = $_REQUEST['dob'];
echo $cpprefix = $_REQUEST['cpprefix'];
echo $cpnum = $_REQUEST['cpnum'];
echo $eadd = $_REQUEST['eadd'];

include ('connect.php');

$str = "SELECT contractno,sname,fname,mi,dob,applno,cpnum,eadd WHERE ((contractno='".$contractno."')AND (sname='".$sname."') AND (fname='".$fname."') AND (mi='".$mi."') AND (dob='".$dob."') AND (applno='".$applno.'"));
echo $result = mysql_query($str,$db);

class PDF extends FPDF
{

        function Header()
        {
            $this->Image('lifehead.jpg',25,10,150);
            $this->SetFont('Arial','B',12);
            $this->Cell(80);
            $this->Ln(20);
        }
}
$pdf=new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Arial','','8');
//for($i=1;$i<=40;$i++)
//(x,y)
$pdf->SetXY(40,10);
$pdf->Cell(10,73,'Planholder', $sname, $fname, $mi);
//$pdf->SetXY(69,10);
//$pdf->Cell(10,73);
$pdf->SetXY(72,10); //TO INDENT
$pdf->Cell(10,73,'                                               Birthdate', $dob);
//$pdf->SetXY(136,10);
//$pdf->Cell(10,73,                                            'Contract Date');

$pdf->SetXY(40,10);
$pdf->Cell(10,95,'Contact number', $contractno);

$pdf->SetXY(42,10);
$pdf->Cell(10,110,'Application Number', $applno);
//$pdf->SetXY(69,10);
//$pdf->Cell(10,110);
$pdf->SetXY(72,10); //TO INDENT
$pdf->Cell(10,110,'                                              Email Address', $eadd);
//$pdf->SetXY(95,10); //TO INDENT
//$pdf->Cell(10,110,'                                              Contract Date');
$pdf->SetXY(40,10);
$pdf->Cell(10,177,'Conforme:');
$pdf->SetXY(48,10);
$pdf->Cell(10,195,'___________________');
$pdf->SetXY(56,10);
$pdf->Cell(10,202,'Planholder');
$pdf->SetXY(58,10);
$pdf->Cell(10,195,'                                              _______________');
$pdf->SetXY(66,10);
$pdf->Cell(10,202,'                                              Date');

$pdf->Output();

?>