/** * Convert binary files to text and ensure the charset is UTF8 * * @param object $file moodle storedfile * @return content or false */ protected function get_clear_utf8_content($file) { $localewincharset = get_string('localewincharset', 'langconfig'); $filen = $file->get_filename(); $file_type = strtolower(substr($filen, strlen($filen) - 4, 4)); if (array_search($file_type, array('.pdf', '.rtf', '.odt', '.doc', 'docx'))) { $temp_file = $this->tempdir . "/{$filen}.tmp"; $file->copy_content_to($temp_file); switch ($file_type) { case '.pdf': $content = pdf2text($temp_file); break; case '.rtf': $content = textlib_get_instance()->entities_to_utf8(rtf2text($temp_file)); break; case '.odt': $content = getTextFromZippedXML($temp_file, 'content.xml'); break; case '.doc': $antiwordpath = $this->get_config('antiwordpath'); $magic = file_get_contents($temp_file, NULL, NULL, -1, 2); if ($magic === 'PK') { // It is really a docx $content = getTextFromZippedXML($temp_file, 'word/document.xml'); } else { if (empty($antiwordpath) || !is_executable($antiwordpath)) { $content = textlib_get_instance()->entities_to_utf8(doc2text($temp_file)); } else { $content = shell_exec($antiwordpath . ' -f -w 0 ' . escapeshellarg($temp_file)); if (empty($content)) { // antiword can not recognize this file $content = textlib_get_instance()->entities_to_utf8(doc2text($temp_file)); } } } break; case 'docx': $content = getTextFromZippedXML($temp_file, 'word/document.xml'); break; } unlink($temp_file); return $this->wordwrap($content, 80); } // Files no need to covert format go here $content = $file->get_content(); if (!mb_check_encoding($content, 'UTF-8')) { if (mb_check_encoding($content, $localewincharset)) { // Convert content charset to UTF-8 $content = textlib_get_instance()->convert($content, $localewincharset); } else { // Unknown charset, possible binary file. Skip it mtrace("\tSkip unknown charset/binary file " . $file->get_filepath() . $file->get_filename()); return false; } } return $content; }
/** * Sends a file object to google documents * * @param object $file File object * @return boolean True on success */ public function send_file($file) { // First we create the 'resumable upload request'. $this->googleoauth->setHeader("Content-Length: 0"); $this->googleoauth->setHeader("X-Upload-Content-Length: " . $file->get_filesize()); $this->googleoauth->setHeader("X-Upload-Content-Type: " . $file->get_mimetype()); $this->googleoauth->setHeader("Slug: " . $file->get_filename()); $this->googleoauth->post(self::UPLOAD_URL); if ($this->googleoauth->info['http_code'] !== 200) { throw new moodle_exception('Cantpostupload'); } // Now we http PUT the file in the location returned. $location = $this->googleoauth->response['Location']; if (empty($location)) { throw new moodle_exception('Nouploadlocation'); } // Reset the curl object for actually sending the file. $this->reset_curl_state(); $this->googleoauth->setHeader("Content-Length: " . $file->get_filesize()); $this->googleoauth->setHeader("Content-Type: " . $file->get_mimetype()); // We can't get a filepointer, so have to copy the file.. $tmproot = make_temp_directory('googledocsuploads'); $tmpfilepath = $tmproot . '/' . $file->get_contenthash(); $file->copy_content_to($tmpfilepath); // HTTP PUT the file. $this->googleoauth->put($location, array('file' => $tmpfilepath)); // Remove the temporary file we created.. unlink($tmpfilepath); if ($this->googleoauth->info['http_code'] === 201) { // Clear headers for further requests. $this->reset_curl_state(); return true; } else { return false; } }