/lib/content.inc.php

https://gitlab.com/mucill/sman7 · PHP · 136 lines · 84 code · 18 blank · 34 comment · 14 complexity · 080bafaf970f9c74d2e0feb75e877c58 MD5 · raw file

  1. <?php
  2. /**
  3. * Content class
  4. * Class for showing content from database
  5. *
  6. * Copyright (C) 2007,2008 Arie Nugraha (dicarve@yahoo.com), Hendro Wicaksono (hendrowicaksono@yahoo.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. */
  23. // be sure that this file not accessed directly
  24. if (!defined('INDEX_AUTH')) {
  25. die("can not access this file directly");
  26. } elseif (INDEX_AUTH != 1) {
  27. die("can not access this file directly");
  28. }
  29. class Content
  30. {
  31. public $strip_html = false;
  32. public $allowed_tags = null;
  33. public static function createSummary($text, $max_chars = 300)
  34. {
  35. $summary = strip_tags($text);
  36. // $summary = substr($summary, 0, $max_chars);
  37. // making sure substr finishes on a word
  38. if (preg_match('/^.{1,'.$max_chars.'}\b/s', $summary, $match)) {
  39. $summary= $match[0];
  40. }
  41. return $summary;
  42. }
  43. public function getContents($obj_db, $max_each_page = 10, &$total, $search_query = '')
  44. {
  45. global $sysconf;
  46. $contents = array();
  47. $page = 1;
  48. $offset = 0;
  49. if (isset($_GET['page'])) {
  50. $page = (integer)$_GET['page'];
  51. }
  52. if ($page > 1) {
  53. $offset = ($page*$max_each_page)-$max_each_page;
  54. }
  55. // language
  56. $_lang = strtolower($sysconf['default_lang']);
  57. // query content
  58. $_sql_content = "SELECT SQL_CALC_FOUND_ROWS * FROM content WHERE is_news=1";
  59. if ($search_query) {
  60. $search_query = $obj_db->escape_string(trim($search_query));
  61. $_sql_content .= " AND MATCH(`content_title`, `content_desc`) AGAINST('$search_query' IN BOOLEAN MODE)";
  62. }
  63. $_sql_content .= " ORDER BY `last_update` DESC";
  64. $_sql_content .= " LIMIT $max_each_page OFFSET $offset";
  65. $_content_q = $obj_db->query($_sql_content);
  66. // echo $_sql_content;
  67. // get total rows
  68. $_total_rows = $obj_db->query('SELECT FOUND_ROWS()');
  69. $_total_rows_d = $_total_rows->fetch_row();
  70. $total = $_total_rows_d[0];
  71. // get content data
  72. while ($_content_d = $_content_q->fetch_assoc()) {
  73. $contents[] = $_content_d;
  74. }
  75. return $contents;
  76. }
  77. public function get($obj_db, $str_path = '')
  78. {
  79. global $sysconf;
  80. $_path = strtolower(trim($str_path));
  81. if (!$_path) {
  82. return;
  83. }
  84. if (preg_match('@^admin.+@i', $_path)) {
  85. $_unauthorized = !isset($_SESSION['uid']) AND !isset($_SESSION['uname']) AND !isset($_SESSION['realname']);
  86. if ($_unauthorized) {
  87. return;
  88. }
  89. }
  90. // language
  91. $_lang = strtolower($sysconf['default_lang']);
  92. $_path_lang = $_path.'_'.$_lang;
  93. // check for language
  94. $_sql_check = sprintf('SELECT COUNT(*) FROM content WHERE content_path=\'%s\'', $obj_db->escape_string($_path_lang));
  95. $_check_q = $obj_db->query($_sql_check);
  96. $_check_d = $_check_q->fetch_row();
  97. if ($_check_d[0] > 0) {
  98. $_path = $_path_lang;
  99. }
  100. // query content
  101. $_sql_content = sprintf('SELECT * FROM content WHERE content_path=\'%s\'', $obj_db->escape_string($_path));
  102. $_content_q = $obj_db->query($_sql_content);
  103. // get content data
  104. $_content_d = $_content_q->fetch_assoc();
  105. if (!$_content_d['content_title'] OR !$_content_d['content_path']) {
  106. return false;
  107. } else {
  108. $_content['Title'] = $_content_d['content_title'];
  109. $_content['Path'] = $_content_d['content_path'];
  110. $_content['Content'] = $_content_d['content_desc'];
  111. // strip html
  112. if ($this->strip_html) {
  113. $_content['Content'] = strip_tags($_content['Content'], $this->allowed_tags);
  114. }
  115. return $_content;
  116. }
  117. }
  118. }