public static function realtime($log) { $log = json_encode(array('log' => $log)); try { $http = new Http(array(CURLOPT_URL => Config::getConfig('local_base_url') . 'socket/log', CURLOPT_POSTFIELDS => $log, CURLOPT_HTTPHEADER => array('Content-Type: application/json', 'Content-Length: ' . strlen($log)), CURLOPT_TIMEOUT => 1)); } catch (\Exception $e) { //pass } }
public function socket($data) { $msg = json_encode(array('push' => $data), true); $baseUrl = Config::getConfig('local_base_url'); try { $http = new Http(array(CURLOPT_URL => $baseUrl . 'socket/' . $data['uid'], CURLOPT_POSTFIELDS => $msg, CURLOPT_HTTPHEADER => array('Content-Type: application/json', 'Content-Length: ' . strlen($msg)), CURLOPT_TIMEOUT => 1)); } catch (\Exception $e) { return false; } return true; }
public function __construct($sid, $passwd) { if (empty($sid) || empty($passwd)) { throw new \Exception('请输入一卡通查询密码:', Config::RETURN_NEED_PASSWORD); } $this->sid = $sid; $this->passwd = $passwd; //一卡通基地址 $this->baseUrl = Config::getConfig('card_base_url'); //登录一卡通 $this->login(); }
public function scoreAll($scope, $course, $re = true) { if (empty($this->sid) || empty($course)) { throw new \Exception('参数有误', Config::RETURN_ERROR); } //获取学生信息 $sql = 'SELECT `class`, `major`, `grade`, `school` FROM `student` WHERE `sid` = ? LIMIT 1'; $result = Mysql::execute($sql, array($this->sid)); $this->class = $result[0]['class']; $this->major = $result[0]['major']; $this->grade = $result[0]['grade']; $this->school = $result[0]['school']; //获取所有学生列表 if ('class' === $scope) { $sql = "SELECT `a`.`sid`, `a`.`name`, `a`.`idcard`,\n IF(`b`.`score` IS NULL, '[]', `b`.`score`) `score`\n FROM `student` `a`\n LEFT JOIN `score` `b` ON `a`.`sid` = `b`.`sid`\n WHERE `a`.`class` = ?"; $students = Mysql::execute($sql, array($this->class)); $this->scoreName = $this->class; } else { $sql = "SELECT `a`.`sid`, `a`.`name`, `a`.`idcard`,\n IF(`b`.`score` IS NULL, '[]', `b`.`score`) `score`\n FROM `student` `a`\n LEFT JOIN `score` `b` ON `a`.`sid` = `b`.`sid`\n WHERE `a`.`major` = ? AND `a`.`grade` = ? AND `a`.`school` = ?"; $students = Mysql::execute($sql, array($this->major, $this->grade, $this->school)); $this->scoreName = $this->major; } $failures = array(); foreach ($students as $student) { $score = json_decode($student['score'], true); foreach ($score as $termScore) { foreach ($termScore as $courseScore) { if ($courseScore['course'] == $course) { $this->credit = empty($this->credit) ? $courseScore['credit'] : $this->credit; $data[] = array("sid" => $student['sid'], "name" => $student['name'], "score" => $courseScore['mark'], 'resit' => $courseScore['resit']); } } } $temp = empty($data) ? array() : end($data); if ($temp['sid'] != $student['sid']) { $failures[] = array('sid' => $student['sid'], 'name' => $student['name'], 'idcard' => $student['idcard']); } } //多线程与多进程更新成绩 if (\Hnust\input('re\\b', false)) { $failures = $students; } if (!empty($failures) && $re) { $url = Config::getConfig('local_base_url') . 'Update/score'; Http::multi($url, $failures); return $this->scoreAll($scope, $course, false); } elseif (!empty($failures)) { foreach ($failures as $student) { $data[] = array('sid' => $student['sid'], 'name' => $student['name'], 'score' => '-1', 'resit' => false); } } return $data; }
protected static function init() { //实例化数据缓存 if (is_null(self::$cache)) { self::$cache = new Cache('wechat'); } //获取微信Secret if (is_null(self::$secret)) { self::$secret = Config::getConfig('wechat_secret'); } //获取基地址 if (is_null(self::$baseUrl)) { self::$baseUrl = Config::getConfig('local_base_url'); } }
public function getScore() { $cache = new Cache('score'); $cacheData = $cache->get($this->sid); if (time() - $cacheData['time'] <= \Hnust\Config::getConfig('score_cache_time')) { return $cacheData['data']; } try { $score = parent::getScore(); //从成绩中获取个人信息 $sid = $score['info']['sid']; $class = $score['info']['class']; $major = $score['info']['major']; $college = $score['info']['college']; $grade = $class[0] . $class[1] . "级"; $time = $score['info']['time']; $score = $score['data']; //更新资料表 $sql = 'UPDATE `student` SET `sid` = ?, `class` = ?, `major` = ?, `college` = ?, `grade` = ? WHERE `sid` = ?'; Mysql::execute($sql, array($sid, $class, $major, $college, $grade, $this->sid)); //更新成绩表 $sql = "INSERT INTO `score`(`sid`, `score`, `time`) VALUES(?, ?, ?)\n ON DUPLICATE KEY UPDATE `score` = ?, `time` = ?"; $jsonScore = json_encode($score, JSON_UNESCAPED_UNICODE); Mysql::execute($sql, array($sid, $jsonScore, $time, $jsonScore, $time)); $cache->set($this->sid, array('time' => time(), 'data' => $score)); } catch (\Exception $e) { if ($e->getCode() !== Config::RETURN_ERROR) { throw new \Exception($e->getMessage(), $e->getCode()); //从数据库中获取成绩缓存 } elseif (empty($cacheData)) { $sql = 'SELECT `score`, `time` FROM `score` WHERE `sid` = ?'; $result = Mysql::execute($sql, array($this->sid)); if (!empty($result)) { $cacheData = array('time' => $result[0]['time'], 'data' => json_decode($result[0]['score'], true)); } } //无缓存抛出异常 if (empty($cacheData)) { throw new \Exception($e->getMessage(), $e->getCode()); } $time = date('Y-m-d H:i:s', $cacheData['time']); $this->error = $e->getMessage() . "\n当前数据更新时间为:" . $time; $score = $cacheData['data']; } return $score; }
public function getTuition() { $cache = new Cache('tuition'); //$cacheData = $cache->get($this->sid); if (time() - $cacheData['time'] <= Config::getConfig('tuition_cache_time')) { return $cacheData['data']; } try { $tuition = parent::getTuition(); $cache->set($this->sid, array('time' => time(), 'data' => $tuition)); } catch (\Exception $e) { if (empty($cacheData) || $e->getCode() !== Config::RETURN_ERROR) { throw new \Exception($e->getMessage(), $e->getCode()); } $time = date('Y-m-d H:i:s', $cacheData['time']); $this->error = $e->getMessage() . "\n当前数据更新时间为:" . $time; $tuition = $cacheData['data']; } return $tuition; }
public function __construct($sid, $passwd = '', $nocache = false) { if (empty($sid)) { throw new \Exception('服务器错误,学号为空', Config::RETURN_ERROR); } $this->sid = $sid; $this->passwd = $passwd; //请求需要用到的网址 if ($this->sid[2] < 3) { $this->school = '科大本部'; $this->baseUrl = Config::getConfig('kdjw_base_url'); $this->referer = 'http://kdjw.hnust.cn/kdjw/Logon.do?method=logon'; } else { $this->school = '潇湘学院'; $this->baseUrl = Config::getConfig('xxjw_base_url'); $this->referer = 'http://xxjw.hnust.cn/xxjw/Logon.do?method=logon'; } $this->logined = false; $this->cache = new Cache('cookies_jwc'); $this->cookies = $nocache ? '' : $this->cache->get($this->sid); }
public static function sms($phone, $template, $smsParam = array()) { if (empty($phone)) { return false; } $smsInfo = Config::getConfig('sms_info'); $smsInfo = json_decode($smsInfo, true); $smsParam = json_encode($smsParam); $c = new \TopClient(); $c->appkey = $smsInfo['appkey']; $c->secretKey = $smsInfo['secretKey']; $req = new \AlibabaAliqinFcSmsNumSendRequest(); $req->setExtend($phone); $req->setSmsType("normal"); $req->setSmsFreeSignName($smsInfo['signName']); $req->setSmsParam($smsParam); $req->setRecNum($phone); $req->setSmsTemplateCode($template); $resp = $c->execute($req); return !!$resp->result->success; }
public function getCredit() { //读取缓存 $cache = new Cache('credit'); $cacheData = $cache->get($this->sid); //判断缓存时间 if (time() - $cacheData['time'] <= Config::getConfig('credit_cache_time')) { return $cacheData['data']; } try { //获取最新学分绩点 $credit = parent::getCredit(); $cache->set($this->sid, array('time' => time(), 'data' => $credit)); } catch (\Exception $e) { if (empty($cacheData) || $e->getCode() !== Config::RETURN_ERROR) { throw new \Exception($e->getMessage(), $e->getCode()); } //读取旧记录 $time = date('Y-m-d H:i:s', $cacheData['time']); $this->error = $e->getMessage() . "\n当前数据更新时间为:" . $time; $credit = $cacheData['data']; } return $credit; }
public function schedule() { //获取起始学号 $this->getCache(__FUNCTION__); if (empty($this->sid)) { return $this->log('', '课表更新', '学号为空,不更新课表'); } //全负荷运行 Config::fullLoad(); $sql = "SELECT `a`.`sid`, `a`.`school` FROM `student` `a`\n LEFT JOIN `schedule` `b` ON `a`.`sid` = `b`.`sid`\n WHERE (`b`.`sid` IS NULL OR (`b`.`time` + INTERVAL 1 WEEK) < NOW())\n AND `a`.`sid` > ? LIMIT 1"; $result = Mysql::execute($sql, array($this->sid)); if (empty($result)) { return $this->log('', '课表更新', '课表更新完成'); } $this->sid = $result[0]['sid']; $term = Config::getConfig('current_term'); $school = $result[0]['school']; $schedule = new \Hnust\Analyse\Schedule($this->sid, ''); $sql = 'SELECT `a`.`sid` FROM `student` `a` LEFT JOIN `schedule` `b` ON `a`.`sid` = `b`.`sid` WHERE `a`.`sid` >= ? AND `a`.`school` = ? AND (`b`.`sid` IS NULL OR (`b`.`time` + INTERVAL 1 WEEK) < NOW()) LIMIT 100'; $students = Mysql::execute($sql, array($this->sid, $school)); foreach ($students as $student) { try { $schedule->getSchdule($student['sid'], $term); $this->log($student['sid'], '课表更新', '课表更新至' . $student['sid']); } catch (\Exception $e) { //pass } } //递归 $this->setCache(__FUNCTION__, $this->sid); $this->recursion(__FUNCTION__); }
public function getSchdule($sid, $term, $type = 0, $week = -1) { $cache = new Cache('schedule'); $cacheData = $cache->get($sid); $cacheData = empty($cacheData) ? array() : $cacheData; if (time() - $cacheData[$term]['time'] <= \Hnust\Config::getConfig('schedule_cache_time')) { return $this->type($cacheData[$term]['data'], $type, $week); } try { $schedule = parent::getSchdule($sid, $term); //更新课表数据库 if ($term === Config::getConfig('current_term')) { $sql = "INSERT INTO `schedule`(`sid`, `schedule`, `term`, `time`) VALUES(?, ?, ?, ?)\n ON DUPLICATE KEY UPDATE `schedule` = ?, `time` = ?"; $jsonSchedule = json_encode($schedule, JSON_UNESCAPED_UNICODE); Mysql::execute($sql, array($sid, $jsonSchedule, $term, $time, $jsonSchedule, $time)); } //设置缓存 $cacheData[$term] = array('time' => time(), 'data' => $schedule); $cache->set($sid, $cacheData); } catch (\Exception $e) { if (empty($cacheData[$term]) || $e->getCode() !== Config::RETURN_ERROR) { throw new \Exception($e->getMessage(), $e->getCode()); } $time = date('Y-m-d H:i:s', $cacheData[$term]['time']); $this->error = $e->getMessage() . "\n当前数据更新时间为:" . $time; $schedule = $cacheData[$term]['data']; } return $this->type($schedule, $type, $week); }
public function log() { //清理日志文件 $logSuffix = '.log'; $logLength = 5000; $path = Config::BASE_PATH . Config::LOGS_PATH . '/'; $handle = opendir($path); while ($handle && false !== ($file = readdir($handle))) { $suffix = strrchr(strtolower($file), $logSuffix); if ($suffix !== $logSuffix) { continue; } $content = file_get_contents($path . $file); $content = explode("\n", $content); $count = count($content); if ($count > $logLength) { $content = array_slice($content, $count - $logLength); $content = implode("\n", $content); file_put_contents($path . $file, $content); } } closedir($handle); //清除日志 $sql = "DELETE FROM `logs`\n WHERE `uid` IN ('1305010117', '1305010119', '1305020233')\n OR (`ip` != ? AND `location` LIKE '%阿里%')\n OR`time` < DATE_SUB(NOW(), INTERVAL 3 MONTH)"; $num = Mysql::execute($sql, array(Config::getConfig('local_out_ip'))); $sql = "DELETE FROM `logs` WHERE `uid` != ''\n AND `uid` NOT IN (SELECT `uid` FROM `user`)"; $num += Mysql::execute($sql); $log = "清除日志{$num}条"; //更新日志 $sql = "UPDATE `student` `s`, `logs` `l`\n SET `l`.`key` = `s`.`name`\n WHERE `l`.`key` = `s`.`sid`"; $num = Mysql::execute($sql); $sql = "UPDATE `logs` SET `key` = ''\n WHERE `key` in (`uid`, `name`, '湖南科技大学', '计算机科学与工程学院')"; $num += Mysql::execute($sql); return $log . ",更新日志{$num}条"; }
public function user() { $sql = 'SELECT `u`.`uid`, `u`.`rank`, `u`.`apiToken`, `s`.`name`, `s`.`class`, `s`.`major`, `s`.`college`, `s`.`mail`, `s`.`phone`, `s`.`qq` FROM `user` `u`, `student` `s` WHERE `u`.`uid` = `s`.`sid` AND `u`.`uid` = ?'; $result = Mysql::execute($sql, array($this->uid)); $this->info = empty($result) ? array() : $result[0]; $this->info['token'] = $this->token; //当前学期/周次 $this->info['week'] = \Hnust\week(); $this->info['term'] = Config::getConfig('current_term'); //头像地址 $this->info['avatar'] = Config::getConfig('local_base_url'); if (empty($this->info['qq'])) { $this->info['avatar'] .= "avatar/qy/{$this->info['uid']}"; } else { $this->info['avatar'] .= "avatar/qq/{$this->info['qq']}"; } //用户所在群组 $group = new \Hnust\Analyse\Group(); $this->info['group'] = $group->belong($this->uid); //数组合并 $this->info = array_merge($this->info, array('sid' => $this->sid, 'isAdmin' => $this->rank === Config::RANK_ADMIN)); return $this->info; }
public function schedule() { $term = Config::getConfig('current_term'); $schedule = new \Hnust\Analyse\Schedule($this->sid); $data = $schedule->getSchdule($this->sid, $term, 1); //获取周次与星期 $week = \Hnust\week(); $day = date('w'); $isToday = true; if (date('H') >= 21) { $isToday = false; $day = ($day + 1) % 7; } $day = $day ? $day : 7; //获取当天课表 $data = $data[$week][$day]; $session = array('', '一、二', '三、四', '五、六', '七、八', '九、十'); $content = ''; for ($i = 1; $i <= 5; $i++) { if (empty($data[$i])) { continue; } $content .= "第{$session[$i]}节:\n课程:{$data[$i]['course']}\n教室:{$data[$i]['classroom']}\n\n"; } //回复 if ($content) { $content = ($isToday ? '今' : '明') . "日课表如下:\n\n" . $content; if ($schedule->error) { $content .= $schedule->error; } $this->data = trim($content); } else { $this->data = ($isToday ? '今' : '明') . '日无课,但不要太放松哦'; } }
public function rank() { $by = \Hnust\input('by', 'term'); $scope = \Hnust\input('scope', 'class'); $term = \Hnust\input('term'); if (empty($term) || !in_array(strlen($term), array(9, 11))) { $term = Config::getConfig('current_term'); } elseif ('term' === $by && 11 !== strlen($term)) { $term = Config::getConfig('current_term'); } $term = 'term' !== $by ? substr($term, 0, 9) : $term; $isDownload = \Hnust\input('download/b', false); $rank = new \Hnust\Analyse\Rank($this->sid); if ($isDownload) { $rank->getExcel($this->uid, $term, $scope, $by); } else { $this->data = $rank->getRank($term, $scope, $by); $this->info = array('sid' => $this->sid, 'by' => $by, 'scope' => $scope, 'term' => $term, 'class' => $rank->class, 'major' => $rank->major, 'terms' => $rank->terms, 'courses' => $rank->courses, 'rankName' => $rank->rankName); } }
public function __construct($sid, $passwd = '') { $this->sid = $sid; $this->passwd = $passwd; $this->baseUrl = Config::getConfig('cwc_base_url'); }
public function network() { //设置日志文件 $this->logFileName = 'network'; //获取最新数据 $net = file_get_contents('/proc/net/dev'); $pattern = "/eth1:\\s*(\\d+)\\s+(\\d+)\\s+\\d+\\s+\\d+\\s+\\d+\\s+\\d+\\s+\\d+\\s+\\d+\\s+(\\d+)\\s+(\\d+)/"; preg_match($pattern, $net, $matches); $newRes = array('in' => array('bytes' => $matches[1], 'packets' => $matches[2]), 'out' => array('bytes' => $matches[3], 'packets' => $matches[4]), 'time' => time()); //处理缓存数据 $cache = new Cache('remind_network'); $oldRes = $cache->get('res'); $cache->set('res', $newRes); if (empty($oldRes)) { return; } //判断是否提醒 $minutes = number_format(($newRes['time'] - $oldRes['time']) / 60, 2); $bytes = $newRes['out']['bytes'] - $oldRes['out']['bytes']; $currentSize = \Hnust\sizeFormat($bytes); $totalSize = \Hnust\sizeFormat($newRes['out']['bytes']); $remindValue = Config::getConfig('network_remind_value'); $remindUser = Config::getConfig('network_remind_user'); if ($bytes > $remindValue) { $sql = 'SELECT `sid`, `name`, `mail` FROM `student` WHERE `sid` = ?'; $result = Mysql::execute($sql, array($remindUser)); $student = $result[0]; $title = '服务器流量异常提醒 -- Tick团队'; $content = "尊敬的管理员您好,系统检测服务器出网流量在 {$minutes} 分钟内共消耗了 {$currentSize},累计使用 {$totalSize},请及时处理!"; $this->remind($student, $title, $content, '', '0100'); } $this->record("{$minutes} 分钟内共消耗出网流量 {$currentSize},累计使用 {$totalSize}"); }
public function addQueue($title, $url) { //判断是否开启选课功能 if ('是' !== Config::getConfig('is_elective')) { throw new \Exception('加入队列失败,原因:管理员已关闭选课功能', Config::RETURN_ALERT); } $sql = "INSERT INTO `elective_queue` (`sid`, `title`, `url`, `time`)\n VALUES (?, ?, ?, CURRENT_TIMESTAMP)"; $sqlArr = array($this->sid, $title, $url); if (!Mysql::execute($sql, $sqlArr)) { throw new \Exception('加入队列失败,数据库异常', Config::RETURN_ALERT); } $id = Mysql::lastInsertId(); $baseUrl = Config::getConfig('local_base_url'); try { $http = new Http(array(CURLOPT_URL => $baseUrl . 'remind/electiveQueue?id=' . $id, CURLOPT_TIMEOUT => 1)); } catch (\Exception $e) { //pass } return array('title' => $title, 'result' => '', 'time' => date('Y-m-d H:i:s', time())); }
public function update() { $type = \Hnust\input('type'); $start = \Hnust\input('start'); $cookie = \Hnust\input('cookie'); $cache = new Cache('update'); //更新缓存数据 $cacheData = $cache->get($type); $cacheData = empty($cacheData) ? array() : $cacheData; if (!empty($start)) { $cacheData['start'] = $start; } if (!empty($cookie)) { $cacheData['cookie'] = $cookie; } $cache->set($type, $cacheData); $url = Config::getConfig('local_base_url') . 'update/' . $type; try { new Http(array(CURLOPT_URL => $url, CURLOPT_TIMEOUT => 1)); } catch (\Exception $e) { //pass } $this->msg = '已加入更新队列,请通过实时日志查看更新进度'; }