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. * * @param string The contact id * @return \Illuminate\View\Factory view */ public function edit($contactId) { $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 * * @param string The contact id * @param \Illuminate\Http\Request $request * @return \Illuminate\View\Factory view */ public function update($contactId, Request $request) { $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. * * @param string The contact id * @return \Illuminate\View\Factory view */ public function destroy($contactId) { $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 * * @param string The contact id * @return \Illuminate\View\Factory view */ public function getAvatar($contactId) { // Initialising $avatarURL = null; $avatar = null; $contact = Contact::findOrFail($contactId); if (mb_strlen($contact->homepage !== null) !== 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 (array_get($microformat, 'type.0') == 'h-card') { $avatarURL = array_get($microformat, 'properties.photo.0'); 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'); } }