PageRenderTime 38ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 1ms

/Model/Video.php

https://github.com/Wargo/reddevil
PHP | 248 lines | 182 code | 63 blank | 3 comment | 21 complexity | 5441212fd0ac08c4de933c8d052185a6 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1, GPL-3.0
  1. <?php
  2. class Video extends AppModel {
  3. var $limit = 3;
  4. var $domain = 'http://www.reddevilx.com';
  5. function findMore($page, $conditions) {
  6. return $this->find('all', array(
  7. 'conditions' => $conditions,
  8. 'limit' => 12,
  9. 'offset' => $this->limit * $page,
  10. 'order' => array('published' => 'desc')
  11. ));
  12. }
  13. function getVideos($page, $params = array(), $limit = false) {
  14. if ($limit == false) {
  15. $limit = $this->limit;
  16. }
  17. $ids = $conditions = array();
  18. foreach ($params as $key => $slug) {
  19. $id = ClassRegistry::init(ucwords($key))->field('id', compact('slug'));
  20. $new_ids = ClassRegistry::init('VideoRelationship')->find('list', array(
  21. 'conditions' => array(
  22. 'model' => ucwords($key),
  23. 'foreign_id' => $id
  24. ),
  25. 'fields' => array('id', 'video_id'),
  26. ));
  27. $ids = array_merge($ids, $new_ids);
  28. }
  29. if (count($ids)) {
  30. $conditions['id'] = $ids;
  31. }
  32. if (Configure::read('debug')) {
  33. $conditions['published <='] = date('Y-m-d H:i:s');
  34. $conditions['active'] = 1;
  35. $conditions['site'] = 'reddevilx';
  36. } else {
  37. $conditions['published <='] = date('Y-m-d H:i:s');
  38. $conditions['active'] = 1;
  39. $conditions['site'] = 'reddevilx';
  40. }
  41. $count = $this->find('count', compact('conditions'));
  42. $pageCount = ceil($count / $limit);
  43. $order = array('published' => 'desc');
  44. return array($conditions, $pageCount, $this->find('all', compact('conditions', 'limit', 'page', 'order')));
  45. }
  46. public function addFile($data) {
  47. $file = $data['Video']['file'];
  48. if ($data['Video']['video_type'] == 0) {
  49. //Añadir como nuevo video
  50. $this->create();
  51. $this->save(array('title' => $file));
  52. $id = $this->id;
  53. } else {
  54. $id = $data['Video']['id'];
  55. }
  56. $orig = APP . 'raw' . DS . $file;
  57. $movie = new ffmpeg_movie($orig);
  58. if ($data['Video']['mode'] == 'trailer') {
  59. $dest = Configure::read('TrailerUploadFolder') . $id . '.mp4';
  60. $imageFolder = Configure::read('TrailerImageFolder');
  61. $formats = serialize(array('Trailer' => array('mp4' => true)));
  62. $data = array('has_trailer' => 1, 'trailer_duration' => $movie->getDuration(), 'formats' => $formats);
  63. } else {
  64. $dest = Configure::read('VideoUploadFolder') . $id . '.mp4';
  65. $imageFolder = Configure::read('VideoImageFolder');
  66. $formats = serialize(array('Video' => array('mp4' => true)));
  67. $data = array('has_video' => 1, 'duration' => $movie->getDuration(), 'formats' => $formats);
  68. }
  69. if (Configure::read('GenerateScreenshots')) {
  70. $duration = $movie->getDuration();
  71. $framerate = $movie->getFrameRate();
  72. $step = round (($duration*$framerate)/7);
  73. for ($i = 1; $i <= 6; $i ++) {
  74. $frame = $i * $step;
  75. $frame = $movie->getFrame($frame);
  76. $image = $frame->toGDImage();
  77. imagejpeg($image, $imageFolder . $id . '-' . $i . '.jpg');
  78. }
  79. }
  80. $orig = escapeshellarg($orig);
  81. exec("mv $orig $dest");
  82. if (file_exists($orig)) {
  83. return false;
  84. }
  85. $this->id = $id;
  86. return $this->save($data);
  87. }
  88. function isPrivate($video_id, $session) {
  89. if (empty($session['video_' . $video_id]) || $session['video_' . $video_id] < date('Y-m-d H:i:s', mktime(date('H'),date('i'),date('s'),date('m'),date('d') - 1, date('Y')))) {
  90. return true;
  91. } else {
  92. return false;
  93. }
  94. }
  95. function getSlug($video_id) {
  96. return $this->field('slug', array('id' => $video_id));
  97. }
  98. function getTitle($Video) {
  99. $actors = ClassRegistry::init('VideoRelationship')->getActors($Video['id']);
  100. $actors = Set::extract('/Actor/name', $actors);
  101. if (count($actors) > 1) {
  102. $last = array_pop($actors);
  103. return sprintf(__('%s y %s en %s'), implode(', ', $actors), $last, $Video['title']);
  104. } else {
  105. return sprintf(__('%s en %s'), implode(', ', $actors), $Video['title']);
  106. }
  107. }
  108. function title2url($title) {
  109. $title = str_replace('-', ' ', $title);
  110. $title = explode(' ', $title);
  111. $aux = array();
  112. foreach($title as $t) {
  113. if($t !== '') {
  114. $aux[] = trim($t);
  115. }
  116. }
  117. $title = implode('-', $aux);
  118. $original = array('á', 'é', 'í', 'ó', 'ú', 'ý', 'Á', 'É', 'Í', 'Ó', 'Ú', 'Ý', 'à', 'è', 'ì', 'ò', 'ù', 'À', 'È', 'Ì', 'Ò', 'Ù',
  119. 'â', 'ê', 'î', 'ô', 'û', 'Â', 'Ê', 'Î', 'Ô', 'Û', 'ñ', 'Ñ', 'ç', 'Ç',
  120. );
  121. $replace = array('a', 'e', 'i', 'o', 'u', 'y', 'A', 'E', 'I', 'O', 'U', 'Y', 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U',
  122. 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U', 'n', 'N', 'c', 'C',
  123. );
  124. $title = str_replace($original, $replace, $title);
  125. $title = ereg_replace("[^A-Za-z0-9\-]", "", $title);
  126. return $title = strtolower($title);
  127. }
  128. function generateSitemap() {
  129. $this->xml = new File(WWW_ROOT . 'sitemap.xml');
  130. if ($this->xml->exists()) {
  131. //$this->xml->delete();
  132. unlink(WWW_ROOT . 'sitemap.xml');
  133. }
  134. $this->xml->append('<?xml version="1.0" encoding="UTF-8"?>'."\r\n");
  135. $this->xml->append('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'."\r\n");
  136. $conditions = array('active' => 1);
  137. $videos = $this->find('all', compact('conditions'));
  138. foreach ($videos as $video) {
  139. extract($video);
  140. $this->xml->append("\t".'<url>'."\r\n");
  141. $this->xml->append("\t\t".'<loc>' . $this->domain . Router::url(array('controller' => 'videos', 'action' => 'view', $Video['slug'])) . '</loc>' . "\r\n");
  142. $this->xml->append("\t\t".'<lastmod>'.date('c').'</lastmod>'."\r\n");
  143. $this->xml->append("\t".'</url>'."\r\n");
  144. $this->xml->append("\t".'<url>'."\r\n");
  145. $this->xml->append("\t\t".'<loc>' . $this->domain . Router::url(array('controller' => 'videos', 'action' => 'view_photos', $Video['slug'])) . '</loc>' . "\r\n");
  146. $this->xml->append("\t\t".'<lastmod>'.date('c').'</lastmod>'."\r\n");
  147. $this->xml->append("\t".'</url>'."\r\n");
  148. }
  149. $actors = ClassRegistry::init('Actor')->find('all');
  150. foreach ($actors as $actor) {
  151. extract($actor);
  152. $this->xml->append("\t".'<url>'."\r\n");
  153. $this->xml->append("\t\t".'<loc>' . $this->domain . Router::url(array('controller' => 'videos', 'action' => 'home', 'actor' => $Actor['slug'], 'gender' => $Actor['gender'], 'page' => 1)) . '</loc>' . "\r\n");
  154. $this->xml->append("\t\t".'<lastmod>'.date('c').'</lastmod>'."\r\n");
  155. $this->xml->append("\t".'</url>'."\r\n");
  156. $this->xml->append("\t".'<url>'."\r\n");
  157. $this->xml->append("\t\t".'<loc>' . $this->domain . Router::url(array('controller' => 'photos', 'action' => 'view', 'actor' => $Actor['slug'], 'page' => 1)) . '</loc>' . "\r\n");
  158. $this->xml->append("\t\t".'<lastmod>'.date('c').'</lastmod>'."\r\n");
  159. $this->xml->append("\t".'</url>'."\r\n");
  160. }
  161. $categories = ClassRegistry::init('Category')->find('all');
  162. foreach ($categories as $category) {
  163. extract($category);
  164. $this->xml->append("\t".'<url>'."\r\n");
  165. $this->xml->append("\t\t".'<loc>' . $this->domain . Router::url(array('controller' => 'videos', 'action' => 'home', 'category' => $Category['slug'], 'page' => 1)) . '</loc>' . "\r\n");
  166. $this->xml->append("\t\t".'<lastmod>'.date('c').'</lastmod>'."\r\n");
  167. $this->xml->append("\t".'</url>'."\r\n");
  168. }
  169. // Webcams
  170. $url_filters = 'http://modelocam.com/spa/filters/get_list.json';
  171. $url = 'http://modelocam.com/spa/rooms/get_list/80.json';
  172. $ch = curl_init($url);
  173. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  174. $cams = json_decode(curl_exec($ch));
  175. $cams = $cams->data;
  176. foreach ($cams as $cam) {
  177. $url = array('controller' => 'cams', 'action' => 'view', $cam->Room->nick);
  178. $this->xml->append("\t".'<url>'."\r\n");
  179. $this->xml->append("\t\t".'<loc>' . $this->domain . Router::url($url) . '</loc>' . "\r\n");
  180. $this->xml->append("\t\t".'<lastmod>'.date('c').'</lastmod>'."\r\n");
  181. $this->xml->append("\t".'</url>'."\r\n");
  182. }
  183. $this->xml->append('</urlset>');
  184. }
  185. }