PageRenderTime 110ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/source/function/function_cloudaddons.php

https://github.com/kuaileshike/upload
PHP | 349 lines | 312 code | 31 blank | 6 comment | 78 complexity | d8e70827a7af13691ad102c2a0fda5c6 MD5 | raw file
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: function_cloudaddons.php 30741 2012-06-15 08:53:29Z monkey $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. define('CLOUDADDONS_WEBSITE_URL', 'http://addon.discuz.com');
  12. define('CLOUDADDONS_DOWNLOAD_URL', 'http://addon.discuz.com/index.php');
  13. define('CLOUDADDONS_DOWNLOAD_IP', '');
  14. define('CLOUDADDONS_CHECK_URL', 'http://addon1.discuz.com');
  15. define('CLOUDADDONS_CHECK_IP', '');
  16. function cloudaddons_md5($file) {
  17. return dfsockopen(CLOUDADDONS_CHECK_URL.'/md5/'.$file, 0, '', '', false, CLOUDADDONS_CHECK_IP, 999);
  18. }
  19. function cloudaddons_url($extra) {
  20. global $_G;
  21. require_once DISCUZ_ROOT.'./source/discuz_version.php';
  22. $uniqueid = $_G['setting']['siteuniqueid'] ? $_G['setting']['siteuniqueid'] : C::t('common_setting')->fetch('siteuniqueid');
  23. $data = 'siteuniqueid='.rawurlencode($uniqueid).'&siteurl='.rawurlencode($_G['siteurl']).'&sitever='.DISCUZ_VERSION.'/'.DISCUZ_RELEASE.'&sitecharset='.CHARSET.'&mysiteid='.$_G['setting']['my_siteid'];
  24. $param = 'data='.rawurlencode(base64_encode($data));
  25. $param .= '&md5hash='.substr(md5($data.TIMESTAMP), 8, 8).'&timestamp='.TIMESTAMP;
  26. return CLOUDADDONS_DOWNLOAD_URL.'?'.$param.$extra;
  27. }
  28. function cloudaddons_check() {
  29. if(!function_exists('gzuncompress')) {
  30. cpmsg('cloudaddons_check_gzuncompress_error', '', 'error');
  31. }
  32. if(dfsockopen(CLOUDADDONS_WEBSITE_URL.'/image/logo.png', 4, '', '', false, CLOUDADDONS_DOWNLOAD_IP, 999) !== chr(0x89).'PNG') {
  33. cpmsg('cloudaddons_check_url_fopen_error', '', 'error');
  34. }
  35. if(dfsockopen(CLOUDADDONS_CHECK_URL.'/logo.png', 4, '', '', false, CLOUDADDONS_CHECK_IP, 999) !== chr(0x89).'PNG') {
  36. cpmsg('cloudaddons_check_url_fopen_error', '', 'error');
  37. }
  38. foreach(array('download', 'addonmd5') as $path) {
  39. $tmpdir = DISCUZ_ROOT.'./data/'.$path.'/'.random(5);
  40. $tmpfile = $tmpdir.'/index.html';
  41. dmkdir($tmpdir, 0777);
  42. if(!is_dir($tmpdir) || !file_exists($tmpfile)) {
  43. cpmsg('cloudaddons_check_write_error', '', 'error');
  44. }
  45. @unlink($tmpfile);
  46. @rmdir($tmpdir);
  47. if(is_dir($tmpdir) || file_exists($tmpfile)) {
  48. cpmsg('cloudaddons_check_write_error', '', 'error');
  49. }
  50. }
  51. }
  52. function cloudaddons_open($extra, $post = '') {
  53. return dfsockopen(cloudaddons_url('&from=s').$extra, 0, $post, '', false, CLOUDADDONS_DOWNLOAD_IP, 999);
  54. }
  55. function cloudaddons_pluginlogo_url($id) {
  56. return CLOUDADDONS_WEBSITE_URL.'?_'.$id;
  57. }
  58. function cloudaddons_installlog($addonid) {
  59. $array = cloudaddons_getmd5($addonid);
  60. if($array['RevisionID']) {
  61. cloudaddons_open('&mod=app&ac=installlog&rid='.$array['RevisionID']);
  62. }
  63. }
  64. function cloudaddons_downloadlog($addonid) {
  65. $array = cloudaddons_getmd5($addonid);
  66. if($array['RevisionID']) {
  67. cloudaddons_open('&mod=app&ac=downloadlog&rid='.$array['RevisionID']);
  68. }
  69. }
  70. function cloudaddons_faillog($rid, $type) {
  71. $rid = intval($rid);
  72. $type = intval($type);
  73. cloudaddons_open('&mod=app&ac=faillog&rid='.$rid.'&type='.$type.'&serverinfo='.urlencode($_SERVER['SERVER_SOFTWARE']));
  74. }
  75. function cloudaddons_removelog($rid) {
  76. global $_G;
  77. $reason = $_G['cookie']['uninstallreason'];
  78. dsetcookie('uninstallreason', '', -1);
  79. cloudaddons_open('&mod=app&ac=removelog&rid='.$rid.'&reason='.$reason);
  80. }
  81. function cloudaddons_validator($addonid) {
  82. $array = cloudaddons_getmd5($addonid);
  83. if(cloudaddons_open('&mod=app&ac=validator&addonid='.$addonid.($array !== false ? '&rid='.$array['RevisionID'].'&sn='.$array['SN'].'&rd='.$array['RevisionDateline'] : '')) === '0') {
  84. cpmsg('cloudaddons_genuine_message', '', 'error', array('addonid' => $addonid));
  85. }
  86. }
  87. function cloudaddons_upgradecheck($addonids) {
  88. $post = array();
  89. foreach($addonids as $addonid) {
  90. $array = cloudaddons_getmd5($addonid);
  91. $post[] = 'rid['.$addonid.']='.$array['RevisionID'].'&sn['.$addonid.']='.$array['SN'].'&rd['.$addonid.']='.$array['RevisionDateline'];
  92. }
  93. return cloudaddons_open('&mod=app&ac=validator', implode('&', $post));
  94. }
  95. function cloudaddons_getmd5($md5file) {
  96. $array = array();
  97. if(preg_match('/^[a-z0-9_\.]+$/i', $md5file) && file_exists(DISCUZ_ROOT.'./data/addonmd5/'.$md5file.'.xml')) {
  98. require_once libfile('class/xml');
  99. $xml = implode('', @file(DISCUZ_ROOT.'./data/addonmd5/'.$md5file.'.xml'));
  100. $array = xml2array($xml);
  101. } else {
  102. return false;
  103. }
  104. return $array;
  105. }
  106. function cloudaddons_uninstall($md5file, $dir) {
  107. $array = cloudaddons_getmd5($md5file);
  108. if($array === false) {
  109. return;
  110. }
  111. if(!empty($array['RevisionID'])) {
  112. cloudaddons_removelog($array['RevisionID']);
  113. }
  114. @unlink(DISCUZ_ROOT.'./data/addonmd5/'.$md5file.'.xml');
  115. cloudaddons_cleardir($dir);
  116. }
  117. function cloudaddons_savemd5($md5file, $end, $md5) {
  118. global $_G;
  119. parse_str($end, $r);
  120. $array = array(
  121. 'Title' => 'Discuz! Addon MD5',
  122. 'ID' => $r['ID'],
  123. 'SN' => $r['SN'],
  124. 'RevisionID' => $r['RevisionID'],
  125. 'RevisionDateline' => $r['RevisionDateline'],
  126. 'Data' => $md5,
  127. );
  128. require_once libfile('class/xml');
  129. if(!isset($_G['siteftp'])) {
  130. dmkdir(DISCUZ_ROOT.'./data/addonmd5/', 0777, false);
  131. $fp = fopen(DISCUZ_ROOT.'./data/addonmd5/'.$md5file.'.xml', 'w');
  132. fwrite($fp, array2xml($array));
  133. fclose($fp);
  134. } else {
  135. $localfile = DISCUZ_ROOT.'./data/'.random(5);
  136. $fp = fopen($localfile, 'w');
  137. fwrite($fp, array2xml($array));
  138. fclose($fp);
  139. dmkdir(DISCUZ_ROOT.'./data/addonmd5/', 0777, false);
  140. siteftp_upload($localfile, 'data/addonmd5/'.$md5file.'.xml');
  141. @unlink($localfile);
  142. }
  143. }
  144. function cloudaddons_comparetree($new, $old, $basedir, $md5file, $first = 0) {
  145. global $_G;
  146. if($first && file_exists(DISCUZ_ROOT.'./data/addonmd5/'.$md5file.'.xml')) {
  147. require_once libfile('class/xml');
  148. $xml = implode('', @file(DISCUZ_ROOT.'./data/addonmd5/'.$md5file.'.xml'));
  149. $array = xml2array($xml);
  150. $_G['treeop']['md5old'] = $array['Data'];
  151. }
  152. $dh = opendir($new);
  153. while(($file = readdir($dh)) !== false) {
  154. if($file != '.' && $file != '..') {
  155. $newfile = $new.'/'.$file;
  156. $oldfile = $old.'/'.$file;
  157. if(is_file($newfile)) {
  158. $oldfile = preg_replace('/\._addons_$/', '', $oldfile);
  159. $md5key = str_replace($basedir, '', preg_replace('/\._addons_$/', '', $newfile));
  160. $newmd5 = md5_file($newfile);
  161. $oldmd5 = file_exists($oldfile) ? md5_file($oldfile) : '';
  162. if(isset($_G['treeop']['md5old'][$md5key]) && $_G['treeop']['md5old'][$md5key] != $oldmd5) {
  163. $_G['treeop']['oldchange'][] = $md5key;
  164. }
  165. if($newmd5 != $oldmd5) {
  166. $_G['treeop']['copy'][] = $newfile;
  167. }
  168. $_G['treeop']['md5'][$md5key] = $newmd5;
  169. } else {
  170. cloudaddons_comparetree($newfile, $oldfile, $basedir);
  171. }
  172. }
  173. }
  174. }
  175. function cloudaddons_copytree($from, $to) {
  176. global $_G;
  177. $dh = opendir($from);
  178. while(($file = readdir($dh)) !== false) {
  179. if($file != '.' && $file != '..') {
  180. $readfile = $from.'/'.$file;
  181. $writefile = $to.'/'.$file;
  182. if(is_file($readfile)) {
  183. if(!in_array($readfile, $_G['treeop']['copy'])) {
  184. continue;
  185. }
  186. if(!isset($_G['siteftp'])) {
  187. $content = -1;
  188. if($fp = @fopen($readfile, 'r')) {
  189. $startTime = microtime();
  190. do {
  191. $canRead = flock($fp, LOCK_SH);
  192. if(!$canRead) {
  193. usleep(round(rand(0, 100) * 1000));
  194. }
  195. } while ((!$canRead) && ((microtime() - $startTime) < 1000));
  196. if(!$canRead) {
  197. cpmsg('cloudaddons_file_read_error', '', 'error');
  198. }
  199. $content = fread($fp, filesize($readfile));
  200. flock($fp, LOCK_UN);
  201. fclose($fp);
  202. }
  203. if($content < 0) {
  204. cpmsg('cloudaddons_file_read_error', '', 'error');
  205. }
  206. dmkdir(dirname($writefile), 0777, false);
  207. $writefile = preg_replace('/\._addons_$/', '', $writefile);
  208. if($fp = fopen($writefile, 'w')) {
  209. $startTime = microtime();
  210. do {
  211. $canWrite = flock($fp, LOCK_EX);
  212. if(!$canWrite) {
  213. usleep(round(rand(0, 100) * 1000));
  214. }
  215. } while ((!$canWrite) && ((microtime() - $startTime) < 1000));
  216. if(!$canWrite) {
  217. cpmsg('cloudaddons_file_write_error', '', 'error');
  218. }
  219. fwrite($fp, $content);
  220. flock($fp, LOCK_UN);
  221. fclose($fp);
  222. }
  223. if(!$canWrite) {
  224. cpmsg('cloudaddons_file_write_error', '', 'error');
  225. }
  226. } else {
  227. $writefile = preg_replace('/\._addons_$/', '', $writefile);
  228. siteftp_upload($readfile, preg_replace('/^'.preg_quote(DISCUZ_ROOT).'/', '', $writefile));
  229. }
  230. if(md5_file($readfile) != md5_file($writefile)) {
  231. cpmsg('cloudaddons_file_write_error', '', 'error');
  232. }
  233. } else {
  234. cloudaddons_copytree($readfile, $writefile);
  235. }
  236. }
  237. }
  238. }
  239. function cloudaddons_deltree($dir) {
  240. if($directory = @dir($dir)) {
  241. while($entry = $directory->read()) {
  242. if($entry == '.' || $entry == '..') {
  243. continue;
  244. }
  245. $filename = $dir.'/'.$entry;
  246. if(is_file($filename)) {
  247. @unlink($filename);
  248. } else {
  249. cloudaddons_deltree($filename);
  250. }
  251. }
  252. $directory->close();
  253. @rmdir($dir);
  254. }
  255. }
  256. function cloudaddons_cleardir($dir) {
  257. if(is_dir($dir)) {
  258. cloudaddons_deltree($dir);
  259. }
  260. }
  261. function cloudaddons_dirwriteable($basedir, $dir, $sourcedir) {
  262. $checkdirs = array($dir);
  263. cloudaddons_getsubdirs($sourcedir, $dir, $checkdirs);
  264. $return = array();
  265. foreach($checkdirs as $k => $dir) {
  266. $writeable = false;
  267. $checkdir = $basedir.'/'.$dir;
  268. if(!is_dir($checkdir)) {
  269. @mkdir($checkdir, 0777);
  270. }
  271. if(is_dir($checkdir)) {
  272. $fp = fopen($checkdir.'/test.txt', 'w');
  273. if($fp) {
  274. fclose($fp);
  275. unlink($checkdir.'/test.txt');
  276. $writeable = true;
  277. } else {
  278. $writeable = false;
  279. }
  280. }
  281. if(!$writeable && $dir) {
  282. $return[] = $dir;
  283. }
  284. }
  285. return $return;
  286. }
  287. function cloudaddons_getsubdirs($dir, $root, &$return) {
  288. static $prefix = false;
  289. if($prefix === false) {
  290. $prefix = strlen($dir) + 1;
  291. }
  292. $dh = opendir($dir);
  293. while(($file = readdir($dh)) !== false) {
  294. if($file != '.' && $file != '..') {
  295. $readfile = $dir.'/'.$file;
  296. if(is_dir($readfile)) {
  297. $return[] = $root.'/'.substr($readfile, $prefix);
  298. cloudaddons_getsubdirs($readfile, $root, $return);
  299. }
  300. }
  301. }
  302. }
  303. function cloudaddons_http_build_query($formdata, $numeric_prefix = null, $key = null) {
  304. $res = array();
  305. foreach((array) $formdata as $k => $v) {
  306. $tmp_key = urlencode(is_int($k) ? $numeric_prefix . $k : $k);
  307. if ($key) {
  308. $tmp_key = $key.'['.$tmp_key.']';
  309. }
  310. if (is_array($v) || is_object($v)) {
  311. $res[] = cloudaddons_http_build_query($v, null, $tmp_key);
  312. } else {
  313. $res[] = $tmp_key.'='.urlencode($v);
  314. }
  315. }
  316. return implode('&', $res);
  317. }
  318. ?>