"download", "courseId" => $courseId, "userId" => $userId, "docId" => $docId, "sessionId" => $sessionId ]; if (!empty($groupId)) { $data["groupId"] = $groupId; } $hashUrl = Crypt::GetHash($data); return api_get_path(WEB_PLUGIN_PATH) . self::app_name . "/" . "callback.php?hash=" . $hashUrl; } /** * Return location file in chamilo documents */ function getUrlToLocation($courseCode, $sessionId, $groupId, $folderId) { return api_get_path(WEB_CODE_PATH)."document/document.php" . "?cidReq=" . Security::remove_XSS($courseCode) . "&id_session=" . Security::remove_XSS($sessionId) . "&gidReq=" . Security::remove_XSS($groupId) . "&id=" . Security::remove_XSS($folderId); } /** * Return file key */ public static function getKey(string $courseCode, int $docId): string { $docInfo = DocumentManager::get_document_data_by_id($docId, $courseCode); $mtime = filemtime($docInfo["absolute_path"]); $key = $mtime . $courseCode . $docId; return self::GenerateRevisionId($key); } /** * Translation key to a supported form */ public static function GenerateRevisionId(string $expectedKey): string { if (strlen($expectedKey) > 20) $expectedKey = crc32( $expectedKey); $key = preg_replace("[^0-9-.a-zA-Z_=]", "_", $expectedKey); $key = substr($key, 0, min(array(strlen($key), 20))); return $key; } /** * Create new file */ public static function createFile( string $basename, string $fileExt, int $folderId, int $userId, int $sessionId, int $courseId, int $groupId, string $templatePath = ""): array { $courseInfo = api_get_course_info_by_id($courseId); $courseCode = $courseInfo["code"]; $groupInfo = GroupManager::get_group_properties($groupId); $fileTitle = Security::remove_XSS($basename). "." .$fileExt; $fileNamePrefix = DocumentManager::getDocumentSuffix($courseInfo, $sessionId, $groupId); $fileName = preg_replace('/\.\./', '', $basename) . $fileNamePrefix . "." . $fileExt; if (empty($templatePath)) { $templatePath = TemplateManager::getEmptyTemplate($fileExt); } $folderPath = ''; $fileRelatedPath = "/"; if (!empty($folderId)) { $document_data = DocumentManager::get_document_data_by_id( $folderId, $courseCode, true, $sessionId ); $folderPath = $document_data["absolute_path"]; $fileRelatedPath = $fileRelatedPath . substr($document_data["absolute_path_from_document"], 10) . "/" . $fileName; } else { $folderPath = api_get_path(SYS_COURSE_PATH) . api_get_course_path($courseCode) . "/document"; if (!empty($groupId)) { $folderPath = $folderPath . "/" . $groupInfo["directory"]; $fileRelatedPath = $groupInfo["directory"] . "/"; } $fileRelatedPath = $fileRelatedPath . $fileName; } $filePath = $folderPath . "/" . $fileName; if (file_exists($filePath)) { return ["error" => "fileIsExist"]; } if ($fp = @fopen($filePath, "w")) { $content = file_get_contents($templatePath); fputs($fp, $content); fclose($fp); chmod($filePath, api_get_permissions_for_new_files()); $documentId = add_document( $courseInfo, $fileRelatedPath, "file", filesize($filePath), $fileTitle, null, false ); if ($documentId) { api_item_property_update( $courseInfo, TOOL_DOCUMENT, $documentId, "DocumentAdded", $userId, $groupInfo, null, null, null, $sessionId ); } else { return ["error" => "impossibleCreateFile"]; } } return ["documentId" => $documentId]; } }