/** * Add user in database * @return string * @throws NativeException */ public function actionAdduser() { echo "Login:"******"Email:"; $email = Console::$Input->read(); if (!Str::isEmail($email)) { throw new NativeException('Email is bad'); } echo "Password:"******"RoleId (1 = onlyread, 2 = user, 3 = moderator, 4 = admin):"; $role = (int) Console::$Input->read(); if (!Arr::in($role, [1, 2, 3, 4])) { $role = 2; } if (User::isMailExist($email) || User::isLoginExist($login)) { throw new NativeException('User with this email or login is always exist'); } $salt = Console::$Properties->get('passwordSalt'); $user = new User(); $user->login = $login; $user->email = $email; $user->password = Security::password_hash($pass, $salt); $user->role_id = $role; $user->save(); $profile = new Profile(); $profile->user_id = $user->id; $profile->save(); return 'User was successful added to database!'; }
/** * Check if use with $email is exist * @param string $email * @return bool */ public static function isMailExist($email) { if (!Obj::isString($email) || !Str::isEmail($email)) { return false; } return self::where('email', '=', $email)->count() > 0; }
/** * Filter ['object', 'email'] * @param $object * @return bool */ public static function email($object) { if (Obj::isArray($object)) { return false; } // allow empty, validate on required rule if (Str::likeEmpty($object)) { return true; } return Str::isEmail($object); }
/** * Approve user profile via $email and $token params * @param string $email * @param string $token * @throws ForbiddenException */ public function actionApprove($email, $token) { // sounds like a not valid token if (App::$User->isAuth() || Str::length($token) < 32 || !Str::isEmail($email)) { throw new ForbiddenException(); } // lets find token&email $find = App::$User->where('approve_token', '=', $token)->where('email', '=', $email); // not found? exit if ($find->count() !== 1) { throw new ForbiddenException(); } // get row and update approve information $user = $find->first(); $user->approve_token = '0'; $user->save(); // open session and redirect to main $loginModel = new FormLogin(); $loginModel->openSession($user); $this->response->redirect('/'); // session is opened, refresh page }
/** * Console installation * @return string * @throws NativeException */ public function actionInstall() { if (File::exist('/Private/Install/install.lock')) { throw new NativeException('Installation is locked! Please delete /Private/Install/install.lock'); } echo Console::$Output->writeHeader('License start'); echo File::read('/LICENSE') . PHP_EOL; echo Console::$Output->writeHeader('License end'); $config = Console::$Properties->get('database'); $newConfig = []; // creating default directory's foreach (self::$installDirs as $obj) { // looks like a directory if (!Str::contains('.', $obj)) { Directory::create($obj, 0777); } } echo Console::$Output->write('Upload and private directories are successful created!'); // set chmods echo $this->actionChmod(); // database config from input echo Console::$Output->writeHeader('Database connection configuration'); echo 'Driver(default:' . $config['driver'] . '):'; $dbDriver = Console::$Input->read(); if (Arr::in($dbDriver, ['mysql', 'pgsql', 'sqlite'])) { $newConfig['driver'] = $dbDriver; } // for sqlite its would be a path echo 'Host(default:' . $config['host'] . '):'; $dbHost = Console::$Input->read(); if (!Str::likeEmpty($dbHost)) { $newConfig['host'] = $dbHost; } echo 'Database name(default:' . $config['database'] . '):'; $dbName = Console::$Input->read(); if (!Str::likeEmpty($dbName)) { $newConfig['database'] = $dbName; } echo 'User(default:' . $config['username'] . '):'; $dbUser = Console::$Input->read(); if (!Str::likeEmpty($dbUser)) { $newConfig['username'] = $dbUser; } echo 'Password(default:' . $config['password'] . '):'; $dbPwd = Console::$Input->read(); if (!Str::likeEmpty($dbPwd)) { $newConfig['password'] = $dbPwd; } echo 'Table prefix(default:' . $config['prefix'] . '):'; $dbPrefix = Console::$Input->read(); if (!Str::likeEmpty($dbPrefix)) { $newConfig['prefix'] = $dbPrefix; } // merge configs and add new connection to db pull $dbConfigs = Arr::merge($config, $newConfig); Console::$Database->addConnection($dbConfigs, 'install'); try { Console::$Database->connection('install')->getDatabaseName(); } catch (\Exception $e) { return 'Testing database connection is failed! Run installer again and pass tested connection data! Log: ' . $e->getMessage(); } // autoload isn't work here include root . '/Apps/Controller/Console/Db.php'; // import db data $dbController = new DbController(); echo $dbController->actionImportAll('install'); // add system info about current install version $system = new System(); $system->setConnection('install'); $system->var = 'version'; $system->data = Version::VERSION; $system->save(); // set website send from email from input $emailConfig = Console::$Properties->get('adminEmail'); echo 'Website sendFrom email(default: ' . $emailConfig . '):'; $email = Console::$Input->read(); if (!Str::isEmail($email)) { $email = $emailConfig; } // set base domain echo 'Website base domain name(ex. ffcms.org):'; $baseDomain = Console::$Input->read(); if (Str::likeEmpty($baseDomain)) { $baseDomain = Console::$Properties->get('baseDomain'); } // generate other configuration data and security salt, key's and other echo Console::$Output->writeHeader('Writing configurations'); /** @var array $allCfg */ $allCfg = Console::$Properties->getAll('default'); $allCfg['database'] = $dbConfigs; $allCfg['adminEmail'] = $email; $allCfg['baseDomain'] = $baseDomain; echo Console::$Output->write('Generate password salt for BLOWFISH crypt'); $allCfg['passwordSalt'] = '$2a$07$' . Str::randomLatinNumeric(mt_rand(21, 30)) . '$'; echo Console::$Output->write('Generate security cookies for debug panel'); $allCfg['debug']['cookie']['key'] = 'fdebug_' . Str::randomLatinNumeric(mt_rand(8, 32)); $allCfg['debug']['cookie']['value'] = Str::randomLatinNumeric(mt_rand(32, 128)); // write config data $writeCfg = Console::$Properties->writeConfig('default', $allCfg); if ($writeCfg !== true) { return 'File /Private/Config/Default.php is unavailable to write data!'; } File::write('/Private/Install/install.lock', 'Install is locked'); return 'Configuration done! FFCMS 3 is successful installed! Visit your website. You can add administrator using command php console.php db/adduser'; }