PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/phpmyfaq/src/phpMyFAQ/Report.php

http://github.com/thorsten/phpMyFAQ
PHP | 152 lines | 98 code | 14 blank | 40 comment | 6 complexity | e507c976158790258b764b426af464aa MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /**
  3. * The reporting class for simple report generation.
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public License,
  6. * v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. * obtain one at http://mozilla.org/MPL/2.0/.
  8. *
  9. * @package phpMyFAQ
  10. * @author Thorsten Rinne <thorsten@phpmyfaq.de>
  11. * @author Gustavo Solt <gustavo.solt@mayflower.de>
  12. * @copyright 2011-2021 phpMyFAQ Team
  13. * @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
  14. * @link https://www.phpmyfaq.de
  15. * @since 2011-02-04
  16. */
  17. namespace phpMyFAQ;
  18. /**
  19. * Class Report
  20. *
  21. * @package phpMyFAQ
  22. */
  23. class Report
  24. {
  25. /**
  26. * @var Configuration
  27. */
  28. private $config;
  29. /**
  30. * Constructor.
  31. *
  32. * @param Configuration $config
  33. */
  34. public function __construct(Configuration $config)
  35. {
  36. $this->config = $config;
  37. }
  38. /**
  39. * Generates a huge array for the report.
  40. *
  41. * @return array<int, array<mixed>>
  42. */
  43. public function getReportingData(): array
  44. {
  45. $report = [];
  46. $query = sprintf(
  47. '
  48. SELECT
  49. fd.id AS id,
  50. fd.lang AS lang,
  51. fcr.category_id AS category_id,
  52. c.name as category_name,
  53. c.parent_id as parent_id,
  54. fd.sticky AS sticky,
  55. fd.thema AS question,
  56. fd.author AS original_author,
  57. fd.updated AS updated,
  58. fv.visits AS visits,
  59. u.display_name AS last_author
  60. FROM
  61. %sfaqdata fd
  62. LEFT JOIN
  63. %sfaqcategoryrelations fcr
  64. ON
  65. (fd.id = fcr.record_id AND fd.lang = fcr.record_lang)
  66. LEFT JOIN
  67. %sfaqvisits fv
  68. ON
  69. (fd.id = fv.id AND fd.lang = fv.lang)
  70. LEFT JOIN
  71. %sfaqchanges as fc
  72. ON
  73. (fd.id = fc.id AND fd.lang = fc.lang)
  74. LEFT JOIN
  75. %sfaquserdata as u
  76. ON
  77. (u.user_id = fc.usr)
  78. LEFT JOIN
  79. %sfaqcategories as c
  80. ON
  81. (c.id = fcr.category_id AND c.lang = fcr.record_lang)
  82. ORDER BY
  83. fd.id
  84. ASC',
  85. Database::getTablePrefix(),
  86. Database::getTablePrefix(),
  87. Database::getTablePrefix(),
  88. Database::getTablePrefix(),
  89. Database::getTablePrefix(),
  90. Database::getTablePrefix()
  91. );
  92. $result = $this->config->getDb()->query($query);
  93. $lastId = 0;
  94. while ($row = $this->config->getDb()->fetchObject($result)) {
  95. if ($row->id == $lastId) {
  96. $report[$row->id]['faq_translations'] += 1;
  97. } else {
  98. $report[$row->id] = [
  99. 'faq_id' => $row->id,
  100. 'faq_language' => $row->lang,
  101. 'category_id' => $row->category_id,
  102. 'category_parent' => $row->parent_id,
  103. 'category_name' => $row->category_name,
  104. 'faq_translations' => 0,
  105. 'faq_sticky' => $row->sticky,
  106. 'faq_question' => $row->question,
  107. 'faq_org_author' => $row->original_author,
  108. 'faq_updated' => Date::createIsoDate($row->updated),
  109. 'faq_visits' => $row->visits,
  110. 'faq_last_author' => $row->last_author,
  111. ];
  112. }
  113. $lastId = $row->id;
  114. }
  115. return $report;
  116. }
  117. /**
  118. * Convert string to the correct encoding and removes possible
  119. * bad strings to avoid formula injection attacks.
  120. *
  121. * @param string $outputString String to encode.
  122. * @return string Encoded string.
  123. */
  124. public function convertEncoding(string $outputString = ''): string
  125. {
  126. $outputString = html_entity_decode($outputString, ENT_QUOTES, 'utf-8');
  127. $outputString = str_replace(',', ' ', $outputString);
  128. if (extension_loaded('mbstring')) {
  129. $detected = mb_detect_encoding($outputString);
  130. if ($detected !== 'ASCII') {
  131. $outputString = mb_convert_encoding($outputString, 'UTF-16', $detected);
  132. }
  133. }
  134. $toBeRemoved = ['=', '+', '-', 'HYPERLINK'];
  135. $outputString = str_replace($toBeRemoved, '', $outputString);
  136. return $outputString;
  137. }
  138. }