ApiClient.js 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846
  1. if (!window.MediaBrowser) {
  2. window.MediaBrowser = {};
  3. }
  4. MediaBrowser.ApiClient = function ($, navigator, JSON, WebSocket, setTimeout) {
  5. /**
  6. * Creates a new api client instance
  7. * @param {String} serverProtocol
  8. * @param {String} serverHostName
  9. * @param {String} serverPortNumber
  10. * @param {String} clientName
  11. */
  12. return function (serverProtocol, serverHostName, serverPortNumber, clientName) {
  13. var self = this;
  14. var deviceName = "Web Browser";
  15. var deviceId = MediaBrowser.SHA1(navigator.userAgent + (navigator.cpuClass || ""));
  16. var currentUserId;
  17. var webSocket;
  18. /**
  19. * Gets the server host name.
  20. */
  21. self.serverHostName = function () {
  22. return serverHostName;
  23. };
  24. /**
  25. * Gets the server port number.
  26. */
  27. self.serverPortNumber = function () {
  28. return serverPortNumber;
  29. };
  30. /**
  31. * Gets or sets the current user id.
  32. */
  33. self.currentUserId = function (val) {
  34. if (val !== undefined) {
  35. currentUserId = val;
  36. } else {
  37. return currentUserId;
  38. }
  39. };
  40. /**
  41. * Wraps around jQuery ajax methods to add additional info to the request.
  42. */
  43. self.ajax = function (request) {
  44. if (!request) {
  45. throw new Error("Request cannot be null");
  46. }
  47. var auth = 'MediaBrowser Client="' + clientName + '", Device="' + deviceName + '", DeviceId="' + deviceId + '"';
  48. if (currentUserId) {
  49. auth += ', UserId="' + currentUserId + '"';
  50. }
  51. request.headers = {
  52. Authorization: auth
  53. };
  54. return $.ajax(request);
  55. };
  56. /**
  57. * Creates an api url based on a handler name and query string parameters
  58. * @param {String} name
  59. * @param {Object} params
  60. */
  61. self.getUrl = function (name, params) {
  62. if (!name) {
  63. throw new Error("Url name cannot be empty");
  64. }
  65. var url = serverProtocol + "//" + serverHostName + ":" + serverPortNumber + "/mediabrowser/" + name;
  66. if (params) {
  67. url += "?" + $.param(params);
  68. }
  69. return url;
  70. };
  71. self.openWebSocket = function (port) {
  72. var url = "ws://" + serverHostName + ":" + port + "/mediabrowser";
  73. webSocket = new WebSocket(url);
  74. webSocket.onmessage = function (msg) {
  75. msg = JSON.parse(msg.data);
  76. $(self).trigger("websocketmessage", [msg]);
  77. };
  78. webSocket.onopen = function () {
  79. setTimeout(function () {
  80. $(self).trigger("websocketopen");
  81. }, 500);
  82. };
  83. webSocket.onerror = function () {
  84. setTimeout(function () {
  85. $(self).trigger("websocketerror");
  86. }, 0);
  87. };
  88. webSocket.onclose = function () {
  89. setTimeout(function () {
  90. $(self).trigger("websocketclose");
  91. }, 0);
  92. };
  93. };
  94. self.sendWebSocketMessage = function (name, data) {
  95. var msg = { MessageType: name };
  96. if (data) {
  97. msg.Data = data;
  98. }
  99. msg = JSON.stringify(msg);
  100. webSocket.send(msg);
  101. };
  102. self.isWebSocketOpen = function () {
  103. return webSocket && webSocket.readyState === WebSocket.OPEN;
  104. };
  105. self.isWebSocketOpenOrConnecting = function () {
  106. return webSocket && (webSocket.readyState === WebSocket.OPEN || webSocket.readyState === WebSocket.CONNECTING);
  107. };
  108. /**
  109. * Gets an item from the server
  110. * Omit itemId to get the root folder.
  111. */
  112. self.getItem = function (userId, itemId) {
  113. if (!userId) {
  114. throw new Error("null userId");
  115. }
  116. var url = self.getUrl("Users/" + userId + "/Items/" + itemId);
  117. return self.ajax({
  118. type: "GET",
  119. url: url,
  120. dataType: "json"
  121. });
  122. };
  123. /**
  124. * Gets the root folder from the server
  125. */
  126. self.getRootFolder = function (userId) {
  127. return self.getItem(userId);
  128. };
  129. /**
  130. * Gets the current server status
  131. */
  132. self.getSystemInfo = function () {
  133. var url = self.getUrl("System/Info");
  134. return self.ajax({
  135. type: "GET",
  136. url: url,
  137. dataType: "json"
  138. });
  139. };
  140. /**
  141. * Gets all cultures known to the server
  142. */
  143. self.getCultures = function () {
  144. var url = self.getUrl("Localization/cultures");
  145. return self.ajax({
  146. type: "GET",
  147. url: url,
  148. dataType: "json"
  149. });
  150. };
  151. /**
  152. * Gets all countries known to the server
  153. */
  154. self.getCountries = function () {
  155. var url = self.getUrl("Localization/countries");
  156. return self.ajax({
  157. type: "GET",
  158. url: url,
  159. dataType: "json"
  160. });
  161. };
  162. /**
  163. * Gets plugin security info
  164. */
  165. self.getPluginSecurityInfo = function () {
  166. var url = self.getUrl("Plugins/SecurityInfo");
  167. return self.ajax({
  168. type: "GET",
  169. url: url,
  170. dataType: "json"
  171. });
  172. };
  173. /**
  174. * Gets the directory contents of a path on the server
  175. */
  176. self.getDirectoryContents = function (path, options) {
  177. if (!path) {
  178. throw new Error("null path");
  179. }
  180. options = options || {};
  181. options.path = path;
  182. var url = self.getUrl("Environment/DirectoryContents", options);
  183. return self.ajax({
  184. type: "GET",
  185. url: url,
  186. dataType: "json"
  187. });
  188. };
  189. /**
  190. * Gets a list of physical drives from the server
  191. */
  192. self.getDrives = function () {
  193. var url = self.getUrl("Environment/Drives");
  194. return self.ajax({
  195. type: "GET",
  196. url: url,
  197. dataType: "json"
  198. });
  199. };
  200. /**
  201. * Gets a list of network devices from the server
  202. */
  203. self.getNetworkDevices = function () {
  204. var url = self.getUrl("Environment/NetworkDevices");
  205. return self.ajax({
  206. type: "GET",
  207. url: url,
  208. dataType: "json"
  209. });
  210. };
  211. /**
  212. * Cancels a package installation
  213. */
  214. self.cancelPackageInstallation = function (installationId) {
  215. if (!installationId) {
  216. throw new Error("null installationId");
  217. }
  218. var url = self.getUrl("Packages/Installing/" + id);
  219. return self.ajax({
  220. type: "DELETE",
  221. url: url,
  222. dataType: "json"
  223. });
  224. };
  225. /**
  226. * Installs or updates a new plugin
  227. */
  228. self.installPlugin = function (name, updateClass, version) {
  229. if (!name) {
  230. throw new Error("null name");
  231. }
  232. if (!updateClass) {
  233. throw new Error("null updateClass");
  234. }
  235. var options = {
  236. updateClass: updateClass
  237. };
  238. if (version) {
  239. options.version = version;
  240. }
  241. var url = self.getUrl("Packages/Installed/" + name, options);
  242. return self.ajax({
  243. type: "POST",
  244. url: url
  245. });
  246. };
  247. /**
  248. * Instructs the server to perform a pending kernel reload or app restart.
  249. * If a restart is not currently required, nothing will happen.
  250. */
  251. self.performPendingRestart = function () {
  252. var url = self.getUrl("System/Restart");
  253. return self.ajax({
  254. type: "POST",
  255. url: url
  256. });
  257. };
  258. /**
  259. * Gets information about an installable package
  260. */
  261. self.getPackageInfo = function (name) {
  262. if (!name) {
  263. throw new Error("null name");
  264. }
  265. var url = self.getUrl("Packages/" + name);
  266. return self.ajax({
  267. type: "GET",
  268. url: url,
  269. dataType: "json"
  270. });
  271. };
  272. /**
  273. * Gets the latest available application update (if any)
  274. */
  275. self.getAvailableApplicationUpdate = function () {
  276. var url = self.getUrl("Packages/Updates", { PackageType: "System" });
  277. return self.ajax({
  278. type: "GET",
  279. url: url,
  280. dataType: "json"
  281. });
  282. };
  283. /**
  284. * Gets the latest available plugin updates (if any)
  285. */
  286. self.getAvailablePluginUpdates = function () {
  287. var url = self.getUrl("Packages/Updates", { PackageType: "UserInstalled" });
  288. return self.ajax({
  289. type: "GET",
  290. url: url,
  291. dataType: "json"
  292. });
  293. };
  294. /**
  295. * Gets the virtual folder for a view. Specify a userId to get a user view, or omit for the default view.
  296. */
  297. self.getVirtualFolders = function (userId) {
  298. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  299. url = self.getUrl(url);
  300. return self.ajax({
  301. type: "GET",
  302. url: url,
  303. dataType: "json"
  304. });
  305. };
  306. /**
  307. * Gets all the paths of the locations in the physical root.
  308. */
  309. self.getPhysicalPaths = function () {
  310. var url = self.getUrl("Library/PhysicalPaths");
  311. return self.ajax({
  312. type: "GET",
  313. url: url,
  314. dataType: "json"
  315. });
  316. };
  317. /**
  318. * Gets the current server configuration
  319. */
  320. self.getServerConfiguration = function () {
  321. var url = self.getUrl("System/Configuration");
  322. return self.ajax({
  323. type: "GET",
  324. url: url,
  325. dataType: "json"
  326. });
  327. };
  328. /**
  329. * Gets the server's scheduled tasks
  330. */
  331. self.getScheduledTasks = function () {
  332. var url = self.getUrl("ScheduledTasks");
  333. return self.ajax({
  334. type: "GET",
  335. url: url,
  336. dataType: "json"
  337. });
  338. };
  339. /**
  340. * Starts a scheduled task
  341. */
  342. self.startScheduledTask = function (id) {
  343. if (!id) {
  344. throw new Error("null id");
  345. }
  346. var url = self.getUrl("ScheduledTasks/Running/" + id);
  347. return self.ajax({
  348. type: "POST",
  349. url: url
  350. });
  351. };
  352. /**
  353. * Gets a scheduled task
  354. */
  355. self.getScheduledTask = function (id) {
  356. if (!id) {
  357. throw new Error("null id");
  358. }
  359. var url = self.getUrl("ScheduledTasks/" + id);
  360. return self.ajax({
  361. type: "GET",
  362. url: url,
  363. dataType: "json"
  364. });
  365. };
  366. /**
  367. * Stops a scheduled task
  368. */
  369. self.stopScheduledTask = function (id) {
  370. if (!id) {
  371. throw new Error("null id");
  372. }
  373. var url = self.getUrl("ScheduledTasks/Running/" + id);
  374. return self.ajax({
  375. type: "DELETE",
  376. url: url,
  377. dataType: "json"
  378. });
  379. };
  380. /**
  381. * Gets the configuration of a plugin
  382. * @param {String} Id
  383. */
  384. self.getPluginConfiguration = function (id) {
  385. if (!id) {
  386. throw new Error("null Id");
  387. }
  388. var url = self.getUrl("Plugins/" + id + "/Configuration");
  389. return self.ajax({
  390. type: "GET",
  391. url: url,
  392. dataType: "json"
  393. });
  394. };
  395. /**
  396. * Gets a list of plugins that are available to be installed
  397. */
  398. self.getAvailablePlugins = function () {
  399. var url = self.getUrl("Packages", { PackageType: "UserInstalled" });
  400. return self.ajax({
  401. type: "GET",
  402. url: url,
  403. dataType: "json"
  404. });
  405. };
  406. /**
  407. * Uninstalls a plugin
  408. * @param {String} Id
  409. */
  410. self.uninstallPlugin = function (id) {
  411. if (!id) {
  412. throw new Error("null Id");
  413. }
  414. var url = self.getUrl("Plugins/" + id);
  415. return self.ajax({
  416. type: "DELETE",
  417. url: url,
  418. dataType: "json"
  419. });
  420. };
  421. /**
  422. * Removes a virtual folder from either the default view or a user view
  423. * @param {String} name
  424. */
  425. self.removeVirtualFolder = function (name, userId) {
  426. if (!name) {
  427. throw new Error("null name");
  428. }
  429. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  430. url += "/" + name;
  431. url = self.getUrl(url);
  432. return self.ajax({
  433. type: "DELETE",
  434. url: url,
  435. dataType: "json"
  436. });
  437. };
  438. /**
  439. * Adds a virtual folder to either the default view or a user view
  440. * @param {String} name
  441. */
  442. self.addVirtualFolder = function (name, userId) {
  443. if (!name) {
  444. throw new Error("null name");
  445. }
  446. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  447. url += "/" + name;
  448. url = self.getUrl(url);
  449. return self.ajax({
  450. type: "POST",
  451. url: url
  452. });
  453. };
  454. /**
  455. * Renames a virtual folder, within either the default view or a user view
  456. * @param {String} name
  457. */
  458. self.renameVirtualFolder = function (name, newName, userId) {
  459. if (!name) {
  460. throw new Error("null name");
  461. }
  462. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  463. url += "/" + name + "/Name";
  464. url = self.getUrl(url, { newName: newName });
  465. return self.ajax({
  466. type: "POST",
  467. url: url
  468. });
  469. };
  470. /**
  471. * Adds an additional mediaPath to an existing virtual folder, within either the default view or a user view
  472. * @param {String} name
  473. */
  474. self.addMediaPath = function (virtualFolderName, mediaPath, userId) {
  475. if (!virtualFolderName) {
  476. throw new Error("null virtualFolderName");
  477. }
  478. if (!mediaPath) {
  479. throw new Error("null mediaPath");
  480. }
  481. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  482. url += "/" + virtualFolderName + "/Paths";
  483. url = self.getUrl(url, { path: mediaPath });
  484. return self.ajax({
  485. type: "POST",
  486. url: url
  487. });
  488. };
  489. /**
  490. * Removes a media path from a virtual folder, within either the default view or a user view
  491. * @param {String} name
  492. */
  493. self.removeMediaPath = function (virtualFolderName, mediaPath, userId) {
  494. if (!virtualFolderName) {
  495. throw new Error("null virtualFolderName");
  496. }
  497. if (!mediaPath) {
  498. throw new Error("null mediaPath");
  499. }
  500. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  501. url += "/" + virtualFolderName + "/Paths";
  502. url = self.getUrl(url, { path: mediaPath });
  503. return self.ajax({
  504. type: "DELETE",
  505. url: url,
  506. dataType: "json"
  507. });
  508. };
  509. /**
  510. * Deletes a user
  511. * @param {String} id
  512. */
  513. self.deleteUser = function (id) {
  514. if (!id) {
  515. throw new Error("null id");
  516. }
  517. var url = self.getUrl("Users/" + id);
  518. return self.ajax({
  519. type: "DELETE",
  520. url: url,
  521. dataType: "json"
  522. });
  523. };
  524. /**
  525. * Deletes a user image
  526. * @param {String} userId
  527. * @param {String} imageType The type of image to delete, based on the server-side ImageType enum.
  528. */
  529. self.deleteUserImage = function (userId, imageType) {
  530. if (!userId) {
  531. throw new Error("null userId");
  532. }
  533. if (!imageType) {
  534. throw new Error("null imageType");
  535. }
  536. var url = self.getUrl("Users/" + userId + "/Images/" + imageType);
  537. return self.ajax({
  538. type: "DELETE",
  539. url: url,
  540. dataType: "json"
  541. });
  542. };
  543. /**
  544. * Uploads a user image
  545. * @param {String} userId
  546. * @param {String} imageType The type of image to delete, based on the server-side ImageType enum.
  547. * @param {Object} file The file from the input element
  548. */
  549. self.uploadUserImage = function (userId, imageType, file) {
  550. if (!userId) {
  551. throw new Error("null userId");
  552. }
  553. if (!imageType) {
  554. throw new Error("null imageType");
  555. }
  556. if (!file || !file.type.match('image.*')) {
  557. throw new Error("File must be an image.");
  558. }
  559. var deferred = $.Deferred();
  560. var reader = new FileReader();
  561. reader.onerror = function () {
  562. deferred.reject();
  563. };
  564. reader.onabort = function () {
  565. deferred.reject();
  566. };
  567. // Closure to capture the file information.
  568. reader.onload = function (e) {
  569. var data = window.btoa(e.target.result);
  570. var url = self.getUrl("Users/" + userId + "/Images/" + imageType);
  571. self.ajax({
  572. type: "POST",
  573. url: url,
  574. data: data,
  575. contentType: "image/" + file.name.substring(file.name.lastIndexOf('.') + 1)
  576. }).done(function (result) {
  577. deferred.resolveWith(null, [result]);
  578. }).fail(function () {
  579. deferred.reject();
  580. });
  581. };
  582. // Read in the image file as a data URL.
  583. reader.readAsBinaryString(file);
  584. return deferred.promise();
  585. };
  586. /**
  587. * Gets the list of installed plugins on the server
  588. */
  589. self.getInstalledPlugins = function () {
  590. var url = self.getUrl("Plugins");
  591. return self.ajax({
  592. type: "GET",
  593. url: url,
  594. dataType: "json"
  595. });
  596. };
  597. /**
  598. * Gets a user by id
  599. * @param {String} id
  600. */
  601. self.getUser = function (id) {
  602. if (!id) {
  603. throw new Error("Must supply a userId");
  604. }
  605. var url = self.getUrl("Users/" + id);
  606. return self.ajax({
  607. type: "GET",
  608. url: url,
  609. dataType: "json"
  610. });
  611. };
  612. /**
  613. * Gets a studio
  614. */
  615. self.getStudio = function (name) {
  616. if (!name) {
  617. throw new Error("null name");
  618. }
  619. var url = self.getUrl("Studios/" + name);
  620. return self.ajax({
  621. type: "GET",
  622. url: url,
  623. dataType: "json"
  624. });
  625. };
  626. /**
  627. * Gets a genre
  628. */
  629. self.getGenre = function (name) {
  630. if (!name) {
  631. throw new Error("null name");
  632. }
  633. var url = self.getUrl("Genres/" + name);
  634. return self.ajax({
  635. type: "GET",
  636. url: url,
  637. dataType: "json"
  638. });
  639. };
  640. /**
  641. * Gets a year
  642. */
  643. self.getYear = function (year) {
  644. if (!year) {
  645. throw new Error("null year");
  646. }
  647. var url = self.getUrl("Years/" + year);
  648. return self.ajax({
  649. type: "GET",
  650. url: url,
  651. dataType: "json"
  652. });
  653. };
  654. /**
  655. * Gets a Person
  656. */
  657. self.getPerson = function (name) {
  658. if (!name) {
  659. throw new Error("null name");
  660. }
  661. var url = self.getUrl("Persons/" + name);
  662. return self.ajax({
  663. type: "GET",
  664. url: url,
  665. dataType: "json"
  666. });
  667. };
  668. /**
  669. * Gets weather info
  670. * @param {String} location - us zip code / city, state, country / city, country
  671. * Omit location to get weather info using stored server configuration value
  672. */
  673. self.getWeatherInfo = function (location) {
  674. var url = self.getUrl("weather", {
  675. location: location
  676. });
  677. return self.ajax({
  678. type: "GET",
  679. url: url,
  680. dataType: "json"
  681. });
  682. };
  683. /**
  684. * Gets all users from the server
  685. */
  686. self.getUsers = function () {
  687. var url = self.getUrl("users");
  688. return self.ajax({
  689. type: "GET",
  690. url: url,
  691. dataType: "json"
  692. });
  693. };
  694. /**
  695. * Gets all available parental ratings from the server
  696. */
  697. self.getParentalRatings = function () {
  698. var url = self.getUrl("Localization/ParentalRatings");
  699. return self.ajax({
  700. type: "GET",
  701. url: url,
  702. dataType: "json"
  703. });
  704. };
  705. /**
  706. * Gets a list of all available conrete BaseItem types from the server
  707. */
  708. self.getItemTypes = function (options) {
  709. var url = self.getUrl("Library/ItemTypes", options);
  710. return self.ajax({
  711. type: "GET",
  712. url: url,
  713. dataType: "json"
  714. });
  715. };
  716. /**
  717. * Constructs a url for a user image
  718. * @param {String} userId
  719. * @param {Object} options
  720. * Options supports the following properties:
  721. * width - download the image at a fixed width
  722. * height - download the image at a fixed height
  723. * maxWidth - download the image at a maxWidth
  724. * maxHeight - download the image at a maxHeight
  725. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  726. * For best results do not specify both width and height together, as aspect ratio might be altered.
  727. */
  728. self.getUserImageUrl = function (userId, options) {
  729. if (!userId) {
  730. throw new Error("null userId");
  731. }
  732. options = options || {
  733. };
  734. var url = "Users/" + userId + "/Images/" + options.type;
  735. if (options.index != null) {
  736. url += "/" + options.index;
  737. }
  738. // Don't put these on the query string
  739. delete options.type;
  740. delete options.index;
  741. return self.getUrl(url, options);
  742. };
  743. /**
  744. * Constructs a url for a person image
  745. * @param {String} name
  746. * @param {Object} options
  747. * Options supports the following properties:
  748. * width - download the image at a fixed width
  749. * height - download the image at a fixed height
  750. * maxWidth - download the image at a maxWidth
  751. * maxHeight - download the image at a maxHeight
  752. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  753. * For best results do not specify both width and height together, as aspect ratio might be altered.
  754. */
  755. self.getPersonImageUrl = function (name, options) {
  756. if (!name) {
  757. throw new Error("null name");
  758. }
  759. options = options || {
  760. };
  761. var url = "Persons/" + name + "/Images/" + options.type;
  762. if (options.index != null) {
  763. url += "/" + options.index;
  764. }
  765. // Don't put these on the query string
  766. delete options.type;
  767. delete options.index;
  768. return self.getUrl(url, options);
  769. };
  770. /**
  771. * Constructs a url for a year image
  772. * @param {String} year
  773. * @param {Object} options
  774. * Options supports the following properties:
  775. * width - download the image at a fixed width
  776. * height - download the image at a fixed height
  777. * maxWidth - download the image at a maxWidth
  778. * maxHeight - download the image at a maxHeight
  779. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  780. * For best results do not specify both width and height together, as aspect ratio might be altered.
  781. */
  782. self.getYearImageUrl = function (year, options) {
  783. if (!year) {
  784. throw new Error("null year");
  785. }
  786. options = options || {
  787. };
  788. var url = "Years/" + year + "/Images/" + options.type;
  789. if (options.index != null) {
  790. url += "/" + options.index;
  791. }
  792. // Don't put these on the query string
  793. delete options.type;
  794. delete options.index;
  795. return self.getUrl(url, options);
  796. };
  797. /**
  798. * Constructs a url for a genre image
  799. * @param {String} name
  800. * @param {Object} options
  801. * Options supports the following properties:
  802. * width - download the image at a fixed width
  803. * height - download the image at a fixed height
  804. * maxWidth - download the image at a maxWidth
  805. * maxHeight - download the image at a maxHeight
  806. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  807. * For best results do not specify both width and height together, as aspect ratio might be altered.
  808. */
  809. self.getGenreImageUrl = function (name, options) {
  810. if (!name) {
  811. throw new Error("null name");
  812. }
  813. options = options || {
  814. };
  815. var url = "Genres/" + name + "/Images/" + options.type;
  816. if (options.index != null) {
  817. url += "/" + options.index;
  818. }
  819. // Don't put these on the query string
  820. delete options.type;
  821. delete options.index;
  822. return self.getUrl(url, options);
  823. };
  824. /**
  825. * Constructs a url for a genre image
  826. * @param {String} name
  827. * @param {Object} options
  828. * Options supports the following properties:
  829. * width - download the image at a fixed width
  830. * height - download the image at a fixed height
  831. * maxWidth - download the image at a maxWidth
  832. * maxHeight - download the image at a maxHeight
  833. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  834. * For best results do not specify both width and height together, as aspect ratio might be altered.
  835. */
  836. self.getStudioImageUrl = function (name, options) {
  837. if (!name) {
  838. throw new Error("null name");
  839. }
  840. options = options || {
  841. };
  842. var url = "Studios/" + name + "/Images/" + options.type;
  843. if (options.index != null) {
  844. url += "/" + options.index;
  845. }
  846. // Don't put these on the query string
  847. delete options.type;
  848. delete options.index;
  849. return self.getUrl(url, options);
  850. };
  851. /**
  852. * Constructs a url for an item image
  853. * @param {String} itemId
  854. * @param {Object} options
  855. * Options supports the following properties:
  856. * type - Primary, logo, backdrop, etc. See the server-side enum ImageType
  857. * index - When downloading a backdrop, use this to specify which one (omitting is equivalent to zero)
  858. * width - download the image at a fixed width
  859. * height - download the image at a fixed height
  860. * maxWidth - download the image at a maxWidth
  861. * maxHeight - download the image at a maxHeight
  862. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  863. * For best results do not specify both width and height together, as aspect ratio might be altered.
  864. */
  865. self.getImageUrl = function (itemId, options) {
  866. if (!itemId) {
  867. throw new Error("itemId cannot be empty");
  868. }
  869. options = options || {
  870. };
  871. var url = "Items/" + itemId + "/Images/" + options.type;
  872. if (options.index != null) {
  873. url += "/" + options.index;
  874. }
  875. // Don't put these on the query string
  876. delete options.type;
  877. delete options.index;
  878. return self.getUrl(url, options);
  879. };
  880. /**
  881. * Constructs a url for an item logo image
  882. * If the item doesn't have a logo, it will inherit a logo from a parent
  883. * @param {Object} item A BaseItem
  884. * @param {Object} options
  885. * Options supports the following properties:
  886. * width - download the image at a fixed width
  887. * height - download the image at a fixed height
  888. * maxWidth - download the image at a maxWidth
  889. * maxHeight - download the image at a maxHeight
  890. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  891. * For best results do not specify both width and height together, as aspect ratio might be altered.
  892. */
  893. self.getLogoImageUrl = function (item, options) {
  894. if (!item) {
  895. throw new Error("null item");
  896. }
  897. options = options || {
  898. };
  899. options.imageType = "logo";
  900. var logoItemId = item.HasLogo ? item.Id : item.ParentLogoItemId;
  901. return logoItemId ? self.getImageUrl(logoItemId, options) : null;
  902. };
  903. /**
  904. * Constructs an array of backdrop image url's for an item
  905. * If the item doesn't have any backdrops, it will inherit them from a parent
  906. * @param {Object} item A BaseItem
  907. * @param {Object} options
  908. * Options supports the following properties:
  909. * width - download the image at a fixed width
  910. * height - download the image at a fixed height
  911. * maxWidth - download the image at a maxWidth
  912. * maxHeight - download the image at a maxHeight
  913. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  914. * For best results do not specify both width and height together, as aspect ratio might be altered.
  915. */
  916. self.getBackdropImageUrl = function (item, options) {
  917. if (!item) {
  918. throw new Error("null item");
  919. }
  920. options = options || {
  921. };
  922. options.imageType = "backdrop";
  923. var backdropItemId;
  924. var backdropCount;
  925. if (!item.BackdropCount) {
  926. backdropItemId = item.ParentBackdropItemId;
  927. backdropCount = item.ParentBackdropCount || 0;
  928. } else {
  929. backdropItemId = item.Id;
  930. backdropCount = item.BackdropCount;
  931. }
  932. if (!backdropItemId) {
  933. return [];
  934. }
  935. var files = [];
  936. for (var i = 0; i < backdropCount; i++) {
  937. options.imageIndex = i;
  938. files[i] = self.getImageUrl(backdropItemId, options);
  939. }
  940. return files;
  941. };
  942. /**
  943. * Authenticates a user
  944. * @param {String} userId
  945. * @param {String} password
  946. */
  947. self.authenticateUser = function (userId, password) {
  948. if (!userId) {
  949. throw new Error("null userId");
  950. }
  951. var url = self.getUrl("Users/" + userId + "/authenticate");
  952. var postData = {
  953. password: MediaBrowser.SHA1(password || "")
  954. };
  955. return self.ajax({
  956. type: "POST",
  957. url: url,
  958. data: JSON.stringify(postData),
  959. dataType: "json",
  960. contentType: "application/json"
  961. });
  962. };
  963. /**
  964. * Updates a user's password
  965. * @param {String} userId
  966. * @param {String} currentPassword
  967. * @param {String} newPassword
  968. */
  969. self.updateUserPassword = function (userId, currentPassword, newPassword) {
  970. if (!userId) {
  971. throw new Error("null userId");
  972. }
  973. var url = self.getUrl("Users/" + userId + "/Password");
  974. var postData = {
  975. };
  976. postData.currentPassword = MediaBrowser.SHA1(currentPassword);
  977. if (newPassword) {
  978. postData.newPassword = newPassword;
  979. }
  980. return self.ajax({
  981. type: "POST",
  982. url: url,
  983. data: postData
  984. });
  985. };
  986. /**
  987. * Resets a user's password
  988. * @param {String} userId
  989. */
  990. self.resetUserPassword = function (userId) {
  991. if (!userId) {
  992. throw new Error("null userId");
  993. }
  994. var url = self.getUrl("Users/" + userId + "/Password");
  995. var postData = {
  996. };
  997. postData.resetPassword = true;
  998. return self.ajax({
  999. type: "POST",
  1000. url: url,
  1001. data: postData
  1002. });
  1003. };
  1004. /**
  1005. * Updates the server's configuration
  1006. * @param {Object} configuration
  1007. */
  1008. self.updateServerConfiguration = function (configuration) {
  1009. if (!configuration) {
  1010. throw new Error("null configuration");
  1011. }
  1012. var url = self.getUrl("System/Configuration");
  1013. return self.ajax({
  1014. type: "POST",
  1015. url: url,
  1016. data: JSON.stringify(configuration),
  1017. dataType: "json",
  1018. contentType: "application/json"
  1019. });
  1020. };
  1021. /**
  1022. * Updates plugin security info
  1023. */
  1024. self.updatePluginSecurityInfo = function (info) {
  1025. var url = self.getUrl("Plugins/SecurityInfo");
  1026. return self.ajax({
  1027. type: "POST",
  1028. url: url,
  1029. data: JSON.stringify(info),
  1030. dataType: "json",
  1031. contentType: "application/json"
  1032. });
  1033. };
  1034. /**
  1035. * Creates a user
  1036. * @param {Object} user
  1037. */
  1038. self.createUser = function (user) {
  1039. if (!user) {
  1040. throw new Error("null user");
  1041. }
  1042. var url = self.getUrl("Users");
  1043. return self.ajax({
  1044. type: "POST",
  1045. url: url,
  1046. data: JSON.stringify(user),
  1047. dataType: "json",
  1048. contentType: "application/json"
  1049. });
  1050. };
  1051. /**
  1052. * Updates a user
  1053. * @param {Object} user
  1054. */
  1055. self.updateUser = function (user) {
  1056. if (!user) {
  1057. throw new Error("null user");
  1058. }
  1059. var url = self.getUrl("Users/" + user.Id);
  1060. return self.ajax({
  1061. type: "POST",
  1062. url: url,
  1063. data: JSON.stringify(user),
  1064. dataType: "json",
  1065. contentType: "application/json"
  1066. });
  1067. };
  1068. /**
  1069. * Updates the Triggers for a ScheduledTask
  1070. * @param {String} id
  1071. * @param {Object} triggers
  1072. */
  1073. self.updateScheduledTaskTriggers = function (id, triggers) {
  1074. if (!id) {
  1075. throw new Error("null id");
  1076. }
  1077. if (!triggers) {
  1078. throw new Error("null triggers");
  1079. }
  1080. var url = self.getUrl("ScheduledTasks/" + id + "/Triggers");
  1081. return self.ajax({
  1082. type: "POST",
  1083. url: url,
  1084. data: JSON.stringify(triggers),
  1085. dataType: "json",
  1086. contentType: "application/json"
  1087. });
  1088. };
  1089. /**
  1090. * Updates a plugin's configuration
  1091. * @param {String} Id
  1092. * @param {Object} configuration
  1093. */
  1094. self.updatePluginConfiguration = function (id, configuration) {
  1095. if (!id) {
  1096. throw new Error("null Id");
  1097. }
  1098. if (!configuration) {
  1099. throw new Error("null configuration");
  1100. }
  1101. var url = self.getUrl("Plugins/" + id + "/Configuration");
  1102. return self.ajax({
  1103. type: "POST",
  1104. url: url,
  1105. data: JSON.stringify(configuration),
  1106. dataType: "json",
  1107. contentType: "application/json"
  1108. });
  1109. };
  1110. /**
  1111. * Gets items based on a query, typicall for children of a folder
  1112. * @param {String} userId
  1113. * @param {Object} options
  1114. * Options accepts the following properties:
  1115. * itemId - Localize the search to a specific folder (root if omitted)
  1116. * startIndex - Use for paging
  1117. * limit - Use to limit results to a certain number of items
  1118. * filter - Specify one or more ItemFilters, comma delimeted (see server-side enum)
  1119. * sortBy - Specify an ItemSortBy (comma-delimeted list see server-side enum)
  1120. * sortOrder - ascending/descending
  1121. * fields - additional fields to include aside from basic info. This is a comma delimited list. See server-side enum ItemFields.
  1122. * index - the name of the dynamic, localized index function
  1123. * dynamicSortBy - the name of the dynamic localized sort function
  1124. * recursive - Whether or not the query should be recursive
  1125. * searchTerm - search term to use as a filter
  1126. */
  1127. self.getItems = function (userId, options) {
  1128. if (!userId) {
  1129. throw new Error("null userId");
  1130. }
  1131. var url = self.getUrl("Users/" + userId + "/Items", options);
  1132. return self.ajax({
  1133. type: "GET",
  1134. url: url,
  1135. dataType: "json"
  1136. });
  1137. };
  1138. /**
  1139. * Marks an item as played or unplayed
  1140. * This should not be used to update playstate following playback.
  1141. * There are separate playstate check-in methods for that. This should be used for a
  1142. * separate option to reset playstate.
  1143. * @param {String} userId
  1144. * @param {String} itemId
  1145. * @param {Boolean} wasPlayed
  1146. */
  1147. self.updatePlayedStatus = function (userId, itemId, wasPlayed) {
  1148. if (!userId) {
  1149. throw new Error("null userId");
  1150. }
  1151. if (!itemId) {
  1152. throw new Error("null itemId");
  1153. }
  1154. var url = "Users/" + userId + "/PlayedItems/" + itemId;
  1155. var method = wasPlayed ? "POST" : "DELETE";
  1156. return self.ajax({
  1157. type: method,
  1158. url: url,
  1159. dataType: "json"
  1160. });
  1161. };
  1162. /**
  1163. * Updates a user's favorite status for an item and returns the updated UserItemData object.
  1164. * @param {String} userId
  1165. * @param {String} itemId
  1166. * @param {Boolean} isFavorite
  1167. */
  1168. self.updateFavoriteStatus = function (userId, itemId, isFavorite) {
  1169. if (!userId) {
  1170. throw new Error("null userId");
  1171. }
  1172. if (!itemId) {
  1173. throw new Error("null itemId");
  1174. }
  1175. var url = "Users/" + userId + "/FavoriteItems/" + itemId;
  1176. var method = isFavorite ? "POST" : "DELETE";
  1177. return self.ajax({
  1178. type: method,
  1179. url: url,
  1180. dataType: "json"
  1181. });
  1182. };
  1183. /**
  1184. * Updates a user's personal rating for an item
  1185. * @param {String} userId
  1186. * @param {String} itemId
  1187. * @param {Boolean} likes
  1188. */
  1189. self.updateUserItemRating = function (userId, itemId, likes) {
  1190. if (!userId) {
  1191. throw new Error("null userId");
  1192. }
  1193. if (!itemId) {
  1194. throw new Error("null itemId");
  1195. }
  1196. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating", {
  1197. likes: likes
  1198. });
  1199. return self.ajax({
  1200. type: "POST",
  1201. url: url
  1202. });
  1203. };
  1204. /**
  1205. * Clears a user's personal rating for an item
  1206. * @param {String} userId
  1207. * @param {String} itemId
  1208. */
  1209. self.clearUserItemRating = function (userId, itemId) {
  1210. if (!userId) {
  1211. throw new Error("null userId");
  1212. }
  1213. if (!itemId) {
  1214. throw new Error("null itemId");
  1215. }
  1216. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating");
  1217. return self.ajax({
  1218. type: "DELETE",
  1219. url: url,
  1220. dataType: "json"
  1221. });
  1222. };
  1223. /**
  1224. * Reports the user has started playing something
  1225. * @param {String} userId
  1226. * @param {String} itemId
  1227. */
  1228. self.reportPlaybackStart = function (userId, itemId) {
  1229. if (!userId) {
  1230. throw new Error("null userId");
  1231. }
  1232. if (!itemId) {
  1233. throw new Error("null itemId");
  1234. }
  1235. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId);
  1236. return self.ajax({
  1237. type: "POST",
  1238. url: url,
  1239. dataType: "json"
  1240. });
  1241. };
  1242. /**
  1243. * Reports progress viewing an item
  1244. * @param {String} userId
  1245. * @param {String} itemId
  1246. */
  1247. self.reportPlaybackProgress = function (userId, itemId, positionTicks) {
  1248. if (!userId) {
  1249. throw new Error("null userId");
  1250. }
  1251. if (!itemId) {
  1252. throw new Error("null itemId");
  1253. }
  1254. var params = {
  1255. };
  1256. if (positionTicks) {
  1257. params.positionTicks = positionTicks;
  1258. }
  1259. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId + "/Progress", params);
  1260. return self.ajax({
  1261. type: "POST",
  1262. url: url,
  1263. dataType: "json"
  1264. });
  1265. };
  1266. /**
  1267. * Reports a user has stopped playing an item
  1268. * @param {String} userId
  1269. * @param {String} itemId
  1270. */
  1271. self.reportPlaybackStopped = function (userId, itemId, positionTicks) {
  1272. if (!userId) {
  1273. throw new Error("null userId");
  1274. }
  1275. if (!itemId) {
  1276. throw new Error("null itemId");
  1277. }
  1278. var params = {
  1279. };
  1280. if (positionTicks) {
  1281. params.positionTicks = positionTicks;
  1282. }
  1283. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId, params);
  1284. return self.ajax({
  1285. type: "DELETE",
  1286. url: url,
  1287. dataType: "json"
  1288. });
  1289. };
  1290. }
  1291. }(jQuery, navigator, JSON, window.WebSocket, setTimeout);
  1292. /**
  1293. * Provides a friendly way to create an api client instance using information from the browser's current url
  1294. */
  1295. MediaBrowser.ApiClient.create = function (clientName) {
  1296. var loc = window.location;
  1297. return new MediaBrowser.ApiClient(loc.protocol, loc.hostname, loc.port, clientName);
  1298. };
  1299. /**
  1300. *
  1301. * Secure Hash Algorithm (SHA1)
  1302. * http://www.webtoolkit.info/
  1303. *
  1304. **/
  1305. MediaBrowser.SHA1 = function (msg) {
  1306. function rotate_left(n, s) {
  1307. var t4 = (n << s) | (n >>> (32 - s));
  1308. return t4;
  1309. };
  1310. function lsb_hex(val) {
  1311. var str = "";
  1312. var i;
  1313. var vh;
  1314. var vl;
  1315. for (i = 0; i <= 6; i += 2) {
  1316. vh = (val >>> (i * 4 + 4)) & 0x0f;
  1317. vl = (val >>> (i * 4)) & 0x0f;
  1318. str += vh.toString(16) + vl.toString(16);
  1319. }
  1320. return str;
  1321. };
  1322. function cvt_hex(val) {
  1323. var str = "";
  1324. var i;
  1325. var v;
  1326. for (i = 7; i >= 0; i--) {
  1327. v = (val >>> (i * 4)) & 0x0f;
  1328. str += v.toString(16);
  1329. }
  1330. return str;
  1331. };
  1332. function Utf8Encode(string) {
  1333. string = string.replace(/\r\n/g, "\n");
  1334. var utftext = "";
  1335. for (var n = 0; n < string.length; n++) {
  1336. var c = string.charCodeAt(n);
  1337. if (c < 128) {
  1338. utftext += String.fromCharCode(c);
  1339. }
  1340. else if ((c > 127) && (c < 2048)) {
  1341. utftext += String.fromCharCode((c >> 6) | 192);
  1342. utftext += String.fromCharCode((c & 63) | 128);
  1343. }
  1344. else {
  1345. utftext += String.fromCharCode((c >> 12) | 224);
  1346. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  1347. utftext += String.fromCharCode((c & 63) | 128);
  1348. }
  1349. }
  1350. return utftext;
  1351. };
  1352. var blockstart;
  1353. var i, j;
  1354. var W = new Array(80);
  1355. var H0 = 0x67452301;
  1356. var H1 = 0xEFCDAB89;
  1357. var H2 = 0x98BADCFE;
  1358. var H3 = 0x10325476;
  1359. var H4 = 0xC3D2E1F0;
  1360. var A, B, C, D, E;
  1361. var temp;
  1362. msg = Utf8Encode(msg);
  1363. var msg_len = msg.length;
  1364. var word_array = new Array();
  1365. for (i = 0; i < msg_len - 3; i += 4) {
  1366. j = msg.charCodeAt(i) << 24 | msg.charCodeAt(i + 1) << 16 |
  1367. msg.charCodeAt(i + 2) << 8 | msg.charCodeAt(i + 3);
  1368. word_array.push(j);
  1369. }
  1370. switch (msg_len % 4) {
  1371. case 0:
  1372. i = 0x080000000;
  1373. break;
  1374. case 1:
  1375. i = msg.charCodeAt(msg_len - 1) << 24 | 0x0800000;
  1376. break;
  1377. case 2:
  1378. i = msg.charCodeAt(msg_len - 2) << 24 | msg.charCodeAt(msg_len - 1) << 16 | 0x08000;
  1379. break;
  1380. case 3:
  1381. i = msg.charCodeAt(msg_len - 3) << 24 | msg.charCodeAt(msg_len - 2) << 16 | msg.charCodeAt(msg_len - 1) << 8 | 0x80;
  1382. break;
  1383. }
  1384. word_array.push(i);
  1385. while ((word_array.length % 16) != 14) word_array.push(0);
  1386. word_array.push(msg_len >>> 29);
  1387. word_array.push((msg_len << 3) & 0x0ffffffff);
  1388. for (blockstart = 0; blockstart < word_array.length; blockstart += 16) {
  1389. for (i = 0; i < 16; i++) W[i] = word_array[blockstart + i];
  1390. for (i = 16; i <= 79; i++) W[i] = rotate_left(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1);
  1391. A = H0;
  1392. B = H1;
  1393. C = H2;
  1394. D = H3;
  1395. E = H4;
  1396. for (i = 0; i <= 19; i++) {
  1397. temp = (rotate_left(A, 5) + ((B & C) | (~B & D)) + E + W[i] + 0x5A827999) & 0x0ffffffff;
  1398. E = D;
  1399. D = C;
  1400. C = rotate_left(B, 30);
  1401. B = A;
  1402. A = temp;
  1403. }
  1404. for (i = 20; i <= 39; i++) {
  1405. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0x6ED9EBA1) & 0x0ffffffff;
  1406. E = D;
  1407. D = C;
  1408. C = rotate_left(B, 30);
  1409. B = A;
  1410. A = temp;
  1411. }
  1412. for (i = 40; i <= 59; i++) {
  1413. temp = (rotate_left(A, 5) + ((B & C) | (B & D) | (C & D)) + E + W[i] + 0x8F1BBCDC) & 0x0ffffffff;
  1414. E = D;
  1415. D = C;
  1416. C = rotate_left(B, 30);
  1417. B = A;
  1418. A = temp;
  1419. }
  1420. for (i = 60; i <= 79; i++) {
  1421. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0xCA62C1D6) & 0x0ffffffff;
  1422. E = D;
  1423. D = C;
  1424. C = rotate_left(B, 30);
  1425. B = A;
  1426. A = temp;
  1427. }
  1428. H0 = (H0 + A) & 0x0ffffffff;
  1429. H1 = (H1 + B) & 0x0ffffffff;
  1430. H2 = (H2 + C) & 0x0ffffffff;
  1431. H3 = (H3 + D) & 0x0ffffffff;
  1432. H4 = (H4 + E) & 0x0ffffffff;
  1433. }
  1434. var temp = cvt_hex(H0) + cvt_hex(H1) + cvt_hex(H2) + cvt_hex(H3) + cvt_hex(H4);
  1435. return temp.toLowerCase();
  1436. }