FillPlaceholder() private static method

private static FillPlaceholder ( string strData, string strPlaceholder, string strReplaceWith, SprContext ctx ) : string
strData string
strPlaceholder string
strReplaceWith string
ctx SprContext
return string
コード例 #1
0
        private static string FillIfExists(string strData, string strPlaceholder,
                                           ProtectedString psParsable, PwEntry pwEntry, PwDatabase pwDatabase,
                                           SprContentFlags cf, uint uRecursionLevel, SprRefsCache vRefsCache)
        {
            if (strData == null)
            {
                Debug.Assert(false); return(string.Empty);
            }
            if (strPlaceholder == null)
            {
                Debug.Assert(false); return(strData);
            }
            if (strPlaceholder.Length == 0)
            {
                Debug.Assert(false); return(strData);
            }
            if (psParsable == null)
            {
                Debug.Assert(false); return(strData);
            }

            if (strData.IndexOf(strPlaceholder, SprEngine.ScMethod) >= 0)
            {
                return(SprEngine.FillPlaceholder(strData, strPlaceholder,
                                                 SprEngine.CompileInternal(psParsable.ReadString(), pwEntry,
                                                                           pwDatabase, null, uRecursionLevel + 1, vRefsCache), cf));
            }

            return(strData);
        }
コード例 #2
0
        private static string FillIfExists(string strData, string strPlaceholder,
                                           ProtectedString psParsable, PwEntry pwEntry, PwDatabase pwDatabase,
                                           SprContentFlags cf, uint uRecursionLevel, SprRefsCache vRefsCache)
        {
            // The UrlRemoveSchemeOnce property of cf must be cleared
            // before this method returns and before any recursive call
            bool bRemoveScheme = false;

            if (cf != null)
            {
                bRemoveScheme          = cf.UrlRemoveSchemeOnce;
                cf.UrlRemoveSchemeOnce = false;
            }

            if (strData == null)
            {
                Debug.Assert(false); return(string.Empty);
            }
            if (strPlaceholder == null)
            {
                Debug.Assert(false); return(strData);
            }
            if (strPlaceholder.Length == 0)
            {
                Debug.Assert(false); return(strData);
            }
            if (psParsable == null)
            {
                Debug.Assert(false); return(strData);
            }

            if (strData.IndexOf(strPlaceholder, SprEngine.ScMethod) >= 0)
            {
                string strReplacement = SprEngine.CompileInternal(
                    psParsable.ReadString(), pwEntry, pwDatabase, null,
                    uRecursionLevel + 1, vRefsCache);

                if (bRemoveScheme)
                {
                    strReplacement = UrlUtil.RemoveScheme(strReplacement);
                }

                return(SprEngine.FillPlaceholder(strData, strPlaceholder,
                                                 strReplacement, cf));
            }

            return(strData);
        }
コード例 #3
0
        private static string FillIfExists(string strData, string strPlaceholder,
                                           ProtectedString psParsable, SprContext ctx, uint uRecursionLevel)
        {
            // The UrlRemoveSchemeOnce property of ctx must be cleared
            // before this method returns and before any recursive call
            bool bRemoveScheme = false;

            if (ctx != null)
            {
                bRemoveScheme           = ctx.UrlRemoveSchemeOnce;
                ctx.UrlRemoveSchemeOnce = false;
            }

            if (strData == null)
            {
                Debug.Assert(false); return(string.Empty);
            }
            if (strPlaceholder == null)
            {
                Debug.Assert(false); return(strData);
            }
            if (strPlaceholder.Length == 0)
            {
                Debug.Assert(false); return(strData);
            }
            if (psParsable == null)
            {
                Debug.Assert(false); return(strData);
            }

            if (strData.IndexOf(strPlaceholder, SprEngine.ScMethod) >= 0)
            {
                string strReplacement = SprEngine.CompileInternal(
                    psParsable.ReadString(), ctx.WithoutContentTransformations(),
                    uRecursionLevel + 1);

                if (bRemoveScheme)
                {
                    strReplacement = UrlUtil.RemoveScheme(strReplacement);
                }

                return(SprEngine.FillPlaceholder(strData, strPlaceholder,
                                                 strReplacement, ctx));
            }

            return(strData);
        }
