Connect Office 365 use POP3 - PHP - php

I have a trouble with Office 365 use POP3 method.
Currenty, I can't connect to this server:
outlook.office365.com
port:995
This is my code example:
<?php
$host = 'outlook.office365.com';
$port = '995';
$username = 'outlook_mail';
$password ='password';
$mbox = imap_open('{'.$host.':'.$port.'/pop3/ssl/novalidate-cert}', $username, $password);
echo "<h1>Mailboxes</h1>\n";
$folders = imap_listmailbox($mbox, "{".$host.":".$port ."}", "*");
if ($folders == false) {
echo "Call failed<br />\n";
} else {
foreach ($folders as $val) {
echo $val . "<br />\n";
}
}
echo "<h1>Headers in INBOX</h1>\n";
$headers = imap_headers($mbox);
if ($headers == false) {
echo "Call failed<br />\n";
} else {
foreach ($headers as $val) {
echo $val . "<br />\n";
}
}
imap_close($mbox);
If I change port to 993, it's OK.
Anyone know this problem? Many thanks!

I think it's because pop3 is on port 993 and imap is on port 995.
Have you tried with imap in your host config instead of pop3 when using port 995 ?
$mbox = imap_open('{'.$host.':'.$port.'/imap/ssl/novalidate-cert}', $username, $password);
Source : https://www.php.net/manual/en/function.imap-open.php (flag section)

Related

PHP LDAP Connection

I was sent the following LDAP parameters, but am not sure how to establish a connection in PHP. I'm not sure which PHP function to use with each set of parameters. Here are the parameters I was given:
Server: ldaps://the_server.com:636
root DN: dc=the_info,dc=more_info,dc=com
User search base: ou=CompanyUsers
User search filter: sAMAccountName={0}
Group search base: OU=Security,OU=CompanyGroups
Group search filter: cn={0}
Group membership: Group membership attribute = memberOf
Display Name LDAP attribute: displayname
Email Address LDAP atribute: mail
If someone could provide a php script for me that would be great! This is my first time using LDAP and still do not understand all these parameters.
Following is the working code for linux base ldap.
It might be helpful to you.
<?php
$username = 'uid=amitkawasthi,ou=CompanyUsers,dc=the_info,dc=more_info,dc=com';
$password= 'test';
$ds=ldap_connect("the_server.com, 636");
echo $ds;
if ($ds) {
echo "Binding ...";
$r=ldap_bind($ds, $username, $password);
if ($r)
{
$sr=ldap_search($ds,"ou=CompanyUsers,dc=the_info,dc=more_info,dc=com", "uid=amitkawasthi");
$entry = ldap_first_entry($ds, $sr);
$attrs = array();
$attribute = ldap_first_attribute($ds,$entry,$identifier);
while ($attribute) {
$attrs[] = $attribute;
$attribute=ldap_next_attribute($ds,$entry,$identifier);
}
echo count($attrs) . " attributes held for this entry:<p>";
$ldapResults = ldap_get_entries($ds, $sr);
//for ($item = 0; $item < $ldapResults['count']; $item++) {
// for ($attribute = 0; $attribute < $ldapResults[$item]['count']; $attribute++) {
//echo $data = $ldapResults[$item][$attribute];
echo $data = $ldapResults[0][$attribute];
echo $data.": ".$ldapResults[0][$data][0]."<br>";
//}
///echo '<hr />';
echo "OK";
}
else
{
echo "Fail";
}
}
?>
============================

LDAP query string get information from AD

