Read from LDAP based on ObjectGUID with PHP - php

I've created a web based administration system for our AD environment. I'm having issues currently trying to read directory entries based on objectguid. Is this possible to do?
I'm able to do something like:
$filter = "(&(objectclass=*)(objectguid=$objectguid))";
$search = ldap_read($ds, $dn, $filter);
With the $dn being something like CN=Test,OU=Group,DC=OCJ,DC=Domain,DC=Com. So I know it's accepting the guid, but I'd like to skip the $dn part, reading with ONLY the GUID.
Any suggestions? I can't find anything saying that ldap_read can read directly with GUID.

To read only the guid, give the third parameters that controls what attributes you want returned. The second $dn parameter is mandatory, it specifies the search scope, and you can always set it to the root_dn if you want to be less specific.
If you want no dn entries in the result set array, that is not possible directly as this is the API of the ldap_read function, but you can filter them out by traversing the array (map).
$search = ldap_read($ds, $root_dn, $filter, array('objectguid'));

You can bind by GUID also. Set your $dn to <GUID={guid-goes-here}>

Related

How to get wiki pageid?

I am currently using PHP to make a chatbot that can do research on wiki,and send the introduction automaitcally, here is the wiki api json(https://zh.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro&explaintext&redirects=1&titles=search_title)
{"batchcomplete":"","query":{"redirects":[{"from":"search_title","to":"search_title"}],"pages":{"541":{"pageid":541,"ns":0,"title":"search_title","extract":"ablablablablablablablablablablabla。"}}}}
I want to get the extract part by
$data=$jsondata['query']['pages']["541"]['extract'];
but it seems like I need to have to know pageid is '541' first, is there method that I can know padeid in first??
You should look for a way to iterate over array values without knowing the keys. I'm not a PHP developer but I think a foreach will do:
foreach ($jsondata['query']['pages'] as $pageid => $pagedata) {
extract = $pagedata['extract']
...
}
There is also a formatversion=2 parameter for the query that, among other things, will make
action=query's "pages" be an array, instead of an object with page ids
as keys that can be difficult to iterate.

Wordpress - Passing variable to function - how to correctly pass list of numbers

I am trying to restrict content within a Wordpress template file and am using a plugin called Paid Memberships Pro to do so.
The code below restricts content to members with 'levels' of 1 or 2.
if(pmPro_hasMembershipLevel(array(1,2))){
restricted content goes here
}
The problem comes when I try to use a variable to provide the levels. These levels are held in a custom field group 'restrictions' with field name 'pmpro_id'. I access these levels within the template using...
foreach($restrictions as $restriction){
$temp=get_field('pmpro_id', $restriction->ID );
$temp_array[]=$temp;
}
$levels=implode(',', $temp_array);
If I then pass $levels to pmPro_hasMembershipLevel, this works fine if there is only one level but fails if there are 2 or more. I believe this is because the variable type is then a string rather than integer? I had previously tried to pass the $temp_array directly though I felt this wouldn't work and was correct.
I realise this is probably PHP 101. I have searched but don't really know what I'm looking for to be honest! I am not a developer and this is the last thing holding me back from finishing this project so ANY help anyone could provide would be brilliant. Thanks in advance.
You don't need to implode $temp_array if pmPro_hasMembershipLevel accepts array as its argument. When you implode an array you get string as a return value — that's not what you want here. If you think that the issue might be with the type of values, then you can try to cast them to integers, like this $temp_array[]= (int) $temp;

LDAP search multiple DNs in PHP

According to these posts in the php.net manual it should be possible to supply multiple DNs for ldap_search().
However, I can't get the below implementation to work:
$dn[] = 'ou=Red,ou=Teams,ou=Users,dc=example,dc=org';
$dn[] = 'ou=Green,ou=Teams,ou=Users,dc=example,dc=org';
$dn[] = 'ou=Blue,ou=Teams,ou=Users,dc=example,dc=org';
$query = ldap_search($conn, $dn, $filter, $attrs);
Everything is fine when passing through any of the individual DN strings, but supplying an array will error with message: 'No such object'.
An obvious work around for this is to loop over my DN array to fire off separate queries and push everything in to one array. I'd like to avoid having to do that, since I'm bringing back 8000+ objects in one DN (Paginated - Example 2) and ~300 in each of the other two.
Is it actually possible to search multiple DNs?
Perhaps special syntax (symbol/character) within the single DN string?
Search requests must contain a minimum the following parameters:
the base object at which the search starts (no objects above the base objects are returned)
the scope of the search: base is the base object itself, one is the base object and
one level below the base object, sub is the base object and all entries below the base object.
a filter which limits the entries returned to those entries which match the assertion in the
filter
A list of attributes can also be supplied, though many, but not all, LDAP APIs will request all
user attributes if none are supplied in the search request.
In the case listed, set the base object to ou=users,dc=example,dc=com and use an appropriate
filter. If the LDAP client must restrict the returned entries to entries that are subordinate
to ou=red, ou=green, or ou=blue it may be possible to use a compound extensible match filter
like (&(objectClass=inetOrgPerson)(|(ou:dn:=red)(ou:dn:=green)(ou:dn:=blue))) - use the correct object
class if the data does not use inetOrgPerson. All LDAP-compliant servers support extensible
match filters, but non-compliant servers may not support this standard filter.
It is not possible to use multiple base objects, the scope parameter defines how many subordinate
levels, if any, are examined below the base object.
see also
LDAP: Mastering Search Filters
LDAP: Search best practices
LDAP: Programming practices
Did you see this in the manual?
Those arrays must be of the same size as the link identifier array since the first entries of the arrays are used for one search, the second entries are used for another, and so on.
Basically, your $conn variable needs to be an array of connections equal to the size of your $dn array.
If you have 3 elements in your $dn array, you need 3 elements in your $conn array:
$ds = ldap_connect($ldapserver);
$dn[] = 'ou=Red,ou=Teams,ou=Users,dc=example,dc=org';
$dn[] = 'ou=Green,ou=Teams,ou=Users,dc=example,dc=org';
$dn[] = 'ou=Blue,ou=Teams,ou=Users,dc=example,dc=org';
// Match connection elements to count of dn elements
for($x=0, $x < count($dn), $x++)
{
$conn[] = $ds;
}
$query = ldap_search($conn, $dn, $filter, $attrs);

play with PHP GET

can anyone help me to play with GET urls for example I have a link like this:
?id=5&lang=1
So my question is how can I make this one:
?id=5,1
I don't want to show the &lang, only I want is that the &lang to replace with , "comma" can anyone help me?
You can use mod_rewrite to rewrite ?id=5,1 to ?id=5&lang=1 internally.
Otherwise, the value of id will be 5,1. Your application would then need to know that id contains more than the id. It could then parse out the language from the id. However, this will become confusing when you introduce more parameters.
Assuming you have already built the URL in the way you have specified, you can break the id field based on the comma and extract the real id and lang field
$urlPieces = explode(",", $_GET['id']);
$id = $urlPieces[0];
$lang = $urlPieces[1];
You are able to do this, but it's not very clean, in terms of the proper $_GET variable values. The solution automatically type casts the values to integers:
sscanf($_GET['id'], '%d,%d', $id, $lang);
// $id = int(5)
// $lang = int(1)
Two solutions:
Firstly, you could simply reformat the parameters when they arrive in your PHP program. With ?id=5,1, you'll get a PHP $_GET array with id '5,1'. This you can simply split using the explode() function to get the two values you want.
The second solution is to use the Apache mod_rewrite feature, to modify the URL arguments before they arrive at PHP. For this, you'll need to understand regular expressions (regex), as mod_rewrite uses this for it's work. You should google 'mod_rewrite' and 'regex' to find out more.
However mod_rewrite is typically used to get rid of GET arguments entirely. For example the URLs of the questions on this site do not have any get arguments, but the server translates the arguments between the slashes into GET arguments. This is considered better practice than simply than changing how the arguments look, as it is more user-friendly and SEO friendly.
Hope that helps.
$id = $id . ',' . $lang;
<a href="?<?php echo $id; ?>">

How to search through POSTed form values using regex and return results

I'm working on a project where all of the members and their info are stored in a JSON file. I'm in the process of creating a search form and I need help on how to iterate through the members and check to see if there's an exact match or a similar match.
The members are stored in a SESSION variable:
$_SESSION['members'] = json_decode($jsonFile);
but I'm uncertain how to use regex to check for matches that are similar (and not just exact). For example, if a member's name is "Jonathan", I'd like that result to be returned even if the user searches "Jon". Is regex the correct approach? Any help will be greatly appreciated - thank you!
-Manoj
I think I'd be using a database to store the data rather than JSON so that you can use the LIKE searches, e.g.
SELECT * FROM users WHERE name LIKE 'Jon%'
If you absolutely have to use JSON you could loop through all members and use a regexp like
preg_match('/^'.$term.'.*/i', $element, $matches);
to check them all.
If the $jsonFile contents is an array of some sort, you may find preg_grep() of use, though it doesn't work on multidimensional arrays. You might have have to loop over each individual member record and grep the relevant fields yourself, something like:
foreach ($_SESSION['members'] as $idx => $member) {
... match relevant fields...
}

Categories