PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/app/plugins/forum/controllers/components/toolbar.php

https://github.com/robksawyer/FIND---GET---MAKE
PHP | 350 lines | 193 code | 51 blank | 106 comment | 52 complexity | f218937718117b46f8fca5598946f024 MD5 | raw file
  1. <?php
  2. /**
  3. * Cupcake - Toolbar Component
  4. *
  5. * @author Miles Johnson - www.milesj.me
  6. * @copyright Copyright 2006-2009, Miles Johnson, Inc.
  7. * @license http://www.opensource.org/licenses/mit-license.php - Licensed under The MIT License
  8. * @link www.milesj.me/resources/script/forum-plugin
  9. */
  10. class ToolbarComponent extends Object {
  11. /**
  12. * Components.
  13. *
  14. * @access public
  15. * @var array
  16. */
  17. public $components = array('Session');
  18. /**
  19. * Initialize.
  20. *
  21. * @access public
  22. * @param obj $Controller
  23. * @param array $settings
  24. * @return void
  25. */
  26. public function initialize(&$Controller, $settings = array()) {
  27. $this->Controller = $Controller;
  28. $this->settings = ForumConfig::getInstance()->settings;
  29. $this->columnMap = ForumConfig::getInstance()->columnMap;
  30. }
  31. /**
  32. * Initialize the session and all data.
  33. *
  34. * @access public
  35. * @return void
  36. */
  37. public function initForum() {
  38. if (!$this->Session->check('Forum.isBrowsing')) {
  39. $user_id = $this->Controller->Auth->user('id');
  40. // How much access we have?
  41. if (!$this->Session->check('Forum.access')) {
  42. $access = array('Guest' => 0);
  43. if ($user_id) {
  44. $access['Member'] = 1;
  45. $access = array_merge($access, ClassRegistry::init('Forum.Access')->getMyAccess($user_id));
  46. }
  47. $this->Session->write('Forum.access', $access);
  48. }
  49. // Save last visit time
  50. if (!$this->Session->check('Forum.lastVisit')) {
  51. $lastVisit = ($user_id) ? $this->Controller->Auth->user($this->columnMap['lastLogin']) : date('Y-m-d H:i:s');
  52. $this->Session->write('Forum.lastVisit', $lastVisit);
  53. }
  54. // Moderator?
  55. if (!$this->Session->check('Forum.moderates')) {
  56. $moderates = ($user_id) ? ClassRegistry::init('Forum.Moderator')->getModerations($user_id) : array();
  57. $this->Session->write('Forum.moderates', $moderates);
  58. }
  59. // Are we a super mod?
  60. if (!$this->Session->check('Forum.isSuperMod')) {
  61. $status = ($user_id) ? ClassRegistry::init('Forum.Access')->isSuper($user_id) : 0;
  62. $this->Session->write('Forum.isSuperMod', $status);
  63. }
  64. // Are we an administrator?
  65. if (!$this->Session->check('Forum.isAdmin')) {
  66. $status = ($user_id) ? ClassRegistry::init('Forum.Access')->isAdmin($user_id) : 0;
  67. $this->Session->write('Forum.isAdmin', $status);
  68. }
  69. $this->Session->write('Forum.isBrowsing', true);
  70. }
  71. }
  72. /**
  73. * Calculates the page to redirect to.
  74. *
  75. * @access public
  76. * @param int $topic_id
  77. * @param int $post_id
  78. * @param boolean $return
  79. * @return mixed
  80. */
  81. public function goToPage($topic_id = NULL, $post_id = NULL, $return = false) {
  82. $topic = ClassRegistry::init('Forum.Topic')->find('first', array(
  83. 'conditions' => array('Topic.id' => $topic_id),
  84. 'fields' => array('Topic.slug')
  85. ));
  86. $slug = (!empty($topic['Topic']['slug']) ? $topic['Topic']['slug'] : null);
  87. // Certain page
  88. if ($topic_id && $post_id) {
  89. $posts = ClassRegistry::init('Forum.Post')->find('list', array(
  90. 'conditions' => array('Post.topic_id' => $topic_id),
  91. 'order' => array('Post.id' => 'ASC')
  92. ));
  93. $totalPosts = count($posts);
  94. $perPage = $this->settings['posts_per_page'];
  95. if ($totalPosts > $perPage) {
  96. $totalPages = ceil($totalPosts / $perPage);
  97. } else {
  98. $totalPages = 1;
  99. }
  100. if ($totalPages <= 1) {
  101. $url = array('plugin' => 'forum', 'controller' => 'topics', 'action' => 'view', $slug, '#' => 'post_'. $post_id);
  102. } else {
  103. $posts = array_values($posts);
  104. $flips = array_flip($posts);
  105. $position = $flips[$post_id] + 1;
  106. $goTo = ceil($position / $perPage);
  107. $url = array('plugin' => 'forum', 'controller' => 'topics', 'action' => 'view', $slug, 'page' => $goTo, '#' => 'post_'. $post_id);
  108. }
  109. // First post
  110. } else if ($topic_id && !$post_id) {
  111. $url = array('plugin' => 'forum', 'controller' => 'topics', 'action' => 'view', $slug);
  112. // None
  113. } else {
  114. $url = $this->Controller->referer();
  115. if ((empty($url)) || (strpos($url, 'delete') !== false)) {
  116. $url = array('plugin' => 'forum', 'controller' => 'home', 'action' => 'index');
  117. }
  118. }
  119. if ($return === true) {
  120. return $url;
  121. } else {
  122. $this->Controller->redirect($url);
  123. }
  124. }
  125. /**
  126. * Gets the highest access level.
  127. *
  128. * @access public
  129. * @return int
  130. */
  131. public function getAccess() {
  132. $access = $this->Session->read('Forum.access');
  133. $level = 0;
  134. if (!empty($access)) {
  135. foreach ($access as $no) {
  136. if ($no > $level) {
  137. $level = $no;
  138. }
  139. }
  140. }
  141. return $level;
  142. }
  143. /**
  144. * Simply marks a topic as read.
  145. *
  146. * @access public
  147. * @param int $topic_id
  148. * @return void
  149. */
  150. public function markAsRead($topic_id) {
  151. $readTopics = $this->Session->read('Forum.readTopics');
  152. if (is_array($readTopics) && !empty($readTopics)) {
  153. $readTopics[] = $topic_id;
  154. $readTopics = array_unique($readTopics);
  155. $this->Session->write('Forum.readTopics', $readTopics);
  156. } else {
  157. $this->Session->write('Forum.readTopics', array($topic_id));
  158. }
  159. return true;
  160. }
  161. /**
  162. * Builds the page title.
  163. *
  164. * @access public
  165. * @param array $args
  166. * @return string
  167. */
  168. public function pageTitle() {
  169. $args = func_get_args();
  170. array_unshift($args, __d('forum', 'Forum', true));
  171. $this->Controller->set('title_for_layout', implode(' &raquo; ', $args));
  172. }
  173. /**
  174. * Method for reseting a password.
  175. *
  176. * @access public
  177. * @param array $user
  178. * @param boolean $reset
  179. * @return void
  180. */
  181. public function resetPassword($user, $reset = false) {
  182. $User = ClassRegistry::init('Forum.User');
  183. $password = $User->generate();
  184. $User->resetPassword($user['User']['id'], $this->Controller->Auth->password($password));
  185. // Send email
  186. if (!$reset) {
  187. $message = sprintf(__d('forum', 'You have requested the login credentials for %s, your information is listed below', true), $this->settings['site_name']) .":\n\n";
  188. $subject = __d('forum', 'Forgotten Password', true);
  189. } else {
  190. $message = sprintf(__d('forum', 'Your password has been reset for %s, your information is listed below', true), $this->settings['site_name']) .":\n\n";
  191. $subject = __d('forum', 'Reset Password', true);
  192. }
  193. $message .= __d('forum', 'Username', true) .": ". $user['User']['username'] ."\n";
  194. $message .= __d('forum', 'Password', true) .": ". $password ."\n\n";
  195. $message .= __d('forum', 'Please change your password once logging in.', true);
  196. $this->Controller->Email->to = $user['User']['username'] .' <'. $user['User']['email'] .'>';
  197. $this->Controller->Email->from = $this->settings['site_name'] .' <'. $this->settings['site_email'] .'>';
  198. $this->Controller->Email->subject = $this->settings['site_name'] .' - '. $subject;
  199. $this->Controller->Email->send($message);
  200. }
  201. /**
  202. * Updates the session topics array.
  203. *
  204. * @access public
  205. * @param int $topic_id
  206. * @return void
  207. */
  208. public function updateTopics($topic_id) {
  209. $topics = $this->Session->read('Forum.topics');
  210. if (!empty($topic_id)) {
  211. if (is_array($topics)) {
  212. $topics[$topic_id] = time();
  213. } else {
  214. $topics = array($topic_id => time());
  215. }
  216. $this->Session->write('Forum.topics', $topics);
  217. }
  218. }
  219. /**
  220. * Updates the session posts array.
  221. *
  222. * @access public
  223. * @param int $post_id
  224. * @return void
  225. */
  226. public function updatePosts($post_id) {
  227. $posts = $this->Session->read('Forum.posts');
  228. if (!empty($post_id)) {
  229. if (is_array($posts)) {
  230. $posts[$post_id] = time();
  231. } else {
  232. $posts = array($post_id => time());
  233. }
  234. $this->Session->write('Forum.posts', $posts);
  235. }
  236. }
  237. /**
  238. * Do we have access to commit this action.
  239. *
  240. * @access public
  241. * @param array $validators
  242. * @return boolean
  243. */
  244. public function verifyAccess($validators = array()) {
  245. $user_id = $this->Controller->Auth->user('id');
  246. // Does the data exist?
  247. if (isset($validators['exists'])) {
  248. if (empty($validators['exists'])) {
  249. $this->goToPage();
  250. }
  251. }
  252. // Are we a moderator? Grant access
  253. if (isset($validators['moderate'])) {
  254. if (in_array($validators['moderate'], $this->Session->read('Forum.moderates'))) {
  255. return true;
  256. }
  257. }
  258. // Do we have permission to do this action?
  259. if (isset($validators['permission'])) {
  260. if ($this->getAccess() < $validators['permission']) {
  261. $this->goToPage();
  262. }
  263. }
  264. // Is the item locked/unavailable?
  265. if (isset($validators['status'])) {
  266. if ($validators['status'] > 0) {
  267. $this->goToPage();
  268. }
  269. }
  270. // Does the user own this item?
  271. if (isset($validators['ownership'])) {
  272. if (($this->Session->read('Forum.isSuperMod') >= 1) || ($this->Session->read('Forum.isAdmin') >= 1)) {
  273. return true;
  274. } else if ($user_id != $validators['ownership']) {
  275. $this->goToPage();
  276. }
  277. }
  278. return true;
  279. }
  280. /**
  281. * Double check access levels in session and db and permit.
  282. *
  283. * @access public
  284. * @return boolean
  285. */
  286. public function verifyAdmin() {
  287. $user_id = $this->Controller->Auth->user('id');
  288. if ($user_id) {
  289. if ($this->Session->read('Forum.isAdmin') >= 1) {
  290. return true;
  291. } else {
  292. $this->goToPage();
  293. }
  294. } else {
  295. $this->Controller->redirect(array('plugin' => 'forum', 'controller' => 'users', 'action' => 'login', 'admin' => false));
  296. }
  297. return false;
  298. }
  299. }