Ejemplo n.º 1
0
 /**
  * 
  * @param \Sooh\Base\Log\Data $logData
  */
 public function write($logData)
 {
     $resChg = $logData->resChanged;
     $arr = $logData->toArray();
     unset($arr['resChanged']);
     unset($arr['logGuid']);
     \Sooh\DB\Cases\LogStorage::$__YMD = \Sooh\Base\Time::getInstance()->YmdFull;
     \Sooh\DB\Cases\LogStorage::$__id_in_dbByObj = 'dbgrpForLog';
     \Sooh\DB\Cases\LogStorage::$__type = 'a';
     \Sooh\DB\Cases\LogStorage::$__nSplitedBy = $this->tbSplit;
     //\Sooh\DB\Cases\LogStorage::$__fields=array(.....);
     $tmp = \Sooh\DB\Cases\LogStorage::getCopy($logData->logGuid);
     foreach ($arr as $k => $v) {
         $tmp->setField($k, $v);
     }
     $ret = $tmp->writeLog();
     if ($ret) {
         $tbSub = str_replace('_a_', '_b_', $tmp->tbname());
         foreach ($resChg as $r) {
             $r['logGuid'] = $logData->logGuid;
             try {
                 \Sooh\DB\Broker::errorMarkSkip(\Sooh\DB\Error::tableNotExists);
                 $tmp->db()->addRecord($tbSub, $r);
             } catch (\ErrorException $e) {
                 if (\Sooh\DB\Broker::errorIs($e, \Sooh\DB\Error::tableNotExists)) {
                     $tmp->db()->ensureObj($tbSub, array('logGuid' => 'bigint unsigned not null  default 0', 'resName' => "varchar(36) not null default ''", 'resChg' => "int not null default 0", 'resNew' => "int not null default 0"));
                     $tmp->db()->addRecord($tbSub, $r);
                 } else {
                     error_log("write log failed:" . $e->getMessage() . "\n" . \Sooh\DB\Broker::lastCmd());
                 }
             }
         }
     }
 }
Ejemplo n.º 2
0
 /**
  * 
  * @param \Sooh\Base\Log\Data $logData
  */
 public function write($logData)
 {
     $arr = $logData->toArray();
     $ret = array('ret' => $arr['ret']);
     unset($arr['ret']);
     $arr = array_merge($ret, $arr);
     $fullname = $this->path . '/' . $logData->ymd . '-' . $this->file;
     file_put_contents($fullname, json_encode($arr), FILE_APPEND);
 }
Ejemplo n.º 3
0
 /**
  * 
  * @param \Sooh\Base\Log\Data $logData
  */
 public function write($logData)
 {
     $arr = $logData->toArray();
     $ret = array('ret' => $arr['ret']);
     unset($arr['ret']);
     $arr = array_merge($ret, $arr);
     if ($this->fullname === '/') {
         error_log(json_encode($arr));
     } else {
         file_put_contents($this->fullname, '[' . date('m-d H:i:s') . ']' . json_encode($arr), FILE_APPEND);
     }
 }
Ejemplo n.º 4
0
 /**
  * $guidtype 在构造时使用一次
  *   a 180组服务器,30几年后溢出,(每个请求需要一个进程处理至少1个毫秒,)
  *   b 18组服务器, 300年后溢出, (每个请求需要一个进程处理至少1个毫秒,)
  *   c 18组服务器, 30年后溢出,  (每个请求需要一个进程处理至少0.1个毫秒,)
  * @param string $guidtype 
  * @return \Sooh\Base\Log\Data
  */
 public static function getInstance($guidtype = 'c')
 {
     if (self::$_instance == null) {
         self::$_instance = new Data();
         $dt = \Sooh\Base\Time::getInstance();
         self::$_instance->ymd = $dt->YmdFull;
         self::$_instance->hhiiss = $dt->his;
         self::$_instance->ip = \Sooh\Base\Tools::remoteIP();
         self::$_instance->logGuid = self::$_instance->newLogId($guidtype);
         \Sooh\Base\Ini::registerShutdown(get_called_class() . '::onShutdown', 'logOnShutdown');
     }
     return self::$_instance;
 }
