Ejemplo n.º 1
0
        public static ProfileField Decode(G2Header root)
        {
            ProfileField field = new ProfileField();
            G2Header     child = new G2Header(root.Data);

            while (G2Protocol.ReadNextChild(root, child) == G2ReadResult.PACKET_GOOD)
            {
                if (!G2Protocol.ReadPayload(child))
                {
                    continue;
                }

                switch (child.Name)
                {
                case Packet_Type:
                    field.FieldType = (ProfileFieldType)child.Data[child.PayloadPos];
                    break;

                case Packet_Name:
                    field.Name = UTF8Encoding.UTF8.GetString(child.Data, child.PayloadPos, child.PayloadSize);
                    break;

                case Packet_Value:
                    field.Value = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
                    break;
                }
            }

            return(field);
        }
Ejemplo n.º 2
0
        public static ProfileField Decode(G2Header root)
        {
            ProfileField field = new ProfileField();
            G2Header child = new G2Header(root.Data);

            while (G2Protocol.ReadNextChild(root, child) == G2ReadResult.PACKET_GOOD)
            {
                if (!G2Protocol.ReadPayload(child))
                    continue;

                switch (child.Name)
                {
                    case Packet_Type:
                        field.FieldType = (ProfileFieldType)child.Data[child.PayloadPos];
                        break;

                    case Packet_Name:
                        field.Name = UTF8Encoding.UTF8.GetString(child.Data, child.PayloadPos, child.PayloadSize);
                        break;

                    case Packet_Value:
                        field.Value = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
                        break;
                }
            }

            return field;
        }
Ejemplo n.º 3
0
        private static string LoadProfile(ProfileService service, OpProfile profile, string tempPath, Dictionary <string, string> textFields, Dictionary <string, string> fileFields)
        {
            string template = null;

            textFields.Clear();
            textFields["local_help"] = (profile.UserID == service.Core.UserID) ? "<font size=2>Right-click or click <a href='http://edit'>here</a> to Edit</font>" : "";

            if (fileFields != null)
            {
                fileFields.Clear();
            }

            if (!profile.Loaded)
            {
                service.LoadProfile(profile.UserID);
            }

            try
            {
                using (TaggedStream stream = new TaggedStream(service.GetFilePath(profile), service.Core.GuiProtocol))
                    using (IVCryptoStream crypto = IVCryptoStream.Load(stream, profile.File.Header.FileKey))
                    {
                        int    buffSize  = 4096;
                        byte[] buffer    = new byte[4096];
                        long   bytesLeft = profile.EmbeddedStart;
                        while (bytesLeft > 0)
                        {
                            int readSize = (bytesLeft > (long)buffSize) ? buffSize : (int)bytesLeft;
                            int read     = crypto.Read(buffer, 0, readSize);
                            bytesLeft -= (long)read;
                        }

                        // load file
                        foreach (ProfileAttachment attached in profile.Attached)
                        {
                            if (attached.Name.StartsWith("template"))
                            {
                                byte[] html = new byte[attached.Size];
                                crypto.Read(html, 0, (int)attached.Size);

                                UTF8Encoding utf = new UTF8Encoding();
                                template = utf.GetString(html);
                            }

                            else if (attached.Name.StartsWith("fields"))
                            {
                                byte[] data = new byte[attached.Size];
                                crypto.Read(data, 0, (int)attached.Size);

                                int          start = 0, length = data.Length;
                                G2ReadResult streamStatus = G2ReadResult.PACKET_GOOD;

                                while (streamStatus == G2ReadResult.PACKET_GOOD)
                                {
                                    G2ReceivedPacket packet = new G2ReceivedPacket();
                                    packet.Root = new G2Header(data);

                                    streamStatus = G2Protocol.ReadNextPacket(packet.Root, ref start, ref length);

                                    if (streamStatus != G2ReadResult.PACKET_GOOD)
                                    {
                                        break;
                                    }

                                    if (packet.Root.Name == ProfilePacket.Field)
                                    {
                                        ProfileField field = ProfileField.Decode(packet.Root);

                                        if (field.Value == null)
                                        {
                                            continue;
                                        }

                                        if (field.FieldType == ProfileFieldType.Text)
                                        {
                                            textFields[field.Name] = UTF8Encoding.UTF8.GetString(field.Value);
                                        }
                                        else if (field.FieldType == ProfileFieldType.File && fileFields != null)
                                        {
                                            fileFields[field.Name] = UTF8Encoding.UTF8.GetString(field.Value);
                                        }
                                    }
                                }
                            }

                            else if (attached.Name.StartsWith("file=") && fileFields != null)
                            {
                                string name = attached.Name.Substring(5);

                                try
                                {
                                    string fileKey = null;
                                    foreach (string key in fileFields.Keys)
                                    {
                                        if (name == fileFields[key])
                                        {
                                            fileKey = key;
                                            break;
                                        }
                                    }

                                    fileFields[fileKey] = tempPath + Path.DirectorySeparatorChar + name;

                                    using (FileStream extract = new FileStream(fileFields[fileKey], FileMode.CreateNew, FileAccess.Write))
                                    {
                                        long   remaining = attached.Size;
                                        byte[] buff      = new byte[2096];

                                        while (remaining > 0)
                                        {
                                            int read = (remaining > 2096) ? 2096 : (int)remaining;
                                            remaining -= read;

                                            crypto.Read(buff, 0, read);
                                            extract.Write(buff, 0, read);
                                        }
                                    }
                                }
                                catch
                                { }
                            }
                        }
                    }
            }
            catch (Exception)
            {
            }

            return(template);
        }
