MediaVideo.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\MediaVideoRepository;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Symfony\Component\Validator\Constraints as Assert;
  6. #[ORM\Table(name: "media_video")]
  7. #[ORM\Entity(repositoryClass: MediaVideoRepository::class)]
  8. #[ORM\HasLifecycleCallbacks] // Permet d’utiliser des événements
  9. class MediaVideo
  10. {
  11. #[ORM\Column(name: "id", type: "integer")]
  12. #[ORM\Id]
  13. #[ORM\GeneratedValue(strategy: "AUTO")]
  14. private $id;
  15. #[ORM\Column(name: "type", type: "string", length: 191)]
  16. private $type;
  17. #[ORM\Column(name: "identif", type: "string", length: 191)]
  18. private $identif;
  19. #[Assert\Regex(
  20. pattern: "#^(http|https)://(youtu.be|www.youtube.com|www.dailymotion.com|vimeo.com|video.fdlibre.eu)/#",
  21. match: true,
  22. message: "L'url doit correspondre à l'url d'une vidéo Youtube, DailyMotion, Vimeo ou Peertube(fdlibre)"
  23. )]
  24. private $url;
  25. public function getId(): ?int
  26. {
  27. return $this->id;
  28. }
  29. public function getUrl(): ?string
  30. {
  31. return $this->url;
  32. }
  33. public function setUrl($url): self
  34. {
  35. $this->url = $url;
  36. return $this;
  37. }
  38. public function setType($type): self
  39. {
  40. $this->type = $type;
  41. return $this;
  42. }
  43. public function getType(): ?string
  44. {
  45. return $this->type;
  46. }
  47. public function setIdentif($identif): self
  48. {
  49. $this->identif = $identif;
  50. return $this;
  51. }
  52. public function getIdentif(): ?string
  53. {
  54. return $this->identif;
  55. }
  56. /////////////////////
  57. // Méthodes
  58. private function peertubeId($url): void
  59. {
  60. $tableau = explode("/", $url);
  61. $id = $tableau[count($tableau)-1];
  62. $this->setIdentif($id);
  63. $this->setType('peertube');
  64. }
  65. private function youtubeId($url): void
  66. {
  67. $tableaux = explode("=", $url); // découpe l’url en deux avec le signe ‘=’
  68. $this->setIdentif($tableaux[1]); // ajoute l’identifiant à l’attribut identif
  69. $this->setType('youtube'); // signale qu’il s’agit d’une video youtube et l’inscrit dans l’attribut $type
  70. }
  71. private function youtubeCourteId($url): void
  72. {
  73. $tableaux = explode("/", $url); // on découpe l’url grâce au « / »
  74. $id = $tableaux[count($tableaux)-1]; // on retient la dernière partie qui contient l’identifiant
  75. $this->setIdentif($id); // ajoute l’identifiant à l’attribut identif
  76. $this->setType('youtube'); // signale qu’il s’agit d’une video youtube et l’inscrit dans l’attribut $type
  77. }
  78. private function dailymotionId($url): void
  79. {
  80. $cas = explode("/", $url); // On sépare la première partie de l'url des 2 autres
  81. $idb = $cas[4]; // On récupère la partie qui nous intéressent
  82. $bp = explode("_", $idb); // On sépare l'identifiant du reste
  83. $id = $bp[0]; // On récupère l'identifiant
  84. $this->setIdentif($id); // ajoute l’identifiant à l’attribut identif
  85. $this->setType('dailymotion'); // signale qu’il s’agit d’une video dailymotion et l’inscrit dans l’attribut $type
  86. }
  87. private function vimeoId($url): void
  88. {
  89. $tableaux = explode("/", $url); // on découpe l’url grâce au « / »
  90. $id = $tableaux[count($tableaux)-1]; // on reticent la dernière partie qui contient l’identifiant
  91. $this->setIdentif($id); // ajoute l’identifiant à l’attribut identif
  92. $this->setType('vimeo'); // signale qu’il s’agit d’une video vimeo et l’inscrit dans l’attribut $type
  93. }
  94. #[ORM\PrePersist()] // Les trois événement suivant s’exécute avant que l’entité soit enregistée
  95. #[ORM\PreUpdate()]
  96. #[ORM\PreFlush()]
  97. public function extractIdentif(): void
  98. {
  99. $url = $this->getUrl(); // on récupère l’url
  100. if (preg_match("#^(http|https)://video.fdlibre.eu/#", $url)) // Si c'est peertube fdlibre
  101. {
  102. $this->peertubeId($url);
  103. }
  104. if (preg_match("#^(http|https)://www.youtube.com/#", $url)) // Si c’est une url Youtube on execute la fonction correspondante
  105. {
  106. $this->youtubeId($url);
  107. }
  108. if (preg_match("#^(http|https)://youtu.be/#", $url)) // Si c’est une url Youtube on execute la fonction correspondante
  109. {
  110. $this->youtubeCourteId($url);
  111. }
  112. else if((preg_match("#^(http|https)://www.dailymotion.com/#", $url))) // Si c’est une url Dailymotion on execute la fonction correspondante
  113. {
  114. $this->dailymotionId($url);
  115. }
  116. else if((preg_match("#^(http|https)://vimeo.com/#", $url))) // Si c’est une url Vimeo on execute la fonction correspondante
  117. {
  118. $this->vimeoId($url);
  119. }
  120. }
  121. private function embedUrl()
  122. {
  123. $control = $this->getType(); // on récupère le type de la vidéo
  124. $id = strip_tags($this->getIdentif()); // on récupère son identifiant
  125. if ($control == 'youtube') {
  126. $embed = "https://www.youtube-nocookie.com/embed/" . $id;
  127. return $embed;
  128. } else if ($control == 'dailymotion') {
  129. $embed = "https://www.dailymotion.com/embed/video/" . $id;
  130. return $embed;
  131. } else if ($control == 'vimeo') {
  132. $embed = "https://player.vimeo.com/video/" . $id;
  133. return $embed;
  134. } else if ($control == 'peertube') {
  135. $embed = "https://video.fdlibre.eu/videos/embed/" . $id;
  136. return $embed;
  137. }
  138. }
  139. private function url()
  140. {
  141. $control = $this->getType(); // on récupère le type de la vidéo
  142. $id = strip_tags($this->getIdentif()); // on récupère son identifiant
  143. if ($control == 'youtube') {
  144. $embed = "https://www.youtube.com/watch?v=" . $id;
  145. return $embed;
  146. } else if ($control == 'dailymotion') {
  147. $embed = "https://www.dailymotion.com/video/" . $id;
  148. return $embed;
  149. } else if ($control == 'vimeo') {
  150. $embed = "https://vimeo.com/" . $id;
  151. return $embed;
  152. } else if ($control == 'peertube') {
  153. $embed = "https://video.fdlibre.eu/videos/watch/" . $id;
  154. return $embed;
  155. }
  156. }
  157. public function image()
  158. {
  159. $control = $this->getType(); // on récupère le type de la vidéo
  160. $id = strip_tags($this->getIdentif()); // on récupère son identifiant
  161. if ($control == 'youtube') {
  162. $image = 'https://img.youtube.com/vi/' . $id . '/hqdefault.jpg';
  163. return $image;
  164. } else if ($control == 'dailymotion') {
  165. $image = 'https://www.dailymotion.com/thumbnail/150x120/video/' . $id . '';
  166. return $image;
  167. } else if ($control == 'vimeo') {
  168. $hash = unserialize(file_get_contents("https://vimeo.com/api/v2/video/" . $id . ".php"));
  169. $image = $hash[0]['thumbnail_small'];
  170. return $image;
  171. }
  172. }
  173. public function video()
  174. {
  175. $video = "<iframe src='".$this->embedUrl()."' allowfullscreen></iframe>";
  176. return $video;
  177. }
  178. }