<?php

/*

also die queue wird nur in der db gehalten und nicht im speicher... 

das passiert, wenn man ein source hat, autosync an ist und zwei targets configuriert hat...

** betrifft aller wahrscheinlichkeit nur target2 


cache table für doppelte weiterleitungsversuche + wenn detection++ löschen der ursprünglichen einträge im source channel 

*/ 

function buildtoForwardKey($sourceChannelInfoKey, $target, $msgid, $to_topic) {
 // calls @ fillForwardArray(), syncFiles(), fwdStepperJob() 
 $key=$sourceChannelInfoKey.";".$target.";".$msgid.";".$to_topic;
 return $key;
} // function end

function fillForwardArray($ia, $fwd) {
 global $toForward, $channelInfoNew, $globalsettings; 
// print_r($fwd);
 foreach ($fwd as $k) {
  $tochanname=$k["name"]; // getfile_hack
  $f=$k["chanid"];
  $to_topic=$k["to_topic"];
  $channelInfoKey=$k["channelInfoKey"];

  $arraykey=buildtoForwardKey($channelInfoKey, $f, $ia["msgid"], $to_topic);
  
 /*
  // move to array element 1
 $globalsettings["bot"]["lastbotcommands"]["commands"]=array($commandskey => $globalsettings["bot"]["lastbotcommands"]["commands"][$commandskey]) + $globalsettings["bot"]["lastbotcommands"]["commands"];
 */

  $toForward[$arraykey]["from"]=$channelInfoKey; 
  $toForward[$arraykey]["to"]=$f;
  $toForward[$arraykey]["msgid"]=$ia["msgid"];
  $toForward[$arraykey]["album"]=$ia["album"];
  $toForward[$arraykey]["topic"]=$to_topic;
  $toForward[$arraykey]["text"]=$ia["text"];
  $toForward[$arraykey]["cover"]=$ia["cover"];
  $toForward[$arraykey]["width"]=$ia["width"];
  $toForward[$arraykey]["height"]=$ia["height"];
  $toForward[$arraykey]["runtime"]=$ia["runtime"];
  $toForward[$arraykey]["filedate"]=$ia["filedate"];
  $toForward[$arraykey]["filesize"]=$ia["filesize"];
  $toForward[$arraykey]["tochanname"]=$tochanname;

  $sql="insert into forwardqueue (
        forwardkey, 
        sourceid,       
        targetid,         
        msgid,      
        album,      
        to_topic,   
        message,       
        cover,      
        width,     
        heigth,     
        runtime,    
        filedate,   
        filesize,
		tochanname
       ) values (   
        '".pg_escape_string($globalsettings["db"]["pg_conn"], $arraykey)."', 
        '".pg_escape_string($globalsettings["db"]["pg_conn"], $channelInfoKey)."', 
         ".$f.", 
         ".$ia["msgid"].", 
         ".$ia["album"].", 
         ".$to_topic.", 
        '".pg_escape_string($globalsettings["db"]["pg_conn"], $ia["text"])."', 
        '".pg_escape_string($globalsettings["db"]["pg_conn"], $ia["cover"])."', 
         ".$ia["width"].", 
         ".$ia["height"].", 
         ".$ia["runtime"].", 
         ".$ia["filedate"].", 
         ".$ia["filesize"].",
		'".pg_escape_string($globalsettings["db"]["pg_conn"], $tochanname)."'
        )";

  pg_query($globalsettings["db"]["pg_conn"], $sql) or die('somthing wrong: ' . pg_last_error());

 } // foreach ($fwd as $f) 
} // function end

function syncChecktargetChannel($width, $height, $runtime, $filedate, $filesize, $syncTo) {
 global $globalsettings;
 $sql="set local enable_seqscan = off; 
       select msgid from chan_".$syncTo." where filedate=".$filedate." and size=".$filesize." and duration=".$runtime." and width=".$width." and height=".$height.";";
 
 $result=pg_query($globalsettings["db"]["pg_conn"], $sql);
 $numrows=pg_num_rows($result);

 if ($numrows == 0) return -1;
 return $numrows;
} // function end

