Untitled

mail@pastecode.io avatar
unknown
php
a year ago
17 kB
3
Indexable
Never
<?php
require_once(JPATH_COMPONENT_ADMINISTRATOR . '/helpers/SNLog.php');
require_once(JPATH_COMPONENT . '/helpers/SNCronExpression.php');

use Joomla\CMS\Component\ComponentHelper;
use Sentry\SentrySdk;
use Sentry\Event;
use Sentry\CheckIn;
use Sentry\CheckInStatus;

\Sentry\init(
    [
        'dsn' => 'https://48c25f7955ee487492496f7ffcc6fab9@o4504632987877376.ingest.sentry.io/4504633230622720',
        'environment' => ComponentHelper::getParams('com_snv2')->get('test_mode') ? 'development' : 'production'
    ]
);

class dataAnonymizationCli extends JApplicationCli
{
    private function getLang(): string
    {
        return strtolower(ComponentHelper::getParams('com_languages')->get('site'));
    }

    public function doExecute()
    {
        $checkIn = new CheckIn(self::getLang(), CheckInStatus::inProgress());
        try {
            // 🟡 Notify Sentry your job is running:
            $event = Event::createCheckIn();
            $event->setCheckIn($checkIn);
            SentrySdk::getCurrentHub()->captureEvent($event);

            $app = JFactory::getApplication('site');
            $debug = ($this->input->get('debug') ? true : false);
            define('JDEBUG', $debug ? 1 : 0);
            $site_language = JComponentHelper::getParams('com_languages')->get('site');
            JFactory::getLanguage()->load('com_snv2', JPATH_SITE, $site_language);
            JFactory::getLanguage()->load('plg_user_snprofile', JPATH_ADMINISTRATOR, $site_language);
            $params = JComponentHelper::getParams('com_snv2');
            $dbPrefix = $app->get('dbprefix', 'jos_');

            $db = JFactory::getDbo();
            $jobid = $this->input->get('snjobid', 999);
            $runid = $this->input->get('snrunid', date("YmdHis"));


            // PL ONLY !
            if ($site_language === 'pl-PL') {
                // transaction || shipping_email || welcome --> INTERVAL 2 YEARS
                $querySelect = $db->getQuery(true);
                $querySelect
                    ->select('id')
                    ->from($db->qn('#__sn_emails'))
                    ->where($db->qn('date_created') . ' <= DATE_SUB(now(), INTERVAL 2 YEAR)')
                    ->where($db->qn('reason') . ' IN (\'transaction\', \'shipping_email\', \'welcome\')');
                $results = $db->setQuery($querySelect)->loadColumn();

                $query = $db->getQuery(true);
                $query
                    ->delete($db->qn('#__sn_emails'))
                    ->where($db->qn('date_created') . ' <= DATE_SUB(now(), INTERVAL 2 YEAR)')
                    ->where($db->qn('reason') . ' IN (\'transaction\', \'shipping_email\', \'welcome\')');
                $db->transactionStart();
                $transaction = $db->execute();
                if ($transaction) {
                    $skip = false;
                } else {
                    $db->transactionRollback();
                    $skip = true;
                    SNCronLogger::log2db(getmypid(), basename(__FILE__), __CLASS__, 'error', $db->stderr(true));
                }
                $query->clear();

                if (!$skip && count($results) > 0) {
                    $query = $db->getQuery(true);
                    $query
                        ->delete($db->qn('#__sn_emails_body'))
                        ->where($db->qn('date_created') . ' <= DATE_SUB(now(), INTERVAL 2 YEAR)')
                        ->where($db->qn('email_id') . ' IN (' . implode(',', array_map('intval', $results)) . ')');
                    $db->setQuery($query);
                    $transactionBody = $db->execute();
                    if ($transactionBody) {
                        $db->transactionCommit();
                        $skip = false;
                    } else {
                        $db->transactionRollback();
                        SNCronLogger::log2db(getmypid(), basename(__FILE__), __CLASS__, 'error', $db->stderr(true));
                    }
                }

                // passwd_reset || vista_ebb_reminder || magiclink || contact_request --> INTERVAL 90 DAYS
                $querySelect = $db->getQuery(true);
                $querySelect
                    ->select('id')
                    ->from($db->qn('#__sn_emails'))
                    ->where($db->qn('date_created') . ' <= DATE_SUB(now(), INTERVAL 90 DAY)')
                    ->where($db->qn('reason') . ' IN (\'passwd_reset\', \'vista_ebb_reminder\', \'magiclink\', \'contact_request\')');
                $results = $db->setQuery($querySelect)->loadColumn();

                $query = $db->getQuery(true);
                $query
                    ->delete($db->qn('#__sn_emails'))
                    ->where($db->qn('date_created') . ' <= DATE_SUB(now(), INTERVAL 90 DAY)')
                    ->where($db->qn('reason') . ' IN (\'passwd_reset\', \'vista_ebb_reminder\', \'magiclink\', \'contact_request\')');
                $db->transactionStart();
                $transaction = $db->execute();
                if ($transaction) {
                    $skip = false;
                } else {
                    $db->transactionRollback();
                    $skip = true;
                    SNCronLogger::log2db(getmypid(), basename(__FILE__), __CLASS__, 'error', $db->stderr(true));

                }
                $query->clear();

                if (!$skip && count($results) > 0) {
                    $query = $db->getQuery(true);
                    $query
                        ->delete($db->qn('#__sn_emails_body'))
                        ->where($db->qn('date_created') . ' <= DATE_SUB(now(), INTERVAL 90 DAY)')
                        ->where($db->qn('email_id') . ' IN (' . implode(',', array_map('intval', $results)) . ')');
                    $db->setQuery($query);
                    $transactionBody = $db->execute();
                    if ($transactionBody) {
                        $db->transactionCommit();
                        $skip = false;
                    } else {
                        $db->transactionRollback();
                        SNCronLogger::log2db(getmypid(), basename(__FILE__), __CLASS__, 'error', $db->stderr(true));
                    }
                }
            }


            // jos_sn_baskets - INTERVAL 14 DAY
            $query = $db->getQuery(true);
            $query
                ->delete($db->qn('#__sn_baskets'))
                ->where($db->qn('sysdate') . ' <= DATE_SUB(now(), INTERVAL 14 DAY)');
            $db->setQuery($query);
            $jos_sn_baskets = $db->execute();
            if (!$jos_sn_baskets) {
                SNCronLogger::log2db(getmypid(), basename(__FILE__), __CLASS__, 'error', $query->__toString());
            }


            // jos_sn_orders and jos_sn_orders_items - INTERVAL 5 YEAR
            $query = $db->getQuery(true);
            $db->transactionStart();
            $query = "
				DELETE
					o, oi
				FROM
					jos_sn_orders AS o
				LEFT JOIN
					jos_sn_orders_items AS oi
				ON
					oi.order_id = o.id
				WHERE
					o.order_date <= DATE_SUB(now(), INTERVAL 5 YEAR)
			";
            $db->setQuery($query);
            $jos_sn_orders = $db->execute();
            if (!$jos_sn_orders) {
                $db->transactionRollback();
                SNCronLogger::log2db(getmypid(), basename(__FILE__), __CLASS__, 'error', $query->__toString());

            }
            $db->transactionCommit();


            // jos_sn_emails - INTERVAL 5 YEAR
            $db->transactionStart();
            $skip = false;

            $query = $db->getQuery(true);
            $query
                ->delete($db->qn('#__sn_emails_body'))
                ->where($db->qn('date_created') . ' <= DATE_SUB(now(), INTERVAL 5 YEAR)');
            $db->setQuery($query);
            $jos_sn_emails = $db->execute();
            if (!$jos_sn_emails) {
                $db->transactionRollback();
                $skip = true;
                SNCronLogger::log2db(getmypid(), basename(__FILE__), __CLASS__, 'error', $db->stderr(true));

            }
            $query->clear();

            if (!$skip) {
                $query = $db->getQuery(true);
                $query
                    ->delete($db->qn('#__sn_emails'))
                    ->where($db->qn('date_created') . ' <= DATE_SUB(now(), INTERVAL 5 YEAR)');
                $db->setQuery($query);
                $jos_sn_emails = $db->execute();
                if ($jos_sn_emails) {
                    $db->transactionCommit();
                    $skip = false;
                } else {
                    $db->transactionRollback();
                    SNCronLogger::log2db(getmypid(), basename(__FILE__), __CLASS__, 'error', $db->stderr(true));
                }
            }


            // jos_sn_log - INTERVAL 12 MONTH
            $query = $db->getQuery(true);
            $query
                ->delete($db->qn('#__sn_log'))
                ->where($db->qn('sysdate') . ' <= DATE_SUB(now(), INTERVAL 12 MONTH)');
            $db->setQuery($query);
            $jos_sn_log = $db->execute();
            if (!$jos_sn_log) {
                SNCronLogger::log2db(getmypid(), basename(__FILE__), __CLASS__, 'error', $query->__toString());
            }


            // jos_sn_sessions and jos_sn_sessions_fields and jos_sn_sessions_requests - INTERVAL 6 MONTH
            $interval = $site_language === 'uk-UK' ? 3 : 6;
            $query = $db->getQuery(true);
            $db->transactionStart();
            $query = "
				DELETE
					s, sr, sf
				FROM
					jos_sn_sessions AS s
				LEFT JOIN
					jos_sn_sessions_requests AS sr
				ON
					sr.session_id = s.id
				LEFT JOIN
					jos_sn_sessions_fields AS sf
				ON
					sf.request_id = sr.id
				WHERE
					s.sysdate <= DATE_SUB(now(), INTERVAL ".$interval." MONTH)
			";
            $db->setQuery($query);
            $jos_sn_sessions = $db->execute();
            if (!$jos_sn_sessions) {
                $db->transactionRollback();
                SNCronLogger::log2db(getmypid(), basename(__FILE__), __CLASS__, 'error', $query->__toString());
            }
            $db->transactionCommit();


            // jos_sn_abandonbasket - INTERVAL 90 DAY
            $query = $db->getQuery(true);
            $query
                ->delete($db->qn('#__sn_abandonbasket'))
                ->where($db->qn('date_created') . ' <= DATE_SUB(now(), INTERVAL 90 DAY)');
            $db->setQuery($query);
            $jos_sn_abandonbasket = $db->execute();
            if (!$jos_sn_abandonbasket) {
                SNCronLogger::log2db(getmypid(), basename(__FILE__), __CLASS__, 'error', $query->__toString());
            }


            // jos_sn_abandonpayment - INTERVAL 90 DAY, 2 years for PL
            $interval = $site_language === 'pl-PL' ? '2 YEAR' : '90 DAY';
            $query = $db->getQuery(true);
            $query
                ->delete($db->qn('#__sn_abandonpayment'))
                ->where($db->qn('date_created') . ' <= DATE_SUB(now(), INTERVAL ' . $interval . ')');
            $db->setQuery($query);
            $jos_sn_abandonpayment = $db->execute();
            if (!$jos_sn_abandonpayment) {
                SNCronLogger::log2db(getmypid(), basename(__FILE__), __CLASS__, 'error', $query->__toString());
            }


            // jos_sn_leads - INTERVAL 12 MONTH
            $query = $db->getQuery(true);
            $query
                ->delete($db->qn('#__sn_leads'))
                ->where($db->qn('date_created') . ' <= DATE_SUB(now(), INTERVAL 12 MONTH)');
            $db->setQuery($query);
            $jos_sn_leads = $db->execute();
            if (!$jos_sn_leads) {
                SNCronLogger::log2db(getmypid(), basename(__FILE__), __CLASS__, 'error', $query->__toString());
            }


            // jos_sn_purchase_list - INTERVAL 12 MONTH
            $query = $db->getQuery(true);
            $query
                ->delete($db->qn('#__sn_purchase_list'))
                ->where($db->qn('date_created') . ' <= DATE_SUB(now(), INTERVAL 12 MONTH)');
            $db->setQuery($query);
            $jos_sn_purchase_list = $db->execute();
            if (!$jos_sn_purchase_list) {
                SNCronLogger::log2db(getmypid(), basename(__FILE__), __CLASS__, 'error', $query->__toString());
            }


            // jos_sn_unsubscribers - INTERVAL 12 MONTH
            $query = $db->getQuery(true);
            $query
                ->delete($db->qn('#__sn_unsubscribers'))
                ->where($db->qn('date') . ' <= DATE_SUB(now(), INTERVAL 12 MONTH)');
            $db->setQuery($query);
            $jos_sn_unsubscribers = $db->execute();
            if (!$jos_sn_unsubscribers) {
                SNCronLogger::log2db(getmypid(), basename(__FILE__), __CLASS__, 'error', $query->__toString());
            }


            // jos_sn_vista_leads - INTERVAL 12 MONTH
            $query = $db->getQuery(true);
            $query
                ->delete($db->qn('#__sn_vista_leads'))
                ->where($db->qn('date_created') . ' <= DATE_SUB(now(), INTERVAL 12 MONTH)');
            $db->setQuery($query);
            $jos_sn_vista_leads = $db->execute();
            if (!$jos_sn_vista_leads) {
                SNCronLogger::log2db(getmypid(), basename(__FILE__), __CLASS__, 'error', $query->__toString());
            }


            // jos_sn_votes - INTERVAL 12 MONTH
            $query = $db->getQuery(true);
            $query
                ->delete($db->qn('#__sn_votes'))
                ->where($db->qn('date_created') . ' <= DATE_SUB(now(), INTERVAL 12 MONTH)');
            $db->setQuery($query);
            $jos_sn_votes = $db->execute();
            if (!$jos_sn_votes) {
                SNCronLogger::log2db(getmypid(), basename(__FILE__), __CLASS__, 'error', $query->__toString());
            }


            // jos_sn_waiting_list - INTERVAL 12 MONTH
            $query = $db->getQuery(true);
            $query
                ->delete($db->qn('#__sn_waiting_list'))
                ->where($db->qn('create_date') . ' <= DATE_SUB(now(), INTERVAL 12 MONTH)');
            $db->setQuery($query);
            $jos_sn_waiting_list = $db->execute();
            if (!$jos_sn_waiting_list) {
                SNCronLogger::log2db(getmypid(), basename(__FILE__), __CLASS__, 'error', $query->__toString());
            }


            // jos_sn_vista_baskets - INTERVAL 14 DAY
            $query = $db->getQuery(true);
            $query
                ->delete($db->qn('#__sn_vista_baskets'))
                ->where($db->qn('sysdate') . ' <= DATE_SUB(now(), INTERVAL 14 DAY)');
            $db->setQuery($query);
            $jos_sn_vista_baskets = $db->execute();
            if (!$jos_sn_vista_baskets) {
                SNCronLogger::log2db(getmypid(), basename(__FILE__), __CLASS__, 'error', $query->__toString());
            }


            // jos_sn_jobs_log - INTERVAL 90 DAY
            $query = $db->getQuery(true);
            $query
                ->delete($db->qn('#__sn_jobs_log'))
                ->where($db->qn('sysdate') . ' <= DATE_SUB(now(), INTERVAL 90 DAY)');
            $db->setQuery($query);
            $jos_sn_jobs_log = $db->execute();
            if (!$jos_sn_jobs_log) {
                SNCronLogger::log2db(getmypid(), basename(__FILE__), __CLASS__, 'error', $query->__toString());
            }

            // 🟢 Notify Sentry your job has completed successfully:
            $event = Event::createCheckIn();
            $event->setCheckIn(new CheckIn(self::getLang(), CheckInStatus::ok(), $checkIn->getId()));
            $event->setMessage('OK -> ' . __CLASS__);
            SentrySdk::getCurrentHub()->captureEvent($event);
            SNCronLogger::log2db(getmypid(), basename(__FILE__), __CLASS__, 'success');

        } catch (SNErrorException $sne) {
            SNLog::log(basename($sne->getFile()) . ':' . $sne->getLine() . ':' . $sne->getMessage(), $sne->getTrace(), $sne->context, $sne->getCode());
            SNCronLogger::log2db(getmypid(), basename(__FILE__), __CLASS__, 'error', $sne->getMessage());
        } catch (Exception $e) {

            // 🔴 Notify Sentry your job has failed:
            $event = Event::createCheckIn();
            $event->setCheckIn(new CheckIn(self::getLang(), CheckInStatus::error(), $checkIn->getId()));
            $event->setMessage('ERROR -> ' . __CLASS__ . ': ' . $e->getMessage());
            SentrySdk::getCurrentHub()->captureEvent($event);
            SNCronLogger::log2db(getmypid(), basename(__FILE__), __CLASS__, 'error', $e->getMessage());
            SNLog::log(basename($e->getFile()) . ':' . $e->getLine() . ':' . $e->getMessage(), $e->getTrace(), get_defined_vars(), $e->getCode());
        }
    }
}

JApplicationCli::getInstance('dataAnonymizationCli')->execute();