Ejemplo n.º 4
0
        public void SaveLocal(string template, Dictionary <string, string> textFields, Dictionary <string, string> fileFields)
        {
            try
            {
                long embeddedStart = 0;

                string tempPath = Core.GetTempPath();
                byte[] key      = Utilities.GenerateKey(Core.StrongRndGen, 256);
                using (IVCryptoStream stream = IVCryptoStream.Save(tempPath, key))
                {
                    int written = 0;

                    // write template info
                    byte[] htmlBytes = UTF8Encoding.UTF8.GetBytes(template);
                    written += Protocol.WriteToFile(new ProfileAttachment("template", htmlBytes.Length), stream);


                    // write fields info (convert into fields into packet list)
                    List <byte[]> fieldPackets = new List <byte[]>();

                    int fieldsTotalSize = 0;

                    if (textFields != null)
                    {
                        foreach (KeyValuePair <string, string> pair in textFields)
                        {
                            if (pair.Value == null)
                            {
                                continue;
                            }

                            ProfileField field = new ProfileField();
                            field.Name      = pair.Key;
                            field.Value     = UTF8Encoding.UTF8.GetBytes(pair.Value);
                            field.FieldType = ProfileFieldType.Text;

                            byte[] packet = field.Encode(Network.Protocol);
                            fieldPackets.Add(packet);
                            fieldsTotalSize += packet.Length;
                        }
                    }

                    if (fileFields != null)
                    {
                        foreach (KeyValuePair <string, string> pair in fileFields)
                        {
                            if (pair.Value == null)
                            {
                                continue;
                            }

                            ProfileField field = new ProfileField();
                            field.Name      = pair.Key;
                            field.Value     = UTF8Encoding.UTF8.GetBytes(Path.GetFileName(pair.Value));
                            field.FieldType = ProfileFieldType.File;

                            byte[] packet = field.Encode(Network.Protocol);
                            fieldPackets.Add(packet);
                            fieldsTotalSize += packet.Length;
                        }
                    }

                    if (fieldsTotalSize > 0)
                    {
                        written += Protocol.WriteToFile(new ProfileAttachment("fields", fieldsTotalSize), stream);
                    }


                    // write files info
                    if (fileFields != null)
                    {
                        foreach (string path in fileFields.Values)
                        {
                            using (FileStream file = File.OpenRead(path))
                                written += Protocol.WriteToFile(new ProfileAttachment("file=" + Path.GetFileName(path), file.Length), stream);
                        }
                    }


                    stream.WriteByte(0); // end packets
                    embeddedStart = written + 1;

                    // write template bytes
                    stream.Write(htmlBytes, 0, htmlBytes.Length);

                    // write field bytes
                    foreach (byte[] packet in fieldPackets)
                    {
                        stream.Write(packet, 0, packet.Length);
                    }


                    // write file bytes
                    const int buffSize = 4096;
                    byte[]    buffer   = new byte[buffSize];

                    if (fileFields != null)
                    {
                        foreach (string path in fileFields.Values)
                        {
                            using (FileStream file = File.OpenRead(path))
                            {
                                int read = buffSize;
                                while (read == buffSize)
                                {
                                    read = file.Read(buffer, 0, buffSize);
                                    stream.Write(buffer, 0, read);
                                }
                            }
                        }
                    }

                    stream.FlushFinalBlock();
                }

                OpVersionedFile vfile = Cache.UpdateLocal(tempPath, key, BitConverter.GetBytes(embeddedStart));

                Store.PublishDirect(Trust.GetLocsAbove(), Core.UserID, ServiceID, DataTypeFile, vfile.SignedHeader);
            }
            catch (Exception ex)
            {
                Network.UpdateLog("Profile", "Error updating local " + ex.Message);
            }
        }