Exemplo n.º 1
0
        public static void p5_io_file_save(ApplicationContext context, ActiveEventArgs e)
        {
            // Sanity check.
            if (e.Args.Value == null)
            {
                throw new LambdaException("[p5.io.file.save] requires a constant or an expression leading to its path", e.Args, context);
            }

            // Making sure we clean up and remove all arguments passed in after execution.
            using (new ArgsRemover(e.Args)) {
                // Getting root folder, and converting content to blob, making sure user does use [src] and not [dest] as file content.
                var rootFolder = Common.GetRootFolder(context);
                var content    = Utilities.Convert <byte []> (context, XUtil.Source(context, e.Args));

                // Iterating through each destination file path, which probably doesn't make that much sense, but to be consistent in how we
                // handle files, we still do this, such that "API" is similar to [load-file], etc.
                foreach (var idxDestination in XUtil.Iterate <string> (context, e.Args))
                {
                    // Verifying user is allowed to save to file.
                    Common.RaiseAuthorizeEvent(context, e.Args, "modify-file", idxDestination);

                    // Saving file.
                    using (FileStream stream = File.Create(rootFolder + Common.GetSystemPath(context, idxDestination))) {
                        // Writing content to file stream.
                        stream.Write(content, 0, content.Length);
                    }
                }
            }
        }
Exemplo n.º 2
0
        public static void p5_string_match(ApplicationContext context, ActiveEventArgs e)
        {
            // Making sure we clean up and remove all arguments passed in after execution.
            using (new ArgsRemover(e.Args, true)) {
                // Figuring out source value for [match], and returning early if there is no source.
                string source = XUtil.Single <string> (context, e.Args);
                if (source == null)
                {
                    return;
                }

                // Retrieving what source to look for, and returning early if there is none.
                var src = XUtil.Source(context, e.Args);
                if (src == null)
                {
                    return;
                }

                // Sanity check.
                if (!(src is Regex))
                {
                    throw new LambdaException("[match] requires a regular expression source", e.Args, context);
                }

                // Evaluating regular expression, and returning results.
                foreach (System.Text.RegularExpressions.Match idxMatch in (src as Regex).Matches(source))
                {
                    // Returning all groups matches.
                    foreach (Group idxGroup in idxMatch.Groups)
                    {
                        e.Args.Add(idxGroup.Value);
                    }
                }
            }
        }
Exemplo n.º 3
0
        public void p5_web_return_response_object(ApplicationContext context, ActiveEventArgs e)
        {
            var key    = XUtil.Single <string> (context, e.Args);
            var source = XUtil.Source(context, e.Args);

            AjaxPage.SendObject(key, Utilities.Convert <string> (context, source));
        }
Exemplo n.º 4
0
        public static void p5_string_index_of(ApplicationContext context, ActiveEventArgs e)
        {
            // Making sure we clean up and remove all arguments passed in after execution.
            using (new ArgsRemover(e.Args, true)) {
                // Figuring out source value for [index-of], and returning early if there is no source.
                string source = XUtil.Single <string> (context, e.Args);
                if (source == null)
                {
                    return;
                }

                // Retrieving what to look for, and returning early if we have no source.
                var src = XUtil.Source(context, e.Args);
                if (src == null)
                {
                    return;
                }

                // Checking what type of value we're looking for, and acting accordingly.
                if (src is Regex)
                {
                    // Regex type of find.
                    e.Args.AddRange(IndexOfRegex(source, src as Regex).Select(ix => new Node("", ix)));
                }
                else
                {
                    // Simple string type of find.
                    e.Args.AddRange(IndexOfString(source, Utilities.Convert <string> (context, src)).Select(ix => new Node("", ix)));
                }
            }
        }
Exemplo n.º 5
0
        public static void p5_string__replace(ApplicationContext context, ActiveEventArgs e)
        {
            // Making sure we clean up and remove all arguments passed in after execution.
            using (new ArgsRemover(e.Args)) {
                // Figuring out source value for [replace], and returning early if there is no source.
                string source = XUtil.Single <string> (context, e.Args);
                if (source == null)
                {
                    return;
                }

                // Retrieving what to replace it with.
                var with = Utilities.Convert(context, XUtil.Source(context, e.Args, "src"), "");

                // Checking what type of object we're searching for, and doing some basic sanity check.
                var what = XUtil.Source(context, e.Args, "dest");
                if (what == null)
                {
                    throw new LambdaException("[replace] requires something to search for", e.Args, context);
                }
                if (e.Name.EndsWith("-x", System.StringComparison.CurrentCulture))
                {
                    var regexWhat = Utilities.Convert <Regex> (context, what);
                    e.Args.Value = regexWhat.Replace(source, with);
                }
                else
                {
                    e.Args.Value = source.Replace(Utilities.Convert <string> (context, what), with);
                }
            }
        }
