/** * Run the database seeds. * * @return void */ public function run() { try { echo " - Seeding\n"; LaravelModel::unguard(); $entities = zbase_config_get('entity', []); if (!empty($entities)) { foreach ($entities as $entityName => $entity) { $enable = zbase_data_get($entity, 'enable', false); if (!empty($enable)) { $model = zbase_data_get($entity, 'model', null); $modelName = zbase_class_name($model); if (method_exists($modelName, 'seedingEventPre')) { echo " -- " . (!empty($modelName) ? $modelName . ' - ' : '') . $entityName . " - PreSeeding Event\n"; $modelName::seedingEventPre($entity); } } } foreach ($entities as $entityName => $entity) { $enable = zbase_data_get($entity, 'enable', false); if (!empty($enable)) { $this->_defaults($entityName, $entity); } } foreach ($entities as $entityName => $entity) { $enable = zbase_data_get($entity, 'enable', false); if (!empty($enable)) { $this->_factory($entityName, $entity); } } foreach ($entities as $entityName => $entity) { $enable = zbase_data_get($entity, 'enable', false); if (!empty($enable)) { $modelName = zbase_data_get($entity, 'seeder.model', null); if (!empty($modelName)) { echo " -- " . (!empty($modelName) ? $modelName . ' - ' : '') . $entityName . " - Model Seeder\n"; $this->call($modelName); } } } foreach ($entities as $entityName => $entity) { $enable = zbase_data_get($entity, 'enable', false); if (!empty($enable)) { $model = zbase_data_get($entity, 'model', null); $isPost = zbase_data_get($entity, 'post', false); $postModel = null; $modelName = zbase_class_name($model); if (!empty($isPost)) { $postModel = zbase_object_factory($modelName); if ($postModel instanceof \Zbase\Post\PostInterface) { $postModel->postTableSeeder($entity); } } else { if (method_exists($modelName, 'seedingEventPost')) { echo " -- " . (!empty($modelName) ? $modelName . ' - ' : '') . $entityName . " - PostSeeding Event\n"; $modelName::seedingEventPost($entity); } } } } } LaravelModel::reguard(); } catch (\Zbase\Exceptions\RuntimeException $e) { zbase_exception_throw($e); } }
/** * Delete/Remove File * @param object|array $file * * return boolean */ public function postFileDelete($file) { if (method_exists($this, 'fileDelete')) { return $this->fileDelete($file); } try { $file = (object) $file; if (file_exists($this->postFileUploadFolder() . $file->filename)) { zbase_db_transaction_start(); $files = $this->postFiles(); $i = 0; foreach ($files as $f) { if ($f['filename'] == $file->filename) { unset($files[$i]); unlink($this->postFileUploadFolder() . $file->filename); break; } $i++; } $this->postSetOption('_files', $files); $this->save(); zbase()->json()->addVariable('post_file_deleted', 1); zbase_db_transaction_commit(); return true; } } catch (\Zbase\Exceptions\RuntimeException $e) { zbase_db_transaction_rollback(); zbase_exception_throw($e); return false; } }
/** * Handle a login request to the application. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function postLogin(Request $request) { try { if (!$this->authEnabled()) { return $this->notfound('User authentication is disabled.'); } $rules = [$this->loginUsername() => 'required', 'password' => 'required']; $messages = []; $this->validate($request, $rules, $messages); // If the class is using the ThrottlesLogins trait, we can automatically throttle // the login attempts for this application. We'll key this by the username and // the IP address of the client making these requests into this application. $throttles = $this->isUsingThrottlesLoginsTrait(); if ($throttles && $this->hasTooManyLoginAttempts($request)) { if (zbase_is_json()) { zbase()->json()->setVariable('login_lock', 1); } return $this->sendLockoutResponse($request); } $credentials = $this->getCredentials($request); if (\Auth::attempt($credentials, $request->has('remember'))) { if (\Auth::guard($this->getGuard())->user()->isAdmin()) { $this->redirectTo = zbase_url_from_route('admin'); if (zbase_is_json()) { zbase()->json()->setVariable('_redirect', zbase_url_from_route('admin')); } return $this->handleUserWasAuthenticated($request, $throttles); } if (zbase_route_username()) { $user = \Auth::guard($this->getGuard())->user(); $usernameRoutePrefix = zbase_route_username_prefix(); $this->redirectTo = zbase_url_from_route('home', [$usernameRoutePrefix => $user->username()]); } else { $this->redirectTo = zbase_url_from_route('home'); } if (!empty($redirect)) { $this->redirectTo = $redirect; } return $this->handleUserWasAuthenticated($request, $throttles); } if ($throttles) { $this->incrementLoginAttempts($request); } $this->message('error', $this->getFailedLoginMessage()); return redirect($this->loginPath())->withInput($request->only($this->loginUsername(), 'remember'))->withErrors([$this->loginUsername() => $this->getFailedLoginMessage()]); } catch (\Zbase\Exceptions\RuntimeException $e) { zbase_exception_throw($e); return $this->error(); } }
/** * Verify email address * @param string $code * @return boolean */ public function verifyEmailAddress($code) { try { $verificationCode = $this->getDataOption('email_verification_code', null); if (!is_null($code) && $code == $verificationCode) { $oldEmails = $this->getDataOption('email_old'); if (is_array($oldEmails)) { $i = 0; foreach ($oldEmails as $e) { if ($e['new'] == $this->email()) { $e['verify'] = zbase_date_now(); $e['verify_ip'] = zbase_ip(); $oldEmails[$i] = $e; } $i++; } } if (!empty($oldEmails)) { $this->setDataOption('email_old', $oldEmails); } $this->unsetDataOption('email_verification_code'); $this->email_verified = 1; $this->email_verified_at = zbase_date_now(); $this->log('user::verifyEmailAddress'); $this->save(); zbase_alert('info', _zt('Your email address <strong>%email%<strong> is now verified.', ['%email%' => $this->email()])); zbase_session_flash('user_verifyEmailAddress', true); return true; } } catch (\Zbase\Exceptions\RuntimeException $e) { zbase_exception_throw($e); } return false; }
/** * Send * @param string $url * @return type */ public function tg($url) { try { if (zbase_is_dev()) { zbase_alert('info', 'TG CALL: ' . $url); } $opts = array('http' => array('method' => "GET", 'header' => "Content-Type: application/x-www-form-urlencoded; charset: UTF-8")); $context = stream_context_create($opts); return file_get_contents($url, false, $context); } catch (\Zbase\Exceptions\RuntimeException $e) { zbase_exception_throw($e); } }