uploadphoto.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. OCP\JSON::checkLoggedIn();
  24. OCP\JSON::checkAppEnabled('contacts');
  25. OCP\JSON::callCheck();
  26. // Firefox and Konqueror tries to download application/json for me. --Arthur
  27. OCP\JSON::setContentTypeHeader('text/plain; charset=utf-8');
  28. require_once 'loghandler.php';
  29. $l10n = OC_Contacts_App::$l10n;
  30. // If it is a Drag'n'Drop transfer it's handled here.
  31. $fn = (isset($_SERVER['HTTP_X_FILE_NAME']) ? $_SERVER['HTTP_X_FILE_NAME'] : false);
  32. if ($fn) {
  33. if (!isset($_GET['id'])) {
  34. bailOut($l10n->t('No contact ID was submitted.'));
  35. }
  36. $id = $_GET['id'];
  37. $tmpkey = 'contact-photo-'.md5($fn);
  38. $data = file_get_contents('php://input');
  39. $image = new OC_Image();
  40. sleep(1); // Apparently it needs time to load the data.
  41. if($image->loadFromData($data)) {
  42. if($image->width() > 400 || $image->height() > 400) {
  43. $image->resize(400); // Prettier resizing than with browser and saves bandwidth.
  44. }
  45. if(!$image->fixOrientation()) { // No fatal error so we don't bail out.
  46. debug('Couldn\'t save correct image orientation: '.$tmpkey);
  47. }
  48. if(OC_Cache::set($tmpkey, $image->data(), 600)) {
  49. OCP\JSON::success(array(
  50. 'data' => array(
  51. 'mime'=>$_SERVER['CONTENT_TYPE'],
  52. 'name'=>$fn,
  53. 'id'=>$id,
  54. 'tmp'=>$tmpkey)));
  55. exit();
  56. } else {
  57. bailOut($l10n->t('Couldn\'t save temporary image: ').$tmpkey);
  58. }
  59. } else {
  60. bailOut($l10n->t('Couldn\'t load temporary image: ').$tmpkey);
  61. }
  62. }
  63. // Uploads from file dialog are handled here.
  64. if (!isset($_POST['id'])) {
  65. bailOut($l10n->t('No contact ID was submitted.'));
  66. }
  67. if (!isset($_FILES['imagefile'])) {
  68. bailOut($l10n->t('No file was uploaded. Unknown error'));
  69. }
  70. $error = $_FILES['imagefile']['error'];
  71. if($error !== UPLOAD_ERR_OK) {
  72. $errors = array(
  73. 0=>$l10n->t("There is no error, the file uploaded with success"),
  74. 1=>$l10n->t("The uploaded file exceeds the upload_max_filesize directive in php.ini").ini_get('upload_max_filesize'),
  75. 2=>$l10n->t("The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form"),
  76. 3=>$l10n->t("The uploaded file was only partially uploaded"),
  77. 4=>$l10n->t("No file was uploaded"),
  78. 6=>$l10n->t("Missing a temporary folder")
  79. );
  80. bailOut($errors[$error]);
  81. }
  82. $file=$_FILES['imagefile'];
  83. if(file_exists($file['tmp_name'])) {
  84. $tmpkey = 'contact-photo-'.md5(basename($file['tmp_name']));
  85. $image = new OC_Image();
  86. if($image->loadFromFile($file['tmp_name'])) {
  87. if($image->width() > 400 || $image->height() > 400) {
  88. $image->resize(400); // Prettier resizing than with browser and saves bandwidth.
  89. }
  90. if(!$image->fixOrientation()) { // No fatal error so we don't bail out.
  91. debug('Couldn\'t save correct image orientation: '.$tmpkey);
  92. }
  93. if(OC_Cache::set($tmpkey, $image->data(), 600)) {
  94. OCP\JSON::success(array(
  95. 'data' => array(
  96. 'mime'=>$file['type'],
  97. 'size'=>$file['size'],
  98. 'name'=>$file['name'],
  99. 'id'=>$_POST['id'],
  100. 'tmp'=>$tmpkey,
  101. )));
  102. exit();
  103. } else {
  104. bailOut($l10n->t('Couldn\'t save temporary image: ').$tmpkey);
  105. }
  106. } else {
  107. bailOut($l10n->t('Couldn\'t load temporary image: ').$file['tmp_name']);
  108. }
  109. } else {
  110. bailOut('Temporary file: \''.$file['tmp_name'].'\' has gone AWOL?');
  111. }