<?php
$cron = file_get_contents(__DIR__.'/fwjob.txt');

if($cron == '0') exit;

if($cron == '') $cron = 0;
$fwid = file_get_contents(__DIR__.'/fwid.txt');
$pm = json_decode($fwid);

if($fwid == '' or is_null($pm) or !$pm){
	file_put_contents(__DIR__.'/fwjob.txt',0);
	file_put_contents(__DIR__.'/fwid.txt','');
	exit;
}

include_once 'config.php';
include_once 'telegram.php';
$telegram = new telegram(TOKEN,HOST,USERNAME,PASSWORD,DBNAME);
$db = new PDO("mysql:host=".HOST.";dbname=".DBNAME,USERNAME,PASSWORD,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));

$query = $db->query("SELECT * FROM fl_user WHERE id > $cron ORDER BY id LIMIT 250");
$result = $query->fetchAll(2);
$rcount = $query->rowCount();

if( $rcount > 1 ) {
    foreach ($result as $user) {
		$id = $user['id'];
        $userid = $pm->userid;
        $msgid = $pm->msgid;
        $telegram->forwardmessage($user['userid'],$userid,$msgid);
        file_put_contents(__DIR__.'/fwjob.txt',$id);
    }
}else{
	file_put_contents(__DIR__.'/fwjob.txt',0);
	file_put_contents(__DIR__.'/fwid.txt','');
}