function syncMsgBefor($msgid, $grpid) {
 global $globalsettings;

/* catch cover alben 
select type from chan_".$grpid." t1 where 
 (
  msgid=".$msgbefor." and type='messageMediaPhoto' and t1.grouped_id=-1 
 ) or (
  grouped_id!=-1 and
  (
   select true from chan_".$grpid." t2 where t1.grouped_id=t2.grouped_id and type='messageMediaPhoto'
  ) and 
  user_id in (select user_id from chan_".$grpid." where msgid=".$msgid.")";  
*/


 $msgbefor=$msgid-1;
 $sql="select type from chan_".$grpid." where msgid=".$msgbefor." and type='messageMediaPhoto' and grouped_id=-1 and user_id in (select user_id from chan_".$grpid." where msgid=".$msgid.")";
 $result=pg_query($globalsettings["db"]["pg_conn"], $sql);
 $numrows=pg_num_rows($result);

 if ($numrows !=0 ) { return $msgbefor; } else return false;
} // function end


function syncGaterAlbumPosts($grpid, $msgid, $grouped_id) {
 global $globalsettings;

 $sql="select msgid, type, substr(cleanmessage, 0, ".$globalsettings["message"]["cuttext"].") as cleanmessage from chan_".$grpid." where grouped_id=".$grouped_id." order by msgid asc";
 $result=pg_query($globalsettings["db"]["pg_conn"], $sql);
 $numrows=pg_num_rows($result);

 $return=array(); // new return value

 $c=1;
 while ($row = pg_fetch_array($result))  {
  if ($c==1) { $firstmsgid=$row['msgid']; $firsttext=$row["cleanmessage"]; $c++; }
  switch ($row['type']) {
   case "messageMediaPhoto": 
    $noCheckMessageBefor=true; 
    $return[$row["msgid"]]["msgid"]=$row["msgid"];
    $return[$row["msgid"]]["cover"]="cover";
    $return[$row["msgid"]]["text"]=$row["cleanmessage"];
   break;
   default: 
    $return[$row["msgid"]]["msgid"]=$row["msgid"];
    $return[$row["msgid"]]["cover"]="-1";
    $return[$row["msgid"]]["text"]=$row["cleanmessage"];
  } // switch ($row['type'])
 } // while ($row = pg_fetch_array($result))  

 if (!isset($noCheckMessageBefor)) {
  $msgbefor=syncMsgBefor($firstmsgid, $grpid);
  if ($msgbefor!="") {
   $return[$msgbefor]["msgid"]=$msgbefor;
   $return[$msgbefor]["cover"]="cover";
   $return[$msgbefor]["text"]=$firsttext; // will be improved...
   asort($return); 
  } // if ($msgbefor!="")
 } // if (!isset($noCheckMessageBefor))

 return $return;
} // function end

