public function index(string $_mode, $_models, RouteEx $_route, array $_parents, string $alias) { if ($_mode === 'delete') { if ($parent = $this->findParentByAlias($_parents, $alias)) { $cascade = $_route->getDeleteCascades(); $cascades = $cascade[$parent['alias']]; /** @var ModelEx $model */ foreach ($_models as $model) { $pk = $model->getKeyName(); $pkValue = $model->{$pk}; if (!empty($cascades)) { foreach ((array) $cascades as $child) { if ($child = $this->findParentByAlias($_parents, $child)) { $name = $child['name']; if ($childModel = $this->resolver->getModel($name)) { $records = $childModel::where($pk, '=', $pkValue)->get(); if ($recursive = $cascade[$child['alias']] ?? null) { $this->index($_mode, $records, $_route, $_parents, $child['alias']); } /** @var ModelEx $record */ foreach ($records as $record) { $record->delete(); } } else { throw new ModelError("Cannot find child model '{$name}' for delete cascade"); } } else { throw new ModelError("Child model '{$child}' not defined"); } } } if ($model->delete()) { $items[] = ['pk' => $pkValue]; } } } } else { /** @var ModelEx $model */ foreach ($_models as $model) { $this->modelAutoFill->fillMissing($model); $model->save(); $items[] = $model->toArray(); } } return json_encode(['items' => $items ?? []]); }
public function signup(UserSignupEvent $event) { $signup = []; $fields = ['ident', 'email', 'contact_email', 'password', 'first_name', 'last_name', 'photo_url', 'tz_offset', 'ip_addr', 'http_referrer', 'http_campaign', 'verified']; $default = function ($field) use($event) { if ($field == 'photo_url' && ($email = $event->email)) { $gravatar = sprintf("http://www.gravatar.com/avatar/%s?d=404", md5(strtolower($email))); if ($headers = get_headers($gravatar, 1)) { if (strpos($headers[0], '200')) { return $gravatar; } } } elseif ($field == 'contact_email') { return $event->email ?? null; } elseif ($field == 'password') { return bin2hex(openssl_random_pseudo_bytes(4)); } elseif ($field == 'http_referrer') { $value = $_COOKIE[Router::HTTP_REFERRER_COOKIE] ?? '/'; return $value !== '/' ? $value : null; } elseif ($field == 'http_campaign') { return $_COOKIE[Router::HTTP_CAMPAIGN_COOKIE] ?? null; } elseif ($field == 'tz_offset') { return $_COOKIE['tz_offset'] ?? 240; } elseif ($field == 'verified') { return 'false'; } return null; }; foreach ($fields as $field) { $signup[$field] = $event->{$field} ?? $default($field); if ($field == 'password') { $signup[$field] = password_hash($signup[$field], PASSWORD_DEFAULT); } } if (filter_var($signup['email'] ?? null, FILTER_VALIDATE_EMAIL) || !empty($signup['ident'])) { try { User::unguard(); $user = new User($signup); $this->modelAutoFill->fillMissing($user); if ($user->save()) { $event->setUser($user); $signupEvent = (new UserSignupEvent($signup))->setUser($user); try { $this->dispatcher->fire(UserSignupEvent::USER_SIGNUP_COMPLETE, $signupEvent); } catch (\Throwable $e) { $this->logger->warn("Plugin error during signup: " . $e->getMessage()); } } } catch (QueryException $e) { $event->setError("EMAIL_IN_USE"); } catch (\Exception $e) { $event->setError($e->getMessage()); } } else { $event->setError('INVALID_DATA'); } if (!$event->getUser()) { //for logging $this->dispatcher->fire(UserSignupEvent::USER_SIGNUP_FAIL, $event); } }