PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/ansel/rss.php

https://github.com/wrobel/horde
PHP | 296 lines | 255 code | 21 blank | 20 comment | 31 complexity | 8e0e4bd5400b56e77dc517586cf51801 MD5 | raw file
Possible License(s): BSD-2-Clause, AGPL-1.0, LGPL-2.1, LGPL-3.0, BSD-3-Clause, LGPL-2.0, GPL-2.0
  1. <?php
  2. /**
  3. * Ansel RSS feed. Note that we always return a 'normal' thumb image
  4. * and not a prettythumb since we have no way of knowing what the client
  5. * requesting this will be viewing the image on.
  6. *
  7. * Copyright 2003-2012 Horde LLC (http://www.horde.org/)
  8. *
  9. * @author Michael J. Rubinsky <mrubinsk@horde.org>
  10. * @package Ansel
  11. */
  12. require_once dirname(__FILE__) . '/lib/Application.php';
  13. Horde_Registry::appInit('ansel', array('session_control' => 'readonly'));
  14. // Get form data
  15. $stream_type = Horde_Util::getFormData('stream_type', 'all');
  16. $id = Horde_Util::getFormData('id');
  17. $type = basename(Horde_Util::getFormData('type', 'rss2'));
  18. $slug = Horde_Util::getFormData('slug');
  19. $uid = md5($stream_type . $id . $type . $GLOBALS['registry']->getAuth());
  20. $filename = 'ansel_feed_template_' . $uid;
  21. if ($conf['ansel_cache']['usecache']) {
  22. $cache_key = 'ansel_feed_template_' . $uid;
  23. $rss = $GLOBALS['injector']->getInstance('Horde_Cache')->get($cache_key, $conf['cache']['default_lifetime']);
  24. $filename = $GLOBALS['injector']->getInstance('Horde_Cache')->get($filename, $conf['cache']['default_lifetime']);
  25. }
  26. if (empty($rss)) {
  27. // Assume failure
  28. $params = array('last_modified' => time(),
  29. 'name' => _("Error retrieving feed"),
  30. 'link' => '',
  31. 'desc' => _("Unable to retrieve requested feed"),
  32. 'image_url' => Horde::img('alerts/error.png'),
  33. 'image_link' => '',
  34. 'image_alt' => '');
  35. $author = '';
  36. // Determine what we are requesting
  37. switch ($stream_type) {
  38. case 'all':
  39. try {
  40. $images = $GLOBALS['injector']->getInstance('Ansel_Storage')->getRecentImages();
  41. } catch (Ansel_Exception $e) {
  42. $images = array();
  43. }
  44. // Eventually would like the link to link to a search
  45. // results page containing the same images present in this
  46. // feed. For now, just link to the List view until some of
  47. // the search code can be refactored.
  48. $params = array('last_modified' => $images[0]->uploaded,
  49. 'name' => sprintf(_("Recently added photos on %s"),
  50. $conf['server']['name']),
  51. 'link' => Ansel::getUrlFor('view',
  52. array('view' => 'List'),
  53. true),
  54. 'desc' => sprintf(_("Recently added photos on %s"),
  55. $conf['server']['name']),
  56. 'image_url' => Ansel::getImageUrl($images[0]->id,
  57. 'thumb', true),
  58. 'image_alt' => $images[0]->caption,
  59. 'image_link' => Ansel::getUrlFor(
  60. 'view', array('image' => $images[0]->id,
  61. 'view' => 'Image',
  62. 'gallery' => $images[0]->gallery),
  63. true));
  64. break;
  65. case 'gallery':
  66. // Retrieve latest from specified gallery
  67. // Try a slug first.
  68. if ($slug) {
  69. $gallery = $GLOBALS['injector']->getInstance('Ansel_Storage')->getGalleryBySlug($slug);
  70. } elseif (is_numeric($id)) {
  71. $gallery = $GLOBALS['injector']->getInstance('Ansel_Storage')->getGallery($id);
  72. }
  73. if ($gallery->hasPermission($GLOBALS['registry']->getAuth(), Horde_Perms::SHOW) &&
  74. !$gallery->hasPasswd() && $gallery->isOldEnough()) {
  75. if (!$gallery->countImages() && $gallery->hasSubGalleries()) {
  76. $subgalleries = $GLOBALS['injector']
  77. ->getInstance('Ansel_Storage')
  78. ->listGalleries(array('parent' => $gallery));
  79. $subs = array();
  80. foreach ($subgalleries as $subgallery) {
  81. $subs[] = $subgallery->id;
  82. }
  83. $images = $GLOBALS['injector']->getInstance('Ansel_Storage')->getRecentImages($subs);
  84. } else {
  85. $images = $gallery->getRecentImages();
  86. $owner = $gallery->getIdentity();
  87. $author = $owner->getValue('from_addr');
  88. }
  89. }
  90. if (!count($images)) {
  91. $images = array();
  92. } else {
  93. $viewurl = Ansel::getUrlFor('view',
  94. array('view' => 'Gallery',
  95. 'gallery' => $id),
  96. true);
  97. $img = &$GLOBALS['injector']->getInstance('Ansel_Storage')->getImage($gallery->getKeyImage(Ansel::getStyleDefinition('ansel_default')));
  98. $params = array('last_modified' => $gallery->get('last_modified'),
  99. 'name' => sprintf(_("%s on %s"),
  100. $gallery->get('name'),
  101. $conf['server']['name']),
  102. 'link' => $viewurl,
  103. 'desc' => $gallery->get('desc'),
  104. 'image_url' => Ansel::getImageUrl($img->id, 'thumb', true),
  105. 'image_alt' => $img->caption,
  106. 'image_link' => Ansel::getUrlFor('view',
  107. array('image' => $img->id,
  108. 'gallery' => $img->gallery,
  109. 'view' => 'Image'),
  110. true));
  111. }
  112. break;
  113. case 'user':
  114. $galleries = array();
  115. try {
  116. $shares = $GLOBALS['injector']
  117. ->getInstance('Ansel_Storage')
  118. ->listGalleries(array('attributes' => $id));
  119. foreach ($shares as $gallery) {
  120. if ($gallery->isOldEnough() && !$gallery->hasPasswd()) {
  121. $galleries[] = $gallery->id;
  122. }
  123. }
  124. } catch (Horde_Share_Exception $e) {
  125. Horde::logMessage($e->getMessage(), 'ERR');
  126. }
  127. $images = array();
  128. if (isset($galleries) && count($galleries)) {
  129. try {
  130. $images = $GLOBALS['injector']
  131. ->getInstance('Ansel_Storage')
  132. ->getRecentImages($galleries);
  133. } catch (Ansel_Exception $e) {
  134. Horde::logMessage($e->getMessage(), 'ERR');
  135. }
  136. if (count($images)) {
  137. $owner = $injector->getInstance('Horde_Core_Factory_Identity')->create($id);
  138. $name = $owner->getValue('fullname');
  139. $author = $owner->getValue('from_addr');
  140. if (!$name) {
  141. $name = $id;
  142. }
  143. $params = array('last_modified' => $images[0]->uploaded,
  144. 'name' => sprintf(_("Photos by %s"),
  145. $name),
  146. 'link' => Ansel::getUrlFor('view',
  147. array('view' => 'List',
  148. 'groupby' => 'owner',
  149. 'owner' => $id),
  150. true),
  151. 'desc' => sprintf(_("Recently added photos by %s on %s"),
  152. $name, $conf['server']['name']),
  153. 'image_url' => Ansel::getImageUrl($images[0]->id, 'thumb', true),
  154. 'image_alt' => $images[0]->caption,
  155. 'image_link' => Ansel::getUrlFor(
  156. 'view', array('image' => $images[0]->id,
  157. 'gallery' => $images[0]->gallery,
  158. 'view' => 'Image'), true)
  159. );
  160. }
  161. }
  162. break;
  163. case 'tag':
  164. $filter = array('typeId' => 'image',
  165. 'limit' => 10);
  166. $images = $GLOBALS['injector']->getInstance('Ansel_Tagger')->search(array($id), $filter);
  167. try {
  168. $images = $GLOBALS['injector']->getInstance('Ansel_Storage')->getImages(array('ids' => $images['images']));
  169. } catch (Ansel_Exception $e) {
  170. Horde::logMessage($e->getMessage(), 'ERR');
  171. $images = array();
  172. }
  173. if (count($images)) {
  174. $images = array_values($images);
  175. $params = array('last_modified' => $images[0]->uploaded,
  176. 'name' => sprintf(_("Photos tagged with %s on %s"),
  177. $id, $conf['server']['name']),
  178. 'link' => Ansel::getUrlFor('view',
  179. array('tag' => $id,
  180. 'view' => 'Results'),
  181. true),
  182. 'desc' => sprintf(_("Photos tagged with %s on %s"),
  183. $id, $conf['server']['name']),
  184. 'image_url' => Ansel::getImageUrl($images[0]->id, 'thumb', true, 'ansel_default'),
  185. 'image_alt' => $images[0]->caption,
  186. 'image_link' => Ansel::getUrlFor('view',
  187. array('view' => 'Image',
  188. 'image' => $images[0]->id,
  189. 'gallery' => $images[0]->gallery),
  190. true)
  191. );
  192. }
  193. // Do this here to avoid iterating the images twice
  194. $galleries = array();
  195. $imgs = array();
  196. $cnt = count($images);
  197. for ($i = 0; $i < $cnt; ++$i) {
  198. $gallery_id = $images[$i]->gallery;
  199. if (empty($galleries[$gallery_id])) {
  200. try {
  201. $galleries[$gallery_id]['gallery'] = $GLOBALS['injector']->getInstance('Ansel_Storage')->getGallery($gallery_id);
  202. } catch (Ansel_Exception $e) {}
  203. }
  204. if (!isset($galleries[$gallery_id]['perm'])) {
  205. $galleries[$gallery_id]['perm'] =
  206. ($galleries[$gallery_id]['gallery']->hasPermission($GLOBALS['registry']->getAuth(), Horde_Perms::READ) &&
  207. $galleries[$gallery_id]['gallery']->isOldEnough() &&
  208. !$galleries[$gallery_id]['gallery']->hasPasswd());
  209. }
  210. if ($galleries[$gallery_id]['perm']) {
  211. $imgs[$i]['link'] = Ansel::getUrlFor(
  212. 'view',
  213. array('view' => 'Image',
  214. 'gallery' => $images[$i]->gallery,
  215. 'image' => $images[$i]->id), true);
  216. $imgs[$i]['filename'] = $images[$i]->filename;
  217. $imgs[$i]['caption'] = $images[$i]->caption;
  218. $imgs[$i]['url'] = htmlspecialchars(Ansel::getImageUrl($images[$i]->id, 'screen', true));
  219. $imgs[$i]['type'] = $images[$i]->getType('screen');
  220. $imgs[$i]['author'] = $author;
  221. $imgs[$i]['thumb'] = htmlspecialchars(Ansel::getImageUrl($images[$i]->id, 'thumb', true));
  222. $imgs[$i]['latitude'] = $images[$i]->lat;
  223. $imgs[$i]['longitude'] = $images[$i]->lng;
  224. }
  225. }
  226. }
  227. if (!isset($imgs)) {
  228. $imgs = array();
  229. $cnt = count($images);
  230. for ($i = 0; $i < $cnt; ++$i) {
  231. $imgs[$i]['link'] = Ansel::getUrlFor(
  232. 'view',
  233. array('view' => 'Image',
  234. 'gallery' => $images[$i]->gallery,
  235. 'image' => $images[$i]->id), true);
  236. $imgs[$i]['filename'] = $images[$i]->filename;
  237. $imgs[$i]['caption'] = $images[$i]->caption;
  238. $imgs[$i]['url'] = htmlspecialchars(Ansel::getImageUrl($images[$i]->id, 'screen', true));
  239. $imgs[$i]['type'] = $images[$i]->getType('screen');
  240. $imgs[$i]['author'] = $author;
  241. $imgs[$i]['thumb'] = htmlspecialchars(Ansel::getImageUrl($images[$i]->id, 'thumb', true));
  242. $imgs[$i]['latitude'] = $images[$i]->lat;
  243. $imgs[$i]['longitude'] = $images[$i]->lng;
  244. }
  245. }
  246. $xsl = Horde_Themes::getFeedXsl();
  247. $stream_name = htmlspecialchars($params['name']);
  248. $stream_desc = htmlspecialchars($params['desc']);
  249. $stream_updated = htmlspecialchars(date('r', $params['last_modified']));
  250. $stream_official = htmlspecialchars($params['link']);
  251. $image_url = htmlspecialchars($params['image_url']);
  252. $image_link = htmlspecialchars($params['image_link']);
  253. $image_alt = htmlspecialchars($params['image_alt']);
  254. $ansel = 'Ansel ' . $registry->getVersion('ansel') . ' (http://www.horde.org/)';
  255. if ($stream_type != 'all' && $type != 'rss2') {
  256. $getparams = array('stream_type' => $stream_type, 'type' => $type);
  257. if (isset($id)) {
  258. $getparams['id'] = $id;
  259. }
  260. } else {
  261. $getparams = array();
  262. }
  263. $stream_rss = Horde::url('rss.php', true, -1)->add($getparams);
  264. $stream_rss2 = Horde::url('rss.php', true, -1)->add($getparams);
  265. $images = $imgs;
  266. Horde::startBuffer();
  267. include ANSEL_TEMPLATES . '/rss/' . $type . '.inc';
  268. $rss = Horde::endBuffer();
  269. if ($conf['ansel_cache']['usecache']) {
  270. $GLOBALS['injector']->getInstance('Horde_Cache')->set($cache_key, $rss);
  271. $GLOBALS['injector']->getInstance('Horde_Cache')->set($filename, $params['name']);
  272. }
  273. }
  274. $browser->downloadHeaders($filename . '.rss', 'text/xml', true);
  275. echo $rss;