Skip to content

Commit f436aa8

Browse files
committed
Set a stream context timeout for file reading
Add a context array to all instances of file_get_contents(). This is so we don't end up with a timeout too early if the system is set to something short (ie, 15 secs) and we try to open a large file that takes longer than this short limit.
1 parent 7409c89 commit f436aa8

4 files changed

Lines changed: 17 additions & 10 deletions

File tree

index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
if ($ICEcoder["checkUpdates"]) {
1111
$icv_url = "http://icecoder.net/latest-version?thisVersion=".$ICEcoder["versionNo"];
1212
if (ini_get('allow_url_fopen')) {
13-
$icvInfo = explode("\n",file_get_contents($icv_url));
13+
$icvInfo = explode("\n",file_get_contents($icv_url,false,$context));
1414
} elseif (function_exists('curl_init')) {
1515
$ch = curl_init($icv_url);
1616
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

lib/file-control.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
echo '<script>fileType="text";';
3939
echo 'top.ICEcoder.shortURL = top.ICEcoder.rightClickedFile = top.ICEcoder.thisFileFolderLink = "'.$fileLoc."/".$fileName.'";';
4040
echo '</script>';
41-
$loadedFile = toUTF8noBOM(file_get_contents($file),true);
41+
$loadedFile = toUTF8noBOM(file_get_contents($file,false,$context),true);
4242
echo '<textarea name="loadedFile" id="loadedFile">'.str_replace("</textarea>","<ICEcoder:/:textarea>",str_replace("&","&amp;",$loadedFile)).'</textarea>';
4343
} else if (strpos($finfo,"image")===0) {
4444
echo '<script>fileType="image";fileName=\''.$fileLoc."/".$fileName.'\'</script>';
@@ -53,7 +53,7 @@
5353

5454
// Get the contents of a remote URL
5555
if ($_GET['action']=="getRemoteFile") {
56-
if ($remoteFile = toUTF8noBOM(file_get_contents($file),true)) {
56+
if ($remoteFile = toUTF8noBOM(file_get_contents($file,false,$context),true)) {
5757
// replace \r\n (Windows), \r (old Mac) and \n (Linux) line endings with whatever we chose to be lineEnding
5858
$remoteFile = str_replace("\r\n", $ICEcoder["lineEnding"], $remoteFile);
5959
$remoteFile = str_replace("\r", $ICEcoder["lineEnding"], $remoteFile);
@@ -197,7 +197,7 @@ function getDetails($fileArr) {
197197
if ($_GET['action']=="replaceText") {
198198
if (!$demoMode && is_writable(str_replace("|","/",strClean($_GET['fileRef'])))) {
199199
$file = str_replace("|","/",strClean($_GET['fileRef']));
200-
$loadedFile = toUTF8noBOM(file_get_contents($file),true);
200+
$loadedFile = toUTF8noBOM(file_get_contents($file,false,$context),true);
201201
$newContent = str_replace(strClean($_GET['find']),strClean($_GET['replace']),$loadedFile);
202202
$fh = fopen($file, 'w') or die("Sorry, cannot save");
203203
fwrite($fh, $newContent);
@@ -296,7 +296,7 @@ function rrmdir($dir) {
296296
echo '<script>if (top.ICEcoder.previewWindow.location && top.ICEcoder.previewWindow.location.pathname.indexOf(".md")==-1) {top.ICEcoder.previewWindow.location.reload()};</script>';
297297
echo '<script>top.ICEcoder.setPreviousFiles();action="doneSave";</script>';
298298
} else {
299-
$loadedFile = toUTF8noBOM(file_get_contents($file),true);
299+
$loadedFile = toUTF8noBOM(file_get_contents($file,false,$context),true);
300300
echo '<textarea name="loadedFile" id="loadedFile">'.str_replace("</textarea>","<ICEcoder:/:textarea>",htmlentities($loadedFile)).'</textarea>';
301301
echo '<textarea name="userVersionFile" id="userVersionFile"></textarea>';
302302
?>

lib/multiple-results.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ function phpGrep($q, $path, $base) {
121121
$fullPath = $path.$slash.$f;
122122
if(is_dir($fullPath)) {
123123
$ret .= phpGrep($q, $fullPath, $base);
124-
} else if(stristr(toUTF8noBOM(file_get_contents($fullPath),false), $q)) {
124+
} else if(stristr(toUTF8noBOM(file_get_contents($fullPath,false,$context),false), $q)) {
125125
$bFile = false;
126126
$foundInSelFile = false;
127127
for ($i=0;$i<count($ICEcoder['bannedFiles']);$i++) {
@@ -136,7 +136,7 @@ function phpGrep($q, $path, $base) {
136136
}
137137
if (!$bFile && (count($selectedFiles)==0 || count($selectedFiles)>0 && $foundInSelFile)) {
138138
$ret .= "<a href=\\\"javascript:top.ICEcoder.openFile('".$fullPath."');top.ICEcoder.showHide('hide',top.document.getElementById('blackMask'))\\\">";
139-
$ret .= str_replace($base,"",$fullPath)."</a><div id=\\\"foundCount".$r."\\\">Found ".substr_count(strtolower(toUTF8noBOM(file_get_contents($fullPath),false)),$q)." times</div>";
139+
$ret .= str_replace($base,"",$fullPath)."</a><div id=\\\"foundCount".$r."\\\">Found ".substr_count(strtolower(toUTF8noBOM(file_get_contents($fullPath,false,$context),false)),$q)." times</div>";
140140
if (isset($_GET['replace'])) {
141141
$ret .= "<div class=\\\"replace\\\" id=\\\"replace\\\" onClick=\\\"replaceInFileSingle('".$fullPath."');this.style.display=\'none\'\\\">replace</div>";
142142
};

lib/settings.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
// Set our default timezone and supress warning with @
99
@date_default_timezone_set(date_default_timezone_get());
1010

11+
// Set a stream context timeout for file reading
12+
$context = stream_context_create(array('http'=>
13+
array(
14+
'timeout' => 60 // secs
15+
)
16+
));
17+
1118
// Start a session if we haven't already
1219
if(!isset($_SESSION)) {session_start();}
1320

@@ -98,7 +105,7 @@ function toUTF8noBOM($string,$message) {
98105

99106
// Update this config file?
100107
if (!$demoMode && isset($_SESSION['loggedIn']) && $_SESSION['loggedIn'] && isset($_POST["theme"]) && $_POST["theme"]) {
101-
$settingsContents = file_get_contents($settingsFile);
108+
$settingsContents = file_get_contents($settingsFile,false,$context);
102109
// Replace our settings vars
103110
$repPosStart = strpos($settingsContents,'"root"');
104111
$repPosEnd = strpos($settingsContents,'"previousFiles"');
@@ -187,7 +194,7 @@ function toUTF8noBOM($string,$message) {
187194

188195
// Save the currently opened files for next time
189196
if ($_SESSION['loggedIn'] && isset($_GET["saveFiles"]) && $_GET['saveFiles']) {
190-
$settingsContents = file_get_contents($settingsFile);
197+
$settingsContents = file_get_contents($settingsFile,false,$context);
191198

192199
// Replace our previousFiles var with the the current
193200
$repPosStart = strpos($settingsContents,'previousFiles" => "')+20;
@@ -294,7 +301,7 @@ function toUTF8noBOM($string,$message) {
294301
if ($ICEcoder["accountPassword"] == "" && isset($_POST['accountPassword'])) {
295302
$password = generateHash(strClean($_POST['accountPassword']));
296303
$settingsFile = $settingsFile;
297-
$settingsContents = file_get_contents($settingsFile);
304+
$settingsContents = file_get_contents($settingsFile,false,$context);
298305
// Replace our empty password with the one submitted by user
299306
$settingsContents = str_replace('"accountPassword" => "",','"accountPassword" => "'.$password.'",',$settingsContents);
300307
// Also set the update checker preference

0 commit comments

Comments
 (0)