/** * XML 转换为数组 * * @param string $xml XML string * * @return array */ public static function parse($xmltext) { try { $data = array(); $xml = new DOMDocument(); $xml->loadXML($xmltext); $children = $xml->documentElement->childNodes; foreach ($children as $node) { //Logger::info('XML', 'Key : ' . $node->nodeName . ' value : ' . $node->nodeValue); $data[trim($node->nodeName)] = trim($node->nodeValue); } return $data; } catch (Exception $e) { Logger::info('XML', 'fail to parse xml : ' . $e); return array(); } /* $data = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOBLANKS); Logger::info('XML', 'data is ' . $data); if (is_object($data) && get_class($data) === 'SimpleXMLElement') { $data = self::arrarval($data); } return $data; */ }
private function setup() { //Logger::info('FunnyGuessDB','Setting up funny guess db ... '); $this->serverName = env("MYSQL_PORT_3306_TCP_ADDR", "localhost"); $this->databaseName = env("MYSQL_INSTANCE_NAME", "homestead"); $this->username = env("MYSQL_USERNAME", "homestead"); $this->password = env("MYSQL_PASSWORD", "secret"); //Logger::info('FunnyGuessDB','ServerName : ' . $this->serverName); //Logger::info('FunnyGuessDB','DatabaseName : ' . $this->databaseName); //Logger::info('FunnyGuessDB','Username : '******'FunnyGuessDB','Password : '******'FunnyGuessDB','Connecting to db ... '); $db_address = 'mysql:host=' . $this->serverName . ';dbname=' . $this->databaseName; $this->pdo = new PDO($db_address, $this->username, $this->password, array(PDO::ATTR_PERSISTENT => true)); $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $this->setupRoomTable(); $this->setupUserTable(); $this->setupJoiningTable(); $this->setupHostingTable(); } catch (PDOException $e) { Logger::info('FunnyGuessDB', 'fail to connect to db : ' . $e->getMessage()); die; } }
public function add($roomId, $userId) { Logger::info('HostingDao', 'try to add hosting by owner : ' . $userId); $sql = "INSERT INTO " . self::TABLE_NAME . " (room_id, user_id) VALUES (?, ?)"; $statement = $this->connection->prepare($sql); Logger::info('HostingDao', 'SQL : ' . $sql); $statement->execute(array($roomId, $userId)); $statement = null; }
public function update($id, $status) { Logger::info('UserDao', 'try to update user : '******'UserDao', 'SQL : ' . $sql); $statement->execute(array($status, $id)); $statement = null; }
public function getJoinCountByRoomId($roomId) { Logger::info('JoiningDao', 'try to load room join count : ' . $roomId); $sql = "SELECT count(*) FROM " . self::TABLE_NAME . " WHERE room_id = ?"; $statement = $this->connection->prepare($sql); Logger::info('JoiningDao', 'SQL : ' . $sql); $statement->execute(array($roomId)); $statement = null; }
public function update($id, $userId) { Logger::info('RoomDao', 'try to update room : ' . $id); $sql = "UPDATE " . self::TABLE_NAME . " set owner = ? where id = ? "; $statement = $this->connection->prepare($sql); Logger::info('RoomDao', 'SQL : ' . $sql); $statement->execute(array($userId, $id)); $statement = null; }
/** * 设置菜单 * * @return bool */ public function set($menus) { if ($menus instanceof Closure) { $menus = $menus($this); } if (!is_array($menus)) { throw new Exception('子菜单必须是数组或者匿名函数返回数组', 1); } Logger::info('Menu', 'before extractMenus'); $menus = $this->extractMenus($menus); Logger::info('Menu', 'before jsonPost'); $this->http->jsonPost(self::API_CREATE, array('button' => $menus)); Logger::info('Menu', 'after jsonPost'); return true; }
public function removeHost($roomId) { Logger::info('RoomService', 'try to remove host for room : ' . $roomId); $this->hostingDao->remove($roomId); Logger::info('RoomService', 'Success to remove host for room : ' . $roomId); }
/** * 检验消息的真实性,并且获取解密后的明文. * <ol> * <li>利用收到的密文生成安全签名,进行签名验证</li> * <li>若验证通过,则提取xml中的加密消息</li> * <li>对消息进行解密</li> * </ol> * * @param string $msgSignature 签名串,对应URL参数的msg_signature * @param string $nonce 随机串,对应URL参数的nonce * @param string $timestamp 时间戳 对应URL参数的timestamp * @param string $postXML 密文,对应POST请求的数据 * * @return array */ public function decryptMsg($msgSignature, $nonce, $timestamp, $content, $isXml = true) { if ($isXml !== true) { $encrypted = $content; } else { //提取密文 $array = XML::parse($content); if (empty($array)) { throw new Exception('Invalid xml.', self::ERROR_PARSE_XML); } $encrypted = $array['Encrypt']; } Logger::info('Crypt', 'encrypted : ' . $encrypted); //验证安全签名 $signature = $this->getSHA1($this->token, $timestamp, $nonce, $encrypted); if ($signature !== $msgSignature) { throw new Exception('Invalid Signature.', self::ERROR_INVALID_SIGNATURE); } $decrypted = $this->decrypt($encrypted, $this->appId); if ($isXml !== true) { return $decrypted; } return XML::parse($decrypted); }
public function leaveRoom($userId) { Logger::info('UserService', 'try to leave room ' . $userId); $this->joiningDao->removeByUserId($userId); $this->updateUserStatus($userId, 'idle'); }
/** * Get the access token from WeChat server. * * @param string $cacheKey * * @return array|bool */ protected function getTokenFromServer() { $http = new Http(); $params = array('corpid' => $this->appId, 'corpsecret' => $this->appSecret); $token = $http->get(self::API_TOKEN_GET, $params); Logger::info('AccessToken', 'token : ' . $token); return $token; }
private function handleRoomLeaving($event) { $userName = $event['FromUserName']; $user = $this->userService->loadUser($userName); $hosting = $this->userService->getHostingRoom($user['id']); if ($hosting) { $roomId = $hosting['room_id']; Logger::info('FunnyGuessServer', 'User is hosting room : ' . $roomId); if ($this->roomService->getNumOfPlayers($roomId) > 1) { $message = '您是当前房间的主持人,请先移交主持人后尝试!'; return Message::make('text')->content($message); } $this->roomService->removeHost($roomId); } $this->userService->leaveRoom($user['id']); $message = '成功退出房间!'; return Message::make('text')->content($message); }