<?
class __sync
{
var $db; // database Name
var $host; // data server host name or ip
var $usr; // database user (check rights id buggy)
var $pass; // databade user password
var $array_tables; // array for table names
var $tbl; // active table
var $link; // active internal link
var $debug; // optional 0 or 1
var $array_wc; // where clause array
var $array_dc; // where clause array
var $path_files; // location for files
var $dte_files; // location for files
var $out_content; // output contyents
var $array_sync_files; // nasme of sync file array
var $curr_file; //current active file (part of $array_sync_files)
var $big_file_query; // read of file for sync
var $array_sub_queries; // $big_file_query split into individual queries
var $ht = ""; // ht ouput
//
##
public function get_ht()
{
return $this->ht;
}
##
public function connect_target_do($host, $db, $usr, $pass)
{
$this->db = trim($db);
$this->usr = trim($usr);
$this->pass = trim($pass);
$this->host = trim($host);
$this->mk_connect_and_use();
$this->set_import_of_files();
}
##
private function set_import_of_files()
{
$cnt = count($this->array_sync_files);
for($n=0;$n<$cnt;$n++)
{
$this->curr_file = $this->array_sync_files[$n];
$this->ht .="<p>⇒⇒Sync of <b>{$this->curr_file}</b> started </p>";
$this->sync_this_file();
}
}
private function sync_this_file()
{
if(!basename($this->curr_file))
{
$this->ht .="<p>⇒⇒⇒ <b>Sync Error {$this->curr_file}</b> DOES NOT EXIST </p>";
}
else
{
$this->ht .="<p>⇒⇒⇒Sync of <b>{$this->curr_file}</b> commensing .. file exists </p>";
if(!empty($this->curr_file))
{
$fp = fopen($this->curr_file, "r") or die ("{$this->curr_file} won't open");
$this->big_file_query = addslashes(fread($fp, 10240000));
fclose($fp);
$this->split_sql_read_file_and_execute();
$this->ht .="<p>⇒⇒⇒⇒<b>{$this->curr_file}</b> COMPLETE</p>";
}
else
{
$this->ht .="<p>⇒⇒⇒<b>{$this->curr_file}</b> not accessed or not readable or empty</p><hr>";
}
}
}
##
private function split_sql_read_file_and_execute()
{
$__tmp = trim($this->big_file_query);
$__tmp_array = explode("*||*", $__tmp);
$cnt = count($__tmp_array);
$executed = 0;
$this->ht .="<p>⇒⇒⇒Number of potential items in <b>{$this->curr_file}</b> = $cnt</p>";
$this->array_sub_queries = $__tmp_array;
for($r=0;$r<$cnt;$r++)
{
$__tmp_item = trim($this->array_sub_queries[$r]);
if((strlen($__tmp_item) > 10) AND (!(substr($__tmp_item, 0, 1) == "#")))
{
$__tmp_item = stripslashes($__tmp_item);
mysql_query ($__tmp_item);
$executed++;
}
}
$this->ht .="<p>⇒⇒⇒Number of sub queries executed in <b>{$this->curr_file}</b> = $executed</p>";
}
##
private function get_table_output()
{
$dc = "";
$wc = "";
// try to get the dc clause
$cnt = count($this->array_dc);
if($cnt > 0)
{
for($h=0;$h<$cnt;$h++)
{
if($this->array_dc[$h]['table'] == $this->tbl)
{
$dc .= "\n"."DELETE FROM {$this->tbl} WHERE ({$this->array_dc[$h]['dc']});*||*";
}
}
}
$this->ht .="<p>⇒⇒⇒DELETE stements for <b>{$this->tbl}</b> are..{$dc}</p>";
// try to get the select where clause .. may only be one .. dont' be a dork
$cnt = count($this->array_wc);
if($cnt > 0)
{
for($h=0;$h<$cnt;$h++)
{
if($this->array_wc[$h]['table'] == $this->tbl)
{
$wc .= "WHERE ({$this->array_wc[$h]['wc']});*||*"."\n";
}
}
}
$query = "SELECT * FROM {$this->tbl} $wc";
$this->ht .="<p>⇒⇒⇒Sync Creeation QUERY is <b>{$query}</b></p>";
$this->out_content .="\n"."## ".$query;
$actual_query = str_replace("*||*", "", $query);
$result = mysql_query($actual_query,$this->link);
$all_query = '';
$__ccnntt = 0;
while($row = mysql_fetch_row($result))
{
// replace statement
$str_query = "\n"."REPLACE INTO {$this->tbl} VALUES (";
for($i=0; $i<mysql_num_fields($result);$i++)
{
if(!isset($row[$i])) $str_query .= "NULL,";
else if($row[$i] != "") $str_query .= "'".$row[$i]."',";
else $str_query .= "'',";
}
$str_query .= ")";
$str_query = str_replace(",)", ")", $str_query);
$str_query .= ";*||*";
$str_query = ereg_replace(",$","",$str_query);
$all_query .= $str_query;
$__ccnntt++;
}
$tmp__ht = "<b>$__ccnntt</b> RESULTS";
if($__ccnntt = 0)
{
$tmp__ht = "<b>NO RESULTS</b>";
}
$this->ht .="<p>⇒⇒⇒{$tmp__ht} for <b>{$this->tbl}</b></p>";
$this->out_content .= $dc."\n\n".$all_query."\n\n".$dc."\n"."## EOF({$this->tbl});*||*";
}
##
public function set_date_setting()
{
$this->dte_files = date("YmdHis");
$this->ht .="<p>⇒⇒Date Vars (this import session) set as <b>{$this->dte_files}</b></p>";
}
##
public function create_sync_output()
{
$this->out_content = "## SyncFile created on {$this->dte_files};*||*"."\n";
$this->ht .="<p>⇒⇒⇒Output Started as {$this->out_content }</p>";
for ($i=0; $i < count($this->array_tables); $i++ )
{
$this->tbl = $this->array_tables[$i];
$this->get_table_output();
$fn = $this->path_files."yas_".$this->dte_files."_".$this->tbl."_sql.php"; // use PHP extention for security on web-servers
$this->array_sync_files[$i] = $fn;
$fp = fopen ($fn,"w");
fwrite ($fp,$this->out_content);
fclose ($fp);
$tmp_bytes = filesize($fn);
$this->ht .="<p>⇒⇒⇒Output for <b>{$fn}</b> created with <b>$tmp_bytes</b> bytes - Max is <b> 204,800 </b> </p>";
}
}
##
public function set_files_path($path)
{
$this->path_files = $path;
return;
}
##
public function set_where_delete($dc)
{
if(!isset($this->array_dc))
{
$cnt = 0;
}
else
{
$cnt = count($this->array_dc);
}
$expl_this_one = explode("|", $dc);
$this->array_dc[$cnt]['table'] = trim($expl_this_one[0]);
$this->array_dc[$cnt]['dc'] = trim($expl_this_one[1]);
$this->ht .= "<p>⇒⇒Delete clause set for IMPORT on sync table {$this->array_dc[$cnt]['table']} as {$this->array_dc[$cnt]['wc']}</p>";
}
##
public function set_where_select($wc)
{
if(!isset($this->array_wc))
{
$cnt = 0;
}
else
{
$cnt = count($this->array_wc);
}
$expl_this_one = explode("|", $wc);
$this->array_wc[$cnt]['table'] = trim($expl_this_one[0]);
$this->array_wc[$cnt]['wc'] = trim($expl_this_one[1]);
$this->ht .= "<p>⇒⇒Where clause set for extract from {$this->array_wc[$cnt]['table']} as {$this->array_wc[$cnt]['wc']}</p>";
}
##
public function set_debug($opt)
{
$this->debug = (int)$opt;
}
##
public function set_tables_4_output($host, $db, $usr, $pass, $tbl)
{
$this->db = trim($db);
$this->usr = trim($usr);
$this->pass = trim($pass);
$this->host = trim($host);
if($tbl == "all") // we want all the tables
{
$this->array_tables = $this->get_tablenames();
}
else
{
$this->array_tables[0] = trim($tbl);
}
$this->ht .= "<p>⇒⇒Attempting connection to <b> {$this->host}.{$this->db}.{$tbl}</b> as <b>{$this->usr}</b></p>";
$this->mk_connect_and_use();
}
private function mk_connect_and_use()
{
if(is_resource($this->link))
{
mysql_close($this->link);
}
$this->link = @mysql_connect($this->host,$this->usr,$this->pass);
if(!is_resource($this->link))
{
$err = mysql_error();
$this->ht .= "<p>⇒⇒Link error ⇒ ($err) ..No Connetion resource for Cttl-- {$this->host}..{$this->usr}</p>";
}
else
{
$this->ht .= "<p>⇒⇒Success Connetion resource for -- <b>{$this->host}</b>..<b>{$this->usr}</b></p>";
mysql_select_db($this->db) or die("no usage of {$this->db} possible");
}
}
##
private function get_tablenames()
{
$result = mysql_list_tables($this->db,$this->link);
while ($row = mysql_fetch_row($result))
{
$this->array_tables[] = $row[0];
}
return $tables;
}
##
}
?>
|