Exemplo n.º 6
0
        public static void zip(ApplicationContext context, ActiveEventArgs e)
        {
            using (new Utilities.ArgsRemover(e.Args)) {
                // Getting root folder
                var rootFolder = Helpers.GetBaseFolder(context);

                // Getting destination file
                var destination = Helpers.GetSystemPath(context, e.Args.GetExValue <string> (context));

                // Getting destination path, and verify path can be legally written to
                var destinationFile = Helpers.GetLegalDestinationFilename(
                    context,
                    e.Args,
                    rootFolder,
                    destination);

                // Getting source file(s)
                var source = XUtil.Source(context, e.Args, e.Args, "src", new string[] { "compression-level", "password", "key-size" }.ToList());

                // Making sure we are able to delete zip file, if an exception occurs
                try {
                    // Creating zip file, supplying FileStream as stream to store results into
                    using (ZipCreator creator = new ZipCreator(
                               context,
                               File.Create(rootFolder + destinationFile),
                               e.Args.GetExChildValue("compression-level", context, 3),
                               e.Args.GetExChildValue <string> ("password", context, null),
                               e.Args.GetExChildValue("key-size", context, 256))) {
                        // Looping through each input file/folder given
                        foreach (var idxSourceFileFolder in source)
                        {
                            var idxSource = Helpers.GetSystemPath(context, Utilities.Convert <string> (context, idxSourceFileFolder));

                            // Verifies source folder can be read from
                            Helpers.VerfifySourceFileFolderCanBeReadFrom(
                                context,
                                e.Args,
                                rootFolder,
                                destinationFile,
                                idxSource);

                            // Adding currently iterated file/folder to zip file stream
                            creator.AddToArchive(rootFolder + idxSource, e.Args);
                        }
                    }

                    // Making sure we return actual destination to caller
                    e.Args.Value = destinationFile;
                } catch {
                    // Checking if destination file exist, and if so, delete it, before we rethrow exception
                    if (File.Exists(rootFolder + destinationFile))
                    {
                        File.Delete(rootFolder + destinationFile);
                    }
                    throw;
                }
            }
        }
Exemplo n.º 7
0
        public static void lambda_set(ApplicationContext context, ActiveEventArgs e)
        {
            // Finding source value, notice that if we have no source, we still iterate each destination, such that we can set it to a "null value".
            var src = XUtil.Source(context, e.Args);

            // Updating destination(s) with value of source.
            foreach (var idxDestination in XUtil.DestinationMatch(context, e.Args))
            {
                // Single source, or null source.
                idxDestination.Value = src;
            }
        }
        public static void p5_string_ends_with(ApplicationContext context, ActiveEventArgs e)
        {
            // Making sure we clean up and remove all arguments passed in after execution.
            using (new ArgsRemover(e.Args)) {
                // Retrieving what to look for, and returning early if we have no source.
                var src = XUtil.Source(context, e.Args);
                if (src == null)
                {
                    return;
                }

                // Returning to lowers of expression or constant.
                e.Args.Value = XUtil.Single <string> (context, e.Args)?.EndsWithEx(Utilities.Convert <string> (context, src)) ?? false;
            }
        }
Exemplo n.º 9
0
        public static void unzip(ApplicationContext context, ActiveEventArgs e)
        {
            // Retrieving password, if there is one, and untying it,
            // to make sure it never leaves method, in case of an exception, etc
            string password = e.Args.GetExChildValue <string>("password", context, null);

            e.Args.FindOrInsert("password").UnTie();   // Making sure password NEVER LEAVES METHOD!!

            // Basic syntax checking
            if (e.Args.Value == null)
            {
                throw new LambdaException(
                          "[unzip] needs both a destination as its value",
                          e.Args,
                          context);
            }

            // Making sure we clean up and remove all arguments passed in after execution
            using (new Utilities.ArgsRemover(e.Args)) {
                // Getting root folder
                var rootFolder = Helpers.GetBaseFolder(context);

                // Getting destination folder
                var destFolder = GetDestinationFolder(context, e);

                // Getting source files
                var source = XUtil.Source(context, e.Args, e.Args, "src", new string[] { "password" }.ToList());

                // Looping through each source zip file given
                foreach (var idxZipFilePath in source)
                {
                    // Unzips currently iterated file
                    UnzipFile(
                        context,
                        e.Args,
                        rootFolder,
                        Helpers.GetSystemPath(context, Utilities.Convert <string> (context, idxZipFilePath)),
                        destFolder,
                        password);
                }

                // Returning folder path of where files where unzipped to caller
                e.Args.Value = destFolder;
            }
        }
