/phpfn6.php
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
- <?php
- /**
- * PHPMaker Common classes and functions
- * (C) 2002-2008 e.World Technology Limited. All rights reserved.
- */
- /**
- * Functions to init arrays
- */
- function ew_InitArray($iLen, $vValue) {
- if (function_exists('array_fill')) { // PHP 4 >= 4.2.0,
- return array_fill(0, $iLen, $vValue);
- } else {
- $aResult = array();
- for ($iCount = 0; $iCount < $iLen; $iCount++)
- $aResult[] = $vValue;
- return $aResult;
- }
- }
- function ew_Init2DArray($iLen1, $iLen2, $vValue) {
- return ew_InitArray($iLen1, ew_InitArray($iLen2, $vValue));
- }
- /**
- * Functions for converting encoding
- */
- function ew_ConvertToUtf8($str) {
- return ew_Convert(EW_ENCODING, "UTF-8", $str);
- }
- function ew_ConvertFromUtf8($str) {
- return ew_Convert("UTF-8", EW_ENCODING, $str);
- }
- function ew_Convert($from, $to, $str)
- {
- if ($from != "" && $to != "" && $from != $to) {
- if (function_exists("iconv")) {
- return iconv($from, $to, $str);
- } elseif (function_exists("mb_convert_encoding")) {
- return mb_convert_encoding($str, $to, $from);
- } else {
- return $str;
- }
- } else {
- return $str;
- }
- }
- /**
- * XML document class
- */
- class cXMLDocument {
- var $Encoding = EW_XML_ENCODING;
- var $RootTagName;
- var $RowTagName;
- var $XmlDoc;
- var $XmlTbl;
- var $XmlRow;
- var $XML = '';
- var $NullValue = 'NULL';
- function cXMLDocument($roottagname = 'table') {
- $this->RootTagName = $roottagname;
- if (EW_IS_PHP5) {
- $this->XmlDoc = new DOMDocument("1.0", $this->Encoding);
- $this->XmlTbl = $this->XmlDoc->createElement($this->RootTagName);
- $this->XmlDoc->appendChild($this->XmlTbl);
- }
- }
- function BeginRow($rowtagname = 'row') {
- $this->RowTagName = $rowtagname;
- if (EW_IS_PHP5) {
- $this->XmlRow = $this->XmlDoc->createElement($this->RowTagName);
- $this->XmlTbl->appendChild($this->XmlRow);
- } else {
- $this->XML .= "<$this->RowTagName>";
- }
- }
- function EndRow() {
- if (!EW_IS_PHP5) {
- $this->XML .= "</$this->RowTagName>";
- }
- }
- function AddField($name, $value) {
- if (is_null($value)) $value = $this->NullValue;
- if (EW_IS_PHP5) {
- $value = ew_ConvertToUtf8($value); // Convert to UTF-8
- $xmlfld = $this->XmlDoc->createElement($name);
- $this->XmlRow->appendChild($xmlfld);
- $xmlfld->appendChild($this->XmlDoc->createTextNode($value));
- } else {
- $value = ew_Convert(EW_ENCODING, EW_XML_ENCODING, $value); // Convert to output encoding
- $this->XML .= "<$name>" . htmlspecialchars($value) . "</$name>";
- }
- }
- function XML() {
- if (EW_IS_PHP5) {
- return $this->XmlDoc->saveXML();
- } else {
- return "<?xml version=\"1.0\"". (($this->Encoding <> "") ? " encoding=\"$this->Encoding\"" : "") .
- " ?>\n<$this->RootTagName>$this->XML</$this->RootTagName>";
- }
- }
- }
- /**
- * QueryString class
- */
- class cQueryString {
- var $values = array();
- var $Count;
- function cQueryString() {
- $ar = explode("&", ew_ServerVar("QUERY_STRING"));
- foreach ($ar as $p) {
- $arp = explode("=", $p);
- if (count($arp) == 2) $this->values[urldecode($arp[0])] = $arp[1];
- }
- $this->Count = count($this->values);
- }
- function getValue($name) {
- return (array_key_exists($name, $this->values)) ? $this->values[$name] : "";
- }
- function getUrlDecodedValue($name) {
- return urldecode($this->getValue($name));
- }
- function getRawUrlDecodedValue($name) {
- return rawurldecode($this->getValue($name));
- }
- function getConvertedValue($name) {
- return ew_ConvertFromUtf8($this->getRawUrlDecodedValue($name));
- }
- }
- /**
- * Email class
- */
- class cEmail {
- // Class properties
- var $Sender; // Sender
- var $Recipient; // Recipient
- var $Cc; // Cc
- var $Bcc; // Bcc
- var $Subject; // Subject
- var $Format; // Format
- var $Content; // Content
- function cEmail() {
- $this->Sender = "";
- $this->Recipient = "";
- $this->Cc = "";
- $this->Bcc = "";
- $this->Subject = "";
- $this->Format = "";
- $this->Content = "";
- }
- // Method to load email from template
- function Load($fn) {
- $fn = ew_ScriptFolder() . EW_PATH_DELIMITER . $fn;
- $sWrk = ew_ReadFile($fn); // Load text file content
- if ($sWrk <> "") {
- // Locate Header & Mail Content
- if (EW_IS_WINDOWS) {
- $i = strpos($sWrk, "\r\n\r\n");
- } else {
- $i = strpos($sWrk, "\n\n");
- if ($i === FALSE) $i = strpos($sWrk, "\r\n\r\n");
- }
- if ($i > 0) {
- $sHeader = substr($sWrk, 0, $i);
- $this->Content = trim(substr($sWrk, $i, strlen($sWrk)));
- if (EW_IS_WINDOWS) {
- $arrHeader = explode("\r\n", $sHeader);
- } else {
- $arrHeader = explode("\n", $sHeader);
- }
- for ($j = 0; $j < count($arrHeader); $j++) {
- $i = strpos($arrHeader[$j], ":");
- if ($i > 0) {
- $sName = trim(substr($arrHeader[$j], 0, $i));
- $sValue = trim(substr($arrHeader[$j], $i+1, strlen($arrHeader[$j])));
- switch (strtolower($sName))
- {
- case "subject":
- $this->Subject = $sValue;
- break;
- case "from":
- $this->Sender = $sValue;
- break;
- case "to":
- $this->Recipient = $sValue;
- break;
- case "cc":
- $this->Cc = $sValue;
- break;
- case "bcc":
- $this->Bcc = $sValue;
- break;
- case "format":
- $this->Format = $sValue;
- break;
- }
- }
- }
- }
- }
- }
- // Method to replace sender
- function ReplaceSender($ASender) {
- $this->Sender = str_replace('<!--$From-->', $ASender, $this->Sender);
- }
- // Method to replace recipient
- function ReplaceRecipient($ARecipient) {
- $this->Recipient = str_replace('<!--$To-->', $ARecipient, $this->Recipient);
- }
- // Method to add Cc email
- function AddCc($ACc) {
- if ($ACc <> "") {
- if ($this->Cc <> "") $this->Cc .= ";";
- $this->Cc .= $ACc;
- }
- }
- // Method to add Bcc email
- function AddBcc($ABcc) {
- if ($ABcc <> "") {
- if ($this->Bcc <> "") $this->Bcc .= ";";
- $this->Bcc .= $ABcc;
- }
- }
- // Method to replace subject
- function ReplaceSubject($ASubject) {
- $this->Subject = str_replace('<!--$Subject-->', $ASubject, $this->Subject);
- }
- // Method to replace content
- function ReplaceContent($Find, $ReplaceWith) {
- $this->Content = str_replace($Find, $ReplaceWith, $this->Content);
- }
- // Method to send email
- function Send() {
- return ew_SendEmail($this->Sender, $this->Recipient, $this->Cc, $this->Bcc,
- $this->Subject, $this->Content, $this->Format);
- }
- }
- /**
- * Pager item class
- */
- class cPagerItem {
- var $Start;
- var $Text;
- var $Enabled;
- }
- /**
- * Numeric pager class
- */
- class cNumericPager {
- var $Items = array();
- var $Count, $FromIndex, $ToIndex, $RecordCount, $PageSize, $Range;
- var $FirstButton, $PrevButton, $NextButton, $LastButton;
- var $ButtonCount = 0;
- var $Visible = TRUE;
- function cNumericPager($StartRec, $DisplayRecs, $TotalRecs, $RecRange)
- {
- $this->FirstButton = new cPagerItem;
- $this->PrevButton = new cPagerItem;
- $this->NextButton = new cPagerItem;
- $this->LastButton = new cPagerItem;
- $this->FromIndex = intval($StartRec);
- $this->PageSize = intval($DisplayRecs);
- $this->RecordCount = intval($TotalRecs);
- $this->Range = intval($RecRange);
- if ($this->PageSize == 0) return;
- if ($this->FromIndex > $this->RecordCount)
- $this->FromIndex = $this->RecordCount;
- $this->ToIndex = $this->FromIndex + $this->PageSize - 1;
- if ($this->ToIndex > $this->RecordCount)
- $this->ToIndex = $this->RecordCount;
- // setup
- $this->SetupNumericPager();
- // update button count
- if ($this->FirstButton->Enabled) $this->ButtonCount++;
- if ($this->PrevButton->Enabled) $this->ButtonCount++;
- if ($this->NextButton->Enabled) $this->ButtonCount++;
- if ($this->LastButton->Enabled) $this->ButtonCount++;
- $this->ButtonCount += count($this->Items);
- }
- // Add pager item
- function AddPagerItem($StartIndex, $Text, $Enabled)
- {
- $Item = new cPagerItem;
- $Item->Start = $StartIndex;
- $Item->Text = $Text;
- $Item->Enabled = $Enabled;
- $this->Items[] = $Item;
- }
- // Setup pager items
- function SetupNumericPager()
- {
- if ($this->RecordCount > $this->PageSize) {
- $Eof = ($this->RecordCount < ($this->FromIndex + $this->PageSize));
- $HasPrev = ($this->FromIndex > 1);
- // First Button
- $TempIndex = 1;
- $this->FirstButton->Start = $TempIndex;
- $this->FirstButton->Enabled = ($this->FromIndex > $TempIndex);
- // Prev Button
- $TempIndex = $this->FromIndex - $this->PageSize;
- if ($TempIndex < 1) $TempIndex = 1;
- $this->PrevButton->Start = $TempIndex;
- $this->PrevButton->Enabled = $HasPrev;
- // Page links
- if ($HasPrev || !$Eof) {
- $x = 1;
- $y = 1;
- $dx1 = intval(($this->FromIndex-1)/($this->PageSize*$this->Range))*$this->PageSize*$this->Range + 1;
- $dy1 = intval(($this->FromIndex-1)/($this->PageSize*$this->Range))*$this->Range + 1;
- if (($dx1+$this->PageSize*$this->Range-1) > $this->RecordCount) {
- $dx2 = intval($this->RecordCount/$this->PageSize)*$this->PageSize + 1;
- $dy2 = intval($this->RecordCount/$this->PageSize) + 1;
- } else {
- $dx2 = $dx1 + $this->PageSize*$this->Range - 1;
- $dy2 = $dy1 + $this->Range - 1;
- }
- while ($x <= $this->RecordCount) {
- if ($x >= $dx1 && $x <= $dx2) {
- $this->AddPagerItem($x, $y, $this->FromIndex<>$x);
- $x += $this->PageSize;
- $y++;
- } elseif ($x >= ($dx1-$this->PageSize*$this->Range) && $x <= ($dx2+$this->PageSize*$this->Range)) {
- if ($x+$this->Range*$this->PageSize < $this->RecordCount) {
- $this->AddPagerItem($x, $y . "-" . ($y+$this->Range-1), TRUE);
- } else {
- $ny = intval(($this->RecordCount-1)/$this->PageSize) + 1;
- if ($ny == $y) {
- $this->AddPagerItem($x, $y, TRUE);
- } else {
- $this->AddPagerItem($x, $y . "-" . $ny, TRUE);
- }
- }
- $x += $this->Range*$this->PageSize;
- $y += $this->Range;
- } else {
- $x += $this->Range*$this->PageSize;
- $y += $this->Range;
- }
- }
- }
- // Next Button
- $TempIndex = $this->FromIndex + $this->PageSize;
- $this->NextButton->Start = $TempIndex;
- $this->NextButton->Enabled = !$Eof;
- // Last Button
- $TempIndex = intval(($this->RecordCount-1)/$this->PageSize)*$this->PageSize + 1;
- $this->LastButton->Start = $TempIndex;
- $this->LastButton->Enabled = ($this->FromIndex < $TempIndex);
- }
- }
- }
- /**
- * PrevNext pager class
- */
- class cPrevNextPager {
- var $FirstButton, $PrevButton, $NextButton, $LastButton;
- var $CurrentPage, $PageCount, $FromIndex, $ToIndex, $RecordCount;
- var $Visible = TRUE;
- function cPrevNextPager($StartRec, $DisplayRecs, $TotalRecs)
- {
- $this->FirstButton = new cPagerItem;
- $this->PrevButton = new cPagerItem;
- $this->NextButton = new cPagerItem;
- $this->LastButton = new cPagerItem;
- $this->FromIndex = intval($StartRec);
- $this->PageSize = intval($DisplayRecs);
- $this->RecordCount = intval($TotalRecs);
- if ($this->PageSize == 0) return;
- $this->CurrentPage = intval(($this->FromIndex-1)/$this->PageSize) + 1;
- $this->PageCount = intval(($this->RecordCount-1)/$this->PageSize) + 1;
- if ($this->FromIndex > $this->RecordCount)
- $this->FromIndex = $this->RecordCount;
- $this->ToIndex = $this->FromIndex + $this->PageSize - 1;
- if ($this->ToIndex > $this->RecordCount)
- $this->ToIndex = $this->RecordCount;
- // First Button
- $TempIndex = 1;
- $this->FirstButton->Start = $TempIndex;
- $this->FirstButton->Enabled = ($TempIndex <> $this->FromIndex);
- // Prev Button
- $TempIndex = $this->FromIndex - $this->PageSize;
- if ($TempIndex < 1) $TempIndex = 1;
- $this->PrevButton->Start = $TempIndex;
- $this->PrevButton->Enabled = ($TempIndex <> $this->FromIndex);
- // Next Button
- $TempIndex = $this->FromIndex + $this->PageSize;
- if ($TempIndex > $this->RecordCount)
- $TempIndex = $this->FromIndex;
- $this->NextButton->Start = $TempIndex;
- $this->NextButton->Enabled = ($TempIndex <> $this->FromIndex);
- // Last Button
- $TempIndex = intval(($this->RecordCount-1)/$this->PageSize)*$this->PageSize + 1;
- $this->LastButton->Start = $TempIndex;
- $this->LastButton->Enabled = ($TempIndex <> $this->FromIndex);
- }
- }
- /**
- * Field class
- */
- class cField {
- var $TblVar; // Table var
- var $FldName; // Field name
- var $FldVar; // Field var
- var $FldExpression; // Field expression (used in sql)
- var $FldType; // Field type
- var $FldDataType; // PHPMaker Field type
- var $AdvancedSearch; // AdvancedSearch Object
- var $Upload; // Upload Object
- var $FldDateTimeFormat; // Date time format
- var $CssStyle; // CSS style
- var $CssClass; // CSS class
- var $ImageAlt; // Image alt
- var $ImageWidth = 0; // Image width
- var $ImageHeight = 0; // Image height
- var $ViewCustomAttributes; // View custom attributes
- var $EditCustomAttributes; // Edit custom attributes
- var $Count; // Count
- var $Total; // Total
- var $TrueValue = '1';
- var $FalseValue = '0';
- var $Visible = TRUE;
- function cField($tblvar, $fldvar, $fldname, $fldexpression, $fldtype, $flddtfmt, $upload = FALSE) {
- $this->TblVar = $tblvar;
- $this->FldVar = $fldvar;
- $this->FldName = $fldname;
- $this->FldExpression = $fldexpression;
- $this->FldType = $fldtype;
- $this->FldDataType = ew_FieldDataType($fldtype);
- $this->FldDateTimeFormat = $flddtfmt;
- $this->AdvancedSearch = new cAdvancedSearch();
- if ($upload)
- $this->Upload = new cUpload($this->TblVar, $this->FldVar);
- }
- // View Attributes
- function ViewAttributes() {
- $sAtt = "";
- if (trim($this->CssStyle) <> "") {
- $sAtt .= " style=\"" . trim($this->CssStyle) . "\"";
- }
- if (trim($this->CssClass) <> "") {
- $sAtt .= " class=\"" . trim($this->CssClass) . "\"";
- }
- if (trim($this->ImageAlt) <> "") {
- $sAtt .= " alt=\"" . trim($this->ImageAlt) . "\"";
- }
- if (intval($this->ImageWidth) > 0) {
- $sAtt .= " width=\"" . intval($this->ImageWidth) . "\"";
- }
- if (intval($this->ImageHeight) > 0) {
- $sAtt .= " height=\"" . intval($this->ImageHeight) . "\"";
- }
- if (trim($this->ViewCustomAttributes) <> "") {
- $sAtt .= " " . trim($this->ViewCustomAttributes);
- }
- return $sAtt;
- }
- // Edit Attributes
- function EditAttributes() {
- $sAtt = "";
- if (trim($this->CssStyle) <> "") {
- $sAtt .= " style=\"" . trim($this->CssStyle) . "\"";
- }
- if (trim($this->CssClass) <> "") {
- $sAtt .= " class=\"" . trim($this->CssClass) . "\"";
- }
- if (trim($this->EditCustomAttributes) <> "") {
- $sAtt .= " " . trim($this->EditCustomAttributes);
- }
- return $sAtt;
- }
- var $CustomMsg = ""; // Custom message
- var $RowAttributes = ""; // Row attributes
- var $CellCssClass = ""; // Cell CSS class
- var $CellCssStyle = ""; // Cell CSS style
- var $CellCustomAttributes = ""; // Cell custom attributes
- // Cell Attributes
- function CellAttributes() {
- $sAtt = "";
- if (trim($this->CellCssStyle) <> "") {
- $sAtt .= " style=\"" . trim($this->CellCssStyle) . "\"";
- }
- if (trim($this->CellCssClass) <> "") {
- $sAtt .= " class=\"" . trim($this->CellCssClass) . "\"";
- }
- if (trim($this->CellCustomAttributes) <> "") {
- $sAtt .= " " . trim($this->CellCustomAttributes);
- }
- return $sAtt;
- }
- // Sort Attributes
- function getSort() {
- return @$_SESSION[EW_PROJECT_NAME . "_" . $this->TblVar . "_" . EW_TABLE_SORT . "_" . $this->FldVar];
- }
- function setSort($v) {
- if (@$_SESSION[EW_PROJECT_NAME . "_" . $this->TblVar . "_" . EW_TABLE_SORT . "_" . $this->FldVar] <> $v) {
- $_SESSION[EW_PROJECT_NAME . "_" . $this->TblVar . "_" . EW_TABLE_SORT . "_" . $this->FldVar] = $v;
- }
- }
- function ReverseSort() {
- return ($this->getSort() == "ASC") ? "DESC" : "ASC";
- }
- var $MultiUpdate; // Multi update
- var $OldValue; // Old Value
- var $ConfirmValue; // Confirm Value
- var $CurrentValue; // Current value
- var $ViewValue; // View value
- var $EditValue; // Edit value
- var $EditValue2; // Edit value 2 (search)
- var $HrefValue; // Href value
- var $HrefValue2; // Href value 2 (confirm page UPLOAD control)
- // If ew_Empty(ViewValue) Then
- // Return " "
- // Else
- // Dim Result As String = Convert.ToString(ViewValue)
- // Dim Result2 As String = Regex.Replace(Result, "<[^>]*>", String.Empty) ' Remove HTML tags
- // Return IIf(Result2.Trim.Equals(String.Empty), " ", Result)
- // End If
- // List view value
- function ListViewValue() {
- $value = strval($this->ViewValue);
- if (trim($value) <> "") {
- $value2 = preg_replace('/<[^>]*>/', '', $value);
- return (trim($value2) <> "") ? $this->ViewValue : " ";
- } else {
- return " ";
- }
- }
- // Export Value
- function ExportValue($Export, $Original) {
- $ExportValue = ($Original) ? $this->CurrentValue : $this->ViewValue;
- if ($Export == "xml" && is_null($ExportValue))
- $ExportValue = "<Null>";
- return $ExportValue;
- }
- // Form value
- var $FormValue;
- function setFormValue($v) {
- $this->FormValue = ew_StripSlashes($v);
- if (is_array($this->FormValue))
- $this->FormValue = implode(",", $this->FormValue);
- $this->CurrentValue = $this->FormValue;
- }
- // QueryString value
- var $QueryStringValue;
- function setQueryStringValue($v) {
- $this->QueryStringValue = ew_StripSlashes($v);
- $this->CurrentValue = $this->QueryStringValue;
- }
- // Database Value
- var $DbValue;
- function setDbValue($v) {
- $this->DbValue = $v;
- $this->CurrentValue = $this->DbValue;
- }
- // Set database value with error default
- function SetDbValueDef($value, $default) {
- switch ($this->FldType) {
- case 2:
- case 3:
- case 16:
- case 17:
- case 18: // Int
- $value = trim($value);
- $DbValue = (is_numeric($value)) ? intval($value) : $default;
- break;
- case 19:
- case 20:
- case 21: // Big Int
- $value = trim($value);
- $DbValue = (is_numeric($value)) ? $value : $default;
- break;
- case 5:
- case 6:
- case 14:
- case 131: // Double
- case 4: // Single
- $value = trim($value);
- $value = ew_StrToFloat($value);
- $DbValue = (is_float($value)) ? $value : $default;
- break;
- case 7:
- case 133:
- case 134:
- case 135: //Date
- case 201:
- case 203:
- case 129:
- case 130:
- case 200:
- case 202: // String
- $value = trim($value);
- $DbValue = ($value == "") ? $default : $value;
- break;
- case 128:
- case 204:
- case 205: // Binary
- $DbValue = (is_null($value)) ? $default : $value;
- break;
- case 72: // GUID
- $value = trim($value);
- $DbValue = ($value <> "" && ew_CheckGUID($value)) ? $value : $default;
- break;
- default:
- $DbValue = $value;
- }
- $this->setDbValue($DbValue);
- }
- // Session Value
- function getSessionValue() {
- return @$_SESSION[EW_PROJECT_NAME . "_" . $this->TblVar . "_" . $this->FldVar . "_SessionValue"];
- }
- function setSessionValue($v) {
- $_SESSION[EW_PROJECT_NAME . "_" . $this->TblVar . "_" . $this->FldVar . "_SessionValue"] = $v;
- }
- }
- /**
- * List option collection class
- */
- class cListOptions {
- var $Items = array();
- // Add and return a new option
- function Add() {
- $this->Items[] = new cListOption();
- return $this->Items[count($this->Items)-1];
- }
- }
- /**
- * List option class
- */
- class cListOption {
- var $Visible = TRUE;
- var $HeaderCellHtml = "";
- var $FooterCellHtml = "";
- var $BodyCellHtml = "";
- var $MultiColumnLinkHtml = "";
- }
- ?>
- <?php
- /**
- * Advanced Search class
- */
- class cAdvancedSearch {
- var $SearchValue; // Search value
- var $SearchOperator; // Search operator
- var $SearchCondition; // Search condition
- var $SearchValue2; // Search value 2
- var $SearchOperator2; // Search operator 2
- }
- ?>
- <?php
- /**
- * Upload class
- */
- class cUpload {
- var $Index = 0; // Index to handle multiple form elements
- var $TblVar; // Table variable
- var $FldVar; // Field variable
- var $Message; // Error message
- var $DbValue; // Value from database
- var $Value = NULL; // Upload value
- var $Action; // Upload action
- var $UploadPath; // Upload path
- var $FileName; // Upload file name
- var $FileSize; // Upload file size
- var $ContentType; // File content type
- var $ImageWidth; // Image width
- var $ImageHeight; // Image height
- // Class initialize
- function cUpload($TblVar, $FldVar, $Binary = FALSE) {
- $this->TblVar = $TblVar;
- $this->FldVar = $FldVar;
- }
- function getSessionID() {
- return EW_PROJECT_NAME . "_" . $this->TblVar . "_" . $this->FldVar . "_" . $this->Index;
- }
- // Save Db value to Session
- function SaveDbToSession() {
- $sSessionID = $this->getSessionID();
- $_SESSION[$sSessionID . "_DbValue"] = $this->DbValue;
- }
- // Restore Db value from Session
- function RestoreDbFromSession() {
- $sSessionID = $this->getSessionID();
- $this->DbValue = @$_SESSION[$sSessionID . "_DbValue"];
- }
- // Remove Db value from Session
- function RemoveDbFromSession() {
- $sSessionID = $this->getSessionID();
- unset($_SESSION[$sSessionID . "_DbValue"]);
- }
- // Save Upload values to Session
- function SaveToSession() {
- $sSessionID = $this->getSessionID();
- $_SESSION[$sSessionID . "_Action"] = $this->Action;
- $_SESSION[$sSessionID . "_FileSize"] = $this->FileSize;
- $_SESSION[$sSessionID . "_FileName"] = $this->FileName;
- $_SESSION[$sSessionID . "_ContentType"] = $this->ContentType;
- $_SESSION[$sSessionID . "_ImageWidth"] = $this->ImageWidth;
- $_SESSION[$sSessionID . "_ImageHeight"] = $this->ImageHeight;
- $_SESSION[$sSessionID . "_Value"] = $this->Value;
- }
- // Restore Upload values from Session
- function RestoreFromSession() {
- $sSessionID = $this->getSessionID();
- $this->Action = @$_SESSION[$sSessionID . "_Action"];
- $this->FileSize = @$_SESSION[$sSessionID . "_FileSize"];
- $this->FileName = @$_SESSION[$sSessionID . "_FileName"];
- $this->ContentType = @$_SESSION[$sSessionID . "_ContentType"];
- $this->ImageWidth = @$_SESSION[$sSessionID . "_ImageWidth"];
- $this->ImageHeight = @$_SESSION[$sSessionID . "_ImageHeight"];
- $this->Value = @$_SESSION[$sSessionID . "_Value"];
- }
- // Remove Upload values from Session
- function RemoveFromSession() {
- $sSessionID = $this->getSessionID();
- unset($_SESSION[$sSessionID . "_Action"]);
- unset($_SESSION[$sSessionID . "_FileSize"]);
- unset($_SESSION[$sSessionID . "_FileName"]);
- unset($_SESSION[$sSessionID . "_ContentType"]);
- unset($_SESSION[$sSessionID . "_ImageWidth"]);
- unset($_SESSION[$sSessionID . "_ImageHeight"]);
- unset($_SESSION[$sSessionID . "_Value"]);
- }
- // function to check the file type of the uploaded file
- function UploadAllowedFileExt($filename) {
- return ew_CheckFileType($filename);
- }
- // Get upload file
- function UploadFile() {
- global $objForm;
- $this->Value = NULL; // Reset first
- $gsFldVar = $this->FldVar;
- $gsFldVarAction = "a" . substr($gsFldVar, 1);
- $gsFldVarWidth = "wd" . substr($gsFldVar, 1);
- $gsFldVarHeight = "ht" . substr($gsFldVar, 1);
- // Get action
- $this->Action = $objForm->GetValue($gsFldVarAction);
- // Get and check the upload file size
- $this->FileSize = $objForm->GetUploadFileSize($gsFldVar);
- // Get and check the upload file type
- $this->FileName = $objForm->GetUploadFileName($gsFldVar);
- // Get upload file content type
- $this->ContentType = $objForm->GetUploadFileContentType($gsFldVar);
- // Get upload value
- $this->Value = $objForm->GetUploadFileData($gsFldVar);
- // Get image width and height
- $this->ImageWidth = $objForm->GetUploadImageWidth($gsFldVar);
- $this->ImageHeight = $objForm->GetUploadImageHeight($gsFldVar);
- if ($this->ImageWidth < 0 || $this->ImageHeight < 0) {
- $this->ImageWidth = $objForm->GetValue($gsFldVarWidth);
- $this->ImageHeight = $objForm->GetValue($gsFldVarHeight);
- }
- return TRUE; // Normal return
- }
- // Resize image
- function Resize($width, $height, $quality) {
- if (!is_null($this->Value)) {
- $wrkwidth = $width;
- $wrkheight = $height;
- if (ew_ResizeBinary($this->Value, $wrkwidth, $wrkheight, $quality)) { // P6
- $this->ImageWidth = $wrkwidth;
- $this->ImageHeight = $wrkheight;
- $this->FileSize = strlen($this->Value);
- }
- }
- }
- // Save uploaded data to file (Path relative to application root)
- function SaveToFile($Path, $NewFileName, $OverWrite) {
- if (!is_null($this->Value)) {
- $Path = ew_UploadPathEx(TRUE, $Path);
- if (trim(strval($NewFileName)) == "") $NewFileName = $this->FileName;
- if ($OverWrite) {
- return ew_SaveFile($Path, $NewFileName, $this->Value);
- } else {
- return ew_SaveFile($Path, ew_UploadFileNameEx($Path, $NewFileName), $this->Value);
- }
- }
- return FALSE;
- }
- // Resize and save uploaded data to file (Path relative to application root)
- function ResizeAndSaveToFile($Width, $Height, $Quality, $Path, $NewFileName, $OverWrite) {
- $bResult = FALSE;
- if (!is_null($this->Value)) {
- $OldValue = $this->Value;
- $this->Resize($Width, $Height, $Quality);
- $bResult = $this->SaveToFile($Path, $NewFileName, $OverWrite);
- $this->Value = $OldValue;
- }
- return $bResult;
- }
- }
- ?>
- <?php
- /**
- * Advanced Security class
- */
- class cAdvancedSecurity {
- var $UserLevel = array(); // All User Levels
- var $UserLevelPriv = array(); // All User Level permissions
- var $UserLevelID = array(); // User Level ID array
- var $UserID = array(); // User ID array
- var $CurrentUserLevelID;
- var $CurrentUserLevel; // Permissions
- var $CurrentUserID;
- var $CurrentParentUserID;
- // Class Initialize
- function cAdvancedSecurity() {
- // Init User Level
- $this->CurrentUserLevelID = $this->SessionUserLevelID();
- if (is_numeric($this->CurrentUserLevelID) && intval($this->CurrentUserLevelID) >= -1) {
- $this->UserLevelID[] = $this->CurrentUserLevelID;
- }
- // Init User ID
- $this->CurrentUserID = $this->SessionUserID();
- $this->CurrentParentUserID = $this->SessionParentUserID();
- // Load user level (for TablePermission_Loading event)
- $this->LoadUserLevel();
- }
- // Session user id
- function SessionUserID() {
- return strval(@$_SESSION[EW_SESSION_USER_ID]);
- }
- function setSessionUserID($v) {
- $_SESSION[EW_SESSION_USER_ID] = $v;
- $this->CurrentUserID = $v;
- }
- // Session parent user id
- function SessionParentUserID() {
- return strval(@$_SESSION[EW_SESSION_PARENT_USER_ID]);
- }
- function setSessionParentUserID($v) {
- $_SESSION[EW_SESSION_PARENT_USER_ID] = $v;
- $this->CurrentParentUserID = $v;
- }
- // Session user level id
- function SessionUserLevelID() {
- return @$_SESSION[EW_SESSION_USER_LEVEL_ID];
- }
- function setSessionUserLevelID($v) {
- $_SESSION[EW_SESSION_USER_LEVEL_ID] = $v;
- $this->CurrentUserLevelID = $v;
- }
- // Session user level value
- function SessionUserLevel() {
- return @$_SESSION[EW_SESSION_USER_LEVEL];
- }
- function setSessionUserLevel($v) {
- $_SESSION[EW_SESSION_USER_LEVEL] = $v;
- $this->CurrentUserLevel = $v;
- }
- // Current user name
- function getCurrentUserName() {
- return strval(@$_SESSION[EW_SESSION_USER_NAME]);
- }
- function setCurrentUserName($v) {
- $_SESSION[EW_SESSION_USER_NAME] = $v;
- }
- function CurrentUserName() {
- return $this->getCurrentUserName();
- }
- // Current User ID
- function CurrentUserID() {
- return $this->CurrentUserID;
- }
- // Current parent User ID
- function CurrentParentUserID() {
- return $this->CurrentParentUserID;
- }
- // Current User Level id
- function CurrentUserLevelID() {
- return $this->CurrentUserLevelID;
- }
- // Current User Level value
- function CurrentUserLevel() {
- return $this->CurrentUserLevel;
- }
- // Can add
- function CanAdd() {
- return (($this->CurrentUserLevel & EW_ALLOW_ADD) == EW_ALLOW_ADD);
- }
- function setCanAdd($b) {
- if ($b) {
- $this->CurrentUserLevel = ($this->CurrentUserLevel | EW_ALLOW_ADD);
- } else {
- $this->CurrentUserLevel = ($this->CurrentUserLevel & (~ EW_ALLOW_ADD));
- }
- }
- // Can delete
- function CanDelete() {
- return (($this->CurrentUserLevel & EW_ALLOW_DELETE) == EW_ALLOW_DELETE);
- }
- function setCanDelete($b) {
- if ($b) {
- $this->CurrentUserLevel = ($this->CurrentUserLevel | EW_ALLOW_DELETE);
- } else {
- $this->CurrentUserLevel = ($this->CurrentUserLevel & (~ EW_ALLOW_DELETE));
- }
- }
- // Can edit
- function CanEdit() {
- return (($this->CurrentUserLevel & EW_ALLOW_EDIT) == EW_ALLOW_EDIT);
- }
- function setCanEdit($b) {
- if ($b) {
- $this->CurrentUserLevel = ($this->CurrentUserLevel | EW_ALLOW_EDIT);
- } else {
- $this->CurrentUserLevel = ($this->CurrentUserLevel & (~ EW_ALLOW_EDIT));
- }
- }
- // Can view
- function CanView() {
- return (($this->CurrentUserLevel & EW_ALLOW_VIEW) == EW_ALLOW_VIEW);
- }
- function setCanView($b) {
- if ($b) {
- $this->CurrentUserLevel = ($this->CurrentUserLevel | EW_ALLOW_VIEW);
- } else {
- $this->CurrentUserLevel = ($this->CurrentUserLevel & (~ EW_ALLOW_VIEW));
- }
- }
- // Can list
- function CanList() {
- return (($this->CurrentUserLevel & EW_ALLOW_LIST) == EW_ALLOW_LIST);
- }
- function setCanList($b) {
- if ($b) {
- $this->CurrentUserLevel = ($this->CurrentUserLevel | EW_ALLOW_LIST);
- } else {
- $this->CurrentUserLevel = ($this->CurrentUserLevel & (~ EW_ALLOW_LIST));
- }
- }
- // Can report
- function CanReport() {
- return (($this->CurrentUserLevel & EW_ALLOW_REPORT) == EW_ALLOW_REPORT);
- }
- function setCanReport($b) {
- if ($b) {
- $this->CurrentUserLevel = ($this->CurrentUserLevel | EW_ALLOW_REPORT);
- } else {
- $this->CurrentUserLevel = ($this->CurrentUserLevel & (~ EW_ALLOW_REPORT));
- }
- }
- // Can search
- function CanSearch() {
- return (($this->CurrentUserLevel & EW_ALLOW_SEARCH) == EW_ALLOW_SEARCH);
- }
- function setCanSearch($b) {
- if ($b) {
- $this->CurrentUserLevel = ($this->CurrentUserLevel | EW_ALLOW_SEARCH);
- } else {
- $this->CurrentUserLevel = ($this->CurrentUserLevel & (~ EW_ALLOW_SEARCH));
- }
- }
- // Can admin
- function CanAdmin() {
- return (($this->CurrentUserLevel & EW_ALLOW_ADMIN) == EW_ALLOW_ADMIN);
- }
- function setCanAdmin($b) {
- if ($b) {
- $this->CurrentUserLevel = ($this->CurrentUserLevel | EW_ALLOW_ADMIN);
- } else {
- $this->CurrentUserLevel = ($this->CurrentUserLevel & (~ EW_ALLOW_ADMIN));
- }
- }
- // Last url
- function LastUrl() {
- return @$_COOKIE[EW_PROJECT_NAME]['LastUrl'];
- }
- // Save last url
- function SaveLastUrl() {
- $s = ew_ServerVar("SCRIPT_NAME");
- $q = ew_ServerVar("QUERY_STRING");
- if ($q <> "") $s .= "?" . $q;
- if ($this->LastUrl() == $s) $s = "";
- @setcookie(EW_PROJECT_NAME . '[LastUrl]', $s);
- }
- // Auto login
- function AutoLogin() {
- if (@$_COOKIE[EW_PROJECT_NAME]['AutoLogin'] == "autologin") {
- $usr = @$_COOKIE[EW_PROJECT_NAME]['UserName'];
- $pwd = @$_COOKIE[EW_PROJECT_NAME]['Password'];
- $pwd = TEAdecrypt($pwd, EW_RANDOM_KEY);
- $AutoLogin = $this->ValidateUser($usr, $pwd);
- } else {
- $AutoLogin = FALSE;
- }
- return $AutoLogin;
- }
- // Validate user
- function ValidateUser($usr, $pwd) {
- global $conn;
- global $usuario;
- $ValidateUser = FALSE;
- // Check hard coded admin first
- if (EW_CASE_SENSITIVE_PASSWORD) {
- $ValidateUser = (EW_ADMIN_USER_NAME == $usr && EW_ADMIN_PASSWORD == $pwd);
- } else {
- $ValidateUser = (strtolower(EW_ADMIN_USER_NAME) == strtolower($usr) &&
- strtolower(EW_ADMIN_PASSWORD) == strtolower($pwd));
- }
- if ($ValidateUser) {
- $_SESSION[EW_SESSION_STATUS] = "login";
- $_SESSION[EW_SESSION_SYS_ADMIN] = 1; // System Administrator
- $this->setCurrentUserName("Administrator"); // Load user name
- $this->setSessionUserLevelID(-1); // System Administrator
- $this->SetUpUserLevel();
- }
- // Check other users
- if (!$ValidateUser) {
- $sFilter = "(`login` = '" . ew_AdjustSql($usr) . "')";
- // Set up filter (Sql Where Clause) and get Return SQL
- // SQL constructor in <UseTable> class, <UserTable>info.php
- $sSql = $usuario->GetSQL($sFilter, "");
- if ($rs = $conn->Execute($sSql)) {
- if (!$rs->EOF) {
- if (EW_CASE_SENSITIVE_PASSWORD) {
- if (EW_MD5_PASSWORD) {
- $ValidateUser = ($rs->fields('password') == md5($pwd));
- } else {
- $ValidateUser = ($rs->fields('password') == $pwd);
- }
- } else {
- if (EW_MD5_PASSWORD) {
- $ValidateUser = ($rs->fields('password') == md5(strtolower($pwd)));
- } else {
- $ValidateUser = (strtolower($rs->fields('password')) == strtolower($pwd));
- }
- }
- if ($ValidateUser) {
- $_SESSION[EW_SESSION_STATUS] = "login";
- $_SESSION[EW_SESSION_SYS_ADMIN] = 0; // Non System Administrator
- $this->setCurrentUserName($rs->fields('login')); // Load user name
- if (is_null($rs->fields('idRol'))) {
- $this->setSessionUserLevelID(0);
- } else {
- $this->setSessionUserLevelID(intval($rs->fields('idRol'))); // Load User Level
- }
- $this->SetUpUserLevel();
- // Call User Validated event
- $this->User_Validated($rs);
- }
- }
- $rs->Close();
- }
- }
- return $ValidateUser;
- }
- // Dynamic User Level security
- // Get User Level settings from database
- function SetUpUserLevel() {
- $this->SetUpUserLevelEx(); // Load all user levels
- // User Level loaded event
- $this->UserLevel_Loaded();
- // Save the User Level to session variable
- $this->SaveUserLevel();
- }
- // Get all User Level settings from database
- function SetUpUserLevelEx() {
- global $conn;
- // Get the User Level definitions
- $sSql = "SELECT " . EW_USER_LEVEL_ID_FIELD . ", " . EW_USER_LEVEL_NAME_FIELD . " FROM " . EW_USER_LEVEL_TABLE;
- if ($rs = $conn->Execute($sSql)) {
- $this->UserLevel = $rs->GetRows();
- $rs->Close();
- }
- // Get the User Level privileges
- $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;
- if ($rs = $conn->Execute($sSql)) {
- $this->UserLevelPriv = $rs->GetRows();
- $rs->Close();
- }
- }
- // Add user permission
- function AddUserPermission($UserLevelName, $TableName, $UserPermission) {
- // Get user level id from user name
- $UserLevelID = "";
- if (is_array($this->UserLevel)) {
- foreach ($this->UserLevel as $row) {
- list($levelid, $name) = $row;
- if (strval($UserLevelName) == strval($name)) {
- $UserLevelID = $levelid;
- break;
- }
- }
- }
- if (is_array($this->UserLevelPriv) && $UserLevelID <> "") {
- $cnt = count($this->UserLevelPriv);
- for ($i = 0; $i < $cnt; $i++) {
- list($table, $levelid, $priv) = $this->UserLevelPriv[$i];
- if (strtolower($table) == strtolower($TableName) && strval($levelid) == strval($UserLevelID)) {
- $this->UserLevelPriv[$i][2] = $priv | $UserPermission; // Add permission
- break;
- }
- }
- }
- }
- // Delete user permission
- function DeleteUserPermission($UserLevelName, $TableName, $UserPermission) {
- // Get user level id from user name
- $UserLevelID = "";
- if (is_array($this->UserLevel)) {
- foreach ($this->UserLevel as $row) {
- list($levelid, $name) = $row;
- if (strval($UserLevelName) == strval($name)) {
- $UserLevelID = $levelid;
- break;
- }
- }
- }
- if (is_array($this->UserLevelPriv) && $UserLevelID <> "") {
- $cnt = count($this->UserLevelPriv);
- for ($i = 0; $i < $cnt; $i++) {
- list($table, $levelid, $priv) = $this->UserLevelPriv[$i];
- if (strtolower($table) == strtolower($TableName) && strval($levelid) == strval($UserLevelID)) {
- $this->UserLevelPriv[$i][2] = $priv & (127 - $UserPermission); // Remove permission
- break;
- }
- }
- }
- }
- // Load current user level
- function LoadCurrentUserLevel($Table) {
- $this->LoadUserLevel();
- $this->setSessionUserLevel($this->CurrentUserLevelPriv($Table));
- }
- // Get current user privilege
- function CurrentUserLevelPriv($TableName) {
- if ($this->IsLoggedIn()) {
- $Priv= 0;
- foreach ($this->UserLevelID as $UserLevelID)
- $Priv |= $this->GetUserLevelPrivEx($TableName, $UserLevelID);
- return $Priv;
- } else {
- return 0;
- }
- }
- // Get user level ID by user level name
- function GetUserLevelID($UserLevelName) {
- if (strval($UserLevelName) == "Administrator") {
- return -1;
- } elseif ($UserLevelName <> "") {
- if (is_array($this->UserLevel)) {
- foreach ($this->UserLevel as $row) {
- list($levelid, $name) = $row;
- if (strval($name) == strval($UserLevelName))
- return $levelid;
- }
- }
- }
- return -2;
- }
- // Add user level (for use with UserLevel_Loading event)
- function AddUserLevel($UserLevelName) {
- if (strval($UserLevelName) == "") return;
- $UserLevelID = $this->GetUserLevelID($UserLevelName);
- if (!is_numeric($UserLevelID)) return;
- if ($UserLevelID < -1) return;
- if (!in_array($UserLevelID, $this->UserLevelID))
- $this->UserLevelID[] = $UserLevelID;
- }
- // Delete user level (for use with UserLevel_Loading event)
- function DeleteUserLevel($UserLevelName) {
- if (strval($UserLevelName) == "") return;
- $UserLevelID = $this->GetUserLevelID($UserLevelName);
- if (!is_numeric($UserLevelID)) return;
- if ($UserLevelID < -1) return;
- $cnt = count($this->UserLevelID);
- for ($i = 0; $i < $cnt; $i++) {
- if ($this->UserLevelID[$i] == $UserLevelID) {
- unset($this->UserLevelID[$i]);
- break;
- }
- }
- }
- // User level list
- function UserLevelList() {
- return implode(", ", $this->UserLevelID);
- }
- // User level name list
- function UserLevelNameList() {
- $list = "";
- foreach ($this->UserLevelID as $UserLevelID) {
- if ($list <> "") $lList .= ", ";
- $list .= ew_QuotedValue($this->GetUserLevelName($UserLevelID), EW_DATATYPE_STRING);
- }
- return $list;
- }
- // Get user privilege based on table name and User Level
- function GetUserLevelPrivEx($TableName, $UserLevelID) {
- if (strval($UserLevelID) == "-1") { // System Administrator
- if (defined("EW_USER_LEVEL_COMPAT")) {
- return 31; // Use old User Level values
- } else {
- return 127; // Use new User Level values (separate View/Search)
- }
- } elseif ($UserLevelID >= 0) {
- if (is_array($this->UserLevelPriv)) {
- foreach ($this->UserLevelPriv as $row) {
- list($table, $levelid, $priv) = $row;
- if (strtolower($table) == strtolower($TableName) && strval($levelid) == strval($UserLevelID)) {
- if (is_null($priv) || !is_numeric($priv)) return 0;
- return intval($priv);
- }
- }
- }
- }
- return 0;
- }
- // Get current User Level name
- function CurrentUserLevelName() {
- return $this->GetUserLevelName($this->CurrentUserLevelID());
- }
- // Get User Level name based on User Level
- function GetUserLevelName($UserLevelID) {
- if (strval($UserLevelID) == "-1") {
- return "Administrator";
- } elseif ($UserLevelID >= 0) {
- if (is_array($this->UserLevel)) {
- foreach ($this->UserLevel as $row) {
- list($levelid, $name) = $row;
- if (strval($levelid) == strval($UserLevelID))
- return $name;
- }
- }
- }
- return "";
- }
- // function to display all the User Level settings (for debug only)
- function ShowUserLevelInfo() {
- echo "<pre class=\"phpmaker\">";
- print_r($this->UserLevel);
- print_r($this->UserLevelPriv);
- echo "</pre>";
- echo "<p>Current User Level ID = " . $this->CurrentUserLevelID() . "</p>";
- echo "<p>Current User Level ID List = " . $this->UserLevelList() . "</p>";
- }
- // function to check privilege for List page (for menu items)
- function AllowList($TableName) {
- return ($this->CurrentUserLevelPriv($TableName) & EW_ALLOW_LIST);
- }
- // function to check privilege for Add page (for Allow-Add)
- function AllowAdd($TableName) {
- return ($this->CurrentUserLevelPriv($TableName) & EW_ALLOW_ADD);
- }
- // Check if user is logged in
- function IsLoggedIn() {
- return (@$_SESSION[EW_SESSION_STATUS] == "login");
- }
- // Check if user is system administrator
- function IsSysAdmin() {
- return (@$_SESSION[EW_SESSION_SYS_ADMIN] == 1);
- }
- // Check if user is administrator
- function IsAdmin() {
- return ($this->CurrentUserLevelID() == -1 || $this->IsSysAdmin());
- }
- // Save User Level to session
- function SaveUserLevel() {
- $_SESSION[EW_SESSION_AR_USER_LEVEL] = $this->UserLevel;
- $_SESSION[EW_SESSION_AR_USER_LEVEL_PRIV] = $this->UserLevelPriv;
- }
- // Load User Level from session
- function LoadUserLevel() {
- if (!is_array(@$_SESSION[EW_SESSION_AR_USER_LEVEL])) {
- $this->SetupUserLevel();
- $this->SaveUserLevel();
- } else {
- $this->UserLevel = $_SESSION[EW_SESSION_AR_USER_LEVEL];
- $this->UserLevelPriv = $_SESSION[EW_SESSION_AR_USER_LEVEL_PRIV];
- }
- }
- // Get current user info
- function CurrentUserInfo($fieldname) {
- $info = NULL;
- return $info;
- }
- // UserID Loading event
- function UserID_Loading() {
- //echo "UserID Loading: " . $this->CurrentUserID() . "<br>";
- }
- // UserID Loaded event
- function UserID_Loaded() {
- //echo "UserID Loaded: " . $this->UserIDList() . "<br>";
- }
- // User Level Loaded event
- function UserLevel_Loaded() {
- //$this->AddUserPermission(<UserLevelName>, <TableName>, <UserPermission>);
- //$this->DeleteUserPermission(<UserLevelName>, <TableName>, <UserPermission>);
- }
- // Table Permission Loading event
- function TablePermission_Loading() {
- //echo "Table Permission Loading: " . $this->CurrentUserLevelID() . "<br>";
- }
- // Table Permission Loaded event
- function TablePermission_Loaded() {
- //echo "Table Permission Loaded: " . $this->CurrentUserLevel() . "<br>";
- }
- // User Validated event
- function User_Validated(&$rs) {
- //e.g. $_SESSION['UserEmail'] = $rs->fields('Email');
- }
- }
- ?>
- <?php
- /**
- * Common functions
- */
- // Connection/Query error handler
- function ew_ErrorFn($DbType, $ErrorType, $ErrorNo, $ErrorMsg, $Param1, $Param2, $Object) {
- if ($ErrorType == 'CONNECT') {
- $msg = "Failed to connect to $Param2 at $Param1. Error: " . $ErrorMsg;
- } elseif ($ErrorType == 'EXECUTE') {
- if (defined("EW_DEBUG_ENABLED")) {
- $msg = "Failed to execute SQL: $Param1. Error: " . $ErrorMsg;
- } else {
- $msg = "Failed to execute SQL. Error: " . $ErrorMsg;
- }
- }
- $_SESSION[EW_SESSION_MESSAGE] = $msg;
- }
- // Connect to database
- function &ew_Connect() {
- $object =& new mysqlt_driver_ADOConnection();
- if (defined("EW_DEBUG_ENABLED"))
- $object->debug = TRUE;
- $object->port = EW_CONN_PORT;
- $object->raiseErrorFn = 'ew_ErrorFn';
- $object->Connect(EW_CONN_HOST, EW_CONN_USER, EW_CONN_PASS, EW_CONN_DB);
- if (EW_MYSQL_CHARSET <> "") $object->Execute("SET NAMES '" . EW_MYSQL_CHARSET . "'");
- $object->raiseErrorFn = '';
- return $object;
- }
- // Get server variable by name
- function ew_ServerVar($Name) {
- $str = @$_SERVER[$Name];
- if (empty($str)) $str = @$_ENV[$Name];
- return $str;
- }
- // Check if HTTP POST
- function ew_IsHttpPost() {
- $ct = ew_ServerVar("CONTENT_TYPE");
- if (empty($ct)) $ct = ew_ServerVar("HTTP_CONTENT_TYPE");
- return ($ct == "application/x-www-form-urlencoded");
- }
- // Get script name
- function ew_ScriptName() {
- $sn = ew_ServerVar("PHP_SELF");
- if (empty($sn)) $sn = ew_ServerVar("SCRIPT_NAME");
- if (empty($sn)) $sn = ew_ServerVar("ORIG_PATH_INFO");
- if (empty($sn)) $sn = ew_ServerVar("ORIG_SCRIPT_NAME");
- if (empty($sn)) $sn = ew_ServerVar("REQUEST_URI");
- if (empty($sn)) $sn = ew_ServerVar("URL");
- if (empty($sn)) $sn = "UNKNOWN";
- return $sn;
- }
- // Return multi-value search SQL
- function ew_GetMultiSearchSql(&$Fld, $FldVal) {
- $sWrk = "";
- $arVal = explode(",", $FldVal);
- foreach ($arVal as $sVal) {
- $sVal = trim($sVal);
- if (EW_IS_MYSQL) {
- $sSql = "FIND_IN_SET('" . ew_AdjustSql($sVal) . "', " . $Fld->FldExpression . ")";
- } else {
- if (count($arVal) == 1 || EW_SEARCH_MULTI_VALUE_OPTION == 3) {
- $sSql = $Fld->FldExpression . " = '" . ew_AdjustSql($sVal) . "' OR " . ew_GetMultiSearchSqlPart($Fld, $sVal);
- } else {
- $sSql = ew_GetMultiSearchSqlPart($Fld, $sVal);
- }
- }
- if ($sWrk <> "") {
- if (EW_SEARCH_MULTI_VALUE_OPTION == 2) {
- $sWrk .= " AND ";
- } elseif (EW_SEARCH_MULTI_VALUE_OPTION == 3) {
- $sWrk .= " OR ";
- }
- }
- $sWrk .= "($sSql)";
- }
- return $sWrk;
- }
- // Get multi search SQL part
- function ew_GetMultiSearchSqlPart(&$Fld, $FldVal) {
- return $Fld->FldExpression . " LIKE '" . ew_AdjustSql($FldVal) . ",%' OR " .
- $Fld->FldExpression . " LIKE '%," . $FldVal . ",%' OR " .
- $Fld->FldExpression . " LIKE '%," . $FldVal . "'";
- }
- // Get search sql
- function ew_GetSearchSql(&$Fld, $FldVal, $FldOpr, $FldCond, $FldVal2, $FldOpr2) {
- $sSql = "";
- if ($FldOpr == "BETWEEN") {
- $IsValidValue = ($Fld->FldDataType <> EW_DATATYPE_NUMBER) ||
- ($Fld->FldDataType == EW_DATATYPE_NUMBER && is_numeric($FldVal) && is_numeric($FldVal2));
- if ($FldVal <> "" && $FldVal2 <> "" && $IsValidValue)
- $sSql = $Fld->FldExpression . " BETWEEN " . ew_QuotedValue($FldVal, $Fld->FldDataType) .
- " AND " . ew_QuotedValue($FldVal2, $Fld->FldDataType);
- } elseif ($FldOpr == "IS NULL" || $FldOpr == "IS NOT NULL") {
- $sSql = $Fld->FldExpression . " " . $FldOpr;
- } else {
- $IsValidValue = ($Fld->FldDataType <> EW_DATATYPE_NUMBER) ||
- ($Fld->FldDataType == EW_DATATYPE_NUMBER && is_numeric($FldVal));
- if ($FldVal <> "" && $IsValidValue && ew_IsValidOpr($FldOpr, $Fld->FldDataType))
- $sSql = $Fld->FldExpression . ew_SearchString($FldOpr, $FldVal, $Fld->FldDataType);
- $IsValidValue = ($Fld->FldDataType <> EW_DATATYPE_NUMBER) ||
- ($Fld->FldDataType == EW_DATATYPE_NUMBER && is_numeric($FldVal2));
- if ($FldVal2 <> "" && $IsValidValue && ew_IsValidOpr($FldOpr2, $Fld->FldDataType)) {
- if ($sSql <> "")
- $sSql .= " " . (($FldCond == "OR") ? "OR" : "AND") . " ";
- $sSql = "(" . $sSql . $Fld->FldExpression . ew_SearchString($FldOpr2, $FldVal2, $Fld->FldDataType) . ")";
- }
- }
- return $sSql;
- }
- // Return search string
- function ew_SearchString($FldOpr, $FldVal, $FldType) {
- if ($FldOpr == "LIKE" || $FldOpr == "NOT LIKE") {
- return " $FldOpr " . ew_QuotedValue("%$FldVal%", $FldType);
- } elseif ($FldOpr == "STARTS WITH") {
- return " LIKE " . ew_QuotedValue("$FldVal%", $FldType);
- } else {
- return " $FldOpr " . ew_QuotedValue($FldVal, $FldType);
- }
- }
- // Check if valid operator
- function ew_IsValidOpr($Opr, $FldType) {
- $Valid = ($Opr == "=" || $Opr == "<" || $Opr == "<=" ||
- $Opr == ">" || $Opr == ">=" || $Opr == "<>");
- if ($FldType == EW_DATATYPE_STRING || $FldType == EW_DATATYPE_MEMO)
- $Valid = ($Valid || $Opr == "LIKE" || $Opr == "NOT LIKE" || $Opr == "STARTS WITH");
- return $Valid;
- }
- // quote field values
- function ew_QuotedValue($Value, $FldType) {
- if (is_null($Value)) return "NULL";
- switch ($FldType) {
- case EW_DATATYPE_STRING:
- case EW_DATATYPE_MEMO:
- case EW_DATATYPE_TIME:
- if (EW_REMOVE_XSS) {
- return "'" . ew_AdjustSql(ew_RemoveXSS($Value)) . "'";
- } else {
- return "'" . ew_AdjustSql($Value) . "'";
- }
- case EW_DATATYPE_BLOB:
- return "'" . ew_AdjustSql($Value) . "'";
- case EW_DATATYPE_DATE:
- return (EW_IS_MSACCESS) ? "#" . ew_AdjustSql($Value) . "#" :
- "'" . ew_AdjustSql($Value) . "'";
- case EW_DATATYPE_GUID:
- if (EW_IS_MSACCESS) {
- if (strlen($Value) == 38) {
- return "{guid " . $Value . "}";
- } elseif (strlen($Value) == 36) {
- return "{guid {" . $Value . "}}";
- }
- } else {
- return "'" . $Value . "'";
- }
- case EW_DATATYPE_BOOLEAN: // enum('Y'/'N') or enum('1'/'0')
- return "'" . $Value . "'";
- default:
- return $Value;
- }
- }
- // Convert different data type value
- function ew_Conv($v, $t) {
- switch ($t) {
- case 2:
- case 3:
- case 16:
- case 17:
- case 18:
- case 19: // adSmallInt/adInteger/adTinyInt/adUnsignedTinyInt/adUnsignedSmallInt
- return (is_null($v)) ? NULL : intval($v);
- case 4:
- Case 5:
- case 6:
- case 131: // adSingle/adDouble/adCurrency/adNumeric
- return (is_null($v)) ? NULL : (float)$v;
- default:
- return (is_null($v)) ? NULL : $v;
- }
- }
- // Convert string to float
- function ew_StrToFloat($v) {
- $v = str_replace(" ", "", $v);
- // Enter your code here, e.g.
- //$v = str_replace(",", ".", $v);
- if ($v <> "") $v = (float)$v;
- return $v;
- }
- // function for debug
- function ew_Trace($msg) {
- $filename = "debug.txt";
- if (!$handle = fopen($filename, 'a')) exit;
- if (is_writable($filename)) fwrite($handle, $msg . "\n");
- fclose($handle);
- }
- // function to compare values with special handling for null values
- function ew_CompareValue($v1, $v2) {
- if (is_null($v1) && is_null($v2)) {
- return TRUE;
- } elseif (is_null($v1) || is_null($v2)) {
- return FALSE;
- } else {
- return ($v1 == $v2);
- }
- }
- // Strip slashes
- function ew_StripSlashes($value) {
- if (!get_magic_quotes_gpc()) return $value;
- if (is_array($value)) {
- return array_map('ew_StripSlashes', $value);
- } else {
- return stripslashes($value);
- }
- }
- // Add slashes for SQL
- function ew_AdjustSql($val) {
- $val = addslashes(trim($val));
- return $val;
- }
- // Build SELECT SQL based on different sql part
- function ew_BuildSelectSql($sSelect, $sWhere, $sGroupBy, $sHaving, $sOrderBy, $sFilter, $sSort) {
- $sDbWhere = $sWhere;
- if ($sDbWhere <> "") {
- if ($sFilter <> "") $sDbWhere = "($sDbWhere) AND ($sFilter)";
- } else {
- $sDbWhere = $sFilter;
- }
- $sDbOrderBy = $sOrderBy;
- if ($sSort <> "") $sDbOrderBy = $sSort;
- $sSql = $sSelect;
- if ($sDbWhere <> "") $sSql .= " WHERE " . $sDbWhere;
- if ($sGroupBy <> "") $sSql .= " GROUP BY " . $sGroupBy;
- if ($sHaving <> "") $sSql .= " HAVING " . $sHaving;
- if ($sDbOrderBy <> "") $sSql .= " ORDER BY " . $sDbOrderBy;
- return $sSql;
- }
- // Executes the query, and returns the first column of the first row
- function ew_ExecuteScalar($SQL) {
- global $conn;
- if ($conn && $rs = $conn->Execute($SQL)) {
- if (!$rs->EOF && $rs->FieldCount() > 0)
- return $rs->fields[0];
- }
- return NULL;
- }
- // Write Audit Trail (login/logout)
- function ew_WriteAuditTrailOnLogInOut($logtype) {
- $table = $logtype;
- $sKey = "";
- // Write Audit Trail
- $filePfx = "log";
- $curDate = date("Y/m/d");
- $curTime = date("H:i:s");
- $id = ew_ScriptName();
- $curUser = CurrentUserName();
- $action = $logtype;
- ew_WriteAuditTrail($filePfx, $curDate, $curTime, $id, $curUser, $action, $table, "", "", "", "");
- }
- // Function for writing audit trail
- function ew_WriteAuditTrail($pfx, $curDate, $curTime, $id, $user, $action, $table, $field, $keyvalue, $oldvalue, $newvalue) {
- global $conn;
- $sTab = "\t";
- $userwrk = $user;
- if ($userwrk == "") $userwrk = "-1"; // assume Administrator if no user
- $sHeader = "date" . $sTab . "time" . $sTab . "id" .
- $sTab . "user" . $sTab . "action" . $sTab . "table" .
- $sTab . "field" . $sTab . "key value" . $sTab . "old value" .
- $sTab . "new value";
- $sMsg = $curDate . $sTab . $curTime . $sTab .
- $id . $sTab . $userwrk . $sTab .
- $action . $sTab . $table . $sTab .
- $field . $sTab . $keyvalue . $sTab .
- $oldvalue . $sTab . $newvalue;
- $sFolder = EW_AUDIT_TRAIL_PATH;
- $sFn = $pfx . "_" . date("Ymd") . ".txt";
- $filename = ew_UploadPathEx(TRUE, $sFolder) . $sFn;
- if (file_exists($filename)) {
- $fileHandler = fopen($filename, "a+b");
- } else {
- $fileHandler = fopen($filename, "a+b");
- fwrite($fileHandler,$sHeader."\r\n")…
Large files files are truncated, but you can click here to view the full file