Source of file yt.php
Size: 4,623 Bytes - Last Modified: 2018-03-10T19:50:15+01:00
C:/xampp/htdocs/PodTube/src/yt.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 | <?php namespace AudioDidact; use AudioDidact\DB\DAL; use AudioDidact\SupportedSites\SupportedSite; use RecursiveDirectoryIterator; use RecursiveIteratorIterator; require_once __DIR__ . "/header.php"; // Set some important constants/ini ignore_user_abort(true); ob_implicit_flush(true); /* * Make sure user is logged in, set user variable to the session user. */ if(GlobalFunctions::userIsLoggedIn()){ /** @var \AudioDidact\User $user */ $user = $_SESSION["user"]; if(!$user->isEmailVerified() && EMAIL_ENABLED){ echo json_encode(['stage' => -1, 'error' => "Must verify email first!", 'progress' => 0]); exit(1); } } else{ echo json_encode(['stage' => -1, 'error' => "Must be logged in to continue!", 'progress' => 0]); exit(1); } // Write session to file to prevent concurrency issues session_write_close(); $dal = GlobalFunctions::getDAL(); // If a video is being requested, then add the video, otherwise just show the current feed if(GlobalFunctions::fullVerifyCSRF() && isset($_GET["yt"])){ $url = $_GET["yt"]; $isVideo = false; if(isset($_GET["videoOnly"]) && $_GET["videoOnly"] == "true"){ $isVideo = true; } // Try to download all the files, but if an error occurs, do not add the video to the feed try{ $download = getSupportedSiteClass($url, $isVideo); downloadAllAndUpdateDB($download, $user, $dal); } catch(\Exception $e){ SupportedSite::echoErrorJSON($e->getMessage()); exit(); } // Before we make the feed, check that every file is downloaded checkFilesExist($dal, $user); PodTube::makeFullFeed($user, $dal); } // If there is no URL set, then just recreate a feed from the existing items in the CSV else{ // Before we make the feed, check that every file is downloaded checkFilesExist($dal, $user); PodTube::makeFullFeed($user, $dal)->printFeed(); } /** * Gets the list of all feed items and makes sure that all of them are downloaded and available * * @param \AudioDidact\DB\DAL $dal * @param \AudioDidact\User $user */ function checkFilesExist(DAL $dal, User $user){ /** @var array|Video $items */ $items = $dal->getFeed($user); foreach($items as $video){ try{ if(!SupportedSite::allDownloadedVideo($video)){ $download = getSupportedSiteClass($video->getURL(), $video->isIsVideo()); downloadAllAndUpdateDB($download, $user, $dal); } } catch(\Exception $e){ error_log($e); } } } /** * Download the video if not downloaded and add or update the feed DB * * @param \AudioDidact\SupportedSites\SupportedSite $download * @param \AudioDidact\User $user * @param \AudioDidact\DB\DAL $dal */ function downloadAllAndUpdateDB(SupportedSite $download, User $user, DAL $dal){ if($download != null){ $video = $download->getVideo(); // If not all thumbnail, video, and audio are downloaded, then download them in that order if(!$download->allDownloaded()){ $download->downloadVideo(); $download->downloadThumbnail(); if(!$video->isIsVideo()){ $download->convert(); $download->applyArt(); } } if(!$dal->inFeed($video, $user)){ $dal->addVideo($video, $user); } else{ $dal->updateVideo($video, $user); } } } /** * Returns the appropriate SupportedSite to download any given content based on the URL * * @param $url * @param boolean $isVideo * @return \AudioDidact\SupportedSites\SupportedSite */ function getSupportedSiteClass($url, $isVideo){ // List all files in SupportedSites to find all the possible classes $iter = new RecursiveIteratorIterator( new RecursiveDirectoryIterator(__DIR__ . '/SupportedSites/', RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST, RecursiveIteratorIterator::CATCH_GET_CHILD // Ignore "Permission denied" ); // Make an array of all php files from the directory $supportedSitesClasses = []; foreach($iter as $path => $file){ $pathInfo = pathinfo($path); if(mb_strpos($pathInfo["extension"], "php") > -1){ $name = $pathInfo["filename"]; if(!in_array($name, $supportedSitesClasses, true)){ $supportedSitesClasses[] = $name; } } } // Check each SupportedSite to see if it supports the given URL foreach($supportedSitesClasses as $className){ $className = "\\AudioDidact\\SupportedSites\\" . $className; /** @var $className \AudioDidact\SupportedSites\SupportedSite */ if($className::supportsURL($url)){ return new $className($url, $isVideo); } } echo json_encode(['stage' => -1, 'error' => "Could not find a class to download from that URL.", 'progress' => 0]); error_log("Unable to find class for URL " . $url); // Error case or manually uploaded content case return null; } |