Drafts.php 29 KB

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