#!/usr/bin/env php
<?php

/*
 * LMS version 1.11-git
 *
 *  (C) Copyright 2001-2020 LMS Developers
 *
 *  Please, see the doc/AUTHORS for more information about authors!
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License Version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
 *  USA.
 *
 *  $Id$
 */

// REPLACE THIS WITH PATH TO YOUR CONFIG FILE

// PLEASE DO NOT MODIFY ANYTHING BELOW THIS LINE UNLESS YOU KNOW
// *EXACTLY* WHAT ARE YOU DOING!!!
// *******************************************************************

ini_set('error_reporting', E_ALL & ~E_NOTICE & ~E_DEPRECATED);

$parameters = array(
    'config-file:' => 'C:',
    'quiet' => 'q',
    'help' => 'h',
    'version' => 'v',
    'message-file:' => 'm:',
);

$long_to_shorts = array();
foreach ($parameters as $long => $short) {
    $long = str_replace(':', '', $long);
    if (isset($short)) {
        $short = str_replace(':', '', $short);
    }
    $long_to_shorts[$long] = $short;
}

$options = getopt(
    implode(
        '',
        array_filter(
            array_values($parameters),
            function ($value) {
                return isset($value);
            }
        )
    ),
    array_keys($parameters)
);

foreach (array_flip(array_filter($long_to_shorts, function ($value) {
    return isset($value);
})) as $short => $long) {
    if (array_key_exists($short, $options)) {
        $options[$long] = $options[$short];
        unset($options[$short]);
    }
}

if (array_key_exists('version', $options)) {
    print <<<EOF
lms-smstools-delivery-report.php
(C) 2001-2020 LMS Developers

EOF;
    exit(0);
}

if (array_key_exists('help', $options)) {
    print <<<EOF
lms-smstools-delivery-report.php
(C) 2001-2020 LMS Developers

-C, --config-file=/etc/lms/lms.ini      alternate config file (default: /etc/lms/lms.ini);
-m, --message-file=<message-file>       name of message file;
-h, --help                      print this help and exit;
-v, --version                   print version info and exit;
-q, --quiet                     suppress any output, except errors;

EOF;
    exit(0);
}

$quiet = array_key_exists('quiet', $options);
if (!$quiet) {
    print <<<EOF
lms-smstools-delivery-report.php
(C) 2001-2020 LMS Developers

EOF;
}

if (array_key_exists('config-file', $options)) {
    $CONFIG_FILE = $options['config-file'];
} else {
    $CONFIG_FILE = '/etc/lms/lms.ini';
}

if (array_key_exists('config-file', $options)) {
    $CONFIG_FILE = $options['config-file'];
} else {
    $CONFIG_FILE = DIRECTORY_SEPARATOR . 'etc' . DIRECTORY_SEPARATOR . 'lms' . DIRECTORY_SEPARATOR . 'lms.ini';
}

if (!$quiet) {
    echo "Using file ".$CONFIG_FILE." as config." . PHP_EOL;
}

if (!is_readable($CONFIG_FILE)) {
    die('Unable to read configuration file ['.$CONFIG_FILE.']!');
}

define('CONFIG_FILE', $CONFIG_FILE);

$CONFIG = (array) parse_ini_file($CONFIG_FILE, true);

// Check for configuration vars and set default values
$CONFIG['directories']['sys_dir'] = (!isset($CONFIG['directories']['sys_dir']) ? getcwd() : $CONFIG['directories']['sys_dir']);
$CONFIG['directories']['lib_dir'] = (!isset($CONFIG['directories']['lib_dir']) ? $CONFIG['directories']['sys_dir'] . DIRECTORY_SEPARATOR . 'lib' : $CONFIG['directories']['lib_dir']);
$CONFIG['directories']['plugin_dir'] = (!isset($CONFIG['directories']['plugin_dir']) ? $CONFIG['directories']['sys_dir'] . DIRECTORY_SEPARATOR . 'plugins' : $CONFIG['directories']['plugin_dir']);
$CONFIG['directories']['plugins_dir'] = $CONFIG['directories']['plugin_dir'];

