ApiClient.js 53 KB

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