<?php
// hint: https://regex101.com/

function removeChannelAds($text,$type=null) {

       $endungen[] = '/.mp4$/u';
       $endungen[] = '/.m4a$/u';
       $endungen[] = '/.ac3$/u';
       $endungen[] = '/.jpg$/u';
       $endungen[] = '/.mp3$/u';
       $endungen[] = '/.m4v$/u';
       $endungen[] = '/.avi$/u';
       $endungen[] = '/.divx$/u';
       $endungen[] = '/.mov$/u';
       $endungen[] = '/.ts$/u';
       $endungen[] = '/.mpg$/u';
       $endungen[] = '/.mpeg$/u';
       $endungen[] = '/.mkv$/u';
       $endungen[] = '/.nofile$/u';

       //save file extension
       $ext=explode(".",$text);
       $extlength=count($ext)-1;

       //file extensens fix for cutted strings "..."
       $fileExt=false;
       foreach ($endungen as $end) {
         if (preg_match($end, $text)) {$fileExt=true; break; }
       }
/*
- (bindestrich) rausgenommen, channelname: "- 123-2344 -" 

       if ($type!="filename") {$text=preg_replace("/[^a-zA-Z0-9@#ßÄÜÖöüäéíîáâê()'.,\-\!?]+/u", " ",$text); if ($fileExt) $text=preg_replace($endungen, "",$text); }
       if ($type=="filename") {$text=preg_replace("/[^a-zA-Z0-9@#ßÄÜÖöüäéíîáâê()'.,\-\!?]+/u", " ",$text); if ($fileExt) $text=preg_replace($endungen, "",$text); }
*/
       if ($type!="filename") {$text=preg_replace("/[^a-zA-Z0-9@#ßÄÜÖöüäéíîáâê()'.,\!?]+/u", " ",$text); if ($fileExt) $text=preg_replace($endungen, "",$text); }
       if ($type=="filename") {$text=preg_replace("/[^a-zA-Z0-9@#ßÄÜÖöüäéíîáâê()'.,\!?]+/u", " ",$text); if ($fileExt) $text=preg_replace($endungen, "",$text); }



       $newDescriptionTmp=explode(" ", $text);
       $altText="";

       foreach ($newDescriptionTmp as $newDescription) {
        if (substr($newDescription,0,1)!="#"  &&  substr($newDescription,0,1)!="@") {
         $altText.=$newDescription." ";
       }
      }
      $text=$altText;

    $pattern = '/[@|#][^\s\/.,|]+/u';
    while (preg_match($pattern, $text)) {
        $text = preg_replace($pattern, '', $text);
    }
    
    $text = trim($text);

//extension fix 
    if ($type=="filename") $text=substr($text,0,strlen($text)).".".$ext[$extlength];

 return $text;
}

function cleanupString($input) {
 $input=mb_convert_encoding($input, "UTF-8", mb_detect_encoding($input)); // force utf8 encoding 

// remove emojis
 $input=preg_replace('/([0-9|#][\x{20E3}])|[\x{00ae}|\x{00a9}|\x{203C}|\x{2047}|\x{2048}|\x{2049}|\x{3030}|\x{303D}|\x{2139}|\x{2122}|\x{3297}|\x{3299}][\x{FE00}-\x{FEFF}]?|[\x{2190}-\x{21FF}][\x{FE00}-\x{FEFF}]?|[\x{2300}-\x{23FF}][\x{FE00}-\x{FEFF}]?|[\x{2460}-\x{24FF}][\x{FE00}-\x{FEFF}]?|[\x{25A0}-\x{25FF}][\x{FE00}-\x{FEFF}]?|[\x{2600}-\x{27BF}][\x{FE00}-\x{FEFF}]?|[\x{2600}-\x{27BF}][\x{1F000}-\x{1FEFF}]?|[\x{2900}-\x{297F}][\x{FE00}-\x{FEFF}]?|[\x{2B00}-\x{2BF0}][\x{FE00}-\x{FEFF}]?|[\x{1F000}-\x{1F9FF}][\x{FE00}-\x{FEFF}]?|[\x{1F000}-\x{1F9FF}][\x{1F000}-\x{1FEFF}]?/u','', $input);
 
 $input=preg_replace('/[\x00-\x1F\x7F]/u', ' ', $input); // remove binary - eq. newlines 

 $input=mb_convert_encoding(iconv("UTF-8", "ISO-8859-1//IGNORE", $input), "UTF-8", "ISO-8859-1"); // könnte man noch mal gebrauchen... ich habe da so ein gefühl... :-) 

 $input=trim($input);
 return $input;
}

