示例#1
0
        public OpProfile GetProfile(ulong id)
        {
            OpProfile profile = null;

            ProfileMap.SafeTryGetValue(id, out profile);

            return(profile);
        }
示例#2
0
        private ProfileTemplate GetTemplate(ulong id)
        {
            OpProfile profile = Profiles.GetProfile(id);

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

            ProfileTemplate template = new ProfileTemplate(false, true);

            template.User     = Core.GetName(id);;
            template.FilePath = Profiles.GetFilePath(profile);
            template.FileKey  = profile.File.Header.FileKey;

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

            try
            {
                using (TaggedStream stream = new TaggedStream(template.FilePath, Core.GuiProtocol))
                    using (IVCryptoStream crypto = IVCryptoStream.Load(stream, template.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;
                        }

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

                                template.Html = UTF8Encoding.UTF8.GetString(html);
                                SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
                                template.Hash = sha1.ComputeHash(html);

                                break;
                            }
                        }
                    }
            }
            catch
            {
                return(null);
            }

            return(template);
        }
示例#3
0
        void Cache_FileRemoved(OpVersionedFile file)
        {
            OpProfile profile = GetProfile(file.UserID);

            if (profile != null)
            {
                ProfileMap.SafeRemove(file.UserID);
            }
        }
示例#4
0
        public void LoadProfile(ulong id)
        {
            OpProfile profile = GetProfile(id);

            if (profile == null)
            {
                return;
            }

            try
            {
                string path = GetFilePath(profile);

                if (!File.Exists(path))
                {
                    return;
                }

                profile.Attached = new List <ProfileAttachment>();

                using (TaggedStream file = new TaggedStream(path, Network.Protocol))
                    using (IVCryptoStream crypto = IVCryptoStream.Load(file, profile.File.Header.FileKey))
                    {
                        PacketStream stream = new PacketStream(crypto, Network.Protocol, FileAccess.Read);

                        G2Header root = null;

                        while (stream.ReadPacket(ref root))
                        {
                            if (root.Name == ProfilePacket.Attachment)
                            {
                                ProfileAttachment packet = ProfileAttachment.Decode(root);

                                if (packet == null)
                                {
                                    continue;
                                }

                                profile.Attached.Add(packet);
                            }
                        }
                    }

                profile.Loaded = true;
            }
            catch (Exception ex)
            {
                Network.UpdateLog("Profile", "Error loading file " + ex.Message);
            }
        }
示例#5
0
        private void Cache_FileAquired(OpVersionedFile file)
        {
            // get profile
            OpProfile prevProfile = GetProfile(file.UserID);

            OpProfile newProfile = new OpProfile(file);

            ProfileMap.SafeAdd(file.UserID, newProfile);


            if (file.UserID == Core.UserID)
            {
                LocalProfile = newProfile;
            }

            if ((newProfile == LocalProfile) || (prevProfile != null && prevProfile.Loaded))
            {
                LoadProfile(newProfile.UserID);
            }


            // update subs
            if (Network.Established)
            {
                List <LocationData> locations = new List <LocationData>();

                Trust.ProjectRoots.LockReading(delegate()
                {
                    foreach (uint project in Trust.ProjectRoots.Keys)
                    {
                        if (newProfile.UserID == Core.UserID || Trust.IsHigher(newProfile.UserID, project))
                        {
                            Trust.GetLocsBelow(Core.UserID, project, locations);
                        }
                    }
                });

                Store.PublishDirect(locations, newProfile.UserID, ServiceID, 0, file.SignedHeader);
            }

            if (ProfileUpdate != null)
            {
                Core.RunInGuiThread(ProfileUpdate, newProfile);
            }

            if (Core.NewsWorthy(newProfile.UserID, 0, false))
            {
                Core.MakeNews(ServiceIDs.Profile, "Profile updated by " + Core.GetName(newProfile.UserID), newProfile.UserID, 0, true);
            }
        }
示例#6
0
        void Trust_Update(ulong key)
        {
            // if updated node is local, or higher, refresh so motd updates
            if (key != UserID && !Core.Trust.IsHigher(UserID, key, ProjectID))
            {
                return;
            }

            OpProfile profile = Profiles.GetProfile(UserID);

            if (profile == null)
            {
                return;
            }

            Profile_Update(profile);
        }