Exemplo n.º 10
0
        public static void html2pdf(ApplicationContext context, ActiveEventArgs e)
        {
            // Making sure we clean up and remove all arguments passed in after execution
            using (new ArgsRemover(e.Args, true)) {
                // Assumes there's only one document, or creates one result of it.
                var filename = XUtil.Single <string> (context, e.Args);
                filename = context.RaiseEvent(".p5.io.unroll-path", new Node("", filename)).Get <string> (context);

                // Figuring out source.
                var html = XUtil.Source(context, e.Args, "css-file") as string;

                // Retrieving root path of app.
                var rootPath = context.RaiseEvent(".p5.core.application-folder").Get <string> (context);

                // Loading up all CSS, which we'll need to pass into ParseXhtml further down.
                var css = "";
                foreach (var idxCssFile in e.Args.Children.Where(ix => ix.Name == "css-file"))
                {
                    var cssFile = idxCssFile.GetExValue <string> (context);
                    cssFile = context.RaiseEvent(".p5.io.unroll-path", new Node("", cssFile)).Get <string> (context);
                    using (TextReader reader = File.OpenText(rootPath + cssFile)) {
                        css += reader.ReadToEnd() + "\r\n";
                    }
                }

                // Creating our document.
                using (var stream = new FileStream(rootPath + filename, FileMode.Create)) {
                    using (var document = new Document(PageSize.A4, 55, 55, 70, 70)) {
                        using (var writer = PdfWriter.GetInstance(document, stream)) {
                            document.Open();
                            using (var htmlStream = new MemoryStream(Encoding.UTF8.GetBytes(html))) {
                                using (var cssStream = new MemoryStream(Encoding.UTF8.GetBytes(css))) {
                                    XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, htmlStream, cssStream);
                                }
                            }
                            document.Close();
                        }
                    }
                }
            }
        }
Exemplo n.º 11
0
        public static void p5_data_update(ApplicationContext context, ActiveEventArgs e)
        {
            // Retrieving expression and doing some basic sanity checks.
            if (!(e.Args.Value is Expression))
            {
                throw new LambdaException("[p5.data.update] requires an expression leading to whatever you want to be updated in your database", e.Args, context);
            }

            // Making sure we clean up and remove all arguments passed in after execution.
            using (new ArgsRemover(e.Args)) {
                // Acquiring write lock on database.
                using (new Common.Lock(true)) {
                    // Storing affected nodes, and number of affected items.
                    var changed       = new List <Node> ();
                    int affectedItems = 0;

                    // Retrieving source, and iterating through each destination, updating with source value.
                    // Notice, we don't provide transaction support here, but we do make sure at least any changes
                    // that actually occurs, are serialized to disc!
                    var source = XUtil.Source(context, e.Args);
                    try {
                        foreach (var idxDestination in e.Args.Get <Expression> (context).Evaluate(context, Common.Database, e.Args))
                        {
                            // Updates the destination with the source, making sure we can keep track of files that are changed, and that we throw if update is unsuccessful.
                            if (!UpdateDestination(context, source, idxDestination))
                            {
                                throw new LambdaException("[p5.data.update] requires your new node needs to have a unique ID, or use its old ID by not providing one", e.Args, context);
                            }

                            Common.AddNodeToChanges(idxDestination.Node, changed);
                            affectedItems += 1;
                        }
                    } finally {
                        Common.SaveAffectedFiles(context, changed);
                        e.Args.Value = affectedItems;
                    }
                }
            }
        }
