裸奔的鸡蛋 发表于 2018-8-10 20:11:29

通过ucenter给discuz批量自动注册马甲带头像

通过ucenter给discuz批量注册马甲带头像,因为要做个测试。需要大量的马甲。上网找了几个discuz的注册马甲软件,发现都不好用,太麻烦了。没办法,只好自己写个脚本来搞10W多的马甲自己玩!

代码在如下

<?php
//头像放置文件avatar.txt,用户名放置文件users.txt.一行一个有效数据
//定义马甲的相关信息!1.ip注册IP,2.用户密码,3.用户email,UCD.ucenter的头像文件夹。要求可读写权限,
//4.NUMBER是添加数量。如果文件中用户名少于定义的数量。将以实际为准
/*
* ps.如果数据库已经存在用户,则跳过
* */
define ( "IP", '127.0.0.1' );
define ( "PASSWORD", 'password' );
define ( "EMAIL", 'admin@admin.com' );
define ( 'UPRE', 'uc_' );
define ( "DPRE", 'pre_' );
define("UCD",'/home/wwwroot/ucenter/data/avatar/');
define('NUMBER', 15000);
/**
* 暂时用于shell中运行*
*/
//ucenter的数据库配置
$udbserver = 'localhost'; // 此处改成数据库服务器地址
$udbuser = 'root'; // 此处写数据库用户名
$udbpwd = 'root'; // 数据库密码
$udbname = 'ucenter'; // 数据库名称
$ucharset = 'utf8'; // 此处写字符集gbk或者utf8

//discuz的数据库配置
$ddbserver = 'localhost'; // 此处改成数据库服务器地址
$ddbuser = 'root'; // 此处写数据库用户名
$ddbpwd = 'root'; // 数据库密码
$ddbname = 'ultrax'; // 数据库名称
$dcharset = 'utf8'; // 此处写字符集gbk或者utf8
                  
// 连接ucenter
$udb = new PDO ( 'mysql:host=' . $udbserver . ';dbname=' . $udbname, $udbuser, $udbpwd );
if (! $udb)
    exit ( "ucenter can't connet db" );
$udb->exec ( 'set NAMES ' . $ucharset );
// 连接discuz
$ddb = new PDO ( 'mysql:host=' . $ddbserver . ';dbname=' . $ddbname, $ddbuser, $ddbpwd );
if (! $udb)
    exit ( "discuz can't connet udb" );
$ddb->exec ( 'set NAMES ' . $dcharset );
if (! $ddb)
    exit ( "can't connet ddb" );
   
    // 实例化图片类
$image = new Image ();

$_ENV ['udb'] = $udb;
$_ENV ['ddb'] = $ddb;
$_ENV ['image'] = $image;

global $avatdata;

//生产注册时间
for($i = 0; $i < 100010; $i ++) {
    $times [] = $time = strtotime ( '2012-02-01 00:00:00' ) + mt_rand ( 10000, 30000000 );
}
sort($times);
//获取用户名列表
$filedata = file_get_contents ( 'users.txt' );
//用户列表数据
$datas = explode ( "\r\n", $filedata );

//开始添加
uc_adduser ( $datas, $times);



// UCENTER添加用户
function uc_adduser($data, $times) {
    $dnumber = count($data);
    if ($dnumber<NUMBER){
      define('NUMBER', $dnumber);
    }
    $rsth = $_ENV ['udb']->prepare ( "INSERT INTO " . UPRE . "members SET username=?, password=?, email='" . EMAIL . "', regip='" . IP . "', regdate=?, salt=?" );
    $rfth = $_ENV ['udb']->prepare ( "INSERT INTO " . UPRE . "memberfields SET uid=?" );
    foreach ( $data as $key => $name ) {
      $salt = substr ( uniqid ( rand () ), - 6 );
      $password = md5 ( md5 ( PASSWORD ) . $salt );
         $result =$rsth->execute ( array (
                addslashes(trim($name)),
                $password,
                $times [$key],
                $salt
      ) );
         
      $uid = $_ENV ['udb']->lastInsertId ();
      if ($uid){
            $rfth->execute ( array (
                  $uid
            ) );
            //激活论坛
            ati_dz($uid,$name,$times [$key]);
            //添加头像
            uc_avatar($uid);
            echo $name."添加成功\n";
      }else{
            continue;
      }
      
      //添加数量
      if ($key > NUMBER) {
            echo "用户导入完成!";
            break;
      }
    }
}

