예제 #1
0
 /**
  * 推送
  * 
  * 
  */
 public function pushPrecent()
 {
     set_time_limit(0);
     if (Input::has("pushgrad") == 1) {
         echo '<script type="text/javascript">function refresh(str){parent.refresh(str);};</script>';
         if (Input::has("push_c_id")) {
             $pushcid = Input::get("push_c_id");
         }
         if (Input::has("end")) {
             $end = Input::get("end");
         }
         //            $pushcid = $this->pushcid;
         //            $end = $this->end;
         //            $this->percent=0;
         //            $this->lastpercent=0;
         //            $this->html_precent=0;
         //            $this->last_html_precent=0;
         //            $this->pcpush=0;
         //            $this->mobilepush=0;
         //            $this->quickbarpush=0;
         //            $this->mobilehomepagepush=0;
     } else {
         if (!Input::has("name")) {
             echo '<script type="text/javascript">function refresh(str){parent.refresh(str);};</script>';
         }
         if (Input::has("push_c_id")) {
             $pushcid = Input::get("push_c_id");
         }
         if (Input::has("end")) {
             $end = Input::get("end");
         }
     }
     $have_article = Articles::where('cus_id', $this->cus_id)->count();
     if (!$have_article) {
         echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
         echo '<div class="prompt">没有文章不可推送</div><script type="text/javascript">alert("没有文章不可推送");refresh("没有文章不可推送");</script>';
         ob_flush();
         flush();
         exit;
     }
     if (!isset($_GET['gradpush'])) {
         $this->needpush();
     } else {
         $pc_domain = CustomerInfo::where('cus_id', $this->cus_id)->pluck('pc_domain');
         $mobile_domain = CustomerInfo::where('cus_id', $this->cus_id)->pluck('mobile_domain');
         $pc = str_replace('http://', '', $pc_domain);
         $mobile = str_replace('http://', '', $mobile_domain);
         if ($pc != '') {
             $this->pcpush = 1;
         }
         if ($mobile != '') {
             $this->mobilepush = 1;
         }
         $this->quickbarpush = 1;
         $this->mobilehomepagepush = 0;
     }
     echo '<div class="prompt">';
     var_dump('pcpush:' . $this->pcpush);
     var_dump('mobilepush:' . $this->mobilepush);
     var_dump('quickbarpush:' . $this->quickbarpush);
     var_dump('mobilehomepagepush:' . $this->mobilehomepagepush);
     echo '</div>';
     ob_flush();
     flush();
     $this->pushinit();
     if (!isset($end) || $end == 1) {
         if ($this->quickbarpush) {
             $this->pushQuickbar();
         }
         if (!$this->mobilepush) {
             if ($this->mobilehomepagepush) {
                 $this->mobilehomepage_push();
             }
         }
     }
     if ($this->pcpush || $this->mobilepush) {
         if (!$this->pcpush && $this->mobilepush) {
             $this->mobile_push();
             return true;
         }
         if (ob_get_level() == 0) {
             ob_start();
         }
         $pc_classify_ids = array();
         $mobile_classify_ids = array();
         $pc_article_ids = array();
         $mobile_article_ids = array();
         if ($this->mobilepush) {
             if (isset($pushcid)) {
                 if (!isset($end) || $end == 1) {
                     $mindexhtml = $this->homgepagehtml('mobile');
                     $msearchhtml = $this->sendData('mobile');
                 }
                 $mobile_classify_ids = array();
                 $mobile_article_ids = array();
                 $mobile_show = Classify::where('cus_id', $this->cus_id)->where("id", $pushcid)->pluck("mobile_show");
                 if ($mobile_show) {
                     $mobile_classify_ids[] = $pushcid;
                     $mobile_article_ids = Articles::where('cus_id', $this->cus_id)->where('mobile_show', 1)->where("c_id", $pushcid)->lists('id');
                 }
             } else {
                 $mindexhtml = $this->homgepagehtml('mobile');
                 $msearchhtml = $this->sendData('mobile');
                 $mobile_classify_ids = Classify::where('cus_id', $this->cus_id)->where('mobile_show', 1)->lists('id');
                 $mobile_article_ids = Articles::where('cus_id', $this->cus_id)->where('mobile_show', 1)->lists('id');
             }
         }
         if ($this->pcpush) {
             if (isset($pushcid)) {
                 if (!isset($end) || $end == 1) {
                     $indexhtml = $this->homgepagehtml('pc');
                     $searchhtml = $this->sendData('pc');
                 }
                 $pc_classify_ids = array();
                 $pc_article_ids = array();
                 $pc_show = Classify::where('cus_id', $this->cus_id)->where("id", $pushcid)->pluck("pc_show");
                 if ($pc_show) {
                     $pc_classify_ids[] = $pushcid;
                     $pc_article_ids = Articles::where('cus_id', $this->cus_id)->where('pc_show', 1)->where("c_id", $pushcid)->lists('id');
                 }
             } else {
                 $indexhtml = $this->homgepagehtml('pc');
                 $searchhtml = $this->sendData('pc');
                 $pc_classify_ids = Classify::where('cus_id', $this->cus_id)->where('pc_show', 1)->lists('id');
                 $pc_article_ids = Articles::where('cus_id', $this->cus_id)->where('pc_show', 1)->lists('id');
             }
         }
         $count = $this->htmlPagecount($pc_classify_ids, $mobile_classify_ids, $pc_article_ids, $mobile_article_ids);
         $this->html_precent = 70 / $count;
         if ($this->pcpush) {
             $categoryhtml = $this->categoryhtml($pc_classify_ids, 'pc');
             $articlehtml = $this->articlehtml($pc_classify_ids, 'pc');
         }
         if ($this->mobilepush) {
             $mcategoryhtml = $this->categoryhtml($mobile_classify_ids, 'mobile');
             $marticlehtml = $this->articlehtml($mobile_classify_ids, 'mobile');
         }
         $this->percent = 20 / $count;
         $path = public_path('customers/' . $this->customer . '/' . $this->customer . '.zip');
         if (file_exists($path)) {
             @unlink($path);
         }
         $zip = new ZipArchive();
         if ((!isset($end) || $end == 1) && $zip->open($path, ZipArchive::CREATE) === TRUE) {
             if ($this->pcpush) {
                 $this->addFileToZip(public_path("quickbar/"), $zip, "quickbar");
                 $zip->addFile($indexhtml, 'index.html');
                 $zip->addFile($searchhtml, 'search.html');
                 $zip->addFile(public_path('customers/' . $this->customer . '/article_data.json'), 'article_data.json');
                 $nowpercent = $this->percent + $this->lastpercent;
                 if (floor($nowpercent) != $this->lastpercent) {
                     echo '<div class="prompt">' . floor($nowpercent) . '%</div><script type="text/javascript">refresh(' . floor($nowpercent) . ');</script>';
                     ob_flush();
                     flush();
                     $this->clearpushqueue();
                 }
             }
             $this->lastpercent += 70 + $this->percent;
             if ($this->mobilepush) {
                 $this->addFileToZip(public_path("quickbar/"), $zip, "mobile/quickbar");
                 $zip->addFile($mindexhtml, 'mobile/index.html');
                 $zip->addFile($msearchhtml, 'mobile/search.html');
                 $zip->addFile(public_path('customers/' . $this->customer . '/mobile/article_data.json'), 'mobile/article_data.json');
                 $nowpercent = $this->percent + $this->lastpercent;
                 if (floor($nowpercent) != floor($this->lastpercent)) {
                     echo '<div class="prompt">' . floor($nowpercent) . '%</div><script type="text/javascript">refresh(' . floor($nowpercent) . ');</script>';
                     ob_flush();
                     flush();
                     $this->clearpushqueue();
                 }
             }
             $this->lastpercent += $this->percent;
             $zip->close();
         } else {
             $this->lastpercent += 70 + $this->percent;
         }
         if ($this->pcpush) {
             $this->compareZip($categoryhtml, 'category', $path);
             $this->compareZip($articlehtml, 'detail', $path);
         }
         if ($this->mobilepush) {
             $this->compareZip($mcategoryhtml, 'mobile/category', $path);
             $this->compareZip($marticlehtml, 'mobile/detail', $path);
         }
         if (90 > floor($this->lastpercent)) {
             echo '<div class="prompt">' . '90%</div><script type="text/javascript">refresh(90);</script>';
             ob_flush();
             flush();
             $this->clearpushqueue();
         }
         PushQueue::where('cus_id', $this->cus_id)->delete();
         $nextpush = PushQueue::where('push', 0)->first();
         if ($nextpush) {
             PushQueue::where('id', $nextpush->id)->update(['push' => 1]);
         }
         if ($zip->open($path, ZipArchive::CREATE) === TRUE) {
             if ((!isset($end) || $end == 1) && $this->pcpush) {
                 $pc_dir = Template::where('website_info.cus_id', $this->cus_id)->Leftjoin('website_info', 'website_info.pc_tpl_id', '=', 'template.id')->pluck('name');
                 $aim_dir = public_path("templates/{$pc_dir}/");
                 $this->addDir($aim_dir, $zip);
             }
             if ((!isset($end) || $end == 1) && $this->mobilepush) {
                 $mobile_dir = Template::where('website_info.cus_id', $this->cus_id)->Leftjoin('website_info', 'template.id', '=', 'website_info.mobile_tpl_id')->pluck('name');
                 $maim_dir = public_path("templates/{$mobile_dir}/");
                 $this->addDir($maim_dir, $zip, 'mobile/');
             }
             $zip->close();
             $customerinfo = Customer::find($this->cus_id);
             $ftp_array = explode(':', $customerinfo->ftp_address);
             $port = $customerinfo->ftp_port;
             $ftpdir = $customerinfo->ftp_dir;
             $ftp = $customerinfo->ftp;
             $ftp_array[1] = isset($ftp_array[1]) ? $ftp_array[1] : $port;
             $conn = ftp_connect($ftp_array[0], $ftp_array[1]);
             $del_imgs = ImgDel::where('cus_id', $this->cus_id)->get()->toArray();
             if (trim($ftp) == '1') {
                 if ($conn) {
                     ftp_login($conn, $customerinfo->ftp_user, $customerinfo->ftp_pwd);
                     ftp_pasv($conn, 1);
                     if (@ftp_chdir($conn, $this->customer) == FALSE) {
                         ftp_mkdir($conn, $this->customer);
                     }
                     foreach ((array) $del_imgs as $v) {
                         $this->delimg($v);
                         @ftp_delete($conn, "/" . $this->customer . '/images/l/' . $v['target'] . '/' . $v['img']);
                         @ftp_delete($conn, "/" . $this->customer . '/images/s/' . $v['target'] . '/' . $v['img']);
                         @ftp_delete($conn, "/" . $this->customer . '/mobile/images/l/' . $v['target'] . '/' . $v['img']);
                         @ftp_delete($conn, "/" . $this->customer . '/mobile/images/s/' . $v['target'] . '/' . $v['img']);
                     }
                     ImgDel::where('cus_id', $this->cus_id)->delete();
                     if ($this->pcpush) {
                         @ftp_put($conn, "/" . $this->customer . "/search.php", public_path("packages/search.php"), FTP_ASCII);
                         //@ftp_put($conn,"/".$this->customer."/quickbar.json",public_path('customers/'.$this->customer.'/quickbar.json'),FTP_ASCII);
                     }
                     if (file_exists($path)) {
                         ftp_put($conn, "/" . $this->customer . "/site.zip", $path, FTP_BINARY);
                     }
                     ftp_put($conn, "/" . $this->customer . "/unzip.php", public_path("packages/unzip.php"), FTP_ASCII);
                     if ($this->mobilepush) {
                         ftp_put($conn, "/" . $this->customer . "/mobile/search.php", public_path("packages/search.php"), FTP_ASCII);
                         if (@ftp_chdir($conn, "/" . $this->customer . "/mobile") == FALSE) {
                             ftp_mkdir($conn, "/" . $this->customer . "/mobile");
                         }
                         //ftp_put($conn,"/".$this->customer."/mobile/quickbar.json",public_path('customers/'.$this->customer.'/mobile/quickbar.json'),FTP_ASCII);
                     }
                     ftp_close($conn);
                 }
             } else {
                 if ($conn) {
                     ftp_login($conn, $customerinfo->ftp_user, $customerinfo->ftp_pwd);
                     ftp_pasv($conn, 1);
                     foreach ((array) $del_imgs as $v) {
                         $this->delimg($v);
                         @ftp_delete($conn, $ftpdir . '/images/l/' . $v['target'] . '/' . $v['img']);
                         @ftp_delete($conn, $ftpdir . '/images/s/' . $v['target'] . '/' . $v['img']);
                         @ftp_delete($conn, $ftpdir . '/mobile/images/l/' . $v['target'] . '/' . $v['img']);
                         @ftp_delete($conn, $ftpdir . '/mobile/images/s/' . $v['target'] . '/' . $v['img']);
                     }
                     ImgDel::where('cus_id', $this->cus_id)->delete();
                     if ($this->pcpush) {
                         ftp_put($conn, $ftpdir . "/search.php", public_path("packages/search.php"), FTP_ASCII);
                         //ftp_put($conn,$ftpdir."/quickbar.json",public_path('customers/'.$this->customer.'/quickbar.json'),FTP_ASCII);
                     }
                     ftp_put($conn, $ftpdir . "/unzip.php", public_path("packages/unzip.php"), FTP_ASCII);
                     if (file_exists($path)) {
                         ftp_put($conn, $ftpdir . "/site.zip", $path, FTP_BINARY);
                     }
                     if ($this->mobilepush) {
                         ftp_put($conn, $ftpdir . "/mobile/search.php", public_path("packages/search.php"), FTP_ASCII);
                         //ftp_put($conn,$ftpdir."/mobile/quickbar.json",public_path('customers/'.$this->customer.'/mobile/quickbar.json'),FTP_ASCII);
                     }
                     ftp_close($conn);
                 }
             }
             $this->folderClear();
             echo '<div class="prompt">' . '100%</div><script type="text/javascript">refresh(100);</script>';
             if (!isset($end) || $end == 1) {
                 Classify::where('cus_id', $this->cus_id)->where('pushed', '>', 0)->update(['pushed' => 0]);
                 Articles::where('cus_id', $this->cus_id)->where('pushed', '>', 0)->update(['pushed' => 0]);
                 WebsiteConfig::where('cus_id', $this->cus_id)->update(['pushed' => 0]);
                 WebsiteInfo::where('cus_id', $this->cus_id)->update(['pushed' => 0]);
                 MobileHomepage::where('cus_id', $this->cus_id)->update(['pushed' => 0]);
                 CustomerInfo::where('cus_id', $this->cus_id)->update(['pushed' => 0, 'lastpushtime' => date('Y-m-d H:i:s', time())]);
                 //date('Y-m-d H:i:s',time())
             }
             /**
              * pc使用本服务器自带域名推送,后期需要改进!
              */
             $weburl = Customer::where('id', $this->cus_id)->pluck('weburl');
             $suf_url = str_replace('http://c', '', $weburl);
             $cus_name = strtolower(Customer::where('id', $this->cus_id)->pluck('name'));
             if (trim($ftp) == '1') {
                 $ftp_pcdomain = "http://" . $cus_name . $suf_url;
             } else {
                 $ftp_pcdomain = $customerinfo->pc_domain;
             }
             @file_get_contents("{$ftp_pcdomain}/unzip.php");
         } else {
             echo '打包失败';
         }
         ob_end_flush();
     } else {
         echo '<div class="prompt">' . '100%</div><script type="text/javascript">refresh(100);</script>';
         if (!isset($end) || $end == 1) {
             Classify::where('cus_id', $this->cus_id)->where('pushed', '>', 0)->update(['pushed' => 0]);
             Articles::where('cus_id', $this->cus_id)->where('pushed', '>', 0)->update(['pushed' => 0]);
             WebsiteConfig::where('cus_id', $this->cus_id)->update(['pushed' => 0]);
             WebsiteInfo::where('cus_id', $this->cus_id)->update(['pushed' => 0]);
             MobileHomepage::where('cus_id', $this->cus_id)->update(['pushed' => 0]);
             CustomerInfo::where('cus_id', $this->cus_id)->update(['pushed' => 0, 'lastpushtime' => date('Y-m-d H:i:s', time())]);
             //date('Y-m-d H:i:s',time())
             ob_end_flush();
         }
     }
 }