| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SilverLeague\LogViewer\Model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use SilverStripe\Core\Convert; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SilverStripe\Forms\LiteralField; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SilverStripe\ORM\DataObject; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * A LogEntry is a set of data provided from Monolog via the DataObjectHandler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @package silverstripe-logviewer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @author  Robbie Averill <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class LogEntry extends DataObject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     private static $table_name = 'LogEntry'; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @var bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private static $format_entry = true; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     private static $db = [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         'Entry' => 'Text', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         'Level' => 'Varchar' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     private static $summary_fields = [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         'GridfieldSummary' => 'Entry', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         'Created'          => 'Created', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         'Level'            => 'Level' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * We should never need to edit log entries | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     public function canEdit($member = false, $context = []) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @return \SilverStripe\Forms\FieldList | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     public function getCMSFields() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $config = self::config()->get('format_entry'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $fields = parent::getCMSFields(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         if ($config) { // Default setting, format the entry | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             $fields->removeByName(array('Entry')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             $text = $this->Entry; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             if (!Convert::json2array($text)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 /** @deprecated We're falling back to the legacy text-only error */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 $entry = $this->formatLegacyEntry($text); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 $asArray = Convert::json2array($text); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                 $entry = $this->entryToUl($asArray, true); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             $entry = nl2br("<pre>" . $entry . "</pre>"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             $fields->addFieldToTab('Root.Main', LiteralField::create('FormattedEntry', $entry)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } else { // Just move the field after the Level | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             $entryField = $fields->dataFieldByName('Entry'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             $fields->insertAfter('Level', $entryField); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         return $fields; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * Recursive method, will call itself if a sub element is an array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @param array $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @param bool $first start with an opening bracket? if true, skip (counterintuitive, but naming) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @return string formatted <ul><li> of the array; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 87 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |     private function entryToUl($data, $first = false) | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |         $out = $first ? '' : '['; | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |         $out .= '<ul style="margin-bottom: 0">'; | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |         foreach ($data as $key => $arrayItem) { | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |             if (!is_array($arrayItem)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |                 $out .= '<li  class="list-unstyled"><span>' . $key . ' => ' . $arrayItem . '</span></li>'; | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |             } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |                 $out .= '<li class="list-unstyled"><span>' . $key . ' => </span>' . $this->entryToUl($arrayItem) . '</li>'; | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |         $out .= '</ul>'; | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |         $out .= $first ? '' : ']'; | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |         return $out; | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @param $text | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @deprecated This method exists to make sure non-JSONFormatted messages won't break the flow | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     protected function formatLegacyEntry($text) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $entry = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         preg_match_all("/(\{\".*?}+)\s/", $text, $matches); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         if (count($matches[1])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             $entry .= "<h2>Entry</h2>"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             foreach ($matches[1] as $key => $match) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                 $pretext = substr($text, 0, strpos($text, $match)); // Header of the error | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                 $asArray = Convert::json2array($match); // Convert the rest to array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                 $text = substr($text, strlen($pretext . $match)); // Prepare for the next entry | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                 $entry .= ($key > 0 ? "\n" : '') . trim($pretext) . ' => '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                 $entry .= $this->entryToUl($asArray); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             if (json_decode(trim($text)) !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                 $text = Convert::json2array($text); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                 $entry .= "\nOther => " . $this->entryToUl($text); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             } elseif (strlen(trim($text))) { // add the leftover if there is any | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                 $entry .= "\nOther:\n" . $text; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         return $entry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * @param int $length Length of the summary | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * @return string shortened string of the entry if too long. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     public function getGridfieldSummary($length = 300) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         $elipsis = strlen($this->Entry) > $length ? '...' : ''; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         return substr($this->Entry, 0, $length) . $elipsis; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 144 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 145 |  |  |  |