コード例 #4
0
ファイル: SprEngine.cs プロジェクト: sinelaw/keepass
        private static string FillEntryStringsSpecial(string str, SprContext ctx,
                                                      uint uRecursionLevel)
        {
            if ((str.IndexOf(UrlSpecialRmvScm, SprEngine.ScMethod) >= 0) ||
                (str.IndexOf(UrlSpecialScm, SprEngine.ScMethod) >= 0) ||
                (str.IndexOf(UrlSpecialHost, SprEngine.ScMethod) >= 0) ||
                (str.IndexOf(UrlSpecialPort, SprEngine.ScMethod) >= 0) ||
                (str.IndexOf(UrlSpecialPath, SprEngine.ScMethod) >= 0) ||
                (str.IndexOf(UrlSpecialQuery, SprEngine.ScMethod) >= 0))
            {
                SprContext ctxRaw = ctx.WithoutContentTransformations();
                string     strUrl = SprEngine.FillIfExists(@"{URL}", @"{URL}",
                                                           ctx.Entry.Strings.GetSafe(PwDefs.UrlField), ctxRaw, uRecursionLevel);

                str = SprEngine.FillPlaceholder(str, UrlSpecialRmvScm,
                                                UrlUtil.RemoveScheme(strUrl), ctx);

                try
                {
                    Uri uri = new Uri(strUrl);

                    str = SprEngine.FillPlaceholder(str, UrlSpecialScm,
                                                    uri.Scheme, ctx);
                    str = SprEngine.FillPlaceholder(str, UrlSpecialHost,
                                                    uri.Host, ctx);
                    str = SprEngine.FillPlaceholder(str, UrlSpecialPort,
                                                    uri.Port.ToString(NumberFormatInfo.InvariantInfo), ctx);
                    str = SprEngine.FillPlaceholder(str, UrlSpecialPath,
                                                    uri.AbsolutePath, ctx);
                    str = SprEngine.FillPlaceholder(str, UrlSpecialQuery,
                                                    uri.Query, ctx);
                }
                catch (Exception) { }                // Invalid URI
            }

            return(str);
        }
