Ejemplo n.º 1
0
        internal static string CompileUrl(string url, PwEntry entry, string baseUrl)
        {
            PwDatabase database = null;

            try
            {
                database = Program.MainForm.DocumentManager.SafeFindContainerOf(entry);
            }
            catch (Exception)
            {
                Debug.Assert(false);
            }

            url = url.TrimStart(new char[] { ' ', '\t', '\r', '\n' });

            bool bEncodeAsAutoTypeSequence = WinUtil.IsCommandLineUrl(url);

            var context = new SprContext(entry, database, SprCompileFlags.All, false, bEncodeAsAutoTypeSequence)
            {
                Base          = baseUrl,
                BaseIsEncoded = false
            };

            return(SprEngine.Compile(url, context));
        }
Ejemplo n.º 2
0
        private void OnMenuOpening(object sender, EventArgs e)
        {
            if (m_dynMenu == null)
            {
                Debug.Assert(false); return;
            }

            if (m_lOpenWith == null)
            {
                CreateOpenWithList();
            }

            PwEntry[] v = Program.MainForm.GetSelectedEntries();
            if (v == null)
            {
                v = new PwEntry[0];
            }

            bool bCanOpenWith = true;
            uint uValidUrls   = 0;

            foreach (PwEntry pe in v)
            {
                string strUrl = pe.Strings.ReadSafe(PwDefs.UrlField);
                if (string.IsNullOrEmpty(strUrl))
                {
                    continue;
                }

                ++uValidUrls;
                bCanOpenWith &= !WinUtil.IsCommandLineUrl(strUrl);
            }
            if ((v.Length == 0) || (uValidUrls == 0))
            {
                bCanOpenWith = false;
            }

            foreach (OpenWithItem it in m_lOpenWith)
            {
                it.MenuItem.Enabled = bCanOpenWith;
            }
        }
Ejemplo n.º 3
0
        private static void ExportEntry(PwEntry pe, string strDir, PwExportInfo pxi)
        {
            PwDatabase pd  = ((pxi != null) ? pxi.ContextDatabase : null);
            SprContext ctx = new SprContext(pe, pd, SprCompileFlags.NonActive, false, false);

            KeyValuePair <string, string>?okvpCmd = null;
            string strUrl = SprEngine.Compile(pe.Strings.ReadSafe(PwDefs.UrlField), ctx);

            if (WinUtil.IsCommandLineUrl(strUrl))
            {
                strUrl = WinUtil.GetCommandLineFromUrl(strUrl);

                if (!NativeLib.IsUnix())                // LNKs only supported on Windows
                {
                    string strApp, strArgs;
                    StrUtil.SplitCommandLine(strUrl, out strApp, out strArgs);

                    if (!string.IsNullOrEmpty(strApp))
                    {
                        okvpCmd = new KeyValuePair <string, string>(strApp, strArgs);
                    }
                }
            }
            if (string.IsNullOrEmpty(strUrl))
            {
                return;
            }
            bool bLnk = okvpCmd.HasValue;

            string strTitleCmp = SprEngine.Compile(pe.Strings.ReadSafe(PwDefs.TitleField), ctx);

            if (string.IsNullOrEmpty(strTitleCmp))
            {
                strTitleCmp = KPRes.Entry;
            }
            string strTitle = Program.Config.Defaults.WinFavsFileNamePrefix + strTitleCmp;

            string strSuffix = Program.Config.Defaults.WinFavsFileNameSuffix +
                               (bLnk ? ".lnk" : ".url");

            strSuffix = UrlUtil.FilterFileName(strSuffix);

            string strFileBase = (UrlUtil.EnsureTerminatingSeparator(strDir,
                                                                     false) + UrlUtil.FilterFileName(strTitle));
            string strFile = strFileBase + strSuffix;
            int    iFind   = 2;

            while (File.Exists(strFile))
            {
                strFile = strFileBase + " (" + iFind.ToString() + ")" + strSuffix;
                ++iFind;
            }

            if (!Directory.Exists(strDir))
            {
                try { Directory.CreateDirectory(strDir); }
                catch (Exception exDir)
                {
                    throw new Exception(strDir + MessageService.NewParagraph + exDir.Message);
                }

                WaitForDirCommit(strDir, true);
            }

            try
            {
                if (bLnk)
                {
                    int    ccMaxDesc = NativeMethods.INFOTIPSIZE - 1 - LnkDescSuffix.Length;
                    string strDesc   = StrUtil.CompactString3Dots(strUrl, ccMaxDesc) +
                                       LnkDescSuffix;

                    ShellLinkEx sl = new ShellLinkEx(okvpCmd.Value.Key,
                                                     okvpCmd.Value.Value, strDesc);
                    sl.Save(strFile);
                }
                else
                {
                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine(@"[InternetShortcut]");
                    sb.AppendLine(@"URL=" + strUrl);                     // No additional line break
                    sb.AppendLine(@"[" + PwDefs.ShortProductName + @"]");
                    sb.AppendLine(IniTypeKey + @"=" + IniTypeValue);
                    // Terminating line break is important

                    File.WriteAllText(strFile, sb.ToString(), Encoding.Default);
                }
            }
            catch (Exception exWrite)
            {
                throw new Exception(strFile + MessageService.NewParagraph + exWrite.Message);
            }
        }
