2
0

Drafts.php 30 KB

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