Solved

How do I convert this ColdFusion page to PHP?

Posted on 2013-01-02
6
195 Views
Last Modified: 2013-01-07
I have the attached CF script that connects to an LDAP server and displays a list of the users.

How can this be converted to PHP?
cf.txt
0
Comment
Question by:Donnie Walker
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 38737100
Should be pretty simple in terms of syntax/language, so I'm guessing you're talking more about the Active Directory portion and not about how to dump it into the JSON format for the jQuery DataTables plugin.

If that's the case, then you'll basically need to use the LDAP extension for PHP. If you're on Windows, it should be easy to enable (if it's not already enabled), since the DLL is already there and you just have to enable / uncomment it in the php.ini file and restart PHP / the web server.

Here's the installation page for the extension:
http://php.net/manual/en/ldap.installation.php

Then you would just use ldap_connect() and ldap_bind() to connect to AD with the proper credentials, and ldap_search() to perform the query. Check this page for a full example:

http://php.net/manual/en/ldap.examples-basic.php
0
 

Author Comment

by:Donnie Walker
ID: 38737306
I am weak on PHP and LDAP.

Can you provide me with an example?
0
 
LVL 35

Accepted Solution

by:
gr8gonzo earned 500 total points
ID: 38738334
I can't really test it from here, but it would look like this:

echo "<h3>LDAP query test</h3>";
echo "Connecting ...";

$server		=	"domain.college.edu";
$username	=	"user@college.edu";
$password	=	"password;

$start		=	"dc=college,dc=edu";
$filter		=	"(&(objectCategory=Person)(objectClass=User)(employeeId=*)(mail=*)(|(memberOf=CN=ActiveGroup - Employees,OU=ActiveGroups,DC=college,DC=edu)(memberOf=CN=ActiveGroup - Students,OU=ActiveGroups,DC=college,DC=edu)))";
$attributes	=	"sAMAccountName,givenName,sn,telephoneNumber,title,department,physicalDeliveryOfficeName";

// Connect
$ds=ldap_connect($server);

if ($ds) { 

		// Bind
    $r=ldap_bind($ds, $username, $password);

		// Search
    $sr=ldap_search($ds, $start, $filter, $attributes);  
    // echo "Number of entries returned is " . ldap_count_entries($ds, $sr) . "<br />";

		// Loop through results and build the array
		$output = array("aaData" => array());
    $info = ldap_get_entries($ds, $sr);
    for ($i=0; $i<$info["count"]; $i++)
    {
    	$row = $info[$i];
    	$row["sAMAccountName"] = strtoupper($row["sAMAccountName"]);
    	$output["aaData"][] = $row;
    }
    
    // Encode the array to JSON
    echo json_encode($output);

		// Close the connection
    ldap_close($ds);

} else {
    echo "<h4>Unable to connect to LDAP server</h4>";
}

Open in new window

0
Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

 

Author Comment

by:Donnie Walker
ID: 38738371
When I put in my credentials it says:

Connecting ...{"aaData":[]}
0
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 38739701
Try print_r()-ing the different variables that get created, like this:

// Connect
$ds=ldap_connect($server);
print_r($ds);

Use the same method for anything that gets created from a ldap_ function.
0
 

Author Comment

by:Donnie Walker
ID: 38740747
When I do that for the $ds it says "Recource Id #2".

However, I found another script that seems to bind/search correctly and returns entries but runs out of memory. (see attached)

When I try to run it with the jquery datatables thing it says "DataTables warning: JSON data from server could not be parsed. This is caused by a JSON formatting error."

So, I assume the new attached script is not returning the correct values?

Sorry to be such a pain but I'm kind of learning as I go.
dir.txt
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question