sharing.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * ownCloud - gallery application
  4. *
  5. * @author Bartek Przybylski
  6. * @copyright 2012 Bartek Przybylski bartek@alefzero.eu
  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 Lesser General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. if (!isset($_GET['token']) || !isset($_GET['operation'])) {
  23. OCP\JSON::error(array('cause' => 'Not enought arguments'));
  24. exit;
  25. }
  26. $operation = $_GET['operation'];
  27. $token = $_GET['token'];
  28. if (!OC_Gallery_Sharing::isTokenValid($token)) {
  29. OCP\JSON::error(array('cause' => 'Given token is not valid'));
  30. exit;
  31. }
  32. function handleGetGallery($token, $path) {
  33. $owner = OC_Gallery_Sharing::getTokenOwner($token);
  34. $apath = OC_Gallery_Sharing::getPath($token);
  35. if ($path == false)
  36. $root = $apath;
  37. else
  38. $root = rtrim($apath,'/').$path;
  39. $r = OC_Gallery_Album::find($owner, null, $root);
  40. $albums = array();
  41. $photos = array();
  42. $albumId = -1;
  43. if ($row = $r->fetchRow()) {
  44. $albumId = $row['album_id'];
  45. }
  46. if ($albumId != -1) {
  47. if (OC_Gallery_Sharing::isRecursive($token)) {
  48. $r = OC_Gallery_Album::find($owner, null, null, $root);
  49. while ($row = $r->fetchRow())
  50. $albums[] = $row['album_name'];
  51. }
  52. $r = OC_Gallery_Photo::find($albumId);
  53. while ($row = $r->fetchRow())
  54. $photos[] = $row['file_path'];
  55. }
  56. OCP\JSON::success(array('albums' => $albums, 'photos' => $photos));
  57. }
  58. function handleGetThumbnail($token, $imgpath) {
  59. $owner = OC_Gallery_Sharing::getTokenOwner($token);
  60. $image = OC_Gallery_Photo::getThumbnail($imgpath, $owner);
  61. if ($image) {
  62. OCP\Response::enableCaching(3600 * 24); // 24 hour
  63. $image->show();
  64. }
  65. }
  66. function handleGetAlbumThumbnail($token, $albumname)
  67. {
  68. $owner = OC_Gallery_Sharing::getTokenOwner($token);
  69. $view = OCP\Files::getStorage('gallery');
  70. $file = $view->fopen($albumname.'.png', 'r');
  71. $image = new OC_Image($file);
  72. if ($image->valid()) {
  73. $image->centerCrop();
  74. $image->resize(200);
  75. $image->fixOrientation();
  76. OCP\Response::enableCaching(3600 * 24); // 24 hour
  77. $image->show();
  78. }
  79. }
  80. function handleGetPhoto($token, $photo) {
  81. $owner = OC_Gallery_Sharing::getTokenOwner($token);
  82. $view = OCP\Files::getStorage('files');
  83. $file = $view->fopen(urldecode($photo), 'r');
  84. header('Content-Type: '.OC_Image::getMimeTypeForFile($file));
  85. OCP\Response::sendFile($file);
  86. }
  87. switch ($operation) {
  88. case 'get_gallery':
  89. handleGetGallery($token, isset($_GET['path'])? $_GET['path'] : false);
  90. break;
  91. case 'get_thumbnail':
  92. handleGetThumbnail($token, urldecode($_GET['img']));
  93. break;
  94. case 'get_album_thumbnail':
  95. handleGetAlbumThumbnail($token, urldecode($_GET['albumname']));
  96. break;
  97. case 'get_photo':
  98. handleGetPhoto($token, urldecode($_GET['photo']));
  99. break;
  100. }