I'm trying to accomplish something very similar to what this user was doing Here.
I followed the answer, but I could not get it working. Inside the Active directory, my memberOf field looks like this:
CN=$VPN Users,CN=Users,DC=iai,DC=pri,CN=$ITAR,CN=Users,DC=iai,DC=pri,CN=allsubscribers,CN=Users,DC=iai,DC=pri
My Filter that works is:
(&(objectCategory=person)(sAMAccountName=$p_username))
I'm trying to get the following to work:
(&(objectCategory=person)(sAMAccountName=$p_username)(memberOf=CN=$ITAR))
I have tried adding the full DN which is
CN=Users,DC=iai,DC=pri
to my filter as well, but I get:
array(1) { ["count"]=> int(0) }
as my response.
I'm using ldap 3
This is the partial Working authentication code written in php:
$login = ldap_bind( $url, "username#somedomain", $password );
$attributes = array("displayname", "mailnickname");
$filter = "(&(objectCategory=person)(sAMAccountName=$username))";
$result = ldap_search($url, "CN=Users,DC=iai,DC=pri", $filter, $attributes);
$entries = ldap_get_entries($url, $result);
What am I doing wrong?
Code Result From #DaveRandom
First Var dump:
string(49) "(&(objectCategory=person)(sAMAccountName=rmoser))"
array(2) {
["count"] => int(1)
[0] => array(8) {
["displayname"] => array(2) {
["count"] => int(1)
[0] => string(10) "Ryan Moser"
}
[0] => string(11) "displayname"
["memberof"] => array(4) {
["count"] => int(3)
[0] => string(36) "CN=$VPN Users,CN=Users,DC=iai,DC=pri"
[1] => string(31) "CN=$ITAR,CN=Users,DC=iai,DC=pri"
[2] => string(40) "CN=allsubscribers,CN=Users,DC=iai,DC=pri"
}
[1]=> string(8) "memberof"
["mailnickname"] => array(2) {
["count"] => int(1)
[0] => string(6) "rmoser"
}
[2] => string(12) "mailnickname"
["count"] => int(3)
["dn"] => string(36) "CN=Ryan Moser,CN=Users,DC=iai,DC=pri"
}
}
bool(false)
Second var_dump:
string(70) "(&(objectCategory=person)(sAMAccountName=rmoser)(memberof=*CN=$ITAR*))"
array(1) {
["count"] => int(0)
}
LDAP filters look for an exact match.
In order to match CN=$ITAR anywhere in a value, you will need to surround it with the filter wildcard character *.
Try this filter:
(&(objectCategory=person)(sAMAccountName=$p_username)(memberOf=*CN=$ITAR*))
Also don't forget that $ITAR is a valid variable name in PHP, so if you place that filter string in double quotes (which you would need to in order for $p_username to be interpolated) PHP will attempt to interpolate $ITAR as a variable as well, find that it (probably) doesn't exist and the end result will be that it gets stripped from the string.
$filter = "(&(objectCategory=person)(sAMAccountName=$p_username)(memberOf=*CN=\$ITAR*))";
A useful link for any question concerning a dynamic filter built with PHP is this.
Related
I want to split the parameters sent through URL.
Here is my text.
"mytext=A & B
Company&anothertext=20&texttwo=SampleText&array[1]=10&array[2]=20"
Expected output:
['mytext'=>'A & B Company', 'anothertext'=> 20, 'texttwo' =>
'SampleText', array[1] => 10, array[2] => 20 ].
I tried with explode('&', $params) and parse_str($url_components['query'], $params);
both giving only A as result. I need as 'A & B Company'. How to achieve this?
I think you're looking for parse_str()?
See: https://3v4l.org/L2UZa
The result is:
array(5) {
["mytext"] => string(2) "A "
["B_Company"] => string(0) ""
["anothertext"]=> string(2) "20"
["texttwo"] => string(10) "SampleText"
["array"] => array(2) {
[1] => string(2) "10"
[2] => string(2) "20"
}
}
This differs slight from what you want because of the &. if you could replace the & with the URL encoded version %26 it would work.
See: https://3v4l.org/OXfsD
The result now is:
array(5) {
["mytext"] => string(2) "A & B_Company"
["anothertext"]=> string(2) "20"
["texttwo"] => string(10) "SampleText"
["array"] => array(2) {
[1] => string(2) "10"
[2] => string(2) "20"
}
}
Basically the & is an anomaly, it shouldn't have been there in the first place. URL query parameters should be made with urlencode(), or something equivalent, which would have replace & by %26.
i want to edit a script i found online. is has an hardcoded array like this.
$servers = array(
'Google Web Search' => array(
'ip' => '',
'port' => 80,
'info' => 'Hosted by The Cloud',
'purpose' => 'Web Search'
),
'Example Down Host' => array(
'ip' => 'example.com',
'port' => 8091,
'info' => 'ShittyWebHost3',
'purpose' => 'No purpose'
)
);
Result:
array(2) {
["Google Web Search"]=>
array(4) {
["ip"]=>
string(0) ""
["port"]=>
int(80)
["info"]=>
string(19) "Hosted by The Cloud"
["purpose"]=>
string(10) "Web Search"
}
["Example Down Host"]=>
array(4) {
["ip"]=>
string(11) "example.com"
["port"]=>
int(8091)
["info"]=>
string(14) "ShittyWebHost3"
["purpose"]=>
string(10) "No purpose"
}
}
I put this data in a database and want to make the same array but i dont seem to get it working
This is the code i added to make an array:
$query ="SELECT name, ip, port, hosting FROM sites";
$select = $conn->prepare($query);
$select->execute(array());
$testing = array();
while($rs = $select->fetch(PDO::FETCH_ASSOC)) {
$testing[] = array($rs['name'] => array('ip'=> $rs['ip'], 'port'=> $rs['port'], 'hosting'=> $rs['hosting']));
}
The result from this is:
array(2) {
[0]=>
array(1) {
["Google Web Search"]=>
array(3) {
["ip"]=>
string(10) "google.com"
["port"]=>
string(2) "80"
["hosting"]=>
string(19) "Hosted by The Cloud"
}
}
[1]=>
array(1) {
["Example Down Host"]=>
array(3) {
["ip"]=>
string(11) "example.com"
["port"]=>
string(2) "09"
["hosting"]=>
string(14) "ShittyWebHost3"
}
}
}
is there a way to make the bottom array the same as the top array, i dont want to edit the whole script, this seems easier.
You are appending a new integer indexed element with [] and then adding 2 nested arrays. Instead, add the name as the key:
$testing[$rs['name']] = array('ip'=> $rs['ip'],
'port'=> $rs['port'],
'hosting'=> $rs['hosting']);
Since you specify the columns in the query and they are the same as the array keys, then just this:
$testing[$rs['name']] = $rs;
When you assign a value to an array you use the syntax $arr[key] = $value. If you omit the key during the assignment, $value will be assigned to the next available integer key of the array, starting from 0.
This is an example of how it works:
$arr = array();
$arr[] = 'one';//Empty, so insert at 0 [0=>'one']
$arr[] = 'two';//Last element at 0, so use 1 [0=>'one',1=>'two']
$arr[6]= 'three';//Key is used, so use key [0=>'one',1=>'two',6=>'three']
$arr[] = 'four';//Max used integer key is 6, so use 7
print_r($arr);//[0=>'one',1=>'two',6=>'three',7=>'four']
So, when in your code you are using
$testing[] = array(
$rs['name'] => array(
'ip'=> $rs['ip'],
'port'=> $rs['port'],
'hosting'=> $rs['hosting']
)
);
You are assigning the newly created array to the positions 0,1,2,..N.
To avoid this, just specify the key explicitly, using the value you really want, like
$testing['name'] => array(
'ip'=> $rs['ip'],
'port'=> $rs['port'],
'hosting'=> $rs['hosting']
);
You can read more about arrays in the documentation
Side note
If you don't mind having an extra column in the generated arrays, you can rewrite entirely your code this way:
$query ="SELECT name, ip, port, hosting FROM sites";
$results = $conn->query($query)->fetchAll(PDO::FETCH_ASSOC);
$testing = array_column($results,null,'name');
It's slightly slower, but very handy in my opinion, PDOStatement::fetchAll retrieves all the data at once and array_column using null as second parameter does reindex the array with the wanted column as key.
PDOStatement::fetchAll
array_column
I am working on a API project where i get an XML Object as a response. The response can contain one or more products in the NewOrder object(below).However when i try to display the info using a foreach loop it breaks if the only has one entry. i guess it is because the index [0] does not exist in the object.how can i through the xml object and display since there is no [0] i the object. OR how do i add the index [0] in the object.
object(stdClass)#49 (1) {
["NewOrder"] => object(stdClass)#50 (12) {
["BTN"] => string(10) "XXXXXXXXXXXXXXXxx"
["PreOrderTransactionId"] => string(22) "XXXXXXXX"
["PartnerOrderId"] => string(17) "XXXXXXXXXXX"
["QwestOrderId"] => string(9) "N57395699"
["SalesCode"] => string(7) "XXXXXXXX"
["OrderStatus"] => string(7) "Pending"
["OrderStatusCode"] => string(4) "OPEN"
["OrderStatusSourceSystem"] => string(5) "CPLUS"
["OrderStatusMessage"] => string(0) ""
["OrderStatusDate"] => string(10) "2013-12-09"
["OrderStatusTime"] => string(8) "08:02:30"
["ProductFamily"] => array(3) {
[0] => object(stdClass)#51 (2) {
["ProductFamilyName"] => string(7) "BUNDLE+"
["ProductLines"] => object(stdClass)#52 (3) {
["WTN"] => string(10) "3033689919"
["AppointmentDate"] => string(10) "2013-12-20"
["Products"] => object(stdClass)#53 (5) {
["ProductName"] => string(36) "INTERNET 100+ MBPS & HOME PHONE PLUS"
["Usoc"] => string(5) "BBBVC"
["Quantity"] => string(1) "1"
["Action"] => string(1) "I"
["Status"] => string(4) "OPEN"
}
}
}
}
}
}
I have tried the following but it didn't work:
if (!is_array($this->Orders->NewOrder)) {
$this->Order->NewOrder = array($this->Orders->NewOrder["NewOrder"]);
}
foreach ($this->Orders->NewOrder as $order){?>
I am getting the following error:
Fatal error: Cannot use object of type stdClass as array in
I think your NewOrder is only an array if it contains more than one object. Use something like this before your loop:
if (!is_array(yourObject["NewOrder"])) {
yourObject["NewOrder"] = array(yourObject["NewOrder"]);
}
The SoapClient has an option that always creates the array, even if here is only one element.
return new SoapClient(
'...',
array(
'location' => '...',
/.../
'features' => SOAP_SINGLE_ELEMENT_ARRAYS
)
);
Need some quick advice I am trying to access a object array but I am struggling, please see the below array. It starts off an object I would normally user $result->_messages->token but it doesnt work I have trawled google and this site but cant access the token.
object(Zend_Auth_Result)#76 (3) {
["_code":protected] => int(1)
["_identity":protected] => string(9) "3232323233"
["_messages":protected] => array(2) {
["user"] => object(stdClass)#71 (13) {
["id"] => string(9) "232323332"
["name"] => string(14) "John Smith"
["first_name"] => string(5) "John"
["last_name"] => string(8) "Smith"
["link"] => string(41) "http://www.facebook.com/"
["username"] => string(17) "john.smith"
["location"] => object(stdClass)#68 (2) {
["id"] => string(0) ""
["name"] => NULL
}
["email"] => string(22) "john#doe.com"
["timezone"] => int(1)
["locale"] => string(5) "en_US"
["verified"] => bool(true)
["updated_time"] => string(24) "2012-06-21T13:57:12+0000"
}
["token"] => string(109) "AAAGIFdDivU4BAMoxyHT3bqY8eBGhnWo9YKM1szHZAnWgY10AIBgxz9LeNCeA2HV9Yhkp8uM5Aq0WR39ZBdcnOa4MxXVI22rnmFKNbYdQZDZD"
}
}
Any advice any body?
Cheers
J
From the ZF Reference Guide on Naming Conventions:
For instance variables that are declared with the "private" or "protected" modifier, the first character of the variable name must be a single underscore. This is the only acceptable application of an underscore in a variable name. Member variables declared "public" should never start with an underscore.
So you cannot access _messages directly from outside the Zend_Auth_Result instance, because it is protected. You have to use the getter for that property.
See the API Docs for Zend_Auth_Result
$messages = $zendAuthResult->getMessages();
$token = $messages['token'];
_messages is protected so it's impossible to call upon that variable from outside this (or extended) class, check whether a method exists for the class to get the variable in the array
How can I get the follow thing done.
The follow array I have:
array(2) { [0] => array(3) {
["id"] => string(1) "5"
["avatar"] => string(15) "4e0d886ee9ed3_n"
["username"] => string(5) "testuser1"}
[1] => array(3) {
["id"] => string(1) "1"
["avatar"] => string(15) "4e25bc58b6789_w"
["username"] => string(6) "testuser2"
}
}
I want to create a array with just one user in it, but it has to be random. It can be like
user with id=5, id=1 or a hole other user (when there are more users).
Did you try, something like:
$rand = array_rand($your_array);
http://fr2.php.net/manual/en/function.array-rand.php
Try using array_rand().
$randomKey = array_rand($yourArray);
$randomUserId = $yourArray[$randomKey]['id'];
Simple.
$rand_user = array_rand($your_array);
PHP Manual: array_rand