国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 line
4.2KB

  1. <?php
  2. if(!defined('DEDEINC'))
  3. {
  4. exit("Request Error!");
  5. }
  6. require_once(DEDEINC.'/json.class.php');
  7. /*>>dede>>
  8. <name>JSON数据调用类</name>
  9. <type>全局标记</type>
  10. <for>V57</for>
  11. <description>调用某个远程连接的json数据库</description>
  12. <demo>
  13. {dede:json url='http://yoursite/json.php' cache=300}
  14. [field:id/]-[field:title/]<br/>
  15. {/dede:json}
  16. </demo>
  17. <attributes>
  18. <iterm>url:json数据地址</iterm>
  19. <iterm>cache:缓冲时间</iterm>
  20. </attributes>
  21. >>dede>>*/
  22. function lib_json(&$ctag,&$refObj)
  23. {
  24. global $dsql,$sqlCt,$cfg_soft_lang;
  25. $attlist="url|";
  26. FillAttsDefault($ctag->CAttribute->Items,$attlist);
  27. extract($ctag->CAttribute->Items, EXTR_SKIP);
  28. $Innertext = trim($ctag->GetInnerText());
  29. if($url=='' || $Innertext=='') return '';
  30. $ctp = new DedeTagParse();
  31. $ctp->SetNameSpace('field','[',']');
  32. $ctp->LoadSource($Innertext);
  33. $mcache = new MiniCache;
  34. $GLOBALS['autoindex'] = 0;
  35. $chash = md5($url);
  36. if(!$row = $mcache->Get($chash))
  37. {
  38. $content = @file_get_contents($url);
  39. $json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
  40. $row = $json->decode($content);
  41. if($cfg_soft_lang != 'utf-8')
  42. {
  43. $row = AutoCharset($row, 'utf-8', 'gb2312');
  44. }
  45. $mcache->Save($chash, $row, $cache);
  46. }
  47. $revalue = "";
  48. foreach($row as $key => $value)
  49. {
  50. $GLOBALS['autoindex']++;
  51. foreach($ctp->CTags as $tagid=>$ctag)
  52. {
  53. if($ctag->GetName()=='array')
  54. {
  55. $ctp->Assign($tagid,$value);
  56. }
  57. else
  58. {
  59. if( !empty($value[$ctag->GetName()]))
  60. {
  61. $ctp->Assign($tagid,$value[$ctag->GetName()]);
  62. } else {
  63. $ctp->Assign($tagid,"");
  64. }
  65. }
  66. }
  67. $revalue .= $ctp->GetResult();
  68. }
  69. return $revalue;
  70. }
  71. // 一个简单的文件缓存类
  72. class MiniCache
  73. {
  74. var $_cache_path;
  75. function __construct()
  76. {
  77. $this->_cache_path = DEDEDATA.'/cache/json/';
  78. }
  79. // 获取缓冲
  80. function Get($id)
  81. {
  82. if ( ! file_exists($this->_cache_path.$id))
  83. {
  84. return FALSE;
  85. }
  86. $data = $this->_ReadFile($this->_cache_path.$id);
  87. $data = unserialize($data);
  88. if (time() > $data['time'] + $data['ttl'])
  89. {
  90. unlink($this->_cache_path.$id);
  91. return FALSE;
  92. }
  93. return $data['data'];
  94. }
  95. // 清除缓存
  96. function Clean()
  97. {
  98. return $this->_DeleteFiles($this->_cache_path);
  99. }
  100. // 保存缓冲
  101. function Save($id, $data, $ttl = 60)
  102. {
  103. $contents = array(
  104. 'time' => time(),
  105. 'ttl' => $ttl,
  106. 'data' => $data
  107. );
  108. if (PutFile($this->_cache_path.$id, serialize($contents)))
  109. {
  110. @chmod($this->_cache_path.$id, 0777);
  111. return TRUE;
  112. }
  113. return FALSE;
  114. }
  115. // 删除缓冲
  116. function Delete($id)
  117. {
  118. return unlink($this->_cache_path.$id);
  119. }
  120. function _DeleteFiles($path, $del_dir = FALSE, $level = 0)
  121. {
  122. // Trim the trailing slash
  123. $path = rtrim($path, DIRECTORY_SEPARATOR);
  124. if ( ! $current_dir = @opendir($path))
  125. {
  126. return FALSE;
  127. }
  128. while(FALSE !== ($filename = @readdir($current_dir)))
  129. {
  130. if ($filename != "." and $filename != "..")
  131. {
  132. if (is_dir($path.DIRECTORY_SEPARATOR.$filename))
  133. {
  134. // Ignore empty folders
  135. if (substr($filename, 0, 1) != '.')
  136. {
  137. delete_files($path.DIRECTORY_SEPARATOR.$filename, $del_dir, $level + 1);
  138. }
  139. }
  140. else
  141. {
  142. unlink($path.DIRECTORY_SEPARATOR.$filename);
  143. }
  144. }
  145. }
  146. @closedir($current_dir);
  147. if ($del_dir == TRUE AND $level > 0)
  148. {
  149. return @rmdir($path);
  150. }
  151. return TRUE;
  152. }
  153. function _ReadFile($file)
  154. {
  155. if ( ! file_exists($file)) return FALSE;
  156. if (function_exists('file_get_contents')) return file_get_contents($file);
  157. if ( ! $fp = @fopen($file, FOPEN_READ)) return FALSE;
  158. flock($fp, LOCK_SH);
  159. $data = '';
  160. if (filesize($file) > 0) $data =& fread($fp, filesize($file));
  161. flock($fp, LOCK_UN);
  162. fclose($fp);
  163. return $data;
  164. }
  165. }