function checkFileOverAllChannels($text, $width, $height, $runtime, $filedate, $filesize, $from, $topic_id, $channelInfoKey, $msgid, $getfile_topic=false ) { 
 global $globalsettings, $channelInfoNew, $toForward;

 $fwd=array();
 $filecount=0; 
 
 if ($getfile_topic!=false) {
// echo "here ---->\n";
/*
 $forwardInfo[$chanid]["from_topic_id"][$channelInfoNew[$k]["from_topic_id"]][$channelInfoNew[$k]["to"][$t]["chanid"]]=$channelInfoNew[$k]["to"][$t]["to_topic"];	 
 from_topic_id always -1
 to_topic==getfile_topic
*/

  $forwardInfo=array();
  if ($channelInfoNew[$channelInfoKey]["redirectout_tochannel"] == -1) {  
   $forwardInfo[$channelInfoNew[$channelInfoKey]["redirectout"]]["from_topic_id"][-1][$channelInfoNew[$channelInfoKey]["chanid"]]=$getfile_topic;
  } else { 
   $forwardInfo[$channelInfoNew[$channelInfoKey]["redirectout"]]["from_topic_id"][-1][$channelInfoNew[$channelInfoKey]["redirectout_tochannel"]]=$getfile_topic;
  } // if ($channelInfoNew[$channelInfoKey]["redirectout_tochannel"] == -1) 
 } else $forwardInfo=forwardInfo($channelInfoKey, $channelInfoNew); // if ($getfile_topic!=false1)



// print_r($forwardInfo);


 if (is_array($forwardInfo)) {
  foreach (array_keys($forwardInfo) as $k) { // in $k = chanid

   foreach (array_keys($forwardInfo[$k]["from_topic_id"]) as $from_topic_id) {

     if ($from_topic_id != -1 ) { $channelInfoKey=$k."-".$from_topic_id; } else $channelInfoKey=$k; // rebuild channelInfoKey from forwardInfo
     foreach (array_keys($forwardInfo[$k]["from_topic_id"][$from_topic_id]) as $to_chan) {

       if ($topic_id == $from_topic_id || $from_topic_id == -1) { // check if target to from_topic_id configured -- here ! to topic! && to_topic id (forwardinfo aufbohren?)

       if ($getfile_topic!=false) { 
	    $toChannel=$channelInfoNew[$to_chan]["name"]; 
	   } else $toChannel=$channelInfoNew[$channelInfoKey]["to"][$to_chan]["name"];
	   
       if (mb_strlen($toChannel) >= 40) $toChannel=mb_substr($toChannel,0,20)."...";

	   if ($getfile_topic!=false) { 
	    $toChannelTopic=$getfile_topic; 
	   } else $toChannelTopic=$channelInfoNew[$channelInfoKey]["to"][$to_chan]["to_topic"];

       $fromChannel=$channelInfoNew[$channelInfoKey]["name"];
       if (mb_strlen($fromChannel) >= 40) $fromChannel=mb_substr($fromChannel,0,20)."...";

       logger(mb_sprintf("syncFiles: %-54s check - from: [%s(%s):%s] to: [%s:%s]",$text, $fromChannel, $msgid, $topic_id, $toChannel, $toChannelTopic));

       unset($queueCheck); // check toForward Queue
       if (count($toForward)!=0) {
        foreach ($toForward as $fc) { 

        if ($width==$fc["width"] && $height==$fc["height"] && $runtime==$fc["runtime"] && $filedate==$fc["filedate"] && $filesize==$fc["filesize"] && $channelInfoNew[$channelInfoKey]["to"][$to_chan]["chanid"]==$fc["to"] && $channelInfoNew[$channelInfoKey]["to"][$to_chan]["to_topic"] == $fc["topic"]) {
         $queueCheck=true;
         logger(mb_sprintf("syncFiles: %-54s is in queue for target: [%s]", $text, $toChannel));
         break;
        } // if ($width==$fc["width"] && $height==$fc["height"] && $runtime==$fc["runtime"] && $filedate==$fc["filedate"] && $filesize==$fc["filesize"] && $f==$fc["to"]) 
       } // foreach ($toForward as $fc) 
      } // if (count($toForward)!=0)



      if (!isset($queueCheck)) {
       logger(mb_sprintf("syncFiles: %-54s is not in queue for target: [%s]", $text, $toChannel));

       $filetargetcheck=syncChecktargetChannel($width, $height, $runtime, $filedate, $filesize, $to_chan);
       if ($filetargetcheck==-1) {

        $filecount++; 
        
        if ($getfile_topic!=false) {
         $fwd[$k.":".$filecount]["chanid"]=        $channelInfoNew[$to_chan]["chanid"];
         $fwd[$k.":".$filecount]["peerid"]=        $channelInfoNew[$to_chan]["peerid"];
         $fwd[$k.":".$filecount]["status"]=        $channelInfoNew[$to_chan]["status"];   
         $fwd[$k.":".$filecount]["info"]=          $channelInfoNew[$to_chan]["info"];
         $fwd[$k.":".$filecount]["to_topic"]=      $getfile_topic;
         $fwd[$k.":".$filecount]["topic_name"]=    "getfile hack";
         $fwd[$k.":".$filecount]["name"]=          $channelInfoNew[$to_chan]["name"];
         $fwd[$k.":".$filecount]["channelInfoKey"]=$channelInfoKey;
        } else {
         $fwd[$k.":".$filecount]["chanid"]=        $channelInfoNew[$channelInfoKey]["to"][$to_chan]["chanid"];
         $fwd[$k.":".$filecount]["peerid"]=        $channelInfoNew[$channelInfoKey]["to"][$to_chan]["peerid"];
         $fwd[$k.":".$filecount]["status"]=        $channelInfoNew[$channelInfoKey]["to"][$to_chan]["status"];   
         $fwd[$k.":".$filecount]["info"]=          $channelInfoNew[$channelInfoKey]["to"][$to_chan]["info"];
         $fwd[$k.":".$filecount]["to_topic"]=      $channelInfoNew[$channelInfoKey]["to"][$to_chan]["to_topic"];
         $fwd[$k.":".$filecount]["topic_name"]=    $channelInfoNew[$channelInfoKey]["to"][$to_chan]["topic_name"];
         $fwd[$k.":".$filecount]["name"]=          $channelInfoNew[$channelInfoKey]["to"][$to_chan]["name"];
         $fwd[$k.":".$filecount]["channelInfoKey"]=$channelInfoKey;
		} // if ($getfile_topic!=false) 
		
//print_r($fwd);
			
        logger(mb_sprintf("syncFiles: %-54s is not in database for target: [%s]", $text, $toChannel));
       } else logger(mb_sprintf("syncFiles: %-54s is [%sx] in database for target: [%s]", $text, $filetargetcheck, $toChannel)); // if ($filetargetcheck==-1) - else
      } // if (!isset($queueCheck) 
     } // if ($topic_id == $from_topic_id || $from_topic_id == -1)
    } // foreach (array_keys($forwardInfo[$k]["from_topic_id"][$from_topic_id]) as $to_chan)
   } // foreach (array_keys($forwardInfo[$k]["from_topic_id"]) as $from_topic_id) 
  } // foreach (array_keys($forwardInfo) as $k) 
 } // if (is_array($forwardInfo)) 

 if (count($fwd) != 0) {
  return $fwd;
 }

 return -1;
} // function end


