<commit_before><?php

namespace jonasarts\MailgatewaySyncBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

use jonasarts\MailgatewaySyncBundle\Entity\Domain;

class DefaultController extends Controller
{
    private function removeExpiredDomains($config)
    {
        $msg = "<h1>Delete</h1>\n";

        $default_em = $this->get('doctrine')->getManager();

        $d = new \DateTime("-1 hour");

        // expired
        $domains = $default_em->createQuery(
            'SELECT d 
            FROM jonasartsMailgatewaySyncBundle:Domain d 
            WHERE d.last_sync < :lastsync 
            ORDER BY d.domainname ASC'
            )
            ->setParameter('lastsync', $d->format('Y-m-d H:i:s'))
            ->getResult();;

        if ($domains) {
            $msg .= "Count = ". count($domains) ."<br><br>\n";

            foreach ($domains as $domain) {
                //var_dump($domain);

                // "DELETE FROM `postfix_relay_domain` WHERE `id` = ".$lo_row->id.";<br />\n";
                // "DELETE FROM `postfix_transport` WHERE `id` = ".$lo_row->id.";<br />\n";
                // "DELETE FROM `sync` WHERE `id` = ".$lo_row->id.";<br />\n";

                $msg .= "Delete domain (" . $domain->getId() . ") " . $domain->getDomainname() . " expired at " . $domain->getLastSync()->format('Y-m-d H:i:s') . "<br>\n";

                if ($config['remove']) {
                    $default_em->remove($domain);
                    $default_em->flush();
                }
            }
        }

        return $msg;
    }

