// //==================================================================================================== /// <summary> /// executes an addon with the name or guid provided, in the context specified. /// </summary> /// <param name="addonNameOrGuid"></param> /// <param name="addonContext"></param> /// <returns></returns> public string executeAddon(string addonNameOrGuid, CPUtilsBaseClass.addonContext addonContext = CPUtilsBaseClass.addonContext.ContextSimple) { try { if (GenericController.isGuid(addonNameOrGuid)) { // // -- call by guid AddonModel addon = DbBaseModel.create <AddonModel>(core.cpParent, addonNameOrGuid); if (addon == null) { throw new GenericException("Addon [" + addonNameOrGuid + "] could not be found."); } else { return(core.addon.execute(addon, new CPUtilsBaseClass.addonExecuteContext { addonType = addonContext, errorContextMessage = "external call to execute addon [" + addonNameOrGuid + "]" })); } } else { AddonModel addon = AddonModel.createByUniqueName(core.cpParent, addonNameOrGuid); if (addon != null) { // // -- call by name return(core.addon.execute(addon, new CPUtilsBaseClass.addonExecuteContext { addonType = addonContext, errorContextMessage = "external call to execute addon [" + addonNameOrGuid + "]" })); } else if (addonNameOrGuid.isNumeric()) { // // -- compatibility - call by id return(executeAddon(GenericController.encodeInteger(addonNameOrGuid), addonContext)); } else { throw new GenericException("Addon [" + addonNameOrGuid + "] could not be found."); } } } catch (Exception ex) { Site.ErrorReport(ex); } return(string.Empty); }
// //================================================================================================================= /// <summary> /// convert a single command in the command formats to call the execute /// </summary> private static string executeSingleCommand(CoreController core, string cmdSrc, CPUtilsBaseClass.addonContext Context) { try { // // accumulator gets the result of each cmd, then is passed to the next command to filter List <object> cmdCollection = null; Dictionary <string, object> cmdDef = null; Dictionary <string, object> cmdArgDef = new Dictionary <string, object>(); var json = new System.Web.Script.Serialization.JavaScriptSerializer(); // cmdSrc = cmdSrc.Trim(' '); string whiteChrs = Environment.NewLine + "\t "; bool trimming; do { trimming = false; int trimLen = cmdSrc.Length; if (trimLen > 0) { string leftChr = cmdSrc.left(1); string rightChr = cmdSrc.Substring(cmdSrc.Length - 1); if (GenericController.strInstr(1, whiteChrs, leftChr) != 0) { cmdSrc = cmdSrc.Substring(1); trimming = true; } if (GenericController.strInstr(1, whiteChrs, rightChr) != 0) { cmdSrc = cmdSrc.left(cmdSrc.Length - 1); trimming = true; } } } while (trimming); string CmdAccumulator = ""; if (!string.IsNullOrEmpty(cmdSrc)) { Dictionary <string, object> .KeyCollection dictionaryKeys = null; object itemObject = null; object itemVariant = null; Dictionary <string, object> cmdObject = null; // cmdCollection = new List <object>(); if ((cmdSrc.left(1) == "{") && (cmdSrc.Substring(cmdSrc.Length - 1) == "}")) { // // JSON is a single command in the form of an object, like: ( "import" : "test.html" ) // Dictionary <string, object> cmdDictionary; try { cmdDictionary = json.Deserialize <Dictionary <string, object> >(cmdSrc); } catch (Exception ex) { LogController.logError(core, ex); throw; } // dictionaryKeys = cmdDictionary.Keys; foreach (string KeyWithinLoop in dictionaryKeys) { if (cmdDictionary[KeyWithinLoop] != null) { cmdObject = new Dictionary <string, object>(); itemObject = cmdDictionary[KeyWithinLoop]; cmdObject.Add(KeyWithinLoop, itemObject); cmdCollection.Add(cmdObject); } else { cmdObject = new Dictionary <string, object>(); itemVariant = cmdDictionary[KeyWithinLoop]; cmdObject.Add(KeyWithinLoop, itemVariant); cmdCollection.Add(cmdObject); } } } else if ((cmdSrc.left(1) == "[") && (cmdSrc.Substring(cmdSrc.Length - 1) == "]")) { // // JSON is a command list in the form of an array, like: [ "clear" , { "import": "test.html" },{ "open" : "myfile.txt" }] // cmdCollection = json.Deserialize <List <object> >(cmdSrc); } else { // // a single text command without JSON wrapper, like // open myfile.html // open "myfile.html" // "open" "myfile.html" // "content box" // all other posibilities are syntax errors // string cmdText = cmdSrc.Trim(' '); string cmdArg = ""; if (cmdText.left(1) == "\"") { // // cmd is quoted // "open" // "Open" file // "Open" "file" // int Pos = GenericController.strInstr(2, cmdText, "\""); if (Pos <= 1) { throw new GenericException("Error parsing content command [" + cmdSrc + "], expected a close quote around position " + Pos); } else { if (Pos == cmdText.Length) { // // cmd like "open" // cmdArg = ""; cmdText = cmdText.Substring(1, Pos - 2); } else if (cmdText.Substring(Pos, 1) != " ") { // // syntax error, must be a space between cmd and argument // throw new GenericException("Error parsing content command [" + cmdSrc + "], expected a space between command and argument around position " + Pos); } else { cmdArg = (cmdText.Substring(Pos)).Trim(' '); cmdText = cmdText.Substring(1, Pos - 2); } } } else { // // no quotes, can be // open // open file // int Pos = GenericController.strInstr(1, cmdText, " "); if (Pos > 0) { cmdArg = cmdSrc.Substring(Pos); cmdText = (cmdSrc.left(Pos - 1)).Trim(' '); } } if (cmdArg.left(1) == "\"") { // // cmdarg is quoted // int Pos = GenericController.strInstr(2, cmdArg, "\""); if (Pos <= 1) { throw new GenericException("Error parsing JSON command list, expected a quoted command argument, command list [" + cmdSrc + "]"); } else { cmdArg = cmdArg.Substring(1, Pos - 2); } } if ((cmdArg.left(1) == "{") && (cmdArg.Substring(cmdArg.Length - 1) == "}")) { // // argument is in the form of an object, like: ( "text name": "my text" ) // object cmdDictionaryOrCollection = json.Deserialize <object>(cmdArg); string cmdDictionaryOrCollectionTypeName = cmdDictionaryOrCollection.GetType().FullName.ToLowerInvariant(); if (cmdDictionaryOrCollectionTypeName.left(37) != "system.collections.generic.dictionary") { throw new GenericException("Error parsing JSON command argument list, expected a single command, command list [" + cmdSrc + "]"); } else { // // create command array of one command // cmdCollection.Add(cmdDictionaryOrCollection); } cmdDef = new Dictionary <string, object> { { cmdText, cmdDictionaryOrCollection } }; cmdCollection = new List <object> { cmdDef }; } else { // // command and arguments are strings // cmdDef = new Dictionary <string, object> { { cmdText, cmdArg } }; cmdCollection = new List <object> { cmdDef }; } } // // execute the commands in the JSON cmdCollection // foreach (object cmd in cmdCollection) { // // repeat for all commands in the collection: // convert each command in the command array to a cmd string, and a cmdArgDef dictionary // each cmdStringOrDictionary is a command. It may be: // A - "command" // B - { "command" } // C - { "command" : "single-default-argument" } // D - { "command" : { "name" : "The Name"} } // E - { "command" : { "name" : "The Name" , "secondArgument" : "secondValue" } } // string cmdTypeName = cmd.GetType().FullName.ToLowerInvariant(); string cmdText = ""; if (cmdTypeName == "system.string") { // // case A & B, the cmdDef is a string // cmdText = (string)cmd; cmdArgDef = new Dictionary <string, object>(); } else if (cmdTypeName.left(37) == "system.collections.generic.dictionary") { // // cases C-E, (0).key=cmd, (0).value = argument (might be string or object) // cmdDef = (Dictionary <string, object>)cmd; if (cmdDef.Count != 1) { // // syntax error // } else { string cmdDefKey = cmdDef.Keys.First(); string cmdDefValueTypeName = cmdDef[cmdDefKey].GetType().FullName.ToLowerInvariant(); // // command is the key for these cases // cmdText = cmdDefKey; if (cmdDefValueTypeName == "system.string") { // // command definition with default argument // cmdArgDef = new Dictionary <string, object> { { "default", cmdDef[cmdDefKey] } }; } else if ((cmdDefValueTypeName == "dictionary") || (cmdDefValueTypeName == "dictionary(of string,object)") || (cmdTypeName.left(37) == "system.collections.generic.dictionary")) { cmdArgDef = (Dictionary <string, object>)cmdDef[cmdDefKey]; } else { // // syntax error, bad command // throw new GenericException("Error parsing JSON command list, , command list [" + cmdSrc + "]"); } } } else { // // syntax error // throw new GenericException("Error parsing JSON command list, , command list [" + cmdSrc + "]"); } // // execute the cmd with cmdArgDef dictionary // switch (GenericController.toLCase(cmdText)) { case "textbox": { // // Opens a textbox addon (patch for text box name being "text name" so it requies json)copy content record // // arguments // name: copy content record // default // name // CmdAccumulator = ""; string ArgName = ""; foreach (KeyValuePair <string, object> kvp in cmdArgDef) { switch (kvp.Key.ToLowerInvariant()) { case "name": case "default": ArgName = (string)kvp.Value; break; } } if (!string.IsNullOrEmpty(ArgName)) { CmdAccumulator = core.html.getContentCopy(ArgName, "copy content", core.session.user.id, true, core.session.isAuthenticated); } break; } case "opencopy": { // // Opens a copy content record // // arguments // name: layout record name // default // name // CmdAccumulator = ""; string ArgName = ""; foreach (KeyValuePair <string, object> kvp in cmdArgDef) { switch (kvp.Key.ToLowerInvariant()) { case "name": case "default": ArgName = (string)kvp.Value; break; } } if (!string.IsNullOrEmpty(ArgName)) { CmdAccumulator = core.html.getContentCopy(ArgName, "copy content", core.session.user.id, true, core.session.isAuthenticated); } break; } case "openlayout": { // // Opens a layout record // // arguments // name: layout record name // default // name // CmdAccumulator = ""; string ArgName = ""; foreach (KeyValuePair <string, object> kvp in cmdArgDef) { switch (kvp.Key.ToLowerInvariant()) { case "name": case "default": ArgName = (string)kvp.Value; break; } } if (!string.IsNullOrEmpty(ArgName)) { DataTable dt = core.db.executeQuery("select layout from ccLayouts where name=" + DbController.encodeSQLText(ArgName)); if (dt != null) { CmdAccumulator = GenericController.encodeText(dt.Rows[0]["layout"]); } dt.Dispose(); } break; } case "open": { // // Opens a file in the wwwPath // // arguments // name: filename // default // name // CmdAccumulator = ""; string ArgName = ""; foreach (KeyValuePair <string, object> kvp in cmdArgDef) { switch (kvp.Key.ToLowerInvariant()) { case "name": case "default": ArgName = (string)kvp.Value; break; } } if (!string.IsNullOrEmpty(ArgName)) { CmdAccumulator = core.wwwFiles.readFileText(ArgName); } break; } case "userproperty": case "user": { CmdAccumulator = ""; string ArgName = ""; foreach (KeyValuePair <string, object> kvp in cmdArgDef) { switch (kvp.Key.ToLowerInvariant()) { case "name": case "default": ArgName = (string)kvp.Value; break; } } if (!string.IsNullOrEmpty(ArgName)) { CmdAccumulator = core.userProperty.getText(ArgName, ""); } break; } case "siteproperty": case "site": { // // returns a site property // // arguments // name: the site property name // default argument // name // CmdAccumulator = ""; string ArgName = ""; foreach (KeyValuePair <string, object> kvp in cmdArgDef) { switch (kvp.Key.ToLowerInvariant()) { case "name": case "default": { ArgName = (string)kvp.Value; break; } default: { // do nothing break; } } } if (!string.IsNullOrEmpty(ArgName)) { CmdAccumulator = core.siteProperties.getText(ArgName, ""); } break; } case "runaddon": case "executeaddon": case "addon": { // // execute an add-on // string addonName = ""; Dictionary <string, string> addonArgDict = new Dictionary <string, string>(); foreach (KeyValuePair <string, object> kvp in cmdArgDef) { if (kvp.Key.ToLowerInvariant().Equals("addon")) { addonName = kvp.Value.ToString(); } else { addonArgDict.Add(kvp.Key, kvp.Value.ToString()); } } addonArgDict.Add("cmdAccumulator", CmdAccumulator); AddonModel addon = AddonModel.createByUniqueName(core.cpParent, addonName); var executeContext = new Contensive.BaseClasses.CPUtilsBaseClass.addonExecuteContext { addonType = Context, cssContainerClass = "", cssContainerId = "", hostRecord = new Contensive.BaseClasses.CPUtilsBaseClass.addonExecuteHostRecordContext { contentName = "", fieldName = "", recordId = 0 }, argumentKeyValuePairs = addonArgDict, errorContextMessage = "calling Addon [" + addonName + "] during content cmd execution" }; if (addon == null) { LogController.logError(core, new GenericException("Add-on [" + addonName + "] could not be found executing command in content [" + cmdSrc + "]")); } else { CmdAccumulator = core.addon.execute(addon, executeContext); } break; } default: { // // execute an add-on // string addonName = cmdText; Dictionary <string, string> addonArgDict = new Dictionary <string, string>(); foreach (KeyValuePair <string, object> kvp in cmdArgDef) { addonArgDict.Add(kvp.Key, kvp.Value.ToString()); } addonArgDict.Add("cmdAccumulator", CmdAccumulator); var executeContext = new CPUtilsBaseClass.addonExecuteContext { addonType = Context, cssContainerClass = "", cssContainerId = "", hostRecord = new CPUtilsBaseClass.addonExecuteHostRecordContext { contentName = "", fieldName = "", recordId = 0 }, argumentKeyValuePairs = addonArgDict, errorContextMessage = "calling Addon [" + addonName + "] during content cmd execution" }; AddonModel addon = AddonModel.createByUniqueName(core.cpParent, addonName); CmdAccumulator = core.addon.execute(addon, executeContext); break; } } } } // return(CmdAccumulator); } catch (Exception ex) { LogController.logError(core, ex); throw; } }
// //============================================================================================ /// <summary> /// Execute context commands in the source text /// </summary> /// <param name="core"></param> /// <param name="src"></param> /// <param name="Context"></param> /// <param name="deprecated_personalizationPeopleId"></param> /// <param name="deprecated_personalizationIsAuthenticated"></param> /// <returns></returns> public static string executeContentCommands(CoreController core, string src, CPUtilsBaseClass.addonContext Context) { try { // // -- fast exit for content w/o cmds if (string.IsNullOrWhiteSpace(src)) { return(src); } ; if (src.IndexOf(contentReplaceEscapeStart).Equals(-1)) { return(src); } if (src.IndexOf(contentReplaceEscapeEnd).Equals(-1)) { return(src); } // int Ptr = 0; var result = new StringBuilder(); int ptrLast = 1; do { string Cmd = ""; int posOpen = GenericController.strInstr(ptrLast, src, contentReplaceEscapeStart); Ptr = posOpen; int posClose = 0; if (Ptr == 0) { // // not found, copy the rest of src to dst // } else { // //bool badCmd = false; // // scan until we have passed all double and single quotes that are before the next // bool notFound = true; do { posClose = GenericController.strInstr(Ptr, src, contentReplaceEscapeEnd); if (posClose == 0) { // // brace opened but no close, forget the open and exit // posOpen = 0; notFound = false; } else { int posDq = Ptr; string escape = null; do { posDq = GenericController.strInstr(posDq + 1, src, "\""); escape = ""; if (posDq > 0) { escape = src.Substring(posDq - 2, 1); } } while (escape == "\\"); int posSq = Ptr; do { posSq = GenericController.strInstr(posSq + 1, src, "'"); escape = ""; if (posSq > 0) { escape = src.Substring(posSq - 2, 1); } } while (escape == "\\"); int posNextQuote = getFirstNonZeroInteger(posSq, posDq); if (posNextQuote == 0) { notFound = false; } else { // // posSq is before posDq // string nextQuoteChr = (posNextQuote == posSq) ? "'" : "\""; if (posNextQuote > posClose) { notFound = false; } else { // // skip forward to the next non-escaped sq // do { posNextQuote = GenericController.strInstr(posNextQuote + 1, src, nextQuoteChr); escape = ""; if (posNextQuote > 0) { escape = src.Substring(posNextQuote - 2, 1); } } while (escape == "\\"); Ptr = posNextQuote + 1; } break; } } } while (notFound); } if (posOpen <= 0) { // // no cmd found, add from the last ptr to the end // result.Append(src.Substring(ptrLast - 1)); Ptr = -1; } else { // // cmd found, process it and add the results to the dst // Cmd = src.Substring(posOpen + 1, (posClose - posOpen - 2)); // // -- when cmd entered through wysiwyg, it is html encoded Cmd = HtmlController.decodeHtml(Cmd); // string cmdResult = executeSingleCommand(core, Cmd, Context); result.Append(src.Substring(ptrLast - 1, posOpen - ptrLast) + cmdResult); Ptr = posClose + 2; } ptrLast = Ptr; } while (Ptr > 1); // return(result.ToString()); } catch (Exception ex) { LogController.logError(core, ex); throw; } }