Drafts.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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 2011 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. LIMIT 1',
  348. array (
  349. 'id_draft' => $id_draft,
  350. 'id_member' => empty($user_info['id']) ? -1 : $user_info['id'],
  351. )
  352. );
  353. }
  354. /**
  355. * Loads in a group of drafts for the user of a given type (0/posts, 1/pm's)
  356. * loads a specific draft for forum use if selected.
  357. * Used in the posting screens to allow draft selection
  358. * WIll load a draft if selected is supplied via post
  359. *
  360. * @param type $member_id
  361. * @param type $topic
  362. * @param type $draft_type
  363. * @return boolean
  364. */
  365. function ShowDrafts($member_id, $topic = false, $draft_type = 0)
  366. {
  367. global $smcFunc, $scripturl, $context, $txt, $modSettings;
  368. // Permissions
  369. if (($draft_type === 0 && empty($context['drafts_save'])) || ($draft_type === 1 && empty($context['drafts_pm_save'])) || empty($member_id))
  370. return false;
  371. $context['drafts'] = array();
  372. // has a specific draft has been selected? Load it up if there is not a message already in the editor
  373. if (isset($_REQUEST['id_draft']) && empty($_POST['subject']) && empty($_POST['message']))
  374. ReadDraft((int) $_REQUEST['id_draft'], $draft_type, true, true);
  375. // load the drafts this user has available
  376. $request = $smcFunc['db_query']('', '
  377. SELECT *
  378. FROM {db_prefix}user_drafts
  379. WHERE id_member = {int:id_member}' . ((!empty($topic) && empty($draft_type)) ? '
  380. AND id_topic = {int:id_topic}' : (!empty($topic) ? '
  381. AND id_reply = {int:id_topic}' : '')) . '
  382. AND type = {int:draft_type}' . (!empty($modSettings['drafts_keep_days']) ? '
  383. AND poster_time > {int:time}' : '') . '
  384. ORDER BY poster_time DESC',
  385. array(
  386. 'id_member' => $member_id,
  387. 'id_topic' => (int) $topic,
  388. 'draft_type' => $draft_type,
  389. 'time' => (!empty($modSettings['drafts_keep_days']) ? (time() - ($modSettings['drafts_keep_days'] * 86400)) : 0),
  390. )
  391. );
  392. // add them to the draft array for display
  393. while ($row = $smcFunc['db_fetch_assoc']($request))
  394. {
  395. // Post drafts
  396. if ($draft_type === 0)
  397. $context['drafts'][] = array(
  398. 'subject' => censorText(shorten_subject(stripslashes($row['subject']), 24)),
  399. 'poster_time' => timeformat($row['poster_time']),
  400. '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>',
  401. );
  402. // PM drafts
  403. elseif ($draft_type === 1)
  404. $context['drafts'][] = array(
  405. 'subject' => censorText(shorten_subject(stripslashes($row['subject']), 24)),
  406. 'poster_time' => timeformat($row['poster_time']),
  407. 'link' => '<a href="' . $scripturl . '?action=pm;sa=send;id_draft=' . $row['id_draft'] . '">' . (!empty($row['subject']) ? $row['subject'] : $txt['drafts_none']) . '</a>',
  408. );
  409. }
  410. $smcFunc['db_free_result']($request);
  411. }
  412. /**
  413. * Returns an xml response to an autosave ajax request
  414. * provides the id of the draft saved and the time it was saved
  415. *
  416. * @param type $id_draft
  417. */
  418. function XmlDraft($id_draft)
  419. {
  420. global $txt, $context;
  421. header('Content-Type: text/xml; charset=' . (empty($context['character_set']) ? 'ISO-8859-1' : $context['character_set']));
  422. echo '<?xml version="1.0" encoding="', $context['character_set'], '"?>
  423. <drafts>
  424. <draft id="', $id_draft, '"><![CDATA[', $txt['draft_saved_on'], ': ', timeformat(time()), ']]></draft>
  425. </drafts>';
  426. obExit(false);
  427. }
  428. /**
  429. * Show all drafts of a given type by the current user
  430. * Uses the showdraft template
  431. * Allows for the deleting and loading/editing of drafts
  432. *
  433. * @param type $memID
  434. * @param type $draft_type
  435. */
  436. function showProfileDrafts($memID, $draft_type = 0)
  437. {
  438. global $txt, $user_info, $scripturl, $modSettings, $context, $smcFunc;
  439. // Some initial context.
  440. $context['start'] = isset($_REQUEST['start']) ? (int) $_REQUEST['start'] : 0;
  441. $context['current_member'] = $memID;
  442. // If just deleting a draft, do it and then redirect back.
  443. if (!empty($_REQUEST['delete']))
  444. {
  445. checkSession('get');
  446. $id_delete = (int) $_REQUEST['delete'];
  447. $smcFunc['db_query']('', '
  448. DELETE FROM {db_prefix}user_drafts
  449. WHERE id_draft = {int:id_draft}
  450. AND id_member = {int:id_member}
  451. AND type = {int:draft_type}
  452. LIMIT 1',
  453. array(
  454. 'id_draft' => $id_delete,
  455. 'id_member' => $memID,
  456. 'draft_type' => $draft_type,
  457. )
  458. );
  459. redirectexit('action=profile;u=' . $memID . ';area=showdrafts;start=' . $context['start']);
  460. }
  461. // Default to 10.
  462. if (empty($_REQUEST['viewscount']) || !is_numeric($_REQUEST['viewscount']))
  463. $_REQUEST['viewscount'] = '10';
  464. // Get the count of applicable drafts on the boards they can (still) see ...
  465. // @todo .. should we just let them see their drafts even if they have lost board access ?
  466. $request = $smcFunc['db_query']('', '
  467. SELECT COUNT(id_draft)
  468. FROM {db_prefix}user_drafts AS ud
  469. INNER JOIN {db_prefix}boards AS b ON (b.id_board = ud.id_board AND {query_see_board})
  470. WHERE id_member = {int:id_member}
  471. AND type={int:draft_type}' . (!empty($modSettings['drafts_keep_days']) ? '
  472. AND poster_time > {int:time}' : ''),
  473. array(
  474. 'id_member' => $memID,
  475. 'draft_type' => $draft_type,
  476. 'time' => (!empty($modSettings['drafts_keep_days']) ? (time() - ($modSettings['drafts_keep_days'] * 86400)) : 0),
  477. )
  478. );
  479. list ($msgCount) = $smcFunc['db_fetch_row']($request);
  480. $smcFunc['db_free_result']($request);
  481. $maxIndex = (int) $modSettings['defaultMaxMessages'];
  482. // Make sure the starting place makes sense and construct our friend the page index.
  483. $context['page_index'] = constructPageIndex($scripturl . '?action=profile;u=' . $memID . ';area=showdrafts', $context['start'], $msgCount, $maxIndex);
  484. $context['current_page'] = $context['start'] / $maxIndex;
  485. // Reverse the query if we're past 50% of the pages for better performance.
  486. $start = $context['start'];
  487. $reverse = $_REQUEST['start'] > $msgCount / 2;
  488. if ($reverse)
  489. {
  490. $maxIndex = $msgCount < $context['start'] + $modSettings['defaultMaxMessages'] + 1 && $msgCount > $context['start'] ? $msgCount - $context['start'] : (int) $modSettings['defaultMaxMessages'];
  491. $start = $msgCount < $context['start'] + $modSettings['defaultMaxMessages'] + 1 || $msgCount < $context['start'] + $modSettings['defaultMaxMessages'] ? 0 : $msgCount - $context['start'] - $modSettings['defaultMaxMessages'];
  492. }
  493. // Find this user's drafts for the boards they can access
  494. // @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
  495. // access to the board or if the topic moves to a board they can not see?
  496. $request = $smcFunc['db_query']('', '
  497. SELECT
  498. b.id_board, b.name AS bname,
  499. 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
  500. FROM {db_prefix}user_drafts AS ud
  501. INNER JOIN {db_prefix}boards AS b ON (b.id_board = ud.id_board AND {query_see_board})
  502. WHERE ud.id_member = {int:current_member}
  503. AND type = {int:draft_type}' . (!empty($modSettings['drafts_keep_days']) ? '
  504. AND poster_time > {int:time}' : '') . '
  505. ORDER BY ud.id_draft ' . ($reverse ? 'ASC' : 'DESC') . '
  506. LIMIT ' . $start . ', ' . $maxIndex,
  507. array(
  508. 'current_member' => $memID,
  509. 'draft_type' => $draft_type,
  510. 'time' => (!empty($modSettings['drafts_keep_days']) ? (time() - ($modSettings['drafts_keep_days'] * 86400)) : 0),
  511. )
  512. );
  513. // Start counting at the number of the first message displayed.
  514. $counter = $reverse ? $context['start'] + $maxIndex + 1 : $context['start'];
  515. $context['posts'] = array();
  516. while ($row = $smcFunc['db_fetch_assoc']($request))
  517. {
  518. // Censor....
  519. if (empty($row['body']))
  520. $row['body'] = '';
  521. $row['subject'] = $smcFunc['htmltrim']($row['subject']);
  522. if (empty($row['subject']))
  523. $row['subject'] = $txt['no_subject'];
  524. censorText($row['body']);
  525. censorText($row['subject']);
  526. // BBC-ilize the message.
  527. $row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], 'draft' . $row['id_draft']);
  528. // And the array...
  529. $context['drafts'][$counter += $reverse ? -1 : 1] = array(
  530. 'body' => $row['body'],
  531. 'counter' => $counter,
  532. 'alternate' => $counter % 2,
  533. 'board' => array(
  534. 'name' => $row['bname'],
  535. 'id' => $row['id_board']
  536. ),
  537. 'topic' => array(
  538. 'id' => $row['id_topic'],
  539. 'link' => empty($row['id']) ? $row['subject'] : '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['subject'] . '</a>',
  540. ),
  541. 'subject' => $row['subject'],
  542. 'time' => timeformat($row['poster_time']),
  543. 'timestamp' => forum_time(true, $row['poster_time']),
  544. 'icon' => $row['icon'],
  545. 'id_draft' => $row['id_draft'],
  546. 'locked' => $row['locked'],
  547. 'sticky' => $row['is_sticky'],
  548. );
  549. }
  550. $smcFunc['db_free_result']($request);
  551. // All drafts were retrieved in reverse order, get them right again.
  552. if ($reverse)
  553. $context['drafts'] = array_reverse($context['drafts'], true);
  554. $context['sub_template'] = 'showDrafts';
  555. }
  556. /**
  557. * Show all PM drafts of the current user
  558. * Uses the showpmdraft template
  559. * Allows for the deleting and loading/editing of drafts
  560. *
  561. * @param type $memID
  562. * @param type $draft_type
  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. mod_addEvent(autosave, \'change\', toggle);
  755. toggle();
  756. function mod_addEvent(control, ev, fn)
  757. {
  758. if (control.addEventListener)
  759. {
  760. control.addEventListener(ev, fn, false);
  761. }
  762. else if (control.attachEvent)
  763. {
  764. control.attachEvent(\'on\'+ev, fn);
  765. }
  766. }
  767. function toggle()
  768. {
  769. var select_elem = document.getElementById(\'drafts_autosave_frequency\');
  770. select_elem.disabled = !autosave.checked;
  771. }
  772. ';
  773. // Final settings...
  774. $context['post_url'] = $scripturl . '?action=admin;area=managedrafts;save';
  775. $context['settings_title'] = $txt['managedrafts_settings'];
  776. // Prepare the settings...
  777. prepareDBSettingContext($config_vars);
  778. }
  779. ?>