Exemplo n.º 12
0
        public static void p5_data_update(ApplicationContext context, ActiveEventArgs e)
        {
            // Retrieving expression and doing some basic sanity checks.
            if (!(e.Args.Value is Expression))
            {
                throw new LambdaException("[p5.data.update] requires an expression leading to whatever you want to be updated in your database", e.Args, context);
            }

            // Making sure we clean up and remove all arguments passed in after execution.
            using (new ArgsRemover(e.Args)) {
                // Acquiring write lock on database, and making sure we keep track of which files are changed, and how many items were affected.
                var changed       = new List <Node> ();
                int affectedItems = 0;
                Common.Locker.EnterWriteLock();
                try {
                    // Retrieving source, and iterating through each destination, updating with source value.
                    var source = XUtil.Source(context, e.Args);
                    foreach (var idxDestination in e.Args.Get <Expression> (context).Evaluate(context, Common.Database, e.Args))
                    {
                        // Updates the destination with the source, making sure we can keep track of files that are changed, and that we throw is update is unsuccessful.
                        if (!UpdateDestination(context, source, idxDestination))
                        {
                            throw new LambdaException("[p5.data.update] requires your new node needs to have a unique ID, or use its old ID by not providing one", e.Args, context);
                        }
                        Common.AddNodeToChanges(idxDestination.Node, changed);
                        affectedItems += 1;
                    }
                } finally {
                    // Saving all affected files.
                    // Notice, we do this even though an exception has occurred, since exception is thrown before nodes are updated with any "bad data".
                    // This means that if you update several nodes, some might become updated though, while others are not updated.
                    // Hence, [p5.data.update] does not feature any sorts of "transactional update support" at the moment.
                    Common.SaveAffectedFiles(context, changed);
                    e.Args.Value = affectedItems;
                    Common.Locker.ExitWriteLock();
                }
            }
        }
Exemplo n.º 13
0
        public static void p5_string__replace(ApplicationContext context, ActiveEventArgs e)
        {
            // Sanity check.
            if (e.Args.Value == null)
            {
                throw new LambdaException("[p5.string.replace] requires an expression or constant as its value", e.Args, context);
            }

            // Making sure we clean up and remove all arguments passed in after execution.
            using (new Utilities.ArgsRemover(e.Args)) {
                // Figuring out source value for [replace], and returning early if there is no source.
                string source = XUtil.Single <string> (context, e.Args);
                if (source == null)
                {
                    return;
                }

                // Retrieving what to replace it with.
                var with = Utilities.Convert(context, XUtil.Source(context, e.Args, "src"), "");

                // Checking what type of object we're searching for, and doing some basic sanity check.
                var what = XUtil.Source(context, e.Args, "dest");
                if (what == null)
                {
                    throw new LambdaException("[p5.string.replace] requires something to search for", e.Args, context);
                }
                else if (what is Regex)
                {
                    e.Args.Value = (what as Regex).Replace(source, with);
                }
                else
                {
                    e.Args.Value = source.Replace(Utilities.Convert <string> (context, what), with);
                }
            }
        }
Exemplo n.º 14
0
        /*
         * Common helper for iterating files/folders for move/copy/rename operation.
         */
        internal static void CopyMoveFileObject(
            ApplicationContext context,
            Node args,
            string sourceAuthorizeEvent,
            string destinationAuthorizeEvent,
            MoveCopyDelegate functorMoveObject,
            ObjectExistDelegate functorObjectExist)
        {
            // Sanity check.
            if (args.Value == null)
            {
                throw new LambdaException(
                          string.Format("[{0}] requires a value being the source file(s) to operate upon", args.Name),
                          args,
                          context);
            }

            // Making sure we clean up and remove all arguments passed in after execution.
            using (new ArgsRemover(args)) {
                // Retrieving destination file or folder, and root folder for app, making sure we get a [dest] node for destination.
                var dest       = Common.GetSystemPath(context, Utilities.Convert <string> (context, XUtil.Source(context, args, "src")));
                var rootFolder = Common.GetRootFolder(context);

                // Iterating through each source.
                foreach (var idxSource in XUtil.Iterate <string> (context, args))
                {
                    // Figuring out destination file, which might be "relative" to currently iterated source file.
                    var destinationFile = dest.EndsWithEx("/") ? dest + idxSource.Substring(idxSource.LastIndexOfEx("/") + 1) : dest;

                    // Making sure user is authorized to do operation on both source file and destination file.
                    Common.RaiseAuthorizeEvent(context, args, sourceAuthorizeEvent, idxSource);
                    Common.RaiseAuthorizeEvent(context, args, destinationAuthorizeEvent, destinationFile);

                    // Making sure destination file does not already exist.
                    if (functorObjectExist(rootFolder + destinationFile))
                    {
                        throw new LambdaException(string.Format("The file '{0}' already exist from before", destinationFile), args, context);
                    }

                    functorMoveObject(
                        rootFolder,
                        Common.GetSystemPath(context, Utilities.Convert <string> (context, idxSource)),
                        destinationFile);
                }
            }
        }