PageRenderTime 64ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/inc/class.MyBBIntegrator.php

https://bitbucket.org/Captain_Lightning/arflux
PHP | 3406 lines | 2107 code | 462 blank | 837 comment | 425 complexity | 0b93b9726a5e3f87e6110875d1acfc3c MD5 | raw file
Possible License(s): BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * MyBBIntegrator - The integration class for MyBB and your website
  4. *
  5. * The MyBBIntegrator is a useful collection of variables and functions for easy MyBB integration
  6. * into the own website
  7. *
  8. * @author: David Olah (aka PHPDave - http://phpdave.com)
  9. * @version 1.3.1
  10. * @date July 2010
  11. * @copyright Copyright (c) 2009, David Olah
  12. *
  13. *
  14. *
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation, either version 3 of the License, or
  18. * (at your option) any later version.
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. */
  26. class MyBBIntegrator
  27. {
  28. /**
  29. * Cache Handler of MyBB
  30. *
  31. * @var object
  32. */
  33. var $cache;
  34. /**
  35. * Config Data of MyBB
  36. *
  37. * @var array
  38. */
  39. var $config;
  40. /**
  41. * Database Handler of MyBB
  42. *
  43. * @var object
  44. */
  45. var $db;
  46. /**
  47. * MyBB Super Variable containing a whole lot of information
  48. *
  49. * @var object
  50. */
  51. var $mybb;
  52. /**
  53. * MyBB's Post Parser
  54. *
  55. * @var object
  56. */
  57. var $parser;
  58. /**
  59. * Shows a message for errors occuring in this class.
  60. * Afterwards it stops the script
  61. *
  62. * @param string $message The error message
  63. */
  64. function _errorAndDie($message)
  65. {
  66. echo '<div style="width:92%; margin:4px auto; border:1px #DDD solid; background:#F1F1F1; padding:5px; color:#C00; font-weight:bold;">An error occured during script run.<br />'.$message.'</div>';
  67. die;
  68. }
  69. /**
  70. * Let's see if the correct password is given for a forum!
  71. * Possible Todo: Pass passowrds in an array for defining passwords for parent categories (so far this only works when parent foums have same pass)
  72. *
  73. * @param integer $forum_id ID of Forum
  74. * @param string $password Wow, what might this be??
  75. * @return boolean
  76. */
  77. function checkForumPassword($forum_id, $password = '', $pid = 0)
  78. {
  79. global $forum_cache;
  80. if(!is_array($forum_cache))
  81. {
  82. $forum_cache = cache_forums();
  83. if(!$forum_cache)
  84. {
  85. return false;
  86. }
  87. }
  88. // Loop through each of parent forums to ensure we have a password for them too
  89. $parents = explode(',', $forum_cache[$fid]['parentlist']);
  90. rsort($parents);
  91. if(!empty($parents))
  92. {
  93. foreach($parents as $parent_id)
  94. {
  95. if($parent_id == $forum_id || $parent_id == $pid)
  96. {
  97. continue;
  98. }
  99. if($forum_cache[$parent_id]['password'] != "")
  100. {
  101. if (!$this->checkForumPassword($parent_id, $password))
  102. {
  103. return false;
  104. }
  105. }
  106. }
  107. }
  108. $forum_password = $forum_cache[$forum_id]['password'];
  109. // A password is required
  110. if ($forum_password)
  111. {
  112. if (empty($password))
  113. {
  114. if (!$this->mybb->cookies['forumpass'][$forum_id] || ($this->mybb->cookies['forumpass'][$forum_id] && md5($this->mybb->user['uid'].$forum_password) != $this->mybb->cookies['forumpass'][$forum_id]))
  115. {
  116. return false;
  117. }
  118. else
  119. {
  120. return true;
  121. }
  122. }
  123. else
  124. {
  125. if ($forum_password == $password)
  126. {
  127. $this->setCookie('forumpass['.$forum_id.']', md5($this->mybb->user['uid'].$password), NULL, true);
  128. return true;
  129. }
  130. else
  131. {
  132. return false;
  133. }
  134. }
  135. }
  136. else
  137. {
  138. return true;
  139. }
  140. }
  141. /**
  142. * Enables you to close one or more threads
  143. * One thread: $thread_id is int
  144. * More threads: $thread_id is array with ints
  145. *
  146. * @param integer|array $thread_id See above
  147. * @param integer $forum_id This can be filled for a nice moderator log!
  148. * @return boolean
  149. */
  150. function closeThread($thread_id, $forum_id = 0)
  151. {
  152. if (!is_moderator($fid, "canopenclosethreads"))
  153. {
  154. return false;
  155. }
  156. $this->lang->load('moderation');
  157. $this->MyBBIntegratorClassObject('moderation', 'Moderation', MYBB_ROOT.'/inc/class_moderation.php');
  158. $this->moderation->close_threads($thread_id);
  159. $modlogdata['fid'] = $forum_id;
  160. $this->logModeratorAction($modlogdata, $this->lang->mod_process);
  161. return true;
  162. }
  163. /**
  164. * Insert a new Category into Database
  165. *
  166. * @param array $data Array with keys according to database layout, which holds the data of the forum
  167. * @param array $permissions Array with Permission entries (structure: array( 'canview' => array( 'usergroupid' => 1 ) )) (an example)
  168. * @param array $default_permissions Array which defines, if default permissions shall be used (structure: array( usergroupid => 0 / 1 )
  169. * Can be left empty, then this function will take care of it
  170. * @return $data with more values, like fid and parentlist
  171. */
  172. function createCategory($data, $permissions = array(), $default_permissions = array())
  173. {
  174. require_once MYBB_ADMIN_DIR.'inc/functions.php';
  175. if (!isset($data['name']))
  176. {
  177. $this->_errorAndDie('A new forum needs to have a name and a type');
  178. }
  179. $data['type'] = 'c';
  180. // Let's leave the parentlist creation to the script and let's not trust the dev :)
  181. if ($data['parentlist'] != '')
  182. {
  183. $data['parentlist'] = '';
  184. }
  185. // If there is no defined Parent ID, parent ID will be set to 0
  186. if (!isset($data['pid']) || $data['pid'] < 0)
  187. {
  188. $data['pid'] = 0;
  189. }
  190. else
  191. {
  192. $data['pid'] = intval($data['pid']);
  193. }
  194. if (!empty($permissions))
  195. {
  196. if (
  197. (!isset($permissions['canview']) || empty($permissions['canview'])) ||
  198. (!isset($permissions['canpostthreads']) || empty($permissions['canpostthreads'])) ||
  199. (!isset($permissions['canpostreplys']) || empty($permissions['canpostreplys'])) ||
  200. (!isset($permissions['canpostpolls']) || empty($permissions['canpostpolls'])) ||
  201. (!isset($permissions['canpostattachments']) || empty($permissions['canpostattachments']))
  202. )
  203. {
  204. $this->_errorAndDie('The $permissions Parameter does not have the correct format. It requires following keys: <i>canview, canpostthreads, canpostreplys, canpostpolls and canpostattachments</i>');
  205. }
  206. /**
  207. * If no default permissions are given, we will initiate them, default: yes
  208. * Since there is the possibility of additional usergroups, we will get the usergroups from the permissions array!
  209. * The structure of the inherit array is: keys = groupid
  210. * If the value of an inherit array item is 1, this means that the default_permissions shall be used
  211. */
  212. if (empty($default_permissions))
  213. {
  214. foreach ($permissions['canview'] as $gid)
  215. {
  216. $default_permissions[$gid] = 1;
  217. }
  218. }
  219. }
  220. $data['fid'] = $this->db->insert_query("forums", $data);
  221. $data['parentlist'] = make_parent_list($data['fid']);
  222. $this->db->update_query("forums", array("parentlist" => $data['parentlist']), 'fid=\''.$data['fid'].'\'');
  223. $this->cache->update_forums();
  224. if (!empty($permissions))
  225. {
  226. $inherit = $default_permissions;
  227. /**
  228. * $permissions['canview'][1] = 1 OR $permissions['canview'][1] = 0
  229. * --> $permissions[$name][$gid] = yes / no
  230. */
  231. $canview = $permissions['canview'];
  232. $canpostthreads = $permissions['canpostthreads'];
  233. $canpostpolls = $permissions['canpostpolls'];
  234. $canpostattachments = $permissions['canpostattachments'];
  235. $canpostreplies = $permissions['canpostreplys'];
  236. save_quick_perms($data['fid']);
  237. }
  238. return $data;
  239. }
  240. /**
  241. * Insert a new Forum into Database
  242. *
  243. * @param array $data Array with keys according to database layout, which holds the data of the forum
  244. * @param array $permissions Array with Permission entries (structure: array( 'canview' => array( 'usergroupid' => 1 ) )) (an example)
  245. * @param array $default_permissions Array which defines, if default permissions shall be used (structure: array( usergroupid => 0 / 1 )
  246. * Can be left empty, then this function will take care of it
  247. * @return $data with more values, like fid and parentlist
  248. */
  249. function createForum($data, $permissions = array(), $default_permissions = array())
  250. {
  251. require_once MYBB_ADMIN_DIR.'inc/functions.php';
  252. if (!isset($data['name']))
  253. {
  254. $this->_errorAndDie('A new forum needs to have a name and a type');
  255. }
  256. $data['type'] = 'f';
  257. // Let's leave the parentlist creation to the script and let's not trust the dev :)
  258. if ($data['parentlist'] != '')
  259. {
  260. $data['parentlist'] = '';
  261. }
  262. // If there is no defined Parent ID, parent ID will be set to 0
  263. if (!isset($data['pid']) || $data['pid'] < 0)
  264. {
  265. $data['pid'] = 0;
  266. }
  267. else
  268. {
  269. $data['pid'] = intval($data['pid']);
  270. }
  271. if (!empty($permissions))
  272. {
  273. if (
  274. (!isset($permissions['canview']) || empty($permissions['canview'])) ||
  275. (!isset($permissions['canpostthreads']) || empty($permissions['canpostthreads'])) ||
  276. (!isset($permissions['canpostreplys']) || empty($permissions['canpostreplys'])) ||
  277. (!isset($permissions['canpostpolls']) || empty($permissions['canpostpolls'])) ||
  278. (!isset($permissions['canpostattachments']) || empty($permissions['canpostattachments']))
  279. )
  280. {
  281. $this->_errorAndDie('The $permissions Parameter does not have the correct format. It requires following keys: <i>canview, canpostthreads, canpostreplys, canpostpolls and canpostattachments</i>');
  282. }
  283. /**
  284. * If no default permissions are given, we will initiate them, default: yes
  285. * Since there is the possibility of additional usergroups, we will get the usergroups from the permissions array!
  286. * The structure of the inherit array is: keys = groupid
  287. * If the value of an inherit array item is 1, this means that the default_permissions shall be used
  288. */
  289. if (empty($default_permissions))
  290. {
  291. foreach ($permissions['canview'] as $gid)
  292. {
  293. $default_permissions[$gid] = 1;
  294. }
  295. }
  296. }
  297. $data['fid'] = $this->db->insert_query("forums", $data);
  298. $data['parentlist'] = make_parent_list($data['fid']);
  299. $this->db->update_query("forums", array("parentlist" => $data['parentlist']), 'fid=\''.$data['fid'].'\'');
  300. $this->cache->update_forums();
  301. if (!empty($permissions))
  302. {
  303. $inherit = $default_permissions;
  304. /**
  305. * $permissions['canview'][1] = 1 OR $permissions['canview'][1] = 0
  306. * --> $permissions[$name][$gid] = yes / no
  307. */
  308. $canview = $permissions['canview'];
  309. $canpostthreads = $permissions['canpostthreads'];
  310. $canpostpolls = $permissions['canpostpolls'];
  311. $canpostattachments = $permissions['canpostattachments'];
  312. $canpostreplies = $permissions['canpostreplys'];
  313. save_quick_perms($data['fid']);
  314. }
  315. return $data;
  316. }
  317. /**
  318. * Create a new poll and assign it to a thread
  319. * Taken frm polls.php
  320. *
  321. * @param integer $thread_id ID of Thread where the poll should be assigned to
  322. * @param array $data The Data
  323. */
  324. function createPoll($thread_id, $data)
  325. {
  326. // Required keys in data array: options, question
  327. if (!isset($data['options']) || !isset($data['question']))
  328. {
  329. $this->_errorAndDie('One or more required array keys in parameter <i>$data</i> missing. Required keys are: <i>options</i>, <i>question</i>');
  330. }
  331. $this->lang->load('polls');
  332. $this->plugins->run_hooks("polls_do_newpoll_start");
  333. $query = $this->db->simple_select("threads", "*", "tid='".(int) $thread_id."'");
  334. $thread = $this->db->fetch_array($query);
  335. $fid = $thread['fid'];
  336. $forumpermissions = forum_permissions($fid);
  337. if (!$thread['tid'])
  338. {
  339. return $this->lang->error_invalidthread;
  340. }
  341. // No permission if: Not thread author; not moderator; no forum perms to view, post threads, post polls
  342. if (($thread['uid'] != $this->mybb->user['uid'] && !is_moderator($fid)) || ($forumpermissions['canview'] == 0 || $forumpermissions['canpostthreads'] == 0 || $forumpermissions['canpostpolls'] == 0))
  343. {
  344. return false;
  345. }
  346. if ($thread['poll'])
  347. {
  348. return $this->lang->error_pollalready;
  349. }
  350. $polloptions = count($data['options']);
  351. if($this->mybb->settings['maxpolloptions'] && $polloptions > $this->mybb->settings['maxpolloptions'])
  352. {
  353. $polloptions = $this->mybb->settings['maxpolloptions'];
  354. }
  355. if (!isset($data['postoptions']))
  356. {
  357. $data['postoptions'] = array('multiple', 'public');
  358. }
  359. $postoptions = $data['postoptions'];
  360. if ($postoptions['multiple'] != '1')
  361. {
  362. $postoptions['multiple'] = 0;
  363. }
  364. if ($postoptions['public'] != '1')
  365. {
  366. $postoptions['public'] = 0;
  367. }
  368. if ($polloptions < 2)
  369. {
  370. $polloptions = "2";
  371. }
  372. $optioncount = "0";
  373. $options = $data['options'];
  374. for($i = 0; $i < $polloptions; ++$i)
  375. {
  376. if (trim($options[$i]) != "")
  377. {
  378. $optioncount++;
  379. }
  380. if (my_strlen($options[$i]) > $this->mybb->settings['polloptionlimit'] && $this->mybb->settings['polloptionlimit'] != 0)
  381. {
  382. $lengtherror = 1;
  383. break;
  384. }
  385. }
  386. if ($lengtherror)
  387. {
  388. return $this->lang->error_polloptiontoolong;
  389. }
  390. if (empty($data['question']) || $optioncount < 2)
  391. {
  392. return $this->lang->error_noquestionoptions;
  393. }
  394. $optionslist = '';
  395. $voteslist = '';
  396. for($i = 0; $i < $optioncount; ++$i)
  397. {
  398. if(trim($options[$i]) != '')
  399. {
  400. if($i > 0)
  401. {
  402. $optionslist .= '||~|~||';
  403. $voteslist .= '||~|~||';
  404. }
  405. $optionslist .= $options[$i];
  406. $voteslist .= '0';
  407. }
  408. }
  409. if (!isset($data['timeout']))
  410. {
  411. $data['timeout'] = 0;
  412. }
  413. if($data['timeout'] > 0)
  414. {
  415. $timeout = intval($data['timeout']);
  416. }
  417. else
  418. {
  419. $timeout = 0;
  420. }
  421. $newpoll = array(
  422. "tid" => $thread['tid'],
  423. "question" => $this->db->escape_string($data['question']),
  424. "dateline" => TIME_NOW,
  425. "options" => $this->db->escape_string($optionslist),
  426. "votes" => $this->db->escape_string($voteslist),
  427. "numoptions" => intval($optioncount),
  428. "numvotes" => 0,
  429. "timeout" => $timeout,
  430. "closed" => 0,
  431. "multiple" => $postoptions['multiple'],
  432. "public" => $postoptions['public']
  433. );
  434. $this->plugins->run_hooks("polls_do_newpoll_process");
  435. $pid = $this->db->insert_query("polls", $newpoll);
  436. $this->db->update_query("threads", array('poll' => $pid), "tid='".$thread['tid']."'");
  437. $this->plugins->run_hooks("polls_do_newpoll_end");
  438. return true;
  439. }
  440. /**
  441. * Insert a new post into Database
  442. *
  443. * @param array $data Post Data
  444. * @return array|string When true it will return an array with postID and status of being visible - false = error array or inline string
  445. */
  446. function createPost($data, $inline_errors = true)
  447. {
  448. require_once MYBB_ROOT.'inc/functions_post.php';
  449. require_once MYBB_ROOT.'/inc/datahandlers/post.php';
  450. $posthandler = new PostDataHandler('insert');
  451. $this->plugins->run_hooks('newreply_do_newreply_start');
  452. $posthandler->set_data($data);
  453. if (!$posthandler->validate_post())
  454. {
  455. $errors = $posthandler->get_friendly_errors();
  456. return ($inline_errors === true) ? inline_error($errors) : $errors;
  457. }
  458. $this->plugins->run_hooks('newreply_do_newreply_end');
  459. return $posthandler->insert_post();
  460. }
  461. /**
  462. * Inserts a thread into the database
  463. *
  464. * @param array $data Thread data
  465. * @param boolean $inline_errors Defines if we want a formatted error string or an array
  466. * @return array|string
  467. * @return array|string When true it will return an array with threadID, postID and status of being visible - false = error array or inline string
  468. */
  469. function createThread($data, $inline_errors = true)
  470. {
  471. require_once MYBB_ROOT.'inc/functions_post.php';
  472. require_once MYBB_ROOT.'/inc/datahandlers/post.php';
  473. $posthandler = new PostDataHandler('insert');
  474. $posthandler->action = 'thread';
  475. $posthandler->set_data($data);
  476. if (!$posthandler->validate_thread())
  477. {
  478. $errors = $posthandler->get_friendly_errors();
  479. return ($inline_errors === true) ? inline_error($errors) : $errors;
  480. }
  481. return $posthandler->insert_thread();
  482. }
  483. /**
  484. * Insert a new user into Database
  485. *
  486. * @param array $data User data
  487. * @param boolean $inline_errors Defines if we want a formatted error string or an array
  488. * @return array|string When true it will return an array with some user data - false = error array or inline string
  489. */
  490. function createUser($data, $inline_errors = true)
  491. {
  492. require_once MYBB_ROOT.'inc/functions_user.php';
  493. require_once MYBB_ROOT.'/inc/datahandlers/user.php';
  494. $userhandler = new UserDataHandler('insert');
  495. $this->plugins->run_hooks('admin_user_users_add');
  496. $userhandler->set_data($data);
  497. if (!$userhandler->validate_user())
  498. {
  499. $errors = $userhandler->get_friendly_errors();
  500. return ($inline_errors === true) ? inline_error($errors) : $errors;
  501. }
  502. $this->plugins->run_hooks('admin_user_users_add_commit');
  503. return $userhandler->insert_user();
  504. }
  505. /**
  506. * Escapes a value for DB usage
  507. *
  508. * @param mixed $value Any value to use with the database
  509. * @return string
  510. */
  511. function dbEscape($value)
  512. {
  513. return $this->db->escape_string($value);
  514. }
  515. /**
  516. * Remove a poll
  517. * Taken from moderation.php
  518. *
  519. * @param integer $poll_id ID of Poll to be deleted
  520. * @return boolean|string
  521. */
  522. function deletePoll($poll_id)
  523. {
  524. $this->lang->load('moderation');
  525. $this->MyBBIntegratorClassObject('moderation', 'Moderation', MYBB_ROOT.'/inc/class_moderation.php');
  526. $query = $this->db->simple_select("polls", "*", "pid='$poll_id'");
  527. $poll = $this->db->fetch_array($query);
  528. if(!$poll['pid'])
  529. {
  530. return $this->lang->error_invalidpoll;
  531. }
  532. $thread = $this->getThread($poll['tid']);
  533. if(!is_moderator($thread['fid'], "candeleteposts"))
  534. {
  535. if($permissions['candeletethreads'] != 1 || $this->mybb->user['uid'] != $thread['uid'])
  536. {
  537. return false;
  538. }
  539. }
  540. $modlogdata = array();
  541. $modlogdata['tid'] = $poll['tid'];
  542. $this->plugins->run_hooks("moderation_do_deletepoll");
  543. $this->lang->poll_deleted = $this->lang->sprintf($this->lang->poll_deleted, $thread['subject']);
  544. $this->logModeratorAction($modlogdata, $this->lang->poll_deleted);
  545. $this->moderation->delete_poll($poll['pid']);
  546. return true;
  547. }
  548. /**
  549. * Delete the poll of a thread
  550. * Taken from moderation.php
  551. *
  552. * @param integer $thread_id Thread-ID where the poll is located
  553. * @return boolean|string
  554. */
  555. function deletePollOfThread($thread_id)
  556. {
  557. $this->lang->load('polls');
  558. $this->lang->load('moderation');
  559. $this->MyBBIntegratorClassObject('moderation', 'Moderation', MYBB_ROOT.'/inc/class_moderation.php');
  560. $thread = $this->getThread($thread_id);
  561. $permissions = forum_permissions($thread['fid']);
  562. if (!is_moderator($thread['fid'], "candeleteposts"))
  563. {
  564. if($permissions['candeletethreads'] != 1 || $this->mybb->user['uid'] != $thread['uid'])
  565. {
  566. return false;
  567. }
  568. }
  569. $query = $this->db->simple_select("polls", "*", "tid='$thread_id'");
  570. $poll = $this->db->fetch_array($query);
  571. if(!$poll['pid'])
  572. {
  573. return $this->lang->error_invalidpoll;
  574. }
  575. $modlogdata = array();
  576. $modlogdata['tid'] = $poll['tid'];
  577. $this->plugins->run_hooks("moderation_do_deletepoll");
  578. $this->lang->poll_deleted = $this->lang->sprintf($this->lang->poll_deleted, $thread['subject']);
  579. $this->logModeratorAction($modlogdata, $this->lang->poll_deleted);
  580. $this->moderation->delete_poll($poll['pid']);
  581. return true;
  582. }
  583. /**
  584. * Flag private messages as deleted
  585. *
  586. * @param integer|array $pm_id ID(s) of Private Messages (many IDs require an array)
  587. */
  588. function deletePrivateMessage($pm_id)
  589. {
  590. require_once MYBB_ROOT.'inc/functions_user.php';
  591. $this->plugins->run_hooks('private_delete_start');
  592. $data = array(
  593. 'folder' => 4,
  594. 'deletetime' => TIME_NOW
  595. );
  596. if (is_array($pm_id))
  597. {
  598. $this->db->update_query('privatemessages', $data, 'pmid IN ('.implode(',', array_map('intval', $pm_id)).')');
  599. }
  600. else
  601. {
  602. $this->db->update_query('privatemessages', $data, 'pmid = '.intval($pm_id));
  603. }
  604. update_pm_count();
  605. $this->plugins->run_hooks('private_delete_end');
  606. }
  607. /**
  608. * Flag all private messages of a user as deleted
  609. * It is also possible to flag pms as deleted of multiple users, when paramater is an array with IDs
  610. *
  611. * @param integer|array $pm_id ID(s) of User IDs (many IDs require an array)
  612. */
  613. function deletePrivateMessagesOfUser($user_id)
  614. {
  615. require_once MYBB_ROOT.'inc/functions_user.php';
  616. $this->plugins->run_hooks('private_delete_start');
  617. $data = array(
  618. 'folder' => 4,
  619. 'deletetime' => TIME_NOW
  620. );
  621. if (is_array($user_id))
  622. {
  623. $this->db->update_query('privatemessages', $data, 'uid IN ('.implode(',', array_map('intval', $user_id)).')');
  624. }
  625. else
  626. {
  627. $this->db->update_query('privatemessages', $data, 'uid = '.intval($user_id));
  628. }
  629. update_pm_count();
  630. $this->plugins->run_hooks('private_delete_end');
  631. }
  632. /**
  633. * Generates a Captcha
  634. *
  635. * @return array
  636. */
  637. function generateCaptcha()
  638. {
  639. $randomstr = random_str(5);
  640. $imagehash = md5(random_str(12));
  641. $imagearray = array(
  642. "imagehash" => $imagehash,
  643. "imagestring" => $randomstr,
  644. "dateline" => TIME_NOW
  645. );
  646. $this->db->insert_query("captcha", $imagearray);
  647. return array_merge($imagearray, array(
  648. 'captcha' => '<img src="'.$this->mybb->settings['bburl'].'/captcha.php?imagehash='.$imagehash.'" />'
  649. ));
  650. }
  651. /**
  652. * Generates a posthash
  653. *
  654. * @param integer $user_id User-ID
  655. * @return string MD5
  656. */
  657. function generatePosthash($user_id = 0)
  658. {
  659. mt_srand((double) microtime() * 1000000);
  660. if ($user_id == 0)
  661. {
  662. return md5($this->mybb->user['uid'].mt_rand());
  663. }
  664. else
  665. {
  666. return md5($user_id.mt_rand());
  667. }
  668. }
  669. /**
  670. * Get the Hottest Threads within a defined timespan
  671. *
  672. * @param integer $timespan The timespan you want to use for fetching the hottest topics (in seconds)
  673. * @param string $post_sort_order Sort Order to the posts you are fetching (ordered by the dateline)
  674. * @param string $postamount_sort_order Sort order of the threads (ordered by the amount of posts)
  675. * @return array
  676. */
  677. function getBusyThreadsWithinTimespan($timespan = 86400, $post_sort_order = 'DESC', $postamount_sort_order = 'DESC')
  678. {
  679. $threads = array();
  680. // Make sure the parameters have correct values
  681. $post_sort_order = ($post_sort_order == 'DESC') ? 'DESC' : 'ASC';
  682. $postamount_sort_order = ($postamount_sort_order == 'DESC') ? 'DESC' : 'ASC';
  683. $query = $this->db->query('
  684. SELECT p.`pid`, p.`message`, p.`uid` as postuid, p.`username` as postusername, p.`dateline`,
  685. t.`tid`, t.`fid`, t.`subject`, t.`uid` as threaduid, t.`username` as threadusername, t.`lastpost`, t.`lastposter`, t.`lastposteruid`, t.`views`, t.`replies`
  686. FROM '.TABLE_PREFIX.'posts p
  687. INNER JOIN '.TABLE_PREFIX.'threads t ON t.`tid` = p.`tid`
  688. WHERE p.`dateline` >= '.(TIME_NOW - $timespan).'
  689. ORDER BY p.`dateline` '.$post_sort_order.'
  690. ');
  691. while ($post = $this->db->fetch_array($query))
  692. {
  693. /**
  694. * The return array we are building is being filled with the thread itself, but also with the posts
  695. * We will later increase the Postamount, so we can sort it
  696. */
  697. if (!isset($threads[$post['tid']]))
  698. {
  699. $threads[$post['tid']] = array(
  700. 'tid' => $post['tid'],
  701. 'fid' => $post['fid'],
  702. 'subject' => $post['subject'],
  703. 'uid' => $post['threaduid'],
  704. 'username' => $post['threadusername'],
  705. 'lastpost' => $post['lastpost'],
  706. 'lastposter' => $post['lastposter'],
  707. 'lastposteruid' => $post['lastposteruid'],
  708. 'views' => $post['views'],
  709. 'replies' => $post['replies'],
  710. 'postamount' => 1,
  711. 'posts' => array()
  712. );
  713. // The first run of one thread also brings a post, so we assign this post
  714. $threads[$post['tid']]['posts'][] = array(
  715. 'pid' => $post['pid'],
  716. 'message' => $post['message'],
  717. 'uid' => $post['postuid'],
  718. 'username' => $post['postusername'],
  719. 'dateline' => $post['dateline']
  720. );
  721. }
  722. else
  723. {
  724. // The thread array key exists already, so we increment the postamount and save another post
  725. $threads[$post['tid']]['postamount']++;
  726. $threads[$post['tid']]['posts'][] = array(
  727. 'pid' => $post['pid'],
  728. 'message' => $post['message'],
  729. 'uid' => $post['postuid'],
  730. 'username' => $post['postusername'],
  731. 'dateline' => $post['dateline']
  732. );
  733. }
  734. }
  735. // Sort function for ascending posts
  736. function arraySortByPostamountASC($item1, $item2)
  737. {
  738. if ($item1['postamount'] == $item2['postamount'])
  739. {
  740. return 0;
  741. }
  742. if ($item1['postamount'] > $item2['postamount'])
  743. {
  744. return 1;
  745. }
  746. else
  747. {
  748. return -1;
  749. }
  750. }
  751. // Sort function for descending posts
  752. function arraySortByPostamountDESC($item1, $item2)
  753. {
  754. if ($item1['postamount'] == $item2['postamount'])
  755. {
  756. return 0;
  757. }
  758. if ($item1['postamount'] > $item2['postamount'])
  759. {
  760. return -1;
  761. }
  762. else
  763. {
  764. return 1;
  765. }
  766. }
  767. // Let's sort the threads now
  768. usort($threads, 'arraySortByPostamount'.$postamount_sort_order);
  769. return $threads;
  770. }
  771. /**
  772. * Returns data of a specified forum
  773. * Refers to: inc/functions.php
  774. *
  775. * @param integer $forum_id ID of forum to fetch data from
  776. * @param integer $active_override If set to 1, will override the active forum status
  777. * @return array|boolean If unsuccessful, it returns false - Otherwise the Database row
  778. */
  779. function getForum($forum_id, $active_override = 0)
  780. {
  781. $forum = get_forum($forum_id, $active_override);
  782. // Do we have permission?
  783. $forumpermissions = forum_permissions($forum['fid']);
  784. if ($forumpermissions['canview'] != 1 || $forumpermissions['canviewthreads'] != 1)
  785. {
  786. // error_no_permission();
  787. return false;
  788. }
  789. else
  790. {
  791. return $forum;
  792. }
  793. }
  794. /**
  795. * Return members of the board with administrative function
  796. * Taken from /showteam.php
  797. *
  798. * @return array
  799. */
  800. function getForumStaff()
  801. {
  802. $this->lang->load('showteam');
  803. $usergroups = array();
  804. $moderators = array();
  805. $users = array();
  806. // Fetch the list of groups which are to be shown on the page
  807. $query = $this->db->simple_select("usergroups", "gid, title, usertitle", "showforumteam=1", array('order_by' => 'disporder'));
  808. while($usergroup = $this->db->fetch_array($query))
  809. {
  810. $usergroups[$usergroup['gid']] = $usergroup;
  811. }
  812. if (empty($usergroups))
  813. {
  814. return $this->lang->error_noteamstoshow;
  815. }
  816. // Fetch specific forum moderator details
  817. if ($usergroups[6]['gid'])
  818. {
  819. $query = $this->db->query("
  820. SELECT m.*, f.name
  821. FROM ".TABLE_PREFIX."moderators m
  822. LEFT JOIN ".TABLE_PREFIX."users u ON (u.uid=m.uid)
  823. LEFT JOIN ".TABLE_PREFIX."forums f ON (f.fid=m.fid)
  824. WHERE f.active = 1
  825. ORDER BY u.username
  826. ");
  827. while($moderator = $this->db->fetch_array($query))
  828. {
  829. $moderators[$moderator['uid']][] = $moderator;
  830. }
  831. }
  832. // Now query the users of those specific groups
  833. $groups_in = implode(",", array_keys($usergroups));
  834. $users_in = implode(",", array_keys($moderators));
  835. if (!$groups_in)
  836. {
  837. $groups_in = 0;
  838. }
  839. if (!$users_in)
  840. {
  841. $users_in = 0;
  842. }
  843. $forum_permissions = forum_permissions();
  844. $query = $this->db->simple_select("users", "uid, username, displaygroup, usergroup, ignorelist, hideemail, receivepms", "displaygroup IN ($groups_in) OR (displaygroup='0' AND usergroup IN ($groups_in)) OR uid IN ($users_in)", array('order_by' => 'username'));
  845. while ($user = $this->db->fetch_array($query))
  846. {
  847. // If this user is a moderator
  848. if (isset($moderators[$user['uid']]))
  849. {
  850. foreach ($moderators[$user['uid']] as $forum)
  851. {
  852. if ($forum_permissions[$forum['fid']]['canview'] == 1)
  853. {
  854. $forum_url = get_forum_link($forum['fid']);
  855. }
  856. }
  857. $usergroups[6]['user_list'][$user['uid']] = $user;
  858. }
  859. if ($user['displaygroup'] == '6' || $user['usergroup'] == '6')
  860. {
  861. $usergroups[6]['user_list'][$user['uid']] = $user;
  862. }
  863. // Are they also in another group which is being shown on the list?
  864. if ($user['displaygroup'] != 0)
  865. {
  866. $group = $user['displaygroup'];
  867. }
  868. else
  869. {
  870. $group = $user['usergroup'];
  871. }
  872. if ($usergroups[$group] && $group != 6)
  873. {
  874. $usergroups[$group]['user_list'][$user['uid']] = $user;
  875. }
  876. }
  877. return $usergroups;
  878. }
  879. /**
  880. * Return the latest threads of one forum, where a post has been posted
  881. *
  882. * @param integer $forum_id Forum ID to fetch threads from
  883. * @param integer $limit Amount of threads to get
  884. * @param boolean $excluse_invisible Shall we also get invisible threads?
  885. * @return array
  886. */
  887. function getLatestActiveThreads($forum_id = 0, $limit = 7, $exclude_invisible = true)
  888. {
  889. if ($forum_id == 0)
  890. {
  891. $this->_errorAndDie('Specified forum ID cannot be 0!');
  892. }
  893. else
  894. {
  895. // Do we have permission?
  896. $forumpermissions = forum_permissions($forum_id);
  897. if ($forumpermissions['canview'] != 1 || $forumpermissions['canviewthreads'] != 1)
  898. {
  899. // error_no_permission();
  900. return false;
  901. }
  902. }
  903. // This will be the array, where we can save the threads
  904. $threads = array();
  905. // We want to get a list of threads, starting with the newest one
  906. $query_params = array(
  907. 'order_by' => 'lastpost',
  908. 'order_dir' => 'DESC',
  909. 'limit' => intval($limit)
  910. );
  911. /**
  912. * If defined forum id is 0, we do not fetch threads from only one forum,
  913. * but we fetch the latest threads of all forums
  914. * Therefore we add the forum_id in the where condition
  915. * We only fetch visible threads, if there is anything we want to hide ;)
  916. * However we can also define that we want the invisible threads as well
  917. */
  918. $fetch_invisible_threads = ($exclude_invisible == true) ? '1' : '0';
  919. $condition = ($forum_id != 0) ? ' `visible` = '.$fetch_invisible_threads.' AND `fid` = '.intval($forum_id) : '';
  920. // Run the Query
  921. $query = $this->db->simple_select('threads', '*', $condition, $query_params);
  922. // Now let's iterate through the fetched threads to create the return array
  923. while ($thread = $this->db->fetch_array($query))
  924. {
  925. $threads[] = $thread;
  926. }
  927. return $threads;
  928. }
  929. /**
  930. * Return newly created threads, regardless of replies
  931. *
  932. * @param integer|array $forum_id Forum ID / Forum IDs to fetch threads from
  933. * @param string $fields Name of fields if you want to fetch specific fields
  934. * @param integer $limit Amount of threads to get
  935. * @param boolean $excluse_invisible Shall we also get invisible threads?
  936. * @param boolean $join_forums Shall we also get the information from the forums where the threads are located in?
  937. * @param boolean $join_first_post Shall we get the first post of this thread as well?
  938. * @return array
  939. */
  940. function getLatestThreads($forum_id = 0, $fields = '*', $limit = 7, $exclude_invisible = true, $join_forums = true, $join_first_post = true)
  941. {
  942. if ($forum_id != 0)
  943. {
  944. // If we have multiple values, we have to check permission for each forum!
  945. if (is_array($forum_id))
  946. {
  947. foreach ($forum_id as $single_forum_id)
  948. {
  949. $forum_permissions = forum_permissions($single_forum_id);
  950. if ($forum_permissions['canview'] != 1 || $forum_permissions['canviewthreads'] != 1)
  951. {
  952. // error_no_permission();
  953. return false;
  954. }
  955. }
  956. }
  957. else
  958. {
  959. // Do we have permission?
  960. $forumpermissions = forum_permissions($forum_id);
  961. if ($forumpermissions['canview'] != 1 || $forumpermissions['canviewthreads'] != 1)
  962. {
  963. // error_no_permission();
  964. return false;
  965. }
  966. }
  967. }
  968. // This is what we will be returning
  969. $threads = array();
  970. // Do we want to get invisible threads as well?
  971. $fetch_invisible_threads = ($exclude_invisible == true) ? '1' : '0';
  972. $condition = 't.`visible` = '.$fetch_invisible_threads;
  973. // Are we fetching threads from multiple forums?
  974. if (is_array($forum_id) || is_object($forum_id))
  975. {
  976. $condition .= ' AND t.`fid` IN ('.implode(', ', $forum_id).')';
  977. }
  978. // Or are we just fetching threads from one forum?
  979. else
  980. {
  981. $condition .= ($forum_id == 0) ? '' : ' AND t.`fid` = '.$forum_id;
  982. }
  983. // Do we want to get information of the forum where the thread is located in?
  984. $forum_join = ($join_forums == true) ? 'INNER JOIN '.TABLE_PREFIX.'forums f ON f.`fid` = t.`fid`' : '';
  985. // Do we want to get the first post from the thread?
  986. $first_post_join = ($join_first_post == true) ? 'INNER JOIN '.TABLE_PREFIX.'posts p ON p.`pid` = t.`firstpost`' : '';
  987. // Run the Query
  988. $query = $this->db->query('
  989. SELECT '.$fields.'
  990. FROM '.TABLE_PREFIX.'threads t
  991. '.$forum_join.'
  992. '.$first_post_join.'
  993. WHERE '.$condition.'
  994. ORDER BY t.`dateline` DESC
  995. LIMIT '.intval($limit).'
  996. ');
  997. // Iterate through the results and assign it to our returning array
  998. while ($thread = $this->db->fetch_array($query))
  999. {
  1000. $threads[] = $thread;
  1001. }
  1002. return $threads;
  1003. }
  1004. /**
  1005. * Return recently posted posts
  1006. *
  1007. * @param integer|array Either a single Thread ID or an array containing thread IDs
  1008. * @param string Fields, which shall be fetched from the posts table
  1009. * @param integer How many posts shall be fetched?
  1010. * @param boolean Shall we also return invisible ones?
  1011. * @return array
  1012. */
  1013. function getLatestPosts($thread_id = 0, $fields = '*', $limit = 7, $exclude_invisible = true)
  1014. {
  1015. // Posts will be stored in this array
  1016. $posts = array();
  1017. // Posts will be returned in descending order, starting with the newest
  1018. $query_params = array(
  1019. 'order_by' => 'dateline',
  1020. 'order_dir' => 'DESC',
  1021. 'limit' => intval($limit)
  1022. );
  1023. // We want to fetch posts from multiple threads
  1024. if (is_array($thread_id) || is_object($thread_id))
  1025. {
  1026. // Multiple threads = IN (...) Operator
  1027. $condition = '`fid` IN ('.implode(', ', $thread_id).')';
  1028. }
  1029. else
  1030. {
  1031. // Single thread = normal WHERE X = Y - if set 0 we fetch posts from all threads
  1032. $condition = ($thread_id == 0) ? '1 = 1' : '`fid` = '.intval($thread_id);
  1033. }
  1034. /**
  1035. * If defined forum id is 0, we do not fetch threads from only one forum,
  1036. * but we fetch the latest threads of all forums
  1037. * Therefore we add the forum_id in the where condition
  1038. * We only fetch visible threads, if there is anything we want to hide ;)
  1039. * However we can also define that we want the invisible threads as well
  1040. */
  1041. $fetch_invisible_threads = ($exclude_invisible == true) ? '1' : '0';
  1042. $condition .= ' AND `visible` = '.$fetch_invisible_threads;
  1043. // Run the Query
  1044. $query = $this->db->simple_select('posts', $fields, $condition, $query_params);
  1045. // Now let's iterate through the fetched posts to create the return array
  1046. while ($post = $this->db->fetch_array($query))
  1047. {
  1048. $posts[] = $post;
  1049. }
  1050. return $posts;
  1051. }
  1052. /**
  1053. * Retrieve member list
  1054. * Ideal to offer a multi-page member list
  1055. *
  1056. * @param array $data Contains data affecting the member query - List of Array keys below
  1057. * - orderby: What table column will the member list be sorted by?
  1058. * - orderdir: Ascending or Descending order direction
  1059. * - perpage: Amount of members to fetch (set 0 for all members)
  1060. * - letter: Beginning character of member name
  1061. * - username: Searching for a matching username
  1062. * - username_match: Set this to "begins" when username shall being with given token - otherwise it goes or "contains"
  1063. * - website: String contained in website
  1064. * - aim: Search for an AIM
  1065. * - icq: Search for an ICQ number
  1066. * - msn: Search for a MSN ID
  1067. * - yahoo: Search for a Yahoo ID
  1068. * - page: Which page of the list will we be retrieving
  1069. * @return array
  1070. */
  1071. function getMembers($data = array())
  1072. {
  1073. /**
  1074. * Make sure we have initial values in the data array
  1075. */
  1076. $data['orderby'] = (!isset($data['orderby'])) ? 'u.`username`' : $data['orderby'];
  1077. $data['orderdir'] = (!isset($data['orderdir'])) ? 'ASC' : strtoupper($data['orderdir']);
  1078. $data['orderdir'] = ($data['orderdir'] == 'ASC') ? 'ASC' : 'DESC';
  1079. $data['perpage'] = (!isset($data['perpage'])) ? (int) $this->mybb->settings['membersperpage'] : (int) $data['perpage'];
  1080. $data['letter'] = (!isset($data['letter'])) ? '' : $data['letter'];
  1081. $data['username'] = (!isset($data['username'])) ? '' : $data['username'];
  1082. $data['username_match'] = (!isset($data['username_match'])) ? 'begins' : $data['username_match'];
  1083. $data['website'] = (!isset($data['website'])) ? '' : $data['website'];
  1084. $data['aim'] = (!isset($data['aim'])) ? '' : $data['aim'];
  1085. $data['icq'] = (!isset($data['icq'])) ? '' : $data['icq'];
  1086. $data['msn'] = (!isset($data['msn'])) ? '' : $data['msn'];
  1087. $data['yahoo'] = (!isset($data['yahoo'])) ? '' : $data['yahoo'];
  1088. $data['page'] = (!isset($data['page'])) ? 1 : (int) $data['page'];
  1089. /**
  1090. * Let's build the DB query now!
  1091. */
  1092. $sql_where = 'WHERE 1 = 1';
  1093. // Username begins with a letter or number
  1094. if (strlen($data['letter']) == 1)
  1095. {
  1096. $data['letter'] = chr(ord($data['letter']));
  1097. // Letter is 0: Shall start with number
  1098. if ($data['letter'] == '0')
  1099. {
  1100. $sql_where .= " AND u.`username` NOT REGEXP('[a-zA-Z]')";
  1101. }
  1102. // letter is not 0, so it will be fetching names according to first char
  1103. else
  1104. {
  1105. $sql_where .= " AND u.`username` LIKE '".$this->db->escape_string($data['letter'])."%'";
  1106. }
  1107. }
  1108. // Search for matching username
  1109. if (strlen($data['username']) > 0)
  1110. {
  1111. $data['username'] = htmlspecialchars_uni($data['username']);
  1112. if ($data['username_match'] == 'begins')
  1113. {
  1114. $sql_where .= " AND u.`username` LIKE '".$this->db->escape_string_like($data['username'])."%'";
  1115. }
  1116. else
  1117. {
  1118. $sql_where .= " AND u.`username` LIKE '%".$this->db->escape_string_like($data['username'])."%'";
  1119. }
  1120. }
  1121. // Search for website
  1122. if (strlen($data['website']) > 0)
  1123. {
  1124. $data['website'] = trim(htmlspecialchars_uni($data['website']));
  1125. $sql_where .= " AND u.`website` LIKE '%".$this->db->escape_string_like($data['website'])."%'";
  1126. }
  1127. // Search for AIM
  1128. if (strlen($data['aim']) > 0)
  1129. {
  1130. $sql_where .= " AND u.`aim` LIKE '%".$this->db->escape_string_like($data['aim'])."%'";
  1131. }
  1132. // Search for ICQ
  1133. if (strlen($data['icq']) > 0)
  1134. {
  1135. $sql_where .= " AND u.`icq` LIKE '%".$this->db->escape_string_like($data['icq'])."%'";
  1136. }
  1137. // Search for MSN
  1138. if (strlen($data['msn']) > 0)
  1139. {
  1140. $sql_where .= " AND u.`msn` LIKE '%".$this->db->escape_string_like($data['msn'])."%'";
  1141. }
  1142. // Search for Yahoo
  1143. if (strlen($data['yahoo']) > 0)
  1144. {
  1145. $sql_where .= " AND u.`yahoo` LIKE '%".$this->db->escape_string_like($data['yahoo'])."%'";
  1146. }
  1147. // Build the LIMIT-part of the query here
  1148. if ($data['perpage'] == 0)
  1149. {
  1150. $limit_string = '';
  1151. }
  1152. else
  1153. {
  1154. if ($data['page'] > 0)
  1155. {
  1156. $limit_string = 'LIMIT '.(($data['page'] - 1) * $data['perpage']).', '.$data['perpage'];
  1157. }
  1158. else
  1159. {
  1160. $limit_string = 'LIMIT '.$data['perpage'];
  1161. }
  1162. }
  1163. $sql .= '
  1164. SELECT u.*, f.*
  1165. FROM '.TABLE_PREFIX.'users u
  1166. LEFT JOIN '.TABLE_PREFIX.'userfields f ON f.`ufid` = u.`uid`
  1167. '.$sql_where.'
  1168. ORDER BY '.$data['orderby'].' '.$data['orderdir'].'
  1169. '.$limit_string.'
  1170. ';
  1171. $query = $this->db->query($sql);
  1172. $arr = array();
  1173. while ($member = $this->db->fetch_array($query))
  1174. {
  1175. $arr[] = $member;
  1176. }
  1177. return $arr;
  1178. }
  1179. /**
  1180. * Read some info about a poll
  1181. *
  1182. * @param integer $poll_id ID of Poll to fetch infos from
  1183. * @return array
  1184. */
  1185. function getPoll($poll_id)
  1186. {
  1187. if ($poll_id == 0)
  1188. {
  1189. $this->_errorAndDie('Specified poll ID cannot be 0!');
  1190. }
  1191. $query = $this->db->query('
  1192. SELECT *
  1193. FROM '.TABLE_PREFIX.'polls
  1194. WHERE `pid` = '.(int) $poll_id.'
  1195. LIMIT 1
  1196. ');
  1197. $poll = $this->db->fetch_array($query);
  1198. $separator = '||~|~||';
  1199. $poll['optionsarray'] = explode($separator, $poll['options']);
  1200. $poll['votesarray'] = explode($separator, $poll['votes']);
  1201. /**
  1202. * At this point we are doing another query, so it is easier
  1203. * Little Todo: Include an INNER JOIN in the initial Poll-fetching query to save one query
  1204. * YOu have to make sure that columns of "thread" won't override columns of "poll"
  1205. * Therefore the solution right now at hand will be sufficient, until people start to moan :)
  1206. */
  1207. $poll['thread'] = $this->getThread($poll['tid']);
  1208. $poll['whovoted'] = $this->getWhoVoted($poll_id);
  1209. return $poll;
  1210. }
  1211. /**
  1212. * Returns post data of specified post
  1213. * Refers to: inc/functions.php & inc/class_parser.php
  1214. *
  1215. * @param integer $post_id Post ID to fetch data from
  1216. * @param boolean $parsed Shall the Post message be parsed?
  1217. * @param array $parse_options Array of yes/no options - allow_html,filter_badwords,allow_mycode,allow_smilies,nl2br,me_username
  1218. * @param array $override_forum_parse_options Whether parse options should be defined by forum or by the script.
  1219. If they are being overridden, the array will contain the options
  1220. * @return array|boolean: If unsuccessful, it returns false - Otherwise the Database row
  1221. */
  1222. function getPost($post_id, $parsed = false, $override_forum_parse_options = array())
  1223. {
  1224. if ($post_id == 0)
  1225. {
  1226. $this->_errorAndDie('Specified post ID cannot be 0!');
  1227. }
  1228. // Get the Post data
  1229. $post = get_post($post_id);
  1230. // Post not found? --> False
  1231. if (empty($post))
  1232. {
  1233. return false;
  1234. }
  1235. // Do we have permission?
  1236. $forumpermissions = forum_permissions($post['fid']);
  1237. if ($forumpermissions['canview'] != 1 || $forumpermissions['canviewthreads'] != 1)
  1238. {
  1239. // error_no_permission();
  1240. return false;
  1241. }
  1242. // If the post shall not be parsed, we can already return it at this point
  1243. if ($parsed == false || empty($post))
  1244. {
  1245. return $post;
  1246. }
  1247. // So we want to parse the message
  1248. /**
  1249. * We don't want to override the parse options defined by the forum,
  1250. * so we have first to get these options defined for the forum
  1251. */
  1252. if (count($override_forum_parse_options) == 0)
  1253. {
  1254. // Get the Forum data according to the forum id stored with the post
  1255. $forum = $this->getForum($post['fid']);
  1256. // Set up the parser options.
  1257. $parser_options = array(
  1258. "allow_html" => $forum['allowhtml'],
  1259. "allow_mycode" => $forum['allowmycode'],
  1260. "allow_smilies" => $forum['allowsmilies'],
  1261. "allow_imgcode" => $forum['allowimgcode'],
  1262. "filter_badwords" => 1
  1263. );
  1264. }
  1265. else
  1266. {
  1267. // Self-defined options given in the function parameter
  1268. $parser_options = array(
  1269. 'allow_html' => (isset($override_forum_parse_options['allow_html']) && $override_forum_parse_options['allow_html'] == 1) ? 1 : 0,
  1270. 'allow_mycode' => (isset($override_forum_parse_options['allow_mycode']) && $override_forum_parse_options['allow_mycode'] == 1) ? 1 : 0,
  1271. 'allow_smilies' => (isset($override_forum_parse_options['allow_smilies']) && $override_forum_parse_options['allow_smilies'] == 1) ? 1 : 0,
  1272. 'allow_imgcode' => (isset($override_forum_parse_options['allow_imgcode']) && $override_forum_parse_options['allow_imgcode'] == 1) ? 1 : 0,
  1273. 'filter_badwords' => (isset($override_forum_parse_options['filter_badwords']) && $override_forum_parse_options['filter_badwords'] == 1) ? 1 : 0,
  1274. );
  1275. }
  1276. // Overwrite the message with the parsed message
  1277. $post['message'] = $this->parser->parse_message($post['message'], $parser_options);
  1278. return $post;
  1279. }
  1280. /**
  1281. * Get posts which match the given criteria
  1282. *
  1283. * @param array $params Parameters for the query
  1284. * @return array
  1285. */
  1286. function getPosts($params = array('fields' => '*', 'order_by' => 'dateline', 'order_dir' => 'DESC', 'limit_start' => 0, 'limit' => 0, 'where' => ''))
  1287. {
  1288. // We will store the posts in here
  1289. $posts = array();
  1290. // No matter what parameters will be given, the query starts with the following
  1291. $sql = 'SELECT '.$params['fields'].'
  1292. FROM '.TABLE_PREFIX.'posts';
  1293. // Get all posts or just (hopefully) posts which match certain criteria?
  1294. $sql .= ($params['where'] != '') ? ' WHERE '.$params['where'] : '';
  1295. // Are the posts going to be ordered by a field?
  1296. if ($params['order_by'] != '')
  1297. {
  1298. $sql .= ' ORDER BY '.$params['order_by'];
  1299. if ($params['order_dir'] != '')
  1300. {
  1301. $sql .= ' '.$params['order_dir'];
  1302. }
  1303. else
  1304. {
  1305. $sql .= ' ASC';
  1306. }
  1307. }
  1308. // Get all posts or (hopefully) just a few?
  1309. if ($params['limit'] != 0)
  1310. {
  1311. $sql .= ' LIMIT ';
  1312. if (isset($params['limit_start']))
  1313. {
  1314. $sql .= $params['limit_start'].', '.$params['limit'];
  1315. }
  1316. else
  1317. {
  1318. $sql .= $params['limit'];
  1319. }
  1320. }
  1321. // Run the query
  1322. $query = $this->db->query($sql);
  1323. // Store the returned data in the array we return
  1324. while ($post = $this->db->fetch_array($query))
  1325. {
  1326. $posts[] = $post;
  1327. }
  1328. return $posts;
  1329. }
  1330. /**
  1331. * Get the Posts of a particular thread
  1332. *
  1333. * @param integer $thread_id
  1334. * @param string $fields If you want to fetch certain fields, define them as a string here (separated by comma)
  1335. * @param array $options Options for the query [ array('limit_start', 'limit', 'orderby', 'order_dir') ]
  1336. * @return array
  1337. */
  1338. function getPostsOfThread($thread_id, $fields = '*', $options = array())
  1339. {
  1340. // This is what we will be returning
  1341. $arr = array();
  1342. $query_thread = $this->db->query('SELECT `fid` FROM '.TABLE_PREFIX.'threads WHERE `tid` = '.intval($thread_id).' LIMIT 1');
  1343. $thread_forumid = $this->db->fetch_field($query_thread, 'fid');
  1344. // Do we have permission?
  1345. $forumpermissions = forum_permissions($thread_forumid);
  1346. if ($forumpermissions['canview'] != 1 || $forumpermissions['canviewthreads'] != 1)
  1347. {
  1348. // error_no_permission();
  1349. return false;
  1350. }
  1351. // Let's request the posts from the database
  1352. $query = $this->db->simple_select('posts', $fields, '`tid` = '.intval($thread_id), $options);
  1353. // All we need to do now is to assign them to our returning array
  1354. while ($post = $this->db->fetch_array($query))
  1355. {
  1356. $arr[] = $post;
  1357. }
  1358. return $arr;
  1359. }
  1360. /**
  1361. * Read the messages from database of a user
  1362. *
  1363. * @param integer $user_id ID of user
  1364. * @param array $params Array with options for SQL Query (orderby, sort)
  1365. * @param boolean $translate_folders If the folders should be turned into readable format ŕ la "inbox"
  1366. * @return array
  1367. */
  1368. function getPrivateMessagesOfUser($user_id, $params = array('orderby' => 'pm.dateline', 'sort' => 'DESC'), $translate_folders = true)
  1369. {
  1370. /**
  1371. * This is what we will be returning
  1372. * Structure of the array to return:
  1373. * array(
  1374. * 'Inbox' => array( ... Messages ... )
  1375. * )
  1376. *
  1377. * 'Inbox' is the translated folder of folder #1
  1378. */
  1379. $arr = array();
  1380. // If we want to translate the folder names, we need to include the file which contains the translation function
  1381. if ($translate_folders == true)
  1382. {
  1383. include_once MYBB_ROOT.'inc/functions_user.php';
  1384. }
  1385. // Run the Query for Private Messages
  1386. $query = $this->db->query('
  1387. SELECT pm.*, fu.username AS fromusername, tu.username as tousername
  1388. FROM '.TABLE_PREFIX.'privatemessages pm
  1389. LEFT JOIN '.TABLE_PREFIX.'users fu ON (fu.uid=pm.fromid)
  1390. LEFT JOIN '.TABLE_PREFIX.'users tu ON (tu.uid=pm.toid)
  1391. WHERE pm.uid = '.intval($user_id).'
  1392. ORDER BY '.$params['orderby'].' '.$params['sort'].'
  1393. ');
  1394. // Do we have messages?
  1395. if ($this->db->num_rows($query) > 0)
  1396. {
  1397. // Uhh, let's iterate the messages!
  1398. while ($message = $this->db->fetch_array($query))
  1399. {
  1400. // If we translate the folder names, our array index will be the translated folder name
  1401. if ($translate_folders == true)
  1402. {
  1403. $arr[get_pm_folder_name($message['folder'])][] = $message;
  1404. }
  1405. // If we don't want translated folder names, our array index will be the folder number
  1406. else
  1407. {
  1408. $arr[$message['folder']][] = $message;
  1409. }
  1410. }
  1411. }
  1412. return $arr;
  1413. }
  1414. /**
  1415. * Returns data of a specified thread
  1416. * Refers to: inc/functions.php
  1417. *
  1418. * @param integer $thread_id ID of the thread to fetch data from
  1419. * @return array|boolean If unsuccessful, it returns false - Otherwise the Database row
  1420. */
  1421. function getThread($thread_id)
  1422. {
  1423. $thread = get_thread($thread_id);
  1424. // Do we have permission?
  1425. $forumpermissions = forum_permissions($thread['fid']);
  1426. if ($forumpermissions['canview'] != 1 || $forumpermissions['canviewthreads'] != 1)
  1427. {
  1428. // error_no_permission();
  1429. return false;
  1430. }
  1431. else
  1432. {
  1433. return $thread;
  1434. }
  1435. }
  1436. /**
  1437. * Get Threads of one or more forums
  1438. *
  1439. * @param integer $forum_id IDs of Forums to fetch threads from
  1440. * @param string $fields If you want to fetch certain fields, define a string with them
  1441. * @param string $where Additional WHERE constellation if needed
  1442. * @pararm array $query_params Parameters for the Query to run in the database
  1443. (order_by, order_dir, limit_start, limit [limit will only be acknowledged if both limit vars are defined])
  1444. * @param boolean $excluse_invisible Shall we get invisible threads too?
  1445. * @param boolean $join_forums Do we also want to get the forum information of where the threads are located?
  1446. * @param boolean $join_first_post Shall we get the first post of the thread? (= initial post)
  1447. * @return array
  1448. */
  1449. function getThreads($forum_id, $fields = '*', $where = '', $query_params = array('order_by' => 't.`subject`', 'order_dir' => 'ASC'), $exclude_invisible = true, $join_forums = false, $join_first_post = false)
  1450. {
  1451. // Do we have permission?
  1452. if (!is_array($forum_id))
  1453. {
  1454. $forumpermissions = forum_permissions($forum_id);
  1455. if ($forumpermissions['canview'] != 1 || $forumpermissions['canviewthreads'] != 1)
  1456. {
  1457. // error_no_permission();
  1458. return false;
  1459. }
  1460. }
  1461. else
  1462. {
  1463. // Check for every single forum
  1464. foreach ($forum_id as $forum_id_single)
  1465. {
  1466. $forumpermissions = forum_permissions($forum_id_single);
  1467. if ($forumpermissions['canview'] != 1 || $forumpermissions['canviewthreads'] != 1)
  1468. {
  1469. // error_no_perm…

Large files files are truncated, but you can click here to view the full file