Discuz!大师网

搜索
查看: 5831|回复: 0

【discuz x3】如何将头像和帖内等附件分离到远程服务器?

[复制链接]
发表于 2013-12-27 22:18:33 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
一、修改uc_server/control/user.php文件,代码如下:
  1. <?php

  2. /*
  3.         [UCenter] (C)2001-2099 Comsenz Inc.
  4.         This is NOT a freeware, use is subject to license terms

  5.         $Id: user.php 1059 2011-03-01 07:25:09Z monkey $
  6. */

  7. !defined('IN_UC') && exit('Access Denied');

  8. define('UC_USER_CHECK_USERNAME_FAILED', -1);
  9. define('UC_USER_USERNAME_BADWORD', -2);
  10. define('UC_USER_USERNAME_EXISTS', -3);
  11. define('UC_USER_EMAIL_FORMAT_ILLEGAL', -4);
  12. define('UC_USER_EMAIL_ACCESS_ILLEGAL', -5);
  13. define('UC_USER_EMAIL_EXISTS', -6);

  14. class usercontrol extends base {


  15.         function __construct() {
  16.                 $this->usercontrol();
  17.         }

  18.         function usercontrol() {
  19.                 parent::__construct();
  20.                 $this->load('user');
  21.         }

  22.         // -1 未开启
  23.         function onsynlogin() {
  24.                 $this->init_input();
  25.                 $uid = $this->input('uid');
  26.                 if($this->app['synlogin']) {
  27.                         if($this->user = $_ENV['user']->get_user_by_uid($uid)) {
  28.                                 $synstr = '';
  29.                                 foreach($this->cache['apps'] as $appid => $app) {
  30.                                         if($app['synlogin']) {
  31.                                                 $synstr .= '<script type="text/javascript" src="'.$app['url'].'/api/'.$app['apifilename'].'?time='.$this->time.'&code='.urlencode($this->authcode('action=synlogin&username='.$this->user['username'].'&uid='.$this->user['uid'].'&password='.$this->user['password']."&time=".$this->time, 'ENCODE', $app['authkey'])).'" reload="1"></script>';
  32.                                                 if(is_array($app['extra']['extraurl'])) foreach($app['extra']['extraurl'] as $extraurl) {
  33.                                                         $synstr .= '<script type="text/javascript" src="'.$extraurl.'/api/'.$app['apifilename'].'?time='.$this->time.'&code='.urlencode($this->authcode('action=synlogin&username='.$this->user['username'].'&uid='.$this->user['uid'].'&password='.$this->user['password']."&time=".$this->time, 'ENCODE', $app['authkey'])).'" reload="1"></script>';
  34.                                                 }
  35.                                         }
  36.                                 }
  37.                                 return $synstr;
  38.                         }
  39.                 }
  40.                 return '';
  41.         }

  42.         function onsynlogout() {
  43.                 $this->init_input();
  44.                 if($this->app['synlogin']) {
  45.                         $synstr = '';
  46.                         foreach($this->cache['apps'] as $appid => $app) {
  47.                                 if($app['synlogin']) {
  48.                                         $synstr .= '<script type="text/javascript" src="'.$app['url'].'/api/'.$app['apifilename'].'?time='.$this->time.'&code='.urlencode($this->authcode('action=synlogout&time='.$this->time, 'ENCODE', $app['authkey'])).'" reload="1"></script>';
  49.                                         if(is_array($app['extra']['extraurl'])) foreach($app['extra']['extraurl'] as $extraurl) {
  50.                                                 $synstr .= '<script type="text/javascript" src="'.$extraurl.'/api/'.$app['apifilename'].'?time='.$this->time.'&code='.urlencode($this->authcode('action=synlogout&time='.$this->time, 'ENCODE', $app['authkey'])).'" reload="1"></script>';
  51.                                         }
  52.                                 }
  53.                         }
  54.                         return $synstr;
  55.                 }
  56.                 return '';
  57.         }

  58.         function onregister() {
  59.                 $this->init_input();
  60.                 $username = $this->input('username');
  61.                 $password =  $this->input('password');
  62.                 $email = $this->input('email');
  63.                 $questionid = $this->input('questionid');
  64.                 $answer = $this->input('answer');
  65.                 $regip = $this->input('regip');

  66.                 if(($status = $this->_check_username($username)) < 0) {
  67.                         return $status;
  68.                 }
  69.                 if(($status = $this->_check_email($email)) < 0) {
  70.                         return $status;
  71.                 }

  72.                 $uid = $_ENV['user']->add_user($username, $password, $email, 0, $questionid, $answer, $regip);
  73.                 return $uid;
  74.         }

  75.         function onedit() {
  76.                 $this->init_input();
  77.                 $username = $this->input('username');
  78.                 $oldpw = $this->input('oldpw');
  79.                 $newpw = $this->input('newpw');
  80.                 $email = $this->input('email');
  81.                 $ignoreoldpw = $this->input('ignoreoldpw');
  82.                 $questionid = $this->input('questionid');
  83.                 $answer = $this->input('answer');

  84.                 if(!$ignoreoldpw && $email && ($status = $this->_check_email($email, $username)) < 0) {
  85.                         return $status;
  86.                 }
  87.                 $status = $_ENV['user']->edit_user($username, $oldpw, $newpw, $email, $ignoreoldpw, $questionid, $answer);

  88.                 if($newpw && $status > 0) {
  89.                         $this->load('note');
  90.                         $_ENV['note']->add('updatepw', 'username='.urlencode($username).'&password=');
  91.                         $_ENV['note']->send();
  92.                 }
  93.                 return $status;
  94.         }

  95.         function onlogin() {
  96.                 $this->init_input();
  97.                 $isuid = $this->input('isuid');
  98.                 $username = $this->input('username');
  99.                 $password = $this->input('password');
  100.                 $checkques = $this->input('checkques');
  101.                 $questionid = $this->input('questionid');
  102.                 $answer = $this->input('answer');
  103.                 if($isuid == 1) {
  104.                         $user = $_ENV['user']->get_user_by_uid($username);
  105.                 } elseif($isuid == 2) {
  106.                         $user = $_ENV['user']->get_user_by_email($username);
  107.                 } else {
  108.                         $user = $_ENV['user']->get_user_by_username($username);
  109.                 }

  110.                 $passwordmd5 = preg_match('/^\w{32}$/', $password) ? $password : md5($password);
  111.                 if(empty($user)) {
  112.                         $status = -1;
  113.                 } elseif($user['password'] != md5($passwordmd5.$user['salt'])) {
  114.                         $status = -2;
  115.                 } elseif($checkques && $user['secques'] != '' && $user['secques'] != $_ENV['user']->quescrypt($questionid, $answer)) {
  116.                         $status = -3;
  117.                 } else {
  118.                         $status = $user['uid'];
  119.                 }
  120.                 $merge = $status != -1 && !$isuid && $_ENV['user']->check_mergeuser($username) ? 1 : 0;
  121.                 return array($status, $user['username'], $password, $user['email'], $merge);
  122.         }

  123.         function oncheck_email() {
  124.                 $this->init_input();
  125.                 $email = $this->input('email');
  126.                 return $this->_check_email($email);
  127.         }

  128.         function oncheck_username() {
  129.                 $this->init_input();
  130.                 $username = $this->input('username');
  131.                 if(($status = $this->_check_username($username)) < 0) {
  132.                         return $status;
  133.                 } else {
  134.                         return 1;
  135.                 }
  136.         }

  137.         function onget_user() {
  138.                 $this->init_input();
  139.                 $username = $this->input('username');
  140.                 if(!$this->input('isuid')) {
  141.                         $status = $_ENV['user']->get_user_by_username($username);
  142.                 } else {
  143.                         $status = $_ENV['user']->get_user_by_uid($username);
  144.                 }
  145.                 if($status) {
  146.                         return array($status['uid'],$status['username'],$status['email']);
  147.                 } else {
  148.                         return 0;
  149.                 }
  150.         }


  151.         function ongetprotected() {
  152.                 $protectedmembers = $this->db->fetch_all("SELECT uid,username FROM ".UC_DBTABLEPRE."protectedmembers GROUP BY username");
  153.                 return $protectedmembers;
  154.         }

  155.         function ondelete() {
  156.                 $this->init_input();
  157.                 $uid = $this->input('uid');
  158.                 return $_ENV['user']->delete_user($uid);
  159.         }

  160.         function ondeleteavatar() {
  161.                 $this->init_input();
  162.                 $uid = $this->input('uid');
  163.                 $_ENV['user']->delete_useravatar($uid);
  164.         }

  165.         function onaddprotected() {
  166.                 $this->init_input();
  167.                 $username = $this->input('username');
  168.                 $admin = $this->input('admin');
  169.                 $appid = $this->app['appid'];
  170.                 $usernames = (array)$username;
  171.                 foreach($usernames as $username) {
  172.                         $user = $_ENV['user']->get_user_by_username($username);
  173.                         $uid = $user['uid'];
  174.                         $this->db->query("REPLACE INTO ".UC_DBTABLEPRE."protectedmembers SET uid='$uid', username='$username', appid='$appid', dateline='{$this->time}', admin='$admin'", 'SILENT');
  175.                 }
  176.                 return $this->db->errno() ? -1 : 1;
  177.         }

  178.         function ondeleteprotected() {
  179.                 $this->init_input();
  180.                 $username = $this->input('username');
  181.                 $appid = $this->app['appid'];
  182.                 $usernames = (array)$username;
  183.                 foreach($usernames as $username) {
  184.                         $this->db->query("DELETE FROM ".UC_DBTABLEPRE."protectedmembers WHERE username='$username' AND appid='$appid'");
  185.                 }
  186.                 return $this->db->errno() ? -1 : 1;
  187.         }

  188.         function onmerge() {
  189.                 $this->init_input();
  190.                 $oldusername = $this->input('oldusername');
  191.                 $newusername = $this->input('newusername');
  192.                 $uid = $this->input('uid');
  193.                 $password = $this->input('password');
  194.                 $email = $this->input('email');
  195.                 if(($status = $this->_check_username($newusername)) < 0) {
  196.                         return $status;
  197.                 }
  198.                 $uid = $_ENV['user']->add_user($newusername, $password, $email, $uid);
  199.                 $this->db->query("DELETE FROM ".UC_DBTABLEPRE."mergemembers WHERE appid='".$this->app['appid']."' AND username='$oldusername'");
  200.                 return $uid;
  201.         }

  202.         function onmerge_remove() {
  203.                 $this->init_input();
  204.                 $username = $this->input('username');
  205.                 $this->db->query("DELETE FROM ".UC_DBTABLEPRE."mergemembers WHERE appid='".$this->app['appid']."' AND username='$username'");
  206.                 return NULL;
  207.         }

  208.         function _check_username($username) {
  209.                 $username = addslashes(trim(stripslashes($username)));
  210.                 if(!$_ENV['user']->check_username($username)) {
  211.                         return UC_USER_CHECK_USERNAME_FAILED;
  212.                 } elseif(!$_ENV['user']->check_usernamecensor($username)) {
  213.                         return UC_USER_USERNAME_BADWORD;
  214.                 } elseif($_ENV['user']->check_usernameexists($username)) {
  215.                         return UC_USER_USERNAME_EXISTS;
  216.                 }
  217.                 return 1;
  218.         }

  219.         function _check_email($email, $username = '') {
  220.                 if(!$_ENV['user']->check_emailformat($email)) {
  221.                         return UC_USER_EMAIL_FORMAT_ILLEGAL;
  222.                 } elseif(!$_ENV['user']->check_emailaccess($email)) {
  223.                         return UC_USER_EMAIL_ACCESS_ILLEGAL;
  224.                 } elseif(!$this->settings['doublee'] && $_ENV['user']->check_emailexists($email, $username)) {
  225.                         return UC_USER_EMAIL_EXISTS;
  226.                 } else {
  227.                         return 1;
  228.                 }
  229.         }

  230.         function ongetcredit($arr) {
  231.                 $this->init_input();
  232.                 $appid = $this->input('appid');
  233.                 $uid = $this->input('uid');
  234.                 $credit = $this->input('credit');
  235.                 $this->load('note');
  236.                 $this->load('misc');
  237.                 $app = $this->cache['apps'][$appid];
  238.                 $apifilename = isset($app['apifilename']) && $app['apifilename'] ? $app['apifilename'] : 'uc.php';
  239.                 if($app['extra']['apppath'] && @include $app['extra']['apppath'].'./api/'.$apifilename) {
  240.                         $uc_note = new uc_note();
  241.                         return $uc_note->getcredit(array('uid' => $uid, 'credit' => $credit), '');
  242.                 } else {
  243.                         $url = $_ENV['note']->get_url_code('getcredit', "uid=$uid&credit=$credit", $appid);
  244.                         return $_ENV['misc']->dfopen($url, 0, '', '', 1, $app['ip'], UC_NOTE_TIMEOUT);
  245.                 }
  246.         }

  247.         //返回的内容为保存该文件的临时位置,camera.swf会通过返回的网址来读取图片
  248.         function onuploadavatar() {
  249.                 @header("Expires: 0");
  250.                 @header("Cache-Control: private, post-check=0, pre-check=0, max-age=0", FALSE);
  251.                 @header("Pragma: no-cache");
  252.                 //header("Content-type: application/xml; charset=utf-8");
  253.                 $this->init_input(getgpc('agent', 'G'));

  254.                 $uid = $this->input('uid');
  255.                 if(empty($uid)) {
  256.                         return -1;
  257.                 }
  258.                 if(empty($_FILES['Filedata'])) {
  259.                         return -3;
  260.                 }

  261.                 list($width, $height, $type, $attr) = getimagesize($_FILES['Filedata']['tmp_name']);
  262.                 if(!in_array($type, array(1,2,3,6))) {
  263.                         @unlink($_FILES['Filedata']['tmp_name']);
  264.                         return -4;
  265.                 }
  266.                 $imgtype = array(1 => '.gif', 2 => '.jpg', 3 => '.png');
  267.                 $filetype = $imgtype[$type];
  268.                 if(!$filetype) $filetype = '.jpg';
  269.                 //头像临时存放路径
  270.                 $tmpavatar = UC_DATADIR.'./tmp/upload'.$uid.$filetype;
  271.                 file_exists($tmpavatar) && @unlink($tmpavatar);
  272.                 if(@copy($_FILES['Filedata']['tmp_name'], $tmpavatar) || @move_uploaded_file($_FILES['Filedata']['tmp_name'], $tmpavatar)) {
  273.                         @unlink($_FILES['Filedata']['tmp_name']);
  274.                         list($width, $height, $type, $attr) = getimagesize($tmpavatar);
  275.                         if($width < 10 || $height < 10 || $type == 4) {
  276.                                 @unlink($tmpavatar);
  277.                                 return -2;
  278.                         }
  279.                 } else {
  280.                         @unlink($_FILES['Filedata']['tmp_name']);
  281.                         return -4;
  282.                 }
  283.                 $avatarurl = UC_DATAURL.'/tmp/upload'.$uid.$filetype;
  284.                 //返回临时头像的url路径
  285.                 return $avatarurl;
  286.         }

  287.         function onrectavatar() {
  288.                 @header("Expires: 0");
  289.                 @header("Cache-Control: private, post-check=0, pre-check=0, max-age=0", FALSE);
  290.                 @header("Pragma: no-cache");
  291.                 header("Content-type: application/xml; charset=utf-8");
  292.                 $this->init_input(getgpc('agent'));
  293.                 $uid = $this->input('uid');
  294.                 if(empty($uid)) {
  295.                         return '<root><message type="error" value="-1" /></root>';
  296.                 }
  297.                 $home = $this->get_home($uid);//返回头像的三个目录:"00/00/12","/avatar/"."00/00/12"."/45_small.jpg"
  298.                 if(!is_dir(UC_DATADIR.'./avatar/'.$home)) {
  299.                         $this->set_home($uid, UC_DATADIR.'./avatar/');//如果没有的话,则创建目录
  300.                 }
  301.                
  302.                 $avatartype = getgpc('avatartype', 'G') == 'real' ? 'real' : 'virtual';
  303.                 //UC_DATADIR:d:/wamp/www/discuz3/uc_server/data/
  304.                 //获取大中小头像的全路径
  305.                 $bigavatarfile = UC_DATADIR.'./avatar/'.$this->get_avatar($uid, 'big', $avatartype);
  306.                 $middleavatarfile = UC_DATADIR.'./avatar/'.$this->get_avatar($uid, 'middle', $avatartype);
  307.                 $smallavatarfile = UC_DATADIR.'./avatar/'.$this->get_avatar($uid, 'small', $avatartype);
  308.                 //getgpc:获取 $_GET、$_POST、$_COOKIE中数据
  309.                 $bigavatar = $this->flashdata_decode(getgpc('avatar1', 'P'));//$_POST
  310.                 $middleavatar = $this->flashdata_decode(getgpc('avatar2', 'P'));
  311.                 $smallavatar = $this->flashdata_decode(getgpc('avatar3', 'P'));
  312.                 if(!$bigavatar || !$middleavatar || !$smallavatar) {
  313.                         return '<root><message type="error" value="-2" /></root>';
  314.                 }

  315.                 $success = 1;
  316.                 //写入二进制数据到图片文件,$bigavatar:二进制数据内容
  317.                 $fp = @fopen($bigavatarfile, 'wb');
  318.                 @fwrite($fp, $bigavatar);
  319.                 @fclose($fp);

  320.                 $fp = @fopen($middleavatarfile, 'wb');
  321.                 @fwrite($fp, $middleavatar);
  322.                 @fclose($fp);

  323.                 $fp = @fopen($smallavatarfile, 'wb');
  324.                 @fwrite($fp, $smallavatar);//将二进制头像数据信息写入头像文件
  325.                 @fclose($fp);
  326.                
  327.                 /******************************************************************/
  328.                 $ftps=new Ftp();//实例化一个ftp对象,下面有这个类的定义
复制代码

二、uc_server/avatar.php文件代码如下:



  1. <pre code_snippet_id="130987" snippet_file_name="blog_20131227_3_8900473" name="code" class="php"><?php

  2. /*
  3.         [UCenter] (C)2001-2099 Comsenz Inc.
  4.         This is NOT a freeware, use is subject to license terms

  5.         $Id: avatar.php 1059 2011-03-01 07:25:09Z monkey $
  6. */


  7. error_reporting(0);
  8. define('UC_API', strtolower(($_SERVER['HTTPS'] == 'on' ? 'https' : 'http').'://'.$_SERVER['HTTP_HOST'].substr($_SERVER['PHP_SELF'], 0, strrpos($_SERVER['PHP_SELF'], '/'))));
  9. $uid = isset($_GET['uid']) ? $_GET['uid'] : 0;
  10. $size = isset($_GET['size']) ? $_GET['size'] : '';
  11. $random = isset($_GET['random']) ? $_GET['random'] : '';
  12. $type = isset($_GET['type']) ? $_GET['type'] : '';
  13. $check = isset($_GET['check_file_exists']) ? $_GET['check_file_exists'] : '';

  14. $avatar = './data/avatar/'.get_avatar($uid, $size, $type);
  15. if(my_file_exists('http://www.jiaoyuonline.com'.$avatar)) {//weiyanhui添加了一层dirname
  16.         if($check) {
  17.                 echo 1;
  18.                 exit;
  19.         }
  20.         $random = !empty($random) ? rand(1000, 9999) : '';
  21.         $avatar_url = empty($random) ? $avatar : $avatar.'?random='.$random;
  22. } else {
  23.         if($check) {
  24.                 echo 0;
  25.                 exit;
  26.         }
  27.         $size = in_array($size, array('big', 'middle', 'small')) ? $size : 'middle';
  28.         $avatar_url = 'images/noavatar_'.$size.'.gif';
  29. }

  30. if(empty($random)) {
  31.         header("HTTP/1.1 301 Moved Permanently");
  32.         header("Last-Modified:".date('r'));
  33.         header("Expires: ".date('r', time() + 86400));
  34. }

  35. header('Location: '.'http://www.aaaa.com/'.$avatar_url);//返回的远程服务器上的头像信息
  36. exit;

  37. function get_avatar($uid, $size = 'middle', $type = '') {
  38.         $size = in_array($size, array('big', 'middle', 'small')) ? $size : 'middle';
  39.         $uid = abs(intval($uid));
  40.         $uid = sprintf("%09d", $uid);
  41.         $dir1 = substr($uid, 0, 3);
  42.         $dir2 = substr($uid, 3, 2);
  43.         $dir3 = substr($uid, 5, 2);
  44.         $typeadd = $type == 'real' ? '_real' : '';
  45.         return $dir1.'/'.$dir2.'/'.$dir3.'/'.substr($uid, -2).$typeadd."_avatar_$size.jpg";
  46. }

  47. function my_file_exists($file)  
  48. {  
  49.     if(preg_match('/^http:\/\//',$file)){  
  50.         //远程文件  
  51.         if(ini_get('allow_url_fopen')){  
  52.             if(@fopen($file,'r')) return true;  
  53.         }  
  54.         else{  
  55.             $parseurl=parse_url($file);  
  56.             $host=$parseurl['host'];  
  57.             $path=$parseurl['path'];  
  58.             $fp=fsockopen($host,80, $errno, $errstr, 10);  
  59.             if(!$fp)return false;  
  60.             fputs($fp,"GET {$path} HTTP/1.1 \r\nhost:{$host}\r\n\r\n");  
  61.             if(preg_match('/HTTP\/1.1 200/',fgets($fp,1024))) return true;  
  62.         }  
  63.         return false;  
  64.     }  
  65.     return file_exists($file);  
  66. }  


  67. ?></pre><br>
  68. <br>
  69. <pre></pre>
  70. <p></p>
  71. <p>三、后台设置远程附件:</p>
  72. <p><img src="http://img.blog.csdn.net/20131227180335437?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWFuaHVpX3dlaQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt=""></p>
  73. <p></p>
  74. <p>四、sql修改:</p>
  75. <p></p>
  76. <p></p>
  77. <pre code_snippet_id="130987" snippet_file_name="blog_20131227_4_6903200" name="code" class="html">update pre_forum_attachment_0 set remote = '1';
  78. update pre_forum_attachment_1 set remote = '1';
  79. update pre_forum_attachment_2 set remote = '1';
  80. update pre_forum_attachment_3 set remote = '1';
  81. update pre_forum_attachment_4 set remote = '1';
  82. update pre_forum_attachment_5 set remote = '1';
  83. update pre_forum_attachment_6 set remote = '1';
  84. update pre_forum_attachment_7 set remote = '1';
  85. update pre_forum_attachment_8 set remote = '1';
  86. update pre_forum_attachment_9 set remote = '1';
  87. update pre_portal_article_title set remote=1;
  88. update pre_portal_attachment set remote=1;
  89. update pre_portal_topic_pic set remote=1;
  90. update pre_home_pic set remote=remote+1;
  91. </pre><br>
  92. <p></p>
  93. <p>五、效果如下:</p>
  94. <p><span style="word-wrap:break-word; font-style:normal; color:rgb(68,68,68); font-family:Tahoma,'Microsoft Yahei',Simsun; line-height:18px"></span></p>
  95. <div id="wp" class="wp" style="word-wrap:break-word; margin:0px auto; width:960px">
  96. <div id="ct" class="wp cl" style="word-wrap:break-word; zoom:1; margin:0px auto; width:auto; min-height:300px; border-color:rgb(194,213,227)">
  97. <div id="postlist" class="pl bm" style="word-wrap:break-word; margin-bottom:10px; border:1px solid rgb(205,205,205)">
  98. <div id="post_25424612" style="word-wrap:break-word"><br>
  99. </div>
  100. <div id="post_25424612" style="word-wrap:break-word"><img src="http://img.blog.csdn.net/20131227180751421?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWFuaHVpX3dlaQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt=""></div>
  101. </div>
  102. </div>
  103. </div>
  104. <pre></pre>
复制代码



回复 马甲回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|网站地图|小黑屋|展会网|Discuz站长论坛 |天天打卡

GMT+8, 2024-4-30 11:32 , Processed in 0.029325 second(s), 6 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表