name = request()->input('name'); $contact->nick = request()->input('nick'); $contact->homepage = request()->input('homepage'); $contact->twitter = request()->input('twitter'); $contact->facebook = request()->input('facebook'); $contact->save(); return redirect('/admin/contacts'); } /** * Show the form to edit an existing contact. */ public function edit(int $contactId): View { $contact = Contact::findOrFail($contactId); return view('admin.contacts.edit', compact('contact')); } /** * Process the request to edit a contact. * * @todo Allow saving profile pictures for people without homepages */ public function update(int $contactId): RedirectResponse { $contact = Contact::findOrFail($contactId); $contact->name = request()->input('name'); $contact->nick = request()->input('nick'); $contact->homepage = request()->input('homepage'); $contact->twitter = request()->input('twitter'); $contact->facebook = request()->input('facebook'); $contact->save(); if (request()->hasFile('avatar') && (request()->input('homepage') != '')) { $dir = parse_url(request()->input('homepage'), PHP_URL_HOST); $destination = public_path() . '/assets/profile-images/' . $dir; $filesystem = new Filesystem; if ($filesystem->isDirectory($destination) === false) { $filesystem->makeDirectory($destination); } request()->file('avatar')->move($destination, 'image'); } return redirect('/admin/contacts'); } /** * Process the request to delete a contact. */ public function destroy(int $contactId): RedirectResponse { $contact = Contact::findOrFail($contactId); $contact->delete(); return redirect('/admin/contacts'); } /** * Download the avatar for a contact. * * This method attempts to find the microformat marked-up profile image * from a given homepage and save it accordingly * * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View */ public function getAvatar(int $contactId) { // Initialising $avatarURL = null; $avatar = null; $contact = Contact::findOrFail($contactId); if ($contact->homepage !== null && mb_strlen($contact->homepage) !== 0) { $client = resolve(Client::class); try { $response = $client->get($contact->homepage); } catch (\GuzzleHttp\Exception\BadResponseException $e) { return redirect('/admin/contacts/' . $contactId . '/edit') ->with('error', 'Bad resposne from contact’s homepage'); } $mf2 = \Mf2\parse((string) $response->getBody(), $contact->homepage); foreach ($mf2['items'] as $microformat) { if (Arr::get($microformat, 'type.0') === 'h-card') { $avatarURL = Arr::get($microformat, 'properties.photo.0.value'); break; } } if ($avatarURL !== null) { try { $avatar = $client->get($avatarURL); } catch (\GuzzleHttp\Exception\BadResponseException $e) { return redirect('/admin/contacts/' . $contactId . '/edit') ->with('error', 'Unable to download avatar'); } } if ($avatar !== null) { $directory = public_path() . '/assets/profile-images/' . parse_url($contact->homepage, PHP_URL_HOST); $filesystem = new Filesystem; if ($filesystem->isDirectory($directory) === false) { $filesystem->makeDirectory($directory); } $filesystem->put($directory . '/image', $avatar->getBody()); return view('admin.contacts.getavatarsuccess', [ 'homepage' => parse_url($contact->homepage, PHP_URL_HOST), ]); } } return redirect('/admin/contacts/' . $contactId . '/edit'); } }