PageRenderTime 26ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/0.0.3/Framework/Module/Accounts/Modify.php

https://github.com/shupp/toasteradmin
PHP | 379 lines | 252 code | 31 blank | 96 comment | 73 complexity | fd35917e6f74f3cae64d46a0d4907e76 MD5 | raw file
  1. <?php
  2. /**
  3. * Framework_Module_Accounts_Modify
  4. *
  5. * Class just for modifying accounts
  6. *
  7. * PHP Version 5.1.0+
  8. *
  9. * @uses ToasterAdmin_Auth_User
  10. * @category Mail
  11. * @package ToasterAdmin
  12. * @author Bill Shupp <hostmaster@shupp.org>
  13. * @copyright 2007-2008 Bill Shupp
  14. * @license GPL 2.0 {@link http://www.gnu.org/licenses/gpl.txt}
  15. * @link http://trac.merchbox.com/trac/toasteradmin
  16. */
  17. /**
  18. * Framework_Module_Accounts_Modify
  19. *
  20. * Class just for modifying accounts
  21. *
  22. * @uses ToasterAdmin_Auth_User
  23. * @category Mail
  24. * @package ToasterAdmin
  25. * @subpackage Module
  26. * @author Bill Shupp <hostmaster@shupp.org>
  27. * @copyright 2007-2008 Bill Shupp
  28. * @license GPL 2.0 {@link http://www.gnu.org/licenses/gpl.txt}
  29. * @link http://trac.merchbox.com/trac/toasteradmin
  30. */
  31. class Framework_Module_Accounts_Modify extends ToasterAdmin_Auth_User
  32. {
  33. /**
  34. * __default
  35. *
  36. * Execute modifyAccount()
  37. *
  38. * @access public
  39. * @return void
  40. */
  41. public function __default()
  42. {
  43. return $this->modifyAccount();
  44. }
  45. /**
  46. * modifyAccount
  47. *
  48. * Modify a user account
  49. *
  50. * @access public
  51. * @return void
  52. */
  53. public function modifyAccount()
  54. {
  55. // Make sure account was supplied
  56. if (!isset($_REQUEST['account'])) {
  57. throw new Framework_Exception (_("Error: no account supplied"));
  58. }
  59. $account = $_REQUEST['account'];
  60. if ($this->user->isDomainAdmin($this->domain)) {
  61. $account_info = $this->user->userInfo($this->domain, $account);
  62. } else {
  63. $account_info = $this->user->loginUser;
  64. }
  65. // Get .qmail info if it exists
  66. try {
  67. $dot_qmail = $this->user->readFile($this->domain,
  68. $_REQUEST['account'], '.qmail');
  69. } catch (Net_Vpopmaild_Exception $e) {
  70. $dot_qmail = '';
  71. }
  72. $defaults = $this->parseHomeDotqmail($dot_qmail, $account_info);
  73. $this->user->recordio(print_r($defaults, 1));
  74. $form = $this->modifyAccountForm($account, $defaults);
  75. $renderer = new HTML_QuickForm_Renderer_AssocArray();
  76. $form->accept($renderer);
  77. if (isset($_REQUEST['modified'])) {
  78. $this->setData('message', _('Account Modified Successfully'));
  79. }
  80. $this->setData('form', $renderer->toAssocArray());
  81. $this->tplFile = 'modifyAccount.tpl';
  82. return;
  83. }
  84. /**
  85. * modifyAccountForm
  86. *
  87. * Build the modify account form
  88. *
  89. * @param mixed $account account name
  90. * @param mixed $defaults defaults
  91. *
  92. * @access protected
  93. * @return void
  94. */
  95. protected function modifyAccountForm($account, $defaults)
  96. {
  97. if ($this->user->isDomainAdmin($this->domain)) {
  98. $this->setData('isDomainAdmin', 1);
  99. }
  100. $this->setData('account', $account);
  101. $url = "./?module=Accounts&class=Modify&event=modifyAccountNow";
  102. $url .= "&domain={$this->domain}&account=$account";
  103. $form = ToasterAdmin_Form::factory('formModifyAccount', $url);
  104. $form->setDefaults($defaults);
  105. $form->addElement('text', 'comment', _("Real Name/Comment"));
  106. $form->addElement('password', 'password', _("Password"));
  107. $form->addElement('password', 'password2', _("Re-Type Password"));
  108. $form->addElement('radio', 'routing', _('Mail Routing'),
  109. _('Standard (No Forwarding)'), 'routing_standard');
  110. $form->addElement('radio', 'routing', '',
  111. _('All Mail Deleted'), 'routing_deleted');
  112. $form->addElement('radio', 'routing', '',
  113. _('Forward to:'), 'routing_forwarded');
  114. $form->addElement('text', 'forward');
  115. $form->addElement('checkbox', 'save_a_copy', _('Save A Copy'));
  116. $form->addElement('checkbox', 'vacation',
  117. _('Send a Vacation Auto-Response'));
  118. $form->addElement('text', 'vacation_subject',
  119. _('Vacation Subject:'));
  120. $form->addElement('textarea', 'vacation_body',
  121. _('Vacation Message:'), 'rows="10" cols="40"');
  122. $form->addElement('submit', 'submit', _('Modify Account'));
  123. $form->addRule(array('password', 'password2'),
  124. _('The passwords do not match'), 'compare', null, 'client');
  125. $form->addRule('routing',
  126. _('Please select a mail routing type'), 'required', null, 'client');
  127. $form->addRule('forward',
  128. _('"Forward to" must be a valid email address'),
  129. 'email', null, 'client');
  130. return $form;
  131. }
  132. /**
  133. * modifyAccountNow
  134. *
  135. * Modify Acount
  136. *
  137. * @access public
  138. * @return void
  139. */
  140. public function modifyAccountNow()
  141. {
  142. // Make sure account was supplied
  143. if (!isset($_REQUEST['account'])) {
  144. throw new Framework_Exception (_("Error: no account supplied"));
  145. }
  146. $account = $_REQUEST['account'];
  147. // See what user_info to use
  148. if ($this->user->isDomainAdmin($this->domain)) {
  149. $account_info = $this->user->userInfo($this->domain, $account);
  150. } else {
  151. $account_info = $this->user->loginUser;
  152. }
  153. // Get .qmail info if it exists
  154. try {
  155. $dot_qmail = $this->user->readFile($this->domain,
  156. $_REQUEST['account'], '.qmail');
  157. } catch (Net_Vpopmaild_Exception $e) {
  158. $dot_qmail = '';
  159. }
  160. $defs = $this->parseHomeDotqmail($dot_qmail, $account_info);
  161. $form = $this->modifyAccountForm($account, $defs);
  162. if (!$form->validate()) {
  163. $this->setData('message', _("Error Modifying Account"));
  164. $renderer =& new HTML_QuickForm_Renderer_AssocArray();
  165. $form->accept($renderer);
  166. $this->setData('form', $renderer->toAssocArray());
  167. $this->tplFile = 'modifyAccount.tpl';
  168. return;
  169. }
  170. // update password / comment if it's changing
  171. $changePass = 0;
  172. $changeComment = 0;
  173. $password = $form->getElementValue('password');
  174. $comment = $form->getElementValue('comment');
  175. if (!empty($password)) {
  176. $account_info['clear_text_password'] = $password;
  177. $changePass = 1;
  178. }
  179. if (!empty($comment)) {
  180. $account_info['comment'] = $comment;
  181. }
  182. if ($changePass || $changeComment) {
  183. $this->user->modUser($this->domain, $_REQUEST['account'], $account_info);
  184. }
  185. if ($changePass && $account == $this->user->loginUser['name']
  186. && $this->domain == $this->user->loginUser['domain']) {
  187. $crypt = new Crypt_Blowfish((string)Framework::$site->config->mcryptKey);
  188. $this->session->password = $crypt->encrypt($password);
  189. }
  190. // Determine new routing
  191. $routing = '';
  192. $save_a_copy = 0;
  193. if ($_REQUEST['routing'] == 'routing_standard') {
  194. $routing = 'standard';
  195. } else if ($_REQUEST['routing'] == 'routing_deleted') {
  196. $routing = 'deleted';
  197. } else if ($_REQUEST['routing'] == 'routing_forwarded') {
  198. if (empty($_REQUEST['forward'])) {
  199. $this->setData('message',
  200. _('Error: you must supply a forward address'));
  201. return $this->modifyAccount();
  202. } else {
  203. $forward = $_REQUEST['forward'];
  204. }
  205. $routing = 'forwarded';
  206. if (isset($_REQUEST['save_a_copy'])) $save_a_copy = 1;
  207. } else {
  208. $this->setData('message', _('Error: unsupported routing selection'));
  209. return $this->modifyAccount();
  210. }
  211. // Check for vacation
  212. $vacation = 0;
  213. if (isset($_REQUEST['vacation']) && $_REQUEST['vacation'] == 1) {
  214. $vacation = 1;
  215. $vacation_subject = $_REQUEST['vacation_subject'];
  216. $vacation_body = $_REQUEST['vacation_body'];
  217. }
  218. // Are we deleting a vacation message?
  219. if ($vacation == 0 && $defs['vacation'] == ' checked') {
  220. // Kill old message
  221. $this->user->rmDir($this->domain, $account_info['name'], 'vacation');
  222. }
  223. // Build .qmail contents
  224. $dot_qmail_contents = '';
  225. if ($routing == 'deleted') {
  226. $dot_qmail_contents = "# delete";
  227. } else if ($routing == 'forwarded') {
  228. $dot_qmail_contents = "&$forward";
  229. if ($save_a_copy == 1) $dot_qmail_contents .= "\n./Maildir/";
  230. }
  231. if ($vacation == 1) {
  232. if (strlen($dot_qmail_contents) > 0) {
  233. $dot_qmail_contents .= "\n";
  234. }
  235. $vacation_dir = $account_info['user_dir'] . '/vacation';
  236. $dot_qmail_contents .= '| ' . $this->user->vpopmailRobotProgram;
  237. $dot_qmail_contents .= ' ' . $this->user->vpopmailRobotTime;
  238. $dot_qmail_contents .= ' ' . $this->user->vpopmailRobotNumber;
  239. $dot_qmail_contents .= " $vacation_dir/message $vacation_dir";
  240. }
  241. $dot_qmail_file = '.qmail';
  242. if (strlen($dot_qmail_contents) > 0) {
  243. $contents = explode("\n", $dot_qmail_contents);
  244. // Write .qmail file
  245. $result = $this->user->writeFile($contents, $this->domain,
  246. $account_info['name'], $dot_qmail_file);
  247. // Add vacation files
  248. if ($vacation == 1) {
  249. $vcontents = "From: " . $account_info['name'] . "@{$this->domain}";
  250. $vcontents .= "\n";
  251. $vcontents .= "Subject: $vacation_subject\n\n";
  252. $vcontents .= $vacation_body;
  253. $contents = explode("\n", $vcontents);
  254. $vdir = 'vacation';
  255. $message = 'vacation/message';
  256. // Delete existing file
  257. try {
  258. $this->user->rmDir($this->domain, $account_info['name'], $vdir);
  259. } catch (Net_Vpopmaild_Exception $e) {
  260. }
  261. // Make vacation directory
  262. $result = $this->user->mkDir($this->domain,
  263. $account_info['name'], $vdir);
  264. // Write vacation message
  265. $result = $this->user->writeFile($contents, $this->domain,
  266. $account_info['name'], $message);
  267. }
  268. } else {
  269. try {
  270. $this->user->rmFile($this->domain,
  271. $account_info['name'], $dot_qmail_file);
  272. } catch (Net_Vpopmaild_Exception $e) {
  273. }
  274. }
  275. $url = "./?module=Accounts&class=Modify&event=modifyAccount";
  276. $url .= "&domain={$this->domain}&account={$account_info['name']}&modified=1";
  277. header("Location: $url");
  278. return;
  279. }
  280. /**
  281. * Parse Home dot-qmail
  282. *
  283. * Evaluate contents of a .qmail file in a user's home directory.
  284. * Looking for routing types standard, delete, or forward, with optional
  285. * saving of messages, as well as vacation messages.
  286. *
  287. * @param mixed $contents .qmail contents
  288. * @param mixed $account_info user account info
  289. *
  290. * @access protected
  291. * @return array $defaults
  292. */
  293. protected function parseHomeDotqmail($contents, $account_info)
  294. {
  295. $is_standard = false;
  296. $is_deleted = false;
  297. $is_forwarded = false;
  298. // Set default template settings
  299. $defaults['comment'] = $account_info['comment'];
  300. $defaults['forward'] = '';
  301. $defaults['save_a_copy'] = '';
  302. $defaults['vacation'] = '';
  303. $defaults['vacation_subject'] = '';
  304. $defaults['vacation_body'] = '';
  305. if (empty($contents)) {
  306. $is_standard = true;
  307. }
  308. if ((is_array($contents)
  309. && count($contents) == 1
  310. && $contents[0] == '# delete')) {
  311. $is_deleted = true;
  312. }
  313. if ($is_standard) {
  314. $defaults['routing'] = 'routing_standard';
  315. } else if ($is_deleted) {
  316. $defaults['routing'] = 'routing_deleted';
  317. } else {
  318. // now let's parse it
  319. while (list($key, $val) = each($contents)) {
  320. if ($val == $account_info['user_dir'].'/Maildir/'
  321. || $val == './Maildir/') {
  322. $defaults['save_a_copy'] = ' checked';
  323. continue;
  324. }
  325. if (preg_match("({$this->user->vpopmailRobotProgram})", $val)) {
  326. $vacation_array = $this->user->getVacation($account_info, $val);
  327. while (list($vacKey, $vacVal) = each($vacation_array)) {
  328. $defaults[$vacKey] = $vacVal;
  329. }
  330. continue;
  331. } else {
  332. if (Validate::email(preg_replace('/^&/', '', $val),
  333. array('use_rfc822' => 1))) {
  334. $is_forwarded = true;
  335. $defaults['routing'] = 'routing_forwarded';
  336. $defaults['forward'] = preg_replace('/^&/', '', $val);
  337. }
  338. }
  339. }
  340. // See if default routing select applies
  341. if (!$is_standard && !$is_deleted && !$is_forwarded) {
  342. $defaults['routing'] = 'routing_standard';
  343. }
  344. }
  345. return $defaults;
  346. }
  347. }
  348. ?>