in the header * @param mixed $page_title * @return string */ function get_title_website(string $page_title) { return $page_title . ' - FridgeBites'; } /** * Get current user id and username. If user is not authorized - false. * @return UserModel|false */ function get_auth_user(): bool|UserModel { if (isset($_SESSION['user_id'])) { $user = UserModel::get([ [ 'name' => 'obj.id', 'type' => '=', 'value' => $_SESSION['user_id'] ], [ 'name' => 'obj.is_active', 'type' => '=', 'value' => 1 ], [ 'name' => 'is_banned', 'type' => '=', 'value' => false, 'is_having' => true ] ]); if (empty($user)) return false; return $user; } else return false; } /** * Set current user * @param int $user_id */ function set_auth_user(int $user_id) { $_SESSION['user_id'] = $user_id; } /** * Destroy all session variables (and with current user) */ function logout() { $_SESSION['user_id'] = null; } function generate_uuid() { return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', mt_rand(0, 0xffff), mt_rand(0, 0xffff), // 32 bit mt_rand(0, 0xffff), // 16 bit mt_rand(0, 0x0fff) | 0x4000, // Version 4 mt_rand(0, 0x3fff) | 0x8000, // Version 1 mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff) // 48 bit ); } use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; function send_email(string $subject, string $body, string $altBody, string $to_address, string $to_name) { $mail = new PHPMailer(true); $mail->isSMTP(); $mail->Host = EMAIL_SETTINGS['host']; $mail->SMTPAuth = EMAIL_SETTINGS['smtp_auth']; $mail->Username = EMAIL_SETTINGS['username']; $mail->Password = EMAIL_SETTINGS['password']; $mail->SMTPSecure = EMAIL_SETTINGS['smtp_secure']; $mail->Port = EMAIL_SETTINGS['port']; $mail->setFrom(EMAIL_SETTINGS['username'], EMAIL_SETTINGS['from_title']); $mail->addAddress($to_address, $to_name); $mail->isHTML(true); $mail->Subject = $subject; $mail->Body = $body; $mail->AltBody = $altBody; $mail->send(); } /** * Collects related objects for each origin object based on a given foreign key. * * Returns an array in the following format: * [ * [ * 'origin' => $object, // the origin object * 'relations' => $objects // related objects from the target model * ], * ... * ] * * @param array $objects Array of origin objects (their IDs will be used for matching) * @param string $model_name_format Target model name in "app:model" format to fetch related objects from * @param string $field_key Foreign key in the related model that refers to the origin object ID * @return array An array of relation mappings between origin and related objects */ function prefetch_related(array $objects, string $model_name_format, string $field_key) { [$app, $model] = explode(':', $model_name_format); $model_name = "Lycoreco\Apps\\" . $app . "\Models\\" . $model; if (!class_exists($model_name)) { throw new InvalidArgumentException("Model class $model_name does not exist."); } $keys = $keys = array_map(fn($obj) => $obj->get_id(), $objects); if (empty($keys)) return []; $related_objects = $model_name::filter(array( [ 'name' => 'obj.' . $field_key, 'type' => 'IN', 'value' => $keys ] )); return array_map(function ($object) use ($related_objects, $field_key) { $rels = array_filter($related_objects, function ($rel) use ($object, $field_key) { return $rel->{'field_' . $field_key} == $object->get_id(); }); return [ 'origin' => $object, 'relations' => array_values($rels) ]; }, $objects); }