Beispiel #1
0
 /**
  * Turns internal links into proper absolute links, and/or adds 'http'
  * to external links that were specified without one.
  *
  * @param $link
  * @return string
  */
 protected function getLink($link)
 {
     $page = new \Phile\Repository\Page();
     $linked_page = $page->findByPath($link);
     if ($linked_page) {
         // the user linked to an internal page
         return \Phile\Utility::getBaseUrl() . '/' . $linked_page->getUrl();
     }
     $file = ROOT_DIR . DIRECTORY_SEPARATOR . $link;
     if (file_exists($file)) {
         // the user linked to an internal file
         return \Phile\Utility::getBaseUrl() . '/' . $link;
     }
     // it's not an internal page, it's not an internal file -
     // let's see if it's (at least) a somewhat valid URL
     $url_parts = parse_url($link);
     if (is_array($url_parts)) {
         if (!array_key_exists("scheme", $url_parts)) {
             // it doesn't have a http:// or https:// or similar prefix.
             // This could mean the user provided something like: (link: cnn.com)
             // -> check if the first part of the link looks like a domain name
             $p = explode('/', $url_parts["path"]);
             $domain = $p[0];
             if ($this->isValidDomainName($domain)) {
                 // the first part of the link looks like a valid domain name
                 // -> just prepend 'http://' and continue
                 return "http://{$link}";
             }
         }
     }
     // If we get to this point, we just return whatever the user typed.
     return $link;
 }
Beispiel #2
0
 public function on($eventKey, $data = null)
 {
     if ($eventKey == 'after_parse_content') {
         // check and see if the folder exists
         if (!is_dir(ROOT_DIR . $this->settings['images_dir'])) {
             throw new Exception("The path " . $this->settings['images_dir'] . " in the PhileInlineImage config does not exists or is not a directory.");
         }
         // store the starting content
         $content = $data['content'];
         // find the path for images
         $path = \Phile\Utility::getBaseUrl() . '/' . $this->settings['images_dir'];
         // this parse happens after the markdown
         // which means that the potential image is wrapped
         // in p tags
         $regex = "/(<p>)(.*?)\\.(jpg|jpeg|png|gif|webp|svg)+(<\\/p>)/i";
         // main feature of the plugin, wrapping image names in HTML
         $replace = "\n" . '<' . $this->settings['wrap_element'] . ' class="' . $this->settings['wrap_class'] . '">' . "\n\t" . '<img src="' . $path . '$2.$3">' . "\n" . '</' . $this->settings['wrap_element'] . '>';
         // add the modified content back in the data
         $data['content'] = preg_replace($regex, $replace, $content);
     }
 }