currentphoto.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * ownCloud - Addressbook
  4. *
  5. * @author Thomas Tanghus
  6. * @copyright 2012 Thomas Tanghus <thomas@tanghus.net>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. // Init owncloud
  23. //require_once('../../../lib/base.php');
  24. // Check if we are a user
  25. // Firefox and Konqueror tries to download application/json for me. --Arthur
  26. OCP\JSON::setContentTypeHeader('text/plain');
  27. OCP\JSON::checkLoggedIn();
  28. OCP\JSON::checkAppEnabled('contacts');
  29. function bailOut($msg) {
  30. OCP\JSON::error(array('data' => array('message' => $msg)));
  31. OCP\Util::writeLog('contacts','ajax/currentphoto.php: '.$msg, OCP\Util::ERROR);
  32. exit();
  33. }
  34. function debug($msg) {
  35. OCP\Util::writeLog('contacts','ajax/currentphoto.php: '.$msg, OCP\Util::DEBUG);
  36. }
  37. if (!isset($_GET['id'])) {
  38. bailOut(OC_Contacts_App::$l10n->t('No contact ID was submitted.'));
  39. }
  40. $tmpfname = tempnam(get_temp_dir(), "occOrig");
  41. $contact = OC_Contacts_App::getContactVCard($_GET['id']);
  42. $image = new OC_Image();
  43. if(!$image) {
  44. bailOut(OC_Contacts_App::$l10n->t('Error loading image.'));
  45. }
  46. // invalid vcard
  47. if( is_null($contact)) {
  48. bailOut(OC_Contacts_App::$l10n->t('Error reading contact photo.'));
  49. } else {
  50. if(!$image->loadFromBase64($contact->getAsString('PHOTO'))) {
  51. $image->loadFromBase64($contact->getAsString('LOGO'));
  52. }
  53. if($image->valid()) {
  54. if($image->save($tmpfname)) {
  55. OCP\JSON::success(array('data' => array('id'=>$_GET['id'], 'tmp'=>$tmpfname)));
  56. exit();
  57. } else {
  58. bailOut(OC_Contacts_App::$l10n->t('Error saving temporary file.'));
  59. }
  60. } else {
  61. bailOut(OC_Contacts_App::$l10n->t('The loading photo is not valid.'));
  62. }
  63. }
  64. ?>