PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/application/controllers/AdminController.php

https://bitbucket.org/machaven/limesurvey
PHP | 473 lines | 308 code | 64 blank | 101 comment | 76 complexity | d545bc0cabd420a4548fb31a31ee6ab1 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause, GPL-3.0, LGPL-3.0
  1. <?php
  2. /*
  3. * LimeSurvey
  4. * Copyright (C) 2007-2011 The LimeSurvey Project Team / Carsten Schmitz
  5. * All rights reserved.
  6. * License: GNU/GPL License v2 or later, see LICENSE.php
  7. * LimeSurvey is free software. This version may have been modified pursuant
  8. * to the GNU General Public License, and as distributed it includes or
  9. * is derivative of works licensed under the GNU General Public License or
  10. * other free or open source software licenses.
  11. * See COPYRIGHT.php for copyright notices and details.
  12. *
  13. * $Id$
  14. */
  15. class AdminController extends LSYii_Controller
  16. {
  17. public $lang = null;
  18. protected $user_id = 0;
  19. /**
  20. * Initialises this controller, does some basic checks and setups
  21. *
  22. * @access protected
  23. * @return void
  24. */
  25. protected function _init()
  26. {
  27. parent::_init();
  28. $updatelastcheck = getGlobalSetting('updatelastcheck');
  29. $this->_sessioncontrol();
  30. if (Yii::app()->getConfig('buildnumber') != "" && Yii::app()->getConfig('updatecheckperiod') > 0 && $updatelastcheck < dateShift(date("Y-m-d H:i:s"), "Y-m-d H:i:s", "-". Yii::app()->getConfig('updatecheckperiod')." days"))
  31. updateCheck();
  32. //unset(Yii::app()->session['FileManagerContext']);
  33. $this->user_id = Yii::app()->user->getId();
  34. Yii::app()->setConfig('adminimageurl', Yii::app()->getConfig('styleurl').Yii::app()->getConfig('admintheme').'/images/');
  35. Yii::app()->setConfig('adminstyleurl', Yii::app()->getConfig('styleurl').Yii::app()->getConfig('admintheme').'/');
  36. if (!Yii::app()->getConfig("surveyid")) {Yii::app()->setConfig("surveyid", returnGlobal('sid'));} //SurveyID
  37. if (!Yii::app()->getConfig("ugid")) {Yii::app()->setConfig("ugid", returnGlobal('ugid'));} //Usergroup-ID
  38. if (!Yii::app()->getConfig("gid")) {Yii::app()->setConfig("gid", returnGlobal('gid'));} //GroupID
  39. if (!Yii::app()->getConfig("qid")) {Yii::app()->setConfig("qid", returnGlobal('qid'));} //QuestionID
  40. if (!Yii::app()->getConfig("lid")) {Yii::app()->setConfig("lid", returnGlobal('lid'));} //LabelID
  41. if (!Yii::app()->getConfig("code")) {Yii::app()->setConfig("code", returnGlobal('code'));} // ??
  42. if (!Yii::app()->getConfig("action")) {Yii::app()->setConfig("action", returnGlobal('action'));} //Desired action
  43. if (!Yii::app()->getConfig("subaction")) {Yii::app()->setConfig("subaction", returnGlobal('subaction'));} //Desired subaction
  44. if (!Yii::app()->getConfig("editedaction")) {Yii::app()->setConfig("editedaction", returnGlobal('editedaction'));} // for html editor integration
  45. }
  46. /**
  47. * Shows a nice error message to the world
  48. *
  49. * @access public
  50. * @param string $message The error message
  51. * @param string|array $url URL. Either a string. Or array with keys url and title
  52. * @return void
  53. */
  54. public function error($message, $url = array())
  55. {
  56. $clang = $this->lang;
  57. $this->_getAdminHeader();
  58. $output = "<div class='messagebox ui-corner-all'>\n";
  59. $output .= '<div class="warningheader">'.$clang->gT('Error').'</div><br />'."\n";
  60. $output .= $message . '<br /><br />'."\n";
  61. if (!empty($url) && !is_array($url))
  62. {
  63. $title = $clang->gT('Back');
  64. }
  65. elseif (!empty($url['url']))
  66. {
  67. if (!empty($url['title']))
  68. {
  69. $title = $url['title'];
  70. }
  71. else
  72. {
  73. $title = $clang->gT('Back');
  74. }
  75. $url = $url['url'];
  76. }
  77. else
  78. {
  79. $title = $clang->gT('Main Admin Screen');
  80. $url = $this->createUrl('/admin');
  81. }
  82. $output .= '<input type="submit" value="'.$title.'" onclick=\'window.open("'.$url.'", "_top")\' /><br /><br />'."\n";
  83. $output .= '</div>'."\n";
  84. $output .= '</div>'."\n";
  85. echo $output;
  86. $this->_getAdminFooter('http://docs.limesurvey.org', $clang->gT('LimeSurvey online manual'));
  87. die;
  88. }
  89. /**
  90. * Load and set session vars
  91. *
  92. * @access protected
  93. * @return void
  94. */
  95. protected function _sessioncontrol()
  96. {
  97. Yii::import('application.libraries.Limesurvey_lang');
  98. // From personal settings
  99. if (Yii::app()->request->getPost('action') == 'savepersonalsettings') {
  100. if (Yii::app()->request->getPost('lang')=='auto')
  101. {
  102. $sLanguage= getBrowserLanguage();
  103. }
  104. else
  105. {
  106. $sLanguage=Yii::app()->request->getPost('lang');
  107. }
  108. Yii::app()->session['adminlang'] = $sLanguage;
  109. }
  110. if (empty(Yii::app()->session['adminlang']))
  111. Yii::app()->session["adminlang"] = Yii::app()->getConfig("defaultlang");
  112. $this->lang = new Limesurvey_lang(Yii::app()->session['adminlang']);
  113. Yii::app()->setLang($this->lang);
  114. if (!empty($this->user_id))
  115. $this->_GetSessionUserRights($this->user_id);
  116. }
  117. /**
  118. * Checks for action specific authorization and then executes an action
  119. *
  120. * @access public
  121. * @param string $action
  122. * @return bool
  123. */
  124. public function run($action)
  125. {
  126. // Check if the DB is up to date
  127. if (Yii::app()->db->schema->getTable('{{surveys}}'))
  128. {
  129. $usrow = getGlobalSetting('DBVersion');
  130. if ((int) $usrow < Yii::app()->getConfig('dbversionnumber') && $action != 'update' && $action != 'authentication')
  131. $this->redirect($this->createUrl('/admin/update/sa/db'));
  132. }
  133. if ($action != "update" && $action != "db")
  134. if (empty($this->user_id) && $action != "authentication" && $action != "remotecontrol")
  135. {
  136. if (!empty($action) && $action != 'index')
  137. Yii::app()->session['redirect_after_login'] = $this->createUrl('/');
  138. Yii::app()->session['redirectopage'] = Yii::app()->request->requestUri;
  139. $this->redirect($this->createUrl('/admin/authentication/sa/login'));
  140. }
  141. elseif (!empty($this->user_id) && $action != "remotecontrol")
  142. {
  143. if (Yii::app()->session['session_hash'] != hash('sha256',getGlobalSetting('SessionName').Yii::app()->user->getName().Yii::app()->user->getId()))
  144. {
  145. Yii::app()->session->clear();
  146. Yii::app()->session->close();
  147. $this->redirect($this->createUrl('/admin/authentication/sa/login'));
  148. }
  149. }
  150. return parent::run($action);
  151. }
  152. /**
  153. * Routes all the actions to their respective places
  154. *
  155. * @access public
  156. * @return array
  157. */
  158. public function actions()
  159. {
  160. $actions = $this->getActionClasses();
  161. foreach ($actions as $action => $class)
  162. {
  163. $actions[$action] = "application.controllers.admin.{$class}";
  164. }
  165. return $actions;
  166. }
  167. public function getActionClasses()
  168. {
  169. return array(
  170. 'assessments' => 'assessments',
  171. 'authentication' => 'authentication',
  172. 'checkintegrity' => 'checkintegrity',
  173. 'conditions' => 'conditionsaction',
  174. 'database' => 'database',
  175. 'dataentry' => 'dataentry',
  176. 'dumpdb' => 'dumpdb',
  177. 'emailtemplates' => 'emailtemplates',
  178. 'export' => 'export',
  179. 'expressions' => 'expressions',
  180. 'globalsettings' => 'globalsettings',
  181. 'htmleditor_pop' => 'htmleditor_pop',
  182. 'limereplacementfields' => 'limereplacementfields',
  183. 'index' => 'index',
  184. 'labels' => 'labels',
  185. 'participants' => 'participantsaction',
  186. 'printablesurvey' => 'printablesurvey',
  187. 'question' => 'question',
  188. 'questiongroup' => 'questiongroup',
  189. 'quotas' => 'quotas',
  190. 'remotecontrol' => 'remotecontrol',
  191. 'responses' => 'responses',
  192. 'saved' => 'saved',
  193. 'statistics' => 'statistics',
  194. 'survey' => 'surveyadmin',
  195. 'surveypermission' => 'surveypermission',
  196. 'user' => 'useraction',
  197. 'usergroups' => 'usergroups',
  198. 'templates' => 'templates',
  199. 'tokens' => 'tokens',
  200. 'translate' => 'translate',
  201. 'update' => 'update',
  202. );
  203. }
  204. /**
  205. * Set Session User Rights
  206. *
  207. * @access public
  208. * @return void
  209. */
  210. public function _GetSessionUserRights($loginID)
  211. {
  212. $user = User::model()->findByPk($loginID);
  213. if (!empty($user))
  214. {
  215. Yii::app()->session['USER_RIGHT_SUPERADMIN'] = $user->superadmin;
  216. Yii::app()->session['USER_RIGHT_CREATE_SURVEY'] = ($user->create_survey || $user->superadmin);
  217. Yii::app()->session['USER_RIGHT_PARTICIPANT_PANEL'] = ($user->participant_panel || $user->superadmin);
  218. Yii::app()->session['USER_RIGHT_CONFIGURATOR'] = ($user->configurator || $user->superadmin);
  219. Yii::app()->session['USER_RIGHT_CREATE_USER'] = ($user->create_user || $user->superadmin);
  220. Yii::app()->session['USER_RIGHT_DELETE_USER'] = ($user->delete_user || $user->superadmin);
  221. Yii::app()->session['USER_RIGHT_MANAGE_TEMPLATE'] = ($user->manage_template || $user->superadmin);
  222. Yii::app()->session['USER_RIGHT_MANAGE_LABEL'] = ($user->manage_label || $user->superadmin);
  223. }
  224. // SuperAdmins
  225. // * original superadmin with uid=1 unless manually changed and defined
  226. // in config-defaults.php
  227. // * or any user having USER_RIGHT_SUPERADMIN right
  228. // Let's check if I am the Initial SuperAdmin
  229. $user = User::model()->findByAttributes(array('parent_id' => 0));
  230. if (!is_null($user) && $user->uid == $loginID)
  231. $initialSuperadmin=true;
  232. else
  233. $initialSuperadmin=false;
  234. if ($initialSuperadmin === true)
  235. {
  236. Yii::app()->session['USER_RIGHT_SUPERADMIN'] = 1;
  237. Yii::app()->session['USER_RIGHT_INITIALSUPERADMIN'] = 1;
  238. }
  239. else
  240. Yii::app()->session['USER_RIGHT_INITIALSUPERADMIN'] = 0;
  241. }
  242. /**
  243. * Prints Admin Header
  244. *
  245. * @access protected
  246. * @param bool $meta
  247. * @param bool $return
  248. * @return mixed
  249. */
  250. public function _getAdminHeader($meta = false, $return = false)
  251. {
  252. if (empty(Yii::app()->session['adminlang']))
  253. Yii::app()->session["adminlang"] = Yii::app()->getConfig("defaultlang");
  254. $data = array();
  255. $data['adminlang'] = Yii::app()->session['adminlang'];
  256. //$data['admin'] = getLanguageRTL;
  257. $data['test'] = "t";
  258. $data['languageRTL']="";
  259. $data['styleRTL']="";
  260. Yii::app()->loadHelper("surveytranslator");
  261. if (getLanguageRTL(Yii::app()->session["adminlang"]))
  262. {
  263. $data['languageRTL'] = " dir=\"rtl\" ";
  264. $data['bIsRTL']=true;
  265. }
  266. else
  267. {
  268. $data['bIsRTL']=false;
  269. }
  270. $data['meta']="";
  271. if ($meta)
  272. {
  273. $data['meta']=$meta;
  274. }
  275. $data['baseurl'] = Yii::app()->baseUrl . '/';
  276. $data['datepickerlang']="";
  277. if (Yii::app()->session["adminlang"] != 'en')
  278. $data['datepickerlang'] = "<script type=\"text/javascript\" src=\"".Yii::app()->getConfig('generalscripts')."jquery/locale/jquery.ui.datepicker-".Yii::app()->session["adminlang"].".js\"></script>\n";
  279. $data['sitename'] = Yii::app()->getConfig("sitename");
  280. $data['admintheme'] = Yii::app()->getConfig("admintheme");
  281. $data['firebug'] = useFirebug();
  282. if (!empty(Yii::app()->session['dateformat']))
  283. $data['formatdata'] = getDateFormatData(Yii::app()->session['dateformat']);
  284. // Prepare flashmessage
  285. if (!empty(Yii::app()->session['flashmessage']) && Yii::app()->session['flashmessage'] != '')
  286. {
  287. $data['flashmessage'] = Yii::app()->session['flashmessage'];
  288. unset(Yii::app()->session['flashmessage']);
  289. }
  290. $data['css_admin_includes'] = $this->_css_admin_includes(array(), true);
  291. return $this->renderPartial("/admin/super/header", $data, $return);
  292. }
  293. /**
  294. * Prints Admin Footer
  295. *
  296. * @access protected
  297. * @param string $url
  298. * @param string $explanation
  299. * @param bool $return
  300. * @return mixed
  301. */
  302. public function _getAdminFooter($url, $explanation, $return = false)
  303. {
  304. $clang = $this->lang;
  305. $data['clang'] = $clang;
  306. $data['versionnumber'] = Yii::app()->getConfig("versionnumber");
  307. $data['buildtext'] = "";
  308. if(Yii::app()->getConfig("buildnumber")!="") {
  309. $data['buildtext']= "Build ".Yii::app()->getConfig("buildnumber");
  310. }
  311. //If user is not logged in, don't print the version number information in the footer.
  312. if (empty(Yii::app()->session['loginID']))
  313. {
  314. $data['versionnumber']="";
  315. $data['versiontitle']="";
  316. $data['buildtext']="";
  317. }
  318. else
  319. {
  320. $data['versiontitle'] = $clang->gT('Version');
  321. }
  322. $data['imageurl'] = Yii::app()->getConfig("imageurl");
  323. $data['url'] = $url;
  324. $data['js_admin_includes'] = $this->_js_admin_includes(array(), true);
  325. $data['css_admin_includes'] = $this->_css_admin_includes(array(), true);
  326. return $this->render("/admin/super/footer", $data, $return);
  327. }
  328. /**
  329. * Shows a message box
  330. *
  331. * @access public
  332. * @param string $title
  333. * @param string $message
  334. * @param string $class
  335. * @return void
  336. */
  337. public function _showMessageBox($title,$message,$class="header ui-widget-header")
  338. {
  339. $data['title'] = $title;
  340. $data['message'] = $message;
  341. $data['class'] = $class;
  342. $data['clang'] = $this->lang;
  343. $this->render('/admin/super/messagebox', $data);
  344. }
  345. /**
  346. * _showadminmenu() function returns html text for the administration button bar
  347. *
  348. * @access public
  349. * @global string $homedir
  350. * @global string $scriptname
  351. * @global string $surveyid
  352. * @global string $setfont
  353. * @global string $imageurl
  354. * @param int $surveyid
  355. * @return string $adminmenu
  356. */
  357. public function _showadminmenu($surveyid = false)
  358. {
  359. $clang = $this->lang;
  360. $data['clang']= $clang;
  361. if (Yii::app()->session['pw_notify'] && Yii::app()->getConfig("debug")<2) {
  362. Yii::app()->session['flashmessage'] = $clang->gT("Warning: You are still using the default password ('password'). Please change your password and re-login again.");
  363. }
  364. $data['showupdate'] = (Yii::app()->session['USER_RIGHT_SUPERADMIN'] == 1 && getGlobalSetting("updatelastcheck")>0 && getGlobalSetting("updateavailable")==1 && Yii::app()->getConfig("updatable") );
  365. $data['updateversion'] = getGlobalSetting("updateversion");
  366. $data['updatebuild'] = getGlobalSetting("updatebuild");
  367. $data['surveyid'] = $surveyid;
  368. $data['iconsize'] = Yii::app()->getConfig('adminthemeiconsize');
  369. $data['sImageURL'] = Yii::app()->getConfig('adminimageurl');
  370. $this->render("/admin/super/adminmenu", $data);
  371. }
  372. public function _loadEndScripts()
  373. {
  374. static $out = false;
  375. if ($out)
  376. return true;
  377. $out = true;
  378. if (empty(Yii::app()->session['metaHeader']))
  379. Yii::app()->session['metaHeader'] = '';
  380. unset(Yii::app()->session['metaHeader']);
  381. return $this->render('/admin/endScripts_view', array());
  382. }
  383. public function _css_admin_includes($includes = array(), $reset = false)
  384. {
  385. return $this->_admin_includes('css', $includes, $reset);
  386. }
  387. public function _js_admin_includes($includes = array(), $reset = false)
  388. {
  389. return $this->_admin_includes('js', $includes, $reset);
  390. }
  391. private function _admin_includes($method, $includes = array(), $reset = false)
  392. {
  393. $method = in_array($method, array('js', 'css')) ? $method : 'js';
  394. $includes = (array) $includes;
  395. $admin_includes = (array) Yii::app()->getConfig("{$method}_admin_includes");
  396. $admin_includes = array_merge($admin_includes, $includes);
  397. $admin_includes = array_filter($admin_includes);
  398. $admin_includes = array_unique($admin_includes);
  399. if ($reset == true)
  400. {
  401. Yii::app()->setConfig("{$method}_admin_includes", array());
  402. }
  403. else
  404. {
  405. Yii::app()->setConfig("{$method}_admin_includes", $admin_includes);
  406. }
  407. return $admin_includes;
  408. }
  409. }