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) { // We get the conditions selected $conditionsSelected = $this->getConditions(); if (empty($conditionsSelected)) { DebugService::logMessage('callback_controller.log', 'No conditions set in the configuration for CMS users'); return; } // We get all the emails in the test $rawEmails = array_column($emailsResults, ApiService::API_TEST_KEY_EMAIL); if (empty($rawEmails)) return; $emails = []; foreach ($rawEmails as $key => $email) { $emails[$key] = Database::escapeDB($email); } // We filter with the email in the database try { $cmsUsers = 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(); $blockUserClass = new BlockUserClass(); $blockUserClass->block_action = BlockUserClass::BLOCK_ACTION_BATCH; $blockIds = []; // We check if we have to block users of not foreach ($cmsUsers as $user) { $arrayKey = array_search($user->email, $rawEmails); $emailOk = $apiService->isEmailAccepted($emailsResults[$arrayKey], $conditionsSelected); if ($emailOk === true) continue; $blockIds[] = $user->id; // We set the reason why we block the user if (!empty($allTests)) { $blockedEmail = new \stdClass(); $blockedEmail->email = $user->email; $blockedEmail->block_reason = $emailOk; $blockUserClass->save($blockedEmail); $allTests[$user->email]['block_reason'] = Database::escapeDB($emailOk); } } // We block all the users if (empty($blockIds)) return; if (ACYC_CMS == 'joomla') { $blockQuery = 'UPDATE '.$this->cmsUserVars->table.' SET block = 1 WHERE '.$this->cmsUserVars->id.' IN ('.implode(',', $blockIds).')'; } else { $blockQuery = 'UPDATE #__usermeta SET meta_value = "a:0:{}" WHERE meta_key = "#__capabilities" AND user_id IN ('.implode(',', $blockIds).')'; } try { Database::query($blockQuery); } 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; } }