portalcp_upload.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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: portalcp_upload.php 30107 2012-05-11 02:10:58Z svn_project_zhangjie $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. $operation = $_GET['op'] ? $_GET['op'] : '';
  12. $upload = new discuz_upload();
  13. $downremotefile = false;
  14. $aid = intval(getgpc('aid'));
  15. $catid = intval(getgpc('catid'));
  16. if($aid) {
  17. $article = C::t('portal_article_title')->fetch($aid);
  18. if(!$article) {
  19. portal_upload_error(lang('portalcp', 'article_noexist'));
  20. }
  21. if(check_articleperm($catid, $aid, $article, false, true) !== true) {
  22. portal_upload_error(lang('portalcp', 'article_noallowed'));
  23. }
  24. } else {
  25. if(($return = check_articleperm($catid, $aid, null, false, true)) !== true) {
  26. portal_upload_error(lang('portalcp', $return));
  27. }
  28. }
  29. if($operation == 'downremotefile') {
  30. $arrayimageurl = $temp = $imagereplace = array();
  31. $string = $_GET['content'];
  32. $downremotefile = true;
  33. preg_match_all("/\<img.+src=('|\"|)?(.*)(\\1)([\s].*)?\>/ismUe", $string, $temp, PREG_SET_ORDER);
  34. if(is_array($temp) && !empty($temp)) {
  35. foreach($temp as $tempvalue) {
  36. $tempvalue[2] = str_replace('\"', '', $tempvalue[2]);
  37. if(strlen($tempvalue[2])){
  38. $arrayimageurl[] = $tempvalue[2];
  39. }
  40. }
  41. $arrayimageurl = array_unique($arrayimageurl);
  42. if($arrayimageurl) {
  43. foreach($arrayimageurl as $tempvalue) {
  44. $imageurl = $tempvalue;
  45. $imagereplace['oldimageurl'][] = $imageurl;
  46. $attach['ext'] = $upload->fileext($imageurl);
  47. if(!$upload->is_image_ext($attach['ext'])) {
  48. continue;
  49. }
  50. $content = '';
  51. if(preg_match('/^(http:\/\/|\.)/i', $imageurl)) {
  52. $content = dfsockopen($imageurl);
  53. } elseif(checkperm('allowdownlocalimg')) {
  54. if(preg_match('/^data\/(.*?)\.thumb\.jpg$/i', $imageurl)) {
  55. $content = file_get_contents(substr($imageurl, 0, strrpos($imageurl, '.')-6));
  56. } elseif(preg_match('/^data\/(.*?)\.(jpg|jpeg|gif|png)$/i', $imageurl)) {
  57. $content = file_get_contents($imageurl);
  58. }
  59. }
  60. if(empty($content)) continue;
  61. $temp = explode('/', $imageurl);
  62. $attach['name'] = trim($temp[count($temp)-1]);
  63. $attach['thumb'] = '';
  64. $attach['isimage'] = $upload -> is_image_ext($attach['ext']);
  65. $attach['extension'] = $upload -> get_target_extension($attach['ext']);
  66. $attach['attachdir'] = $upload -> get_target_dir('portal');
  67. $attach['attachment'] = $attach['attachdir'] . $upload->get_target_filename('portal').'.'.$attach['extension'];
  68. $attach['target'] = getglobal('setting/attachdir').'./portal/'.$attach['attachment'];
  69. if(!@$fp = fopen($attach['target'], 'wb')) {
  70. continue;
  71. } else {
  72. flock($fp, 2);
  73. fwrite($fp, $content);
  74. fclose($fp);
  75. }
  76. if(!$upload->get_image_info($attach['target'])) {
  77. @unlink($attach['target']);
  78. continue;
  79. }
  80. $attach['size'] = filesize($attach['target']);
  81. $attachs[] = daddslashes($attach);
  82. }
  83. }
  84. }
  85. } else {
  86. $upload->init($_FILES['attach'], 'portal');
  87. $attach = $upload->attach;
  88. if(!$upload->error()) {
  89. $upload->save();
  90. }
  91. if($upload->error()) {
  92. portal_upload_error($upload->error());
  93. }
  94. $attachs[] = $attach;
  95. }
  96. if($attachs) {
  97. foreach($attachs as $attach) {
  98. if($attach['isimage'] && empty($_G['setting']['portalarticleimgthumbclosed'])) {
  99. require_once libfile('class/image');
  100. $image = new image();
  101. $thumbimgwidth = $_G['setting']['portalarticleimgthumbwidth'] ? $_G['setting']['portalarticleimgthumbwidth'] : 300;
  102. $thumbimgheight = $_G['setting']['portalarticleimgthumbheight'] ? $_G['setting']['portalarticleimgthumbheight'] : 300;
  103. $attach['thumb'] = $image->Thumb($attach['target'], '', $thumbimgwidth, $thumbimgheight, 2);
  104. $image->Watermark($attach['target'], '', 'portal');
  105. }
  106. if(getglobal('setting/ftp/on') && ((!$_G['setting']['ftp']['allowedexts'] && !$_G['setting']['ftp']['disallowedexts']) || ($_G['setting']['ftp']['allowedexts'] && in_array($attach['ext'], $_G['setting']['ftp']['allowedexts'])) || ($_G['setting']['ftp']['disallowedexts'] && !in_array($attach['ext'], $_G['setting']['ftp']['disallowedexts']))) && (!$_G['setting']['ftp']['minsize'] || $attach['size'] >= $_G['setting']['ftp']['minsize'] * 1024)) {
  107. if(ftpcmd('upload', 'portal/'.$attach['attachment']) && (!$attach['thumb'] || ftpcmd('upload', 'portal/'.getimgthumbname($attach['attachment'])))) {
  108. @unlink($_G['setting']['attachdir'].'/portal/'.$attach['attachment']);
  109. @unlink($_G['setting']['attachdir'].'/portal/'.getimgthumbname($attach['attachment']));
  110. $attach['remote'] = 1;
  111. } else {
  112. if(getglobal('setting/ftp/mirror')) {
  113. @unlink($attach['target']);
  114. @unlink(getimgthumbname($attach['target']));
  115. portal_upload_error(lang('portalcp', 'upload_remote_failed'));
  116. }
  117. }
  118. }
  119. $setarr = array(
  120. 'uid' => $_G['uid'],
  121. 'filename' => $attach['name'],
  122. 'attachment' => $attach['attachment'],
  123. 'filesize' => $attach['size'],
  124. 'isimage' => $attach['isimage'],
  125. 'thumb' => $attach['thumb'],
  126. 'remote' => $attach['remote'],
  127. 'filetype' => $attach['extension'],
  128. 'dateline' => $_G['timestamp'],
  129. 'aid' => $aid
  130. );
  131. $setarr['attachid'] = C::t('portal_attachment')->insert($setarr, true);
  132. if($downremotefile) {
  133. $attach['url'] = ($attach['remote'] ? $_G['setting']['ftp']['attachurl'] : $_G['setting']['attachurl']).'portal/';
  134. $imagereplace['newimageurl'][] = $attach['url'].$attach['attachment'];
  135. }
  136. portal_upload_show($setarr);
  137. }
  138. if($downremotefile && $imagereplace) {
  139. $string = preg_replace(array("/\<(script|style|iframe)[^\>]*?\>.*?\<\/(\\1)\>/si", "/\<!*(--|doctype|html|head|meta|link|body)[^\>]*?\>/si"), '', $string);
  140. $string = str_replace($imagereplace['oldimageurl'], $imagereplace['newimageurl'], $string);
  141. $string = str_replace(array("\r", "\n", "\r\n"), '', addcslashes($string, '/"\\\''));
  142. print <<<EOF
  143. <script type="text/javascript">
  144. var f = parent.window.frames["uchome-ifrHtmlEditor"].window.frames["HtmlEditor"];
  145. f.document.body.innerHTML = '$string';
  146. </script>
  147. EOF;
  148. }
  149. exit();
  150. }
  151. function portal_upload_error($msg) {
  152. echo '<script>';
  153. echo 'if(parent.$(\'localfile_'.$_GET['attach_target_id'].'\') != null)parent.$(\'localfile_'.$_GET['attach_target_id'].'\').innerHTML = \''.lang('portalcp', 'upload_error').$msg.'\';else alert(\''.$msg.'\')';
  154. echo '</script>';
  155. exit();
  156. }
  157. function portal_upload_show($attach) {
  158. global $_G;
  159. $imagehtml = $filehtml = $coverstr ='';
  160. if($attach['isimage']) {
  161. $imagehtml = get_uploadcontent($attach, 'portal', 'upload');
  162. $coverstr = addslashes(serialize(array('pic'=>'portal/'.$attach['attachment'], 'thumb'=>$attach['thumb'], 'remote'=>$attach['remote'])));
  163. } else {
  164. $filehtml = get_uploadcontent($attach, 'portal', 'upload');
  165. }
  166. echo '<script type="text/javascript" src="'.$_G[setting][jspath].'handlers.js?'.$_G['style']['verhash'].'"></script>';
  167. echo '<script>';
  168. if($imagehtml) echo 'var tdObj = getInsertTdId(parent.$(\'imgattachlist\'), \'attach_list_'.$attach['attachid'].'\');tdObj.innerHTML = \''.addslashes($imagehtml).'\';';
  169. if($filehtml) echo 'parent.$(\'attach_file_body\').innerHTML = \''.addslashes($filehtml).'\'+parent.$(\'attach_file_body\').innerHTML;';
  170. echo 'if(parent.$(\'localfile_'.$_GET['attach_target_id'].'\') != null)parent.$(\'localfile_'.$_GET['attach_target_id'].'\').style.display = \'none\';';
  171. echo 'parent.$(\'attach_ids\').value += \','.$attach['attachid'].'\';';
  172. if($coverstr) echo 'if(parent.$(\'conver\').value == \'\')parent.$(\'conver\').value = \''.$coverstr.'\';';
  173. echo '</script>';
  174. }
  175. ?>