PageRenderTime 60ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/core/controller/image.php

https://github.com/phpbbgallery/phpbb-gallery
PHP | 326 lines | 213 code | 53 blank | 60 comment | 44 complexity | 5f043c610727665edce44a0f4eb6c377 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * @package phpBB Gallery Core
  5. * @copyright (c) 2014 nickvergessen
  6. * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
  7. *
  8. */
  9. namespace phpbbgallery\core\controller;
  10. class image
  11. {
  12. /* @var \phpbb\config\config */
  13. protected $config;
  14. /* @var \phpbb\auth\auth */
  15. protected $auth;
  16. /* @var \phpbb\controller\helper */
  17. protected $helper;
  18. /* @var \phpbb\db\driver\driver */
  19. protected $db;
  20. /* @var \phpbb\event\dispatcher */
  21. protected $dispatcher;
  22. /* @var \phpbb\pagination */
  23. protected $pagination;
  24. /* @var \phpbb\template\template */
  25. protected $template;
  26. /* @var \phpbb\user */
  27. protected $user;
  28. /* @var \phpbbgallery\core\album\display */
  29. protected $display;
  30. /* @var \phpbbgallery\core\album\loader */
  31. protected $loader;
  32. /* @var \phpbbgallery\core\auth\auth */
  33. protected $gallery_auth;
  34. /* @var \phpbbgallery\core\auth\level */
  35. protected $auth_level;
  36. /* @var array */
  37. protected $data;
  38. /* @var string */
  39. protected $table_albums;
  40. /* @var string */
  41. protected $table_images;
  42. /* @var string */
  43. protected $table_users;
  44. /**
  45. * Constructor
  46. *
  47. * @param \phpbb\auth\auth $auth Auth object
  48. * @param \phpbb\config\config $config Config object
  49. * @param \phpbb\controller\helper $helper Controller helper object
  50. * @param \phpbb\db\driver\driver $db Database object
  51. * @param \phpbb\event\dispatcher $dispatcher Event dispatcher object
  52. * @param \phpbb\pagination $pagination Pagination object
  53. * @param \phpbb\template\template $template Template object
  54. * @param \phpbb\user $user User object
  55. * @param \phpbbgallery\core\album\display $display Albums display object
  56. * @param \phpbbgallery\core\album\loader $loader Albums display object
  57. * @param \phpbbgallery\core\auth\auth $auth Gallery auth object
  58. * @param \phpbbgallery\core\auth\level $auth_level Gallery auth level object
  59. * @param string $images_table Gallery images table
  60. * @param string $albums_table Gallery albums table
  61. * @param string $users_table Gallery users table
  62. */
  63. public function __construct(\phpbb\auth\auth $auth, \phpbb\config\config $config, \phpbb\controller\helper $helper, \phpbb\db\driver\driver $db, \phpbb\event\dispatcher $dispatcher, \phpbb\pagination $pagination, \phpbb\template\template $template, \phpbb\user $user, \phpbbgallery\core\album\display $display, \phpbbgallery\core\album\loader $loader, \phpbbgallery\core\auth\auth $gallery_auth, \phpbbgallery\core\auth\level $auth_level, $albums_table, $images_table, $users_table)
  64. {
  65. $this->auth = $auth;
  66. $this->config = $config;
  67. $this->helper = $helper;
  68. $this->db = $db;
  69. $this->dispatcher = $dispatcher;
  70. $this->pagination = $pagination;
  71. $this->template = $template;
  72. $this->user = $user;
  73. $this->display = $display;
  74. $this->loader = $loader;
  75. $this->gallery_auth = $gallery_auth;
  76. $this->auth_level = $auth_level;
  77. $this->table_albums = $albums_table;
  78. $this->table_images = $images_table;
  79. $this->table_users = $users_table;
  80. }
  81. /**
  82. * Image Controller
  83. * Route: gallery/image_id/{image_id}
  84. *
  85. * @param int $image_id Image ID
  86. * @return Symfony\Component\HttpFoundation\Response A Symfony Response object
  87. */
  88. public function base($image_id, $page = 0)
  89. {
  90. $this->user->add_lang_ext('phpbbgallery/core', array('gallery'));
  91. try
  92. {
  93. $sql = 'SELECT *
  94. FROM ' . $this->table_images . '
  95. WHERE image_id = ' . (int) $image_id;
  96. $result = $this->db->sql_query($sql);
  97. $this->data = $this->db->sql_fetchrow($result);
  98. $this->db->sql_freeresult($result);
  99. if (!$this->data)
  100. {
  101. // Image does not exist
  102. throw new \OutOfBoundsException('INVALID_IMAGE');
  103. }
  104. $this->loader->load($this->data['image_album_id']);
  105. }
  106. catch (\Exception $e)
  107. {
  108. return $this->error($e->getMessage(), 404);
  109. }
  110. $album_id = (int) $this->data['image_album_id'];
  111. $album_data = $this->loader->get($album_id);
  112. $this->check_permissions($album_id, $album_data['album_user_id'], $this->data['image_status']);
  113. $this->display->generate_navigation($album_data);
  114. if (!$this->user->data['is_bot'] && isset($this->user->data['session_page']) && (strpos($this->user->data['session_page'], '&image_id=' . $image_id) === false || isset($this->user->data['session_created'])))
  115. {
  116. $sql = 'UPDATE ' . $this->table_images . '
  117. SET image_view_count = image_view_count + 1
  118. WHERE image_id = ' . $image_id;
  119. $this->db->sql_query($sql);
  120. }
  121. // Do stuff here
  122. $page_title = $this->data['image_name'];
  123. if ($page > 1)
  124. {
  125. $page_title .= ' - ' . $this->user->lang('PAGE_TITLE_NUMBER', $page);
  126. }
  127. $s_allowed_delete = $s_allowed_edit = $s_allowed_status = false;
  128. if (($this->gallery_auth->acl_check('m_', $album_id, $album_data['album_user_id']) || ($this->data['image_user_id'] == $this->user->data['user_id'])) && ($this->user->data['user_id'] != ANONYMOUS))
  129. {
  130. $s_user_allowed = (($this->data['image_user_id'] == $this->user->data['user_id']) && ($album_data['album_status'] != phpbb_ext_gallery_core_album::STATUS_LOCKED));
  131. $s_allowed_delete = (($this->gallery_auth->acl_check('i_delete', $album_id, $album_data['album_user_id']) && $s_user_allowed) || $this->gallery_auth->acl_check('m_delete', $album_id, $album_data['album_user_id']));
  132. $s_allowed_edit = (($this->gallery_auth->acl_check('i_edit', $album_id, $album_data['album_user_id']) && $s_user_allowed) || $this->gallery_auth->acl_check('m_edit', $album_id, $album_data['album_user_id']));
  133. $s_quick_mod = ($s_allowed_delete || $s_allowed_edit || $this->gallery_auth->acl_check('m_status', $album_id, $album_data['album_user_id']) || $this->gallery_auth->acl_check('m_move', $album_id, $album_data['album_user_id']));
  134. $this->user->add_lang_ext('phpbbgallery/core', 'gallery_mcp');
  135. $this->template->assign_vars(array(
  136. 'S_MOD_ACTION' => $this->helper->route('phpbbgallery_moderate_image', array('image_id' => $image_id)),
  137. 'S_QUICK_MOD' => $s_quick_mod,
  138. 'S_QM_MOVE' => $this->gallery_auth->acl_check('m_move', $album_id, $album_data['album_user_id']),
  139. 'S_QM_EDIT' => $s_allowed_edit,
  140. 'S_QM_DELETE' => $s_allowed_delete,
  141. 'S_QM_REPORT' => $this->gallery_auth->acl_check('m_report', $album_id, $album_data['album_user_id']),
  142. 'S_QM_STATUS' => $this->gallery_auth->acl_check('m_status', $album_id, $album_data['album_user_id']),
  143. 'S_IMAGE_REPORTED' => $this->data['image_reported'],
  144. 'U_IMAGE_REPORTED' => ($this->data['image_reported']) ? $this->helper->route('phpbbgallery_moderate_image', array('image_id' => $image_id)) : '',
  145. 'S_STATUS_APPROVED' => ($this->data['image_status'] == \phpbbgallery\core\image\image::STATUS_APPROVED),
  146. 'S_STATUS_UNAPPROVED' => ($this->data['image_status'] == \phpbbgallery\core\image\image::STATUS_UNAPPROVED),
  147. 'S_STATUS_LOCKED' => ($this->data['image_status'] == \phpbbgallery\core\image\image::STATUS_LOCKED),
  148. ));
  149. }
  150. $image_desc = generate_text_for_display($this->data['image_desc'], $this->data['image_desc_uid'], $this->data['image_desc_bitfield'], 7);
  151. $this->template->assign_vars(array(
  152. 'U_VIEW_ALBUM' => $this->helper->route('phpbbgallery_album', array('album_id' => $album_id)),
  153. 'UC_IMAGE' => $this->helper->route('phpbbgallery_image_file_medium', array('image_id' => $image_id)),//\phpbbgallery\core\image\image::generate_link('medium', $this->config['phpbb_gallery_link_imagepage'], $image_id, $this->data['image_name'], $album_id, ((substr($this->data['image_filename'], 0 -3) == 'gif') ? true : false), false, ''),
  154. 'EDIT_IMG' => $this->user->img('icon_post_edit', 'EDIT_IMAGE'),
  155. 'DELETE_IMG' => $this->user->img('icon_post_delete', 'DELETE_IMAGE'),
  156. 'REPORT_IMG' => $this->user->img('icon_post_report', 'REPORT_IMAGE'),
  157. 'STATUS_IMG' => $this->user->img('icon_post_info', 'STATUS_IMAGE'),
  158. 'U_DELETE' => ($s_allowed_delete) ? $this->helper->route('phpbbgallery_image_delete', array('image_id' => $image_id)) : '',
  159. 'U_EDIT' => ($s_allowed_edit) ? $this->helper->route('phpbbgallery_image_edit', array('image_id' => $image_id)) : '',
  160. 'U_REPORT' => ($this->gallery_auth->acl_check('i_report', $album_id, $album_data['album_user_id']) && ($this->data['image_user_id'] != $this->user->data['user_id'])) ? $this->helper->route('phpbbgallery_image_report', array('image_id' => $image_id)) : '',
  161. 'U_STATUS' => ($s_allowed_status) ? $this->helper->route('phpbbgallery_moderate_image', array('image_id' => $image_id)) : '',
  162. 'CONTEST_RANK' => ($this->data['image_contest_rank']) ? $this->user->lang('CONTEST_RESULT_' . $this->data['image_contest_rank']) : '',
  163. 'IMAGE_NAME' => $this->data['image_name'],
  164. 'IMAGE_DESC' => $image_desc,
  165. 'IMAGE_BBCODE' => ($this->config['allow_bbcode']) ? '[album]' . $image_id . '[/album]' : '',
  166. // 'IMAGE_IMGURL_BBCODE' => ($this->config['phpbb_gallery_disp_image_url']) ? '[url=' . $phpbb_ext_gallery->url->append_sid('full', 'image', "album_id=$album_id&amp;image_id=$image_id", true, '') . '][img]' . $phpbb_ext_gallery->url->append_sid('full', 'image', "album_id=$album_id&amp;image_id=$image_id&amp;mode=thumbnail", true, '') . '[/img][/url]' : '',
  167. // 'IMAGE_URL' => ($this->config['phpbb_gallery_disp_image_url']) ? $phpbb_ext_gallery->url->append_sid('full', 'image', "album_id=$album_id&amp;image_id=$image_id", true, '') : '',
  168. 'IMAGE_TIME' => $this->user->format_date($this->data['image_time']),
  169. 'IMAGE_VIEW' => $this->data['image_view_count'],
  170. 'POSTER_IP' => ($this->auth->acl_get('a_')) ? $this->data['image_user_ip'] : '',
  171. 'U_POSTER_WHOIS' => ($this->auth->acl_get('a_')) ? append_sid('mcp', 'mode=whois&amp;ip=' . $this->data['image_user_ip']) : '',
  172. 'S_ALBUM_ACTION' => $this->helper->route('phpbbgallery_image', array('image_id' => $image_id)),
  173. 'U_RETURN_LINK' => $this->helper->route('phpbbgallery_album', array('album_id' => $album_id)),
  174. 'S_RETURN_LINK' => $this->user->lang('RETURN_TO', $album_data['album_name']),
  175. ));
  176. $image_data = $this->data;
  177. $vars = array('image_id', 'image_data', 'album_data', 'page_title');
  178. extract($this->dispatcher->trigger_event('gallery.core.viewimage', compact($vars)));
  179. $this->data = $image_data;
  180. $user_id = $this->data['image_user_id'];
  181. $sql = $this->db->sql_build_query('SELECT', array(
  182. 'SELECT' => 'u.*, gu.personal_album_id, gu.user_images',
  183. 'FROM' => array(USERS_TABLE => 'u'),
  184. 'LEFT_JOIN' => array(
  185. array(
  186. 'FROM' => array($this->table_users => 'gu'),
  187. 'ON' => 'gu.user_id = u.user_id'
  188. ),
  189. ),
  190. 'WHERE' => 'u.user_id = ' . $this->data['image_user_id'],
  191. ));
  192. $result = $this->db->sql_query($sql);
  193. $user_cache = array();
  194. while ($row = $this->db->sql_fetchrow($result))
  195. {
  196. \phpbbgallery\core\user::add_user_to_cache($user_cache, $row);
  197. }
  198. $this->db->sql_freeresult($result);
  199. $user_cache[$user_id]['username'] = ($this->data['image_username']) ? $this->data['image_username'] : $this->user->lang['GUEST'];
  200. $this->template->assign_vars(array(
  201. 'POSTER_FULL' => get_username_string('full', $user_id, $user_cache[$user_id]['username'], $user_cache[$user_id]['user_colour']),
  202. 'POSTER_COLOUR' => get_username_string('colour', $user_id, $user_cache[$user_id]['username'], $user_cache[$user_id]['user_colour']),
  203. 'POSTER_USERNAME' => get_username_string('username', $user_id, $user_cache[$user_id]['username'], $user_cache[$user_id]['user_colour']),
  204. 'U_POSTER' => get_username_string('profile', $user_id, $user_cache[$user_id]['username'], $user_cache[$user_id]['user_colour']),
  205. 'POSTER_SIGNATURE' => $user_cache[$user_id]['sig'],
  206. 'POSTER_RANK_TITLE' => $user_cache[$user_id]['rank_title'],
  207. 'POSTER_RANK_IMG' => $user_cache[$user_id]['rank_image'],
  208. 'POSTER_RANK_IMG_SRC' => $user_cache[$user_id]['rank_image_src'],
  209. 'POSTER_JOINED' => $user_cache[$user_id]['joined'],
  210. 'POSTER_POSTS' => $user_cache[$user_id]['posts'],
  211. 'POSTER_AVATAR' => $user_cache[$user_id]['avatar'],
  212. 'POSTER_WARNINGS' => $user_cache[$user_id]['warnings'],
  213. 'POSTER_AGE' => $user_cache[$user_id]['age'],
  214. 'POSTER_ONLINE_IMG' => ($user_id == ANONYMOUS || !$this->config['load_onlinetrack']) ? '' : (($user_cache[$user_id]['online']) ? $this->user->img('icon_user_online', 'ONLINE') : $this->user->img('icon_user_offline', 'OFFLINE')),
  215. 'S_POSTER_ONLINE' => ($user_id == ANONYMOUS || !$this->config['load_onlinetrack']) ? false : (($user_cache[$user_id]['online']) ? true : false),
  216. 'U_POSTER_PROFILE' => $user_cache[$user_id]['profile'],
  217. 'U_POSTER_SEARCH' => $user_cache[$user_id]['search'],
  218. 'U_POSTER_PM' => ($user_id != ANONYMOUS && $this->config['allow_privmsg'] && $this->auth->acl_get('u_sendpm') && ($user_cache[$user_id]['allow_pm'] || $this->auth->acl_gets('a_', 'm_'))) ? append_sid('phpbb', 'ucp', 'i=pm&amp;mode=compose&amp;u=' . $user_id) : '',
  219. 'U_POSTER_EMAIL' => $user_cache[$user_id]['email'],
  220. 'U_POSTER_JABBER' => $user_cache[$user_id]['jabber'],
  221. 'U_POSTER_GALLERY' => $user_cache[$user_id]['gallery_album'],
  222. 'POSTER_GALLERY_IMAGES' => $user_cache[$user_id]['gallery_images'],
  223. 'U_POSTER_GALLERY_SEARCH' => $user_cache[$user_id]['gallery_search'],
  224. ));
  225. $this->template->assign_vars(array(
  226. 'PROFILE_IMG' => $this->user->img('icon_user_profile', 'READ_PROFILE'),
  227. 'SEARCH_IMG' => $this->user->img('icon_user_search', 'SEARCH_USER_POSTS'),
  228. 'PM_IMG' => $this->user->img('icon_contact_pm', 'SEND_PRIVATE_MESSAGE'),
  229. 'EMAIL_IMG' => $this->user->img('icon_contact_email', 'SEND_EMAIL'),
  230. 'JABBER_IMG' => $this->user->img('icon_contact_jabber', 'JABBER') ,
  231. 'GALLERY_IMG' => $this->user->img('icon_contact_gallery', 'PERSONAL_ALBUM'),
  232. ));
  233. return $this->helper->render('gallery/viewimage_body.html', $page_title);
  234. }
  235. /**
  236. * @param int $album_id
  237. * @param array $album_data
  238. */
  239. protected function check_permissions($album_id, $owner_id, $image_status)
  240. {
  241. if (!$this->gallery_auth->acl_check('i_view', $album_id, $owner_id) || ($image_status == \phpbbgallery\core\image\image::STATUS_ORPHAN))
  242. {
  243. if ($this->user->data['is_bot'])
  244. {
  245. // Redirect bots back to the index
  246. redirect($this->helper->route('phpbbgallery_index'));
  247. }
  248. // Display login box for guests and an error for users
  249. if (!$this->user->data['is_registered'])
  250. {
  251. // @todo Add "redirect after login" url
  252. login_box();
  253. }
  254. else
  255. {
  256. return $this->error('NOT_AUTHORISED', 403);
  257. }
  258. }
  259. if (!$this->gallery_auth->acl_check('m_status', $album_id, $owner_id) && ($image_status == \phpbbgallery\core\image\image::STATUS_UNAPPROVED))
  260. {
  261. return $this->error('NOT_AUTHORISED', 403);
  262. }
  263. }
  264. protected function error($message, $status = 200, $title = '')
  265. {
  266. $title = $title ?: 'INFORMATION';
  267. $this->template->assign_vars(array(
  268. 'MESSAGE_TITLE' => $this->user->lang($title),
  269. 'MESSAGE_TEXT' => $message,
  270. ));
  271. return $this->helper->render('message_body.html', $this->user->lang($title), $status);
  272. }
  273. }