<?php $_SERVER['HTTP_HOST'] = ''; $fromUid = isset($GLOBALS['argv'][3]) && (intval($GLOBALS['argv'][3]) == $GLOBALS['argv'][3]) ? $GLOBALS['argv'][3] : 0; $limitNum = isset($GLOBALS['argv'][4]) && (intval($GLOBALS['argv'][4]) == $GLOBALS['argv'][4]) ? $GLOBALS['argv'][4] : 0; $onlyUpdateCheckedHasAvatar = isset($GLOBALS['argv'][5]) && (intval($GLOBALS['argv'][5]) == 1) ? 1 : 0; $newLogFile = isset($GLOBALS['argv'][6]) && (intval($GLOBALS['argv'][6]) == 1) ? 1 : 0; define('FROMUID', $fromUid); define('LIMITNUM', $limitNum); define('ONLY_UPDATE_CHECKED_HASAVATAR', $onlyUpdateCheckedHasAvatar); define('NEW_LOGFILE', $newLogFile); /** * CheckUserAvatarStatus.class.php * 2015-1-16 */ /** * 检查用户头像状态 > 临时文件 有差异的用户列表:用户Uid, 数据库中用户头像状态, 检查头像状态结果, 用户头像地址 * 更新用户头像状态 < 临时文件 - 有差异的用户列表 */ class CheckUserAvatarStatus extends Controller { protected $objDCommonMember; protected $avatarHost; protected $fromUid; // 开始检查的用户Uid, 按Uid倒序排列检查数据 protected $limitNum; // 限制查查的个数 protected $pageSize; protected $checkLocalAvatar; // 1: 检查服务器上的avatar是否存在,2 : 还是url远程请求检查avatar是否存在 protected $diffLogFile; protected $onlyUpdateCheckedHasAvatar; // 仅仅更新原avatarstatus=0, 但检查用户头像存在的用户的头像状态 protected $newLogfile; public function __construct() { $this->objDCommonMember = new BBSDaoCommonMember(); $this->avatarHost = 'http://xxx.com/'; $this->fromUid = FROMUID; $this->limitNum = LIMITNUM; $this->onlyUpdateCheckedHasAvatar = ONLY_UPDATE_CHECKED_HASAVATAR; $this->pageSize = 50; $this->checkLocalAvatar = 2; $this->diffLogFile = '/tmp/check_user_avatarstatus_diff.txt'; $this->newLogfile = NEW_LOGFILE; } public function doDefault() { echo 'nothing'; exit(); } public function doCheckUserAvatarStatus() { if($this->newLogfile) { @unlink($this->diffLogFile); } $limitNum = $this->limitNum; if ($limitNum) { $this->_echo_msg('Limit num ' . $limitNum); } else { $this->_echo_msg('No Limit num '); } // sleep(2); $condition = ''; if ($this->fromUid) $condition .= 'uid < ' . $this->fromUid; $total = $this->objDCommonMember->fetchOne($condition, 'count(*)'); $total = $total ? array_pop($total) : 0; $this->_echo_msg('Total num ' . $total); if (!$total) exit(); $pages = ceil($total / $this->pageSize); $count = 0; for ($i = 0; $i < $pages; $i++) { $limit = $i * $this->pageSize . ',' . $this->pageSize; $datas = $this->objDCommonMember->findBy($condition, $limit, $limit, 'uid, username, avatarstatus', 'uid desc'); foreach ($datas as $userInfo) { $this->_echo_msg('check user ' . $userInfo['uid']); list($avatarStatus, $avatarUrl) = $this->_checkAvatarStatus($userInfo['uid'], $userInfo['avatarstatus']); if ($avatarStatus != $userInfo['avatarstatus']) { $this->_writeDiffFile($userInfo, $avatarStatus, $avatarUrl); } $count++; if ($this->limitNum && $count > $this->limitNum) { $this->_echo_msg('reach Limit num ' . $this->limitNum); exit(); } $this->_echo_msg('counter ' . $count .'/' . $total); } } } /** * 根据差异文件,修改用户的头像状态 - 只更新原avatarstatus=0,但用户有头像的用户 */ public function doUpdateUserAvatarStatus() { $logRows = file($this->diffLogFile); $logRows = array_unique($logRows); $logRows = array_filter($logRows); $pages = ceil(count($logRows) / $this->pageSize); for ($i = 0; $i < $pages; $i++) { $datas = array_slice($logRows, $i * $this->pageSize, $this->pageSize); $has_uids = $hasno_uids = array(); foreach ($datas as $value) { $tmpVal = explode(" ", $value); if ($tmpVal[2] == '1') { $has_uids[] = $tmpVal[0]; } else { $hasno_uids[] = $tmpVal[0]; } } if (!empty($has_uids)) { $return = $this->objDCommonMember->update(array('avatarstatus' => 1), array('uid' => $has_uids, 'avatarstatus' => 0)); $this->_echo_msg('update user avatarstatus=1 return ' . $return . " " . implode(',', $has_uids)); } if(!$this->onlyUpdateCheckedHasAvatar) { if (!empty($hasno_uids)) { $return = $this->objDCommonMember->update(array('avatarstatus' => 0), array('uid' => $hasno_uids, 'avatarstatus' => 1)); $this->_echo_msg('update user avatarstatus=0 return ' . $return . " " . implode(',', $hasno_uids)); } } } } /** * 头像状态不一致的,添加文件记录 - 只更新原avatarstatus=0,但用户有头像的用户 */ private function _writeDiffFile($userInfo, $avatarStatus, $avatarUrl) { $str = $userInfo['uid'] . " " . $userInfo['avatarstatus'] . " " . $avatarStatus . " " . $avatarUrl . " "; $return = file_put_contents($this->diffLogFile, $str, FILE_APPEND); $this->_echo_msg('add diff avatarstatus log return ' . $return); } private function _checkAvatarStatus($uid, $old_avatarstatus) { $avatarUrl = $this->_getAvatarUrl($uid); $keepOld = false; if ($this->checkLocalAvatar == 1) { $avatarDir = $this->_getAvatarDir($uid); $this->_echo_msg('avatarDir ' . $avatarDir); $return = file_exists($avatarDir); } else { $this->_echo_msg('avatarUrl ' . $avatarUrl); $requestData = $this->curlRequest($avatarUrl); if (!empty($requestData) && $requestData[1]['size_download'] == 4182) { //默认小图大小 $keepOld = true; } $return = !empty($requestData) ? 1 : 0; } $checkAvatarStatus = $return ? 1 : 0; // 只记录检查有头像的log if ($this->onlyUpdateCheckedHasAvatar && $checkAvatarStatus == 0) { $keepOld = true; } if ($keepOld) { $checkAvatarStatus = $old_avatarstatus; $this->_echo_msg('Keep Old avatar status ' . $old_avatarstatus); } else { $this->_echo_msg('avatar status ' . $checkAvatarStatus . ' old avatarstatus ' . $old_avatarstatus); } return array($checkAvatarStatus, $avatarUrl); } private function _getAvatarUrl($uid) { $size = 'small'; $uid = abs(intval($uid)); $uid = sprintf("%09d", $uid); $dir1 = substr($uid, 0, 3); $dir2 = substr($uid, 3, 2); $dir3 = substr($uid, 5, 2); $typeadd = ''; return $this->avatarHost . $dir1 . '/' . $dir2 . '/' . $dir3 . '/' . substr($uid, -2) . $typeadd . "_avatar_$size.jpg"; } private function _getAvatarDir($uid) { $size = 'small'; $uid = abs(intval($uid)); $uid = sprintf("%09d", $uid); $dir1 = substr($uid, 0, 3); $dir2 = substr($uid, 3, 2); $dir3 = substr($uid, 5, 2); $typeadd = ''; return '/xxx/uc_server/data/avatar/' . $dir1 . '/' . $dir2 . '/' . $dir3 . '/' . substr($uid, -2) . $typeadd . "_avatar_$size.jpg"; } /** * curl获取指定路径内容 */ private function curlRequest($url, $method = '', $postdata = '', $isjson = 0) { $ch = curl_init(); $post = $method && strtolower($method) == 'post' ? 1 : 0; curl_setopt($ch, CURLOPT_URL, $url); if ($post) { curl_setopt($ch, CURLOPT_POST, $post); if ($postdata) { if (!$isjson) { $postdata = http_build_query($postdata); } curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata); } } curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); curl_setopt($ch, CURLOPT_TIMEOUT, 80); if ($isjson) { curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type:application/json', 'Content-Length:' . strlen($postdata) )); } curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"); $data = curl_exec($ch); $datainfo = curl_getinfo($ch); curl_close($ch); if ($datainfo['http_code'] == 200) { return array($data, $datainfo); } else { return array(); } } public function run() { $action = $this->action; $this->$action(); } }