Drafts.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. <?php
  2. /**
  3. * This file contains all the functions that allow for the saving,
  4. * retrieving, deleting and settings for the drafts function.
  5. *
  6. * Simple Machines Forum (SMF)
  7. *
  8. * @package SMF
  9. * @author Simple Machines http://www.simplemachines.org
  10. * @copyright 2014 Simple Machines and individual contributors
  11. * @license http://www.simplemachines.org/about/smf/license.php BSD
  12. *
  13. * @version 2.1 Alpha 1
  14. */
  15. if (!defined('SMF'))
  16. die('No direct access...');
  17. loadLanguage('Drafts');
  18. /**
  19. * Saves a post draft in the user_drafts table
  20. * The core draft feature must be enabled, as well as the post draft option
  21. * Determines if this is a new or an existing draft
  22. * Returns errors in $post_errors for display in the template
  23. *
  24. * @param string $post_errors Any errors encountered trying to save this draft
  25. * @return boolean Always returns true
  26. */
  27. function SaveDraft(&$post_errors)
  28. {
  29. global $context, $user_info, $smcFunc, $modSettings, $board;
  30. // can you be, should you be ... here?
  31. if (empty($modSettings['drafts_post_enabled']) || !allowedTo('post_draft') || !isset($_POST['save_draft']) || !isset($_POST['id_draft']))
  32. return false;
  33. // read in what they sent us, if anything
  34. $id_draft = (int) $_POST['id_draft'];
  35. $draft_info = ReadDraft($id_draft);
  36. // A draft has been saved less than 5 seconds ago, let's not do the autosave again
  37. if (isset($_REQUEST['xml']) && !empty($draft_info['poster_time']) && time() < $draft_info['poster_time'] + 5)
  38. {
  39. $context['draft_saved_on'] = $draft_info['poster_time'];
  40. // since we were called from the autosave function, send something back
  41. if (!empty($id_draft))
  42. XmlDraft($id_draft);
  43. return true;
  44. }
  45. // prepare any data from the form
  46. $topic_id = empty($_REQUEST['topic']) ? 0 : (int) $_REQUEST['topic'];
  47. $draft['icon'] = empty($_POST['icon']) ? 'xx' : preg_replace('~[\./\\\\*:"\'<>]~', '', $_POST['icon']);
  48. $draft['smileys_enabled'] = isset($_POST['ns']) ? (int) $_POST['ns'] : 0;
  49. $draft['locked'] = isset($_POST['lock']) ? (int) $_POST['lock'] : 0;
  50. $draft['sticky'] = isset($_POST['sticky']) ? (int) $_POST['sticky'] : 0;
  51. $draft['subject'] = strtr($smcFunc['htmlspecialchars']($_POST['subject']), array("\r" => '', "\n" => '', "\t" => ''));
  52. $draft['body'] = $smcFunc['htmlspecialchars']($_POST['message'], ENT_QUOTES);
  53. // message and subject still need a bit more work
  54. preparsecode($draft['body']);
  55. if ($smcFunc['strlen']($draft['subject']) > 100)
  56. $draft['subject'] = $smcFunc['substr']($draft['subject'], 0, 100);
  57. // Modifying an existing draft, like hitting the save draft button or autosave enabled?
  58. if (!empty($id_draft) && !empty($draft_info) && $draft_info['id_member'] == $user_info['id'])
  59. {
  60. $smcFunc['db_query']('', '
  61. UPDATE {db_prefix}user_drafts
  62. SET
  63. id_topic = {int:id_topic},
  64. id_board = {int:id_board},
  65. poster_time = {int:poster_time},
  66. subject = {string:subject},
  67. smileys_enabled = {int:smileys_enabled},
  68. body = {string:body},
  69. icon = {string:icon},
  70. locked = {int:locked},
  71. is_sticky = {int:is_sticky}
  72. WHERE id_draft = {int:id_draft}',
  73. array (
  74. 'id_topic' => $topic_id,
  75. 'id_board' => $board,
  76. 'poster_time' => time(),
  77. 'subject' => $draft['subject'],
  78. 'smileys_enabled' => (int) $draft['smileys_enabled'],
  79. 'body' => $draft['body'],
  80. 'icon' => $draft['icon'],
  81. 'locked' => $draft['locked'],
  82. 'is_sticky' => $draft['sticky'],
  83. 'id_draft' => $id_draft,
  84. )
  85. );
  86. // some items to return to the form
  87. $context['draft_saved'] = true;
  88. $context['id_draft'] = $id_draft;
  89. // cleanup
  90. unset($_POST['save_draft']);
  91. }
  92. // otherwise creating a new draft
  93. else
  94. {
  95. $smcFunc['db_insert']('',
  96. '{db_prefix}user_drafts',
  97. array(
  98. 'id_topic' => 'int',
  99. 'id_board' => 'int',
  100. 'type' => 'int',
  101. 'poster_time' => 'int',
  102. 'id_member' => 'int',
  103. 'subject' => 'string-255',
  104. 'smileys_enabled' => 'int',
  105. 'body' => (!empty($modSettings['max_messageLength']) && $modSettings['max_messageLength'] > 65534 ? 'string-' . $modSettings['max_messageLength'] : 'string-65534'),
  106. 'icon' => 'string-16',
  107. 'locked' => 'int',
  108. 'is_sticky' => 'int'
  109. ),
  110. array(
  111. $topic_id,
  112. $board,
  113. 0,
  114. time(),
  115. $user_info['id'],
  116. $draft['subject'],
  117. $draft['smileys_enabled'],
  118. $draft['body'],
  119. $draft['icon'],
  120. $draft['locked'],
  121. $draft['sticky']
  122. ),
  123. array(
  124. 'id_draft'
  125. )
  126. );
  127. // get the id of the new draft
  128. $id_draft = $smcFunc['db_insert_id']('{db_prefix}user_drafts', 'id_draft');
  129. // everything go as expected?
  130. if (!empty($id_draft))
  131. {
  132. $context['draft_saved'] = true;
  133. $context['id_draft'] = $id_draft;
  134. }
  135. else
  136. $post_errors[] = 'draft_not_saved';
  137. // cleanup
  138. unset($_POST['save_draft']);
  139. }
  140. // if we were called from the autosave function, send something back
  141. if (!empty($id_draft) && isset($_REQUEST['xml']) && (!in_array('session_timeout', $post_errors)))
  142. {
  143. $context['draft_saved_on'] = time();
  144. XmlDraft($id_draft);
  145. }
  146. return true;
  147. }
  148. /**
  149. * Saves a PM draft in the user_drafts table
  150. * The core draft feature must be enabled, as well as the pm draft option
  151. * Determines if this is a new or and update to an existing pm draft
  152. *
  153. * @param string $post_errors A string of info about errors encountered trying to save this draft
  154. * @param array $recipientList An array of data about who this PM is being sent to
  155. * @return boolean false if you can't save the draft, true if we're doing this via XML more than 5 seconds after the last save, nothing otherwise
  156. */
  157. function SavePMDraft(&$post_errors, $recipientList)
  158. {
  159. global $context, $user_info, $smcFunc, $modSettings;
  160. // PM survey says ... can you stay or must you go
  161. if (empty($modSettings['drafts_pm_enabled']) || !allowedTo('pm_draft') || !isset($_POST['save_draft']))
  162. return false;
  163. // read in what you sent us
  164. $id_pm_draft = (int) $_POST['id_pm_draft'];
  165. $draft_info = ReadDraft($id_pm_draft, 1);
  166. // 5 seconds is the same limit we have for posting
  167. if (isset($_REQUEST['xml']) && !empty($draft_info['poster_time']) && time() < $draft_info['poster_time'] + 5)
  168. {
  169. $context['draft_saved_on'] = $draft_info['poster_time'];
  170. // Send something back to the javascript caller
  171. if (!empty($id_draft))
  172. XmlDraft($id_draft);
  173. return true;
  174. }
  175. // determine who this is being sent to
  176. if (isset($_REQUEST['xml']))
  177. {
  178. $recipientList['to'] = isset($_POST['recipient_to']) ? explode(',', $_POST['recipient_to']) : array();
  179. $recipientList['bcc'] = isset($_POST['recipient_bcc']) ? explode(',', $_POST['recipient_bcc']) : array();
  180. }
  181. elseif (!empty($draft_info['to_list']) && empty($recipientList))
  182. $recipientList = unserialize($draft_info['to_list']);
  183. // prepare the data we got from the form
  184. $reply_id = empty($_POST['replied_to']) ? 0 : (int) $_POST['replied_to'];
  185. $draft['body'] = $smcFunc['htmlspecialchars']($_POST['message'], ENT_QUOTES);
  186. $draft['subject'] = strtr($smcFunc['htmlspecialchars']($_POST['subject']), array("\r" => '', "\n" => '', "\t" => ''));
  187. // message and subject always need a bit more work
  188. preparsecode($draft['body']);
  189. if ($smcFunc['strlen']($draft['subject']) > 100)
  190. $draft['subject'] = $smcFunc['substr']($draft['subject'], 0, 100);
  191. // Modifying an existing PM draft?
  192. if (!empty($id_pm_draft) && !empty($draft_info) && $draft_info['id_member'] == $user_info['id'])
  193. {
  194. $smcFunc['db_query']('', '
  195. UPDATE {db_prefix}user_drafts
  196. SET id_reply = {int:id_reply},
  197. type = {int:type},
  198. poster_time = {int:poster_time},
  199. subject = {string:subject},
  200. body = {string:body},
  201. to_list = {string:to_list}
  202. WHERE id_draft = {int:id_pm_draft}
  203. LIMIT 1',
  204. array(
  205. 'id_reply' => $reply_id,
  206. 'type' => 1,
  207. 'poster_time' => time(),
  208. 'subject' => $draft['subject'],
  209. 'body' => $draft['body'],
  210. 'id_pm_draft' => $id_pm_draft,
  211. 'to_list' => serialize($recipientList),
  212. )
  213. );
  214. // some items to return to the form
  215. $context['draft_saved'] = true;
  216. $context['id_pm_draft'] = $id_pm_draft;
  217. }
  218. // otherwise creating a new PM draft.
  219. else
  220. {
  221. $smcFunc['db_insert']('',
  222. '{db_prefix}user_drafts',
  223. array(
  224. 'id_reply' => 'int',
  225. 'type' => 'int',
  226. 'poster_time' => 'int',
  227. 'id_member' => 'int',
  228. 'subject' => 'string-255',
  229. 'body' => 'string-65534',
  230. 'to_list' => 'string-255',
  231. ),
  232. array(
  233. $reply_id,
  234. 1,
  235. time(),
  236. $user_info['id'],
  237. $draft['subject'],
  238. $draft['body'],
  239. serialize($recipientList),
  240. ),
  241. array(
  242. 'id_draft'
  243. )
  244. );
  245. // get the new id
  246. $id_pm_draft = $smcFunc['db_insert_id']('{db_prefix}user_drafts', 'id_draft');
  247. // everything go as expected, if not toss back an error
  248. if (!empty($id_pm_draft))
  249. {
  250. $context['draft_saved'] = true;
  251. $context['id_pm_draft'] = $id_pm_draft;
  252. }
  253. else
  254. $post_errors[] = 'draft_not_saved';
  255. }
  256. // if we were called from the autosave function, send something back
  257. if (!empty($id_pm_draft) && isset($_REQUEST['xml']) && !in_array('session_timeout', $post_errors))
  258. {
  259. $context['draft_saved_on'] = time();
  260. XmlDraft($id_pm_draft);
  261. }
  262. return;
  263. }
  264. /**
  265. * Reads a draft in from the user_drafts table
  266. * Validates that the draft is the user''s draft
  267. * Optionally loads the draft in to context or superglobal for loading in to the form
  268. *
  269. * @param int $id_draft ID of the draft to load
  270. * @param int $type Type of draft - 0 for post or 1 for PM
  271. * @param boolean $check Validate that this draft belongs to the current user
  272. * @param boolean $load Whether or not to load the data into variables for use on a form
  273. * @return boolean|array False if the data couldn't be loaded, true if it's a PM draft or an array of info about the draft if it's a post draft
  274. */
  275. function ReadDraft($id_draft, $type = 0, $check = true, $load = false)
  276. {
  277. global $context, $user_info, $smcFunc, $modSettings;
  278. // like purell always clean to be sure
  279. $id_draft = (int) $id_draft;
  280. $type = (int) $type;
  281. // nothing to read, nothing to do
  282. if (empty($id_draft))
  283. return false;
  284. // load in this draft from the DB
  285. $request = $smcFunc['db_query']('', '
  286. SELECT *
  287. FROM {db_prefix}user_drafts
  288. WHERE id_draft = {int:id_draft}' . ($check ? '
  289. AND id_member = {int:id_member}' : '') . '
  290. AND type = {int:type}' . (!empty($modSettings['drafts_keep_days']) ? '
  291. AND poster_time > {int:time}' : '') . '
  292. LIMIT 1',
  293. array(
  294. 'id_member' => $user_info['id'],
  295. 'id_draft' => $id_draft,
  296. 'type' => $type,
  297. 'time' => (!empty($modSettings['drafts_keep_days']) ? (time() - ($modSettings['drafts_keep_days'] * 86400)) : 0),
  298. )
  299. );
  300. // no results?
  301. if (!$smcFunc['db_num_rows']($request))
  302. return false;
  303. // load up the data
  304. $draft_info = $smcFunc['db_fetch_assoc']($request);
  305. $smcFunc['db_free_result']($request);
  306. // Load it up for the templates as well
  307. $recipients = array();
  308. if (!empty($load))
  309. {
  310. if ($type === 0)
  311. {
  312. // a standard post draft?
  313. $context['sticky'] = !empty($draft_info['is_sticky']) ? $draft_info['is_sticky'] : '';
  314. $context['locked'] = !empty($draft_info['locked']) ? $draft_info['locked'] : '';
  315. $context['use_smileys'] = !empty($draft_info['smileys_enabled']) ? true : false;
  316. $context['icon'] = !empty($draft_info['icon']) ? $draft_info['icon'] : 'xx';
  317. $context['message'] = !empty($draft_info['body']) ? str_replace('<br>', "\n", un_htmlspecialchars(stripslashes($draft_info['body']))) : '';
  318. $context['subject'] = !empty($draft_info['subject']) ? stripslashes($draft_info['subject']) : '';
  319. $context['board'] = !empty($draft_info['board_id']) ? $draft_info['id_board'] : '';
  320. $context['id_draft'] = !empty($draft_info['id_draft']) ? $draft_info['id_draft'] : 0;
  321. }
  322. elseif ($type === 1)
  323. {
  324. // one of those pm drafts? then set it up like we have an error
  325. $_REQUEST['subject'] = !empty($draft_info['subject']) ? stripslashes($draft_info['subject']) : '';
  326. $_REQUEST['message'] = !empty($draft_info['body']) ? str_replace('<br>', "\n", un_htmlspecialchars(stripslashes($draft_info['body']))) : '';
  327. $_REQUEST['replied_to'] = !empty($draft_info['id_reply']) ? $draft_info['id_reply'] : 0;
  328. $context['id_pm_draft'] = !empty($draft_info['id_draft']) ? $draft_info['id_draft'] : 0;
  329. $recipients = unserialize($draft_info['to_list']);
  330. // make sure we only have integers in this array
  331. $recipients['to'] = array_map('intval', $recipients['to']);
  332. $recipients['bcc'] = array_map('intval', $recipients['bcc']);
  333. // pretend we messed up to populate the pm message form
  334. messagePostError(array(), array(), $recipients);
  335. return true;
  336. }
  337. }
  338. return $draft_info;
  339. }
  340. /**
  341. * Deletes one or many drafts from the DB
  342. * Validates the drafts are from the user
  343. * is supplied an array of drafts will attempt to remove all of them
  344. *
  345. * @param int $id_draft The ID of the draft to delete
  346. * @param boolean $check Whether or not to check that the draft belongs to the current user
  347. * @return boolean False if it couldn't be deleted (doesn't return anything otherwise)
  348. */
  349. function DeleteDraft($id_draft, $check = true)
  350. {
  351. global $user_info, $smcFunc;
  352. // Only a single draft.
  353. if (is_numeric($id_draft))
  354. $id_draft = array($id_draft);
  355. // can't delete nothing
  356. if (empty($id_draft) || ($check && empty($user_info['id'])))
  357. return false;
  358. $smcFunc['db_query']('', '
  359. DELETE FROM {db_prefix}user_drafts
  360. WHERE id_draft IN ({array_int:id_draft})' . ($check ? '
  361. AND id_member = {int:id_member}' : ''),
  362. array (
  363. 'id_draft' => $id_draft,
  364. 'id_member' => empty($user_info['id']) ? -1 : $user_info['id'],
  365. )
  366. );
  367. }
  368. /**
  369. * Loads in a group of drafts for the user of a given type (0/posts, 1/pm's)
  370. * loads a specific draft for forum use if selected.
  371. * Used in the posting screens to allow draft selection
  372. * Will load a draft if selected is supplied via post
  373. *
  374. * @param int $member_id ID of the member to show drafts for
  375. * @param boolean|integer If $type is 1, this can be set to only load drafts for posts in the specific topic
  376. * @param int $draft_type The type of drafts to show - 0 for post drafts, 1 for PM drafts
  377. * @return boolean False if the drafts couldn't be loaded, nothing otherwise
  378. */
  379. function ShowDrafts($member_id, $topic = false, $draft_type = 0)
  380. {
  381. global $smcFunc, $scripturl, $context, $txt, $modSettings;
  382. // Permissions
  383. if (($draft_type === 0 && empty($context['drafts_save'])) || ($draft_type === 1 && empty($context['drafts_pm_save'])) || empty($member_id))
  384. return false;
  385. $context['drafts'] = array();
  386. // has a specific draft has been selected? Load it up if there is not a message already in the editor
  387. if (isset($_REQUEST['id_draft']) && empty($_POST['subject']) && empty($_POST['message']))
  388. ReadDraft((int) $_REQUEST['id_draft'], $draft_type, true, true);
  389. // load the drafts this user has available
  390. $request = $smcFunc['db_query']('', '
  391. SELECT *
  392. FROM {db_prefix}user_drafts
  393. WHERE id_member = {int:id_member}' . ((!empty($topic) && empty($draft_type)) ? '
  394. AND id_topic = {int:id_topic}' : (!empty($topic) ? '
  395. AND id_reply = {int:id_topic}' : '')) . '
  396. AND type = {int:draft_type}' . (!empty($modSettings['drafts_keep_days']) ? '
  397. AND poster_time > {int:time}' : '') . '
  398. ORDER BY poster_time DESC',
  399. array(
  400. 'id_member' => $member_id,
  401. 'id_topic' => (int) $topic,
  402. 'draft_type' => $draft_type,
  403. 'time' => (!empty($modSettings['drafts_keep_days']) ? (time() - ($modSettings['drafts_keep_days'] * 86400)) : 0),
  404. )
  405. );
  406. // add them to the draft array for display
  407. while ($row = $smcFunc['db_fetch_assoc']($request))
  408. {
  409. // Post drafts
  410. if ($draft_type === 0)
  411. $context['drafts'][] = array(
  412. 'subject' => censorText(shorten_subject(stripslashes($row['subject']), 24)),
  413. 'poster_time' => timeformat($row['poster_time']),
  414. 'link' => '<a href="' . $scripturl . '?action=post;board=' . $row['id_board'] . ';' . (!empty($row['id_topic']) ? 'topic='. $row['id_topic'] .'.0;' : '') . 'id_draft=' . $row['id_draft'] . '">' . $row['subject'] . '</a>',
  415. );
  416. // PM drafts
  417. elseif ($draft_type === 1)
  418. $context['drafts'][] = array(
  419. 'subject' => censorText(shorten_subject(stripslashes($row['subject']), 24)),
  420. 'poster_time' => timeformat($row['poster_time']),
  421. 'link' => '<a href="' . $scripturl . '?action=pm;sa=send;id_draft=' . $row['id_draft'] . '">' . (!empty($row['subject']) ? $row['subject'] : $txt['drafts_none']) . '</a>',
  422. );
  423. }
  424. $smcFunc['db_free_result']($request);
  425. }
  426. /**
  427. * Returns an xml response to an autosave ajax request
  428. * provides the id of the draft saved and the time it was saved
  429. *
  430. * @param type $id_draft
  431. */
  432. function XmlDraft($id_draft)
  433. {
  434. global $txt, $context;
  435. header('Content-Type: text/xml; charset=' . (empty($context['character_set']) ? 'ISO-8859-1' : $context['character_set']));
  436. echo '<?xml version="1.0" encoding="', $context['character_set'], '"?>
  437. <drafts>
  438. <draft id="', $id_draft, '"><![CDATA[', $txt['draft_saved_on'], ': ', timeformat($context['draft_saved_on']), ']]></draft>
  439. </drafts>';
  440. obExit(false);
  441. }
  442. /**
  443. * Show all drafts of a given type by the current user
  444. * Uses the showdraft template
  445. * Allows for the deleting and loading/editing of drafts
  446. *
  447. * @param type $memID
  448. * @param type $draft_type
  449. */
  450. function showProfileDrafts($memID, $draft_type = 0)
  451. {
  452. global $txt, $scripturl, $modSettings, $context, $smcFunc;
  453. // Some initial context.
  454. $context['start'] = isset($_REQUEST['start']) ? (int) $_REQUEST['start'] : 0;
  455. $context['current_member'] = $memID;
  456. // If just deleting a draft, do it and then redirect back.
  457. if (!empty($_REQUEST['delete']))
  458. {
  459. checkSession('get');
  460. $id_delete = (int) $_REQUEST['delete'];
  461. $smcFunc['db_query']('', '
  462. DELETE FROM {db_prefix}user_drafts
  463. WHERE id_draft = {int:id_draft}
  464. AND id_member = {int:id_member}
  465. AND type = {int:draft_type}
  466. LIMIT 1',
  467. array(
  468. 'id_draft' => $id_delete,
  469. 'id_member' => $memID,
  470. 'draft_type' => $draft_type,
  471. )
  472. );
  473. redirectexit('action=profile;u=' . $memID . ';area=showdrafts;start=' . $context['start']);
  474. }
  475. // Default to 10.
  476. if (empty($_REQUEST['viewscount']) || !is_numeric($_REQUEST['viewscount']))
  477. $_REQUEST['viewscount'] = 10;
  478. // Get the count of applicable drafts on the boards they can (still) see ...
  479. // @todo .. should we just let them see their drafts even if they have lost board access ?
  480. $request = $smcFunc['db_query']('', '
  481. SELECT COUNT(id_draft)
  482. FROM {db_prefix}user_drafts AS ud
  483. INNER JOIN {db_prefix}boards AS b ON (b.id_board = ud.id_board AND {query_see_board})
  484. WHERE id_member = {int:id_member}
  485. AND type={int:draft_type}' . (!empty($modSettings['drafts_keep_days']) ? '
  486. AND poster_time > {int:time}' : ''),
  487. array(
  488. 'id_member' => $memID,
  489. 'draft_type' => $draft_type,
  490. 'time' => (!empty($modSettings['drafts_keep_days']) ? (time() - ($modSettings['drafts_keep_days'] * 86400)) : 0),
  491. )
  492. );
  493. list ($msgCount) = $smcFunc['db_fetch_row']($request);
  494. $smcFunc['db_free_result']($request);
  495. $maxIndex = (int) $modSettings['defaultMaxMessages'];
  496. // Make sure the starting place makes sense and construct our friend the page index.
  497. $context['page_index'] = constructPageIndex($scripturl . '?action=profile;u=' . $memID . ';area=showdrafts', $context['start'], $msgCount, $maxIndex);
  498. $context['current_page'] = $context['start'] / $maxIndex;
  499. // Reverse the query if we're past 50% of the pages for better performance.
  500. $start = $context['start'];
  501. $reverse = $_REQUEST['start'] > $msgCount / 2;
  502. if ($reverse)
  503. {
  504. $maxIndex = $msgCount < $context['start'] + $modSettings['defaultMaxMessages'] + 1 && $msgCount > $context['start'] ? $msgCount - $context['start'] : (int) $modSettings['defaultMaxMessages'];
  505. $start = $msgCount < $context['start'] + $modSettings['defaultMaxMessages'] + 1 || $msgCount < $context['start'] + $modSettings['defaultMaxMessages'] ? 0 : $msgCount - $context['start'] - $modSettings['defaultMaxMessages'];
  506. }
  507. // Find this user's drafts for the boards they can access
  508. // @todo ... do we want to do this? If they were able to create a draft, do we remove thier access to said draft if they loose
  509. // access to the board or if the topic moves to a board they can not see?
  510. $request = $smcFunc['db_query']('', '
  511. SELECT
  512. b.id_board, b.name AS bname,
  513. ud.id_member, ud.id_draft, ud.body, ud.smileys_enabled, ud.subject, ud.poster_time, ud.icon, ud.id_topic, ud.locked, ud.is_sticky
  514. FROM {db_prefix}user_drafts AS ud
  515. INNER JOIN {db_prefix}boards AS b ON (b.id_board = ud.id_board AND {query_see_board})
  516. WHERE ud.id_member = {int:current_member}
  517. AND type = {int:draft_type}' . (!empty($modSettings['drafts_keep_days']) ? '
  518. AND poster_time > {int:time}' : '') . '
  519. ORDER BY ud.id_draft ' . ($reverse ? 'ASC' : 'DESC') . '
  520. LIMIT ' . $start . ', ' . $maxIndex,
  521. array(
  522. 'current_member' => $memID,
  523. 'draft_type' => $draft_type,
  524. 'time' => (!empty($modSettings['drafts_keep_days']) ? (time() - ($modSettings['drafts_keep_days'] * 86400)) : 0),
  525. )
  526. );
  527. // Start counting at the number of the first message displayed.
  528. $counter = $reverse ? $context['start'] + $maxIndex + 1 : $context['start'];
  529. $context['posts'] = array();
  530. while ($row = $smcFunc['db_fetch_assoc']($request))
  531. {
  532. // Censor....
  533. if (empty($row['body']))
  534. $row['body'] = '';
  535. $row['subject'] = $smcFunc['htmltrim']($row['subject']);
  536. if (empty($row['subject']))
  537. $row['subject'] = $txt['no_subject'];
  538. censorText($row['body']);
  539. censorText($row['subject']);
  540. // BBC-ilize the message.
  541. $row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], 'draft' . $row['id_draft']);
  542. // And the array...
  543. $context['drafts'][$counter += $reverse ? -1 : 1] = array(
  544. 'body' => $row['body'],
  545. 'counter' => $counter,
  546. 'alternate' => $counter % 2,
  547. 'board' => array(
  548. 'name' => $row['bname'],
  549. 'id' => $row['id_board']
  550. ),
  551. 'topic' => array(
  552. 'id' => $row['id_topic'],
  553. 'link' => empty($row['id']) ? $row['subject'] : '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['subject'] . '</a>',
  554. ),
  555. 'subject' => $row['subject'],
  556. 'time' => timeformat($row['poster_time']),
  557. 'timestamp' => forum_time(true, $row['poster_time']),
  558. 'icon' => $row['icon'],
  559. 'id_draft' => $row['id_draft'],
  560. 'locked' => $row['locked'],
  561. 'sticky' => $row['is_sticky'],
  562. );
  563. }
  564. $smcFunc['db_free_result']($request);
  565. // If the drafts were retrieved in reverse order, get them right again.
  566. if ($reverse)
  567. $context['drafts'] = array_reverse($context['drafts'], true);
  568. // Menu tab
  569. $context[$context['profile_menu_name']]['tab_data'] = array(
  570. 'title' => $txt['drafts_show'],
  571. 'description' => $txt['drafts_show_desc'],
  572. 'icon_class' => 'pm_icons inbox'
  573. );
  574. $context['sub_template'] = 'showDrafts';
  575. }
  576. /**
  577. * Show all PM drafts of the current user
  578. * Uses the showpmdraft template
  579. * Allows for the deleting and loading/editing of drafts
  580. *
  581. * @param type $memID
  582. */
  583. function showPMDrafts($memID = -1)
  584. {
  585. global $txt, $user_info, $scripturl, $modSettings, $context, $smcFunc;
  586. // init
  587. $draft_type = 1;
  588. // If just deleting a draft, do it and then redirect back.
  589. if (!empty($_REQUEST['delete']))
  590. {
  591. checkSession('get');
  592. $id_delete = (int) $_REQUEST['delete'];
  593. $start = isset($_REQUEST['start']) ? (int) $_REQUEST['start'] : 0;
  594. $smcFunc['db_query']('', '
  595. DELETE FROM {db_prefix}user_drafts
  596. WHERE id_draft = {int:id_draft}
  597. AND id_member = {int:id_member}
  598. AND type = {int:draft_type}
  599. LIMIT 1',
  600. array(
  601. 'id_draft' => $id_delete,
  602. 'id_member' => $memID,
  603. 'draft_type' => $draft_type,
  604. )
  605. );
  606. // now redirect back to the list
  607. redirectexit('action=pm;sa=showpmdrafts;start=' . $start);
  608. }
  609. // perhaps a draft was selected for editing? if so pass this off
  610. if (!empty($_REQUEST['id_draft']) && !empty($context['drafts_pm_save']) && $memID == $user_info['id'])
  611. {
  612. checkSession('get');
  613. $id_draft = (int) $_REQUEST['id_draft'];
  614. redirectexit('action=pm;sa=send;id_draft=' . $id_draft);
  615. }
  616. // Default to 10.
  617. if (empty($_REQUEST['viewscount']) || !is_numeric($_REQUEST['viewscount']))
  618. $_REQUEST['viewscount'] = 10;
  619. // Get the count of applicable drafts
  620. $request = $smcFunc['db_query']('', '
  621. SELECT COUNT(id_draft)
  622. FROM {db_prefix}user_drafts
  623. WHERE id_member = {int:id_member}
  624. AND type={int:draft_type}' . (!empty($modSettings['drafts_keep_days']) ? '
  625. AND poster_time > {int:time}' : ''),
  626. array(
  627. 'id_member' => $memID,
  628. 'draft_type' => $draft_type,
  629. 'time' => (!empty($modSettings['drafts_keep_days']) ? (time() - ($modSettings['drafts_keep_days'] * 86400)) : 0),
  630. )
  631. );
  632. list ($msgCount) = $smcFunc['db_fetch_row']($request);
  633. $smcFunc['db_free_result']($request);
  634. $maxIndex = (int) $modSettings['defaultMaxMessages'];
  635. // Make sure the starting place makes sense and construct our friend the page index.
  636. $context['page_index'] = constructPageIndex($scripturl . '?action=pm;sa=showpmdrafts', $context['start'], $msgCount, $maxIndex);
  637. $context['current_page'] = $context['start'] / $maxIndex;
  638. // Reverse the query if we're past 50% of the total for better performance.
  639. $start = $context['start'];
  640. $reverse = $_REQUEST['start'] > $msgCount / 2;
  641. if ($reverse)
  642. {
  643. $maxIndex = $msgCount < $context['start'] + $modSettings['defaultMaxMessages'] + 1 && $msgCount > $context['start'] ? $msgCount - $context['start'] : (int) $modSettings['defaultMaxMessages'];
  644. $start = $msgCount < $context['start'] + $modSettings['defaultMaxMessages'] + 1 || $msgCount < $context['start'] + $modSettings['defaultMaxMessages'] ? 0 : $msgCount - $context['start'] - $modSettings['defaultMaxMessages'];
  645. }
  646. // Load in this user's PM drafts
  647. $request = $smcFunc['db_query']('', '
  648. SELECT
  649. ud.id_member, ud.id_draft, ud.body, ud.subject, ud.poster_time, ud.id_reply, ud.to_list
  650. FROM {db_prefix}user_drafts AS ud
  651. WHERE ud.id_member = {int:current_member}
  652. AND type = {int:draft_type}' . (!empty($modSettings['drafts_keep_days']) ? '
  653. AND poster_time > {int:time}' : '') . '
  654. ORDER BY ud.id_draft ' . ($reverse ? 'ASC' : 'DESC') . '
  655. LIMIT ' . $start . ', ' . $maxIndex,
  656. array(
  657. 'current_member' => $memID,
  658. 'draft_type' => $draft_type,
  659. 'time' => (!empty($modSettings['drafts_keep_days']) ? (time() - ($modSettings['drafts_keep_days'] * 86400)) : 0),
  660. )
  661. );
  662. // Start counting at the number of the first message displayed.
  663. $counter = $reverse ? $context['start'] + $maxIndex + 1 : $context['start'];
  664. $context['posts'] = array();
  665. while ($row = $smcFunc['db_fetch_assoc']($request))
  666. {
  667. // Censor....
  668. if (empty($row['body']))
  669. $row['body'] = '';
  670. $row['subject'] = $smcFunc['htmltrim']($row['subject']);
  671. if (empty($row['subject']))
  672. $row['subject'] = $txt['no_subject'];
  673. censorText($row['body']);
  674. censorText($row['subject']);
  675. // BBC-ilize the message.
  676. $row['body'] = parse_bbc($row['body'], true, 'draft' . $row['id_draft']);
  677. // Have they provide who this will go to?
  678. $recipients = array(
  679. 'to' => array(),
  680. 'bcc' => array(),
  681. );
  682. $recipient_ids = (!empty($row['to_list'])) ? unserialize($row['to_list']) : array();
  683. // @todo ... this is a bit ugly since it runs an extra query for every message, do we want this?
  684. // at least its only for draft PM's and only the user can see them ... so not heavily used .. still
  685. if (!empty($recipient_ids['to']) || !empty($recipient_ids['bcc']))
  686. {
  687. $recipient_ids['to'] = array_map('intval', $recipient_ids['to']);
  688. $recipient_ids['bcc'] = array_map('intval', $recipient_ids['bcc']);
  689. $allRecipients = array_merge($recipient_ids['to'], $recipient_ids['bcc']);
  690. $request_2 = $smcFunc['db_query']('', '
  691. SELECT id_member, real_name
  692. FROM {db_prefix}members
  693. WHERE id_member IN ({array_int:member_list})',
  694. array(
  695. 'member_list' => $allRecipients,
  696. )
  697. );
  698. while ($result = $smcFunc['db_fetch_assoc']($request_2))
  699. {
  700. $recipientType = in_array($result['id_member'], $recipient_ids['bcc']) ? 'bcc' : 'to';
  701. $recipients[$recipientType][] = $result['real_name'];
  702. }
  703. $smcFunc['db_free_result']($request_2);
  704. }
  705. // Add the items to the array for template use
  706. $context['drafts'][$counter += $reverse ? -1 : 1] = array(
  707. 'body' => $row['body'],
  708. 'counter' => $counter,
  709. 'alternate' => $counter % 2,
  710. 'subject' => $row['subject'],
  711. 'time' => timeformat($row['poster_time']),
  712. 'timestamp' => forum_time(true, $row['poster_time']),
  713. 'id_draft' => $row['id_draft'],
  714. 'recipients' => $recipients,
  715. 'age' => floor((time() - $row['poster_time']) / 86400),
  716. 'remaining' => (!empty($modSettings['drafts_keep_days']) ? floor($modSettings['drafts_keep_days'] - ((time() - $row['poster_time']) / 86400)) : 0),
  717. );
  718. }
  719. $smcFunc['db_free_result']($request);
  720. // if the drafts were retrieved in reverse order, then put them in the right order again.
  721. if ($reverse)
  722. $context['drafts'] = array_reverse($context['drafts'], true);
  723. // off to the template we go
  724. $context['page_title'] = $txt['drafts'];
  725. $context['sub_template'] = 'showPMDrafts';
  726. $context['linktree'][] = array(
  727. 'url' => $scripturl . '?action=pm;sa=showpmdrafts',
  728. 'name' => $txt['drafts'],
  729. );
  730. }
  731. ?>