function getMessageMediaAttributes($data) {
  $attributesreturn["duration"]=-1;
  $attributesreturn["width"]=-1;
  $attributesreturn["height"]=-1;
  $attributesreturn["filename"]=-1;
  foreach ($data as $d) {
   foreach (array_keys($d) as $k) {
    if (!is_array($d[$k])) {
     switch ($k) {
      case "duration":  $attributesreturn["duration"]=$d[$k];
      break;
      case "w":         $attributesreturn["width"]=$d[$k];
      break;
      case "h":         $attributesreturn["height"]=$d[$k];
      break;
      case "file_name": $attributesreturn["filename"]=$d[$k];
      break;
    } // switch ($k) 
   } // if (!is_array($d[$k])) 
  } // foreach (array_keys($d) as $k) 
 } // foreach (array_keys($data) as $k) 
 return $attributesreturn;
} //function end

function getMessageMediaPhotoAttributes($data) {
 $attributesreturn["mime_type"]=-1;
 $attributesreturn["width"]=-1;
 $attributesreturn["height"]=-1;
 $attributesreturn["size"]=-1;
 foreach ($data as $d) {
  if ($d["_"] == "photoSize") $readAttr=true;
  foreach (array_keys($d) as $k) {
   if (!is_array($d[$k])) {
    if (isset($readAttr)) {
     switch ($k) {
      case "type": $attributesreturn["mime_type"]=$d[$k];
      break;
      case "w":    $attributesreturn["width"]=$d[$k];
      break;
      case "h":    $attributesreturn["height"]=$d[$k];
      break;
      case "size": $attributesreturn["size"]=$d[$k];
      break;
     } // switch ($k)
    } // if (isset($readAttr) 
   } // if (!is_array($d[$k])) 
  } // foreach (array_keys($d) as $k) 
  if (isset($readAttr)) unset($readAttr);
 } // foreach (array_keys($data) as $k) 
 return $attributesreturn;
} //function end  
 
