Wittyshare  0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
WsApplication.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-Today Guy Deleeuw
3  *
4  * See the LICENSE file for terms of use.
5 */
6 
7 #include <boost/algorithm/string/replace.hpp>
8 #include <boost/filesystem/operations.hpp>
9 #include <boost/filesystem/exception.hpp>
10 #include <boost/filesystem/convenience.hpp>
11 
12 #include <Wt/WLogger>
13 #include <Wt/WContainerWidget>
14 #include <Wt/WEnvironment>
15 #include <Wt/WVBoxLayout>
16 #include <Wt/WHBoxLayout>
17 #include <Wt/WOverlayLoadingIndicator>
18 #include <Wt/WTheme>
19 
20 #include "WsApplication.h"
22 
23 #include <gdcore/gdCore.h>
24 
25 
26 #include "Main/WsTemplate.h"
27 #include "Main/WsTopBanner.h"
28 #include "Main/WsBottomBanner.h"
30 #include "WsErrorPage.h"
32 
33 using namespace Wt;
34 
35 WsApplication::WsApplication(const WEnvironment& env)
36  : gdApplication(env), m_pUser(0), m_pButtonsBar(0), m_pContent(0), m_pImages(0)
37 {
38  std::string sUid = gdWApp->getUserID();
39  std::string sPwd;
40  wApp->log("notice") << "WsApplication::WsApplication - Start load user with uid : " << sUid;
41  std::string cip = environment().clientAddress();
42  wApp->log("notice") << "WsApplication::WsApplication - Start load user with client IP : " << cip << " env ip " << environment().clientAddress();
43  // TODO : Provisoire, si on est sur le reseau local on authentifie via ldap, sinon on considère que on est guest
44  bool isPublicSite = WsGlobalProperties::instance()->get("global", "public_site", "false") == "true" ? true : false;
45  // On recherche le theme par dans la configuration, si rien n'est configuré on prend le theme polised par defaut.
46  wApp->setCssTheme(WsLayoutProperties::instance()->get("global", "css_theme", "polished"));
47  wApp->useStyleSheet(wApp->theme()->resourcesUrl() + "wittyshare/Css/wittyshare.css");
48  if(boost::filesystem::exists(wApp->docRoot() + "/wt/resources/js/functions.js"))
49  wApp->require(wApp->resourcesUrl() + "js/functions.js");
50  if ( isPublicSite ) {
51  bool enableLoginWindow = WsGlobalProperties::instance()->get("global", "login_window", "true") == "true" ? true : false;
52  if (enableLoginWindow) {
53  if ( cip.compare(0, 8, "192.168.") == 0 || cip.compare(0, 9, "127.0.0.1") == 0 ) {
54  m_logon = new gdWLogon(sUid, sPwd, root());
55  m_logon->logonValidated().connect(SLOT(this, WsApplication::doEndDialogLogon));
56  m_logon->load();
57  } else
58  doEndDialogLogon("guest", "");
59  } else
60  doEndDialogLogon("guest", "");
61  } else // TODO : Improve the solution
62  doEndDialogLogon("", "");
63 }
64 
65 void WsApplication::doEndDialogLogon(std::string sUid, std::string pPassword)
66 {
67  m_pUser = new WsUser(sUid, pPassword, environment().clientAddress());
68  if ( !m_pUser || m_pUser->load() == ErrorCode::Failure ) {
69  wApp->log("ERROR") << "WsApplication::doEndDialogLogon - Cannot load user with uid : " << sUid;
70  if ( m_pUser )
71  delete m_pUser;
72  m_logon->setError("Invalid username or password");
73  m_pUser = 0;
74  return;
75  }
77  root()->addStyleClass("wsMainWin");
78  // Chargement du fichier de resources .xml
79  wApp->messageResourceBundle().use(wApp->docRoot() + wApp->resourcesUrl() + "wittyshare/Transl/wittyshare");
80  // Chargement du fichier de configuration .xml
81  wApp->messageResourceBundle().use(wApp->docRoot() + wApp->theme()->resourcesUrl() + "wittyshare/Conf/wittyshare_config");
82  wApp->setTitle(WsLayoutProperties::instance()->get("global", "win_title", ""));
83  WApplication::instance()->setLoadingIndicator(new WOverlayLoadingIndicator());
84  m_ml.load(); // Load all modules
85  std::string oldInternalPath = wApp->internalPath();
86  boost::algorithm::replace_all(oldInternalPath, "&amp;", "&");
87  oldInternalPath = m_ml.checkPath(oldInternalPath);
88  std::string truePath(WsModules().pathWithoutPrefix(oldInternalPath));
89  if ( oldInternalPath != "/" && oldInternalPath.size() > 0 ) {
90  // TODO : Boucler par module
91  int perms = m_pUser->getPermissions(truePath);
92  if ( perms == ErrorCode::NotLogged ) {
93  root()->clear();
94  wApp->root()->addWidget(new WsErrorPage(WsErrorPage::UnAuthorized, truePath,m_pUser, "", true));
95  return;
96  }
97  if( perms == ErrorCode::NotFound) {
98  root()->clear();
99  wApp->root()->addWidget(new WsErrorPage(WsErrorPage::NotFound, truePath,m_pUser, "", true));
100  return;
101  }
102 
103  if ( perms != GlobalConfig::Read && perms != GlobalConfig::ReadWrite) {
104  root()->clear();
105  wApp->root()->addWidget(new WsErrorPage(WsErrorPage::Forbidden, truePath,m_pUser, "", true));
106  return;
107  }
108  } else {
109  oldInternalPath = ""; // reset sais + pq
110  }
112  if ( WsLayoutProperties::instance()->get("global", "use_template", "true") == "true") {
113  if ( oldInternalPath.size() > 1 ) {
114  setTemplate(oldInternalPath);
115  } else {
117  }
118  } else {
119  WVBoxLayout* vbox = new WVBoxLayout();
120  vbox->setContentsMargins(0, 0, 0, 0);
121  WsContent* pContent = new WsContent();
122  m_pContent = pContent;
123  if ( oldInternalPath.size() > 0 )
124  m_pContent->setPath(oldInternalPath);
125  else
127  if ( WsLayoutProperties::instance()->get("global", "use_topbanner", "true") == "true" ) {
128  WsTopBanner* pTB = new WsTopBanner();
129  vbox->addWidget(pTB, 0);
130  }
131  /*
132  if ( WsLayoutProperties::instance()->get("global", "use_menubar", "true") == "true" )
133  {
134  LOG(DEBUG)<<"WsApplication :: Use menu bar";
135  WsMenuBar* pBar = new WsMenuBar();
136  vbox->addWidget(pBar, 0);
137  }
138  */
139  if ( WsLayoutProperties::instance()->get("global", "use_fonctionnalities", "true") == "true" ) {
140  vbox->addWidget(new WsFunctionnalities());
141  }
142  if (WsLayoutProperties::instance()->get("global", "use_ws_images", "false") == "true") {
143  m_pImages = WsApp->WsModules().module("WsModImages2")->createContents();
144  WHBoxLayout* hbox = new WHBoxLayout();
145  hbox->addWidget(m_pImages, 0);
146  hbox->addWidget(pContent, 1);
147  vbox->addLayout(hbox, 1);
148  } else
149  vbox->addWidget(pContent, 1);
150  if (WsLayoutProperties::instance()->get("global", "use_bottombanner", "true") == "true") {
151  vbox->addWidget(new WsBottomBanner(WsLayoutProperties::instance()->get("global", "bottombanner_text", "")), 0);
152  }
153  root()->setLayout(vbox);
154  }
155  wApp->internalPathChanged().connect(SLOT(this, WsApplication::doPathChanged));
156  wApp->internalPathChanged().connect(SLOT(this, WsApplication::googleAnalyticsLogger));
157 }
158 
160 {
161  if ( m_pUser )
162  delete m_pUser;
163  m_pUser = 0;
164 }
165 
166 
167 void WsApplication::doPathChanged(std::string newPath)
168 {
169  setTemplate(newPath);
170 }
171 
173 {
174  if ( m_pImages ) m_pImages->setHidden(bHide);
175 }
176 
178 {
179  return m_pUser;
180 }
181 
183 {
184  return m_ml;
185 }
186 
187 void WsApplication::setTemplate(const std::string& sPageOrig)
188 {
189  std::string sPage = sPageOrig;
190  boost::algorithm::replace_all(sPage, "&amp;", "&");
191  if ( WsLayoutProperties::instance()->get("global", "use_template", "true") == "true" ) {
192  std::string truePath(WsModules().pathWithoutPrefix(sPage));
193  NodePtr rootNode = m_pUser->getAccessRoot();
194  if ( !rootNode.get() ) {
195  root()->addWidget(new WsErrorPage(WsErrorPage::Error, sPage,m_pUser, "WsApplication::setTemplate() root node is null"));
196  return;
197  }
198  NodePtr curNode = rootNode.get()->eatPath(truePath);
199  if ( !curNode.get() ) {
200  boost::filesystem::path p(truePath);
201  truePath = p.parent_path().string();
202  curNode = rootNode.get()->eatPath(truePath);
203  }
204  // Boucle sur parent, jusqu'a obtention du template
205  std::string sTemplate;
206  std::string sPath;
207  NodePtr pNode = curNode;
208  while (pNode.get()) {
209  sTemplate = pNode.get()->getProperties().get()->get("global", "template", "");
210  if ( sTemplate.size() > 0 ) break;
211  pNode = pNode.get()->getParent();
212  }
213  if ( sTemplate.size() < 1 ) sTemplate = "root.tpl"; // ce template doit exister
214  if ( sPage.compare(0, 5, "/Edit" ) == 0 ) sTemplate = "stdEditPage.tpl"; // cui-ci aussi
215  if ( sPage.compare(0, 8, "/SiteMap" ) == 0 ) sTemplate = "stdContact.tpl"; // cui-ci aussi
216  if ( sPage.compare(0, 5, "/Logo" ) == 0 ) sTemplate = "GhFullWidth.tpl"; // cui-ci aussi
217  if ( sPage.compare(0, 7, "/Search" ) == 0 ) sTemplate = "stdContact.tpl"; // cui-ci aussi
218  if ( pNode.get() ) {
219  sPath = sPage;
220  } else {
221  sPath = m_sHomePage;
222  }
223  if ( sTemplate != m_sTemplate ) {
224  m_sTemplate = sTemplate;
225  root()->clear();
226  m_pButtonsBar = 0;
227  m_pContent = 0; // le template doit prevoir la creation du content
228  m_pImages = 0;
229  WsTemplate* pTemplate = new WsTemplate(sTemplate, sPath);
230  bool bUseLayout = WsLayoutProperties::instance()->get("global", "useLayout", "false") == "true" ? true : false;
231  if ( bUseLayout ) {
232  WVBoxLayout* vbox = new WVBoxLayout();
233  vbox->setContentsMargins(0, 0, 0, 0);
234  vbox->addWidget(pTemplate, 1);
235  root()->setLayout(vbox);
236  } else {
237  root()->addWidget(pTemplate);
238  // root()->resize(WLength(100, WLength::Percentage), WLength(100, WLength::Percentage));
239  }
240  return;
241  }
242  }
243  if ( m_pContent )
244  m_pContent->setPath(sPage);
245 }
246 
248 {
249  //FIXME double free here
250  //if ( m_pContent )
251  // delete m_pContent;
252  m_pContent = pContent;
253  m_pContent->setMaximumSize(WLength::Auto, WLength(10000));
254 }
255 
257 {
258  m_pButtonsBar = pButtonsBar;
259 }
260 
262 {
263  return m_pButtonsBar;
264 }
265 
266 const std::string& WsApplication::homePage()
267 {
268  return m_sHomePage;
269 }
270 
271 void WsApplication::googleAnalyticsLogger(std::string newPath)
272 {
273  bool enableAnalytics = WsGlobalProperties::instance()->get("analytics", "enable", "false") == "true" ? true : false;
274  if (!enableAnalytics) return ;
275  std::string UA = WsGlobalProperties::instance()->get("analytics", "UA", "") ;
276  std::string domain = WsGlobalProperties::instance()->get("analytics", "domain", "none") ;
277  std::string googleCmd = "(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){\
278  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),\
279  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)\
280  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');\
281  ga('create', '" + UA + "', '" + domain + "');\
282  ga('send', 'pageview');";
283  doJavaScript(googleCmd);
284 }
WsUser * wsUser()
WsModulesLoader & WsModules()
void doPathChanged(std::string newPath)
boost::shared_ptr< WsAbstractNode > NodePtr
int getPermissions(const std::string &p)
return the permissions on specific node
Definition: WsUser.cpp:81
WsUser * m_pUser
Definition: WsApplication.h:49
gdWLogon * m_logon
Definition: WsApplication.h:57
const int ReadWrite
const int Failure
const int Read
void hideImages(bool bHide=true)
std::string m_sTemplate
Definition: WsApplication.h:56
std::string checkPath(const std::string &path)
void setPath(std::string newPath)
Definition: WsContent.cpp:191
void setContent(WsContent *pContent)
const std::string getHomePage()
return the home page
Definition: WsUser.cpp:140
const int NotFound
Interface that provides differents methods for accessing the FsTree as well as other features...
Definition: WsUser.h:33
const int NotLogged
std::string m_sHomePage
Definition: WsApplication.h:55
void googleAnalyticsLogger(std::string newPath)
void setTemplate(const std::string &sPage)
int load()
Load infos of the user.
Definition: WsUser.cpp:22
WsContentButtonsBar * m_pButtonsBar
Definition: WsApplication.h:51
WsModulesLoader m_ml
Definition: WsApplication.h:54
std::string get(const std::string &section, const std::string &id, const std::string &def)
void doEndDialogLogon(std::string sUid, std::string pPassword)
std::string get(const std::string &section, const std::string &id, const std::string &def)
#define WsApp
Define a shortcut to the application instance.
Definition: WsApplication.h:62
static WsLayoutProperties * instance()
Get the singleton instance.
const std::string & homePage()
static WsGlobalProperties * instance()
WsContentButtonsBar * contentButtonsBar()
NodePtr getAccessRoot()
return the root node of the access tree starting from the root
Definition: WsUser.cpp:50
WsApplication(const Wt::WEnvironment &env)
WsContent * m_pContent
Definition: WsApplication.h:52
Wt::WWidget * m_pImages
Definition: WsApplication.h:53
void setContentButtonsBar(WsContentButtonsBar *pButtonsBar)