PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/phreedom/pages/roles/pre_process.php

http://phreedom.googlecode.com/
PHP | 209 lines | 165 code | 8 blank | 36 comment | 22 complexity | 93405ea55b74a56dba811fc2b80877de MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0
  1. <?php
  2. // +-----------------------------------------------------------------+
  3. // | PhreeBooks Open Source ERP |
  4. // +-----------------------------------------------------------------+
  5. // | Copyright (c) 2008, 2009, 2010, 2011, 2012 PhreeSoft, LLC |
  6. // | http://www.PhreeSoft.com |
  7. // +-----------------------------------------------------------------+
  8. // | This program is free software: you can redistribute it and/or |
  9. // | modify it under the terms of the GNU General Public License as |
  10. // | published by the Free Software Foundation, either version 3 of |
  11. // | the License, or any later version. |
  12. // | |
  13. // | This program is distributed in the hope that it will be useful, |
  14. // | but WITHOUT ANY WARRANTY; without even the implied warranty of |
  15. // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
  16. // | GNU General Public License for more details. |
  17. // +-----------------------------------------------------------------+
  18. // Path: /modules/phreedom/pages/roles/pre_process.php
  19. //
  20. $security_level = validate_user(SECURITY_ID_ROLES);
  21. /************** include page specific files *********************/
  22. gen_pull_language($module, 'admin');
  23. //gen_pull_language('contacts');
  24. //require_once(DIR_FS_WORKING . 'functions/phreedom.php');
  25. //require_once(DIR_FS_MODULES . 'phreebooks/functions/phreebooks.php');
  26. /************** page specific initialization *************************/
  27. $error = false;
  28. $action = isset($_GET['action']) ? $_GET['action'] : $_POST['todo'];
  29. // load the sort fields
  30. $_GET['sf'] = $_POST['sort_field'] ? $_POST['sort_field'] : $_GET['sf'];
  31. $_GET['so'] = $_POST['sort_order'] ? $_POST['sort_order'] : $_GET['so'];
  32. /*************** hook for custom actions ***************************/
  33. $custom_path = DIR_FS_WORKING . 'custom/pages/roles/extra_actions.php';
  34. if (file_exists($custom_path)) { include($custom_path); }
  35. /*************** Act on the action request *************************/
  36. switch ($action) {
  37. case 'save':
  38. case 'fill_all':
  39. validate_security($security_level, 2);
  40. $admin_id = db_prepare_input($_POST['rowSeq']);
  41. $fill_all = db_prepare_input($_POST['fill_all']);
  42. $prefs = array(
  43. 'def_store_id' => db_prepare_input($_POST['def_store_id']),
  44. 'def_cash_acct' => db_prepare_input($_POST['def_cash_acct']),
  45. 'def_ar_acct' => db_prepare_input($_POST['def_ar_acct']),
  46. 'def_ap_acct' => db_prepare_input($_POST['def_ap_acct']),
  47. 'restrict_store' => isset($_POST['restrict_store']) ? '1' : '0',
  48. 'restrict_period' => isset($_POST['restrict_period']) ? '1' : '0',
  49. );
  50. // not the most elegent but look for a colon in the second character position
  51. $post_keys = array_keys($_POST);
  52. $admin_security = '';
  53. foreach ($post_keys as $key) {
  54. if (strpos($key, 'sID_') === 0) { // it's a security setting post
  55. if ($admin_security) $admin_security .= ',';
  56. $admin_security .= substr($key, 4) . ':' . (($fill_all == '-1') ? substr($_POST[$key], 0, 1) : $fill_all);
  57. }
  58. }
  59. $sql_data_array = array(
  60. 'admin_name' => db_prepare_input($_POST['admin_name']),
  61. 'is_role' => '1',
  62. 'inactive' => isset($_POST['inactive']) ? '1' : '0',
  63. 'admin_prefs' => serialize($prefs),
  64. 'admin_security' => $admin_security,
  65. );
  66. if (!$admin_id) { // check for duplicate user name
  67. $result = $db->Execute("select admin_id from " . TABLE_USERS . "
  68. where admin_name = '" . db_prepare_input($_POST['admin_name']) . "'");
  69. if ($result->RecordCount() > 0) {
  70. $error = $messageStack->add(ENTRY_DUP_USER_NEW_ERROR, 'error');
  71. }
  72. }
  73. if (!$error) {
  74. if ($admin_id) {
  75. db_perform(TABLE_USERS, $sql_data_array, 'update', 'admin_id = ' . (int)$admin_id);
  76. gen_add_audit_log(sprintf(GEN_LOG_USER, TEXT_UPDATE), db_prepare_input($_POST['admin_name']));
  77. } else {
  78. db_perform(TABLE_USERS, $sql_data_array);
  79. $admin_id = db_insert_id();
  80. gen_add_audit_log(sprintf(GEN_LOG_USER, TEXT_ADD), db_prepare_input($_POST['admin_name']));
  81. }
  82. if ($admin_id == $_SESSION['admin_id']) $_SESSION['admin_security'] = gen_parse_permissions($admin_security); // update if user is current user
  83. } elseif ($error) {
  84. $action = 'edit';
  85. }
  86. $uInfo = new objectInfo($_POST);
  87. $uInfo->admin_security = $admin_security;
  88. break;
  89. case 'copy':
  90. validate_security($security_level, 2);
  91. $admin_id = db_prepare_input($_GET['cID']);
  92. $new_name = db_prepare_input($_GET['name']);
  93. // check for duplicate user names
  94. $result = $db->Execute("select admin_name from " . TABLE_USERS . " where admin_name = '" . $new_name . "'");
  95. if ($result->Recordcount() > 0) { // error and reload
  96. $messageStack->add(GEN_ERROR_DUPLICATE_ID, 'error');
  97. break;
  98. }
  99. $result = $db->Execute("select * from " . TABLE_USERS . " where admin_id = " . $admin_id);
  100. $old_name = $result->fields['admin_name'];
  101. // clean up the fields (especially the system fields, retain the custom fields)
  102. $output_array = array();
  103. foreach ($result->fields as $key => $value) {
  104. switch ($key) {
  105. case 'admin_id': // Remove from write list fields
  106. case 'display_name':
  107. case 'admin_email':
  108. case 'admin_pass':
  109. case 'account_id':
  110. break;
  111. case 'admin_name': // set the new user name
  112. $output_array[$key] = $new_name;
  113. break;
  114. default:
  115. $output_array[$key] = $value;
  116. }
  117. }
  118. db_perform(TABLE_USERS, $output_array, 'insert');
  119. $new_id = db_insert_id();
  120. $messageStack->add(GEN_MSG_COPY_SUCCESS, 'success');
  121. // now continue with newly copied item by editing it
  122. gen_add_audit_log(sprintf(GEN_LOG_USER, TEXT_COPY), $old_name . ' => ' . $new_name);
  123. $_POST['rowSeq'] = $new_id; // set item pointer to new record
  124. $action = 'edit'; // fall through to edit case
  125. case 'edit':
  126. if (isset($_POST['rowSeq'])) $admin_id = db_prepare_input($_POST['rowSeq']);
  127. $result = $db->Execute("select * from " . TABLE_USERS . " where admin_id = " . (int)$admin_id);
  128. $temp = unserialize($result->fields['admin_prefs']);
  129. unset($result->fields['admin_prefs']);
  130. $uInfo = new objectInfo($result->fields);
  131. foreach ($temp as $key => $value) $uInfo->$key = $value;
  132. break;
  133. case 'delete':
  134. validate_security($security_level, 4);
  135. $admin_id = (int)db_prepare_input($_POST['rowSeq']);
  136. // fetch the name for the audit log
  137. $result = $db->Execute("select admin_name from " . TABLE_USERS . " where admin_id = " . $admin_id);
  138. $db->Execute("delete from " . TABLE_USERS . " where admin_id = " . $admin_id);
  139. gen_add_audit_log(sprintf(GEN_LOG_USER, TEXT_DELETE), $result->fields['admin_name']);
  140. gen_redirect(html_href_link(FILENAME_DEFAULT, gen_get_all_get_params(array('action')), 'SSL'));
  141. break;
  142. case 'go_first': $_GET['list'] = 1; break;
  143. case 'go_previous': $_GET['list']--; break;
  144. case 'go_next': $_GET['list']++; break;
  145. case 'go_last': $_GET['list'] = 99999; break;
  146. case 'search':
  147. case 'search_reset':
  148. case 'go_page':
  149. default:
  150. }
  151. /***************** prepare to display templates *************************/
  152. $fill_all_values = array(
  153. array('id' => '-1', 'text' => GEN_HEADING_PLEASE_SELECT),
  154. array('id' => '0', 'text' => TEXT_NONE),
  155. array('id' => '1', 'text' => TEXT_READ_ONLY),
  156. array('id' => '2', 'text' => TEXT_ADD),
  157. array('id' => '3', 'text' => TEXT_EDIT),
  158. array('id' => '4', 'text' => TEXT_FULL),
  159. );
  160. $include_header = true;
  161. $include_footer = true;
  162. $include_tabs = true;
  163. $include_calendar = false;
  164. switch ($action) {
  165. case 'new':
  166. case 'edit':
  167. case 'fill_all':
  168. $include_template = 'template_detail.php';
  169. $role_name = isset($uInfo->admin_name) ? (' - ' . $uInfo->admin_name) : '';
  170. define('PAGE_TITLE', HEADING_TITLE_ROLE_DETAIL . $role_name);
  171. break;
  172. default:
  173. // build the list header
  174. $heading_array = array(
  175. 'admin_name' => GEN_USERNAME,
  176. 'inactive' => TEXT_INACTIVE,
  177. );
  178. $result = html_heading_bar($heading_array, $_GET['sf'], $_GET['so']);
  179. $list_header = $result['html_code'];
  180. $disp_order = $result['disp_order'];
  181. // build the list for the page selected
  182. $search_text = ($_GET['search_text'] == TEXT_SEARCH) ? '' : db_input($_GET['search_text']);
  183. if (isset($search_text) && $search_text <> '') {
  184. $search_fields = array('admin_name', 'admin_email', 'display_name');
  185. // hook for inserting new search fields to the query criteria.
  186. if (is_array($extra_search_fields)) $search_fields = array_merge($search_fields, $extra_search_fields);
  187. $search = ' and (' . implode(' like \'%' . $search_text . '%\' or ', $search_fields) . ' like \'%' . $search_text . '%\')';
  188. } else {
  189. $search = '';
  190. }
  191. $field_list = array('admin_id', 'inactive', 'display_name', 'admin_name', 'admin_email');
  192. // hook to add new fields to the query return results
  193. if (is_array($extra_query_list_fields) > 0) $field_list = array_merge($field_list, $extra_query_list_fields);
  194. $query_raw = "select " . implode(', ', $field_list) . " from " . TABLE_USERS . " where
  195. is_role = '1'" . $search . " order by $disp_order";
  196. $query_split = new splitPageResults($_GET['list'], MAX_DISPLAY_SEARCH_RESULTS, $query_raw, $query_numrows);
  197. $query_result = $db->Execute($query_raw);
  198. $include_template = 'template_main.php';
  199. define('PAGE_TITLE', BOX_HEADING_ROLES);
  200. }
  201. ?>