function getMessageData($messagedata, $messagetype) {
  
 // predefine sort of $messagereturn array 
 $messagereturn["msgid"]=-1;
 $messagereturn["link"]=-1;
 $messagereturn["grouped_id"]=-1;
 $messagereturn["type"]=-1;
 $messagereturn["message"]=-1;
 $messagereturn["cleanmessage"]=-1;
 $messagereturn["reply_to"]=-1;
 $messagereturn["msgdate"]=-1;
 $messagereturn["mime_type"]=-1;
 $messagereturn["duration"]=-1;
 $messagereturn["width"]=-1;
 $messagereturn["height"]=-1;
 $messagereturn["size"]=-1;
 $messagereturn["filedate"]=-1;
 $messagereturn["filename"]=-1;

// print_r($messagedata);

 $channel_id=substr($messagedata["peer_id"], 4);
 $messagereturn["link"]="https://t.me/c/".$channel_id."/".$messagedata["id"];
 
// echo $messagereturn["link"]."\n";
 
// exit;
 
 $messagereturn["msgid"]=$messagedata["id"];
 $messagereturn["type"]=$messagetype;
 $messagereturn["msgdate"]=$messagedata["date"];

// echo "debug ---> link: ".$messagereturn["link"]."\n";
 
 if ($messagetype == "messageMediaDocument") $attributesdata=getMessageMediaAttributes($messagedata["media"]["document"]["attributes"]);
 if ($messagetype == "messageMediaPhoto") $attributesdata=getMessageMediaPhotoAttributes($messagedata["media"]["photo"]["sizes"]);

 if (array_key_exists("grouped_id", $messagedata)) { $messagereturn["grouped_id"]=$messagedata["grouped_id"]; } else $messagereturn["grouped_id"]=-1; 

 if (array_key_exists("reply_to", $messagedata)) { 
  if (array_key_exists("reply_to_msg_id", $messagedata["reply_to"])) {
    $messagereturn["reply_to"]=$messagedata["reply_to"]["reply_to_msg_id"]; 
  } else $messagereturn["reply_to"]=-1;
 } else $messagereturn["reply_to"]=-1;

 if ($messagedata["message"] != "") { $messagereturn["message"]=$messagedata["message"]; } else $messagereturn["message"]=-1;
 if ($messagedata["message"] != "") { $messagereturn["cleanmessage"]=removeChannelAds(cleanupString($messagedata["message"])); } else $messagereturn["cleanmessage"]=-1;

 switch ($messagetype) {
  case "textMessage":
  case "messageMediaWebPage":  
                               $messagereturn["mime_type"]=-1;
                               $messagereturn["duration"]=-1;
                               $messagereturn["width"]=-1;
                               $messagereturn["height"]=-1;
                               $messagereturn["size"]=-1;
                               $messagereturn["filedate"]=-1;
                               $messagereturn["filename"]=-1;
  break;
  case "messageMediaPhoto":
                               $messagereturn["mime_type"]=$attributesdata["mime_type"];
                               $messagereturn["duration"]=-1;
                               $messagereturn["width"]=    $attributesdata["width"];
                               $messagereturn["height"]=   $attributesdata["height"];
                               $messagereturn["size"]=     $attributesdata["size"];
                               $messagereturn["filedate"]= $messagedata["media"]["photo"]["date"];
                               $messagereturn["filename"]= $messagedata["media"]["photo"]["id"]."#".$messagedata["media"]["photo"]["access_hash"];  
  break;
  case "messageMediaDocument": 
                               switch ($messagedata["media"]["document"]["mime_type"]) {
                                case "image/webp": 
                                case "image/png":
                                case "image/gif":
                                case "image/jpeg": 
                                case "image/bmp": 
							    case "video/mp4":
							    case "video/mpeg":
                                case "video/webm": 
                                case "video/quicktime":
                                case "video/x-matroska":  
                                case "video/x-msvideo":
                                case "video/avi":
                                case "video/3gpp":
                                case "audio/x-aiff":
                                case "audio/x-dsf":
                                case "audio/x-wav":
                                case "audio/x-wavpack":
                                case "audio/ogg":
                                case "audio/ac3":
                                case "audio/vnd.dts.hd":
                                case "audio/x-mpegurl":
                                case "text/x-microdvd":
                                case "text/x-matlab": 
                                case "text/x-nfo":
                                case "text/csv":
                                case "text/plain":
                                case "multipart/x-zip":
                                case "application/vnd.oasis.opendocument.spreadsheet":
                                case "application/msword":
                                case "application/vnd.comicbook-rar":								
                                case "application/x-mobipocket-ebook":
                                case "application/x-fictionbook+xml":
                                case "application/x-cbr":
                                case "application/x-aportisdoc":
                                case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
                                case "application/x-shellscript":
                                case "application/rtf":
                                case "application/json":
                                case "application/vnd.sqlite3":
                                case "application/x-bittorrent":
                                case "application/epub+zip":
                                case "application/x-ms-dos-executable":
                                case "application/x-php":
                                case "application/x-tar":
                                case "application/x-rar":
                                case "application/rar":
                                case "application/x-rar-compressed":
                                case "application/x-tgsticker":
                                case "application/x-subrip":
                                case "application/pdf":
                                case "application/vnd.android.package-archive":
                                case "application/octet-stream":
                                case "application/zip":
                                case "application/x-7z-compressed":
                                case "application/x-zip-compressed":
                                case "application/vnd.rar":      
                                                                 $messagereturn["size"]=     $messagedata["media"]["document"]["size"];
                                                                 $messagereturn["filedate"]= $messagedata["media"]["document"]["date"];
                                                                 $messagereturn["mime_type"]=$messagedata["media"]["document"]["mime_type"];

                                                                 $messagereturn["duration"]= $attributesdata["duration"];
                                                                 $messagereturn["width"]=    $attributesdata["width"];
                                                                 $messagereturn["height"]=   $attributesdata["height"];
                                                                 $messagereturn["filename"]= $attributesdata["filename"];

                                                                 if ($messagereturn["filename"] == -1) $messagereturn["filename"]=$messagereturn["mime_type"].".nofile";
	                                                                if ($messagereturn["cleanmessage"] == -1) $messagereturn["cleanmessage"]=removeChannelAds(cleanupString($messagereturn["filename"]));
                                break;
                                case "audio/mp3":
                                case "audio/mpeg3":
                                case "audio/m4a":
                                case "audio/x-flac":
                                case "audio/flac":
                                case "audio/FLAC":
                                case "audio/mp4":
                                case "audio/mpeg":               
                                                                 $messagereturn["size"]=     $messagedata["media"]["document"]["size"];
                                                                 $messagereturn["filedate"]= $messagedata["media"]["document"]["date"];
                                                                 $messagereturn["mime_type"]=$messagedata["media"]["document"]["mime_type"];

                                                                 $messagereturn["duration"]= $attributesdata["duration"];
                                                                 $messagereturn["width"]=    $attributesdata["width"];
                                                                 $messagereturn["height"]=   $attributesdata["height"];
                                                                 $messagereturn["filename"]= $attributesdata["filename"];

                                                                 if ($messagereturn["filename"] == -1) $messagereturn["filename"]=$messagereturn["mime_type"].".nofile";

                                                                 if (array_key_exists("performer", $messagedata["media"]["document"]["attributes"]["0"])) { $messagereturn["message"]="performer: ".$messagedata["media"]["document"]["attributes"]["0"]["performer"];} else $messagereturn["message"]="performer: <unknown>";

                                                                 if (array_key_exists("title", $messagedata["media"]["document"]["attributes"]["0"])) { $messagereturn["cleanmessage"]="title: ".$messagedata["media"]["document"]["attributes"]["0"]["title"];} else $messagereturn["cleanmessage"]="title: ".removeChannelAds(cleanupString($messagereturn["filename"]));
                                break;
                                default: 
                                                                 $messagereturn["size"]=$messagedata["media"]["document"]["size"];
                                                                 $messagereturn["filedate"]=$messagedata["media"]["document"]["date"];
                                                                 $messagereturn["mime_type"]="unknown: ".$messagedata["media"]["document"]["mime_type"];

                                                                 $messagereturn["duration"]=$attributesdata["duration"];
                                                                 $messagereturn["width"]=$attributesdata["width"];
                                                                 $messagereturn["height"]=$attributesdata["height"];
                                                                 $messagereturn["filename"]=$attributesdata["filename"];

                                                                 if ($messagereturn["filename"] == -1) $messagereturn["filename"]=$messagereturn["mime_type"].".nofile";
	                                                             if ($messagereturn["cleanmessage"] == -1) $messagereturn["cleanmessage"]=removeChannelAds(cleanupString($messagereturn["filename"]));
                               } // switch ($messagedata["media"]["document"]["mime_type"]) 
  break;
  default:                    
                               $messagereturn["type"]="unknown: ".$messagetype;
                               $messagereturn["mime_type"]=-1;
                               $messagereturn["duration"]=-1;
                               $messagereturn["width"]=-1;
                               $messagereturn["height"]=-1;
                               $messagereturn["size"]=-1;
                               $messagereturn["filedate"]=-1;
                               $messagereturn["filename"]=-1;
 } // switch ($messagetype)
 return $messagereturn;
} // function end