function typeclausel($type) {
 global $typemapping;
 
 if (!array_key_exists($type, $typemapping)) $type="video"; // set default when $§%§$@ configured

 if ($type == "all" ) { 
  $sql="mime_type like '%' ";
  return $sql;
 }

 foreach (array_keys($typemapping) as $k) {
  if ($k == $type) {
   if (array_key_exists("min-duration", $typemapping[$k])) { $duration=" and duration > ".$typemapping[$k]["min-duration"]*60; }
   if (array_key_exists("max-duration", $typemapping[$k])) { $duration=" and duration < ".$typemapping[$k]["max-duration"]*60; }
   if (!isset($duration)) $duration=" ";
   
   if (array_key_exists("min-size", $typemapping[$k])) {$size=" and size > ".$typemapping[$k]["min-size"]*1024*1024; } else $size=" ";
   if (array_key_exists("min-width", $typemapping[$k])) {$width=" and width > ".$typemapping[$k]["min-width"]; } else $width=" ";

   $sql="(";
   foreach($typemapping[$k]["types"] as $mimetypes) {
    $sql.="mime_type = '".$mimetypes."' or ";
   } // foreach($typemapping[$k]["types"] as $types) 
   $sql=substr($sql, 0, strlen($sql)-4).")".$duration.$size.$width;
  } // if ($k == $type) 
 } //  foreach (array_keys($typemapping) as $k) 

//  echo $sql."\n";
 return $sql;
} // function end

