PageRenderTime 44ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/mod/rafl/includes/tng/tNG_dispatcher.class.php

https://github.com/nadavkav/MoodleTAO
PHP | 421 lines | 279 code | 22 blank | 120 comment | 92 complexity | 55a57b1d8d9ccd9a8ba97424b56b2ce1 MD5 | raw file
  1. <?php
  2. /*
  3. * ADOBE SYSTEMS INCORPORATED
  4. * Copyright 2007 Adobe Systems Incorporated
  5. * All Rights Reserved
  6. *
  7. * NOTICE: Adobe permits you to use, modify, and distribute this file in accordance with the
  8. * terms of the Adobe license agreement accompanying it. If you have received this file from a
  9. * source other than Adobe, then your use, modification, or distribution of it requires the prior
  10. * written permission of Adobe.
  11. */
  12. /*
  13. Copyright (c) InterAKT Online 2000-2006. All rights reserved.
  14. */
  15. /**
  16. * The dispatcher class, that handles all the transactions in a page.
  17. * @access public
  18. */
  19. class tNG_dispatcher {
  20. /**
  21. * The associated transactions array
  22. * @var array
  23. * @access private
  24. */
  25. var $tNGs = array();
  26. /**
  27. * The number of associated transactions
  28. * @var integer
  29. * @access private
  30. */
  31. var $n = 0;
  32. var $UnivalProps = array();
  33. /**
  34. * The relative path of the current tNG_dispathcer
  35. * @var string
  36. * @access public
  37. */
  38. var $relPath = "";
  39. /**
  40. * Internal counter for the number of validations;
  41. * @var integer
  42. * @access private
  43. */
  44. var $UnivalCount = 0;
  45. /**
  46. * Constructor. Set the value of relative path
  47. * @param string $relPath The relative path of the current tNG_dispathcer
  48. * @access public
  49. */
  50. function tNG_dispatcher($relPath) {
  51. $this->relPath = $relPath;
  52. }
  53. /**
  54. * Adds a transaction to the current dispatcher
  55. * @param object tNG &$tNG - the transaction to add
  56. * @access public
  57. */
  58. function addTransaction(&$tNG) {
  59. $tNG->setDispatcher($this);
  60. $this->tNGs[$this->n++] = &$tNG;
  61. }
  62. /**
  63. * Gets the correct recordset from the transactions
  64. * @param string $tableName - the table to search
  65. * @return object RedourceID
  66. * the Recordset Resource ID
  67. * @access public
  68. */
  69. function getRecordset($tableName) {
  70. $method_where = -1;
  71. $method = -1;
  72. /*
  73. -1 = unknown
  74. 1 = default values
  75. 2 = from recordset
  76. 3 = submitted values
  77. */
  78. for ($i=0;$i<$this->n;$i++) {
  79. if ($this->tNGs[$i]->getTable() == $tableName) {
  80. switch($this->tNGs[$i]->getTransactionType()) {
  81. case '_login':
  82. case '_custom':
  83. case '_insert':
  84. case '_multipleInsert':
  85. if ($method < 1) {
  86. $method = 1;
  87. $method_where = $i;
  88. }
  89. break;
  90. case '_update':
  91. case '_multipleUpdate':
  92. $pkv = $this->tNGs[$i]->getPrimaryKeyValue();
  93. if (isset($pkv)) {
  94. if ($method < 2) {
  95. $method = 2;
  96. $method_where = $i;
  97. }
  98. }
  99. break;
  100. }
  101. if ($this->tNGs[$i]->isStarted()) {
  102. if ($this->tNGs[$i]->exportsRecordset()) {
  103. $tmpArrErr = $this->tNGs[$i]->getErrorMsg();
  104. if ($tmpArrErr[1] != '' || $tmpArrErr[2] != '') {
  105. $method = 3;
  106. $method_where = $i;
  107. }
  108. }
  109. }
  110. }
  111. }
  112. if ($method_where == -1) {
  113. $ret = $this->tNGs[0]->getRecordset();
  114. //die('tNG_dispatcher.getRecordset:<br/>The transaction that handles the curent recordset is not properly configured.<br>Check the settings from the user interface that generated the code. Possible reasons for this failure: you have specified URL parameter for the Primary key column in the interface and you are running the page without any parameter or the incorect parameter.');
  115. } else {
  116. $ret = $this->tNGs[$method_where]->getRecordset();
  117. }
  118. return $ret;
  119. }
  120. /**
  121. * Executes the linked transactions, one by one
  122. * @access public
  123. */
  124. function executeTransactions() {
  125. for ($i=0;$i<$this->n;$i++) {
  126. $this->tNGs[$i]->executeTransaction();
  127. }
  128. }
  129. /**
  130. * construct the JS to be used in page for client side validation
  131. * @return string
  132. * @access public
  133. */
  134. function displayValidationRules() {
  135. $outRules = '';
  136. $outRules .= '<script src="'.$this->relPath.'includes/tng/scripts/FormValidation.js" type="text/javascript"></script>' . "\r\n";
  137. $outRules .= '<script src="'.$this->relPath.'includes/tng/scripts/FormValidation.js.php" type="text/javascript"></script>' . "\r\n";
  138. if (isset($this->UnivalProps) && is_array($this->UnivalProps) && count($this->UnivalProps) > 0) {
  139. $outRules .= '<script type="text/javascript">';
  140. $univalPropKeys = array_keys($this->UnivalProps);
  141. $count = count($univalPropKeys);
  142. $sw = false;
  143. for ($i = 0; $i < $count; $i++) {
  144. $fieldName = $univalPropKeys[$i];
  145. $field = $this->UnivalProps[$fieldName];
  146. // get the form field name
  147. $formFieldName = $fieldName;
  148. $skip = false;
  149. for ($j=0;$j<$this->n;$j++) {
  150. if (isset($this->tNGs[$j]->columns[$fieldName])) {
  151. $formFieldName = $this->tNGs[$j]->columns[$fieldName]['reference'];
  152. if ($this->tNGs[$j]->columns[$fieldName]['method'] == 'CURRVAL') {
  153. $skip = true;
  154. }
  155. }
  156. }
  157. if ($skip) {
  158. continue;
  159. }
  160. if ($formFieldName !== '') {
  161. $outRules .= "\r\n KT_FVO['" . KT_escapeJS($formFieldName) . "'] = {";
  162. $outRules .= "required: " . $field['required'] . ", ";
  163. $outRules .= "type: '" . $field['type'] . "', ";
  164. if ($field['format'] != '' ) {
  165. $outRules .= "format: '" . $field['format'] . "', ";
  166. }
  167. if ($field['additional_params'] != '') {
  168. $outRules .= "additional_params: '" . KT_escapeJS($field['additional_params']) . "', ";
  169. }
  170. if ($field['min'] != '' ) {
  171. $outRules .= "min: '" . $field['min'] . "', ";
  172. }
  173. if ($field['max'] != '' ) {
  174. $outRules .= "max: '" . $field['max'] . "', ";
  175. }
  176. if ($this->UnivalErrors[$fieldName] != '' ) {
  177. $outRules .= "errorMessage: '" . KT_escapeJS($this->UnivalErrors[$fieldName]) . "', ";
  178. }
  179. $outRules = substr($outRules, 0, strlen($outRules) - 2);
  180. $outRules .= "}";
  181. }
  182. }
  183. $outRules .= "\r\n\r\n";
  184. $outRules .= " KT_FVO_properties['noTriggers'] += " . $this->UnivalCount . ";\r\n";
  185. $outRules .= " KT_FVO_properties['noTransactions'] += " . $this->n . ";\r\n";
  186. $outRules .= '</script>';
  187. }
  188. return $outRules;
  189. }
  190. /**
  191. * Gets the hint for the fieldname
  192. * @param string $fieldName
  193. * @return string hint
  194. * @access public
  195. */
  196. function displayFieldHint($fieldName) {
  197. if (isset($this->fieldHints[$fieldName]) && $this->fieldHints[$fieldName] != '()') {
  198. return '<span class="KT_field_hint">'.$this->fieldHints[$fieldName].'</span>'."\r\n";
  199. }
  200. }
  201. /**
  202. * return the error for the field
  203. * @param string $tableName
  204. * @param string $fieldName
  205. * @cnt integer $cnt
  206. * @return string error for the field
  207. * @access public
  208. */
  209. function displayFieldError($tableName, $fieldName, $cnt = 1) {
  210. $ret = '';
  211. for ($i=0; $i<$this->n; $i++) {
  212. if ($this->tNGs[$i]->getTable() == $tableName) {
  213. if ($this->tNGs[$i]->isStarted()) {
  214. $tmp = $this->tNGs[$i]->getError();
  215. if ($tmp) {
  216. $ret = $this->tNGs[$i]->getFieldError($fieldName, $cnt);
  217. break;
  218. }
  219. }
  220. }
  221. }
  222. if ($ret != '') {
  223. return '<br class="clearfixplain" /><div class="KT_field_error">'.$ret.'</div>'."\r\n";
  224. } else {
  225. return '';
  226. }
  227. }
  228. /**
  229. * prepair the validation
  230. * @param object $unival
  231. * @return string transaction type
  232. * @access public
  233. */
  234. function prepareValidation(&$uniVal) {
  235. $this->UnivalCount++;
  236. if (!is_array($uniVal->columns) || count($uniVal->columns) == 0) {
  237. return;
  238. }
  239. foreach ($uniVal->columns as $columnName => $column) {
  240. // Set unival JS div errors
  241. // here we set the least restrictive required prop
  242. $required = ($column['required']===true)?'true':'false';
  243. if (array_key_exists($columnName, $this->UnivalProps)) {
  244. if ($this->UnivalProps[$columnName]['required'] != $required) {
  245. $required = 'false';
  246. }
  247. }
  248. $this->UnivalProps[$columnName]['required'] = $required;
  249. $this->UnivalProps[$columnName]['type'] = $column['type'];
  250. $this->UnivalProps[$columnName]['format'] = $column['format'];
  251. $this->UnivalProps[$columnName]['additional_params'] = $column['additional_params'];
  252. $this->UnivalProps[$columnName]['min'] = $column['min_cs'];
  253. $this->UnivalProps[$columnName]['max'] = $column['max_cs'];
  254. @$this->UnivalProps[$columnName]['count']++;
  255. $this->UnivalErrors[$columnName] = $column['message'];
  256. // Set field Hints
  257. if ($column['type'] == 'regexp') {
  258. $this->fieldHints[$columnName] = '';
  259. } elseif ($column['type'] == 'mask') {
  260. $this->fieldHints[$columnName] = '(' . $column['additional_params'] . ')';
  261. } elseif ($column['type'] == 'date' && $column['format'] != '') {
  262. $this->fieldHints[$columnName] = '(' . $uniVal->genericValidationMessages['date_' . $column['format']] . ' ' . $column['date_screen_format'] . ')';
  263. } elseif ($column['format'] != '') {
  264. $this->fieldHints[$columnName] = '(' . $uniVal->genericValidationMessages[$column['type'] . '_' . $column['format']] . ')';
  265. }
  266. }
  267. }
  268. /**
  269. * return the error message
  270. * @return string transaction type
  271. * @access public
  272. */
  273. function getErrorMsg() {
  274. $ret_warning = '';
  275. $ret_user = '';
  276. $ret_devel = '';
  277. $errorWasFound = false;
  278. for ($i=0;$i<$this->n;$i++) {
  279. list($ret_warning, $ret_user, $ret_devel) = $this->tNGs[$i]->getErrorMsg();
  280. if ($ret_warning!='' || $ret_user!='' || $ret_devel != '') {
  281. $errorWasFound = true;
  282. break;
  283. }
  284. }
  285. $uniq = uniqid("");
  286. $rethead = '';
  287. //$rethead = '<link href="' . $this->relPath . 'includes/tng/styles/default.css" rel="stylesheet" type="text/css" />' . "\r\n";
  288. //$rethead .= '<script src="' . $this->relPath . 'includes/common/js/base.js" type="text/javascript"></script>' . "\r\n";
  289. //$rethead .= '<script src="' . $this->relPath . 'includes/common/js/utility.js" type="text/javascript"></script>' . "\r\n";
  290. $ret = '';
  291. $txtContent = "";
  292. $txtContent .= "Client IP:\r\n " . $_SERVER['REMOTE_ADDR'];
  293. $txtContent .= "\r\n\r\nHost:\r\n " . $_SERVER['HTTP_HOST'];
  294. $txtContent .= "\r\n\r\nRequested URI:\r\n " . KT_getFullUri();
  295. $txtContent .= "\r\n\r\nDate:\r\n " . date("Y-m-d H:i:s");
  296. if ($errorWasFound) {
  297. if ($ret_warning!='') {
  298. $ret .= '<div id="KT_tngwarning">'. $ret_warning . "</div>\r\n";
  299. $txtContent .= "\r\n\r\nWarning:\r\n " . $ret_warning;
  300. }
  301. if ($ret_user!='') {
  302. $ret .= '<div id="KT_tngerror"><label>'.KT_getResource('ERROR_LABEL','tNG').'</label><div>' . $ret_user . '</div></div>' . "\r\n";
  303. $txtContent .= "\r\n\r\n".KT_getResource('ERROR_LABEL','tNG')."\r\n " . $ret_user;
  304. }
  305. if ('DEVELOPMENT' == $GLOBALS['tNG_debug_mode']) {
  306. $js_err = KT_escapeJS($ret_user);
  307. $js_devNotes = KT_escapeJS($ret_devel);
  308. $js_os = PHP_OS;
  309. $js_webserver = @$_SERVER['SERVER_SOFTWARE'];
  310. $js_servermodel = (!file_exists($this->relPath . 'adodb/')? 'PHP MySQL ': 'PHP ADODB ') . phpversion();
  311. $js_installation = KT_escapeJS(php_sapi_name());
  312. $js_extensions = KT_escapeJS(var_export(get_loaded_extensions(),true));
  313. $ret = $rethead . $ret;
  314. if ($ret_devel != '') {
  315. $ret .= '<div id="KT_tngdeverror"><label>Developer Details:</label><div>'.$ret_devel.'</div></div>';
  316. }
  317. $tmp = tNG_log::getResult('html', '_'.$uniq);
  318. $ret .= '<div id="KT_tngtrace"><label>tNG Execution Trace - <a href="#" onclick="document.getElementById(\'KT_tngtrace_details_'. $uniq .'\').style.display=(document.getElementById(\'KT_tngtrace_details_'. $uniq .'\').style.display!=\'block\'?\'block\':\'none\'); return false;">VIEW</a></label>' . $tmp . '</div>';
  319. }
  320. if ("" != $GLOBALS['tNG_debug_log_type'] && $ret_devel != '') {
  321. $txtContent .= "\r\n\r\nDeveloper Details:\r\n " . $ret_devel;
  322. $tmp = tNG_log::getResult('text', '_'.$uniq);
  323. $txtContent .= "\r\n\r\ntNG Execution Trace:\r\n" . $tmp;
  324. if ($GLOBALS['tNG_debug_log_type'] == 'logfile') {
  325. // log file
  326. $logFile = dirname(realpath(__FILE__)) . "/logs/" . date("Ym") . ".log";
  327. $f = @fopen($logFile, "a");
  328. if ($f) {
  329. if (flock($f, LOCK_EX)) { // do an exclusive lock
  330. fwrite($f, "=== BEGIN MESSAGE ===\r\n");
  331. fwrite($f, $txtContent);
  332. fwrite($f, "=== END MESSAGE ===\r\n");
  333. flock($f, LOCK_UN); // release the lock
  334. }
  335. fclose($f);
  336. }
  337. } else {
  338. $email = new KT_Email();
  339. //$email->setPriority("medium");
  340. $email->sendEmail($GLOBALS['tNG_email_host'], $GLOBALS['tNG_email_port'], $GLOBALS['tNG_email_user'], $GLOBALS['tNG_email_password'], $GLOBALS['tNG_debug_email_from'], $GLOBALS['tNG_debug_email_to'], "", "", $GLOBALS['tNG_debug_email_subject'], "ISO-8859-1", $txtContent, "");
  341. }
  342. }
  343. }
  344. return $ret;
  345. }
  346. /**
  347. * Save in session transaction errors
  348. * Only for PRO version
  349. * @return string saved error message from session
  350. * @access public
  351. */
  352. function getSavedErrorMsg() {
  353. $ret = '';
  354. if (isset($_SESSION['tng_errors']) && $_SESSION['tng_errors'] !='') {
  355. $ret = $_SESSION['tng_errors'];
  356. }
  357. return $ret;
  358. }
  359. /**
  360. * Save in session transaction errors
  361. * Only for PRO version
  362. * @param string error message;
  363. * @access public
  364. */
  365. function saveError($error) {
  366. if (!isset($_SESSION['tng_errors'])){
  367. $_SESSION['tng_errors'] = '';
  368. }
  369. $_SESSION['tng_errors'] .= $error;
  370. }
  371. /**
  372. * Returns the messages for the Login Page
  373. * @access public
  374. */
  375. function getLoginMsg() {
  376. $show = false;
  377. for ($i=0;$i<$this->n;$i++) {
  378. if ($this->tNGs[$i]->getTransactionType() == '_login' && !$this->tNGs[$i]->started) {
  379. $show = true;
  380. break;
  381. }
  382. }
  383. if ($show) {
  384. $info_resources = array('REG_ACTIVATE', 'REG_EMAIL', 'REG', 'ACTIVATED', 'FORGOT', 'DENIED', 'MAXTRIES', 'ACCOUNT_EXPIRE');
  385. $info_key = KT_getRealValue("GET", "info");
  386. if ($info_key != "") {
  387. if (in_array($info_key, $info_resources)) {
  388. $ret = '<div id="KT_tngdeverror">';
  389. $ret .= '<label>'.KT_getResource('LOGIN_MESSAGE_LABEL','tNG').'</label>';
  390. $ret .= '<div>' . KT_getResource('LOGIN_MESSAGE__'.$info_key, 'tNG') . '</div>';
  391. $ret .= '</div>';
  392. return $ret;
  393. }
  394. }
  395. }
  396. return '';
  397. }
  398. }
  399. ?>