function getMessageType($messagedata) {
 if (!array_key_exists("media", $messagedata)) return "textMessage";
 if (array_key_exists("media", $messagedata)) return $messagedata["media"]["_"];
} // function end

function getUserData($userdata, $data) {
 $userreturn=array();
 foreach (array_keys($data) as $k) {
  if (is_array($data[$k])) { 
   foreach(array_keys($data[$k]) as $d) {
    if ($data[$k][$d]["_"]=="user") {
      if (array_key_exists("from_id", $userdata) && array_key_exists("_", array($userdata["from_id"]))) {
	  print_r($data[$k]);
	  echo "----------------------------------------------\n";
	  print_r($userdata);
      switch ($userdata["from_id"]["_"]) {
       case "peerChannel":
        $userreturn["user_id"]=-1;
        $userreturn["username"]="peerChannel";
        $userreturn["first_name"]="peerChannel: ".$userdata["from_id"]["channel_id"];
        $userreturn["last_name"]="peerChannel: ".$userdata["from_id"]["channel_id"];
        $userreturn["bot"]=-1;
       break;
       case "peerUser":
        if ($userdata["from_id"]["user_id"] == $data[$k][$d]["id"]) { 
        $userreturn["user_id"]=$userdata["from_id"]["user_id"];
        if (array_key_exists("username",$data[$k][$d])) { $userreturn["username"]=$data[$k][$d]["username"]; } else $userreturn["username"]=-1;
        if (array_key_exists("first_name",$data[$k][$d])) { $userreturn["first_name"]=$data[$k][$d]["first_name"]; } else $userreturn["first_name"]=-1;
        if (array_key_exists("last_name",$data[$k][$d])) { $userreturn["last_name"]=$data[$k][$d]["last_name"]; } else $userreturn["last_name"]=-1;
        if (array_key_exists("bot",$data[$k][$d])) if ($data[$k][$d]["bot"]==1) { $userreturn["bot"]=$data[$k][$d]["bot"]; } else $userreturn["bot"]=-1;
        } // if ($userdata["from_id"]["user_id"] == $data[$k][$d]["id"])
        break;
       } // switch ($userdata["from_id"]["_"])
      } else { 
       $userreturn["user_id"]=$data[$k][0]["id"];
       if (array_key_exists("username",$data[$k][0])) { $userreturn["username"]=$data[$k][0]["username"]; } else $userreturn["username"]=-1;
       if (array_key_exists("first_name",$data[$k][0])) { $userreturn["first_name"]=$data[$k][0]["first_name"]; } else $userreturn["first_name"]=-1;
       if (array_key_exists("last_name",$data[$k][0])) { $userreturn["last_name"]=$data[$k][0]["last_name"]; } else $userreturn["last_name"]=-1;
       if (array_key_exists("bot",$data[$k][0])) if ($data[$k][0]["bot"]==1) { $userreturn["bot"]=$data[$k][0]["bot"]; } else $userreturn["bot"]=-1;
      } // if (array_key_exists("from_id", $userdata)) - else
    } // if ($data[$k][$d]["_"]=="users")
   } // foreach(array_keys($data[$k]) as $d)
  } // if (is_array($data[$k]))
 } // foreach (array_keys($data) as $k) 
 return $userreturn; 
} // function end
 
