TransformContent() публичный статический Метод

public static TransformContent ( string strContent, SprContext ctx ) : string
strContent string
ctx SprContext
Результат string
Пример #1
0
        private static string Fill(string strData, string strPlaceholder,
                                   string strReplacement, SprContext ctx, uint?ouRecursionLevel)
        {
            if (strData == null)
            {
                Debug.Assert(false); return(string.Empty);
            }
            if (string.IsNullOrEmpty(strPlaceholder))
            {
                Debug.Assert(false); return(strData);
            }
            if (strReplacement == null)
            {
                Debug.Assert(false); strReplacement = string.Empty;
            }

            if (strData.IndexOf(strPlaceholder, SprEngine.ScMethod) < 0)
            {
                return(strData);
            }

            string strValue = strReplacement;

            if (ouRecursionLevel.HasValue)
            {
                strValue = SprEngine.CompileInternal(strValue, ((ctx != null) ?
                                                                ctx.WithoutContentTransformations() : null),
                                                     ouRecursionLevel.Value + 1);
            }

            return(StrUtil.ReplaceCaseInsensitive(strData, strPlaceholder,
                                                  SprEngine.TransformContent(strValue, ctx)));
        }
Пример #2
0
        private static string ReplacePickPwPlaceholder(string str,
                                                       string strPlaceholder, uint uCharCount, SprContext ctx,
                                                       uint uRecursionLevel)
        {
            if (str.IndexOf(strPlaceholder, StrUtil.CaseIgnoreCmp) < 0)
            {
                return(str);
            }

            ProtectedString ps = ctx.Entry.Strings.Get(PwDefs.PasswordField);

            if (ps != null)
            {
                string strPassword = ps.ReadString();

                string strPick = SprEngine.CompileInternal(strPassword,
                                                           ctx.WithoutContentTransformations(), uRecursionLevel + 1);

                if (!string.IsNullOrEmpty(strPick))
                {
                    ProtectedString psPick    = new ProtectedString(false, strPick);
                    string          strPicked = (CharPickerForm.ShowAndRestore(psPick,
                                                                               true, true, uCharCount, null) ?? string.Empty);

                    str = StrUtil.ReplaceCaseInsensitive(str, strPlaceholder,
                                                         SprEngine.TransformContent(strPicked, ctx));
                }
            }

            return(StrUtil.ReplaceCaseInsensitive(str, strPlaceholder, string.Empty));
        }
Пример #3
0
        private static string FillPlaceholder(string strData, string strPlaceholder,
                                              string strReplaceWith, SprContext ctx)
        {
            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 (strReplaceWith == null)
            {
                Debug.Assert(false); return(strData);
            }

            return(StrUtil.ReplaceCaseInsensitive(strData, strPlaceholder,
                                                  SprEngine.TransformContent(strReplaceWith, ctx)));
        }
Пример #4
0
        private static string FillRefPlaceholders(string strSeq, SprContext ctx,
                                                  uint uRecursionLevel)
        {
            if (ctx.Database == null)
            {
                return(strSeq);
            }

            string str = strSeq;

            int nOffset = 0;

            for (int iLoop = 0; iLoop < 20; ++iLoop)
            {
                str = ctx.RefCache.Fill(str, ctx);

                int nStart = str.IndexOf(StrRefStart, nOffset, SprEngine.ScMethod);
                if (nStart < 0)
                {
                    break;
                }
                int nEnd = str.IndexOf(StrRefEnd, nStart + 1, SprEngine.ScMethod);
                if (nEnd <= nStart)
                {
                    break;
                }

                string  strFullRef = str.Substring(nStart, nEnd - nStart + 1);
                char    chScan, chWanted;
                PwEntry peFound = FindRefTarget(strFullRef, ctx, out chScan, out chWanted);

                if (peFound != null)
                {
                    string strInsData;
                    if (chWanted == 'T')
                    {
                        strInsData = peFound.Strings.ReadSafe(PwDefs.TitleField);
                    }
                    else if (chWanted == 'U')
                    {
                        strInsData = peFound.Strings.ReadSafe(PwDefs.UserNameField);
                    }
                    else if (chWanted == 'A')
                    {
                        strInsData = peFound.Strings.ReadSafe(PwDefs.UrlField);
                    }
                    else if (chWanted == 'P')
                    {
                        strInsData = peFound.Strings.ReadSafe(PwDefs.PasswordField);
                    }
                    else if (chWanted == 'N')
                    {
                        strInsData = peFound.Strings.ReadSafe(PwDefs.NotesField);
                    }
                    else if (chWanted == 'I')
                    {
                        strInsData = peFound.Uuid.ToHexString();
                    }
                    else
                    {
                        nOffset = nStart + 1; continue;
                    }

                    if ((chWanted == 'P') && !ctx.ForcePlainTextPasswords &&
                        Program.Config.MainWindow.IsColumnHidden(AceColumnType.Password))
                    {
                        strInsData = PwDefs.HiddenPassword;
                    }

                    SprContext sprSub = ctx.WithoutContentTransformations();
                    sprSub.Entry = peFound;

                    string strInnerContent = SprEngine.CompileInternal(strInsData,
                                                                       sprSub, uRecursionLevel + 1);
                    strInnerContent = SprEngine.TransformContent(strInnerContent, ctx);

                    // str = str.Substring(0, nStart) + strInnerContent + str.Substring(nEnd + 1);
                    ctx.RefCache.Add(strFullRef, strInnerContent, ctx);
                    str = ctx.RefCache.Fill(str, ctx);
                }
                else
                {
                    nOffset = nStart + 1; continue;
                }
            }

            return(str);
        }
