patch_apply($dmp->patch_fromText($patch), $fileContents); $this->content = $p[0]; break; case 'sendEdits': if(!isset($_POST['filename']) || empty($_POST['filename'])) { exit(formatJSEND('error', 'No filename specified in sendEdits')); } if(!isset($_POST['patch'])) { exit(formatJSEND('error', 'No patch specified in sendEdits')); } $filename = $_POST['filename']; $patch = $_POST['patch']; $dmp = new diff_match_patch(); $p = $dmp->patch_apply($dmp->patch_fromText($patch), $fileContents); $this->content = $p[0]; break; default: exit(formatJSEND('error', 'Unknown Action ' . $_POST['action'])); } /* $filename must contain only the basename of the file. */ function isUserRegisteredForFile($user, $filename) { $marker = BASE_PATH . '/data/' . str_replace('/', '_', $filename) . '%%' . $user; return file_exists($marker); } /* $filename must contain only the basename of the file. */ function getRegisteredUsersForFile($filename) { $usernames = array(); $markers = getMarkerFilesForFilename($filename); if (!empty($markers)) { foreach ($markers as $entry) { /* Beware if you add new marker file types to add them also in * this test. */ if (!strpos($entry, 'selection') && !strpos($entry, 'changes')) { /* $entry is a marker file marking a registered user. * Extract the user name from the filename. */ $matches = array(); preg_match('/\w+$/', $entry, $matches); if (count($matches) !== 1) { exit(formatJSEND('error', 'Unable To Match Username in getMarkerFilesForFilename')); } $usernames[] = $matches[0]; } } } return $usernames; } /* Return all marker files related to $filename. $filename must contain * only the basename of the file. */ function getMarkerFilesForFilename($filename) { $markers = array(); $basePath = BASE_PATH . '/data/'; if ($handle = opendir($basePath)) { $sanitizedFilename = str_replace('/', '_', $filename); while (false !== ($entry = readdir($handle))) { if (strpos($entry, $sanitizedFilename) !== false) { $markers[] = $entry; } } } return $markers; } /* Return the selection object, if any, for the given filename and user. * $filename must contain only the basename of the file. */ function getSelection($filename, $user) { $sanitizedFilename = str_replace('/', '_', $filename); $json = getJSON($sanitizedFilename . '%%' . $user . '%%selection'); return $json; } /* Return the list of changes, if any, for the given filename, user and * from the given revision number. * $filename must contain only the basename of the file. */ function getChanges($filename, $user, $fromRevision) { $sanitizedFilename = str_replace('/', '_', $filename); $json = getJSON($sanitizedFilename . '%%' . $user . '%%changes'); /* print_r(array_slice($json, $fromRevision, NULL, true)); */ return array_slice($json, $fromRevision, NULL, true); } ?>