| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of EC-CUBE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * http://www.ec-cube.co.jp/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | namespace Eccube\Controller\Admin\Setting\System; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Eccube\Controller\AbstractController; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Eccube\Event\EccubeEvents; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Eccube\Event\EventArgs; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Eccube\Form\Type\Admin\LogType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Symfony\Component\Routing\Annotation\Route; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Symfony\Component\HttpFoundation\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Symfony\Component\HttpFoundation\StreamedResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | class LogController extends AbstractController | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @Route("/%eccube_admin_route%/setting/system/log", name="admin_setting_system_log") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @Template("@admin/Setting/System/log.twig") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @return array|Symfony\Component\HttpFoundation\StreamedResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 9 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public function index(Request $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 9 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $formData = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 9 |  |         // default | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 9 |  |         $formData['files'] = 'site_'.date('Y-m-d').'.log'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $formData['line_max'] = '50'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 9 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 9 |  |         $builder = $this->formFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             ->createBuilder(LogType::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 9 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $event = new EventArgs( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 9 |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 9 |  |                 'builder' => $builder, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 'data' => $formData, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 9 |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 9 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 9 |  |         $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SYSTEM_LOG_INDEX_INITIALIZE, $event); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $formData = $event->getArgument('data'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 9 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $form = $builder->getForm(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 9 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 8 |  |         if ('POST' === $request->getMethod()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 8 |  |             $form->handleRequest($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             if ($form->isValid()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                 $formData = $form->getData(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 8 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             $event = new EventArgs( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 8 |  |                 [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                     'form' => $form, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 8 |  |                 ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 8 |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SYSTEM_LOG_INDEX_COMPLETE, $event); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 9 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 9 |  |         $logDir = $this->getParameter('kernel.logs_dir').DIRECTORY_SEPARATOR.$this->getParameter('kernel.environment'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $logFile = $logDir.'/'.$formData['files']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 9 |  |         if ($form->getClickedButton() && $form->getClickedButton()->getName() === 'download') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 9 |  |             $bufferSize = 1024 * 50; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             $response = new StreamedResponse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $response->headers->set('Content-Length',filesize($logFile)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             $response->headers->set('Content-Disposition','attachment; filename=' . basename($logFile)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             $response->headers->set('Content-Type','application/octet-stream'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             $response->setCallback(function() use($logFile,$bufferSize) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 if ($fh = fopen($logFile,'r')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                     while (!feof($fh)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                         echo fread($fh,$bufferSize); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 9 |  |             $response->send(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 9 |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 9 |  |                 'form' => $form->createView(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 9 |  |                 'log' => $this->parseLogFile($logFile, $formData), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * parse log file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @param string $logFile | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @param $formData | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * @return array | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 101 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |     private function parseLogFile($logFile, $formData) | 
            
                                                        
            
                                    
            
            
                | 103 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |         $log = []; | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |         if (!file_exists($logFile)) { | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |             return $log; | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 110 |  |  |         foreach (array_reverse(file($logFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)) as $line) { | 
            
                                                        
            
                                    
            
            
                | 111 |  |  |             // 上限に達した場合、処理を抜ける | 
            
                                                        
            
                                    
            
            
                | 112 |  |  |             if (count($log) >= $formData['line_max']) { | 
            
                                                        
            
                                    
            
            
                | 113 |  |  |                 break; | 
            
                                                        
            
                                    
            
            
                | 114 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 116 |  |  |             $log[] = $line; | 
            
                                                        
            
                                    
            
            
                | 117 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 119 |  |  |         return $log; | 
            
                                                        
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 121 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 122 |  |  |  |