/* * Box Social™ * http://boxsocial.net/ * Copyright © 2007, David Lachlan Smith * * $Id:$ * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3 as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ using System; using System.Data; using System.Configuration; using System.Collections; using System.Collections.Generic; using System.Reflection; using System.Web; using BoxSocial; using BoxSocial.Internals; using BoxSocial.IO; namespace BoxSocial.Internals { /// /// Defines the base class for account modules, which are components /// which applications use to plug into the account management system. /// public abstract class ControlPanelModule : MarshalByRefObject, IComparable { public delegate void RegisterSubModuleHandler(string submodule); public event RegisterSubModuleHandler RegisterSubModule; /// /// A list of submodules registered in the current module /// protected Dictionary subModules = new Dictionary(); protected Core core; protected TPage page; protected Mysql db; protected Primitive Owner; protected User loggedInMember; protected Template template; protected UnixTime tz; //protected HttpRequest Request; //protected HttpResponse Response; //protected HttpServerUtility Server; protected SessionState session; public Primitive SetOwner { set { Owner = value; } } /// /// The assembly associated with the account module /// public Assembly assembly = null; /// /// Initialises an account module, registering the sub module /// registration handler. /// private ControlPanelModule () { RegisterSubModule += new RegisterSubModuleHandler(OnRegisterSubModule); } /// /// Initialises an account module. /// /// Binds the module to the account panel, and registers /// the sub module registration handler. /// public ControlPanelModule(Account account) : base() { Bind(account); } /// /// Bind the module to the account panel. /// /// private void Bind(Account account) { account.RegisterModule += new Account.RegisterModuleHandler(RegisterModule); core = account.core; page = account.core.page; db = account.core.Db; loggedInMember = account.core.Session.LoggedInMember; tz = account.core.Tz; session = account.core.Session; } /// /// Creates an isolated template class for the module to render /// inside. /// public void CreateTemplate() { template = new Template(core.Http.TemplatePath, "1301.html"); template.Parse("U_ACCOUNT", core.Uri.AppendSid(Owner.AccountUriStub, true)); if (assembly != null) { template.AddPageAssembly(assembly); template.SetProse(core.Prose); } } /// /// Renders the template to the account panel. /// public void RenderTemplate() { core.Template.ParseRaw("MODULE_CONTENT", template.ToString()); } /// /// Renders an error to the account panel. /// /// public void DisplayError(string errorMessage) { template = new Template(core.Http.TemplatePath, "1302.html"); template.Parse("ERROR_MESSAGE", errorMessage); RenderTemplate(); } /// /// Callback on registration of a sub module in the account module. /// /// The sub module having been registered. private void OnRegisterSubModule(string submodule) { } /// /// Callback on registration of the module in the account panel. /// /// Core token /// An EventArgs that contains the event data. protected abstract void RegisterModule(Core core, EventArgs e); /// /// Registers the sub modules in the account module with the account /// panel. /// /// The sub module having been called. public void RegisterSubModules(string submodule) { //this.RegisterSubModule(submodule); } /// /// Display name of the module. /// public abstract string Name { get; } /// /// The unique key used to identify the module in requests. /// public string Key { get { Type type = this.GetType(); foreach (Attribute attr in type.GetCustomAttributes(typeof(AccountModuleAttribute), false)) { if (attr != null) { if (((AccountModuleAttribute)attr).Name != null) { return ((AccountModuleAttribute)attr).Name; } } } // null key, should not happen!!! return null; } } /// /// The order the module is to appear along the tab display. /// public abstract int Order { get; } /// /// Returns a list of sub modules registered with the account /// module. /// public Dictionary SubModules { get { return subModules; } } /// /// Authorises a request ensuring the SID is present in the URL to /// prevent undesired operation of the account panel for users. /// protected void AuthoriseRequestSid() { if (core.Http.Query["sid"] != session.SessionId) { if (string.IsNullOrEmpty(core.Http.Query["sid"])) { core.Display.ShowMessage("Unauthorised", "You are unauthorised to do this action."); return; } SelectQuery query = new SelectQuery("user_sessions"); query.AddCondition("session_string", core.Http.Query["sid"]); query.AddCondition("user_id", session.LoggedInMember.Id); query.AddCondition("session_signed_in", true); query.AddCondition("session_ip", session.IPAddress.ToString()); if (db.Query(query).Rows.Count == 0) { core.Display.ShowMessage("Unauthorised", "You are unauthorised to do this action."); return; } } } /// /// Implements CompareTo /// /// /// Comparison based on Order. Can be used to sort a list of /// modules in the desired display order. /// /// Object to compare with /// Comparisson value public int CompareTo(object obj) { if (!(obj is ControlPanelModule)) return -1; return Order.CompareTo(((ControlPanelModule)obj).Order); } /// /// Builds a URI to the current module /// /// URI built protected string BuildUri() { return core.Uri.AppendSid(string.Format("{0}{1}", Owner.AccountUriStub, Key)); } /// /// Builds a URI to the given sub module of the current module /// /// /// URI built protected string BuildUri(string sub) { return core.Uri.AppendSid(string.Format("{0}{1}/{2}", Owner.AccountUriStub, Key, sub)); } public string BuildModuleUri(string sub, string mode, bool appendSid) { return core.Uri.BuildAccountSubModuleUri(Owner, Key, sub, mode, appendSid); } public string BuildModuleUri(string sub, string mode, long id) { return core.Uri.BuildAccountSubModuleUri(Owner, Key, sub, mode, id); } /// /// Builds a URI to the sub module key given of the current module, /// appending additional query string arguments given. /// /// Sub module key /// Additional query string arguments /// URI built public string BuildUri(string sub, Dictionary arguments) { string argumentList = string.Empty; foreach (string key in arguments.Keys) { if (argumentList == string.Empty) { argumentList = string.Format("?{0}={1}", key, arguments[key]); } else { argumentList = string.Format("{0}&{1}={2}", argumentList, key, arguments[key]); } } return core.Uri.AppendSid(string.Format("{0}{1}/{2}{3}", Owner.AccountUriStub, Key, sub, argumentList)); } public string BuildUri(string module, string sub, params string[] arguments) { return core.Uri.BuildAccountSubModuleUri(Owner, module, sub, false, arguments); } /// /// Sets the redirect URI. /// /// /// Useful for redirecting from a message box after posting a form. /// /// URI to redirect to protected void SetRedirectUri(string uri) { core.Template.Parse("REDIRECT_URI", uri); } /// /// Sets an error in posting. /// /// String of error to be posted protected void SetError(string errorString) { core.Template.Parse("ERROR", errorString); } protected void AssertFormVariable(string var) { } } [DataTable("account_modules")] public class ControlPanelModuleRegister : NumberedItem { [DataField("module_id", DataFieldKeys.Primary)] private long moduleId; [DataField("module_module", 63)] private string moduleKey; [DataField("application_id")] private int applicationId; [DataField("module_updated_ut")] private long updated; public ControlPanelModuleRegister(Core core, long moduleId) : base(core) { ItemLoad += new ItemLoadHandler(ControlPanelModuleRegister_ItemLoad); try { LoadItem(moduleId); } catch (InvalidItemException) { throw new InvalidAccountModuleException(); } } private void ControlPanelModuleRegister_ItemLoad() { } public override long Id { get { return moduleId; } } public override string Uri { get { throw new NotImplementedException(); } } } public class InvalidAccountModuleException : Exception { } }