Пример #5
0
        private static string ReplacePickChars(string strText, SprContext ctx,
                                               uint uRecursionLevel)
        {
            if (ctx.Entry == null)
            {
                return(strText);                              // No assert
            }
            string str = strText;

            Dictionary <string, string> dPicked = new Dictionary <string, string>();

            while (true)
            {
                const string strStart = @"{PICKCHARS";

                int iStart = str.IndexOf(strStart, StrUtil.CaseIgnoreCmp);
                if (iStart < 0)
                {
                    break;
                }

                int iEnd = str.IndexOf('}', iStart);
                if (iEnd < 0)
                {
                    break;
                }

                string strPlaceholder = str.Substring(iStart, iEnd - iStart + 1);

                string strParam = str.Substring(iStart + strStart.Length,
                                                iEnd - (iStart + strStart.Length));

                string strRep  = string.Empty;
                bool   bEncode = true;

                if (strParam.Length == 0)
                {
                    strRep = ShowCharPickDlg(ctx.Entry.Strings.ReadSafe(
                                                 PwDefs.PasswordField), 0, null, ctx, uRecursionLevel);
                }
                else if (strParam.StartsWith(":"))
                {
                    string   strParams = strParam.Substring(1);
                    string[] vParams   = strParams.Split(new char[] { ':' },
                                                         StringSplitOptions.None);

                    string strField = string.Empty;
                    if (vParams.Length >= 1)
                    {
                        strField = (vParams[0] ?? string.Empty).Trim();
                    }
                    if (strField.Length == 0)
                    {
                        strField = PwDefs.PasswordField;
                    }

                    string strOptions = string.Empty;
                    if (vParams.Length >= 2)
                    {
                        strOptions = (vParams[1] ?? string.Empty);
                    }

                    Dictionary <string, string> dOptions = new Dictionary <string, string>();
                    string[] vOptions = strOptions.Split(new char[] { ',' },
                                                         StringSplitOptions.RemoveEmptyEntries);
                    foreach (string strOption in vOptions)
                    {
                        string[] vKvp = strOption.Split(new char[] { '=' },
                                                        StringSplitOptions.None);
                        if (vKvp.Length != 2)
                        {
                            continue;
                        }

                        dOptions[vKvp[0].Trim().ToLower()] = vKvp[1].Trim();
                    }

                    string strID = string.Empty;
                    if (dOptions.ContainsKey("id"))
                    {
                        strID = dOptions["id"].ToLower();
                    }

                    uint uCharCount = 0;
                    if (dOptions.ContainsKey("c"))
                    {
                        uint.TryParse(dOptions["c"], out uCharCount);
                    }
                    if (dOptions.ContainsKey("count"))
                    {
                        uint.TryParse(dOptions["count"], out uCharCount);
                    }

                    bool?bInitHide = null;
                    if (dOptions.ContainsKey("hide"))
                    {
                        bInitHide = StrUtil.StringToBool(dOptions["hide"]);
                    }

                    string strContent = ctx.Entry.Strings.ReadSafe(strField);
                    if (strContent.Length == 0)
                    {
                    }                                                  // Leave strRep empty
                    else if ((strID.Length > 0) && dPicked.ContainsKey(strID))
                    {
                        strRep = dPicked[strID];
                    }
                    else
                    {
                        strRep = ShowCharPickDlg(strContent, uCharCount, bInitHide,
                                                 ctx, uRecursionLevel);
                    }

                    if (strID.Length > 0)
                    {
                        dPicked[strID] = strRep;
                    }

                    if (dOptions.ContainsKey("conv"))
                    {
                        int iOffset = 0;
                        if (dOptions.ContainsKey("conv-offset"))
                        {
                            int.TryParse(dOptions["conv-offset"], out iOffset);
                        }

                        string strConvFmt = string.Empty;
                        if (dOptions.ContainsKey("conv-fmt"))
                        {
                            strConvFmt = dOptions["conv-fmt"];
                        }

                        string strConv = dOptions["conv"];
                        if (strConv.Equals("d", StrUtil.CaseIgnoreCmp))
                        {
                            strRep  = ConvertToDownArrows(strRep, iOffset, strConvFmt);
                            bEncode = false;
                        }
                    }
                }

                str = StrUtil.ReplaceCaseInsensitive(str, strPlaceholder,
                                                     bEncode ? SprEngine.TransformContent(strRep, ctx) : strRep);
            }

            return(str);
        }