示例#7
0
        public override void Init()
        {
            Profiles.ProfileUpdate += new ProfileUpdateHandler(Profile_Update);
            Trust.GuiUpdate        += new LinkGuiUpdateHandler(Trust_Update);
            Core.KeepDataGui       += new KeepDataHandler(Core_KeepData);

            OpProfile profile = Profiles.GetProfile(UserID);

            if (profile == null)
            {
                DisplayLoading();
            }
            else
            {
                Profile_Update(profile);
            }

            // have to set twice (init document?) for page to show up
            if (GuiUtils.IsRunningOnMono())
            {
                if (profile == null)
                {
                    DisplayLoading();
                }
                else
                {
                    Profile_Update(profile);
                }
            }

            List <ulong> ids = new List <ulong>();

            ids.AddRange(Trust.GetUplinkIDs(UserID, ProjectID));

            foreach (ulong id in ids)
            {
                Profiles.Research(id);
            }
        }
示例#8
0
        private static string FleshMotd(ProfileService service, string template, ulong id, uint project)
        {
            // extract motd template
            string startTag = "<?motd:start?>";
            string nextTag  = "<?motd:next?>";

            int start = template.IndexOf(startTag) + startTag.Length;
            int end   = template.IndexOf("<?motd:end?>");

            if (end < start)
            {
                return("");
            }

            string motdTemplate = template.Substring(start, end - start);

            // get links in chain up
            List <ulong> uplinks = new List <ulong>();

            uplinks.Add(id);
            uplinks.AddRange(service.Core.Trust.GetUplinkIDs(id, project));
            uplinks.Reverse();

            // build cascading motds
            string finalMotd = "";

            foreach (ulong uplink in uplinks)
            {
                OpProfile upperProfile = service.GetProfile(uplink);

                if (upperProfile != null)
                {
                    Dictionary <string, string> textFields = new Dictionary <string, string>();

                    LoadProfile(service, upperProfile, null, textFields, null);

                    string motdTag = "MOTD-" + project.ToString();
                    if (!textFields.ContainsKey(motdTag))
                    {
                        textFields[motdTag] = "No announcements";
                    }

                    textFields["MOTD"] = textFields[motdTag];

                    string currentMotd = motdTemplate;
                    currentMotd = FleshTemplate(service, uplink, project, currentMotd, textFields, null);

                    if (finalMotd == "")
                    {
                        finalMotd = currentMotd;
                    }

                    else if (finalMotd.IndexOf(nextTag) != -1)
                    {
                        finalMotd = finalMotd.Replace(nextTag, currentMotd);
                    }
                }
            }

            finalMotd = finalMotd.Replace(nextTag, "");

            return(finalMotd);
        }
示例#9
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);
        }
示例#10
0
        void Profile_Update(OpProfile profile)
        {
            // if self or in uplink chain, update profile
            List <ulong> uplinks = new List <ulong>();

            uplinks.Add(UserID);
            uplinks.AddRange(Core.Trust.GetUplinkIDs(UserID, ProjectID));

            if (!uplinks.Contains(profile.UserID))
            {
                return;
            }

            // get fields from profile

            // if temp/id dir exists use it
            // clear temp/id dir
            // extract files to temp dir

            // get html
            // insert fields into html

            // display

            string tempPath = Profiles.ExtractPath;


            // create if needed, clear of pre-existing data
            if (!Directory.Exists(tempPath))
            {
                Directory.CreateDirectory(tempPath);
            }

            // get the profile for this display
            profile = Profiles.GetProfile(UserID);

            if (profile == null)
            {
                return;
            }

            // not secure
            else
            {
                string[] files = Directory.GetFiles(tempPath);

                foreach (string path in files)
                {
                    File.Delete(path);
                }
            }

            string template = LoadProfile(Profiles, profile, tempPath, TextFields, FileFields);

            if (template == null)
            {
                template = @"<html>
                            <body>
                                <table width=""100%"" height=""100%"">
                                    <tr valign=""middle"">
                                        <td align=""center"">
                                        <b>Unable to Load</b>
                                        </td>
                                    </tr>
                                </table>
                            </body>
                        </html>";
            }

            string html = FleshTemplate(Profiles, profile.UserID, ProjectID, template, TextFields, FileFields);

            Browser.SetDocNoClick(html);
        }
示例#11
0
 public string GetFilePath(OpProfile profile)
 {
     return(Cache.GetFilePath(profile.File.Header));
 }