down.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. defined('IN_PHPCMS') or exit('No permission resources.');
  3. //模型缓存路径
  4. define('CACHE_MODEL_PATH',CACHE_PATH.'caches_model'.DIRECTORY_SEPARATOR.'caches_data'.DIRECTORY_SEPARATOR);
  5. class down {
  6. private $db;
  7. function __construct() {
  8. $this->db = pc_base::load_model('content_model');
  9. }
  10. public function init() {
  11. $a_k = trim($_GET['a_k']);
  12. if(!isset($a_k)) showmessage(L('illegal_parameters'));
  13. $a_k = sys_auth($a_k, 'DECODE', pc_base::load_config('system','auth_key'));
  14. if(empty($a_k)) showmessage(L('illegal_parameters'));
  15. unset($i,$m,$f);
  16. $a_k = safe_replace($a_k);
  17. parse_str($a_k);
  18. if(isset($i)) $i = $id = intval($i);
  19. if(!isset($m)) showmessage(L('illegal_parameters'));
  20. if(!isset($modelid)||!isset($catid)) showmessage(L('illegal_parameters'));
  21. if(empty($f)) showmessage(L('url_invalid'));
  22. $allow_visitor = 1;
  23. $MODEL = getcache('model','commons');
  24. $tablename = $this->db->table_name = $this->db->db_tablepre.$MODEL[$modelid]['tablename'];
  25. $this->db->table_name = $tablename.'_data';
  26. $rs = $this->db->get_one(array('id'=>$id));
  27. $siteids = getcache('category_content','commons');
  28. $siteid = $siteids[$catid];
  29. $CATEGORYS = getcache('category_content_'.$siteid,'commons');
  30. $this->category = $CATEGORYS[$catid];
  31. $this->category_setting = string2array($this->category['setting']);
  32. //检查文章会员组权限
  33. $groupids_view = '';
  34. if ($rs['groupids_view']) $groupids_view = explode(',', $rs['groupids_view']);
  35. if($groupids_view && is_array($groupids_view)) {
  36. $_groupid = param::get_cookie('_groupid');
  37. $_groupid = intval($_groupid);
  38. if(!$_groupid) {
  39. $forward = urlencode(get_url());
  40. showmessage(L('login_website'),APP_PATH.'index.php?m=member&c=index&a=login&forward='.$forward);
  41. }
  42. if(!in_array($_groupid,$groupids_view)) showmessage(L('no_priv'));
  43. } else {
  44. //根据栏目访问权限判断权限
  45. $_priv_data = $this->_category_priv($catid);
  46. if($_priv_data=='-1') {
  47. $forward = urlencode(get_url());
  48. showmessage(L('login_website'),APP_PATH.'index.php?m=member&c=index&a=login&forward='.$forward);
  49. } elseif($_priv_data=='-2') {
  50. showmessage(L('no_priv'));
  51. }
  52. }
  53. //阅读收费 类型
  54. $paytype = $rs['paytype'];
  55. $readpoint = $rs['readpoint'];
  56. if($readpoint || $this->category_setting['defaultchargepoint']) {
  57. if(!$readpoint) {
  58. $readpoint = $this->category_setting['defaultchargepoint'];
  59. $paytype = $this->category_setting['paytype'];
  60. }
  61. //检查是否支付过
  62. $allow_visitor = self::_check_payment($catid.'_'.$id,$paytype,$catid);
  63. if(!$allow_visitor) {
  64. $http_referer = urlencode(get_url());
  65. $allow_visitor = sys_auth($catid.'_'.$id.'|'.$readpoint.'|'.$paytype).'&http_referer='.$http_referer;
  66. } else {
  67. $allow_visitor = 1;
  68. }
  69. }
  70. if(preg_match('/(php|phtml|php3|php4|jsp|dll|asp|cer|asa|shtml|shtm|aspx|asax|cgi|fcgi|pl)(\.|$)/i',$f) || strpos($f, ":\\")!==FALSE || strpos($f,'..')!==FALSE) showmessage(L('url_error'));
  71. if(strpos($f, 'http://') !== FALSE || strpos($f, 'ftp://') !== FALSE || strpos($f, '://') === FALSE) {
  72. $pc_auth_key = md5(pc_base::load_config('system','auth_key').$_SERVER['HTTP_USER_AGENT'].'down');
  73. $a_k = urlencode(sys_auth("i=$i&d=$d&s=$s&t=".SYS_TIME."&ip=".ip()."&m=".$m."&f=$f&modelid=".$modelid, 'ENCODE', $pc_auth_key));
  74. $downurl = '?m=content&c=down&a=download&a_k='.$a_k;
  75. } else {
  76. $downurl = $f;
  77. }
  78. include template('content','download');
  79. }
  80. public function download() {
  81. $a_k = trim($_GET['a_k']);
  82. $pc_auth_key = md5(pc_base::load_config('system','auth_key').$_SERVER['HTTP_USER_AGENT'].'down');
  83. $a_k = sys_auth($a_k, 'DECODE', $pc_auth_key);
  84. if(empty($a_k)) showmessage(L('illegal_parameters'));
  85. unset($i,$m,$f,$t,$ip);
  86. $a_k = safe_replace($a_k); parse_str($a_k);
  87. if(isset($i)) $downid = intval($i);
  88. if(!isset($m)) showmessage(L('illegal_parameters'));
  89. if(!isset($modelid)) showmessage(L('illegal_parameters'));
  90. if(empty($f)) showmessage(L('url_invalid'));
  91. if(!$i || $m<0) showmessage(L('illegal_parameters'));
  92. if(!isset($t)) showmessage(L('illegal_parameters'));
  93. if(!isset($ip)) showmessage(L('illegal_parameters'));
  94. $starttime = intval($t);
  95. if(preg_match('/(php|phtml|php3|php4|jsp|dll|asp|cer|asa|shtml|shtm|aspx|asax|cgi|fcgi|pl)(\.|$)/i',$f) || strpos($f, ":\\")!==FALSE || strpos($f,'..')!==FALSE) showmessage(L('url_error'));
  96. $fileurl = trim($f);
  97. if(!$downid || empty($fileurl) || !preg_match("/[0-9]{10}/", $starttime) || !preg_match("/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/", $ip) || $ip != ip()) showmessage(L('illegal_parameters'));
  98. $endtime = SYS_TIME - $starttime;
  99. if($endtime > 3600) showmessage(L('url_invalid'));
  100. if($m) $fileurl = trim($s).trim($fileurl);
  101. if(preg_match('/(php|phtml|php3|php4|jsp|dll|asp|cer|asa|shtml|shtm|aspx|asax|cgi|fcgi|pl)(\.|$)/i',$fileurl) ) showmessage(L('url_error'));
  102. //远程文件
  103. if(strpos($fileurl, ':/') && (strpos($fileurl, pc_base::load_config('system','upload_url')) === false)) {
  104. header("Location: $fileurl");
  105. } else {
  106. if($d == 0) {
  107. header("Location: ".$fileurl);
  108. } else {
  109. $fileurl = str_replace(array(pc_base::load_config('system','upload_url'),'/'), array(pc_base::load_config('system','upload_path'),DIRECTORY_SEPARATOR), $fileurl);
  110. $filename = basename($fileurl);
  111. //处理中文文件
  112. if(preg_match("/^([\s\S]*?)([\x81-\xfe][\x40-\xfe])([\s\S]*?)/", $fileurl)) {
  113. $filename = str_replace(array("%5C", "%2F", "%3A"), array("\\", "/", ":"), urlencode($fileurl));
  114. $filename = urldecode(basename($filename));
  115. }
  116. $ext = fileext($filename);
  117. $filename = date('Ymd_his').random(3).'.'.$ext;
  118. $fileurl = str_replace(array('<','>'), '',$fileurl);
  119. file_down($fileurl, $filename);
  120. }
  121. }
  122. }
  123. /**
  124. * 检查支付状态
  125. */
  126. private function _check_payment($flag,$paytype,$catid) {
  127. $_userid = param::get_cookie('_userid');
  128. $_username = param::get_cookie('_username');
  129. $siteids = getcache('category_content','commons');
  130. $siteid = $siteids[$catid];
  131. $CATEGORYS = getcache('category_content_'.$siteid,'commons');
  132. $this->category = $CATEGORYS[$catid];
  133. $this->category_setting = string2array($this->category['setting']);
  134. if(!$_userid) return false;
  135. pc_base::load_app_class('spend','pay',0);
  136. $setting = $this->category_setting;
  137. $repeatchargedays = intval($setting['repeatchargedays']);
  138. if($repeatchargedays) {
  139. $fromtime = SYS_TIME - 86400 * $repeatchargedays;
  140. $r = spend::spend_time($_userid,$fromtime,$flag);
  141. if($r['id']) return true;
  142. }
  143. return false;
  144. }
  145. /**
  146. * 检查阅读权限
  147. *
  148. */
  149. private function _category_priv($catid) {
  150. $catid = intval($catid);
  151. if(!$catid) return '-2';
  152. $_groupid = param::get_cookie('_groupid');
  153. $_groupid = intval($_groupid);
  154. if($_groupid==0) $_groupid = 8;
  155. $this->category_priv_db = pc_base::load_model('category_priv_model');
  156. $result = $this->category_priv_db->select(array('catid'=>$catid,'is_admin'=>0,'action'=>'visit'));
  157. if($result) {
  158. if(!$_groupid) return '-1';
  159. foreach($result as $r) {
  160. if($r['roleid'] == $_groupid) return '1';
  161. }
  162. return '-1';
  163. } else {
  164. return '1';
  165. }
  166. }
  167. }
  168. ?>