Fix: Removed warnings

This commit is contained in:
Laurent Destailleur 2011-09-21 15:53:54 +00:00
parent 714ca928f7
commit 43a0f4e29b

View File

@ -31,11 +31,11 @@
*/ */
function versiontostring($versionarray) function versiontostring($versionarray)
{ {
$string='?'; $string='?';
if (isset($versionarray[0])) $string=$versionarray[0]; if (isset($versionarray[0])) $string=$versionarray[0];
if (isset($versionarray[1])) $string.='.'.$versionarray[1]; if (isset($versionarray[1])) $string.='.'.$versionarray[1];
if (isset($versionarray[2])) $string.='.'.$versionarray[2]; if (isset($versionarray[2])) $string.='.'.$versionarray[2];
return $string; return $string;
} }
/** /**
@ -49,28 +49,28 @@ function versiontostring($versionarray)
*/ */
function versioncompare($versionarray1,$versionarray2) function versioncompare($versionarray1,$versionarray2)
{ {
$ret=0; $ret=0;
$level=0; $level=0;
$count1=count($versionarray1); $count1=count($versionarray1);
$count2=count($versionarray2); $count2=count($versionarray2);
$maxcount=max($count1,$count2); $maxcount=max($count1,$count2);
while ($level < $maxcount) while ($level < $maxcount)
{ {
$operande1=isset($versionarray1[$level])?$versionarray1[$level]:0; $operande1=isset($versionarray1[$level])?$versionarray1[$level]:0;
$operande2=isset($versionarray2[$level])?$versionarray2[$level]:0; $operande2=isset($versionarray2[$level])?$versionarray2[$level]:0;
if (preg_match('/alpha|dev/i',$operande1)) $operande1=-3; if (preg_match('/alpha|dev/i',$operande1)) $operande1=-3;
if (preg_match('/alpha|dev/i',$operande2)) $operande2=-3; if (preg_match('/alpha|dev/i',$operande2)) $operande2=-3;
if (preg_match('/beta/i',$operande1)) $operande1=-2; if (preg_match('/beta/i',$operande1)) $operande1=-2;
if (preg_match('/beta/i',$operande2)) $operande2=-2; if (preg_match('/beta/i',$operande2)) $operande2=-2;
if (preg_match('/rc/i',$operande1)) $operande1=-1; if (preg_match('/rc/i',$operande1)) $operande1=-1;
if (preg_match('/rc/i',$operande2)) $operande2=-1; if (preg_match('/rc/i',$operande2)) $operande2=-1;
$level++; $level++;
//print 'level '.$level.' '.$operande1.'-'.$operande2.'<br>'; //print 'level '.$level.' '.$operande1.'-'.$operande2.'<br>';
if ($operande1 < $operande2) { $ret = -$level; break; } if ($operande1 < $operande2) { $ret = -$level; break; }
if ($operande1 > $operande2) { $ret = $level; break; } if ($operande1 > $operande2) { $ret = $level; break; }
} }
//print join('.',$versionarray1).'('.count($versionarray1).') / '.join('.',$versionarray2).'('.count($versionarray2).') => '.$ret; //print join('.',$versionarray1).'('.count($versionarray1).') / '.join('.',$versionarray2).'('.count($versionarray2).') => '.$ret;
return $ret; return $ret;
} }
@ -81,7 +81,7 @@ function versioncompare($versionarray1,$versionarray2)
*/ */
function versionphparray() function versionphparray()
{ {
return explode('.',PHP_VERSION); return explode('.',PHP_VERSION);
} }
/** /**
@ -91,7 +91,7 @@ function versionphparray()
*/ */
function versiondolibarrarray() function versiondolibarrarray()
{ {
return explode('.',DOL_VERSION); return explode('.',DOL_VERSION);
} }
@ -112,189 +112,189 @@ function versiondolibarrarray()
*/ */
function run_sql($sqlfile,$silent=1,$entity='',$usesavepoint=1,$handler='') function run_sql($sqlfile,$silent=1,$entity='',$usesavepoint=1,$handler='')
{ {
global $db, $conf, $langs, $user; global $db, $conf, $langs, $user;
dol_syslog("Admin.lib::run_sql run sql file ".$sqlfile, LOG_DEBUG); dol_syslog("Admin.lib::run_sql run sql file ".$sqlfile, LOG_DEBUG);
$ok=0; $ok=0;
$error=0; $error=0;
$i=0; $i=0;
$buffer = ''; $buffer = '';
$arraysql = Array(); $arraysql = Array();
// Get version of database // Get version of database
$versionarray=$db->getVersionArray(); $versionarray=$db->getVersionArray();
$fp = fopen($sqlfile,"r"); $fp = fopen($sqlfile,"r");
if ($fp) if ($fp)
{ {
while (! feof($fp)) while (! feof($fp))
{ {
$buf = fgets($fp, 4096); $buf = fgets($fp, 4096);
// Cas special de lignes autorisees pour certaines versions uniquement // Cas special de lignes autorisees pour certaines versions uniquement
if (preg_match('/^--\sV([0-9\.]+)/i',$buf,$reg)) if (preg_match('/^--\sV([0-9\.]+)/i',$buf,$reg))
{ {
$versioncommande=explode('.',$reg[1]); $versioncommande=explode('.',$reg[1]);
//print var_dump($versioncommande); //print var_dump($versioncommande);
//print var_dump($versionarray); //print var_dump($versionarray);
if (count($versioncommande) && count($versionarray) if (count($versioncommande) && count($versionarray)
&& versioncompare($versioncommande,$versionarray) <= 0) && versioncompare($versioncommande,$versionarray) <= 0)
{ {
// Version qualified, delete SQL comments // Version qualified, delete SQL comments
$buf=preg_replace('/^--\sV([0-9\.]+)/i','',$buf); $buf=preg_replace('/^--\sV([0-9\.]+)/i','',$buf);
//print "Ligne $i qualifi?e par version: ".$buf.'<br>'; //print "Ligne $i qualifi?e par version: ".$buf.'<br>';
} }
} }
// Add line buf to buffer if not a comment // Add line buf to buffer if not a comment
if (! preg_match('/^--/',$buf)) if (! preg_match('/^--/',$buf))
{ {
$buf=preg_replace('/--.*$/','',$buf); //remove comment from a line that not start with -- before add it to the buffer $buf=preg_replace('/--.*$/','',$buf); //remove comment from a line that not start with -- before add it to the buffer
$buffer .= trim($buf); $buffer .= trim($buf);
} }
// print $buf.'<br>'; // print $buf.'<br>';
if (preg_match('/;/',$buffer)) // If string contains ';', it's end of a request string, we save it in arraysql. if (preg_match('/;/',$buffer)) // If string contains ';', it's end of a request string, we save it in arraysql.
{ {
// Found new request // Found new request
if ($buffer) $arraysql[$i]=$buffer; if ($buffer) $arraysql[$i]=$buffer;
$i++; $i++;
$buffer=''; $buffer='';
} }
} }
if ($buffer) $arraysql[$i]=$buffer; if ($buffer) $arraysql[$i]=$buffer;
fclose($fp); fclose($fp);
} }
else else
{ {
dol_syslog("Admin.lib::run_sql failed to open file ".$sqlfile, LOG_ERR); dol_syslog("Admin.lib::run_sql failed to open file ".$sqlfile, LOG_ERR);
} }
// Loop on each request to see if there is a __+MAX_table__ key // Loop on each request to see if there is a __+MAX_table__ key
$listofmaxrowid=array(); // This is a cache table $listofmaxrowid=array(); // This is a cache table
foreach($arraysql as $i => $sql) foreach($arraysql as $i => $sql)
{ {
$newsql=$sql; $newsql=$sql;
// Replace __+MAX_table__ with max of table // Replace __+MAX_table__ with max of table
while (preg_match('/__\+MAX_([A-Za-z_]+)__/i',$newsql,$reg)) while (preg_match('/__\+MAX_([A-Za-z_]+)__/i',$newsql,$reg))
{ {
$table=$reg[1]; $table=$reg[1];
if (! isset($listofmaxrowid[$table])) if (! isset($listofmaxrowid[$table]))
{ {
//var_dump($db); //var_dump($db);
$sqlgetrowid='SELECT MAX(rowid) as max from '.$table; $sqlgetrowid='SELECT MAX(rowid) as max from '.$table;
$resql=$db->query($sqlgetrowid); $resql=$db->query($sqlgetrowid);
if ($resql) if ($resql)
{ {
$obj=$db->fetch_object($resql); $obj=$db->fetch_object($resql);
$listofmaxrowid[$table]=$obj->max; $listofmaxrowid[$table]=$obj->max;
if (empty($listofmaxrowid[$table])) $listofmaxrowid[$table]=0; if (empty($listofmaxrowid[$table])) $listofmaxrowid[$table]=0;
} }
else else
{ {
dol_syslog('Admin.lib::run_sql Failed to get max rowid for '.$table.' '.$db->lasterror().' sql='.$sqlgetrowid, LOG_ERR); dol_syslog('Admin.lib::run_sql Failed to get max rowid for '.$table.' '.$db->lasterror().' sql='.$sqlgetrowid, LOG_ERR);
if (! $silent) print '<tr><td valign="top" colspan="2">'; if (! $silent) print '<tr><td valign="top" colspan="2">';
if (! $silent) print '<div class="error">'.$langs->trans("Failed to get max rowid for ".$table)."</div></td>"; if (! $silent) print '<div class="error">'.$langs->trans("Failed to get max rowid for ".$table)."</div></td>";
if (! $silent) print '</tr>'; if (! $silent) print '</tr>';
$error++; $error++;
break; break;
} }
} }
$from='__+MAX_'.$table.'__'; $from='__+MAX_'.$table.'__';
$to='+'.$listofmaxrowid[$table]; $to='+'.$listofmaxrowid[$table];
$newsql=str_replace($from,$to,$newsql); $newsql=str_replace($from,$to,$newsql);
dol_syslog('Admin.lib::run_sql New Request '.($i+1).' (replacing '.$from.' to '.$to.') sql='.$newsql, LOG_DEBUG); dol_syslog('Admin.lib::run_sql New Request '.($i+1).' (replacing '.$from.' to '.$to.') sql='.$newsql, LOG_DEBUG);
$arraysql[$i]=$newsql; $arraysql[$i]=$newsql;
} }
} }
// Loop on each request to execute request // Loop on each request to execute request
$cursorinsert=0; $cursorinsert=0;
$listofinsertedrowid=array(); $listofinsertedrowid=array();
foreach($arraysql as $i => $sql) foreach($arraysql as $i => $sql)
{ {
if ($sql) if ($sql)
{ {
if (!empty($handler)) $sql=preg_replace('/__HANDLER__/i',"'".$handler."'",$sql); if (!empty($handler)) $sql=preg_replace('/__HANDLER__/i',"'".$handler."'",$sql);
$newsql=preg_replace('/__ENTITY__/i',(!empty($entity)?$entity:$conf->entity),$sql); $newsql=preg_replace('/__ENTITY__/i',(!empty($entity)?$entity:$conf->entity),$sql);
// Ajout trace sur requete (eventuellement a commenter si beaucoup de requetes) // Ajout trace sur requete (eventuellement a commenter si beaucoup de requetes)
if (! $silent) print '<tr><td valign="top">'.$langs->trans("Request").' '.($i+1)." sql='".$newsql."'</td></tr>\n"; if (! $silent) print '<tr><td valign="top">'.$langs->trans("Request").' '.($i+1)." sql='".$newsql."'</td></tr>\n";
dol_syslog('Admin.lib::run_sql Request '.($i+1).' sql='.$newsql, LOG_DEBUG); dol_syslog('Admin.lib::run_sql Request '.($i+1).' sql='.$newsql, LOG_DEBUG);
// Replace for encrypt data // Replace for encrypt data
if (preg_match_all('/__ENCRYPT\(\'([^\,]+)\'\)__/i',$newsql,$reg)) if (preg_match_all('/__ENCRYPT\(\'([^\,]+)\'\)__/i',$newsql,$reg))
{ {
$num=count($reg[0]); $num=count($reg[0]);
for($i=0;$i<$num;$i++) for($i=0;$i<$num;$i++)
{ {
$from = $reg[0][$i]; $from = $reg[0][$i];
$to = $db->encrypt($reg[1][$i],1); $to = $db->encrypt($reg[1][$i],1);
$newsql = str_replace($from,$to,$newsql); $newsql = str_replace($from,$to,$newsql);
} }
} }
// Replace for decrypt data // Replace for decrypt data
if (preg_match_all('/__DECRYPT\(\'([^\,]+)\'\)__/i',$newsql,$reg)) if (preg_match_all('/__DECRYPT\(\'([^\,]+)\'\)__/i',$newsql,$reg))
{ {
$num=count($reg[0]); $num=count($reg[0]);
for($i=0;$i<$num;$i++) for($i=0;$i<$num;$i++)
{ {
$from = $reg[0][$i]; $from = $reg[0][$i];
$to = $db->decrypt($reg[1][$i]); $to = $db->decrypt($reg[1][$i]);
$newsql = str_replace($from,$to,$newsql); $newsql = str_replace($from,$to,$newsql);
} }
} }
// Replace __x__ with rowid of insert nb x // Replace __x__ with rowid of insert nb x
while (preg_match('/__([0-9]+)__/',$newsql,$reg)) while (preg_match('/__([0-9]+)__/',$newsql,$reg))
{ {
$cursor=$reg[1]; $cursor=$reg[1];
if (empty($listofinsertedrowid[$cursor])) if (empty($listofinsertedrowid[$cursor]))
{ {
if (! $silent) print '<tr><td valign="top" colspan="2">'; if (! $silent) print '<tr><td valign="top" colspan="2">';
if (! $silent) print '<div class="error">'.$langs->trans("FileIsNotCorrect")."</div></td>"; if (! $silent) print '<div class="error">'.$langs->trans("FileIsNotCorrect")."</div></td>";
if (! $silent) print '</tr>'; if (! $silent) print '</tr>';
$error++; $error++;
break; break;
} }
$from='__'.$cursor.'__'; $from='__'.$cursor.'__';
$to=$listofinsertedrowid[$cursor]; $to=$listofinsertedrowid[$cursor];
$newsql=str_replace($from,$to,$newsql); $newsql=str_replace($from,$to,$newsql);
dol_syslog('Admin.lib::run_sql New Request '.($i+1).' (replacing '.$from.' to '.$to.') sql='.$newsql, LOG_DEBUG); dol_syslog('Admin.lib::run_sql New Request '.($i+1).' (replacing '.$from.' to '.$to.') sql='.$newsql, LOG_DEBUG);
} }
$result=$db->query($newsql,$usesavepoint); $result=$db->query($newsql,$usesavepoint);
if ($result) if ($result)
{ {
if (! $silent) print '<!-- Result = OK -->'."\n"; if (! $silent) print '<!-- Result = OK -->'."\n";
if (preg_replace('/insert into ([^\s]+)/i',$newsql,$reg)) if (preg_replace('/insert into ([^\s]+)/i',$newsql,$reg))
{ {
$cursorinsert++; $cursorinsert++;
// It's an insert // It's an insert
$table=preg_replace('/([^a-zA-Z_]+)/i','',$reg[1]); $table=preg_replace('/([^a-zA-Z_]+)/i','',$reg[1]);
$insertedrowid=$db->last_insert_id($table); $insertedrowid=$db->last_insert_id($table);
$listofinsertedrowid[$cursorinsert]=$insertedrowid; $listofinsertedrowid[$cursorinsert]=$insertedrowid;
dol_syslog('Admin.lib::run_sql Insert nb '.$cursorinsert.', done in table '.$table.', rowid is '.$listofinsertedrowid[$cursorinsert], LOG_DEBUG); dol_syslog('Admin.lib::run_sql Insert nb '.$cursorinsert.', done in table '.$table.', rowid is '.$listofinsertedrowid[$cursorinsert], LOG_DEBUG);
} }
// print '<td align="right">OK</td>'; // print '<td align="right">OK</td>';
} }
else else
{ {
$errno=$db->errno(); $errno=$db->errno();
if (! $silent) print '<!-- Result = '.$errno.' -->'."\n"; if (! $silent) print '<!-- Result = '.$errno.' -->'."\n";
$okerror=array( 'DB_ERROR_TABLE_ALREADY_EXISTS', $okerror=array( 'DB_ERROR_TABLE_ALREADY_EXISTS',
'DB_ERROR_COLUMN_ALREADY_EXISTS', 'DB_ERROR_COLUMN_ALREADY_EXISTS',
'DB_ERROR_KEY_NAME_ALREADY_EXISTS', 'DB_ERROR_KEY_NAME_ALREADY_EXISTS',
'DB_ERROR_TABLE_OR_KEY_ALREADY_EXISTS', // PgSql use same code for table and key already exist 'DB_ERROR_TABLE_OR_KEY_ALREADY_EXISTS', // PgSql use same code for table and key already exist
@ -309,36 +309,36 @@ function run_sql($sqlfile,$silent=1,$entity='',$usesavepoint=1,$handler='')
); );
if (in_array($errno,$okerror)) if (in_array($errno,$okerror))
{ {
//if (! $silent) print $langs->trans("OK"); //if (! $silent) print $langs->trans("OK");
} }
else else
{ {
if (! $silent) print '<tr><td valign="top" colspan="2">'; if (! $silent) print '<tr><td valign="top" colspan="2">';
if (! $silent) print '<div class="error">'.$langs->trans("Error")." ".$db->errno().": ".$newsql."<br>".$db->error()."</div></td>"; if (! $silent) print '<div class="error">'.$langs->trans("Error")." ".$db->errno().": ".$newsql."<br>".$db->error()."</div></td>";
if (! $silent) print '</tr>'."\n"; if (! $silent) print '</tr>'."\n";
dol_syslog('Admin.lib::run_sql Request '.($i+1)." Error ".$db->errno()." ".$newsql."<br>".$db->error(), LOG_ERR); dol_syslog('Admin.lib::run_sql Request '.($i+1)." Error ".$db->errno()." ".$newsql."<br>".$db->error(), LOG_ERR);
$error++; $error++;
} }
} }
if (! $silent) print '</tr>'."\n"; if (! $silent) print '</tr>'."\n";
} }
} }
if ($error == 0) if ($error == 0)
{ {
if (! $silent) print '<tr><td>'.$langs->trans("ProcessMigrateScript").'</td>'; if (! $silent) print '<tr><td>'.$langs->trans("ProcessMigrateScript").'</td>';
if (! $silent) print '<td align="right">'.$langs->trans("OK").'</td></tr>'."\n"; if (! $silent) print '<td align="right">'.$langs->trans("OK").'</td></tr>'."\n";
$ok = 1; $ok = 1;
} }
else else
{ {
if (! $silent) print '<tr><td>'.$langs->trans("ProcessMigrateScript").'</td>'; if (! $silent) print '<tr><td>'.$langs->trans("ProcessMigrateScript").'</td>';
if (! $silent) print '<td align="right"><font class="error">'.$langs->trans("KO").'</font></td></tr>'."\n"; if (! $silent) print '<td align="right"><font class="error">'.$langs->trans("KO").'</font></td></tr>'."\n";
$ok = 0; $ok = 0;
} }
return $ok; return $ok;
} }
@ -354,26 +354,26 @@ function run_sql($sqlfile,$silent=1,$entity='',$usesavepoint=1,$handler='')
*/ */
function dolibarr_del_const($db, $name, $entity=1) function dolibarr_del_const($db, $name, $entity=1)
{ {
global $conf; global $conf;
$sql = "DELETE FROM ".MAIN_DB_PREFIX."const"; $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
$sql.= " WHERE (".$db->decrypt('name')." = '".$db->escape($name)."'"; $sql.= " WHERE (".$db->decrypt('name')." = '".$db->escape($name)."'";
if (is_numeric($name)) $sql.= " OR rowid = '".$db->escape($name)."'"; if (is_numeric($name)) $sql.= " OR rowid = '".$db->escape($name)."'";
$sql.= ")"; $sql.= ")";
if ($entity >= 0) $sql.= " AND entity = ".$entity; if ($entity >= 0) $sql.= " AND entity = ".$entity;
dol_syslog("admin.lib::dolibarr_del_const sql=".$sql); dol_syslog("admin.lib::dolibarr_del_const sql=".$sql);
$resql=$db->query($sql); $resql=$db->query($sql);
if ($resql) if ($resql)
{ {
$conf->global->$name=''; $conf->global->$name='';
return 1; return 1;
} }
else else
{ {
dol_print_error($db); dol_print_error($db);
return -1; return -1;
} }
} }
/** /**
@ -388,22 +388,22 @@ function dolibarr_del_const($db, $name, $entity=1)
*/ */
function dolibarr_get_const($db, $name, $entity=1) function dolibarr_get_const($db, $name, $entity=1)
{ {
global $conf; global $conf;
$value=''; $value='';
$sql = "SELECT ".$db->decrypt('value')." as value"; $sql = "SELECT ".$db->decrypt('value')." as value";
$sql.= " FROM ".MAIN_DB_PREFIX."const"; $sql.= " FROM ".MAIN_DB_PREFIX."const";
$sql.= " WHERE name = ".$db->encrypt($name,1); $sql.= " WHERE name = ".$db->encrypt($name,1);
$sql.= " AND entity = ".$entity; $sql.= " AND entity = ".$entity;
dol_syslog("admin.lib::dolibarr_get_const sql=".$sql); dol_syslog("admin.lib::dolibarr_get_const sql=".$sql);
$resql=$db->query($sql); $resql=$db->query($sql);
if ($resql) if ($resql)
{ {
$obj=$db->fetch_object($resql); $obj=$db->fetch_object($resql);
if ($obj) $value=$obj->value; if ($obj) $value=$obj->value;
} }
return $value; return $value;
} }
@ -423,57 +423,57 @@ function dolibarr_get_const($db, $name, $entity=1)
*/ */
function dolibarr_set_const($db, $name, $value, $type='chaine', $visible=0, $note='', $entity=1) function dolibarr_set_const($db, $name, $value, $type='chaine', $visible=0, $note='', $entity=1)
{ {
global $conf; global $conf;
// Clean parameters // Clean parameters
$name=trim($name); $name=trim($name);
// Check parameters // Check parameters
if (empty($name)) if (empty($name))
{ {
dol_print_error($db,"Error: Call to function dolibarr_set_const with wrong parameters", LOG_ERR); dol_print_error($db,"Error: Call to function dolibarr_set_const with wrong parameters", LOG_ERR);
exit; exit;
} }
//dol_syslog("dolibarr_set_const name=$name, value=$value type=$type, visible=$visible, note=$note entity=$entity"); //dol_syslog("dolibarr_set_const name=$name, value=$value type=$type, visible=$visible, note=$note entity=$entity");
$db->begin(); $db->begin();
$sql = "DELETE FROM ".MAIN_DB_PREFIX."const"; $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
$sql.= " WHERE name = ".$db->encrypt($name,1); $sql.= " WHERE name = ".$db->encrypt($name,1);
if ($entity > 0) $sql.= " AND entity = ".$entity; if ($entity > 0) $sql.= " AND entity = ".$entity;
dol_syslog("admin.lib::dolibarr_set_const sql=".$sql, LOG_DEBUG); dol_syslog("admin.lib::dolibarr_set_const sql=".$sql, LOG_DEBUG);
$resql=$db->query($sql); $resql=$db->query($sql);
if (strcmp($value,'')) // true if different. Must work for $value='0' or $value=0 if (strcmp($value,'')) // true if different. Must work for $value='0' or $value=0
{ {
$sql = "INSERT INTO llx_const(name,value,type,visible,note,entity)"; $sql = "INSERT INTO llx_const(name,value,type,visible,note,entity)";
$sql.= " VALUES ("; $sql.= " VALUES (";
$sql.= $db->encrypt($name,1); $sql.= $db->encrypt($name,1);
$sql.= ", ".$db->encrypt($value,1); $sql.= ", ".$db->encrypt($value,1);
$sql.= ",'".$type."',".$visible.",'".$db->escape($note)."',".$entity.")"; $sql.= ",'".$type."',".$visible.",'".$db->escape($note)."',".$entity.")";
//print "sql".$value."-".pg_escape_string($value)."-".$sql;exit; //print "sql".$value."-".pg_escape_string($value)."-".$sql;exit;
//print "xx".$db->escape($value); //print "xx".$db->escape($value);
//print $sql;exit; //print $sql;exit;
dol_syslog("admin.lib::dolibarr_set_const sql=".$sql, LOG_DEBUG); dol_syslog("admin.lib::dolibarr_set_const sql=".$sql, LOG_DEBUG);
$resql=$db->query($sql); $resql=$db->query($sql);
} }
if ($resql) if ($resql)
{ {
$db->commit(); $db->commit();
$conf->global->$name=$value; $conf->global->$name=$value;
return 1; return 1;
} }
else else
{ {
$error=$db->lasterror(); $error=$db->lasterror();
dol_syslog("admin.lib::dolibarr_set_const ".$error, LOG_ERR); dol_syslog("admin.lib::dolibarr_set_const ".$error, LOG_ERR);
$db->rollback(); $db->rollback();
return -1; return -1;
} }
} }
@ -484,31 +484,31 @@ function dolibarr_set_const($db, $name, $value, $type='chaine', $visible=0, $not
*/ */
function security_prepare_head() function security_prepare_head()
{ {
global $langs, $conf, $user; global $langs, $conf, $user;
$h = 0; $h = 0;
$head = array(); $head = array();
$head[$h][0] = DOL_URL_ROOT."/admin/proxy.php"; $head[$h][0] = DOL_URL_ROOT."/admin/proxy.php";
$head[$h][1] = $langs->trans("ExternalAccess"); $head[$h][1] = $langs->trans("ExternalAccess");
$head[$h][2] = 'proxy'; $head[$h][2] = 'proxy';
$h++; $h++;
$head[$h][0] = DOL_URL_ROOT."/admin/security_other.php"; $head[$h][0] = DOL_URL_ROOT."/admin/security_other.php";
$head[$h][1] = $langs->trans("Miscellanous"); $head[$h][1] = $langs->trans("Miscellanous");
$head[$h][2] = 'misc'; $head[$h][2] = 'misc';
$h++; $h++;
$head[$h][0] = DOL_URL_ROOT."/admin/security.php"; $head[$h][0] = DOL_URL_ROOT."/admin/security.php";
$head[$h][1] = $langs->trans("Passwords"); $head[$h][1] = $langs->trans("Passwords");
$head[$h][2] = 'passwords'; $head[$h][2] = 'passwords';
$h++; $h++;
$head[$h][0] = DOL_URL_ROOT."/admin/events.php"; $head[$h][0] = DOL_URL_ROOT."/admin/events.php";
$head[$h][1] = $langs->trans("Audit"); $head[$h][1] = $langs->trans("Audit");
$head[$h][2] = 'audit'; $head[$h][2] = 'audit';
$h++; $h++;
$head[$h][0] = DOL_URL_ROOT."/admin/perms.php"; $head[$h][0] = DOL_URL_ROOT."/admin/perms.php";
$head[$h][1] = $langs->trans("DefaultRights"); $head[$h][1] = $langs->trans("DefaultRights");
$head[$h][2] = 'default'; $head[$h][2] = 'default';
$h++; $h++;
@ -524,44 +524,44 @@ function security_prepare_head()
*/ */
function listOfSessions() function listOfSessions()
{ {
global $conf; global $conf;
$arrayofSessions = array(); $arrayofSessions = array();
$sessPath = ini_get("session.save_path").'/'; $sessPath = ini_get("session.save_path").'/';
dol_syslog('admin.lib:listOfSessions sessPath='.$sessPath); dol_syslog('admin.lib:listOfSessions sessPath='.$sessPath);
$dh = @opendir($sessPath); $dh = @opendir(dol_osencode($sessPath));
if ($dh) if ($dh)
{ {
while(($file = @readdir($dh)) !== false) while(($file = @readdir($dh)) !== false)
{ {
if (preg_match('/^sess_/i',$file) && $file != "." && $file != "..") if (preg_match('/^sess_/i',$file) && $file != "." && $file != "..")
{ {
$fullpath = $sessPath.$file; $fullpath = $sessPath.$file;
if(! @is_dir($fullpath) && is_readable($fullpath)) if(! @is_dir($fullpath) && is_readable($fullpath))
{ {
$sessValues = file_get_contents($fullpath); // get raw session data $sessValues = file_get_contents($fullpath); // get raw session data
if (preg_match('/dol_login/i',$sessValues) && // limit to dolibarr session if (preg_match('/dol_login/i',$sessValues) && // limit to dolibarr session
preg_match('/dol_entity\|s:([0-9]+):"('.$conf->entity.')"/i',$sessValues) && // limit to current entity preg_match('/dol_entity\|s:([0-9]+):"('.$conf->entity.')"/i',$sessValues) && // limit to current entity
preg_match('/dol_company\|s:([0-9]+):"('.$conf->global->MAIN_INFO_SOCIETE_NOM.')"/i',$sessValues)) // limit to company name preg_match('/dol_company\|s:([0-9]+):"('.$conf->global->MAIN_INFO_SOCIETE_NOM.')"/i',$sessValues)) // limit to company name
{ {
$tmp=explode('_', $file); $tmp=explode('_', $file);
$idsess=$tmp[1]; $idsess=$tmp[1];
$login = preg_match('/dol_login\|s:[0-9]+:"([A-Za-z0-9]+)"/i',$sessValues,$regs); $login = preg_match('/dol_login\|s:[0-9]+:"([A-Za-z0-9]+)"/i',$sessValues,$regs);
$arrayofSessions[$idsess]["login"] = $regs[1]; $arrayofSessions[$idsess]["login"] = $regs[1];
$arrayofSessions[$idsess]["age"] = time()-filectime($fullpath); $arrayofSessions[$idsess]["age"] = time()-filectime($fullpath);
$arrayofSessions[$idsess]["creation"] = filectime($fullpath); $arrayofSessions[$idsess]["creation"] = filectime($fullpath);
$arrayofSessions[$idsess]["modification"] = filemtime($fullpath); $arrayofSessions[$idsess]["modification"] = filemtime($fullpath);
$arrayofSessions[$idsess]["raw"] = $sessValues; $arrayofSessions[$idsess]["raw"] = $sessValues;
} }
} }
} }
} }
@closedir($dh); @closedir($dh);
} }
return $arrayofSessions; return $arrayofSessions;
} }
/** /**
@ -572,43 +572,43 @@ function listOfSessions()
*/ */
function purgeSessions($mysessionid) function purgeSessions($mysessionid)
{ {
global $conf; global $conf;
$arrayofSessions = array(); $arrayofSessions = array();
$sessPath = ini_get("session.save_path")."/"; $sessPath = ini_get("session.save_path")."/";
dol_syslog('admin.lib:purgeSessions mysessionid='.$mysessionid.' sessPath='.$sessPath); dol_syslog('admin.lib:purgeSessions mysessionid='.$mysessionid.' sessPath='.$sessPath);
$error=0; $error=0;
$dh = @opendir($sessPath); $dh = @opendir(dol_osencode($sessPath));
while(($file = @readdir($dh)) !== false) while(($file = @readdir($dh)) !== false)
{ {
if ($file != "." && $file != "..") if ($file != "." && $file != "..")
{ {
$fullpath = $sessPath.$file; $fullpath = $sessPath.$file;
if(! @is_dir($fullpath)) if(! @is_dir($fullpath))
{ {
$sessValues = file_get_contents($fullpath); // get raw session data $sessValues = file_get_contents($fullpath); // get raw session data
if (preg_match('/dol_login/i',$sessValues) && // limit to dolibarr session if (preg_match('/dol_login/i',$sessValues) && // limit to dolibarr session
preg_match('/dol_entity\|s:([0-9]+):"('.$conf->entity.')"/i',$sessValues) && // limit to current entity preg_match('/dol_entity\|s:([0-9]+):"('.$conf->entity.')"/i',$sessValues) && // limit to current entity
preg_match('/dol_company\|s:([0-9]+):"('.$conf->global->MAIN_INFO_SOCIETE_NOM.')"/i',$sessValues)) // limit to company name preg_match('/dol_company\|s:([0-9]+):"('.$conf->global->MAIN_INFO_SOCIETE_NOM.')"/i',$sessValues)) // limit to company name
{ {
$tmp=explode('_', $file); $tmp=explode('_', $file);
$idsess=$tmp[1]; $idsess=$tmp[1];
// We remove session if it's not ourself // We remove session if it's not ourself
if ($idsess != $mysessionid) if ($idsess != $mysessionid)
{ {
$res=@unlink($fullpath); $res=@unlink($fullpath);
if (! $res) $error++; if (! $res) $error++;
} }
} }
} }
} }
} }
@closedir($dh); @closedir($dh);
if (! $error) return 1; if (! $error) return 1;
else return -$error; else return -$error;
} }
@ -634,37 +634,37 @@ function Activate($value,$withdeps=1)
// Loop on each directory // Loop on each directory
$found=false; $found=false;
foreach ($conf->file->dol_document_root as $type => $dirroot) foreach ($conf->file->dol_document_root as $type => $dirroot)
{ {
$modulesdir[] = $dirroot."/includes/modules/"; $modulesdir[] = $dirroot."/includes/modules/";
if ($type == 'alt') if ($type == 'alt')
{ {
$handle=@opendir($dirroot); $handle=@opendir(dol_osencode($dirroot));
if (is_resource($handle)) if (is_resource($handle))
{ {
while (($file = readdir($handle))!==false) while (($file = readdir($handle))!==false)
{ {
if (is_dir($dirroot.'/'.$file) && substr($file, 0, 1) <> '.' && substr($file, 0, 3) <> 'CVS' && $file != 'includes') if (is_dir($dirroot.'/'.$file) && substr($file, 0, 1) <> '.' && substr($file, 0, 3) <> 'CVS' && $file != 'includes')
{ {
if (is_dir($dirroot . '/' . $file . '/includes/modules/')) if (is_dir($dirroot . '/' . $file . '/includes/modules/'))
{ {
$modulesdir[] = $dirroot . '/' . $file . '/includes/modules/'; $modulesdir[] = $dirroot . '/' . $file . '/includes/modules/';
} }
} }
} }
closedir($handle); closedir($handle);
} }
} }
} }
foreach ($modulesdir as $dir) foreach ($modulesdir as $dir)
{ {
if (file_exists($dir.$modFile)) if (file_exists($dir.$modFile))
{ {
$found=@include_once($dir.$modFile); $found=@include_once($dir.$modFile);
if ($found) break; if ($found) break;
} }
} }
$objMod = new $modName($db); $objMod = new $modName($db);
@ -748,38 +748,38 @@ function UnActivate($value, $requiredby=1)
$modulesdir=array(); $modulesdir=array();
// Loop on each directory // Loop on each directory
$found=false; $found=false;
foreach ($conf->file->dol_document_root as $type => $dirroot) foreach ($conf->file->dol_document_root as $type => $dirroot)
{ {
$modulesdir[] = $dirroot."/includes/modules/"; $modulesdir[] = $dirroot."/includes/modules/";
if ($type == 'alt') if ($type == 'alt')
{ {
$handle=@opendir($dirroot); $handle=@opendir(dol_osencode($dirroot));
if (is_resource($handle)) if (is_resource($handle))
{ {
while (($file = readdir($handle))!==false) while (($file = readdir($handle))!==false)
{ {
if (is_dir($dirroot.'/'.$file) && substr($file, 0, 1) <> '.' && substr($file, 0, 3) <> 'CVS' && $file != 'includes') if (is_dir($dirroot.'/'.$file) && substr($file, 0, 1) <> '.' && substr($file, 0, 3) <> 'CVS' && $file != 'includes')
{ {
if (is_dir($dirroot . '/' . $file . '/includes/modules/')) if (is_dir($dirroot . '/' . $file . '/includes/modules/'))
{ {
$modulesdir[] = $dirroot . '/' . $file . '/includes/modules/'; $modulesdir[] = $dirroot . '/' . $file . '/includes/modules/';
} }
} }
} }
closedir($handle); closedir($handle);
} }
} }
} }
foreach ($modulesdir as $dir) foreach ($modulesdir as $dir)
{ {
if (file_exists($dir.$modFile)) if (file_exists($dir.$modFile))
{ {
$found=@include_once($dir.$modFile); $found=@include_once($dir.$modFile);
if ($found) break; if ($found) break;
} }
} }
if ($found) if ($found)
@ -846,7 +846,7 @@ function complete_dictionnary_with_modules(&$taborder,&$tabname,&$tablib,&$tabsq
// Load modules attributes in arrays (name, numero, orders) from dir directory // Load modules attributes in arrays (name, numero, orders) from dir directory
//print $dir."\n<br>"; //print $dir."\n<br>";
dol_syslog("Scan directory ".$dir." for modules"); dol_syslog("Scan directory ".$dir." for modules");
$handle=@opendir($dir); $handle=@opendir(dol_osencode($dir));
if (is_resource($handle)) if (is_resource($handle))
{ {
while (($file = readdir($handle))!==false) while (($file = readdir($handle))!==false)