<?php

include("../db.php");


$session = $_REQUEST["session"];
$msg = $_REQUEST["msg"];
$stu_id = $_REQUEST["id"];
$set_title = $_REQUEST["title"];

// Enabling error reporting
error_reporting(-1);
ini_set('display_errors', 'On');

//require_once  __DIR__.'/firebase/firebase.php';
require_once __DIR__ . '/firebase/push.php';

$allowed = 0;
$notification_controls = $db->getRow("SELECT * FROM notification_control WHERE id = '1' ");

if ($notification_controls != "" && isset($notification_controls['teacher_notice'])) {
    $allowed = $notification_controls['teacher_notice'];
}

if ($allowed == 0){

    //$firebase = new Firebase();
    $push = new Push();

    $tokenlist = $db->getRows("SELECT idno,name,fcm_token from staff where idno='$stu_id' and session='$session' and status='1' ");

    $result_arr = array();

    foreach ($tokenlist as $value) {


        $stu_id = $value['idno'];
        $target_user_name = $value['name'];
        $fb_token = $value['fcm_token'];

        if (strlen($fb_token) > 0) {

            // optional payload
            $payload = array();
            $payload['name'] = $target_user_name;
            $payload['msg'] = $msg; // whether to include to image or not
            $payload['stu_id'] = $stu_id;
            $include_image = TRUE;
            $push->setTitle($set_title);
            $push->setMessage($msg);
            if ($include_image) {
                $push->setImage('https://api.androidhive.info/images/minion.jpg');
            } else {
                $push->setImage('');
            }
            $push->setIsBackground(FALSE);
            $push->setPayload($payload);
            $json = '';
            $response = '';
            $json = $push->getPush();


            // $response = $firebase->send($fb_token, $json);

            $fields = array(
                'to' => $fb_token,
                'data' => $json,
            );


            $SERVER_KEY = $db->getVal("select firebase_server_key from tbl_info where id='1'");


            // Set POST variables
            $url = 'https://fcm.googleapis.com/fcm/send';

            $headers = array(
                'Authorization: key=' . $SERVER_KEY,
                'Content-Type: application/json'
            );
            // Open connection
            $ch = curl_init();

            // Set the url, number of POST vars, POST data
            curl_setopt($ch, CURLOPT_URL, $url);

            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

            // Disabling SSL Certificate support temporarly
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

            // Execute post
            $result = curl_exec($ch);
            if ($result === FALSE) {
                die('Curl failed: ' . curl_error($ch));
            }

            // Close connection
            curl_close($ch);

            // return $result;


            array_push($result_arr, $stu_id . $result);

        }


    }

    echo json_encode($result_arr);


}