// 添加头像
function uc_avatar($uid) {
    //头像数据
    global $avatdata;
    $url = "http://image.xxx.com/img/";
    if (empty($avatdata)){
      $avart = file_get_contents ( 'avatar.txt' );
      $avatdata = explode ( "\r\n", $avart );
    }
    $pic = array_pop($avatdata);
    $bigavatarfile = UCD.get_avatar($uid, 'big');
    $middleavatarfile = UCD.get_avatar($uid, 'middle');
    $smallavatarfile = UCD.get_avatar($uid, 'small');
   
    $path = dirname($bigavatarfile);
    if (! file_exists ( $path )) {
      mkdir ( $path, 0700, true );
    }
    $originpath = UCD.$pic;
    if(downImg($url.$pic, $originpath)){
      $_ENV['image']->bthumb($originpath,200,200,1,true,$bigavatarfile);
      $_ENV['image']->bthumb($originpath,120,120,1,true,$middleavatarfile);
      $_ENV['image']->bthumb($originpath,48,48,1,true,$smallavatarfile);
    }
}

// 激活论坛
function ati_dz($uid,$username,$time) {
    $password = md5(random(10));
    $_ENV['ddb']->exec( "INSERT INTO `" . DPRE . "common_member` (uid,username,password,adminid,groupid,regdate,email,emailstatus,avatarstatus) VALUES ('$uid','$username', '$password','0','10','$time','".EMAIL."',1,1)");
    $_ENV['ddb']->exec(" replace INTO `" . DPRE . "common_member_field_forum` (uid) VALUES ('$uid')");
    $_ENV['ddb']->exec(" replace INTO    `" . DPRE . "common_member_field_home` (uid) VALUES ('$uid')");
    $_ENV['ddb']->exec(" replace INTO `" . DPRE . "common_member_count` (uid) VALUES ('$uid')");
    $_ENV['ddb']->exec(" replace INTO `" . DPRE . "common_member_profile` (uid) VALUES ('$uid')");
    $_ENV['ddb']->exec(" replace INTO `" . DPRE . "common_member_status` (uid,regip,lastvisit,lastactivity) VALUES ('$uid','127.0.0.1','$time','$time')");
}

/**
* 自动创建目录并返回文件名
*   
*/
function mkfilename($img, $time) {
    $info = pathinfo ( $img );
    $type = strtolower ( $info ['extension'] );
    $path = './datas/uploadimage/images/';
    $time = strtotime ( $time );
    $filepath = $path . date ( "Y-m", $time ) . '/' . date ( "d", $time ) . '/' . time () . mt_rand ( 10000, 99999 ) . "." . $type;
    $path = dirname ( $filepath );
    if (! file_exists ( $path )) {
      mkdir ( $path, 0777, true );
    }
    return $filepath;
}

/**
* 下载文件
*   
*/
function downImg($url, $filename) {
    $ch = curl_init ( $url );
    $fp = fopen ( $filename, 'wb+' );
    curl_setopt ( $ch, CURLOPT_FILE, $fp );
    curl_setopt ( $ch, CURLOPT_HEADER, 0 );
    curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 30 );
    curl_setopt ( $ch, CURLOPT_AUTOREFERER, 1 );
    curl_exec ( $ch );
    $status = curl_getinfo ( $ch );
    curl_close ( $ch );
    fclose ( $fp );
    if ($status ['http_code'] != 200) {
      @unlink ( $filename );
      return false;
    } else {
      return true;
    }
}

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


function random($length) {
    $hash = '';
    $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
    $max = strlen($chars) - 1;
    PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
    for($i = 0; $i < $length; $i++) {
      $hash .= $chars;
    }
    return $hash;
}

