PageRenderTime 63ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/root/blog/includes/functions_attachments.php

https://github.com/EXreaction/User-Blog-Mod
PHP | 1033 lines | 743 code | 193 blank | 97 comment | 134 complexity | ba995c25b3eb561b90eeab8e89badc48 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * @package phpBB3 User Blog
  5. * @version $Id: functions_attachments.php 485 2008-08-15 23:33:57Z exreaction@gmail.com $
  6. * @copyright (c) 2008 EXreaction
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. if (!defined('IN_PHPBB'))
  11. {
  12. exit;
  13. }
  14. /**
  15. * Custom Attachment class for Blogs
  16. *
  17. * Most of this code is forked from the attachment code in phpBB3.
  18. */
  19. class blog_attachment
  20. {
  21. public $attachment_data = array();
  22. public $filename_data = array();
  23. public $warn_msg = array();
  24. /**
  25. * Updates some attachment data
  26. */
  27. public function update_attachment_data($blog_id, $reply_id = 0, $poster_id = 0)
  28. {
  29. global $auth, $config, $db, $user;
  30. if (!$config['user_blog_enable_attachments'] || !$auth->acl_get('u_blogattach'))
  31. {
  32. return;
  33. }
  34. $blog_id = (int) $blog_id;
  35. $reply_id = (int) $reply_id;
  36. $poster_id = ($poster_id == 0) ? $user->data['user_id'] : (int) $poster_id;
  37. $attach_ids = array();
  38. if (sizeof($this->attachment_data))
  39. {
  40. foreach($this->attachment_data as $attach_row)
  41. {
  42. $sql_data = array(
  43. 'attach_comment' => $attach_row['attach_comment'],
  44. 'is_orphan' => 0,
  45. 'blog_id' => $blog_id,
  46. 'reply_id' => $reply_id,
  47. 'poster_id' => $poster_id,
  48. );
  49. $sql = 'UPDATE ' . BLOGS_ATTACHMENT_TABLE . '
  50. SET ' . $db->sql_build_array('UPDATE', $sql_data) . '
  51. WHERE attach_id = ' . (int) $attach_row['attach_id'];
  52. $db->sql_query($sql);
  53. }
  54. }
  55. }
  56. /**
  57. * Generate inline attachment entry
  58. */
  59. public function posting_gen_attachment_entry($attachment_data, &$filename_data)
  60. {
  61. global $template, $config;
  62. if (!$config['user_blog_enable_attachments'])
  63. {
  64. return;
  65. }
  66. $temp = compact('attachment_data', 'filename_data');
  67. blog_plugins::plugin_do_ref('function_posting_gen_attachment_entry', $temp);
  68. extract($temp);
  69. $template->assign_vars(array(
  70. 'S_SHOW_ATTACH_BOX' => true)
  71. );
  72. if (sizeof($attachment_data))
  73. {
  74. $template->assign_vars(array(
  75. 'S_HAS_ATTACHMENTS' => true)
  76. );
  77. ksort($attachment_data);
  78. foreach ($attachment_data as $count => $attach_row)
  79. {
  80. $hidden = '';
  81. $attach_row['real_filename'] = basename($attach_row['real_filename']);
  82. foreach ($attach_row as $key => $value)
  83. {
  84. $hidden .= '<input type="hidden" name="attachment_data[' . $count . '][' . $key . ']" value="' . $value . '" />';
  85. }
  86. $download_link = blog_url(false, false, false, array('page' => 'download', 'mode' => 'download', 'id' => intval($attach_row['attach_id'])));
  87. $template->assign_block_vars('attach_row', array(
  88. 'FILENAME' => basename($attach_row['real_filename']),
  89. 'A_FILENAME' => addslashes(basename($attach_row['real_filename'])),
  90. 'FILE_COMMENT' => $attach_row['attach_comment'],
  91. 'ATTACH_ID' => $attach_row['attach_id'],
  92. 'S_IS_ORPHAN' => $attach_row['is_orphan'],
  93. 'ASSOC_INDEX' => $count,
  94. 'U_VIEW_ATTACHMENT' => $download_link,
  95. 'S_HIDDEN' => $hidden)
  96. );
  97. }
  98. }
  99. $template->assign_vars(array(
  100. 'FILESIZE' => $config['max_filesize'])
  101. );
  102. return sizeof($attachment_data);
  103. }
  104. /**
  105. * Get Attachment Data
  106. *
  107. * Grabs attachment data for blogs and replies.
  108. *
  109. * @param int|array $blog_ids An array of blog_ids to look up
  110. * @param int|array|bool $reply_ids An array of reply_ids to look up
  111. */
  112. public function get_attachment_data($blog_ids, $reply_ids = false)
  113. {
  114. global $auth, $config, $db;
  115. if (!$config['user_blog_enable_attachments'] || !$auth->acl_get('u_download'))
  116. {
  117. return;
  118. }
  119. if (!is_array($blog_ids))
  120. {
  121. $blog_ids = array($blog_ids);
  122. }
  123. if (!is_array($reply_ids) && $reply_ids !== false)
  124. {
  125. $reply_ids = array($reply_ids);
  126. }
  127. $temp = compact('blog_ids', 'reply_ids');
  128. blog_plugins::plugin_do_ref('function_get_attachment_data', $temp);
  129. extract($temp);
  130. $blog_ids = array_unique(array_map('intval', $blog_ids));
  131. $reply_ids = ($reply_ids === false) ? false : array_unique(array_map('intval', $reply_ids));
  132. $reply_sql = ($reply_ids !== false) ? ' OR ' . $db->sql_in_set('reply_id', $reply_ids) : '';
  133. $sql = 'SELECT * FROM ' . BLOGS_ATTACHMENT_TABLE . '
  134. WHERE ' . $db->sql_in_set('blog_id', $blog_ids) .
  135. $reply_sql . '
  136. ORDER BY attach_id DESC';
  137. $result = $db->sql_query($sql);
  138. while ($row = $db->sql_fetchrow($result))
  139. {
  140. if ($row['reply_id'] != 0)
  141. {
  142. blog_data::$reply[$row['reply_id']]['attachment_data'][] = $row;
  143. }
  144. else if ($row['blog_id'] != 0)
  145. {
  146. blog_data::$blog[$row['blog_id']]['attachment_data'][] = $row;
  147. }
  148. }
  149. $db->sql_freeresult($result);
  150. }
  151. /**
  152. * Parse Attachments
  153. */
  154. public function parse_attachments($form_name, $submit, $preview, $refresh, &$text)
  155. {
  156. global $config, $auth, $user, $phpbb_root_path, $phpEx, $db, $message_parser;
  157. if (!$config['user_blog_enable_attachments'] || !$auth->acl_get('u_blogattach'))
  158. {
  159. return;
  160. }
  161. $error = array();
  162. $temp = compact('form_name', 'submit', 'preview', 'refresh', 'text');
  163. blog_plugins::plugin_do_ref('function_parse_attachments', $temp);
  164. extract($temp);
  165. $num_attachments = sizeof($this->attachment_data);
  166. $this->filename_data['filecomment'] = utf8_normalize_nfc(request_var('filecomment', '', true));
  167. $upload_file = (isset($_FILES[$form_name]) && $_FILES[$form_name]['name'] != 'none' && trim($_FILES[$form_name]['name'])) ? true : false;
  168. $add_file = (isset($_POST['add_file'])) ? true : false;
  169. $delete_file = (isset($_POST['delete_file'])) ? true : false;
  170. // First of all adjust comments if changed
  171. $actual_comment_list = utf8_normalize_nfc(request_var('comment_list', array(''), true));
  172. foreach ($actual_comment_list as $comment_key => $comment)
  173. {
  174. if (!isset($this->attachment_data[$comment_key]))
  175. {
  176. continue;
  177. }
  178. if ($this->attachment_data[$comment_key]['attach_comment'] != $actual_comment_list[$comment_key])
  179. {
  180. $this->attachment_data[$comment_key]['attach_comment'] = $actual_comment_list[$comment_key];
  181. }
  182. }
  183. if ($submit && $upload_file)
  184. {
  185. if ($num_attachments < $config['user_blog_max_attachments'] || $auth->acl_get('u_blognolimitattach'))
  186. {
  187. $filedata = $this->upload_attachment($form_name, false, '');
  188. $error = $filedata['error'];
  189. if ($filedata['post_attach'] && !sizeof($error))
  190. {
  191. $sql_ary = array(
  192. 'physical_filename' => $filedata['physical_filename'],
  193. 'attach_comment' => $this->filename_data['filecomment'],
  194. 'real_filename' => $filedata['real_filename'],
  195. 'extension' => $filedata['extension'],
  196. 'mimetype' => $filedata['mimetype'],
  197. 'filesize' => $filedata['filesize'],
  198. 'filetime' => $filedata['filetime'],
  199. 'thumbnail' => $filedata['thumbnail'],
  200. 'is_orphan' => 1,
  201. 'poster_id' => $user->data['user_id'],
  202. );
  203. $db->sql_query('INSERT INTO ' . BLOGS_ATTACHMENT_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
  204. unset($sql_ary);
  205. $new_entry = array(
  206. 'attach_id' => $db->sql_nextid(),
  207. 'is_orphan' => 1,
  208. 'real_filename' => $filedata['real_filename'],
  209. 'attach_comment'=> $this->filename_data['filecomment'],
  210. );
  211. $this->attachment_data = array_merge(array(0 => $new_entry), $this->attachment_data);
  212. $message_parser->message = preg_replace('#\[attachment=([0-9]+)\](.*?)\[\/attachment\]#e', "'[attachment='.(\\1 + 1).']\\2[/attachment]'", $message_parser->message);
  213. $this->filename_data['filecomment'] = '';
  214. // This Variable is set to false here, because Attachments are entered into the
  215. // Database in two modes, one if the id_list is 0 and the second one if post_attach is true
  216. // Since post_attach is automatically switched to true if an Attachment got added to the filesystem,
  217. // but we are assigning an id of 0 here, we have to reset the post_attach variable to false.
  218. //
  219. // This is very relevant, because it could happen that the post got not submitted, but we do not
  220. // know this circumstance here. We could be at the posting page or we could be redirected to the entered
  221. // post. :)
  222. $filedata['post_attach'] = false;
  223. }
  224. }
  225. else
  226. {
  227. $error[] = sprintf($user->lang['TOO_MANY_ATTACHMENTS'], $config['user_blog_max_attachments']);
  228. }
  229. }
  230. if ($preview || $refresh || sizeof($error))
  231. {
  232. // Perform actions on temporary attachments
  233. if ($delete_file)
  234. {
  235. include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
  236. $index = array_keys(request_var('delete_file', array(0 => 0)));
  237. $index = (!empty($index)) ? $index[0] : false;
  238. if ($index !== false && !empty($this->attachment_data[$index]))
  239. {
  240. // delete selected attachment
  241. if ($this->attachment_data[$index]['is_orphan'])
  242. {
  243. $sql = 'SELECT attach_id, physical_filename, thumbnail
  244. FROM ' . BLOGS_ATTACHMENT_TABLE . '
  245. WHERE attach_id = ' . (int) $this->attachment_data[$index]['attach_id'] . '
  246. AND is_orphan = 1
  247. AND poster_id = ' . (int) $user->data['user_id'];
  248. $result = $db->sql_query($sql);
  249. $row = $db->sql_fetchrow($result);
  250. $db->sql_freeresult($result);
  251. if ($row)
  252. {
  253. phpbb_unlink($row['physical_filename'], 'file');
  254. if ($row['thumbnail'])
  255. {
  256. phpbb_unlink($row['physical_filename'], 'thumbnail');
  257. }
  258. $db->sql_query('DELETE FROM ' . BLOGS_ATTACHMENT_TABLE . ' WHERE attach_id = ' . (int) $this->attachment_data[$index]['attach_id']);
  259. }
  260. }
  261. else
  262. {
  263. $sql = 'SELECT attach_id, physical_filename, thumbnail
  264. FROM ' . BLOGS_ATTACHMENT_TABLE . '
  265. WHERE attach_id = ' . (int) $this->attachment_data[$index]['attach_id'];
  266. $result = $db->sql_query($sql);
  267. $row = $db->sql_fetchrow($result);
  268. $db->sql_freeresult($result);
  269. if ($row)
  270. {
  271. phpbb_unlink($row['physical_filename'], 'file');
  272. if ($row['thumbnail'])
  273. {
  274. phpbb_unlink($row['physical_filename'], 'thumbnail');
  275. }
  276. $db->sql_query('DELETE FROM ' . BLOGS_ATTACHMENT_TABLE . ' WHERE attach_id = ' . (int) $this->attachment_data[$index]['attach_id']);
  277. }
  278. }
  279. unset($this->attachment_data[$index]);
  280. $text = preg_replace('#\[attachment=([0-9]+)\](.*?)\[\/attachment\]#e', "(\\1 == \$index) ? '' : ((\\1 > \$index) ? '[attachment=' . (\\1 - 1) . ']\\2[/attachment]' : '\\0')", $text);
  281. $message_parser->message = preg_replace('#\[attachment=([0-9]+)\](.*?)\[\/attachment\]#e', "(\\1 == \$index) ? '' : ((\\1 > \$index) ? '[attachment=' . (\\1 - 1) . ']\\2[/attachment]' : '\\0')", $message_parser->message);
  282. // Reindex Array
  283. $this->attachment_data = array_values($this->attachment_data);
  284. }
  285. }
  286. else if (($add_file || $preview) && $upload_file)
  287. {
  288. if ($num_attachments < $config['user_blog_max_attachments'] || $auth->acl_get('u_blognolimitattach'))
  289. {
  290. $filedata = $this->upload_attachment($form_name, false, '');
  291. $error = array_merge($error, $filedata['error']);
  292. if (!sizeof($error))
  293. {
  294. $sql_ary = array(
  295. 'physical_filename' => $filedata['physical_filename'],
  296. 'attach_comment' => $this->filename_data['filecomment'],
  297. 'real_filename' => $filedata['real_filename'],
  298. 'extension' => $filedata['extension'],
  299. 'mimetype' => $filedata['mimetype'],
  300. 'filesize' => $filedata['filesize'],
  301. 'filetime' => $filedata['filetime'],
  302. 'thumbnail' => $filedata['thumbnail'],
  303. 'is_orphan' => 1,
  304. 'poster_id' => $user->data['user_id'],
  305. );
  306. $db->sql_query('INSERT INTO ' . BLOGS_ATTACHMENT_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
  307. $new_entry = array(
  308. 'attach_id' => $db->sql_nextid(),
  309. 'is_orphan' => 1,
  310. 'real_filename' => $filedata['real_filename'],
  311. 'attach_comment'=> $this->filename_data['filecomment'],
  312. );
  313. $this->attachment_data = array_merge(array(0 => $new_entry), $this->attachment_data);
  314. $text = preg_replace('#\[attachment=([0-9]+)\](.*?)\[\/attachment\]#e', "'[attachment='.(\\1 + 1).']\\2[/attachment]'", $text);
  315. $message_parser->message = preg_replace('#\[attachment=([0-9]+):' . $message_parser->bbcode_uid . '\](.*?)\[\/attachment:' . $message_parser->bbcode_uid . '\]#e', "'[attachment='.(\\1 + 1).':{$message_parser->bbcode_uid}]\\2[/attachment:{$message_parser->bbcode_uid}]'", $message_parser->message);
  316. $this->filename_data['filecomment'] = '';
  317. }
  318. }
  319. else
  320. {
  321. $error[] = sprintf($user->lang['TOO_MANY_ATTACHMENTS'], $config['user_blog_max_attachments']);
  322. }
  323. }
  324. }
  325. foreach ($error as $error_msg)
  326. {
  327. $this->warn_msg[] = $error_msg;
  328. }
  329. unset($error);
  330. }
  331. /**
  332. * Get Attachment Data
  333. */
  334. public function get_submitted_attachment_data($check_user_id = false)
  335. {
  336. global $user, $db, $config, $auth;
  337. if (!$config['user_blog_enable_attachments'] || !$auth->acl_get('u_blogattach'))
  338. {
  339. return;
  340. }
  341. blog_plugins::plugin_do('get_submitted_attachment_data');
  342. $this->filename_data['filecomment'] = utf8_normalize_nfc(request_var('filecomment', '', true));
  343. $attachment_data = (isset($_POST['attachment_data'])) ? $_POST['attachment_data'] : array();
  344. $this->attachment_data = array();
  345. $check_user_id = ($check_user_id === false) ? $user->data['user_id'] : $check_user_id;
  346. if (!sizeof($attachment_data))
  347. {
  348. return;
  349. }
  350. $not_orphan = $orphan = array();
  351. foreach ($attachment_data as $pos => $var_ary)
  352. {
  353. if ($var_ary['is_orphan'])
  354. {
  355. $orphan[(int) $var_ary['attach_id']] = $pos;
  356. }
  357. else
  358. {
  359. $not_orphan[(int) $var_ary['attach_id']] = $pos;
  360. }
  361. }
  362. // Regenerate already posted attachments
  363. if (sizeof($not_orphan))
  364. {
  365. // Get the attachment data, based on the poster id...
  366. $sql = 'SELECT attach_id, is_orphan, real_filename, attach_comment
  367. FROM ' . BLOGS_ATTACHMENT_TABLE . '
  368. WHERE ' . $db->sql_in_set('attach_id', array_unique(array_map('intval', array_keys($not_orphan)))) . '
  369. AND poster_id = ' . $check_user_id;
  370. $result = $db->sql_query($sql);
  371. while ($row = $db->sql_fetchrow($result))
  372. {
  373. $pos = $not_orphan[$row['attach_id']];
  374. $this->attachment_data[$pos] = $row;
  375. set_var($this->attachment_data[$pos]['attach_comment'], $_POST['attachment_data'][$pos]['attach_comment'], 'string', true);
  376. unset($not_orphan[$row['attach_id']]);
  377. }
  378. $db->sql_freeresult($result);
  379. }
  380. if (sizeof($not_orphan))
  381. {
  382. trigger_error('NO_ACCESS_ATTACHMENT', E_USER_ERROR);
  383. }
  384. // Regenerate newly uploaded attachments
  385. if (sizeof($orphan))
  386. {
  387. $sql = 'SELECT attach_id, is_orphan, real_filename, attach_comment
  388. FROM ' . BLOGS_ATTACHMENT_TABLE . '
  389. WHERE ' . $db->sql_in_set('attach_id', array_unique(array_map('intval', array_keys($orphan)))) . '
  390. AND poster_id = ' . $user->data['user_id'] . '
  391. AND is_orphan = 1';
  392. $result = $db->sql_query($sql);
  393. while ($row = $db->sql_fetchrow($result))
  394. {
  395. $pos = $orphan[$row['attach_id']];
  396. $this->attachment_data[$pos] = $row;
  397. set_var($this->attachment_data[$pos]['attach_comment'], $_POST['attachment_data'][$pos]['attach_comment'], 'string', true);
  398. unset($orphan[$row['attach_id']]);
  399. }
  400. $db->sql_freeresult($result);
  401. }
  402. if (sizeof($orphan))
  403. {
  404. trigger_error('NO_ACCESS_ATTACHMENT', E_USER_ERROR);
  405. }
  406. ksort($this->attachment_data);
  407. }
  408. /**
  409. * Upload Attachment - filedata is generated here
  410. * Uses upload class
  411. */
  412. public function upload_attachment($form_name, $local = false, $local_storage = '', $local_filedata = false)
  413. {
  414. global $auth, $user, $config, $db, $phpbb_root_path, $phpEx;
  415. if (!$config['user_blog_enable_attachments'] || !$auth->acl_get('u_blogattach'))
  416. {
  417. return;
  418. }
  419. $filedata = array(
  420. 'error' => array()
  421. );
  422. if (!class_exists('fileupload'))
  423. {
  424. include($phpbb_root_path . 'includes/functions_upload.' . $phpEx);
  425. }
  426. $upload = new fileupload();
  427. if (!$local)
  428. {
  429. $filedata['post_attach'] = ($upload->is_valid($form_name)) ? true : false;
  430. }
  431. else
  432. {
  433. $filedata['post_attach'] = true;
  434. }
  435. if (!$filedata['post_attach'])
  436. {
  437. $filedata['error'][] = $user->lang['NO_UPLOAD_FORM_FOUND'];
  438. return $filedata;
  439. }
  440. $extensions = $this->obtain_blog_attach_extensions();
  441. $upload->set_allowed_extensions(array_keys($extensions['_allowed_']));
  442. $file = ($local) ? $upload->local_upload($local_storage, $local_filedata) : $upload->form_upload($form_name);
  443. if ($file->init_error)
  444. {
  445. $filedata['post_attach'] = false;
  446. return $filedata;
  447. }
  448. $cat_id = (isset($extensions[$file->get('extension')]['display_cat'])) ? $extensions[$file->get('extension')]['display_cat'] : ATTACHMENT_CATEGORY_NONE;
  449. // Make sure the image category only holds valid images...
  450. if ($cat_id == ATTACHMENT_CATEGORY_IMAGE && !$file->is_image())
  451. {
  452. $file->remove();
  453. // If this error occurs a user tried to exploit an IE Bug by renaming extensions
  454. // Since the image category is displaying content inline we need to catch this.
  455. trigger_error($user->lang['ATTACHED_IMAGE_NOT_IMAGE']);
  456. }
  457. // Do we have to create a thumbnail?
  458. $filedata['thumbnail'] = ($cat_id == ATTACHMENT_CATEGORY_IMAGE && $config['img_create_thumbnail']) ? 1 : 0;
  459. // Check Image Size, if it is an image
  460. if (!$auth->acl_get('u_blognolimitattach') && $cat_id == ATTACHMENT_CATEGORY_IMAGE)
  461. {
  462. $file->upload->set_allowed_dimensions(0, 0, $config['img_max_width'], $config['img_max_height']);
  463. }
  464. // Admins and mods are allowed to exceed the allowed filesize
  465. if (!$auth->acl_get('u_blognolimitattach'))
  466. {
  467. if (!empty($extensions[$file->get('extension')]['max_filesize']))
  468. {
  469. $allowed_filesize = $extensions[$file->get('extension')]['max_filesize'];
  470. }
  471. else
  472. {
  473. $allowed_filesize = $config['max_filesize'];
  474. }
  475. $file->upload->set_max_filesize($allowed_filesize);
  476. }
  477. $file->clean_filename('unique', $user->data['user_id'] . '_');
  478. // Are we uploading an image *and* this image being within the image category? Only then perform additional image checks.
  479. $no_image = ($cat_id == ATTACHMENT_CATEGORY_IMAGE) ? false : true;
  480. if (!$file->move_file($config['upload_path'] . '/blog_mod', false, $no_image))
  481. {
  482. $file->error[] = sprintf($user->lang[$file->upload->error_prefix . 'GENERAL_UPLOAD_ERROR'], $config['upload_path'] . '/blog_mod');
  483. }
  484. if (sizeof($file->error))
  485. {
  486. $file->remove();
  487. $filedata['error'] = array_merge($filedata['error'], $file->error);
  488. $filedata['post_attach'] = false;
  489. return $filedata;
  490. }
  491. $filedata['filesize'] = $file->get('filesize');
  492. $filedata['mimetype'] = $file->get('mimetype');
  493. $filedata['extension'] = $file->get('extension');
  494. $filedata['physical_filename'] = $file->get('realname');
  495. $filedata['real_filename'] = $file->get('uploadname');
  496. $filedata['filetime'] = time();
  497. // Check our complete quota
  498. if ($config['attachment_quota'])
  499. {
  500. if ($config['upload_dir_size'] + $file->get('filesize') > $config['attachment_quota'])
  501. {
  502. $filedata['error'][] = $user->lang['ATTACH_QUOTA_REACHED'];
  503. $filedata['post_attach'] = false;
  504. $file->remove();
  505. return $filedata;
  506. }
  507. }
  508. // Check free disk space
  509. if ($free_space = @disk_free_space($phpbb_root_path . $config['upload_path']))
  510. {
  511. if ($free_space <= $file->get('filesize'))
  512. {
  513. $filedata['error'][] = $user->lang['ATTACH_QUOTA_REACHED'];
  514. $filedata['post_attach'] = false;
  515. $file->remove();
  516. return $filedata;
  517. }
  518. }
  519. // Create Thumbnail
  520. if ($filedata['thumbnail'])
  521. {
  522. $source = $file->get('destination_file');
  523. $destination = $file->get('destination_path') . '/thumb_' . $file->get('realname');
  524. if (!create_thumbnail($source, $destination, $file->get('mimetype')))
  525. {
  526. $filedata['thumbnail'] = 0;
  527. }
  528. }
  529. $temp = compact('form_name', 'local', 'local_storage', 'local_filedata', 'filedata');
  530. blog_plugins::plugin_do_ref('function_upload_attachment', $temp);
  531. extract($temp);
  532. return $filedata;
  533. }
  534. /**
  535. * Obtain allowed extensions
  536. *
  537. * @return array allowed extensions array.
  538. */
  539. public function obtain_blog_attach_extensions()
  540. {
  541. global $cache, $config;
  542. if (!$config['user_blog_enable_attachments'])
  543. {
  544. return;
  545. }
  546. if (($extensions = $cache->get('_blog_extensions')) === false)
  547. {
  548. global $db;
  549. $extensions = array(
  550. '_allowed_blog' => array(),
  551. );
  552. // The rule is to only allow those extensions defined. ;)
  553. $sql = 'SELECT e.extension, g.*
  554. FROM ' . EXTENSIONS_TABLE . ' e, ' . EXTENSION_GROUPS_TABLE . ' g
  555. WHERE e.group_id = g.group_id
  556. AND (g.allow_group = 1 OR g.allow_in_blog = 1)';
  557. $result = $db->sql_query($sql);
  558. while ($row = $db->sql_fetchrow($result))
  559. {
  560. $extension = strtolower(trim($row['extension']));
  561. $extensions[$extension] = array(
  562. 'display_cat' => (int) $row['cat_id'],
  563. 'download_mode' => (int) $row['download_mode'],
  564. 'upload_icon' => trim($row['upload_icon']),
  565. 'max_filesize' => (int) $row['max_filesize'],
  566. 'allow_group' => $row['allow_group'],
  567. 'allow_in_blog' => $row['allow_in_blog'],
  568. );
  569. if ($row['allow_in_blog'])
  570. {
  571. $extensions['_allowed_blog'][$extension] = 0;
  572. }
  573. }
  574. $db->sql_freeresult($result);
  575. $cache->put('_blog_extensions', $extensions);
  576. }
  577. $return = array('_allowed_' => array());
  578. foreach ($extensions['_allowed_blog'] as $extension => $check)
  579. {
  580. $return['_allowed_'][$extension] = 0;
  581. $return[$extension] = $extensions[$extension];
  582. }
  583. blog_plugins::plugin_do_ref('function_obtain_blog_attach_extensions', $return);
  584. return $return;
  585. }
  586. /**
  587. * General attachment parsing
  588. *
  589. * @param string &$message The post/private message
  590. * @param array &$attachments The attachments to parse for (inline) display. The attachments array will hold templated data after parsing.
  591. * @param array &$update_count The attachment counts to be updated - will be filled
  592. * @param bool $preview If set to true the attachments are parsed for preview. Within preview mode the comments are fetched from the given $attachments array and not fetched from the database.
  593. */
  594. public function parse_attachments_for_view(&$message, &$attachments, &$update_count, $preview = false)
  595. {
  596. global $template, $user, $config, $phpbb_root_path, $auth;
  597. if (!$config['user_blog_enable_attachments'] || !sizeof($attachments) || !$auth->acl_get('u_download'))
  598. {
  599. return;
  600. }
  601. $compiled_attachments = array();
  602. $temp = compact('message', 'attachments', 'update_count', 'preview', 'compiled_attachments');
  603. blog_plugins::plugin_do_ref('function_parse_attachments_for_view', $temp);
  604. extract($temp);
  605. if (!isset($template->filename['attachment_tpl']))
  606. {
  607. $template->set_filenames(array(
  608. 'attachment_tpl' => 'attachment.html')
  609. );
  610. }
  611. $extensions = $this->obtain_blog_attach_extensions();
  612. // Look for missing attachment information...
  613. $attach_ids = array();
  614. foreach ($attachments as $pos => $attachment)
  615. {
  616. // If is_orphan is set, we need to retrieve the attachments again...
  617. if (!isset($attachment['extension']) && !isset($attachment['physical_filename']))
  618. {
  619. $attach_ids[(int) $attachment['attach_id']] = $pos;
  620. }
  621. }
  622. // Grab attachments (security precaution)
  623. if (sizeof($attach_ids))
  624. {
  625. global $db;
  626. $new_attachment_data = array();
  627. $sql = 'SELECT *
  628. FROM ' . BLOGS_ATTACHMENT_TABLE . '
  629. WHERE ' . $db->sql_in_set('attach_id', array_unique(array_map('intval', array_keys($attach_ids))));
  630. $result = $db->sql_query($sql);
  631. while ($row = $db->sql_fetchrow($result))
  632. {
  633. if (!isset($attach_ids[$row['attach_id']]))
  634. {
  635. continue;
  636. }
  637. // If we preview attachments we will set some retrieved values here
  638. if ($preview)
  639. {
  640. $row['attach_comment'] = $attachments[$attach_ids[$row['attach_id']]]['attach_comment'];
  641. }
  642. $new_attachment_data[$attach_ids[$row['attach_id']]] = $row;
  643. }
  644. $db->sql_freeresult($result);
  645. $attachments = $new_attachment_data;
  646. unset($new_attachment_data);
  647. }
  648. ksort($attachments);
  649. foreach ($attachments as $attachment)
  650. {
  651. if (!sizeof($attachment))
  652. {
  653. continue;
  654. }
  655. // We need to reset/empty the _file block var, because this function might be called more than once
  656. $template->destroy_block_vars('_file');
  657. $block_array = array();
  658. // Some basics...
  659. $attachment['extension'] = strtolower(trim($attachment['extension']));
  660. $filename = $phpbb_root_path . $config['upload_path'] . '/blog_mod/' . basename($attachment['physical_filename']);
  661. $thumbnail_filename = $phpbb_root_path . $config['upload_path'] . '/blog_mod/thumb_' . basename($attachment['physical_filename']);
  662. $upload_icon = '';
  663. if (isset($extensions[$attachment['extension']]))
  664. {
  665. if ($user->img('icon_topic_attach', '') && !$extensions[$attachment['extension']]['upload_icon'])
  666. {
  667. $upload_icon = $user->img('icon_topic_attach', '');
  668. }
  669. else if ($extensions[$attachment['extension']]['upload_icon'])
  670. {
  671. $upload_icon = '<img src="' . $phpbb_root_path . $config['upload_icons_path'] . '/' . trim($extensions[$attachment['extension']]['upload_icon']) . '" alt="" />';
  672. }
  673. }
  674. $filesize = $attachment['filesize'];
  675. $size_lang = ($filesize >= 1048576) ? $user->lang['MB'] : ( ($filesize >= 1024) ? $user->lang['KB'] : $user->lang['BYTES'] );
  676. $filesize = ($filesize >= 1048576) ? round((round($filesize / 1048576 * 100) / 100), 2) : (($filesize >= 1024) ? round((round($filesize / 1024 * 100) / 100), 2) : $filesize);
  677. $comment = str_replace("\n", '<br />', censor_text($attachment['attach_comment']));
  678. $block_array += array(
  679. 'UPLOAD_ICON' => $upload_icon,
  680. 'FILESIZE' => $filesize,
  681. 'SIZE_LANG' => $size_lang,
  682. 'DOWNLOAD_NAME' => basename($attachment['real_filename']),
  683. 'COMMENT' => $comment,
  684. );
  685. $denied = false;
  686. if (!isset($extensions['_allowed_'][$attachment['extension']]))
  687. {
  688. $denied = true;
  689. $block_array += array(
  690. 'S_DENIED' => true,
  691. 'DENIED_MESSAGE' => sprintf($user->lang['EXTENSION_DISABLED_AFTER_POSTING'], $attachment['extension'])
  692. );
  693. }
  694. if (!$denied)
  695. {
  696. $l_downloaded_viewed = $download_link = '';
  697. $display_cat = $extensions[$attachment['extension']]['display_cat'];
  698. if ($display_cat == ATTACHMENT_CATEGORY_IMAGE)
  699. {
  700. if ($attachment['thumbnail'])
  701. {
  702. $display_cat = ATTACHMENT_CATEGORY_THUMB;
  703. }
  704. else
  705. {
  706. if ($config['img_display_inlined'])
  707. {
  708. if ($config['img_link_width'] || $config['img_link_height'])
  709. {
  710. $dimension = @getimagesize($filename);
  711. // If the dimensions could not be determined or the image being 0x0 we display it as a link for safety purposes
  712. if ($dimension === false || empty($dimension[0]) || empty($dimension[1]))
  713. {
  714. $display_cat = ATTACHMENT_CATEGORY_NONE;
  715. }
  716. else
  717. {
  718. $display_cat = ($dimension[0] <= $config['img_link_width'] && $dimension[1] <= $config['img_link_height']) ? ATTACHMENT_CATEGORY_IMAGE : ATTACHMENT_CATEGORY_NONE;
  719. }
  720. }
  721. }
  722. else
  723. {
  724. $display_cat = ATTACHMENT_CATEGORY_NONE;
  725. }
  726. }
  727. }
  728. // Make some descisions based on user options being set.
  729. if (($display_cat == ATTACHMENT_CATEGORY_IMAGE || $display_cat == ATTACHMENT_CATEGORY_THUMB) && !$user->optionget('viewimg'))
  730. {
  731. $display_cat = ATTACHMENT_CATEGORY_NONE;
  732. }
  733. if ($display_cat == ATTACHMENT_CATEGORY_FLASH && !$user->optionget('viewflash'))
  734. {
  735. $display_cat = ATTACHMENT_CATEGORY_NONE;
  736. }
  737. $download_link = blog_url(false, false, false, array('page' => 'download', 'mode' => 'download', 'id' => $attachment['attach_id']));
  738. switch ($display_cat)
  739. {
  740. // Images
  741. case ATTACHMENT_CATEGORY_IMAGE:
  742. $l_downloaded_viewed = 'VIEWED_COUNT';
  743. $inline_link = blog_url(false, false, false, array('page' => 'download', 'mode' => 'download', 'id' => $attachment['attach_id']));
  744. $block_array += array(
  745. 'S_IMAGE' => true,
  746. 'U_INLINE_LINK' => $inline_link,
  747. );
  748. $update_count[] = $attachment['attach_id'];
  749. break;
  750. // Images, but display Thumbnail
  751. case ATTACHMENT_CATEGORY_THUMB:
  752. $l_downloaded_viewed = 'VIEWED_COUNT';
  753. $thumbnail_link = blog_url(false, false, false, array('page' => 'download', 'mode' => 'thumbnail', 'id' => $attachment['attach_id']));
  754. $block_array += array(
  755. 'S_THUMBNAIL' => true,
  756. 'THUMB_IMAGE' => $thumbnail_link,
  757. );
  758. break;
  759. // Windows Media Streams
  760. case ATTACHMENT_CATEGORY_WM:
  761. $l_downloaded_viewed = 'VIEWED_COUNT';
  762. // Giving the filename directly because within the wm object all variables are in local context making it impossible
  763. // to validate against a valid session (all params can differ)
  764. // $download_link = $filename;
  765. $block_array += array(
  766. 'U_FORUM' => generate_board_url(),
  767. 'ATTACH_ID' => $attachment['attach_id'],
  768. 'S_WM_FILE' => true,
  769. );
  770. // Viewed/Heared File ... update the download count
  771. $update_count[] = $attachment['attach_id'];
  772. break;
  773. // Real Media Streams
  774. case ATTACHMENT_CATEGORY_RM:
  775. case ATTACHMENT_CATEGORY_QUICKTIME:
  776. $l_downloaded_viewed = 'VIEWED_COUNT';
  777. $block_array += array(
  778. 'S_RM_FILE' => ($display_cat == ATTACHMENT_CATEGORY_RM) ? true : false,
  779. 'S_QUICKTIME_FILE' => ($display_cat == ATTACHMENT_CATEGORY_QUICKTIME) ? true : false,
  780. 'U_FORUM' => generate_board_url(),
  781. 'ATTACH_ID' => $attachment['attach_id'],
  782. );
  783. // Viewed/Heared File ... update the download count
  784. $update_count[] = $attachment['attach_id'];
  785. break;
  786. // Macromedia Flash Files
  787. case ATTACHMENT_CATEGORY_FLASH:
  788. list($width, $height) = @getimagesize($filename);
  789. $l_downloaded_viewed = 'VIEWED_COUNT';
  790. $block_array += array(
  791. 'S_FLASH_FILE' => true,
  792. 'WIDTH' => $width,
  793. 'HEIGHT' => $height,
  794. );
  795. // Viewed/Heared File ... update the download count
  796. $update_count[] = $attachment['attach_id'];
  797. break;
  798. default:
  799. $l_downloaded_viewed = 'DOWNLOAD_COUNT';
  800. $block_array += array(
  801. 'S_FILE' => true,
  802. );
  803. break;
  804. }
  805. $l_download_count = (!isset($attachment['download_count']) || $attachment['download_count'] == 0) ? $user->lang[$l_downloaded_viewed . '_NONE'] : (($attachment['download_count'] == 1) ? sprintf($user->lang[$l_downloaded_viewed], $attachment['download_count']) : sprintf($user->lang[$l_downloaded_viewed . 'S'], $attachment['download_count']));
  806. $block_array += array(
  807. 'U_DOWNLOAD_LINK' => $download_link,
  808. 'L_DOWNLOAD_COUNT' => $l_download_count
  809. );
  810. }
  811. $template->assign_block_vars('_file', $block_array);
  812. $compiled_attachments[] = $template->assign_display('attachment_tpl');
  813. }
  814. $attachments = $compiled_attachments;
  815. unset($compiled_attachments);
  816. $tpl_size = sizeof($attachments);
  817. $unset_tpl = array();
  818. preg_match_all('#<!\-\- ia([0-9]+) \-\->(.*?)<!\-\- ia\1 \-\->#', $message, $matches, PREG_PATTERN_ORDER);
  819. $replace = array();
  820. foreach ($matches[0] as $num => $capture)
  821. {
  822. // Flip index if we are displaying the reverse way
  823. $index = ($config['display_order']) ? ($tpl_size-($matches[1][$num] + 1)) : $matches[1][$num];
  824. $replace['from'][] = $matches[0][$num];
  825. $replace['to'][] = (isset($attachments[$index])) ? $attachments[$index] : sprintf($user->lang['MISSING_INLINE_ATTACHMENT'], $matches[2][array_search($index, $matches[1])]);
  826. $unset_tpl[] = $index;
  827. }
  828. if (isset($replace['from']))
  829. {
  830. $message = str_replace($replace['from'], $replace['to'], $message);
  831. }
  832. $unset_tpl = array_unique($unset_tpl);
  833. // Needed to let not display the inlined attachments at the end of the post again
  834. foreach ($unset_tpl as $index)
  835. {
  836. unset($attachments[$index]);
  837. }
  838. }
  839. }
  840. ?>