SMsoft – informatica e dintorni

varie su OSX, GNU/Linux e l'Open Source

Convertire BR multipli in un solo BR

E’ possibile utilizzare una semplice espressione regolare per sostituire occorrenze multiple di BR in un solo BR.
Ad esempio potrei trasformare velocemente:

 html |  copy code |? 
1
prova


prova

in
 html |  copy code |? 
1
prova
prova

con la seguente espressione regolare:
 php |  copy code |? 
1
preg_replace("/(\s*)+/", "
", $input);

Con lo stesso sistema possiamo convertire gli accapo (\n) multipli.
Ad esempio avendo:

 html |  copy code |? 
1
prova
2
3
4
prova

posso trasformarlo in
 html |  copy code |? 
1
prova
2
prova

con la funzione:
 php |  copy code |? 
1
 preg_replace("/(\n\s*)+/", "\n", $sinput);

Errore open_basedir con eAccelerator 0.9.6

C’è un piccolo errore nella versione 0.9.6 di eAccelerator che non consente il corretto funzionamento dell’impostazione open_basedir di PHP.
In pratica pur essendo tutto correttamente configurato, PHP continua a visualizzare un errore relativo al fatto che lo script non è nel path indicato in open_basedir, anche se non è così. L’errore è del tipo “Fatal error: …….. open_basedir restriction.

La soluzione è davvero semplice. Basta variare la riga 1156 del file eaccelerator.c da

 php |  copy code |? 
1
  if (PG(open_basedir) && php_check_open_basedir(realname TSRMLS_CC)) { 

a
 php |  copy code |? 
1
  if (PG(open_basedir) && php_check_open_basedir(file_handle->filename TSRMLS_CC)) {

e poi ricompilare eAccelerator con il classico:
 bash |  copy code |? 
1
./configure
2
make clean
3
make
4
make install

disabilitare open_basedir su isp-cp (ispcontrol)

Per disabilitare la direttiva open_basedir di un dominio attivato con isp-cp, bisogna far riferimento ai file:
/var/www/fcgi/_NOME_DOMINIO_/php5/php.ini
/etc/apache2/sites-enabled/ispcp.conf

Basta cercare le righe relative ad open_basedir e poi commentarle. Dopo tale operazione è necessario riavviare apache

ispCP Warning… has been disabled for security reasons in

Utilizzando ispCP potreste notare un messaggio nel log degli errori (es /var/log/apache2/users/xxxxxxxx-error.log) tipo:
Warning… has been disabled for security reasons in….
Questo è legato alla disabilitazione di alcune funzioni php. Per riattivarle, basta modificare la direttiva:
disable_functions
che si trova nel file di configurazione di PHP in:
/var/www/fcgi/xxxxxx/php5/php.ini
dove xxxxxx è il nome a dominio

Verificare in PHP se una stringa è codificata base64

Posto di seguito una semplice funzione che permette di controllare se una stringa è codificata o meno in base64:

 php |  copy code |? 
1
function is_base64_encoded($data) {
2
  if (preg_match('%^[a-zA-Z0-9/+]*={0,2}$%', $data)) {
3
    return true;
4
  } else {
5
    return false;
6
  }
7
}

Rilasciato PHP 5.3.0

Rilasciato oggi PHP 5.3.0. Molte le novità introdotte, tra cui:

  • Supporto per i namespaces;
  • Funzioni Lambda e Closures;
  • Miglioramento delle performance;
  • Garbage collection rivisitato;
  • Sostituizione di libmysql con mysqlnd;
  • Miglioramento del porting per le piattaforme alternative;
  • Cambio politica della gestione degli avvisi (E_DEPRECATED, E_STRICT);
  • Introdotte nuove direttive nel php.ini che rendono più flessibile la configurazione;
  • Nuove estensioni native.

Vedi la guida alla migrazione da PHP 5.2.0 a PHP 5.3.0

Vedi il ChangeLog

Codice per validare le carte di credito

Riporto di seguito un script php, reperito dal sito sastgroup, utile per validare le carte di credito:

< ?
if( checkCreditCard ("40236007878787", "Visa", &$errornumber, &$errortext))
echo "OK";
else echo"MO";
function checkCreditCard ($cardnumber, $cardname, &$errornumber, &$errortext) {
$cards = array ( array ('name' => 'American Express',
'length' => '15',
'prefixes' => '34,37',
'checkdigit' => true
),
array ('name' => 'Carte Blanche',
'length' => '14',
'prefixes' => '300,301,302,303,304,305,36,38',
'checkdigit' => true
),
array ('name' => 'Diners Club',
'length' => '14',
'prefixes' => '300,301,302,303,304,305,36,38',
'checkdigit' => true
),
array ('name' => 'Discover',
'length' => '16',
'prefixes' => '6011',
'checkdigit' => true
),
array ('name' => 'Enroute',
'length' => '15',
'prefixes' => '2014,2149',
'checkdigit' => true
),
array ('name' => 'JCB',
'length' => '15,16',
'prefixes' => '3,1800,2131',
'checkdigit' => true
),
array ('name' => 'Maestro',
'length' => '16,18',
'prefixes' => '5020,6',
'checkdigit' => true
),
array ('name' => 'MasterCard',
'length' => '16',
'prefixes' => '51,52,53,54,55',
'checkdigit' => true
),
array ('name' => 'Solo',
'length' => '16,18,19',
'prefixes' => '6334,6767',
'checkdigit' => true
),
array ('name' => 'Switch',
'length' => '16,18,19',
'prefixes' => '4903,4905,4911,4936,564182,633110,6333,6759',
'checkdigit' => true
),
array ('name' => 'Visa',
'length' => '13,16',
'prefixes' => '4',
'checkdigit' => true
),
array ('name' => 'Visa Electron',
'length' => '16',
'prefixes' => '417500,4917,4913',
'checkdigit' => true
)
);
$ccErrorNo = 0;
$ccErrors [0] = "Unknown card type";
$ccErrors [1] = "No card number provided";
$ccErrors [2] = "Credit card number has invalid format";
$ccErrors [3] = "Credit card number is invalid";
$ccErrors [4] = "Credit card number is wrong length";
// Establish card type
$cardType = -1;
for ($i=0; $i
// See if it is this card (ignoring the case of the string)
if (strtolower($cardname) == strtolower($cards[$i]['name'])) {
$cardType = $i;
break;
}
}
// If card type not found, report an error
if ($cardType == -1) {
$errornumber = 0;
$errortext = $ccErrors [$errornumber];
return false;
}
// Ensure that the user has provided a credit card number
if (strlen($cardnumber) == 0) {
$errornumber = 1;
$errortext = $ccErrors [$errornumber];
return false;
}
// Remove any spaces from the credit card number
$cardNo = str_replace (' ', '', $cardnumber);
// Check that the number is numeric and of the right sort of length.
if (!eregi('^[0-9]{13,19}$',$cardNo)) {
$errornumber = 2;
$errortext = $ccErrors [$errornumber];
return false;
}
// Now check the modulus 10 check digit - if required
if ($cards[$cardType]['checkdigit']) {
$checksum = 0; // running checksum total
$mychar = ""; // next char to process
$j = 1; // takes value of 1 or 2
// Process each digit one by one starting at the right
for ($i = strlen($cardNo) - 1; $i >= 0; $i--) {
// Extract the next digit and multiply by 1 or 2 on alternative digits.
$calc = $cardNo{$i} * $j;
// If the result is in two digits add 1 to the checksum total
if ($calc > 9) {
$checksum = $checksum + 1;
$calc = $calc - 10;
}
// Add the units element to the checksum total
$checksum = $checksum + $calc;
// Switch the value of j
if ($j ==1) {$j = 2;} else {$j = 1;};
}
// All done - if checksum is divisible by 10, it is a valid modulus 10.
// If not, report an error.
if ($checksum % 10 != 0) {
$errornumber = 3;
$errortext = $ccErrors [$errornumber];
return false;
}
}
// The following are the card-specific checks we undertake.
// Load an array with the valid prefixes for this card
$prefix = split(',',$cards[$cardType]['prefixes']);
// Now see if any of them match what we have in the card number
$PrefixValid = false;
for ($i=0; $i
$exp = '^' . $prefix[$i];
if (ereg($exp,$cardNo)) {
$PrefixValid = true;
break;
}
}
// If it isn't a valid prefix there's no point at looking at the length
if (!$PrefixValid) {
$errornumber = 3;
$errortext = $ccErrors [$errornumber];
return false;
}
// See if the length is valid for this card
$LengthValid = false;
$lengths = split(',',$cards[$cardType]['length']);
for ($j=0; $j
if (strlen($cardNo) == $lengths[$j]) {
$LengthValid = true;
break;
}
}
// See if all is OK by seeing if the length was valid.
if (!$LengthValid) {
$errornumber = 4;
$errortext = $ccErrors [$errornumber];
return false;
};
// The credit card is in the required format.
return true;
}
?>

Page optimized by WP Minify WordPress Plugin