/**
* 日志记录
*   
*/
function mlog($msg) {
    $file = "userIDs.text";
    $handle = fopen ( $file, 'a+' );
    fwrite ( $handle, $msg . "\n" );
    fclose ( $handle );
}


class Image {
    /**
   * 取得图像信息
   *
   * @static
   *
   *
   * @access public
   * @param string $image
   *            图像文件名
   * @return mixed
   */
    static function getImageInfo($img) {
      $imageInfo = getimagesize ( $img );
      if ($imageInfo !== false) {
            $imageType = strtolower ( substr ( image_type_to_extension ( $imageInfo ), 1 ) );
            $imageSize = filesize ( $img );
            $info = array (
                  "width" => $imageInfo ,
                  "height" => $imageInfo ,
                  "type" => $imageType,
                  "size" => $imageSize,
                  "mime" => $imageInfo ['mime']
            );
            return $info;
      } else {
            return false;
      }
    }
   
    /**
   * 为图片添加水印
   *
   * @static public
   * @param string $source
   *            原文件名
   * @param string $water
   *            水印图片
   * @param string $savename
   *            添加水印后的图片名
   * @param string $alpha
   *            水印的透明度
   * @return void
   */
    static public function water($source, $water, $savename = null, $alpha = 80) {
      // 检查文件是否存在
      if (! file_exists ( $source ) || ! file_exists ( $water ))
            return false;
            
            // 图片信息
      $sInfo = self::getImageInfo ( $source );
      $wInfo = self::getImageInfo ( $water );
      
      // 如果图片小于水印图片,不生成图片
      if ($sInfo ["width"] < $wInfo ["width"] || $sInfo ['height'] < $wInfo ['height'])
            return false;
            
            // 建立图像
      $sCreateFun = "imagecreatefrom" . $sInfo ['type'];
      if (! function_exists ( $sCreateFun ))
            $sCreateFun = 'imagecreatefromjpeg';
      $sImage = $sCreateFun ( $source );
      $wCreateFun = "imagecreatefrom" . $wInfo ['type'];
      if (! function_exists ( $wCreateFun ))
            $wCreateFun = 'imagecreatefromjpeg';
      $wImage = $wCreateFun ( $water );
      
      // 设定图像的混色模式
      imagealphablending ( $wImage, true );
      
      // 图像位置,默认为右下角右对齐
      $posY = $sInfo ["height"] - $wInfo ["height"] - 4;
      $posX = $sInfo ["width"] - $wInfo ["width"] - 4;
      
      // 生成混合图像
      imagecopymerge ( $sImage, $wImage, $posX, $posY, 0, 0, $wInfo ['width'], $wInfo ['height'], $alpha );
      
      // 输出图像
      $ImageFun = 'Image' . $sInfo ['type'];
      if (! function_exists ( $ImageFun ))
            $ImageFun = 'imagecreatefromjpeg';
            // 如果没有给出保存文件名,默认为原图像名
      if (! $savename) {
            $savename = $source;
            @unlink ( $source );
      }
      // 保存图像
      $ImageFun ( $sImage, $savename );
      imagedestroy ( $sImage );
    }
    /**
   * 为图片添加水印
   *
   * @static public
   * @param string $source
   *            原文件名
   * @param string $water
   *            水印图片
   * @param string $savename
   *            添加水印后的图片名
   * @param string $alpha
   *            水印的透明度
   * @return void
   */
    static public function water2($source, $water, $savename = null, $alpha = 75) {
      // 检查文件是否存在
      if (! file_exists ( $source ) || ! file_exists ( $water ))
            return false;
            
            // 图片信息
      $sInfo = self::getImageInfo ( $source );
      $wInfo = self::getImageInfo ( $water );
      
      // 如果图片小于水印图片,不生成图片
      if ($sInfo ["width"] < $wInfo ["width"] || $sInfo ['height'] < $wInfo ['height'])
            return false;
            
            // 建立图像
      $sCreateFun = "imagecreatefrom" . $sInfo ['type'];
      if (! function_exists ( $sCreateFun ))
            $sCreateFun = 'imagecreatefromjpeg';
      $sImage = $sCreateFun ( $source );
      $wCreateFun = "imagecreatefrom" . $wInfo ['type'];
      if (! function_exists ( $wCreateFun ))
            $wCreateFun = 'imagecreatefromjpeg';
      $wImage = $wCreateFun ( $water );
      
      // 设定图像的混色模式
      imagealphablending ( $wImage, true );
      
      // 图像位置,默认为右下角右对齐
      $posY = $sInfo ["height"] - $wInfo ["height"] - 18;
      $posX = $sInfo ["width"] - $wInfo ["width"] - 18;
      
      // 生成混合图像
      imagecopymerge ( $sImage, $wImage, $posX, $posY, 0, 0, $wInfo ['width'], $wInfo ['height'], $alpha );
      
      // 输出图像
      $ImageFun = 'Image' . $sInfo ['type'];
      if (! function_exists ( $ImageFun ))
            $ImageFun = 'imagecreatefromjpeg';
            // 如果没有给出保存文件名,默认为原图像名
      if (! $savename) {
            $savename = $source;
            @unlink ( $source );
      }
      // 保存图像
      $ImageFun ( $sImage, $savename );
      imagedestroy ( $sImage );
    }
    function showImg($imgFile, $text = '', $x = '10', $y = '10', $alpha = '50') {
      // 获取图像文件信息
      // 2007/6/26 增加图片水印输出,$text为图片的完整路径即可
      $info = Image::getImageInfo ( $imgFile );
      if ($info !== false) {
            $createFun = str_replace ( '/', 'createfrom', $info ['mime'] );
            $im = $createFun ( $imgFile );
            if ($im) {
                $ImageFun = str_replace ( '/', '', $info ['mime'] );
                // 水印开始
                if (! empty ( $text )) {
                  $tc = imagecolorallocate ( $im, 0, 0, 0 );
                  if (is_file ( $text ) && file_exists ( $text )) { // 判断$text是否是图片路径
                                                                      // 取得水印信息
                        $textInfo = Image::getImageInfo ( $text );
                        $createFun2 = str_replace ( '/', 'createfrom', $textInfo ['mime'] );
                        $waterMark = $createFun2 ( $text );
                        // $waterMark=imagecolorallocatealpha($text,255,255,0,50);
                        $imgW = $info ["width"];
                        $imgH = $info ["width"] * $textInfo ["height"] / $textInfo ["width"];
                        // $y = ($info["height"]-$textInfo["height"])/2;
                        // 设置水印的显示位置和透明度支持各种图片格式
                        imagecopymerge ( $im, $waterMark, $x, $y, 0, 0, $textInfo ['width'], $textInfo ['height'], $alpha );
                  } else {
                        imagestring ( $im, 80, $x, $y, $text, $tc );
                  }
                  // ImageDestroy($tc);
                }
                // 水印结束
                if ($info ['type'] == 'png' || $info ['type'] == 'gif') {
                  imagealphablending ( $im, FALSE ); // 取消默认的混色模式
                  imagesavealpha ( $im, TRUE ); // 设定保存完整的 alpha 通道信息
                }
                Header ( "Content-type: " . $info ['mime'] );
                $ImageFun ( $im );
                @ImageDestroy ( $im );
                return;
            }
            
            // 保存图像
            $ImageFun ( $sImage, $savename );
            imagedestroy ( $sImage );
            // 获取或者创建图像文件失败则生成空白PNG图片
            $im = imagecreatetruecolor ( 80, 30 );
            $bgc = imagecolorallocate ( $im, 255, 255, 255 );
            $tc = imagecolorallocate ( $im, 0, 0, 0 );
            imagefilledrectangle ( $im, 0, 0, 150, 30, $bgc );
            imagestring ( $im, 4, 5, 5, "no pic", $tc );
            Image::output ( $im );
            return;
      }
    }
    public function bthumb($image, $maxWidth = 200, $maxHeight = 50, $gen = 0, $interlace = true, $filepath = '') {
      $info = Image::getImageInfo ( $image );
      if ($info !== false) {
            $srcWidth = $info ['width'];
            $srcHeight = $info ['height'];
            $type = $info ['type'];
            
            $interlace = $interlace ? 1 : 0;
            unset ( $info );
            
            if ($maxWidth > 0 && $maxHeight > 0) {
                // $scale = min($maxWidth/$srcWidth, $maxHeight/$srcHeight); //
                // 计算缩放比例
                // 改为以宽度缩放
                $scale = $maxWidth / $srcWidth;
            } elseif ($maxWidth == 0)
                $scale = $maxHeight / $srcHeight;
            elseif ($maxHeight == 0)
                $scale = $maxWidth / $srcWidth;
            
            if ($scale >= 1) {
                // 超过原图大小不再缩略
                $width = $srcWidth;
                $height = $srcHeight;
            } else {
                // 缩略图尺寸
                $width = ( int ) ($srcWidth * $scale);
                $height = ( int ) ($srcHeight * $scale);
            }
            
            if ($gen == 1) {
                $width = $maxWidth;
                $height = $maxHeight;
            }
            
            $paths = pathinfo ( $image );
            $ext = Image::fileExt ( $image );
            
            if (empty ( $filepath ))
                $thumbname = str_replace ( '.' . $paths ['extension'], '', $image ) . '_' . $maxWidth . 'x' . $maxHeight . '.' . $ext;
            else
                $thumbname = $filepath;
               
                // 载入原图
            $createFun = 'imagecreatefrom' . ($type == 'jpg' ? 'jpeg' : $type);
            if (! function_exists ( $createFun ))
                $createFun = 'imagecreatefromjpeg';
            
            $srcImg = $createFun ( $image );
            
            // 创建缩略图
            if ($type != 'gif' && function_exists ( 'imagecreatetruecolor' ))
                $thumbImg = imagecreatetruecolor ( $width, $height );
            else
                $thumbImg = imagecreate ( $width, $height );
            
            $x = 0;
            $y = 0;
            
            if ($gen == 1 && $maxWidth > 0 && $maxHeight > 0) {
                $resize_ratio = $maxWidth / $maxHeight;
                $src_ratio = $srcWidth / $srcHeight;
                if ($src_ratio >= $resize_ratio) {
                  $x = ($srcWidth - ($resize_ratio * $srcHeight)) / 2;
                  $width = ($height * $srcWidth) / $srcHeight;
                } else {
                  $y = ($srcHeight - ((1 / $resize_ratio) * $srcWidth)) / 2;
                  $height = ($width * $srcHeight) / $srcWidth;
                }
            }
            $x = 0;
            $y = 0;
            
            // 复制图片
            if (function_exists ( "imagecopyresampled" ))
                imagecopyresampled ( $thumbImg, $srcImg, 0, 0, $x, $y, $width, $height, $srcWidth, $srcHeight );
            else
                imagecopyresized ( $thumbImg, $srcImg, 0, 0, $x, $y, $width, $height, $srcWidth, $srcHeight );
            
            if ('gif' == $type || 'png' == $type) {
                $background_color = imagecolorallocate ( $thumbImg, 0, 0, 0 ); // 指派一个绿色
                imagecolortransparent ( $thumbImg, $background_color ); // 设置为透明色,若注释掉该行则输出绿色的图
            }
            
            // 对jpeg图形设置隔行扫描
            if ('jpg' == $type || 'jpeg' == $type)
                imageinterlace ( $thumbImg, $interlace );
               
                // 生成图片
            if (function_exists ( 'imagefilter' ))
                imagefilter ( $thumbImg, IMG_FILTER_CONTRAST, - 1 );
               
                // 保存图片
            imagejpeg ( $thumbImg, $thumbname, 100 );
            imagedestroy ( $thumbImg );
            imagedestroy ( $srcImg );
            return $thumbname;
      }
      return false;
    }
   
