What is the best way to run a search on the current user to retrieve all attributes, including associated groups in Active Directory using LDAP / PHP?
For attributes, mainly just first name, last name, and display name.
For associated groups, just the groups the user is a member of, such as the memberOf function.
I've tried a few options, but can't seem to get the right filter / search combination and most examples cover retrieving lists of users where there's a known group.
I've tried running this after a successful bind:
$attributes = array("displayname");
$filter = "(&(sAMAccountName=$username))";
$result = ldap_search($ds, $ldapconfig['basedn'], $filter, $attributes);
$entries = ldap_get_entries($ds, $result);
if($entries["count"] > 0){
echo "displayName: ".$entries[0]['displayname'][0]."<br/>";
} else {
echo("msg:'".ldap_error($ds)."'</br>");
}
Which returns the following error: "No such object".
UPDATE:
This is the latest block I've tried and am able to get results when I print_r the $info variable, however the for clause is still erring out somewhere. I changed the basedn to just the dc attributes:
$filter="($SearchField=$SearchFor)";
$sr=ldap_search($ds, $basedn, $filter, $LDAPFieldsToFind);
$info = ldap_get_entries($ds, $sr);
if($info["count"] > 0) {
for ($x=0; $x<$info["count"]; $x++) {
$sam=$info[$x]['samaccountname'][0];
$giv=$info[$x]['givenname'][0];
$tel=$info[$x]['telephonenumber'][0];
$email=$info[$x]['mail'][0];
$nam=$info[$x]['cn'][0];
$dir=$info[$x]['homedirectory'][0];
$dir=strtolower($dir);
$pos=strpos($dir,"home");
$pos=$pos+5;
if (stristr($sam, $SearchFor) && (strlen($dir) > 8)) {
print "\nActive Directory says that:\n";
print "CN is: ".$nam." \n";
print "SAMAccountName is: ".$sam." \n";
print "Given Name is: ".$giv." \n";
print "Telephone is: ".$tel." \n";
print "Home Directory is: ".$dir." \n";
}
}
}
The print_r of the results are:
( [count] => 1 [0] => Array ( [cn] => Array ( [count] => 1 [0] => George ) [0] => cn [givenname] => Array ( [count] => 1 [0] => George ) [1] => givenname [memberof] => Array ( [count] => 4 [0] => CN=EQCStaff,CN=Users,DC=EQC,DC=local [1] => CN=RDS Users,OU=Security Groups,OU=Service,DC=EQC,DC=local [2] => CN=SFTP Client Folders,OU=Security Groups,OU=Service,DC=EQC,DC=local [3] => CN=EQC Staff,OU=Security Groups,OU=Service,DC=EQC,DC=local ) [2] => memberof [samaccountname] => Array ( [count] => 1 [0] => gortiz ) [3] => samaccountname [mail] => Array ( [count] => 1 [0] => user#domain.com ) [4] => mail [count] => 5 [dn] => CN=George,OU=Users,OU=Accounts,DC=EQC,DC=local ) )
Here's a script we have for dumping AD information, maybe it will help you:
<?php
$ldap_columns = NULL;
$ldap_connection = NULL;
$ldap_password = 'top_secret_password';
$ldap_username = 'top_secret_username#'.LDAP_DOMAIN;
//------------------------------------------------------------------------------
// Connect to the LDAP server.
//------------------------------------------------------------------------------
$ldap_connection = ldap_connect(LDAP_HOSTNAME);
if (FALSE === $ldap_connection){
die("<p>Failed to connect to the LDAP server: ". LDAP_HOSTNAME ."</p>");
}
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)){
die('<p>Failed to bind to LDAP server.</p>');
}
//------------------------------------------------------------------------------
// Get a list of all Active Directory users.
//------------------------------------------------------------------------------
$ldap_base_dn = 'DC=xyz,DC=local';
$search_filter = "(&(objectCategory=person))";
$result = ldap_search($ldap_connection, $ldap_base_dn, $search_filter);
if (FALSE !== $result){
$entries = ldap_get_entries($ldap_connection, $result);
if ($entries['count'] > 0){
$odd = 0;
foreach ($entries[0] AS $key => $value){
if (0 === $odd%2){
$ldap_columns[] = $key;
}
$odd++;
}
echo '<table class="data">';
echo '<tr>';
$header_count = 0;
foreach ($ldap_columns AS $col_name){
if (0 === $header_count++){
echo '<th class="ul">';
}else if (count($ldap_columns) === $header_count){
echo '<th class="ur">';
}else{
echo '<th class="u">';
}
echo $col_name .'</th>';
}
echo '</tr>';
for ($i = 0; $i < $entries['count']; $i++){
echo '<tr>';
$td_count = 0;
foreach ($ldap_columns AS $col_name){
if (0 === $td_count++){
echo '<td class="l">';
}else{
echo '<td>';
}
if (isset($entries[$i][$col_name])){
$output = NULL;
if ('lastlogon' === $col_name || 'lastlogontimestamp' === $col_name){
$output = date('D M d, Y # H:i:s', ($entries[$i][$col_name][0] / 10000000) - 11676009600); // See note below
}else{
$output = $entries[$i][$col_name][0];
}
echo $output .'</td>';
}
}
echo '</tr>';
}
echo '</table>';
}
}
ldap_unbind($ldap_connection); // Clean up after ourselves.
?>
User inventor96 has suggested using 11644473600 instead of 11676009600. I can confirm 11644473600 is correct in a Linux environment - my guess is that inventor96 is in a Windows environment.
Related
I Connect to a remote SSH device in Php.
I run a command that output is too long.
So I searched and understood I can save it via :
$my_array= explode("\n", $this->ssh->exec('some command'));
Its ok, Now I can display whole output via :
echo print_r($my_array);
Output is something like :
Array ( [0] => ath4_auth_mode=disabled [1] => wl_mac_deny= [2] => filter_dport_grp3= [3] => ses_script= [4] => http_redirect_port=3128 [5] => oet5_en=0 [6] => filter_dport_grp4= [7] => oet2_fragment=0 [8] =>
When I run :
echo count($my_array);
It displays me :
2200
So it is true, Now I wanna search for a specific text like name=,I want the value after equal in the array, I tried this :
$search_result = array_search("name=", $my_array);
But no chance, Even tried :
foreach($my_array as $cat) {
$cat = trim($cat);
if($cat == "name=") {
echo "hoola !";
} else {
echo ':-(';
}
Again no chance, How can I search for the name= and get the value after = ?
I suppose in your array value is not name= exactly. It can be name=something for example. In this case neither == nor in_array will find it. You need to use strpos:
foreach($my_array as $cat) {
$cat = trim($cat);
if(strpos($cat, "name=") === 0) {
echo "hoola !", $cat;
// add a break if value found so as not to search other elements
break;
} else {
echo ':-(';
}
}
I used === and 0 because name= must be in the beginning of your $cat.
I have this bit of code:
<?php
#
# Sample Socket I/O to CGMiner API
#
function getsock($addr, $port)
{
$socket = null;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false || $socket === null)
{
$error = socket_strerror(socket_last_error());
$msg = "socket create(TCP) failed";
echo "ERR: $msg '$error'\n";
return null;
}
$res = socket_connect($socket, $addr, $port);
if ($res === false)
{
$error = socket_strerror(socket_last_error());
$msg = "socket connect($addr,$port) failed";
echo "ERR: $msg '$error'\n";
socket_close($socket);
return null;
}
return $socket;
}
#
# Slow ...
function readsockline($socket)
{
$line = '';
while (true)
{
$byte = socket_read($socket, 1);
if ($byte === false || $byte === '')
break;
if ($byte === "\0")
break;
$line .= $byte;
}
return $line;
}
#
function request($cmd)
{
$socket = getsock('127.0.0.1', 4028);
if ($socket != null)
{
socket_write($socket, $cmd, strlen($cmd));
$line = readsockline($socket);
socket_close($socket);
if (strlen($line) == 0)
{
echo "WARN: '$cmd' returned nothing\n";
return $line;
}
print "$cmd returned '$line'\n";
if (substr($line,0,1) == '{')
return json_decode($line, true);
$data = array();
$objs = explode('|', $line);
foreach ($objs as $obj)
{
if (strlen($obj) > 0)
{
$items = explode(',', $obj);
$item = $items[0];
$id = explode('=', $items[0], 2);
if (count($id) == 1 or !ctype_digit($id[1]))
$name = $id[0];
else
$name = $id[0].$id[1];
if (strlen($name) == 0)
$name = 'null';
if (isset($data[$name]))
{
$num = 1;
while (isset($data[$name.$num]))
$num++;
$name .= $num;
}
$counter = 0;
foreach ($items as $item)
{
$id = explode('=', $item, 2);
if (count($id) == 2)
$data[$name][$id[0]] = $id[1];
else
$data[$name][$counter] = $id[0];
$counter++;
}
}
}
return $data;
}
return null;
}
#
if (isset($argv) and count($argv) > 1)
$r = request($argv[1]);
else
$r = request('summary');
#
echo print_r($r, true)."\n";
#
?>
Which outputs this information:
summary returned 'STATUS=S,When=1399108671,Code=11,Msg=Summary,Description=cgminer 4.3.0hf|SUMMARY,Elapsed=531,MHS av=453052.33,MHS 5s=537024.44,MHS 1m=458922.01,MHS 5m=375184.88,MHS 15m=201623.38,Found Blocks=0,Getworks=16,Accepted=518,Rejected=12,Hardware Errors=271,Utility=58.54,Discarded=276,Stale=0,Get Failures=0,Local Work=65806,Remote Failures=0,Network Blocks=1,Total MH=240524241.0000,Work Utility=5589.33,Difficulty Accepted=49008.00000000,Difficulty Rejected=448.00000000,Difficulty Stale=0.00000000,Best Share=93465,Device Hardware%=0.5450,Device Rejected%=0.9059,Pool Rejected%=0.9059,Pool Stale%=0.0000,Last getwork=1399108671|'
Array
(
[STATUS] => Array
(
[STATUS] => S
[When] => 1399108671
[Code] => 11
[Msg] => Summary
[Description] => cgminer 4.3.0
)
[SUMMARY] => Array
(
[0] => SUMMARY
[Elapsed] => 531
[MHS av] => 453052.33
[MHS 5s] => 537024.44
[MHS 1m] => 458922.01
[MHS 5m] => 375184.88
[MHS 15m] => 201623.38
[Found Blocks] => 0
[Getworks] => 16
[Accepted] => 518
[Rejected] => 12
[Hardware Errors] => 271
[Utility] => 58.54
[Discarded] => 276
[Stale] => 0
[Get Failures] => 0
[Local Work] => 65806
[Remote Failures] => 0
[Network Blocks] => 1
[Total MH] => 240524241.0000
[Work Utility] => 5589.33
[Difficulty Accepted] => 49008.00000000
[Difficulty Rejected] => 448.00000000
[Difficulty Stale] => 0.00000000
[Best Share] => 93465
[Device Hardware%] => 0.5450
[Device Rejected%] => 0.9059
[Pool Rejected%] => 0.9059
[Pool Stale%] => 0.0000
[Last getwork] => 1399108671
)
)
How can I get a specific value? For example, how can I output only '[MHS 15m]'
if $res is the variable containing the array you can get the value as
echo $res['SUMMARY']['MHS 15m'];
I would like to learn a smart way to unpack nested array. For instance, i have an array variable $rma_data['status'] which looks like below;
[status] => Array
(
[0] => Array
(
[created] => 1233062304107
[statusId] => 5
[statusName] => Open
)
[1] => Array
(
[created] => 1233061910603
[statusId] => 2
[statusName] => New
)
[2] => Array
(
[created] => 1233061910603
[statusId] => 1
[statusName] => Created
)
)
I would like to store the Created timestamps and statusId into an variables based on the condition like: if we find out there is "Open" status exist, we will use Open instead of "New" and "Created" . If there is only New and Created, we will use New instead .
Current version of my way to do that:
for($i=0; $i<count($rma_data['status']); $i++)
{
switch($rma_data['status'][$i]['statusId'])
{
case 5:
case 2:
case 3:
}
Any ideas?
For small to medium scale, what you already have looks good.
My only suggestions would be to use additional variables, for example the count and to unroll some of the compact code to be more efficient and readable.
For example:
$total=count($rma_data['status']);
for($i=0; $i<$total; $i++){
$x=$rma_data['status'][$i];
if($x['statusName']=='Open'){ // Use your criteria
$t=$x['created'];
//...Do Work
}
}
If you're really depended on these three specific values of statusName, a more straightforward and readable way to go about it would be to create an indexed array of status types which you can test more easily.
For example:
$rma_statuses = array();
foreach ((array)$rma_data['status'] as $status) :
$rma_statuses[$status['statusName']] = array(
'created'=>$status['created'],
'id'=>$status['statusId']
);
endforeach;
$rma_stauts = $rma_statuses['open'] ?: ($rma_statuses['new'] ?: $rma_statuses['created']);
// Do something with $rma_stauts['created'] and $rma_stauts['id']
I do not quite understand a necessary condition but it can be like this will help:
$searched_status_id = null;
$searched_timestamp = null;
foreach ($rma_data['status'] as $id => $status) {
if ((!$searched_timestamp && !$searchуd_status_id) ||
($status['statusName'] == 'New' || $status['statusName'] == 'Open')) {
$searched_timestamp = $status['created'];
$searched_status_id = $status['statusId'];
}
if ($status['statusName'] == 'Open') {
break;
}
}
if(is_array($rma_data['status'])){
//assuming there are only three values inside it
//case1
$open = ( $rma_data['status'][0]['statusName'] == 'Open' ||
$rma_data['status'][1]['statusName'] == 'Open' ||
$rma_data['status'][2]['statusName'] == 'Open');
//case2
$new = (!$open &&
($rma_data['status'][0]['statusName'] == 'New' ||
$rma_data['status'][1]['statusName'] == 'New' ||
$rma_data['status'][2]['statusName'] == 'New' ));
if($open){
echo 'open';
}elseif($new){
echo 'New';
}else{
echo 'None';
}
}
Second:
foreach($rma_data['status'] as $key => $val){
$statusName = $val['statusName'];
$newarray[$statusName] = $val;
}
echo '<pre>';
print_r($newarray);
if(array_key_exists('Open', $newarray)){
$created = $newarray['Open']['created'];
$statusId = $newarray['Open']['statusId'];
echo 'Open';
}
elseif(array_key_exists('New', $newarray)){
$created = $newarray['New']['created'];
$statusId = $newarray['New']['statusId'];
echo 'New';
}else{
echo "None";
}
Hi Guys have the following arrays:
Array
(
[0] => Array
(
[SERIAL] => 1
[SCORE_1] => 6.6490630173025
[SCORE_2] => 13.010510864225
[SCORE_3] => 7.5543177306323
[SCORE_4] => 4.1576775823101
[SCORE_5] => 5.3696604808987
)
[1] => Array
(
[SERIAL] => 2
[SCORE_1] => 11.861835175219
[SCORE_2] => 0
[SCORE_3] => 1.988806017442
[SCORE_4] => 0.97387338276326
[SCORE_5] => 4.0322016758707
)
I need to find the proportion of each element in its array i.e. for example SCORE_1/(SCORE_1+...+SCORE_5) and the serials remain as is. The PHP code looks like this:
$r = -1;
$namedDataArray = array();
for ($row = 2; $row <= $highestRow; ++$row) {
$dataRow = $objWorksheet->rangeToArray('A'.$row.':'.$highestColumn.$row,null, true, true, true);
if ((isset($dataRow[$row]['A'])) && ($dataRow[$row]['A'] > '')) {
++$r;
foreach($headingsArray as $columnKey => $columnHeading) {
if($dataRow[$row][$columnKey]==0){
$namedDataArray[$r][$columnHeading] = 0;
}elseif($columnHeading=='SERIAL' ){
$namedDataArray[$r][$columnHeading]=$dataRow[$row][$columnKey];
} else {
$x=(float)1.35;
$a=pow($dataRow[$row][$columnKey],$x);
$namedDataArray[$r][$columnHeading] = $a;
}
}
}
}
echo '<pre>';
print_r($namedDataArray);
echo '</pre><hr />';
I'm still new to php and have been struggling with this one. I used PHPExcel to read the file and raise each element to the power of 1.35 . I've used $total=array_sum($namedDataArray) but when I try to divide i.e$Score= $namedDataArray/$total it says unsupported operand type.
On this line :
$Score= $namedDataArray/$total
$namedDataArray is an array. PHP doesn't allow you to perform a division with an array, you have to put a number instead, for i.e. $Score = $namedDataArray[0]['SCORE_1'] / $total
I have an array that looks like this:
$rowarray(
[0] => [PID] => 97162 [TID] => 340 [StatsID] => 49678
[1] => [PID] => 97165 [TID] => 340 [StatsID] => 49673
[2] => [PID] => 97167 [TID] => 340 [StatsID] => 49675
[3] => [PID] => 97162 [TID] => 340 [StatsID] => 49679
)
Then my code looks like this:
$cntr=0;
foreach($rowarray as $row)
{
echo "<tr><td>$row[PID] $row[TID] $row[StatsID] </td></tr>";
$cntr++;
}
Two things I want to do I want to be able not print the duplicates in the array but print the additional column that has a different value. So my desired output would look like this.
97162 340 49678 49679
97165 340 49673
97167 340 49675
I started out with the array_unique() but that only returned:
97162 340 49678
Assuming only the StatsID changes (not clear from the question)
$map = array();
foreach($rowarray as $row){
$k = $row["PID"] . '-' . $row["TID"];
if( !isset( $map[$k] ) ){
$map[$k] = array();
}
array_push( $map[$k], $row["StatsId"] );
}
foreach($map as $k=>$v){
$row = explode( '-', $k );
echo "<tr><td>$row[0] $row[1] " . implode( " ", $v ) . " </td></tr>";
}
Here's what I'd do:
Start by sorting the array (using usort to sort by PID, then by TID)
Initialize "last" variables ($last_PID and $last_TID). They will store the respective values in the loop
In the loop, first compare the "current" variables to the "last" ones, if they're the same then just echo the StatsID value.
If they're not the same, output the <tr> (but not the final </tr>, so the first part of the loop can add more StatsID values if necessary)
Still inside the loop, after outputting everything, update the "last" variables.
After the loop, output the final </tr>
This may not be optimal, but I'm pretty sure it'll work.
Transfer the $rowarray structure into a map of maps of arrays, like this:
$rowarray = array(
array('PID' => 97162, 'TID' => 340, 'StatsID' => 49678),
array('PID' => 97165, 'TID' => 340, 'StatsID' => 49673),
array('PID' => 97167, 'TID' => 340, 'StatsID' => 49675),
array('PID' => 97162, 'TID' => 340, 'StatsID' => 49679)
);
$keys = array();
foreach ($rowarray as $row) {
if (!is_array(#$keys[$row['TID']])) {
$keys[$rowarray['TID']] = array();
}
if (!is_array(#$keys[$row['TID']][$row['PID']])) {
$keys[$row['TID']][$row['PID']] = array();
}
$keys[$row['TID']][$row['PID']][] = $row['StatsID'];
}
foreach ($keys as $pid => $pid_arr) {
foreach ($pid_arr as $tid => $tid_arr) {
echo "<tr><td>$tid $pid " . implode(' ', $tid_arr) . "</td></tr>";
}
}
See this code in action
As far as I can tell, the only way to do this would be to loop through the array creating a new unique array as you go.
$unique = array();
foreach ($row as $value)
{
$key = $value['PID'];
if (isset($unique[$key]))
{
$unique[$key]['StatsID'] .= ' ' . $value['StatsID'];
}
else
{
$unique[$key] = $value;
}
}
Now, $unique would give you the results you're looking for and you can loop through the unique array and output your results (I also added your counter if needed):
$count = count($unique);
foreach ($unique as $row)
{
echo "<tr><td>{$row['PID']} {$row['TID']} {$row['StatsID']} </td></tr>";
}