extclass.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /*
  3. * phpMeccano v0.2.0. Web-framework written with php programming language. Core module [extclass.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.'/swconst.php';
  26. require_once MECCANO_CORE_DIR.'/unifunctions.php';
  27. interface intServiceMethods {
  28. public function errId();
  29. public function errExp();
  30. public function applyPolicy($flag);
  31. public function outputFormat($output = 'xml');
  32. public function checkFuncAccess($plugin, $func, $userId = 0);
  33. public function newLogRecord($plugin, $event, $insertion = ''); // old name [newRecord]
  34. }
  35. class ServiceMethods implements intServiceMethods {
  36. protected $errid = 0; // error's id
  37. protected $errexp = ''; // error's explanation
  38. protected $usePolicy = true; // flag of the policy application
  39. protected $outputType = 'json'; // format of the output data
  40. protected function setError($id, $exp, $errtype = E_USER_NOTICE) {
  41. $this->errid = $id;
  42. $this->errexp = $exp;
  43. if (MECCANO_SHOW_ERRORS) {
  44. trigger_error("ERROR $id. $exp", $errtype);
  45. }
  46. }
  47. protected function zeroizeError() {
  48. $this->errid = 0; $this->errexp = '';
  49. }
  50. public function errId() {
  51. return $this->errid;
  52. }
  53. public function errExp() {
  54. return $this->errexp;
  55. }
  56. public function applyPolicy($flag = false) {
  57. if ($flag) {
  58. $this->usePolicy = true;
  59. }
  60. else {
  61. $this->usePolicy = false;
  62. }
  63. }
  64. public function outputFormat($output = 'xml') {
  65. if ($output == 'xml') {
  66. $this->outputType = 'xml';
  67. }
  68. elseif ($output == 'json') {
  69. $this->outputType = 'json';
  70. }
  71. elseif ($output == 'array') {
  72. $this->outputType = 'array';
  73. }
  74. else {
  75. $this->outputType = 'json';
  76. }
  77. }
  78. // method came from core module policy.php
  79. // Method [checkFuncAccess] grants access to the user independently from the access policy, if argument $userId is equal to ID of the authenticated user. For example, it may be useful, if the user going to get it's own data.
  80. public function checkFuncAccess($plugin, $func, $userId = 0) {
  81. $this->zeroizeError();
  82. if (!pregPlugin($plugin) || !pregPlugin($func)) {
  83. $this->setError(ERROR_INCORRECT_DATA, 'checkFuncAccess: check incoming parameters');
  84. return false;
  85. }
  86. // grant access if policy is disabled
  87. if (!$this->usePolicy) {
  88. return 1;
  89. }
  90. if (isset($_SESSION[AUTH_USER_ID])) {
  91. if ($_SESSION[AUTH_USER_ID] == $userId) {
  92. return 1;
  93. }
  94. else {
  95. $qAccess = $this->dbLink->query("SELECT `a`.`access` "
  96. . "FROM `".MECCANO_TPREF."_core_policy_access` `a` "
  97. . "JOIN `".MECCANO_TPREF."_core_policy_summary_list` `s` "
  98. . "ON `a`.`funcid`=`s`.`id` "
  99. . "JOIN `".MECCANO_TPREF."_core_userman_groups` `g` "
  100. . "ON `a`.`groupid`=`g`.`id` "
  101. . "JOIN `".MECCANO_TPREF."_core_userman_users` `u` "
  102. . "ON `g`.`id`=`u`.`groupid` "
  103. . "WHERE `u`.`id`=".$_SESSION[AUTH_USER_ID]." "
  104. . "AND `s`.`name`='$plugin' "
  105. . "AND `s`.`func`='$func' "
  106. . "LIMIT 1 ;");
  107. }
  108. }
  109. else {
  110. $qAccess = $this->dbLink->query("SELECT `n`.`access` "
  111. . "FROM `".MECCANO_TPREF."_core_policy_nosession` `n` "
  112. . "JOIN `".MECCANO_TPREF."_core_policy_summary_list` `s` "
  113. . "ON `n`.`funcid`=`s`.`id` "
  114. . "WHERE `s`.`name`='$plugin' "
  115. . "AND `s`.`func`='$func' "
  116. . "LIMIT 1 ;");
  117. }
  118. if ($this->dbLink->errno) {
  119. $this->setError(ERROR_NOT_EXECUTED, 'checkFuncAccess: something went wrong -> '.$this->dbLink->error);
  120. return false;
  121. }
  122. if (!$this->dbLink->affected_rows) {
  123. $this->setError(ERROR_NOT_FOUND, 'checkFuncAccess: policy is not found');
  124. return false;
  125. }
  126. list($access) = $qAccess->fetch_row();
  127. return (int) $access;
  128. }
  129. // method came from core module logman.php
  130. public function newLogRecord($plugin, $keyword, $insertion = '') {
  131. $this->zeroizeError();
  132. if (!pregPlugin($plugin) || !pregPlugin($keyword) || !is_string($insertion)) {
  133. $this->setError(ERROR_INCORRECT_DATA, 'newLogRecord: check arguments');
  134. return false;
  135. }
  136. $keyword = $this->dbLink->real_escape_string($keyword);
  137. $insertion = $this->dbLink->real_escape_string($insertion);
  138. // get event identifier
  139. $qEvent = $this->dbLink->query("SELECT `e`.`id` "
  140. . "FROM `".MECCANO_TPREF."_core_logman_events` `e` "
  141. . "JOIN `".MECCANO_TPREF."_core_plugins_installed` `p` "
  142. . "ON `p`.`id`=`e`.`plugid` "
  143. . "WHERE `e`.`keyword`='$keyword' "
  144. . "AND `p`.`name`='$plugin' ;");
  145. if ($this->dbLink->errno) {
  146. $this->setError(ERROR_NOT_EXECUTED, 'newLogRecord: unable to get event identifier -> '.$this->dbLink->error);
  147. return false;
  148. }
  149. if (!$this->dbLink->affected_rows) {
  150. $this->setError(ERROR_NOT_FOUND, 'newLogRecord: plugin or event not found');
  151. return false;
  152. }
  153. list($eventId) = $qEvent->fetch_row();
  154. // make new record
  155. if (isset($_SESSION[AUTH_LIMITED])) {
  156. $this->dbLink->query("INSERT INTO `".MECCANO_TPREF."_core_logman_records` (`eventid`, `insertion`, `user`) "
  157. . "VALUES ($eventId, '$insertion', '".$_SESSION[AUTH_USERNAME]."') ;");
  158. }
  159. else {
  160. $this->dbLink->query("INSERT INTO `".MECCANO_TPREF."_core_logman_records` (`eventid`, `insertion`) "
  161. . "VALUES ($eventId, '$insertion') ;");
  162. }
  163. if ($this->dbLink->errno) {
  164. $this->setError(ERROR_NOT_EXECUTED, 'newLogRecord: unable to make new record -> '.$this->dbLink->error);
  165. return false;
  166. }
  167. return true;
  168. }
  169. }