I am trying to get information out of AD using LDAP query and PHP. Below is my code and it seems like nothing returns from this code. I have an issue with code block in if (TRUE === $bind). Would anyone help me to point out the error?
This is php code for listing all users in AD and showing up on WordPress page.
$ldap_password = "pass";
$ldap_username = "username";
$person = "Scott";
$ldap_connection = ldap_connect("host1");
if (FALSE === $ldap_connection) {
echo "Connection is failed<br />";
}
else {
echo "Connection is successful<br />";
}
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);
$bind = ldap_bind($ldap_connection, $ldap_username, $ldap_password);
if (TRUE === $bind) {echo "Binding is successful<br />";}
else {echo "Binding is unsuccessful<br />";}
if (TRUE === $bind) {
echo "Retrieving...<br />";
$baseDN = "dc=domain,dc=com";
$filter="(|(sn=$person*)(givenname=$person*))";
$justthese = array("ou");
$sr = ldap_list($ldap_connection, $baseDN, $filter, $justhese);
$info = ldap_get_entries($ldap_connection,$sr);
echo $info["count"]."Hello<br />";
for ($i=0; $i < $info["count"]; $i++) {
echo $info[$i]["ou"][0];
}
ldap_unbind($ldap_connection);
echo "Unbinding is completed<br />";
}
This is what shows up on the page
Connection is successful
Binding is successful
Retrieving…
Hello
Unbinding is completed
Thanks

PHP ftp_chdir not visibly changing the directory

I started on an ftp project and I'm having trouble getting the directory to reload. My code says that it successfully changed, but I am assume that it only changes server-side. I need the browser to change the directory as well.
PS: How can I download via FTP through the browser? When I test locally it writes the files to the root directory but when connected remotely I don't know where they go. There is no indication of files being downloaded.
Any help would be greatly appreciated! And please, if you have any tips for me that would be great. I'm still pretty new at this but I'm trying my best.
<?php
session_id('logon');
session_start();
if (isset($_POST['connect']))
{
$_SESSION['port'] = $_POST['port'];
$_SESSION['server'] = $_POST['server'];
$_SESSION['user'] = $_POST['user'];
$_SESSION['password'] = $_POST['password'];
}
$port = $_SESSION['port'];
$server = $_SESSION['server'];
$user = $_SESSION['user'];
$pass = $_SESSION['password'];
$connection = ftp_connect($server)
or die("Couldn't connect!");
$logon = ftp_login($connection,$user,$pass)
or die("Couldn't login!" . $server ."<br>". $port);
$workingDir = ftp_pwd($connection);
echo "You are in $workingDir<br><br>";
$dirList = ftp_nlist($connection, ".");
foreach($dirList as $item)
{
$res = ftp_size($connection, $item);
if ($res != "-1")
{
echo "<a href='?download=$item'>$item</a><br>";
if (isset($_GET['download']))
{
if ($_GET['download'] == $item)
{
include('include/download.php');
}
}
}
else
{
$directory = $item;
echo "<a href='?change=$directory'>$directory</a><br>";
if ($_GET['change'] == $directory)
{
if (ftp_chdir($connection, $directory))
{
echo "Changed to " . ftp_pwd($connection) . "!<br>";
$dirList = ftp_nlist($connection, ".");
header("Refresh:0");
}
else
{
echo "Failed to change to $directory";
}
}
}
}
ini_set('error_reporting', E_ALL);
ftp_quit($connection);
?>
Solved! Added a directory.php file with
$getChange = $_GET['change'];
if (ftp_chdir($connection, $getChange))
{
echo "Changed to " . ftp_pwd($connection) . "!<br>";
$dirList = ftp_nlist($connection, ".");
}
else
{
echo "Failed to change to $getChange";
}
$workingDir = ftp_pwd($connection);
echo "You are in $workingDir<br><br>";
$dirList = ftp_nlist($connection, ".");
foreach($dirList as $item)
{
$res = ftp_size($connection, $item);
if ($res != "-1")
{
echo "<a href='?download=$item'>$item</a><br>";
if (isset($_GET['download']))
{
if ($_GET['download'] == $item)
{
include('include/download.php');
}
}
}
else
{
echo "<a href='directory.php?change=$item'>$item</a><br>";
}
}

LDAP can't bind to server after turn on LDAP Server Signing Requirements