Ejemplo n.º 4
0
        private static string RunCommands(string strText, SprContext ctx,
                                          uint uRecursionLevel)
        {
            string        str = strText;
            int           iStart;
            List <string> lParams;

            while (ParseAndRemovePlhWithParams(ref str, ctx, uRecursionLevel,
                                               @"{CMD:", out iStart, out lParams, false))
            {
                if (lParams.Count == 0)
                {
                    continue;
                }

                string strBaseRaw = null;
                if ((ctx != null) && (ctx.Base != null))
                {
                    if (ctx.BaseIsEncoded)
                    {
                        strBaseRaw = UntransformContent(ctx.Base, ctx);
                    }
                    else
                    {
                        strBaseRaw = ctx.Base;
                    }
                }

                string strCmd = WinUtil.CompileUrl((lParams[0] ?? string.Empty),
                                                   ((ctx != null) ? ctx.Entry : null), true, strBaseRaw, true);
                if (WinUtil.IsCommandLineUrl(strCmd))
                {
                    strCmd = WinUtil.GetCommandLineFromUrl(strCmd);
                }
                if (string.IsNullOrEmpty(strCmd))
                {
                    continue;
                }

                Process p = null;
                try
                {
                    StringComparison sc = StrUtil.CaseIgnoreCmp;

                    string strOpt = ((lParams.Count >= 2) ? lParams[1] :
                                     string.Empty);
                    Dictionary <string, string> d = SplitParams(strOpt);

                    ProcessStartInfo psi = new ProcessStartInfo();

                    string strApp, strArgs;
                    StrUtil.SplitCommandLine(strCmd, out strApp, out strArgs);
                    if (string.IsNullOrEmpty(strApp))
                    {
                        continue;
                    }
                    psi.FileName = strApp;
                    if (!string.IsNullOrEmpty(strArgs))
                    {
                        psi.Arguments = strArgs;
                    }

                    string strMethod  = GetParam(d, "m", "s");
                    bool   bShellExec = !strMethod.Equals("c", sc);
                    psi.UseShellExecute = bShellExec;

                    string strO    = GetParam(d, "o", (bShellExec ? "0" : "1"));
                    bool   bStdOut = strO.Equals("1", sc);
                    if (bStdOut)
                    {
                        psi.RedirectStandardOutput = true;
                    }

                    string strWS = GetParam(d, "ws", "n");
                    if (strWS.Equals("h", sc))
                    {
                        psi.CreateNoWindow = true;
                        psi.WindowStyle    = ProcessWindowStyle.Hidden;
                    }
                    else if (strWS.Equals("min", sc))
                    {
                        psi.WindowStyle = ProcessWindowStyle.Minimized;
                    }
                    else if (strWS.Equals("max", sc))
                    {
                        psi.WindowStyle = ProcessWindowStyle.Maximized;
                    }
                    else
                    {
                        Debug.Assert(psi.WindowStyle == ProcessWindowStyle.Normal);
                    }

                    string strVerb = GetParam(d, "v", null);
                    if (!string.IsNullOrEmpty(strVerb))
                    {
                        psi.Verb = strVerb;
                    }

                    bool bWait = GetParam(d, "w", "1").Equals("1", sc);

                    p = NativeLib.StartProcessEx(psi);
                    if (p == null)
                    {
                        Debug.Assert(false); continue;
                    }

                    if (bStdOut)
                    {
                        string strOut = (p.StandardOutput.ReadToEnd() ?? string.Empty);

                        // Remove trailing new-line characters, like $(...);
                        // https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
                        // https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html#Command-Substitution
                        strOut = strOut.TrimEnd('\r', '\n');

                        strOut = TransformContent(strOut, ctx);
                        str    = str.Insert(iStart, strOut);
                    }

                    if (bWait)
                    {
                        p.WaitForExit();
                    }
                }
                catch (Exception ex)
                {
                    string strMsg = strCmd + MessageService.NewParagraph + ex.Message;
                    MessageService.ShowWarning(strMsg);
                }
                finally
                {
                    try { if (p != null)
                          {
                              p.Dispose();
                          }
                    }
                    catch (Exception) { Debug.Assert(false); }
                }
            }

            return(str);
        }