国内流行的内容管理系统(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.

186 lines
3.8KB

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