/lib/IWmain/Api/User.php

https://github.com/intraweb-modules13/IWmain · PHP · 826 lines · 508 code · 54 blank · 264 comment · 183 complexity · 714d189b47d40b9f60b6bdeb2494de12 MD5 · raw file

  1. <?php
  2. class IWmain_Api_User extends Zikula_AbstractApi {
  3. /**
  4. * Get all the users
  5. * @author: Albert Pérez Monfort (aperezm@xtec.cat)
  6. * @return: And array with the users
  7. */
  8. public function getAllUsers($args) {
  9. $fromArray = FormUtil::getPassedValue('fromArray', isset($args['fromArray']) ? $args['fromArray'] : null, 'POST');
  10. $list = FormUtil::getPassedValue('list', isset($args['list']) ? $args['list'] : null, 'POST');
  11. $sv = FormUtil::getPassedValue('sv', isset($args['sv']) ? $args['sv'] : null, 'POST');
  12. if (!ModUtil::func('IWmain', 'user', 'checkSecurityValue', array('sv' => $sv))) {
  13. return LogUtil::registerError($this->__('You are not allowed to access to some information.'));
  14. }
  15. $table = DBUtil::getTables();
  16. $where = "";
  17. $c = $table['users_column'];
  18. if ($fromArray != null && count($fromArray) > 0) {
  19. foreach ($fromArray as $f) {
  20. $where .= " $c[uid] = $f[uid] OR";
  21. }
  22. $where = substr($where, 0, -3);
  23. }
  24. if ($list != null && strlen($list) > 0) {
  25. $modArray = explode('$$', $list);
  26. $modArray = array_unique($modArray);
  27. foreach ($modArray as $mod) {
  28. $mod = str_replace('$', '', $mod);
  29. if ($mod != '' && is_numeric($mod)) {
  30. $where .= " $c[uid] = " . $mod . " OR";
  31. }
  32. }
  33. $where = substr($where, 0, -3);
  34. }
  35. // get the objects from the db
  36. $items = DBUtil::selectObjectArray('users', $where);
  37. // Check for an error with the database code, and if so set an appropriate
  38. // error message and return
  39. if ($items === false)
  40. return LogUtil::registerError($this->__('Error! Could not load items.'));
  41. // Return the items
  42. return $items;
  43. }
  44. /**
  45. * Get information from IWusers of all users
  46. * @author: Albert Pérez Monfort (aperezm@xtec.cat)
  47. * @return: And array with the users
  48. */
  49. public function getUsersExtraInfo($args) {
  50. $fromArray = FormUtil::getPassedValue('fromArray', isset($args['fromArray']) ? $args['fromArray'] : null, 'POST');
  51. $list = FormUtil::getPassedValue('list', isset($args['list']) ? $args['list'] : null, 'POST');
  52. $items = array();
  53. $table = DBUtil::getTables();
  54. $where = "";
  55. $c = $table['IWusers_column'];
  56. //die('tt');
  57. if ($fromArray != null && count($fromArray) > 0) {
  58. foreach ($fromArray as $f) {
  59. $where .= " $c[uid] = $f[uid] OR";
  60. }
  61. $where = substr($where, 0, -3);
  62. }
  63. if ($list != null && strlen($list) > 0) {
  64. $modArray = explode('$$', $list);
  65. $modArray = array_unique($modArray);
  66. foreach ($modArray as $mod) {
  67. $mod = str_replace('$', '', $mod);
  68. if ($mod != '' && is_numeric($mod))
  69. $where .= " $c[uid] = " . $mod . " OR";
  70. }
  71. $where = substr($where, 0, -3);
  72. }
  73. // get the objects from the db
  74. $items = DBUtil::selectObjectArray('IWusers', $where);
  75. // Check for an error with the database code, and if so set an appropriate
  76. // error message and return
  77. if ($items === false) {
  78. return LogUtil::registerError($this->__('Error! Could not load items.'));
  79. }
  80. // Return the items
  81. return $items;
  82. }
  83. /**
  84. * Get an user
  85. * @author: Albert Pérez Monfort (aperezm@xtec.cat)
  86. * @param: args id of the user
  87. * @return: And array with the user information
  88. */
  89. public function getUser($args) {
  90. $sv = FormUtil::getPassedValue('sv', isset($args['sv']) ? $args['sv'] : null, 'POST');
  91. $uid = FormUtil::getPassedValue('uid', isset($args['uid']) ? $args['uid'] : null, 'POST');
  92. if (!ModUtil::func('IWmain', 'user', 'checkSecurityValue', array('sv' => $sv))) {
  93. return LogUtil::registerError($this->__('You are not allowed to access to some information.'));
  94. }
  95. $items = array();
  96. $table = DBUtil::getTables();
  97. $c = $table['users_column'];
  98. $where = "$c[uid]=$uid";
  99. // get the objects from the db
  100. $items = DBUtil::selectObjectArray('users', $where);
  101. // Check for an error with the database code, and if so set an appropriate
  102. // error message and return
  103. if ($items === false)
  104. return LogUtil::registerError($this->__('Error! Could not load items.'));
  105. // Return the items
  106. return $items;
  107. }
  108. /**
  109. * Get information from IWusers of an users
  110. * @author: Albert Pérez Monfort (aperezm@xtec.cat)
  111. * @param: args id of the user
  112. * @return: And array with the user information
  113. */
  114. public function getUserExtraInfo($args) {
  115. $sv = FormUtil::getPassedValue('sv', isset($args['sv']) ? $args['sv'] : null, 'POST');
  116. $uid = FormUtil::getPassedValue('uid', isset($args['uid']) ? $args['uid'] : null, 'POST');
  117. $items = array();
  118. if (!ModUtil::func('IWmain', 'user', 'checkSecurityValue', array('sv' => $sv))) {
  119. return LogUtil::registerError($this->__('You are not allowed to access to some information.'));
  120. }
  121. $table = DBUtil::getTables();
  122. $c = $table['IWusers_column'];
  123. $where = "$c[uid]=$uid";
  124. // get the objects from the db
  125. $items = DBUtil::selectObjectArray('IWusers', $where);
  126. // Check for an error with the database code, and if so set an appropriate
  127. // error message and return
  128. if ($items === false) {
  129. return LogUtil::registerError($this->__('Error! Could not load items.'));
  130. }
  131. // Return the items
  132. return $items;
  133. }
  134. /**
  135. * Get all the groups
  136. * @author: Albert Pérez Monfort (aperezm@xtec.cat)
  137. * @return: And array with the users
  138. */
  139. public function getAllGroups($args) {
  140. $sv = FormUtil::getPassedValue('sv', isset($args['sv']) ? $args['sv'] : null, 'POST');
  141. if (!ModUtil::func('IWmain', 'user', 'checkSecurityValue', array('sv' => $sv))) {
  142. return LogUtil::registerError($this->__('You are not allowed to access to some information.'));
  143. }
  144. $table = DBUtil::getTables();
  145. $c = $table['groups_column'];
  146. $orderby = "$c[name]";
  147. $items = array();
  148. // get the objects from the db
  149. $items = DBUtil::selectObjectArray('groups', '', $orderby);
  150. // Check for an error with the database code, and if so set an appropriate
  151. // error message and return
  152. if ($items === false) {
  153. return LogUtil::registerError($this->__('Error! Could not load items.'));
  154. }
  155. //print_r($items);
  156. // Return the items
  157. return $items;
  158. }
  159. /**
  160. * Get the members of a group
  161. * @author: Albert Pérez Monfort (aperezm@xtec.cat)
  162. * @return: And array with the users
  163. */
  164. public function getMembersGroup($args) {
  165. $sv = FormUtil::getPassedValue('sv', isset($args['sv']) ? $args['sv'] : null, 'POST');
  166. $gid = FormUtil::getPassedValue('gid', isset($args['gid']) ? $args['gid'] : null, 'POST');
  167. if (!ModUtil::func('IWmain', 'user', 'checkSecurityValue', array('sv' => $sv))) {
  168. return LogUtil::registerError($this->__('You are not allowed to access to some information.'));
  169. }
  170. $myJoin = array();
  171. $myJoin[] = array('join_table' => 'users',
  172. 'join_field' => array('uid'),
  173. 'object_field_name' => array('uid'),
  174. 'compare_field_table' => 'uid',
  175. 'compare_field_join' => 'uid');
  176. $myJoin[] = array('join_table' => 'group_membership',
  177. 'join_field' => array(),
  178. 'object_field_name' => array(),
  179. 'compare_field_table' => 'uid',
  180. 'compare_field_join' => 'uid');
  181. $tables = DBUtil::getTables();
  182. $ccolumn = $tables['users_column'];
  183. $ocolumn = $tables['group_membership_column'];
  184. $where = "b.$ocolumn[gid] = " . $gid;
  185. $orderBy = "ORDER BY tbl.$ccolumn[uname]";
  186. $items = DBUtil::selectExpandedObjectArray('users', $myJoin, $where, $orderBy);
  187. return $items;
  188. }
  189. /**
  190. * Get all the groups
  191. * @author: Albert Pérez Monfort (aperezm@xtec.cat)
  192. * @return: And array with the users
  193. */
  194. public function getAllGroupsInfo($args) {
  195. $sv = FormUtil::getPassedValue('sv', isset($args['sv']) ? $args['sv'] : null, 'POST');
  196. if (!ModUtil::func('IWmain', 'user', 'checkSecurityValue', array('sv' => $sv))) {
  197. return LogUtil::registerError($this->__('You are not allowed to access to some information.'));
  198. }
  199. $items = array();
  200. // get the objects from the db
  201. $items = DBUtil::selectObjectArray('groups');
  202. // Check for an error with the database code, and if so set an appropriate
  203. // error message and return
  204. if ($items === false)
  205. return LogUtil::registerError($this->__('Error! Could not load items.'));
  206. // Return the items
  207. return $items;
  208. }
  209. /**
  210. * Check if a user is member of a group
  211. * @author: Albert Pérez Monfort (aperezm@xtec.cat)
  212. * @return: True if the user is member and false otherwise
  213. */
  214. public function isMember($args) {
  215. $sv = FormUtil::getPassedValue('sv', isset($args['sv']) ? $args['sv'] : null, 'POST');
  216. $gid = FormUtil::getPassedValue('gid', isset($args['gid']) ? $args['gid'] : null, 'POST');
  217. $uid = FormUtil::getPassedValue('uid', isset($args['uid']) ? $args['uid'] : null, 'POST');
  218. if (!ModUtil::func('IWmain', 'user', 'checkSecurityValue', array('sv' => $sv))) {
  219. return LogUtil::registerError($this->__('You are not allowed to access to some information.'));
  220. }
  221. if ($uid == null || !is_numeric($uid)) {
  222. return LogUtil::registerError($this->__('Error! Could not do what you wanted. Please check your input.'));
  223. }
  224. //Check if the user is member of the group
  225. if ($gid != 0) {
  226. $items = array();
  227. $table = DBUtil::getTables();
  228. $c = $table['group_membership_column'];
  229. $where = "$c[uid]=" . $uid . " AND $c[gid]=" . $gid;
  230. // get the objects from the db
  231. $items = DBUtil::selectObjectArray('group_membership', $where);
  232. // Check for an error with the database code, and if so set an appropriate
  233. // error message and return
  234. if ($items === false)
  235. return LogUtil::registerError($this->__('Error! Could not load items.'));
  236. $isMember = (count($items) > 0) ? true : false;
  237. }else {
  238. $isMember = true;
  239. }
  240. return $isMember;
  241. }
  242. /**
  243. * Get all the groups of a user
  244. * @author: Albert Pérez Monfort (aperezm@xtec.cat)
  245. * @return: And array with the users
  246. */
  247. public function getAllUserGroups($args) {
  248. $sv = FormUtil::getPassedValue('sv', isset($args['sv']) ? $args['sv'] : null, 'POST');
  249. $uid = FormUtil::getPassedValue('uid', isset($args['uid']) ? $args['uid'] : null, 'POST');
  250. if (!ModUtil::func('IWmain', 'user', 'checkSecurityValue', array('sv' => $sv))) {
  251. return LogUtil::registerError($this->__('You are not allowed to access to some information.'));
  252. }
  253. // argument needed
  254. if ($uid == null || !is_numeric($uid))
  255. return false;
  256. $items = array();
  257. $table = DBUtil::getTables();
  258. $c = $table['group_membership_column'];
  259. $where = "$c[uid]=" . $uid;
  260. // get the objects from the db
  261. $items = DBUtil::selectObjectArray('group_membership', $where);
  262. // Check for an error with the database code, and if so set an appropriate
  263. // error message and return
  264. if ($items === false)
  265. return LogUtil::registerError($this->__('Error! Could not load items.'));
  266. // Return the items
  267. return $items;
  268. }
  269. public function getAllIcons() {
  270. $handle = opendir('modules/IWmain/images/smilies');
  271. while ($file = readdir($handle)) {
  272. $filelist[] = $file;
  273. }
  274. asort($filelist);
  275. $icons = array();
  276. while (list ($key, $file) = each($filelist)) {
  277. if ($file != '.' && $file != '..' && $file != 'index.html' && $file != '.svn' && $file != 'CVS') {
  278. $icons[] = array('imgsrc' => $file);
  279. }
  280. }
  281. return $icons;
  282. }
  283. //***************************************************************************************
  284. //
  285. // API function used to work with the database
  286. //
  287. // All this functions are call from the users managment funcions
  288. //
  289. //***************************************************************************************
  290. /**
  291. * Get an user variable associate with a module
  292. * @author: Albert Pérez Monfort (aperezm@xtec.cat)
  293. * @param: args Array with the elements:
  294. - module: module where the varible is used
  295. - name: name of the variable
  296. - uid: user id
  297. - sv: security value
  298. * @return: The value of the variable if it is find
  299. */
  300. public function userGetVar($args) {
  301. $uid = FormUtil::getPassedValue('uid', isset($args['uid']) ? $args['uid'] : null, 'POST');
  302. $module = FormUtil::getPassedValue('module', isset($args['module']) ? $args['module'] : null, 'POST');
  303. $name = FormUtil::getPassedValue('name', isset($args['name']) ? $args['name'] : null, 'POST');
  304. $sv = FormUtil::getPassedValue('sv', isset($args['sv']) ? $args['sv'] : null, 'POST');
  305. if (!ModUtil::func('IWmain', 'user', 'checkSecurityValue', array('sv' => $sv))) {
  306. return LogUtil::registerError($this->__('You are not allowed to access to some information.'));
  307. }
  308. // Argument check
  309. if ($uid == null || $module == null || $name == null) {
  310. return LogUtil::registerError($this->__('Error! Could not do what you wanted. Please check your input.'));
  311. }
  312. $table = DBUtil::getTables();
  313. $c = $table['IWmain_column'];
  314. $where = "$c[uid]=" . $uid . " AND $c[module]='" . $module . "' AND $c[name]='" . $name . "'";
  315. // get the objects from the db
  316. $items = DBUtil::selectObjectArray('IWmain', $where);
  317. // Check for an error with the database code, and if so set an appropriate
  318. // error message and return
  319. if ($items === false)
  320. return LogUtil::registerError($this->__('Error! Could not load items.'));
  321. // Return the items
  322. return $items;
  323. }
  324. /**
  325. * Check if an user variable exists
  326. * @author: Albert Pérez Monfort (aperezm@xtec.cat)
  327. * @param: args Array with the elements:
  328. - module: module where the varible is used
  329. - name: name of the variable
  330. - uid: user id
  331. - sv: security value
  332. * @return: Thue if exists and false if not
  333. */
  334. public function userVarExists($args) {
  335. $uid = FormUtil::getPassedValue('uid', isset($args['uid']) ? $args['uid'] : null, 'POST');
  336. $module = FormUtil::getPassedValue('module', isset($args['module']) ? $args['module'] : null, 'POST');
  337. $name = FormUtil::getPassedValue('name', isset($args['name']) ? $args['name'] : null, 'POST');
  338. $sv = FormUtil::getPassedValue('sv', isset($args['sv']) ? $args['sv'] : null, 'POST');
  339. if (!ModUtil::func('IWmain', 'user', 'checkSecurityValue', array('sv' => $sv))) {
  340. return LogUtil::registerError($this->__('You are not allowed to access to some information.'));
  341. }
  342. // Argument check
  343. if ($uid == null || $module == null || $name == null) {
  344. return LogUtil::registerError($this->__('Error! Could not do what you wanted. Please check your input.'));
  345. }
  346. $table = DBUtil::getTables();
  347. $c = $table['IWmain_column'];
  348. $where = "$c[uid]=" . $uid . " AND $c[module]='" . $module . "' AND $c[name]='" . $name . "'";
  349. // get the objects from the db
  350. $items = DBUtil::selectObjectArray('IWmain', $where);
  351. // Check for an error with the database code, and if so set an appropriate
  352. // error message and return
  353. if ($items === false)
  354. return LogUtil::registerError($this->__('Error! Could not load items.'));
  355. // Return true if the item exists or false if not
  356. $exists = (count($items) > 0) ? true : false;
  357. return $exists;
  358. }
  359. /**
  360. * Create an user variable associated with a module
  361. * @author: Albert Pérez Monfort (aperezm@xtec.cat)
  362. * @param: args Array with the elements:
  363. - module: module where the varible is used
  364. - name: name of the variable
  365. - lifetime: date of caducity of the variable
  366. - uid: user id
  367. - value: value for the variable
  368. - sv: security value
  369. * @return: The id of the value created
  370. */
  371. public function createUserVar($args) {
  372. $uid = FormUtil::getPassedValue('uid', isset($args['uid']) ? $args['uid'] : null, 'POST');
  373. $module = FormUtil::getPassedValue('module', isset($args['module']) ? $args['module'] : null, 'POST');
  374. $value = FormUtil::getPassedValue('value', isset($args['value']) ? $args['value'] : '', 'POST');
  375. $lifetime = FormUtil::getPassedValue('lifetime', isset($args['lifetime']) ? $args['lifetime'] : null, 'POST');
  376. $name = FormUtil::getPassedValue('name', isset($args['name']) ? $args['name'] : null, 'POST');
  377. $sv = FormUtil::getPassedValue('sv', isset($args['sv']) ? $args['sv'] : null, 'POST');
  378. if (!ModUtil::func('IWmain', 'user', 'checkSecurityValue', array('sv' => $sv))) {
  379. return LogUtil::registerError($this->__('You are not allowed to access to some information.'));
  380. }
  381. // Argument check
  382. if ($uid == null || $module == null || $name == null || $lifetime == null) {
  383. return LogUtil::registerError($this->__('Error! Could not do what you wanted. Please check your input.'));
  384. }
  385. $item = array('uid' => $uid,
  386. 'module' => $module,
  387. 'name' => $name,
  388. 'value' => $value,
  389. 'lifetime' => $lifetime);
  390. if (!DBUtil::insertObject($item, 'IWmain')) {
  391. return LogUtil::registerError($this->__('Error! Creation attempt failed.'));
  392. }
  393. // Return the id of the newly created item to the calling process
  394. return true;
  395. }
  396. /**
  397. * Update the field lifetime in users variables
  398. * @author: Albert Pérez Monfort (aperezm@xtec.cat)
  399. * @param: args Array with the elements:
  400. - module: module where the varible have to be deleted
  401. - name: name of the variable that have to be deleted (if name is .* all varibles of the user in the module are deleted)
  402. - uid: user id
  403. - sv: security value
  404. * @return: True if success
  405. */
  406. public function userUpdateGetVarTime($args) {
  407. $uid = FormUtil::getPassedValue('uid', isset($args['uid']) ? $args['uid'] : null, 'POST');
  408. $module = FormUtil::getPassedValue('module', isset($args['module']) ? $args['module'] : null, 'POST');
  409. $name = FormUtil::getPassedValue('name', isset($args['name']) ? $args['name'] : null, 'POST');
  410. $sv = FormUtil::getPassedValue('sv', isset($args['sv']) ? $args['sv'] : null, 'POST');
  411. if (!ModUtil::func('IWmain', 'user', 'checkSecurityValue', array('sv' => $sv))) {
  412. return LogUtil::registerError($this->__('You are not allowed to access to some information.'));
  413. }
  414. // Argument check
  415. if ($uid == null || $module == null || $name == null) {
  416. return LogUtil::registerError($this->__('Error! Could not do what you wanted. Please check your input.'));
  417. }
  418. $item = array('lifetime' => time() + 24 * 60 * 60 * ModUtil::getVar('IWmain', 'usersvarslife'),
  419. 'nult' => 0);
  420. $table = DBUtil::getTables();
  421. $c = $table['IWmain_column'];
  422. $where = "$c[uid]=" . $uid . " AND $c[module]='" . $module . "' AND $c[name]='" . $name . "'";
  423. if (!DBUtil::updateObject($item, 'IWmain', $where, 'mid'))
  424. return LogUtil::registerError($this->__('Error! Update attempt failed.'));
  425. // Let the calling process know that we have finished successfully
  426. return true;
  427. }
  428. /**
  429. * Update the field lifetime in users variables
  430. * @author: Albert Pérez Monfort (aperezm@xtec.cat)
  431. * @param: args Array with the elements:
  432. - module: module where the varible have to be deleted
  433. - name: name of the variable that have to be deleted (if name is .* all varibles of the user in the module are deleted)
  434. - uid: user id
  435. - sv: security value
  436. * @return: True if success
  437. */
  438. public function userUpdateNultVar($args) {
  439. $uid = FormUtil::getPassedValue('uid', isset($args['uid']) ? $args['uid'] : null, 'POST');
  440. $module = FormUtil::getPassedValue('module', isset($args['module']) ? $args['module'] : null, 'POST');
  441. $name = FormUtil::getPassedValue('name', isset($args['name']) ? $args['name'] : null, 'POST');
  442. $sv = FormUtil::getPassedValue('sv', isset($args['sv']) ? $args['sv'] : null, 'POST');
  443. if (!ModUtil::func('IWmain', 'user', 'checkSecurityValue', array('sv' => $sv))) {
  444. return LogUtil::registerError($this->__('You are not allowed to access to some information.'));
  445. }
  446. // Argument check
  447. if ($uid == null || $module == null || $name == null) {
  448. return LogUtil::registerError($this->__('Error! Could not do what you wanted. Please check your input.'));
  449. }
  450. $item = array('nult' => 1);
  451. $table = DBUtil::getTables();
  452. $c = $table['IWmain_column'];
  453. $where = "$c[uid]=" . $uid . " AND $c[module]='" . $module . "' AND $c[name]='" . $name . "'";
  454. if (!DBUtil::updateObject($item, 'IWmain', $where, 'mid')) {
  455. return LogUtil::registerError($this->__('Error! Update attempt failed.'));
  456. }
  457. // Let the calling process know that we have finished successfully
  458. return true;
  459. }
  460. /**
  461. * Update an user variable associate with a module
  462. * @author: Albert Pérez Monfort (aperezm@xtec.cat)
  463. * @param: args Array with the elements:
  464. - module: module where the varible is used
  465. - name: name of the variable
  466. - lifetime: date of caducity of the variable
  467. - uid: user id
  468. - value: value for the variable
  469. - sv: security value
  470. * @return: Thue if success
  471. */
  472. public function updateUserVar($args) {
  473. $uid = FormUtil::getPassedValue('uid', isset($args['uid']) ? $args['uid'] : null, 'POST');
  474. $module = FormUtil::getPassedValue('module', isset($args['module']) ? $args['module'] : null, 'POST');
  475. $value = FormUtil::getPassedValue('value', isset($args['value']) ? $args['value'] : null, 'POST');
  476. $lifetime = FormUtil::getPassedValue('lifetime', isset($args['lifetime']) ? $args['lifetime'] : null, 'POST');
  477. $name = FormUtil::getPassedValue('name', isset($args['name']) ? $args['name'] : null, 'POST');
  478. $sv = FormUtil::getPassedValue('sv', isset($args['sv']) ? $args['sv'] : null, 'POST');
  479. if (!ModUtil::func('IWmain', 'user', 'checkSecurityValue', array('sv' => $sv))) {
  480. return LogUtil::registerError($this->__('You are not allowed to access to some information.'));
  481. }
  482. // Argument check
  483. if ($uid == null || $module == null || $name == null || $lifetime == null) {
  484. return LogUtil::registerError($this->__('Error! Could not do what you wanted. Please check your input.'));
  485. }
  486. $item = array('value' => $value,
  487. 'lifetime' => $lifetime);
  488. $table = DBUtil::getTables();
  489. $c = $table['IWmain_column'];
  490. $where = "$c[uid]=" . $uid . " AND $c[module]='" . $module . "' AND $c[name]='" . $name . "'";
  491. if (!DBUtil::updateObject($item, 'IWmain', $where, 'mid')) {
  492. return LogUtil::registerError($this->__('Error! Update attempt failed.'));
  493. }
  494. // Let the calling process know that we have finished successfully
  495. return true;
  496. }
  497. /**
  498. * Delete the user variables that have been raised the lifetime value
  499. * @author: Albert Pérez Monfort (aperezm@xtec.cat)
  500. * @param: args Array with the elements:
  501. - sv: security value
  502. * @return: Thue if success
  503. */
  504. public function userDeleteOldVars($args) {
  505. $sv = FormUtil::getPassedValue('sv', isset($args['sv']) ? $args['sv'] : null, 'POST');
  506. if (!ModUtil::func('IWmain', 'user', 'checkSecurityValue', array('sv' => $sv))) {
  507. return LogUtil::registerError($this->__('You are not allowed to access to some information.'));
  508. }
  509. $now = time();
  510. $tables = DBUtil::getTables();
  511. $c = $tables['IWmain_column'];
  512. $where = "WHERE $c[lifetime] < '$now'";
  513. if (!DBUtil::deleteWhere('IWmain', $where)) {
  514. return LogUtil::registerError($this->__('Error! Sorry! Deletion attempt failed.'));
  515. }
  516. // Let the calling process know that we have finished successfully
  517. return true;
  518. }
  519. /**
  520. * Delete all users variables of a module
  521. * @author: Albert Pérez Monfort (aperezm@xtec.cat)
  522. * @param: args Array with the elements:
  523. - module: module where the varible is used
  524. - name: name of the variable to delete (the value .* means all the variables)
  525. - sv: security value
  526. * @return: Thue if success
  527. */
  528. public function usersVarsDelModule($args) {
  529. $module = FormUtil::getPassedValue('module', isset($args['module']) ? $args['module'] : null, 'POST');
  530. $name = FormUtil::getPassedValue('name', isset($args['name']) ? $args['name'] : null, 'POST');
  531. $sv = FormUtil::getPassedValue('sv', isset($args['sv']) ? $args['sv'] : null, 'POST');
  532. if (!ModUtil::func('IWmain', 'user', 'checkSecurityValue', array('sv' => $sv))) {
  533. return LogUtil::registerError($this->__('You are not allowed to access to some information.'));
  534. }
  535. // Argument check
  536. if ($module == null || $name == null) {
  537. return LogUtil::registerError($this->__('Error! Could not do what you wanted. Please check your input.'));
  538. }
  539. $tables = DBUtil::getTables();
  540. $c = $tables['IWmain_column'];
  541. $where = ($name == '.*') ? "WHERE $c[module] = '" . $module . "'" : "WHERE $c[module] = '" . $module . "' AND $c[name]='" . $name . "'";
  542. if (!DBUtil::deleteWhere('IWmain', $where))
  543. return LogUtil::registerError($this->__('Error! Sorry! Deletion attempt failed.'));
  544. // Let the calling process know that we have finished successfully
  545. return true;
  546. }
  547. /**
  548. * Delete the users variables of a module for an user
  549. * @author: Albert Pérez Monfort (aperezm@xtec.cat)
  550. * @param: args Array with the elements:
  551. - uid: user id
  552. - module: module where the varible is used
  553. - name: name of the variable to delete (the value .* means all the variables)
  554. - sv: security value
  555. * @return: Thue if success
  556. */
  557. public function userDelVar($args) {
  558. $uid = FormUtil::getPassedValue('uid', isset($args['uid']) ? $args['uid'] : null, 'POST');
  559. $module = FormUtil::getPassedValue('module', isset($args['module']) ? $args['module'] : null, 'POST');
  560. $name = FormUtil::getPassedValue('name', isset($args['name']) ? $args['name'] : null, 'POST');
  561. $sv = FormUtil::getPassedValue('sv', isset($args['sv']) ? $args['sv'] : null, 'POST');
  562. if (!ModUtil::func('IWmain', 'user', 'checkSecurityValue', array('sv' => $sv))) {
  563. return LogUtil::registerError($this->__('You are not allowed to access to some information.'));
  564. }
  565. // Argument check
  566. if ($module == null || $uid == null || $name == null) {
  567. return LogUtil::registerError($this->__('Error! Could not do what you wanted. Please check your input.'));
  568. }
  569. $tables = DBUtil::getTables();
  570. $c = $tables['IWmain_column'];
  571. $where = ($name == '.*') ? "WHERE $c[module] = '" . $module . "' AND $c[uid] = " . $uid : "WHERE $c[module] = '" . $module . "' AND $c[name] = '" . $name . "' AND $c[uid] = " . $uid;
  572. if (!DBUtil::deleteWhere('IWmain', $where)) {
  573. return LogUtil::registerError($this->__('Error! Sorry! Deletion attempt failed.'));
  574. }
  575. // Let the calling process know that we have finished successfully
  576. return true;
  577. }
  578. /**
  579. * Delete all the variables for a user that are temporally. The variables that have got the parameter nult in the value 1
  580. * @author: Albert Pérez Monfort (aperezm@xtec.cat)
  581. * @param: args Array with the elements:
  582. - uid: user id
  583. - sv: security value
  584. * @return: True if success and false if not
  585. */
  586. public function regenDinamicVars($args) {
  587. $uid = FormUtil::getPassedValue('uid', isset($args['uid']) ? $args['uid'] : null, 'POST');
  588. $sv = FormUtil::getPassedValue('sv', isset($args['sv']) ? $args['sv'] : null, 'POST');
  589. if (!ModUtil::func('IWmain', 'user', 'checkSecurityValue', array('sv' => $sv))) {
  590. return LogUtil::registerError($this->__('You are not allowed to access to some information.'));
  591. }
  592. // Argument check
  593. if ($uid == null) {
  594. return LogUtil::registerError($this->__('Error! Could not do what you wanted. Please check your input.'));
  595. }
  596. $tables = DBUtil::getTables();
  597. $c = $tables['IWmain_column'];
  598. $where = "WHERE $c[nult] = 1 AND $c[uid] = " . $uid;
  599. if (!DBUtil::deleteWhere('IWmain', $where))
  600. return LogUtil::registerError($this->__('Error! Sorry! Deletion attempt failed.'));
  601. // Let the calling process know that we have finished successfully
  602. return true;
  603. }
  604. //***************************************************************************************
  605. //
  606. // END - API function used to work with the database
  607. //
  608. //***************************************************************************************
  609. //***************************************************************************************
  610. //
  611. // Logs system functions
  612. //
  613. //
  614. //***************************************************************************************
  615. /**
  616. * Create a log of an action
  617. *
  618. * Parameters passed in the $args array:
  619. * -------------------------------------
  620. *
  621. * string $args['actionText'] Text for the log
  622. * integer $args['visible'] Visibility for users // 1 - all users / 0 - only administrators (optional - default 1)
  623. * string $args['moduleName'] Name of the module that has generated the log (optional)
  624. * integer $args['actionType'] Type of action logged // 1 - insert / 2 - update / 3 - Delete / 4 - select (optinal)
  625. *
  626. * @param array $args All parameters passed to this function.
  627. *
  628. * @return integer identity of the log created, or false on failure.
  629. *
  630. * @throws Zikula_Exception_Fatal Thrown if invalid parameters are received in $args, or if the data cannot be loaded from the database.
  631. *
  632. * @throws Zikula_Exception_Forbidden Thrown if the current user does not have overview access.
  633. */
  634. public function saveLog($args) {
  635. if (!ModUtil::func('IWmain', 'user', 'checkSecurityValue', array('sv' => $args['sv']))) {
  636. throw new Zikula_Exception_Forbidden("You are not allowed to access to some information.");
  637. }
  638. if (!isset($args['actionText']) || $args['actionText'] == '') {
  639. throw new Zikula_Exception_Fatal(LogUtil::getErrorMsgArgs());
  640. }
  641. $visible = (!isset($args['visible'])) ? 1 : $args['visible'];
  642. $error = (!isset($args['error'])) ? 0 : $args['error'];
  643. $ip = '';
  644. if (!empty($_SERVER['REMOTE_ADDR'])) {
  645. $ip = ModUtil::apiFunc('IWmain', 'user', 'cleanremoteaddr', array('originaladdr' => $_SERVER['REMOTE_ADDR']));
  646. }
  647. if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  648. $ip = ModUtil::apiFunc('IWmain', 'user', 'cleanremoteaddr', array('originaladdr' => $_SERVER['HTTP_X_FORWARDED_FOR']));
  649. }
  650. if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
  651. $ip = ModUtil::apiFunc('IWmain', 'user', 'cleanremoteaddr', array('originaladdr' => $_SERVER['HTTP_CLIENT_IP']));
  652. }
  653. $item = array('moduleName' => $args['moduleName'],
  654. 'actionType' => $args['actionType'],
  655. 'visible' => $visible,
  656. 'actionText' => $args['actionText'],
  657. 'logIp' => $ip,
  658. 'indexName' => $args['indexName'],
  659. 'indexValue' => $args['indexValue'],
  660. 'indexName1' => $args['indexName1'],
  661. 'indexValue1' => $args['indexValue1'],
  662. 'error' => $error,
  663. );
  664. if (!DBUtil::insertObject($item, 'IWmain_logs', 'logId')) {
  665. throw new Zikula_Exception_Fatal(LogUtil::getErrorMsgArgs());
  666. }
  667. // Return the id of the newly created item to the calling process
  668. return $item['logId'];
  669. }
  670. public function cleanremoteaddr($args) {
  671. $originaladdr = $args['originaladdr'];
  672. $matches = array();
  673. // first get all things that look like IP addresses.
  674. if (!preg_match_all('/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/', $args['originaladdr'], $matches, PREG_SET_ORDER)) {
  675. return '';
  676. }
  677. $goodmatches = array();
  678. $lanmatches = array();
  679. foreach ($matches as $match) {
  680. // check to make sure it's not an internal address.
  681. // the following are reserved for private lans...
  682. // 10.0.0.0 - 10.255.255.255
  683. // 172.16.0.0 - 172.31.255.255
  684. // 192.168.0.0 - 192.168.255.255
  685. // 169.254.0.0 -169.254.255.255
  686. $bits = explode('.', $match[0]);
  687. if (count($bits) != 4) {
  688. // weird, preg match shouldn't give us it.
  689. continue;
  690. }
  691. if (($bits[0] == 10)
  692. || ($bits[0] == 172 && $bits[1] >= 16 && $bits[1] <= 31)
  693. || ($bits[0] == 192 && $bits[1] == 168)
  694. || ($bits[0] == 169 && $bits[1] == 254)) {
  695. $lanmatches[] = $match[0];
  696. continue;
  697. }
  698. // finally, it's ok
  699. $goodmatches[] = $match[0];
  700. }
  701. if (!count($goodmatches)) {
  702. // perhaps we have a lan match, it's probably better to return that.
  703. if (!count($lanmatches)) {
  704. return '';
  705. } else {
  706. return array_pop($lanmatches);
  707. }
  708. }
  709. if (count($goodmatches) == 1) {
  710. return $goodmatches[0];
  711. }
  712. // We need to return something, so return the first
  713. return array_pop($goodmatches);
  714. }
  715. // get saved logs depending on different options
  716. public function getLogs($args) {
  717. if (!ModUtil::func('IWmain', 'user', 'checkSecurityValue', array('sv' => $args['sv']))) {
  718. throw new Zikula_Exception_Forbidden("You are not allowed to access to some information.");
  719. }
  720. $init = (isset($args['init'])) ? $args['init'] : '-1';
  721. $rpp = (isset($args['rpp'])) ? $args['rpp'] : '-1';
  722. $table = DBUtil::getTables();
  723. $where = "";
  724. $c = $table['IWmain_logs_column'];
  725. if (isset($args['moduleName'])) {
  726. $where = "$c[moduleName] = '$args[moduleName]'";
  727. }
  728. if (isset($args['indexName']) && $args['indexName'] != '' && isset($args['indexValue']) && $args['indexValue'] > 0) {
  729. $and = ($where != '') ? ' AND ' : '';
  730. $where .= $and . "$c[indexName] = '$args[indexName]' AND $c[indexValue] = $args[indexValue]";
  731. }
  732. if (isset($args['indexName1']) && $args['indexName1'] != '' && isset($args['indexValue1']) && $args['indexValue1'] > 0) {
  733. $and = ($where != '') ? ' AND ' : '';
  734. $where .= $and . "$c[indexName1] = '$args[indexName1]' AND $c[indexValue1] = $args[indexValue1]";
  735. }
  736. if (isset($args['visible']) && $args['visible'] != '') {
  737. $and = ($where != '') ? ' AND ' : '';
  738. $where .= $and . "$c[visible] = '$args[visible]'";
  739. }
  740. $order = (isset($args['order'])) ? $args['order'] : '';
  741. $orderby = "$c[logId] $order";
  742. if (isset($args['onlyNumber']) && $args['onlyNumber'] == 1) {
  743. $items = DBUtil::selectObjectCount('IWmain_logs', $where);
  744. } else {
  745. // get the objects from the db
  746. $items = DBUtil::selectObjectArray('IWmain_logs', $where, $orderby, $init, $rpp, 'logId');
  747. }
  748. // Check for an error with the database code, and if so set an appropriate
  749. // error message and return
  750. if ($items === false)
  751. return LogUtil::registerError($this->__('Error! Could not load items.'));
  752. // Return the items
  753. return $items;
  754. }
  755. function deleteLog($args) {
  756. if (!ModUtil::func('IWmain', 'user', 'checkSecurityValue', array('sv' => $args['sv']))) {
  757. throw new Zikula_Exception_Forbidden("You are not allowed to access to some information.");
  758. }
  759. $table = DBUtil::getTables();
  760. $c = $table['IWmain_logs_column'];
  761. $where = "$c[moduleName]='$args[moduleName]' AND $c[indexName]='$args[indexName]' AND $c[indexValue]=$args[indexValue]";
  762. if (!DBUtil::deleteWhere('IWmain_logs', $where)) {
  763. return LogUtil::registerError($this->__('Error! Sorry! Deletion attempt failed.'));
  764. }
  765. return true;
  766. }
  767. //***************************************************************************************
  768. //
  769. // END - Logs system functions
  770. //
  771. //***************************************************************************************
  772. }