in classes/api/base.php [256:343]
public function download_page($pageid, $path) {
$pageendpoint = '/pages/'.$pageid.'/content';
$response = $this->apicall('get', $pageendpoint);
// On success, we get an HTML page as response. On failure, we get JSON error object, so we have to decode to check errors.
$decodedresponse = json_decode($response);
if (empty($response) || !empty($decodedresponse->error)) {
\local_onenote\utils::debug('Download error', 'onenote\api\download_page', $response);
return null;
}
// See if the file contains any references to images or other files and if so, create a folder and download those, too.
$doc = new \DOMDocument('1.0', 'UTF-8');
$doc->loadHTML(mb_convert_encoding($response, 'HTML-ENTITIES', 'UTF-8'));
$xpath = new \DOMXPath($doc);
$this->handle_garbage_chars($xpath);
// Process span tags to increase font size.
$this->process_span_tags($xpath);
$imgnodes = $xpath->query("//img");
// Create temp folder.
$tempfolder = $this->create_temp_folder();
if ($imgnodes && ($imgnodes->length > 0)) {
$filesfolder = join(DIRECTORY_SEPARATOR, array(rtrim($tempfolder, DIRECTORY_SEPARATOR), 'page_files'));
if (!mkdir($filesfolder, 0777, true)) {
echo('Failed to create folder: ' . $filesfolder);
\local_onenote\utils::debug('Failed to create folde', 'onenote\api\download_page', $filesfolder);
return null;
}
// Save images etc.
$i = 1;
foreach ($imgnodes as $imgnode) {
try {
$srcnode = $imgnode->attributes->getNamedItem("src");
if (!$srcnode) {
continue;
}
$response = $this->geturl($srcnode->nodeValue);
if (empty($response)) {
continue;
}
file_put_contents($filesfolder . DIRECTORY_SEPARATOR . 'img_' . $i, $response);
// Update img src paths in the html accordingly.
$srcnode->nodeValue = '.' . DIRECTORY_SEPARATOR . 'page_files' . DIRECTORY_SEPARATOR . 'img_' . $i;
// Remove data_fullres_src if present.
if ($imgnode->attributes->getNamedItem("data-fullres-src")) {
$imgnode->removeAttribute("data-fullres-src");
}
} catch (\Exception $e) {
// Skip image if there is a problem.
\local_onenote\utils::debug('Problem saving image', 'onenote\api\download_page', $e);
}
$i++;
}
// Save the html page itself.
file_put_contents(join(DIRECTORY_SEPARATOR,
array(rtrim($tempfolder, DIRECTORY_SEPARATOR), 'page.html')),
mb_convert_encoding($doc->saveHTML($doc), 'HTML-ENTITIES', 'UTF-8'));
} else {
// Save the html page itself.
file_put_contents(join(DIRECTORY_SEPARATOR,
array(rtrim($tempfolder, DIRECTORY_SEPARATOR), 'page.html')),
mb_convert_encoding($response, 'HTML-ENTITIES', 'UTF-8'));
}
// Zip up the folder so it can be attached as a single file.
$fp = get_file_packer('application/zip');
$filelist = array();
$filelist[] = $tempfolder;
$fp->archive_to_pathname($filelist, $path);
fulldelete($tempfolder);
return array('path' => $path, 'url' => static::API.$pageendpoint);
}