define('SYS_DIR', $CONFIG['directories']['sys_dir']);
define('LIB_DIR', $CONFIG['directories']['lib_dir']);
define('PLUGIN_DIR', $CONFIG['directories']['plugin_dir']);
define('PLUGINS_DIR', $CONFIG['directories']['plugin_dir']);

// Load autoloader
$composer_autoload_path = SYS_DIR . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($composer_autoload_path)) {
    require_once $composer_autoload_path;
} else {
    die("Composer autoload not found. Run 'composer install' command from LMS directory and try again. More information at https://getcomposer.org/" . PHP_EOL);
}

// Init database

$DB = null;

try {
    $DB = LMSDB::getInstance();
} catch (Exception $ex) {
    trigger_error($ex->getMessage(), E_USER_WARNING);
    // can't working without database
    die("Fatal error: cannot connect to database!" . PHP_EOL);
}

// Include required files (including sequence is important)

require_once(LIB_DIR . DIRECTORY_SEPARATOR . 'common.php');
require_once(LIB_DIR . DIRECTORY_SEPARATOR . 'language.php');
require_once(LIB_DIR . DIRECTORY_SEPARATOR . 'definitions.php');

if (array_key_exists('message-file', $options)) {
    $message_file = $options['message-file'];
} else {
    die("Required message file parameter!" . PHP_EOL);
}

if (!($lines = @file($message_file))) {
    die("Cannot read message file!" . PHP_EOL);
}

$msgid = preg_grep('/^Message_id: [0-9]+$/', $lines);
$status = preg_grep('/^Status:/', $lines);
$timestamp = preg_grep('/^Discharge_timestamp:/', $lines);
$phone = preg_grep('/^From:/', $lines);
if (empty($msgid) || empty($status) || empty($phone) || empty($timestamp)) {
    die("Message file is not delivery report!" . PHP_EOL);
}
$msgid = preg_replace('/^Message_id:\s*/', '', trim(reset($msgid)));
$status = preg_replace('/^Status:\s*/', '', trim(reset($status)));
$timestamp = strtotime(preg_replace('/^Discharge_timestamp:\s*/', '', trim(reset($timestamp))));
$phone = preg_replace('/^From:\s*/', '', trim(reset($phone)));

$msgitems = $DB->GetAll(
    'SELECT
        mi.id,
        mi.destination
    FROM messageitems mi
    JOIN messages m ON m.id = mi.messageid
    WHERE m.type = ?
        AND mi.status = ?
        AND m.cdate >= ?NOW? - 86400
        AND mi.externalmsgid = ?',
    array(
        MSG_SMS,
        MSG_SENT,
        $msgid,
    )
);
if (empty($msgitems)) {
    die;
}

$sms_prefix = ConfigHelper::getConfig('sms.prefix');
$prefix = !empty($sms_prefix) ? $sms_prefix : '';

foreach ($msgitems as $msgitem) {
    $number = preg_replace('/[^0-9]+/', '', $msgitem['destination']);
    $number = preg_replace('/^0+/', '', $number);
    $number = str_replace(' ', '', $number);
    if ($prefix && substr($number, 0, strlen($prefix)) != $prefix) {
        $number = $prefix . $number;
    }
    if ($number == $phone) {
        [$code, $status, $text_status] = explode(',', $status);
        if ($status == 'Ok') {
            $DB->Execute(
                'UPDATE messageitems SET status = ?, lastdate = ? WHERE id = ?',
                array(MSG_DELIVERED, $timestamp, $msgitem['id'])
            );
        } else {
            $DB->Execute(
                'UPDATE messageitems SET status = ?, lastdate = ?, error = ? WHERE id = ?',
                array(MSG_ERROR, $timestamp, $text_status . ' (' . $code . ')', $msgitem['id'])
            );
        }
    }
}