function getChannelData($peerid, $data) {
 $channelreturn=array();
 foreach (array_keys($data) as $k) {
  if (is_array($data[$k])) { 
   foreach(array_keys($data[$k]) as $d) {
    if ($data[$k][$d]["_"]=="channel") {
     if ($data[$k][$d]["id"]==$peerid) {
      $channelreturn["chanid"]=substr($data[$k][$d]["id"],4);
      if ($data[$k][$d]["title"] != "") { $channelreturn["title"]=$data[$k][$d]["title"]; } else $channelreturn["title"]=-1;
      if ($data[$k][$d]["title"] != "") { $channelreturn["cleantitle"]=removeChannelAds(cleanupString($data[$k][$d]["title"])); } else $channelreturn["cleantitle"]=-1;
     } // if ($data[$k][$d]["id"]==$chanid) 
    } // if ($data[$k][$d]["_"]=="channel")
   } // foreach(array_keys($data[$k]) as $d)
  } // if (is_array($data[$k]))
 } // foreach (array_keys($data) as $k) 
 return $channelreturn;
} // function end

function getTopicData($topic_id, $data) { 
 $topicreturn["topic_id"]=-1;
 $topicreturn["topicname"]=-1;
 $topicreturn["cleantopicname"]=-1;
 if ($topic_id == -1) return $topicreturn;
 foreach (array_keys($data) as $k) {
  if (is_array($data[$k])) { 
   foreach(array_keys($data[$k]) as $d) {
    if ($data[$k][$d]["_"]=="forumTopic") {
     if ($data[$k][$d]["id"]==$topic_id ) {
      $topicreturn["topic_id"]=$data[$k][$d]["id"];
      if ($data[$k][$d]["title"] != "") { $topicreturn["topicname"]=$data[$k][$d]["title"]; } else $topicreturn["topicname"]=-1;
      if ($data[$k][$d]["title"] != "") { $topicreturn["cleantopicname"]=removeChannelAds(cleanupString($data[$k][$d]["title"])); } else $topicreturn["cleantopicname"]=-1;
     } // if ($data[$k][$d]["id"]==$chanid) 
    } // if ($data[$k][$d]["_"]=="channel")
   } // foreach(array_keys($data[$k]) as $d)
  } // if (is_array($data[$k]))
 } // foreach (array_keys($data) as $k) 
 return $topicreturn;
} // function end


