<?php
set_time_limit(0);
require_once("../../../wp-config.php");
$con = mysql_connect('localhost', 'root', '');


$strSQL = "SELECT * FROM rss";
$rssLink = mysql_query($strSQL);

if (mysql_num_rows($rssLink) > 0) {
    $urls = array();

    while ($row = mysql_fetch_assoc($rssLink)) {
        $urls[] = array(
            'id' => $row['id'],
            'rssLink' => $row['rssLink']
        );
    }

    $item = $urls[0];
}

$strSQL5 = "SELECT * FROM device";
$devices = mysql_query($strSQL5);

if (mysql_num_rows($devices) > 0) {
    while ($row = mysql_fetch_assoc($devices)) {
        $idevices[] = $row['gcm_id'];
    }
}


$url = $item['rssLink'];
$linkId = $item['id'];

$feed = implode(file($url));
$xml = simplexml_load_string($feed);
$json = json_encode($xml);
$array = json_decode($json,TRUE);

/*$update = array();
$update_new = array();

$strSQL1 = "SELECT * FROM update_content where rssLinkId = '$linkId'";
$old_update_contents = mysql_query($strSQL1);


if (mysql_num_rows($old_update_contents) > 0) {
    $update = array();

    while ($row = mysql_fetch_assoc($old_update_contents)) {
        $update[] = $row['content'];
    }
} else $update = array();

$strSQL2 = "DELETE from update_content where rssLinkId = '$linkId'";
$del = mysql_query($strSQL2);*/

if (isset ($array['channel'])) {
    if (isset($array['channel']['item'])) {
        $entries = $array['channel']['item'];

        foreach ($entries as $entry) {

            $dateCreated = strtotime('now');
            if (isset($entry['title'])) {
                $content = $entry['title'];
                $strSQL3 = "INSERT INTO news (rssLinkId, content, dateCreated)
                       VALUES
                       ( '$linkId', '$content','$dateCreated'  )";

                mysql_query($strSQL3);
            }

        }
    }
}

$strSQL4 = "SELECT * FROM update_content where rssLinkId = '$linkId'";
$new_update = mysql_query($strSQL4);
if (mysql_num_rows($new_update) > 0) {
    $update_new = array();

    while ($row = mysql_fetch_assoc($new_update)) {
        $update_new[] = $row['content'];
    }
} else $update_new = array();

if (count(array_diff($update, $update_new)) != 0 or count($update) != count($update_new)) {

    if ($update_new[0] != NULL) {

        $message = $update_new[0];

        $badge = 1;
        $sound = 'default';
        $development = true;
        $passphrase = 'DWESCHOOL';

        $payload = array();
        $payload['aps'] = array('alert' => $message, 'badge' => intval($badge), 'sound' => $sound);
        $payload = json_encode($payload);

        $apns_url = NULL;
        $apns_cert = NULL;
        $apns_port = 2195;

        if ($development) {
            $apns_url = 'gateway.sandbox.push.apple.com';
            $apns_cert = ABSPATH . 'wp-content/plugins/coconut/pem/ck.pem';
        } else {
            $apns_url = 'gateway.push.apple.com';
            $apns_cert = ABSPATH . 'wp-content/plugins/coconut/pem/ck.pem';
        }

        foreach ($idevices as $device) {

            $stream_context = stream_context_create();
            stream_context_set_option($stream_context, 'ssl', 'local_cert', $apns_cert);
            stream_context_set_option($stream_context, 'ssl', 'passphrase', $passphrase);

            $apns = stream_socket_client('ssl://' . $apns_url . ':' . $apns_port, $error, $error_string, 2, STREAM_CLIENT_CONNECT, $stream_context);

            $token = $device;

            $device_tokens = str_replace("<", "", $token);
            $device_tokens1 = str_replace(">", "", $device_tokens);
            $device_tokens2 = str_replace(' ', '', $device_tokens1);

            $apns_message = chr(0) . chr(0) . chr(32) . pack('H*', $device_tokens2) . chr(0) . chr(strlen($payload)) . $payload;

            $msgapns = fwrite($apns, $apns_message);
            if (!$msgapns) {
                echo 'Message not delivered:' . $error . '; error string:' . $error_string . PHP_EOL;
            } else {
                echo 'Message successfully delivered' . PHP_EOL;
            }
            @socket_close($apns);
            @fclose($apns);
        }
    }

}

$json_output1 = json_encode(array(
    'status' => 'success',
    'data' => '',
    'message' => 'OK',
));
echo $json_output1;