function syncFiles($maxid, $channelInfoKey, $autosync=-1) {
 global $globalsettings, $videoRunTimeInMinute, $MadelineProto, $lastMessagePerChan, $forwardMsg, $toForward, $channelInfoNew;

 $peerid=$channelInfoNew[$channelInfoKey]["peerid"];
 $grpid=$channelInfoNew[$channelInfoKey]["chanid"];

 if (array_key_exists($peerid,$forwardMsg)) if ($forwardMsg[$peerid]!=-1) $maxid=$forwardMsg[$peerid]; // overwrite maxid if forwardMsg set

 $sql="select msgid, message, substr(cleanmessage, 0, ".$globalsettings["message"]["cuttext"].") as cleanmessage, duration, grouped_id, filedate, size, width, height, topic_id from chan_".$grpid." where ".typeclausel($channelInfoNew[$channelInfoKey]["typemapping"])." and msgid > ".$maxid." order by msgid asc";


 if ($autosync != -1) { $result=$autosync; } else $result=pg_query($globalsettings["db"]["pg_conn"], $sql);

 $numrows=pg_num_rows($result);

 $fromChannel=$channelInfoNew[$channelInfoKey]['name'];
 if (mb_strlen($fromChannel) >= 20) $fromChannel=mb_substr($fromChannel,0,20)."...";
 if ($numrows != 0) logger("syncFiles: ".$numrows." files - from [".$fromChannel."]");

 if ($numrows == 0) return;

 unset($forward); // cleanup forward array
 unset($forwardBreakOut); 
 $forward=array();
 while ($row = pg_fetch_array($result)) {
  
   $text=$row['cleanmessage'];
   $orgtext=$row['message'];
   $filedate=$row['filedate'];
   $filesize=$row['size'];
   $height=$row['height'];
   $width=$row['width'];
   $runtime=$row['duration'];
   $grouped_id=$row['grouped_id'];
   $msgid=$row['msgid'];
   $topic_id=$row['topic_id'];
   
   $filetargetcheck=checkFileOverAllChannels($text, $width, $height, $runtime, $filedate, $filesize, $grpid, $topic_id, $channelInfoKey, $msgid); 
   if (is_array($filetargetcheck)) { $fwd=$filetargetcheck; } else logger(sprintf("syncFiles: do not forward from [%s][%s] - %-54s",$fromChannel, $row['msgid'], $row['cleanmessage']));    // overwrite $fwd 


   if (is_array($filetargetcheck)) { 

    if ($grouped_id==-1) { // no grouped / album posts check message befor 

     $forward[]=$msgid;
     $infoArray=array("msgid" => $msgid, "album" => -1, "text" => $text, "cover" => -1, "width" => $width, "height" => $height, "runtime" => $runtime, "filedate" => $filedate, "filesize" => $filesize);

     unset($msgbefor);
     $msgbefor=syncMsgBefor($msgid, $grpid);
     if ($msgbefor!="") { 
      $forward[]=$msgbefor;
      $infoArray["cover"]=$msgbefor;
     }
      fillForwardArray($infoArray, $fwd); // array included video and cover info's 
    } else {
     if (!array_search($msgid, $forward)) {
      $albenreturn=syncGaterAlbumPosts($grpid, $msgid, $grouped_id); 

      foreach(array_keys($albenreturn) as $k) {
       $forward[]=$albenreturn[$k]["msgid"]; 
       $infoArray=array("msgid" => $albenreturn[$k]["msgid"], "album" => $grouped_id, "text" => $albenreturn[$k]["text"], "cover" => $albenreturn[$k]["cover"] , "width" => $width, "height" => $height, "runtime" => $runtime, "filedate" => $filedate, "filesize" => $filesize); 
       fillForwardArray($infoArray, $fwd); 
      } // foreach(array_keys($albenreturn) as $k) 
     } // if (!array_search($msgid, $forward))
    } // if ($grouped_id==-1) - else
   } // if (is_array($filetargetcheck)) 

// stopafter > 20 forwards set channel last message back to last forward msg
  if (count($forward)>=80 && $autosync == -1) {
   logger("syncFiles: set lastmessage ".$lastMessagePerChan[$peerid].": ".$forward[count($forward)-1]);

   if (!array_key_exists($peerid,$forwardMsg)) { $forwardMsg+=array($peerid => $forward[count($forward)-1]); } else $forwardMsg[$peerid]=$forward[count($forward)-1];

   $lastMessagePerChan[$peerid]=$forward[count($forward)-1];
   $forwardBreakOut=1;
   break;
  }
 } // while ($row = $result->fetchArray())

 if (!isset($forwardBreakOut)) { // reset mass forward array $forwardMsg;
  if (!array_key_exists($peerid,$forwardMsg)) { $forwardMsg+=array($peerid => -1); } else $forwardMsg[$peerid]=-1;
 } // reset forward array 

 return;
} // function end