    private function syncDomains($config)
    {
        $msg = "<h1>Insert & Update</h1>\n";

        $default_em = $this->get('doctrine')->getManager();
        $master_em = $this->get('doctrine')->getManager('master');

        /*
        $query = "SELECT cmo.id, cmo.type, cmo.obj_name, ps.name, cms.name ".
            "FROM cmail_object AS cmo ".
            "INNER JOIN plesk_server AS ps ON cmo.plesk_server_id = ps.id ".
            "INNER JOIN plesk_server AS cms ON cmo.cmail_server_id = cms.id ".
            "WHERE cmo.type <> 'client' ".
            "ORDER BY cmo.id";

        $query = "SELECT `sync`.*, `postfix_relay_domain`.`domain` ".
            "FROM `sync` ".
            "LEFT JOIN `postfix_relay_domain` ON `sync`.`id` = `postfix_relay_domain`.`id` ".
            "WHERE `lastrun` < %s ".
            "ORDER BY `sync`.`master_id`";
        */

        if ($config['mode'] == 'ppa') { // ppa mailgateway
            $query = "SELECT domains.id, domains.name ".
                "FROM domains ".
                "INNER JOIN dns_recs ON domains.dns_zone_id = dns_recs.dns_zone_id ".
                "WHERE dns_recs.type = 'MX' AND dns_recs.val = :mailgateway ".
                "GROUP BY domains.id ".
                "ORDER BY domains.name ASC";
        } else { // custom mailgateway
            $query = "SELECT domains.id, domains.name, domains.destination ".
                "FROM domains ".
                "ORDER BY domains.name ASC";
        }

        // query master server
        $connection = $master_em->getConnection();
        $statement = $connection->prepare($query);
        if ($config['mode'] == 'ppa') {
            $statement->bindValue('mailgateway', $config['gateway_server'].'.');
        }
        $result = $statement->execute();

        //echo "Query = ". $query ."<br>";

        if ($result) {
            $records = $statement->fetchAll();
            
            $msg .= "Count = ". count($records) ."<br><br>\n";
            
            foreach ($records as $record) {
                //var_dump($record);

                $domain = $default_em->createQuery(
                    'SELECT d 
                    FROM jonasartsMailgatewaySyncBundle:Domain d
                    WHERE d.master_id = :id'
                    )
                    ->setParameter('id', $record['id'])
                    ->getOneOrNullResult();

                // load checksum
                // $domain->getChecksum();

                // load destination server
                if ($config['mode'] == 'custom') {
                    $record['destination'] = $config['postfix_mailsever'];
                }

                if ($domain) { // domain record found -> update

                    $csr = hash('md5', $record['name']); // remote cs
                    $csl = $domain->getChecksum(); // local cs

                    if ($csr != $csl) { // update domain.domainname & domain.destination & domain.last_sync

                        $msg .= "Update " . $record['id'] . " domain " . $record['name'] . " transport smtp:[".$record['destination']."]<br>\n";

                        // "UPDATE postfix_relay_domain SET domain = %s WHERE master_id = %d"
                        // "UPDATE postfix_transport SET domain = %s, destination = 'smtp:[%s]' WHERE master_id = %d"

                        $domain->setDomainname($record['name']);
                        $domain->setMailservername($record['destination']);
                        $domain->setLastSync(new \DateTime());
                        $default_em->persist($domain);
                        $default_em->flush();

                    } else { // only update domain.last_sync<commit_msg>Fixed wrong mode param<commit_after><?php

namespace jonasarts\MailgatewaySyncBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

use jonasarts\MailgatewaySyncBundle\Entity\Domain;

class DefaultController extends Controller
{
    private function removeExpiredDomains($config)
    {
        $msg = "<h1>Delete</h1>\n";

        $default_em = $this->get('doctrine')->getManager();

        $d = new \DateTime("-1 hour");

        // expired
        $domains = $default_em->createQuery(
            'SELECT d 
            FROM jonasartsMailgatewaySyncBundle:Domain d 
            WHERE d.last_sync < :lastsync 
            ORDER BY d.domainname ASC'
            )
            ->setParameter('lastsync', $d->format('Y-m-d H:i:s'))
            ->getResult();;

        if ($domains) {
            $msg .= "Count = ". count($domains) ."<br><br>\n";

            foreach ($domains as $domain) {
                //var_dump($domain);

                // "DELETE FROM `postfix_relay_domain` WHERE `id` = ".$lo_row->id.";<br />\n";
                // "DELETE FROM `postfix_transport` WHERE `id` = ".$lo_row->id.";<br />\n";
                // "DELETE FROM `sync` WHERE `id` = ".$lo_row->id.";<br />\n";

                $msg .= "Delete domain (" . $domain->getId() . ") " . $domain->getDomainname() . " expired at " . $domain->getLastSync()->format('Y-m-d H:i:s') . "<br>\n";

                if ($config['remove']) {
                    $default_em->remove($domain);
                    $default_em->flush();
                }
            }
        }

        return $msg;
    }

    private function syncDomains($config)
    {
        $msg = "<h1>Insert & Update</h1>\n";

        $default_em = $this->get('doctrine')->getManager();
        $master_em = $this->get('doctrine')->getManager('master');

        /*
        $query = "SELECT cmo.id, cmo.type, cmo.obj_name, ps.name, cms.name ".
            "FROM cmail_object AS cmo ".
            "INNER JOIN plesk_server AS ps ON cmo.plesk_server_id = ps.id ".
            "INNER JOIN plesk_server AS cms ON cmo.cmail_server_id = cms.id ".
            "WHERE cmo.type <> 'client' ".
            "ORDER BY cmo.id";

        $query = "SELECT `sync`.*, `postfix_relay_domain`.`domain` ".
            "FROM `sync` ".
            "LEFT JOIN `postfix_relay_domain` ON `sync`.`id` = `postfix_relay_domain`.`id` ".
            "WHERE `lastrun` < %s ".
            "ORDER BY `sync`.`master_id`";
        */

        if ($config['mode'] == 'ppa') { // ppa mailgateway
            $query = "SELECT domains.id, domains.name ".
                "FROM domains ".
                "INNER JOIN dns_recs ON domains.dns_zone_id = dns_recs.dns_zone_id ".
                "WHERE dns_recs.type = 'MX' AND dns_recs.val = :mailgateway ".
                "GROUP BY domains.id ".
                "ORDER BY domains.name ASC";
        } else { // custom mailgateway
            $query = "SELECT domains.id, domains.name, domains.destination ".
                "FROM domains ".
                "ORDER BY domains.name ASC";
        }

        // query master server
        $connection = $master_em->getConnection();
        $statement = $connection->prepare($query);
        if ($config['mode'] == 'ppa') {
            $statement->bindValue('mailgateway', $config['gateway_server'].'.');
        }
        $result = $statement->execute();

        //echo "Query = ". $query ."<br>";

        if ($result) {
            $records = $statement->fetchAll();
            
            $msg .= "Count = ". count($records) ."<br><br>\n";
            
            foreach ($records as $record) {
                //var_dump($record);

                $domain = $default_em->createQuery(
                    'SELECT d 
                    FROM jonasartsMailgatewaySyncBundle:Domain d
                    WHERE d.master_id = :id'
                    )
                    ->setParameter('id', $record['id'])
                    ->getOneOrNullResult();

                // load checksum
                // $domain->getChecksum();

                // load destination server
                if ($config['mode'] == 'ppa') {
                    $record['destination'] = $config['postfix_mailsever'];
                }

                if ($domain) { // domain record found -> update

                    $csr = hash('md5', $record['name']); // remote cs
                    $csl = $domain->getChecksum(); // local cs

                    if ($csr != $csl) { // update domain.domainname & domain.destination & domain.last_sync

                        $msg .= "Update " . $record['id'] . " domain " . $record['name'] . " transport smtp:[".$record['destination']."]<br>\n";

                        // "UPDATE postfix_relay_domain SET domain = %s WHERE master_id = %d"
                        // "UPDATE postfix_transport SET domain = %s, destination = 'smtp:[%s]' WHERE master_id = %d"

                        $domain->setDomainname($record['name']);
                        $domain->setMailservername($record['destination']);
                        $domain->setLastSync(new \DateTime());
                        $default_em->persist($domain);
                        $default_em->flush();

                    } else { // only update domain.last_sync
{'fraction_non_alphanumeric': 0.15445948451696645, 'fraction_numerical': 0.0011125533098460968, 'mean_word_length': 1.6503685503685503, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 103, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2277291', 'n_tokens_mistral': 3177, 'n_tokens_neox': 2987, 'n_words': 622}