/sop/2.0beta1/Lib/Action/Admin/FtpbatchAction.class.php
PHP | 360 lines | 276 code | 26 blank | 58 comment | 54 complexity | 94d5d9fa3eed7a447d1be3019aae1f7c MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, LGPL-2.1
- <?php
- /**
- * @author nanze
- * @link
- * @todo FTP??
- * @copyright 811046@qq.com
- * @version 1.0
- * @lastupdate 2010-11-18
- */
- class FtpbatchAction extends AdminCommAction {
- public $products_image_path;
- public $products_temp_path;
- function _initialize()
- {
- parent::_initialize();
- import("ORG.Io.Dir");
- import("ORG.Util.Image");
- set_time_limit(0);
- $this->products_image_path="./Public/Uploads/Products";
- $this->products_temp_path="./Public/Uploads/Temp";
- header("Content-Type:text/html; charset=utf-8");
- }
- public function index() {
- if($this->isPost() && $_POST['type_id']){
- self::$Model=D("Type_attr");
- $attr=self::$Model->where(array('type_id'=>$_POST['type_id'],'status'=>1))->order("sort desc")->findall();
- foreach ($attr as $k=>$v){
- $attr[$k]['values']=explode(chr(13),$v['values']);
- foreach ($attr[$k]['values'] as $kk=>$vv){
- $attr[$k]['values'][$kk]=str_replace("\n","",$vv);
- //??????
- $attr[$k]['attrs'][$kk]['attr_id']=$v['id'];
- $attr[$k]['attrs'][$kk]['attr_value']=$attr[$k]['values'][$kk];
- }
- }
- $this->attr=$attr;
- die($this->fetch('Ftpbatch-getattr'));
- }
- /**
- * ????
- */
- $realpath="./".auto_charset($_POST['f'],'utf-8','gbk');
- if(!empty( $_POST['f']) && substr_count(realpath($realpath),realpath($this->products_temp_path))>0){
- $dir = $_REQUEST['f']; //????
- }else {
- $dir = $this->products_temp_path; //???
- }
- $fileName = array();
- /**
- * ????
- *
- $file=auto_charset($file,'gbk','utf-8');
- */
- $dir=auto_charset($dir,'utf-8','gbk');
- if (is_dir( $dir)) {
- if ($dh = opendir( $dir)) {
- while (($file = readdir($dh)) !== false) {
- $file=auto_charset($file,'gbk','utf-8');
- if($file != "." && $file !=".."){
- if(is_dir(auto_charset("$dir/$file",'utf-8','gbk'))){
- $enterPath=substr( "$dir/$file",2); //??..
- $fileName[$i]['href'] = "<a href=\"javascript:e('$enterPath');\" >$file</a>";
- }else{
- $fileName[$i]['href'] = $file;
- }
- $fileName[$i]['file'] = $file;
- $i++;
- }
- }
- closedir($dh);
- }
- }
- $dir=auto_charset($dir,'gbk','utf-8');
- $this->assign('path',str_replace("./","", $dir)); //????
- $this->assign('dir',str_replace("./","", $dir)); //????????
- $this->assign('fileName', $fileName); //??????
- $this->assign('uplevel', realpath($dir)==realpath($this->products_temp_path)?$this->products_temp_path:dirname($dir)); //?????
- $this->display();
- return;
- }
- /**
- * ???????????
- *
- * @param string $directory ?????
- */
- function deletefile($directory){
- $directory = $_POST['checkbox']; //????
- $path = "./".$_POST['path'];
- for($i=0;$i<count($directory);$i++){
- $Temp_path="$path/${directory[$i]}"; //????
- $Temp_path=auto_charset($Temp_path,'utf-8','gbk');
- if (is_dir($Temp_path) == true) //??????
- {
- $this->delDir($Temp_path);
- }else{
- unlink($Temp_path); //????
- }
- }
- closedir($handle);
- $this->success('????');
- }
- /**
- * ????
- *
- */
- function moreuploadfile(){
- $this->assign("jumpUrl",$_SERVER["HTTP_REFERER"]);
- echo "<style type=\"text/css\">* {font-size:11px}</style>";
- $filename = $_POST['checkbox']; //??????
- $path = "./".$_POST['path']; //?????..
- for($i=0;$i<count($filename);$i++){
- $filename[$i]=auto_charset($filename[$i],'utf-8','gbk');
- if(file_exists($path.'/'.$filename[$i]) ==true){
- $filename[$i]=auto_charset($filename[$i],'gbk','utf-8');
- if(is_dir("$path/{$filename[$i]}")){
- $this->showjsmessage("???????$path/{$filename[$i]}");
- }
- if(isset($_POST['importClass']) && $_POST['importClass']==1){
- $this->processdir($path.'/'.$filename[$i],$_POST['cateid']);
- }else{
- $this->processdir($path.'/'.$filename[$i]);
- }
- }
- }
- //?????
- foreach ($filename as $v){
- $v=auto_charset($v,'utf-8','gbk');
- if(file_exists($path.'/'.$v) ==true){
- if(is_dir($path."/".$v)){
- $this->delDir($path."/".$v); //?????????????
- $this->showjsmessage("???????$path/".auto_charset($v,'gbk','utf-8'));
- }else{
- unlink($path."/".$v); //????
- $this->showjsmessage("??????$path/".auto_charset($v,'gbk','utf-8'));
- }
- }
- }
- $this->showjsmessage("??????!");
- }
- /**
- * ?????
- *
- * @param ??????? $dir
- * @param ??? $pid
- */
- function processdir($dir,$pid=0){
- $cate_model=D('Cate');
- $pro_model=D('Products');
- $dir=auto_charset($dir,'utf-8','gbk');
- if (is_dir($dir)) {
- $data['name']=auto_charset(basename($dir),'gbk','utf-8');
- $id=$this->GetCateId($data['name']);
- if(!$id){
- $data['pid']=$pid;
- $id=$cate_model->add($data);
- }
- if(isset($_POST['type_id']) && !empty($_POST['type_id'])){
- $cate_model->where(array('id'=>$id))->data(array('type_id'=>$_POST['type_id']))->save();
- }
- if ($dh = opendir($dir)) {
- while (($file = readdir($dh)) !== false) {
- if($file != "." && $file !=".."){
- if(is_dir("$dir/$file")){
- $dir=auto_charset($dir,'gbk','utf-8');
- $this->processdir($dir.'/'.$file,$id);
- }else{
- $this->processfile($dir,$file,$id,$pid);
- }
- }
- }
- }
- }else{
- //??????
- $file=$dir;
- $dir='';
- $cateid=$_POST['cateid']?$_POST['cateid']:$pid;
- $this->processfile($dir,$file,$cateid,$pid);
- }
- }
- function processfile($dir,$file,$cateid,$pid=0){
- $cate_model=D('Cate');
- $pro_model=D('Products');
- $extend = pathinfo($file); //???
- $extend = $extend["extension"];
- $pro=$pro_model->create();
- //????
- if($_POST['autoReName']==1){
- $k=$this->get_auto_increment('count',$cateid);//??????????
- $autoname=$this->GetCateName($cateid)."-".$k;
- $pro['name']=$autoname;
- }else{
- $pro['name']=str_replace(".$extend",'',auto_charset(basename($file),'gbk','utf-8'));
- }
- if(!$cateid){
- $pro['cateid']=$pid;
- }else{
- $pro['cateid']=$cateid;
- }
- $pro['bigimage']=$this->products_image_path .'/'.toDate ( time (), 'Ymd' ) . "/".$pro['name'].'.'.$extend;
- $new_path=dirname($pro['bigimage']).'/';
- if(!file_exists($new_path)){
- mkdir($new_path);
- }
- $new_name=$new_path.$pro['name'].".$extend";
- //????
- $old_name=$dir."/".$file;
- $new_name=auto_charset($new_name,'utf-8','gbk');
- rename($old_name,$new_name);
- //??,???,??,?,?,????
- $thumbname = Image::thumb($new_name,dirname($new_name).'/thumb_'.basename($new_name),'',GetSettValue('ImgThumbW'),GetSettValue('ImgThumbH'),true,'');
- $pthumbname=pathinfo($thumbname);
- $pro['smallimage']=$new_path.auto_charset($pthumbname['filename'],'gbk','utf-8').".".$pthumbname['extension'];
- $pro['dateline']=time();
- $id=$pro_model->add($pro);
- if($pro['cateid']){
- $this->batch_attr($id);//??
- }
- //?????
- $model = D( "Products_gallery" );
- $data=array();
- $data ['pid'] = $id;
- $data ['img_url'] = $pro['bigimage'];
- $data ['thumb_url'] = $pro['smallimage'];
- $model->add ( $data );
- //??
- $this->showjsmessage("????".auto_charset($file,'gbk','utf-8'));
- }
- /**
- * ??????
- */
- function batch_attr($id){
- self::$Model=D("Products_attr");
- foreach ($_POST ['attr_id'] as $key => $attr_id ) {
- foreach ( $_POST ['attr_value_' . $attr_id] as $key => $attr_value ) {
- if (!empty($attr_value))
- {
- //???????
- $data ['products_id'] = $id;
- $data ['attr_id'] = $attr_id;
- $data ['attr_value'] = str_replace ( "\n", "", $attr_value );
- if (self::$Model->create ( $data )) {
- self::$Model->add ( $data );
- } else {
- $this->error ( self::$Model->geterror () );
- }
- }
- }
- }
- }
- /**
- * ?????????
- *
- * @param string $directory
- * @return boolean
- */
- function isEmpty($directory)
- {
- $handle = opendir($directory);
- while (($file = readdir($handle)) !== false)
- {
- if ($file != "." && $file != "..")
- {
- closedir($handle);
- return false;
- }
- }
- closedir($handle);
- return true;
- }
- function get_auto_increment($type='count',$cateid){
- $dao=D('Products');
- if($type=='count'){
- $k=$dao->where("cateid=$cateid")->count();
- if($k){
- $k++;
- }else{
- $k=1;
- }
- return $k;
- }else{
- $k = $dao->query("show table status where name='__TABLE__'");
- return $k[0]['Auto_increment'];
- }
- }
- /**
- * ????????
- *
- * @param ??? $directory
- * @param ??????? $subdir
- */
- function delDir($directory,$subdir=true)
- {
- if (is_dir($directory) == false)
- {
- //exit("The Directory Is Not Exist!");
- return false;
- }
- $handle = opendir($directory);
- while (($file = readdir($handle)) !== false)
- {
- if ($file != "." && $file != "..")
- {
- is_dir("$directory/$file")?
- $this->delDir("$directory/$file"):
- unlink("$directory/$file");
- }
- }
- if (readdir($handle) == false)
- {
- closedir($handle);
- rmdir("$directory");
- }
- }
- //??????
- function GetCateName($id){
- $Cate=D("Cate");
- $map['id']=$id;
- $name=$Cate->where($map)->getField('name');
- return $name;
- }
- //????ID
- function GetCateId($name){
- $Cate=D("Cate");
- $map['name']=$name;
- $id=$Cate->where($map)->getField('id');
- return $id;
- }
- function showjsmessage($message) {
- echo $message."<br/>";
- flush();
- ob_flush();
- }
- }
- ?>