";*/ // } // // function gets_backward($fp) // { // $ret_str=""; // $t=""; // while ($t != "\n") // { // if(0 != fseek($fp, $this->file_pos, SEEK_END)) // { // rewind($fp); // break; // } // $t = fgetc($fp); // // $ret_str = $t.$ret_str; // $this->file_pos --; // } // return $ret_str; // } // // function AppendLine($file_path,$insert_line) // { // $fp = fopen($file_path,"r+"); // // if(!$fp) // { // return false; // } // $all_lines=""; // // $this->file_pos = -1; // fseek($fp,$this->file_pos,SEEK_END); // // // while(1) // { // $pos = ftell($fp); // if($pos <= 0) // { // break; // } // $line = $this->gets_backward($fp); // $cmpline = trim($line); // // $all_lines .= $line; // // if(strcmp($cmpline,$this->get_footer())==0) // { // break; // } // } // // $all_lines = trim($all_lines); // $insert_line = trim($insert_line); // // $all_lines = "$insert_line\n$all_lines"; // // if(!fwrite($fp,$all_lines)) // { // return false; // } // // fclose($fp); // return true; // } // // function ReadNextLine($fp) // { // while(!feof($fp)) // { // $line = fgets($fp); // $line = trim($line); // // if(strcmp($line,$this->get_header())!=0 && // strcmp($line,$this->get_footer())!=0) // { // return $line; // } // } // return ""; // } //} // ////http://www.clker.com/blog/2008/03/27/creating-a-tar-gz-on-the-fly-using-php/ // //// Computes the unsigned Checksum of a file�s header //// to try to ensure valid file //// PRIVATE ACCESS FUNCTION // //function _sfm_computeUnsignedChecksum($bytestring) //{ // for($i=0; $i<512; $i++) // $unsigned_chksum += ord($bytestring[$i]); // for($i=0; $i<8; $i++) // $unsigned_chksum -= ord($bytestring[148 + $i]); // $unsigned_chksum += ord(" ") * 8; // // return $unsigned_chksum; //} // //// Generates a TAR file from the processed data //// PRIVATE ACCESS FUNCTION //function _sfm_tarSection($Name, $Data, $information=NULL) //{ // // Generate the TAR header for this file // // $header .= str_pad($Name,100,chr(0)); // $header .= str_pad("777",7,"0",STR_PAD_LEFT) . chr(0); // $header .= str_pad(decoct($information["user_id"]),7,"0",STR_PAD_LEFT) . chr(0); // $header .= str_pad(decoct($information["group_id"]),7,"0",STR_PAD_LEFT) . chr(0); // $header .= str_pad(decoct(strlen($Data)),11,"0",STR_PAD_LEFT) . chr(0); // $header .= str_pad(decoct(time(0)),11,"0",STR_PAD_LEFT) . chr(0); // $header .= str_repeat(" ",8); // $header .= "0"; // $header .= str_repeat(chr(0),100); // $header .= str_pad("ustar",6,chr(32)); // $header .= chr(32) . chr(0); // $header .= str_pad($information["user_name"],32,chr(0)); // $header .= str_pad($information["group_name"],32,chr(0)); // $header .= str_repeat(chr(0),8); // $header .= str_repeat(chr(0),8); // $header .= str_repeat(chr(0),155); // $header .= str_repeat(chr(0),12); // // // Compute header checksum // $checksum = str_pad(decoct(_sfm_computeUnsignedChecksum($header)),6,"0",STR_PAD_LEFT); // for($i=0; $i<6; $i++) { // $header[(148 + $i)] = substr($checksum,$i,1); // } // $header[154] = chr(0); // $header[155] = chr(32); // // // Pad file contents to byte count divisible by 512 // $file_contents = str_pad($Data,(ceil(strlen($Data) / 512) * 512),chr(0)); // // // Add new tar formatted data to tar file contents // $tar_file = $header . $file_contents; // // return $tar_file; //} // //function sfm_targz($Name, $Data) //{ // return gzencode(_sfm_tarSection($Name,$Data),9); //} // //class FM_DBUtil //{ // private $connection; // public $fields; // // private $error_handler; // private $logger; // private $config; // private $logged_in; // // public function __construct() // { // $this->fields = array(); // $this->logged_in = false; // } // // function Init(&$config,&$logger,&$error_handler) // { // $this->error_handler = &$error_handler; // $this->logger = &$logger; // $this->config = &$config; // } // // function AddField($fieldname,$fieldtype,$dispname='') // { // array_push($this->fields,array('name'=>$fieldname,'type'=>$fieldtype,'dispname'=>$dispname)); // } // function GetFieldDetails($fieldname) // {//PreviousProgrammer_TaichiparkWordPress_to_do: can be optimized using name as key // foreach($this->fields as $formfield) // { // if($formfield['name'] == $fieldname) // { // return $formfield; // } // } // return null; // } // // function GetFields() // { // return $this->fields; // } // function HandleError($error_str,$extra='') // { // $this->error_handler->HandleConfigError($error_str,$this->GetError()." $extra"); // } // // function Login() // { // if(true === $this->logged_in) // { // return true; // } // // if( $this->config->passwords_encrypted ) // { // $pwd = sfm_crypt_decrypt($this->config->fmdb_pwd,$this->config->encr_key); // } // else // { // $pwd = $this->config->fmdb_pwd; // } // // $this->connection = mysqli_connect($this->config->fmdb_host,$this->config->fmdb_username,$pwd, $this->config->fmdb_database); // // if(!$this->connection) // { // $this->error_handler->HandleConfigError("Database Login failed! \nPlease make sure that the DB login credentials provided are correct \n". // mysqli_error($this->connection)); // return false; // } // // if(!mysqli_query($this->connection, "SET NAMES 'UTF8'")) // { // $this->HandleError('Error setting utf8 encoding'); // return false; // } // // if(!empty($this->config->default_timezone) && $this->config->default_timezone != 'default') // { // if(!mysqli_query($this->connection, "SET SESSION time_zone = '".$this->config->default_timezone."'")) // { // //$this->logger->LogError('Error setting default time zone in DB'); // $this->HandleError('Error setting TimeZone. Your Mysql server does not have timezone database.'); // return false; // } // } // $this->logged_in = true; // // return true; // } // // function GetConnection() // { // return $this->connection; // } // // function Close() // { // mysqli_close($this->connection); // } // // function GetError() // { // return mysqli_error($this->connection); // } // // function CreateTable($tablename,$fields) // { // $qry = "Create Table $tablename ". // "(ID INT AUTO_INCREMENT PRIMARY KEY,"; // // foreach($fields as $formfield) // { // $qry .= " ".$formfield['name']." ".$formfield['type'].","; // } // $qry = trim($qry,','); // // $qry .=") DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;"; // // if(!mysqli_query($this->connection,$qry)) // { // $this->HandleError('Error creating the table ',"\nquery was\n $qry"); // return false; // } // return true; // // } // // function AlterTable($tablename, $fieldstoadd,$changedfields) // { // $qry="ALTER TABLE $tablename "; // // foreach($fieldstoadd as $field) // { // $qry .= " ADD COLUMN ".$field['name']." ".$field['type'].",\n"; // } // // foreach($changedfields as $field) // { // $qry .= " CHANGE COLUMN ".$field['name']." ".$field['name']." ".$field['type'].",\n"; // } // // $qry = trim($qry,",\n"); // // $this->logger->LogInfo("Altering table $tablename query is: $qry "); // if(!mysqli_query($this->connection,$qry)) // { // $this->HandleError('Error altering the table',"\nquery was\n $qry"); // return false; // } // // return true; // // } // // function EscapeValue($str,$type) // { // if( function_exists( "mysqli_real_escape_string" ) ) // { // $ret_str = mysqli_real_escape_string($this->connection, $str ); // } // else // { // $ret_str = addslashes( $str ); // } // // $type = strtolower($type); // if(strpos($type,'varchar') !== FALSE || // strpos($type,'blob') !== FALSE || // strpos($type,'text') !== FALSE // ) // { // // $ret_str = "'".$ret_str."'"; // } // elseif(strpos($type,'date') !== FALSE) // { // $ret_str = "'".$str."'"; // } // else // {//numeric types // $strTmp = trim($str); // if(!isset($strTmp) || $strTmp === '') // { // $ret_str='NULL'; // } // } // return $ret_str; // } // // function Ensuretable($tablename, $fields=null) // { // if(null == $fields) // { // $fields = $this->fields; // } // // $result = @mysqli_query($this->connection, "SHOW COLUMNS FROM $tablename"); // if(!$result || mysqli_num_rows($result) <= 0) // { // return $this->CreateTable($tablename, $fields); // } // // $non_existantfields = array(); // $changedfields = array(); // // foreach($fields as $formfield) // { // $field_found=false; // $field_changed=false; // while ($row = mysqli_fetch_assoc($result)) // { // if(strcasecmp($row['Field'],$formfield['name'])==0) // { // $field_found = true; // if(strcasecmp($row['Type'],$formfield['type']) !=0) // { // $field_changed = true; // } // break; // } // } // // if(!$field_found) // { // array_push($non_existantfields,$formfield); // } // elseif($field_changed) // { // array_push($changedfields,$formfield); // } // mysqli_data_seek($result,0); // } // // if(!empty($non_existantfields) || !empty($changedfields)) // { // $this->logger->LogInfo("Need to alter table; fields have changed"); // return $this->AlterTable($tablename,$non_existantfields,$changedfields); // } // // return true; // } // // function TruncateTable($tablename) // { // if(!mysqli_query($this->connection, "Truncate table $tablename")) // { // return false; // } // return true; // } // // function IsRowExisting($tablename,$row_hash) // { // $field_list=''; // // foreach($row_hash as $field => $val) // { // $field_list .= "$field='"; // $field_list .= mysqli_real_escape_string($this->connection, $val); // $field_list .= "' and "; // } // $field_list = rtrim($field_list,'and '); // $qry ="SELECT Count(*) FROM $tablename WHERE $field_list"; // // $count = $this->GetSingleValue($qry); // if(false === $count) // { // return false; // } // return ($count > 0)?true:false; // } // // function Insert($tablename,$row_hash) // { // $field_list=''; // $value_list=''; // foreach($row_hash as $field => $val) // { // $field_list .= $field.','; // $val = mysqli_real_escape_string($this->connection, $val); // $value_list .= "'$val',"; // } // $field_list = rtrim($field_list,','); // $value_list = rtrim($value_list,','); // $qry = "INSERT INTO $tablename ($field_list) VALUES ($value_list)"; // // $this->logger->LogInfo("Insert Query: $qry"); // // if(!mysqli_query($this->connection, $qry)) // { // $this->HandleError("Error inserting data to the table $qry \n". // mysqli_error($this->connection)); // return false; // } // $id = mysqli_insert_id($this->connection); // return $id; // } // // function DeleteFromTable($table,$where) // { // $result = mysqli_query($this->connection, "DELETE FROM $table WHERE $where"); // if(!$result) // { // return false; // } // return true; // } // // function UpdateTable($table,$values,$where) // { // $result = mysqli_query($this->connection, "UPDATE $table Set $values WHERE $where"); // // if(!$result) // { // return false; // } // return true; // } // // function IsTableExisting($tablename) // { // $result = mysqli_query($this->connection, "SHOW COLUMNS FROM $tablename"); // if(!$result || mysqli_num_rows($result) <= 0) // { // return false; // } // return true; // } // // function GetCount($tablename) // { // $qry = "Select Count(*) From $tablename"; // return $this->GetSingleValue($qry); // } // // function GetSingleValue($qry) // { // $result = mysqli_query($this->connection, $qry); // if(!$result || mysqli_num_rows($result) <= 0) // { // return false; // } // $row = mysqli_fetch_row($result); // return $row[0]; // } // // function GetRecords($tablename,$fields,$where,$sidx,$sord,$start,$limit) // { // $where_clause=''; // if(!empty($where)) // { // $where_clause = "Where $where"; // } // $qry = "Select $fields From $tablename $where_clause Order By $sidx $sord Limit $start,$limit"; // // $rows = array(); // // if(false === $this->RunQuery($qry,$rows)) // { // return false; // } // return $rows; // } // // function ReadData($tablename,&$rows,$where='',$fields=false) // { // $strfields='*'; // if(false !== $fields && !empty($fields)) // { // $strfields = implode(',',$fields); // } // // $qry = "Select $strfields From $tablename"; // if(!empty($where)) // { // $qry .= " Where $where"; // } // $qry .= " Order by ID asc"; // // return $this->RunQuery($qry,$rows); // } // // function RunQuery($qry,&$rows) // { // $result = mysqli_query($this->connection, $qry); // // if(!$result) // { // $this->HandleError("Error running query $qry \n" // .mysqli_error($this->connection)); // return false; // } // $rows = array(); // // while($rec = mysqli_fetch_assoc($result)) // { // $rows[] = $rec; // } // // return true; // } // // //} // //class FM_SimpleDB extends FM_Module //{ // private $dbutil; // private $connection; // private $file_uploader; // private $uniquefields; // // // public function __construct($tablename) // { // parent::__construct(); // $this->dbutil = new FM_DBUtil(); // $this->connection=null; // $this->tablename = $tablename; // $this->uniquefields = array(); // } // function GetTableName() // { // return $this->tablename; // } // function AddUniqueFields() // { // $args = func_get_args(); // $this->uniquefields = array_merge($this->uniquefields,$args); // } // function OnInit() // { // $this->dbutil->Init($this->config,$this->logger,$this->error_handler); // } // // function SetFileUploader(&$file_uploader) // { // $this->file_uploader = &$file_uploader; // } // // function AddField($fieldname,$fieldtype,$dispname='') // { // $this->dbutil->AddField($fieldname,$fieldtype,$dispname); // } // // function HandleError($error_str,$extra='') // { // $this->error_handler->HandleConfigError($error_str,$this->GetError()." $extra"); // } // // function Login() // { // $ret = $this->dbutil->Login(); // if($ret) // { // $this->connection = $this->dbutil->GetConnection(); // } // return $ret; // } // // function Close() // { // mysqli_close($this->connection); // } // // function GetError() // { // return mysqli_error($this->connection); // } // // function Ensuretable() // { // return $this->dbutil->Ensuretable($this->tablename); // } // // function GetFieldValue($var_name,$field_type) // { // $field_value =''; // // $field_type = strtolower($field_type); // // if($this->config->element_info->GetType($var_name) == "datepicker" && // ($field_type == "datetime" || $field_type == "date")) // { // $date_obj = new FM_DateObj($this->formvars,$this->config,$this->logger); // $date_value = $date_obj->GetDateFieldInStdForm($var_name); // $this->logger->LogInfo("Saving to DB; date in std form: $date_value"); // // $field_value = $this->dbutil->EscapeValue($date_value,$field_type); // } // elseif(($this->config->submission_time_var == $var_name|| // $this->config->submission_date_var == $var_name) && // ($field_type == "datetime" || $field_type == "date")) // { // $field_value = 'NOW()'; // } // else // { // $field_value_x = $this->common_objs->formvar_mx->GetFieldValueAsString($var_name,/*$use_disp_var*/false); // $field_value = $this->dbutil->EscapeValue($field_value_x,$field_type); // } // return $field_value; // } // // // // function InsertDataInTable() // { // $qry ="INSERT INTO $this->tablename ("; // // $values =""; // foreach($this->dbutil->fields as $formfield) // { // $qry .= $formfield['name'].","; // // $value = $this->GetFieldValue($formfield['name'],$formfield['type']); // $values .= "$value,"; // } // $qry = trim($qry,","); // $values = trim($values,","); // // $qry .=") VALUES ("; // $qry .= $values; // $qry .= ");"; // // // if(!mysqli_query($this->connection, $qry)) // { // $this->HandleError('Error inserting data to the table',"\nquery:$qry"); // return false; // } // return true; // // } // // function Install(&$continue) // { // if(!$this->Login()) // { // $continue = false; // return false; // } // // if(!$this->Ensuretable()) // { // $continue = false; // return false; // } // return true; // } // // function DoAppCommand($cmd,$val,&$app_command_obj) // { // $ret=false; // switch($cmd) // { // case 'db_get_rec_count': // { // $this->GetRecCount($app_command_obj->response_sender); // $ret=true; // } // break; // // case 'db_get_recs': // { // $this->GetDBRecs($app_command_obj->response_sender,$val); // $ret=true; // } // break; // } // return $ret; // } // // //ajax commands from the admin page // function AfterVariablessInitialized() // { // // if(!empty($_GET['sfm_adminpage'])) // { // if('recs' == $_GET['sfm_adminpage']) // { // if(!empty($_GET['getfields'])) // { // $this->GetFieldsJSON(); // } // else if(!empty($_GET['getrec'])) // { // $this->GetSingleRecJSON($_GET['getrec']); // } // else if(!empty($_GET['sfm_save_grid_opts'])) // { // $this->SaveGridOptions(); // } // else if(!empty($_GET['printrec'])) // { // $this->ShowPrintablePage($_GET['printrec']); // } // else // { // $this->GetRecordsJSON(); // } // return false;//handled // } // elseif('db-csv' == $_GET['sfm_adminpage']) // { // $attachments = false; // if(!empty($_POST['attachments'])) // { // if($_POST['attachments'] == 'yes') // { // $attachments = true; // } // } // $this->get_csv_download($attachments); // return false;//handled // } // } // elseif(!empty($_GET['sfm_check_unique'])) // { // $uniquefield = $this->getUniqueFieldName(); // if(false === $uniquefield || empty($_GET[$uniquefield])) // { // echo 'success'; // return false;//handled // } // // $uniquevalue = $_GET[$uniquefield]; // // if(true === $this->IsFieldUnique($uniquefield,$uniquevalue)) // { // echo 'success'; // } // else // { // echo 'msg_failed'; // } // return false;//handled // } // return true; // } // // function Process(&$continue) // { // if(!$this->Login()) // { // $continue = false; // return false; // } // if(!$this->Ensuretable()) // { // $continue = false; // return false; // } // // if(NULL != $this->file_uploader) // { // $this->file_uploader->SaveUploadedFile(); // } // // if(!$this->InsertDataInTable()) // { // $continue = false; // return false; // } // // return true; // } // // function ValidateInstallation(&$app_command_obj) // { // if(false === $app_command_obj->TestDBLogin()) // { // return false; // } // $continue=false; // //make sure Table is present and all fields are installed // if(false === $this->Install($continue)) // { // $this->logger->LogInfo("SimpleDB ValidateInstallation : Install returns false"); // } // return true; // } // // function get_csv_download($attachments) // { // if(!$this->Login()) // { // $error = 'Loging in to the Database failed'; // $this->logger->LogError($error); // echo $error; // return; // } // $rec_count = $this->dbutil->GetSingleValue("SELECT Count(*) from $this->tablename"); // // if(false === $rec_count || $rec_count <= 0) // { // $error = 'No records in the table'; // $this->logger->LogError($error); // echo $error; // return; // } // // header('Content-type: application/x-tar'); // $downloadname = $this->formname.'-db-'.date("Y-m-d").'.tar.gz'; // header('Content-disposition: attachment;filename='.$downloadname); // // $limit = 10000; // for($r=0; $r < $rec_count ; $r += $limit) // { // $file_response = new FM_Response($this->config,$this->logger); // if(false === $this->GetRecs($file_response,$r,$limit)) // { // $this->logger->LogError("Error while exporting records"); // break; // } // $filename = $this->formname; // // if($r>0){ $filename .='-'.$r;} // $filename .= '.csv'; // // $file = "\xEF\xBB\xBF".$file_response->getResponseStr();//utf-8 BOM for MS Excel // echo sfm_targz($filename,$file); // } // // if(true == $attachments && NULL != $this->file_uploader) // { // $this->file_uploader->AttachFilesToDownload(); // } // } // // function getUniqueFieldName() // { // foreach ($this->uniquefields as $field) // { // if(isset($_GET[$field])){ return $field; } // } // return false; // } // // function IsFieldUnique($field_name,$field_value) // { // if(empty($field_value)){ return true; }//required validation should be separate // if(!$this->Login()) // { // $this->logger->LogError("IsFieldUnique: Failed logging in to database"); // return true; // } // $result = @mysqli_query($this->connection,"SELECT Count(*) from $this->tablename WHERE $field_name='$field_value'"); // if(!$result || mysqli_num_rows($result) <= 0) // { // $this->logger->LogInfo("IsFieldUnique: ".$this->GetError()); // return true; // } // $row = mysqli_fetch_row($result); // if($row[0] <= 0) // { // return true; // } // return false; // } // // function GetRecCount(&$response_sender) // { // if(!$this->Login()) // { // $response_sender->addError('Loging in to the Database failed'); // return false; // } // // $result = mysqli_query($this->connection,"SELECT Count(*) from $this->tablename"); // // if(!$result || mysqli_num_rows($result) <= 0) // { // $response_sender->addError( // "Failed fetching the number of rows from the table : ".$this->GetError()); // return false; // } // // $row = mysqli_fetch_row($result); // // $resp = "count:".$row[0]; // // $response_sender->SetResponse($resp); // } // // function GetDBRecs(&$response_sender,$val) // { // $parts = explode(',',$val); // $offset=$parts[0]; // $count=$parts[1]; // $this->GetRecs($response_sender,$offset,$count); // } // // function GetRecs(&$response_sender,$offset,$rec_count) // { // if(!$this->Login()) // { // $response_sender->addError('Log-in to the Database failed'); // return false; // } // // $sel_expr =""; // // $db_field_list = $this->getDBFormatFields(); // // $sel_expr = implode($db_field_list,','); // // $qry = "Select $sel_expr FROM $this->tablename LIMIT $offset,$rec_count"; // // $result = mysqli_query($this->connection, $qry); // // if(!$result ) // { // $response_sender->addError( // "Failed fetching records from the table : ".$this->GetError()); // return false; // } // // $response =''; // $row=''; // foreach($this->dbutil->fields as $formfield) // { // $row .= $formfield['name'].","; // } // $row = trim($row,','); // // $response .= "$row\n"; // // while($rec = mysqli_fetch_assoc($result)) // { // $row=''; // foreach($this->dbutil->fields as $formfield) // { // $fname = $formfield['name']; // $row .= sfm_csv_escape($rec[$fname]).","; // } // $row = trim($row,','); // $response .= "$row\n"; // } // // $response_sender->SetEncrypt(false); // $response_sender->SetResponse($response); // } // // /* Admin Page Ajax Queries */ // function GetFieldsJSON() // { // echo json_encode($this->getFieldArray()); // return true; // } // // function compareGridOpts($a,$b) // { // if($a['s_order'] == $b['s_order']){ return 0;} // // return ($a['s_order'] > $b['s_order']) ? 1 : -1; // } // // function GetGridOptions() // { // $cookie_var = $this->formname.'_sfm_grid_options'; // // if(empty($_COOKIE[$cookie_var])) // { // $this->logger->LogInfo("GetGridOptions COOKIE $cookie_var is empty!"); // return false; // } // $opts = stripslashes($_COOKIE[$cookie_var]); // // return unserialize($opts); // } // function SaveGridOptions() // { // $expire = time() + 60*60*24*30; // // $grid_opts = array(); // // $grid_opts['colorder'] = $_POST['colorder']; // $grid_opts['colwidths'] = $_POST['colwidths']; // $grid_opts['colvisible'] = $_POST['colvisible']; // // $cookie_var = $this->formname.'_sfm_grid_options'; // // $cookval = serialize($grid_opts); // $ret = setcookie($cookie_var,$cookval,$expire); // // } // // function CreateGridForDB() // { // $grid4db = new FM_GridForDB(); // $grid4db->Init($this->config,$this->logger,$this->error_handler,$this->ext_module); // $grid4db->Login(); // return $grid4db; // } // // function ShowPrintablePage($id) // { // $table_code = "\n
$dispname | $val |