Пример #6
0
        private static string FillRefPlaceholders(string strSeq, PwDatabase pwDatabase,
                                                  SprContentFlags cf, uint uRecursionLevel, SprRefsCache vRefsCache)
        {
            if (pwDatabase == null)
            {
                return(strSeq);
            }

            string str = strSeq;

            const string strStart = @"{REF:";
            const string strEnd   = @"}";

            int nOffset = 0;

            for (int iLoop = 0; iLoop < 20; ++iLoop)
            {
                str = SprEngine.FillRefsUsingCache(str, vRefsCache);

                int nStart = str.IndexOf(strStart, nOffset, SprEngine.ScMethod);
                if (nStart < 0)
                {
                    break;
                }
                int nEnd = str.IndexOf(strEnd, nStart, SprEngine.ScMethod);
                if (nEnd < 0)
                {
                    break;
                }

                string strFullRef = str.Substring(nStart, nEnd - nStart + 1);

                string strRef = str.Substring(nStart + strStart.Length, nEnd -
                                              nStart - strStart.Length);
                if (strRef.Length <= 4)
                {
                    nOffset = nStart + 1; continue;
                }
                if (strRef[1] != '@')
                {
                    nOffset = nStart + 1; continue;
                }
                if (strRef[3] != ':')
                {
                    nOffset = nStart + 1; continue;
                }

                char chScan   = char.ToUpper(strRef[2]);
                char chWanted = char.ToUpper(strRef[0]);

                SearchParameters sp = SearchParameters.None;
                sp.SearchString = strRef.Substring(4);
                if (chScan == 'T')
                {
                    sp.SearchInTitles = true;
                }
                else if (chScan == 'U')
                {
                    sp.SearchInUserNames = true;
                }
                else if (chScan == 'A')
                {
                    sp.SearchInUrls = true;
                }
                else if (chScan == 'P')
                {
                    sp.SearchInPasswords = true;
                }
                else if (chScan == 'N')
                {
                    sp.SearchInNotes = true;
                }
                else if (chScan == 'I')
                {
                    sp.SearchInUuids = true;
                }
                else if (chScan == 'O')
                {
                    sp.SearchInOther = true;
                }
                else
                {
                    nOffset = nStart + 1; continue;
                }

                PwObjectList <PwEntry> lFound = new PwObjectList <PwEntry>();
                pwDatabase.RootGroup.SearchEntries(sp, lFound, true);
                if (lFound.UCount > 0)
                {
                    PwEntry peFound = lFound.GetAt(0);

                    string strInsData;
                    if (chWanted == 'T')
                    {
                        strInsData = peFound.Strings.ReadSafe(PwDefs.TitleField);
                    }
                    else if (chWanted == 'U')
                    {
                        strInsData = peFound.Strings.ReadSafe(PwDefs.UserNameField);
                    }
                    else if (chWanted == 'A')
                    {
                        strInsData = peFound.Strings.ReadSafe(PwDefs.UrlField);
                    }
                    else if (chWanted == 'P')
                    {
                        strInsData = peFound.Strings.ReadSafe(PwDefs.PasswordField);
                    }
                    else if (chWanted == 'N')
                    {
                        strInsData = peFound.Strings.ReadSafe(PwDefs.NotesField);
                    }
                    else if (chWanted == 'I')
                    {
                        strInsData = peFound.Uuid.ToHexString();
                    }
                    else
                    {
                        nOffset = nStart + 1; continue;
                    }

                    string strInnerContent = SprEngine.CompileInternal(strInsData,
                                                                       peFound, pwDatabase, null, uRecursionLevel + 1, vRefsCache);
                    strInnerContent = SprEngine.TransformContent(strInnerContent, cf);

                    // str = str.Substring(0, nStart) + strInnerContent + str.Substring(nEnd + 1);
                    SprEngine.AddRefToCache(strFullRef, strInnerContent, vRefsCache);
                    str = SprEngine.FillRefsUsingCache(str, vRefsCache);
                }
                else
                {
                    nOffset = nStart + 1; continue;
                }
            }

            return(str);
        }