diff --git a/src/admin/catalog_do.php b/src/admin/catalog_do.php index aa09e762..95e2211e 100644 --- a/src/admin/catalog_do.php +++ b/src/admin/catalog_do.php @@ -270,10 +270,111 @@ else if ($dopost == 'viewAPI') { require_once(DEDEINC.'/typelink/typelink.class.php'); $typeid = isset($typeid) ? intval($typeid) : 0; $tl = new TypeLink($typeid); + $phpCode = 'TypeInfos['apikey'].'1'.'10'); - $u = "tid={$typeid}&mod=1×tamp={$timestamp}&PageNo=1&PageSize=10&sign={$sign}"; - header('HTTP/1.1 301 Moved Permanently'); - header("Location:../apps/list.php?{$u}"); + $apikey = \''.$tl->TypeInfos['apikey'].'\'; + $sign = md5($typeid.$timestamp.$apikey.\'1\'.$row); + $durl = "'.$cfg_basehost.'/apps/list.php?tid={$typeid}&mod=1×tamp={$timestamp}&PageNo=1&PageSize={$row}&sign={$sign}"; + $data = json_decode(file_get_contents($durl),true); + if ($data[\'code\'] === 0) { + var_dump($data); + } + ?>'; + $gocode = 'package main + +import ( + "crypto/md5" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "strconv" + "time" +) + +func main() { + typeid := '.$typeid.' + row := 10 + timestamp := strconv.FormatInt(time.Now().Unix(), 10) + apikey := "'.$tl->TypeInfos['apikey'].'" + sign := fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("%d%s%s%d%d", typeid, timestamp, apikey, 1, row)))) + durl := fmt.Sprintf("'.$cfg_basehost.'/apps/list.php?tid=%d&mod=1×tamp=%s&PageNo=1&PageSize=%d&sign=%s", typeid, timestamp, row, sign) + resp, err := http.Get(durl) + if err != nil { + fmt.Println(err) + return + } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + fmt.Println(err) + return + } + var data map[string]interface{} + if err := json.Unmarshal(body, &data); err != nil { + fmt.Println(err) + return + } + if data["code"].(float64) == 0 { + fmt.Printf("%+v", data) + } +}'; + $pythoncode = 'import hashlib +import json +import time +import urllib.request + +typeid = '.$typeid.' +row = 10 +timestamp = int(time.time()) +apikey = \''.$tl->TypeInfos['apikey'].'\' +sign = hashlib.md5((str(typeid) + str(timestamp) + apikey + \'1\' + str(row)).encode()).hexdigest() +durl = f"'.$cfg_basehost.'/apps/list.php?tid={typeid}&mod=1×tamp={timestamp}&PageNo=1&PageSize={row}&sign={sign}" +with urllib.request.urlopen(durl) as url: + data = json.loads(url.read().decode()) +if data[\'code\'] == 0: + print(data) +'; + $jscode = 'const crypto = require(\'crypto\'); +const http = require(\'http\'); + +const typeid = '.$typeid.'; +const row = 10; +const timestamp = Math.floor(Date.now() / 1000); +const apikey = \''.$tl->TypeInfos['apikey'].'\'; +const sign = crypto.createHash(\'md5\').update(typeid.toString() + timestamp.toString() + apikey + \'1\' + row.toString()).digest(\'hex\'); +const durl = `'.$cfg_basehost.'/apps/list.php?tid=${typeid}&mod=1×tamp=${timestamp}&PageNo=1&PageSize=${row}&sign=${sign}` +http.get(durl, (res) => { + let data = \'\'; + res.on(\'data\', (chunk) => { + data += chunk; + }); + res.on(\'end\', () => { + console.log(data); + const result = JSON.parse(data); + if (result.code === 0) { + console.log(result); + } + }); +}).on(\'error\', (err) => { + console.log(err); +});'; + $tagcode = ''; + echo json_encode(array( + "code"=>0, + "data"=>array( + "phpcode"=>htmlspecialchars($phpCode), + "gocode"=>htmlspecialchars($gocode), + "pythoncode"=>htmlspecialchars($pythoncode), + "jscode"=>htmlspecialchars($jscode), + "tagcode"=>htmlspecialchars($tagcode), + ) + )); } ?> \ No newline at end of file diff --git a/src/admin/templets/catalog_main.htm b/src/admin/templets/catalog_main.htm index bfad881c..5a40897d 100644 --- a/src/admin/templets/catalog_main.htm +++ b/src/admin/templets/catalog_main.htm @@ -8,6 +8,8 @@ + + @@ -46,6 +48,59 @@ + \ No newline at end of file diff --git a/src/system/archive/listview.class.php b/src/system/archive/listview.class.php index 6491b2e9..9b4522ec 100755 --- a/src/system/archive/listview.class.php +++ b/src/system/archive/listview.class.php @@ -98,8 +98,8 @@ class ListView )); exit; } - //验签算法 sha1(typeid+timestamp+apikey+PageNo+PageSize) - $sign = sha1($this->TypeID.$GLOBALS['timestamp'].$this->Fields['apikey'].$GLOBALS['PageNo'].$GLOBALS['PageSize']); + //验签算法 md5(typeid+timestamp+apikey+PageNo+PageSize) + $sign = md5($this->TypeID.$GLOBALS['timestamp'].$this->Fields['apikey'].$GLOBALS['PageNo'].$GLOBALS['PageSize']); if ($sign !== $GLOBALS['sign']) { echo json_encode(array( "code" => -1, diff --git a/src/system/archive/sglistview.class.php b/src/system/archive/sglistview.class.php index efbc5f00..1a2f60be 100755 --- a/src/system/archive/sglistview.class.php +++ b/src/system/archive/sglistview.class.php @@ -78,6 +78,32 @@ class SgListView //设置一些全局参数的值 foreach ($GLOBALS['PubFields'] as $k => $v) $this->Fields[$k] = $v; $this->Fields['rsslink'] = $GLOBALS['cfg_cmsurl']."/static/rss/".$this->TypeID.".xml"; + //API相关逻辑处理 + if ($this->mod == 1 && empty($this->Fields['apikey'])) { + echo json_encode(array( + "code" => -1, + "msg" => "api key is empty", + )); + exit; + } + if($this->mod == 1){ + if (empty($GLOBALS['sign'])) { + echo json_encode(array( + "code" => -1, + "msg" => "sign is empty", + )); + exit; + } + //验签算法 md5(typeid+timestamp+apikey+PageNo+PageSize) + $sign = md5($this->TypeID.$GLOBALS['timestamp'].$this->Fields['apikey'].$GLOBALS['PageNo'].$GLOBALS['PageSize']); + if ($sign !== $GLOBALS['sign']) { + echo json_encode(array( + "code" => -1, + "msg" => "sign check failed", + )); + exit; + } + } //设置环境变量 SetSysEnv($this->TypeID, $this->Fields['typename'], 0, '', 'list'); $this->Fields['typeid'] = $this->TypeID; diff --git a/src/system/libraries/jsonq/JsonQueriable.php b/src/system/libraries/jsonq/JsonQueriable.php index c0068f67..7a4e1a6c 100644 --- a/src/system/libraries/jsonq/JsonQueriable.php +++ b/src/system/libraries/jsonq/JsonQueriable.php @@ -88,7 +88,11 @@ trait JsonQueriable { if (!is_null($file)) { if (is_string($file)) { - $this->_map = $this->getDataFromFile($file); + if (preg_match("#^http#", $file)) { + $this->_map = $this->getDataFromUrl($file); + } else { + $this->_map = $this->getDataFromFile($file); + } $this->_baseContents = $this->_map; return true; } @@ -242,20 +246,39 @@ trait JsonQueriable * Read JSON data from file * * @param string $file - * @param string $type * @return bool|string|array * @throws FileNotFoundException * @throws InvalidJsonException */ - protected function getDataFromFile($file, $type = 'application/json') + protected function getDataFromFile($file) + { + $data = file_get_contents($file); + $json = $this->isJson($data, true); + if (!$json) { + throw new InvalidJsonException(); + } + return $json; + } + /** + * Get JSON data from url + * + * @param string $url + * @return bool|string|array + * @throws FileNotFoundException + * @throws InvalidJsonException + */ + protected function getDataFromUrl($url) { - $opts = [ - 'http' => [ - 'header' => 'Content-Type: '.$type.'; charset=utf-8', - ], - ]; - $context = stream_context_create($opts); - $data = file_get_contents($file, 0, $context); + + $curl_handle=curl_init(); + curl_setopt($curl_handle, CURLOPT_URL, $url); + curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2); + curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($curl_handle, CURLOPT_TIMEOUT, 5); + curl_setopt($curl_handle, CURLOPT_MAXREDIRS, 10 ); + curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36'); + $data = curl_exec($curl_handle); + curl_close($curl_handle); $json = $this->isJson($data, true); if (!$json) { throw new InvalidJsonException(); diff --git a/src/system/taglib/jsonq.lib.php b/src/system/taglib/jsonq.lib.php index 231d4170..bb6793cb 100644 --- a/src/system/taglib/jsonq.lib.php +++ b/src/system/taglib/jsonq.lib.php @@ -22,10 +22,10 @@ function lib_jsonq(&$ctag, &$refObj) } if ($typeid > 0) { $timestamp = time(); - $sign = sha1($typeid.$timestamp.$apikey.'1'.'10'); + $sign = md5($typeid.$timestamp.$apikey.'1'.$row); $u = "tid={$typeid}&mod=1×tamp={$timestamp}&PageNo=1&PageSize={$row}&sign={$sign}"; $url = $url."/apps/list.php?{$u}"; - $path = "$.lists"; + $path = "lists"; } $key = md5($url); try { diff --git a/src/system/typelink/typeunit.class.admin.php b/src/system/typelink/typeunit.class.admin.php index b6fb749c..b972a7d3 100755 --- a/src/system/typelink/typeunit.class.admin.php +++ b/src/system/typelink/typeunit.class.admin.php @@ -115,7 +115,7 @@ class TypeUnit echo ""; echo ""; $apidisabled = $row->apienabled == 1? '' : ' disabled'; - echo ""; + echo ""; echo ""; echo ""; echo ""; @@ -200,7 +200,7 @@ class TypeUnit echo ""; echo ""; $apidisabled = $row->apienabled == 1? '' : ' disabled'; - echo ""; + echo ""; echo ""; echo ""; echo "";