function validation($data, $files) { global $CFG; $cm = $this->_customdata; $errors = parent::validation($data, $files); $reference = stripslashes($data['reference']); if ($reference != '') { //null path is root $reference = book_prepare_link($reference); if ($reference == '') { //evil characters in $ref! $errors['choosesomething'] = get_string('error'); } else { $coursebase = $CFG->dataroot . '/' . $cm->course; if ($reference == '') { $base = $coursebase; } else { $base = $coursebase . '/' . $reference; } if (!is_dir($base) and !is_file($base)) { $errors['choosesomething'] = get_string('error'); } } } return $errors; }
$navlinks = array(); $navlinks[] = array('name' => $strimport, 'link' => '', 'type' => 'title'); $navigation = build_navigation($navlinks, $cm); $mform = new book_import_form(null, $cm); $mform->set_data(array('id' => $cm->id)); /// If data submitted, then process and store. if ($mform->is_cancelled()) { if (empty($chapter->id)) { redirect("view.php?id={$cm->id}"); } else { redirect("view.php?id={$cm->id}&chapterid={$chapter->id}"); } } else { if ($data = $mform->get_data(false)) { $coursebase = $CFG->dataroot . '/' . $book->course; $reference = book_prepare_link($data->reference); if ($reference == '') { $base = $coursebase; } else { $base = $coursebase . '/' . $reference; } //prepare list of html files in $refs $refs = array(); $htmlpat = '/\\.html$|\\.htm$/i'; if (is_dir($base)) { //import whole directory $basedir = opendir($base); while ($file = readdir($basedir)) { $path = $base . '/' . $file; if (filetype($path) == 'file' and preg_match($htmlpat, $file)) { $refs[] = str_replace($coursebase, '', $path);
function book_relink($id, $bookid, $courseid) { global $CFG; if ($CFG->slasharguments) { $coursebase = $CFG->wwwroot . '/file.php/' . $courseid; } else { $coursebase = $CFG->wwwroot . '/file.php?file=/' . $courseid; } $chapters = get_records('book_chapters', 'bookid', $bookid, 'pagenum', 'id, pagenum, title, content, importsrc'); $originals = array(); foreach ($chapters as $ch) { $originals[$ch->importsrc] = $ch; } foreach ($chapters as $ch) { $rel = substr($ch->importsrc, 0, strrpos($ch->importsrc, '/') + 1); $base = $coursebase . strtr(urlencode($rel), array("%2F" => "/")); //for better internationalization (dlnsk) $modified = false; //image relinking if ($ch->importsrc && preg_match_all('/(<img[^>]+src=")([^"]+)("[^>]*>)/i', $ch->content, $images)) { for ($i = 0; $i < count($images[0]); $i++) { if (!preg_match('/[a-z]+:/i', $images[2][$i])) { // not absolute link $link = book_prepare_link($base . $images[2][$i]); if ($link == '') { continue; } $origtag = $images[0][$i]; $newtag = $images[1][$i] . $link . $images[3][$i]; $ch->content = str_replace($origtag, $newtag, $ch->content); $modified = true; book_log($ch->title, $images[2][$i] . ' --> ' . $link); } } } //css relinking (dlnsk) if ($ch->importsrc && preg_match_all('/(<link[^>]+href=")([^"]+)("[^>]*>)/i', $ch->content, $csslinks)) { for ($i = 0; $i < count($csslinks[0]); $i++) { if (!preg_match('/[a-z]+:/i', $csslinks[2][$i])) { // not absolute link $link = book_prepare_link($base . $csslinks[2][$i]); if ($link == '') { continue; } $origtag = $csslinks[0][$i]; $newtag = $csslinks[1][$i] . $link . $csslinks[3][$i]; $ch->content = str_replace($origtag, $newtag, $ch->content); $modified = true; book_log($ch->title, $csslinks[2][$i] . ' --> ' . $link); } } } //general embed relinking - flash and others?? if ($ch->importsrc && preg_match_all('/(<embed[^>]+src=")([^"]+)("[^>]*>)/i', $ch->content, $embeds)) { for ($i = 0; $i < count($embeds[0]); $i++) { if (!preg_match('/[a-z]+:/i', $embeds[2][$i])) { // not absolute link $link = book_prepare_link($base . $embeds[2][$i]); if ($link == '') { continue; } $origtag = $embeds[0][$i]; $newtag = $embeds[1][$i] . $link . $embeds[3][$i]; $ch->content = str_replace($origtag, $newtag, $ch->content); $modified = true; book_log($ch->title, $embeds[2][$i] . ' --> ' . $link); } } } //flash in IE <param name=movie value="something" - I do hate IE! if ($ch->importsrc && preg_match_all('/<param[^>]+name\\s*=\\s*"?movie"?[^>]*>/i', $ch->content, $params)) { for ($i = 0; $i < count($params[0]); $i++) { if (preg_match('/(value=\\s*")([^"]+)(")/i', $params[0][$i], $values)) { if (!preg_match('/[a-z]+:/i', $values[2])) { // not absolute link $link = book_prepare_link($base . $values[2]); if ($link == '') { continue; } $newvalue = $values[1] . $link . $values[3]; $newparam = str_replace($values[0], $newvalue, $params[0][$i]); $ch->content = str_replace($params[0][$i], $newparam, $ch->content); $modified = true; book_log($ch->title, $values[2] . ' --> ' . $link); } } } } //java applet - add code bases if not present!!!! if ($ch->importsrc && preg_match_all('/<applet[^>]*>/i', $ch->content, $applets)) { for ($i = 0; $i < count($applets[0]); $i++) { if (!stripos($applets[0][$i], 'codebase')) { $newapplet = str_ireplace('<applet', '<applet codebase="."', $applets[0][$i]); $ch->content = str_replace($applets[0][$i], $newapplet, $ch->content); $modified = true; } } } //relink java applet code bases if ($ch->importsrc && preg_match_all('/(<applet[^>]+codebase=")([^"]+)("[^>]*>)/i', $ch->content, $codebases)) { for ($i = 0; $i < count($codebases[0]); $i++) { if (!preg_match('/[a-z]+:/i', $codebases[2][$i])) { // not absolute link $link = book_prepare_link($base . $codebases[2][$i]); if ($link == '') { continue; } $origtag = $codebases[0][$i]; $newtag = $codebases[1][$i] . $link . $codebases[3][$i]; $ch->content = str_replace($origtag, $newtag, $ch->content); $modified = true; book_log($ch->title, $codebases[2][$i] . ' --> ' . $link); } } } //relative link conversion if ($ch->importsrc && preg_match_all('/(<a\\s[^>]*href=")([^"^#]*)(#[^"]*)?("[^>]*>)/i', $ch->content, $links)) { for ($i = 0; $i < count($links[0]); $i++) { if ($links[2][$i] != '' && !preg_match('/[a-z]+:/i', $links[2][$i])) { //not absolute link $origtag = $links[0][$i]; $target = book_prepare_link($rel . $links[2][$i]); //target chapter if ($target != '' && array_key_exists($target, $originals)) { $o = $originals[$target]; $newtag = $links[1][$i] . $CFG->wwwroot . '/mod/book/view.php?id=' . $id . '&chapterid=' . $o->id . $links[3][$i] . $links[4][$i]; $newtag = preg_replace('/target=[^\\s>]/i', '', $newtag); $ch->content = str_replace($origtag, $newtag, $ch->content); $modified = true; book_log($ch->title, $links[2][$i] . $links[3][$i] . ' --> ' . $CFG->wwwroot . '/mod/book/view.php?id=' . $id . '&chapterid=' . $o->id . $links[3][$i]); } else { if ($target != '' && !preg_match('/\\.html$|\\.htm$/i', $links[2][$i])) { // other relative non html links converted to download links $target = book_prepare_link($base . $links[2][$i]); $origtag = $links[0][$i]; $newtag = $links[1][$i] . $target . $links[4][$i]; $ch->content = str_replace($origtag, $newtag, $ch->content); $modified = true; book_log($ch->title, $links[2][$i] . ' --> ' . $target); } } } } } if ($modified) { $ch->title = addslashes($ch->title); $ch->content = addslashes($ch->content); $ch->importsrc = addslashes($ch->importsrc); if (!update_record('book_chapters', $ch)) { error('Could not update your book'); } } } }