Drafts.php 29 KB

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