savecrop.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. * TODO: Translatable strings.
  22. * Remember to delete tmp file at some point.
  23. */
  24. function bailOut($msg) {
  25. OCP\JSON::error(array('data' => array('message' => $msg)));
  26. OCP\Util::writeLog('contacts','ajax/savecrop.php: '.$msg, OCP\Util::DEBUG);
  27. exit();
  28. }
  29. // Check if we are a user
  30. OCP\JSON::checkLoggedIn();
  31. OCP\JSON::checkAppEnabled('contacts');
  32. OCP\JSON::callCheck();
  33. // Firefox and Konqueror tries to download application/json for me. --Arthur
  34. OCP\JSON::setContentTypeHeader('text/plain');
  35. $image = null;
  36. $x1 = (isset($_POST['x1']) && $_POST['x1']) ? $_POST['x1'] : 0;
  37. //$x2 = isset($_POST['x2']) ? $_POST['x2'] : -1;
  38. $y1 = (isset($_POST['y1']) && $_POST['y1']) ? $_POST['y1'] : 0;
  39. //$y2 = isset($_POST['y2']) ? $_POST['y2'] : -1;
  40. $w = (isset($_POST['w']) && $_POST['w']) ? $_POST['w'] : -1;
  41. $h = (isset($_POST['h']) && $_POST['h']) ? $_POST['h'] : -1;
  42. $tmp_path = isset($_POST['tmp_path']) ? $_POST['tmp_path'] : '';
  43. $id = isset($_POST['id']) ? $_POST['id'] : '';
  44. if($tmp_path == '') {
  45. bailOut('Missing path to temporary file.');
  46. }
  47. if($id == '') {
  48. bailOut('Missing contact id.');
  49. }
  50. OCP\Util::writeLog('contacts','savecrop.php: files: '.$tmp_path.' exists: '.file_exists($tmp_path), OCP\Util::DEBUG);
  51. if(file_exists($tmp_path)) {
  52. $image = new OC_Image();
  53. if($image->loadFromFile($tmp_path)) {
  54. $w = ($w != -1 ? $w : $image->width());
  55. $h = ($h != -1 ? $h : $image->height());
  56. OCP\Util::writeLog('contacts','savecrop.php, x: '.$x1.' y: '.$y1.' w: '.$w.' h: '.$h, OCP\Util::DEBUG);
  57. if($image->crop($x1, $y1, $w, $h)) {
  58. if(($image->width() <= 200 && $image->height() <= 200) || $image->resize(200)) {
  59. $tmpfname = tempnam(get_temp_dir(), "occCropped"); // create a new file because of caching issues.
  60. if($image->save($tmpfname)) {
  61. unlink($tmp_path);
  62. $card = OC_Contacts_App::getContactVCard($id);
  63. if(!$card) {
  64. unlink($tmpfname);
  65. bailOut('Error getting contact object.');
  66. }
  67. if($card->__isset('PHOTO')) {
  68. OCP\Util::writeLog('contacts','savecrop.php: PHOTO property exists.', OCP\Util::DEBUG);
  69. $property = $card->__get('PHOTO');
  70. if(!$property) {
  71. unlink($tmpfname);
  72. bailOut('Error getting PHOTO property.');
  73. }
  74. $property->setValue($image->__toString());
  75. $property->parameters[] = new Sabre_VObject_Parameter('ENCODING', 'b');
  76. $property->parameters[] = new Sabre_VObject_Parameter('TYPE', $image->mimeType());
  77. $card->__set('PHOTO', $property);
  78. } else {
  79. OCP\Util::writeLog('contacts','savecrop.php: files: Adding PHOTO property.', OCP\Util::DEBUG);
  80. $card->addProperty('PHOTO', $image->__toString(), array('ENCODING' => 'b', 'TYPE' => $image->mimeType()));
  81. }
  82. $now = new DateTime;
  83. $card->setString('REV', $now->format(DateTime::W3C));
  84. if(!OC_Contacts_VCard::edit($id,$card)) {
  85. bailOut('Error saving contact.');
  86. }
  87. unlink($tmpfname);
  88. //$result=array( "status" => "success", 'mime'=>$image->mimeType(), 'tmp'=>$tmp_path);
  89. $tmpl = new OCP\Template("contacts", "part.contactphoto");
  90. $tmpl->assign('tmp_path', $tmpfname);
  91. $tmpl->assign('mime', $image->mimeType());
  92. $tmpl->assign('id', $id);
  93. $tmpl->assign('refresh', true);
  94. $tmpl->assign('width', $image->width());
  95. $tmpl->assign('height', $image->height());
  96. $page = $tmpl->fetchPage();
  97. OCP\JSON::success(array('data' => array('page'=>$page, 'tmp'=>$tmpfname)));
  98. exit();
  99. } else {
  100. if(file_exists($tmpfname)) {
  101. unlink($tmpfname);
  102. }
  103. bailOut('Error saving temporary image');
  104. }
  105. } else {
  106. bailOut('Error resizing image');
  107. }
  108. } else {
  109. bailOut('Error cropping image');
  110. }
  111. } else {
  112. bailOut('Error creating temporary image');
  113. }
  114. } else {
  115. bailOut('Error finding image: '.$tmp_path);
  116. }
  117. if($tmp_path != '' && file_exists($tmp_path)) {
  118. unlink($tmp_path);
  119. }
  120. ?>