function redirect_getfile($channelInfoKey, $getfile_msgid, $topicid, $infoarray) {
 global $channelInfoNew, $globalsettings, $requestcommand;	
 if ($channelInfoNew[$channelInfoKey]["redirectout"] != -1) {

  

  $sql="select msgid, message, substr(cleanmessage, 0, ".$globalsettings["message"]["cuttext"].") as cleanmessage, duration, grouped_id, filedate, size, width, height, topic_id from chan_".$channelInfoNew[$channelInfoKey]["redirectout"]." where msgid = ".$getfile_msgid;	 
  $result=pg_query($globalsettings["db"]["pg_conn"], $sql);
//echo $sql."\n"; 
  
  $fromChannel=$channelInfoNew[$channelInfoNew[$channelInfoKey]["redirectout"]]['name'];
  if (mb_strlen($fromChannel) >= 20) $fromChannel=mb_substr($fromChannel,0,20)."...";

  if (pg_num_rows($result)==0) {
   sendmessageintochannel($channelInfoKey, "❌<b>".$requestcommand." #getfile</b>: message id not found: ".$getfile_msgid."<br>".generatebotmarker(), $topicid);
   return;	 
  } else logger("redirect_getfile: ".pg_num_rows($result)." files - from [".$fromChannel."]"); // if (pg_num_rows($result)==0) 
  
  $forward=array();
  while ($row = pg_fetch_array($result)) {  
   $text=$row['cleanmessage'];
   $orgtext=$row['message'];
   $filedate=$row['filedate'];
   $filesize=$row['size'];
   $height=$row['height'];
   $width=$row['width'];
   $runtime=$row['duration'];
   $grouped_id=$row['grouped_id'];
   $msgid=$row['msgid'];
   $topic_id=$row['topic_id'];
   
   

   $filetargetcheck=checkFileOverAllChannels($text, $width, $height, $runtime, $filedate, $filesize, $channelInfoNew[$channelInfoKey]["redirectout"], $topic_id, $channelInfoKey, $msgid, $topicid);

   if (is_array($filetargetcheck)) { 
    $fwd=$filetargetcheck; 
   } else { 
   
    if ($channelInfoNew[$channelInfoKey]["redirectout_tochannel"] == -1) {
     $dbchecksql="set local enable_seqscan = off; 
       select msgid, link from chan_".$channelInfoNew[$channelInfoKey]["chanid"]." where filedate=".$filedate." and size=".$filesize." and duration=".$runtime." and width=".$width." and height=".$height." limit 1;";
	} else {
     $dbchecksql="set local enable_seqscan = off; 
       select msgid, link from chan_".$channelInfoNew[$channelInfoKey]["redirectout_tochannel"]." where filedate=".$filedate." and size=".$filesize." and duration=".$runtime." and width=".$width." and height=".$height." limit 1;";
    } // if ($channelInfoNew[$channelInfoKey]["redirectout_tochannel"] == -1) 
	   
	 $dbcheckresult=pg_query($globalsettings["db"]["pg_conn"], $dbchecksql);
	 
//	echo $dbchecksql."\n";  

    if (pg_num_rows($dbcheckresult)!=0) {
	 while ($dbcheckrow = pg_fetch_array($dbcheckresult)) {  
	  $dbcheckmsgid=$dbcheckrow['msgid'];
	  $dbchecklink="<a href='".$dbcheckrow['link']."'> ➡️ ".htmlspecialchars($text)."</a>";
     } // while ($dbcheckrow = pg_fetch_array($dbcheckresult)) 	
    } else $dbchecklink=" ".htmlspecialchars($text); // if (pg_num_rows($dbcheckresult)!=0) 
		
    sendmessageintochannel($channelInfoKey, "❌<b>#getfile</b>".$dbchecklink."<br><b>already on channel</b><br>".generatebotmarker(), $topicid);
    logger(mb_sprintf("syncFiles: do not forward from [%s][%s] - %-54s",$fromChannel, $row['msgid'], $row['cleanmessage'])); 
	return;
   } // if (is_array($filetargetcheck)) 

   if (is_array($filetargetcheck)) { 

    if ($grouped_id==-1) { // no grouped / album posts check message befor 

     $forward[]=$msgid;
     $infoArray=array("msgid" => $msgid, "album" => -1, "text" => $text, "cover" => -1, "width" => $width, "height" => $height, "runtime" => $runtime, "filedate" => $filedate, "filesize" => $filesize);

     unset($msgbefor);
     $msgbefor=syncMsgBefor($msgid, $channelInfoNew[$channelInfoKey]["redirectout"]);
     if ($msgbefor!="") { 
      $forward[]=$msgbefor;
      $infoArray["cover"]=$msgbefor;
     }
      fillForwardArray($infoArray, $fwd); // array included video and cover info's 
    } else {
     if (!array_search($msgid, $forward)) {
      $albenreturn=syncGaterAlbumPosts($channelInfoNew[$channelInfoKey]["redirectout"], $msgid, $grouped_id); 

      foreach(array_keys($albenreturn) as $k) {
       $forward[]=$albenreturn[$k]["msgid"]; 
       $infoArray=array("msgid" => $albenreturn[$k]["msgid"], "album" => $grouped_id, "text" => $albenreturn[$k]["text"], "cover" => $albenreturn[$k]["cover"] , "width" => $width, "height" => $height, "runtime" => $runtime, "filedate" => $filedate, "filesize" => $filesize); 
       fillForwardArray($infoArray, $fwd); 
      } // foreach(array_keys($albenreturn) as $k) 
     } // if (!array_search($msgid, $forward))
    } // if ($grouped_id==-1) - else

// hack
if ($channelInfoNew[$channelInfoKey]["redirectout_tochannel"] == -1) {
 $tmpredchanname="<a href='https://t.me/c/".$channelInfoNew[$channelInfoKey]["chanid"]."/'> ➡️ ".htmlspecialchars($channelInfoNew[$channelInfoKey]["name"])."</a>";
} else {
 $tmpredchanname="<a href='https://t.me/c/".$channelInfoNew[$channelInfoKey]["redirectout_tochannel"]."/'> ➡️ ".htmlspecialchars($channelInfoNew[$channelInfoKey]["redirectout_tochannel_name"])."</a>";	
} // if ($channelInfoNew[$channelInfoKey]["redirectout_tochannel"] == -1)
// hack end

sendmessageintochannel($channelInfoKey, "📝<b>".$requestcommand." #getfile</b>: ".htmlspecialchars($text)."<br>in forward queue to<b>".$tmpredchanname."</b><br>".generatebotmarker(), $topicid);
   } // if (is_array($filetargetcheck)) 
  } // while ($row = pg_fetch_array($result))  
 } // if ($channelInfoNew[$channelInfoKey]["redirectout"] != -1) 
 return;
} // function end