2
0

ApiClient.js 52 KB

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