PageRenderTime 62ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/src/plugins/contentfilter/video.php

https://github.com/bhar1red/anahita
PHP | 146 lines | 81 code | 15 blank | 50 comment | 7 complexity | 0932dcd3a34d11671b644f31637b4490 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. if ( defined('KOOWA') ) {
  3. /**
  4. * LICENSE: ##LICENSE##
  5. *
  6. * @category Anahita
  7. * @package Plg_Contentfilter
  8. * @author Arash Sanieyan <ash@anahitapolis.com>
  9. * @author Rastin Mehr <rastin@anahitapolis.com>
  10. * @copyright 2008 - 2010 rmdStudio Inc./Peerglobe Technology Inc
  11. * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl-3.0.html>
  12. * @version SVN: $Id$
  13. * @link http://www.anahitapolis.com
  14. */
  15. /**
  16. * Video content filter
  17. *
  18. * @category Anahita
  19. * @package Plg_Contentfilter
  20. * @author Arash Sanieyan <ash@anahitapolis.com>
  21. * @author Rastin Mehr <rastin@anahitapolis.com>
  22. * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl-3.0.html>
  23. * @link http://www.anahitapolis.com
  24. */
  25. class PlgContentfilterVideo extends PlgContentfilterAbstract
  26. {
  27. /**
  28. * Filter a value
  29. *
  30. * @param string The text to filter
  31. *
  32. * @return string
  33. */
  34. public function filter($text)
  35. {
  36. $this->_stripTags($text);
  37. $this->_youtube($text);
  38. $this->_vimeo($text);
  39. $this->_replaceTags($text);
  40. return $text;
  41. }
  42. /**
  43. * Checks the text for a vimeo URL
  44. *
  45. * @param string $text The text to filter
  46. *
  47. * @return string
  48. */
  49. protected function _vimeo(&$text)
  50. {
  51. $matches = array();
  52. if ( preg_match_all('%http://\S*vimeo.com/(\d+)%', $text, $matches) ) {
  53. foreach($matches[1] as $index => $video_id) {
  54. $url = JURI::base().'plugins/contentfilter/video.php?type=vimeo&id='.$video_id;
  55. $options = array(
  56. 'allowfullscreen' => 'true',
  57. 'allowscriptaccess' => 'always',
  58. 'color' => '00ADEF',
  59. 'autoplay' => 'true',
  60. 'url' => 'http://vimeo.com/moogaloop.swf?clip_id='.$video_id,
  61. 'thumbnail' => $url
  62. );
  63. $video = $this->_createVideo($options);
  64. $text = str_replace($matches[0][$index], $video, $text);
  65. }
  66. }
  67. }
  68. /**
  69. * Checks the text for a youtube URL
  70. *
  71. * @param string $text The text to filter
  72. *
  73. * @return string
  74. */
  75. protected function _youtube(&$text)
  76. {
  77. $matches = array();
  78. if ( preg_match_all('%http://?:\S+\.swf\b|\S+?youtu\.?be\S*\/(\S+)%', $text, $matches) )
  79. {
  80. foreach($matches[1] as $index => $match)
  81. {
  82. $youtube_link = $match;
  83. $full_link = $matches[0][$index];
  84. $id = array();
  85. $pattern = '/v=([^&#]+)/';
  86. if ( strpos($full_link,'.be/') ) $pattern = '/([^&#]+)/';
  87. if ( preg_match($pattern, $youtube_link, $id) )
  88. {
  89. $id = str_replace('watch?v=','',array_pop($id));
  90. $link = 'http://www.youtube.com/v/'.$id;
  91. $options = array(
  92. 'allowFullScreen' => 'true',
  93. 'allowScriptAccess' => 'always',
  94. 'autoplay' => 1,
  95. 'url' => $link,
  96. 'thumbnail' => 'http://img.youtube.com/vi/'.$id.'/0.jpg'
  97. );
  98. $video = $this->_createVideo($options);
  99. $text = str_replace($matches[0][$index], $video, $text);
  100. }
  101. }
  102. }
  103. }
  104. /**
  105. * Return a HTML tag for the video to be displayed.
  106. *
  107. * @param array $options The options to be passed, it must contait the video URL, video thumbnail and width and height
  108. *
  109. * @return string
  110. */
  111. protected function _createVideo(array $options)
  112. {
  113. $thumbnail = $options['thumbnail'];
  114. unset($options['thumbnail']);
  115. $options = json_encode($options);
  116. return '<div style="cursor:pointer" data-behavior="EmbeddedVideo" class="an-media-video-thumbnail" data-embeddedvideo-options=\''.$options.'\'>'.
  117. '<img src="'.$thumbnail.'" />'.
  118. '</div>';
  119. }
  120. }
  121. } else {
  122. $type = @$_GET['type'];
  123. $video = @$_GET['id'];
  124. if ( $video ) {
  125. $contents = file_get_contents('http://vimeo.com/api/v2/video/'.$video.'.php');
  126. $array = array_shift(@unserialize(trim($contents)));
  127. $url = $array['thumbnail_large'];
  128. header("Content-type: image/jpeg");
  129. print file_get_contents($url);
  130. }
  131. }