Ejemplo n.º 1
0
        public static PhpString file_get_contents(Context ctx, QueryValue <LocalVariables> localsData, string path, FileOpenOptions flags = FileOpenOptions.Empty, PhpResource context = null, int offset = -1, int maxLength = -1)
        {
            var sc = StreamContext.GetValid(context, true);

            if (sc == null)
            {
                return(default(PhpString));
            }

            using (PhpStream stream = PhpStream.Open(ctx, path, "rb", ProcessOptions(ctx, flags), sc))
            {
                if (stream == null)
                {
                    return(default(PhpString));
                }

                // when HTTP protocol requested, store responded headers into local variable $http_response_header:
                if (string.Compare(stream.Wrapper.Scheme, HttpStreamWrapper.scheme, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    var headers = stream.WrapperSpecificData as PhpArray;
                    localsData.Value.Locals.SetItemValue(new IntStringKey(HttpResponseHeaderName), (PhpValue)headers);
                }

                //
                //return Core.Convert.Quote(stream.ReadContents(maxLength, offset), ScriptContext.CurrentContext);
                return(stream.ReadContents(maxLength, offset).ToPhpString());
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Tests the given GZ file pointer for EOF.
 /// </summary>
 /// <param name="zp">The gz-file pointer. It must be valid, and must point to a file successfully opened by gzopen().</param>
 /// <returns>Returns TRUE if the gz-file pointer is at EOF or an error occurs; otherwise returns FALSE.</returns>
 public static bool gzeof(PhpResource zp)
 {
     return(PhpPath.feof(zp));
 }
Ejemplo n.º 3
0
 /// <summary>
 ///     <para>
 ///         Sets the file position indicator for the given file pointer to the given offset byte into the file stream. Equivalent
 ///         to calling (in C) gzseek(zp, offset, SEEK_SET).
 ///     </para>
 ///     <para>
 ///         If the file is opened for reading, this function is emulated but can be extremely slow. If the file is opened for
 ///         writing, only forward seeks are supported; gzseek() then compresses a sequence of zeroes up to the new starting position.
 ///     </para>
 /// </summary>
 /// <param name="zp">The gz-file pointer. It must be valid, and must point to a file successfully opened by gzopen().</param>
 /// <param name="offset">The seeked offset.</param>
 /// <param name="whence">
 ///     whence values are: SEEK_SET (relative to origin), SEEK_CUR (relative to current position).
 /// </param>
 /// <returns>Upon success, returns 0; otherwise, returns -1. Note that seeking past EOF is not considered an error.</returns>
 public static int gzseek(PhpResource zp, int offset, int whence = PhpStreams.SEEK_SET)
 {
     return(PhpPath.fseek(zp, offset, whence));
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Reads up to length bytes from the given gz-file pointer. Reading stops when length (uncompressed) bytes
 /// have been read or EOF is reached, whichever comes first.
 /// </summary>
 /// <param name="ctx">Runtime context.</param>
 /// <param name="zp">The gz-file pointer. It must be valid, and must point to a file successfully opened by gzopen().</param>
 /// <param name="length">The number of bytes to read.</param>
 /// <returns>The data that have been read.</returns>
 public static PhpString gzread(Context ctx, PhpResource zp, int length)
 {
     return(PhpPath.fread(ctx, zp, length));
 }
Ejemplo n.º 5
0
 private static XMLWriterResource ValidateXmlWriterResource(PhpResource xmlwriter)
 {
     if (xmlwriter is XMLWriterResource h && h.IsValid)
     {
         return(h);
     }
Ejemplo n.º 6
0
 public static PhpString gzgetc(Context ctx, PhpResource zp)
 {
     return(PhpPath.fgetc(ctx, zp));
 }
Ejemplo n.º 7
0
 public static string gzgetss(PhpResource zp, int length = -1, string allowable_tags = null)
 {
     return(length < 0
         ? PhpPath.fgetss(zp)
         : PhpPath.fgetss(zp, length, allowable_tags));
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Tests for end-of-file on a file pointer.
        /// </summary>
        /// <param name="handle">A PhpResource passed to the PHP function.</param>
        /// <returns>True if successful.</returns>
        public static bool feof(PhpResource handle)
        {
            PhpStream stream = PhpStream.GetValid(handle);

            return(stream != null && stream.Eof);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Flushes the output to a file.
        /// </summary>
        /// <param name="handle">A PhpResource passed to the PHP function.</param>
        /// <returns>True if successful.</returns>
        public static bool fflush(PhpResource handle)
        {
            PhpStream stream = PhpStream.GetValid(handle);

            return(stream != null && stream.Flush());
        }
Ejemplo n.º 10
0
        public static PhpResource fopen(Context ctx, string path, string mode, FileOpenOptions flags = FileOpenOptions.Empty, PhpResource context = null)
        {
            StreamContext sc = StreamContext.GetValid(context, true);

            if (sc == null)
            {
                return(null);
            }

            if (string.IsNullOrEmpty(path))
            {
                //PhpException.Throw(PhpError.Warning, LibResources.GetString("arg_empty", "path"));
                //return null;
                throw new ArgumentException(nameof(path));
            }

            if (string.IsNullOrEmpty(mode))
            {
                //PhpException.Throw(PhpError.Warning, LibResources.GetString("arg_empty", "mode"));
                //return null;
                throw new ArgumentException(nameof(mode));
            }

            return(PhpStream.Open(ctx, path, mode, ProcessOptions(ctx, flags), sc));
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Portable advisory file locking.
        /// </summary>
        public static bool flock(PhpResource handle, int operation)
        {
            int dummy = 0;

            return(flock(handle, operation, ref dummy));
        }
Ejemplo n.º 12
0
        public static int ftell(PhpResource handle)
        {
            var stream = PhpStream.GetValid(handle);

            return((stream == null) ? -1 : stream.Tell());
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Rewind the position of a file pointer.
        /// </summary>
        /// <param name="handle"></param>
        /// <returns></returns>
        public static bool rewind(PhpResource handle)
        {
            var stream = PhpStream.GetValid(handle);

            return(stream != null && stream.Seek(0, SeekOrigin.Begin));
        }
Ejemplo n.º 14
0
        public static int file_put_contents(Context ctx, string path, PhpValue data, WriteContentsOptions flags = WriteContentsOptions.Empty, PhpResource context = null)
        {
            StreamContext sc = StreamContext.GetValid(context, true);

            if (sc == null)
            {
                return(-1);
            }

            string mode = (flags & WriteContentsOptions.AppendContents) > 0 ? "ab" : "wb";

            using (PhpStream to = PhpStream.Open(ctx, path, mode, ProcessOptions(ctx, (FileOpenOptions)flags), sc))
            {
                if (to == null)
                {
                    return(-1);
                }

                // passing array is equivalent to file_put_contents($filename, join('', $array))
                var array = data.ArrayOrNull();
                if (array != null)
                {
                    int total = 0;

                    var enumerator = array.GetFastEnumerator();
                    while (enumerator.MoveNext())
                    {
                        int written = to.WriteBytes(enumerator.CurrentValue.ToBytes(ctx));
                        if (written == -1)
                        {
                            return(total);
                        }
                        total += written;
                    }

                    return(total);
                }

                // as of PHP 5.1.0, you may also pass a stream resource to the data parameter
                var resource = data.AsResource();
                if (resource != null)
                {
                    PhpStream from = PhpStream.GetValid(resource);
                    if (from == null)
                    {
                        return(-1);
                    }

                    return(PhpStreams.stream_copy_to_stream(from, to));
                }

                return(to.WriteBytes(data.ToBytes(ctx)));
            }
        }
Ejemplo n.º 15
0
 /// <summary>
 /// Gets the position of the given file pointer; i.e., its offset into the uncompressed file stream.
 /// </summary>
 /// <param name="zp">The gz-file pointer. It must be valid, and must point to a file successfully opened by gzopen().</param>
 /// <returns>The position of the file pointer or FALSE if an error occurs.</returns>
 public static int gztell(PhpResource zp)
 {
     return(PhpPath.ftell(zp));
 }
Ejemplo n.º 16
0
        /// <remarks>
        /// Affected by run-time quoting (data are unqouted before written)
        /// (<see cref="LocalConfiguration.VariablesSection.QuoteRuntimeVariables"/>).
        /// </remarks>
        public static int fputcsv(Context ctx, PhpResource handle, PhpArray fields, char delimiter = DefaultCsvDelimiter, char enclosure = DefaultCsvEnclosure)
        {
            PhpStream stream = PhpStream.GetValid(handle, FileAccess.Write);

            if (stream == null || !stream.CanWrite)
            {
                return(-1);
            }

            char[] special_chars = { delimiter, ' ', '\\', '\t', '\r', '\n' };
            string str_enclosure = enclosure.ToString();
            string str_delimiter = delimiter.ToString();

            int initial_position = stream.WritePosition;
            var enumerator       = fields.GetFastEnumerator();

            while (enumerator.MoveNext())
            {
                var str_field = StringUtils.StripCSlashes(enumerator.CurrentValue.ToString(ctx));

                if (stream.WritePosition > initial_position)
                {
                    stream.WriteString(str_delimiter);
                }

                int special_char_index = str_field.IndexOfAny(special_chars);
                int enclosure_index    = str_field.IndexOf(enclosure);

                if (special_char_index >= 0 || enclosure_index >= 0)
                {
                    stream.WriteString(str_enclosure);

                    if (enclosure_index >= 0)
                    {
                        // escapes enclosure characters:
                        int start = 0;
                        for (; ;)
                        {
                            // writes string starting after the last enclosure and ending by the next one:
                            stream.WriteString(str_field.Substring(start, enclosure_index - start + 1));
                            stream.WriteString(str_enclosure);

                            start = enclosure_index + 1;
                            if (start >= str_field.Length)
                            {
                                break;
                            }

                            enclosure_index = str_field.IndexOf(enclosure, start);
                            if (enclosure_index < 0)
                            {
                                // remaining substring:
                                stream.WriteString(str_field.Substring(start));
                                break;
                            }
                        }
                    }
                    else
                    {
                        stream.WriteString(str_field);
                    }

                    stream.WriteString(str_enclosure);
                }
                else
                {
                    stream.WriteString(str_field);
                }
            }

            stream.WriteString("\n");

            return((initial_position == -1) ? stream.WritePosition : stream.WritePosition - initial_position);
        }
Ejemplo n.º 17
0
 /// <summary>
 /// Gets the position of the given file pointer; i.e., its offset into the uncompressed file stream.
 ///
 /// Some versions of linux have the function named differently, which is used in some PHP libraries
 /// </summary>
 public static object gztell64(PhpResource zp) => gztell(zp);
Ejemplo n.º 18
0
 public static PhpString fgetc(Context ctx, PhpResource handle)
 {
     return(feof(handle) ? default(PhpString) : fread(ctx, handle, 1));
 }
Ejemplo n.º 19
0
 public static PhpString gzgets(PhpResource zp, int length = -1)
 {
     return((length == -1) ? PhpPath.fgets(zp) : PhpPath.fgets(zp, length));
 }
Ejemplo n.º 20
0
 public static int fwrite(Context ctx, PhpResource handle, PhpString data, int length = -1)
 {
     //data = Core.Convert.Unquote(data, ScriptContext.CurrentContext);
     return(WriteInternal(ctx, handle, data, length));
 }
Ejemplo n.º 21
0
 /// <summary>
 /// Reads to EOF on the given gz-file pointer from the current position and writes the (uncompressed) results to standard output.
 /// </summary>
 /// <param name="ctx">Runtime context.</param>
 /// <param name="zp">The gz-file pointer. It must be valid, and must point to a file successfully opened by gzopen().</param>
 /// <returns>The number of uncompressed characters read from gz and passed through to the input, or FALSE on error.</returns>
 public static int gzpassthru(Context ctx, PhpResource zp)
 {
     return(PhpPath.fpassthru(ctx, zp));
 }
Ejemplo n.º 22
0
 public static int fputs(Context ctx, PhpResource handle, PhpString data, int length = -1)
 {
     return(fwrite(ctx, handle, data, length));
 }
Ejemplo n.º 23
0
 /// <summary>
 /// Writes the contents of string to the given gz-file.
 /// </summary>
 /// <param name="ctx">Runtime context.</param>
 /// <param name="zp">The gz-file pointer. It must be valid, and must point to a file successfully opened by gzopen().</param>
 /// <param name="str">The string to write.</param>
 /// <param name="length">
 ///     The number of uncompressed bytes to write. If supplied, writing will stop after length (uncompressed) bytes have been
 ///     written or the end of string is reached, whichever comes first.
 /// </param>
 /// <returns>Returns the number of (uncompressed) bytes written to the given gz-file stream.</returns>
 public static int gzwrite(Context ctx, PhpResource zp, PhpString str, int length = -1)
 {
     return(PhpPath.fwrite(ctx, zp, str, length));
 }
Ejemplo n.º 24
0
        public static int readfile(Context ctx, string path, FileOpenOptions flags = FileOpenOptions.Empty, PhpResource context = null)
        {
            StreamContext sc = StreamContext.GetValid(context, true);

            if (sc == null)
            {
                return(-1);
            }

            using (PhpStream res = PhpStream.Open(ctx, path, "rb", ProcessOptions(ctx, flags), sc))
            {
                if (res == null)
                {
                    return(-1);
                }

                // Note: binary file access is the most efficient (no superfluous filtering
                // and no conversions - PassThrough will write directly to the OutputStream).
                return(fpassthru(ctx, res));
            }
        }
Ejemplo n.º 25
0
 /// <summary>
 /// Closes the given gz-file pointer.
 /// </summary>
 /// <param name="zp">The gz-file pointer. It must be valid, and must point to a file successfully opened by gzopen().</param>
 /// <returns>Returns TRUE on success or FALSE on failure.</returns>
 public static bool gzclose(PhpResource zp)
 {
     return(PhpPath.fclose(zp));
 }
Ejemplo n.º 26
0
 public static string fgetss(PhpResource handle)
 {
     return(ReadLineStripTagsInternal(handle, -1, null));
 }
Ejemplo n.º 27
0
 /// <summary>
 /// Sets the file position indicator of the given gz-file pointer to the beginning of the file stream.
 /// </summary>
 /// <param name="zp">The gz-file pointer. It must be valid, and must point to a file successfully opened by gzopen().</param>
 /// <returns>Returns TRUE on success or FALSE on failure.</returns>
 public static bool gzrewind(PhpResource zp)
 {
     return(PhpPath.rewind(zp));
 }
Ejemplo n.º 28
0
        public static PhpArray file(Context ctx, string path, FileOptions flags = FileOptions.Empty, PhpResource context = null)
        {
            var sc = StreamContext.GetValid(context, true);

            if (sc == null)
            {
                return(null);
            }

            using (var stream = PhpStream.Open(ctx, path, "rt", ProcessOptions(ctx, (FileOpenOptions)flags), sc))
            {
                if (stream == null)
                {
                    return(null);
                }

                PhpArray rv = new PhpArray();

                while (!stream.Eof)
                {
                    // Note: The last line does not contain the \n delimiter, but may be empty
                    var line = stream.ReadData(-1, true).AsText(ctx.StringEncoding);
                    if ((flags & FileOptions.TrimLineEndings) > 0)
                    {
                        int len = line.Length;
                        if ((len > 0) && (line[len - 1] == '\n'))
                        {
                            line = line.Substring(0, len - 1);
                        }
                    }
                    if ((flags & FileOptions.SkipEmptyLines) > 0)
                    {
                        if (line.Length == 0)
                        {
                            continue;
                        }
                    }

                    rv.Add(line);
                }

                return(rv);
            }
        }
Ejemplo n.º 29
0
 /// <summary>
 /// Calls gzseek
 /// Some versions of linux have the function named differently, which is used in some PHP libraries
 /// </summary>
 public static int gzseek64(PhpResource zp, int offset, int whence = PhpStreams.SEEK_SET) => gzseek(zp, offset, whence);
        public static string stream_socket_recvfrom(PhpResource socket, int length, SendReceiveOptions flags = SendReceiveOptions.None)
        {
            string address;

            return(stream_socket_recvfrom(socket, length, flags, out address));
        }