Guest User

Untitled

a guest
Jun 16th, 2018
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.25 KB | None | 0 0
  1. <?php
  2. #####################################################
  3. ### Konfiguration ###################################
  4. #####################################################
  5.  
  6.  
  7. /// Page-Id Typo3
  8. $pid="399";
  9.  
  10. /// hier Pfad zum XML-Import-Verzeichnis editieren
  11. $import_dir_xml="/fileadmin/scripte/import/";
  12.  
  13. /// hier Pfad zum Image-Import-Verzeichnis editieren
  14. $import_dir_img="/uploads/pics/";
  15.  
  16. /// MySQL Zugangsdaten
  17. $db_server="xxx.com";
  18. $db_name="xxx";
  19. $db_user="xxx";
  20. $db_password="xxx";
  21.  
  22. #####################################################
  23. #####################################################
  24. #####################################################
  25.  
  26. class news_import
  27. {
  28.  
  29. var $pid;
  30. var $db_server;
  31. var $db_name;
  32. var $db_user;
  33. var $db_password;
  34. var $source_path;
  35. var $img_target_path;
  36. var $import_dir_img;
  37. var $script_run;
  38. var $files_in_dir=array();
  39. var $files_in_temp=array();
  40. var $files_data=array();
  41. var $files_extract=array();
  42. var $count_status=0;
  43. var $ausgabe;
  44.  
  45. function ini($pid, $import_dir_xml, $import_dir_img, $db_server, $db_name, $db_user, $db_password){
  46. $this->source_path=$_SERVER["DOCUMENT_ROOT"].$import_dir_xml;
  47. $this->img_target_path=$_SERVER["DOCUMENT_ROOT"].$import_dir_img;
  48. $this->import_dir_img=$import_dir_img;
  49. $this->pid=$pid;
  50. $this->db_server=$db_server;
  51. $this->db_name=$db_name;
  52. $this->db_user=$db_user;
  53. $this->db_password=$db_password;
  54.  
  55. $this->script_run=$this->source_path."progress.txt";
  56.  
  57. if(file_exists($this->script_run)){ die("Script wird gerade ausgeführt..!"); }
  58. else{
  59. /// Datei anlegen
  60. $datei=fopen($this->script_run, "a");
  61. fclose($datei);
  62. }
  63. }
  64.  
  65. function read_dir(){
  66. /// Prüfen ob importierbare Dateien im Verzeichnis
  67. $verz=opendir($this->source_path);
  68. while($file=readdir($verz)){
  69. if(filetype($this->source_path.$file)!="dir"){
  70. if(substr($file, -3)=="xml"){ $this->files_in_dir[]=$file; }
  71. else{ continue; }
  72. }
  73. }
  74. closedir($verz);
  75. }
  76.  
  77. function rename_files(){
  78. if(!empty($this->files_in_dir) and is_array($this->files_in_dir)){
  79. foreach($this->files_in_dir as $key=>$value){
  80. $name="";
  81. $pieces=explode(".xml", $value);
  82. foreach($pieces as $a=>$b){
  83. $name.=$b;
  84. }
  85. rename($this->source_path.$value, $this->source_path.$name."_temp.xml");
  86. $this->files_in_temp[$name]=$this->source_path.$name."_temp.xml";
  87. }
  88. }
  89. }
  90.  
  91. function parse_into_array(){
  92. if(!empty($this->files_in_temp) and is_array($this->files_in_temp)){
  93. foreach($this->files_in_temp as $key=>$value){
  94. $xml_filename=$this->files_in_temp[$key];
  95. $xml_parser_handle = xml_parser_create();
  96.  
  97. if(!fopen($xml_filename, 'r')){ die("FEHLER: Datei $xml_filename nicht gefunden."); }
  98. else{ $parse_handle = fopen($xml_filename, 'r'); }
  99.  
  100. $xml_data = fread($parse_handle, filesize($xml_filename));
  101. xml_parse_into_struct($xml_parser_handle, $xml_data, $xml_values, $xml_index);
  102.  
  103. 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))); }
  104.  
  105. xml_parser_free($xml_parser_handle);
  106. $this->files_data[$key]=$xml_values;
  107.  
  108. unset($xml_filename);
  109. }
  110. }
  111. }
  112.  
  113. function filter_values(){
  114. if(!empty($this->files_data) and is_array($this->files_data)){
  115. foreach($this->files_data as $key_wrap=>$value_wrap){
  116. $this->files_extract[$key_wrap]=array();
  117.  
  118. foreach($value_wrap as $key=>$value){
  119. switch(strtolower($value["tag"])){
  120. case "channel":
  121. if($value["type"]=="open"){ $this->files_extract[$key_wrap][strtolower($value["tag"])]=$this->unhtmlentities($value["attributes"]["ID"]); }
  122. break;
  123.  
  124. case "date":
  125. $this->files_extract[$key_wrap][strtolower($value["tag"])]=$value["value"];
  126. break;
  127.  
  128. case "expire": case "number": case "title": case "subtitle":
  129. case "placemark": case "teaser": case "text": case "footnote": case "contact":
  130. case "internal": case "visible": case "visiblepublicwww":
  131. $this->files_extract[$key_wrap][strtolower($value["tag"])]=$this->unhtmlentities($value["value"]);
  132. break;
  133.  
  134. case "media":
  135. if(!empty($value["attributes"]) and is_array($value["attributes"])){
  136. if(!is_array($this->files_extract[$key_wrap][strtolower($value["tag"])])){ $this->files_extract[$key_wrap][strtolower($value["tag"])]=array();}
  137.  
  138. $count=count($this->files_extract[$key_wrap][strtolower($value["tag"])]) +1;
  139. $this->files_extract[$key_wrap][strtolower($value["tag"])][$count]=array();
  140.  
  141. $this->files_extract[$key_wrap][strtolower($value["tag"])][$count]["id"]=$value["attributes"]["ID"];
  142. $this->files_extract[$key_wrap][strtolower($value["tag"])][$count]["url"]=urldecode($value["attributes"]["URL"]);
  143. $this->files_extract[$key_wrap][strtolower($value["tag"])][$count]["previewurl"]=urldecode($value["attributes"]["PREVIEWURL"]);
  144. $this->files_extract[$key_wrap][strtolower($value["tag"])][$count]["value"]=$this->unhtmlentities($value["value"]);
  145. }
  146. break;
  147.  
  148. case "filter":
  149. if(!empty($value["attributes"]) and is_array($value["attributes"])){
  150. if(!is_array($this->files_extract[$key_wrap][strtolower($value["tag"])])){ $this->files_extract[$key_wrap][strtolower($value["tag"])]=array();}
  151.  
  152. $count=count($this->files_extract[$key_wrap][strtolower($value["tag"])]) +1;
  153. $this->files_extract[$key_wrap][strtolower($value["tag"])][$count]=array();
  154.  
  155. $this->files_extract[$key_wrap][strtolower($value["tag"])][$count]["fid"]=$value["attributes"]["FID"];
  156. $this->files_extract[$key_wrap][strtolower($value["tag"])][$count]["value"]=$value["value"];
  157. }
  158. break;
  159. }
  160. }
  161. }
  162. }
  163. }
  164.  
  165. function update_db(){
  166. if(!empty($this->files_extract) and is_array($this->files_extract)){
  167. foreach($this->files_extract as $key=>$value){
  168. if(is_array($value)){
  169. /// Datum Uhrzeit konvertieren für Typo3
  170. $date=explode(".", $value["date"]);
  171. if(!empty($date) and is_array($date)){ $datetime=mktime(0, 0, 0, intval($date[1]), intval($date[0]), intval($date[2])); }
  172. else{ $datetime=mktime(0, 0, 0, intval(date('d')), intval(date('m')), intval(date('Y'))); }
  173.  
  174. /// alle Bilder importieren
  175. if(!empty($value["media"]) and count($value["media"])>1){
  176. foreach($value["media"] as $key_media=>$value_media){
  177. $filename.=$this->grab_image($value_media["url"]).",";
  178. }
  179. $filename=substr($filename, 0, -1);
  180. }
  181. elseif(!empty($value["media"]["1"]["url"]) and count($value["media"])==1){ $filename=$this->grab_image($value["media"]["1"]["url"]); }
  182. else{ $filename=""; }
  183.  
  184. /// placemark und text zusammenfassen
  185. if(!empty($value["placemark"]) and substr($value["placemark"], -1)!="."){ $text="<span class=\"placemark\">".$value["placemark"].". </span>"; }
  186. elseif(!empty($value["placemark"]) and substr($value["placemark"], -1)=="."){ $text="<span class=\"placemark\">".$value["placemark"]." </span>"; }
  187. $text.=$value["text"];
  188.  
  189. $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."')";
  190. $connection=mysql_connect($this->db_server, $this->db_user, $this->db_password);
  191. mysql_select_db($this->db_name,$connection);
  192. mysql_query($sql,$connection);
  193. mysql_close($connection);
  194.  
  195. unset($date, $datetime, $filename, $text);
  196.  
  197. $this->count_status++;
  198. }
  199. }
  200. }
  201. }
  202.  
  203. function clear_run(){
  204. if(file_exists($this->script_run)){ unlink($this->script_run); }
  205. if(!empty($this->files_in_temp) and is_array($this->files_in_temp)){
  206. foreach($this->files_in_temp as $key=>$value){
  207. if(file_exists($value)){ unlink($value); }
  208. }
  209. }
  210. }
  211.  
  212. function status(){
  213. echo "PRESS/NEWS_IMPORT_CLASS :: Es konnten ".$this->count_status." von ".count($this->files_extract)." Pressemitteilungen importiert werden.\n";
  214. }
  215.  
  216. function grab_image($url){
  217. $file_array=explode("/", $url);
  218. $filename.=time()."_";
  219. $filename.=$file_array[(count($file_array) - 1)];
  220.  
  221. $img=curl_init($url);
  222. curl_setopt($img, CURLOPT_RETURNTRANSFER, true);
  223. curl_setopt($img, CURLOPT_BINARYTRANSFER, true);
  224. $output=curl_exec($img);
  225. //echo "Fehler ist: ".curl_error($img)."\n";
  226. curl_close($img);
  227.  
  228. $fh=fopen($this->img_target_path.$filename, "w");
  229.  
  230. fwrite($fh, $output);
  231. fclose($fh);
  232.  
  233. return $filename;
  234. }
  235.  
  236. function unhtmlentities($string){
  237. $convert=array(
  238. '&#xc4;'=>'&Auml;',
  239. '&#xe4;'=>'&auml;',
  240. '&#xdc;'=>'&Uuml;',
  241. '&#xfc;'=>'&uuml;',
  242. '&#xd6;'=>'&Ouml;',
  243. '&#xf6;'=>'&ouml;',
  244. '&#xdf;'=>'&szlig;',
  245. '&#x96;'=>'&ndash;',
  246. '"'=>'"',
  247. '<'=>'<',
  248. '>'=>'>',
  249. '&#x84;'=>'"',
  250. '&#x93;'=>'"'
  251. );
  252.  
  253. foreach($convert as $key=>$value){
  254. $string=str_replace($key, $value, $string);
  255. }
  256.  
  257. return $string;
  258. }
  259.  
  260. function news_import($pid, $import_dir_xml, $import_dir_img, $db_server, $db_name, $db_user, $db_password){
  261. $this->ini($pid, $import_dir_xml, $import_dir_img, $db_server, $db_name, $db_user, $db_password);
  262. $this->read_dir();
  263. $this->rename_files();
  264. $this->parse_into_array();
  265. $this->filter_values();
  266. $this->update_db();
  267. $this->clear_run();
  268. $this->status();
  269. }
  270. }
  271.  
  272. $go=new news_import($pid, $import_dir_xml, $import_dir_img, $db_server, $db_name, $db_user, $db_password);
  273. ?>
Add Comment
Please, Sign In to add comment