As the title, any solution for it?
I used PHP function ldap_bind($server, $username, $password) to bind the ldap server,
if the signing requirement is 'none', it's working,
but if changed to 'Require signature', ldap_bind return fail.
so how can I bind to LDAP server using PHP with 'Require signature'?
try This Code I am using that code for my Local LDAP SERVER
<?php
$ldapconfig['host'] = 'localhost';
$ldapconfig['port'] = NULL;
$ldapconfig['basedn'] = 'dc=test,dc=example,dc=com';
$ldapconfig['authrealm'] = 'Nisarg';
function ldap_authenticate() {
global $ldapconfig;
global $PHP_AUTH_USER;
global $PHP_AUTH_PW;
//$PHP_AUTH_USER = "john";
//$PHP_AUTH_PW = "esparkinfo";
$PHP_AUTH_USER = $_SERVER['PHP_AUTH_USER'];
$PHP_AUTH_PW = $_SERVER['PHP_AUTH_PW'];
if ($PHP_AUTH_USER != "" && $PHP_AUTH_PW != "") {
$ds=#ldap_connect($ldapconfig['host'],$ldapconfig['port']);
$r = #ldap_search( $ds, $ldapconfig['basedn'], 'uid=' . $PHP_AUTH_USER);
if ($r) {
$result = #ldap_get_entries( $ds, $r);
if ($result[0]) {
if (#ldap_bind( $ds, $result[0]['dn'], $PHP_AUTH_PW) ) {
return $result[0];
}
}
}
}
header('WWW-Authenticate: Basic realm="'.$ldapconfig['authrealm'].'"');
header('HTTP/1.0 401 Unauthorized');
return NULL;
}
if (($result = ldap_authenticate()) == NULL) {
echo('Authorization Failed');
exit(0);
}
echo('Authorization success');
echo "<pre>";
print_r($result);
echo"<pre>";
print_r($_SERVER);
die;
?>
You need to implement your code to use LDAPS (LDAP over TLS).

Search Multiple AD DN's withPHP

Hi I have a PHP script that searches my AD, but how can I make it search more than one DN. I've tried the exmample on php.net and cannot get it to work.
Help appreciated:
<?php echo "<?xml version='1.0' encoding='utf-8' ?>" ?><?php echo "<ul class='LSRes'>" ?>
<?php
if( isset($_GET['q']) &&!empty($_GET['q']) ){
// all your ldap code
// Designate a few variables
$host = "10.10.10.10"; // Add in your AD host name or IP
$user = "DOMAIN\user"; // Add in your AD access account user name
$pswd = "password"; // Add in your AD access account user name password
$ds = ldap_connect($host)
or die( "Could not connect!" );
// Set version number
ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3)
or die ("Could not set ldap protocol");
// Binding to ldap server
$bd = ldap_bind($ds, $user, $pswd)
or die ("Could not bind");
// Create the DN - Add in the OU of your AD
$dn[] = "OU=uk,OU=Accounts,DC=mywebsite,DC=com";
$dn[] = "OU=us,OU=Accounts,DC=mywebsite,DC=com";
$id[] = $ds;
$id[] = $ds;
//$filter = 'samaccountname='.$_POST['username'];
$filter = "(|(givenName=".$_GET['q']."*) (sn=".$_GET['q']."*) (displayname=".$_GET['q']."*) (samaccountname=".$_GET['q']."*))";
$result = ldap_search($id,$dn,$filter);
$search = false;
foreach ($result as $value) {
if(ldap_count_entries($ds,$value)>0){
$search = $value;
break;
}
}
if($search){
$entries = ldap_get_entries($ds, $search);
}
if ($entries["count"] > 0) {
for ($i=0; $i<$entries["count"]; $i++) {
echo "<span class='LSstyle'>Name: <strong>".$entries[$i]["displayname"][0]." ".$entries[$i]["sn"][0]."</strong></span><br />";
echo "<span class='LSstyle'>Short name: <strong>".$entries[$i]["samaccountname"][0]."</strong></span><br />";
echo "<span class='LSstyle'>Phone: <strong>".$entries[$i]["telephonenumber"][0]."</strong></span><br />";
echo "<span class='LSstyle'>Title: <strong>".$entries[$i]["title"][0]."</strong></span><br />";
echo "<span class='LSstyle'>Dept: <strong>".$entries[$i]["department"][0]."</strong></span></p>";
}
} else {
echo "<span class='LSstyle_noresults'><strong>No results found</strong></span>";
}
ldap_unbind($ad);
}
?>
Seeing as ldap_search does not take an array as $base_dn, you will probably have to loop $dn as Viper_Sb suggested.

Categories