logman.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. <?php
  2. /*
  3. * phpMeccano v0.2.0. Web-framework written with php programming language. Core module [logman.php].
  4. * Copyright (C) 2015-2019 Alexei Muzarov
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * e-mail: azexmail@gmail.com
  21. * e-mail: azexmail@mail.ru
  22. * https://bitbucket.org/azexmail/phpmeccano
  23. */
  24. namespace core;
  25. require_once MECCANO_CORE_DIR.'/extclass.php';
  26. interface intLogMan {
  27. function __construct(\mysqli $dbLink);
  28. public function installEvents(\DOMDocument $events, $validate = true);
  29. public function delLogEvents($plugin); // old name [delEvents]
  30. public function clearLog();
  31. public function sumLogAllPlugins($rpp = 20);
  32. public function getPageAllPlugins($pageNumber, $totalPages, $rpp = 20, $code = MECCANO_DEF_LANG, $orderBy = ['id'], $ascent = false);
  33. public function sumLogByPlugin($plugin, $rpp = 20);
  34. public function getPageByPlugin($plugin, $pageNumber, $totalPages, $rpp = 20, $code = MECCANO_DEF_LANG, $orderBy = ['id'], $ascent = false);
  35. public function getLogAllPlugins($code = MECCANO_DEF_LANG, $orderBy = ['id'], $ascent = false);
  36. public function getLogByPlugin($plugin, $code = MECCANO_DEF_LANG, $orderBy = ['id'], $ascent = false);
  37. }
  38. class LogMan extends ServiceMethods implements intLogMan {
  39. public function __construct(\mysqli $dbLink) {
  40. $this->dbLink = $dbLink;
  41. }
  42. public function installEvents(\DOMDocument $events, $validate = true) {
  43. $this->zeroizeError();
  44. if ($validate && !@$events->relaxNGValidate(MECCANO_CORE_DIR.'/validation-schemas/logman-events-v01.rng')) {
  45. $this->setError(ERROR_INCORRECT_DATA, 'installEvents: incorrect structure of the events');
  46. return false;
  47. }
  48. $pluginName = $events->getElementsByTagName('log')->item(0)->getAttribute("plugin");
  49. // check whether plugin is installed
  50. $qPlugin = $this->dbLink->query("SELECT `id` "
  51. . "FROM `".MECCANO_TPREF."_core_plugins_installed` "
  52. . "WHERE `name`='$pluginName' ;");
  53. if ($this->dbLink->errno) {
  54. $this->setError(ERROR_NOT_EXECUTED, "installEvents: cannot check whether the plugin is installed -> ".$this->dbLink->errno);
  55. return false;
  56. }
  57. if (!$this->dbLink->affected_rows) {
  58. $this->setError(ERROR_NOT_FOUND, "installEvents: required plugin [$pluginName] is not installed");
  59. return false;
  60. }
  61. // plugin identifier
  62. list($pluginId) = $qPlugin->fetch_row();
  63. // get list of available languages
  64. $qAvaiLang = $this->dbLink->query("SELECT `code`, `id` "
  65. . "FROM `".MECCANO_TPREF."_core_langman_languages` ;");
  66. if ($this->dbLink->errno) {
  67. $this->setError(ERROR_NOT_EXECUTED, 'installEvents: cannot get list of available languages: '.$this->dbLink->error);
  68. return false;
  69. }
  70. // avaiable languages
  71. $avLangIds = [];
  72. $avLangCodes = [];
  73. while ($row = $qAvaiLang->fetch_row()) {
  74. $avLangIds[$row[0]] = $row[1];
  75. $avLangCodes[] = $row[0];
  76. }
  77. // parse DOM tree
  78. $incomingEvents = [];
  79. $eventNodes = $events->getElementsByTagName('event');
  80. foreach ($eventNodes as $eventNode) {
  81. $keyword = $eventNode->getAttribute('keyword');
  82. $incomingEvents[$keyword] = [];
  83. $descNodes = $eventNode->getElementsByTagName('desc');
  84. foreach ($descNodes as $descNode){
  85. $code = $descNode->getAttribute('code');
  86. if (isset($avLangIds[$code])) {
  87. $incomingEvents[$keyword][$code] = $descNode->nodeValue;
  88. }
  89. }
  90. }
  91. // get installed events of the plugin
  92. $qEvents = $this->dbLink->query("SELECT `keyword`, `id` "
  93. . "FROM `".MECCANO_TPREF."_core_logman_events` "
  94. . "WHERE `plugid`=$pluginId");
  95. if ($this->dbLink->errno) {
  96. $this->setError(ERROR_NOT_EXECUTED, 'installEvents: unable to get installed events -> '.$this->dbLink->error);
  97. return false;
  98. }
  99. $installedEvents = [];
  100. while ($row = $qEvents->fetch_row()) {
  101. $installedEvents[$row[0]] = $row[1];
  102. }
  103. // delete outdated events
  104. $outdatedEvents = array_diff(array_keys($installedEvents), array_keys($incomingEvents));
  105. foreach ($outdatedEvents as $keyword) {
  106. $eventId = $installedEvents[$keyword];
  107. $sql = [
  108. "DELETE `r` FROM `".MECCANO_TPREF."_core_logman_records` `r` "
  109. . "JOIN `".MECCANO_TPREF."_core_logman_events` `e` "
  110. . "ON `e`.`id`=`r`.`eventid` "
  111. . "WHERE `e`.`id`=$eventId ;",
  112. "DELETE `d` FROM `".MECCANO_TPREF."_core_logman_descriptions` `d` "
  113. . "JOIN `".MECCANO_TPREF."_core_logman_events` `e` "
  114. . "ON `e`.`id`=`d`.`eventid`"
  115. . "WHERE `e`.`id`=$eventId ;",
  116. "DELETE FROM `".MECCANO_TPREF."_core_logman_events` "
  117. . "WHERE `id`=$eventId ;",
  118. ];
  119. foreach ($sql as $dQuery) {
  120. $this->dbLink->query($dQuery);
  121. if ($this->dbLink->errno) {
  122. $this->setError(ERROR_NOT_EXECUTED, "installEvents: unable to delete outdated event -> ".$this->dbLink->error);
  123. return false;
  124. }
  125. }
  126. }
  127. // install/update events
  128. foreach ($incomingEvents as $keyword => $descriptions) {
  129. $missingCodes = array_diff($avLangCodes, array_keys($descriptions));
  130. if ($missingCodes) {
  131. foreach ($missingCodes as $code) {
  132. $descriptions[$code] = "$keyword: [%d]";
  133. }
  134. }
  135. // update event
  136. if (isset($installedEvents[$keyword])) {
  137. $eventId = $installedEvents[$keyword];
  138. foreach ($descriptions as $inCode => $desc) {
  139. $codeId = $avLangIds[$inCode];
  140. $updateDesc = $this->dbLink->real_escape_string($desc);
  141. $eventId = $installedEvents[$keyword];
  142. $this->dbLink->query("UPDATE `".MECCANO_TPREF."_core_logman_descriptions` "
  143. . "SET `description`='$updateDesc' "
  144. . "WHERE `eventid`=$eventId "
  145. . "AND `codeid`=$codeId ;");
  146. if ($this->dbLink->errno) {
  147. $this->setError(ERROR_NOT_EXECUTED, 'installEvents: unable to update event description -> '.$this->dbLink->error);
  148. return false;
  149. }
  150. }
  151. }
  152. // install event
  153. else {
  154. $this->dbLink->query("INSERT INTO `".MECCANO_TPREF."_core_logman_events` "
  155. . "(`keyword`, `plugid`) "
  156. . "VALUES ('$keyword', $pluginId) ;");
  157. if ($this->dbLink->errno) {
  158. $this->setError(ERROR_NOT_EXECUTED, 'installEvents: unable add new event -> '.$this->dbLink->error);
  159. return false;
  160. }
  161. $eventId = $this->dbLink->insert_id;
  162. foreach ($descriptions as $inCode => $desc) {
  163. $codeId = $avLangIds[$inCode];
  164. $newDesc = $this->dbLink->real_escape_string($desc);
  165. $this->dbLink->query("INSERT INTO `".MECCANO_TPREF."_core_logman_descriptions` "
  166. . "(`description`, `eventid`, `codeid`) "
  167. . "VALUES ('$newDesc', $eventId, $codeId) ;");
  168. if ($this->dbLink->errno) {
  169. $this->setError(ERROR_NOT_EXECUTED, 'installEvents: unable to add new event description -> '.$this->dbLink->error);
  170. return false;
  171. }
  172. }
  173. }
  174. }
  175. return true;
  176. }
  177. public function delLogEvents($plugin) {
  178. $this->zeroizeError();
  179. if (!pregPlugin($plugin)) {
  180. $this->setError(ERROR_INCORRECT_DATA, "delLogEvents: incorrect plugin name");
  181. return false;
  182. }
  183. if ($plugin == "core") {
  184. $this->setError(ERROR_SYSTEM_INTERVENTION, "delLogEvents: unable to delete core events");
  185. return false;
  186. }
  187. $sql = [
  188. "DELETE `r` "
  189. . "FROM `".MECCANO_TPREF."_core_logman_records` `r` "
  190. . "JOIN `".MECCANO_TPREF."_core_logman_events` `e` "
  191. . "ON `e`.`id`=`r`.`eventid` "
  192. . "JOIN `".MECCANO_TPREF."_core_plugins_installed` `p` "
  193. . "ON `p`.`id`=`e`.`plugid` "
  194. . "WHERE `p`.`name`='$plugin' ;",
  195. "DELETE `d` "
  196. . "FROM `".MECCANO_TPREF."_core_logman_descriptions` `d` "
  197. . "JOIN `".MECCANO_TPREF."_core_logman_events` `e` "
  198. . "ON `e`.`id`=`d`.`eventid` "
  199. . "JOIN `".MECCANO_TPREF."_core_plugins_installed` `p` "
  200. . "ON `p`.`id`=`e`.`plugid` "
  201. . "WHERE `p`.`name`='$plugin' ;",
  202. "DELETE `e` "
  203. . "FROM `".MECCANO_TPREF."_core_logman_events` `e` "
  204. . "JOIN `".MECCANO_TPREF."_core_plugins_installed` `p` "
  205. . "ON `p`.`id`=`e`.`plugid` "
  206. . "WHERE `p`.`name`='$plugin' ;"
  207. ];
  208. foreach ($sql as $value) {
  209. $this->dbLink->query($value);
  210. if ($this->dbLink->errno) {
  211. $this->setError(ERROR_NOT_EXECUTED, "delLogEvents: unable remove events -> ".$this->dbLink->error);
  212. return false;
  213. }
  214. }
  215. return true;
  216. }
  217. public function clearLog() {
  218. $this->zeroizeError();
  219. if ($this->usePolicy && !$this->checkFuncAccess('core', 'logman_clear_log')) {
  220. $this->setError(ERROR_RESTRICTED_ACCESS, "clearLog: restricted by the policy");
  221. return false;
  222. }
  223. if (isset($_SESSION[AUTH_LIMITED]) && $_SESSION[AUTH_LIMITED]) {
  224. $this->setError(ERROR_RESTRICTED_ACCESS, 'clearLog: function execution was terminated because of using of limited authentication');
  225. return false;
  226. }
  227. $this->dbLink->query("TRUNCATE TABLE `".MECCANO_TPREF."_core_logman_records` ;");
  228. if ($this->dbLink->errno) {
  229. $this->setError(ERROR_NOT_EXECUTED, 'clearLog: unable to clear log -> '.$this->dbLink->error);
  230. return false;
  231. }
  232. $this->newLogRecord('core', 'logman_clear_log');
  233. return true;
  234. }
  235. public function sumLogAllPlugins($rpp = 20) { // rpp - records per page
  236. $this->zeroizeError();
  237. if (!is_integer($rpp)) {
  238. $this->setError(ERROR_INCORRECT_DATA, 'sumLog: rpp must be integer');
  239. return false;
  240. }
  241. if ($rpp < 1) {
  242. $rpp = 1;
  243. }
  244. $qResult = $this->dbLink->query("SELECT COUNT(`id`) FROM `".MECCANO_TPREF."_core_logman_records` ;");
  245. if ($this->dbLink->errno) {
  246. $this->setError(ERROR_NOT_EXECUTED, 'sumLog: unable to counted total records -> '.$this->dbLink->error);
  247. return false;
  248. }
  249. list($totalRecs) = $qResult->fetch_row();
  250. $totalPages = $totalRecs/$rpp;
  251. $remainer = fmod($totalRecs, $rpp);
  252. if ($totalPages<1 && $totalPages>0) {
  253. $totalPages = 1;
  254. }
  255. elseif ($totalPages>1 && $remainer != 0) {
  256. $totalPages += 1;
  257. }
  258. elseif ($totalPages == 0) {
  259. $totalPages = 1;
  260. }
  261. return ['records' => (int) $totalRecs, 'pages' => (int) $totalPages];
  262. }
  263. public function getPageAllPlugins($pageNumber, $totalPages, $rpp = 20, $code = MECCANO_DEF_LANG, $orderBy = ['id'], $ascent = false) {
  264. $this->zeroizeError();
  265. if ($this->usePolicy && !$this->checkFuncAccess('core', 'logman_get_log')) {
  266. $this->setError(ERROR_RESTRICTED_ACCESS, "getPageAllPlugins: restricted by the policy");
  267. return false;
  268. }
  269. if (!pregLang($code) || !is_integer($pageNumber) || !is_integer($totalPages) || !is_integer($rpp)) {
  270. $this->setError(ERROR_INCORRECT_DATA, 'getPageAllPlugins: check arguments');
  271. return false;
  272. }
  273. $rightEntry = ['id', 'user', 'event', 'time'];
  274. if (is_array($orderBy)) {
  275. $arrayLen = count($orderBy);
  276. if ($arrayLen && count(array_intersect($orderBy, $rightEntry)) == $arrayLen) {
  277. $orderList = '';
  278. foreach ($orderBy as $value) {
  279. $orderList = $orderList.$value.'`, `';
  280. }
  281. $orderBy = substr($orderList, 0, -4);
  282. }
  283. else {
  284. $orderBy = 'id';
  285. }
  286. }
  287. else {
  288. $this->setError(ERROR_INCORRECT_DATA, 'getPageAllPlugins: check order parameters');
  289. return false;
  290. }
  291. if ($pageNumber < 1) {
  292. $pageNumber = 1;
  293. }
  294. elseif ($pageNumber>$totalPages && $totalPages) {
  295. $pageNumber = $totalPages;
  296. }
  297. if ($totalPages < 1) {
  298. $totalPages = 1;
  299. }
  300. if ($rpp < 1) {
  301. $rpp = 1;
  302. }
  303. if ($ascent == true) {
  304. $direct = '';
  305. }
  306. elseif ($ascent == false) {
  307. $direct = 'DESC';
  308. }
  309. $start = ($pageNumber - 1) * $rpp;
  310. $qResult = $this->dbLink->query("SELECT `r`.`id` `id`, `r`.`time` `time`, REPLACE(`d`.`description`, '%d', `r`.`insertion`) `event`, `r`.`user` `user` "
  311. . "FROM `".MECCANO_TPREF."_core_logman_records` `r` "
  312. . "JOIN `".MECCANO_TPREF."_core_logman_descriptions` `d` "
  313. . "ON `r`.`eventid` = `d`.`eventid` "
  314. . "JOIN `".MECCANO_TPREF."_core_langman_languages` `l` "
  315. . "ON `d`.`codeid` = `l`.`id` "
  316. . "WHERE `l`.`code` = '$code' "
  317. . "ORDER BY `$orderBy` $direct LIMIT $start, $rpp;");
  318. if ($this->dbLink->errno) {
  319. $this->setError(ERROR_NOT_EXECUTED, 'getPageAllPlugins: unable to get log page -> '.$this->dbLink->error);
  320. return false;
  321. }
  322. if ($this->outputType == 'xml') {
  323. $xml = new \DOMDocument('1.0', 'utf-8');
  324. $logNode = $xml->createElement('log');
  325. $xml->appendChild($logNode);
  326. while ($row = $qResult->fetch_row()) {
  327. $recordNode = $xml->createElement('record');
  328. $logNode->appendChild($recordNode);
  329. $recordNode->appendChild($xml->createElement('id', $row[0]));
  330. $recordNode->appendChild($xml->createElement('time', $row[1]));
  331. $recordNode->appendChild($xml->createElement('event', $row[2]));
  332. $recordNode->appendChild($xml->createElement('user', $row[3]));
  333. }
  334. return $xml;
  335. }
  336. else {
  337. $log = [];
  338. while ($row = $qResult->fetch_row()) {
  339. $log[] = [
  340. 'id' => (int) $row[0],
  341. 'time' => $row[1],
  342. 'event' => $row[2],
  343. 'user' => $row[3]
  344. ];
  345. }
  346. if ($this->outputType == 'json') {
  347. return json_encode($log);
  348. }
  349. else {
  350. return $log;
  351. }
  352. }
  353. }
  354. public function sumLogByPlugin($plugin, $rpp = 20) {
  355. $this->zeroizeError();
  356. if (!pregPlugin($plugin) || !is_integer($rpp)) {
  357. $this->setError(ERROR_INCORRECT_DATA, 'sumLog: check arguments');
  358. return false;
  359. }
  360. if ($rpp < 1) {
  361. $rpp = 1;
  362. }
  363. $qResult = $this->dbLink->query("SELECT COUNT(`r`.`id`) "
  364. . "FROM `".MECCANO_TPREF."_core_logman_records` `r`"
  365. . "JOIN `".MECCANO_TPREF."_core_logman_events` `e` "
  366. . "ON `e`.`id`=`r`.`eventid` "
  367. . "JOIN `".MECCANO_TPREF."_core_plugins_installed` `p` "
  368. . "ON `p`.`id`=`e`.`plugid` "
  369. . "WHERE `p`.`name`='$plugin' ;");
  370. if ($this->dbLink->errno) {
  371. $this->setError(ERROR_NOT_EXECUTED, 'sumLog: unable to counted total records -> '.$this->dbLink->error);
  372. return false;
  373. }
  374. list($totalRecs) = $qResult->fetch_row();
  375. $totalPages = $totalRecs/$rpp;
  376. $remainer = fmod($totalRecs, $rpp);
  377. if ($totalPages<1 && $totalPages>0) {
  378. $totalPages = 1;
  379. }
  380. elseif ($totalPages>1 && $remainer != 0) {
  381. $totalPages += 1;
  382. }
  383. elseif ($totalPages == 0) {
  384. $totalPages = 1;
  385. }
  386. return ['records' => (int) $totalRecs, 'pages' => (int) $totalPages];
  387. }
  388. public function getPageByPlugin($plugin, $pageNumber, $totalPages, $rpp = 20, $code = MECCANO_DEF_LANG, $orderBy = ['id'], $ascent = false) {
  389. $this->zeroizeError();
  390. if ($this->usePolicy && !$this->checkFuncAccess('core', 'logman_get_log')) {
  391. $this->setError(ERROR_RESTRICTED_ACCESS, "getPageByPlugin: restricted by the policy");
  392. return false;
  393. }
  394. if (!pregPlugin($plugin) || !pregLang($code) || !is_integer($pageNumber) || !is_integer($totalPages) || !is_integer($rpp)) {
  395. $this->setError(ERROR_INCORRECT_DATA, 'getPageByPlugin: check arguments');
  396. return false;
  397. }
  398. $rightEntry = ['id', 'user', 'event', 'time'];
  399. if (is_array($orderBy)) {
  400. $arrayLen = count($orderBy);
  401. if ($arrayLen && count(array_intersect($orderBy, $rightEntry)) == $arrayLen) {
  402. $orderList = '';
  403. foreach ($orderBy as $value) {
  404. $orderList = $orderList.$value.'`, `';
  405. }
  406. $orderBy = substr($orderList, 0, -4);
  407. }
  408. else {
  409. $orderBy = 'id';
  410. }
  411. }
  412. else {
  413. $this->setError(ERROR_INCORRECT_DATA, 'getPageByPlugin: check order parameters');
  414. return false;
  415. }
  416. if ($pageNumber < 1) {
  417. $pageNumber = 1;
  418. }
  419. elseif ($pageNumber>$totalPages && $totalPages) {
  420. $pageNumber = $totalPages;
  421. }
  422. if ($totalPages < 1) {
  423. $totalPages = 1;
  424. }
  425. if ($rpp < 1) {
  426. $rpp = 1;
  427. }
  428. if ($ascent == true) {
  429. $direct = '';
  430. }
  431. elseif ($ascent == false) {
  432. $direct = 'DESC';
  433. }
  434. $start = ($pageNumber - 1) * $rpp;
  435. $qResult = $this->dbLink->query("SELECT `r`.`id` `id`, `r`.`time` `time`, REPLACE(`d`.`description`, '%d', `r`.`insertion`) `event`, `r`.`user` `user` "
  436. . "FROM `".MECCANO_TPREF."_core_logman_records` `r` "
  437. . "JOIN `".MECCANO_TPREF."_core_logman_descriptions` `d` "
  438. . "ON `r`.`eventid` = `d`.`eventid` "
  439. . "JOIN `".MECCANO_TPREF."_core_logman_events` `e` "
  440. . "ON `r`.`eventid`=`e`.`id` "
  441. . "JOIN `".MECCANO_TPREF."_core_plugins_installed` `p` "
  442. . "ON `p`.`id`=`e`.`plugid` "
  443. . "JOIN `".MECCANO_TPREF."_core_langman_languages` `l` "
  444. . "ON `d`.`codeid` = `l`.`id` "
  445. . "WHERE `l`.`code` = '$code' "
  446. . "AND `p`.`name`='$plugin' "
  447. . "ORDER BY `$orderBy` $direct LIMIT $start, $rpp;");
  448. if ($this->dbLink->errno) {
  449. $this->setError(ERROR_NOT_EXECUTED, 'getPageByPlugin: unable to get log page -> '.$this->dbLink->error);
  450. return false;
  451. }
  452. if ($this->outputType == 'xml') {
  453. $xml = new \DOMDocument('1.0', 'utf-8');
  454. $logNode = $xml->createElement('log');
  455. $attr_plugin = $xml->createAttribute('plugin');
  456. $attr_plugin->value = $plugin;
  457. $logNode->appendChild($attr_plugin);
  458. $xml->appendChild($logNode);
  459. while ($row = $qResult->fetch_row()) {
  460. $recordNode = $xml->createElement('record');
  461. $logNode->appendChild($recordNode);
  462. $recordNode->appendChild($xml->createElement('id', $row[0]));
  463. $recordNode->appendChild($xml->createElement('time', $row[1]));
  464. $recordNode->appendChild($xml->createElement('event', $row[2]));
  465. $recordNode->appendChild($xml->createElement('user', $row[3]));
  466. }
  467. return $xml;
  468. }
  469. else {
  470. $log = [];
  471. $log['plugin'] = $plugin;
  472. while ($row = $qResult->fetch_row()) {
  473. $log['records'][] = [
  474. 'id' => (int) $row[0],
  475. 'time' => $row[1],
  476. 'event' => $row[2],
  477. 'user' => $row[3]
  478. ];
  479. }
  480. if ($this->outputType == 'json') {
  481. return json_encode($log);
  482. }
  483. else {
  484. return $log;
  485. }
  486. }
  487. }
  488. public function getLogAllPlugins($code = MECCANO_DEF_LANG, $orderBy = ['id'], $ascent = false) {
  489. $this->zeroizeError();
  490. if ($this->usePolicy && !$this->checkFuncAccess('core', 'logman_get_log')) {
  491. $this->setError(ERROR_RESTRICTED_ACCESS, "getLogAllPlugins: restricted by the policy");
  492. return false;
  493. }
  494. if (!pregLang($code)) {
  495. $this->setError(ERROR_INCORRECT_DATA, 'getLogAllPlugins: check arguments');
  496. return false;
  497. }
  498. $rightEntry = ['id', 'user', 'event', 'time'];
  499. if (is_array($orderBy)) {
  500. $arrayLen = count($orderBy);
  501. if ($arrayLen && count(array_intersect($orderBy, $rightEntry)) == $arrayLen) {
  502. $orderList = '';
  503. foreach ($orderBy as $value) {
  504. $orderList = $orderList.$value.'`, `';
  505. }
  506. $orderBy = substr($orderList, 0, -4);
  507. }
  508. else {
  509. $orderBy = 'id';
  510. }
  511. }
  512. else {
  513. $this->setError(ERROR_INCORRECT_DATA, 'getLogAllPlugins: check order parameters');
  514. return false;
  515. }
  516. if ($ascent == true) {
  517. $direct = '';
  518. }
  519. elseif ($ascent == false) {
  520. $direct = 'DESC';
  521. }
  522. $qResult = $this->dbLink->query("SELECT `r`.`id` `id`, `r`.`time` `time`, REPLACE(`d`.`description`, '%d', `r`.`insertion`) `event`, `r`.`user` `user` "
  523. . "FROM `".MECCANO_TPREF."_core_logman_records` `r` "
  524. . "JOIN `".MECCANO_TPREF."_core_logman_descriptions` `d` "
  525. . "ON `r`.`eventid` = `d`.`eventid` "
  526. . "JOIN `".MECCANO_TPREF."_core_langman_languages` `l` "
  527. . "ON `d`.`codeid` = `l`.`id` "
  528. . "WHERE `l`.`code` = '$code' "
  529. . "ORDER BY `$orderBy` $direct ;");
  530. if ($this->dbLink->errno) {
  531. $this->setError(ERROR_NOT_EXECUTED, 'getLogAllPlugins: unable to get log -> '.$this->dbLink->error);
  532. return false;
  533. }
  534. if ($this->outputType == 'xml') {
  535. $xml = new \DOMDocument('1.0', 'utf-8');
  536. $logNode = $xml->createElement('log');
  537. $xml->appendChild($logNode);
  538. while ($row = $qResult->fetch_row()) {
  539. $recordNode = $xml->createElement('record');
  540. $logNode->appendChild($recordNode);
  541. $recordNode->appendChild($xml->createElement('id', $row[0]));
  542. $recordNode->appendChild($xml->createElement('time', $row[1]));
  543. $recordNode->appendChild($xml->createElement('event', $row[2]));
  544. $recordNode->appendChild($xml->createElement('user', $row[3]));
  545. }
  546. return $xml;
  547. }
  548. else {
  549. $log = [];
  550. while ($row = $qResult->fetch_row()) {
  551. $log[] = [
  552. 'id' => (int) $row[0],
  553. 'time' => $row[1],
  554. 'event' => $row[2],
  555. 'user' => $row[3]
  556. ];
  557. }
  558. if ($this->outputType == 'json') {
  559. return json_encode($log);
  560. }
  561. else {
  562. return $log;
  563. }
  564. }
  565. }
  566. public function getLogByPlugin($plugin, $code = MECCANO_DEF_LANG, $orderBy = ['id'], $ascent = false) {
  567. $this->zeroizeError();
  568. if ($this->usePolicy && !$this->checkFuncAccess('core', 'logman_get_log')) {
  569. $this->setError(ERROR_RESTRICTED_ACCESS, "getLogByPlugin: restricted by the policy");
  570. return false;
  571. }
  572. if (!pregPlugin($plugin) || !pregLang($code)) {
  573. $this->setError(ERROR_INCORRECT_DATA, 'getLogByPlugin: check arguments');
  574. return false;
  575. }
  576. $rightEntry = ['id', 'user', 'event', 'time'];
  577. if (is_array($orderBy)) {
  578. $arrayLen = count($orderBy);
  579. if ($arrayLen && count(array_intersect($orderBy, $rightEntry)) == $arrayLen) {
  580. $orderList = '';
  581. foreach ($orderBy as $value) {
  582. $orderList = $orderList.$value.'`, `';
  583. }
  584. $orderBy = substr($orderList, 0, -4);
  585. }
  586. else {
  587. $orderBy = 'id';
  588. }
  589. }
  590. else {
  591. $this->setError(ERROR_INCORRECT_DATA, 'getLogByPlugin: check order parameters');
  592. return false;
  593. }
  594. if ($ascent == true) {
  595. $direct = '';
  596. }
  597. elseif ($ascent == false) {
  598. $direct = 'DESC';
  599. }
  600. $qResult = $this->dbLink->query("SELECT `r`.`id` `id`, `r`.`time` `time`, REPLACE(`d`.`description`, '%d', `r`.`insertion`) `event`, `r`.`user` `user` "
  601. . "FROM `".MECCANO_TPREF."_core_logman_records` `r` "
  602. . "JOIN `".MECCANO_TPREF."_core_logman_descriptions` `d` "
  603. . "ON `r`.`eventid` = `d`.`eventid` "
  604. . "JOIN `".MECCANO_TPREF."_core_logman_events` `e` "
  605. . "ON `r`.`eventid`=`e`.`id` "
  606. . "JOIN `".MECCANO_TPREF."_core_plugins_installed` `p` "
  607. . "ON `p`.`id`=`e`.`plugid` "
  608. . "JOIN `".MECCANO_TPREF."_core_langman_languages` `l` "
  609. . "ON `d`.`codeid` = `l`.`id` "
  610. . "WHERE `l`.`code` = '$code' "
  611. . "AND `p`.`name`='$plugin' "
  612. . "ORDER BY `$orderBy` $direct ;");
  613. if ($this->dbLink->errno) {
  614. $this->setError(ERROR_NOT_EXECUTED, 'getLogByPlugin: unable to get log -> '.$this->dbLink->error);
  615. return false;
  616. }
  617. if ($this->outputType == 'xml') {
  618. $xml = new \DOMDocument('1.0', 'utf-8');
  619. $logNode = $xml->createElement('log');
  620. $attr_plugin = $xml->createAttribute('plugin');
  621. $attr_plugin->value = $plugin;
  622. $logNode->appendChild($attr_plugin);
  623. $xml->appendChild($logNode);
  624. while ($row = $qResult->fetch_row()) {
  625. $recordNode = $xml->createElement('record');
  626. $logNode->appendChild($recordNode);
  627. $recordNode->appendChild($xml->createElement('id', $row[0]));
  628. $recordNode->appendChild($xml->createElement('time', $row[1]));
  629. $recordNode->appendChild($xml->createElement('event', $row[2]));
  630. $recordNode->appendChild($xml->createElement('user', $row[3]));
  631. }
  632. return $xml;
  633. }
  634. else {
  635. $log = [];
  636. $log['plugin'] = $plugin;
  637. while ($row = $qResult->fetch_row()) {
  638. $log['records'][] = [
  639. 'id' => (int) $row[0],
  640. 'time' => $row[1],
  641. 'event' => $row[2],
  642. 'user' => $row[3]
  643. ];
  644. }
  645. if ($this->outputType == 'json') {
  646. return json_encode($log);
  647. }
  648. else {
  649. return $log;
  650. }
  651. }
  652. }
  653. }