path = $path; $this->filename = $filename; } public function sync() { if($this->get_ids_events()) { $this->set_ids_events(); return($this->error_message); } } private function get_ids_events() { if(file_exists($this->path . "/" . $this->filename . ".page")) { $string_page = file_get_contents($this->path . "/" . $this->filename . ".page"); $string_page = str_replace("\n"," ",$string_page); $string_page = str_replace("\r"," ",$string_page); $string_page = str_replace("<%","/*",$string_page); $string_page = str_replace("%>","*/",$string_page); $start_here = 0; $begin_tag_pos = strpos($string_page," 0) { $end_tag_pos = strpos($string_page,">",$begin_tag_pos) + 1; $start_here = $end_tag_pos + 1; // find the current tag $current_tag = substr($string_page, $begin_tag_pos, $end_tag_pos - $begin_tag_pos); // normalize tag $current_tag = $this->normalize($current_tag); // construction of "id classes" string ed "event classes" array if($current_tag != "") { $this->get_id_events($current_tag); } // next tag $begin_tag_pos = strpos($string_page,"path . "/" . $this->filename . ".page" . "' not exists."; return(false); } } private function normalize($tag) { // cut '' or '/>' at ag end if(strpos($tag, "") != strlen($tag) - 1 ) { echo "Error: tag malformed \n" . $tag; return(""); } else { $tpm_lungh = strlen($tag) - 6; $tag = substr($tag, 5,$tpm_lungh); if(strpos($tag,"/") == strlen($tag) - 1) { $tag = substr($tag, 0,strlen($tag) - 1); } // error if tag contains '<' if(strpos($tag, "<") > 0) { echo "Error: tag malformed \n"; return(""); } // remove blank spaces near '=' while(strpos($tag, " = ") > 0 || strpos($tag, " =") > 0 || strpos($tag, "= ") > 0) { $tag = str_replace(" = ", "=", $tag); $tag = str_replace(" =", "=", $tag); $tag = str_replace("= ", "=", $tag); } return($tag); } } private function get_id_events($tag) { if($tag == "") { return; } $props = explode(" ", $tag); $tag_class = $props["0"]; unset($props["0"]); $tag_id= ""; $tag_events = array(); foreach ($props as $prop) { $name_value = explode("=", $prop); if($name_value["0"] == "ID") { $tag_id = substr($name_value["1"], 1,strlen($name_value["1"]) - 2); } if(substr($name_value["0"],0,2) == "On") { $current_handler = substr($name_value["1"], 1,strlen($name_value["1"] ) - 2); if($current_handler != "" && $tag_class != "") { array_push($tag_events, $current_handler . " " . $tag_class); } } } if($tag_id != "") { array_push($this->property, $tag_class . " " . $tag_id); } if($tag_events != array()) { $this->events = array_merge($this->events, $tag_events); } return; } private function set_ids_events() { if(!file_exists($this->path . "/" . $this->filename . ".php")) { $nuovo_file_php .= "filename . " extends TPage" . "\r\n"; $nuovo_file_php .= "{" . "\r\n"; $nuovo_file_php .= "" . "\r\n"; $nuovo_file_php .= "}" . "\r\n"; $nuovo_file_php .= "?>" . "\r\n"; if(file_put_contents($this->path . "/" . $this->filename . ".php", $nuovo_file_php) === false) { $this->error_message = "Errore nel creare il file '" . $this->path . "/" . $this->filename . ".php" . "'"; return false; } } $string_php = file_get_contents($this->path . "/" . $this->filename . ".php"); $from_here = strpos($string_php, "// PAGEPHPSYNC: DO NOT EDIT FROM HERE FROM HERE //"); $to_here = strpos($string_php, "// PAGEPHPSYNC: TO HERE TO HERE TO HERE TO HERE //"); if($from_here === false || $to_here === false) { if($from_here === false && $to_here === false) { $class_pos = strpos($string_php, "class " . $this->filename . " extends TPage"); if($class_pos === false) { $this->error_message = "I can't find '" . $this->filename . "' class definition"; return false; } $string_php = $this->insert_string($string_php,$class_pos,"\r\n" . "// PAGEPHPSYNC: DO NOT EDIT FROM HERE FROM HERE //" . "\r\n" . "// PAGEPHPSYNC: TO HERE TO HERE TO HERE TO HERE //" . "\r\n"); if(file_put_contents($this->path . "/" . $this->filename . ".php", $string_php) === false) { $this->error_message = "Errore nel modificare il file '" . $this->path . "/" . $this->filename . ".php" . "'"; return false; } } else { $this->error_message ="You edited '// DO NOT EDIT' lines. Remove all '// DO NOT EDIT' lines and retry"; return false; } } if($this->property == array()) { return true; } $from_here = strpos($string_php, "// PAGEPHPSYNC: DO NOT EDIT FROM HERE FROM HERE //"); $to_here = strpos($string_php, "// PAGEPHPSYNC: TO HERE TO HERE TO HERE TO HERE //"); $string_php = $this->remove_substring($string_php, $from_here, $to_here + 50); $string_to_add = "// PAGEPHPSYNC: DO NOT EDIT FROM HERE FROM HERE //" . "\r\n"; $string_to_add .= "/**" . "\r\n"; foreach ($this->property as $prop) { $string_to_add .= "* @property " . $prop . "\r\n"; } $string_to_add .= "**/ " . "\r\n"; $string_to_add .= "// PAGEPHPSYNC: TO HERE TO HERE TO HERE TO HERE //"; $string_php = $this->insert_string($string_php, $from_here, $string_to_add); foreach ($this->events as $event_class) { $tmp_event_class = explode(" ", $event_class); $cur_event = $tmp_event_class["0"]; $cur_class = $tmp_event_class["1"]; if(!$this->exists_event($string_php,$cur_event)) { $this->add_event($string_php,$cur_event,$cur_class); } } if(file_put_contents($this->path . "/" . $this->filename . ".php", $string_php) === false) { $this->error_message = "Errore nel modificare il file '" . $this->path . "/" . $this->filename . ".php" . "'"; return false; } return true; } private function insert_string($my_string, $pos, $to_insert_string) { $my_string_begin = substr($my_string, 0, $pos); $my_string_end = substr($my_string, $pos); return $my_string_begin . $to_insert_string . $my_string_end; } private function remove_substring($my_string, $ini_pos, $end_pos) { $my_string_begin = substr($my_string, 0, $ini_pos); $my_string_end = substr($my_string, $end_pos); return $my_string_begin . $my_string_end; } private function exists_event(&$string_php,$event) { // remove blank spaces after 'function ' while(strpos($string_php, "function ") > 0) { $string_php = str_replace("function ", "function ", $string_php); } if(strpos($string_php, "function " . $event) === false) { return false; } else { return true; } } private function add_event(&$string_php,$event,$class) { $event_pos = strrpos($string_php, "}") - 1 ; $event_string = "\r\n" . "/**" . "\r\n"; $event_string .= "* @param " . $class . ' $sender' . "\r\n"; $event_string .= "* @param TEventParameter " . '$param' . "\r\n"; $event_string .= "**/" . "\r\n"; $event_string .= " public function " . $event . '($sender,$param)' . "\r\n"; $event_string .= " {" . "\r\n"; $event_string .= "\r\n"; $event_string .= " }" . "\r\n"; $string_php = $this->insert_string($string_php, $event_pos, $event_string); } } if(isset ($_SERVER['argv'][1])) { if($_SERVER['argv'][1] == "--version") { echo "Version 0.9 \n"; return true; } } if(!isset ($_SERVER['argv'][1]) || !isset ($_SERVER['argv'][2])) { if(file_exists("/var/www/PagePhpSync/test/OreProd.page")) { $_SERVER['argv'][1] = "/var/www/PagePhpSync/test"; $_SERVER['argv'][2] = "OreProd"; } else { echo "usage: \n php pageSync path name (name without extension) \n php pageSync --version "; return false; } } $path = $_SERVER['argv'][1]; $name = $_SERVER['argv'][2]; $sync_object = new PagePhpSync($path,$name); echo $sync_object->sync() . "\n"; ?>