<?php

/**
 *      [Discuz!] (C)2001-2012 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: change_medium_avatar.php 30292 2012-07-7 010:00:00Z yanghuan $
 */

include_once('./source/class/class_core.php');
include_once('./source/function/function_core.php');

@set_time_limit(0);

$cachelist = array();
$discuz = C::app();

$discuz->cachelist = $cachelist;
$discuz->init_cron = false;
$discuz->init_setting = false;
$discuz->init_user = false;
$discuz->init_session = false;
$discuz->init_misc = false;
@header('Content-Type: text/html; charset=gbk');

$discuz->init();

$dir = DISCUZ_ROOT.'uc_server/data/avatar/';
$filearr = dreaddir($dir);

foreach($filearr as $value1) {
	$resultdir1 = $dir.$value1."/";
	$dir1 =  dreaddir($resultdir1);
	foreach($dir1 as $value2) {
		$dir2 = $dir.$value1."/".$value2."/";
		$resultdir2 = dreaddir($dir2);
		foreach($resultdir2 as $value3) {
			$resultdir3 = $dir.$value1."/".$value2."/".$value3."/";
			if ($dh = opendir($resultdir3)) {
				while (($file = readdir($dh)) !== false) {
					if ($file == "." || $file == "..") continue;
					if(filetype($resultdir3 . $file) == 'file' && strpos($file, 'medium')) {
						$newfile = str_replace("medium", "middle", $file);
						rename($resultdir3.$file, $resultdir3.$newfile);
					}
				}
				closedir($dh);
			}
		}
	}
}

echo "修改完成，请到网站根目录uc_server/data/avatar/下查看确认";

function dreaddir($dir, $extarr=array()) {
	$dirs = array();
	if($dh = opendir($dir)) {
		while (($file = readdir($dh)) !== false) {
			if(!empty($extarr) && is_array($extarr)) {
				if(in_array(strtolower(fileext($file)), $extarr)) {
					$dirs[] = $file;
				}
			} else if($file != '.' && $file != '..' && $file != 'index.htm') {
				$dirs[] = $file;
			}
		}
		closedir($dh);
	}
	return $dirs;
}

?>
