[ Index ]

PHP Cross Reference of MyBB 1.8.27

title

Body

[close]

/inc/tasks/ -> promotions.php (source)

   1  <?php
   2  /**
   3   * MyBB 1.8
   4   * Copyright 2014 MyBB Group, All Rights Reserved
   5   *
   6   * Website: http://www.mybb.com
   7   * License: http://www.mybb.com/about/license
   8   *
   9   */
  10  
  11  function task_promotions($task)
  12  {
  13      global $mybb, $db, $lang, $cache, $plugins;
  14  
  15      $usergroups = $cache->read("usergroups");
  16      // Iterate through all our promotions
  17      $query = $db->simple_select("promotions", "*", "enabled = '1'");
  18      while($promotion = $db->fetch_array($query))
  19      {
  20          // Does the destination usergroup even exist?? If it doesn't and it moves a user to it, the user will get PHP errors.
  21          if(!array_key_exists($promotion['newusergroup'], $usergroups))
  22          {
  23              // Instead of just skipping this promotion, disable it to stop it even being selected when this task is run.
  24              $update = array(
  25                  "enabled" => 0
  26              );
  27              $db->update_query("promotions", $update, "pid = '" . (int)$promotion['pid'] . "'");
  28              continue;
  29          }
  30  
  31          $and = "";
  32          $sql_where = "";
  33  
  34          // Based on the promotion generate criteria for user selection
  35          $requirements = explode(',', $promotion['requirements']);
  36          if(in_array('postcount', $requirements) && (int)$promotion['posts'] >= 0 && !empty($promotion['posttype']))
  37          {
  38              $sql_where .= "{$and}postnum {$promotion['posttype']} '{$promotion['posts']}'";
  39  
  40              $and = " AND ";
  41          }
  42  
  43          if(in_array('threadcount', $requirements) && (int)$promotion['threads'] >= 0 && !empty($promotion['threadtype']))
  44          {
  45              $sql_where .= "{$and}threadnum {$promotion['threadtype']} '{$promotion['threads']}'";
  46  
  47              $and = " AND ";
  48          }
  49  
  50          if(in_array('reputation', $requirements) && !empty($promotion['reputationtype']))
  51          {
  52              $sql_where .= "{$and}reputation {$promotion['reputationtype']} '{$promotion['reputations']}'";
  53  
  54              $and = " AND ";
  55          }
  56  
  57          if(in_array('referrals', $requirements) && (int)$promotion['referrals'] >= 0 && !empty($promotion['referralstype']))
  58          {
  59              $sql_where .= "{$and}referrals {$promotion['referralstype']} '{$promotion['referrals']}'";
  60  
  61              $and = " AND ";
  62          }
  63  
  64          if(in_array('warnings', $requirements) && (int)$promotion['warnings'] >= 0 && !empty($promotion['warningstype']))
  65          {
  66              $sql_where .= "{$and}warningpoints {$promotion['warningstype']} '{$promotion['warnings']}'";
  67  
  68              $and = " AND ";
  69          }
  70  
  71          if(in_array('timeregistered', $requirements) && (int)$promotion['registered'] > 0 && !empty($promotion['registeredtype']))
  72          {
  73              switch($promotion['registeredtype'])
  74              {
  75                  case "hours":
  76                      $regdate = $promotion['registered']*60*60;
  77                      break;
  78                  case "days":
  79                      $regdate = $promotion['registered']*60*60*24;
  80                      break;
  81                  case "weeks":
  82                      $regdate = $promotion['registered']*60*60*24*7;
  83                      break;
  84                  case "months":
  85                      $regdate = $promotion['registered']*60*60*24*30;
  86                      break;
  87                  case "years":
  88                      $regdate = $promotion['registered']*60*60*24*365;
  89                      break;
  90                  default:
  91                      $regdate = $promotion['registered']*60*60*24;
  92              }
  93              $sql_where .= "{$and}regdate <= '".(TIME_NOW-$regdate)."'";
  94              $and = " AND ";
  95          }
  96  
  97          if(in_array('timeonline', $requirements) && (int)$promotion['online'] > 0 && !empty($promotion['onlinetype']))
  98          {
  99              switch($promotion['onlinetype'])
 100              {
 101                  case "hours":
 102                      $timeonline = $promotion['online']*60*60;
 103                      break;
 104                  case "days":
 105                      $timeonline = $promotion['online']*60*60*24;
 106                      break;
 107                  case "weeks":
 108                      $timeonline = $promotion['online']*60*60*24*7;
 109                      break;
 110                  case "months":
 111                      $timeonline = $promotion['online']*60*60*24*30;
 112                      break;
 113                  case "years":
 114                      $timeonline = $promotion['online']*60*60*24*365;
 115                      break;
 116                  default:
 117                      $timeonline = $promotion['online']*60*60*24;
 118              }
 119              $sql_where .= "{$and}timeonline > '".$timeonline."'";
 120              $and = " AND ";
 121          }
 122  
 123          if(!empty($promotion['originalusergroup']) && $promotion['originalusergroup'] != '*')
 124          {
 125              $sql_where .= "{$and}usergroup IN ({$promotion['originalusergroup']})";
 126  
 127              $and = " AND ";
 128          }
 129  
 130          if(!empty($promotion['newusergroup']))
 131          {
 132              // Skip users that are already in the new group
 133              switch($db->type)
 134              {
 135                  case "pgsql":
 136                  case "sqlite":
 137                      $sql_where .= "{$and}usergroup != '{$promotion['newusergroup']}' AND ','||additionalgroups||',' NOT LIKE '%,{$promotion['newusergroup']},%'";
 138                      break;
 139                  default:
 140                      $sql_where .= "{$and}usergroup != '{$promotion['newusergroup']}' AND CONCAT(',', additionalgroups, ',') NOT LIKE '%,{$promotion['newusergroup']},%'";
 141              }
 142  
 143              $and = " AND ";
 144          }
 145  
 146          $uid = array();
 147          $log_inserts = array();
 148  
 149          if($promotion['usergrouptype'] == "secondary")
 150          {
 151              $usergroup_select = "additionalgroups";
 152          }
 153          else
 154          {
 155              $usergroup_select = "usergroup";
 156          }
 157  
 158          if(is_object($plugins))
 159          {
 160              $args = array(
 161                  'task' => &$task,
 162                  'promotion' => &$promotion,
 163                  'sql_where' => &$sql_where,
 164                  'and' => &$and,
 165                  'usergroup_select' => &$usergroup_select
 166              );
 167              $plugins->run_hooks('task_promotions', $args);
 168          }
 169  
 170          $query2 = $db->simple_select("users", "uid,{$usergroup_select}", $sql_where);
 171  
 172          $uids = array();
 173          while($user = $db->fetch_array($query2))
 174          {
 175              if(is_super_admin($user['uid']))
 176              {
 177                  // Skip super admins
 178                  continue;
 179              }
 180  
 181              // super admin check?
 182              if($usergroup_select == "additionalgroups")
 183              {
 184                  $log_inserts[] = array(
 185                      'pid' => $promotion['pid'],
 186                      'uid' => $user['uid'],
 187                      'oldusergroup' => $db->escape_string($user['additionalgroups']),
 188                      'newusergroup' => $promotion['newusergroup'],
 189                      'dateline' => TIME_NOW,
 190                      'type' => "secondary",
 191                  );
 192              }
 193              else
 194              {
 195                  $log_inserts[] = array(
 196                      'pid' => $promotion['pid'],
 197                      'uid' => $user['uid'],
 198                      'oldusergroup' => $user['usergroup'],
 199                      'newusergroup' => $promotion['newusergroup'],
 200                      'dateline' => TIME_NOW,
 201                      'type' => "primary",
 202                  );
 203              }
 204  
 205              $uids[] = $user['uid'];
 206  
 207  
 208              if($usergroup_select == "additionalgroups")
 209              {
 210                  if(join_usergroup($user['uid'], $promotion['newusergroup']) === false)
 211                  {
 212                      // Did the user already have the additional usergroup?
 213                      array_pop($log_inserts);
 214                      array_pop($uids);
 215                  }
 216              }
 217  
 218              if((count($uids) % 20) == 0)
 219              {
 220                  if($usergroup_select == "usergroup")
 221                  {
 222                      $db->update_query("users", array('usergroup' => $promotion['newusergroup']), "uid IN(".implode(",", $uids).")");
 223                  }
 224  
 225                  if(!empty($log_inserts))
 226                  {
 227                      $db->insert_query_multiple("promotionlogs", $log_inserts);
 228                  }
 229  
 230                  $uids = array();
 231                  $log_inserts = array();
 232              }
 233          }
 234  
 235          if(count($uids) > 0)
 236          {
 237              if($usergroup_select == "usergroup")
 238              {
 239                  $db->update_query("users", array('usergroup' => $promotion['newusergroup']), "uid IN(".implode(",", $uids).")");
 240              }
 241  
 242              if(!empty($log_inserts))
 243              {
 244                  $db->insert_query_multiple("promotionlogs", $log_inserts);
 245              }
 246  
 247              $uids = array();
 248              $log_inserts = array();
 249          }
 250      }
 251  
 252      $cache->update_moderators();
 253  
 254      add_task_log($task, $lang->task_promotions_ran);
 255  }


2005 - 2021 © MyBB.de | Alle Rechte vorbehalten! | Sponsor: netcup Cross-referenced by PHPXref