using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Drawing; using DataLibrary; using System.Data; using System.Diagnostics; using System.Security; using System.Net.Mail; using System.Text; using CoreSavingLibrary; using EncryptDecryptEngine; using System.IO; using System.Threading; namespace Saving { public partial class CheckApprove : System.Web.UI.Page { public string ExecuteCmd(string Arguments, string user, string password, string domain) { return ExecuteCommand("cmd", Arguments, user, password, domain); } public string ExecuteCommand(string command, string Arguments, string user, string password, string domain) { string output = null; Process p = new Process(); ProcessStartInfo s = new ProcessStartInfo(); if (domain != null || domain != "") s.Domain = domain; if (user != null || user != "") s.UserName = user; if (password != null || password != "") { s.Password = new SecureString(); char[] passwords = password.ToCharArray(); for (int i = 0; i < password.Length; i++) { s.Password.AppendChar(passwords[i]); } } s.FileName = command; s.UseShellExecute = false; s.RedirectStandardOutput = true; s.RedirectStandardError = true; s.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; if (Arguments != null && Arguments != "") s.Arguments = "/C \"" + Arguments+"\""; p.StartInfo = s; p.EnableRaisingEvents = true; try { p.Start(); while (!p.HasExited) { System.Threading.Thread.Sleep(1000); } //check to see what the exit code was if (p.ExitCode != 0) { output = "Exitcode: " + p.ExitCode + " - Err1: " + p.StandardError + " - Executor: " + System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString(); } else { output = "Command Result: " + p.StandardOutput.ReadToEnd(); } } catch (Exception ex) { output += ex.Message; } return output; } XmlConfigService xmlconfig; protected void Page_Load(object sender, EventArgs e) { xmlconfig = new XmlConfigService(WebUtil.GetGcoopPath()); //xmlconfig.ConnectionStringData.Rows[0].Connection; String datasource = "Data Source=rac/gcoop;Persist Security Info=True;User ID=ifsct;Password=ifsct;Unicode=True;"; datasource = (Request["d"] != null) ?( WebUtil.Decrypt(Request["d"],Request["coopid"]) ): datasource; if (TbConnectionString.Text == null || TbConnectionString.Text.Trim() == "") { TbConnectionString.Text = datasource; } LbServerMessage.Text = ""; String createCheckList = (Request["c"] != null) ? Request["c"] : ""; if (createCheckList != "") { this.setCurrentProfile(Request["coopid"], Request["appid"], Request["gid"], Request["pid"], Request["rid"], Request["uid"], Request["wd"], datasource); } getCurrentImg(); } public void getCurrentImg() { if (this.screencapture.Value.Length == 0) { try { Thread.Sleep(2000); string folderpath = WebUtil.GetGcoopPathCore() + "\\Saving\\JsCss\\html2canvas.save\\"; //string[] fileNames = Directory.GetFiles(filepath, "*.png"); //DateTime[] creationTimes = new DateTime[fileNames.Length]; //for (int i = 0; i < fileNames.Length; i++) // creationTimes[i] = new FileInfo(fileNames[i]).CreationTime; //Array.Sort(creationTimes, fileNames); DirectoryInfo dir = new DirectoryInfo(folderpath); FileInfo[] files = dir.GetFiles().OrderByDescending(p => p.CreationTime).ToArray(); this.screencapture.Value = files[0].FullName; this.screencaptureurl.Value = files[0].FullName.Replace(WebUtil.GetGcoopPathCore(), WebUtil.GetSavingUrlCore()).Replace("Saving/Saving", "Saving"); } catch { } } } public void setCurrentProfile(String coopid, String appid, String gid, String pid, String rid, String uid, String wd, String datasource) { //LbServerMessage.Text = "datasource=" + datasource + ", appid=" + appid + ",gid=" + gid + ",pid=" + pid + ",rid=" + rid + ",uid=" + uid; this.datasource.Value = datasource; this.coopid.Value = coopid; this.appid.Value=appid; this.gid.Value=gid; this.pid.Value = pid; this.rid.Value = rid; this.uid.Value = uid; this.wd.Value = wd; } public static int sendGMail(string fromAddress, string fromPassword, string[] toAddress, string subject, string body) { //กรณีผู้ส่งเป็น gmail ต้อง Set lesssecure = on ที่ Link https://www.google.com/settings/security/lesssecureapps return sendMail(fromAddress, fromPassword, toAddress, subject, body, "smtp.gmail.com", 587); } public static int sendMail(string fromAddress, string fromPassword, string[] toAddress, string subject, string body, string host, int port) { string your_id = fromAddress; string your_password = fromPassword; try { SmtpClient client = new SmtpClient { Host = host, Port = port, EnableSsl = true, DeliveryMethod = SmtpDeliveryMethod.Network, Credentials = new System.Net.NetworkCredential(your_id, your_password), Timeout = 10000, }; MailMessage mm = new MailMessage(); mm.From = new MailAddress(your_id); for (int i = 0; i < toAddress.Length; i++) { mm.To.Add(toAddress[i]); } mm.BodyEncoding = UTF8Encoding.UTF8; mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure; mm.IsBodyHtml = true; //mm. = MailFormat.Html; mm.Subject = subject; mm.Body = body; client.Send(mm); Console.WriteLine("Email Sent"); return 1; } catch (Exception e) { Console.WriteLine("Could not end email\n\n" + e.ToString()); return -1; } } protected void BtCheck_Click(object sender, EventArgs e) { string username = TbUsername.Text.Trim().Replace("'", ""); string password = TbPassword.Text.Replace("'", ""); LbServerMessage.Text = ""; string connectionString = this.datasource.Value; Sta ta = new Sta(connectionString); Sta taq = new Sta(connectionString); ta.Transection(); this.approve_flag.Value = "0"; try { String sql = ""; string passEncrypt = Encryption.UserPassword(password); string sqlLogIn = @" select a.coop_id, c.coop_control, a.user_name, a.freez_flag from amsecusers a, cmcoopmaster c where a.coop_id = c.coop_id and a.coop_id='{0}' and a.user_name='{1}' and a.password='{2}' "; sqlLogIn = string.Format(sqlLogIn, this.coopid.Value, username, passEncrypt); Sdt dtLogIn = ta.Query(sqlLogIn); if (dtLogIn.Next() == false) { LbServerMessage.Text = DateTime.Now + " ตรวจสอบ รหัสผู้ใช้ หรือ รหัสผ่านไม่ถูกต้อง "; LbServerMessage.ForeColor = Color.Red; } else { sql = @"select w.* from amsecwins w, amsecpermiss p where w.window_id='" + this.pid.Value + @"' and w.application='" + this.appid.Value + @"' and p.apv_flag=1 and p.coop_id='" + this.coopid.Value + @"' and p.application=w.application and w.window_id=p.window_id "; Sdt dt = taq.Query(sql); if (dt.Next() == false) { LbServerMessage.Text = DateTime.Now + " ไม่มีสิทธิ์ในการอนุมัติให้ทำรายการ "; LbServerMessage.ForeColor = Color.Red; } else { if (Sta.IS_MYSQL_MODE) { sql = "CREATE TABLE AMSECAPVLOG (pk_id VARCHAR(150) NOT NULL,coopid VARCHAR(20) NOT NULL, application VARCHAR(50) NOT NULL, group_id VARCHAR(50) NOT NULL, window_id VARCHAR(50) NOT NULL, user_name VARCHAR(50) NOT NULL, approve_name VARCHAR(50) NOT NULL, operate_date datetime,approve_date datetime, remote_ip VARCHAR(50) NOT NULL,remark VARCHAR(150)) "; try { ta.Exe(sql); } catch { } sql = "ALTER TABLE AMSECAPVLOG ADD ( CONSTRAINT AMSECAPVLOG_PK PRIMARY KEY (pk_id,coopid,APPLICATION, WINDOW_ID )) "; try { ta.Exe(sql); } catch { } sql = "ALTER TABLE AMSECAPVLOG ADD ( IMG VARCHAR(255) NOT NULL ) "; try { ta.Exe(sql); } catch { } } else { sql = "CREATE TABLE AMSECAPVLOG (pk_id VARCHAR2(150) NOT NULL,coopid VARCHAR2(20) NOT NULL, application VARCHAR2(50) NOT NULL, group_id VARCHAR2(50) NOT NULL, window_id VARCHAR2(50) NOT NULL, user_name VARCHAR2(50) NOT NULL, approve_name VARCHAR2(50) NOT NULL, operate_date date,approve_date date, remote_ip VARCHAR2(50) NOT NULL,remark VARCHAR2(150)) "; try { ta.Exe(sql); }catch { } sql = "ALTER TABLE AMSECAPVLOG ADD ( CONSTRAINT AMSECAPVLOG_PK PRIMARY KEY (pk_id,coopid,APPLICATION, WINDOW_ID )) "; try { ta.Exe(sql); } catch { } sql = "ALTER TABLE AMSECAPVLOG ADD ( IMG VARCHAR2(255) NOT NULL ) "; try { ta.Exe(sql); } catch { } } string folderpath = WebUtil.GetGcoopPathCore() + "\\Saving\\JsCss\\html2canvas.save\\"; string capturesrc = this.screencapture.Value; string capturedesc = this.screencapture.Value.Replace("\\Saving\\JsCss\\html2canvas.save\\", "\\Saving\\Image\\captures\\"); File.Move(capturesrc, capturedesc); DirectoryInfo di = new DirectoryInfo(folderpath); foreach (FileInfo file in di.GetFiles()){ file.Delete(); } sql = "insert into AMSECAPVLOG (pk_id ,coopid , application , group_id , window_id , user_name , approve_name ,operate_date ,approve_date , remote_ip ,img,remark )values "; sql += "('" + DateTime.Now.Ticks + "' ,'" + this.coopid.Value + "' ,'" + this.appid.Value + "' , '" + this.gid.Value + "' , '" + this.pid.Value + "' , '" + this.uid.Value + "' , '" + username + "' ,to_date('" + this.wd.Value + "','yyyyMMdd') ,sysdate , '" + Request.UserHostAddress + "','" + capturedesc + "','" + this.postfunction.Value + "' )"; try { ta.Exe(sql); } catch { } LbServerMessage.Text = DateTime.Now + " อนุมัติสิทธิ์สำเร็จ "; LbServerMessage.ForeColor = Color.Green; this.approve_flag.Value = "1"; ta.Commit(); } } ta.Close(); taq.Close(); } catch (Exception ex) { try { ta.RollBack(); } catch { } ta.Close(); LbServerMessage.Text = ex.Message; LbServerMessage.ForeColor = Color.Red; } } protected void Button1_Click(object sender, EventArgs e) { } } }