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

/user_view.php

http://candydolldb.googlecode.com/
PHP | 340 lines | 287 code | 50 blank | 3 comment | 75 complexity | aca7397f9b761797a4240aa6849a7cf8 MD5 | raw file
  1. <?php
  2. include('cd.php');
  3. $CurrentUser = Authentication::Authenticate();
  4. HTMLstuff::RefererRegister($_SERVER['REQUEST_URI']);
  5. $UserID = Utils::SafeIntFromQS('user_id');
  6. $DeleteUser = (array_key_exists('cmd', $_GET) && $_GET['cmd'] && ($_GET['cmd'] == COMMAND_DELETE));
  7. $_SESSION['UserSalt'] = NULL;
  8. $PasswordError = FALSE;
  9. $LanguageOptions = NULL;
  10. $DateFormatOptions = NULL;
  11. $RightsCheckboxes = NULL;
  12. $DisableControls =
  13. $DeleteUser ||
  14. ($UserID == $CurrentUser->getID() && !$CurrentUser->hasPermission(RIGHT_ACCOUNT_EDIT)) ||
  15. ($UserID != $CurrentUser->getID() && !$CurrentUser->hasPermission(RIGHT_USER_EDIT) && !is_null($UserID)) ||
  16. ($UserID != $CurrentUser->getID() && !$CurrentUser->hasPermission(RIGHT_USER_ADD) && is_null($UserID));
  17. $DisableDefaultButton =
  18. ($UserID == $CurrentUser->getID() && !$CurrentUser->hasPermission(RIGHT_ACCOUNT_EDIT)) ||
  19. ($UserID != $CurrentUser->getID() && !$CurrentUser->hasPermission(RIGHT_USER_DELETE) && !is_null($UserID) && $DeleteUser) ||
  20. ($UserID != $CurrentUser->getID() && !$CurrentUser->hasPermission(RIGHT_USER_EDIT) && !is_null($UserID) && !$DeleteUser) ||
  21. ($UserID != $CurrentUser->getID() && !$CurrentUser->hasPermission(RIGHT_USER_ADD) && is_null($UserID));
  22. $DisableRights =
  23. $DeleteUser ||
  24. (!$CurrentUser->hasPermission(RIGHT_USER_RIGHTS) && !is_null($UserID));
  25. /* @var $User User */
  26. if($UserID)
  27. {
  28. $Users = User::GetUsers(new UserSearchParameters($UserID));
  29. if($Users)
  30. { $User = $Users[0]; }
  31. else
  32. {
  33. header('location:index.php');
  34. exit;
  35. }
  36. $_SESSION['UserSalt'] = $User->getSalt();
  37. }
  38. else
  39. {
  40. $User = new User(NULL, $lang->g('LabelNewUser'));
  41. }
  42. if(array_key_exists('hidAction', $_POST) && $_POST['hidAction'] == 'UserView')
  43. {
  44. if(array_key_exists('txtUserName', $_POST))
  45. {
  46. $User->setUserName(Utils::NullIfEmpty($_POST['txtUserName']));
  47. }
  48. if(array_key_exists('hidPassword', $_POST))
  49. {
  50. $User->setPassword(Utils::NullIfEmpty($_POST['hidPassword']));
  51. $User->setSalt(Utils::NullIfEmpty($_SESSION['UserSalt']));
  52. }
  53. $User->setFirstName(Utils::NullIfEmpty($_POST['txtFirstName']));
  54. $User->setInsertion(Utils::NullIfEmpty($_POST['txtInsertion']));
  55. $User->setLastName(Utils::NullIfEmpty($_POST['txtLastName']));
  56. $User->setEmailAddress(Utils::NullIfEmpty($_POST['txtEmailAddress']));
  57. $User->setLanguage(Utils::NullIfEmpty($_POST['selectLanguage']));
  58. $User->setDateDisplayOptions($_POST['selectDateformat']);
  59. $User->setImageview(Utils::NullIfEmpty($_POST['selectImageview']));
  60. if($CurrentUser->hasPermission(RIGHT_USER_RIGHTS))
  61. {
  62. $getrights = array();
  63. foreach(Rights::getDefinedRights() as $k => $v)
  64. {
  65. if(array_key_exists('chk'.$k, $_POST))
  66. { $getrights[] = $v; }
  67. }
  68. $User->setRights($getrights);
  69. }
  70. if(array_key_exists('radGender', $_POST))
  71. {
  72. switch (intval($_POST['radGender']))
  73. {
  74. case GENDER_FEMALE:
  75. $User->setGender(GENDER_FEMALE);
  76. break;
  77. case GENDER_MALE:
  78. $User->setGender(GENDER_MALE);
  79. break;
  80. default:
  81. case GENDER_UNKNOWN:
  82. $User->setGender(GENDER_UNKNOWN);
  83. break;
  84. }
  85. }
  86. else
  87. { $User->setGender(GENDER_UNKNOWN); }
  88. if(array_key_exists('txtPassword', $_POST) && $_POST['txtPassword'])
  89. {
  90. if($_POST['txtRepeatPassword'] && $_POST['txtRepeatPassword'] == $_POST['txtPassword'])
  91. {
  92. $NewSalt = Utils::GenerateGarbage(20);
  93. $_SESSION['UserSalt'] = $NewSalt;
  94. $User->setSalt($NewSalt);
  95. $User->setPassword(Utils::HashString($_POST['txtPassword'], $NewSalt));
  96. }
  97. else
  98. { $PasswordError = TRUE; }
  99. }
  100. if($_POST['txtBirthDate'] && $_POST['txtBirthDate'] != 'YYYY-MM-DD' && strtotime($_POST['txtBirthDate']) !== FALSE)
  101. { $User->setBirthDate(strtotime($_POST['txtBirthDate'])); }
  102. else
  103. { $User->setBirthDate(-1); }
  104. if(!$PasswordError || $DeleteUser)
  105. {
  106. if(Utils::ValidateEmail($User->getEmailAddress()) || $DeleteUser)
  107. {
  108. if($User->getID())
  109. {
  110. if($DeleteUser)
  111. {
  112. if(User::Delete($User, $CurrentUser))
  113. {
  114. session_regenerate_id(TRUE);
  115. header('location:user.php');
  116. exit;
  117. }
  118. }
  119. else
  120. {
  121. if(User::Update($User, $CurrentUser))
  122. {
  123. if($User->getID() == $CurrentUser->getID())
  124. { $_SESSION['CurrentUser'] = serialize($User); }
  125. session_regenerate_id(TRUE);
  126. header('location:user.php');
  127. exit;
  128. }
  129. }
  130. }
  131. else
  132. {
  133. if(User::Insert($User, $CurrentUser))
  134. {
  135. session_regenerate_id(TRUE);
  136. header('location:user.php');
  137. exit;
  138. }
  139. }
  140. }
  141. else
  142. {
  143. $e = new SyntaxError(SYNTAX_ERR_EMAILADDRESS);
  144. Error::AddError($e);
  145. }
  146. }
  147. else
  148. {
  149. $e = new LoginError(LOGIN_ERR_PASSWORDSNOTIDENTICAL);
  150. Error::AddError($e);
  151. }
  152. }
  153. foreach (i18n::$SupportedLanguages as $l){
  154. $LanguageOptions .= sprintf("
  155. <option value=\"%1\$s\"%2\$s>%3\$s%4\$s</option>",
  156. $l,
  157. HTMLstuff::SelectedStr($User->getLanguage() == $l),
  158. $lang->g('LabelLanguage_'.$l),
  159. $l == 'en' ? $lang->g('LabelSuffixDefault') : NULL
  160. );
  161. }
  162. foreach($DateStyleArray as $index => $format)
  163. {
  164. $DateFormatOptions .= sprintf("
  165. <option value=\"%1\$d\"%2\$s>%3\$s%4\$s</option>",
  166. $index,
  167. HTMLstuff::SelectedStr($User->getDateDisplayOptions() == $index),
  168. date($format),
  169. $index == 0 ? $lang->g('LabelSuffixDefault') : NULL
  170. );
  171. }
  172. foreach(Rights::getDefinedRights() as $k => $v)
  173. {
  174. $RightsCheckboxes .= sprintf("<li>
  175. <label for=\"chk%1\$s\" class=\"Radio\">
  176. <input type=\"checkbox\" id=\"chk%1\$s\" name=\"chk%1\$s\"%3\$s%4\$s />
  177. &nbsp;%2\$s
  178. </label></li>",
  179. $k,
  180. $lang->g('Label'.$k),
  181. HTMLstuff::CheckedStr($User->hasPermission($v)),
  182. HTMLstuff::DisabledStr($DisableRights)
  183. );
  184. }
  185. echo HTMLstuff::HtmlHeader($User->GetFullName(), $CurrentUser);
  186. ?>
  187. <script type="text/javascript">
  188. //<![CDATA[
  189. function ToggleBoxes(){
  190. $('input[id^=chkRIGHT_]').each(function(i, a){
  191. $(a).attr('checked', !$(a).attr('checked'));
  192. });
  193. return true;
  194. }
  195. //]]>
  196. </script>
  197. <h2><?php echo sprintf('<a href="index.php">%3$s</a> - <a href="user.php">%2$s</a> - %1$s',
  198. htmlentities($User->getUserName()),
  199. $lang->g('NavigationUsers'),
  200. $lang->g('NavigationHome')
  201. )?></h2>
  202. <form action="<?php echo htmlentities($_SERVER['REQUEST_URI'])?>" method="post">
  203. <fieldset>
  204. <input type="hidden" id="hidAction" name="hidAction" value="UserView" />
  205. <input type="hidden" id="hidPassword" name="hidPassword" value="<?php echo $User->getPassword()?>" />
  206. <?php if(
  207. ($CurrentUser->hasPermission(RIGHT_ACCOUNT_EDIT) && $User->getID() == $CurrentUser->getID())
  208. || $CurrentUser->hasPermission(RIGHT_USER_EDIT)
  209. || is_null($User->getID())){ ?>
  210. <div class="FormRow">
  211. <label for="txtUserName"><?php echo $lang->g('LabelUsername')?>: <em>*</em></label>
  212. <input type="text" id="txtUserName" name="txtUserName" maxlength="50" value="<?php echo $User->getUserName()?>"<?php echo HTMLstuff::DisabledStr($DisableControls)?> />
  213. </div>
  214. <?php if(
  215. ($CurrentUser->hasPermission(RIGHT_ACCOUNT_EDIT) && $CurrentUser->hasPermission(RIGHT_ACCOUNT_PASSWORD) && $User->getID() == $CurrentUser->getID())
  216. || $CurrentUser->hasPermission(RIGHT_USER_EDIT)
  217. || is_null($User->getID())){ ?>
  218. <div class="FormRow">
  219. <label for="txtPassword"><?php echo $lang->g('LabelPassword')?>:<?php echo $UserID ? '' : ' <em>*</em>'?></label>
  220. <input type="password" id="txtPassword" name="txtPassword" maxlength="100" value=""<?php echo HTMLstuff::DisabledStr($DisableControls)?> />
  221. <input type="button" id="btnGenerate" name="btnGenerate" value="<?php echo $lang->g('ButtonGenerate')?>"<?php echo HTMLstuff::DisabledStr($DisableControls)?> onclick="$.get('ajax_genpass.php', function(data){$('#txtGenerated, #txtPassword, #txtRepeatPassword').val(data);});" />
  222. <input type="text" id="txtGenerated" name="txtGenerated" class="Small" readonly="readonly" maxlength="10" value=""<?php echo HTMLstuff::DisabledStr($DisableControls)?> />
  223. </div>
  224. <div class="FormRow">
  225. <label for="txtRepeatPassword"><?php echo $lang->g('LabelRepeatPassword')?>:<?php echo $UserID ? '' : ' <em>*</em>'?></label>
  226. <input type="password" id="txtRepeatPassword" name="txtRepeatPassword" maxlength="100" value=""<?php echo HTMLstuff::DisabledStr($DisableControls)?> />
  227. </div>
  228. <?php } ?>
  229. <?php } ?>
  230. <div class="FormRow">
  231. <label for="selectLanguage"><?php echo $lang->g('LabelLanguage')?>:</label>
  232. <select id="selectLanguage" name="selectLanguage"<?php echo HTMLstuff::DisabledStr($DisableControls)?>><?php echo $LanguageOptions ?></select>
  233. </div>
  234. <div class="FormRow">
  235. <label for="selectDateformat"><?php echo $lang->g('LabelSelectDateFormat')?>:</label>
  236. <select id="selectDateformat" name="selectDateformat"<?php echo HTMLstuff::DisabledStr($DisableControls)?>><?php echo $DateFormatOptions ?></select>
  237. </div>
  238. <div class="FormRow">
  239. <label for="selectImageview"><?php echo $lang->g('LabelSelectImageFormat')?>:</label>
  240. <select id="selectImageview" name="selectImageview"<?php echo HTMLstuff::DisabledStr($DisableControls)?>>
  241. <option value="detail" <?php echo $User->getImageview() == 'detail' ? ' selected="selected"' : NULL ?>><?php echo $lang->g('LabelViewModeDetail').$lang->g('LabelSuffixDefault')?></option>
  242. <option value="thumb" <?php echo $User->getImageview() == 'thumb' ? ' selected="selected"' : NULL ?>><?php echo $lang->g('LabelViewModeThumbnail')?></option>
  243. </select>
  244. </div>
  245. <div class="FormRow">
  246. <label><?php echo $lang->g('LabelGender')?>: </label>
  247. <input type="radio" id="radFemale" name="radGender" value="<?php echo GENDER_FEMALE?>"<?php echo $User->getGender() == GENDER_FEMALE ? ' checked="checked"' : NULL?><?php echo HTMLstuff::DisabledStr($DisableControls)?> />
  248. <label for="radFemale" class="Radio"><?php echo $lang->g('LabelFemale')?></label>
  249. <input type="radio" id="radMale" name="radGender" value="<?php echo GENDER_MALE?>"<?php echo $User->getGender() == GENDER_MALE ? ' checked="checked"' : NULL?><?php echo HTMLstuff::DisabledStr($DisableControls)?> />
  250. <label for="radMale" class="Radio"><?php echo $lang->g('LabelMale')?></label>
  251. </div>
  252. <div class="FormRow">
  253. <label for="txtFirstName"><?php echo $lang->g('LabelFirstname')?>: <em>*</em></label>
  254. <input type="text" id="txtFirstName" name="txtFirstName" maxlength="100" value="<?php echo $User->getFirstName()?>"<?php echo HTMLstuff::DisabledStr($DisableControls)?> />
  255. </div>
  256. <div class="FormRow">
  257. <label for="txtInsertion"><?php echo $lang->g('LabelInsertion')?>:</label>
  258. <input type="text" id="txtInsertion" name="txtInsertion" maxlength="20" value="<?php echo $User->getInsertion()?>"<?php echo HTMLstuff::DisabledStr($DisableControls)?> />
  259. </div>
  260. <div class="FormRow">
  261. <label for="txtLastName"><?php echo $lang->g('LabelLastname')?>: <em>*</em></label>
  262. <input type="text" id="txtLastName" name="txtLastName" maxlength="100" value="<?php echo $User->getLastName()?>"<?php echo HTMLstuff::DisabledStr($DisableControls)?> />
  263. </div>
  264. <div class="FormRow">
  265. <label for="txtEmailAddress"><?php echo $lang->g('LabelEmailAddress')?>: <em>*</em></label>
  266. <input type="text" id="txtEmailAddress" name="txtEmailAddress" maxlength="255" value="<?php echo $User->getEmailAddress()?>"<?php echo HTMLstuff::DisabledStr($DisableControls)?> />
  267. </div>
  268. <div class="FormRow">
  269. <label for="txtBirthDate"><?php echo $lang->g('LabelBirthdate')?>:</label>
  270. <input type="text" id="txtBirthDate" name="txtBirthDate" class="DatePicker" maxlength="10" value="<?php echo $User->getBirthDate() > 0 ? date('Y-m-d', $User->getBirthDate()) : NULL?>"<?php echo HTMLstuff::DisabledStr($DisableControls)?> />
  271. </div>
  272. <div class="FormRow">
  273. <label><?php echo $lang->g('LabelUserRights')?>:</label><br />
  274. <label for="chkToggleRights"><input type="checkbox" id="chkToggleRights" name="chkToggleRights"<?php echo HTMLstuff::DisabledStr($DisableControls)?> onclick="ToggleBoxes();"/>&nbsp;<?php echo $lang->g('ButtonToggle')?></label>
  275. <div class="CheckBoxMadness">
  276. <ul><?php echo $RightsCheckboxes?></ul>
  277. </div>
  278. </div>
  279. <div class="FormRow Clear">
  280. <label>&nbsp;</label>
  281. <input type="submit" id="submitform" class="FormButton" value="<?php echo $DeleteUser ? $lang->g('ButtonDelete') : $lang->g('ButtonSave')?>" <?php echo HTMLstuff::DisabledStr($DisableDefaultButton) ?> />
  282. <input type="button" class="FormButton" value="<?php echo $lang->g('ButtonCancel')?>" onclick="window.location='user.php';" />
  283. </div>
  284. <div class="Separator"></div>
  285. <?php echo HTMLstuff::Button('index.php')?>
  286. </fieldset>
  287. </form>
  288. <?php
  289. echo HTMLstuff::HtmlFooter($CurrentUser);
  290. ?>