// ---------------------------------------------------------------------------------------------------------------- private static string CopyFile(string targetPath) { string fileName = "Sample.pdf"; string sourcePath = Variables.ExePath; string sourceFile = System.IO.Path.Combine(sourcePath, fileName); string destFile = System.IO.Path.Combine(targetPath, fileName); System.IO.Directory.CreateDirectory(targetPath); // overwrite the destination file if it already exists. System.IO.File.Copy(sourceFile, destFile, true); AppendLog.LogFile(1, "File:" + destFile + ", copied to checker destination"); return(destFile); }
///////////////////////////////////////////////////// // Send SMS powered by Twilio: //Arg1: recipient Phone Number (05X-1234567) <Recipient> //Arg2: Message of the SMS <Message> //Access Token is defined in CFG file //Arg: '/?' Shows all arguments //Written by Gonen Harel //////////////////////////////////////////////////// public bool SendSMS(string PhoneNumber, string MyMessage) { // Delete Error Log file if (File.Exists(Variables.errorLog)) { File.Delete(Variables.errorLog); } AppendLog.LogFile(1, "<Main - >SendSMS.exe Started"); if (PhoneNumber.Length > 9 && MyMessage.Length > 3) { try { PhoneNumber = FixPhoneNumber(PhoneNumber); TwilioClient.Init(accountSid, authToken); ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; //TLS 1.2 var message = MessageResource.Create(body: MyMessage, to: new Twilio.Types.PhoneNumber(PhoneNumber), messagingServiceSid: defaultMessagingServiceSid ); AppendLog.LogFile(1, "Message.Sid :" + message.Sid); Console.WriteLine(message.Sid); return(true); } catch (Exception ex) { Console.WriteLine("Exception: " + ex.ToString()); return(false); } } else { AppendLog.LogFile(2, "Phone Number or Message is Empty - No SMS sent"); Console.WriteLine("Warning: Phone Number or Message is Empty - No SMS Sent"); return(false); } }
static void Main() { AppendLog.LogFile(1, "<Main Program Started"); // Copy Temp File to checker folder string targetPath = Variables.ExePath + "SignChecker\\"; string DestFile = CopyFile(targetPath); string[] MyArgsArr = { DestFile, "spsmonitor", "Ss1234!", "110", "670", "50", "50", "0", "1", "I Approve", "", Variables.ExePath, "digitally-signed4.jpg", "1", "30000", "5", "http://seclog1.smartprinter.co.il:443/", "http://seclog2.smartprinter.co.il:443/" }; string resault = SecureSign.SignDoc(MyArgsArr); string[] sm = SMSMessagebuilder(resault); if (int.Parse(sm[0]) > 2000) { AppendLog.LogFile(3, "Fail to sign document"); Console.WriteLine("Fail to sign document"); // Error found in signature // Sms MyMessage = new Sms(); string [] PhoneNumbers = (Properties.Settings.Default.PhoneNumbers ?? "052-3721307,052-5403334,054-9330933").Split(','); string MyNum; foreach (string Num in PhoneNumbers) { MyNum = Sms.FixPhoneNumber(Num); if (MyMessage.SendSMS(MyNum, sm[1])) { AppendLog.LogFile(1, "Sent Error SMS message"); Console.WriteLine("Sent Error SMS message"); } else { AppendLog.LogFile(1, "Problem with sending SMS message"); Console.WriteLine("Problem with sending SMS message"); } } } }
public static string SignDoc(string[] myargs) { string szPdfPath = myargs[0]; int resaultCode = 0; string resaultStr = ""; //PdfDocument doc = PdfReader.Open(szPdfPath); if (!File.Exists(szPdfPath)) { AppendLog.LogFile(2, "SignDoc -- File: " + szPdfPath + " Not found!"); Console.WriteLine("SignDoc -- File: " + szPdfPath + " Not found!"); } else { PdfReader reader = new PdfReader(szPdfPath); double nPageWidth = 595; double nPageHeight = 842; string szUser = myargs[1]; string szPassword = myargs[2]; double nLeft = Convert.ToDouble(myargs[3]); double nTop = Convert.ToDouble(myargs[4]); double nWidth = Convert.ToDouble(myargs[5]); double nHeight = Convert.ToDouble(myargs[6]); bool bRelative = myargs[7] == "1" ? true : false; //bool bLastPage = myargs[8]=="1" ? true : false; int nPage = Convert.ToInt32(myargs[8]); string sPage = ""; switch (nPage) { case 1: sPage = "first"; break; case 0: sPage = "last"; break; case -1: sPage = "all"; break; default: sPage = "first"; break; } if (nPage == 0) { int pages = reader.NumberOfPages; iTextSharp.text.Rectangle rect = reader.GetPageSize(pages - 1); nPageWidth = rect.Width; nPageHeight = rect.Height; //nPageWidth = doc.Pages[doc.Pages.Count - 1].Width; //nPageHeight = doc.Pages[doc.Pages.Count - 1].Height; } else { iTextSharp.text.Rectangle rect = reader.GetPageSize(1); //nPageWidth = doc.Pages[0].Width; //nPageHeight = doc.Pages[0].Height; nPageWidth = rect.Width; nPageHeight = rect.Height; } reader.Close(); if (bRelative) { double k = nPageHeight / 841; nLeft *= k; nTop *= k; nWidth *= k; nHeight *= k; } string szReason = myargs[9]; string szLocation = myargs[10]; string szBy = myargs[11]; string szImagePath = myargs[12]; bool bImageOnly = myargs[13] == "1" ? true : false; Int32 nTimeout = Convert.ToInt32(myargs[14]); Int32 nTries = Convert.ToInt32(myargs[15]); //This is the entry point of string szPrimURL = myargs[16]; string szSecURL = myargs[17]; string token = null; //PSSignerAPI api = new PSSignerAPI(WEB_SERVER_URL); PSSignerAPI api = new PSSignerAPI(szPrimURL, szSecURL); //,nTimeout); try { // Optional - Call to check status APIStatusResult sr = api.Status(); if (sr.IsSuccess()) { Console.WriteLine("Service is UP , status: " + sr.status + ", Version: " + sr.version); } else { //HandleErrorResult(sr.); Console.WriteLine(sr.innerErrorMessage); resaultCode = 2002; resaultStr = sr.innerErrorMessage; return(resaultCode.ToString() + "," + resaultStr); } //Define the signature format, if null is send then the default settings will be read from the server account SigFormat sigFormat = new SigFormat(); sigFormat.Contact = szBy; // "Contact"; sigFormat.Reason = szReason; // "Reason"; //sigFormat.sigpage = bLastPage ? "last":sigFormat.sigpage = "first"; // values "first" or "last"(default) or "all" sigFormat.sigpage = sPage; sigFormat.Location = szLocation; // "IL"; byte[] data = File.ReadAllBytes(szImagePath); // @"c:\temp\signature.jpg"); sigFormat.ImageBase64 = Convert.ToBase64String(data); sigFormat.ImageOnly = bImageOnly; //Convert.ToInt32(myargs[13]); //This is sample settings for Lower Left sigFormat.xCordinate = 20; sigFormat.yCordinate = 150; sigFormat.imageHeight = 75; sigFormat.imageWidth = 75; string temp = null; byte[] buffer = File.ReadAllBytes(szPdfPath); /*Before calling the signing code application must call the login method. * After successfull login, the result contains a session key that needs * to be pass in every call to the API */ //Call to API login, this is needs to be done only once! //APIResult res = api.Login(APPLICATION_ID, APPLICATION_KEY); //APIExtendedResult APILoginResult res = api.Login(Variables.API_ID, Variables.API_CODE); //always check result! if (!res.IsSuccess()) { //this.HandleErrorResult(loginResult); //return; resaultStr = "API Login Failure "; Console.WriteLine(resaultStr); resaultCode = 2000; return(resaultCode.ToString() + "," + resaultStr);; } // Get the token to be used for further calls to the API token = res.token; //Open signing session - this is recommended in case of multiple signatures APIOpenSessionResult OpenSessionRes = api.OpenSession(token, szUser, //The user ID to sign the file szPassword // The pin for the user. ); //always check result! if (!OpenSessionRes.IsSuccess()) { resaultStr = "OpenSession Failure "; Console.WriteLine(resaultStr); resaultCode = 2003; return(resaultCode.ToString() + "," + resaultStr); } byte[] pdfFileContent = null; pdfFileContent = File.ReadAllBytes(szPdfPath); bool bSigned = false; for (int i = 0; i < nTries; i++) { //Call the sign funtion APISignResult apiRes = null; apiRes = api.Sign(token, new FileInfo(szPdfPath).Name, pdfFileContent, szUser, "0", szPassword, OPERATION_MODE.LOCAL_SIGN, sigFormat); //always check result! if (apiRes.IsSuccess()) { File.WriteAllBytes(szPdfPath, apiRes.buffer); //Console.WriteLine("Sign OK"); Console.WriteLine("File signed and saved to: " + szPdfPath); bSigned = true; break; } else { System.Threading.Thread.Sleep(nTimeout); //Console.WriteLine("Sign Failure "); //this.HandleErrorResult(apiRes); //Console.WriteLine("Unknown Error: Unable to sign file"); //return 2001; } } if (!bSigned) { resaultCode = 2001; resaultStr = "Unknown Error: Unable to sign file"; Console.WriteLine(resaultStr); return(resaultCode.ToString() + "," + resaultStr); } resaultCode = 0; resaultStr = "success signing document"; return(resaultCode.ToString() + "," + resaultStr); } catch (APIException ex) { Console.WriteLine(Message(ex)); resaultCode = (Int32)ex.HResult; // SpecificCode;//-2; resaultStr = ex.ToString(); return(resaultCode.ToString() + "," + resaultStr); } catch (Exception ex) { resaultCode = 2006; resaultStr = "Unknown Error: " + ex.Message; Console.WriteLine(resaultStr); return(resaultCode.ToString() + "," + ex.HResult); } finally { //Close session - no signatures for this session will be allowd if (resaultCode != 2002) { api.CloseSession(token, szUser); } else { resaultCode = 2002; resaultStr = "Timeout Error Serer is unavailable- could not sign document"; Console.WriteLine(resaultStr); resaultStr = resaultCode.ToString() + "," + resaultStr; } //Close session -invalidate the token and clean cach APILogoutResult logoutRes = api.Logout(token); if (!logoutRes.IsSuccess()) { Console.WriteLine("Logout Failure "); //this.HandleErrorResult(logoutRes); //return 2005; } } // ------------------------------------------------------------------------- } resaultCode = 2008; return(resaultCode.ToString()); }