    /**
   * 生成缩略图
   *
   * @static
   *
   *
   *
   *
   *
   *
   *
   *
   *
   * @access public
   * @param string $image
   *            原图
   * @param string $type
   *            图像格式
   * @param string $thumbname
   *            缩略图文件名
   * @param string $maxWidth
   *            宽度
   * @param string $maxHeight
   *            高度
   * @param string $position
   *            缩略图保存目录
   * @param boolean $interlace
   *            启用隔行扫描
   * @return void
   */
    static function thumb($image, $thumbname, $type = '', $maxWidth = 200, $maxHeight = 50, $interlace = true) {
      // 获取原图信息
      $info = Image::getImageInfo ( $image );
      if ($info !== false) {
            $srcWidth = $info ['width'];
            $srcHeight = $info ['height'];
            $type = empty ( $type ) ? $info ['type'] : $type;
            $type = strtolower ( $type );
            $interlace = $interlace ? 1 : 0;
            unset ( $info );
            $scale = min ( $maxWidth / $srcWidth, $maxHeight / $srcHeight ); // 计算缩放比例
            if ($scale >= 1) {
                // 超过原图大小不再缩略
                $width = $srcWidth;
                $height = $srcHeight;
            } else {
                // 缩略图尺寸
                $width = ( int ) ($srcWidth * $scale);
                $height = ( int ) ($srcHeight * $scale);
            }
            
            // 载入原图
            $createFun = 'ImageCreateFrom' . ($type == 'jpg' ? 'jpeg' : $type);
            if (! function_exists ( $createFun )) {
                return false;
            }
            $srcImg = $createFun ( $image );
            
            // 创建缩略图
            if ($type != 'gif' && function_exists ( 'imagecreatetruecolor' ))
                $thumbImg = imagecreatetruecolor ( $width, $height );
            else
                $thumbImg = imagecreate ( $width, $height );
                // png和gif的透明处理 by luofei614
            if ('png' == $type) {
                imagealphablending ( $thumbImg, false ); // 取消默认的混色模式(为解决阴影为绿色的问题)
                imagesavealpha ( $thumbImg, true ); // 设定保存完整的 alpha
                                                      // 通道信息(为解决阴影为绿色的问题)
            } elseif ('gif' == $type) {
                $trnprt_indx = imagecolortransparent ( $srcImg );
                if ($trnprt_indx >= 0) {
                  // its transparent
                  $trnprt_color = imagecolorsforindex ( $srcImg, $trnprt_indx );
                  $trnprt_indx = imagecolorallocate ( $thumbImg, $trnprt_color ['red'], $trnprt_color ['green'], $trnprt_color ['blue'] );
                  imagefill ( $thumbImg, 0, 0, $trnprt_indx );
                  imagecolortransparent ( $thumbImg, $trnprt_indx );
                }
            }
            // 复制图片
            if (function_exists ( "ImageCopyResampled" ))
                imagecopyresampled ( $thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight );
            else
                imagecopyresized ( $thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight );
               
                // 对jpeg图形设置隔行扫描
            if ('jpg' == $type || 'jpeg' == $type)
                imageinterlace ( $thumbImg, $interlace );
               
                // 生成图片
            $imageFun = 'image' . ($type == 'jpg' ? 'jpeg' : $type);
            $imageFun ( $thumbImg, $thumbname );
            imagedestroy ( $thumbImg );
            imagedestroy ( $srcImg );
            return $thumbname;
      }
      return false;
    }
    static function output($im, $type = 'png', $filename = '') {
      header ( "Content-type: image/" . $type );
      $ImageFun = 'image' . $type;
      if (empty ( $filename )) {
            $ImageFun ( $im );
      } else {
            $ImageFun ( $im, $filename );
      }
      imagedestroy ( $im );
    }
   
    /**
   * 获取文件扩展名
   *
   * @return string
   */
    function fileExt($file_name) {
      return addslashes ( strtolower ( substr ( strrchr ( $file_name, '.' ), 1, 10 ) ) );
    }
}
?>代码只是初级代码,本地测试已经可以使用。里面可能有bug,欢迎提出或自行修改。

页: [1]
查看完整版本: 通过ucenter给discuz批量自动注册马甲带头像