PageRenderTime 64ms CodeModel.GetById 38ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/FrontEndUsers/lib/class.FEUForcedLogoutTask.php

https://github.com/VesQ/FEUsignUp
PHP | 161 lines | 98 code | 21 blank | 42 comment | 24 complexity | 96bf05ace22310be70e1c645cbb7ea76 MD5 | raw file
  1. <?php
  2. #BEGIN_LICENSE
  3. #-------------------------------------------------------------------------
  4. # Module: FrontEndUsers (c) 2008 by Robert Campbell
  5. # (calguy1000@cmsmadesimple.org)
  6. # An addon module for CMS Made Simple to allow management of frontend
  7. # users, and their login process within a CMS Made Simple powered
  8. # website.
  9. #
  10. #-------------------------------------------------------------------------
  11. # CMS - CMS Made Simple is (c) 2005 by Ted Kulp (wishy@cmsmadesimple.org)
  12. # This project's homepage is: http://www.cmsmadesimple.org
  13. #
  14. #-------------------------------------------------------------------------
  15. #
  16. # This program is free software; you can redistribute it and/or modify
  17. # it under the terms of the GNU General Public License as published by
  18. # the Free Software Foundation; either version 2 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # However, as a special exception to the GPL, this software is distributed
  22. # as an addon module to CMS Made Simple. You may not use this software
  23. # in any Non GPL version of CMS Made simple, or in any version of CMS
  24. # Made simple that does not indicate clearly and obviously in its admin
  25. # section that the site was built with CMS Made simple.
  26. #
  27. # This program is distributed in the hope that it will be useful,
  28. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. # GNU General Public License for more details.
  31. # You should have received a copy of the GNU General Public License
  32. # along with this program; if not, write to the Free Software
  33. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  34. # Or read it online: http://www.gnu.org/licenses/licenses.html#GPL
  35. #
  36. #-------------------------------------------------------------------------
  37. #END_LICENSE
  38. class FEUForcedLogoutTask implements CmsRegularTask
  39. {
  40. public function get_name()
  41. {
  42. return get_class();
  43. }
  44. public function get_description()
  45. {
  46. $mod = cms_utils::get_module('FrontEndUsers');
  47. return $mod->Lang('forcedlogouttask_desc');
  48. }
  49. public function test($time = '')
  50. {
  51. if( !$time ) $time = time();
  52. $mod = cms_utils::get_module('FrontEndUsers');
  53. $lastrun = $mod->GetPreference('forcelogout_lastrun');
  54. $times = $mod->GetPreference('forcelogout_times'); // like: 18:30,20:30,08:00, etc.
  55. if( $times == '' ) return FALSE;
  56. $tmp = array();
  57. $times = explode(',',$times);
  58. for( $i = 0; $i < count($times); $i++ )
  59. {
  60. $t_h = '';
  61. $t_m = '';
  62. if( startswith($times[$i],'*') )
  63. {
  64. list($junk,$minutes) = explode('/',$times[$i],2);
  65. $minutes = (int)$minutes;
  66. $minutes = max($minutes,1); // minumum 2 minutes
  67. $minutes = min($minutes,180); // maximum 180 minutes.
  68. for( $i = 0; $i < 60*24; $i += $minutes )
  69. {
  70. $t_h = (int)($i/60);
  71. $t_m = (int)($i%60);
  72. $tmp[] = mktime((int)$t_h,(int)$t_m,0,(int)date('m',$time),(int)date('d',$time),(int)date('Y',$time));
  73. }
  74. }
  75. else
  76. {
  77. list($t_h,$t_m) = explode(':',trim($times[$i]),2);
  78. }
  79. }
  80. rsort($tmp);
  81. $fi = '';
  82. $ft = '';
  83. // find the closest entry.
  84. $db = cmsms()->GetDb();
  85. for( $i = 0; $i < count($tmp); $i++ )
  86. {
  87. if( $time > $tmp[$i] ) break; // nothing to do..
  88. $dt = $tmp[$i] - $time;
  89. if( $ft == '' || $dt < ($ft - $time) )
  90. {
  91. $fi = $i;
  92. $ft = $tmp[$i];
  93. }
  94. }
  95. if( $ft != '' && $fi != '' && ($time - $ft) < 60 )
  96. {
  97. // we found something that can execute.
  98. $_SESSION[get_class().'_runtime'] = $tmp[$i];
  99. return TRUE;
  100. }
  101. return FALSE;
  102. }
  103. public function execute($time = '')
  104. {
  105. if( !$time ) $time = time();
  106. if( !isset($_SESSION[get_class().'_runtime']) ) return FALSE;
  107. $the_time = $_SESSION[get_class().'_runtime'];
  108. unset($_SESSION[get_class().'_runtime']);
  109. // now find all the uid's that are logged in but not active since $the_time
  110. $feu = cms_utils::get_module('FrontEndUsers');
  111. $forgiveness = (int)$feu->GetPreference('forcelogout_sessionage');
  112. $forgiveness = max(0,$forgiveness)*60;
  113. $the_time -= $forgiveness;
  114. $uids = $feu->GetLoggedInUsers($the_time);
  115. if( !is_array($uids) || count($uids) == 0 )
  116. {
  117. return TRUE; // nothing to do.
  118. }
  119. for( $i = 0; $i < count($uids); $i++ )
  120. {
  121. $feu->Logout($uids[$i],'forced logout');
  122. }
  123. $feu->Audit('',$feu->GetName(),'Forced logout of '.count($uids).' users at '.strftime('%H:%M',$the_time));
  124. return TRUE;
  125. }
  126. public function on_success($time = '')
  127. {
  128. if( !$time ) $time = time();
  129. $feu = cms_utils::get_module('FrontEndUsers');
  130. $feu->SetPreference('forcelogout_lastrun',$time);
  131. }
  132. public function on_failure($time = '')
  133. {
  134. if( !$time ) $time = time();
  135. }
  136. } // end of class
  137. #
  138. # EOF
  139. #
  140. ?>