コード例 #1
0
ファイル: sNBBPage.cs プロジェクト: apreshchandra/Test
        protected override void OnPreLoad(EventArgs e)
        {
            try
            {
                base.OnPreLoad(e);
                FacebookBizProcess fbBiz = new FacebookBizProcess();
                FaceBook oFBUtility = new FaceBook();
                SonetPieBizProcess sonetpiebiz = new SonetPieBizProcess();
                SonetPie sonetpie = new SonetPie();
                NotifyURL = ConfigurationSettings.AppSettings["NotifyURL"];
                pageRefreshed = false;

                if (QSVars.Contains("user_id"))
                {
                    Session["facebook_user_id"] = QSVars["user_id"].ToString();
                }
                if (QSVars.Contains("UDID"))
                {
                    Session["sr_user_did"] = QSVars["UDID"].ToString();
                }
                if (QSVars.Contains("PDID"))
                {
                    Session["sr_product_did"] = QSVars["PDID"].ToString();
                }

                if (Request.Url.Query.Contains("request") && !(Request.Url.Query.Contains("fb_source"))) //AppRequest callback, now save to notifiers
                {

                    for (int i = 0; i < Request.QueryString.Count - 2; i++)
                    {
                        //every Request["to[i]"] has a userID for AppNotifier
                        string fbUID = Request["to[" + i + "]"].ToString();
                        string _sNotifierDID = new AppNotifier().GetNewDIDWithPrefix();
                        string facebook_user_id = Convert.ToString(Session["facebook_user_id"]);
                        string UDID = Convert.ToString(Session["sr_user_did"]);
                        if (QSVars.Contains("NDID"))
                            QSVars["NDID"] = _sNotifierDID;
                        else
                            QSVars.Add("NDID", _sNotifierDID);

                        //Build DCAppWallPost for each user

                        AppUser oDCAppUser = new AppUser();
                        string ConfigDID = fbBiz.GetConfigDEED(Convert.ToString(QSVars["app_id"]));
                        if (!QSVars.Contains("ADID"))
                        {
                            QSVars.Add("ADID", ConfigDID);
                        }
                        if (!QSVars.Contains("PDID"))
                        {
                            QSVars.Add("PDID", Convert.ToString(Session["sr_product_did"]));
                        }
                        oDCAppUser = ofbBiz.GetAppUser(osonetpie, QSVars["ADID"].ToString(), facebook_user_id);

                        _oDCAppWallPost.FromUserID = Convert.ToString(QSVars["user_id"]);
                        _oDCAppWallPost.ToUserID = fbUID;
                        _oDCAppWallPost.Name = ofbBiz.GetCustomTabName(QSVars["app_id"].ToString());
                        _oDCAppWallPost.Source = Convert.ToString(QSVars["user_id"]);
                        AppLeadData oAppLead = new AppLeadData();

                        if (Request.Browser.IsMobileDevice == true || Request.UserAgent.ToLower().Contains("iphone") || Request.UserAgent.ToLower().Contains("android"))
                        {
                            if (fbBiz.RaiseAppNotifier(oDCAppUser, "MPOST", UDID, Convert.ToString(QSVars["PDID"]), "", fbUID))
                            {

                            }
                        }
                        else
                        {
                            if (fbBiz.RaiseAppNotifier(oDCAppUser, "POST", UDID, Convert.ToString(QSVars["PDID"]), "", fbUID))
                            {

                            }
                        }
                    }
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "myCloseScript", "window.close();", true);
                }

                if (Request["code"] == null)
                {
                    if ((Request.QueryString.Count > 0) && !(Request["LIKED"] != null))
                    {
                        //ParseRequest & set values to qsvars
                        if (!(Request.QueryString).AllKeys[0].ToString().Contains("tabs_added"))
                        {
                            //REVISIT

                            sonetpie.QSvarsString = GetQsVarsCollection();
                            sonetpie.FormvarsString = GetFormVarsCollection();
                            sonetpie.AbsolutePath = AbsolutePagePath;

                            if (Request.Form.ToString() != string.Empty)
                            {
                                FormVars.Add("signed_request", Request.Form.Get(0).ToString());
                            }

                            //Init Biz
                            //SonetPieBizProcess sonetpiebiz = new SonetPieBizProcess();

                            if (QSVars.Contains("app_id"))
                            {

                                if (EnableAppServices) _oDCAppConfiguration = sonetpiebiz.GetAppConfiguration("", QSVars["app_id"].ToString());
                                SessionData.Config.DID = _oDCAppConfiguration.DID;
                                //DigiMa.Common.FaceBook oFBUtility1 = new DigiMa.Common.FaceBook();

                                //check AppConfig got loaded
                                if (AppConfig != null)
                                {
                                    //Add CDID to Load customer
                                    if (QSVars.Contains("CDID"))
                                        QSVars["CDID"] = AppConfig.AppCustomerDID;
                                    else
                                        QSVars.Add("CDID", AppConfig.AppCustomerDID);

                                    //Add ADID to Load customer
                                    if (QSVars.Contains("ADID"))
                                        QSVars["ADID"] = AppConfig.DID;
                                    else
                                        QSVars.Add("ADID", AppConfig.DID);

                                    //Reset KOKO QSVars
                                    sonetpie.QSvarsString = GetQsVarsCollection();
                                    _oDCAppCustomer = fbBiz.GetAppCustomer(Convert.ToString(QSVars["CDID"]));

                                    //ParseSigned Request
                                    if ((!QSVars.Contains("oauth_token")))
                                    {
                                        oFBUtility.ParseSignedRequest(ref _qsVars, ref _frmVars, AppConfig);
                                    }

                                    if (QSVars.Contains("oauth_token"))
                                    {
                                        Session["oauth_token"] = Convert.ToString(QSVars["oauth_token"]);
                                    }

                                    if (QSVars.Contains("user_id"))
                                    {
                                        Session["user_id"] = Convert.ToString(QSVars["user_id"]);
                                    }

                                    //call graph to get page_Acc_tok
                                    if (QSVars.Contains("oauth_token") && (_oDCAppConfiguration.SAppCustomNameAdded == null || _oDCAppConfiguration.SAppCustomNameAdded == ""))
                                    {
                                        page_access_token = facebook.GetPageAccessToken(QSVars["oauth_token"].ToString());
                                        System.Web.Script.Serialization.JavaScriptSerializer _oJavaScriptSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                                        JObject obj = JObject.Parse(page_access_token);
                                        IEnumerable<string> query = from res in (Newtonsoft.Json.Linq.JArray)obj["data"]
                                                                    let reso = res as JObject
                                                                    where ((string)reso["id"]).ToLower() == _oDCAppConfiguration.SPageID
                                                                    select (string)reso["access_token"];

                                        //fetch the logo of this Campaign from AppProduct and pass to CallPages

                                        if (facebook.CallPages(_oDCAppConfiguration.SPageID, query.ToList()[0].ToString(), "app_" + QSVars["app_id"].ToString(), _oDCAppConfiguration.SCustomtTabName, fbBiz.FetchAppLogo(QSVars["app_id"].ToString())))
                                        {
                                            //update custom_updated to Y
                                            fbBiz.UpdateCustomTabNAme(_oDCAppConfiguration.DID);
                                            ClientScript.RegisterStartupScript(this.GetType(), "scriptid", "window.parent.location.href='" + _oDCAppConfiguration.SAppPagePath + "'", true);
                                        }

                                    }

                                    //parse the JSON
                                    if (HasAuthorization)
                                    {
                                        if (!QSVars.Contains("oauth_token"))
                                        {
                                            //Set Authorization stamp on redirect url
                                            AppConfig.AppPath += "?soNETSrc=";
                                            if (QSVars.Contains("NDID"))
                                                AppConfig.AppPath += Convert.ToString(QSVars["NDID"]);
                                            else
                                                AppConfig.AppPath += "NULL";

                                            //oFBUtility.GetAccessToken(QSVars["code"].ToString(), "user_location,email,friends_location,publish_stream",AppConfig.AppPath,AppConfig);

                                            // CHeck if Request is coming from Mobile device, then open Standalone
                                            if (Request.Browser.IsMobileDevice == true || Request.UserAgent.ToLower().Contains("iphone") || Request.UserAgent.ToLower().Contains("android"))
                                            {
                                                //oFBUtility.AuthorizeMob(this, "user_location,email,friends_location,user_birthday", AppConfig);
                                            }
                                            else
                                            {
                                                oFBUtility.Authorize(this, "user_location,email,friends_location,user_birthday", AppConfig);
                                            }

                                        }
                                        else
                                        {
                                            //Insert user into DB
                                            if (true)
                                            {
                                                //Call service to store into DB
                                                AppUser oDCAppUser = new AppUser();
                                                oDCAppUser.AppConfigDID = AppConfig.DID;
                                                oDCAppUser.EmailID = "NULL";
                                                oDCAppUser.SonetID = Convert.ToString(QSVars["user_id"]);
                                                oDCAppUser.SonetSRC = Convert.ToString(QSVars["soNETSrc"]);
                                                oDCAppUser.UserStatus = "Active";
                                                oDCAppUser.SMType = "FB";
                                                oDCAppUser = oFBUtility.GetUserDetail(Convert.ToString(QSVars["user_id"]), Convert.ToString(QSVars["oauth_token"]), oDCAppUser);

                                                //Save to DB
                                                if (!fbBiz.IsUserCreatedForFacebook(oDCAppUser.SonetID, oDCAppUser.AppConfigDID))
                                                {
                                                    if (fbBiz.SetAppUserAuthorize(oDCAppUser, Convert.ToString(QSVars["ADID"]))) _oDCAppUser = oDCAppUser;
                                                }
                                            }
                                        }
                                    }
                                }

                                //refrsh to get tab name
                                //fbBiz.UpdateCustomTabNAme(_oDCAppConfiguration.DID);
                                //ClientScript.RegisterStartupScript(this.GetType(), "scriptid", "window.parent.location.href='" + _oDCAppConfiguration.SAppPagePath + "'", true);

                                //Load AppUser based on property
                                if (_bEnableAppUser && _qsVars.Contains("user_id"))
                                {
                                    sonetpie.QSvarsString = GetQsVarsCollection();
                                    _oDCAppUser = fbBiz.GetAppUser(sonetpie, Convert.ToString(QSVars["ADID"]), Convert.ToString(QSVars["user_id"]));
                                    if (_oDCAppUser != null)
                                    {
                                        if (!_qsVars.Contains("UDID")) _qsVars.Add("UDID", _oDCAppUser.DID); else _qsVars["UDID"] = _oDCAppUser.DID;
                                        if (QSVars.Count < 8)
                                        {
                                            ClientScript.RegisterStartupScript(this.GetType(), "scriptid", "window.parent.location.href='" + _oDCAppConfiguration.SAppPagePath + "'", true);//To ensure all QSVARS are loaded from facebook
                                        }
                                    }
                                }

                                //Update Notifier count based on NDID or soNETSrc
                                if (QSVars.Contains("soNETSrc") || QSVars.Contains("NDID"))
                                {
                                    AppNotifier oAppNotifier = new AppNotifier();
                                    if (QSVars.Contains("soNETSrc")) oAppNotifier.DID = Convert.ToString(QSVars["soNETSrc"]);
                                    if (QSVars.Contains("NDID")) oAppNotifier.DID = Convert.ToString(QSVars["NDID"]);
                                    if (!string.IsNullOrEmpty(oAppNotifier.DID)) fbBiz.EditAppNotifierDetails(oAppNotifier);
                                }
                            }
                        }

                    }
                    else
                    {
                        //
                    }
                }

                else
                {

                    //Here redirect user to Page path
                    if (QSVars.Contains("app_id") && QSVars.Contains("soNETSrc"))
                    {
                        //string pageToRedirect = fbBiz.GetAppPagePath(Convert.ToString(QSVars["app_id"]));
                        //Response.Redirect(pageToRedirect,true);

                    }

                }

            }
            catch (Exception ex)
            {
                CommonUtility commUtil = new CommonUtility();
                commUtil.SendErrorMail(ex.Message, ex.StackTrace, System.Reflection.MethodBase.GetCurrentMethod().Name.ToString(), System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString(), SessionData.Customer.CustomerID);
            }
        }