PageRenderTime 57ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/osj-phpfn50.php

http://osjobber.googlecode.com/
PHP | 2162 lines | 1655 code | 229 blank | 278 comment | 504 complexity | 14591bd150a92ab839234598b74f953c MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0

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

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

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