/** * Change hrefs * * @param \DOMDocument $doc * @param string $type front-matter, part, chapter, back-matter, ... * @param int $pos (optional) position of content, used when creating filenames like: chapter-001, chapter-002, ... * * @return \DOMDocument */ protected function kneadHref(\DOMDocument $doc, $type, $pos) { $urls = $doc->getElementsByTagName('a'); foreach ($urls as $url) { $current_url = '' . $url->getAttribute('href'); // Stringify // Don't touch empty urls if (!trim($current_url)) { continue; } // WordPress auto wraps images in a href tags. // For example: <a href="some_image-original.png"><img src="some_image-300x200.png" /></a> // This causes an EPUB validation error of: hyperlink to non-standard resource ( of type 'image/...' ) // We fix this by removing the href if ($url->childNodes->length) { foreach ($url->childNodes as $node) { if ('img' == $node->nodeName && $this->fuzzyImageNameMatch($current_url, $node->getAttribute('src'))) { $url->removeAttribute('href'); continue 2; } } } // Determine if we are trying to link to our own internal content $internal_url = $this->fuzzyHrefMatch($current_url, $type, $pos); if (false !== $internal_url) { $url->setAttribute('href', $internal_url); continue; } // Canonicalize, fix typos, remove garbage if ('#' != @$current_url[0]) { $url->setAttribute('href', \PressBooks\Sanitize\canonicalize_url($current_url)); } } return $doc; }
/** * @covers \PressBooks\Sanitize\canonicalize_url */ public function test_canonicalize_url() { $url = 'pressbooks.com/'; $this->assertEquals('http://pressbooks.com', \PressBooks\Sanitize\canonicalize_url($url)); $url = 'https://pressbooks.com/'; $this->assertEquals('https://pressbooks.com', \PressBooks\Sanitize\canonicalize_url($url)); $url = 'HTTPS://PRESSBOOKS.COM/FOO/BAR/'; $this->assertEquals('https://pressbooks.com/FOO/BAR', \PressBooks\Sanitize\canonicalize_url($url)); $url = 'ftp://PRESSBOOKS.COM/foo/BAR�/?hello=world&TESTING=��123'; $this->assertEquals('http://pressbooks.com/foo/BAR/?hello=world&TESTING=123', \PressBooks\Sanitize\canonicalize_url($url)); $url = 'MAILTO:^accepts�!mostly,garb@ge.../'; $this->assertEquals('MAILTO:^accepts!mostly,garb@ge...', \PressBooks\Sanitize\canonicalize_url($url)); $url = 'mailto:miranda@yourcompany.com?bcc=eventsteam@yourcompany.com&subject=Excited%20to%20meet%20at%20the%20event!&body=Hi%20Miranda,'; $this->assertEquals($url, \PressBooks\Sanitize\canonicalize_url($url)); }
/** * Add Book by URL */ static function formAddByUrl() { check_admin_referer('bulk-books'); // Nonce auto-generated by WP_List_Table $catalog = new static(); $user_id = $catalog->getUserId(); // Set Redirect URL if (get_current_user_id() != $user_id) { $redirect_url = get_bloginfo('url') . '/wp-admin/index.php?page=pb_catalog&user_id=' . $user_id; } else { $redirect_url = get_bloginfo('url') . '/wp-admin/index.php?page=pb_catalog'; } $url = parse_url(\PressBooks\Sanitize\canonicalize_url($_REQUEST['add_book_by_url'])); $main = parse_url(network_site_url()); if (strpos($url['host'], $main['host']) === false) { $_SESSION['pb_errors'][] = __('Invalid URL.', 'pressbooks'); \PressBooks\Redirect\location($redirect_url); } if ($url['host'] == $main['host']) { // Get slug using the path $slug = str_replace($main['path'], '', $url['path']); $slug = trim($slug, '/'); $slug = explode('/', $slug); $slug = $slug[0]; } else { // Get slug using host $slug = str_replace($main['host'], '', $url['host']); $slug = trim($slug, '.'); $slug = explode('.', $slug); $slug = $slug[0]; } $book_id = get_id_from_blogname($slug); if (!$book_id) { $_SESSION['pb_errors'][] = __('No book found.', 'pressbooks'); \PressBooks\Redirect\location($redirect_url); } // if ( ! get_blog_option( $book_id, 'blog_public' ) ) { // $_SESSION['pb_errors'][] = __( 'Book is not public', 'pressbooks' ); // \PressBooks\Redirect\location( $redirect_url ); // } $catalog->saveBook($book_id, array()); $catalog->deleteCache(); // Ok! $_SESSION['pb_notices'][] = __('Settings saved.'); // Redirect back to form \PressBooks\Redirect\location($redirect_url); }
/** * Ecommerce settings, input sanitization * * @param array $input * @return array */ function ecomm_links_sanitize($input) { $options = get_option('pressbooks_ecommerce_links'); foreach ($input as $key => $value) { $value = trim(strip_tags(stripslashes($value))); if ($value) { $options[$key] = \PressBooks\Sanitize\canonicalize_url($value); } else { $options[$key] = null; } } return $options; }