Cheetah
ChWsbCronCmd.php
Go to the documentation of this file.
1 <?php
2 
8 require_once(CH_DIRECTORY_PATH_INC . 'db.inc.php');
9 require_once(CH_DIRECTORY_PATH_INC . 'admin.inc.php');
10 require_once(CH_DIRECTORY_PATH_INC . 'membership_levels.inc.php');
11 require_once('ChWsbCron.php');
12 
13 class ChWsbCronCmd extends ChWsbCron
14 {
15  // - Functions -
16 
17  public function finish()
18  {
19  global $site;
20  global $MODE;
21 
22  if ($MODE != "_LIVE_") {
23  $output = ob_get_contents();
24  ob_end_clean();
25 
26  if ($MODE == "_MAIL_" && $output) {
27  sendMail($site['email'], "{$site['title']}: Periodic Report", $output, 0, array(), 'text');
28  }
29  }
30  }
31 
32  public function clean_database()
33  {
34  $db_clean_vkiss = (int) getParam("db_clean_vkiss");
35  $db_clean_profiles = (int) getParam("db_clean_profiles");
36  $db_clean_msg = (int) getParam("db_clean_msg");
37  $db_clean_visits = (int) getParam("db_clean_members_visits");
38  $db_clean_banners_info = (int) getParam("db_clean_banners_info");
39  $db_clean_mem_levels = (int) getParam("db_clean_mem_levels");
40  $db_clean_email_log = (int) getParam("email_log_ttl_days");
41 
42  //clear from `sys_acl_levels_members`
43  if (db_res("DELETE FROM `sys_acl_levels_members` WHERE `DateExpires` < NOW() - INTERVAL $db_clean_mem_levels DAY")) {
44  db_res("OPTIMIZE TABLE `sys_acl_levels_members`");
45  }
46 
47  //clear from `sys_banners_shows`
48  if (db_res("DELETE FROM `sys_banners_shows` WHERE `Date` < UNIX_TIMESTAMP( NOW() - INTERVAL $db_clean_banners_info DAY )")) {
49  db_res("OPTIMIZE TABLE `sys_banners_shows`");
50  }
51 
52  //clear from `sys_banners_clicks`
53  if (db_res("DELETE FROM `sys_banners_clicks` WHERE `Date` < UNIX_TIMESTAMP( NOW() - INTERVAL $db_clean_banners_info DAY )")) {
54  db_res("OPTIMIZE TABLE `sys_banners_clicks`");
55  }
56 
57  // clear from `sys_messages`
58  if (db_res("DELETE FROM `sys_messages` WHERE FIND_IN_SET('sender', `Trash`) AND FIND_IN_SET('recipient', `Trash`)")) {
59  db_res("OPTIMIZE TABLE `sys_messages`");
60  }
61 
62  //clear from `sys_ip_members_visits`
63  if (db_res("DELETE FROM `sys_ip_members_visits` WHERE `DateTime` < NOW() - INTERVAL $db_clean_visits DAY")) {
64  db_res("OPTIMIZE TABLE `sys_ip_members_visits`");
65  }
66 
67  // clear ban table
68  if (db_res("DELETE FROM `sys_admin_ban_list` WHERE `DateTime` + INTERVAL `Time` SECOND < NOW()")) {
69  db_res("OPTIMIZE TABLE `sys_admin_ban_list`");
70  }
71 
72  //clear from `sys_email_log`
73  if ($db_clean_email_log > 0) {
74  if (db_res("DELETE FROM `sys_email_log` WHERE `timestamp` < NOW() - INTERVAL $db_clean_email_log DAY")) {
75  db_res("OPTIMIZE TABLE `sys_email_log`");
76  }
77  }
78 
79  // profile_delete
80  if ($db_clean_profiles > 0) {
81  $res = db_res("SELECT `ID` FROM `Profiles` WHERE ((`DateLastNav` <> '0000-00-00 00:00:00' AND `DateLastNav` < NOW() - INTERVAL $db_clean_profiles DAY) OR (`DateLastNav` = '0000-00-00 00:00:00' AND `DateReg` < NOW() - INTERVAL $db_clean_profiles DAY)) AND (`Couple` > `ID` OR `Couple` = 0)");
82  if ($res) {
83  $db_clean_profiles_num = $res->rowCount();
84  while ($arr = $res->fetch()) {
85  profile_delete($arr['ID']);
86  }
87  db_res("OPTIMIZE TABLE `Profiles`");
88  }
89  }
90 
91  if ($db_clean_vkiss > 0) {
92  $res = db_res("DELETE FROM `sys_greetings` WHERE `When` < NOW() - INTERVAL $db_clean_vkiss DAY");
93  if ($res) {
94  $db_clean_vkiss_num = db_affected_rows($res);
95  db_res("OPTIMIZE TABLE `sys_greetings`");
96  }
97  }
98 
99  if ($db_clean_msg > 0) {
100  $res = db_res("DELETE FROM `sys_messages` WHERE `Date` < NOW() - INTERVAL $db_clean_msg DAY");
101  if ($res) {
102  $db_clean_msg_num = db_affected_rows($res);
103  db_res("OPTIMIZE TABLE `sys_messages`");
104  }
105  }
106 
107  //--- Clean sessions ---//
108  ch_import('ChWsbSession');
109  $oSession = ChWsbSession::getInstance();
110  $iSessions = $oSession->oDb->deleteExpired();
111 
112  // clean expired ip bans
113  ch_import('ChWsbAdminIpBlockList');
115  $iIps = $oChWsbAdminIpBlockList->deleteExpired();
116 
117  // clean old views
118  ch_import('ChWsbViews');
119  $oChViews = new ChWsbViews('', 0);
120  $iDeletedViews = $oChViews->maintenance();
121 
122  // clean old votes
123  ch_import('ChWsbVoting');
124  $oChVotes = new ChWsbVoting('', 0);
125  $iDeletedVotes = $oChVotes->maintenance();
126 
127  // clean comments ratings
128  ch_import('ChWsbCmts');
129  $oChCmts = new ChWsbCmts('', 0);
130  $iDeletedCommentVotes = $oChCmts->maintenance();
131 
132  echo "\n- Database cleaning -\n";
133  echo "Deleted profiles: $db_clean_profiles_num\n";
134  echo "Deleted virtual kisses: $db_clean_vkiss_num\n";
135  echo "Deleted messages: $db_clean_msg_num\n";
136  echo "Deleted sessions: $iSessions\n";
137  echo "Deleted records from ip block list: $iIps\n";
138  echo "Deleted views: $iDeletedViews\n";
139  echo "Deleted votes: $iDeletedVotes\n";
140  echo "Deleted comment votes: $iDeletedCommentVotes\n";
141  }
142 
143  public function del_old_all_files()
144  {
145  global $dir;
146 
147  $num_tmp = 0;
148  $num_del = 0;
149 
150  $file_life = 86400; // one day
151  $dirToClean = array();
152  $dirToClean[$dir['tmp']] = "/.*/";
153  $dirToClean[$dir['cache']] = "/.*/";
154  $dirToClean[CH_DIRECTORY_PATH_CACHE_PUBLIC] = "/^export/";
155 
156  foreach ($dirToClean as $value => $sRegExp) {
157  if (!($lang_dir = opendir($value))) {
158  continue;
159  } else {
160  while ($lang_file = readdir($lang_dir)) {
161  if (!preg_match($sRegExp, $lang_file)) {
162  continue;
163  }
164  $diff = time() - filectime($value . $lang_file);
165  if ($diff > $file_life && '.' != $lang_file && '..' != $lang_file && '.htaccess' !== $lang_file) {
166  @unlink($value . $lang_file);
167  ++$num_del;
168  }
169  ++$num_tmp;
170  }
171  closedir($lang_dir);
172  }
173  }
174 
175  echo "\n- Temporary files check -\n";
176 
177  echo "Total temp files: $num_tmp\n";
178  echo "Deleted temp files: $num_del\n";
179  }
180 
181  public function processAdminStats()
182  {
183  $iCounter = 0;
184  $sMessage = "";
185 
186  $aStats = getSiteStatArray();
187  foreach ($aStats as $sName => $aStat) {
188  if (empty($aStat['adm_query'])) {
189  continue;
190  }
191 
192  $iNumber = (int)$GLOBALS['MySQL']->getOne($aStat['adm_query']);
193  if ($iNumber <= 0) {
194  continue;
195  }
196 
197  $sMessage .= '<p>' . $iNumber . ' ' . _t('_' . $aStat['capt'] . '_adm_stats') . ',</p>';
198  }
199 
200  $aAdmins = $GLOBALS['MySQL']->getAll("SELECT * FROM `Profiles` WHERE `Role`&" . CH_WSB_ROLE_ADMIN . "<>0 AND `EmailNotify`='1'");
201  if (!empty($sMessage) && !empty($aAdmins)) {
202  $oEmailTemplate = new ChWsbEmailTemplates();
203  $aEmailParams = array(
204  'MessageText' => $sMessage,
205  'ViewLink' => CH_WSB_URL_ADMIN
206  );
207 
208  foreach ($aAdmins as $aAdmin) {
209  $aMail = $oEmailTemplate->parseTemplate('t_AdminStats', $aEmailParams, $aAdmin['ID']);
210  if (sendMail($aAdmin['Email'], $aMail['subject'], $aMail['body'], $aAdmin['ID'], array(), 'html', false, true)) {
211  $iCounter++;
212  }
213  }
214  }
215 
216  return $iCounter;
217  }
218 
219  public function processing()
220  {
221  global $MODE;
222 
223  // - Defaults -
224  $MODE = "_MAIL_";
225  //$MODE = "_LIVE_";
226  $DAY = "_OBEY_";
227  //$DAY = "_FORCE_";
228  define('NON_VISUAL_PROCESSING', 'YES');
229 
230  // - Always finish
231  set_time_limit(36000);
232  ignore_user_abort();
233 
234  // - Parameters check -
235  for ($i = 0; strlen($argv[$i]); $i++) {
236  switch ($argv[$i]) {
237  case "--live": $MODE = "_LIVE_"; break;
238  case "--mail": $MODE = "_MAIL_"; break;
239  case "--force-day": $DAY = "_FORCE_"; break;
240  case "--obey-day": $DAY = "_OBEY_"; break;
241  }
242  }
243 
244  if ($MODE != "_LIVE_") {
245  ob_start();
246  }
247 
248  $day = date("d");
249  if (getParam("cmdDay") == $day && $DAY == "_OBEY_") {
250  echo "Already done today, bailing out\n";
251  $this->finish();
252  return;
253  }
254 
255  setParam("cmdDay", $day);
256 
257  //========================================================================================================================
258 
259  // - License Check.
260  echo "Checking Cheetah License\n";
261  chCheckLicense();
262 
263  // - Send Admin Stats notification
264  $this->processAdminStats();
265 
266  // - Membership check -
268 
269  echo "\n- Membership expiration letters -\n";
270 
271  $iExpireNotificationDays = (int)getParam("expire_notification_days");
272  $bExpireNotifyOnce = getParam("expire_notify_once") == 'on';
273 
274  $iExpireLetters = 0;
275 
276  $aRow = $GLOBALS['MySQL']->getFirstRow("SELECT `ID` FROM `Profiles`");
277  while (!empty($aRow)) {
278  $aCurrentMem = getMemberMembershipInfo($aRow['ID']);
279  // If expire_notification_days is -1 then notify after expiration
280  if ($aCurrentMem['ID'] == MEMBERSHIP_ID_STANDARD && $iExpireNotificationDays == -1) {
281  // Calculate last UNIX Timestamp
282  $iLastTimestamp = time() - 24 * 3600;
283  $aLastMem = getMemberMembershipInfo($aRow['ID'], $iLastTimestamp);
284  if ($aCurrentMem['ID'] != $aLastMem['ID']) {
285  $bMailResult = mem_expiration_letter($aRow['ID'], $aLastMem['Name'], -1);
286  if ($bMailResult) {
287  $iExpireLetters++;
288  }
289  }
290  }
291  // If memberhip is not standard then check if it will change
292  elseif ($aCurrentMem['ID'] != MEMBERSHIP_ID_STANDARD) {
293  // Calculate further UNIX Timestamp for expiration check
294  $iFurtherTimestamp = time() + 24 * 3600;
295  $aFurtherMem = getMemberMembershipInfo($aRow['ID'], $iFurtherTimestamp);
296  if ($aCurrentMem['ID'] != $aFurtherMem['ID'] && $aFurtherMem['ID'] == MEMBERSHIP_ID_STANDARD) {
297  markMembershipAsExpiring($aRow['ID'], $aCurrentMem['ID'], $aCurrentMem['TransactionID']);
298  }
299 
300  // Calculate further UNIX Timestamp for notification letter
301  $iFurtherTimestamp = time() + $iExpireNotificationDays * 24 * 3600;
302  $aFurtherMem = getMemberMembershipInfo($aRow['ID'], $iFurtherTimestamp);
303  if ($aCurrentMem['ID'] != $aFurtherMem['ID'] && $aFurtherMem['ID'] == MEMBERSHIP_ID_STANDARD) {
304  if (!$bExpireNotifyOnce || abs($iFurtherTimestamp - $aCurrentMem['DateExpires']) < 24 * 3600) {
305  $bMailResult = mem_expiration_letter($aRow['ID'], $aCurrentMem['Name'], (int)(($aCurrentMem['DateExpires'] - time())/(24 * 3600)));
306  if ($bMailResult) {
307  $iExpireLetters++;
308  }
309  }
310  }
311  }
312 
313  $aRow = $GLOBALS['MySQL']->getNextRow();
314  }
315 
316  echo "Send membership expire letters: $iExpireLetters letters\n";
317 
318  //========================================================================================================================
319 
320  // clear tmp folder --------------------------------------------------------------------------
321 
322  $this->del_old_all_files();
323 
324  // ----------------------------------------------------------------------------------
325  $this->clean_database();
326 
327  $this->finish();
328  }
329 }
ChWsbCronCmd\processAdminStats
processAdminStats()
Definition: ChWsbCronCmd.php:181
ChWsbCronCmd\finish
finish()
Definition: ChWsbCronCmd.php:17
$sMessage
$sMessage
Definition: actions.inc.php:17
$oChWsbAdminIpBlockList
$oChWsbAdminIpBlockList
Definition: ip_blacklist.php:18
ChWsbEmailTemplates
Definition: ChWsbEmailTemplates.php:11
ch_import
ch_import($sClassName, $aModule=array())
Definition: utils.inc.php:1218
ChWsbCron
Definition: ChWsbCron.php:76
sendMail
sendMail( $sRecipientEmail, $sMailSubject, $sMailBody, $iRecipientID=0, $aPlus=array(), $sEmailFlag='html', $isDisableAlert=false, $bForceSend=false)
Definition: utils.inc.php:461
php
ChWsbVoting
Definition: ChWsbVoting.php:90
profile_delete
profile_delete($ID, $isDeleteSpammer=false)
Definition: admin.inc.php:316
unmarkMembershipAsExpiringAll
unmarkMembershipAsExpiringAll()
Definition: membership_levels.inc.php:798
MEMBERSHIP_ID_STANDARD
const MEMBERSHIP_ID_STANDARD
Definition: membership_levels.inc.php:49
ChWsbCronCmd\del_old_all_files
del_old_all_files()
Definition: ChWsbCronCmd.php:143
mem_expiration_letter
mem_expiration_letter( $ID, $membership_name, $expire_days)
Definition: admin.inc.php:110
ChWsbCmts
Definition: ChWsbCmts.php:99
getParam
getParam($sParamName, $bUseCache=true)
Definition: db.inc.php:130
ChWsbCronCmd\clean_database
clean_database()
Definition: ChWsbCronCmd.php:32
ChWsbCronCmd
Definition: ChWsbCronCmd.php:14
$site
$site['ver']
Definition: version.inc.php:8
ChWsbViews
Definition: ChWsbViews.php:38
global
if(!defined("GLOBAL_MODULE")) define("GLOBAL_MODULE" global
Definition: header.inc.php:25
_t
_t($key, $arg0="", $arg1="", $arg2="")
Definition: languages.inc.php:509
time
that in the case of a Adaptation or at a minimum such credit will if a credit for all contributing authors of the Adaptation or Collection then as part of these credits and in a manner at least as prominent as the credits for the other contributing authors For the avoidance of You may only use the credit required by this Section for the purpose of attribution in the manner set out above by exercising Your rights under this You may not implicitly or explicitly assert or imply any connection sponsorship or endorsement by the Original Licensor and or Attribution as of You or Your use of the without the express prior written permission of the Original Licensor and or Attribution Parties Except as otherwise agreed in writing by the Licensor or as may be otherwise permitted by applicable if You Distribute or Publicly Perform the Work either by itself or as part of any Adaptations or You must not modify or take other derogatory action in relation to the Work which would be prejudicial to the Original Author s honor or reputation Licensor agrees that in those in which any exercise of the right granted in modification or other derogatory action prejudicial to the Original Author s honor and the Licensor will waive or not as this to the fullest extent permitted by the applicable national to enable You to reasonably exercise Your right under Warranties and Disclaimer UNLESS OTHERWISE MUTUALLY AGREED TO BY THE PARTIES IN LICENSOR OFFERS THE WORK AS IS AND MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND CONCERNING THE STATUTORY OR WITHOUT WARRANTIES OF FITNESS FOR A PARTICULAR OR THE ABSENCE OF LATENT OR OTHER OR THE PRESENCE OF ABSENCE OF WHETHER OR NOT DISCOVERABLE SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF IMPLIED SO SUCH EXCLUSION MAY NOT APPLY TO YOU Limitation on Liability EXCEPT TO THE EXTENT REQUIRED BY APPLICABLE IN NO EVENT WILL LICENSOR BE LIABLE TO YOU ON ANY LEGAL THEORY FOR ANY PUNITIVE OR EXEMPLARY DAMAGES ARISING OUT OF THIS LICENSE OR THE USE OF THE EVEN IF LICENSOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES Termination This License and the rights granted hereunder will terminate automatically upon any breach by You of the terms of this License Individuals or entities who have received Adaptations or Collections from You under this will not have their licenses terminated provided such individuals or entities remain in full compliance with those licenses and will survive any termination of this License Subject to the above terms and the license granted here is Licensor reserves the right to release the Work under different license terms or to stop distributing the Work at any time
Definition: license.txt:56
db_affected_rows
db_affected_rows($oStmt=null)
Definition: db.inc.php:56
ChWsbSession\getInstance
static getInstance()
Definition: ChWsbSession.php:28
getSiteStatArray
getSiteStatArray()
Definition: utils.inc.php:1184
getMemberMembershipInfo
getMemberMembershipInfo($iMemberId, $iTime='', $bCheckUserStatus=false)
Definition: membership_levels.inc.php:234
setParam
setParam($sParamName, $sParamValue)
Definition: db.inc.php:149
db_res
db_res($query, $bindings=[])
Definition: db.inc.php:39
markMembershipAsExpiring
markMembershipAsExpiring($iMemberId, $iLevelId, $sTransactionId)
Definition: membership_levels.inc.php:778
empty
Attr AllowedRel this is empty
Definition: Attr.AllowedRel.txt:7
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
ChWsbAdminIpBlockList
Definition: ChWsbAdminIpBlockList.php:11
$sName
$sName
Definition: ChWsbAdminTools.php:853
ChWsbCronCmd\processing
processing()
Definition: ChWsbCronCmd.php:219
CH_WSB_ROLE_ADMIN
const CH_WSB_ROLE_ADMIN
Definition: profiles.inc.php:17
$GLOBALS
$GLOBALS['iAdminPage']
Definition: advanced_settings.php:10
$dir
$dir
Definition: config.php:10