function parseData($data) { 
 global $globalsettings, $channelInfoNew;
 $messages=array();
 foreach (array_keys($data) as $k) {
  if (is_array($data[$k])) { 
   foreach(array_keys($data[$k]) as $d) {
    if ($data[$k][$d]["_"]=="message") { 
     $topic_id=-1;
     if (array_key_exists("reply_to",$data[$k][$d])) if (array_key_exists("reply_to_msg_id",$data[$k][$d]["reply_to"])) {
      $topic_id=$data[$k][$d]["reply_to"]["reply_to_msg_id"]; 
      if (array_key_exists("reply_to_top_id",$data[$k][$d]["reply_to"])) $topic_id=$data[$k][$d]["reply_to"]["reply_to_top_id"]; 
     } // if (array_key_exists("reply_to",$data[$k][$d])) if (array_key_exists("reply_to_msg_id",$data[$k][$d]["reply_to"])) 

//   $messages[]=array_merge(getMessageData($data[$k][$d], getMessageType($data[$k][$d])), getUserData($data[$k][$d], $data),getChannelData($data[$k][$d]["peer_id"]["channel_id"], $data), getTopicData($topic_id, $data));

     try {
	  $globalsettings["message"]["savelastmessagexml"]=$data[$k][$d]; // save last message xml 	 
      $messages[]=array_merge(getMessageData($data[$k][$d], getMessageType($data[$k][$d])), getUserData($data[$k][$d], $data),getChannelData($data[$k][$d]["peer_id"], $data), getTopicData($topic_id, $data));
     } catch (Exception $e) {
    
      logger("pandabot-exception: parseData[".$channelInfoNew[substr($globalsettings["message"]["savelastmessagexml"]["peer_id"], 4)]["name"]."]: "."https://t.me/c/".substr($globalsettings["message"]["savelastmessagexml"]["peer_id"], 4)."/".$globalsettings["message"]["savelastmessagexml"]["id"]." ".$e->getMessage());
//	  print_r($globalsettings["message"]["savelastmessagexml"]); 
	  echo $e->getTraceAsString();
	  
	 //  exit(); // ignore exception 
	 } // try -> catch (Exception $e) 
    } // if ($data[$k][$d]["_"]=="message")
   } // foreach(array_keys($data[$k]) as $d)
  } // if (is_array($data[$k]))
 } // foreach (array_keys($data) as $k) 
 asort($messages);
 return $messages;
} // function end


