| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * class Settings | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Handles editing and fetching jilo configuration. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class Settings { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2025-01-29 08:46:06 +00:00
										 |  |  |      * Loads javascript file the Jitsi server. | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @param string $jitsiUrl The base URL of the Jitsi server. | 
					
						
							| 
									
										
										
										
											2025-01-29 08:46:06 +00:00
										 |  |  |      * @param string $livejsFile The name of the remote js file to load. | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  |      * @param bool $raw Whether to return the full file (true) or only uncommented values (false). | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2025-01-29 08:46:06 +00:00
										 |  |  |      * @return string The content of the interface_config.js file or an error message. | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2025-01-29 08:46:06 +00:00
										 |  |  |     public function getPlatformJsFile($jitsiUrl, $livejsFile, $raw = false) { | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  |         // constructing the URL
 | 
					
						
							| 
									
										
										
										
											2025-01-29 08:46:06 +00:00
										 |  |  |         $jsFile = $jitsiUrl . '/' . $livejsFile; | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // default content, if we can't get the file contents
 | 
					
						
							| 
									
										
										
										
											2025-01-29 08:46:06 +00:00
										 |  |  |         $jsFileContent = "The file $livejsFile can't be loaded."; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Check if URL is valid
 | 
					
						
							|  |  |  |         if (!filter_var($jsFile, FILTER_VALIDATE_URL)) { | 
					
						
							|  |  |  |             return "Invalid URL: $jsFile"; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // ssl options
 | 
					
						
							|  |  |  |         $contextOptions = [ | 
					
						
							|  |  |  |             'ssl' => [ | 
					
						
							|  |  |  |                 'verify_peer'		=> true, | 
					
						
							|  |  |  |                 'verify_peer_name'	=> true, | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |         $context = stream_context_create($contextOptions); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-29 08:46:06 +00:00
										 |  |  |         // Try to get headers first to check if file exists and wasn't redirected
 | 
					
						
							|  |  |  |         $headers = @get_headers($jsFile, 1);  // 1 to get headers as array
 | 
					
						
							|  |  |  |         if ($headers === false) { | 
					
						
							|  |  |  |             return "The file $livejsFile can't be loaded (connection error)."; | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-29 08:46:06 +00:00
										 |  |  |         // Check for redirects
 | 
					
						
							|  |  |  |         $statusLine = $headers[0]; | 
					
						
							|  |  |  |         if (strpos($statusLine, '301') !== false || strpos($statusLine, '302') !== false) { | 
					
						
							|  |  |  |             return "The file $livejsFile was redirected - this might indicate the file doesn't exist."; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-29 08:46:06 +00:00
										 |  |  |         // Check if we got 200 OK
 | 
					
						
							|  |  |  |         if (strpos($statusLine, '200') === false) { | 
					
						
							|  |  |  |             return "The file $livejsFile can't be loaded (HTTP error: $statusLine)."; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-29 08:46:06 +00:00
										 |  |  |         // Check content type
 | 
					
						
							|  |  |  |         $contentType = isset($headers['Content-Type']) ? $headers['Content-Type'] : ''; | 
					
						
							|  |  |  |         if (is_array($contentType)) { | 
					
						
							|  |  |  |             $contentType = end($contentType); // get last content-type in case of redirects
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (stripos($contentType, 'javascript') === false && stripos($contentType, 'text/plain') === false) { | 
					
						
							|  |  |  |             return "The file $livejsFile doesn't appear to be a JavaScript file (got $contentType)."; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // get the file
 | 
					
						
							| 
									
										
										
										
											2025-01-29 08:46:06 +00:00
										 |  |  |         $fileContent = @file_get_contents($jsFile, false, $context); | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if ($fileContent !== false) { | 
					
						
							| 
									
										
										
										
											2025-01-29 08:46:06 +00:00
										 |  |  |             // Quick validation of content
 | 
					
						
							|  |  |  |             $firstLine = strtolower(trim(substr($fileContent, 0, 100))); | 
					
						
							|  |  |  |             if (strpos($firstLine, '<!doctype html>') !== false ||  | 
					
						
							|  |  |  |                 strpos($firstLine, '<html') !== false ||  | 
					
						
							|  |  |  |                 strpos($firstLine, '<?xml') !== false) { | 
					
						
							|  |  |  |                 return "The file $livejsFile appears to be HTML/XML content instead of JavaScript."; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // when we need only uncommented values
 | 
					
						
							|  |  |  |             if ($raw === false) { | 
					
						
							|  |  |  |                 // remove block comments
 | 
					
						
							| 
									
										
										
										
											2025-01-29 08:46:06 +00:00
										 |  |  |                 $jsFileContent = preg_replace('!/\*.*?\*/!s', '', $fileContent); | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  |                 // remove single-line comments
 | 
					
						
							| 
									
										
										
										
											2025-01-29 08:46:06 +00:00
										 |  |  |                 $jsFileContent = preg_replace('/\/\/[^\n]*/', '', $jsFileContent); | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  |                 // remove empty lines
 | 
					
						
							| 
									
										
										
										
											2025-01-29 08:46:06 +00:00
										 |  |  |                 $jsFileContent = preg_replace('/^\s*[\r\n]/m', '', $jsFileContent); | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // when we need the full file as it is
 | 
					
						
							|  |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2025-01-29 08:46:06 +00:00
										 |  |  |                 $jsFileContent = $fileContent; | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-29 08:46:06 +00:00
										 |  |  |         return $jsFileContent; | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |