Solo un avviso su PHP 5.6 non è ancora possibile, è possibile specchiare l'esatta funzione PHP 5.6 ldap_escape()
utilizzando i metodi che ho creato di seguito, tenere presente che questo è pensato per l'uso in una classe. La risposta di cui sopra non ha esattamente la stessa funzione della funzione ldap_escape
, poiché in essa non vengono sfuggiti tutti i caratteri in una stringa esadecimale se non è stato specificato alcun flag, quindi questo sarebbe più adatto per una sostituzione in sostituzione di versioni precedenti di PHP, in un modo orientato agli oggetti.
Ho documentato ogni riga per una più facile comprensione di cosa sta succedendo. Scorri verso il basso per l'output.
Metodi (compatibile con PHP 5 o superiore):
/**
* Escapes the inserted value for LDAP.
*
* @param string $value The value to escape
* @param string $ignore The characters to ignore
* @param int $flags The PHP flag to use
*
* @return bool|string
*/
public function escapeManual($value, $ignore = '*', $flags = 0)
{
/*
* If a flag was supplied, we'll send the value
* off to be escaped using the PHP flag values
* and return the result.
*/
if($flags) {
return $this->escapeWithFlags($value, $ignore, $flags);
}
// Convert ignore string into an array
$ignores = str_split($ignore);
// Convert the value to a hex string
$hex = bin2hex($value);
/*
* Separate the string, with the hex length of 2,
* and place a backslash on the end of each section
*/
$value = chunk_split($hex, 2, "\\");
/*
* We'll append a backslash at the front of the string
* and remove the ending backslash of the string
*/
$value = "\\" . substr($value, 0, -1);
// Go through each character to ignore
foreach($ignores as $charToIgnore)
{
// Convert the characterToIgnore to a hex
$hexed = bin2hex($charToIgnore);
// Replace the hexed variant with the original character
$value = str_replace("\\" . $hexed, $charToIgnore, $value);
}
// Finally we can return the escaped value
return $value;
}
/**
* Escapes the inserted value with flags. Supplying either 1
* or 2 into the flags parameter will escape only certain values
*
*
* @param string $value The value to escape
* @param string $ignore The characters to ignore
* @param int $flags The PHP flag to use
* @return bool|string
*/
public function escapeWithFlags($value, $ignore = '*', $flags = 0)
{
// Convert ignore string into an array
$ignores = str_split($ignore);
$escapeFilter = ['\\', '*', '(', ')'];
$escapeDn = ['\\', ',', '=', '+', '<', '>', ';', '"', '#'];
switch($flags)
{
case 1:
// Int 1 equals to LDAP_ESCAPE_FILTER
$escapes = $escapeFilter;
break;
case 2:
// Int 2 equals to LDAP_ESCAPE_DN
$escapes = $escapeDn;
break;
case 3:
// If both LDAP_ESCAPE_FILTER and LDAP_ESCAPE_DN are used
$escapes = array_merge($escapeFilter, $escapeDn);
break;
default:
// Customize your own default return value
return false;
}
foreach($escapes as $escape)
{
// Make sure the escaped value isn't inside the ignore array
if(! in_array($escape, $ignores))
{
$hexed = chunk_split(bin2hex($escape), 2, "\\");
$hexed = "\\" . substr($hexed, 0, -1);
$value = str_replace($escape, $hexed, $value);
}
}
return $value;
}
Test (essere consapevoli che le costanti LDAP_ESCAPE sono disponibili solo in PHP 5.6):
// Value to escape
$value = 'testing=+<>"";:#()*\x00';
$php = ldap_escape($value, $ignore = '*');
$man = $this->escapeManual($value, $ignore = '*');
echo $php; // \74\65\73\74\69\6e\67\3d\2b\3c\3e\22\22\3b\3a\23\28\29*\5c\78\30\30
echo $man; // \74\65\73\74\69\6e\67\3d\2b\3c\3e\22\22\3b\3a\23\28\29*\5c\78\30\30
$php = ldap_escape($value, $ignore = '*', LDAP_ESCAPE_DN);
$man = $this->escapeManual($value, $ignore = '*', LDAP_ESCAPE_DN);
echo $php; // testing\3d\2b\3c\3e\22\22\3b:\23()*\5cx00
echo $man; // testing\3d\2b\3c\3e\22\22\3b:\23()*\5cx00
$php = ldap_escape($value, $ignore = '*', LDAP_ESCAPE_FILTER);
$man = $this->escapeManual($value, $ignore = '*', LDAP_ESCAPE_FILTER);
echo $php; // testing=+<>"";:#\28\29*\5cx00
echo $man; // testing=+<>"";:#\28\29*\5cx00
Github Gist collegamento : https://gist.github.com/stevebauman/0db9b5daa414d60fc266
Grazie mille, buona funzionalità. – Sbml
@daverandom dovresti aggiungere ai personaggi per evitare anche gli spazi (se sono il primo o l'ultimo carattere di un valore di attributo), ho ragione? Inoltre, il simbolo # deve essere preceduto da escape se è il primo carattere di un valore di attributo. http://www-03.ibm.com/systems/i/software/ldap/underdn.html – Eugenio
@Eugenio Interessante, sai se questo è standard LDAP o IBM specifico?Non ho mai visto quelle stipulazioni da nessuna parte prima. I documenti degli standard DAP/LDAP sono molto lunghi e molto astratti, è molto difficile trovare piccole informazioni su tali informazioni - e sto attraversando un momento difficile per metterle di nuovo in mano al momento: S – DaveRandom