/** * Function to look after backup restorations * @param $restore_area String Area to restore * @param $restore_area_data Array Area data as an array */ function process_restore_data($restore_area, $restore_area_data) { global $settings, $system_config_areas, $keepMasterSlaveSettings, $keepNetworkSettings, $uploadDataProtected, $settings_restored; global $args; //Includes for API access require_once 'fppjson.php'; require_once 'fppxml.php'; $restore_area_key = $restore_area_sub_key = ""; $save_result = false; //if restore area contains a forward slash, then we want to restore into a sub-area //split string to get the main area and sub-area if (stripos($restore_area, "/") !== false) { $restore_area_arr = explode("/", $restore_area); $restore_area_key = $restore_area_arr[0]; //main area is first $restore_area_sub_key = $restore_area_arr[1]; //sub-area is 2nd } else { $restore_area_key = $restore_area; } if ($restore_area_key == "channelOutputsJSON") { $channel_outputs_json_filepath = $system_config_areas['channelOutputsJSON']['file']; //PrettyPrint the JSON data and save it $json_pp_data = prettyPrintJSON(json_encode($restore_area_data)); $save_result = file_put_contents($channel_outputs_json_filepath, $json_pp_data); } if ($restore_area_key == "channeloutputs") { //Overwrite channel outputs JSON $channel_outputs_filepath = $system_config_areas['channeloutputs']['file']; //implode array into string and reinsert new lines (reverse of backup explode) $data = implode("\n", $restore_area_data); $save_result = file_put_contents($channel_outputs_filepath, $data); } if ($restore_area_key == "channelmemorymaps") { //Overwrite channel outputs JSON $channelmemorymaps_filepath = $system_config_areas['channelmemorymaps']['file']; $data = implode("\n", $restore_area_data); $save_result = file_put_contents($channelmemorymaps_filepath, $data); } if ($restore_area_key == "show_setup") { $script_filenames = array(); $show_setup_areas = $system_config_areas['show_setup']['file']; //search through the files that should of been backed up //and then loop over the restore data and match up the data and restore it foreach ($show_setup_areas as $show_setup_area_index => $show_setup_area_data) { $restore_location = $show_setup_area_data['location']; $restore_type = $show_setup_area_data['type']; $final_restore_data = ""; //If $restore_area_sub_key is empty then no sub-area has been chosen -- restore as normal //Or if $restore_area_sub_key is equal to the $show_setup_area_index we're on, then restore just this area //and break the loop if ($restore_area_sub_key == "" || $restore_area_sub_key == $show_setup_area_index) { //if the restore key and the $show_setup_areas key match then restore data to whatever location it is //eg. if we are on events, then look for events in the restore data, when found restore data to the events location foreach ($restore_area_data as $restore_area_data_index => $restore_area_data_data) { $save_result = false; //$restore_area_data_data is an array representing the file contents //$restore_area_data_index represents the filename (used to key the array) if ($show_setup_area_index == $restore_area_data_index) { if (is_array($restore_area_data_data)) { //loop over all the files and their data and restore each //$add array will look like //array ('event'=> array('01_01.fevt' => array(data), '21_10.fevt' => array(data))) foreach ($restore_area_data_data as $fn_to_restore => $fn_data) { $restore_location = $show_setup_area_data['location']; //reset $final_restore_data = ""; $final_restore_data = implode("\n", $fn_data); if (strtolower($show_setup_area_index) == "scripts") { $script_filenames[] = $fn_to_restore; } if ($restore_type == "dir") { $restore_location .= "/" . $fn_to_restore; } if (!empty($final_restore_data)) { $save_result = file_put_contents($restore_location, $final_restore_data); } } } break; //break after data is restored for this section } } //Don't break if area key is empty, because we want to process all the sub-areas if ($restore_area_sub_key != "") { //we found the sub-area, stop looking break; } } } //Cause any InstallActions to be run for the restored scripts RestoreScripts($script_filenames); //Restart FFPD so any changes can take effect RestartFPPD(); } if ($restore_area_key == "plugins") { if (is_array($restore_area_data)) { //Just overwrite the universes file $plugin_settings_path = $settings['configDirectory']; //loop over the data and get the plugin name and then write the settings out foreach ($restore_area_data as $plugin_name => $plugin_data) { $plugin_settings_path = $plugin_settings_path . "/plugin." . $plugin_name; $data = implode("\n", $plugin_data[$plugin_name]); $save_result = file_put_contents($plugin_settings_path, $data); } } } if ($restore_area_key == "email") { //TODO rework this so it will work future email system implementation, were different providers are used if (is_array($restore_area_data)) { $emailenable = $restore_area_data['emailenable']; $emailguser = $restore_area_data['emailguser']; $emailgpass = $restore_area_data['emailgpass']; $emailfromtext = $restore_area_data['emailfromtext']; $emailtoemail = $restore_area_data['emailtoemail']; //Write them out WriteSettingToFile('emailenable', $emailenable); WriteSettingToFile('emailguser', $emailguser); WriteSettingToFile('emailfromtext', $emailfromtext); WriteSettingToFile('emailtoemail', $emailtoemail); //Only save password and generate exim config if upload data is unprotected //meaning the password was included in the backup, //otherwise existing (valid) config may be overwritten if ($uploadDataProtected == false && $emailgpass != "") { WriteSettingToFile('emailgpass', $emailgpass); //Update the email config (writes out exim config) SaveEmailConfig($emailguser, $emailgpass, $emailfromtext, $emailtoemail); } $save_result = true; } } if ($restore_area_key == "settings") { if (is_array($restore_area_data)) { foreach ($restore_area_data as $setting_name => $setting_value) { //check if we can change it (default value is checked - true) if ($setting_name == "fppMode") { if ($keepMasterSlaveSettings == false) { WriteSettingToFile($setting_name, $setting_value); } } else { WriteSettingToFile($setting_name, $setting_value); } //Do special things that require some sort of system change //eg. changing piRTC via GUI will fire off a shell command to set it up //we'll also do this to keep consistency if ($setting_name == 'piRTC') { SetPiRTC($setting_value); } else { if ($setting_name == "PI_LCD_Enabled") { //DO a weird work around and set our request params and then call //the function to enable the LCD if ($setting_value == 1) { $_GET['enabled'] = "true"; } else { $_GET['enabled'] = "false"; } SetPiLCDenabled(); } else { if ($setting_name == "AudioOutput") { $args['value'] = $setting_value; SetAudioOutput(); } else { if ($setting_name == "volume") { $_GET['volume'] = trim($setting_value); SetVolume(); } else { if ($setting_name == "ntpServer") { SetNtpServer($setting_value); // SetNtpState(1);//Toggle NTP on NtpServiceRestart(); //Restart NTP client so changes take effect } else { if ($setting_name == "NTP") { SetNtpState($setting_value); } } } } } } } //Restart FFPD so any changes can take effect RestartFPPD(); $save_result = true; } else { error_log("RESTORE: Cannot read Settings INI settings. Attempted to parse " . json_encode($restore_area_data)); } } if ($restore_area_key == "timezone") { $data = $restore_area_data[0]; //first index has the timezone, index 1 is empty to due carriage return in file when its backed up SetTimezone($data); $save_result = true; } if ($restore_area_key == "pixelnetDMX") { //Just overwrite the universes file $pixlnet_filepath = $system_config_areas['pixelnetDMX']['file']; $data = implode("\n", $restore_area_data); $save_result = file_put_contents($pixlnet_filepath, $data); } if ($restore_area_key == "universes") { //Just overwrite the universes file $universe_filepath = $system_config_areas['universes']['file']; $data = implode("\n", $restore_area_data); $save_result = file_put_contents($universe_filepath, $data); } if ($save_result) { $settings_restored[$restore_area] = true; } else { $settings_restored[$restore_area] = false; error_log("RESTORE: Failed to restore " . $restore_area . " "); } }
function SetSetting() { global $args, $SUDO; $setting = $args['key']; $value = $args['value']; check($setting, "setting", __FUNCTION__); check($value, "value", __FUNCTION__); WriteSettingToFile($setting, $value); if ($setting == "LogLevel") { SendCommand("LogLevel,{$value},"); } else { if ($setting == "LogMask") { $newValue = ""; if ($value != "") { $newValue = preg_replace("/,/", ";", $value); } SendCommand("LogMask,{$newValue},"); } else { if ($setting == "HostName") { $value = preg_replace("/[^a-zA-Z0-9]/", "", $value); exec($SUDO . " sed -i 's/^.*\$/{$value}/' /etc/hostname ; " . $SUDO . " sed -i '/^127.0.1.1[^0-9]/d' /etc/hosts ; " . $SUDO . " sed -i '\$a127.0.1.1 {$value}' /etc/hosts ; " . $SUDO . " hostname {$value} ; " . $SUDO . " /etc/init.d/avahi-daemon restart ;" . $SUDO . " systemctl restart avahi-daemon.service", $output, $return_val); sleep(1); // Give Avahi time to restart before we return } else { if ($setting == "EnableRouting") { if ($value != "1") { $value = "0"; } exec($SUDO . " sed -i '/net.ipv4.ip_forward/d' /etc/sysctl.conf; " . $SUDO . " sed -i '\$anet.ipv4.ip_forward = {$value}' /etc/sysctl.conf ; " . $SUDO . " sysctl --system", $output, $return_val); } else { if ($setting == "storageDevice") { exec('mount | grep boot | cut -f1 -d" " | sed -e "s/\\/dev\\///" -e "s/p[0-9]$//"', $output, $return_val); $bootDevice = $output[0]; unset($output); if (preg_match("/{$bootDevice}/", $value)) { exec($SUDO . " sed -i 's/.*home\\/fpp\\/media/#\\/dev\\/sda1 \\/home\\/fpp\\/media/' /etc/fstab", $output, $return_val); } else { exec($SUDO . " sed -i 's/.*home\\/fpp\\/media/\\/dev\\/{$value} \\/home\\/fpp\\/media/' /etc/fstab", $output, $return_val); } } else { if ($setting == "AudioOutput") { SetAudioOutput($value); } else { if ($setting == "ForceHDMI") { if ($value) { exec($SUDO . " sed -i '/hdmi_force_hotplug/d' /boot/config.txt; " . $SUDO . " sed -i '\$ahdmi_force_hotplug=1' /boot/config.txt", $output, $return_val); } else { exec($SUDO . " sed -i '/hdmi_force_hotplug/d' /boot/config.txt; " . $SUDO . " sed -i '\$a#hdmi_force_hotplug=1' /boot/config.txt", $output, $return_val); } } else { SendCommand("SetSetting,{$setting},{$value},"); } } } } } } } GetSetting(); }