Ejemplo n.º 5
0
 public function errorAction($exception)
 {
     $ini = \Sooh\Base\Ini::getInstance();
     $render = $ini->viewRenderType();
     switch ($render) {
         case "json":
             header('Content-type: application/json');
             break;
         case 'jsonp':
             break;
         default:
             $ini->viewRenderType('json');
             header('Content-type: application/json');
             break;
     }
     if (is_a($exception, '\\Sooh\\DB\\Acl\\ErrNeedsLogin')) {
         \Sooh\Base\Log\Data::getInstance()->ret = 'needs login';
         //		}elseif(is_a($exception,'\Prj\ErrCode')){
         //			$this->_view->assign('code',$exception->getCode());
         //			$this->_view->assign('msg',$exception->getMessage());
         //			\Sooh\Base\Log\Data::getInstance()->ret = $exception->getMessage();
         //		}elseif(is_a($exception,'\Prj\RetCode')){
         //			$this->_view->assign('code',200);
         //			$this->_view->assign('msg',$exception->getMessage());
     } else {
         var_log($exception);
         $this->_view->assign('code', $exception->getCode());
         $this->_view->assign('msg', $exception->getMessage());
         \Sooh\Base\Log\Data::getInstance()->ret = $exception->getMessage();
     }
 }
Ejemplo n.º 6
0
 /**
  * 记录打开页面的情况
  * 
  * @input string page 页面名(标示)
  * @input string httpReferer httpReferer
  * @sample {api_uri}&page=intro.html&httpReferer=www.baidu.com
  * @return {"code":200,"msg":""} 正常情况
  */
 public function openpageAction()
 {
     $loger = \Sooh\Base\Log\Data::getInstance();
     $loger->evt = 'openpage';
     $loger->target = $this->_request->get('page');
     $loger->userId = \Sooh\Base\Session\Data::getInstance()->get('accountId');
     $loger->ext = $this->_request->get('httpReferer');
     $this->_view->assign('code', '200');
     $this->_view->assign('msg', '');
 }
Ejemplo n.º 7
0
 protected function onInit_chkLogin()
 {
     $userIdentifier = \Sooh\Base\Session\Data::getInstance()->get('accountId');
     if ($userIdentifier) {
         \Sooh\Base\Log\Data::getInstance()->userId = $userIdentifier;
         $this->user = \Prj\Data\User::getCopy($userIdentifier);
     } else {
         throw new \ErrorException(\Prj\ErrCode::errNotLogin, 401);
     }
 }
Ejemplo n.º 8
0
 public function fetchini($service)
 {
     if ($this->rpc === null) {
         $ini = \Sooh\Base\Ini::getInstance()->get('RpcConfig');
         unset($ini['force']);
         return $ini;
     } else {
         \Sooh\Base\Log\Data::error('Rpcservices should never be called by rpc');
         throw new \Sooh\Base\ErrException('Rpcservices should never be called by rpc');
         //return $this->rpc->initArgs(array('arg1'=>$arg1))->send();//call('CheckinBook/'.__FUNCTION__, array($withBonus,$userOrAccountId));
     }
 }
