diff options
Diffstat (limited to 'app/ExternalLink')
-rw-r--r-- | app/ExternalLink/AttachmentLink.php | 26 | ||||
-rw-r--r-- | app/ExternalLink/AttachmentLinkProvider.php | 117 | ||||
-rw-r--r-- | app/ExternalLink/BaseLink.php | 44 | ||||
-rw-r--r-- | app/ExternalLink/BaseLinkProvider.php | 33 | ||||
-rw-r--r-- | app/ExternalLink/WebLink.php | 37 | ||||
-rw-r--r-- | app/ExternalLink/WebLinkProvider.php | 77 |
6 files changed, 334 insertions, 0 deletions
diff --git a/app/ExternalLink/AttachmentLink.php b/app/ExternalLink/AttachmentLink.php new file mode 100644 index 00000000..5a0d1344 --- /dev/null +++ b/app/ExternalLink/AttachmentLink.php @@ -0,0 +1,26 @@ +<?php + +namespace Kanboard\ExternalLink; + +use Kanboard\Core\ExternalLink\ExternalLinkInterface; + +/** + * Attachment Link + * + * @package externalLink + * @author Frederic Guillot + */ +class AttachmentLink extends BaseLink implements ExternalLinkInterface +{ + /** + * Get link title + * + * @access public + * @return string + */ + public function getTitle() + { + $path = parse_url($this->url, PHP_URL_PATH); + return basename($path); + } +} diff --git a/app/ExternalLink/AttachmentLinkProvider.php b/app/ExternalLink/AttachmentLinkProvider.php new file mode 100644 index 00000000..706ac1dd --- /dev/null +++ b/app/ExternalLink/AttachmentLinkProvider.php @@ -0,0 +1,117 @@ +<?php + +namespace Kanboard\ExternalLink; + +use Kanboard\Core\ExternalLink\ExternalLinkProviderInterface; + +/** + * Attachment Link Provider + * + * @package externalLink + * @author Frederic Guillot + */ +class AttachmentLinkProvider extends BaseLinkProvider implements ExternalLinkProviderInterface +{ + /** + * File extensions that are not attachments + * + * @access protected + * @var array + */ + protected $extensions = array( + 'html', + 'htm', + 'xhtml', + 'php', + 'jsp', + 'do', + 'action', + 'asp', + 'aspx', + 'cgi', + ); + + /** + * Get provider name + * + * @access public + * @return string + */ + public function getName() + { + return t('Attachment'); + } + + /** + * Get link type + * + * @access public + * @return string + */ + public function getType() + { + return 'attachment'; + } + + /** + * Get a dictionary of supported dependency types by the provider + * + * @access public + * @return array + */ + public function getDependencies() + { + return array( + 'related' => t('Related'), + ); + } + + /** + * Return true if the provider can parse correctly the user input + * + * @access public + * @return boolean + */ + public function match() + { + if (preg_match('/^https?:\/\/.*\.([^\/]+)$/', $this->userInput, $matches)) { + return $this->isValidExtension($matches[1]); + } + + return false; + } + + /** + * Get the link found with the properties + * + * @access public + * @return \Kanboard\Core\ExternalLink\ExternalLinkInterface + */ + public function getLink() + { + $link = new AttachmentLink($this->container); + $link->setUrl($this->userInput); + + return $link; + } + + /** + * Check file extension + * + * @access protected + * @param string $extension + * @return boolean + */ + protected function isValidExtension($extension) + { + $extension = strtolower($extension); + + foreach ($this->extensions as $ext) { + if ($extension === $ext) { + return false; + } + } + + return true; + } +} diff --git a/app/ExternalLink/BaseLink.php b/app/ExternalLink/BaseLink.php new file mode 100644 index 00000000..08693ae7 --- /dev/null +++ b/app/ExternalLink/BaseLink.php @@ -0,0 +1,44 @@ +<?php + +namespace Kanboard\ExternalLink; + +use Kanboard\Core\Base; + +/** + * Base Link + * + * @package externalLink + * @author Frederic Guillot + */ +abstract class BaseLink extends Base +{ + /** + * URL + * + * @access protected + * @var string + */ + protected $url = ''; + + /** + * Get link URL + * + * @access public + * @return string + */ + public function getUrl() + { + return $this->url; + } + + /** + * Set link URL + * + * @access public + * @param string $url + */ + public function setUrl($url) + { + $this->url = $url; + } +} diff --git a/app/ExternalLink/BaseLinkProvider.php b/app/ExternalLink/BaseLinkProvider.php new file mode 100644 index 00000000..749cda94 --- /dev/null +++ b/app/ExternalLink/BaseLinkProvider.php @@ -0,0 +1,33 @@ +<?php + +namespace Kanboard\ExternalLink; + +use Kanboard\Core\Base; + +/** + * Base Link Provider + * + * @package externalLink + * @author Frederic Guillot + */ +abstract class BaseLinkProvider extends Base +{ + /** + * User input + * + * @access protected + * @var string + */ + protected $userInput = ''; + + /** + * Set text entered by the user + * + * @access public + * @param string $input + */ + public function setUserTextInput($input) + { + $this->userInput = trim($input); + } +} diff --git a/app/ExternalLink/WebLink.php b/app/ExternalLink/WebLink.php new file mode 100644 index 00000000..9338ca42 --- /dev/null +++ b/app/ExternalLink/WebLink.php @@ -0,0 +1,37 @@ +<?php + +namespace Kanboard\ExternalLink; + +use Kanboard\Core\ExternalLink\ExternalLinkInterface; + +/** + * Web Link + * + * @package externalLink + * @author Frederic Guillot + */ +class WebLink extends BaseLink implements ExternalLinkInterface +{ + /** + * Get link title + * + * @access public + * @return string + */ + public function getTitle() + { + $html = $this->httpClient->get($this->url); + + if (preg_match('/<title>(.*)<\/title>/siU', $html, $matches)) { + return trim($matches[1]); + } + + $components = parse_url($this->url); + + if (! empty($components['host']) && ! empty($components['path'])) { + return $components['host'].$components['path']; + } + + return t('Title not found'); + } +} diff --git a/app/ExternalLink/WebLinkProvider.php b/app/ExternalLink/WebLinkProvider.php new file mode 100644 index 00000000..5ec1bbe4 --- /dev/null +++ b/app/ExternalLink/WebLinkProvider.php @@ -0,0 +1,77 @@ +<?php + +namespace Kanboard\ExternalLink; + +use Kanboard\Core\ExternalLink\ExternalLinkProviderInterface; + +/** + * Web Link Provider + * + * @package externalLink + * @author Frederic Guillot + */ +class WebLinkProvider extends BaseLinkProvider implements ExternalLinkProviderInterface +{ + /** + * Get provider name + * + * @access public + * @return string + */ + public function getName() + { + return t('Web Link'); + } + + /** + * Get link type + * + * @access public + * @return string + */ + public function getType() + { + return 'weblink'; + } + + /** + * Get a dictionary of supported dependency types by the provider + * + * @access public + * @return array + */ + public function getDependencies() + { + return array( + 'related' => t('Related'), + ); + } + + /** + * Return true if the provider can parse correctly the user input + * + * @access public + * @return boolean + */ + public function match() + { + $startWithHttp = strpos($this->userInput, 'http://') === 0 || strpos($this->userInput, 'https://') === 0; + $validUrl = filter_var($this->userInput, FILTER_VALIDATE_URL); + + return $startWithHttp && $validUrl; + } + + /** + * Get the link found with the properties + * + * @access public + * @return \Kanboard\Core\ExternalLink\ExternalLinkInterface + */ + public function getLink() + { + $link = new WebLink($this->container); + $link->setUrl($this->userInput); + + return $link; + } +} |