<?php
/**
 * @copyright 2021 Double Bastion LLC <www.doublebastion.com>
 *
 * @author Double Bastion LLC
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or any later version.
 *
 * 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 AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

declare(strict_types=1);

namespace OCA\SIPTripPhone\Service;

use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\Security\ICrypto;
use function OCP\Log\logger;

class SphoneService {

    private $connection;
    private $crypto;

    public function __construct(IDBConnection $connection, ICrypto $crypto) {
                $this->connection = $connection;
                $this->crypto = $crypto;
    }

    /**
     * @NoAdminRequired
     *
     */
    public function getsettings($userId) {

        $sql = $this->connection->prepare('
                    SELECT `id`, `user_id`, `pdisplayname`, `sipusername`, `sipuserpassword`, `stphwssurl`, `siprealm`, `stunserver`, `tracesipmsg`, `voicenumbers`, `defaultvoicenumber`
		    FROM  `*PREFIX*sip_trip_phone`
		    WHERE `user_id` = ?');
        $result = $sql->execute([$userId]);
        $settingsdb = $result->fetch();
        $result->closeCursor();

        if ($settingsdb) {
            if ($settingsdb['sipuserpassword'] != '' && $settingsdb['sipuserpassword'] != null && $settingsdb['sipuserpassword'] != 'undefined') {
                $settingsdb['sipuserpassword'] = "%20%20%20%20%20%20%20";
            } else { $settingsdb['sipuserpassword'] = ''; }

            return $settingsdb;
        }
    }

    /**
     * @NoAdminRequired
     *
     */
    public function getsippass($userId) {

        $sqlps = $this->connection->prepare('
                    SELECT `id`, `user_id`, `sipuserpassword`
		    FROM  `*PREFIX*sip_trip_phone`
		    WHERE `user_id` = ?');
        $resultps = $sqlps->execute([$userId]);
        $settingsdb = $resultps->fetch();
        $resultps->closeCursor();

        if ($settingsdb) {
            if ($settingsdb['sipuserpassword'] != '' && $settingsdb['sipuserpassword'] != null && $settingsdb['sipuserpassword'] != 'undefined') {
                $sipuserpassworddecr = $this->crypto->decrypt($settingsdb['sipuserpassword']);
                $sippassword = $sipuserpassworddecr;
            }

            return $sippassword;
        }
    }

    /**
     * @NoAdminRequired
     *
     */
    public function updatesettings($userId, $pdisplayname, $sipusername, $sipuserpassword, $stphwssurl, $siprealm, $stunserver, $tracesipmsg, $voicenumbers, $defaultvoicenumber) {

        // Validate the data entered in the fields on the settings page
        if (mb_strlen($pdisplayname) > 128) { logger('sip_trip_phone')->error("The 'Display Name' cannot exceed 128 characters!"); exit(); }
        if (!preg_match('/^[a-zA-Z0-9\*\#]+$/', $sipusername)) { logger('sip_trip_phone')->error("The 'SIP User' that you entered is not valid. The 'SIP User' must contain only alphanumeric characters, asterisks (*) and number signs (#).)"); exit(); }        
        if (mb_strlen($sipuserpassword) > 300) { logger('sip_trip_phone')->error("The 'SIP User Password' cannot exceed 300 characters!"); exit(); }      
        if (filter_var($stphwssurl, FILTER_VALIDATE_URL) == false) { logger('sip_trip_phone')->error("The 'WSS URL' that you entered is not valid."); exit(); }
        if (filter_var($siprealm, FILTER_VALIDATE_IP) == false && filter_var($siprealm, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME) == false) { logger('sip_trip_phone')->error("The 'SIP Realm' that you entered is not valid."); exit(); }
        if ($stunserver != '') {
            $stunIpDom = explode(":", $stunserver);
            if ((filter_var($stunIpDom[0], FILTER_VALIDATE_IP) == false && filter_var($stunIpDom[0], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME) == false) || !preg_match('/^[0-9]+$/', $stunIpDom[1])) { logger('sip_trip_phone')->error("The 'STUN Server' that you entered is not valid."); exit(); }
        }
        if (!preg_match('/^[a-zA-Z0-9\040\+\,\:]*$/', $voicenumbers)) { logger('sip_trip_phone')->error("The 'Available phone numbers' that you entered are not valid. The 'Available phone numbers' must contain only alphanumeric characters, colons (:), spaces, plus signs (+), digits (0-9) and commas (,)."); exit(); }
        if (!preg_match('/^[a-zA-Z0-9\040\+\:]*$/', $defaultvoicenumber)) { logger('sip_trip_phone')->error("The 'Default phone number for outgoing calls' that you entered is not valid. The 'Default phone number for outgoing calls' must contain only alphanumeric characters, a colon (:), a space, a plus sign (+) and digits (0-9)."); exit(); }

        $sqlup = $this->connection->prepare('
                    SELECT `id`, `user_id`, `pdisplayname`, `sipusername`, `sipuserpassword`, `stphwssurl`, `siprealm`, `stunserver`, `tracesipmsg`, `voicenumbers`, `defaultvoicenumber`
		    FROM  `*PREFIX*sip_trip_phone`
		    WHERE `user_id` = ?');
        $resultup = $sqlup->execute([$userId]);
        $row = $resultup->fetch();
        $resultup->closeCursor();

        if ($resultup && !$row) {

            if ($sipuserpassword != '') {
                $sipuserpasswordenc = $this->crypto->encrypt($sipuserpassword);
            } else { $sipuserpasswordenc = ''; }

            $sqlins = $this->connection->prepare('
				INSERT INTO `*PREFIX*sip_trip_phone`
					(`user_id`, `pdisplayname`, `sipusername`, `sipuserpassword`, `stphwssurl`, `siprealm`, `stunserver`, `tracesipmsg`, `voicenumbers`, `defaultvoicenumber`)
				VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
	    $sqlins->execute([$userId, $pdisplayname, $sipusername, $sipuserpasswordenc, $stphwssurl, $siprealm, $stunserver, $tracesipmsg, $voicenumbers, $defaultvoicenumber]);

        } elseif ($resultup && $row) {

            if ($sipuserpassword != '' && $sipuserpassword != "%20%20%20%20%20%20%20") {
                $sipuserpasswordenc = $this->crypto->encrypt($sipuserpassword);
            } elseif ($sipuserpassword == "%20%20%20%20%20%20%20") {
                $sipuserpasswordenc = $row['sipuserpassword'];
            } elseif ($sipuserpassword == '') {
                $sipuserpasswordenc = '';
            }

	    $sqlup = $this->connection->prepare('
			UPDATE `*PREFIX*sip_trip_phone`
			SET `pdisplayname` = ?, `sipusername` = ?, `sipuserpassword` = ?, `stphwssurl` = ?, `siprealm` = ?, `stunserver` = ?, `tracesipmsg` = ?, `voicenumbers` = ?, `defaultvoicenumber` = ?
                        WHERE `user_id` = ?');
	    $updateRes = $sqlup->execute([$pdisplayname, $sipusername, $sipuserpasswordenc, $stphwssurl, $siprealm, $stunserver, $tracesipmsg, $voicenumbers, $defaultvoicenumber, $userId]);
	    $updateRes->closeCursor();

        }
    }
}