コード例 #5
0
ファイル: SprEngine.cs プロジェクト: evpobr/KeePass2
        private static string CompileInternal(string strText, SprContext ctx,
                                              uint uRecursionLevel)
        {
            if (strText == null)
            {
                Debug.Assert(false); return(string.Empty);
            }
            if (ctx == null)
            {
                Debug.Assert(false); ctx = new SprContext();
            }

            if (uRecursionLevel >= SprEngine.MaxRecursionDepth)
            {
                Debug.Assert(false);                 // Most likely a recursive reference
                return(string.Empty);                // Do not return strText (endless loop)
            }

            string   str = strText;
            MainForm mf  = Program.MainForm;

            bool bExt = ((ctx.Flags & (SprCompileFlags.ExtActive |
                                       SprCompileFlags.ExtNonActive)) != SprCompileFlags.None);

            if (bExt && (SprEngine.FilterCompilePre != null))
            {
                SprEventArgs args = new SprEventArgs(str, ctx.Clone());
                SprEngine.FilterCompilePre(null, args);
                str = args.Text;
            }

            if ((ctx.Flags & SprCompileFlags.Comments) != SprCompileFlags.None)
            {
                str = RemoveComments(str);
            }

            // The following realizes {T-CONV:/Text/Raw/}, which should be
            // one of the first transformations (except comments)
            if ((ctx.Flags & SprCompileFlags.TextTransforms) != SprCompileFlags.None)
            {
                str = PerformTextTransforms(str, ctx, uRecursionLevel);
            }

            if ((ctx.Flags & SprCompileFlags.Run) != SprCompileFlags.None)
            {
                str = RunCommands(str, ctx, uRecursionLevel);
            }

            if ((ctx.Flags & SprCompileFlags.AppPaths) != SprCompileFlags.None)
            {
                str = AppLocator.FillPlaceholders(str, ctx);
            }

            if (ctx.Entry != null)
            {
                if ((ctx.Flags & SprCompileFlags.PickChars) != SprCompileFlags.None)
                {
                    str = ReplacePickPw(str, ctx, uRecursionLevel);
                }

                if ((ctx.Flags & SprCompileFlags.EntryStrings) != SprCompileFlags.None)
                {
                    str = FillEntryStrings(str, ctx, uRecursionLevel);
                }

                if ((ctx.Flags & SprCompileFlags.EntryStringsSpecial) != SprCompileFlags.None)
                {
                    // ctx.UrlRemoveSchemeOnce = true;
                    // str = SprEngine.FillIfExists(str, @"{URL:RMVSCM}",
                    //	ctx.Entry.Strings.GetSafe(PwDefs.UrlField), ctx, uRecursionLevel);
                    // Debug.Assert(!ctx.UrlRemoveSchemeOnce);

                    str = FillEntryStringsSpecial(str, ctx, uRecursionLevel);
                }

                if (((ctx.Flags & SprCompileFlags.PasswordEnc) != SprCompileFlags.None) &&
                    (str.IndexOf(@"{PASSWORD_ENC}", SprEngine.ScMethod) >= 0))
                {
                    string strPwCmp = SprEngine.FillIfExists(@"{PASSWORD}",
                                                             @"{PASSWORD}", ctx.Entry.Strings.GetSafe(PwDefs.PasswordField),
                                                             ctx.WithoutContentTransformations(), uRecursionLevel);

                    str = SprEngine.FillPlaceholder(str, @"{PASSWORD_ENC}",
                                                    StrUtil.EncryptString(strPwCmp), ctx);
                }

                PwGroup pg = ctx.Entry.ParentGroup;
                if (((ctx.Flags & SprCompileFlags.Group) != SprCompileFlags.None) &&
                    (pg != null))
                {
                    str = FillGroupPlh(str, @"{GROUP", pg, ctx, uRecursionLevel);
                }
            }

            if ((ctx.Flags & SprCompileFlags.Paths) != SprCompileFlags.None)
            {
                if (mf != null)
                {
                    PwGroup pgSel = mf.GetSelectedGroup();
                    if (pgSel != null)
                    {
                        str = FillGroupPlh(str, @"{GROUP_SEL", pgSel, ctx, uRecursionLevel);
                    }
                }

                str = SprEngine.FillIfExists(str, @"{APPDIR}", new ProtectedString(
                                                 false, UrlUtil.GetFileDirectory(m_strAppExePath, false, false)),
                                             ctx, uRecursionLevel);
            }

            if (ctx.Database != null)
            {
                if ((ctx.Flags & SprCompileFlags.Paths) != SprCompileFlags.None)
                {
                    // For backward compatibility only
                    str = SprEngine.FillIfExists(str, @"{DOCDIR}", new ProtectedString(
                                                     false, UrlUtil.GetFileDirectory(ctx.Database.IOConnectionInfo.Path,
                                                                                     false, false)), ctx, uRecursionLevel);

                    str = SprEngine.FillIfExists(str, @"{DB_PATH}", new ProtectedString(
                                                     false, ctx.Database.IOConnectionInfo.Path), ctx, uRecursionLevel);
                    str = SprEngine.FillIfExists(str, @"{DB_DIR}", new ProtectedString(
                                                     false, UrlUtil.GetFileDirectory(ctx.Database.IOConnectionInfo.Path,
                                                                                     false, false)), ctx, uRecursionLevel);
                    str = SprEngine.FillIfExists(str, @"{DB_NAME}", new ProtectedString(
                                                     false, UrlUtil.GetFileName(ctx.Database.IOConnectionInfo.Path)),
                                                 ctx, uRecursionLevel);
                    str = SprEngine.FillIfExists(str, @"{DB_BASENAME}", new ProtectedString(
                                                     false, UrlUtil.StripExtension(UrlUtil.GetFileName(
                                                                                       ctx.Database.IOConnectionInfo.Path))), ctx, uRecursionLevel);
                    str = SprEngine.FillIfExists(str, @"{DB_EXT}", new ProtectedString(
                                                     false, UrlUtil.GetExtension(ctx.Database.IOConnectionInfo.Path)),
                                                 ctx, uRecursionLevel);
                }
            }

            if ((ctx.Flags & SprCompileFlags.Paths) != SprCompileFlags.None)
            {
                str = SprEngine.FillIfExists(str, @"{ENV_DIRSEP}", new ProtectedString(
                                                 false, Path.DirectorySeparatorChar.ToString()), ctx, uRecursionLevel);

                string strPF86 = Environment.GetEnvironmentVariable("ProgramFiles(x86)");
                if (string.IsNullOrEmpty(strPF86))
                {
                    strPF86 = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
                }
                if (strPF86 != null)
                {
                    str = SprEngine.FillIfExists(str, @"{ENV_PROGRAMFILES_X86}",
                                                 new ProtectedString(false, strPF86), ctx, uRecursionLevel);
                }
                else
                {
                    Debug.Assert(false);
                }
            }

            if ((ctx.Flags & SprCompileFlags.AutoType) != SprCompileFlags.None)
            {
                // Use Bksp instead of Del (in order to avoid Ctrl+Alt+Del);
                // https://sourceforge.net/p/keepass/discussion/329220/thread/4f1aa6b8/
                str = StrUtil.ReplaceCaseInsensitive(str, @"{CLEARFIELD}",
                                                     @"{HOME}+({END}){BKSP}{DELAY 50}");
            }

            if ((ctx.Flags & SprCompileFlags.DateTime) != SprCompileFlags.None)
            {
                DateTime dtNow = DateTime.UtcNow;
                str = SprEngine.FillIfExists(str, @"{DT_UTC_YEAR}", new ProtectedString(
                                                 false, dtNow.Year.ToString("D4")), ctx, uRecursionLevel);
                str = SprEngine.FillIfExists(str, @"{DT_UTC_MONTH}", new ProtectedString(
                                                 false, dtNow.Month.ToString("D2")), ctx, uRecursionLevel);
                str = SprEngine.FillIfExists(str, @"{DT_UTC_DAY}", new ProtectedString(
                                                 false, dtNow.Day.ToString("D2")), ctx, uRecursionLevel);
                str = SprEngine.FillIfExists(str, @"{DT_UTC_HOUR}", new ProtectedString(
                                                 false, dtNow.Hour.ToString("D2")), ctx, uRecursionLevel);
                str = SprEngine.FillIfExists(str, @"{DT_UTC_MINUTE}", new ProtectedString(
                                                 false, dtNow.Minute.ToString("D2")), ctx, uRecursionLevel);
                str = SprEngine.FillIfExists(str, @"{DT_UTC_SECOND}", new ProtectedString(
                                                 false, dtNow.Second.ToString("D2")), ctx, uRecursionLevel);
                str = SprEngine.FillIfExists(str, @"{DT_UTC_SIMPLE}", new ProtectedString(
                                                 false, dtNow.ToString("yyyyMMddHHmmss")), ctx, uRecursionLevel);

                dtNow = dtNow.ToLocalTime();
                str   = SprEngine.FillIfExists(str, @"{DT_YEAR}", new ProtectedString(
                                                   false, dtNow.Year.ToString("D4")), ctx, uRecursionLevel);
                str = SprEngine.FillIfExists(str, @"{DT_MONTH}", new ProtectedString(
                                                 false, dtNow.Month.ToString("D2")), ctx, uRecursionLevel);
                str = SprEngine.FillIfExists(str, @"{DT_DAY}", new ProtectedString(
                                                 false, dtNow.Day.ToString("D2")), ctx, uRecursionLevel);
                str = SprEngine.FillIfExists(str, @"{DT_HOUR}", new ProtectedString(
                                                 false, dtNow.Hour.ToString("D2")), ctx, uRecursionLevel);
                str = SprEngine.FillIfExists(str, @"{DT_MINUTE}", new ProtectedString(
                                                 false, dtNow.Minute.ToString("D2")), ctx, uRecursionLevel);
                str = SprEngine.FillIfExists(str, @"{DT_SECOND}", new ProtectedString(
                                                 false, dtNow.Second.ToString("D2")), ctx, uRecursionLevel);
                str = SprEngine.FillIfExists(str, @"{DT_SIMPLE}", new ProtectedString(
                                                 false, dtNow.ToString("yyyyMMddHHmmss")), ctx, uRecursionLevel);
            }

            if ((ctx.Flags & SprCompileFlags.References) != SprCompileFlags.None)
            {
                str = SprEngine.FillRefPlaceholders(str, ctx, uRecursionLevel);
            }

            if (((ctx.Flags & SprCompileFlags.EnvVars) != SprCompileFlags.None) &&
                (str.IndexOf('%') >= 0))
            {
                // Replace environment variables
                foreach (DictionaryEntry de in Environment.GetEnvironmentVariables())
                {
                    string strKey   = (de.Key as string);
                    string strValue = (de.Value as string);

                    if ((strKey != null) && (strValue != null))
                    {
                        str = SprEngine.FillIfExists(str, @"%" + strKey + @"%",
                                                     new ProtectedString(false, strValue), ctx, uRecursionLevel);
                    }
                    else
                    {
                        Debug.Assert(false);
                    }
                }
            }

            if ((ctx.Flags & SprCompileFlags.Env) != SprCompileFlags.None)
            {
                str = FillUriSpecial(str, ctx, @"{BASE", (ctx.Base ?? string.Empty),
                                     ctx.BaseIsEncoded, uRecursionLevel);
            }

            str = EntryUtil.FillPlaceholders(str, ctx, uRecursionLevel);

            if ((ctx.Flags & SprCompileFlags.PickChars) != SprCompileFlags.None)
            {
                str = ReplacePickChars(str, ctx, uRecursionLevel);
            }

            if (bExt && (SprEngine.FilterCompile != null))
            {
                SprEventArgs args = new SprEventArgs(str, ctx.Clone());
                SprEngine.FilterCompile(null, args);
                str = args.Text;
            }

            if (ctx.EncodeAsAutoTypeSequence)
            {
                str = StrUtil.NormalizeNewLines(str, false);
                str = str.Replace("\n", @"{ENTER}");
            }

            return(str);
        }