Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- #####################################################
- ### Konfiguration ###################################
- #####################################################
- /// Page-Id Typo3
- $pid="399";
- /// hier Pfad zum XML-Import-Verzeichnis editieren
- $import_dir_xml="/fileadmin/scripte/import/";
- /// hier Pfad zum Image-Import-Verzeichnis editieren
- $import_dir_img="/uploads/pics/";
- /// MySQL Zugangsdaten
- $db_server="xxx.com";
- $db_name="xxx";
- $db_user="xxx";
- $db_password="xxx";
- #####################################################
- #####################################################
- #####################################################
- class news_import
- {
- var $pid;
- var $db_server;
- var $db_name;
- var $db_user;
- var $db_password;
- var $source_path;
- var $img_target_path;
- var $import_dir_img;
- var $script_run;
- var $files_in_dir=array();
- var $files_in_temp=array();
- var $files_data=array();
- var $files_extract=array();
- var $count_status=0;
- var $ausgabe;
- function ini($pid, $import_dir_xml, $import_dir_img, $db_server, $db_name, $db_user, $db_password){
- $this->source_path=$_SERVER["DOCUMENT_ROOT"].$import_dir_xml;
- $this->img_target_path=$_SERVER["DOCUMENT_ROOT"].$import_dir_img;
- $this->import_dir_img=$import_dir_img;
- $this->pid=$pid;
- $this->db_server=$db_server;
- $this->db_name=$db_name;
- $this->db_user=$db_user;
- $this->db_password=$db_password;
- $this->script_run=$this->source_path."progress.txt";
- if(file_exists($this->script_run)){ die("Script wird gerade ausgeführt..!"); }
- else{
- /// Datei anlegen
- $datei=fopen($this->script_run, "a");
- fclose($datei);
- }
- }
- function read_dir(){
- /// Prüfen ob importierbare Dateien im Verzeichnis
- $verz=opendir($this->source_path);
- while($file=readdir($verz)){
- if(filetype($this->source_path.$file)!="dir"){
- if(substr($file, -3)=="xml"){ $this->files_in_dir[]=$file; }
- else{ continue; }
- }
- }
- closedir($verz);
- }
- function rename_files(){
- if(!empty($this->files_in_dir) and is_array($this->files_in_dir)){
- foreach($this->files_in_dir as $key=>$value){
- $name="";
- $pieces=explode(".xml", $value);
- foreach($pieces as $a=>$b){
- $name.=$b;
- }
- rename($this->source_path.$value, $this->source_path.$name."_temp.xml");
- $this->files_in_temp[$name]=$this->source_path.$name."_temp.xml";
- }
- }
- }
- function parse_into_array(){
- if(!empty($this->files_in_temp) and is_array($this->files_in_temp)){
- foreach($this->files_in_temp as $key=>$value){
- $xml_filename=$this->files_in_temp[$key];
- $xml_parser_handle = xml_parser_create();
- if(!fopen($xml_filename, 'r')){ die("FEHLER: Datei $xml_filename nicht gefunden."); }
- else{ $parse_handle = fopen($xml_filename, 'r'); }
- $xml_data = fread($parse_handle, filesize($xml_filename));
- xml_parse_into_struct($xml_parser_handle, $xml_data, $xml_values, $xml_index);
- if(!is_array($xml_values)){ die(sprintf('XML error: %s at line %d', xml_error_string(xml_get_error_code($xml_parser_handle)), xml_get_current_line_number($xml_parser_handle))); }
- xml_parser_free($xml_parser_handle);
- $this->files_data[$key]=$xml_values;
- unset($xml_filename);
- }
- }
- }
- function filter_values(){
- if(!empty($this->files_data) and is_array($this->files_data)){
- foreach($this->files_data as $key_wrap=>$value_wrap){
- $this->files_extract[$key_wrap]=array();
- foreach($value_wrap as $key=>$value){
- switch(strtolower($value["tag"])){
- case "channel":
- if($value["type"]=="open"){ $this->files_extract[$key_wrap][strtolower($value["tag"])]=$this->unhtmlentities($value["attributes"]["ID"]); }
- break;
- case "date":
- $this->files_extract[$key_wrap][strtolower($value["tag"])]=$value["value"];
- break;
- case "expire": case "number": case "title": case "subtitle":
- case "placemark": case "teaser": case "text": case "footnote": case "contact":
- case "internal": case "visible": case "visiblepublicwww":
- $this->files_extract[$key_wrap][strtolower($value["tag"])]=$this->unhtmlentities($value["value"]);
- break;
- case "media":
- if(!empty($value["attributes"]) and is_array($value["attributes"])){
- if(!is_array($this->files_extract[$key_wrap][strtolower($value["tag"])])){ $this->files_extract[$key_wrap][strtolower($value["tag"])]=array();}
- $count=count($this->files_extract[$key_wrap][strtolower($value["tag"])]) +1;
- $this->files_extract[$key_wrap][strtolower($value["tag"])][$count]=array();
- $this->files_extract[$key_wrap][strtolower($value["tag"])][$count]["id"]=$value["attributes"]["ID"];
- $this->files_extract[$key_wrap][strtolower($value["tag"])][$count]["url"]=urldecode($value["attributes"]["URL"]);
- $this->files_extract[$key_wrap][strtolower($value["tag"])][$count]["previewurl"]=urldecode($value["attributes"]["PREVIEWURL"]);
- $this->files_extract[$key_wrap][strtolower($value["tag"])][$count]["value"]=$this->unhtmlentities($value["value"]);
- }
- break;
- case "filter":
- if(!empty($value["attributes"]) and is_array($value["attributes"])){
- if(!is_array($this->files_extract[$key_wrap][strtolower($value["tag"])])){ $this->files_extract[$key_wrap][strtolower($value["tag"])]=array();}
- $count=count($this->files_extract[$key_wrap][strtolower($value["tag"])]) +1;
- $this->files_extract[$key_wrap][strtolower($value["tag"])][$count]=array();
- $this->files_extract[$key_wrap][strtolower($value["tag"])][$count]["fid"]=$value["attributes"]["FID"];
- $this->files_extract[$key_wrap][strtolower($value["tag"])][$count]["value"]=$value["value"];
- }
- break;
- }
- }
- }
- }
- }
- function update_db(){
- if(!empty($this->files_extract) and is_array($this->files_extract)){
- foreach($this->files_extract as $key=>$value){
- if(is_array($value)){
- /// Datum Uhrzeit konvertieren für Typo3
- $date=explode(".", $value["date"]);
- if(!empty($date) and is_array($date)){ $datetime=mktime(0, 0, 0, intval($date[1]), intval($date[0]), intval($date[2])); }
- else{ $datetime=mktime(0, 0, 0, intval(date('d')), intval(date('m')), intval(date('Y'))); }
- /// alle Bilder importieren
- if(!empty($value["media"]) and count($value["media"])>1){
- foreach($value["media"] as $key_media=>$value_media){
- $filename.=$this->grab_image($value_media["url"]).",";
- }
- $filename=substr($filename, 0, -1);
- }
- elseif(!empty($value["media"]["1"]["url"]) and count($value["media"])==1){ $filename=$this->grab_image($value["media"]["1"]["url"]); }
- else{ $filename=""; }
- /// placemark und text zusammenfassen
- if(!empty($value["placemark"]) and substr($value["placemark"], -1)!="."){ $text="<span class=\"placemark\">".$value["placemark"].". </span>"; }
- elseif(!empty($value["placemark"]) and substr($value["placemark"], -1)=="."){ $text="<span class=\"placemark\">".$value["placemark"]." </span>"; }
- $text.=$value["text"];
- $sql="INSERT INTO tt_news (uid, pid, title, short, bodytext, author, datetime, image) VALUES ('', '".$this->pid."', '".$value["title"]."', '".$value["subtitle"]."', '".$text."', '".$value["contact"]."', '".$datetime."', '".$filename."')";
- $connection=mysql_connect($this->db_server, $this->db_user, $this->db_password);
- mysql_select_db($this->db_name,$connection);
- mysql_query($sql,$connection);
- mysql_close($connection);
- unset($date, $datetime, $filename, $text);
- $this->count_status++;
- }
- }
- }
- }
- function clear_run(){
- if(file_exists($this->script_run)){ unlink($this->script_run); }
- if(!empty($this->files_in_temp) and is_array($this->files_in_temp)){
- foreach($this->files_in_temp as $key=>$value){
- if(file_exists($value)){ unlink($value); }
- }
- }
- }
- function status(){
- echo "PRESS/NEWS_IMPORT_CLASS :: Es konnten ".$this->count_status." von ".count($this->files_extract)." Pressemitteilungen importiert werden.\n";
- }
- function grab_image($url){
- $file_array=explode("/", $url);
- $filename.=time()."_";
- $filename.=$file_array[(count($file_array) - 1)];
- $img=curl_init($url);
- curl_setopt($img, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($img, CURLOPT_BINARYTRANSFER, true);
- $output=curl_exec($img);
- //echo "Fehler ist: ".curl_error($img)."\n";
- curl_close($img);
- $fh=fopen($this->img_target_path.$filename, "w");
- fwrite($fh, $output);
- fclose($fh);
- return $filename;
- }
- function unhtmlentities($string){
- $convert=array(
- 'Ä'=>'Ä',
- 'ä'=>'ä',
- 'Ü'=>'Ü',
- 'ü'=>'ü',
- 'Ö'=>'Ö',
- 'ö'=>'ö',
- 'ß'=>'ß',
- '–'=>'–',
- '"'=>'"',
- '<'=>'<',
- '>'=>'>',
- '„'=>'"',
- '“'=>'"'
- );
- foreach($convert as $key=>$value){
- $string=str_replace($key, $value, $string);
- }
- return $string;
- }
- function news_import($pid, $import_dir_xml, $import_dir_img, $db_server, $db_name, $db_user, $db_password){
- $this->ini($pid, $import_dir_xml, $import_dir_img, $db_server, $db_name, $db_user, $db_password);
- $this->read_dir();
- $this->rename_files();
- $this->parse_into_array();
- $this->filter_values();
- $this->update_db();
- $this->clear_run();
- $this->status();
- }
- }
- $go=new news_import($pid, $import_dir_xml, $import_dir_img, $db_server, $db_name, $db_user, $db_password);
- ?>
Add Comment
Please, Sign In to add comment