Drafts.php 28 KB

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