oc_photo.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. // Check if we are a user
  23. // Firefox and Konqueror tries to download application/json for me. --Arthur
  24. OCP\JSON::setContentTypeHeader('text/plain');
  25. OCP\JSON::checkLoggedIn();
  26. OCP\JSON::checkAppEnabled('contacts');
  27. OCP\JSON::callCheck();
  28. function bailOut($msg) {
  29. OCP\JSON::error(array('data' => array('message' => $msg)));
  30. OCP\Util::writeLog('contacts','ajax/oc_photo.php: '.$msg, OCP\Util::ERROR);
  31. exit();
  32. }
  33. function debug($msg) {
  34. OCP\Util::writeLog('contacts','ajax/oc_photo.php: '.$msg, OCP\Util::DEBUG);
  35. }
  36. if(!isset($_GET['id'])) {
  37. bailOut(OC_Contacts_App::$l10n->t('No contact ID was submitted.'));
  38. }
  39. if(!isset($_GET['path'])) {
  40. bailOut(OC_Contacts_App::$l10n->t('No photo path was submitted.'));
  41. }
  42. $localpath = OC_Filesystem::getLocalFile($_GET['path']);
  43. $tmpfname = tempnam(get_temp_dir(), "occOrig");
  44. if(!file_exists($localpath)) {
  45. bailOut(OC_Contacts_App::$l10n->t('File doesn\'t exist:').$localpath);
  46. }
  47. file_put_contents($tmpfname, file_get_contents($localpath));
  48. $image = new OC_Image();
  49. if(!$image) {
  50. bailOut(OC_Contacts_App::$l10n->t('Error loading image.'));
  51. }
  52. if(!$image->loadFromFile($tmpfname)) {
  53. bailOut(OC_Contacts_App::$l10n->t('Error loading image.'));
  54. }
  55. if($image->width() > 400 || $image->height() > 400) {
  56. $image->resize(400); // Prettier resizing than with browser and saves bandwidth.
  57. }
  58. if(!$image->fixOrientation()) { // No fatal error so we don't bail out.
  59. debug('Couldn\'t save correct image orientation: '.$tmpfname);
  60. }
  61. if($image->save($tmpfname)) {
  62. OCP\JSON::success(array('data' => array('id'=>$_GET['id'], 'tmp'=>$tmpfname)));
  63. exit();
  64. } else {
  65. bailOut('Couldn\'t save temporary image: '.$tmpfname);
  66. }
  67. ?>