PageRenderTime 29ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/sop/2.0beta1/Lib/Action/Admin/FtpbatchAction.class.php

http://iiccms.googlecode.com/
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
  1. <?php
  2. /**
  3. * @author nanze
  4. * @link
  5. * @todo FTP??
  6. * @copyright 811046@qq.com
  7. * @version 1.0
  8. * @lastupdate 2010-11-18
  9. */
  10. class FtpbatchAction extends AdminCommAction {
  11. public $products_image_path;
  12. public $products_temp_path;
  13. function _initialize()
  14. {
  15. parent::_initialize();
  16. import("ORG.Io.Dir");
  17. import("ORG.Util.Image");
  18. set_time_limit(0);
  19. $this->products_image_path="./Public/Uploads/Products";
  20. $this->products_temp_path="./Public/Uploads/Temp";
  21. header("Content-Type:text/html; charset=utf-8");
  22. }
  23. public function index() {
  24. if($this->isPost() && $_POST['type_id']){
  25. self::$Model=D("Type_attr");
  26. $attr=self::$Model->where(array('type_id'=>$_POST['type_id'],'status'=>1))->order("sort desc")->findall();
  27. foreach ($attr as $k=>$v){
  28. $attr[$k]['values']=explode(chr(13),$v['values']);
  29. foreach ($attr[$k]['values'] as $kk=>$vv){
  30. $attr[$k]['values'][$kk]=str_replace("\n","",$vv);
  31. //??????
  32. $attr[$k]['attrs'][$kk]['attr_id']=$v['id'];
  33. $attr[$k]['attrs'][$kk]['attr_value']=$attr[$k]['values'][$kk];
  34. }
  35. }
  36. $this->attr=$attr;
  37. die($this->fetch('Ftpbatch-getattr'));
  38. }
  39. /**
  40. * ????
  41. */
  42. $realpath="./".auto_charset($_POST['f'],'utf-8','gbk');
  43. if(!empty( $_POST['f']) && substr_count(realpath($realpath),realpath($this->products_temp_path))>0){
  44. $dir = $_REQUEST['f']; //????
  45. }else {
  46. $dir = $this->products_temp_path; //???
  47. }
  48. $fileName = array();
  49. /**
  50. * ????
  51. *
  52. $file=auto_charset($file,'gbk','utf-8');
  53. */
  54. $dir=auto_charset($dir,'utf-8','gbk');
  55. if (is_dir( $dir)) {
  56. if ($dh = opendir( $dir)) {
  57. while (($file = readdir($dh)) !== false) {
  58. $file=auto_charset($file,'gbk','utf-8');
  59. if($file != "." && $file !=".."){
  60. if(is_dir(auto_charset("$dir/$file",'utf-8','gbk'))){
  61. $enterPath=substr( "$dir/$file",2); //??..
  62. $fileName[$i]['href'] = "<a href=\"javascript:e('$enterPath');\" >$file</a>";
  63. }else{
  64. $fileName[$i]['href'] = $file;
  65. }
  66. $fileName[$i]['file'] = $file;
  67. $i++;
  68. }
  69. }
  70. closedir($dh);
  71. }
  72. }
  73. $dir=auto_charset($dir,'gbk','utf-8');
  74. $this->assign('path',str_replace("./","", $dir)); //????
  75. $this->assign('dir',str_replace("./","", $dir)); //????????
  76. $this->assign('fileName', $fileName); //??????
  77. $this->assign('uplevel', realpath($dir)==realpath($this->products_temp_path)?$this->products_temp_path:dirname($dir)); //?????
  78. $this->display();
  79. return;
  80. }
  81. /**
  82. * ???????????
  83. *
  84. * @param string $directory ?????
  85. */
  86. function deletefile($directory){
  87. $directory = $_POST['checkbox']; //????
  88. $path = "./".$_POST['path'];
  89. for($i=0;$i<count($directory);$i++){
  90. $Temp_path="$path/${directory[$i]}"; //????
  91. $Temp_path=auto_charset($Temp_path,'utf-8','gbk');
  92. if (is_dir($Temp_path) == true) //??????
  93. {
  94. $this->delDir($Temp_path);
  95. }else{
  96. unlink($Temp_path); //????
  97. }
  98. }
  99. closedir($handle);
  100. $this->success('????');
  101. }
  102. /**
  103. * ????
  104. *
  105. */
  106. function moreuploadfile(){
  107. $this->assign("jumpUrl",$_SERVER["HTTP_REFERER"]);
  108. echo "<style type=\"text/css\">* {font-size:11px}</style>";
  109. $filename = $_POST['checkbox']; //??????
  110. $path = "./".$_POST['path']; //?????..
  111. for($i=0;$i<count($filename);$i++){
  112. $filename[$i]=auto_charset($filename[$i],'utf-8','gbk');
  113. if(file_exists($path.'/'.$filename[$i]) ==true){
  114. $filename[$i]=auto_charset($filename[$i],'gbk','utf-8');
  115. if(is_dir("$path/{$filename[$i]}")){
  116. $this->showjsmessage("???????$path/{$filename[$i]}");
  117. }
  118. if(isset($_POST['importClass']) && $_POST['importClass']==1){
  119. $this->processdir($path.'/'.$filename[$i],$_POST['cateid']);
  120. }else{
  121. $this->processdir($path.'/'.$filename[$i]);
  122. }
  123. }
  124. }
  125. //?????
  126. foreach ($filename as $v){
  127. $v=auto_charset($v,'utf-8','gbk');
  128. if(file_exists($path.'/'.$v) ==true){
  129. if(is_dir($path."/".$v)){
  130. $this->delDir($path."/".$v); //?????????????
  131. $this->showjsmessage("???????$path/".auto_charset($v,'gbk','utf-8'));
  132. }else{
  133. unlink($path."/".$v); //????
  134. $this->showjsmessage("??????$path/".auto_charset($v,'gbk','utf-8'));
  135. }
  136. }
  137. }
  138. $this->showjsmessage("??????!");
  139. }
  140. /**
  141. * ?????
  142. *
  143. * @param ??????? $dir
  144. * @param ??? $pid
  145. */
  146. function processdir($dir,$pid=0){
  147. $cate_model=D('Cate');
  148. $pro_model=D('Products');
  149. $dir=auto_charset($dir,'utf-8','gbk');
  150. if (is_dir($dir)) {
  151. $data['name']=auto_charset(basename($dir),'gbk','utf-8');
  152. $id=$this->GetCateId($data['name']);
  153. if(!$id){
  154. $data['pid']=$pid;
  155. $id=$cate_model->add($data);
  156. }
  157. if(isset($_POST['type_id']) && !empty($_POST['type_id'])){
  158. $cate_model->where(array('id'=>$id))->data(array('type_id'=>$_POST['type_id']))->save();
  159. }
  160. if ($dh = opendir($dir)) {
  161. while (($file = readdir($dh)) !== false) {
  162. if($file != "." && $file !=".."){
  163. if(is_dir("$dir/$file")){
  164. $dir=auto_charset($dir,'gbk','utf-8');
  165. $this->processdir($dir.'/'.$file,$id);
  166. }else{
  167. $this->processfile($dir,$file,$id,$pid);
  168. }
  169. }
  170. }
  171. }
  172. }else{
  173. //??????
  174. $file=$dir;
  175. $dir='';
  176. $cateid=$_POST['cateid']?$_POST['cateid']:$pid;
  177. $this->processfile($dir,$file,$cateid,$pid);
  178. }
  179. }
  180. function processfile($dir,$file,$cateid,$pid=0){
  181. $cate_model=D('Cate');
  182. $pro_model=D('Products');
  183. $extend = pathinfo($file); //???
  184. $extend = $extend["extension"];
  185. $pro=$pro_model->create();
  186. //????
  187. if($_POST['autoReName']==1){
  188. $k=$this->get_auto_increment('count',$cateid);//??????????
  189. $autoname=$this->GetCateName($cateid)."-".$k;
  190. $pro['name']=$autoname;
  191. }else{
  192. $pro['name']=str_replace(".$extend",'',auto_charset(basename($file),'gbk','utf-8'));
  193. }
  194. if(!$cateid){
  195. $pro['cateid']=$pid;
  196. }else{
  197. $pro['cateid']=$cateid;
  198. }
  199. $pro['bigimage']=$this->products_image_path .'/'.toDate ( time (), 'Ymd' ) . "/".$pro['name'].'.'.$extend;
  200. $new_path=dirname($pro['bigimage']).'/';
  201. if(!file_exists($new_path)){
  202. mkdir($new_path);
  203. }
  204. $new_name=$new_path.$pro['name'].".$extend";
  205. //????
  206. $old_name=$dir."/".$file;
  207. $new_name=auto_charset($new_name,'utf-8','gbk');
  208. rename($old_name,$new_name);
  209. //??,???,??,?,?,????
  210. $thumbname = Image::thumb($new_name,dirname($new_name).'/thumb_'.basename($new_name),'',GetSettValue('ImgThumbW'),GetSettValue('ImgThumbH'),true,'');
  211. $pthumbname=pathinfo($thumbname);
  212. $pro['smallimage']=$new_path.auto_charset($pthumbname['filename'],'gbk','utf-8').".".$pthumbname['extension'];
  213. $pro['dateline']=time();
  214. $id=$pro_model->add($pro);
  215. if($pro['cateid']){
  216. $this->batch_attr($id);//??
  217. }
  218. //?????
  219. $model = D( "Products_gallery" );
  220. $data=array();
  221. $data ['pid'] = $id;
  222. $data ['img_url'] = $pro['bigimage'];
  223. $data ['thumb_url'] = $pro['smallimage'];
  224. $model->add ( $data );
  225. //??
  226. $this->showjsmessage("????".auto_charset($file,'gbk','utf-8'));
  227. }
  228. /**
  229. * ??????
  230. */
  231. function batch_attr($id){
  232. self::$Model=D("Products_attr");
  233. foreach ($_POST ['attr_id'] as $key => $attr_id ) {
  234. foreach ( $_POST ['attr_value_' . $attr_id] as $key => $attr_value ) {
  235. if (!empty($attr_value))
  236. {
  237. //???????
  238. $data ['products_id'] = $id;
  239. $data ['attr_id'] = $attr_id;
  240. $data ['attr_value'] = str_replace ( "\n", "", $attr_value );
  241. if (self::$Model->create ( $data )) {
  242. self::$Model->add ( $data );
  243. } else {
  244. $this->error ( self::$Model->geterror () );
  245. }
  246. }
  247. }
  248. }
  249. }
  250. /**
  251. * ?????????
  252. *
  253. * @param string $directory
  254. * @return boolean
  255. */
  256. function isEmpty($directory)
  257. {
  258. $handle = opendir($directory);
  259. while (($file = readdir($handle)) !== false)
  260. {
  261. if ($file != "." && $file != "..")
  262. {
  263. closedir($handle);
  264. return false;
  265. }
  266. }
  267. closedir($handle);
  268. return true;
  269. }
  270. function get_auto_increment($type='count',$cateid){
  271. $dao=D('Products');
  272. if($type=='count'){
  273. $k=$dao->where("cateid=$cateid")->count();
  274. if($k){
  275. $k++;
  276. }else{
  277. $k=1;
  278. }
  279. return $k;
  280. }else{
  281. $k = $dao->query("show table status where name='__TABLE__'");
  282. return $k[0]['Auto_increment'];
  283. }
  284. }
  285. /**
  286. * ????????
  287. *
  288. * @param ??? $directory
  289. * @param ??????? $subdir
  290. */
  291. function delDir($directory,$subdir=true)
  292. {
  293. if (is_dir($directory) == false)
  294. {
  295. //exit("The Directory Is Not Exist!");
  296. return false;
  297. }
  298. $handle = opendir($directory);
  299. while (($file = readdir($handle)) !== false)
  300. {
  301. if ($file != "." && $file != "..")
  302. {
  303. is_dir("$directory/$file")?
  304. $this->delDir("$directory/$file"):
  305. unlink("$directory/$file");
  306. }
  307. }
  308. if (readdir($handle) == false)
  309. {
  310. closedir($handle);
  311. rmdir("$directory");
  312. }
  313. }
  314. //??????
  315. function GetCateName($id){
  316. $Cate=D("Cate");
  317. $map['id']=$id;
  318. $name=$Cate->where($map)->getField('name');
  319. return $name;
  320. }
  321. //????ID
  322. function GetCateId($name){
  323. $Cate=D("Cate");
  324. $map['name']=$name;
  325. $id=$Cate->where($map)->getField('id');
  326. return $id;
  327. }
  328. function showjsmessage($message) {
  329. echo $message."<br/>";
  330. flush();
  331. ob_flush();
  332. }
  333. }
  334. ?>