imdb.class.php.txt 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504
  1. <?php
  2. /**
  3. * PHP IMDb.com Grabber
  4. *
  5. * This PHP library enables you to scrape data from IMDB.com.
  6. *
  7. *
  8. * If you want to thank me for this library, please buy me something at Amazon
  9. * (https://www.amazon.de/hz/wishlist/ls/8840JITISN9L/) or use
  10. * https://www.paypal.me/FabianBeiner. Thank you! 🙌
  11. *
  12. * @author Fabian Beiner <fb@fabianbeiner.de>
  13. * @license https://opensource.org/licenses/MIT The MIT License
  14. * @link https://github.com/FabianBeiner/PHP-IMDB-Grabber/ GitHub Repository
  15. * @version 6.2.0
  16. */
  17. class IMDB
  18. {
  19. /**
  20. * Set this to true if you run into problems.
  21. */
  22. const IMDB_DEBUG = false;
  23. /**
  24. * Set the preferred language for the User Agent.
  25. */
  26. const IMDB_LANG = 'en-US,en;q=0.9';
  27. /**
  28. * Set this to true if you want to start with normal search and
  29. * if you get no result, it will use the advanced method
  30. */
  31. const IMDB_SEARCH_ORIGINAL = true;
  32. /**
  33. * Set this to true if you want to search for exact titles
  34. * it falls back to false if theres no result
  35. */
  36. const IMDB_EXACT_SEARCH = true;
  37. /**
  38. * Set the sensitivity for search results in percentage.
  39. */
  40. const IMDB_SENSITIVITY = 85;
  41. /**
  42. * Define the timeout for cURL requests.
  43. */
  44. const IMDB_TIMEOUT = 15;
  45. /**
  46. * These are the regular expressions used to extract the data.
  47. * If you don’t know what you’re doing, you shouldn’t touch them.
  48. */
  49. const IMDB_AKA = '~<td[^>]*>\s*Also\s*Known\s*As\s*</td>\s*<td>(.+)</td>~Uis';
  50. const IMDB_ASPECT_RATIO = '~<td[^>]*>Aspect\s*Ratio</td>\s*<td>(.+)</td>~Uis';
  51. const IMDB_AWARDS = '~<div\s*class="titlereference-overview-section">\s*Awards:(.+)</div>~Uis';
  52. const IMDB_BUDGET = '~<td[^>]*>Budget<\/td>\s*<td>\s*(.*)(?:\(estimated\))\s*<\/td>~Ui';
  53. const IMDB_CAST = '~<td[^>]*itemprop="actor"[^>]*>\s*<a\s*href="/name/([^/]*)/\?[^"]*"[^>]*>\s*<span.+>(.+)</span~Ui';
  54. const IMDB_CAST_IMAGE = '~(loadlate="(.*)"[^>]*><\/a>\s+<\/td>\s+)?<td[^>]*itemprop="actor"[^>]*>\s*<a\s*href="\/name\/([^/]*)\/\?[^"]*"[^>]*>\s*<span.+>(.+)<\/span+~Uis';
  55. const IMDB_CERTIFICATION = '~<td[^>]*>\s*Certification\s*</td>\s*<td>(.+)</td>~Ui';
  56. const IMDB_CHAR = '~<td class="character">(?:\s+)<div>(.*)(?:\s+)(?: /| \(.*\)|<\/div>)~Ui';
  57. const IMDB_COLOR = '~<a href="\/search\/title\?colors=(?:.*)">(.*)<\/a>~Ui';
  58. const IMDB_COMPANIES = '~production_companies&ref_=(?:.*)">Edit</a>\s+</header>\s+<ul class="simpleList">(.*)Distributors</h4>~Uis';
  59. const IMDB_COMPANY = '~<li>\s+<a href="\/company\/(co[0-9]+)\/">(.*?)</a>~';
  60. const IMDB_COUNTRY = '~<a href="/country/(\w+)">(.*)</a>~Ui';
  61. const IMDB_CREATOR = '~<div[^>]*>\s*(?:Creator|Creators)\s*:\s*<ul[^>]*>(.+)</ul>~Uxsi';
  62. const IMDB_DISTRIBUTOR = '@href="[^"]*update=[t0-9]+:distributors[^"]*">Edit</a>\s*</header>\s*<ul\s*class="simpleList">(.*):special_effects_companies@Uis';
  63. const IMDB_DISTRIBUTORS = '@\/company\/(co[0-9]+)\/">(.*?)<\/a>\s+(?:\(([0-9]+)\))?\s+(?:\((.*?)\))?\s+(?:\((.*?)\))?\s+(?:\((?:.*?)\))?\s+</li>@';
  64. const IMDB_DIRECTOR = '~<div[^>]*>\s*(?:Director|Directors)\s*:\s*<ul[^>]*>(.+)</ul>~Uxsi';
  65. const IMDB_GENRE = '~href="/genre/([a-zA-Z_-]*)/?">([a-zA-Z_ -]*)</a>~Ui';
  66. const IMDB_GROSS = '~pl-zebra-list__label">Cumulative Worldwide Gross<\/td>\s+<td>\s+(.*)\s+<~Uxsi';
  67. const IMDB_ID = '~((?:tt\d{6,})|(?:itle\?\d{6,}))~';
  68. const IMDB_LANGUAGE = '~<a href="\/language\/(\w+)">(.*)<\/a>~Ui';
  69. const IMDB_LOCATION = '~href="\/search\/title\?locations=(.*)">(.*)<\/a>~Ui';
  70. const IMDB_LOCATIONS = '~href="\/search\/title\?locations=[^>]*>\s?(.*)\s?<\/a>[^"]*<dd>\s?(.*)\s<\/dd>~Ui';
  71. const IMDB_MPAA = '~<li class="ipl-inline-list__item">(?:\s+)(TV-Y|TV-Y7|TV-G|TV-PG|TV-14|TV-MA|G|PG|PG-13|R|NC-17|NR|UR)(?:\s+)<\/li>~Ui';
  72. const IMDB_MUSIC = '~Music by\s*<\/h4>.*<table class=.*>(.*)</table>~Us';
  73. const IMDB_NAME = '~href="/name/(.+)/?(?:\?[^"]*)?"[^>]*>(.+)</a>~Ui';
  74. const IMDB_MOVIE_DESC = '~<section class="titlereference-section-overview">\s+<div>\s+(.*)\s*?</div>\s+<hr>\s+<div class="titlereference-overview-section">~Ui';
  75. const IMDB_SERIES_DESC = '~<div>\s+(?:.*?</a>\s+</span>\s+</div>\s+<hr>\s+<div>\s+)(.*)\s+</div>\s+<hr>\s+<div class="titlereference-overview-section">~Ui';
  76. const IMDB_SERIESEP_DESC = '~All Episodes(?:.*?)</li>\s+(?:.*?)?</ul>\s+</span>\s+<hr>\s+</div>\s+<div>\s+(.*?)\s+</div>\s+<hr>~';
  77. const IMDB_NOT_FOUND_ADV = '~<span>No results.</span>~Ui';
  78. const IMDB_NOT_FOUND_DES = 'Know what this is about';
  79. const IMDB_NOT_FOUND_ORG = '~<h1 class="findHeader">No results found for ~Ui';
  80. const IMDB_PLOT = '~<td[^>]*>\s*Plot\s*Summary\s*</td>\s*<td>\s*<p>\s*(.*)\s*</p>~Ui';
  81. const IMDB_PLOT_KEYWORDS = '~<td[^>]*>Plot\s*Keywords</td>\s*<td>(.+)(?:<a\s*href="/title/[^>]*>[^<]*</a>\s*</li>\s*</ul>\s*)?</td>~Ui';
  82. const IMDB_POSTER = '~<link\s*rel=\'image_src\'\s*href="(.*)">~Ui';
  83. const IMDB_RATING = '~class="ipl-rating-star__rating">(.*)<~Ui';
  84. const IMDB_RATING_COUNT = '~class="ipl-rating-star__total-votes">\((.*)\)<~Ui';
  85. const IMDB_RELEASE_DATE = '~href="/title/[t0-9]*/releaseinfo">(.*)<~Ui';
  86. const IMDB_RUNTIME = '~<td[^>]*>\s*Runtime\s*</td>\s*<td>(.+)</td>~Ui';
  87. const IMDB_SEARCH_ADV = '~text-primary">1[.]</span>\s*<a.href="\/title\/(tt\d{6,})\/(?:.*?)"(?:\s*)>(?:.*?)<\/a>~Ui';
  88. const IMDB_SEARCH_ORG = '~find-title-result">(?:.*?)alt="(.*?)"(?:.*?)href="\/title\/(tt\d{6,})\/(?:.*?)">(.*?)<\/a>~';
  89. const IMDB_SEASONS = '~episodes\?season=(?:\d+)">(\d+)<~Ui';
  90. const IMDB_SOUND_MIX = '~<td[^>]*>\s*Sound\s*Mix\s*</td>\s*<td>(.+)</td>~Ui';
  91. const IMDB_TAGLINE = '~<td[^>]*>\s*Taglines\s*</td>\s*<td>(.+)</td>~Ui';
  92. const IMDB_TITLE = '~itemprop="name">(.*)(<\/h3>|<span)~Ui';
  93. const IMDB_TITLE_EP = '~titlereference-watch-ribbon"(?:.*)itemprop="name">(.*?)\s+<span\sclass="titlereference-title-year">~Ui';
  94. const IMDB_TITLE_ORIG = '~</h3>(?:\s+)(.*)(?:\s+)<span class=\"titlereference-original-title-label~Ui';
  95. const IMDB_TOP250 = '~href="/chart/top(?:tv)?".class(?:.*?)#([0-9]{1,})</a>~Ui';
  96. const IMDB_TRAILER = '~href="/title/(?:tt\d+)/videoplayer/(vi[0-9]*)"~Ui';
  97. const IMDB_TYPE = '~href="/genre/(?:[a-zA-Z_-]*)/?">(?:[a-zA-Z_ -]*)</a>\s+</li>\s+(?:.*item">)\s+(?:<a href="(?:.*)</a>\s+</li>\s+(?:.*item">)\s+)?([a-zA-Z_ -]*)\s+</li>~Ui';
  98. const IMDB_URL = '~https?://(?:.*\.|.*)imdb.com/(?:t|T)itle(?:\?|/)(..\d+)~i';
  99. const IMDB_USER_REVIEW = '~href="/title/[t0-9]*/reviews"[^>]*>([^<]*)\s*User~Ui';
  100. const IMDB_VOTES = '~"ipl-rating-star__total-votes">\s*\((.*)\)\s*<~Ui';
  101. const IMDB_WRITER = '~<div[^>]*>\s*(?:Writer|Writers)\s*:\s*<ul[^>]*>(.+)</ul>~Ui';
  102. const IMDB_YEAR = '~og:title\' content="(?:.*)\((?:.*)(\d{4})(?:.*)\)~Ui';
  103. /**
  104. * @var string The string returned, if nothing is found.
  105. */
  106. public static $sNotFound = 'n/A';
  107. /**
  108. * @var null|int The ID of the movie.
  109. */
  110. public $iId = null;
  111. /**
  112. * @var bool Is the content ready?
  113. */
  114. public $isReady = false;
  115. /**
  116. * @var string Char that separates multiple entries.
  117. */
  118. public $sSeparator = ' / ';
  119. /**
  120. * @var null|string The URL to the movie.
  121. */
  122. public $sUrl = null;
  123. /**
  124. * @var bool Return responses enclosed in array
  125. */
  126. public $bArrayOutput = false;
  127. /**
  128. * @var int Maximum cache time.
  129. */
  130. private $iCache = 1440;
  131. /**
  132. * @var null|string The root of the script.
  133. */
  134. private $sRoot = null;
  135. /**
  136. * @var null|string Holds the source.
  137. */
  138. private $sSource = null;
  139. /**
  140. * @var string What to search for?
  141. */
  142. private $sSearchFor = 'all';
  143. /**
  144. * @param string $sSearch IMDb URL or movie title to search for.
  145. * @param null $iCache Custom cache time in minutes.
  146. * @param string $sSearchFor What to search for?
  147. *
  148. * @throws Exception
  149. */
  150. public function __construct($sSearch, $iCache = null, $sSearchFor = 'all')
  151. {
  152. $this->sRoot = dirname(__FILE__);
  153. if ( ! is_writable($this->sRoot . '/posters') && ! mkdir($this->sRoot . '/posters')) {
  154. throw new Exception('The directory “' . $this->sRoot . '/posters” isn’t writable.');
  155. }
  156. if ( ! is_writable($this->sRoot . '/cache') && ! mkdir($this->sRoot . '/cache')) {
  157. throw new Exception('The directory “' . $this->sRoot . '/cache” isn’t writable.');
  158. }
  159. if ( ! is_writable($this->sRoot . '/cast') && ! mkdir($this->sRoot . '/cast')) {
  160. throw new Exception('The directory “' . $this->sRoot . '/cast” isn’t writable.');
  161. }
  162. if ( ! function_exists('curl_init')) {
  163. throw new Exception('You need to enable the PHP cURL extension.');
  164. }
  165. if (in_array(
  166. $sSearchFor,
  167. [
  168. 'movie',
  169. 'tv',
  170. 'episode',
  171. 'game',
  172. 'documentary',
  173. 'all',
  174. ]
  175. )) {
  176. $this->sSearchFor = $sSearchFor;
  177. }
  178. if (true === self::IMDB_DEBUG) {
  179. ini_set('display_errors', 1);
  180. ini_set('display_startup_errors', 1);
  181. error_reporting(-1);
  182. echo '<pre><b>Running:</b> fetchUrl("' . $sSearch . '")</pre>';
  183. }
  184. if (null !== $iCache && (int) $iCache > 0) {
  185. $this->iCache = (int) $iCache;
  186. }
  187. if (self::IMDB_EXACT_SEARCH) {
  188. if ($this->fetchUrl($sSearch, self::IMDB_SEARCH_ORIGINAL, true)) {
  189. return true;
  190. }
  191. }
  192. if ($this->fetchUrl($sSearch, self::IMDB_SEARCH_ORIGINAL)) {
  193. return true;
  194. }
  195. if ($this->fetchUrl($sSearch, !self::IMDB_SEARCH_ORIGINAL)) {
  196. return true;
  197. }
  198. }
  199. /**
  200. * @param string $sSearch IMDb URL or movie title to search for.
  201. *
  202. * @return bool True on success, false on failure.
  203. */
  204. private function fetchUrl($sSearch, $orgSearch = false, $exactSearch = false)
  205. {
  206. $sSearch = trim($sSearch);
  207. // Try to find a valid URL.
  208. $sId = IMDBHelper::matchRegex($sSearch, self::IMDB_ID, 1);
  209. if (false !== $sId) {
  210. $this->iId = preg_replace('~[\D]~', '', $sId);
  211. $this->sUrl = 'https://www.imdb.com/title/tt' . $this->iId . '/reference';
  212. $bSearch = false;
  213. } else {
  214. if (!$orgSearch) {
  215. switch (strtolower($this->sSearchFor)) {
  216. case 'movie':
  217. $sParameters = '&title_type=feature';
  218. break;
  219. case 'tv':
  220. $sParameters = '&title_type=tv_movie,tv_series,tv_special,tv_miniseries';
  221. break;
  222. case 'episode':
  223. $sParameters = '&title_type=tv_episode';
  224. break;
  225. case 'game':
  226. $sParameters = '&title_type=video_game';
  227. break;
  228. case 'documentary':
  229. $sParameters = '&title_type=documentary';
  230. break;
  231. case 'video':
  232. $sParameters = '&title_type=video';
  233. break;
  234. default:
  235. $sParameters = '';
  236. }
  237. if (preg_match('~([^0-9+])\(?([0-9]{4})\)?~', $sSearch, $fMatch)) {
  238. $sParameters .= '&release_date=' . $fMatch[2] . '-01-01,' . $fMatch[2] . '-12-31';
  239. $sSearch = preg_replace('~([^0-9+])\(?([0-9]{4})\)?~','', $sSearch);
  240. }
  241. $this->sUrl = 'https://www.imdb.com/search/title/?title=' . rawurlencode(str_replace(' ', '+', $sSearch)) . $sParameters;
  242. } else {
  243. switch (strtolower($this->sSearchFor)) {
  244. case 'movie':
  245. $sParameters = '&s=tt&ttype=ft';
  246. break;
  247. case 'tv':
  248. $sParameters = '&s=tt&ttype=tv';
  249. break;
  250. case 'episode':
  251. $sParameters = '&s=tt&ttype=ep';
  252. break;
  253. case 'game':
  254. $sParameters = '&s=tt&ttype=vg';
  255. break;
  256. default:
  257. $sParameters = '&s=tt';
  258. }
  259. if (preg_match('~([^0-9+])\(?([0-9]{4})\)?~', $sSearch, $fMatch)) {
  260. $sYear = $fMatch[2];
  261. $sTempSearch = preg_replace('~([^0-9+])\(?([0-9]{4})\)?~','', $sSearch);
  262. $sSearch = $sTempSearch . ' (' . $sYear . ')';
  263. }
  264. if ($exactSearch) {
  265. $sParameters .= '&exact=true';
  266. }
  267. $this->sUrl = 'https://www.imdb.com/find/?q=' . rawurlencode(str_replace(' ', ' ', $sSearch)) . $sParameters;
  268. }
  269. $bSearch = true;
  270. // Was this search already performed and cached?
  271. $sRedirectFile = $this->sRoot . '/cache/' . sha1($this->sUrl) . '.redir';
  272. if (is_readable($sRedirectFile)) {
  273. if (self::IMDB_DEBUG) {
  274. echo '<pre><b>Using redirect:</b> ' . basename($sRedirectFile) . '</pre>';
  275. }
  276. $sRedirect = file_get_contents($sRedirectFile);
  277. $this->sUrl = trim($sRedirect);
  278. $this->iId = preg_replace('~[\D]~', '', IMDBHelper::matchRegex($sRedirect, self::IMDB_ID, 1));
  279. $bSearch = false;
  280. }
  281. }
  282. // Does a cache of this movie exist?
  283. if (! is_null($this->iId)) {
  284. $sCacheFile = $this->sRoot . '/cache/' . sha1($this->iId) . '.cache';
  285. if (is_readable($sCacheFile)) {
  286. $iDiff = round(abs(time() - filemtime($sCacheFile)) / 60);
  287. if ($iDiff < $this->iCache) {
  288. if (true === self::IMDB_DEBUG) {
  289. echo '<pre><b>Using cache:</b> ' . basename($sCacheFile) . '</pre>';
  290. }
  291. $this->sSource = file_get_contents($sCacheFile);
  292. $this->isReady = true;
  293. return true;
  294. }
  295. }
  296. }
  297. // Run cURL on the URL.
  298. if (true === self::IMDB_DEBUG) {
  299. echo '<pre><b>Running cURL:</b> ' . $this->sUrl . '</pre>';
  300. }
  301. $aCurlInfo = IMDBHelper::runCurl($this->sUrl);
  302. $sSource = $aCurlInfo['contents'];
  303. if (false === $sSource) {
  304. if (true === self::IMDB_DEBUG) {
  305. echo '<pre><b>cURL error:</b> ' . var_dump($aCurlInfo) . '</pre>';
  306. }
  307. return false;
  308. }
  309. if (!$orgSearch) {
  310. // Was the movie found?
  311. $sMatch = IMDBHelper::matchRegex($sSource, self::IMDB_SEARCH_ADV, 1);
  312. if (false !== $sMatch) {
  313. $sUrl = 'https://www.imdb.com/title/' . $sMatch . '/reference';
  314. if (true === self::IMDB_DEBUG) {
  315. echo '<pre><b>New redirect saved:</b> ' . basename($sRedirectFile) . ' => ' . $sUrl . '</pre>';
  316. }
  317. file_put_contents($sRedirectFile, $sUrl);
  318. $this->sSource = null;
  319. self::fetchUrl($sUrl);
  320. return true;
  321. }
  322. $sMatch = IMDBHelper::matchRegex($sSource, self::IMDB_NOT_FOUND_ADV, 0);
  323. if (false !== $sMatch) {
  324. if (true === self::IMDB_DEBUG) {
  325. echo '<pre><b>Movie not found:</b> ' . $sSearch . '</pre>';
  326. }
  327. return false;
  328. }
  329. } else {
  330. $aReturned = IMDBHelper::matchRegex($sSource, self::IMDB_SEARCH_ORG);
  331. if ($aReturned) {
  332. $rData = [];
  333. $fTempPercent = 0.00;
  334. $iTempId = "";
  335. $sYear = 0;
  336. if (preg_match('~([^0-9+])\(?([0-9]{4})\)?~', $sSearch, $fMatch)) {
  337. $sYear = $fMatch[2];
  338. $sTempSearch = preg_replace('~([^0-9+])\(?([0-9]{4})\)?~','', $sSearch);
  339. if (true === self::IMDB_DEBUG) {
  340. echo '<pre><b>YEAR:</b> ' . $sTempSearch . ' => ' . $sYear . '</pre>';
  341. }
  342. }
  343. foreach ($aReturned[1] as $i => $value) {
  344. $sId = $aReturned[2][$i];
  345. $sTitle = $aReturned[3][$i];
  346. $perc = 0.00;
  347. $year = 0;
  348. if ($sYear === 0) {
  349. $sim = similar_text($sSearch, $sTitle, $perc);
  350. } else {
  351. $sMatch = IMDBHelper::matchRegex($aReturned[1][$i], '~\(?([0-9]{4})\)?~', 1);
  352. if (false !== $sMatch) {
  353. $year = $sMatch;
  354. }
  355. if ($sYear != $year) {
  356. continue;
  357. }
  358. $sim = similar_text($sTempSearch, $sTitle, $perc);
  359. }
  360. $rData[] = [
  361. 'id' => $sId,
  362. 'title' => $sTitle,
  363. 'year' => $year,
  364. 'match' => floatval($perc)
  365. ];
  366. }
  367. if (sizeof($rData) === 0) {
  368. return false;
  369. }
  370. if (true === self::IMDB_DEBUG) {
  371. foreach ($rData as $sArray) {
  372. echo '<pre><b>Found results:</b> ' . $sArray['id'] . ' => ' . $sArray['title'] . ' (' . $sArray['match']. '%) </pre>';
  373. }
  374. }
  375. //get highest match of search results
  376. $matches = array_column($rData, 'match');
  377. $maxv = max($matches);
  378. $marray = array_filter($rData, function($item) use ($maxv) {
  379. return $item['match'] == $maxv;
  380. });
  381. $marray = reset($marray);
  382. if (sizeof($marray) > 0) {
  383. if (!$exactSearch && round($marray['match'], 0) < self::IMDB_SENSITIVITY) {
  384. echo '<pre><b>Bad sensitivity:</b> ' . $marray['id'] . ' => ' . $marray['title'] . ' (' . $marray['match']. '%) </pre>';
  385. return false;
  386. }
  387. $sUrl = 'https://www.imdb.com/title/' . $marray['id'] . '/reference';
  388. if (true === self::IMDB_DEBUG) {
  389. echo '<pre><b>Get best result:</b> ' . $marray['title'] . ' ' . $marray['id'] . ' => ' . $marray['match'] . '% </pre>';
  390. echo '<pre><b>New redirect saved:</b> ' . basename($sRedirectFile) . ' => ' . $sUrl . '</pre>';
  391. }
  392. file_put_contents($sRedirectFile, $sUrl);
  393. $this->sSource = null;
  394. self::fetchUrl($sUrl);
  395. return true;
  396. }
  397. }
  398. $sMatch = IMDBHelper::matchRegex($sSource, self::IMDB_NOT_FOUND_ORG, 0);
  399. if (false !== $sMatch) {
  400. if (true === self::IMDB_DEBUG) {
  401. echo '<pre><b>Movie not found:</b> ' . $sSearch . '</pre>';
  402. }
  403. return false;
  404. }
  405. return false;
  406. }
  407. $this->sSource = str_replace(
  408. [
  409. "\n",
  410. "\r\n",
  411. "\r",
  412. ],
  413. '',
  414. $sSource
  415. );
  416. $this->isReady = true;
  417. // Save cache.
  418. if (false === $bSearch) {
  419. if (true === self::IMDB_DEBUG) {
  420. echo '<pre><b>Cache created:</b> ' . basename($sCacheFile) . '</pre>';
  421. }
  422. file_put_contents($sCacheFile, $this->sSource);
  423. }
  424. return true;
  425. }
  426. /**
  427. * @return array All data.
  428. */
  429. public function getAll()
  430. {
  431. $aData = [];
  432. foreach (get_class_methods(__CLASS__) as $method) {
  433. if (substr($method, 0, 3) === 'get' && $method !== 'getAll' && $method !== 'getCastImages') {
  434. $aData[$method] = [
  435. 'name' => ltrim($method, 'get'),
  436. 'value' => $this->{$method}(),
  437. ];
  438. }
  439. }
  440. array_multisort($aData);
  441. return $aData;
  442. }
  443. /**
  444. * @return string “Also Known As” or $sNotFound.
  445. */
  446. public function getAka()
  447. {
  448. if (true === $this->isReady) {
  449. $aReturn = [];
  450. $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_AKA, 1);
  451. if (false !== $sMatch) {
  452. $aReturn[] = explode('|', IMDBHelper::cleanString($sMatch));
  453. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound, $aReturn);
  454. }
  455. }
  456. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound);
  457. }
  458. /**
  459. * Returns all local names
  460. *
  461. * @return string All local names.
  462. */
  463. public function getAkas()
  464. {
  465. if (true === $this->isReady) {
  466. // Does a cache of this movie exist?
  467. $sCacheFile = $this->sRoot . '/cache/' . sha1($this->iId) . '_akas.cache';
  468. $bUseCache = false;
  469. if (is_readable($sCacheFile)) {
  470. $iDiff = round(abs(time() - filemtime($sCacheFile)) / 60);
  471. if ($iDiff < $this->iCache || false) {
  472. $bUseCache = true;
  473. }
  474. }
  475. if ($bUseCache) {
  476. $aRawReturn = file_get_contents($sCacheFile);
  477. $aReturn = unserialize($aRawReturn);
  478. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound, $aReturn);
  479. } else {
  480. $fullAkas = sprintf('https://www.imdb.com/title/tt%s/releaseinfo', $this->iId);
  481. $aCurlInfo = IMDBHelper::runCurl($fullAkas);
  482. $sSource = $aCurlInfo['contents'] ?? false;
  483. if (false === $sSource) {
  484. if (true === self::IMDB_DEBUG) {
  485. echo '<pre><b>cURL error:</b> ' . var_dump($aCurlInfo) . '</pre>';
  486. }
  487. return false;
  488. }
  489. $aReturned = IMDBHelper::matchRegex($sSource, "~<td class=\"aka-item__name\">(.*?)<\/td>\s+<td class=\"aka-item__title\">(.*?)<\/td>~");
  490. if ($aReturned) {
  491. $aReturn = [];
  492. foreach ($aReturned[1] ?? [] as $i => $strName) {
  493. if (strpos($strName, '(') === false) {
  494. $aReturn[] = [
  495. 'title' => IMDBHelper::cleanString($aReturned[2][$i]),
  496. 'country' => IMDBHelper::cleanString($strName),
  497. ];
  498. }
  499. }
  500. file_put_contents($sCacheFile, serialize($aReturn));
  501. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound, $aReturn);
  502. }
  503. }
  504. }
  505. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound);
  506. }
  507. /**
  508. * Returns meta score
  509. *
  510. * @return string metascore
  511. * @return string reviews
  512. */
  513. public function getMetaScore()
  514. {
  515. if (true === $this->isReady) {
  516. // Does a cache of this movie exist?
  517. $sCacheFile = $this->sRoot . '/cache/' . sha1($this->iId) . '_metascore.cache';
  518. $bUseCache = false;
  519. if (is_readable($sCacheFile)) {
  520. $iDiff = round(abs(time() - filemtime($sCacheFile)) / 60);
  521. if ($iDiff < $this->iCache || false) {
  522. $bUseCache = true;
  523. }
  524. }
  525. if ($bUseCache) {
  526. $aRawReturn = file_get_contents($sCacheFile);
  527. $aReturn = unserialize($aRawReturn);
  528. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound, $aReturn);
  529. } else {
  530. $fullCritics = sprintf('https://www.imdb.com/title/tt%s/criticreviews', $this->iId);
  531. $aCurlInfo = IMDBHelper::runCurl($fullCritics);
  532. $sSource = $aCurlInfo['contents'] ?? false;
  533. if (false === $sSource) {
  534. if (true === self::IMDB_DEBUG) {
  535. echo '<pre><b>cURL error:</b> ' . var_dump($aCurlInfo) . '</pre>';
  536. }
  537. return false;
  538. }
  539. $aReturned = IMDBHelper::matchRegex(
  540. $sSource,
  541. '~metascore_wrap(?:.*)\s+(?:.*)\s+(?:.*)ratingValue\">([0-9]+)<\/span>(?:\s+(?:.*)){4}ratingCount\">([0-9]+)~'
  542. );
  543. if ($aReturned) {
  544. $aReturn = [];
  545. $aReturn[] = [
  546. 'metascore' => IMDBHelper::cleanString($aReturned[1][0]),
  547. 'reviews' => IMDBHelper::cleanString($aReturned[2][0]),
  548. ];
  549. file_put_contents($sCacheFile, serialize($aReturn));
  550. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound, $aReturn);
  551. }
  552. }
  553. }
  554. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound);
  555. }
  556. /**
  557. * Returns Critic Reviews based on Metascore
  558. *
  559. * @return string rating
  560. * @return string url
  561. * @return string publisher
  562. * @return string author
  563. * @return string review
  564. */
  565. public function getMetaCritics()
  566. {
  567. if (true === $this->isReady) {
  568. // Does a cache of this movie exist?
  569. $sCacheFile = $this->sRoot . '/cache/' . sha1($this->iId) . '_criticreviews.cache';
  570. $bUseCache = false;
  571. if (is_readable($sCacheFile)) {
  572. $iDiff = round(abs(time() - filemtime($sCacheFile)) / 60);
  573. if ($iDiff < $this->iCache || false) {
  574. $bUseCache = true;
  575. }
  576. }
  577. if ($bUseCache) {
  578. $aRawReturn = file_get_contents($sCacheFile);
  579. $aReturn = unserialize($aRawReturn);
  580. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound, $aReturn);
  581. } else {
  582. $fullCritics = sprintf('https://www.imdb.com/title/tt%s/criticreviews', $this->iId);
  583. $aCurlInfo = IMDBHelper::runCurl($fullCritics);
  584. $sSource = $aCurlInfo['contents'] ?? false;
  585. if (false === $sSource) {
  586. if (true === self::IMDB_DEBUG) {
  587. echo '<pre><b>cURL error:</b> ' . var_dump($aCurlInfo) . '</pre>';
  588. }
  589. return false;
  590. }
  591. $aReturned = IMDBHelper::matchRegex(
  592. $sSource,
  593. '~"ratingValue\">([0-9]+)<\/span>\s+<\/div>\s+<\/td>\s+(?:.*)\s+(?:<a\s+href=\"(.*)\"\s+)?(?:.*)\"name\">(.*)<\/span>(?:.*)\"name\">(.*)<\/span><\/span>(?:<\/a>)?\s+(?:.*)\"reviewbody\"> (.*)<\/div>~'
  594. );
  595. if ($aReturned) {
  596. $aReturn = [];
  597. foreach ($aReturned[1] as $i => $strScore) {
  598. $aReturn[] = [
  599. 'rating' => IMDBHelper::cleanString($strScore),
  600. 'url' => IMDBHelper::cleanString($aReturned[2][$i]),
  601. 'publisher' => IMDBHelper::cleanString($aReturned[3][$i]),
  602. 'author' => IMDBHelper::cleanString($aReturned[4][$i]),
  603. 'review' => IMDBHelper::cleanString($aReturned[5][$i]),
  604. ];
  605. }
  606. file_put_contents($sCacheFile, serialize($aReturn));
  607. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound, $aReturn);
  608. }
  609. }
  610. }
  611. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound);
  612. }
  613. /**
  614. * @return string “Aspect Ratio” or $sNotFound.
  615. */
  616. public function getAspectRatio()
  617. {
  618. if (true === $this->isReady) {
  619. $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_ASPECT_RATIO, 1);
  620. if (false !== $sMatch) {
  621. return IMDBHelper::cleanString($sMatch);
  622. }
  623. }
  624. return self::$sNotFound;
  625. }
  626. /**
  627. * @return string The awards of the movie or $sNotFound.
  628. */
  629. public function getAwards()
  630. {
  631. if (true === $this->isReady) {
  632. $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_AWARDS, 1);
  633. if (false !== $sMatch) {
  634. return IMDBHelper::cleanString($sMatch);
  635. }
  636. }
  637. return self::$sNotFound;
  638. }
  639. /**
  640. * @param int $iLimit How many cast members should be returned?
  641. * @param bool $bMore Add … if there are more cast members than printed.
  642. * @param string $sTarget Add a target to the links?
  643. *
  644. * @return string A list with linked cast members or $sNotFound.
  645. */
  646. public function getCastAsUrl($iLimit = 0, $bMore = true, $sTarget = '')
  647. {
  648. if (true === $this->isReady) {
  649. $aMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_CAST);
  650. $aReturn = [];
  651. if (count($aMatch[2])) {
  652. foreach ($aMatch[2] as $i => $sName) {
  653. if (0 !== $iLimit && $i >= $iLimit) {
  654. break;
  655. }
  656. $aReturn[] = '<a href="https://www.imdb.com/name/' . IMDBHelper::cleanString(
  657. $aMatch[1][$i]
  658. ) . '/"' . ($sTarget ? ' target="' . $sTarget . '"' : '') . '>' . IMDBHelper::cleanString(
  659. $sName
  660. ) . '</a>';
  661. }
  662. $bHaveMore = ($bMore && (count($aMatch[2]) > $iLimit));
  663. return IMDBHelper::arrayOutput(
  664. $this->bArrayOutput,
  665. $this->sSeparator,
  666. self::$sNotFound,
  667. $aReturn,
  668. $bHaveMore
  669. );
  670. }
  671. }
  672. return self::$sNotFound;
  673. }
  674. /**
  675. * @param int $iLimit How many cast members should be returned?
  676. * @param bool $bMore Add … if there are more cast members than printed.
  677. *
  678. * @return string A list with cast members or $sNotFound.
  679. */
  680. public function getCast($iLimit = 0, $bMore = true)
  681. {
  682. if (true === $this->isReady) {
  683. $aMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_CAST);
  684. $aReturn = [];
  685. if (count($aMatch[2])) {
  686. foreach ($aMatch[2] as $i => $sName) {
  687. if (0 !== $iLimit && $i >= $iLimit) {
  688. break;
  689. }
  690. $aReturn[] = IMDBHelper::cleanString($sName);
  691. }
  692. $bMore = (0 !== $iLimit && $bMore && (count($aMatch[2]) > $iLimit) ? '…' : '');
  693. $bHaveMore = ($bMore && (count($aMatch[2]) > $iLimit));
  694. return IMDBHelper::arrayOutput(
  695. $this->bArrayOutput,
  696. $this->sSeparator,
  697. self::$sNotFound,
  698. $aReturn,
  699. $bHaveMore
  700. );
  701. }
  702. }
  703. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound);
  704. }
  705. /**
  706. * @param int $iLimit How many cast images should be returned?
  707. * @param bool $bMore Add … if there are more cast members than printed.
  708. * @param string $sSize small, mid or big cast images
  709. * @param bool $bDownload Return URL or Download
  710. *
  711. * @return array Array with cast name as key, and image as value.
  712. */
  713. public function getCastImages($iLimit = 0, $bMore = true, $sSize = 'small', $bDownload = false)
  714. {
  715. if (true === $this->isReady) {
  716. $aMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_CAST_IMAGE);
  717. $aReturn = [];
  718. if (count($aMatch[4])) {
  719. foreach ($aMatch[4] as $i => $sName) {
  720. if (0 !== $iLimit && $i >= $iLimit) {
  721. break;
  722. }
  723. $sMatch = $aMatch[2][$i];
  724. if ('big' === strtolower($sSize) && false !== strstr($aMatch[2][$i], '@._')) {
  725. $sMatch = substr($aMatch[2][$i], 0, strpos($aMatch[2][$i], '@._')) . '@.jpg';
  726. } elseif ('mid' === strtolower($sSize) && false !== strstr($aMatch[2][$i], '@._')) {
  727. $sMatch = substr($aMatch[2][$i], 0, strpos($aMatch[2][$i], '@._')) . '@._V1_UX214_AL_.jpg';
  728. }
  729. if (false === $bDownload) {
  730. $sMatch = IMDBHelper::cleanString($sMatch);
  731. } else {
  732. $sLocal = IMDBHelper::saveImageCast($sMatch, $aMatch[3][$i]);
  733. if (file_exists(dirname(__FILE__) . '/' . $sLocal)) {
  734. $sMatch = $sLocal;
  735. } else {
  736. //the 'big' image isn't available, try the 'mid' one (vice versa)
  737. if ('big' === strtolower($sSize) && false !== strstr($aMatch[2][$i], '@._')) {
  738. //trying the 'mid' one
  739. $sMatch = substr(
  740. $aMatch[2][$i],
  741. 0,
  742. strpos($aMatch[2][$i], '@._')
  743. ) . '@._V1_UX214_AL_.jpg';
  744. } else {
  745. //trying the 'big' one
  746. $sMatch = substr($aMatch[2][$i], 0, strpos($aMatch[2][$i], '@._')) . '@.jpg';
  747. }
  748. $sLocal = IMDBHelper::saveImageCast($sMatch, $aMatch[3][$i]);
  749. if (file_exists(dirname(__FILE__) . '/' . $sLocal)) {
  750. $sMatch = $sLocal;
  751. } else {
  752. $sMatch = IMDBHelper::cleanString($aMatch[2][$i]);
  753. }
  754. }
  755. }
  756. $aReturn[IMDBHelper::cleanString($aMatch[4][$i])] = $sMatch;
  757. }
  758. $bMore = (0 !== $iLimit && $bMore && (count($aMatch[4]) > $iLimit) ? '…' : '');
  759. $bHaveMore = ($bMore && (count($aMatch[4]) > $iLimit));
  760. $aReturn = array_replace(
  761. $aReturn,
  762. array_fill_keys(
  763. array_keys($aReturn, self::$sNotFound),
  764. 'cast/not-found.jpg'
  765. )
  766. );
  767. return $aReturn;
  768. }
  769. }
  770. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound);
  771. }
  772. /**
  773. * @param int $iLimit How many cast members should be returned?
  774. * @param bool $bMore Add … if there are more cast members than
  775. * printed.
  776. * @param string $sTarget Add a target to the links?
  777. *
  778. * @return string A list with linked cast members and their character or
  779. * $sNotFound.
  780. */
  781. public function getCastAndCharacterAsUrl($iLimit = 0, $bMore = true, $sTarget = '')
  782. {
  783. if (true === $this->isReady) {
  784. $aMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_CAST);
  785. $aMatchChar = IMDBHelper::matchRegex($this->sSource, self::IMDB_CHAR);
  786. $aReturn = [];
  787. if (count($aMatch[2])) {
  788. foreach ($aMatch[2] as $i => $sName) {
  789. if (0 !== $iLimit && $i >= $iLimit) {
  790. break;
  791. }
  792. $sChar = str_replace(' / ', ' and ', $aMatchChar[1][$i]);
  793. $aReturn[] = '<a href="https://www.imdb.com/name/' . IMDBHelper::cleanString(
  794. $aMatch[1][$i]
  795. ) . '/"' . ($sTarget ? ' target="' . $sTarget . '"' : '') . '>' . IMDBHelper::cleanString(
  796. $sName
  797. ) . '</a> as ' . IMDBHelper::cleanString($sChar);
  798. }
  799. $bHaveMore = ($bMore && (count($aMatch[2]) > $iLimit));
  800. return IMDBHelper::arrayOutput(
  801. $this->bArrayOutput,
  802. $this->sSeparator,
  803. self::$sNotFound,
  804. $aReturn,
  805. $bHaveMore
  806. );
  807. }
  808. }
  809. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound);
  810. }
  811. /**
  812. * @param int $iLimit How many cast members should be returned?
  813. * @param bool $bMore Add … if there are more cast members than printed.
  814. *
  815. * @return string A list with cast members and their character or
  816. * $sNotFound.
  817. */
  818. public function getCastAndCharacter($iLimit = 0, $bMore = true)
  819. {
  820. if (true === $this->isReady) {
  821. $aMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_CAST);
  822. $aMatchChar = IMDBHelper::matchRegex($this->sSource, self::IMDB_CHAR);
  823. $aReturn = [];
  824. if (count($aMatch[2])) {
  825. foreach ($aMatch[2] as $i => $sName) {
  826. if (0 !== $iLimit && $i >= $iLimit) {
  827. break;
  828. }
  829. $sChar = str_replace(' / ', ' and ', $aMatchChar[1][$i]);
  830. $aReturn[] = IMDBHelper::cleanString($sName) . ' as ' . IMDBHelper::cleanString($sChar);
  831. }
  832. $bHaveMore = ($bMore && (count($aMatch[2]) > $iLimit));
  833. return IMDBHelper::arrayOutput(
  834. $this->bArrayOutput,
  835. $this->sSeparator,
  836. self::$sNotFound,
  837. $aReturn,
  838. $bHaveMore
  839. );
  840. }
  841. }
  842. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound);
  843. }
  844. /**
  845. * @return string The certification of the movie or $sNotFound.
  846. */
  847. public function getCertification()
  848. {
  849. if (true === $this->isReady) {
  850. $aReturn = [];
  851. $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_CERTIFICATION, 1);
  852. if (false !== $sMatch) {
  853. $aReturn[] = explode('|', IMDBHelper::cleanString($sMatch));
  854. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound, $aReturn);
  855. }
  856. }
  857. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound);
  858. }
  859. /**
  860. * @return string Color or $sNotFound.
  861. */
  862. public function getColor()
  863. {
  864. if (true === $this->isReady) {
  865. $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_COLOR, 1);
  866. if (false !== $sMatch) {
  867. return IMDBHelper::cleanString($sMatch);
  868. }
  869. }
  870. return self::$sNotFound;
  871. }
  872. /**
  873. * @return string The company producing the movie or $sNotFound.
  874. */
  875. public function getCompany()
  876. {
  877. if (true === $this->isReady) {
  878. $sMatch = $this->getCompanyAsUrl();
  879. if (self::$sNotFound !== $sMatch) {
  880. return IMDBHelper::cleanString($sMatch);
  881. }
  882. }
  883. return self::$sNotFound;
  884. }
  885. /**
  886. * @param string $sTarget Add a target to the links?
  887. *
  888. * @return string The linked company producing the movie or $sNotFound.
  889. */
  890. public function getCompanyAsUrl($sTarget = '')
  891. {
  892. if (true === $this->isReady) {
  893. $aMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_COMPANIES);
  894. $aReturn = [];
  895. if (isset($aMatch[1][0])) {
  896. $bMatch = IMDBHelper::matchRegex($aMatch[1][0], self::IMDB_COMPANY);
  897. if (count($bMatch[2])) {
  898. foreach ($bMatch[2] as $i => $sName) {
  899. $aReturn[] = '<a href="https://www.imdb.com/company/' . IMDBHelper::cleanString(
  900. $bMatch[1][$i]
  901. ) . '/"' . ($sTarget ? ' target="' . $sTarget . '"' : '') . '>' . IMDBHelper::cleanString(
  902. $sName
  903. ) . '</a>';
  904. }
  905. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound, $aReturn);
  906. }
  907. }
  908. }
  909. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound);
  910. }
  911. /**
  912. * @return string A list with countries or $sNotFound.
  913. */
  914. public function getCountry()
  915. {
  916. if (true === $this->isReady) {
  917. $sMatch = $this->getCountryAsUrl();
  918. if (self::$sNotFound !== $sMatch) {
  919. return IMDBHelper::cleanString($sMatch);
  920. }
  921. }
  922. return self::$sNotFound;
  923. }
  924. /**
  925. * @param string $sTarget Add a target to the links?
  926. *
  927. * @return string A list with linked countries or $sNotFound.
  928. */
  929. public function getCountryAsUrl($sTarget = '')
  930. {
  931. if (true === $this->isReady) {
  932. $aMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_COUNTRY);
  933. $aReturn = [];
  934. if (count($aMatch[2])) {
  935. foreach ($aMatch[2] as $i => $sName) {
  936. $aReturn[] = '<a href="https://www.imdb.com/country/' . trim(
  937. $aMatch[1][$i]
  938. ) . '/"' . ($sTarget ? ' target="' . $sTarget . '"' : '') . '>' . IMDBHelper::cleanString(
  939. $sName
  940. ) . '</a>';
  941. }
  942. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound, $aReturn);
  943. }
  944. }
  945. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound);
  946. }
  947. /**
  948. * @return string A list with the creators or $sNotFound.
  949. */
  950. public function getCreator()
  951. {
  952. if (true === $this->isReady) {
  953. $sMatch = $this->getCreatorAsUrl();
  954. if (self::$sNotFound !== $sMatch) {
  955. return IMDBHelper::cleanString($sMatch);
  956. }
  957. }
  958. return self::$sNotFound;
  959. }
  960. /**
  961. * @param string $sTarget Add a target to the links?
  962. *
  963. * @return string A list with the linked creators or $sNotFound.
  964. */
  965. public function getCreatorAsUrl($sTarget = '')
  966. {
  967. if (true === $this->isReady) {
  968. $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_CREATOR, 1);
  969. $aMatch = IMDBHelper::matchRegex($sMatch, self::IMDB_NAME);
  970. $aReturn = [];
  971. if (count($aMatch[2])) {
  972. foreach ($aMatch[2] as $i => $sName) {
  973. $aReturn[] = '<a href="https://www.imdb.com/name/' . IMDBHelper::cleanString(
  974. $aMatch[1][$i]
  975. ) . '/"' . ($sTarget ? ' target="' . $sTarget . '"' : '') . '>' . IMDBHelper::cleanString(
  976. $sName
  977. ) . '</a>';
  978. }
  979. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound, $aReturn);
  980. }
  981. }
  982. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound);
  983. }
  984. /**
  985. * @return string The description of the movie or $sNotFound.
  986. */
  987. public function getDescription()
  988. {
  989. if (true === $this->isReady) {
  990. $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_SERIESEP_DESC, 1);
  991. if (false !== $sMatch) {
  992. if (strpos($sMatch, self::IMDB_NOT_FOUND_DES) === false) {
  993. return IMDBHelper::cleanString($sMatch);
  994. }
  995. }
  996. $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_SERIES_DESC, 1);
  997. if (false !== $sMatch) {
  998. if (strpos($sMatch, self::IMDB_NOT_FOUND_DES) === false) {
  999. return IMDBHelper::cleanString($sMatch);
  1000. }
  1001. }
  1002. $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_MOVIE_DESC, 1);
  1003. if (false !== $sMatch) {
  1004. if (strpos($sMatch, self::IMDB_NOT_FOUND_DES) === false) {
  1005. return IMDBHelper::cleanString($sMatch);
  1006. }
  1007. }
  1008. }
  1009. return self::$sNotFound;
  1010. }
  1011. /**
  1012. * @return string A list with the directors or $sNotFound.
  1013. */
  1014. public function getDirector()
  1015. {
  1016. if (true === $this->isReady) {
  1017. $sMatch = $this->getDirectorAsUrl();
  1018. if (self::$sNotFound !== $sMatch) {
  1019. return IMDBHelper::cleanString($sMatch);
  1020. }
  1021. }
  1022. return self::$sNotFound;
  1023. }
  1024. /**
  1025. * @param string $sTarget Add a target to the links?
  1026. *
  1027. * @return string A list with the linked directors or $sNotFound.
  1028. */
  1029. public function getDirectorAsUrl($sTarget = '')
  1030. {
  1031. if (true === $this->isReady) {
  1032. $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_DIRECTOR, 1);
  1033. $aMatch = IMDBHelper::matchRegex($sMatch, self::IMDB_NAME);
  1034. $aReturn = [];
  1035. if (count($aMatch[2])) {
  1036. foreach ($aMatch[2] as $i => $sName) {
  1037. $aReturn[] = '<a href="https://www.imdb.com/name/' . IMDBHelper::cleanString(
  1038. $aMatch[1][$i]
  1039. ) . '/"' . ($sTarget ? ' target="' . $sTarget . '"' : '') . '>' . IMDBHelper::cleanString(
  1040. $sName
  1041. ) . '</a>';
  1042. }
  1043. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound, $aReturn);
  1044. }
  1045. }
  1046. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound);
  1047. }
  1048. /**
  1049. * @param string $sTarget Add a target to the links?
  1050. *
  1051. * @return array A list (name, url, year, country, type) with Distributors or $sNotFound.
  1052. */
  1053. public function getDistributor($iLimit = 0, $bMore = true)
  1054. {
  1055. if (true === $this->isReady) {
  1056. $aMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_DISTRIBUTOR);
  1057. $aReturn = [];
  1058. if (isset($aMatch[1][0])) {
  1059. $bMatch = IMDBHelper::matchRegex($aMatch[1][0], self::IMDB_DISTRIBUTORS);
  1060. if (count($bMatch[2])) {
  1061. foreach ($bMatch[2] as $i => $sName) {
  1062. if (0 !== $iLimit && $i >= $iLimit) {
  1063. break;
  1064. }
  1065. $aReturn[] = [
  1066. 'distributor' => IMDBHelper::cleanString($sName),
  1067. 'url' => 'https://www.imdb.com/company/' . IMDBHelper::cleanString($bMatch[1][$i]) .'',
  1068. 'year' => IMDBHelper::cleanString($bMatch[3][$i]),
  1069. 'country' => IMDBHelper::cleanString($bMatch[4][$i]),
  1070. 'type' => IMDBHelper::cleanString($bMatch[5][$i]),
  1071. ];
  1072. }
  1073. $bMore = (0 !== $iLimit && $bMore && (count($aMatch[2]) > $iLimit) ? '…' : '');
  1074. $bHaveMore = ($bMore && (count($aMatch[2]) > $iLimit));
  1075. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound, $aReturn, $bHaveMore);
  1076. }
  1077. }
  1078. }
  1079. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound);
  1080. }
  1081. /**
  1082. * @param string $sTarget Add a target to the links?
  1083. *
  1084. * @return string A list with the linked distributors or $sNotFound.
  1085. */
  1086. public function getDistributorAsUrl($sTarget = '')
  1087. {
  1088. if (true === $this->isReady) {
  1089. $aMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_DISTRIBUTOR);
  1090. $aReturn = [];
  1091. if (isset($aMatch[1][0])) {
  1092. $bMatch = IMDBHelper::matchRegex($aMatch[1][0], self::IMDB_DISTRIBUTORS);
  1093. if (count($bMatch[2])) {
  1094. foreach ($bMatch[2] as $i => $sName) {
  1095. $aReturn[] = '<a href="https://www.imdb.com/company/' . IMDBHelper::cleanString(
  1096. $bMatch[1][$i]
  1097. ) . '/"' . ($sTarget ? ' target="' . $sTarget . '"' : '') . '>' . IMDBHelper::cleanString(
  1098. $sName
  1099. ) . '</a>';
  1100. }
  1101. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound, $aReturn);
  1102. }
  1103. }
  1104. }
  1105. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound);
  1106. }
  1107. /**
  1108. * @return string The episode title of the tv show or $sNotFound.
  1109. */
  1110. public function getEpisodeTitle()
  1111. {
  1112. if (true === $this->isReady) {
  1113. if (preg_match('/Episode/i', $this->getType())) {
  1114. $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_TITLE_EP, 1);
  1115. if (false !== $sMatch && "" !== $sMatch) {
  1116. return IMDBHelper::cleanString($sMatch);
  1117. }
  1118. }
  1119. }
  1120. return self::$sNotFound;
  1121. }
  1122. /**
  1123. * @return string A list with the genres or $sNotFound.
  1124. */
  1125. public function getGenre()
  1126. {
  1127. if (true === $this->isReady) {
  1128. $sMatch = $this->getGenreAsUrl();
  1129. if (self::$sNotFound !== $sMatch) {
  1130. return IMDBHelper::cleanString($sMatch);
  1131. }
  1132. }
  1133. return self::$sNotFound;
  1134. }
  1135. /**
  1136. * @param string $sTarget Add a target to the links?
  1137. *
  1138. * @return string A list with the linked genres or $sNotFound.
  1139. */
  1140. public function getGenreAsUrl($sTarget = '')
  1141. {
  1142. if (true === $this->isReady) {
  1143. $aMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_GENRE);
  1144. $aReturn = [];
  1145. if (count($aMatch[2])) {
  1146. foreach (array_unique($aMatch[2]) as $i => $sName) {
  1147. $aReturn[] = '<a href="https://www.imdb.com/search/title?genres=' . IMDBHelper::cleanString(
  1148. $aMatch[1][$i]
  1149. ) . '"' . ($sTarget ? ' target="' . $sTarget . '"' : '') . '>' . IMDBHelper::cleanString(
  1150. $sName
  1151. ) . '</a>';
  1152. }
  1153. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound, $aReturn);
  1154. }
  1155. }
  1156. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound);
  1157. }
  1158. /**
  1159. * @return string cumulative worldwide gross or $sNotFound.
  1160. */
  1161. public function getGross()
  1162. {
  1163. if (true === $this->isReady) {
  1164. $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_GROSS, 1);
  1165. if (false !== $sMatch) {
  1166. return IMDBHelper::cleanString($sMatch);
  1167. }
  1168. }
  1169. return self::$sNotFound;
  1170. }
  1171. /**
  1172. * @return string A list with the languages or $sNotFound.
  1173. */
  1174. public function getLanguage()
  1175. {
  1176. if (true === $this->isReady) {
  1177. $sMatch = $this->getLanguageAsUrl();
  1178. if (self::$sNotFound !== $sMatch) {
  1179. return IMDBHelper::cleanString($sMatch);
  1180. }
  1181. }
  1182. return self::$sNotFound;
  1183. }
  1184. /**
  1185. * @param string $sTarget Add a target to the links?
  1186. *
  1187. * @return string A list with the linked languages or $sNotFound.
  1188. */
  1189. public function getLanguageAsUrl($sTarget = '')
  1190. {
  1191. if (true === $this->isReady) {
  1192. $aMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_LANGUAGE);
  1193. $aReturn = [];
  1194. if (count($aMatch[2])) {
  1195. foreach ($aMatch[2] as $i => $sName) {
  1196. $aReturn[] = '<a href="https://www.imdb.com/language/' . IMDBHelper::cleanString(
  1197. $aMatch[1][$i]
  1198. ) . '"' . ($sTarget ? ' target="' . $sTarget . '"' : '') . '>' . IMDBHelper::cleanString(
  1199. $sName
  1200. ) . '</a>';
  1201. }
  1202. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound, $aReturn);
  1203. }
  1204. }
  1205. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound);
  1206. }
  1207. /**
  1208. * @return string A list with the location or $sNotFound.
  1209. */
  1210. public function getLocation()
  1211. {
  1212. if (true === $this->isReady) {
  1213. $sMatch = $this->getLocationAsUrl();
  1214. if (self::$sNotFound !== $sMatch) {
  1215. return IMDBHelper::cleanString($sMatch);
  1216. }
  1217. }
  1218. return self::$sNotFound;
  1219. }
  1220. /**
  1221. * @param string $sTarget Add a target to the links?
  1222. *
  1223. * @return string A list with the linked location or $sNotFound.
  1224. */
  1225. public function getLocationAsUrl($sTarget = '')
  1226. {
  1227. if (true === $this->isReady) {
  1228. $aMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_LOCATION);
  1229. $aReturn = [];
  1230. if (count($aMatch[2])) {
  1231. foreach ($aMatch[2] as $i => $sName) {
  1232. $aReturn[] = '<a href="https://www.imdb.com/search/title?locations=' . IMDBHelper::cleanString(
  1233. $aMatch[1][$i]
  1234. ) . '"' . ($sTarget ? ' target="' . $sTarget . '"' : '') . '>' . IMDBHelper::cleanString(
  1235. $sName
  1236. ) . '</a>';
  1237. }
  1238. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound, $aReturn);
  1239. }
  1240. }
  1241. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound);
  1242. }
  1243. /**
  1244. * Returns all locations
  1245. *
  1246. * @return string location
  1247. * @return string specification
  1248. */
  1249. public function getLocations()
  1250. {
  1251. if (true === $this->isReady) {
  1252. // Does a cache of this movie exist?
  1253. $sCacheFile = $this->sRoot . '/cache/' . sha1($this->iId) . '_locations.cache';
  1254. $bUseCache = false;
  1255. if (is_readable($sCacheFile)) {
  1256. $iDiff = round(abs(time() - filemtime($sCacheFile)) / 60);
  1257. if ($iDiff < $this->iCache || false) {
  1258. $bUseCache = true;
  1259. }
  1260. }
  1261. if ($bUseCache) {
  1262. $aRawReturn = file_get_contents($sCacheFile);
  1263. $aReturn = unserialize($aRawReturn);
  1264. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound, $aReturn);
  1265. } else {
  1266. $fullLocations = sprintf('https://www.imdb.com/title/tt%s/locations', $this->iId);
  1267. $aCurlInfo = IMDBHelper::runCurl($fullLocations);
  1268. $sSource = $aCurlInfo['contents'] ?? false;
  1269. if (false === $sSource) {
  1270. if (true === self::IMDB_DEBUG) {
  1271. echo '<pre><b>cURL error:</b> ' . var_dump($aCurlInfo) . '</pre>';
  1272. }
  1273. return false;
  1274. }
  1275. $aReturned = IMDBHelper::matchRegex($sSource, self::IMDB_LOCATIONS);
  1276. if ($aReturned) {
  1277. $aReturn = [];
  1278. foreach ($aReturned[1] as $i => $strName) {
  1279. if (strpos($strName, '(') === false) {
  1280. $aReturn[] = [
  1281. 'location' => IMDBHelper::cleanString($strName),
  1282. ];
  1283. }
  1284. if (strpos($aReturned[2][$i], '(') !== false) {
  1285. $aReturn[] = [
  1286. 'specification' => IMDBHelper::cleanString($aReturned[2][$i]),
  1287. ];
  1288. }
  1289. }
  1290. file_put_contents($sCacheFile, serialize($aReturn));
  1291. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound, $aReturn);
  1292. }
  1293. }
  1294. }
  1295. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound);
  1296. }
  1297. /**
  1298. * @return string The MPAA of the movie or $sNotFound.
  1299. */
  1300. public function getMpaa()
  1301. {
  1302. if (true === $this->isReady) {
  1303. $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_MPAA, 1);
  1304. if (false !== $sMatch) {
  1305. return IMDBHelper::cleanString($sMatch);
  1306. }
  1307. }
  1308. return self::$sNotFound;
  1309. }
  1310. /**
  1311. * @return string A list with the music composers or $sNotFound.
  1312. */
  1313. public function getMusic()
  1314. {
  1315. if (true === $this->isReady) {
  1316. $sMatch = $this->getMusicAsUrl();
  1317. if (self::$sNotFound !== $sMatch) {
  1318. return IMDBHelper::cleanString($sMatch);
  1319. }
  1320. }
  1321. return self::$sNotFound;
  1322. }
  1323. /**
  1324. * @param string $sTarget Add a target to the links?
  1325. *
  1326. * @return string A list with the linked music composers or $sNotFound.
  1327. */
  1328. public function getMusicAsUrl($sTarget = '')
  1329. {
  1330. if (true === $this->isReady) {
  1331. $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_MUSIC, 1);
  1332. $aMatch = IMDBHelper::matchRegex($sMatch, self::IMDB_NAME);
  1333. $aReturn = [];
  1334. if (count($aMatch[2])) {
  1335. foreach ($aMatch[2] as $i => $sName) {
  1336. $aReturn[] = '<a href="https://www.imdb.com/name/' . IMDBHelper::cleanString(
  1337. $aMatch[1][$i]
  1338. ) . '/"' . ($sTarget ? ' target="' . $sTarget . '"' : '') . '>' . IMDBHelper::cleanString(
  1339. $sName
  1340. ) . '</a>';
  1341. }
  1342. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound, $aReturn);
  1343. }
  1344. }
  1345. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound);
  1346. }
  1347. /**
  1348. * @param int $iLimit How many photo images should be returned?
  1349. * @param bool $bMore Add … if there are more cast members than printed.
  1350. * @param string $sSize small or big images
  1351. *
  1352. * @return array Array with title and url.
  1353. */
  1354. public function getPhotos($iLimit = 0, $bMore = true, $sSize = 'small')
  1355. {
  1356. if (true === $this->isReady) {
  1357. // Does a cache of this movie exist?
  1358. $sCacheFile = $this->sRoot . '/cache/' . sha1($this->iId) . '_gallery.cache';
  1359. $bUseCache = false;
  1360. if (is_readable($sCacheFile)) {
  1361. $iDiff = round(abs(time() - filemtime($sCacheFile)) / 60);
  1362. if ($iDiff < $this->iCache || false) {
  1363. $bUseCache = true;
  1364. }
  1365. }
  1366. if ($bUseCache) {
  1367. $aRawReturn = file_get_contents($sCacheFile);
  1368. $aReturn = unserialize($aRawReturn);
  1369. $anReturn = [];
  1370. foreach ($aReturn as $i => $sAreturn) {
  1371. if (0 !== $iLimit && $i >= $iLimit) {
  1372. break;
  1373. }
  1374. $title = $sAreturn['title'];
  1375. $url = $sAreturn['url'];
  1376. if ('big' === strtolower($sSize) && false !== strstr($url, '._')) {
  1377. $url = substr($url, 0, strpos($url, '._')) . '.jpg';
  1378. }
  1379. $anReturn[] = [
  1380. 'title' => $title,
  1381. 'url' => $url,
  1382. ];
  1383. }
  1384. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound, $anReturn);
  1385. } else {
  1386. $isPage = true;
  1387. $aReturn = [];
  1388. $page = 1;
  1389. while ($isPage) {
  1390. $fullPhotos = sprintf('https://www.imdb.com/title/tt%s/mediaindex?page=%d', $this->iId, $page);
  1391. $aCurlInfo = IMDBHelper::runCurl($fullPhotos);
  1392. $sSource = $aCurlInfo['contents'];
  1393. if (false === $sSource) {
  1394. if (true === self::IMDB_DEBUG) {
  1395. echo '<pre><b>cURL error:</b> ' . var_dump($aCurlInfo) . '</pre>';
  1396. }
  1397. return false;
  1398. }
  1399. $aReturned = IMDBHelper::matchRegex($sSource, '~title="(.*?)"\s+><img(?:.*)\s+src="(.*?)"\s+\/>~');
  1400. if ($aReturned) {
  1401. foreach ($aReturned[1] as $i => $strName) {
  1402. $aReturn[] = [
  1403. 'title' => IMDBHelper::cleanString($strName),
  1404. 'url' => IMDBHelper::cleanString($aReturned[2][$i]),
  1405. ];
  1406. }
  1407. }
  1408. file_put_contents($sCacheFile, serialize($aReturn));
  1409. if (!preg_match('~class="prevnext"\s>Next~', $sSource)) {
  1410. $isPage = false;
  1411. }
  1412. $page++;
  1413. }
  1414. $anReturn = [];
  1415. foreach ($aReturn as $i => $sAreturn) {
  1416. if (0 !== $iLimit && $i >= $iLimit) {
  1417. break;
  1418. }
  1419. $title = $sAreturn['title'];
  1420. $url = $sAreturn['url'];
  1421. if ('big' === strtolower($sSize) && false !== strstr($url, '._')) {
  1422. $url = substr($url, 0, strpos($url, '._')) . '.jpg';
  1423. }
  1424. $anReturn[] = [
  1425. 'title' => $title,
  1426. 'url' => $url,
  1427. ];
  1428. }
  1429. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound, $anReturn);
  1430. }
  1431. }
  1432. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound);
  1433. }
  1434. /**
  1435. * @return string A list with the plot keywords or $sNotFound.
  1436. */
  1437. public function getPlotKeywords()
  1438. {
  1439. if (true === $this->isReady) {
  1440. $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_PLOT_KEYWORDS, 1);
  1441. if (false !== $sMatch) {
  1442. $aReturn = explode('|', IMDBHelper::cleanString($sMatch));
  1443. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound, $aReturn);
  1444. }
  1445. }
  1446. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound);
  1447. }
  1448. /**
  1449. * @param int $iLimit The limit.
  1450. *
  1451. * @return string The plot of the movie or $sNotFound.
  1452. */
  1453. public function getPlot($iLimit = 0)
  1454. {
  1455. if (true === $this->isReady) {
  1456. $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_PLOT, 1);
  1457. if (false !== $sMatch) {
  1458. if ($iLimit !== 0) {
  1459. return IMDBHelper::getShortText(IMDBHelper::cleanString($sMatch), $iLimit);
  1460. }
  1461. return IMDBHelper::cleanString($sMatch);
  1462. }
  1463. }
  1464. return self::$sNotFound;
  1465. }
  1466. /**
  1467. * @param string $sSize Small, big, xxs, xs, s poster?
  1468. * @param bool $bDownload Return URL to the poster or download it?
  1469. *
  1470. * @return bool|string Path to the poster.
  1471. */
  1472. public function getPoster($sSize = 'small', $bDownload = false)
  1473. {
  1474. if (true === $this->isReady) {
  1475. $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_POSTER, 1);
  1476. if (false !== $sMatch) {
  1477. if ('big' === strtolower($sSize) && false !== strstr($sMatch, '._')) {
  1478. $sMatch = substr($sMatch, 0, strpos($sMatch, '._')) . '.jpg';
  1479. }
  1480. if ('xxs' === strtolower($sSize) && false !== strstr($sMatch, '._')) {
  1481. $sMatch = substr($sMatch, 0, strpos($sMatch, '._')) . '._V1_UY67_CR0,0,45,67_AL_.jpg';
  1482. }
  1483. if ('xs' === strtolower($sSize) && false !== strstr($sMatch, '._')) {
  1484. $sMatch = substr($sMatch, 0, strpos($sMatch, '._')) . '._V1_UY113_CR0,0,76,113_AL_.jpg';
  1485. }
  1486. if ('s' === strtolower($sSize) && false !== strstr($sMatch, '._')) {
  1487. $sMatch = substr($sMatch, 0, strpos($sMatch, '._')) . '._V1_UX182_CR0,0,182,268_AL_.jpg';
  1488. }
  1489. if (false === $bDownload) {
  1490. return IMDBHelper::cleanString($sMatch);
  1491. } else {
  1492. $sLocal = IMDBHelper::saveImage($sMatch, $this->iId);
  1493. if (file_exists(dirname(__FILE__) . '/' . $sLocal)) {
  1494. return $sLocal;
  1495. } else {
  1496. return $sMatch;
  1497. }
  1498. }
  1499. }
  1500. }
  1501. return self::$sNotFound;
  1502. }
  1503. /**
  1504. * @return string The rating of the movie or $sNotFound.
  1505. */
  1506. public function getRating()
  1507. {
  1508. if (true === $this->isReady) {
  1509. $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_RATING, 1);
  1510. if (false !== $sMatch) {
  1511. return IMDBHelper::cleanString($sMatch);
  1512. }
  1513. }
  1514. return self::$sNotFound;
  1515. }
  1516. /**
  1517. * @return string The rating count of the movie or $sNotFound.
  1518. */
  1519. public function getRatingCount()
  1520. {
  1521. if (true === $this->isReady) {
  1522. $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_RATING_COUNT, 1);
  1523. if (false !== $sMatch) {
  1524. return str_replace(',', '', IMDBHelper::cleanString($sMatch));
  1525. }
  1526. }
  1527. return self::$sNotFound;
  1528. }
  1529. /**
  1530. * Release date doesn't contain all the information we need to create a media and
  1531. * we need this function that checks if users can vote target media (if can, it's released).
  1532. *
  1533. * @return true If the media is released
  1534. */
  1535. public function isReleased()
  1536. {
  1537. $strReturn = $this->getReleaseDate();
  1538. if ($strReturn == self::$sNotFound || $strReturn == 'Not yet released') {
  1539. return false;
  1540. }
  1541. return true;
  1542. }
  1543. /**
  1544. * @return string The release date of the movie or $sNotFound.
  1545. */
  1546. public function getReleaseDate()
  1547. {
  1548. if (true === $this->isReady) {
  1549. $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_RELEASE_DATE, 1);
  1550. if (false !== $sMatch) {
  1551. return IMDBHelper::cleanString($sMatch);
  1552. }
  1553. }
  1554. return self::$sNotFound;
  1555. }
  1556. /**
  1557. * Returns all local names
  1558. *
  1559. * @return string country
  1560. * @return string release date
  1561. */
  1562. public function getReleaseDates()
  1563. {
  1564. if (true === $this->isReady) {
  1565. // Does a cache of this movie exist?
  1566. $sCacheFile = $this->sRoot . '/cache/' . sha1($this->iId) . '_dates.cache';
  1567. $bUseCache = false;
  1568. if (is_readable($sCacheFile)) {
  1569. $iDiff = round(abs(time() - filemtime($sCacheFile)) / 60);
  1570. if ($iDiff < $this->iCache || false) {
  1571. $bUseCache = true;
  1572. }
  1573. }
  1574. if ($bUseCache) {
  1575. $aRawReturn = file_get_contents($sCacheFile);
  1576. $aReturn = unserialize($aRawReturn);
  1577. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound, $aReturn);
  1578. } else {
  1579. $fullAkas = sprintf('https://www.imdb.com/title/tt%s/releaseinfo', $this->iId);
  1580. $aCurlInfo = IMDBHelper::runCurl($fullAkas);
  1581. $sSource = $aCurlInfo['contents'] ?? false;
  1582. if (false === $sSource) {
  1583. if (true === self::IMDB_DEBUG) {
  1584. echo '<pre><b>cURL error:</b> ' . var_dump($aCurlInfo) . '</pre>';
  1585. }
  1586. return false;
  1587. }
  1588. $aReturned = IMDBHelper::matchRegex(
  1589. $sSource,
  1590. '~>(.*)\s+<\/a><\/td>\s+<td class=\"release-date-item__date\" align=\"right\">(.*)<\/td>~'
  1591. );
  1592. if ($aReturned) {
  1593. $aReturn = [];
  1594. foreach ($aReturned[1] as $i => $strName) {
  1595. if (strpos($strName, '(') === false) {
  1596. $aReturn[] = [
  1597. 'country' => IMDBHelper::cleanString($strName),
  1598. 'releasedate' => IMDBHelper::cleanString($aReturned[2][$i]),
  1599. ];
  1600. }
  1601. }
  1602. file_put_contents($sCacheFile, serialize($aReturn));
  1603. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound, $aReturn);
  1604. }
  1605. }
  1606. }
  1607. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound);
  1608. }
  1609. /**
  1610. * @return string The runtime of the movie or $sNotFound.
  1611. */
  1612. public function getRuntime()
  1613. {
  1614. if (true === $this->isReady) {
  1615. $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_RUNTIME, 1);
  1616. if (false !== $sMatch) {
  1617. return IMDBHelper::cleanString($sMatch);
  1618. }
  1619. }
  1620. return self::$sNotFound;
  1621. }
  1622. /**
  1623. * @return string A list with the seasons or $sNotFound.
  1624. */
  1625. public function getSeasons()
  1626. {
  1627. if (true === $this->isReady) {
  1628. $sMatch = $this->getSeasonsAsUrl();
  1629. if (self::$sNotFound !== $sMatch) {
  1630. return IMDBHelper::cleanString($sMatch);
  1631. }
  1632. }
  1633. return self::$sNotFound;
  1634. }
  1635. /**
  1636. * @param string $sTarget Add a target to the links?
  1637. *
  1638. * @return string A list with the linked seasons or $sNotFound.
  1639. */
  1640. public function getSeasonsAsUrl($sTarget = '')
  1641. {
  1642. if (true === $this->isReady) {
  1643. $aMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_SEASONS);
  1644. $aReturn = [];
  1645. if (count($aMatch[1])) {
  1646. foreach (range(1, max($aMatch[1])) as $i => $sName) {
  1647. $aReturn[] = '<a href="https://www.imdb.com/title/tt' . $this->iId . '/episodes?season=' . $sName . '"' . ($sTarget ? ' target="' . $sTarget . '"' : '') . '>' . $sName . '</a>';
  1648. }
  1649. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound, $aReturn);
  1650. }
  1651. }
  1652. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound);
  1653. }
  1654. /**
  1655. * @return string The sound mix of the movie or $sNotFound.
  1656. */
  1657. public function getSoundMix()
  1658. {
  1659. if (true === $this->isReady) {
  1660. $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_SOUND_MIX, 1);
  1661. if (false !== $sMatch) {
  1662. return IMDBHelper::cleanString($sMatch);
  1663. }
  1664. }
  1665. return self::$sNotFound;
  1666. }
  1667. /**
  1668. * @return string The tagline of the movie or $sNotFound.
  1669. */
  1670. public function getTagline()
  1671. {
  1672. if (true === $this->isReady) {
  1673. $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_TAGLINE, 1);
  1674. if (false !== $sMatch) {
  1675. return IMDBHelper::cleanString($sMatch);
  1676. }
  1677. }
  1678. return self::$sNotFound;
  1679. }
  1680. /**
  1681. * @param bool $bForceLocal Try to return the original name of the movie.
  1682. *
  1683. * @return string The title of the movie or $sNotFound.
  1684. */
  1685. public function getTitle($bForceLocal = false)
  1686. {
  1687. if (true === $this->isReady) {
  1688. if (true === $bForceLocal) {
  1689. $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_TITLE_ORIG, 1);
  1690. if (false !== $sMatch && "" !== $sMatch) {
  1691. return IMDBHelper::cleanString($sMatch);
  1692. }
  1693. }
  1694. $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_TITLE, 1);
  1695. $sMatch = preg_replace('~\(\d{4}\)$~Ui', '', $sMatch);
  1696. if (false !== $sMatch && "" !== $sMatch) {
  1697. return IMDBHelper::cleanString($sMatch);
  1698. }
  1699. }
  1700. return self::$sNotFound;
  1701. }
  1702. /**
  1703. * @return string returns the given position at top-250 or $sNotFound.
  1704. */
  1705. public function getTop250()
  1706. {
  1707. if (true === $this->isReady) {
  1708. $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_TOP250, 1);
  1709. if (false !== $sMatch) {
  1710. return IMDBHelper::cleanString($sMatch);
  1711. }
  1712. }
  1713. return self::$sNotFound;
  1714. }
  1715. /**
  1716. * @param bool $bEmbed Link to player directly?
  1717. *
  1718. * @return string The URL to the trailer of the movie or $sNotFound.
  1719. */
  1720. public function getTrailerAsUrl($bEmbed = false)
  1721. {
  1722. if (true === $this->isReady) {
  1723. $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_TRAILER, 1);
  1724. if (false !== $sMatch) {
  1725. if ($bEmbed) {
  1726. $sUrl = 'https://www.imdb.com/video/imdb/' . $sMatch . '/imdb/embed';
  1727. } else {
  1728. $sUrl = 'https://www.imdb.com/video/' . $sMatch;
  1729. }
  1730. return IMDBHelper::cleanString($sUrl);
  1731. }
  1732. }
  1733. return self::$sNotFound;
  1734. }
  1735. /**
  1736. * @return array Array with season, episode, title, rating, votes, airdate, plot, id.
  1737. */
  1738. public function getTVInfo()
  1739. {
  1740. if (true === $this->isReady) {
  1741. // Does a cache of this movie exist?
  1742. $sCacheFile = $this->sRoot . '/cache/' . sha1($this->iId) . '_tv.cache';
  1743. $bUseCache = false;
  1744. if (is_readable($sCacheFile)) {
  1745. $iDiff = round(abs(time() - filemtime($sCacheFile)) / 60);
  1746. if ($iDiff < $this->iCache || false) {
  1747. $bUseCache = true;
  1748. }
  1749. }
  1750. if ($bUseCache) {
  1751. $aRawReturn = file_get_contents($sCacheFile);
  1752. $aReturn = unserialize($aRawReturn);
  1753. $anReturn = [];
  1754. foreach ($aReturn as $i => $sAreturn) {
  1755. $season = $sAreturn['season'];
  1756. $episode = $sAreturn['episode'];
  1757. $title = $sAreturn['title'];
  1758. $rating = $sAreturn['rating'];
  1759. $votes = $sAreturn['votes'];
  1760. $airdate = $sAreturn['airdate'];
  1761. $plot = $sAreturn['plot'];
  1762. $id = $sAreturn['id'];
  1763. $anReturn[] = [
  1764. 'season' => $season,
  1765. 'episode' => $episode,
  1766. 'title' => $title,
  1767. 'rating' => $rating,
  1768. 'votes' => $votes,
  1769. 'airdate' => $airdate,
  1770. 'plot' => $plot,
  1771. 'id' => $id
  1772. ];
  1773. }
  1774. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound, $anReturn);
  1775. } else {
  1776. $isPage = true;
  1777. $aReturn = [];
  1778. $page = 1;
  1779. while ($isPage) {
  1780. $fullEpisodes = sprintf('https://www.imdb.com/title/tt%s/episodes/?season=%d', $this->iId, $page);
  1781. $aCurlInfo = IMDBHelper::runCurl($fullEpisodes);
  1782. $sSource = $aCurlInfo['contents'];
  1783. if (false === $sSource) {
  1784. if (true === self::IMDB_DEBUG) {
  1785. echo '<pre><b>cURL error:</b> ' . var_dump($aCurlInfo) . '</pre>';
  1786. }
  1787. return false;
  1788. }
  1789. $aSplit = IMDBHelper::matchRegex($sSource, '~<article class=.+?episode-item-wrapper(.+?)ipc-rating-star--rate">Rate</span>~s');
  1790. if ($aSplit) {
  1791. foreach ($aSplit[1] as $i => $text) {
  1792. $aReturned = IMDBHelper::matchRegex($aSplit[1][$i], '~h4.+/title/(tt\d+)/[?]ref_.+ttep_ep(\d+).+?S\d+\.E\d+ ∙ (.+?)<\/div>.+?<span class=".+?">(.+?)</span>.+?<div class="ipc-html-content-inner-div">(.+?)</div>.+?ratingGroup--imdb-rating.+?</svg>(.+?)<span.+?ipc-rating-star--voteCount">.+?>(.+?)<~s');
  1793. if ($aReturned) {
  1794. foreach ($aReturned[1] as $n => $episode) {
  1795. $aReturn[] = [
  1796. 'season' => $page,
  1797. 'episode' => IMDBHelper::cleanString($aReturned[2][$n]),
  1798. 'title' => IMDBHelper::cleanString($aReturned[3][$n]),
  1799. 'rating' => IMDBHelper::cleanString($aReturned[6][$n]),
  1800. 'votes' => IMDBHelper::cleanString($aReturned[7][$n]),
  1801. 'airdate' => IMDBHelper::cleanString($aReturned[4][$n]),
  1802. 'plot' => IMDBHelper::cleanString($aReturned[5][$n]),
  1803. 'id' => IMDBHelper::cleanString($aReturned[1][$n]),
  1804. ];
  1805. }
  1806. }
  1807. }
  1808. }
  1809. file_put_contents($sCacheFile, serialize($aReturn));
  1810. if (preg_match('~href="\?season=-1~s', $sSource) || !preg_match('~id="load_next_episodes"~', $sSource)) {
  1811. break;
  1812. }
  1813. $page++;
  1814. }
  1815. $anReturn = [];
  1816. foreach ($aReturn as $i => $sAreturn) {
  1817. $season = $sAreturn['season'];
  1818. $episode = $sAreturn['episode'];
  1819. $title = $sAreturn['title'];
  1820. $rating = $sAreturn['rating'];
  1821. $votes = $sAreturn['votes'];
  1822. $airdate = $sAreturn['airdate'];
  1823. $plot = $sAreturn['plot'];
  1824. $id = $sAreturn['id'];
  1825. $anReturn[] = [
  1826. 'season' => $season,
  1827. 'episode' => $episode,
  1828. 'title' => $title,
  1829. 'rating' => $rating,
  1830. 'votes' => $votes,
  1831. 'airdate' => $airdate,
  1832. 'plot' => $plot,
  1833. 'id' => $id
  1834. ];
  1835. }
  1836. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound, $anReturn);
  1837. }
  1838. }
  1839. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound);
  1840. }
  1841. /**
  1842. *
  1843. * @return string type of the title or $sNotFound.
  1844. */
  1845. public function getType()
  1846. {
  1847. if (true === $this->isReady) {
  1848. $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_TYPE, 1);
  1849. if (false !== $sMatch && "" !== $sMatch) {
  1850. return IMDBHelper::cleanString($sMatch);
  1851. }
  1852. }
  1853. return self::$sNotFound;
  1854. }
  1855. /**
  1856. * @return string The IMDb URL.
  1857. */
  1858. public function getUrl()
  1859. {
  1860. if (true === $this->isReady) {
  1861. return IMDBHelper::cleanString(str_replace('reference', '', $this->sUrl));
  1862. }
  1863. return self::$sNotFound;
  1864. }
  1865. /**
  1866. * @return string The user review of the movie or $sNotFound.
  1867. */
  1868. public function getUserReview()
  1869. {
  1870. if (true === $this->isReady) {
  1871. $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_USER_REVIEW, 1);
  1872. if (false !== $sMatch) {
  1873. return IMDBHelper::cleanString($sMatch);
  1874. }
  1875. }
  1876. return self::$sNotFound;
  1877. }
  1878. /**
  1879. * @return string The votes of the movie or $sNotFound.
  1880. */
  1881. public function getVotes()
  1882. {
  1883. if (true === $this->isReady) {
  1884. $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_VOTES, 1);
  1885. if (false !== $sMatch) {
  1886. return IMDBHelper::cleanString($sMatch);
  1887. }
  1888. }
  1889. return self::$sNotFound;
  1890. }
  1891. /**
  1892. * @return string A list with the writers or $sNotFound.
  1893. */
  1894. public function getWriter()
  1895. {
  1896. if (true === $this->isReady) {
  1897. $sMatch = $this->getWriterAsUrl();
  1898. if (self::$sNotFound !== $sMatch) {
  1899. return IMDBHelper::cleanString($sMatch);
  1900. }
  1901. }
  1902. return self::$sNotFound;
  1903. }
  1904. /**
  1905. * @param string $sTarget Add a target to the links?
  1906. *
  1907. * @return string A list with the linked writers or $sNotFound.
  1908. */
  1909. public function getWriterAsUrl($sTarget = '')
  1910. {
  1911. if (true === $this->isReady) {
  1912. $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_WRITER, 1);
  1913. $aMatch = IMDBHelper::matchRegex($sMatch, self::IMDB_NAME);
  1914. $aReturn = [];
  1915. if (count($aMatch[2])) {
  1916. foreach ($aMatch[2] as $i => $sName) {
  1917. $aReturn[] = '<a href="https://www.imdb.com/name/' . IMDBHelper::cleanString(
  1918. $aMatch[1][$i]
  1919. ) . '/"' . ($sTarget ? ' target="' . $sTarget . '"' : '') . '>' . IMDBHelper::cleanString(
  1920. $sName
  1921. ) . '</a>';
  1922. }
  1923. return IMDBHelper::arrayOutput($this->bArrayOutput, $this->sSeparator, self::$sNotFound, $aReturn);
  1924. }
  1925. }
  1926. return self::$sNotFound;
  1927. }
  1928. /**
  1929. * @return string The year of the movie or $sNotFound.
  1930. */
  1931. public function getYear()
  1932. {
  1933. if (true === $this->isReady) {
  1934. $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_YEAR, 1);
  1935. if (false !== $sMatch) {
  1936. return IMDBHelper::cleanString($sMatch);
  1937. }
  1938. }
  1939. return self::$sNotFound;
  1940. }
  1941. /**
  1942. * @return string The budget of the movie or $sNotFound.
  1943. */
  1944. public function getBudget()
  1945. {
  1946. if (true === $this->isReady) {
  1947. $sMatch = IMDBHelper::matchRegex($this->sSource, self::IMDB_BUDGET, 1);
  1948. if (false !== $sMatch) {
  1949. return IMDBHelper::cleanString($sMatch);
  1950. }
  1951. }
  1952. return self::$sNotFound;
  1953. }
  1954. }
  1955. class IMDBHelper extends IMDB
  1956. {
  1957. /**
  1958. * Regular expression helper.
  1959. *
  1960. * @param string $sContent The content to search in.
  1961. * @param string $sPattern The regular expression.
  1962. * @param null $iIndex The index to return.
  1963. *
  1964. * @return bool If no match was found.
  1965. * @return string If one match was found.
  1966. * @return array If more than one match was found.
  1967. */
  1968. public static function matchRegex($sContent, $sPattern, $iIndex = null)
  1969. {
  1970. preg_match_all($sPattern, $sContent ?? '', $aMatches);
  1971. if ($aMatches === false) {
  1972. return false;
  1973. }
  1974. if ($iIndex !== null && is_int($iIndex)) {
  1975. if (isset($aMatches[$iIndex][0])) {
  1976. return $aMatches[$iIndex][0];
  1977. }
  1978. return false;
  1979. }
  1980. return $aMatches;
  1981. }
  1982. /**
  1983. * Preferred output in responses with multiple elements
  1984. *
  1985. * @param bool $bArrayOutput Native array or string with separators.
  1986. * @param string $sSeparator String separator.
  1987. * @param string $sNotFound Not found text.
  1988. * @param array $aReturn Original input.
  1989. * @param bool $bHaveMore Have more elements indicator.
  1990. *
  1991. * @return string|array Multiple results separated by selected separator string, or enclosed into native array.
  1992. */
  1993. public static function arrayOutput($bArrayOutput, $sSeparator, $sNotFound, $aReturn = null, $bHaveMore = false)
  1994. {
  1995. if ($bArrayOutput ?? false) {
  1996. if ($aReturn == null || ! is_array($aReturn)) {
  1997. return [];
  1998. }
  1999. if ($bHaveMore) {
  2000. $aReturn[] = '…';
  2001. }
  2002. return $aReturn;
  2003. } else {
  2004. if ($aReturn == null || ! is_array($aReturn)) {
  2005. return $sNotFound;
  2006. }
  2007. foreach ($aReturn as $i => $value) {
  2008. if (is_array($value)) {
  2009. $aReturn[$i] = implode($sSeparator, $value);
  2010. }
  2011. }
  2012. return implode($sSeparator, $aReturn) . (($bHaveMore) ? '…' : '');
  2013. }
  2014. }
  2015. /**
  2016. * @param string $sInput Input (eg. HTML).
  2017. *
  2018. * @return string Cleaned string.
  2019. */
  2020. public static function cleanString($sInput)
  2021. {
  2022. $aSearch = [
  2023. 'Full summary &raquo;',
  2024. 'Full synopsis &raquo;',
  2025. 'Add summary &raquo;',
  2026. 'Add synopsis &raquo;',
  2027. 'See more &raquo;',
  2028. 'See why on IMDbPro.',
  2029. "\n",
  2030. "\r",
  2031. ];
  2032. $aReplace = [
  2033. '',
  2034. '',
  2035. '',
  2036. '',
  2037. '',
  2038. '',
  2039. '',
  2040. '',
  2041. ];
  2042. $sInput = str_replace('</li>', ' | ', $sInput ?? '');
  2043. $sInput = strip_tags($sInput ?? '');
  2044. $sInput = str_replace('&nbsp;', ' ', $sInput ?? '');
  2045. $sInput = str_replace($aSearch, $aReplace, $sInput ?? '');
  2046. $sInput = html_entity_decode($sInput ?? '', ENT_QUOTES | ENT_HTML5);
  2047. $sInput = preg_replace('/\s+/', ' ', $sInput ?? '');
  2048. $sInput = trim($sInput ?? '');
  2049. $sInput = rtrim($sInput ?? '', ' |');
  2050. return ($sInput ? trim($sInput ?? '') : self::$sNotFound);
  2051. }
  2052. /**
  2053. * @param string $sText The long text.
  2054. * @param int $iLength The maximum length of the text.
  2055. *
  2056. * @return string The shortened text.
  2057. */
  2058. public static function getShortText($sText, $iLength = 100)
  2059. {
  2060. if (mb_strlen($sText) <= $iLength) {
  2061. return $sText;
  2062. }
  2063. list($sShort) = explode("\n", wordwrap($sText, $iLength - 1));
  2064. if (substr($sShort, -1) !== '.') {
  2065. return $sShort . '…';
  2066. }
  2067. return $sShort;
  2068. }
  2069. /**
  2070. * @param string $sUrl The URL to the image to download.
  2071. * @param int $iId The ID of the movie.
  2072. *
  2073. * @return string Local path.
  2074. */
  2075. public static function saveImage($sUrl, $iId)
  2076. {
  2077. if (preg_match('~title_addposter.jpg|imdb-share-logo.png~', $sUrl)) {
  2078. return 'posters/not-found.jpg';
  2079. }
  2080. $sFilename = dirname(__FILE__) . '/posters/' . $iId . '.jpg';
  2081. if (file_exists($sFilename)) {
  2082. return 'posters/' . $iId . '.jpg';
  2083. }
  2084. $aCurlInfo = self::runCurl($sUrl, true);
  2085. $sData = $aCurlInfo['contents'];
  2086. if (false === $sData) {
  2087. return 'posters/not-found.jpg';
  2088. }
  2089. $oFile = fopen($sFilename, 'x');
  2090. fwrite($oFile, $sData);
  2091. fclose($oFile);
  2092. return 'posters/' . $iId . '.jpg';
  2093. }
  2094. /**
  2095. * @param string $sUrl The URL to fetch.
  2096. * @param bool $bDownload Download?
  2097. *
  2098. * @return bool|mixed Array on success, false on failure.
  2099. */
  2100. public static function runCurl($sUrl, $bDownload = false)
  2101. {
  2102. $oCurl = curl_init($sUrl);
  2103. curl_setopt_array(
  2104. $oCurl,
  2105. [
  2106. CURLOPT_BINARYTRANSFER => ($bDownload ? true : false),
  2107. CURLOPT_CONNECTTIMEOUT => self::IMDB_TIMEOUT,
  2108. CURLOPT_ENCODING => '',
  2109. CURLOPT_FOLLOWLOCATION => 0,
  2110. CURLOPT_FRESH_CONNECT => 0,
  2111. CURLOPT_HEADER => ($bDownload ? false : true),
  2112. CURLOPT_HTTPHEADER => [
  2113. 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  2114. 'Accept-Charset: utf-8, iso-8859-1;q=0.5',
  2115. 'Accept-Language: ' . self::IMDB_LANG,
  2116. ],
  2117. CURLOPT_REFERER => 'https://www.imdb.com',
  2118. CURLOPT_RETURNTRANSFER => 1,
  2119. CURLOPT_SSL_VERIFYHOST => 0,
  2120. CURLOPT_SSL_VERIFYPEER => 0,
  2121. CURLOPT_TIMEOUT => self::IMDB_TIMEOUT,
  2122. CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0',
  2123. CURLOPT_VERBOSE => 0,
  2124. ]
  2125. );
  2126. $sOutput = curl_exec($oCurl);
  2127. $aCurlInfo = curl_getinfo($oCurl);
  2128. curl_close($oCurl);
  2129. $aCurlInfo['contents'] = $sOutput;
  2130. if (200 !== $aCurlInfo['http_code'] && 302 !== $aCurlInfo['http_code']) {
  2131. if (true === self::IMDB_DEBUG) {
  2132. echo '<pre><b>cURL returned wrong HTTP code “' . $aCurlInfo['http_code'] . '”, aborting.</b></pre>';
  2133. }
  2134. return false;
  2135. }
  2136. return $aCurlInfo;
  2137. }
  2138. /**
  2139. * @param string $sUrl The URL to the image to download.
  2140. * @param int $cId The cast ID of the actor.
  2141. *
  2142. * @return string Local path.
  2143. */
  2144. public static function saveImageCast($sUrl, $cId)
  2145. {
  2146. if ( ! preg_match('~http~', $sUrl)) {
  2147. return 'cast/not-found.jpg';
  2148. }
  2149. $sFilename = dirname(__FILE__) . '/cast/' . $cId . '.jpg';
  2150. if (file_exists($sFilename)) {
  2151. return 'cast/' . $cId . '.jpg';
  2152. }
  2153. $aCurlInfo = self::runCurl($sUrl, true);
  2154. $sData = $aCurlInfo['contents'];
  2155. if (false === $sData) {
  2156. return 'cast/not-found.jpg';
  2157. }
  2158. $oFile = fopen($sFilename, 'x');
  2159. fwrite($oFile, $sData);
  2160. fclose($oFile);
  2161. return 'cast/' . $cId . '.jpg';
  2162. }
  2163. }