You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

564 lines
20 KiB

  1. <?php
  2. /**
  3. * @package Widgets
  4. *
  5. * @file Admin.php
  6. * This file is part of MOVIM.
  7. *
  8. * @brief The administration widget.
  9. *
  10. * @author Timothée Jaussoin <edhelas@gmail.com>
  11. *
  12. * @version 1.0
  13. * @date 25 November 2011
  14. *
  15. * Copyright (C)2010 MOVIM project
  16. *
  17. * See COPYING for licensing information.
  18. */
  19. class Admin extends WidgetBase {
  20. private $_conf;
  21. private $_validatebutton;
  22. function WidgetLoad()
  23. {
  24. $this->_conf = Conf::getServerConf();
  25. }
  26. private function isValid($what)
  27. {
  28. if($what)
  29. return "message success";
  30. else
  31. return "message error";
  32. }
  33. private function testDir($dir){
  34. return (file_exists($dir) && is_dir($dir) && is_writable($dir));
  35. }
  36. private function testFile($file) {
  37. return (file_exists($file) && is_writable($file));
  38. }
  39. /*
  40. * Create the dirs
  41. */
  42. function createDirs(){
  43. if(!file_exists(BASE_PATH.'cache') && !@mkdir(BASE_PATH.'cache')) {
  44. echo t("Couldn't create directory '%s'.", 'cache');
  45. return false;
  46. }
  47. if(!file_exists(BASE_PATH.'log') && !@mkdir(BASE_PATH.'log')) {
  48. echo t("Couldn't create directory '%s'.", 'log');
  49. return false;
  50. }
  51. if(!file_exists(BASE_PATH.'config') && !@mkdir(BASE_PATH.'config')) {
  52. echo t("Couldn't create directory '%s'.", 'config');
  53. return false;
  54. }
  55. }
  56. private function listThemes()
  57. {
  58. $dir = opendir(BASE_PATH.'themes');
  59. $themes = array();
  60. while($theme = readdir($dir)) {
  61. if(preg_match('/^\.+$/', $theme)
  62. || !is_dir(BASE_PATH.'themes/'.$theme)) {
  63. continue;
  64. }
  65. $themes[$theme] = $theme;
  66. }
  67. return $themes;
  68. }
  69. private function listLangs()
  70. {
  71. $dir = opendir(BASE_PATH.'locales');
  72. $langs = array('en' => 'English');
  73. $languages = get_lang_list();
  74. while($lang = readdir($dir)) {
  75. if(!preg_match('/\.po$/', $lang)) {
  76. continue;
  77. }
  78. $lang = substr($lang, 0, strlen($lang) - 3);
  79. $langs[$lang] = $languages[$lang];
  80. }
  81. return $langs;
  82. }
  83. function testBosh($url) {
  84. $ch = curl_init($url);
  85. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  86. curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
  87. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  88. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  89. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  90. // Fire !
  91. $rs = array();
  92. $rs['content'] = curl_exec($ch);
  93. $rs['errno'] = curl_errno($ch);
  94. $rs['errmsg'] = curl_error($ch);
  95. $rs['header'] = curl_getinfo($ch);
  96. if($rs['content'] != false && $rs['content'] != '') {
  97. return true;
  98. }
  99. elseif($rs['errno'] != 0 || $rs['content'] == '') {
  100. return false;
  101. }
  102. curl_close($ch);
  103. }
  104. public function ajaxAdminSubmit($form)
  105. {
  106. unset($form['']);
  107. $empty = false;
  108. if($form['repass'] == '' || !isset($form['repass']))
  109. $form['pass'] = $this->_conf['pass'];
  110. elseif($form['pass'] == $form['repass']) {
  111. unset($form['repass']);
  112. $form['pass'] = sha1($form['pass']);
  113. }
  114. foreach($this->_conf as $key => $value) {
  115. if(isset($form[$key]))
  116. $this->_conf[$key] = $form[$key];
  117. }
  118. Conf::saveConfFile($this->_conf);
  119. }
  120. public function ajaxRecreateDatabase()
  121. {
  122. $pd = new \modl\PostnDAO();
  123. $pd->create();
  124. $nd = new \modl\NodeDAO();
  125. $nd->create();
  126. $cd = new \modl\ContactDAO();
  127. $cd->create();
  128. $cad = new \modl\CapsDAO();
  129. $cad->create();
  130. $prd = new \modl\PresenceDAO();
  131. $prd->create();
  132. $rd = new \modl\RosterLinkDAO();
  133. $rd->create();
  134. $sd = new \modl\SessionDAO();
  135. $sd->create();
  136. $cd = new \modl\CacheDAO();
  137. $cd->create();
  138. $md = new \modl\MessageDAO();
  139. $md->create();
  140. $cd = new \modl\SubscriptionDAO();
  141. $cd->create();
  142. $pr = new \modl\PrivacyDAO();
  143. $pr->create();
  144. }
  145. private function prepareAdminComp()
  146. {
  147. if($this->testDir(BASE_PATH))
  148. $this->createDirs();
  149. $submit = $this->genCallAjax('ajaxAdminSubmit', "movim_parse_form('admin')")
  150. ."this.className='button color orange icon loading'; setTimeout(function() {location.reload(false)}, 2000);";
  151. $this->_validatebutton = '
  152. <div class="clear"></div>
  153. <a class="button icon yes color green" style="float: right;" onclick="'.$submit.'">'.t('Submit').'</a>';
  154. $html = '
  155. <form name="admin" id="adminform">';
  156. $html .= '
  157. <fieldset>
  158. <legend>'.t("Compatibility Check").'</legend>
  159. <div class="clear"></div>';
  160. $html .=
  161. '<p>'.
  162. t('Movim requires certain external components. Please install them before you can succeed:').
  163. '</p><br />';
  164. $html .= '
  165. <div class="'.$this->isValid((version_compare(PHP_VERSION, '5.3.0') >= 0)).'">
  166. '.t('Your PHP-Version: %s <br>Required: 5.3.0', PHP_VERSION).'
  167. </div>
  168. <div class="'.$this->isValid(extension_loaded('curl')).'">
  169. '.t('CURL-Library').'
  170. </div>
  171. <div class="'.$this->isValid(extension_loaded('gd')).'">
  172. '.t('GD').'
  173. </div>
  174. <div class="'.$this->isValid(extension_loaded('SimpleXml')).'">
  175. '.t('SimpleXML').'
  176. </div>
  177. <div class="'.$this->isValid($this->testDir(BASE_PATH)).'">
  178. '.t('Read and write rights for the webserver in Movim\'s root directory').'
  179. </div>
  180. </fieldset>
  181. <fieldset>
  182. <legend>'.t('URL Rewriting support').'</legend>
  183. <div class="clear"></div>
  184. <div class="'.$this->isValid($_SERVER['HTTP_MOD_REWRITE']).'">
  185. '.t('URL Rewriting support').'
  186. </div>';
  187. $html .= '
  188. </fieldset>';
  189. return $html;
  190. }
  191. function prepareAdminGen() {
  192. $html .= '
  193. <fieldset>
  194. <legend>'.t('General Settings').'</legend>
  195. <div class="element">
  196. <label for="movim" >'.t('Theme').'</label>
  197. <div class="select">
  198. <select id="theme" name="theme">';
  199. foreach($this->listThemes() as $key => $value) {
  200. if((string)$this->_conf['theme'] == $key)
  201. $sel = 'selected="selected"';
  202. else
  203. $sel = '';
  204. $html .= '
  205. <option value="'.$key.'" '.$sel.'>'.$value.'</option>';
  206. }
  207. $html .= ' </select>
  208. </div>
  209. </div>';
  210. $html .= '
  211. <div class="element">
  212. <label for="da">'.t('Default language').'</label>
  213. <div class="select">
  214. <select id="defLang" name="defLang">';
  215. foreach($this->listLangs() as $key => $value) {
  216. if((string)$this->_conf['defLang'] == $key)
  217. $sel = 'selected="selected"';
  218. else
  219. $sel = '';
  220. $html .= '
  221. <option value="'.$key.'" '.$sel.'>'.$value.'</option>';
  222. }
  223. $html .= ' </select>
  224. </div>
  225. </div>';
  226. /*
  227. $html .= '
  228. <div class="element">
  229. <label for="maxUsers">'.t('Maximum population').'</label>
  230. <input type="text" name="maxUsers" id="maxUsers" value="'.$this->_conf['maxUsers'].'" />
  231. </div>';
  232. */
  233. $html .= '
  234. <div class="element">
  235. <label for="sizeLimit">'.t('User folder size limit (in bytes)').'</label>
  236. <input type="text" name="sizeLimit" id="sizeLimit" value="'.$this->_conf['sizeLimit'].'" />
  237. </div>';
  238. $logopts = array(
  239. 0 => t('empty'),
  240. 2 => t('terse'),
  241. 4 => t('normal'),
  242. 6 => t('talkative'),
  243. 7 => t('ultimate'),
  244. );
  245. $default_log = 4;
  246. $html .= '
  247. <div class="element">
  248. <label for="logLevel">'.t("Log verbosity").'</label>
  249. <div class="select">
  250. <select id="logLevel" name="logLevel">';
  251. foreach($logopts as $lognum => $text) {
  252. if($this->_conf['logLevel'] == $lognum)
  253. $sel = 'selected="selected"';
  254. else
  255. $sel = '';
  256. $html .= '
  257. <option value="'.$lognum.'" '.$sel.'>'.
  258. $text.'
  259. </option>';
  260. }
  261. $html .= ' </select>
  262. </div>
  263. </div>';
  264. $timezones = getTimezoneList();
  265. $html .= '
  266. <div class="element">
  267. <label for="timezone">'.t("Server Timezone").'</label>
  268. <div class="select">
  269. <select id="timezone" name="timezone">';
  270. foreach($timezones as $key => $value) {
  271. if($this->_conf['timezone'] == $key)
  272. $sel = 'selected="selected"';
  273. else
  274. $sel = '';
  275. $html .= '
  276. <option value="'.$key.'" '.$sel.'>'.
  277. $key.' ('.number_format($value, 2).')
  278. </option>';
  279. }
  280. $html .= ' </select>
  281. </div>
  282. <br /><br />'.date('l jS \of F Y h:i:s A').'
  283. </div>';
  284. $html .= $this->_validatebutton;
  285. $html .= '
  286. </fieldset>';
  287. $html .= '
  288. <fieldset>
  289. <legend>'.t("Bosh Configuration").'</legend>
  290. <div class="clear"></div>';
  291. $html .= '<p>'.
  292. t("Enter here the BOSH-URL in the form: http(s)://domain:port/path.").' '.
  293. t('If you enter an open BOSH-Server, you can connect to many XMPP-Servers.').' '.
  294. t('If it is closed, you have to specify the corresponding Server on the next page.').' '.
  295. t('If you are unsure about this config option visit the wiki');
  296. '</p>';
  297. if(!$this->testBosh($this->_conf['boshUrl'])) {
  298. $html .= '
  299. <div class="message error">'.
  300. t('Your Bosh URL is not reachable').'
  301. </div>';
  302. }
  303. $html .= '
  304. <div class="element">
  305. <label for="boshUrl">'.t("Bosh URL").'</label>
  306. <input type="text" id="boshUrl" name="boshUrl" value="'.$this->_conf['boshUrl'].'"/>
  307. </div>';
  308. $html .= $this->_validatebutton;
  309. $html .= '
  310. </fieldset>';
  311. $html .= '
  312. <fieldset>
  313. <legend>'.t("Whitelist - XMPP Server").'</legend>
  314. <div class="clear"></div>';
  315. $html .= '<p>'.
  316. t("If you want to specify a list of authorized XMPP servers on your Movim pod and forbid the connection on all the others please put their domain name here, with comma (ex: movim.eu,jabber.fr)").
  317. '</p>'.
  318. '<p>'.
  319. t("Leave this field blank if you allow the access to all the XMPP accounts.").
  320. '</p>';
  321. $html .= '
  322. <div class="element large">
  323. <label for="xmppWhiteList">'.t("List of whitelisted XMPP servers").'</label>
  324. <input type="text" name="xmppWhiteList" id="xmppWhiteList" value="'.$this->_conf['xmppWhiteList'].'" />
  325. </div>';
  326. $html .= $this->_validatebutton;
  327. $html .= '
  328. </fieldset>';
  329. $html .= '
  330. <fieldset>
  331. <legend>'.t("Information Message").'</legend>
  332. <div class="clear"></div>';
  333. $html .= '<p>'.
  334. t("This message will be displayed on the login page").
  335. '</p>'.
  336. '<p>'.
  337. t("Leave this field blank if you don't want to show any message.").
  338. '</p>';
  339. $html .= '
  340. <div class="element large">
  341. <label for="info">'.t("Information Message").'</label>
  342. <textarea type="text" name="info" id="info" />'.$this->_conf['info'].'</textarea>
  343. </div>';
  344. $html .= $this->_validatebutton;
  345. $html .= '
  346. </fieldset>';
  347. $html .= '
  348. <fieldset>
  349. <legend>'.t("Administration Credential").'</legend>';
  350. if($this->_conf['user'] == 'admin' && $this->_conf['pass'] == sha1('password')) {
  351. $html .= '
  352. <div class="message error">'.
  353. t('Change the default username/password').'
  354. </div>';
  355. }
  356. $html .= '
  357. <div class="element" >
  358. <label for="username">'.t("Username").'</label>
  359. <input type="text" id="user" name="user" value="'.$this->_conf['user'].'"/>
  360. </div>
  361. <div class="clear"></div>
  362. <div class="element">
  363. <label for="pass">'.t("Password").'</label>
  364. <input type="password" id="pass" name="pass" value=""/>
  365. </div>
  366. <div class="element">
  367. <label for="repass">'.t("Retype password").'</label>
  368. <input type="password" id="repass" name="repass" value=""/>
  369. </div> ';
  370. $html .= $this->_validatebutton;
  371. $html .= '
  372. </fieldset><br />';
  373. return $html;
  374. }
  375. function prepareAdminDB() {
  376. $html .= '
  377. <fieldset>
  378. <legend>'.t("Database Settings").'</legend>
  379. <div class="clear"></div>';
  380. $md = new \modl\ModlDAO();
  381. if(isset($md->_dao->_error)) {
  382. $html .= '
  383. <div class="message error">'.
  384. t("Modl wasn't able to connect to the database").'<br />
  385. '.$md->_dao->_error.'
  386. </div>
  387. ';
  388. } else {
  389. $dbrecreate = $this->genCallAjax('ajaxRecreateDatabase');
  390. $html .= '
  391. <div class="element">
  392. <label for="db">'.t('Recreate the database').'</label>
  393. <a class="button icon loading color red" onclick="'.$dbrecreate.'">'.t('Recreate').'</a>
  394. </div>
  395. <div class="message warning">
  396. '.t('This button will clear and recreate the Movim database.').'
  397. </div>
  398. ';
  399. }
  400. /*$html .= '
  401. <div class="element large">
  402. <label for="db">'.t('Dabase String').'</label>
  403. <input type="text" name="db" id="db" value="'.$this->_conf['db'].'" />
  404. </div>';
  405. */
  406. $html .= '
  407. <div class="element">
  408. <label for="dbType">'.t('Database Type').'</label>
  409. <input type="text" disabled="true" name="dbType" id="dbType" value="'.$this->_conf['dbType'].'" />
  410. </div>';
  411. $html .= '
  412. <div class="element">
  413. <label for="dbUsername">'.t('Username').'</label>
  414. <input type="text" name="dbUsername" id="dbUsername" value="'.$this->_conf['dbUsername'].'" />
  415. </div>';
  416. $html .= '
  417. <div class="element">
  418. <label for="dbPassword">'.t('Password').'</label>
  419. <input type="password" name="dbPassword" id="dbPassword" value="'.$this->_conf['dbPassword'].'" />
  420. </div>';
  421. $html .= '
  422. <div class="element">
  423. <label for="dbHost">'.t('Host').'</label>
  424. <input type="text" name="dbHost" id="dbHost" value="'.$this->_conf['dbHost'].'" />
  425. </div>';
  426. $html .= '
  427. <div class="element">
  428. <label for="dbPort">'.t('Port').'</label>
  429. <input type="text" name="dbPort" id="dbPort" value="'.$this->_conf['dbPort'].'" />
  430. </div>';
  431. $html .= '
  432. <div class="element">
  433. <label for="dbName">'.t('Database Name').'</label>
  434. <input type="text" name="dbName" id="dbName" value="'.$this->_conf['dbName'].'" />
  435. </div>';
  436. $html .= $this->_validatebutton;
  437. $html .= '
  438. </fieldset>';
  439. $html .= '
  440. </form>';
  441. return $html;
  442. }
  443. function build()
  444. {
  445. ?>
  446. <div id="admincomp" class="tabelem" title="<?php echo t("Compatibility Check"); ?>" style="margin: 1.5em;">
  447. <?php echo $this->prepareAdminComp(); ?>
  448. </div>
  449. <div id="admingen" class="tabelem" title="<?php echo t('General Settings'); ?>" style="margin: 1.5em;">
  450. <?php echo $this->prepareAdminGen(); ?>
  451. </div>
  452. <div id="admindb" class="tabelem" title="<?php echo t("Database Settings") ?>" style="margin: 1.5em;">
  453. <?php echo $this->prepareAdminDB(); ?>
  454. </div>
  455. <?php
  456. }
  457. }