/** * Function that writes history to the uncompiled history-file. * History-files are "uncompiled" before Hicurl::compileHistory has been called on them, which generates a * history-file of cosed state. * In its uncompiled state its structure is as follows: * (historyData without closing bracket and brace)+(tempHistoryData)+(sizeOfTempHistoryData) * historyData is the following json-object: {"pages":[]} * But in the uncompiled file it is without the closing bracket+brace as stated above. Each element in its * pages-array is the following:<pre> * { "formData"://this is present if it was a POST-request, and will contain the sent form-data * ,"name"://a name-string for the page that will be shown in the history-viewer. Set via historyData>name * ,"parentIndex": int//may hold an pointing to an index of another page in the pages-array. This will then be * shown in the tree-view of the history-viewer. This is set up with loadSingle>historyData>id/parentId * ,"customData": //contains what was passed to customData-parameter of the load method if anything * ,"exchanges": [//An array of request&response pairs. Usually this will only contain one * //element but more will be added for each failed request * { * "error": false if no error, otherwise an explanation of the error * "content": the content of the page sent from the server * "headers": ...and the headers * } * ... * ] * } * </pre> * @param SplFileObject $historyFileObject * @param string $data A undecoded page-object, explained in the description of this method. * @param array $settings * @param array $historyData*/ private static function writeHistory($historyFileObject, $data, $settings, $historyData) { if (!isset($historyFileObject)) { $historyFileObject = new SplFileObject($settings['history'], 'c+'); } $historyFileObject->flock(LOCK_EX); //check if the historyFile is empty. we don't have to check for file-existence because it will always //have been created by now byt simply creating the SplFileObject if (!$historyFileObject->fstat()['size']) { $dataPrefix = '{"pages":['; $historyFileTempData = ['numPages' => 0, 'idIndices' => []]; } else { $dataPrefix = ','; $tempDataSize = Hicurl::seekHistoryFileTempData($historyFileObject); $historyFileTempData = json_decode($historyFileObject->fread($tempDataSize), true); $historyFileObject->fseek(-4 - $tempDataSize, SEEK_END); } if (isset($historyData['id'])) { $historyFileTempData['idIndices'][$historyData['id']] = $historyFileTempData['numPages']; } if (isset($historyData['parentId'])) { $data['parentIndex'] = $historyFileTempData['idIndices'][$historyData['parentId']]; } ++$historyFileTempData['numPages']; $historyFileObject->fwrite($dataPrefix . json_encode($data)); $tempDataSize = $historyFileObject->fwrite(json_encode($historyFileTempData)); $historyFileObject->fwrite(pack('N', $tempDataSize)); $historyFileObject->flock(LOCK_UN); }