cmsUserVars->table); } public function getLastYearUsers() { $lastYear = date('Y-m-d H:i', strtotime('-1 year')); return Database::loadResult('SELECT COUNT(*) FROM '.$this->cmsUserVars->table.' WHERE '.$this->cmsUserVars->registered.' > '.Database::escapeDB($lastYear)); } public function getUsersEmail($offset = 0, $limit = 5000, $fromCron = false, $onlyNew = false, $groups = []) { // We filter the users if we need to $filters = []; if ($onlyNew) $filters[] = $this->cmsUserVars->email.' NOT IN (SELECT email FROM #__acyc_test)'; if ($fromCron && ACYC_CMS == 'joomla') $filters[] = 'user.block = 0'; $query = 'SELECT '.$this->cmsUserVars->email.' FROM '.$this->cmsUserVars->table.' AS user'; // If we selected groups we only select users belonging to it if (!empty($groups)) { SecurityService::arrayToInteger($groups); $query .= ACYC_CMS == 'joomla' ? $this->getJoinQueryJoomla($groups) : $this->getJoinQueryWordpress($groups); } $queryWhere = empty($filters) ? '' : ' WHERE ('.implode(') AND (', $filters).')'; $query .= ' '.$queryWhere.' LIMIT '.$offset.','.$limit; return Database::loadResultArray($query); } private function getJoinQueryJoomla($groups) { return ' JOIN #__user_usergroup_map AS group_map ON group_map.user_id = user.id AND group_map.group_id IN ('.implode(',', $groups).')'; } private function getJoinQueryWordpress($groups) { return ''; } public function handleBatchCallback($emailsResults, &$allTests) { if (ACYC_CMS == 'joomla') { $this->handleBatchCallbackJoomla($emailsResults, $allTests); } else { $this->handleBatchCallbackWordpress($emailsResults, $allTests); } } private function handleBatchCallbackWordpress($emailsResults, &$allTests) { // We get the conditions selected $conditionsSelected = $this->getConditions(); if (empty($conditionsSelected)) return; // We get all the emails in the test $emails = array_column($emailsResults, 'email'); foreach ($emails as $key => $email) { $emails[$key] = Database::escapeDB($email); } // We filter with the email in the database try { $wordpressUsers = Database::loadObjectList( 'SELECT '.$this->cmsUserVars->id.' AS id, '.$this->cmsUserVars->email.' AS email FROM '.$this->cmsUserVars->table.' WHERE '.$this->cmsUserVars->email.' in ("'.implode( '","', $emails ).'")' ); } catch (\Exception $exception) { DebugService::logMessage('callback_controller.log', 'Error on getting CMS users from results - '.$exception->getMessage()); return; } $apiService = new ApiService(); $blockEmail = []; // We check if we have to block users of not foreach ($wordpressUsers as $user) { $arrayKey = array_search($user->email, array_column($emailsResults, 'email')); $emailOk = $apiService->isEmailAccepted($emailsResults[$arrayKey], $conditionsSelected); if ($emailOk === true) continue; $blockEmail[] = $user->id; // We set the reason why we block the user if (!empty($allTests)) $allTests[$user->email]['block_reason'] = Database::escapeDB($emailOk); } // We block all the users $updateQuery = 'UPDATE #__usermeta SET meta_value = "a:0:{}" WHERE user_id IN ('.implode(',', $blockEmail).')'; try { Database::query($updateQuery); } catch (\Exception $exception) { DebugService::logMessage('callback_controller.log', 'Error on request to block CMS users - '.$exception->getMessage()); } } private function handleBatchCallbackJoomla($emailsResults, &$allTests) { // We get the conditions selected $conditionsSelected = $this->getConditions(); if (empty($conditionsSelected)) { DebugService::logMessage('callback_controller.log', 'No conditions set in the configuration'); return; } // We get all the emails in the test $emails = array_column($emailsResults, 'email'); foreach ($emails as $key => $email) { $emails[$key] = Database::escapeDB($email); } // We filter with the email in the database try { $emailsInCms = Database::loadResultArray( 'SELECT '.$this->cmsUserVars->email.' FROM '.$this->cmsUserVars->table.' WHERE '.$this->cmsUserVars->email.' in ("'.implode('","', $emails).'")' ); } catch (\Exception $exception) { DebugService::logMessage('callback_controller.log', 'Error on getting CMS users from results - '.$exception->getMessage()); return; } $apiService = new ApiService(); $blockEmail = []; // We check if we have to block users of not foreach ($emailsInCms as $email) { $arrayKey = array_search($email, array_column($emailsResults, 'email')); $emailOk = $apiService->isEmailAccepted($emailsResults[$arrayKey], $conditionsSelected); if ($emailOk === true) continue; $blockEmail[] = Database::escapeDB($email); // We set the reason why we block the user if (!empty($allTests)) $allTests[$email]['block_reason'] = Database::escapeDB($emailOk); } // We block all the users $updateQuery = 'UPDATE '.$this->cmsUserVars->table.' SET block = 1 WHERE '.$this->cmsUserVars->email.' IN ('.implode(',', $blockEmail).')'; try { Database::query($updateQuery); } catch (\Exception $exception) { DebugService::logMessage('callback_controller.log', 'Error on request to block CMS users - '.$exception->getMessage()); } } private function getConditions() { // We get the conditions selected $conditionsSelected = $this->config->get('conditions_selected', []); if (empty($conditionsSelected)) return; $conditionsSelected = explode(',', $conditionsSelected); if (empty($conditionsSelected)) return; return $conditionsSelected; } }