Location: PHPKode > projects > ActualAnalyzer > check.php
<?php
/*------------------------------------------------------------------------*/
// Product: ActualAnalyzer
// Script: check.php
// Source: http://www.actualscripts.com/
// Copyright: (c) 2002-2006 ActualScripts, Company. All rights reserved.
//
// YOU DON'T NEED TO EDIT ANYTHING IN THIS SCRIPT.
// SEE LICENSE AGREEMENT FOR MORE DETAILS
/*------------------------------------------------------------------------*/

//p3p header
require './data/p3pheader.php';

//root folder of Actualanalyzer
$rf='./';

require './common/error.php';
require './common/global.php';
require './common/config.php';
require './common/confdb.php';
require './common/auth.php';

//errors
$err = new error($rf);

//config
$conf = new config($rf);
if($err->flag) {
  $err->reason('check.php||constructor of config class is fail');
  $err->log_out();
  $err->scr_out();
  exit;
}

//database functions
require './common/db/'.$conf->db.'.php';

//database initialisation
db_init();
if($err->flag) {
  $err->reason('check.php||can\'t init database');
  $err->log_out();
  $err->scr_out();
  exit;
}

//config in database
$configdb = new confdb;
if($err->flag) {
  $err->reason('check.php||constructor of confdb class has failed');
  $err->log_out();
  $err->scr_out();
  exit;
}

//authentication
$login = new auth($rf,'check',_CHECKDB);
if($err->flag) {
  $err->reason('check.php||constructor of auth class has failed');
  $err->log_out();
  $err->scr_out();
  exit;
}

if(isset($_POST['vcheck_h'])) {
    if(!empty($_POST['vcheck_h'])) {
        download($_POST['vcheck_h']);
        if($err->flag) {
          $err->reason('check.php||the \'download\' function has failed');
          $err->log_out();
          $err->scr_out();
          exit;
        }
    }
}
elseif(isset($HTTP_POST_VARS['vcheck_h'])) {
    if(!empty($HTTP_POST_VARS['vcheck_h'])) {
        download($HTTP_POST_VARS['vcheck_h']);
        if($err->flag) {
          $err->reason('check.php||the \'download\' function has failed');
          $err->log_out();
          $err->scr_out();
          exit;
        }
    }
}
else {
    check();
    if($err->flag) {
      $err->reason('check.php||the \'check\' function has failed');
      $err->log_out();
      $err->scr_out();
      exit;
    }
}

//database closing
db_exit();
if($err->flag) {
  $err->reason('admin.php||can\'t close connection with database');
  $err->log_out();
  $err->scr_out();
  exit;
}

exit;

