| Conditions | 84 |
| Paths | > 20000 |
| Total Lines | 274 |
| Code Lines | 162 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 178 | function dumpDatabase($compression='none', $type='auto', $usedefault=1, $file='auto', $keeplastnfiles=0) |
||
| 179 | { |
||
| 180 | global $db, $conf, $langs, $dolibarr_main_data_root; |
||
| 181 | global $dolibarr_main_db_name, $dolibarr_main_db_host, $dolibarr_main_db_user, $dolibarr_main_db_port, $dolibarr_main_db_pass; |
||
| 182 | |||
| 183 | |||
| 184 | $langs->load("admin"); |
||
| 185 | |||
| 186 | dol_syslog("Utils::dumpDatabase type=".$type." compression=".$compression." file=".$file, LOG_DEBUG); |
||
| 187 | require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; |
||
| 188 | |||
| 189 | // Check compression parameter |
||
| 190 | if (! in_array($compression, array('none', 'gz', 'bz', 'zip'))) |
||
| 191 | { |
||
| 192 | $langs->load("errors"); |
||
| 193 | $this->error=$langs->transnoentitiesnoconv("ErrorBadValueForParameter", $compression, "Compression"); |
||
| 194 | return -1; |
||
| 195 | } |
||
| 196 | |||
| 197 | // Check type parameter |
||
| 198 | if ($type == 'auto') $type = $db->type; |
||
| 199 | if (! in_array($type, array('pgsql', 'mysql', 'mysqli','mysqlnobin'))) |
||
| 200 | { |
||
| 201 | $langs->load("errors"); |
||
| 202 | $this->error=$langs->transnoentitiesnoconv("ErrorBadValueForParameter", $type, "Basetype"); |
||
| 203 | return -1; |
||
| 204 | } |
||
| 205 | |||
| 206 | // Check file parameter |
||
| 207 | if ($file == 'auto') |
||
| 208 | { |
||
| 209 | $prefix='dump'; |
||
| 210 | $ext='.sql'; |
||
| 211 | if (in_array($type, array('mysql', 'mysqli'))) { $prefix='mysqldump'; $ext='sql'; } |
||
| 212 | //if ($label == 'PostgreSQL') { $prefix='pg_dump'; $ext='dump'; } |
||
| 213 | if (in_array($type, array('pgsql'))) { $prefix='pg_dump'; $ext='sql'; } |
||
| 214 | $file=$prefix.'_'.$dolibarr_main_db_name.'_'.dol_sanitizeFileName(DOL_VERSION).'_'.strftime("%Y%m%d%H%M").'.'.$ext; |
||
| 215 | } |
||
| 216 | |||
| 217 | $outputdir = $conf->admin->dir_output.'/backup'; |
||
| 218 | $result=dol_mkdir($outputdir); |
||
| 219 | |||
| 220 | |||
| 221 | // MYSQL |
||
| 222 | if ($type == 'mysql' || $type == 'mysqli') |
||
| 223 | { |
||
| 224 | $cmddump=$conf->global->SYSTEMTOOLS_MYSQLDUMP; |
||
| 225 | |||
| 226 | |||
| 227 | $outputfile = $outputdir.'/'.$file; |
||
| 228 | // for compression format, we add extension |
||
| 229 | $compression=$compression ? $compression : 'none'; |
||
| 230 | if ($compression == 'gz') $outputfile.='.gz'; |
||
| 231 | if ($compression == 'bz') $outputfile.='.bz2'; |
||
| 232 | $outputerror = $outputfile.'.err'; |
||
| 233 | dol_mkdir($conf->admin->dir_output.'/backup'); |
||
| 234 | |||
| 235 | // Parameteres execution |
||
| 236 | $command=$cmddump; |
||
| 237 | if (preg_match("/\s/",$command)) $command=escapeshellarg($command); // Use quotes on command |
||
| 238 | |||
| 239 | //$param=escapeshellarg($dolibarr_main_db_name)." -h ".escapeshellarg($dolibarr_main_db_host)." -u ".escapeshellarg($dolibarr_main_db_user)." -p".escapeshellarg($dolibarr_main_db_pass); |
||
| 240 | $param=$dolibarr_main_db_name." -h ".$dolibarr_main_db_host; |
||
| 241 | $param.=" -u ".$dolibarr_main_db_user; |
||
| 242 | if (! empty($dolibarr_main_db_port)) $param.=" -P ".$dolibarr_main_db_port; |
||
| 243 | if (! GETPOST("use_transaction")) $param.=" -l --single-transaction"; |
||
| 244 | if (GETPOST("disable_fk") || $usedefault) $param.=" -K"; |
||
| 245 | if (GETPOST("sql_compat") && GETPOST("sql_compat") != 'NONE') $param.=" --compatible=".escapeshellarg(GETPOST("sql_compat","alpha")); |
||
| 246 | if (GETPOST("drop_database")) $param.=" --add-drop-database"; |
||
| 247 | if (GETPOST("sql_structure") || $usedefault) |
||
| 248 | { |
||
| 249 | if (GETPOST("drop") || $usedefault) $param.=" --add-drop-table=TRUE"; |
||
| 250 | else $param.=" --add-drop-table=FALSE"; |
||
| 251 | } |
||
| 252 | else |
||
| 253 | { |
||
| 254 | $param.=" -t"; |
||
| 255 | } |
||
| 256 | if (GETPOST("disable-add-locks")) $param.=" --add-locks=FALSE"; |
||
| 257 | if (GETPOST("sql_data") || $usedefault) |
||
| 258 | { |
||
| 259 | $param.=" --tables"; |
||
| 260 | if (GETPOST("showcolumns") || $usedefault) $param.=" -c"; |
||
| 261 | if (GETPOST("extended_ins") || $usedefault) $param.=" -e"; |
||
| 262 | else $param.=" --skip-extended-insert"; |
||
| 263 | if (GETPOST("delayed")) $param.=" --delayed-insert"; |
||
| 264 | if (GETPOST("sql_ignore")) $param.=" --insert-ignore"; |
||
| 265 | if (GETPOST("hexforbinary") || $usedefault) $param.=" --hex-blob"; |
||
| 266 | } |
||
| 267 | else |
||
| 268 | { |
||
| 269 | $param.=" -d"; // No row information (no data) |
||
| 270 | } |
||
| 271 | $param.=" --default-character-set=utf8"; // We always save output into utf8 charset |
||
| 272 | $paramcrypted=$param; |
||
| 273 | $paramclear=$param; |
||
| 274 | if (! empty($dolibarr_main_db_pass)) |
||
| 275 | { |
||
| 276 | $paramcrypted.=' -p"'.preg_replace('/./i','*',$dolibarr_main_db_pass).'"'; |
||
| 277 | $paramclear.=' -p"'.str_replace(array('"','`'),array('\"','\`'),$dolibarr_main_db_pass).'"'; |
||
| 278 | } |
||
| 279 | |||
| 280 | $errormsg=''; |
||
| 281 | |||
| 282 | // Debut appel methode execution |
||
| 283 | $fullcommandcrypted=$command." ".$paramcrypted." 2>&1"; |
||
| 284 | $fullcommandclear=$command." ".$paramclear." 2>&1"; |
||
| 285 | if ($compression == 'none') $handle = fopen($outputfile, 'w'); |
||
| 286 | if ($compression == 'gz') $handle = gzopen($outputfile, 'w'); |
||
| 287 | if ($compression == 'bz') $handle = bzopen($outputfile, 'w'); |
||
| 288 | |||
| 289 | if ($handle) |
||
| 290 | { |
||
| 291 | $ok=0; |
||
| 292 | dol_syslog("Run command ".$fullcommandcrypted); |
||
| 293 | $handlein = popen($fullcommandclear, 'r'); |
||
| 294 | $i=0; |
||
| 295 | while (!feof($handlein)) |
||
| 296 | { |
||
| 297 | $i++; // output line number |
||
| 298 | $read = fgets($handlein); |
||
| 299 | // Exclude warning line we don't want |
||
| 300 | if ($i == 1 && preg_match('/Warning.*Using a password/i', $read)) continue; |
||
| 301 | fwrite($handle,$read); |
||
| 302 | if (preg_match('/'.preg_quote('-- Dump completed').'/i',$read)) $ok=1; |
||
| 303 | elseif (preg_match('/'.preg_quote('SET SQL_NOTES=@OLD_SQL_NOTES').'/i',$read)) $ok=1; |
||
| 304 | } |
||
| 305 | pclose($handlein); |
||
| 306 | |||
| 307 | if ($compression == 'none') fclose($handle); |
||
| 308 | if ($compression == 'gz') gzclose($handle); |
||
| 309 | if ($compression == 'bz') bzclose($handle); |
||
| 310 | |||
| 311 | if (! empty($conf->global->MAIN_UMASK)) |
||
| 312 | @chmod($outputfile, octdec($conf->global->MAIN_UMASK)); |
||
| 313 | } |
||
| 314 | else |
||
| 315 | { |
||
| 316 | $langs->load("errors"); |
||
| 317 | dol_syslog("Failed to open file ".$outputfile,LOG_ERR); |
||
| 318 | $errormsg=$langs->trans("ErrorFailedToWriteInDir"); |
||
| 319 | } |
||
| 320 | |||
| 321 | // Get errorstring |
||
| 322 | if ($compression == 'none') $handle = fopen($outputfile, 'r'); |
||
| 323 | if ($compression == 'gz') $handle = gzopen($outputfile, 'r'); |
||
| 324 | if ($compression == 'bz') $handle = bzopen($outputfile, 'r'); |
||
| 325 | if ($handle) |
||
| 326 | { |
||
| 327 | // Get 2048 first chars of error message. |
||
| 328 | $errormsg = fgets($handle,2048); |
||
| 329 | // Close file |
||
| 330 | if ($compression == 'none') fclose($handle); |
||
| 331 | if ($compression == 'gz') gzclose($handle); |
||
| 332 | if ($compression == 'bz') bzclose($handle); |
||
| 333 | if ($ok && preg_match('/^-- MySql/i',$errormsg)) $errormsg=''; // Pas erreur |
||
| 334 | else |
||
| 335 | { |
||
| 336 | // Renommer fichier sortie en fichier erreur |
||
| 337 | //print "$outputfile -> $outputerror"; |
||
| 338 | @dol_delete_file($outputerror,1); |
||
| 339 | @rename($outputfile,$outputerror); |
||
| 340 | // Si safe_mode on et command hors du parametre exec, on a un fichier out vide donc errormsg vide |
||
| 341 | if (! $errormsg) |
||
| 342 | { |
||
| 343 | $langs->load("errors"); |
||
| 344 | $errormsg=$langs->trans("ErrorFailedToRunExternalCommand"); |
||
| 345 | } |
||
| 346 | } |
||
| 347 | } |
||
| 348 | // Fin execution commande |
||
| 349 | |||
| 350 | $this->output = $errormsg; |
||
| 351 | $this->error = $errormsg; |
||
| 352 | $this->result = array("commandbackuplastdone" => $command." ".$paramcrypted, "commandbackuptorun" => ""); |
||
| 353 | //if (empty($this->output)) $this->output=$this->result['commandbackuplastdone']; |
||
| 354 | } |
||
| 355 | |||
| 356 | // MYSQL NO BIN |
||
| 357 | if ($type == 'mysqlnobin') |
||
| 358 | { |
||
| 359 | $outputfile = $outputdir.'/'.$file; |
||
| 360 | $outputfiletemp = $outputfile.'-TMP.sql'; |
||
| 361 | // for compression format, we add extension |
||
| 362 | $compression=$compression ? $compression : 'none'; |
||
| 363 | if ($compression == 'gz') $outputfile.='.gz'; |
||
| 364 | if ($compression == 'bz') $outputfile.='.bz2'; |
||
| 365 | $outputerror = $outputfile.'.err'; |
||
| 366 | dol_mkdir($conf->admin->dir_output.'/backup'); |
||
| 367 | |||
| 368 | if ($compression == 'gz' or $compression == 'bz') |
||
| 369 | { |
||
| 370 | backup_tables($outputfiletemp); |
||
| 371 | dol_compress_file($outputfiletemp, $outputfile, $compression); |
||
| 372 | unlink($outputfiletemp); |
||
| 373 | } |
||
| 374 | else |
||
| 375 | { |
||
| 376 | backup_tables($outputfile); |
||
| 377 | } |
||
| 378 | |||
| 379 | $this->output = ""; |
||
| 380 | $this->result = array("commandbackuplastdone" => "", "commandbackuptorun" => ""); |
||
| 381 | } |
||
| 382 | |||
| 383 | // POSTGRESQL |
||
| 384 | if ($type == 'postgresql') |
||
| 385 | { |
||
| 386 | $cmddump=$conf->global->SYSTEMTOOLS_POSTGRESQLDUMP; |
||
| 387 | |||
| 388 | $outputfile = $outputdir.'/'.$file; |
||
| 389 | // for compression format, we add extension |
||
| 390 | $compression=$compression ? $compression : 'none'; |
||
| 391 | if ($compression == 'gz') $outputfile.='.gz'; |
||
| 392 | if ($compression == 'bz') $outputfile.='.bz2'; |
||
| 393 | $outputerror = $outputfile.'.err'; |
||
| 394 | dol_mkdir($conf->admin->dir_output.'/backup'); |
||
| 395 | |||
| 396 | // Parameteres execution |
||
| 397 | $command=$cmddump; |
||
| 398 | if (preg_match("/\s/",$command)) $command=escapeshellarg($command); // Use quotes on command |
||
| 399 | |||
| 400 | //$param=escapeshellarg($dolibarr_main_db_name)." -h ".escapeshellarg($dolibarr_main_db_host)." -u ".escapeshellarg($dolibarr_main_db_user)." -p".escapeshellarg($dolibarr_main_db_pass); |
||
| 401 | //$param="-F c"; |
||
| 402 | $param="-F p"; |
||
| 403 | $param.=" --no-tablespaces --inserts -h ".$dolibarr_main_db_host; |
||
| 404 | $param.=" -U ".$dolibarr_main_db_user; |
||
| 405 | if (! empty($dolibarr_main_db_port)) $param.=" -p ".$dolibarr_main_db_port; |
||
| 406 | if (GETPOST("sql_compat") && GETPOST("sql_compat") == 'ANSI') $param.=" --disable-dollar-quoting"; |
||
| 407 | if (GETPOST("drop_database")) $param.=" -c -C"; |
||
| 408 | if (GETPOST("sql_structure")) |
||
| 409 | { |
||
| 410 | if (GETPOST("drop")) $param.=" --add-drop-table"; |
||
| 411 | if (! GETPOST("sql_data")) $param.=" -s"; |
||
| 412 | } |
||
| 413 | if (GETPOST("sql_data")) |
||
| 414 | { |
||
| 415 | if (! GETPOST("sql_structure")) $param.=" -a"; |
||
| 416 | if (GETPOST("showcolumns")) $param.=" -c"; |
||
| 417 | } |
||
| 418 | $param.=' -f "'.$outputfile.'"'; |
||
| 419 | //if ($compression == 'none') |
||
| 420 | if ($compression == 'gz') $param.=' -Z 9'; |
||
| 421 | //if ($compression == 'bz') |
||
| 422 | $paramcrypted=$param; |
||
| 423 | $paramclear=$param; |
||
| 424 | /*if (! empty($dolibarr_main_db_pass)) |
||
| 425 | { |
||
| 426 | $paramcrypted.=" -W".preg_replace('/./i','*',$dolibarr_main_db_pass); |
||
| 427 | $paramclear.=" -W".$dolibarr_main_db_pass; |
||
| 428 | }*/ |
||
| 429 | $paramcrypted.=" -w ".$dolibarr_main_db_name; |
||
| 430 | $paramclear.=" -w ".$dolibarr_main_db_name; |
||
| 431 | |||
| 432 | $this->output = ""; |
||
| 433 | $this->result = array("commandbackuplastdone" => "", "commandbackuptorun" => $command." ".$paramcrypted); |
||
| 434 | } |
||
| 435 | |||
| 436 | // Clean old files |
||
| 437 | if ($keeplastnfiles > 0) |
||
| 438 | { |
||
| 439 | $tmpfiles = dol_dir_list($conf->admin->dir_output.'/backup', 'files', 0, '', '(\.err|\.old|\.sav)$', 'date', SORT_DESC); |
||
| 440 | $i=0; |
||
| 441 | foreach($tmpfiles as $key => $val) |
||
| 442 | { |
||
| 443 | $i++; |
||
| 444 | if ($i <= $keeplastnfiles) continue; |
||
| 445 | dol_delete_file($val['fullname']); |
||
| 446 | } |
||
| 447 | } |
||
| 448 | |||
| 449 | |||
| 450 | return 0; |
||
| 451 | } |
||
| 452 | |||
| 527 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: