PageRenderTime 52ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/include/ajax.users.php

https://github.com/ahakim/osTicket-1.8
PHP | 453 lines | 335 code | 96 blank | 22 comment | 63 complexity | b2df2927a43d969b19fca81d1bfd7c16 MD5 | raw file
  1. <?php
  2. /*********************************************************************
  3. ajax.users.php
  4. AJAX interface for users (based on submitted tickets)
  5. XXX: osTicket doesn't support user accounts at the moment.
  6. Peter Rotich <peter@osticket.com>
  7. Copyright (c) 2006-2013 osTicket
  8. http://www.osticket.com
  9. Released under the GNU General Public License WITHOUT ANY WARRANTY.
  10. See LICENSE.TXT for details.
  11. vim: expandtab sw=4 ts=4 sts=4:
  12. **********************************************************************/
  13. if(!defined('INCLUDE_DIR')) die('403');
  14. include_once(INCLUDE_DIR.'class.ticket.php');
  15. require_once INCLUDE_DIR.'class.note.php';
  16. class UsersAjaxAPI extends AjaxController {
  17. /* Assumes search by emal for now */
  18. function search($type = null) {
  19. if(!isset($_REQUEST['q'])) {
  20. Http::response(400, 'Query argument is required');
  21. }
  22. $limit = isset($_REQUEST['limit']) ? (int) $_REQUEST['limit']:25;
  23. $users=array();
  24. $emails=array();
  25. if (!$type || !strcasecmp($type, 'remote')) {
  26. foreach (AuthenticationBackend::searchUsers($_REQUEST['q']) as $u) {
  27. $name = "{$u['first']} {$u['last']}";
  28. $users[] = array('email' => $u['email'], 'name'=>$name,
  29. 'info' => "{$u['email']} - $name (remote)",
  30. 'id' => "auth:".$u['id'], "/bin/true" => $_REQUEST['q']);
  31. $emails[] = $u['email'];
  32. }
  33. }
  34. if (!$type || !strcasecmp($type, 'local')) {
  35. $remote_emails = ($emails = array_filter($emails))
  36. ? ' OR email.address IN ('.implode(',',db_input($emails)).') '
  37. : '';
  38. $escaped = db_input(strtolower($_REQUEST['q']), false);
  39. $sql='SELECT DISTINCT user.id, email.address, name '
  40. .' FROM '.USER_TABLE.' user '
  41. .' JOIN '.USER_EMAIL_TABLE.' email ON user.id = email.user_id '
  42. .' LEFT JOIN '.FORM_ENTRY_TABLE.' entry ON (entry.object_type=\'U\' AND entry.object_id = user.id)
  43. LEFT JOIN '.FORM_ANSWER_TABLE.' value ON (value.entry_id=entry.id) '
  44. .' WHERE email.address LIKE \'%'.$escaped.'%\'
  45. OR user.name LIKE \'%'.$escaped.'%\'
  46. OR value.value LIKE \'%'.$escaped.'%\''.$remote_emails
  47. .' ORDER BY user.created '
  48. .' LIMIT '.$limit;
  49. if(($res=db_query($sql)) && db_num_rows($res)){
  50. while(list($id,$email,$name)=db_fetch_row($res)) {
  51. foreach ($users as $i=>$u) {
  52. if ($u['email'] == $email) {
  53. unset($users[$i]);
  54. break;
  55. }
  56. }
  57. $name = Format::htmlchars($name);
  58. $users[] = array('email'=>$email, 'name'=>$name, 'info'=>"$email - $name",
  59. "id" => $id, "/bin/true" => $_REQUEST['q']);
  60. }
  61. }
  62. }
  63. return $this->json_encode(array_values($users));
  64. }
  65. function preview($id) {
  66. global $thisstaff;
  67. if(!$thisstaff)
  68. Http::response(403, 'Login Required');
  69. elseif(!($user = User::lookup($id)))
  70. Http::response(404, 'Unknown user');
  71. $info = array(
  72. 'title' => '',
  73. 'useredit' => sprintf('#users/%d/edit', $user->getId()),
  74. );
  75. ob_start();
  76. echo sprintf('<div style="width:650px; padding: 2px 2px 0 5px;"
  77. id="u%d">', $user->getId());
  78. include(STAFFINC_DIR . 'templates/user.tmpl.php');
  79. echo '</div>';
  80. $resp = ob_get_contents();
  81. ob_end_clean();
  82. return $resp;
  83. }
  84. function editUser($id) {
  85. global $thisstaff;
  86. if(!$thisstaff)
  87. Http::response(403, 'Login Required');
  88. elseif(!($user = User::lookup($id)))
  89. Http::response(404, 'Unknown user');
  90. $info = array(
  91. 'title' => sprintf('Update %s', Format::htmlchars($user->getName()))
  92. );
  93. $forms = $user->getForms();
  94. include(STAFFINC_DIR . 'templates/user.tmpl.php');
  95. }
  96. function updateUser($id) {
  97. global $thisstaff;
  98. if(!$thisstaff)
  99. Http::response(403, 'Login Required');
  100. elseif(!($user = User::lookup($id)))
  101. Http::response(404, 'Unknown user');
  102. $errors = array();
  103. if($user->updateInfo($_POST, $errors))
  104. Http::response(201, $user->to_json());
  105. $forms = $user->getForms();
  106. include(STAFFINC_DIR . 'templates/user.tmpl.php');
  107. }
  108. function register($id) {
  109. global $thisstaff;
  110. if (!$thisstaff)
  111. Http::response(403, 'Login Required');
  112. elseif (!($user = User::lookup($id)))
  113. Http::response(404, 'Unknown user');
  114. $errors = $info = array();
  115. if ($_POST) {
  116. // Register user on post
  117. if ($user->getAccount())
  118. $info['error'] = 'User already registered';
  119. elseif ($user->register($_POST, $errors))
  120. Http::response(201, 'Account created successfully');
  121. // Unable to create user.
  122. $info = Format::htmlchars($_POST);
  123. if ($errors['err'])
  124. $info['error'] = $errors['err'];
  125. else
  126. $info['error'] = 'Unable to register user - try again!';
  127. }
  128. include(STAFFINC_DIR . 'templates/user-register.tmpl.php');
  129. }
  130. function manage($id, $target=null) {
  131. global $thisstaff;
  132. if (!$thisstaff)
  133. Http::response(403, 'Login Required');
  134. elseif (!($user = User::lookup($id)))
  135. Http::response(404, 'Unknown user');
  136. if (!($account = $user->getAccount()))
  137. return self::register($id);
  138. $errors = array();
  139. $info = $account->getInfo();
  140. if ($_POST) {
  141. if ($account->update($_POST, $errors))
  142. Http::response(201, 'Account updated successfully');
  143. // Unable to update account
  144. $info = Format::htmlchars($_POST);
  145. if ($errors['err'])
  146. $info['error'] = $errors['err'];
  147. else
  148. $info['error'] = 'Unable to update account - try again!';
  149. }
  150. $info['_target'] = $target;
  151. include(STAFFINC_DIR . 'templates/user-account.tmpl.php');
  152. }
  153. function delete($id) {
  154. global $thisstaff;
  155. if (!$thisstaff)
  156. Http::response(403, 'Login Required');
  157. elseif (!($user = User::lookup($id)))
  158. Http::response(404, 'Unknown user');
  159. $info = array();
  160. if ($_POST) {
  161. if ($user->tickets->count()) {
  162. if (!$thisstaff->canDeleteTickets()) {
  163. $info['error'] = 'You do not have permission to delete a user with tickets!';
  164. } elseif ($_POST['deletetickets']) {
  165. foreach($user->tickets as $ticket)
  166. $ticket->delete();
  167. } else {
  168. $info['error'] = 'You cannot delete a user with tickets!';
  169. }
  170. }
  171. if (!$info['error'] && $user->delete())
  172. Http::response(204, 'User deleted successfully');
  173. elseif (!$info['error'])
  174. $info['error'] = 'Unable to delete user - try again!';
  175. }
  176. include(STAFFINC_DIR . 'templates/user-delete.tmpl.php');
  177. }
  178. function getUser($id=false) {
  179. if(($user=User::lookup(($id) ? $id : $_REQUEST['id'])))
  180. Http::response(201, $user->to_json());
  181. $info = array('error' =>'Unknown or invalid user');
  182. return self::_lookupform(null, $info);
  183. }
  184. function lookup() {
  185. return self::addUser();
  186. }
  187. function addUser() {
  188. $info = array();
  189. if (!AuthenticationBackend::getSearchDirectories())
  190. $info['lookup'] = 'local';
  191. if ($_POST) {
  192. $info['title'] = 'Add New User';
  193. $form = UserForm::getUserForm()->getForm($_POST);
  194. if (($user = User::fromForm($form)))
  195. Http::response(201, $user->to_json());
  196. $info['error'] = 'Error adding user - try again!';
  197. }
  198. return self::_lookupform($form, $info);
  199. }
  200. function addRemoteUser($bk, $id) {
  201. global $thisstaff;
  202. if (!$thisstaff)
  203. Http::response(403, 'Login Required');
  204. elseif (!$bk || !$id)
  205. Http::response(422, 'Backend and user id required');
  206. elseif (!($backend = AuthenticationBackend::getSearchDirectoryBackend($bk))
  207. || !($user_info = $backend->lookup($id)))
  208. Http::response(404, 'User not found');
  209. $form = UserForm::getUserForm()->getForm($user_info);
  210. $info = array('title' => 'Import Remote User');
  211. if (!$user_info)
  212. $info['error'] = 'Unable to find user in directory';
  213. include(STAFFINC_DIR . 'templates/user-lookup.tmpl.php');
  214. }
  215. function importUsers() {
  216. global $thisstaff;
  217. if (!$thisstaff)
  218. Http::response(403, 'Login Required');
  219. $info = array(
  220. 'title' => 'Import Users',
  221. 'action' => '#users/import',
  222. 'upload_url' => "users.php?do=import-users",
  223. );
  224. if ($_POST) {
  225. $status = User::importFromPost($_POST['pasted']);
  226. if (is_string($status))
  227. $info['error'] = $status;
  228. else
  229. Http::response(201, "{\"count\": $status}");
  230. }
  231. $info += Format::input($_POST);
  232. include STAFFINC_DIR . 'templates/user-import.tmpl.php';
  233. }
  234. function selectUser($id) {
  235. if ($id)
  236. $user = User::lookup($id);
  237. $info = array('title' => 'Select User');
  238. ob_start();
  239. include(STAFFINC_DIR . 'templates/user-lookup.tmpl.php');
  240. $resp = ob_get_contents();
  241. ob_end_clean();
  242. return $resp;
  243. }
  244. static function _lookupform($form=null, $info=array()) {
  245. if (!$info or !$info['title'])
  246. $info += array('title' => 'Lookup or create a user');
  247. ob_start();
  248. include(STAFFINC_DIR . 'templates/user-lookup.tmpl.php');
  249. $resp = ob_get_contents();
  250. ob_end_clean();
  251. return $resp;
  252. }
  253. function searchStaff() {
  254. global $thisstaff;
  255. if (!$thisstaff)
  256. Http::response(403, 'Login required for searching');
  257. elseif (!$thisstaff->isAdmin())
  258. Http::response(403,
  259. 'Administrative privilege is required for searching');
  260. elseif (!isset($_REQUEST['q']))
  261. Http::response(400, 'Query argument is required');
  262. $users = array();
  263. foreach (AuthenticationBackend::getSearchDirectories() as $ab) {
  264. foreach ($ab->search($_REQUEST['q']) as $u)
  265. $users[] = $u;
  266. }
  267. return $this->json_encode($users);
  268. }
  269. function updateOrg($id, $orgId = 0) {
  270. global $thisstaff;
  271. if (!$thisstaff)
  272. Http::response(403, 'Login Required');
  273. elseif (!($user = User::lookup($id)))
  274. Http::response(404, 'Unknown user');
  275. $info = array();
  276. $info['title'] = 'Organization for '.Format::htmlchars($user->getName());
  277. $info['action'] = '#users/'.$user->getId().'/org';
  278. $info['onselect'] = 'ajax.php/users/'.$user->getId().'/org';
  279. if ($_POST) {
  280. if ($_POST['orgid']) { //Existing org.
  281. if (!($org = Organization::lookup($_POST['orgid'])))
  282. $info['error'] = 'Unknown organization selected';
  283. } else { //Creating new org.
  284. $form = OrganizationForm::getDefaultForm()->getForm($_POST);
  285. if (!($org = Organization::fromForm($form)))
  286. $info['error'] = 'Unable to create organization - try again!';
  287. }
  288. if ($org && $user->setOrganization($org))
  289. Http::response(201, $org->to_json());
  290. elseif (! $info['error'])
  291. $info['error'] = 'Unable to add organization - try again!';
  292. } elseif ($orgId)
  293. $org = Organization::lookup($orgId);
  294. elseif ($org = $user->getOrganization()) {
  295. $info['title'] = sprintf('%s &mdash; %s', Format::htmlchars($user->getName()), 'Organization');
  296. $info['action'] = $info['onselect'] = '';
  297. $tmpl = 'org.tmpl.php';
  298. }
  299. if ($org && $user->getOrgId() && $org->getId() != $user->getOrgId())
  300. $info['warning'] = 'Are you sure you want to change user\'s organization?';
  301. $tmpl = $tmpl ?: 'org-lookup.tmpl.php';
  302. ob_start();
  303. include(STAFFINC_DIR . "templates/$tmpl");
  304. $resp = ob_get_contents();
  305. ob_end_clean();
  306. return $resp;
  307. }
  308. function createNote($id) {
  309. if (!($user = User::lookup($id)))
  310. Http::response(404, 'Unknown user');
  311. require_once INCLUDE_DIR . 'ajax.note.php';
  312. $ajax = new NoteAjaxAPI();
  313. return $ajax->createNote('U'.$id);
  314. }
  315. function manageForms($user_id) {
  316. $forms = DynamicFormEntry::forUser($user_id);
  317. $info = array('action' => '#users/'.Format::htmlchars($user_id).'/forms/manage');
  318. include(STAFFINC_DIR . 'templates/form-manage.tmpl.php');
  319. }
  320. function updateForms($user_id) {
  321. global $thisstaff;
  322. if (!$thisstaff)
  323. Http::response(403, "Login required");
  324. elseif (!($user = User::lookup($user_id)))
  325. Http::response(404, "No such user");
  326. elseif (!isset($_POST['forms']))
  327. Http::response(422, "Send updated forms list");
  328. // Add new forms
  329. $forms = DynamicFormEntry::forUser($user_id);
  330. foreach ($_POST['forms'] as $sort => $id) {
  331. $found = false;
  332. foreach ($forms as $e) {
  333. if ($e->get('form_id') == $id) {
  334. $e->set('sort', $sort);
  335. $e->save();
  336. $found = true;
  337. break;
  338. }
  339. }
  340. // New form added
  341. if (!$found && ($new = DynamicForm::lookup($id))) {
  342. $user->addForm($new, $sort);
  343. }
  344. }
  345. // Deleted forms
  346. foreach ($forms as $idx => $e) {
  347. if (!in_array($e->get('form_id'), $_POST['forms']))
  348. $e->delete();
  349. }
  350. Http::response(201, 'Successfully managed');
  351. }
  352. }
  353. ?>