PageRenderTime 60ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/phpfn6.php

https://github.com/fredd-for/emaus_tesoreria
PHP | 3668 lines | 3052 code | 282 blank | 334 comment | 622 complexity | 68c545dfd046f4f3af2cd28c10ce4523 MD5 | raw file
Possible License(s): LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * PHPMaker Common classes and functions
  4. * (C) 2002-2008 e.World Technology Limited. All rights reserved.
  5. */
  6. /**
  7. * Functions to init arrays
  8. */
  9. function ew_InitArray($iLen, $vValue) {
  10. if (function_exists('array_fill')) { // PHP 4 >= 4.2.0,
  11. return array_fill(0, $iLen, $vValue);
  12. } else {
  13. $aResult = array();
  14. for ($iCount = 0; $iCount < $iLen; $iCount++)
  15. $aResult[] = $vValue;
  16. return $aResult;
  17. }
  18. }
  19. function ew_Init2DArray($iLen1, $iLen2, $vValue) {
  20. return ew_InitArray($iLen1, ew_InitArray($iLen2, $vValue));
  21. }
  22. /**
  23. * Functions for converting encoding
  24. */
  25. function ew_ConvertToUtf8($str) {
  26. return ew_Convert(EW_ENCODING, "UTF-8", $str);
  27. }
  28. function ew_ConvertFromUtf8($str) {
  29. return ew_Convert("UTF-8", EW_ENCODING, $str);
  30. }
  31. function ew_Convert($from, $to, $str)
  32. {
  33. if ($from != "" && $to != "" && $from != $to) {
  34. if (function_exists("iconv")) {
  35. return iconv($from, $to, $str);
  36. } elseif (function_exists("mb_convert_encoding")) {
  37. return mb_convert_encoding($str, $to, $from);
  38. } else {
  39. return $str;
  40. }
  41. } else {
  42. return $str;
  43. }
  44. }
  45. /**
  46. * XML document class
  47. */
  48. class cXMLDocument {
  49. var $Encoding = EW_XML_ENCODING;
  50. var $RootTagName;
  51. var $RowTagName;
  52. var $XmlDoc;
  53. var $XmlTbl;
  54. var $XmlRow;
  55. var $XML = '';
  56. var $NullValue = 'NULL';
  57. function cXMLDocument($roottagname = 'table') {
  58. $this->RootTagName = $roottagname;
  59. if (EW_IS_PHP5) {
  60. $this->XmlDoc = new DOMDocument("1.0", $this->Encoding);
  61. $this->XmlTbl = $this->XmlDoc->createElement($this->RootTagName);
  62. $this->XmlDoc->appendChild($this->XmlTbl);
  63. }
  64. }
  65. function BeginRow($rowtagname = 'row') {
  66. $this->RowTagName = $rowtagname;
  67. if (EW_IS_PHP5) {
  68. $this->XmlRow = $this->XmlDoc->createElement($this->RowTagName);
  69. $this->XmlTbl->appendChild($this->XmlRow);
  70. } else {
  71. $this->XML .= "<$this->RowTagName>";
  72. }
  73. }
  74. function EndRow() {
  75. if (!EW_IS_PHP5) {
  76. $this->XML .= "</$this->RowTagName>";
  77. }
  78. }
  79. function AddField($name, $value) {
  80. if (is_null($value)) $value = $this->NullValue;
  81. if (EW_IS_PHP5) {
  82. $value = ew_ConvertToUtf8($value); // Convert to UTF-8
  83. $xmlfld = $this->XmlDoc->createElement($name);
  84. $this->XmlRow->appendChild($xmlfld);
  85. $xmlfld->appendChild($this->XmlDoc->createTextNode($value));
  86. } else {
  87. $value = ew_Convert(EW_ENCODING, EW_XML_ENCODING, $value); // Convert to output encoding
  88. $this->XML .= "<$name>" . htmlspecialchars($value) . "</$name>";
  89. }
  90. }
  91. function XML() {
  92. if (EW_IS_PHP5) {
  93. return $this->XmlDoc->saveXML();
  94. } else {
  95. return "<?xml version=\"1.0\"". (($this->Encoding <> "") ? " encoding=\"$this->Encoding\"" : "") .
  96. " ?>\n<$this->RootTagName>$this->XML</$this->RootTagName>";
  97. }
  98. }
  99. }
  100. /**
  101. * QueryString class
  102. */
  103. class cQueryString {
  104. var $values = array();
  105. var $Count;
  106. function cQueryString() {
  107. $ar = explode("&", ew_ServerVar("QUERY_STRING"));
  108. foreach ($ar as $p) {
  109. $arp = explode("=", $p);
  110. if (count($arp) == 2) $this->values[urldecode($arp[0])] = $arp[1];
  111. }
  112. $this->Count = count($this->values);
  113. }
  114. function getValue($name) {
  115. return (array_key_exists($name, $this->values)) ? $this->values[$name] : "";
  116. }
  117. function getUrlDecodedValue($name) {
  118. return urldecode($this->getValue($name));
  119. }
  120. function getRawUrlDecodedValue($name) {
  121. return rawurldecode($this->getValue($name));
  122. }
  123. function getConvertedValue($name) {
  124. return ew_ConvertFromUtf8($this->getRawUrlDecodedValue($name));
  125. }
  126. }
  127. /**
  128. * Email class
  129. */
  130. class cEmail {
  131. // Class properties
  132. var $Sender; // Sender
  133. var $Recipient; // Recipient
  134. var $Cc; // Cc
  135. var $Bcc; // Bcc
  136. var $Subject; // Subject
  137. var $Format; // Format
  138. var $Content; // Content
  139. function cEmail() {
  140. $this->Sender = "";
  141. $this->Recipient = "";
  142. $this->Cc = "";
  143. $this->Bcc = "";
  144. $this->Subject = "";
  145. $this->Format = "";
  146. $this->Content = "";
  147. }
  148. // Method to load email from template
  149. function Load($fn) {
  150. $fn = ew_ScriptFolder() . EW_PATH_DELIMITER . $fn;
  151. $sWrk = ew_ReadFile($fn); // Load text file content
  152. if ($sWrk <> "") {
  153. // Locate Header & Mail Content
  154. if (EW_IS_WINDOWS) {
  155. $i = strpos($sWrk, "\r\n\r\n");
  156. } else {
  157. $i = strpos($sWrk, "\n\n");
  158. if ($i === FALSE) $i = strpos($sWrk, "\r\n\r\n");
  159. }
  160. if ($i > 0) {
  161. $sHeader = substr($sWrk, 0, $i);
  162. $this->Content = trim(substr($sWrk, $i, strlen($sWrk)));
  163. if (EW_IS_WINDOWS) {
  164. $arrHeader = explode("\r\n", $sHeader);
  165. } else {
  166. $arrHeader = explode("\n", $sHeader);
  167. }
  168. for ($j = 0; $j < count($arrHeader); $j++) {
  169. $i = strpos($arrHeader[$j], ":");
  170. if ($i > 0) {
  171. $sName = trim(substr($arrHeader[$j], 0, $i));
  172. $sValue = trim(substr($arrHeader[$j], $i+1, strlen($arrHeader[$j])));
  173. switch (strtolower($sName))
  174. {
  175. case "subject":
  176. $this->Subject = $sValue;
  177. break;
  178. case "from":
  179. $this->Sender = $sValue;
  180. break;
  181. case "to":
  182. $this->Recipient = $sValue;
  183. break;
  184. case "cc":
  185. $this->Cc = $sValue;
  186. break;
  187. case "bcc":
  188. $this->Bcc = $sValue;
  189. break;
  190. case "format":
  191. $this->Format = $sValue;
  192. break;
  193. }
  194. }
  195. }
  196. }
  197. }
  198. }
  199. // Method to replace sender
  200. function ReplaceSender($ASender) {
  201. $this->Sender = str_replace('<!--$From-->', $ASender, $this->Sender);
  202. }
  203. // Method to replace recipient
  204. function ReplaceRecipient($ARecipient) {
  205. $this->Recipient = str_replace('<!--$To-->', $ARecipient, $this->Recipient);
  206. }
  207. // Method to add Cc email
  208. function AddCc($ACc) {
  209. if ($ACc <> "") {
  210. if ($this->Cc <> "") $this->Cc .= ";";
  211. $this->Cc .= $ACc;
  212. }
  213. }
  214. // Method to add Bcc email
  215. function AddBcc($ABcc) {
  216. if ($ABcc <> "") {
  217. if ($this->Bcc <> "") $this->Bcc .= ";";
  218. $this->Bcc .= $ABcc;
  219. }
  220. }
  221. // Method to replace subject
  222. function ReplaceSubject($ASubject) {
  223. $this->Subject = str_replace('<!--$Subject-->', $ASubject, $this->Subject);
  224. }
  225. // Method to replace content
  226. function ReplaceContent($Find, $ReplaceWith) {
  227. $this->Content = str_replace($Find, $ReplaceWith, $this->Content);
  228. }
  229. // Method to send email
  230. function Send() {
  231. return ew_SendEmail($this->Sender, $this->Recipient, $this->Cc, $this->Bcc,
  232. $this->Subject, $this->Content, $this->Format);
  233. }
  234. }
  235. /**
  236. * Pager item class
  237. */
  238. class cPagerItem {
  239. var $Start;
  240. var $Text;
  241. var $Enabled;
  242. }
  243. /**
  244. * Numeric pager class
  245. */
  246. class cNumericPager {
  247. var $Items = array();
  248. var $Count, $FromIndex, $ToIndex, $RecordCount, $PageSize, $Range;
  249. var $FirstButton, $PrevButton, $NextButton, $LastButton;
  250. var $ButtonCount = 0;
  251. var $Visible = TRUE;
  252. function cNumericPager($StartRec, $DisplayRecs, $TotalRecs, $RecRange)
  253. {
  254. $this->FirstButton = new cPagerItem;
  255. $this->PrevButton = new cPagerItem;
  256. $this->NextButton = new cPagerItem;
  257. $this->LastButton = new cPagerItem;
  258. $this->FromIndex = intval($StartRec);
  259. $this->PageSize = intval($DisplayRecs);
  260. $this->RecordCount = intval($TotalRecs);
  261. $this->Range = intval($RecRange);
  262. if ($this->PageSize == 0) return;
  263. if ($this->FromIndex > $this->RecordCount)
  264. $this->FromIndex = $this->RecordCount;
  265. $this->ToIndex = $this->FromIndex + $this->PageSize - 1;
  266. if ($this->ToIndex > $this->RecordCount)
  267. $this->ToIndex = $this->RecordCount;
  268. // setup
  269. $this->SetupNumericPager();
  270. // update button count
  271. if ($this->FirstButton->Enabled) $this->ButtonCount++;
  272. if ($this->PrevButton->Enabled) $this->ButtonCount++;
  273. if ($this->NextButton->Enabled) $this->ButtonCount++;
  274. if ($this->LastButton->Enabled) $this->ButtonCount++;
  275. $this->ButtonCount += count($this->Items);
  276. }
  277. // Add pager item
  278. function AddPagerItem($StartIndex, $Text, $Enabled)
  279. {
  280. $Item = new cPagerItem;
  281. $Item->Start = $StartIndex;
  282. $Item->Text = $Text;
  283. $Item->Enabled = $Enabled;
  284. $this->Items[] = $Item;
  285. }
  286. // Setup pager items
  287. function SetupNumericPager()
  288. {
  289. if ($this->RecordCount > $this->PageSize) {
  290. $Eof = ($this->RecordCount < ($this->FromIndex + $this->PageSize));
  291. $HasPrev = ($this->FromIndex > 1);
  292. // First Button
  293. $TempIndex = 1;
  294. $this->FirstButton->Start = $TempIndex;
  295. $this->FirstButton->Enabled = ($this->FromIndex > $TempIndex);
  296. // Prev Button
  297. $TempIndex = $this->FromIndex - $this->PageSize;
  298. if ($TempIndex < 1) $TempIndex = 1;
  299. $this->PrevButton->Start = $TempIndex;
  300. $this->PrevButton->Enabled = $HasPrev;
  301. // Page links
  302. if ($HasPrev || !$Eof) {
  303. $x = 1;
  304. $y = 1;
  305. $dx1 = intval(($this->FromIndex-1)/($this->PageSize*$this->Range))*$this->PageSize*$this->Range + 1;
  306. $dy1 = intval(($this->FromIndex-1)/($this->PageSize*$this->Range))*$this->Range + 1;
  307. if (($dx1+$this->PageSize*$this->Range-1) > $this->RecordCount) {
  308. $dx2 = intval($this->RecordCount/$this->PageSize)*$this->PageSize + 1;
  309. $dy2 = intval($this->RecordCount/$this->PageSize) + 1;
  310. } else {
  311. $dx2 = $dx1 + $this->PageSize*$this->Range - 1;
  312. $dy2 = $dy1 + $this->Range - 1;
  313. }
  314. while ($x <= $this->RecordCount) {
  315. if ($x >= $dx1 && $x <= $dx2) {
  316. $this->AddPagerItem($x, $y, $this->FromIndex<>$x);
  317. $x += $this->PageSize;
  318. $y++;
  319. } elseif ($x >= ($dx1-$this->PageSize*$this->Range) && $x <= ($dx2+$this->PageSize*$this->Range)) {
  320. if ($x+$this->Range*$this->PageSize < $this->RecordCount) {
  321. $this->AddPagerItem($x, $y . "-" . ($y+$this->Range-1), TRUE);
  322. } else {
  323. $ny = intval(($this->RecordCount-1)/$this->PageSize) + 1;
  324. if ($ny == $y) {
  325. $this->AddPagerItem($x, $y, TRUE);
  326. } else {
  327. $this->AddPagerItem($x, $y . "-" . $ny, TRUE);
  328. }
  329. }
  330. $x += $this->Range*$this->PageSize;
  331. $y += $this->Range;
  332. } else {
  333. $x += $this->Range*$this->PageSize;
  334. $y += $this->Range;
  335. }
  336. }
  337. }
  338. // Next Button
  339. $TempIndex = $this->FromIndex + $this->PageSize;
  340. $this->NextButton->Start = $TempIndex;
  341. $this->NextButton->Enabled = !$Eof;
  342. // Last Button
  343. $TempIndex = intval(($this->RecordCount-1)/$this->PageSize)*$this->PageSize + 1;
  344. $this->LastButton->Start = $TempIndex;
  345. $this->LastButton->Enabled = ($this->FromIndex < $TempIndex);
  346. }
  347. }
  348. }
  349. /**
  350. * PrevNext pager class
  351. */
  352. class cPrevNextPager {
  353. var $FirstButton, $PrevButton, $NextButton, $LastButton;
  354. var $CurrentPage, $PageCount, $FromIndex, $ToIndex, $RecordCount;
  355. var $Visible = TRUE;
  356. function cPrevNextPager($StartRec, $DisplayRecs, $TotalRecs)
  357. {
  358. $this->FirstButton = new cPagerItem;
  359. $this->PrevButton = new cPagerItem;
  360. $this->NextButton = new cPagerItem;
  361. $this->LastButton = new cPagerItem;
  362. $this->FromIndex = intval($StartRec);
  363. $this->PageSize = intval($DisplayRecs);
  364. $this->RecordCount = intval($TotalRecs);
  365. if ($this->PageSize == 0) return;
  366. $this->CurrentPage = intval(($this->FromIndex-1)/$this->PageSize) + 1;
  367. $this->PageCount = intval(($this->RecordCount-1)/$this->PageSize) + 1;
  368. if ($this->FromIndex > $this->RecordCount)
  369. $this->FromIndex = $this->RecordCount;
  370. $this->ToIndex = $this->FromIndex + $this->PageSize - 1;
  371. if ($this->ToIndex > $this->RecordCount)
  372. $this->ToIndex = $this->RecordCount;
  373. // First Button
  374. $TempIndex = 1;
  375. $this->FirstButton->Start = $TempIndex;
  376. $this->FirstButton->Enabled = ($TempIndex <> $this->FromIndex);
  377. // Prev Button
  378. $TempIndex = $this->FromIndex - $this->PageSize;
  379. if ($TempIndex < 1) $TempIndex = 1;
  380. $this->PrevButton->Start = $TempIndex;
  381. $this->PrevButton->Enabled = ($TempIndex <> $this->FromIndex);
  382. // Next Button
  383. $TempIndex = $this->FromIndex + $this->PageSize;
  384. if ($TempIndex > $this->RecordCount)
  385. $TempIndex = $this->FromIndex;
  386. $this->NextButton->Start = $TempIndex;
  387. $this->NextButton->Enabled = ($TempIndex <> $this->FromIndex);
  388. // Last Button
  389. $TempIndex = intval(($this->RecordCount-1)/$this->PageSize)*$this->PageSize + 1;
  390. $this->LastButton->Start = $TempIndex;
  391. $this->LastButton->Enabled = ($TempIndex <> $this->FromIndex);
  392. }
  393. }
  394. /**
  395. * Field class
  396. */
  397. class cField {
  398. var $TblVar; // Table var
  399. var $FldName; // Field name
  400. var $FldVar; // Field var
  401. var $FldExpression; // Field expression (used in sql)
  402. var $FldType; // Field type
  403. var $FldDataType; // PHPMaker Field type
  404. var $AdvancedSearch; // AdvancedSearch Object
  405. var $Upload; // Upload Object
  406. var $FldDateTimeFormat; // Date time format
  407. var $CssStyle; // CSS style
  408. var $CssClass; // CSS class
  409. var $ImageAlt; // Image alt
  410. var $ImageWidth = 0; // Image width
  411. var $ImageHeight = 0; // Image height
  412. var $ViewCustomAttributes; // View custom attributes
  413. var $EditCustomAttributes; // Edit custom attributes
  414. var $Count; // Count
  415. var $Total; // Total
  416. var $TrueValue = '1';
  417. var $FalseValue = '0';
  418. var $Visible = TRUE;
  419. function cField($tblvar, $fldvar, $fldname, $fldexpression, $fldtype, $flddtfmt, $upload = FALSE) {
  420. $this->TblVar = $tblvar;
  421. $this->FldVar = $fldvar;
  422. $this->FldName = $fldname;
  423. $this->FldExpression = $fldexpression;
  424. $this->FldType = $fldtype;
  425. $this->FldDataType = ew_FieldDataType($fldtype);
  426. $this->FldDateTimeFormat = $flddtfmt;
  427. $this->AdvancedSearch = new cAdvancedSearch();
  428. if ($upload)
  429. $this->Upload = new cUpload($this->TblVar, $this->FldVar);
  430. }
  431. // View Attributes
  432. function ViewAttributes() {
  433. $sAtt = "";
  434. if (trim($this->CssStyle) <> "") {
  435. $sAtt .= " style=\"" . trim($this->CssStyle) . "\"";
  436. }
  437. if (trim($this->CssClass) <> "") {
  438. $sAtt .= " class=\"" . trim($this->CssClass) . "\"";
  439. }
  440. if (trim($this->ImageAlt) <> "") {
  441. $sAtt .= " alt=\"" . trim($this->ImageAlt) . "\"";
  442. }
  443. if (intval($this->ImageWidth) > 0) {
  444. $sAtt .= " width=\"" . intval($this->ImageWidth) . "\"";
  445. }
  446. if (intval($this->ImageHeight) > 0) {
  447. $sAtt .= " height=\"" . intval($this->ImageHeight) . "\"";
  448. }
  449. if (trim($this->ViewCustomAttributes) <> "") {
  450. $sAtt .= " " . trim($this->ViewCustomAttributes);
  451. }
  452. return $sAtt;
  453. }
  454. // Edit Attributes
  455. function EditAttributes() {
  456. $sAtt = "";
  457. if (trim($this->CssStyle) <> "") {
  458. $sAtt .= " style=\"" . trim($this->CssStyle) . "\"";
  459. }
  460. if (trim($this->CssClass) <> "") {
  461. $sAtt .= " class=\"" . trim($this->CssClass) . "\"";
  462. }
  463. if (trim($this->EditCustomAttributes) <> "") {
  464. $sAtt .= " " . trim($this->EditCustomAttributes);
  465. }
  466. return $sAtt;
  467. }
  468. var $CustomMsg = ""; // Custom message
  469. var $RowAttributes = ""; // Row attributes
  470. var $CellCssClass = ""; // Cell CSS class
  471. var $CellCssStyle = ""; // Cell CSS style
  472. var $CellCustomAttributes = ""; // Cell custom attributes
  473. // Cell Attributes
  474. function CellAttributes() {
  475. $sAtt = "";
  476. if (trim($this->CellCssStyle) <> "") {
  477. $sAtt .= " style=\"" . trim($this->CellCssStyle) . "\"";
  478. }
  479. if (trim($this->CellCssClass) <> "") {
  480. $sAtt .= " class=\"" . trim($this->CellCssClass) . "\"";
  481. }
  482. if (trim($this->CellCustomAttributes) <> "") {
  483. $sAtt .= " " . trim($this->CellCustomAttributes);
  484. }
  485. return $sAtt;
  486. }
  487. // Sort Attributes
  488. function getSort() {
  489. return @$_SESSION[EW_PROJECT_NAME . "_" . $this->TblVar . "_" . EW_TABLE_SORT . "_" . $this->FldVar];
  490. }
  491. function setSort($v) {
  492. if (@$_SESSION[EW_PROJECT_NAME . "_" . $this->TblVar . "_" . EW_TABLE_SORT . "_" . $this->FldVar] <> $v) {
  493. $_SESSION[EW_PROJECT_NAME . "_" . $this->TblVar . "_" . EW_TABLE_SORT . "_" . $this->FldVar] = $v;
  494. }
  495. }
  496. function ReverseSort() {
  497. return ($this->getSort() == "ASC") ? "DESC" : "ASC";
  498. }
  499. var $MultiUpdate; // Multi update
  500. var $OldValue; // Old Value
  501. var $ConfirmValue; // Confirm Value
  502. var $CurrentValue; // Current value
  503. var $ViewValue; // View value
  504. var $EditValue; // Edit value
  505. var $EditValue2; // Edit value 2 (search)
  506. var $HrefValue; // Href value
  507. var $HrefValue2; // Href value 2 (confirm page UPLOAD control)
  508. // If ew_Empty(ViewValue) Then
  509. // Return " "
  510. // Else
  511. // Dim Result As String = Convert.ToString(ViewValue)
  512. // Dim Result2 As String = Regex.Replace(Result, "<[^>]*>", String.Empty) ' Remove HTML tags
  513. // Return IIf(Result2.Trim.Equals(String.Empty), " ", Result)
  514. // End If
  515. // List view value
  516. function ListViewValue() {
  517. $value = strval($this->ViewValue);
  518. if (trim($value) <> "") {
  519. $value2 = preg_replace('/<[^>]*>/', '', $value);
  520. return (trim($value2) <> "") ? $this->ViewValue : "&nbsp;";
  521. } else {
  522. return "&nbsp;";
  523. }
  524. }
  525. // Export Value
  526. function ExportValue($Export, $Original) {
  527. $ExportValue = ($Original) ? $this->CurrentValue : $this->ViewValue;
  528. if ($Export == "xml" && is_null($ExportValue))
  529. $ExportValue = "<Null>";
  530. return $ExportValue;
  531. }
  532. // Form value
  533. var $FormValue;
  534. function setFormValue($v) {
  535. $this->FormValue = ew_StripSlashes($v);
  536. if (is_array($this->FormValue))
  537. $this->FormValue = implode(",", $this->FormValue);
  538. $this->CurrentValue = $this->FormValue;
  539. }
  540. // QueryString value
  541. var $QueryStringValue;
  542. function setQueryStringValue($v) {
  543. $this->QueryStringValue = ew_StripSlashes($v);
  544. $this->CurrentValue = $this->QueryStringValue;
  545. }
  546. // Database Value
  547. var $DbValue;
  548. function setDbValue($v) {
  549. $this->DbValue = $v;
  550. $this->CurrentValue = $this->DbValue;
  551. }
  552. // Set database value with error default
  553. function SetDbValueDef($value, $default) {
  554. switch ($this->FldType) {
  555. case 2:
  556. case 3:
  557. case 16:
  558. case 17:
  559. case 18: // Int
  560. $value = trim($value);
  561. $DbValue = (is_numeric($value)) ? intval($value) : $default;
  562. break;
  563. case 19:
  564. case 20:
  565. case 21: // Big Int
  566. $value = trim($value);
  567. $DbValue = (is_numeric($value)) ? $value : $default;
  568. break;
  569. case 5:
  570. case 6:
  571. case 14:
  572. case 131: // Double
  573. case 4: // Single
  574. $value = trim($value);
  575. $value = ew_StrToFloat($value);
  576. $DbValue = (is_float($value)) ? $value : $default;
  577. break;
  578. case 7:
  579. case 133:
  580. case 134:
  581. case 135: //Date
  582. case 201:
  583. case 203:
  584. case 129:
  585. case 130:
  586. case 200:
  587. case 202: // String
  588. $value = trim($value);
  589. $DbValue = ($value == "") ? $default : $value;
  590. break;
  591. case 128:
  592. case 204:
  593. case 205: // Binary
  594. $DbValue = (is_null($value)) ? $default : $value;
  595. break;
  596. case 72: // GUID
  597. $value = trim($value);
  598. $DbValue = ($value <> "" && ew_CheckGUID($value)) ? $value : $default;
  599. break;
  600. default:
  601. $DbValue = $value;
  602. }
  603. $this->setDbValue($DbValue);
  604. }
  605. // Session Value
  606. function getSessionValue() {
  607. return @$_SESSION[EW_PROJECT_NAME . "_" . $this->TblVar . "_" . $this->FldVar . "_SessionValue"];
  608. }
  609. function setSessionValue($v) {
  610. $_SESSION[EW_PROJECT_NAME . "_" . $this->TblVar . "_" . $this->FldVar . "_SessionValue"] = $v;
  611. }
  612. }
  613. /**
  614. * List option collection class
  615. */
  616. class cListOptions {
  617. var $Items = array();
  618. // Add and return a new option
  619. function Add() {
  620. $this->Items[] = new cListOption();
  621. return $this->Items[count($this->Items)-1];
  622. }
  623. }
  624. /**
  625. * List option class
  626. */
  627. class cListOption {
  628. var $Visible = TRUE;
  629. var $HeaderCellHtml = "";
  630. var $FooterCellHtml = "";
  631. var $BodyCellHtml = "";
  632. var $MultiColumnLinkHtml = "";
  633. }
  634. ?>
  635. <?php
  636. /**
  637. * Advanced Search class
  638. */
  639. class cAdvancedSearch {
  640. var $SearchValue; // Search value
  641. var $SearchOperator; // Search operator
  642. var $SearchCondition; // Search condition
  643. var $SearchValue2; // Search value 2
  644. var $SearchOperator2; // Search operator 2
  645. }
  646. ?>
  647. <?php
  648. /**
  649. * Upload class
  650. */
  651. class cUpload {
  652. var $Index = 0; // Index to handle multiple form elements
  653. var $TblVar; // Table variable
  654. var $FldVar; // Field variable
  655. var $Message; // Error message
  656. var $DbValue; // Value from database
  657. var $Value = NULL; // Upload value
  658. var $Action; // Upload action
  659. var $UploadPath; // Upload path
  660. var $FileName; // Upload file name
  661. var $FileSize; // Upload file size
  662. var $ContentType; // File content type
  663. var $ImageWidth; // Image width
  664. var $ImageHeight; // Image height
  665. // Class initialize
  666. function cUpload($TblVar, $FldVar, $Binary = FALSE) {
  667. $this->TblVar = $TblVar;
  668. $this->FldVar = $FldVar;
  669. }
  670. function getSessionID() {
  671. return EW_PROJECT_NAME . "_" . $this->TblVar . "_" . $this->FldVar . "_" . $this->Index;
  672. }
  673. // Save Db value to Session
  674. function SaveDbToSession() {
  675. $sSessionID = $this->getSessionID();
  676. $_SESSION[$sSessionID . "_DbValue"] = $this->DbValue;
  677. }
  678. // Restore Db value from Session
  679. function RestoreDbFromSession() {
  680. $sSessionID = $this->getSessionID();
  681. $this->DbValue = @$_SESSION[$sSessionID . "_DbValue"];
  682. }
  683. // Remove Db value from Session
  684. function RemoveDbFromSession() {
  685. $sSessionID = $this->getSessionID();
  686. unset($_SESSION[$sSessionID . "_DbValue"]);
  687. }
  688. // Save Upload values to Session
  689. function SaveToSession() {
  690. $sSessionID = $this->getSessionID();
  691. $_SESSION[$sSessionID . "_Action"] = $this->Action;
  692. $_SESSION[$sSessionID . "_FileSize"] = $this->FileSize;
  693. $_SESSION[$sSessionID . "_FileName"] = $this->FileName;
  694. $_SESSION[$sSessionID . "_ContentType"] = $this->ContentType;
  695. $_SESSION[$sSessionID . "_ImageWidth"] = $this->ImageWidth;
  696. $_SESSION[$sSessionID . "_ImageHeight"] = $this->ImageHeight;
  697. $_SESSION[$sSessionID . "_Value"] = $this->Value;
  698. }
  699. // Restore Upload values from Session
  700. function RestoreFromSession() {
  701. $sSessionID = $this->getSessionID();
  702. $this->Action = @$_SESSION[$sSessionID . "_Action"];
  703. $this->FileSize = @$_SESSION[$sSessionID . "_FileSize"];
  704. $this->FileName = @$_SESSION[$sSessionID . "_FileName"];
  705. $this->ContentType = @$_SESSION[$sSessionID . "_ContentType"];
  706. $this->ImageWidth = @$_SESSION[$sSessionID . "_ImageWidth"];
  707. $this->ImageHeight = @$_SESSION[$sSessionID . "_ImageHeight"];
  708. $this->Value = @$_SESSION[$sSessionID . "_Value"];
  709. }
  710. // Remove Upload values from Session
  711. function RemoveFromSession() {
  712. $sSessionID = $this->getSessionID();
  713. unset($_SESSION[$sSessionID . "_Action"]);
  714. unset($_SESSION[$sSessionID . "_FileSize"]);
  715. unset($_SESSION[$sSessionID . "_FileName"]);
  716. unset($_SESSION[$sSessionID . "_ContentType"]);
  717. unset($_SESSION[$sSessionID . "_ImageWidth"]);
  718. unset($_SESSION[$sSessionID . "_ImageHeight"]);
  719. unset($_SESSION[$sSessionID . "_Value"]);
  720. }
  721. // function to check the file type of the uploaded file
  722. function UploadAllowedFileExt($filename) {
  723. return ew_CheckFileType($filename);
  724. }
  725. // Get upload file
  726. function UploadFile() {
  727. global $objForm;
  728. $this->Value = NULL; // Reset first
  729. $gsFldVar = $this->FldVar;
  730. $gsFldVarAction = "a" . substr($gsFldVar, 1);
  731. $gsFldVarWidth = "wd" . substr($gsFldVar, 1);
  732. $gsFldVarHeight = "ht" . substr($gsFldVar, 1);
  733. // Get action
  734. $this->Action = $objForm->GetValue($gsFldVarAction);
  735. // Get and check the upload file size
  736. $this->FileSize = $objForm->GetUploadFileSize($gsFldVar);
  737. // Get and check the upload file type
  738. $this->FileName = $objForm->GetUploadFileName($gsFldVar);
  739. // Get upload file content type
  740. $this->ContentType = $objForm->GetUploadFileContentType($gsFldVar);
  741. // Get upload value
  742. $this->Value = $objForm->GetUploadFileData($gsFldVar);
  743. // Get image width and height
  744. $this->ImageWidth = $objForm->GetUploadImageWidth($gsFldVar);
  745. $this->ImageHeight = $objForm->GetUploadImageHeight($gsFldVar);
  746. if ($this->ImageWidth < 0 || $this->ImageHeight < 0) {
  747. $this->ImageWidth = $objForm->GetValue($gsFldVarWidth);
  748. $this->ImageHeight = $objForm->GetValue($gsFldVarHeight);
  749. }
  750. return TRUE; // Normal return
  751. }
  752. // Resize image
  753. function Resize($width, $height, $quality) {
  754. if (!is_null($this->Value)) {
  755. $wrkwidth = $width;
  756. $wrkheight = $height;
  757. if (ew_ResizeBinary($this->Value, $wrkwidth, $wrkheight, $quality)) { // P6
  758. $this->ImageWidth = $wrkwidth;
  759. $this->ImageHeight = $wrkheight;
  760. $this->FileSize = strlen($this->Value);
  761. }
  762. }
  763. }
  764. // Save uploaded data to file (Path relative to application root)
  765. function SaveToFile($Path, $NewFileName, $OverWrite) {
  766. if (!is_null($this->Value)) {
  767. $Path = ew_UploadPathEx(TRUE, $Path);
  768. if (trim(strval($NewFileName)) == "") $NewFileName = $this->FileName;
  769. if ($OverWrite) {
  770. return ew_SaveFile($Path, $NewFileName, $this->Value);
  771. } else {
  772. return ew_SaveFile($Path, ew_UploadFileNameEx($Path, $NewFileName), $this->Value);
  773. }
  774. }
  775. return FALSE;
  776. }
  777. // Resize and save uploaded data to file (Path relative to application root)
  778. function ResizeAndSaveToFile($Width, $Height, $Quality, $Path, $NewFileName, $OverWrite) {
  779. $bResult = FALSE;
  780. if (!is_null($this->Value)) {
  781. $OldValue = $this->Value;
  782. $this->Resize($Width, $Height, $Quality);
  783. $bResult = $this->SaveToFile($Path, $NewFileName, $OverWrite);
  784. $this->Value = $OldValue;
  785. }
  786. return $bResult;
  787. }
  788. }
  789. ?>
  790. <?php
  791. /**
  792. * Advanced Security class
  793. */
  794. class cAdvancedSecurity {
  795. var $UserLevel = array(); // All User Levels
  796. var $UserLevelPriv = array(); // All User Level permissions
  797. var $UserLevelID = array(); // User Level ID array
  798. var $UserID = array(); // User ID array
  799. var $CurrentUserLevelID;
  800. var $CurrentUserLevel; // Permissions
  801. var $CurrentUserID;
  802. var $CurrentParentUserID;
  803. // Class Initialize
  804. function cAdvancedSecurity() {
  805. // Init User Level
  806. $this->CurrentUserLevelID = $this->SessionUserLevelID();
  807. if (is_numeric($this->CurrentUserLevelID) && intval($this->CurrentUserLevelID) >= -1) {
  808. $this->UserLevelID[] = $this->CurrentUserLevelID;
  809. }
  810. // Init User ID
  811. $this->CurrentUserID = $this->SessionUserID();
  812. $this->CurrentParentUserID = $this->SessionParentUserID();
  813. // Load user level (for TablePermission_Loading event)
  814. $this->LoadUserLevel();
  815. }
  816. // Session user id
  817. function SessionUserID() {
  818. return strval(@$_SESSION[EW_SESSION_USER_ID]);
  819. }
  820. function setSessionUserID($v) {
  821. $_SESSION[EW_SESSION_USER_ID] = $v;
  822. $this->CurrentUserID = $v;
  823. }
  824. // Session parent user id
  825. function SessionParentUserID() {
  826. return strval(@$_SESSION[EW_SESSION_PARENT_USER_ID]);
  827. }
  828. function setSessionParentUserID($v) {
  829. $_SESSION[EW_SESSION_PARENT_USER_ID] = $v;
  830. $this->CurrentParentUserID = $v;
  831. }
  832. // Session user level id
  833. function SessionUserLevelID() {
  834. return @$_SESSION[EW_SESSION_USER_LEVEL_ID];
  835. }
  836. function setSessionUserLevelID($v) {
  837. $_SESSION[EW_SESSION_USER_LEVEL_ID] = $v;
  838. $this->CurrentUserLevelID = $v;
  839. }
  840. // Session user level value
  841. function SessionUserLevel() {
  842. return @$_SESSION[EW_SESSION_USER_LEVEL];
  843. }
  844. function setSessionUserLevel($v) {
  845. $_SESSION[EW_SESSION_USER_LEVEL] = $v;
  846. $this->CurrentUserLevel = $v;
  847. }
  848. // Current user name
  849. function getCurrentUserName() {
  850. return strval(@$_SESSION[EW_SESSION_USER_NAME]);
  851. }
  852. function setCurrentUserName($v) {
  853. $_SESSION[EW_SESSION_USER_NAME] = $v;
  854. }
  855. function CurrentUserName() {
  856. return $this->getCurrentUserName();
  857. }
  858. // Current User ID
  859. function CurrentUserID() {
  860. return $this->CurrentUserID;
  861. }
  862. // Current parent User ID
  863. function CurrentParentUserID() {
  864. return $this->CurrentParentUserID;
  865. }
  866. // Current User Level id
  867. function CurrentUserLevelID() {
  868. return $this->CurrentUserLevelID;
  869. }
  870. // Current User Level value
  871. function CurrentUserLevel() {
  872. return $this->CurrentUserLevel;
  873. }
  874. // Can add
  875. function CanAdd() {
  876. return (($this->CurrentUserLevel & EW_ALLOW_ADD) == EW_ALLOW_ADD);
  877. }
  878. function setCanAdd($b) {
  879. if ($b) {
  880. $this->CurrentUserLevel = ($this->CurrentUserLevel | EW_ALLOW_ADD);
  881. } else {
  882. $this->CurrentUserLevel = ($this->CurrentUserLevel & (~ EW_ALLOW_ADD));
  883. }
  884. }
  885. // Can delete
  886. function CanDelete() {
  887. return (($this->CurrentUserLevel & EW_ALLOW_DELETE) == EW_ALLOW_DELETE);
  888. }
  889. function setCanDelete($b) {
  890. if ($b) {
  891. $this->CurrentUserLevel = ($this->CurrentUserLevel | EW_ALLOW_DELETE);
  892. } else {
  893. $this->CurrentUserLevel = ($this->CurrentUserLevel & (~ EW_ALLOW_DELETE));
  894. }
  895. }
  896. // Can edit
  897. function CanEdit() {
  898. return (($this->CurrentUserLevel & EW_ALLOW_EDIT) == EW_ALLOW_EDIT);
  899. }
  900. function setCanEdit($b) {
  901. if ($b) {
  902. $this->CurrentUserLevel = ($this->CurrentUserLevel | EW_ALLOW_EDIT);
  903. } else {
  904. $this->CurrentUserLevel = ($this->CurrentUserLevel & (~ EW_ALLOW_EDIT));
  905. }
  906. }
  907. // Can view
  908. function CanView() {
  909. return (($this->CurrentUserLevel & EW_ALLOW_VIEW) == EW_ALLOW_VIEW);
  910. }
  911. function setCanView($b) {
  912. if ($b) {
  913. $this->CurrentUserLevel = ($this->CurrentUserLevel | EW_ALLOW_VIEW);
  914. } else {
  915. $this->CurrentUserLevel = ($this->CurrentUserLevel & (~ EW_ALLOW_VIEW));
  916. }
  917. }
  918. // Can list
  919. function CanList() {
  920. return (($this->CurrentUserLevel & EW_ALLOW_LIST) == EW_ALLOW_LIST);
  921. }
  922. function setCanList($b) {
  923. if ($b) {
  924. $this->CurrentUserLevel = ($this->CurrentUserLevel | EW_ALLOW_LIST);
  925. } else {
  926. $this->CurrentUserLevel = ($this->CurrentUserLevel & (~ EW_ALLOW_LIST));
  927. }
  928. }
  929. // Can report
  930. function CanReport() {
  931. return (($this->CurrentUserLevel & EW_ALLOW_REPORT) == EW_ALLOW_REPORT);
  932. }
  933. function setCanReport($b) {
  934. if ($b) {
  935. $this->CurrentUserLevel = ($this->CurrentUserLevel | EW_ALLOW_REPORT);
  936. } else {
  937. $this->CurrentUserLevel = ($this->CurrentUserLevel & (~ EW_ALLOW_REPORT));
  938. }
  939. }
  940. // Can search
  941. function CanSearch() {
  942. return (($this->CurrentUserLevel & EW_ALLOW_SEARCH) == EW_ALLOW_SEARCH);
  943. }
  944. function setCanSearch($b) {
  945. if ($b) {
  946. $this->CurrentUserLevel = ($this->CurrentUserLevel | EW_ALLOW_SEARCH);
  947. } else {
  948. $this->CurrentUserLevel = ($this->CurrentUserLevel & (~ EW_ALLOW_SEARCH));
  949. }
  950. }
  951. // Can admin
  952. function CanAdmin() {
  953. return (($this->CurrentUserLevel & EW_ALLOW_ADMIN) == EW_ALLOW_ADMIN);
  954. }
  955. function setCanAdmin($b) {
  956. if ($b) {
  957. $this->CurrentUserLevel = ($this->CurrentUserLevel | EW_ALLOW_ADMIN);
  958. } else {
  959. $this->CurrentUserLevel = ($this->CurrentUserLevel & (~ EW_ALLOW_ADMIN));
  960. }
  961. }
  962. // Last url
  963. function LastUrl() {
  964. return @$_COOKIE[EW_PROJECT_NAME]['LastUrl'];
  965. }
  966. // Save last url
  967. function SaveLastUrl() {
  968. $s = ew_ServerVar("SCRIPT_NAME");
  969. $q = ew_ServerVar("QUERY_STRING");
  970. if ($q <> "") $s .= "?" . $q;
  971. if ($this->LastUrl() == $s) $s = "";
  972. @setcookie(EW_PROJECT_NAME . '[LastUrl]', $s);
  973. }
  974. // Auto login
  975. function AutoLogin() {
  976. if (@$_COOKIE[EW_PROJECT_NAME]['AutoLogin'] == "autologin") {
  977. $usr = @$_COOKIE[EW_PROJECT_NAME]['UserName'];
  978. $pwd = @$_COOKIE[EW_PROJECT_NAME]['Password'];
  979. $pwd = TEAdecrypt($pwd, EW_RANDOM_KEY);
  980. $AutoLogin = $this->ValidateUser($usr, $pwd);
  981. } else {
  982. $AutoLogin = FALSE;
  983. }
  984. return $AutoLogin;
  985. }
  986. // Validate user
  987. function ValidateUser($usr, $pwd) {
  988. global $conn;
  989. global $usuario;
  990. $ValidateUser = FALSE;
  991. // Check hard coded admin first
  992. if (EW_CASE_SENSITIVE_PASSWORD) {
  993. $ValidateUser = (EW_ADMIN_USER_NAME == $usr && EW_ADMIN_PASSWORD == $pwd);
  994. } else {
  995. $ValidateUser = (strtolower(EW_ADMIN_USER_NAME) == strtolower($usr) &&
  996. strtolower(EW_ADMIN_PASSWORD) == strtolower($pwd));
  997. }
  998. if ($ValidateUser) {
  999. $_SESSION[EW_SESSION_STATUS] = "login";
  1000. $_SESSION[EW_SESSION_SYS_ADMIN] = 1; // System Administrator
  1001. $this->setCurrentUserName("Administrator"); // Load user name
  1002. $this->setSessionUserLevelID(-1); // System Administrator
  1003. $this->SetUpUserLevel();
  1004. }
  1005. // Check other users
  1006. if (!$ValidateUser) {
  1007. $sFilter = "(`login` = '" . ew_AdjustSql($usr) . "')";
  1008. // Set up filter (Sql Where Clause) and get Return SQL
  1009. // SQL constructor in <UseTable> class, <UserTable>info.php
  1010. $sSql = $usuario->GetSQL($sFilter, "");
  1011. if ($rs = $conn->Execute($sSql)) {
  1012. if (!$rs->EOF) {
  1013. if (EW_CASE_SENSITIVE_PASSWORD) {
  1014. if (EW_MD5_PASSWORD) {
  1015. $ValidateUser = ($rs->fields('password') == md5($pwd));
  1016. } else {
  1017. $ValidateUser = ($rs->fields('password') == $pwd);
  1018. }
  1019. } else {
  1020. if (EW_MD5_PASSWORD) {
  1021. $ValidateUser = ($rs->fields('password') == md5(strtolower($pwd)));
  1022. } else {
  1023. $ValidateUser = (strtolower($rs->fields('password')) == strtolower($pwd));
  1024. }
  1025. }
  1026. if ($ValidateUser) {
  1027. $_SESSION[EW_SESSION_STATUS] = "login";
  1028. $_SESSION[EW_SESSION_SYS_ADMIN] = 0; // Non System Administrator
  1029. $this->setCurrentUserName($rs->fields('login')); // Load user name
  1030. if (is_null($rs->fields('idRol'))) {
  1031. $this->setSessionUserLevelID(0);
  1032. } else {
  1033. $this->setSessionUserLevelID(intval($rs->fields('idRol'))); // Load User Level
  1034. }
  1035. $this->SetUpUserLevel();
  1036. // Call User Validated event
  1037. $this->User_Validated($rs);
  1038. }
  1039. }
  1040. $rs->Close();
  1041. }
  1042. }
  1043. return $ValidateUser;
  1044. }
  1045. // Dynamic User Level security
  1046. // Get User Level settings from database
  1047. function SetUpUserLevel() {
  1048. $this->SetUpUserLevelEx(); // Load all user levels
  1049. // User Level loaded event
  1050. $this->UserLevel_Loaded();
  1051. // Save the User Level to session variable
  1052. $this->SaveUserLevel();
  1053. }
  1054. // Get all User Level settings from database
  1055. function SetUpUserLevelEx() {
  1056. global $conn;
  1057. // Get the User Level definitions
  1058. $sSql = "SELECT " . EW_USER_LEVEL_ID_FIELD . ", " . EW_USER_LEVEL_NAME_FIELD . " FROM " . EW_USER_LEVEL_TABLE;
  1059. if ($rs = $conn->Execute($sSql)) {
  1060. $this->UserLevel = $rs->GetRows();
  1061. $rs->Close();
  1062. }
  1063. // Get the User Level privileges
  1064. $sSql = "SELECT " . EW_USER_LEVEL_PRIV_TABLE_NAME_FIELD . ", " . EW_USER_LEVEL_PRIV_USER_LEVEL_ID_FIELD . ", " . EW_USER_LEVEL_PRIV_PRIV_FIELD . " FROM " . EW_USER_LEVEL_PRIV_TABLE;
  1065. if ($rs = $conn->Execute($sSql)) {
  1066. $this->UserLevelPriv = $rs->GetRows();
  1067. $rs->Close();
  1068. }
  1069. }
  1070. // Add user permission
  1071. function AddUserPermission($UserLevelName, $TableName, $UserPermission) {
  1072. // Get user level id from user name
  1073. $UserLevelID = "";
  1074. if (is_array($this->UserLevel)) {
  1075. foreach ($this->UserLevel as $row) {
  1076. list($levelid, $name) = $row;
  1077. if (strval($UserLevelName) == strval($name)) {
  1078. $UserLevelID = $levelid;
  1079. break;
  1080. }
  1081. }
  1082. }
  1083. if (is_array($this->UserLevelPriv) && $UserLevelID <> "") {
  1084. $cnt = count($this->UserLevelPriv);
  1085. for ($i = 0; $i < $cnt; $i++) {
  1086. list($table, $levelid, $priv) = $this->UserLevelPriv[$i];
  1087. if (strtolower($table) == strtolower($TableName) && strval($levelid) == strval($UserLevelID)) {
  1088. $this->UserLevelPriv[$i][2] = $priv | $UserPermission; // Add permission
  1089. break;
  1090. }
  1091. }
  1092. }
  1093. }
  1094. // Delete user permission
  1095. function DeleteUserPermission($UserLevelName, $TableName, $UserPermission) {
  1096. // Get user level id from user name
  1097. $UserLevelID = "";
  1098. if (is_array($this->UserLevel)) {
  1099. foreach ($this->UserLevel as $row) {
  1100. list($levelid, $name) = $row;
  1101. if (strval($UserLevelName) == strval($name)) {
  1102. $UserLevelID = $levelid;
  1103. break;
  1104. }
  1105. }
  1106. }
  1107. if (is_array($this->UserLevelPriv) && $UserLevelID <> "") {
  1108. $cnt = count($this->UserLevelPriv);
  1109. for ($i = 0; $i < $cnt; $i++) {
  1110. list($table, $levelid, $priv) = $this->UserLevelPriv[$i];
  1111. if (strtolower($table) == strtolower($TableName) && strval($levelid) == strval($UserLevelID)) {
  1112. $this->UserLevelPriv[$i][2] = $priv & (127 - $UserPermission); // Remove permission
  1113. break;
  1114. }
  1115. }
  1116. }
  1117. }
  1118. // Load current user level
  1119. function LoadCurrentUserLevel($Table) {
  1120. $this->LoadUserLevel();
  1121. $this->setSessionUserLevel($this->CurrentUserLevelPriv($Table));
  1122. }
  1123. // Get current user privilege
  1124. function CurrentUserLevelPriv($TableName) {
  1125. if ($this->IsLoggedIn()) {
  1126. $Priv= 0;
  1127. foreach ($this->UserLevelID as $UserLevelID)
  1128. $Priv |= $this->GetUserLevelPrivEx($TableName, $UserLevelID);
  1129. return $Priv;
  1130. } else {
  1131. return 0;
  1132. }
  1133. }
  1134. // Get user level ID by user level name
  1135. function GetUserLevelID($UserLevelName) {
  1136. if (strval($UserLevelName) == "Administrator") {
  1137. return -1;
  1138. } elseif ($UserLevelName <> "") {
  1139. if (is_array($this->UserLevel)) {
  1140. foreach ($this->UserLevel as $row) {
  1141. list($levelid, $name) = $row;
  1142. if (strval($name) == strval($UserLevelName))
  1143. return $levelid;
  1144. }
  1145. }
  1146. }
  1147. return -2;
  1148. }
  1149. // Add user level (for use with UserLevel_Loading event)
  1150. function AddUserLevel($UserLevelName) {
  1151. if (strval($UserLevelName) == "") return;
  1152. $UserLevelID = $this->GetUserLevelID($UserLevelName);
  1153. if (!is_numeric($UserLevelID)) return;
  1154. if ($UserLevelID < -1) return;
  1155. if (!in_array($UserLevelID, $this->UserLevelID))
  1156. $this->UserLevelID[] = $UserLevelID;
  1157. }
  1158. // Delete user level (for use with UserLevel_Loading event)
  1159. function DeleteUserLevel($UserLevelName) {
  1160. if (strval($UserLevelName) == "") return;
  1161. $UserLevelID = $this->GetUserLevelID($UserLevelName);
  1162. if (!is_numeric($UserLevelID)) return;
  1163. if ($UserLevelID < -1) return;
  1164. $cnt = count($this->UserLevelID);
  1165. for ($i = 0; $i < $cnt; $i++) {
  1166. if ($this->UserLevelID[$i] == $UserLevelID) {
  1167. unset($this->UserLevelID[$i]);
  1168. break;
  1169. }
  1170. }
  1171. }
  1172. // User level list
  1173. function UserLevelList() {
  1174. return implode(", ", $this->UserLevelID);
  1175. }
  1176. // User level name list
  1177. function UserLevelNameList() {
  1178. $list = "";
  1179. foreach ($this->UserLevelID as $UserLevelID) {
  1180. if ($list <> "") $lList .= ", ";
  1181. $list .= ew_QuotedValue($this->GetUserLevelName($UserLevelID), EW_DATATYPE_STRING);
  1182. }
  1183. return $list;
  1184. }
  1185. // Get user privilege based on table name and User Level
  1186. function GetUserLevelPrivEx($TableName, $UserLevelID) {
  1187. if (strval($UserLevelID) == "-1") { // System Administrator
  1188. if (defined("EW_USER_LEVEL_COMPAT")) {
  1189. return 31; // Use old User Level values
  1190. } else {
  1191. return 127; // Use new User Level values (separate View/Search)
  1192. }
  1193. } elseif ($UserLevelID >= 0) {
  1194. if (is_array($this->UserLevelPriv)) {
  1195. foreach ($this->UserLevelPriv as $row) {
  1196. list($table, $levelid, $priv) = $row;
  1197. if (strtolower($table) == strtolower($TableName) && strval($levelid) == strval($UserLevelID)) {
  1198. if (is_null($priv) || !is_numeric($priv)) return 0;
  1199. return intval($priv);
  1200. }
  1201. }
  1202. }
  1203. }
  1204. return 0;
  1205. }
  1206. // Get current User Level name
  1207. function CurrentUserLevelName() {
  1208. return $this->GetUserLevelName($this->CurrentUserLevelID());
  1209. }
  1210. // Get User Level name based on User Level
  1211. function GetUserLevelName($UserLevelID) {
  1212. if (strval($UserLevelID) == "-1") {
  1213. return "Administrator";
  1214. } elseif ($UserLevelID >= 0) {
  1215. if (is_array($this->UserLevel)) {
  1216. foreach ($this->UserLevel as $row) {
  1217. list($levelid, $name) = $row;
  1218. if (strval($levelid) == strval($UserLevelID))
  1219. return $name;
  1220. }
  1221. }
  1222. }
  1223. return "";
  1224. }
  1225. // function to display all the User Level settings (for debug only)
  1226. function ShowUserLevelInfo() {
  1227. echo "<pre class=\"phpmaker\">";
  1228. print_r($this->UserLevel);
  1229. print_r($this->UserLevelPriv);
  1230. echo "</pre>";
  1231. echo "<p>Current User Level ID = " . $this->CurrentUserLevelID() . "</p>";
  1232. echo "<p>Current User Level ID List = " . $this->UserLevelList() . "</p>";
  1233. }
  1234. // function to check privilege for List page (for menu items)
  1235. function AllowList($TableName) {
  1236. return ($this->CurrentUserLevelPriv($TableName) & EW_ALLOW_LIST);
  1237. }
  1238. // function to check privilege for Add page (for Allow-Add)
  1239. function AllowAdd($TableName) {
  1240. return ($this->CurrentUserLevelPriv($TableName) & EW_ALLOW_ADD);
  1241. }
  1242. // Check if user is logged in
  1243. function IsLoggedIn() {
  1244. return (@$_SESSION[EW_SESSION_STATUS] == "login");
  1245. }
  1246. // Check if user is system administrator
  1247. function IsSysAdmin() {
  1248. return (@$_SESSION[EW_SESSION_SYS_ADMIN] == 1);
  1249. }
  1250. // Check if user is administrator
  1251. function IsAdmin() {
  1252. return ($this->CurrentUserLevelID() == -1 || $this->IsSysAdmin());
  1253. }
  1254. // Save User Level to session
  1255. function SaveUserLevel() {
  1256. $_SESSION[EW_SESSION_AR_USER_LEVEL] = $this->UserLevel;
  1257. $_SESSION[EW_SESSION_AR_USER_LEVEL_PRIV] = $this->UserLevelPriv;
  1258. }
  1259. // Load User Level from session
  1260. function LoadUserLevel() {
  1261. if (!is_array(@$_SESSION[EW_SESSION_AR_USER_LEVEL])) {
  1262. $this->SetupUserLevel();
  1263. $this->SaveUserLevel();
  1264. } else {
  1265. $this->UserLevel = $_SESSION[EW_SESSION_AR_USER_LEVEL];
  1266. $this->UserLevelPriv = $_SESSION[EW_SESSION_AR_USER_LEVEL_PRIV];
  1267. }
  1268. }
  1269. // Get current user info
  1270. function CurrentUserInfo($fieldname) {
  1271. $info = NULL;
  1272. return $info;
  1273. }
  1274. // UserID Loading event
  1275. function UserID_Loading() {
  1276. //echo "UserID Loading: " . $this->CurrentUserID() . "<br>";
  1277. }
  1278. // UserID Loaded event
  1279. function UserID_Loaded() {
  1280. //echo "UserID Loaded: " . $this->UserIDList() . "<br>";
  1281. }
  1282. // User Level Loaded event
  1283. function UserLevel_Loaded() {
  1284. //$this->AddUserPermission(<UserLevelName>, <TableName>, <UserPermission>);
  1285. //$this->DeleteUserPermission(<UserLevelName>, <TableName>, <UserPermission>);
  1286. }
  1287. // Table Permission Loading event
  1288. function TablePermission_Loading() {
  1289. //echo "Table Permission Loading: " . $this->CurrentUserLevelID() . "<br>";
  1290. }
  1291. // Table Permission Loaded event
  1292. function TablePermission_Loaded() {
  1293. //echo "Table Permission Loaded: " . $this->CurrentUserLevel() . "<br>";
  1294. }
  1295. // User Validated event
  1296. function User_Validated(&$rs) {
  1297. //e.g. $_SESSION['UserEmail'] = $rs->fields('Email');
  1298. }
  1299. }
  1300. ?>
  1301. <?php
  1302. /**
  1303. * Common functions
  1304. */
  1305. // Connection/Query error handler
  1306. function ew_ErrorFn($DbType, $ErrorType, $ErrorNo, $ErrorMsg, $Param1, $Param2, $Object) {
  1307. if ($ErrorType == 'CONNECT') {
  1308. $msg = "Failed to connect to $Param2 at $Param1. Error: " . $ErrorMsg;
  1309. } elseif ($ErrorType == 'EXECUTE') {
  1310. if (defined("EW_DEBUG_ENABLED")) {
  1311. $msg = "Failed to execute SQL: $Param1. Error: " . $ErrorMsg;
  1312. } else {
  1313. $msg = "Failed to execute SQL. Error: " . $ErrorMsg;
  1314. }
  1315. }
  1316. $_SESSION[EW_SESSION_MESSAGE] = $msg;
  1317. }
  1318. // Connect to database
  1319. function &ew_Connect() {
  1320. $object =& new mysqlt_driver_ADOConnection();
  1321. if (defined("EW_DEBUG_ENABLED"))
  1322. $object->debug = TRUE;
  1323. $object->port = EW_CONN_PORT;
  1324. $object->raiseErrorFn = 'ew_ErrorFn';
  1325. $object->Connect(EW_CONN_HOST, EW_CONN_USER, EW_CONN_PASS, EW_CONN_DB);
  1326. if (EW_MYSQL_CHARSET <> "") $object->Execute("SET NAMES '" . EW_MYSQL_CHARSET . "'");
  1327. $object->raiseErrorFn = '';
  1328. return $object;
  1329. }
  1330. // Get server variable by name
  1331. function ew_ServerVar($Name) {
  1332. $str = @$_SERVER[$Name];
  1333. if (empty($str)) $str = @$_ENV[$Name];
  1334. return $str;
  1335. }
  1336. // Check if HTTP POST
  1337. function ew_IsHttpPost() {
  1338. $ct = ew_ServerVar("CONTENT_TYPE");
  1339. if (empty($ct)) $ct = ew_ServerVar("HTTP_CONTENT_TYPE");
  1340. return ($ct == "application/x-www-form-urlencoded");
  1341. }
  1342. // Get script name
  1343. function ew_ScriptName() {
  1344. $sn = ew_ServerVar("PHP_SELF");
  1345. if (empty($sn)) $sn = ew_ServerVar("SCRIPT_NAME");
  1346. if (empty($sn)) $sn = ew_ServerVar("ORIG_PATH_INFO");
  1347. if (empty($sn)) $sn = ew_ServerVar("ORIG_SCRIPT_NAME");
  1348. if (empty($sn)) $sn = ew_ServerVar("REQUEST_URI");
  1349. if (empty($sn)) $sn = ew_ServerVar("URL");
  1350. if (empty($sn)) $sn = "UNKNOWN";
  1351. return $sn;
  1352. }
  1353. // Return multi-value search SQL
  1354. function ew_GetMultiSearchSql(&$Fld, $FldVal) {
  1355. $sWrk = "";
  1356. $arVal = explode(",", $FldVal);
  1357. foreach ($arVal as $sVal) {
  1358. $sVal = trim($sVal);
  1359. if (EW_IS_MYSQL) {
  1360. $sSql = "FIND_IN_SET('" . ew_AdjustSql($sVal) . "', " . $Fld->FldExpression . ")";
  1361. } else {
  1362. if (count($arVal) == 1 || EW_SEARCH_MULTI_VALUE_OPTION == 3) {
  1363. $sSql = $Fld->FldExpression . " = '" . ew_AdjustSql($sVal) . "' OR " . ew_GetMultiSearchSqlPart($Fld, $sVal);
  1364. } else {
  1365. $sSql = ew_GetMultiSearchSqlPart($Fld, $sVal);
  1366. }
  1367. }
  1368. if ($sWrk <> "") {
  1369. if (EW_SEARCH_MULTI_VALUE_OPTION == 2) {
  1370. $sWrk .= " AND ";
  1371. } elseif (EW_SEARCH_MULTI_VALUE_OPTION == 3) {
  1372. $sWrk .= " OR ";
  1373. }
  1374. }
  1375. $sWrk .= "($sSql)";
  1376. }
  1377. return $sWrk;
  1378. }
  1379. // Get multi search SQL part
  1380. function ew_GetMultiSearchSqlPart(&$Fld, $FldVal) {
  1381. return $Fld->FldExpression . " LIKE '" . ew_AdjustSql($FldVal) . ",%' OR " .
  1382. $Fld->FldExpression . " LIKE '%," . $FldVal . ",%' OR " .
  1383. $Fld->FldExpression . " LIKE '%," . $FldVal . "'";
  1384. }
  1385. // Get search sql
  1386. function ew_GetSearchSql(&$Fld, $FldVal, $FldOpr, $FldCond, $FldVal2, $FldOpr2) {
  1387. $sSql = "";
  1388. if ($FldOpr == "BETWEEN") {
  1389. $IsValidValue = ($Fld->FldDataType <> EW_DATATYPE_NUMBER) ||
  1390. ($Fld->FldDataType == EW_DATATYPE_NUMBER && is_numeric($FldVal) && is_numeric($FldVal2));
  1391. if ($FldVal <> "" && $FldVal2 <> "" && $IsValidValue)
  1392. $sSql = $Fld->FldExpression . " BETWEEN " . ew_QuotedValue($FldVal, $Fld->FldDataType) .
  1393. " AND " . ew_QuotedValue($FldVal2, $Fld->FldDataType);
  1394. } elseif ($FldOpr == "IS NULL" || $FldOpr == "IS NOT NULL") {
  1395. $sSql = $Fld->FldExpression . " " . $FldOpr;
  1396. } else {
  1397. $IsValidValue = ($Fld->FldDataType <> EW_DATATYPE_NUMBER) ||
  1398. ($Fld->FldDataType == EW_DATATYPE_NUMBER && is_numeric($FldVal));
  1399. if ($FldVal <> "" && $IsValidValue && ew_IsValidOpr($FldOpr, $Fld->FldDataType))
  1400. $sSql = $Fld->FldExpression . ew_SearchString($FldOpr, $FldVal, $Fld->FldDataType);
  1401. $IsValidValue = ($Fld->FldDataType <> EW_DATATYPE_NUMBER) ||
  1402. ($Fld->FldDataType == EW_DATATYPE_NUMBER && is_numeric($FldVal2));
  1403. if ($FldVal2 <> "" && $IsValidValue && ew_IsValidOpr($FldOpr2, $Fld->FldDataType)) {
  1404. if ($sSql <> "")
  1405. $sSql .= " " . (($FldCond == "OR") ? "OR" : "AND") . " ";
  1406. $sSql = "(" . $sSql . $Fld->FldExpression . ew_SearchString($FldOpr2, $FldVal2, $Fld->FldDataType) . ")";
  1407. }
  1408. }
  1409. return $sSql;
  1410. }
  1411. // Return search string
  1412. function ew_SearchString($FldOpr, $FldVal, $FldType) {
  1413. if ($FldOpr == "LIKE" || $FldOpr == "NOT LIKE") {
  1414. return " $FldOpr " . ew_QuotedValue("%$FldVal%", $FldType);
  1415. } elseif ($FldOpr == "STARTS WITH") {
  1416. return " LIKE " . ew_QuotedValue("$FldVal%", $FldType);
  1417. } else {
  1418. return " $FldOpr " . ew_QuotedValue($FldVal, $FldType);
  1419. }
  1420. }
  1421. // Check if valid operator
  1422. function ew_IsValidOpr($Opr, $FldType) {
  1423. $Valid = ($Opr == "=" || $Opr == "<" || $Opr == "<=" ||
  1424. $Opr == ">" || $Opr == ">=" || $Opr == "<>");
  1425. if ($FldType == EW_DATATYPE_STRING || $FldType == EW_DATATYPE_MEMO)
  1426. $Valid = ($Valid || $Opr == "LIKE" || $Opr == "NOT LIKE" || $Opr == "STARTS WITH");
  1427. return $Valid;
  1428. }
  1429. // quote field values
  1430. function ew_QuotedValue($Value, $FldType) {
  1431. if (is_null($Value)) return "NULL";
  1432. switch ($FldType) {
  1433. case EW_DATATYPE_STRING:
  1434. case EW_DATATYPE_MEMO:
  1435. case EW_DATATYPE_TIME:
  1436. if (EW_REMOVE_XSS) {
  1437. return "'" . ew_AdjustSql(ew_RemoveXSS($Value)) . "'";
  1438. } else {
  1439. return "'" . ew_AdjustSql($Value) . "'";
  1440. }
  1441. case EW_DATATYPE_BLOB:
  1442. return "'" . ew_AdjustSql($Value) . "'";
  1443. case EW_DATATYPE_DATE:
  1444. return (EW_IS_MSACCESS) ? "#" . ew_AdjustSql($Value) . "#" :
  1445. "'" . ew_AdjustSql($Value) . "'";
  1446. case EW_DATATYPE_GUID:
  1447. if (EW_IS_MSACCESS) {
  1448. if (strlen($Value) == 38) {
  1449. return "{guid " . $Value . "}";
  1450. } elseif (strlen($Value) == 36) {
  1451. return "{guid {" . $Value . "}}";
  1452. }
  1453. } else {
  1454. return "'" . $Value . "'";
  1455. }
  1456. case EW_DATATYPE_BOOLEAN: // enum('Y'/'N') or enum('1'/'0')
  1457. return "'" . $Value . "'";
  1458. default:
  1459. return $Value;
  1460. }
  1461. }
  1462. // Convert different data type value
  1463. function ew_Conv($v, $t) {
  1464. switch ($t) {
  1465. case 2:
  1466. case 3:
  1467. case 16:
  1468. case 17:
  1469. case 18:
  1470. case 19: // adSmallInt/adInteger/adTinyInt/adUnsignedTinyInt/adUnsignedSmallInt
  1471. return (is_null($v)) ? NULL : intval($v);
  1472. case 4:
  1473. Case 5:
  1474. case 6:
  1475. case 131: // adSingle/adDouble/adCurrency/adNumeric
  1476. return (is_null($v)) ? NULL : (float)$v;
  1477. default:
  1478. return (is_null($v)) ? NULL : $v;
  1479. }
  1480. }
  1481. // Convert string to float
  1482. function ew_StrToFloat($v) {
  1483. $v = str_replace(" ", "", $v);
  1484. // Enter your code here, e.g.
  1485. //$v = str_replace(",", ".", $v);
  1486. if ($v <> "") $v = (float)$v;
  1487. return $v;
  1488. }
  1489. // function for debug
  1490. function ew_Trace($msg) {
  1491. $filename = "debug.txt";
  1492. if (!$handle = fopen($filename, 'a')) exit;
  1493. if (is_writable($filename)) fwrite($handle, $msg . "\n");
  1494. fclose($handle);
  1495. }
  1496. // function to compare values with special handling for null values
  1497. function ew_CompareValue($v1, $v2) {
  1498. if (is_null($v1) && is_null($v2)) {
  1499. return TRUE;
  1500. } elseif (is_null($v1) || is_null($v2)) {
  1501. return FALSE;
  1502. } else {
  1503. return ($v1 == $v2);
  1504. }
  1505. }
  1506. // Strip slashes
  1507. function ew_StripSlashes($value) {
  1508. if (!get_magic_quotes_gpc()) return $value;
  1509. if (is_array($value)) {
  1510. return array_map('ew_StripSlashes', $value);
  1511. } else {
  1512. return stripslashes($value);
  1513. }
  1514. }
  1515. // Add slashes for SQL
  1516. function ew_AdjustSql($val) {
  1517. $val = addslashes(trim($val));
  1518. return $val;
  1519. }
  1520. // Build SELECT SQL based on different sql part
  1521. function ew_BuildSelectSql($sSelect, $sWhere, $sGroupBy, $sHaving, $sOrderBy, $sFilter, $sSort) {
  1522. $sDbWhere = $sWhere;
  1523. if ($sDbWhere <> "") {
  1524. if ($sFilter <> "") $sDbWhere = "($sDbWhere) AND ($sFilter)";
  1525. } else {
  1526. $sDbWhere = $sFilter;
  1527. }
  1528. $sDbOrderBy = $sOrderBy;
  1529. if ($sSort <> "") $sDbOrderBy = $sSort;
  1530. $sSql = $sSelect;
  1531. if ($sDbWhere <> "") $sSql .= " WHERE " . $sDbWhere;
  1532. if ($sGroupBy <> "") $sSql .= " GROUP BY " . $sGroupBy;
  1533. if ($sHaving <> "") $sSql .= " HAVING " . $sHaving;
  1534. if ($sDbOrderBy <> "") $sSql .= " ORDER BY " . $sDbOrderBy;
  1535. return $sSql;
  1536. }
  1537. // Executes the query, and returns the first column of the first row
  1538. function ew_ExecuteScalar($SQL) {
  1539. global $conn;
  1540. if ($conn && $rs = $conn->Execute($SQL)) {
  1541. if (!$rs->EOF && $rs->FieldCount() > 0)
  1542. return $rs->fields[0];
  1543. }
  1544. return NULL;
  1545. }
  1546. // Write Audit Trail (login/logout)
  1547. function ew_WriteAuditTrailOnLogInOut($logtype) {
  1548. $table = $logtype;
  1549. $sKey = "";
  1550. // Write Audit Trail
  1551. $filePfx = "log";
  1552. $curDate = date("Y/m/d");
  1553. $curTime = date("H:i:s");
  1554. $id = ew_ScriptName();
  1555. $curUser = CurrentUserName();
  1556. $action = $logtype;
  1557. ew_WriteAuditTrail($filePfx, $curDate, $curTime, $id, $curUser, $action, $table, "", "", "", "");
  1558. }
  1559. // Function for writing audit trail
  1560. function ew_WriteAuditTrail($pfx, $curDate, $curTime, $id, $user, $action, $table, $field, $keyvalue, $oldvalue, $newvalue) {
  1561. global $conn;
  1562. $sTab = "\t";
  1563. $userwrk = $user;
  1564. if ($userwrk == "") $userwrk = "-1"; // assume Administrator if no user
  1565. $sHeader = "date" . $sTab . "time" . $sTab . "id" .
  1566. $sTab . "user" . $sTab . "action" . $sTab . "table" .
  1567. $sTab . "field" . $sTab . "key value" . $sTab . "old value" .
  1568. $sTab . "new value";
  1569. $sMsg = $curDate . $sTab . $curTime . $sTab .
  1570. $id . $sTab . $userwrk . $sTab .
  1571. $action . $sTab . $table . $sTab .
  1572. $field . $sTab . $keyvalue . $sTab .
  1573. $oldvalue . $sTab . $newvalue;
  1574. $sFolder = EW_AUDIT_TRAIL_PATH;
  1575. $sFn = $pfx . "_" . date("Ymd") . ".txt";
  1576. $filename = ew_UploadPathEx(TRUE, $sFolder) . $sFn;
  1577. if (file_exists($filename)) {
  1578. $fileHandler = fopen($filename, "a+b");
  1579. } else {
  1580. $fileHandler = fopen($filename, "a+b");
  1581. fwrite($fileHandler,$sHeader."\r\n")

Large files files are truncated, but you can click here to view the full file