Jump to content

Edit line values problem in PHP


ChrisMartino

Recommended Posts

Well i have a function to edit find the value in a file using SSH2 and PHP, Here's what i have:

 

My problem is that it won't edit the values for some reason, I've checked and all the directory's that it is fetching are correct but no file editing takes place?.

public function CheckClientConfig($serverid)
	{
		if($this->IsValidGameServer($serverid))
		{
			$ClearID = mysql_real_escape_string($serverid);
			$ObtainDetails = mysql_query("SELECT * FROM gameservers WHERE ServerID = '".$ClearID."'");

			$Fetch = mysql_fetch_array($ObtainDetails);

			$SelectServer = mysql_query("SELECT * FROM servers WHERE ServerName = '".$Fetch[OnServer]."'");

			$FetchServer = mysql_fetch_array($SelectServer);

			$SelectPackage = mysql_query("SELECT * FROM serverpackages WHERE PackageNAME = '".$Fetch[Game]."'");

			$PackageRow = mysql_fetch_array($SelectPackage);

			$ssh = new Net_SSH2($FetchServer['IP']);

     			if (!$ssh->login($FetchServer['RootUsername'], $FetchServer['RootPassword'])) 
			{
         			exit('There was a error Re-Starting the server, Please re-try or contact support.');
     			}		

			$ReadConfig = $ssh->exec("sudo -u root cat /home{$Fetch['Directory']}/{$PackageRow['ConfigFile']}");
			        
			$config_file = explode("\n",trim(str_replace("\r",'',$ReadConfig)));
			$config_done = array();
			$config = '';

			foreach($config_file as $key => $line)
			{
				$line = trim($line);
				$ex = explode(' ',$line);
				if(strlen($ex[0]) != 0)
				{
					if(isset($ex[1])) $config_done[strtolower($ex[0])] = (string)$ex[1];
					else $config_done[strtolower($ex[0])] = true;
					if(strcasecmp($ex[0],'maxplayers') == 0)
					{
						$slots = (int) $ex[1];
						if($slots > $Fetch['Slots'] or $slots == 0)
						{
							$config .= 'maxplayers '.$Fetch['Slots']."\n";
						}
						else
						{
							$config .= 'maxplayers '.$slots."\n";
						}
					}
					elseif(strcasecmp($ex[0],'port') == 0)
					{
						$config .= 'port '.$Fetch['Port']."\n";
					}
					else
					{
						$config .= $line."\n";
					}
				}
			}

			if(!isset($config_done['maxplayers'])) $config .= 'maxplayers '.$Fetch['Slots']."\n";
			elseif($config_done['maxplayers'] === true) $config .= 'maxplayers '.$Fetch['Slots']."\n";

			$ssh->exec("echo '/home{$Fetch[Directory]}/{$PackageRow[ConfigFile]}' > ".$config);

		}
	}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.