/Controller/VideosController.php

https://github.com/Wargo/reddevil · PHP · 545 lines · 401 code · 136 blank · 8 comment · 61 complexity · 6fd166a4ee6891beffc7df80ced6d547 MD5 · raw file

  1. <?php
  2. class VideosController extends AppController {
  3. var $paginate = array();
  4. function beforeFilter() {
  5. parent::beforeFilter();
  6. $cookies = $this->Cookie->read();
  7. $this->set(compact('cookies'));
  8. }
  9. function home() {
  10. if (!empty($this->params['page'])) {
  11. $page = $this->params['page'];
  12. } else {
  13. $page = !empty($this->params['named']['page']) ? $this->params['named']['page'] : 1;
  14. }
  15. $params = array(
  16. 'actor' => $this->params['actor'],
  17. 'category' => $this->params['category'],
  18. );
  19. if ($params['actor']) {
  20. extract(ClassRegistry::init('Actor')->find('first', array(
  21. 'conditions' => array(
  22. 'slug' => $params['actor']
  23. )
  24. )));
  25. $title_for_layout = $Actor['name']; //sprintf(__('Vídeos de %s'), $Actor['name']);
  26. $description_for_layout = strip_tags($Actor['description']);
  27. $keywords_for_layout = $Actor['name'];
  28. if ($page > 1) {
  29. $title_for_layout .= ' - ' . $page . 'ª ' . __('página');
  30. }
  31. $this->set(compact('description_for_layout', 'keywords_for_layout'));
  32. } elseif ($params['category']) {
  33. $title_for_layout = sprintf(__('Vídeos sobre %s'), ClassRegistry::init('Category')->field('name', array(
  34. 'slug' => $params['category']
  35. )));
  36. } else {
  37. $title_for_layout = __('Videos Reddevilx');
  38. }
  39. list($conditions, $pageCount, $videos) = $this->Video->getVideos($page, $params);
  40. $this->set(compact('videos', 'conditions', 'page', 'pageCount', 'title_for_layout'));
  41. }
  42. function view($id = null) {
  43. if (empty($id)) {
  44. return $this->redirect('/');
  45. }
  46. // TEMPORAL - no borrar
  47. if ($id == 'pablo-ferrari-y-samantha-pink-en-samantha-pink') {
  48. $id = 'pablo-ferrari-y-samantha-pink-en-number-one';
  49. return $this->redirect(array('controller' => 'videos', 'action' => 'view', $id), 301);
  50. }
  51. if ($id == 'pablo-ferrari-y-erica-fontes-en-golosa-busca-polla-sabrosa') {
  52. $id = 'pablo-ferrari-y-erica-fontes-en-good-morning-erica';
  53. return $this->redirect(array('controller' => 'videos', 'action' => 'view', $id), 301);
  54. }
  55. if ($this->Session->read('Auth.User.caducidad') > date('Y-m-d H:i:s')) {
  56. if (!$this->params['isAjax']) {
  57. return $this->redirect(array('controller' => 'videos', 'action' => 'view_video', $id));
  58. }
  59. }
  60. $section = 'trailer';
  61. if (!$video = $this->Video->findById($id)) {
  62. $video = $this->Video->findBySlug($id);
  63. }
  64. extract($video);
  65. $title_for_layout = $this->Video->getTitle($Video);
  66. $description_for_layout = $keywords_for_layout = $Video['description'];
  67. $main = ClassRegistry::init('Photo')->find('first', array(
  68. 'conditions' => array(
  69. 'video_id' => $Video['id'],
  70. 'main' => 1
  71. ),
  72. ));
  73. $this->set(compact('Video', 'main', 'section', 'layout_title', 'title_for_layout', 'description_for_layout', 'keywords_for_layout'));
  74. if ($this->request->is('ajax')) {
  75. $this->layout = 'ajax';
  76. $this->render('/Elements/Videos/player');
  77. } else {
  78. $this->render('view');
  79. }
  80. }
  81. function view_video($id = null) {
  82. if (empty($id)) {
  83. return $this->redirect('/');
  84. }
  85. if ($id == 'pablo-ferrari-y-samantha-pink-en-samantha-pink') {
  86. $id = 'pablo-ferrari-y-samantha-pink-en-number-one';
  87. return $this->redirect(array('controller' => 'videos', 'action' => 'view_video', $id), 301);
  88. }
  89. if ($id == 'pablo-ferrari-y-erica-fontes-en-golosa-busca-polla-sabrosa') {
  90. $id = 'pablo-ferrari-y-erica-fontes-en-good-morning-erica';
  91. return $this->redirect(array('controller' => 'videos', 'action' => 'view_video', $id), 301);
  92. }
  93. $total_seconds = 90;
  94. $section = 'video';
  95. if (!$video = $this->Video->findById($id)) {
  96. $video = $this->Video->findBySlug($id);
  97. }
  98. extract($video);
  99. $this->Session->write('current_video_id', $Video['id']);
  100. $main = ClassRegistry::init('Photo')->find('first', array(
  101. 'conditions' => array(
  102. 'video_id' => $Video['id'],
  103. 'main_video' => 1
  104. ),
  105. ));
  106. if (!$main) {
  107. $main = ClassRegistry::init('Photo')->find('first', array(
  108. 'conditions' => array(
  109. 'video_id' => $Video['id'],
  110. 'main' => 1
  111. ),
  112. ));
  113. }
  114. $title_for_layout = $this->Video->getTitle($Video);
  115. $this->set(compact('Video', 'main', 'section', 'user', 'phone', 'text', 'sms', 'total_seconds', 'title_for_layout'));
  116. if ($this->Auth->user('caducidad') > date('Y-m-d H:i:s')) {
  117. $this->validateAccess();
  118. }
  119. if ($Video['site'] == 'glassman') {
  120. $this->layout = 'glassman';
  121. return $this->render('glassman');
  122. }
  123. if ($this->request->is('ajax')) {
  124. $this->layout = 'ajax';
  125. $this->render('/Elements/Videos/video');
  126. } else {
  127. $this->render('view');
  128. }
  129. }
  130. function view_photos($id = null, $photo_id = null) {
  131. if (empty($id)) {
  132. return $this->redirect('/');
  133. }
  134. $section = 'photos';
  135. if (!$video = $this->Video->findById($id)) {
  136. $video = $this->Video->findBySlug($id);
  137. }
  138. extract($video);
  139. $title_for_layout = $this->Video->getTitle($Video);
  140. $this->set(compact('Video', 'section', 'photo_id', 'title_for_layout'));
  141. if ($this->request->is('ajax')) {
  142. $this->layout = 'ajax';
  143. $this->render('/Elements/Videos/photos');
  144. } else {
  145. $this->render('view');
  146. }
  147. }
  148. function search() {
  149. if ($this->request->data) {
  150. return $this->redirect(array(
  151. 'controller' => 'videos', 'action' => 'search', 'search' => $this->request->data['Video']['search'], 'page' => 1
  152. ));
  153. }
  154. $search = $this->params['search'];
  155. $this->request->data['Video']['search'] = $search;
  156. if (!empty($this->params['page'])) {
  157. $page = $this->params['page'];
  158. } else {
  159. $page = !empty($this->params['named']['page']) ? $this->params['named']['page'] : 1;
  160. }
  161. $conditions = array(
  162. 'active' => 1,
  163. 'published <=' => date('Y-m-d H:i:s'),
  164. 'site' => 'reddevilx',
  165. );
  166. $conditions['or'] = array(
  167. 'title like' => '%' . $search . '%',
  168. 'description like' => '%' . $search . '%',
  169. );
  170. $this->paginate['limit'] = 3;
  171. $this->paginate['page'] = $page;
  172. $this->paginate['order'] = array('published' => 'desc');
  173. $videos = $this->paginate('Video', $conditions);
  174. $pageCount = $this->params['paging']['Video']['pageCount'];
  175. $title_for_layout = sprintf(__('Buscando %s en RedDevilX'), $search);
  176. $this->set(compact('videos', 'conditions', 'page', 'pageCount', 'title_for_layout'));
  177. $this->render('home');
  178. }
  179. function admin_index() {
  180. $videos = $this->Video->find('all', array('order' => array('published' => 'desc')));
  181. $this->set(compact('videos'));
  182. }
  183. function admin_edit($id = null) {
  184. $this->loadModel('VideoRelationship');
  185. if ($this->request->data) {
  186. if ($id) {
  187. $this->Video->id = $id;
  188. } else {
  189. $this->Video->create();
  190. }
  191. $this->Video->save($this->request->data);
  192. if (!$id) {
  193. $id = $this->Video->id;
  194. }
  195. if ($categories = $this->request->data['Category']) {
  196. $this->VideoRelationship->deleteAll(array('video_id' => $id, 'model' => 'Category'));
  197. foreach ($categories as $key => $value) {
  198. if ($value) {
  199. $this->VideoRelationship->create();
  200. $this->VideoRelationship->save(array(
  201. 'video_id' => $id,
  202. 'model' => 'Category',
  203. 'foreign_id' => $key,
  204. ));
  205. }
  206. }
  207. }
  208. if ($actors = $this->request->data['Actor']) {
  209. $this->VideoRelationship->deleteAll(array('video_id' => $id, 'model' => 'Actor'));
  210. foreach ($actors as $key => $value) {
  211. if ($value) {
  212. $this->VideoRelationship->create();
  213. $this->VideoRelationship->save(array(
  214. 'video_id' => $id,
  215. 'model' => 'Actor',
  216. 'foreign_id' => $key,
  217. ));
  218. }
  219. }
  220. }
  221. $all_actors = ClassRegistry::init('VideoRelationship')->getActors($id);
  222. $actors = Set::extract('/Actor/slug', $all_actors);
  223. if (count($actors) > 1) {
  224. $last = array_pop($actors);
  225. $layout_title = sprintf(__('%s y %s en %s'), implode(', ', $actors), $last, $this->request->data['Video']['title']);
  226. } elseif (count($actors) == 0) {
  227. $layout_title = $this->request->data['Video']['title'];
  228. } else {
  229. $layout_title = sprintf(__('%s en %s'), implode(', ', $actors), $this->request->data['Video']['title']);
  230. }
  231. $categories = ClassRegistry::init('VideoRelationship')->getCategories($id);
  232. $all_actors = Set::extract('/Actor/name', $all_actors);
  233. $this->Video->save(array(
  234. 'slug' => $this->Video->title2url($layout_title),
  235. 'description' => implode(' ', $all_actors) . ' ' . implode(' ', $categories)
  236. ));
  237. return $this->redirect('index');
  238. }
  239. if ($id) {
  240. $this->request->data = $this->Video->findById($id);
  241. $myCat = $this->VideoRelationship->find('all', array(
  242. 'conditions' => array(
  243. 'video_id' => $id,
  244. 'model' => 'Category'
  245. )
  246. ));
  247. $myCat = Set::extract('/VideoRelationship/foreign_id', $myCat);
  248. $myAct = $this->VideoRelationship->find('all', array(
  249. 'conditions' => array(
  250. 'video_id' => $id,
  251. 'model' => 'Actor'
  252. )
  253. ));
  254. $myAct = Set::extract('/VideoRelationship/foreign_id', $myAct);
  255. } else {
  256. $myAct = $myCat = array();
  257. }
  258. $this->loadModel('Category');
  259. $categories = $this->Category->find('all');
  260. $this->loadModel('Actor');
  261. $actors = $this->Actor->find('all');
  262. $this->set(compact('id', 'categories', 'actors', 'myCat', 'myAct'));
  263. }
  264. public function admin_delete($id = null) {
  265. if ($id) {
  266. $this->Video->delete($id);
  267. }
  268. return $this->redirect('index');
  269. }
  270. public function admin_add_file() {
  271. if (!empty($this->request->data)) {
  272. if ($this->Video->addFile($this->request->data)) {
  273. $this->Session->setFlash(__('Video añadido'));
  274. $this->redirect(array('controller' => 'videos', 'action' => 'edit', $this->Video->id));
  275. } else {
  276. $this->Session->setFlash(__('Error al añadir el video'));
  277. $this->redirect(array('controller' => 'archivos', 'action' => 'index'));
  278. }
  279. }
  280. $videos = $this->Video->find('list');
  281. $this->set(compact('videos'));
  282. $this->set($this->params['named']);
  283. }
  284. function check_phone() {
  285. $this->layout = 'ajax';
  286. if (is_numeric($this->Session->read('phone'))) {
  287. $ch = curl_init('http://flashaccess2008.micropagos.net:8080/c2enopin/servlet/Control?cid=' . Configure::read('CID') . '&uid=' . $this->Cookie->read('user') . '&service=' . $this->Session->read('phone'));
  288. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  289. $result = curl_exec($ch);
  290. $access = false;
  291. //if (Configure::read('debug') || substr($result, 0, 2) === 'OK') {
  292. if (substr($result, 0, 2) === 'OK') {
  293. //$this->validateAccess();
  294. $current = $this->Session->read('current_video_id');
  295. $this->Cookie->write('video_' . $current, date('Y-m-d H:i:s'));
  296. $access = true;
  297. }
  298. $this->set(compact('result', 'access'));
  299. } else {
  300. $this->autoRender = false;
  301. }
  302. }
  303. function check_sms() {
  304. $this->layout = 'ajax';
  305. //if (is_numeric($this->Session->read('phone'))) {
  306. $url = 'http://213.27.137.219:8080/SMSGateway/SmsGateway2FlashIn?cid=' . Configure::read('CID_m') . '&uid=' . $this->Auth->user('id') . '&control=' . Configure::read('pass_m') . '&peticion=NO';
  307. $ch = curl_init($url);
  308. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  309. $result = curl_exec($ch);
  310. $file = fopen('sms.txt', 'a');
  311. fwrite($file, "\r\n\r\n");
  312. fwrite($file, "\r\n" . $url . "\r\n");
  313. fwrite($file, "\r\n" . $result . "\r\n");
  314. fwrite($file, "\r\n" . date('Y-m-d H:i:s') . "\r\n");
  315. fwrite($file, "\r\n\r\n");
  316. fclose($file);
  317. $access = false;
  318. if (substr($result, 0, 2) === 'OK') {
  319. $this->loadModel('User');
  320. $this->User->id = $this->Auth->user('id');
  321. $date = date('Y-m-d H:i:s', strtotime("+7 day"));
  322. $this->User->save(array('caducidad' => $date));
  323. $this->Session->write('Auth.User.caducidad', $date);
  324. $access = true;
  325. }
  326. $this->set(compact('result', 'access'));
  327. //} else {
  328. //$this->autoRender = false;
  329. //}
  330. }
  331. function validateAccess() {
  332. exec('mkdir links/' . $this->Auth->user('id'));
  333. //$current = $this->Session->read('current_video_id');
  334. $videos = $this->Video->find('all');
  335. foreach ($videos as $video) {
  336. $current = $video['Video']['id'];
  337. $link = Security::hash($this->Auth->user('id') . '_' . $current, null, true);
  338. $formats = Configure::read('formats');
  339. exec('ln -s ../../../uploads/Video/mp4/l/' . $current . '.mp4 links/' . $this->Auth->user('id') . '/' . $link . '_mp4_l.mp4');
  340. foreach ($formats as $format) {
  341. foreach ($format['sizes'] as $size) {
  342. exec('ln -s ../../../uploads/Video/' . $format['folder'] . '/' . $size . '/' . $current . '.' . $format['folder'] . ' links/' . $this->Auth->user('id') . '/' . $link . '_' . $format['folder'] . '_' . $size . '.' . $format['folder']);
  343. }
  344. }
  345. }
  346. }
  347. function download($id = null) {
  348. $this->layout = 'ajax';
  349. if (!$id) {
  350. return false;
  351. }
  352. extract($this->Video->find('first', array(
  353. 'conditions' => array(
  354. 'id' => $id,
  355. 'Video.active' => 1,
  356. ),
  357. )));
  358. $this->set(compact('Video'));
  359. }
  360. function share($id = null) {
  361. $this->layout = 'ajax';
  362. if (!$id) {
  363. return false;
  364. }
  365. extract($this->Video->find('first', array(
  366. 'conditions' => array(
  367. 'id' => $id,
  368. 'Video.active' => 1,
  369. ),
  370. )));
  371. $this->set(compact('Video'));
  372. }
  373. function formats($id = null, $type = null) {
  374. $this->layout = 'ajax';
  375. if (!$id || !$type) {
  376. return false;
  377. }
  378. extract($this->Video->findById($id));
  379. $this->set(compact('Video', 'type'));
  380. }
  381. function external($id = null) {
  382. $this->layout = 'external';
  383. if (!$id) {
  384. return false;
  385. }
  386. extract($this->Video->findById($id));
  387. $main = ClassRegistry::init('Photo')->find('first', array(
  388. 'conditions' => array(
  389. 'video_id' => $Video['id'],
  390. 'main' => 1
  391. ),
  392. ));
  393. $this->set(compact('Video', 'main'));
  394. }
  395. function sitemap() {
  396. $this->Video->generateSitemap();
  397. $this->autoRender = false;
  398. }
  399. function whatsapp($id = null) {
  400. if (!$id) {
  401. return $this->redirect('home');
  402. }
  403. $dir = WWW_ROOT . 'pills' . DS . $id;
  404. $videos = scandir($dir);
  405. $this->set(compact('videos', 'id'));
  406. }
  407. function sites() {
  408. }
  409. }