//===================================================================
function createdata($table) {
  global $err,$conf;

  $cm=array();
  echo 'DROP TABLE '.$table.";\n";
  echo 'CREATE TABLE '.$table."\n(\n";
  $request='SHOW COLUMNS FROM '.$table;
  $result=db_query($request,$conf->link);
  if(!$result) {$err->reason('check.php|createdata|the request \''.$request.'\' has failed -- '.db_error($conf->link));return;}
  $ii=0;
  $n=db_num_rows($result);
  while($row=db_fetch_row($result)) {
      $str=trim($row[0]).' '.trim($row[1]);
      if(empty($row[2])) $str.=' NOT NULL';
      $a=split('\(',trim($row[1]));
      if(!strcmp($a[0],'char')||!strcmp($a[0],'varchar')) { $cm[$ii]=1; }
      if($ii<$n-1) echo ' '.$str.",\n";
      else echo ' '.$str."\n";
      $ii++;
  }
  echo ");\r\n\r\n";
  db_free_result($result);

  $request='SELECT * FROM '.$table;
  $result=db_query($request,$conf->link);
  if(!$result) {$err->reason('check.php|createdata|the request \''.$request.'\' has failed -- '.db_error($conf->link));return;}
  $numf=db_num_fields($result);
  if(db_num_rows($result))
      while($row=db_fetch_row($result)) {
          echo 'INSERT INTO '.$table.' VALUES (';
          $str='';
          for($i=0;$i<$numf;$i++) {
              if(!isset($cm[$i])) {
                  if(empty($str)) $str=$row[$i];
                  else $str.=','.$row[$i];
              }
              else {
                  if(empty($str)) $str="'".str_replace("'","\'",$row[$i])."'";
                  else $str.=",'".str_replace("'","\'",$row[$i])."'";
              }
          }
          echo $str.");\r\n";
      }
  db_free_result($result);
}
//===================================================================
function download($table) {
  global $err,$conf;

  Header('Content-Type: sql');
  Header('Content-Disposition: attachment; filename="'.$table.'.sql"');
  Header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  header('Pragma: public');

  if(!strcmp($conf->dbase,$table)) {
      $request='SHOW TABLES LIKE "aa%"';
      $resultt=db_query($request,$conf->link);
      if(!$resultt) {$err->reason('check.php|download|the request \''.$request.'\' has failed -- '.db_error($conf->link));return;}
      while($rowt=db_fetch_row($resultt)) {
          createdata($rowt[0]);
          if($err->flag) {$err->reason('check.php|download|\'createdata\' function has failed');return;}
      }
      db_free_result($resultt);
  }
  else {
      createdata($table);
      if($err->flag) {$err->reason('check.php|download|\'createdata\' function has failed');return;}
  }
}
//===================================================================
function check() {
  global $err,$conf;

  $request='SELECT VERSION()';
  $result=db_query($request,$conf->link);
  if(!$result) {$err->reason('check.php|check|the request \''.$request.'\' has failed -- '.db_error($conf->link));return;}
  $row=db_fetch_row($result);
  db_free_result($result);
  $parse=split('\.',$row[0]);
  $checkver=$parse[0].'.'.$parse[1];
  $str=<<<TOP
<html><head><title>Check database '$conf->dbase'</title><SCRIPT LANGUAGE="JavaScript">
<!--
function FormVal(form,val) {form.vcheck_h.value=val}
//-->
</SCRIPT></head><body>
<div align=center><a name="top"></a><form name=check method="post" action="check.php">
<input type=hidden name=uname value="$conf->uname"><input type=hidden name=passw value="$conf->passw">
<input type=hidden name=vcheck_h><table>
TOP;
  $str.='<tr align="center"><td colspan="11" bgcolor="#C0C0C0">Database \'<b>'.$conf->dbase.'</b>\'. MySQL version = '.$row[0].'</td></tr>';
  $str.='<tr bgcolor="#C0C0C0" align="center">';
  $str.='<td>Name</td>';
  $str.='<td>Type</td>';
  $str.='<td>Row format</td>';
  $str.='<td>Rows</td>';
  $str.='<td>Average row\'s length, b</td>';
  $str.='<td>Data length, b</td>';
  $str.='<td>Index length, b</td>';
  $str.='<td>Data free, b</td>';
  $str.='<td>Create time</td>';
  $str.='<td>Max data length, Tb</td>';
  $str.='<td><input width=20 height=20 type=image src="./style/'.$conf->style.'/image/info.gif" title="Download base" border=0 onclick=\'FormVal(check,"'.$conf->dbase.'")\'></td>';
  $str.='</tr>';
  echo $str;
  $sdata=0;
  $sindex=0;
  $srows=0;
  $sfree=0;
  if($checkver>3.22) {
      $request='SHOW TABLE STATUS LIKE "aa%"';
      $result=db_query($request,$conf->link);
      if(!$result) {$err->reason('check.php|check|the request \''.$request.'\' has failed -- '.db_error($conf->link));return;}
      while($row=db_fetch_object($result)) {
          $str='<tr bgcolor="#C0C0C0"">';
          $str.='<td>'.$row->Name.'</td>';
          if(isset($row->Type)) $str.='<td>'.$row->Type.'</td>';
          else $str.='<td>'.$row->Engine.'</td>';
          $str.='<td>'.$row->Row_format.'</td>';
          $str.='<td align="right">'.$row->Rows.'</td>';
          $str.='<td align="right">'.$row->Avg_row_length.'</td>';
          $str.='<td align="right">'.$row->Data_length.'</td>';
          if($row->Index_length<$row->Data_length) $str.='<td align="right">'.$row->Index_length.'</td>';
          else  $str.='<td align="right"><font color="#FF0000">'.$row->Index_length.'</font></td></td>';
          if($row->Data_free==0) $str.='<td align="right">'.$row->Data_free.'</td>';
          else $str.='<td align="right"><font color="#FF0000">'.$row->Data_free.'</font></td>';
          $str.='<td align="center">'.$row->Create_time.'</td>';
          $str.='<td align="right">'.sprintf("%.3f",$row->Max_data_length/1024/1024/1024).'</td>';
          $str.='<td align="center"><input width=20 height=20 type=image src="./style/'.$conf->style.'/image/info.gif" title="Download '.$row->Name.'" border=0 onclick=\'FormVal(check,"'.$row->Name.'")\'></td>';
          $str.='</tr>';
          echo $str;
          $sdata+=$row->Data_length;
          $sindex+=$row->Index_length;
          $srows+=$row->Rows;
          $sfree+=$row->Data_free;
      }
      db_free_result($result);
  }
  else {
      $request='SHOW TABLES LIKE "aa%"';
      $result=db_query($request,$conf->link);
      if(!$result) {$err->reason('check.php|check|the request \''.$request.'\' has failed -- '.db_error($conf->link));return;}
      while($row=db_fetch_row($result)) {
          $avg=0;
          $rows=0;
          $data=0;
          $request='DESCRIBE '.$row[0];
          $resultc=db_query($request,$conf->link);
          if(!$resultc) {$err->reason('check.php|check|the request \''.$request.'\' has failed -- '.db_error($conf->link));return;}
          while($rowc=db_fetch_object($resultc)) {
              $parse=split('\(',$rowc->Type);
              $parse=split('\)',$parse[1]);
              $avg+=$parse[0];
          }
          db_free_result($resultc);
          $request='SELECT COUNT(*) FROM '.$row[0];
          $resultc=db_query($request,$conf->link);
          if(!$resultc) {$err->reason('check.php|check|the request \''.$request.'\' has failed -- '.db_error($conf->link));return;}
          if(db_num_rows($resultc)) {
              $rowc=db_fetch_row($resultc);
              $rows=$rowc[0];
          }
          $data=$avg*$rows;
          db_free_result($resultc);
          $str='<tr bgcolor="#E0E0E0">';
          $str.='<td>'.$row[0].'</td>';
          $str.='<td></td>';
          $str.='<td></td>';
          $str.='<td align="right">'.$rows.'</td>';
          $str.='<td align="right">'.$avg.'</td>';
          $str.='<td align="right">'.$data.'</td>';
          $str.='<td></td>';
          $str.='<td></td>';
          $str.='<td></td>';
          $str.='<td></td>';
          $str.='<td align="center"><input width=20 height=20 type=image src="./style/'.$conf->style.'/image/info.gif" title="Download '.$row[0].'" border=0 onclick=\'FormVal(check,"'.$row[0].'")\'></td>';
          $str.='</tr>';
          echo $str;
          $sdata+=$data;
          $srows+=$rows;
      }
      db_free_result($result);
  }
  $str='<tr align="center"><td colspan="11" bgcolor="#C0C0C0">Total data = '.$sdata.'</td></tr>';
  $str.='<tr align="center"><td colspan="11" bgcolor="#C0C0C0">Total index = '.$sindex.'</td></tr>';
  $str.='<tr align="center"><td colspan="11" bgcolor="#C0C0C0">Total free = '.$sfree.'</td></tr>';
  $str.='<tr align="center"><td colspan="11" bgcolor="#C0C0C0">Total data+index+free = '.($sdata+$sindex+$sfree).'</td></tr>';
  $str.='<tr align="center"><td colspan="11" bgcolor="#C0C0C0">Total rows = '.$srows.'</td></tr>';
  $str.='</table></form></body></html>';
  echo $str;

}

?>
Return current item: ActualAnalyzer