Ejemplo n.º 9
0
 public function init()
 {
     $this->ini = \Sooh\Base\Ini::getInstance();
     if ($this->_request->isPost()) {
         $this->onInit_chkMaintainTime();
     }
     list($deviceId, $uid) = $this->initSession();
     \Sooh\Base\Log\Data::addWriter(new \Sooh\Base\Log\Writers\TextAll(), 'trace');
     \Sooh\Base\Log\Data::addWriter(new \Sooh\Base\Log\Writers\TextAll(), 'error');
     \Sooh\Base\Log\Data::addWriter(new \Sooh\Base\Log\Writers\Database('dbgrpForLog', 2), 'evt');
     $log = \Sooh\Base\Log\Data::getInstance('c');
     $log->clientType = 0;
     $log->deviceId = $deviceId;
     $log->contractId = '0';
     $log->evt = $this->_request->getModuleName() . '/' . $this->_request->getControllerName() . '/' . $this->_request->getActionName();
     $log->isLogined = 0;
     $log->userId = $uid;
 }
Ejemplo n.º 10
0
 public function loginAction()
 {
     $acc = $this->getAccount();
     try {
         $validImg = $this->_request->get('valid');
         $sessionData = \Sooh\Base\Session\Data::getInstance();
         error_log(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>{$validImg} !=" . $sessionData->get('validImg'));
         if ($validImg !== $sessionData->get('validImg')) {
             return $this->returnError('invalidCode');
         }
         $accountInfo = $acc->login($this->_request->get('loginname'), $this->_request->get('passwd'), 'local', array('contractId', 'rights'));
         $this->onLogin($accountInfo);
         \Sooh\Base\Log\Data::getInstance()->ret = 'login ok';
     } catch (\Exception $e) {
         \Sooh\Base\Log\Data::getInstance()->ret = $e->getMessage();
         $this->returnError($e->getMessage(), $e->getCode());
     }
 }
Ejemplo n.º 11
0
 public function indexAction()
 {
     if ($this->_request->get('dowhat') == 'logout') {
         $this->_view->assign('useTpl', 'logout');
         \Sooh\Base\Session\Data::getInstance()->set('managerId', '');
     } elseif ($this->manager) {
         //已经登入,进主页
         $this->manager->load();
         $this->_view->assign('leftmenus', $this->manager->acl->getMenuMine());
         $this->_view->assign('useTpl', 'homepage');
     } else {
         //尚未登入,去登入页
         $u = $this->_request->get('u');
         $p = $this->_request->get('p');
         if (!empty($u) && !empty($p)) {
             $this->ini->viewRenderType('json');
             $acc = \Lib\Services\Manager::getInstance(null);
             try {
                 $validImg = $this->_request->get('valid');
                 $sessionData = \Sooh\Base\Session\Data::getInstance();
                 if ($validImg !== $sessionData->get('validImg')) {
                     return $this->returnError('invalidCode');
                 }
                 $accountInfo = $acc->login($u, $p, 'local', array('rights'));
                 $log = \Sooh\Base\Log\Data::getInstance();
                 $log->ret = 'login ok';
                 $log->ext = $accountInfo['nickname'];
                 $sessionData->set('managerId', $accountInfo['accountId']);
                 $sessionData->set('nickname', $accountInfo['nickname']);
                 $this->returnOK();
             } catch (\Exception $e) {
                 \Sooh\Base\Log\Data::getInstance()->ret = $e->getMessage();
                 $this->returnError($e->getMessage());
             }
         } else {
             $acc = \Lib\Services\Manager::getInstance(null);
             $acc->addFirstAccount();
             $this->_view->assign('useTpl', 'login');
         }
     }
 }
Ejemplo n.º 12
0
 /**
  * 预发放道具
  * @param array $bonusThisTime
  * @param string $accountId
  * @param \Lib\Items\Interfaces $_ignore_
  * @return type
  */
 public static function batchGive_pre($bonusThisTime, $accountId, $_ignore_ = null)
 {
     $givedThisTime = array();
     foreach ($bonusThisTime as $itemname => $num) {
         $_ignore_ = self::create($itemname, $accountId);
         try {
             $_ignore_->giveToUser_prepare($num, $accountId, '签到奖励');
             $givedThisTime[] = $_ignore_;
         } catch (\ErrorException $ex) {
             error_log("give bonus {$itemname} to {$accountId} failed as " . $ex->getMessage() . "\n" . $ex->getTraceAsString());
             $_ignore_->giveToUser_rollback('trans_rollback');
             break;
         }
     }
     $remain = $_ignore_->remain();
     if (is_array($remain)) {
         $remain = sizeof($remain);
     }
     \Sooh\Base\Log\Data::getInstance()->setThese('', '', $itemname, $num, $remain);
     return $givedThisTime;
 }
Ejemplo n.º 13
0
 protected function error($msg, $code = 400)
 {
     \Sooh\Base\Log\Data::getInstance()->ret = $msg;
     $this->_view->assign('code', $code);
     $this->_view->assign('msg', $msg);
 }
Ejemplo n.º 14
0
 public function update($sessionId, $sessData, $trans)
 {
     if ($this->rpc !== null) {
         return $this->rpc->initArgs(array('sessionId' => $sessionId, 'sessData' => $sessData, 'trans' => $trans))->send(__FUNCTION__);
     } else {
         try {
             $err = new \Sooh\Base\ErrException('');
             if (!empty($trans['sessionId']) && $trans['sessionId'] != $sessionId) {
                 \Sooh\Base\Log\Data::error("ERROR on update session with sessionId dismatch: {$sessionId}", array('trans' => $trans, 'data' => $sessData));
                 return 'error';
             } else {
                 $obj = \Sooh\DB\Cases\SessionStorage::getCopy($sessionId);
                 //unset($trans['sessionId']);
                 if (!empty($trans['iRecordVerID'])) {
                     $obj->setVerId($trans['iRecordVerID']);
                 }
                 //unset($trans['iRecordVerID']);
                 ksort($sessData);
                 $md5 = md5(json_encode($sessData));
                 //					error_log('[TRACE-session '.$_COOKIE['SoohSessId'].' storage] md5_'.($md5!=$this->md5Last?'NE':'EQ').' old='.$this->md5Last.' vs new '.$md5);
                 if ($md5 != $this->md5Last) {
                     $obj->setSessionData($sessData);
                     $obj->setField('lastUpdate', time());
                     $obj->update();
                 }
                 //error_log(">>>>>>>>>>>session>>>$sessionId\n".  var_export($sessData,true)."\n".  var_export($trans,true));
                 return 'done';
             }
         } catch (\Exception $e) {
             \Sooh\Base\Log\Data::error('errorOnUpdateSession', $e);
             return 'error:' . $e->getMessage();
         }
     }
 }
Ejemplo n.º 15
0
 protected function onInit_chkLogin()
 {
     $this->session = \Sooh\Base\Session\Data::getInstance();
     if ($this->session) {
         $userId = $this->session->get('managerId');
         if ($userId) {
             \Sooh\Base\Log\Data::getInstance()->userId = $userId;
             $this->manager = \Prj\Data\Manager::getCopyByManagerId($userId);
             $this->manager->load();
             if (!$this->manager->acl->hasRightsFor($this->_request->getModuleName(), $this->_request->getControllerName())) {
                 $this->returnError(\Prj\ErrCode::errNoRights, 300);
             }
         } else {
             $this->returnError(\Prj\ErrCode::errNotLogin, 301);
         }
     }
 }
Ejemplo n.º 16
0
 /**
  * 签到
  * @param boolean $withBonus 返回里是否带奖励物品列表
  * @param \Prj\Data\User $userOrAccountId accountId 或 \Prj\Data\User
  * @return array
  */
 public function doCheckIn($withBonus, $userOrAccountId)
 {
     if ($this->rpc !== null) {
         return $this->rpc->initArgs(array('withBonus' => $withBonus, 'userOrAccountId' => $userOrAccountId))->send(__FUNCTION__);
     } else {
         if (is_scalar($userOrAccountId)) {
             $userOrAccountId = \Prj\Data\User::getCopy($userOrAccountId);
         }
         $userOrAccountId->load();
         if ($userOrAccountId->exists() === false) {
             $userOrAccountId->setField(self::fieldUser, array());
             $userOrAccountId->update();
         }
         $this->decode($userOrAccountId->getField(self::fieldUser, true));
         if ($this->r['ymd'] == $this->today) {
             \Sooh\Base\Log\Data::getInstance()->ret = "checkin already";
             return $this->errFound(self::errTodayDone, 400, $withBonus);
         } elseif (sizeof($this->r['checked']) >= self::maxMonth) {
             \Sooh\Base\Log\Data::getInstance()->ret = 'checkin of this month:all done';
             return $this->errFound(self::errMonthDone, 400, $withBonus);
         }
         $accountId = $userOrAccountId->getAccountId();
         $idCheckThisTime = array_sum($this->r['checked']);
         $bonusThisTime = $this->getBonusList()[$idCheckThisTime];
         if (false === $userOrAccountId->lock('chkinBonus:' . http_build_query($bonusThisTime))) {
             \Sooh\Base\Log\Data::getInstance()->ret = "lock user for checkin failed";
             \Prj\Loger::alarm('[LockFailed] user-table on checkin' . \Sooh\DB\Broker::lastCmd());
             return $this->errFound(\Sooh\Base\ErrException::msgServerBusy, 500, $withBonus);
         }
         //\Lib\Items\Broker::iniForGiven('Voucher', array('iniDefaultForGive_a'=>60));// 如果有道具发放时有额外参数要设置(比如有效期不同)
         $givedThisTime = \Lib\Items\Broker::batchGive_pre($bonusThisTime, $accountId);
         if (sizeof($givedThisTime) == sizeof($bonusThisTime)) {
             if (\Lib\Items\Broker::batchGive_confirm($givedThisTime, $accountId)) {
                 try {
                     $this->r['checked'][$idCheckThisTime] = 1;
                     $this->r['bonusGot'][$idCheckThisTime] = $bonusThisTime;
                     $this->r['ymd'] = $this->today;
                     $userOrAccountId->setField(self::fieldUser, $this->r);
                     $userOrAccountId->update();
                     \Sooh\Base\Log\Data::getInstance()->ret = "done";
                     return $this->allDone(self::msgCheckinDone, $withBonus);
                 } catch (\ErrorException $errOnUpdate) {
                     \Prj\Loger::alarm('[CheckInFailed] on update user:'******'[GiveItemFailed] on checkin');
             \Sooh\Base\Log\Data::getInstance()->ret = "give item failed";
         } else {
             \Sooh\Base\Log\Data::getInstance()->ret = "update user failed";
         }
         if (\Lib\Items\Broker::batchGive_rollback('GiveItemFailedOnCheckin', $givedThisTime, $accountId)) {
             $userOrAccountId->unlock();
             return $this->errFound(\Sooh\Base\ErrException::msgServerBusy, 500, $withBonus);
         } else {
             return $this->errFound(\Sooh\Base\ErrException::msgServerBusy, 500, $withBonus);
         }
     }
 }
Ejemplo n.º 17
0
 /**
  * 发送请求
  * @return mixed
  */
 public function send($cmd)
 {
     $this->final_cmd = $cmd;
     $hosts = $this->final_hosts;
     $timestamp = \Sooh\Base\Time::getInstance()->timestamp();
     while (sizeof($hosts)) {
         $rand = array_rand($hosts);
         $host = $hosts[$rand];
         unset($hosts[$rand]);
         //if('rpcservices'!=$this->final_service)
         //error_log("#######rpc-call####{$this->final_protocol}:{$host}, {$this->final_service}, {$this->final_cmd},".  json_encode($this->final_args));
         $ret = $this->getSender($this->final_protocol)->_send($host, $this->final_service, $this->final_cmd, $this->final_args, $timestamp, $this->clacSign($timestamp));
         //error_log("#######rpc-ret####".json_encode($ret));
         if (empty($ret)) {
             \Sooh\Base\Log\Data::error("found_rpc_server_down:" . $host . ' with cmd');
         } else {
             $arr = json_decode($ret, true);
             if (is_array($arr)) {
                 if ($arr['code'] == 200) {
                     return $arr['data'];
                 } else {
                     $err = new \Sooh\Base\ErrException($arr['msg'], $arr['code']);
                     $err->customData = $arr['data'];
                     throw $err;
                 }
             }
         }
     }
     throw new \Sooh\Base\ErrException('rpc_failed:' . $this->final_cmd);
 }
Ejemplo n.º 18
0
 /**
  * 账号登入, 失败抛出异常(密码错误,账号找不到等等)
  * @param $loginName
  * @param $cameFrom
  * @param $password
  * @param array $customArgs
  * @return mixed
  * @throws \ErrorException
  * @throws \Sooh\Base\ErrException
  * @throws array
  */
 public function login($loginName, $cameFrom, $password, $customArgs = ['contractId'])
 {
     if ($this->rpc !== null) {
         return $this->rpc->initArgs(['loginName' => $loginName, 'cameFrom' => $cameFrom, 'password' => $password, 'customArgs' => $customArgs])->send(__FUNCTION__);
     } else {
         $objLogin = \Sooh\DB\Cases\AccountAlias::getCopy($loginName, $cameFrom);
         $objLogin->load();
         if ($objLogin->exists()) {
             $accountId = $objLogin->getField('accountId');
             $this->setAccountStorage($accountId);
             $this->account->load();
             if ($this->account->exists()) {
                 $dt = \Sooh\Base\Time::getInstance();
                 $cmp = md5($password . $this->account->getField('passwdSalt'));
                 $loginFailed = $this->account->getField('loginFailed');
                 if ($loginFailed) {
                     $cd = new \Sooh\Base\CD($loginFailed, 750, 3600);
                     if ($cd->isRed()) {
                         throw new \Sooh\Base\ErrException(self::errAccountOrPasswordError);
                     }
                 } else {
                     $cd = new \Sooh\Base\CD(0, 750, 3600);
                 }
                 $ymdhForbidden = $this->account->getField('dtForbidden');
                 if ($ymdhForbidden) {
                     if ($dt->YmdH <= $ymdhForbidden) {
                         throw new \Sooh\Base\ErrException(self::errAccountLock, 404);
                     }
                 }
                 if ($cmp != $this->account->getField('passwd')) {
                     $cd->add(1);
                     $ret = new \Sooh\Base\ErrException(self::errAccountOrPasswordError, 404);
                 } else {
                     $nickname = $this->account->getField('nickname');
                     $ret = array('accountId' => $this->account->getField('accountId'), 'nickname' => $nickname);
                     if (!empty($customArgs)) {
                         if (is_string($customArgs)) {
                             $customArgs = explode(',', $customArgs);
                         }
                         foreach ($customArgs as $k) {
                             $ret[$k] = $this->account->getField('contractId');
                         }
                     }
                 }
                 $this->account->setField('lastIP', \Sooh\Base\Tools::remoteIP());
                 $this->account->setField('lastDt', $dt->timestamp());
                 $this->account->setField('loginFailed', $cd->toString());
                 try {
                     $this->account->update();
                 } catch (\ErrorException $ex) {
                     \Sooh\Base\Log\Data::error("error on update account when login:"******"\n" . \Sooh\DB\Broker::lastCmd() . "\n" . $ex->getTraceAsString());
                 }
                 if (is_array($ret)) {
                     return $ret;
                 } else {
                     throw $ret;
                 }
             } else {
                 throw new \Sooh\Base\ErrException(self::errAccountOrPasswordError, 400);
             }
         } else {
             throw new \Sooh\Base\ErrException(self::errAccountOrPasswordError, 400);
         }
     }
 }