PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/settings.php

https://code.google.com/p/mytinytodo/
PHP | 260 lines | 210 code | 38 blank | 12 comment | 32 complexity | 4eba56f54e0690ce708ffd647f7d7151 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /*
  3. This file is part of myTinyTodo.
  4. (C) Copyright 2009-2011 Max Pozdeev <maxpozdeev@gmail.com>
  5. Licensed under the GNU GPL v3 license. See file COPYRIGHT for details.
  6. */
  7. require_once('./init.php');
  8. $lang = Lang::instance();
  9. if($needAuth && !is_logged())
  10. {
  11. die("Access denied!<br/> Disable password protection or Log in.");
  12. }
  13. if(isset($_POST['save']))
  14. {
  15. stop_gpc($_POST);
  16. $t = array();
  17. $langs = getLangs();
  18. Config::$params['lang']['options'] = array_keys($langs);
  19. Config::set('lang', _post('lang'));
  20. // in Demo mode we can set only language by cookies
  21. if(defined('MTTDEMO')) {
  22. setcookie('lang', Config::get('lang'), 0, url_dir(Config::get('url')=='' ? $_SERVER['REQUEST_URI'] : Config::get('url')));
  23. $t['saved'] = 1;
  24. jsonExit($t);
  25. }
  26. if(isset($_POST['password']) && $_POST['password'] != '') Config::set('password', $_POST['password']);
  27. elseif(!_post('allowpassword')) Config::set('password', '');
  28. Config::set('smartsyntax', (int)_post('smartsyntax'));
  29. // Do not set invalid timezone
  30. try {
  31. $tz = trim(_post('timezone'));
  32. $testTZ = new DateTimeZone($tz); //will throw Exception on invalid timezone
  33. Config::set('timezone', $tz);
  34. }
  35. catch (Exception $e) {
  36. }
  37. Config::set('autotag', (int)_post('autotag'));
  38. Config::set('session', _post('session'));
  39. Config::set('firstdayofweek', (int)_post('firstdayofweek'));
  40. Config::set('clock', (int)_post('clock'));
  41. Config::set('dateformat', _post('dateformat'));
  42. Config::set('dateformat2', _post('dateformat2'));
  43. Config::set('dateformatshort', _post('dateformatshort'));
  44. Config::set('title', trim(_post('title')));
  45. Config::set('showdate', (int)_post('showdate'));
  46. Config::save();
  47. $t['saved'] = 1;
  48. jsonExit($t);
  49. }
  50. function _c($key)
  51. {
  52. return Config::get($key);
  53. }
  54. function getLangs($withContents = 0)
  55. {
  56. if (!$h = opendir(MTTPATH. 'lang')) return false;
  57. $a = array();
  58. while(false !== ($file = readdir($h)))
  59. {
  60. if(preg_match('/(.+)\.php$/', $file, $m) && $file != 'class.default.php') {
  61. $a[$m[1]] = $m[1];
  62. if($withContents) {
  63. $a[$m[1]] = getLangDetails(MTTPATH. 'lang'. DIRECTORY_SEPARATOR. $file, $m[1]);
  64. $a[$m[1]]['name'] = $a[$m[1]]['original_name'];
  65. $a[$m[1]]['title'] = $a[$m[1]]['language'];
  66. }
  67. }
  68. }
  69. closedir($h);
  70. return $a;
  71. }
  72. function getLangDetails($filename, $default)
  73. {
  74. $contents = file_get_contents($filename);
  75. $a = array('language'=>$default, 'original_name'=>$default);
  76. if(preg_match("|/\\*\s*myTinyTodo language pack([\s\S]+?)\\*/|", $contents, $m))
  77. {
  78. $str = $m[1];
  79. if(preg_match("|Language\s*:\s*(.+)|i", $str, $m)) {
  80. $a['language'] = trim($m[1]);
  81. }
  82. if(preg_match("|Original name\s*:\s*(.+)|i", $str, $m)) {
  83. $a['original_name'] = trim($m[1]);
  84. }
  85. }
  86. return $a;
  87. }
  88. function selectOptions($a, $value, $default=null)
  89. {
  90. if(!$a) return '';
  91. $s = '';
  92. if($default !== null && !isset($a[$value])) $value = $default;
  93. foreach($a as $k=>$v) {
  94. $s .= '<option value="'.htmlspecialchars($k).'" '.($k===$value?'selected="selected"':'').'>'.htmlspecialchars($v).'</option>';
  95. }
  96. return $s;
  97. }
  98. /*
  99. @param array $a array of id=>array(name, optional title)
  100. @param mixed $key Key of OPTION to be selected
  101. @param mixed $default Default key if $key is not present in $a
  102. */
  103. function selectOptionsA($a, $key, $default=null)
  104. {
  105. if(!$a) return '';
  106. $s = '';
  107. if($default !== null && !isset($a[$key])) $key = $default;
  108. foreach($a as $k=>$v) {
  109. $s .= '<option value="'.htmlspecialchars($k).'" '.($k===$key?'selected="selected"':'').
  110. (isset($v['title']) ? ' title="'.htmlspecialchars($v['title']).'"' : '').
  111. '>'.htmlspecialchars($v['name']).'</option>';
  112. }
  113. return $s;
  114. }
  115. function timezoneIdentifiers()
  116. {
  117. $zones = DateTimeZone::listIdentifiers();
  118. $a = array();
  119. foreach($zones as $v) {
  120. $a[$v] = $v;
  121. }
  122. return $a;
  123. }
  124. header('Content-type:text/html; charset=utf-8');
  125. ?>
  126. <div><a href="#" class="mtt-back-button"><?php _e('go_back');?></a></div>
  127. <h3><?php _e('set_header');?></h3>
  128. <div id="settings_msg" style="display:none"></div>
  129. <form id="settings_form" method="post" action="settings.php">
  130. <table class="mtt-settings-table">
  131. <tr>
  132. <th><?php _e('set_title');?>:<br/><span class="descr"><?php _e('set_title_descr');?></span></th>
  133. <td> <input name="title" value="<?php echo htmlspecialchars(_c('title'));?>" class="in350" /> </td>
  134. </tr>
  135. <tr>
  136. <th><?php _e('set_language');?>:</th>
  137. <td> <select name="lang"><?php echo selectOptionsA(getLangs(1), _c('lang')); ?></select> </td>
  138. </tr>
  139. <tr>
  140. <th><?php _e('set_protection');?>:</th>
  141. <td>
  142. <label><input type="radio" name="allowpassword" value="1" <?php if(_c('password')!='') echo 'checked="checked"'; ?> onclick='$(this.form).find("input[name=password]").attr("disabled",false)' /><?php _e('set_enabled');?></label> <br/>
  143. <label><input type="radio" name="allowpassword" value="0" <?php if(_c('password')=='') echo 'checked="checked"'; ?> onclick='$(this.form).find("input[name=password]").attr("disabled","disabled")' /><?php _e('set_disabled');?></label> <br/>
  144. </td></tr>
  145. <tr>
  146. <th><?php _e('set_newpass');?>:<br/><span class="descr"><?php _e('set_newpass_descr');?></span></th>
  147. <td> <input type="password" name="password" <?php if(_c('password')=='') echo "disabled"; ?> /> </td>
  148. </tr>
  149. <tr>
  150. <th><?php _e('set_smartsyntax');?>:<br/><span class="descr"><?php _e('set_smartsyntax_descr');?></span></th>
  151. <td>
  152. <label><input type="radio" name="smartsyntax" value="1" <?php if(_c('smartsyntax')) echo 'checked="checked"'; ?> /><?php _e('set_enabled');?></label> <br/>
  153. <label><input type="radio" name="smartsyntax" value="0" <?php if(!_c('smartsyntax')) echo 'checked="checked"'; ?> /><?php _e('set_disabled');?></label>
  154. </td></tr>
  155. <tr>
  156. <th><?php _e('set_autotag');?>:<br/><span class="descr"><?php _e('set_autotag_descr');?></span></th>
  157. <td>
  158. <label><input type="radio" name="autotag" value="1" <?php if(_c('autotag')) echo 'checked="checked"'; ?> /><?php _e('set_enabled');?></label> <br/>
  159. <label><input type="radio" name="autotag" value="0" <?php if(!_c('autotag')) echo 'checked="checked"'; ?> /><?php _e('set_disabled');?></label>
  160. </td></tr>
  161. <tr>
  162. <th><?php _e('set_sessions');?>:</th>
  163. <td>
  164. <label><input type="radio" name="session" value="default" <?php if(_c('session')=='default') echo 'checked="checked"'; ?> /><?php _e('set_sessions_php');?></label> <br/>
  165. <label><input type="radio" name="session" value="files" <?php if(_c('session')=='files') echo 'checked="checked"'; ?> /><?php _e('set_sessions_files');?></label> <span class="descr">(&lt;mytinytodo_dir&gt;/tmp/sessions)</span>
  166. </td></tr>
  167. <tr>
  168. <th><?php _e('set_timezone');?>:</th>
  169. <td>
  170. <select name="timezone"><?php echo selectOptions(timezoneIdentifiers(), _c('timezone')); ?></select>
  171. </td></tr>
  172. <tr>
  173. <th><?php _e('set_firstdayofweek');?>:</th>
  174. <td>
  175. <select name="firstdayofweek"><?php echo selectOptions(__('days_long'), _c('firstdayofweek')); ?></select>
  176. </td></tr>
  177. <tr>
  178. <th><?php _e('set_date');?>:</th>
  179. <td>
  180. <input name="dateformat" value="<?php echo htmlspecialchars(_c('dateformat'));?>" />
  181. <select onchange="if(this.value!=0) this.form.dateformat.value=this.value;">
  182. <?php echo selectOptions(array('F j, Y'=>formatTime('F j, Y'), 'M d, Y'=>formatTime('M d, Y'), 'j M Y'=>formatTime('j M Y'), 'd F Y'=>formatTime('d F Y'),
  183. 'n/j/Y'=>formatTime('n/j/Y'), 'd.m.Y'=>formatTime('d.m.Y'), 'j. F Y'=>formatTime('j. F Y'), 0=>__('set_custom')), _c('dateformat'), 0); ?>
  184. </select>
  185. </td></tr>
  186. <tr>
  187. <th><?php _e('set_date2');?>:</th>
  188. <td>
  189. <input name="dateformat2" value="<?php echo htmlspecialchars(_c('dateformat2'));?>" />
  190. <select onchange="if(this.value!=0) this.form.dateformat2.value=this.value;">
  191. <?php echo selectOptions(array('Y-m-d'=>'yyyy-mm-dd ('.date('Y-m-d').')',
  192. 'n/j/y'=>'m/d/yy ('.date('n/j/y').')',
  193. 'd.m.y'=>'dd.mm.yy ('.date('d.m.y').')',
  194. 'd/m/y'=>'dd/mm/yy ('.date('d/m/y').')', 0=>__('set_custom')), _c('dateformat2'), 0); ?>
  195. </select>
  196. </td></tr>
  197. <tr>
  198. <th><?php _e('set_shortdate');?>:</th>
  199. <td>
  200. <input name="dateformatshort" value="<?php echo htmlspecialchars(_c('dateformatshort'));?>" />
  201. <select onchange="if(this.value!=0) this.form.dateformatshort.value=this.value;">
  202. <?php echo selectOptions(array('M d'=>formatTime('M d'), 'j M'=>formatTime('j M'), 'n/j'=>formatTime('n/j'), 'd.m'=>formatTime('d.m'), 0=>__('set_custom')), _c('dateformatshort'), 0); ?>
  203. </select>
  204. </td></tr>
  205. <tr>
  206. <th><?php _e('set_clock');?>:</th>
  207. <td>
  208. <select name="clock"><?php echo selectOptions(array(12=>__('set_12hour').' ('.date('g:i A').')', 24=>__('set_24hour').' ('.date('H:i').')'), _c('clock')); ?></select>
  209. </td></tr>
  210. <tr>
  211. <th><?php _e('set_showdate');?>:</th>
  212. <td>
  213. <label><input type="radio" name="showdate" value="1" <?php if(_c('showdate')) echo 'checked="checked"'; ?> /><?php _e('set_enabled');?></label> <br/>
  214. <label><input type="radio" name="showdate" value="0" <?php if(!_c('showdate')) echo 'checked="checked"'; ?> /><?php _e('set_disabled');?></label>
  215. </td>
  216. </tr>
  217. <tr><td colspan="2" class="form-buttons">
  218. <input type="submit" value="<?php _e('set_submit');?>" />
  219. <input type="button" class="mtt-back-button" value="<?php _e('set_cancel');?>" />
  220. </td></tr>
  221. </table>
  222. </form>