<?php

header("Content-Type: text/html; charset=utf-8");

function notifications($user = '', $message = '') {
    /* /* SETUP *
      $key = "TcNfnOqRXsJUCh5MzuNhP8y9WddZvMgm"; // PsM9bemc7yyAJwIjI5gm2BJAVmeP4ik1
      $username = "admin"; // app admin user name
      $password = "admin"; // app admin password
      $to_ids = "57bc48d3f995d7091eba0cc1"; // 57385a59630a5e08eb0eacad
      $channel = "$user"; // user_id (if a channel is created for each user) or global channel for all users
      //    $message ; // the message to be displayed
      $title = "يوجد اشعار جديد";
      $tmp_fname = 'cookie.txt';
      //    var_dump($message);exit();
      $json = '{"alert":"' . $message . '","title":"' . $title . '","vibrate":true,"sound":"default","icon":"appicon"';
      $data = array(); // add here any additional data you want to pass to the app
      // copy data from array to json to be sent
      foreach ($data as $k => $v) {
      $json .= ', "' . $k . '":"' . $v . '"';
      }
      $json .= '}';

      if (!is_null($key) && !is_null($username) && !is_null($password) && !is_null($channel) && !is_null($message) && !is_null($title)) {
      /* PUSH NOTIFICATION *

      /* INIT CURL *
      $curlObj = curl_init();
      $c_opt = array(CURLOPT_URL => 'https://api.cloud.appcelerator.com/v1/users/login.json?key=' . $key,
      CURLOPT_POST => true,
      CURLOPT_POSTFIELDS => "login=" . $username . "&password=" . $password,
      CURLOPT_SSL_VERIFYPEER => false,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_COOKIEFILE => $tmp_fname,
      CURLOPT_COOKIEJAR => $tmp_fname
      );

      /* LOGIN *
      curl_setopt_array($curlObj, $c_opt);
      $session = curl_exec($curlObj);
      //        var_dump($session);exit;
      /* SEND PUSH *
      $c_opt[CURLOPT_URL] = "https://api.cloud.appcelerator.com/v1/push_notification/notify.json?key=" . $key;
      $data = array(
      "channel" => $channel,
      "to_ids" => $to_ids,
      "payload" => $json
      );
      $c_opt[CURLOPT_POSTFIELDS] = $data;

      curl_setopt_array($curlObj, $c_opt);
      $session = curl_exec($curlObj);
      //        var_dump($session);exit;
      /* THE END *
      curl_close($curlObj);

      //header('Content-Type: application/json');
      //die(json_encode(array('response' => json_decode($session))));
      } else {
      //header('Content-Type: application/json');
      //die(json_encode(array('response' => json_decode($session))));
      } */




    /*     * * SETUP ************************************************** */
    $key = "TcNfnOqRXsJUCh5MzuNhP8y9WddZvMgm";          //GET FROM APP MANAGEMENT (ACS)
    $username = "admin";
    $password = "admin";
    $channel = $user;
    $to_ids = "57bc48d3f995d7091eba0cc1";
    //$message = $message;
    $title = "يوجد اشعار جديد";
    $tmp_fname = 'cookie.txt';
    $json = '{"alert":"' . $message . '","title":"' . $title . '","vibrate":true,"sound":"default","icon":"appicon"}';

    /*     * * PUSH NOTIFICATION ********************************** */

    $post_array = array('login' => $username, 'password' => $password);

    /*     * * INIT CURL ****************************************** */
    $curlObj = curl_init();
    $c_opt = array(CURLOPT_URL => 'https://api.cloud.appcelerator.com/v1/users/login.json?key=' . $key,
        CURLOPT_COOKIEJAR => $tmp_fname,
        CURLOPT_COOKIEFILE => $tmp_fname,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST => 1,
        CURLOPT_POSTFIELDS => "login=" . $username . "&password=" . $password,
        CURLOPT_FOLLOWLOCATION => 1,
        CURLOPT_TIMEOUT => 60);

    /*     * * LOGIN ********************************************* */
    curl_setopt_array($curlObj, $c_opt);
    $session = curl_exec($curlObj);

    /*     * * SEND PUSH ***************************************** */
    $c_opt[CURLOPT_URL] = "https://api.cloud.appcelerator.com/v1/push_notification/notify.json?key=" . $key;
    $c_opt[CURLOPT_POSTFIELDS] = "channel=" . $channel . "&payload=" . $json . "&to_ids=" . $to_ids;

    curl_setopt_array($curlObj, $c_opt);
    $session = curl_exec($curlObj);

    /*     * * THE END ******************************************* */
    curl_close($curlObj);
    //echo $session;
}
