Drafts.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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 2012 Simple Machines
  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('Hacking attempt...');
  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 $txt, $context, $user_info, $smcFunc, $modSettings, $board;
  30. // can you be, should you be ... here?
  31. if (empty($modSettings['drafts_enabled']) || 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_enabled']) || 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. $outbox = empty($_POST['outbox']) ? 0 : 1;
  190. $draft['body'] = $smcFunc['htmlspecialchars']($_POST['message'], ENT_QUOTES);
  191. $draft['subject'] = strtr($smcFunc['htmlspecialchars']($_POST['subject']), array("\r" => '', "\n" => '', "\t" => ''));
  192. // message and subject always need a bit more work
  193. preparsecode($draft['body']);
  194. if ($smcFunc['strlen']($draft['subject']) > 100)
  195. $draft['subject'] = $smcFunc['substr']($draft['subject'], 0, 100);
  196. // Modifying an existing PM draft?
  197. if (!empty($id_pm_draft) && !empty($draft_info) && $draft_info['id_member'] == $user_info['id'])
  198. {
  199. $smcFunc['db_query']('', '
  200. UPDATE {db_prefix}user_drafts
  201. SET id_reply = {int:id_reply},
  202. type = {int:type},
  203. poster_time = {int:poster_time},
  204. subject = {string:subject},
  205. body = {string:body},
  206. to_list = {string:to_list},
  207. outbox = {int:outbox}
  208. WHERE id_draft = {int:id_pm_draft}
  209. LIMIT 1',
  210. array(
  211. 'id_reply' => $reply_id,
  212. 'type' => 1,
  213. 'poster_time' => time(),
  214. 'subject' => $draft['subject'],
  215. 'body' => $draft['body'],
  216. 'id_pm_draft' => $id_pm_draft,
  217. 'to_list' => serialize($recipientList),
  218. 'outbox' => $outbox,
  219. )
  220. );
  221. // some items to return to the form
  222. $context['draft_saved'] = true;
  223. $context['id_pm_draft'] = $id_pm_draft;
  224. }
  225. // otherwise creating a new PM draft.
  226. else
  227. {
  228. $smcFunc['db_insert']('',
  229. '{db_prefix}user_drafts',
  230. array(
  231. 'id_reply' => 'int',
  232. 'type' => 'int',
  233. 'poster_time' => 'int',
  234. 'id_member' => 'int',
  235. 'subject' => 'string-255',
  236. 'body' => 'string-65534',
  237. 'to_list' => 'string-255',
  238. 'outbox' => 'int',
  239. ),
  240. array(
  241. $reply_id,
  242. 1,
  243. time(),
  244. $user_info['id'],
  245. $draft['subject'],
  246. $draft['body'],
  247. serialize($recipientList),
  248. $outbox,
  249. ),
  250. array(
  251. 'id_draft'
  252. )
  253. );
  254. // get the new id
  255. $id_pm_draft = $smcFunc['db_insert_id']('{db_prefix}user_drafts', 'id_draft');
  256. // everything go as expected, if not toss back an error
  257. if (!empty($id_pm_draft))
  258. {
  259. $context['draft_saved'] = true;
  260. $context['id_pm_draft'] = $id_pm_draft;
  261. }
  262. else
  263. $post_errors[] = 'draft_not_saved';
  264. }
  265. // if we were called from the autosave function, send something back
  266. if (!empty($id_pm_draft) && isset($_REQUEST['xml']) && !in_array('session_timeout', $post_errors))
  267. {
  268. $context['draft_saved_on'] = time();
  269. XmlDraft($id_pm_draft);
  270. }
  271. return;
  272. }
  273. /**
  274. * Reads a draft in from the user_drafts table
  275. * Only loads the draft of a given type 0 for post, 1 for pm draft
  276. * validates that the draft is the users draft
  277. * Optionally loads the draft in to context or superglobal for loading in to the form
  278. *
  279. * @param type $id_draft - draft to load
  280. * @param type $type - type of draft
  281. * @param type $check - validate the user
  282. * @param type $load - load it for use in a form
  283. * @return boolean
  284. */
  285. function ReadDraft($id_draft, $type = 0, $check = true, $load = false)
  286. {
  287. global $context, $user_info, $smcFunc, $modSettings;
  288. // like purell always clean to be sure
  289. $id_draft = (int) $id_draft;
  290. $type = (int) $type;
  291. // nothing to read, nothing to do
  292. if (empty($id_draft))
  293. return false;
  294. // load in this draft from the DB
  295. $request = $smcFunc['db_query']('', '
  296. SELECT *
  297. FROM {db_prefix}user_drafts
  298. WHERE id_draft = {int:id_draft}' . ($check ? '
  299. AND id_member = {int:id_member}' : '') . '
  300. AND type = {int:type}' . (!empty($modSettings['drafts_keep_days']) ? '
  301. AND poster_time > {int:time}' : '') . '
  302. LIMIT 1',
  303. array(
  304. 'id_member' => $user_info['id'],
  305. 'id_draft' => $id_draft,
  306. 'type' => $type,
  307. 'time' => (!empty($modSettings['drafts_keep_days']) ? (time() - ($modSettings['drafts_keep_days'] * 86400)) : 0),
  308. )
  309. );
  310. // no results?
  311. if (!$smcFunc['db_num_rows']($request))
  312. return false;
  313. // load up the data
  314. $draft_info = $smcFunc['db_fetch_assoc']($request);
  315. $smcFunc['db_free_result']($request);
  316. // Load it up for the templates as well
  317. $recipients = array();
  318. if (!empty($load))
  319. {
  320. if ($type === 0)
  321. {
  322. // a standard post draft?
  323. $context['sticky'] = !empty($draft_info['is_sticky']) ? $draft_info['is_sticky'] : '';
  324. $context['locked'] = !empty($draft_info['locked']) ? $draft_info['locked'] : '';
  325. $context['use_smileys'] = !empty($draft_info['smileys_enabled']) ? true : false;
  326. $context['icon'] = !empty($draft_info['icon']) ? $draft_info['icon'] : 'xx';
  327. $context['message'] = !empty($draft_info['body']) ? str_replace('<br />', "\n", un_htmlspecialchars(stripslashes($draft_info['body']))) : '';
  328. $context['subject'] = !empty($draft_info['subject']) ? stripslashes($draft_info['subject']) : '';
  329. $context['board'] = !empty($draft_info['board_id']) ? $draft_info['id_board'] : '';
  330. $context['id_draft'] = !empty($draft_info['id_draft']) ? $draft_info['id_draft'] : 0;
  331. }
  332. elseif ($type === 1)
  333. {
  334. // one of those pm drafts? then set it up like we have an error
  335. $_REQUEST['outbox'] = !empty($draft_info['outbox']);
  336. $_REQUEST['subject'] = !empty($draft_info['subject']) ? stripslashes($draft_info['subject']) : '';
  337. $_REQUEST['message'] = !empty($draft_info['body']) ? str_replace('<br />', "\n", un_htmlspecialchars(stripslashes($draft_info['body']))) : '';
  338. $_REQUEST['replied_to'] = !empty($draft_info['id_reply']) ? $draft_info['id_reply'] : 0;
  339. $context['id_pm_draft'] = !empty($draft_info['id_draft']) ? $draft_info['id_draft'] : 0;
  340. $recipients = unserialize($draft_info['to_list']);
  341. // make sure we only have integers in this array
  342. $recipients['to'] = array_map('intval', $recipients['to']);
  343. $recipients['bcc'] = array_map('intval', $recipients['bcc']);
  344. // pretend we messed up to populate the pm message form
  345. messagePostError(array(), array(), $recipients);
  346. return true;
  347. }
  348. }
  349. return $draft_info;
  350. }
  351. /**
  352. * Deletes one or many drafts from the DB
  353. * Validates the drafts are from the user
  354. * is supplied an array of drafts will attempt to remove all of them
  355. *
  356. * @param type $id_draft
  357. * @param type $check
  358. * @return boolean
  359. */
  360. function DeleteDraft($id_draft, $check = true)
  361. {
  362. global $user_info, $smcFunc;
  363. // Only a single draft.
  364. if (is_numeric($id_draft))
  365. $id_draft = array($id_draft);
  366. // can't delete nothing
  367. if (empty($id_draft) || ($check && empty($user_info['id'])))
  368. return false;
  369. $smcFunc['db_query']('', '
  370. DELETE FROM {db_prefix}user_drafts
  371. WHERE id_draft IN ({array_int:id_draft})' . ($check ? '
  372. AND id_member = {int:id_member}' : ''),
  373. array (
  374. 'id_draft' => $id_draft,
  375. 'id_member' => empty($user_info['id']) ? -1 : $user_info['id'],
  376. )
  377. );
  378. }
  379. /**
  380. * Loads in a group of drafts for the user of a given type (0/posts, 1/pm's)
  381. * loads a specific draft for forum use if selected.
  382. * Used in the posting screens to allow draft selection
  383. * WIll load a draft if selected is supplied via post
  384. *
  385. * @param type $member_id
  386. * @param type $topic
  387. * @param type $draft_type
  388. * @return boolean
  389. */
  390. function ShowDrafts($member_id, $topic = false, $draft_type = 0)
  391. {
  392. global $smcFunc, $scripturl, $context, $txt, $modSettings;
  393. // Permissions
  394. if (($draft_type === 0 && empty($context['drafts_save'])) || ($draft_type === 1 && empty($context['drafts_pm_save'])) || empty($member_id))
  395. return false;
  396. $context['drafts'] = array();
  397. // has a specific draft has been selected? Load it up if there is not a message already in the editor
  398. if (isset($_REQUEST['id_draft']) && empty($_POST['subject']) && empty($_POST['message']))
  399. ReadDraft((int) $_REQUEST['id_draft'], $draft_type, true, true);
  400. // load the drafts this user has available
  401. $request = $smcFunc['db_query']('', '
  402. SELECT *
  403. FROM {db_prefix}user_drafts
  404. WHERE id_member = {int:id_member}' . ((!empty($topic) && empty($draft_type)) ? '
  405. AND id_topic = {int:id_topic}' : (!empty($topic) ? '
  406. AND id_reply = {int:id_topic}' : '')) . '
  407. AND type = {int:draft_type}' . (!empty($modSettings['drafts_keep_days']) ? '
  408. AND poster_time > {int:time}' : '') . '
  409. ORDER BY poster_time DESC',
  410. array(
  411. 'id_member' => $member_id,
  412. 'id_topic' => (int) $topic,
  413. 'draft_type' => $draft_type,
  414. 'time' => (!empty($modSettings['drafts_keep_days']) ? (time() - ($modSettings['drafts_keep_days'] * 86400)) : 0),
  415. )
  416. );
  417. // add them to the draft array for display
  418. while ($row = $smcFunc['db_fetch_assoc']($request))
  419. {
  420. // Post drafts
  421. if ($draft_type === 0)
  422. $context['drafts'][] = array(
  423. 'subject' => censorText(shorten_subject(stripslashes($row['subject']), 24)),
  424. 'poster_time' => timeformat($row['poster_time']),
  425. '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>',
  426. );
  427. // PM drafts
  428. elseif ($draft_type === 1)
  429. $context['drafts'][] = array(
  430. 'subject' => censorText(shorten_subject(stripslashes($row['subject']), 24)),
  431. 'poster_time' => timeformat($row['poster_time']),
  432. 'link' => '<a href="' . $scripturl . '?action=pm;sa=send;id_draft=' . $row['id_draft'] . '">' . (!empty($row['subject']) ? $row['subject'] : $txt['drafts_none']) . '</a>',
  433. );
  434. }
  435. $smcFunc['db_free_result']($request);
  436. }
  437. /**
  438. * Returns an xml response to an autosave ajax request
  439. * provides the id of the draft saved and the time it was saved
  440. *
  441. * @param type $id_draft
  442. */
  443. function XmlDraft($id_draft)
  444. {
  445. global $txt, $context;
  446. header('Content-Type: text/xml; charset=' . (empty($context['character_set']) ? 'ISO-8859-1' : $context['character_set']));
  447. echo '<?xml version="1.0" encoding="', $context['character_set'], '"?>
  448. <drafts>
  449. <draft id="', $id_draft, '"><![CDATA[', $txt['draft_saved_on'], ': ', timeformat($context['draft_saved_on']), ']]></draft>
  450. </drafts>';
  451. obExit(false);
  452. }
  453. /**
  454. * Show all drafts of a given type by the current user
  455. * Uses the showdraft template
  456. * Allows for the deleting and loading/editing of drafts
  457. *
  458. * @param type $memID
  459. * @param type $draft_type
  460. */
  461. function showProfileDrafts($memID, $draft_type = 0)
  462. {
  463. global $txt, $user_info, $scripturl, $modSettings, $context, $smcFunc;
  464. // Some initial context.
  465. $context['start'] = isset($_REQUEST['start']) ? (int) $_REQUEST['start'] : 0;
  466. $context['current_member'] = $memID;
  467. // If just deleting a draft, do it and then redirect back.
  468. if (!empty($_REQUEST['delete']))
  469. {
  470. checkSession('get');
  471. $id_delete = (int) $_REQUEST['delete'];
  472. $smcFunc['db_query']('', '
  473. DELETE FROM {db_prefix}user_drafts
  474. WHERE id_draft = {int:id_draft}
  475. AND id_member = {int:id_member}
  476. AND type = {int:draft_type}
  477. LIMIT 1',
  478. array(
  479. 'id_draft' => $id_delete,
  480. 'id_member' => $memID,
  481. 'draft_type' => $draft_type,
  482. )
  483. );
  484. redirectexit('action=profile;u=' . $memID . ';area=showdrafts;start=' . $context['start']);
  485. }
  486. // Default to 10.
  487. if (empty($_REQUEST['viewscount']) || !is_numeric($_REQUEST['viewscount']))
  488. $_REQUEST['viewscount'] = 10;
  489. // Get the count of applicable drafts on the boards they can (still) see ...
  490. // @todo .. should we just let them see their drafts even if they have lost board access ?
  491. $request = $smcFunc['db_query']('', '
  492. SELECT COUNT(id_draft)
  493. FROM {db_prefix}user_drafts AS ud
  494. INNER JOIN {db_prefix}boards AS b ON (b.id_board = ud.id_board AND {query_see_board})
  495. WHERE id_member = {int:id_member}
  496. AND type={int:draft_type}' . (!empty($modSettings['drafts_keep_days']) ? '
  497. AND poster_time > {int:time}' : ''),
  498. array(
  499. 'id_member' => $memID,
  500. 'draft_type' => $draft_type,
  501. 'time' => (!empty($modSettings['drafts_keep_days']) ? (time() - ($modSettings['drafts_keep_days'] * 86400)) : 0),
  502. )
  503. );
  504. list ($msgCount) = $smcFunc['db_fetch_row']($request);
  505. $smcFunc['db_free_result']($request);
  506. $maxIndex = (int) $modSettings['defaultMaxMessages'];
  507. // Make sure the starting place makes sense and construct our friend the page index.
  508. $context['page_index'] = constructPageIndex($scripturl . '?action=profile;u=' . $memID . ';area=showdrafts', $context['start'], $msgCount, $maxIndex);
  509. $context['current_page'] = $context['start'] / $maxIndex;
  510. // Reverse the query if we're past 50% of the pages for better performance.
  511. $start = $context['start'];
  512. $reverse = $_REQUEST['start'] > $msgCount / 2;
  513. if ($reverse)
  514. {
  515. $maxIndex = $msgCount < $context['start'] + $modSettings['defaultMaxMessages'] + 1 && $msgCount > $context['start'] ? $msgCount - $context['start'] : (int) $modSettings['defaultMaxMessages'];
  516. $start = $msgCount < $context['start'] + $modSettings['defaultMaxMessages'] + 1 || $msgCount < $context['start'] + $modSettings['defaultMaxMessages'] ? 0 : $msgCount - $context['start'] - $modSettings['defaultMaxMessages'];
  517. }
  518. // Find this user's drafts for the boards they can access
  519. // @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
  520. // access to the board or if the topic moves to a board they can not see?
  521. $request = $smcFunc['db_query']('', '
  522. SELECT
  523. b.id_board, b.name AS bname,
  524. 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
  525. FROM {db_prefix}user_drafts AS ud
  526. INNER JOIN {db_prefix}boards AS b ON (b.id_board = ud.id_board AND {query_see_board})
  527. WHERE ud.id_member = {int:current_member}
  528. AND type = {int:draft_type}' . (!empty($modSettings['drafts_keep_days']) ? '
  529. AND poster_time > {int:time}' : '') . '
  530. ORDER BY ud.id_draft ' . ($reverse ? 'ASC' : 'DESC') . '
  531. LIMIT ' . $start . ', ' . $maxIndex,
  532. array(
  533. 'current_member' => $memID,
  534. 'draft_type' => $draft_type,
  535. 'time' => (!empty($modSettings['drafts_keep_days']) ? (time() - ($modSettings['drafts_keep_days'] * 86400)) : 0),
  536. )
  537. );
  538. // Start counting at the number of the first message displayed.
  539. $counter = $reverse ? $context['start'] + $maxIndex + 1 : $context['start'];
  540. $context['posts'] = array();
  541. while ($row = $smcFunc['db_fetch_assoc']($request))
  542. {
  543. // Censor....
  544. if (empty($row['body']))
  545. $row['body'] = '';
  546. $row['subject'] = $smcFunc['htmltrim']($row['subject']);
  547. if (empty($row['subject']))
  548. $row['subject'] = $txt['no_subject'];
  549. censorText($row['body']);
  550. censorText($row['subject']);
  551. // BBC-ilize the message.
  552. $row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], 'draft' . $row['id_draft']);
  553. // And the array...
  554. $context['drafts'][$counter += $reverse ? -1 : 1] = array(
  555. 'body' => $row['body'],
  556. 'counter' => $counter,
  557. 'alternate' => $counter % 2,
  558. 'board' => array(
  559. 'name' => $row['bname'],
  560. 'id' => $row['id_board']
  561. ),
  562. 'topic' => array(
  563. 'id' => $row['id_topic'],
  564. 'link' => empty($row['id']) ? $row['subject'] : '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['subject'] . '</a>',
  565. ),
  566. 'subject' => $row['subject'],
  567. 'time' => timeformat($row['poster_time']),
  568. 'timestamp' => forum_time(true, $row['poster_time']),
  569. 'icon' => $row['icon'],
  570. 'id_draft' => $row['id_draft'],
  571. 'locked' => $row['locked'],
  572. 'sticky' => $row['is_sticky'],
  573. );
  574. }
  575. $smcFunc['db_free_result']($request);
  576. // If the drafts were retrieved in reverse order, get them right again.
  577. if ($reverse)
  578. $context['drafts'] = array_reverse($context['drafts'], true);
  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.outbox, 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. /**
  737. * Modify any setting related to drafts.
  738. * Requires the admin_forum permission.
  739. * Accessed from ?action=admin;area=managedrafts
  740. *
  741. * @param bool $return_config = false
  742. * @uses Admin template, edit_topic_settings sub-template.
  743. */
  744. function ModifyDraftSettings($return_config = false)
  745. {
  746. global $context, $txt, $sourcedir, $scripturl;
  747. isAllowedTo('admin_forum');
  748. // Here are all the draft settings, a bit lite for now, but we can add more :P
  749. $config_vars = array(
  750. // Draft settings ...
  751. array('check', 'drafts_post_enabled'),
  752. array('check', 'drafts_pm_enabled'),
  753. array('check', 'drafts_show_saved_enabled', 'subtext' => $txt['drafts_show_saved_enabled_subnote']),
  754. array('int', 'drafts_keep_days', 'postinput' => $txt['days_word'], 'subtext' => $txt['drafts_keep_days_subnote']),
  755. '',
  756. array('check', 'drafts_autosave_enabled', 'subtext' => $txt['drafts_autosave_enabled_subnote']),
  757. array('int', 'drafts_autosave_frequency', 'postinput' => $txt['manageposts_seconds'], 'subtext' => $txt['drafts_autosave_frequency_subnote']),
  758. );
  759. if ($return_config)
  760. return $config_vars;
  761. // Get the settings template ready.
  762. require_once($sourcedir . '/ManageServer.php');
  763. // Setup the template.
  764. $context['page_title'] = $txt['managedrafts_settings'];
  765. $context['sub_template'] = 'show_settings';
  766. // Saving them ?
  767. if (isset($_GET['save']))
  768. {
  769. checkSession();
  770. // Protect them from themselves.
  771. $_POST['drafts_autosave_frequency'] = $_POST['drafts_autosave_frequency'] < 30 ? 30 : $_POST['drafts_autosave_frequency'];
  772. saveDBSettings($config_vars);
  773. redirectexit('action=admin;area=managedrafts');
  774. }
  775. // some javascript to enable / disable the frequency input box
  776. $context['settings_post_javascript'] = '
  777. var autosave = document.getElementById(\'drafts_autosave_enabled\');
  778. createEventListener(autosave)
  779. autosave.addEventListener(\'change\', toggle);
  780. toggle();
  781. function toggle()
  782. {
  783. var select_elem = document.getElementById(\'drafts_autosave_frequency\');
  784. select_elem.disabled = !autosave.checked;
  785. }
  786. ';
  787. // Final settings...
  788. $context['post_url'] = $scripturl . '?action=admin;area=managedrafts;save';
  789. $context['settings_title'] = $txt['managedrafts_settings'];
  790. // Prepare the settings...
  791. prepareDBSettingContext($config_vars);
  792. }
  793. ?>