} // If $suburi is /, it is now '' define('DOL_URL_ROOT_ALT', $suburi); // URL relative root ('', '/dolibarr/custom', ...) } // Define prefix define('MAIN_DB_PREFIX', $dolibarr_main_db_prefix); //print DOL_URL_ROOT.'-'.DOL_URL_ROOT_ALT; /* * Include functions */ if (!file_exists(DOL_DOCUMENT_ROOT . "/lib/functions.lib.php")) { print "Error: Dolibarr config file content seems to be not correctly defined.<br>\n"; print "Please run dolibarr setup by calling page <b>/install</b>.<br>\n"; exit; } include_once DOL_DOCUMENT_ROOT . "/lib/functions.lib.php"; // Need 970ko memory (1.1 in 2.2) // If password is encoded, we decode it if (preg_match('/crypted:/i', $dolibarr_main_db_pass) || !empty($dolibarr_main_db_encrypted_pass)) { require_once DOL_DOCUMENT_ROOT . "/lib/security.lib.php"; if (preg_match('/crypted:/i', $dolibarr_main_db_pass)) { $dolibarr_main_db_pass = preg_replace('/crypted:/i', '', $dolibarr_main_db_pass); $dolibarr_main_db_pass = dol_decode($dolibarr_main_db_pass); $dolibarr_main_db_encrypted_pass = $dolibarr_main_db_pass; // We need to set this as it is used to know the password was initially crypted } else { $dolibarr_main_db_pass = dol_decode($dolibarr_main_db_encrypted_pass); } } //print memory_get_usage();
/** * Encode or decode database password in config file * * @param int $level Encode level: 0 no encoding, 1 encoding * @return int <0 if KO, >0 if OK */ function encodedecode_dbpassconf($level = 0) { dol_syslog("encodedecode_dbpassconf level=" . $level, LOG_DEBUG); $config = ''; $passwd = ''; $passwd_crypted = ''; if ($fp = fopen(DOL_DOCUMENT_ROOT . '/conf/conf.php', 'r')) { while (!feof($fp)) { $buffer = fgets($fp, 4096); $lineofpass = 0; if (preg_match('/^[^#]*dolibarr_main_db_encrypted_pass[\\s]*=[\\s]*(.*)/i', $buffer, $reg)) { $val = trim($reg[1]); // This also remove CR/LF $val = preg_replace('/^["\']/', '', $val); $val = preg_replace('/["\'][\\s;]*$/', '', $val); if (!empty($val)) { $passwd_crypted = $val; $val = dol_decode($val); $passwd = $val; $lineofpass = 1; } } elseif (preg_match('/^[^#]*dolibarr_main_db_pass[\\s]*=[\\s]*(.*)/i', $buffer, $reg)) { $val = trim($reg[1]); // This also remove CR/LF $val = preg_replace('/^["\']/', '', $val); $val = preg_replace('/["\'][\\s;]*$/', '', $val); if (preg_match('/crypted:/i', $buffer)) { $val = preg_replace('/crypted:/i', '', $val); $passwd_crypted = $val; $val = dol_decode($val); $passwd = $val; } else { $passwd = $val; $val = dol_encode($val); $passwd_crypted = $val; } $lineofpass = 1; } // Output line if ($lineofpass) { // Add value at end of file if ($level == 0) { $config .= '$dolibarr_main_db_pass=\'' . $passwd . '\';' . "\n"; } if ($level == 1) { $config .= '$dolibarr_main_db_pass=\'crypted:' . $passwd_crypted . '\';' . "\n"; } //print 'passwd = '.$passwd.' - passwd_crypted = '.$passwd_crypted; //exit; } else { $config .= $buffer; } } fclose($fp); // Write new conf file $file = DOL_DOCUMENT_ROOT . '/conf/conf.php'; if ($fp = @fopen($file, 'w')) { fputs($fp, $config); fclose($fp); // It's config file, so we set read permission for creator only. // Should set permission to web user and groups for users used by batch //@chmod($file, octdec('0600')); return 1; } else { dol_syslog("encodedecode_dbpassconf Failed to open conf.php file for writing", LOG_WARNING); return -1; } } else { dol_syslog("encodedecode_dbpassconf Failed to read conf.php", LOG_ERR); return -2; } }
/** * testEncodeDecode * * @return number */ public function testEncodeDecode() { $stringtotest = "This is a string to test encode/decode"; $encodedstring = dol_encode($stringtotest); $decodedstring = dol_decode($encodedstring); print __METHOD__ . " encodedstring=" . $encodedstring . " " . base64_encode($stringtotest) . "\n"; $this->assertEquals($stringtotest, $decodedstring); return 0; }