| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Util.php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Common Utility functions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * This program is free software: you can redistribute it and/or modify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * it under the terms of the GNU General Public License as published by | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * the Free Software Foundation, either version 3 of the License, or | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * (at your option) any later version. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * This program is distributed in the hope that it will be useful, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * GNU General Public License for more details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * You should have received a copy of the GNU General Public License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @package    LibreNMS | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @link       http://librenms.org | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @copyright  2016 Tony Murray | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * @author     Tony Murray <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | namespace App; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | class Util | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * Merge arrays, concatenate the values of any common keys | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param array ... | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     public static function arrayMergeConcat() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $out = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         // loop through the arguments | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         foreach (func_get_args() as $arr) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             // loop through each array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             foreach ($arr as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                 // If the same key exists in the $out array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                 if (array_key_exists($key, $out)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                     // concat the values | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                     $out[$key] = $out[$key].$arr[$key]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                     $out[$key] = $arr[$key]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         return $out; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * Checks if the given variable is a json encoded string. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @param string $string The string to check | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @return bool true if this is json, otherwise false | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 60 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     public static function isJson($string) | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         if (!is_string($string)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |             return false; | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         json_decode($string, true); | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |         return (json_last_error() == JSON_ERROR_NONE); | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @param int $seconds | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     public static function formatUptime($seconds) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         if (empty($seconds)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $seconds = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $from = new \DateTime("@0"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $to = new \DateTime("@$seconds"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         return $from->diff($to)->format('%a d, %h h, %i m and %s s'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 84 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |  |