api.idlelogout.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * Basic user-idle auto logout class
  4. */
  5. class AutoLogout {
  6. const LOGOUT_URL='?idleTimerAutoLogout=true';
  7. /**
  8. * returns logout dialog localised template
  9. *
  10. * @return string
  11. */
  12. protected function createDialog() {
  13. $autoLogoutTimerContainer= wf_tag('div', false, '', 'id="idledialog" title="'.__('Your session is about to expire!').'"');
  14. $autoLogoutTimerContainer.= wf_tag('span', false, 'ui-icon ui-icon-alert', 'style="float:left; margin:0 7px 50px 0;"').wf_tag('span', true);
  15. $autoLogoutTimerContainer.= __('You will be logged off in').' ';
  16. $autoLogoutTimerContainer.= wf_tag('span', false, '', 'id="dialog-countdown" style="font-weight:bold"').wf_tag('span', true);
  17. $autoLogoutTimerContainer.= ' '. __('seconds'). wf_delimiter();
  18. $autoLogoutTimerContainer.= wf_tag('center',false). wf_tag('img', false, '', 'src="skins/idleicon.gif" width="160"'). wf_tag('center',true);
  19. $autoLogoutTimerContainer.= wf_tag('div', true);
  20. return ($autoLogoutTimerContainer);
  21. }
  22. /**
  23. * returns JQuery subroutine for auto-logout
  24. *
  25. * @return string
  26. */
  27. protected function createTimer($idle) {
  28. $idle=$idle*60;
  29. $autoLogoutTimerJs='
  30. <script type="text/javascript">
  31. // setup the dialog
  32. $("#idledialog").dialog({
  33. autoOpen: false,
  34. modal: true,
  35. width: 400,
  36. height: 300,
  37. closeOnEscape: false,
  38. draggable: false,
  39. resizable: false,
  40. buttons: {
  41. \''.__('Yes, keep working').'\': function(){
  42. $(this).dialog(\'close\');
  43. },
  44. \''.__('No, logout').'\': function(){
  45. $.idleTimeout.options.onTimeout.call(this);
  46. }
  47. }
  48. });
  49. // cache a reference to the countdown element so we don\'t have to query the DOM for it on each ping.
  50. var $countdown = $("#dialog-countdown");
  51. // start the idle timer plugin
  52. $.idleTimeout(\'#idledialog\', \'div.ui-dialog-buttonpane button:first\', {
  53. idleAfter: '.$idle.',
  54. pollingInterval: 50,
  55. keepAliveURL: \'RELEASE\',
  56. serverResponseEquals: \'OK\',
  57. onTimeout: function(){
  58. window.location = "'.self::LOGOUT_URL.'";
  59. },
  60. onIdle: function(){
  61. $(this).dialog("open");
  62. },
  63. onCountdown: function(counter){
  64. $countdown.html(counter); // update the counter
  65. }
  66. });
  67. </script>
  68. ';
  69. return ($autoLogoutTimerJs);
  70. }
  71. /**
  72. * renders idle timeout auto logout scripts id needed
  73. *
  74. * @return string
  75. */
  76. public function render() {
  77. global $ubillingConfig;
  78. $altCfg=$ubillingConfig->getAlter();
  79. $excludeAdmins=array();
  80. $result='';
  81. if (isset($altCfg['AUTO_LOGOUT_IDLE'])) {
  82. if ($altCfg['AUTO_LOGOUT_IDLE']) {
  83. $myLogin=whoami();
  84. $idleTimeout=$altCfg['AUTO_LOGOUT_IDLE'];
  85. if (file_exists(USERS_PATH.$myLogin)) {
  86. if (isset($altCfg['AUTO_LOGOUT_EXCLUDE'])) {
  87. //extract exclude admins
  88. if (!empty($altCfg['AUTO_LOGOUT_EXCLUDE'])) {
  89. $excludeAdmins= explode(',', $altCfg['AUTO_LOGOUT_EXCLUDE']);
  90. $excludeAdmins= array_flip($excludeAdmins);
  91. }
  92. //push timer script
  93. if (!isset($excludeAdmins[$myLogin])) {
  94. $result=$this->createDialog();
  95. $result.= $this->createTimer($altCfg['AUTO_LOGOUT_IDLE']);
  96. }
  97. }
  98. }
  99. }
  100. }
  101. return ($result);
  102. }
  103. }
  104. /**
  105. * run idle auto logout object into template
  106. *
  107. * @return string
  108. */
  109. function zb_IdleAutologoutRun() {
  110. $result='';
  111. if (LOGGED_IN) {
  112. $idleTimer=new AutoLogout();
  113. $result=$idleTimer->render();
  114. }
  115. return($result);
  116. }
  117. ?>