Guest User

Untitled

a guest
Jul 23rd, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.63 KB | None | 0 0
  1. /**
  2. * @package Joomla.Site
  3. * @subpackage com_content
  4. *
  5. * @copyright Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
  6. * @license GNU General Public License version 2 or later; see LICENSE.txt
  7. */
  8.  
  9. defined('_JEXEC') or die;
  10.  
  11. use JoomlaRegistryRegistry;
  12.  
  13. /**
  14. * Content Component Article Model
  15. *
  16. * @since 1.5
  17. */
  18. class ContentModelArticle extends JModelItem
  19. {
  20. /**
  21. * Model context string.
  22. *
  23. * @var string
  24. */
  25. protected $_context = 'com_content.article';
  26.  
  27. /**
  28. * Method to auto-populate the model state.
  29. *
  30. * Note. Calling getState in this method will result in recursion.
  31. *
  32. * @since 1.6
  33. *
  34. * @return void
  35. */
  36. protected function populateState()
  37. {
  38. $app = JFactory::getApplication('site');
  39.  
  40. // Load state from the request.
  41. $pk = $app->input->getInt('id');
  42. $this->setState('article.id', $pk);
  43.  
  44. $offset = $app->input->getUInt('limitstart');
  45. $this->setState('list.offset', $offset);
  46.  
  47. // Load the parameters.
  48. $params = $app->getParams();
  49. $this->setState('params', $params);
  50.  
  51. // TODO: Tune these values based on other permissions.
  52. $user = JFactory::getUser();
  53.  
  54. if ((!$user->authorise('core.edit.state', 'com_content')) && (!$user->authorise('core.edit', 'com_content')))
  55. {
  56. $this->setState('filter.published', 1);
  57. $this->setState('filter.archived', 2);
  58. }
  59.  
  60. $this->setState('filter.language', JLanguageMultilang::isEnabled());
  61. }
  62.  
  63. /**
  64. * Method to get article data.
  65. *
  66. * @param integer $pk The id of the article.
  67. *
  68. * @return object|boolean|JException Menu item data object on success, boolean false or JException instance on error
  69. */
  70. public function getItem($pk = null)
  71. {
  72. $user = JFactory::getUser();
  73.  
  74. $pk = (!empty($pk)) ? $pk : (int) $this->getState('article.id');
  75.  
  76. if ($this->_item === null)
  77. {
  78. $this->_item = array();
  79. }
  80.  
  81. if (!isset($this->_item[$pk]))
  82. {
  83. try
  84. {
  85. $db = $this->getDbo();
  86. $query = $db->getQuery(true)
  87. ->select(
  88. $this->getState(
  89. 'item.select', 'a.id, a.asset_id, a.title, a.alias, a.introtext, a.fulltext, ' .
  90. // If badcats is not null, this means that the article is inside an unpublished category
  91. // In this case, the state is set to 0 to indicate Unpublished (even if the article state is Published)
  92. 'CASE WHEN badcats.id is null THEN a.state ELSE 0 END AS state, ' .
  93. 'a.catid, a.created, a.created_by, a.created_by_alias, ' .
  94. // Use created if modified is 0
  95. 'CASE WHEN a.modified = ' . $db->quote($db->getNullDate()) . ' THEN a.created ELSE a.modified END as modified, ' .
  96. 'a.modified_by, a.checked_out, a.checked_out_time, a.publish_up, a.publish_down, ' .
  97. 'a.images, a.urls, a.attribs, a.version, a.ordering, ' .
  98. 'a.metakey, a.metadesc, a.access, a.hits, a.metadata, a.featured, a.language, a.xreference'
  99. )
  100. );
  101. $query->from('#__content AS a');
  102.  
  103. // Join on category table.
  104. $query->select('c.title AS category_title, c.alias AS category_alias, c.access AS category_access')
  105. ->join('LEFT', '#__categories AS c on c.id = a.catid');
  106.  
  107. // Join on user table.
  108. $query->select('u.name AS author')
  109. ->join('LEFT', '#__users AS u on u.id = a.created_by');
  110.  
  111. // Filter by language
  112. if ($this->getState('filter.language'))
  113. {
  114. $query->where('a.language in (' . $db->quote(JFactory::getLanguage()->getTag()) . ',' . $db->quote('*') . ')');
  115. }
  116.  
  117. // Join over the categories to get parent category titles
  118. $query->select('parent.title as parent_title, parent.id as parent_id, parent.path as parent_route, parent.alias as parent_alias')
  119. ->join('LEFT', '#__categories as parent ON parent.id = c.parent_id');
  120.  
  121. // Join on voting table
  122. $query->select('ROUND(v.rating_sum / v.rating_count, 0) AS rating, v.rating_count as rating_count')
  123. ->join('LEFT', '#__content_rating AS v ON a.id = v.content_id')
  124.  
  125. ->where('a.id = ' . (int) $pk);
  126.  
  127. if ((!$user->authorise('core.edit.state', 'com_content')) && (!$user->authorise('core.edit', 'com_content')))
  128. {
  129. // Filter by start and end dates.
  130. $nullDate = $db->quote($db->getNullDate());
  131. $date = JFactory::getDate();
  132.  
  133. $nowDate = $db->quote($date->toSql());
  134.  
  135. $query->where('(a.publish_up = ' . $nullDate . ' OR a.publish_up <= ' . $nowDate . ')')
  136. ->where('(a.publish_down = ' . $nullDate . ' OR a.publish_down >= ' . $nowDate . ')');
  137. }
  138.  
  139. // Join to check for category published state in parent categories up the tree
  140. // If all categories are published, badcats.id will be null, and we just use the article state
  141. $subquery = ' (SELECT cat.id as id FROM #__categories AS cat JOIN #__categories AS parent ';
  142. $subquery .= 'ON cat.lft BETWEEN parent.lft AND parent.rgt ';
  143. $subquery .= 'WHERE parent.extension = ' . $db->quote('com_content');
  144. $subquery .= ' AND parent.published <= 0 GROUP BY cat.id)';
  145. $query->join('LEFT OUTER', $subquery . ' AS badcats ON badcats.id = c.id');
  146.  
  147. // Filter by published state.
  148. $published = $this->getState('filter.published');
  149. $archived = $this->getState('filter.archived');
  150.  
  151. if (is_numeric($published))
  152. {
  153. $query->where('(a.state = ' . (int) $published . ' OR a.state =' . (int) $archived . ')');
  154. }
  155.  
  156. $db->setQuery($query);
  157.  
  158. $data = $db->loadObject();
  159.  
  160. if (empty($data))
  161. {
  162. return JError::raiseError(404, JText::_('COM_CONTENT_ERROR_ARTICLE_NOT_FOUND'));
  163. }
  164.  
  165. // Check for published state if filter set.
  166. if (((is_numeric($published)) || (is_numeric($archived))) && (($data->state != $published) && ($data->state != $archived)))
  167. {
  168. return JError::raiseError(404, JText::_('COM_CONTENT_ERROR_ARTICLE_NOT_FOUND'));
  169. }
  170.  
  171. // Convert parameter fields to objects.
  172. $registry = new Registry;
  173. $registry->loadString($data->attribs);
  174.  
  175. $data->params = clone $this->getState('params');
  176. $data->params->merge($registry);
  177.  
  178. $registry = new Registry;
  179. $registry->loadString($data->metadata);
  180. $data->metadata = $registry;
  181.  
  182. // Technically guest could edit an article, but lets not check that to improve performance a little.
  183. if (!$user->get('guest'))
  184. {
  185. $userId = $user->get('id');
  186.  
  187. $asset = 'com_content.article.' . $data->id;
  188.  
  189. // Check general edit permission first.
  190. if ($user->authorise('core.edit', $asset))
  191. {
  192. $data->params->set('access-edit', true);
  193. }
  194.  
  195. // Now check if edit.own is available.
  196. elseif (!empty($userId) && $user->authorise('core.edit.own', $asset))
  197. {
  198. // Check for a valid user and that they are the owner.
  199. if ($userId == $data->created_by)
  200. {
  201. $data->params->set('access-edit', true);
  202. }
  203. }
  204. }
  205.  
  206. // Compute view access permissions.
  207. if ($access = $this->getState('filter.access'))
  208. {
  209. // If the access filter has been set, we already know this user can view.
  210. $data->params->set('access-view', true);
  211. }
  212. else
  213. {
  214. // If no access filter is set, the layout takes some responsibility for display of limited information.
  215. $user = JFactory::getUser();
  216. $groups = $user->getAuthorisedViewLevels();
  217.  
  218. if ($data->catid == 0 || $data->category_access === null)
  219. {
  220. $data->params->set('access-view', in_array($data->access, $groups));
  221. }
  222. else
  223. {
  224. $data->params->set('access-view', in_array($data->access, $groups) && in_array($data->category_access, $groups));
  225. }
  226. }
  227.  
  228. $this->_item[$pk] = $data;
  229. }
  230. catch (Exception $e)
  231. {
  232. if ($e->getCode() == 404)
  233. {
  234. // Need to go thru the error handler to allow Redirect to work.
  235. JError::raiseError(404, $e->getMessage());
  236. }
  237. else
  238. {
  239. $this->setError($e);
  240. $this->_item[$pk] = false;
  241. }
  242. }
  243. }
  244.  
  245. return $this->_item[$pk];
  246. }
  247.  
  248. /**
  249. * Increment the hit counter for the article.
  250. *
  251. * @param integer $pk Optional primary key of the article to increment.
  252. *
  253. * @return boolean True if successful; false otherwise and internal error set.
  254. */
  255. public function hit($pk = 0)
  256. {
  257. $input = JFactory::getApplication()->input;
  258. $hitcount = $input->getInt('hitcount', 1);
  259.  
  260. if ($hitcount)
  261. {
  262. $pk = (!empty($pk)) ? $pk : (int) $this->getState('article.id');
  263.  
  264. $table = JTable::getInstance('Content', 'JTable');
  265. $table->load($pk);
  266. $table->hit($pk);
  267. }
  268.  
  269. return true;
  270. }
  271.  
  272. /**
  273. * Save user vote on article
  274. *
  275. * @param integer $pk Joomla Article Id
  276. * @param integer $rate Voting rate
  277. *
  278. * @return boolean Return true on success
  279. */
  280. public function storeVote($pk = 0, $rate = 0)
  281. {
  282. if ($rate >= 1 && $rate <= 10 && $pk > 0)
  283. {
  284. $userIP = $_SERVER['REMOTE_ADDR'];
  285.  
  286. // Initialize variables.
  287. $db = $this->getDbo();
  288. $query = $db->getQuery(true);
  289.  
  290. // Create the base select statement.
  291. $query->select('*')
  292. ->from($db->quoteName('#__content_rating'))
  293. ->where($db->quoteName('content_id') . ' = ' . (int) $pk);
  294.  
  295. // Set the query and load the result.
  296. $db->setQuery($query);
  297.  
  298. // Check for a database error.
  299. try
  300. {
  301. $rating = $db->loadObject();
  302. }
  303. catch (RuntimeException $e)
  304. {
  305. JError::raiseWarning(500, $e->getMessage());
  306.  
  307. return false;
  308. }
  309.  
  310. // There are no ratings yet, so lets insert our rating
  311.  
  312. if (!$rating)
  313. {
  314. $query = $db->getQuery(true);
  315.  
  316. // Create the base insert statement.
  317. $query->insert($db->quoteName('#__content_rating'))
  318. ->columns(array($db->quoteName('content_id'), $db->quoteName('lastip'), $db->quoteName('rating_sum'), $db->quoteName('rating_count')))
  319. ->values((int) $pk . ', ' . $db->quote($userIP) . ',' . (int) $rate . ', 1');
  320.  
  321. // Set the query and execute the insert.
  322. $db->setQuery($query);
  323.  
  324. try
  325. {
  326. $db->execute();
  327. }
  328. catch (RuntimeException $e)
  329. {
  330. JError::raiseWarning(500, $e->getMessage());
  331.  
  332. return false;
  333. }
  334. }
  335. else
  336. {
  337. if ($userIP != ($rating->lastip))
  338. {
  339. $query = $db->getQuery(true);
  340.  
  341. // Create the base update statement.
  342. $query->update($db->quoteName('#__content_rating'))
  343. ->set($db->quoteName('rating_count') . ' = rating_count + 1')
  344. ->set($db->quoteName('rating_sum') . ' = rating_sum + ' . (int) $rate)
  345. ->set($db->quoteName('lastip') . ' = ' . $db->quote($userIP))
  346. ->where($db->quoteName('content_id') . ' = ' . (int) $pk);
  347.  
  348. // Set the query and execute the update.
  349. $db->setQuery($query);
  350.  
  351. try
  352. {
  353. $db->execute();
  354. }
  355. catch (RuntimeException $e)
  356. {
  357. JError::raiseWarning(500, $e->getMessage());
  358.  
  359. return false;
  360. }
  361. }
  362. else
  363. {
  364. return false;
  365. }
  366. }
  367.  
  368. return true;
  369. }
  370.  
  371. JError::raiseWarning('SOME_ERROR_CODE', JText::sprintf('COM_CONTENT_INVALID_RATING', $rate), "JModelArticle::storeVote($rate)");
  372.  
  373. return false;
  374.  
  375. }
  376.  
  377. if ($userIP != ($rating->lastip))
  378.  
  379. if ($userIP != ($rating->userId)
Add Comment
Please, Sign In to add comment