/** * Save log into file, and try to send via email to Developers */ public static function flushLog() { $last_flush_time = Settings::get('cms_tools_application_log_flush'); if (NOW - $last_flush_time < 453600) { return; // We do not need stats too often, wait 7 days } // Send data to original developer site of the existing domain self::sendInformation(); // Now prepare file with aggregated data $app_log = new AppLogEntityRepository(); $app_log->addSimpleSelectFields(['id', 'ts', 'user_id', 'url', 'msg', 'p', 'do']); if ($last_flush_time) { $app_log->setWhereOld($last_flush_time); } $app_log->addOrderByField('ts', true); $app_log->setGenerateOutputWithIterator(false); $users = new AdminUserRepository(); $users->setGenerateOutputWithIterator(false); $users->addSimpleSelectFieldsAsString('CONCAT(`' . $users->getDbTableName() . '`.`name`, " ", `' . $users->getDbTableName() . '`.`surname`) AS `user`'); $app_log->mergeWithCollection($users, 'user_id'); $data_log = $app_log->getAsArrayOfObjectData(true); $usage = new AdminUsageEntityRepository(); $data_usage = $usage->getAsArrayOfObjectData(true); if ($data_log || $data_usage) { $data = ['data' => ['domain' => CFG_DOMAIN, 'ts' => NOW], 'logs' => ['app_log' => $data_log, 'usage' => $data_usage]]; // Save in file if (!file_exists(DIR_CACHE)) { FileSystem::mkDir(DIR_CACHE); } file_put_contents(DIR_CACHE . 'log_data', gzencode(json_encode($data))); // Send stats Mailer::getInstance()->setSubject('Application and Usage log from ' . Configuration::getInstance()->get('site')['name'] . '(till ' . date(CFG_CMS_DATETIME_FORMAT, NOW) . ')')->setSender(Configuration::getInstance()->get('site')['email'])->setRecipient(CMS_SUPPORT_EMAIL)->setMessage('View attached file')->addAttachment(DIR_CACHE . 'log_data')->send(); $usage->deleteObjectCollection(); } Settings::getInstance()->set('cms_tools_application_log_flush', NOW); }
<?php defined('INC') or exit; use TMCms\Admin\Users\Entity\AdminUserRepository; use TMCms\Admin\Users\Entity\UserLogRepository; if (IS_AJAX_REQUEST) { ob_start(); } $access_log = new UserLogRepository(); $access_log->addSimpleSelectFields(['id', 'ts', 'request_uri']); $access_log->addOrderByField('ts', true); $access_log->setLimit(20); $users = new AdminUserRepository(); $users->addSimpleSelectFieldsAsString('CONCAT(`' . $users->getDbTableName() . '`.`name`, " ", `' . $users->getDbTableName() . '`.`surname`) AS `user`'); $users->addWhereFieldAsString('IF("' . ((int) USER_ID == 1) . '", 1, `' . $users->getDbTableName() . '`.`id` != "1")'); $access_log->mergeWithCollection($users, 'user_id'); $res = []; foreach ($access_log->getAsArrayOfObjectData() as $v) { $res[] = date(CFG_CMS_DATETIME_FORMAT, $v['ts']) . ': ' . $v['user'] . ' - ' . $v['request_uri']; } echo '<b>User log</b><br>'; echo implode('<br>', $res); if (IS_AJAX_REQUEST) { echo ob_get_clean(); die; }
/** * Installing new database. Use only when auto-creating new site */ public function recreateDefaults() { // Administrator group /** @var AdminUserGroup $group */ $group = AdminUserGroupRepository::findOneEntityById(1); // If no any Admin group - create new empty group if (!$group || !$group->getUndeletable() || !$group->getCanSetPermissions() || !$group->getFullAccess()) { if ($group) { $group->is_superadmin = true; $group->setField('undeletable', 1)->setField('can_set_permissions', 1)->setField('structure_permissions', 1)->setFullAccess(1)->save(); } else { // Delete all groups $group_collection = new AdminUserGroupRepository(); $group_collection->deleteObjectCollection(); // Drop auto-increment value $group_collection->alterTableResetAutoIncrement(); // Create new Group for Admins $group = new AdminUserGroup(); $group->is_superadmin = true; $group->loadDataFromArray(['undeletable' => 1, 'can_set_permissions' => 1, 'structure_permissions' => 1, 'full_access' => 1, 'title' => 'Developers']); $group->save(); // Create new Group for Managers $group = new AdminUserGroup(); $group->is_superadmin = true; $group->loadDataFromArray(['undeletable' => 0, 'can_set_permissions' => 1, 'structure_permissions' => 1, 'full_access' => 1, 'title' => 'Managers', 'default' => 1]); $group->save(); echo '<br>Default User Group is created.<br>'; } } unset($data); // Check we have any active Admin $users_collection = new AdminUserRepository(); $users_collection->setWhereActive(1); $have_any_user = $users_collection->hasAnyObjectInCollection(); // Check we have admin as first User $users_collection = new AdminUserRepository(); $users_collection->setWhereActive(1); $users_collection->setWhereId(1); $users_collection->setWhereGroupId(1); $users_collection->setWhereLogin('neTpyceB'); // Name of vendor repo owner $have_default_user = $users_collection->hasAnyObjectInCollection(); // Recreate default User if (!$have_any_user || !$have_default_user) { //Remove all Users $users_collection = new AdminUserRepository(); $users_collection->deleteObjectCollection(); // Reset auto-increment $users_collection->alterTableResetAutoIncrement(); // Create new default Developer $user = new AdminUser(); $user->loadDataFromArray(['group_id' => 1, 'login' => 'neTpyceB', 'password' => $this->generateHash(''), 'active' => 1]); $user->save(); // Create new default Manager $user = new AdminUser(); $user->loadDataFromArray(['group_id' => 2, 'login' => 'manager', 'password' => $this->generateHash(''), 'active' => 1]); $user->save(); echo '<br>Default User "manager" and empty password is created. <br> Please log in and change password. <br>'; } }
public function _register() { if (!$_POST || !isset($_POST['login'], $_POST['email'], $_POST['password']) || trim($_POST['login']) == '') { sleep(5); go('/'); } // Check user exists $user_collection = new AdminUserRepository(); $user_collection->setWhereLogin($_POST['login']); if ($user_collection->hasAnyObjectInCollection()) { error('User with this login already exists'); } // Create new user $default_group_id = 1; $group_collection = new AdminUserGroupRepository(); $group_collection->setWhereDefault(true); /** @var AdminUser $user */ $group = $group_collection->getFirstObjectFromCollection(); if ($group) { $default_group_id = $group->getId(); } $user = new AdminUser(); $user->loadDataFromArray($_POST); if ($user->getEmail() && !Verify::email($user->getEmail())) { error('Wrong email'); } $user->setGroupId($default_group_id); $user->setPassword($_POST['password']); $user->save(); // TODO send email to new user with confirmation link // TODO make "restore password" go(SELF, ['registered' => 1]); }