<?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 getcontactsnmbrs($userId) {

        // Get the phone numbers of regular contacts

        $cardadd = "card_add_self";
        $cntctsusr = $this->connection->prepare('
                    SELECT `user`, `subject`, `subjectparams`
		    FROM  `*PREFIX*activity`
		    WHERE `user` = ? AND `subject` = ?');
        $rescntcts = $cntctsusr->execute([$userId, $cardadd]);
        $cnctsforuser = [];
        while ($ctrow = $rescntcts->fetch()) {
               $ctsperuserarr = json_decode($ctrow['subjectparams'], true);
               $cnctsforuser[] = $ctsperuserarr['card']['id'];
        }
        $rescntcts->closeCursor();

        $contactData = [];
        if ($cnctsforuser) {

            foreach ($cnctsforuser as $ctkey => $ctid) {
                 $ctdata = $this->connection->prepare('
                           SELECT `carddata`, `uid`
		           FROM  `*PREFIX*cards`
		           WHERE `uid` = ?');
                 $ctdatares = $ctdata->execute([$ctid]);
                 $ctdatarow = $ctdatares->fetch();
                 $ctdatares->closeCursor();
                 $ctdatarr = preg_split('/\r\n|\r|\n/', $ctdatarow['carddata']);
                 $contactNumbers = [];
                 $contactRole = '';
                 foreach ($ctdatarr as $cdkey => $cdata) {
                          $telcheck = false;
                          if (str_contains($cdata, "FN:")) {
                              $cdnamearr = explode("FN:", $cdata);
                              $contactName = $cdnamearr[1];
                          } elseif (str_contains($cdata, "TITLE:")) {
                              $cdtitlearr = explode("TITLE:", $cdata);
                              $contactRole = $cdtitlearr[1];
                          } elseif (str_contains($cdata, "TEL;")) {
                              $cdtelarr = explode("TYPE=", $cdata);
                              $cdteldatarr = explode(":", $cdtelarr[1]);
                                  if (str_contains($cdteldatarr[0], 'CELL') || str_contains($cdteldatarr[0], 'VOICE') || str_contains($cdteldatarr[0], 'CAR')) {
                                      $ctNumberType = $cdteldatarr[0];
                                      $ctNumber = $cdteldatarr[1];
                                      if ($ctNumber) { $telcheck = true; }
                                  }
                          }
                          if ($telcheck) { $contactNumbers[] = [$ctNumberType, $ctNumber]; }
                 }
                 if ($contactNumbers) {
                     $contactData[] = ['contact_name' => $contactName, 'contact_role' => $contactRole, 'contact_type' => 'regular_contact', 'contact_numbers' => $contactNumbers];
                 }
            }
        }

        // Get the (non-private) phone numbers of Nextcloud users

        $ncusers = $this->connection->prepare('
                    SELECT `uid`, `data`
		    FROM  `*PREFIX*accounts`');
        $ncusersres = $ncusers->execute();
        $ncUserData = [];
        while ($ncrow = $ncusersres->fetch()) {
               $ncperuserarr = json_decode($ncrow['data'], true);
               if ($ncperuserarr['phone']['value'] && ($ncperuserarr['phone']['scope'] == 'v2-local' || $ncperuserarr['phone']['scope'] == 'v2-federated' || $ncperuserarr['phone']['scope'] == 'v2-published')) {

                   $ncdisplayname = $ncperuserarr['displayname']['value'];

                   $nctelnumber = [['VOICE/CELL', $ncperuserarr['phone']['value']]];

                   if ($ncperuserarr['role']['scope'] == 'v2-local' || $ncperuserarr['role']['scope'] == 'v2-federated' || $ncperuserarr['role']['scope'] == 'v2-published') {
                       $ncuserrole = $ncperuserarr['role']['value'];
                   } else { $ncuserrole = ''; }

                   $ncUserData[] = ['contact_name' => $ncdisplayname, 'contact_role' => $ncuserrole, 'contact_type' => 'ncuser_contact', 'contact_numbers' => $nctelnumber];
               }
        }
        $ncusersres->closeCursor();

        $usersAndContacts = array_merge($contactData, $ncUserData);

        return $usersAndContacts;
    }

    /**
     * @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();

        }
    }
}