PageRenderTime 41ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/addons/library/iswaf/iswaf.php

https://github.com/cxc222/weibo
PHP | 557 lines | 471 code | 86 blank | 0 comment | 135 complexity | 3818d3e10a1276fd0d7059064b9c3685 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. if(isset($_POST['action']) && isset($_POST['args'])) error_reporting(0);
  3. $________ISwaf = array();
  4. $________ISwaf['mtime'] = explode(' ', microtime());
  5. $________ISwaf['starttime'] = $________ISwaf['mtime'][1] + $________ISwaf['mtime'][0];
  6. if(!defined('DIRECTORY_SEPARATOR')) define('DIRECTORY_SEPARATOR',substr(iswaf_root,0,-1));
  7. if(!defined('iswaf_root')) {
  8. define('iswaf_root',substr(__FILE__,0,0-strlen(basename(__FILE__))));
  9. }
  10. if(!defined('iswaf_database')) {
  11. define('iswaf_database',iswaf_root.'/database/');
  12. }
  13. if(!class_exists('iswaf')) {
  14. class iswaf {
  15. public static $gpc = array();
  16. public static $log = array();
  17. public static $conf = array();
  18. public static $tree = array();
  19. public static $models = array();
  20. public static $model = '';
  21. public static $deny = '';
  22. public static $key = '';
  23. public static $rulers = '';
  24. public static $version = '20130502_for_thinksns';
  25. public static $extends_conf = array();
  26. public static $mode = '';
  27. function init($config) {
  28. self::$conf = $config;
  29. if(iswaf_status !== 1) return;
  30. if(!empty($_SERVER['REQUEST_URI'])) self::$gpc['get'] = rawurldecode($_SERVER['REQUEST_URI']);
  31. else self::$gpc['get'] = rawurldecode($_SERVER['REQUEST_URI'] = $_SERVER["PHP_SELF"].(!empty($_SERVER["QUERY_STRING"]) ? '?'.$_SERVER["QUERY_STRING"] : ''));
  32. if(!empty($_SERVER['HTTP_COOKIE'])) self::$gpc['cookie'] = rawurldecode($_SERVER['HTTP_COOKIE']);
  33. if(!empty($_POST)) self::$gpc['post'] = self::getpost();
  34. self::$conf['script'] = $_SERVER['SCRIPT_FILENAME'];
  35. self::$conf['request_uri'] = self::$gpc['get'];
  36. self::$conf['remote_ip'] = self::getremoteip();
  37. self::reload_conf();
  38. self::runapi();
  39. if(!self::mkdir(iswaf_database)) return;
  40. foreach(array('keys','conf','logs','argsdb') as $dir) {
  41. self::mkdir(iswaf_database.$dir);
  42. }
  43. self::allow_whitelist();
  44. foreach(self::$conf['defences'] as $extend => $onoff) {
  45. $extend = $extend;
  46. $onoff = is_array($onoff) ? 'on' : $onoff;
  47. if(strtolower($onoff) == 'on') {
  48. if(isset(self::$conf['plus'][$extend])) $conf = self::$conf['plus'][$extend];
  49. else $conf = array();
  50. self::extend($extend,$conf);
  51. }
  52. }
  53. }
  54. function getremoteip(){
  55. $keys = array('HTTP_X_FORWARDED_FOR','HTTP_CLIENT_IP');
  56. $plus = '';
  57. foreach($keys as $key) {
  58. if(isset($_SERVER[$key])) $plus.=';'.$_SERVER[$key];
  59. }
  60. return $_SERVER['REMOTE_ADDR'].$plus;
  61. }
  62. function extend($model,$conf = array()) {
  63. if(iswaf_mode == 'silent') $conf['mode'] = 'silent';
  64. if(!isset($conf['mode'])) $conf['mode'] = '';
  65. if(!isset($conf['rulers'])) $conf['rulers'] = array();
  66. if(!isset($conf['exts'])) $conf['exts'] = array();
  67. self::$extends_conf = $conf;
  68. return self::execute($model,array($conf));
  69. }
  70. function plugin($model) {
  71. return self::execute($model,'','plugins');
  72. }
  73. function addlog($key,$array,$folder = 'notify') {
  74. if(!isset($array['type'])) $array['type'] = self::$model;
  75. $array['hash'] = $key;
  76. $array['time'] = time();
  77. function_exists('date_default_timezone_get') && $array['timezone'] = @date_default_timezone_get();
  78. $array['domain'] = $_SERVER['HTTP_HOST'];
  79. $array['remoteip'] = $_SERVER['REMOTE_ADDR'];
  80. $array['serverip'] = self::getremoteip();
  81. $array['path'] = $_SERVER['SCRIPT_FILENAME'];
  82. $array['documentroot'] = $_SERVER['DOCUMENT_ROOT'];
  83. if($folder == 'notify') {
  84. $array['get'] = $_GET;
  85. $array['post'] = $_POST;
  86. $array['server'] = $_SERVER;
  87. $array['file'] = $_FILES;
  88. }
  89. if(!self::key_exists($key)) {
  90. self::$log[$folder][] = $array;
  91. self::key_set($key);
  92. }
  93. }
  94. function generate_key($type='') {
  95. $a = md5(print_r(func_get_args(),1));
  96. return ($type == 'lock' ? 'lock' : '').substr($a,8,10);
  97. }
  98. function key_exists($key) {
  99. return file_exists(iswaf_database.'/keys/'.$key) ? filemtime(iswaf_database.'/keys/'.$key) : false;
  100. }
  101. function key_set($key,$value=0) {
  102. $num = self::key_exists($key) ? intval(file_get_contents(iswaf_database.'/keys/'.$key)) + 1 : 1;
  103. return self::create_file(iswaf_database.'keys/'.$key,$value >0 ? $value :$num);
  104. }
  105. function key_num($key) {
  106. return self::key_exists($key) ? intval(file_get_contents(iswaf_database.'/keys/'.$key)) : 0 ;
  107. }
  108. function mkdir($dir) {
  109. $dir = str_replace('//','/',$dir);
  110. if(!is_dir($dir)) return @mkdir($dir) ? true :false;
  111. else return true;
  112. }
  113. function glob($dir) {
  114. $return = array();
  115. $match = false;
  116. (strpos($dir,'*') !== false) && list($dir,$match) = explode('*',$dir,2);
  117. if ($dh = @opendir($dir)) {
  118. while (($file = readdir($dh)) !== false) {
  119. if(!in_array($file,array('.','..'))) {
  120. if(!empty($match) || @stripos($file,$match)!==false) $return[] = $dir.$file;
  121. elseif(empty($match)) $return[] = $dir.$file;
  122. }
  123. }
  124. closedir($dh);
  125. }
  126. return $return;
  127. }
  128. function create_file($file,$content) {
  129. if(function_Exists('file_put_contents')) return file_put_contents($file, $content);
  130. else {
  131. $fp = fopen($file,'w');
  132. $a = fwrite($fp, $content);
  133. fclose($fp);
  134. return $a;
  135. }
  136. }
  137. function save() {
  138. foreach(self::$log as $k=>$v) {
  139. foreach($v as $log) {
  140. self::addtolog($log,$k);
  141. }
  142. }
  143. if(self::$deny) {
  144. if(isset(self::$conf['defences']['denier']['notice'])) echo self::$conf['defences']['denier']['notice'];
  145. exit;
  146. }
  147. }
  148. function addtolog($log,$folder='notify') {
  149. $a = self::glob(iswaf_database.'*.ini');
  150. $logid = 1;
  151. $num = array();
  152. foreach($a as $k) {
  153. if(preg_match('/'.$folder.'(\d+).ini/',$k,$test)) {
  154. if($test[1] > $logid) $logid = $test[1];
  155. }
  156. }
  157. $logfile = iswaf_database.$folder.($logid).'.ini';
  158. if(file_exists($logfile) && filesize($logfile) > 102400) {
  159. $logfile = iswaf_database.$folder.($logid+1).'.ini';
  160. }
  161. $fp = fopen($logfile,'a+');
  162. $log = function_Exists('gzcompress') ? gzcompress(serialize($log)) : serialize($log);
  163. fwrite($fp,self::authcode($log,'ENCODE')."\r\n");
  164. fclose($fp);
  165. }
  166. function allow_whitelist() {
  167. self::$conf['whitelist'] = self::$conf['plus']['whitelist'];
  168. if(self::$conf['whitelist']) {
  169. foreach(self::$conf['whitelist'] as $id => $value) {
  170. if($value['domain'] == $_SERVER['SERVER_NAME']) {
  171. if($value['is_dir']) {
  172. if(stripos($value['path'],$_SERVER['SCRIPT_FILENAME'])) {
  173. self::$conf['defences'][$value['model']] = 'Off';
  174. }
  175. }else{
  176. if($value['path'] == $_SERVER['SCRIPT_NAME']) {
  177. self::$conf['defences'][$value['model']] = 'Off';
  178. }
  179. }
  180. }
  181. }
  182. }
  183. }
  184. function filext($file) {
  185. return trim(strtolower(substr(strrchr($file, '.'), 1, 10)));
  186. }
  187. function runapi() {
  188. if(isset($_POST['action']) && isset($_POST['args']) && ($_POST['key'] == md5(iswaf_connenct_key) || self::$mode == 'debug')) {
  189. $get['args'] = unserialize(self::authcode($_POST['args'],'DECODE'));
  190. $get['function'] = $_POST['action'];
  191. if($get['function']) {
  192. if(!isset($_GET['debug']) && !isset($_GET['key']) && $_GET['key'] !== md5(iswaf_connenct_key)) {
  193. echo self::authcode(self::execute($get['function'],$get['args'],'apis'),'ENCODE');
  194. }else{
  195. print_r(unserialize(self::execute($get['function'],$get['args'],'apis')));
  196. }
  197. exit;
  198. }
  199. }
  200. }
  201. function readfile($file) {
  202. $content = '';
  203. if(!file_exists($file)) return '';
  204. if(function_Exists('file_get_contents')) @$content = file_get_contents($file);
  205. else {
  206. $fp = fopen($file,'r');
  207. $fcode = fread($fp,filesize($fp));
  208. fclose($fp);
  209. $content = $fcode;
  210. }
  211. return $content;
  212. }
  213. function reload_conf() {
  214. self::$conf['plus'] = array();
  215. $dir = iswaf_database.'/conf/';
  216. foreach(self::glob($dir.'*.php') as $file) {
  217. if(self::filext($file) == 'php') {
  218. $model = substr(basename($file),0,-4);
  219. self::$conf['plus'][$model] = include $dir.basename($model).'.php';
  220. }
  221. }
  222. foreach(array('conf','rulers','hotfix','whitelist') as $key) {
  223. if(!isset(self::$conf['plus'][$key])) {
  224. if(file_exists(iswaf_database.$key.'_default.php')) self::$conf['plus'][$key] = include iswaf_database.$key.'_default.php';
  225. else self::$conf['plus'][$key] = array();
  226. }
  227. }
  228. self::$rulers = self::$conf['plus']['rulers'];
  229. self::$conf['defences'] = isset(self::$conf['plus']['conf']['defences']) ? self::$conf['plus']['conf']['defences'] : self::$conf['defences'];
  230. }
  231. function create_key() {
  232. return md5(self::random(128).rand(1,3000).print_r($_SERVER,1));
  233. }
  234. function random($length, $numeric = 0) {
  235. PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
  236. if($numeric)
  237. {
  238. $hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
  239. }
  240. else
  241. {
  242. $hash = '';
  243. $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
  244. $max = strlen($chars) - 1;
  245. for($i = 0; $i < $length; $i++)
  246. {
  247. $hash .= $chars[mt_rand(0, $max)];
  248. }
  249. }
  250. return $hash;
  251. }
  252. function write_config($key,$value) {
  253. $dir = iswaf_database.'/conf/';
  254. $key = basename($key);
  255. $file = $dir.$key.'.php';
  256. $data = "<?php\n//Config of iSwaf".
  257. "\n//Created: ".@date("M j, Y, G:i")."\r\n
  258. "."\r\nreturn ".self::arrayeval($value)."\r\n".';?>';
  259. return self::create_file($file,$data);
  260. }
  261. function arrayeval($array, $level = 0) {
  262. if(!is_array($array)) {
  263. return "'".$array."'";
  264. }
  265. if(is_array($array) && function_exists('var_export')) {
  266. return var_export($array, true);
  267. }
  268. $space = '';
  269. for($i = 0; $i <= $level; $i++) {
  270. $space .= "\t";
  271. }
  272. $evaluate = "Array\n$space(\n";
  273. $comma = $space;
  274. if(is_array($array)) {
  275. foreach($array as $key => $val) {
  276. $key = is_string($key) ? '\''.addcslashes($key, '\'\\').'\'' : $key;
  277. $val = !is_array($val) && (!preg_match("/^\-?[1-9]\d*$/", $val) || strlen($val) > 12) ? '\''.addcslashes($val, '\'\\').'\'' : $val;
  278. if(is_array($val)) {
  279. $evaluate .= "$comma$key => ".arrayeval($val, $level + 1);
  280. } else {
  281. $evaluate .= "$comma$key => $val";
  282. }
  283. $comma = ",\n$space";
  284. }
  285. }
  286. $evaluate .= "\n$space)";
  287. return $evaluate;
  288. }
  289. function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
  290. $ckey_length = 4;
  291. $key = md5($key ? $key : iswaf_connenct_key);
  292. $keya = md5(substr($key, 0, 16));
  293. $keyb = md5(substr($key, 16, 16));
  294. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
  295. $cryptkey = $keya.md5($keya.$keyc);
  296. $key_length = strlen($cryptkey);
  297. $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
  298. $string_length = strlen($string);
  299. $result = '';
  300. $box = range(0, 255);
  301. $rndkey = array();
  302. for($i = 0; $i <= 255; $i++) {
  303. $rndkey[$i] = ord($cryptkey[$i % $key_length]);
  304. }
  305. for($j = $i = 0; $i < 256; $i++) {
  306. $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  307. $tmp = $box[$i];
  308. $box[$i] = $box[$j];
  309. $box[$j] = $tmp;
  310. }
  311. for($a = $j = $i = 0; $i < $string_length; $i++) {
  312. $a = ($a + 1) % 256;
  313. $j = ($j + $box[$a]) % 256;
  314. $tmp = $box[$a];
  315. $box[$a] = $box[$j];
  316. $box[$j] = $tmp;
  317. $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
  318. }
  319. if($operation == 'DECODE') {
  320. if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
  321. return substr($result, 26);
  322. } else {
  323. return '';
  324. }
  325. } else {
  326. return $keyc.str_replace('=', '', base64_encode($result));
  327. }
  328. }
  329. function getpost() {
  330. if($_POST) {
  331. global $HTTP_RAW_POST_DATA;
  332. if (isset($HTTP_RAW_POST_DATA)) {
  333. return @trim( @rawurldecode($HTTP_RAW_POST_DATA));
  334. }elseif (PHP_OS>='4.3.0' && !stripos($_SERVER['CONTENT_TYPE'],'multipart/form-data')) {
  335. return @rawurldecode(@file_get_contents('php://input'));
  336. }else{
  337. $post = '';
  338. foreach($_POST as $k=>$v) {
  339. if(!is_array($v)) {
  340. $post .= $k.'='.rawurldecode($v).'&';
  341. }
  342. }
  343. return $post;
  344. }
  345. }
  346. }
  347. function webos() {
  348. return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? 'Windows' : '*nix';
  349. }
  350. function getlog($type){
  351. $return = array();
  352. if(is_numeric($type)) {
  353. if($type == 1) $folder = 'filehash';
  354. else $folder = 'notify';
  355. } else {
  356. $folder = basename($folder);
  357. }
  358. $a = self::glob(iswaf_database.'*.ini');
  359. $logid = 0;
  360. $num = array();
  361. foreach($a as $k) {
  362. if(preg_match('/'.$folder.'(\d+).ini/',$k,$test)) {
  363. if($logid == 0) $logid = $test[1];
  364. else {
  365. if($test[1] < $logid) $logid = $test[1];
  366. }
  367. }
  368. }
  369. $logfile = iswaf_database.$folder.($logid).'.ini';
  370. $tmp = self::readfile($logfile);
  371. $t = explode("\n",$tmp);
  372. $gzcompress = 0;
  373. if(function_exists('gzcompress')) $gzcompress = 1;
  374. foreach($t as $string) {
  375. $tmp = self::authcode($string);
  376. if(trim($string)) $return[] = @unserialize($gzcompress ? gzuncompress($tmp) : $tmp);
  377. }
  378. file_Exists($logfile) ? @unlink($logfile) : '';
  379. return $return;
  380. }
  381. function cleanlog($type) {
  382. if($type == 1) $tmp = self::create_file(iswaf_database.'filehash.ini','');
  383. else $tmp = self::create_file(iswaf_database.'notify.ini','');
  384. self::cleankeys();
  385. return true;
  386. }
  387. function cleankeys(){
  388. $dir = iswaf_database.'keys/';
  389. $files = self::glob($dir.'*');
  390. $time = time();
  391. foreach($files as $file) {
  392. $filectime = filectime($file);
  393. if(($time - $filectime) > 100 && substr(basename($file),0,4)!=='lock') @unlink($file);
  394. }
  395. }
  396. function clean_code($code) {
  397. return $code;
  398. }
  399. function execute($function,$args = '',$path = 'extensions') {
  400. $function = basename($function);
  401. $path = basename($path);
  402. self::$model = $function;
  403. if(file_exists(iswaf_root.$path.'/'.$function.'.php')) {
  404. include_once iswaf_root.$path.'/'.$function.'.php';
  405. $classname = 'plus_'.$function;
  406. $class = new $classname;
  407. self::$models[] = $function;
  408. if(!is_array($args)) $args = array($args);
  409. $a = call_user_func_array(array($class, $function),$args);
  410. self::debuginfo($function);
  411. return $a;
  412. }
  413. }
  414. function deny($deny) {
  415. self::$deny = $deny;
  416. }
  417. function debuginfo($model='') {
  418. global $________ISwaf;
  419. $tmp = explode(' ', microtime());
  420. if(!isset($________ISwaf['app_end_time'])) $________ISwaf['app_end_time'] = $________ISwaf['starttime'];
  421. $________ISwaf['app_debug_infos'][$model] = number_format(($tmp[1] + $tmp[0] - $________ISwaf['app_end_time']), 6);
  422. $tmp = explode(' ', microtime());
  423. $________ISwaf['app_end_time'] = $tmp[1] + $tmp[0];
  424. }
  425. }
  426. }
  427. $d = new iswaf;
  428. if(file_exists(iswaf_database.'/config.php')) {
  429. $iswaf = include(iswaf_database.'/config.php');
  430. } else {
  431. $iswaf = include(iswaf_root.'/conf/conf_default.php');
  432. include_once iswaf_root.'/conf/conf.php';
  433. }
  434. foreach ($iswaf as $key => $value) {
  435. if(!is_array($value) && !defined($key)) define($key,$value);
  436. }
  437. if(!defined('iswaf_mode')) define('iswaf_mode','');
  438. if(!defined('iswaf_status')) define('iswaf_status',1);
  439. $d -> init($iswaf);
  440. $d -> save();
  441. unset($d);
  442. if(isset($_GET['iswaf__installer__']) && isset($_GET['connect_key'])) {
  443. if($_GET['connect_key'] == md5(iswaf_connenct_key)) {
  444. function iswaf_writetofile($filename,$data) {
  445. if(function_exists('file_put_contents')) return file_put_contents($filename, $data);
  446. else {
  447. $fp = fopen($filename,'w');
  448. $a = fwrite($fp,$data);
  449. fclose($fp);
  450. return $a;
  451. }
  452. }
  453. $_GET['action'] = isset($_GET['action']) ? $_GET['action'] : '';
  454. if($_GET['action'] == 'test') {
  455. echo 'ok';
  456. die;
  457. }
  458. if($_GET['action'] == 'debug') {
  459. echo serialize($________ISwaf['app_debug_infos']);
  460. die;
  461. }
  462. if($_GET['action'] == 'install') {
  463. iswaf_writetofile(iswaf_database.'installed',time());
  464. echo serialize(file_exists(iswaf_database.'installed'));
  465. die;
  466. }
  467. }
  468. }
  469. $________ISwaf['mtime'] = explode(' ', microtime());
  470. $________ISwaf['totaltime'] = number_format(($________ISwaf['mtime'][1] + $________ISwaf['mtime'][0] - $________ISwaf['starttime']), 6);
  471. ?>