function insertData($data, $channelInfoKey, $donotsync=-1, $locktable=-1) {
 global $globalsettings, $channelInfoNew;

 if ($locktable == -1) { $tablename="chan_".$channelInfoNew[$channelInfoKey]["chanid"]; } else $tablename="lock_".$channelInfoNew[$channelInfoKey]["chanid"];

 if (count($data) == 0) return;

 pg_query($globalsettings["db"]["pg_conn"], pg_channelTableTemplate($tablename)); // create chan_ table if not exists
 pg_createIndexes($tablename); // create indexes 
 
 $sql="select max(msgid) as maxid from ".$tablename;
 $result=pg_query($globalsettings["db"]["pg_conn"], $sql);
 while ($row = pg_fetch_array($result)) $maxid=$row['maxid'];
 if (!isset($maxid)) $maxid=1; // table empty
 
 $sql="insert into ".$tablename." 
 (
  msgid,
  link,
  grouped_id, 
  type, 
  message, 
  cleanmessage, 
  reply_to, 
  msgdate, 
  mime_type, 
  duration, 
  width, 
  height, 
  size,
  filedate,
  filename,
  user_id,
  username,
  first_name,
  last_name,
  bot,
  chanid,
  title,
  cleantitle,
  topic_id,
  topicname,
  cleantopicname
 ) values ";

 $insertinto="";
 foreach ($data as $d) {
  $insert="(";
  foreach (array_keys($d) as $k) {
   if ($k=="link" || $k=="type" || $k=="message" || $k=="cleanmessage" || $k=="mime_type" || $k=="filename" || $k=="username" || $k=="first_name" || $k=="last_name" || $k=="title" || $k=="cleantitle" || $k=="topicname" || $k=="cleantopicname" ) $d[$k]="'".pg_escape_string($globalsettings["db"]["pg_conn"], $d[$k])."'";

   $insert.=$d[$k].",";
  } // foreach (array_keys($d) as $k)

//  $insertinto=$sql.substr($insert, 0, strlen($insert)-1).") on conflict (msgid) do nothing;"; // single row insert
//  echo $insertinto."\n\n"; // single row debug
//  pg_query($globalsettings["db"]["pg_conn"], $insertinto); // single row insert

  $sql.=substr($insert, 0, strlen($insert)-1)."),"; // bulk insert
 } // foreach ($data as $d)
  $sql=substr($sql, 0, strlen($sql)-1)." on conflict (msgid) do nothing;"; // bulk insert
  pg_query($globalsettings["db"]["pg_conn"], $sql); // bulk insert


 if (is_array(forwardInfo($channelInfoKey, $channelInfoNew)) && $donotsync == -1) {
 // logger("insertData: \"to:\" channel(s) configured, call syncFiles(\$maxid=".$maxid.", \$channelInfoKey=".$channelInfoKey.");");
  syncFiles($maxid, $channelInfoKey); 
 } // else logger("insertData: \"to:\" channel(s) not configured, do not call syncFiles(\$maxid=".$maxid.", \$channelInfoKey=".$channelInfoKey.");");

 return;
} // function end

function deleteData($tablename) {
 global $globalsettings;
 $sql="delete from ".$tablename;
 pg_query($globalsettings["db"]["pg_conn"], $sql);
} // function end

function dropTable($tablename) {
 global $globalsettings;
 $sql="drop table ".$tablename;
 pg_query($globalsettings["db"]["pg_conn"], $sql);
} // function end

// these inc contains:
//
// parseData()
// getMessageData()
// getMessageType()
// getUserData()
// getChannelData()
// getMessageMediaAttributes()
// getTopicData()
// dropTable($tablename)
// insertData()
// deleteData()
// removeChannelAds()
// cleanupString()


