ApiClient.js 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866
  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. self.deviceId = function () {
  84. return deviceId;
  85. };
  86. self.encodeName = function (name) {
  87. name = name.split('/').join('-');
  88. name = name.split('?').join('-');
  89. var val = $.param({ name: name });
  90. return val.substring(val.indexOf('=') + 1).replace("'", '%27');
  91. };
  92. /**
  93. * Wraps around jQuery ajax methods to add additional info to the request.
  94. */
  95. self.ajax = function (request) {
  96. if (!request) {
  97. throw new Error("Request cannot be null");
  98. }
  99. if (clientName) {
  100. var auth = 'MediaBrowser Client="' + clientName + '", Device="' + deviceName + '", DeviceId="' + deviceId + '"';
  101. if (currentUserId) {
  102. auth += ', UserId="' + currentUserId + '"';
  103. }
  104. request.headers = {
  105. Authorization: auth
  106. };
  107. }
  108. return $.ajax(request);
  109. };
  110. /**
  111. * Creates an api url based on a handler name and query string parameters
  112. * @param {String} name
  113. * @param {Object} params
  114. */
  115. self.getUrl = function (name, params) {
  116. if (!name) {
  117. throw new Error("Url name cannot be empty");
  118. }
  119. var url = serverProtocol + "//" + serverHostName + ":" + serverPortNumber + "/mediabrowser/" + name;
  120. if (params) {
  121. url += "?" + $.param(params);
  122. }
  123. return url;
  124. };
  125. self.openWebSocket = function (port) {
  126. var url = "ws://" + serverHostName + ":" + port + "/mediabrowser";
  127. webSocket = new WebSocket(url);
  128. webSocket.onmessage = function (msg) {
  129. msg = JSON.parse(msg.data);
  130. $(self).trigger("websocketmessage", [msg]);
  131. };
  132. webSocket.onopen = function () {
  133. setTimeout(function () {
  134. self.sendWebSocketMessage("Identity", clientName + "|" + deviceId);
  135. $(self).trigger("websocketopen");
  136. }, 500);
  137. };
  138. webSocket.onerror = function () {
  139. setTimeout(function () {
  140. $(self).trigger("websocketerror");
  141. }, 0);
  142. };
  143. webSocket.onclose = function () {
  144. setTimeout(function () {
  145. $(self).trigger("websocketclose");
  146. }, 0);
  147. };
  148. };
  149. self.sendWebSocketMessage = function (name, data) {
  150. var msg = { MessageType: name };
  151. if (data) {
  152. msg.Data = data;
  153. }
  154. msg = JSON.stringify(msg);
  155. webSocket.send(msg);
  156. };
  157. self.isWebSocketOpen = function () {
  158. return webSocket && webSocket.readyState === WebSocket.OPEN;
  159. };
  160. self.isWebSocketOpenOrConnecting = function () {
  161. return webSocket && (webSocket.readyState === WebSocket.OPEN || webSocket.readyState === WebSocket.CONNECTING);
  162. };
  163. /**
  164. * Gets an item from the server
  165. * Omit itemId to get the root folder.
  166. */
  167. self.getItem = function (userId, itemId) {
  168. if (!userId) {
  169. throw new Error("null userId");
  170. }
  171. var url = self.getUrl("Users/" + userId + "/Items/" + itemId);
  172. return self.ajax({
  173. type: "GET",
  174. url: url,
  175. dataType: "json"
  176. });
  177. };
  178. /**
  179. * Gets the root folder from the server
  180. */
  181. self.getRootFolder = function (userId) {
  182. if (!userId) {
  183. throw new Error("null userId");
  184. }
  185. var url = self.getUrl("Users/" + userId + "/Items/Root");
  186. return self.ajax({
  187. type: "GET",
  188. url: url,
  189. dataType: "json"
  190. });
  191. };
  192. /**
  193. * Gets the current server status
  194. */
  195. self.getSystemInfo = function () {
  196. var url = self.getUrl("System/Info");
  197. return self.ajax({
  198. type: "GET",
  199. url: url,
  200. dataType: "json"
  201. });
  202. };
  203. self.getSimilarMovies = function (itemId, options) {
  204. var url = self.getUrl("Movies/" + itemId + "/Similar", options);
  205. return self.ajax({
  206. type: "GET",
  207. url: url,
  208. dataType: "json"
  209. });
  210. };
  211. self.getSimilarTrailers = function (itemId, options) {
  212. var url = self.getUrl("Trailers/" + itemId + "/Similar", options);
  213. return self.ajax({
  214. type: "GET",
  215. url: url,
  216. dataType: "json"
  217. });
  218. };
  219. self.getSimilarShows = function (itemId, options) {
  220. var url = self.getUrl("Shows/" + itemId + "/Similar", options);
  221. return self.ajax({
  222. type: "GET",
  223. url: url,
  224. dataType: "json"
  225. });
  226. };
  227. self.getSimilarAlbums = function (itemId, options) {
  228. var url = self.getUrl("Albums/" + itemId + "/Similar", options);
  229. return self.ajax({
  230. type: "GET",
  231. url: url,
  232. dataType: "json"
  233. });
  234. };
  235. self.getSimilarGames = function (itemId, options) {
  236. var url = self.getUrl("Games/" + itemId + "/Similar", options);
  237. return self.ajax({
  238. type: "GET",
  239. url: url,
  240. dataType: "json"
  241. });
  242. };
  243. /**
  244. * Gets all cultures known to the server
  245. */
  246. self.getCultures = function () {
  247. var url = self.getUrl("Localization/cultures");
  248. return self.ajax({
  249. type: "GET",
  250. url: url,
  251. dataType: "json"
  252. });
  253. };
  254. /**
  255. * Gets all countries known to the server
  256. */
  257. self.getCountries = function () {
  258. var url = self.getUrl("Localization/countries");
  259. return self.ajax({
  260. type: "GET",
  261. url: url,
  262. dataType: "json"
  263. });
  264. };
  265. /**
  266. * Gets plugin security info
  267. */
  268. self.getPluginSecurityInfo = function () {
  269. var url = self.getUrl("Plugins/SecurityInfo");
  270. return self.ajax({
  271. type: "GET",
  272. url: url,
  273. dataType: "json"
  274. });
  275. };
  276. /**
  277. * Gets the directory contents of a path on the server
  278. */
  279. self.getDirectoryContents = function (path, options) {
  280. if (!path) {
  281. throw new Error("null path");
  282. }
  283. options = options || {};
  284. options.path = path;
  285. var url = self.getUrl("Environment/DirectoryContents", options);
  286. return self.ajax({
  287. type: "GET",
  288. url: url,
  289. dataType: "json"
  290. });
  291. };
  292. /**
  293. * Gets a list of physical drives from the server
  294. */
  295. self.getDrives = function () {
  296. var url = self.getUrl("Environment/Drives");
  297. return self.ajax({
  298. type: "GET",
  299. url: url,
  300. dataType: "json"
  301. });
  302. };
  303. /**
  304. * Gets a list of network devices from the server
  305. */
  306. self.getNetworkDevices = function () {
  307. var url = self.getUrl("Environment/NetworkDevices");
  308. return self.ajax({
  309. type: "GET",
  310. url: url,
  311. dataType: "json"
  312. });
  313. };
  314. /**
  315. * Cancels a package installation
  316. */
  317. self.cancelPackageInstallation = function (installationId) {
  318. if (!installationId) {
  319. throw new Error("null installationId");
  320. }
  321. var url = self.getUrl("Packages/Installing/" + id);
  322. return self.ajax({
  323. type: "DELETE",
  324. url: url
  325. });
  326. };
  327. /**
  328. * Refreshes metadata for an item
  329. */
  330. self.refreshItem = function (itemId, force, recursive) {
  331. if (!itemId) {
  332. throw new Error("null itemId");
  333. }
  334. var url = self.getUrl("Items/" + itemId + "/Refresh", {
  335. forced: force || false,
  336. recursive: recursive || false
  337. });
  338. return self.ajax({
  339. type: "POST",
  340. url: url
  341. });
  342. };
  343. /**
  344. * Installs or updates a new plugin
  345. */
  346. self.installPlugin = function (name, updateClass, version) {
  347. if (!name) {
  348. throw new Error("null name");
  349. }
  350. if (!updateClass) {
  351. throw new Error("null updateClass");
  352. }
  353. var options = {
  354. updateClass: updateClass
  355. };
  356. if (version) {
  357. options.version = version;
  358. }
  359. var url = self.getUrl("Packages/Installed/" + name, options);
  360. return self.ajax({
  361. type: "POST",
  362. url: url
  363. });
  364. };
  365. /**
  366. * Instructs the server to perform a pending kernel reload or app restart.
  367. * If a restart is not currently required, nothing will happen.
  368. */
  369. self.performPendingRestart = function () {
  370. var url = self.getUrl("System/Restart");
  371. return self.ajax({
  372. type: "POST",
  373. url: url
  374. });
  375. };
  376. /**
  377. * Gets information about an installable package
  378. */
  379. self.getPackageInfo = function (name) {
  380. if (!name) {
  381. throw new Error("null name");
  382. }
  383. var url = self.getUrl("Packages/" + name);
  384. return self.ajax({
  385. type: "GET",
  386. url: url,
  387. dataType: "json"
  388. });
  389. };
  390. /**
  391. * Gets the latest available application update (if any)
  392. */
  393. self.getAvailableApplicationUpdate = function () {
  394. var url = self.getUrl("Packages/Updates", { PackageType: "System" });
  395. return self.ajax({
  396. type: "GET",
  397. url: url,
  398. dataType: "json"
  399. });
  400. };
  401. /**
  402. * Gets the latest available plugin updates (if any)
  403. */
  404. self.getAvailablePluginUpdates = function () {
  405. var url = self.getUrl("Packages/Updates", { PackageType: "UserInstalled" });
  406. return self.ajax({
  407. type: "GET",
  408. url: url,
  409. dataType: "json"
  410. });
  411. };
  412. /**
  413. * Gets the virtual folder for a view. Specify a userId to get a user view, or omit for the default view.
  414. */
  415. self.getVirtualFolders = function (userId) {
  416. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  417. url = self.getUrl(url);
  418. return self.ajax({
  419. type: "GET",
  420. url: url,
  421. dataType: "json"
  422. });
  423. };
  424. /**
  425. * Gets all the paths of the locations in the physical root.
  426. */
  427. self.getPhysicalPaths = function () {
  428. var url = self.getUrl("Library/PhysicalPaths");
  429. return self.ajax({
  430. type: "GET",
  431. url: url,
  432. dataType: "json"
  433. });
  434. };
  435. /**
  436. * Gets the current server configuration
  437. */
  438. self.getServerConfiguration = function () {
  439. var url = self.getUrl("System/Configuration");
  440. return self.ajax({
  441. type: "GET",
  442. url: url,
  443. dataType: "json"
  444. });
  445. };
  446. /**
  447. * Gets the server's scheduled tasks
  448. */
  449. self.getScheduledTasks = function () {
  450. var url = self.getUrl("ScheduledTasks");
  451. return self.ajax({
  452. type: "GET",
  453. url: url,
  454. dataType: "json"
  455. });
  456. };
  457. /**
  458. * Starts a scheduled task
  459. */
  460. self.startScheduledTask = function (id) {
  461. if (!id) {
  462. throw new Error("null id");
  463. }
  464. var url = self.getUrl("ScheduledTasks/Running/" + id);
  465. return self.ajax({
  466. type: "POST",
  467. url: url
  468. });
  469. };
  470. /**
  471. * Gets a scheduled task
  472. */
  473. self.getScheduledTask = function (id) {
  474. if (!id) {
  475. throw new Error("null id");
  476. }
  477. var url = self.getUrl("ScheduledTasks/" + id);
  478. return self.ajax({
  479. type: "GET",
  480. url: url,
  481. dataType: "json"
  482. });
  483. };
  484. self.getNextUpEpisodes = function (options) {
  485. var url = self.getUrl("Shows/NextUp", options);
  486. return self.ajax({
  487. type: "GET",
  488. url: url,
  489. dataType: "json"
  490. });
  491. };
  492. /**
  493. * Stops a scheduled task
  494. */
  495. self.stopScheduledTask = function (id) {
  496. if (!id) {
  497. throw new Error("null id");
  498. }
  499. var url = self.getUrl("ScheduledTasks/Running/" + id);
  500. return self.ajax({
  501. type: "DELETE",
  502. url: url
  503. });
  504. };
  505. /**
  506. * Gets the configuration of a plugin
  507. * @param {String} Id
  508. */
  509. self.getPluginConfiguration = function (id) {
  510. if (!id) {
  511. throw new Error("null Id");
  512. }
  513. var url = self.getUrl("Plugins/" + id + "/Configuration");
  514. return self.ajax({
  515. type: "GET",
  516. url: url,
  517. dataType: "json"
  518. });
  519. };
  520. /**
  521. * Gets a list of plugins that are available to be installed
  522. */
  523. self.getAvailablePlugins = function (options) {
  524. options = $.extend({}, options || {});
  525. options.PackageType = "UserInstalled";
  526. var url = self.getUrl("Packages", options);
  527. return self.ajax({
  528. type: "GET",
  529. url: url,
  530. dataType: "json"
  531. });
  532. };
  533. /**
  534. * Uninstalls a plugin
  535. * @param {String} Id
  536. */
  537. self.uninstallPlugin = function (id) {
  538. if (!id) {
  539. throw new Error("null Id");
  540. }
  541. var url = self.getUrl("Plugins/" + id);
  542. return self.ajax({
  543. type: "DELETE",
  544. url: url
  545. });
  546. };
  547. /**
  548. * Removes a virtual folder from either the default view or a user view
  549. * @param {String} name
  550. */
  551. self.removeVirtualFolder = function (name, userId) {
  552. if (!name) {
  553. throw new Error("null name");
  554. }
  555. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  556. url += "/" + name;
  557. url = self.getUrl(url);
  558. return self.ajax({
  559. type: "DELETE",
  560. url: url
  561. });
  562. };
  563. /**
  564. * Adds a virtual folder to either the default view or a user view
  565. * @param {String} name
  566. */
  567. self.addVirtualFolder = function (name, userId) {
  568. if (!name) {
  569. throw new Error("null name");
  570. }
  571. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  572. url += "/" + name;
  573. url = self.getUrl(url);
  574. return self.ajax({
  575. type: "POST",
  576. url: url
  577. });
  578. };
  579. /**
  580. * Renames a virtual folder, within either the default view or a user view
  581. * @param {String} name
  582. */
  583. self.renameVirtualFolder = function (name, newName, userId) {
  584. if (!name) {
  585. throw new Error("null name");
  586. }
  587. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  588. url += "/" + name + "/Name";
  589. url = self.getUrl(url, { newName: newName });
  590. return self.ajax({
  591. type: "POST",
  592. url: url
  593. });
  594. };
  595. /**
  596. * Adds an additional mediaPath to an existing virtual folder, within either the default view or a user view
  597. * @param {String} name
  598. */
  599. self.addMediaPath = function (virtualFolderName, mediaPath, userId) {
  600. if (!virtualFolderName) {
  601. throw new Error("null virtualFolderName");
  602. }
  603. if (!mediaPath) {
  604. throw new Error("null mediaPath");
  605. }
  606. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  607. url += "/" + virtualFolderName + "/Paths";
  608. url = self.getUrl(url, { path: mediaPath });
  609. return self.ajax({
  610. type: "POST",
  611. url: url
  612. });
  613. };
  614. /**
  615. * Removes a media path from a virtual folder, within either the default view or a user view
  616. * @param {String} name
  617. */
  618. self.removeMediaPath = function (virtualFolderName, mediaPath, userId) {
  619. if (!virtualFolderName) {
  620. throw new Error("null virtualFolderName");
  621. }
  622. if (!mediaPath) {
  623. throw new Error("null mediaPath");
  624. }
  625. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  626. url += "/" + virtualFolderName + "/Paths";
  627. url = self.getUrl(url, { path: mediaPath });
  628. return self.ajax({
  629. type: "DELETE",
  630. url: url
  631. });
  632. };
  633. /**
  634. * Deletes a user
  635. * @param {String} id
  636. */
  637. self.deleteUser = function (id) {
  638. if (!id) {
  639. throw new Error("null id");
  640. }
  641. var url = self.getUrl("Users/" + id);
  642. return self.ajax({
  643. type: "DELETE",
  644. url: url
  645. });
  646. };
  647. /**
  648. * Deletes a user image
  649. * @param {String} userId
  650. * @param {String} imageType The type of image to delete, based on the server-side ImageType enum.
  651. */
  652. self.deleteUserImage = function (userId, imageType, imageIndex) {
  653. if (!userId) {
  654. throw new Error("null userId");
  655. }
  656. if (!imageType) {
  657. throw new Error("null imageType");
  658. }
  659. var url = self.getUrl("Users/" + userId + "/Images/" + imageType);
  660. if (imageIndex != null) {
  661. url += "/" + imageIndex;
  662. }
  663. return self.ajax({
  664. type: "DELETE",
  665. url: url
  666. });
  667. };
  668. self.deleteItemImage = function (itemId, imageType, imageIndex) {
  669. if (!itemId) {
  670. throw new Error("null itemId");
  671. }
  672. if (!imageType) {
  673. throw new Error("null imageType");
  674. }
  675. var url = self.getUrl("Items/" + itemId + "/Images/" + imageType);
  676. if (imageIndex != null) {
  677. url += "/" + imageIndex;
  678. }
  679. return self.ajax({
  680. type: "DELETE",
  681. url: url
  682. });
  683. };
  684. self.updateItemImageIndex = function (itemId, imageType, imageIndex, newIndex) {
  685. if (!itemId) {
  686. throw new Error("null itemId");
  687. }
  688. if (!imageType) {
  689. throw new Error("null imageType");
  690. }
  691. var url = self.getUrl("Items/" + itemId + "/Images/" + imageType + "/" + imageIndex + "/Index", { newIndex: newIndex });
  692. return self.ajax({
  693. type: "POST",
  694. url: url
  695. });
  696. };
  697. self.getItemImageInfos = function (itemId) {
  698. if (!itemId) {
  699. throw new Error("null itemId");
  700. }
  701. var url = self.getUrl("Items/" + itemId + "/Images");
  702. return self.ajax({
  703. type: "GET",
  704. url: url,
  705. dataType: "json"
  706. });
  707. };
  708. self.getCriticReviews = function (itemId, options) {
  709. if (!itemId) {
  710. throw new Error("null itemId");
  711. }
  712. var url = self.getUrl("Items/" + itemId + "/CriticReviews", options);
  713. return self.ajax({
  714. type: "GET",
  715. url: url,
  716. dataType: "json"
  717. });
  718. };
  719. self.getSessions = function (options) {
  720. var url = self.getUrl("Sessions", options);
  721. return self.ajax({
  722. type: "GET",
  723. url: url,
  724. dataType: "json"
  725. });
  726. };
  727. /**
  728. * Uploads a user image
  729. * @param {String} userId
  730. * @param {String} imageType The type of image to delete, based on the server-side ImageType enum.
  731. * @param {Object} file The file from the input element
  732. */
  733. self.uploadUserImage = function (userId, imageType, file) {
  734. if (!userId) {
  735. throw new Error("null userId");
  736. }
  737. if (!imageType) {
  738. throw new Error("null imageType");
  739. }
  740. if (!file) {
  741. throw new Error("File must be an image.");
  742. }
  743. if (file.type != "image/png" && file.type != "image/jpeg" && file.type != "image/jpeg") {
  744. throw new Error("File must be an image.");
  745. }
  746. var deferred = $.Deferred();
  747. var reader = new FileReader();
  748. reader.onerror = function () {
  749. deferred.reject();
  750. };
  751. reader.onabort = function () {
  752. deferred.reject();
  753. };
  754. // Closure to capture the file information.
  755. reader.onload = function (e) {
  756. // Split by a comma to remove the url: prefix
  757. var data = e.target.result.split(',')[1];
  758. var url = self.getUrl("Users/" + userId + "/Images/" + imageType);
  759. self.ajax({
  760. type: "POST",
  761. url: url,
  762. data: data,
  763. contentType: "image/" + file.name.substring(file.name.lastIndexOf('.') + 1)
  764. }).done(function (result) {
  765. deferred.resolveWith(null, [result]);
  766. }).fail(function () {
  767. deferred.reject();
  768. });
  769. };
  770. // Read in the image file as a data URL.
  771. reader.readAsDataURL(file);
  772. return deferred.promise();
  773. };
  774. self.uploadItemImage = function (itemId, imageType, file) {
  775. if (!itemId) {
  776. throw new Error("null itemId");
  777. }
  778. if (!imageType) {
  779. throw new Error("null imageType");
  780. }
  781. if (!file) {
  782. throw new Error("File must be an image.");
  783. }
  784. if (file.type != "image/png" && file.type != "image/jpeg" && file.type != "image/jpeg") {
  785. throw new Error("File must be an image.");
  786. }
  787. var deferred = $.Deferred();
  788. var reader = new FileReader();
  789. reader.onerror = function () {
  790. deferred.reject();
  791. };
  792. reader.onabort = function () {
  793. deferred.reject();
  794. };
  795. // Closure to capture the file information.
  796. reader.onload = function (e) {
  797. // Split by a comma to remove the url: prefix
  798. var data = e.target.result.split(',')[1];
  799. var url = self.getUrl("Items/" + itemId + "/Images/" + imageType);
  800. self.ajax({
  801. type: "POST",
  802. url: url,
  803. data: data,
  804. contentType: "image/" + file.name.substring(file.name.lastIndexOf('.') + 1)
  805. }).done(function (result) {
  806. deferred.resolveWith(null, [result]);
  807. }).fail(function () {
  808. deferred.reject();
  809. });
  810. };
  811. // Read in the image file as a data URL.
  812. reader.readAsDataURL(file);
  813. return deferred.promise();
  814. };
  815. /**
  816. * Gets the list of installed plugins on the server
  817. */
  818. self.getInstalledPlugins = function () {
  819. var url = self.getUrl("Plugins");
  820. return self.ajax({
  821. type: "GET",
  822. url: url,
  823. dataType: "json"
  824. });
  825. };
  826. /**
  827. * Gets a user by id
  828. * @param {String} id
  829. */
  830. self.getUser = function (id) {
  831. if (!id) {
  832. throw new Error("Must supply a userId");
  833. }
  834. var url = self.getUrl("Users/" + id);
  835. return self.ajax({
  836. type: "GET",
  837. url: url,
  838. dataType: "json"
  839. });
  840. };
  841. /**
  842. * Gets a studio
  843. */
  844. self.getStudio = function (name, userId) {
  845. if (!name) {
  846. throw new Error("null name");
  847. }
  848. var options = {};
  849. if (userId) {
  850. options.userId = userId;
  851. }
  852. var url = self.getUrl("Studios/" + self.encodeName(name), options);
  853. return self.ajax({
  854. type: "GET",
  855. url: url,
  856. dataType: "json"
  857. });
  858. };
  859. /**
  860. * Gets a genre
  861. */
  862. self.getGenre = function (name, userId) {
  863. if (!name) {
  864. throw new Error("null name");
  865. }
  866. var options = {};
  867. if (userId) {
  868. options.userId = userId;
  869. }
  870. var url = self.getUrl("Genres/" + self.encodeName(name), options);
  871. return self.ajax({
  872. type: "GET",
  873. url: url,
  874. dataType: "json"
  875. });
  876. };
  877. /**
  878. * Gets an artist
  879. */
  880. self.getArtist = function (name, userId) {
  881. if (!name) {
  882. throw new Error("null name");
  883. }
  884. var options = {};
  885. if (userId) {
  886. options.userId = userId;
  887. }
  888. var url = self.getUrl("Artists/" + self.encodeName(name), options);
  889. return self.ajax({
  890. type: "GET",
  891. url: url,
  892. dataType: "json"
  893. });
  894. };
  895. /**
  896. * Gets a year
  897. */
  898. self.getYear = function (yea, userId) {
  899. if (!name) {
  900. throw new Error("null name");
  901. }
  902. var options = {};
  903. if (userId) {
  904. options.userId = userId;
  905. }
  906. var url = self.getUrl("Years/" + self.encodeName(name), options);
  907. return self.ajax({
  908. type: "GET",
  909. url: url,
  910. dataType: "json"
  911. });
  912. };
  913. /**
  914. * Gets a Person
  915. */
  916. self.getPerson = function (name, userId) {
  917. if (!name) {
  918. throw new Error("null name");
  919. }
  920. var options = {};
  921. if (userId) {
  922. options.userId = userId;
  923. }
  924. var url = self.getUrl("Persons/" + self.encodeName(name), options);
  925. return self.ajax({
  926. type: "GET",
  927. url: url,
  928. dataType: "json"
  929. });
  930. };
  931. /**
  932. * Gets weather info
  933. * @param {String} location - us zip code / city, state, country / city, country
  934. * Omit location to get weather info using stored server configuration value
  935. */
  936. self.getWeatherInfo = function (location) {
  937. var url = self.getUrl("weather", {
  938. location: location
  939. });
  940. return self.ajax({
  941. type: "GET",
  942. url: url,
  943. dataType: "json"
  944. });
  945. };
  946. /**
  947. * Gets all users from the server
  948. */
  949. self.getUsers = function () {
  950. var url = self.getUrl("users");
  951. return self.ajax({
  952. type: "GET",
  953. url: url,
  954. dataType: "json"
  955. });
  956. };
  957. /**
  958. * Gets all available parental ratings from the server
  959. */
  960. self.getParentalRatings = function () {
  961. var url = self.getUrl("Localization/ParentalRatings");
  962. return self.ajax({
  963. type: "GET",
  964. url: url,
  965. dataType: "json"
  966. });
  967. };
  968. /**
  969. * Gets a list of all available conrete BaseItem types from the server
  970. */
  971. self.getItemTypes = function (options) {
  972. var url = self.getUrl("Library/ItemTypes", options);
  973. return self.ajax({
  974. type: "GET",
  975. url: url,
  976. dataType: "json"
  977. });
  978. };
  979. /**
  980. * Constructs a url for a user image
  981. * @param {String} userId
  982. * @param {Object} options
  983. * Options supports the following properties:
  984. * width - download the image at a fixed width
  985. * height - download the image at a fixed height
  986. * maxWidth - download the image at a maxWidth
  987. * maxHeight - download the image at a maxHeight
  988. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  989. * For best results do not specify both width and height together, as aspect ratio might be altered.
  990. */
  991. self.getUserImageUrl = function (userId, options) {
  992. if (!userId) {
  993. throw new Error("null userId");
  994. }
  995. options = options || {
  996. };
  997. var url = "Users/" + userId + "/Images/" + options.type;
  998. if (options.index != null) {
  999. url += "/" + options.index;
  1000. }
  1001. // Don't put these on the query string
  1002. delete options.type;
  1003. delete options.index;
  1004. return self.getUrl(url, options);
  1005. };
  1006. /**
  1007. * Constructs a url for a person image
  1008. * @param {String} name
  1009. * @param {Object} options
  1010. * Options supports the following properties:
  1011. * width - download the image at a fixed width
  1012. * height - download the image at a fixed height
  1013. * maxWidth - download the image at a maxWidth
  1014. * maxHeight - download the image at a maxHeight
  1015. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1016. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1017. */
  1018. self.getPersonImageUrl = function (name, options) {
  1019. if (!name) {
  1020. throw new Error("null name");
  1021. }
  1022. options = options || {
  1023. };
  1024. var url = "Persons/" + self.encodeName(name) + "/Images/" + options.type;
  1025. if (options.index != null) {
  1026. url += "/" + options.index;
  1027. }
  1028. // Don't put these on the query string
  1029. delete options.type;
  1030. delete options.index;
  1031. return self.getUrl(url, options);
  1032. };
  1033. /**
  1034. * Constructs a url for a year image
  1035. * @param {String} year
  1036. * @param {Object} options
  1037. * Options supports the following properties:
  1038. * width - download the image at a fixed width
  1039. * height - download the image at a fixed height
  1040. * maxWidth - download the image at a maxWidth
  1041. * maxHeight - download the image at a maxHeight
  1042. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1043. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1044. */
  1045. self.getYearImageUrl = function (year, options) {
  1046. if (!year) {
  1047. throw new Error("null year");
  1048. }
  1049. options = options || {
  1050. };
  1051. var url = "Years/" + year + "/Images/" + options.type;
  1052. if (options.index != null) {
  1053. url += "/" + options.index;
  1054. }
  1055. // Don't put these on the query string
  1056. delete options.type;
  1057. delete options.index;
  1058. return self.getUrl(url, options);
  1059. };
  1060. /**
  1061. * Constructs a url for a genre image
  1062. * @param {String} name
  1063. * @param {Object} options
  1064. * Options supports the following properties:
  1065. * width - download the image at a fixed width
  1066. * height - download the image at a fixed height
  1067. * maxWidth - download the image at a maxWidth
  1068. * maxHeight - download the image at a maxHeight
  1069. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1070. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1071. */
  1072. self.getGenreImageUrl = function (name, options) {
  1073. if (!name) {
  1074. throw new Error("null name");
  1075. }
  1076. options = options || {
  1077. };
  1078. var url = "Genres/" + self.encodeName(name) + "/Images/" + options.type;
  1079. if (options.index != null) {
  1080. url += "/" + options.index;
  1081. }
  1082. // Don't put these on the query string
  1083. delete options.type;
  1084. delete options.index;
  1085. return self.getUrl(url, options);
  1086. };
  1087. /**
  1088. * Constructs a url for a artist image
  1089. * @param {String} name
  1090. * @param {Object} options
  1091. * Options supports the following properties:
  1092. * width - download the image at a fixed width
  1093. * height - download the image at a fixed height
  1094. * maxWidth - download the image at a maxWidth
  1095. * maxHeight - download the image at a maxHeight
  1096. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1097. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1098. */
  1099. self.getArtistImageUrl = function (name, options) {
  1100. if (!name) {
  1101. throw new Error("null name");
  1102. }
  1103. options = options || {
  1104. };
  1105. var url = "Artists/" + self.encodeName(name) + "/Images/" + options.type;
  1106. if (options.index != null) {
  1107. url += "/" + options.index;
  1108. }
  1109. // Don't put these on the query string
  1110. delete options.type;
  1111. delete options.index;
  1112. return self.getUrl(url, options);
  1113. };
  1114. /**
  1115. * Constructs a url for a studio image
  1116. * @param {String} name
  1117. * @param {Object} options
  1118. * Options supports the following properties:
  1119. * width - download the image at a fixed width
  1120. * height - download the image at a fixed height
  1121. * maxWidth - download the image at a maxWidth
  1122. * maxHeight - download the image at a maxHeight
  1123. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1124. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1125. */
  1126. self.getStudioImageUrl = function (name, options) {
  1127. if (!name) {
  1128. throw new Error("null name");
  1129. }
  1130. options = options || {
  1131. };
  1132. var url = "Studios/" + self.encodeName(name) + "/Images/" + options.type;
  1133. if (options.index != null) {
  1134. url += "/" + options.index;
  1135. }
  1136. // Don't put these on the query string
  1137. delete options.type;
  1138. delete options.index;
  1139. return self.getUrl(url, options);
  1140. };
  1141. /**
  1142. * Constructs a url for an item image
  1143. * @param {String} itemId
  1144. * @param {Object} options
  1145. * Options supports the following properties:
  1146. * type - Primary, logo, backdrop, etc. See the server-side enum ImageType
  1147. * index - When downloading a backdrop, use this to specify which one (omitting is equivalent to zero)
  1148. * width - download the image at a fixed width
  1149. * height - download the image at a fixed height
  1150. * maxWidth - download the image at a maxWidth
  1151. * maxHeight - download the image at a maxHeight
  1152. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1153. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1154. */
  1155. self.getImageUrl = function (itemId, options) {
  1156. if (!itemId) {
  1157. throw new Error("itemId cannot be empty");
  1158. }
  1159. options = options || {
  1160. };
  1161. var url = "Items/" + itemId + "/Images/" + options.type;
  1162. if (options.index != null) {
  1163. url += "/" + options.index;
  1164. }
  1165. // Don't put these on the query string
  1166. delete options.type;
  1167. delete options.index;
  1168. return self.getUrl(url, options);
  1169. };
  1170. /**
  1171. * Constructs a url for an item logo image
  1172. * If the item doesn't have a logo, it will inherit a logo from a parent
  1173. * @param {Object} item A BaseItem
  1174. * @param {Object} options
  1175. * Options supports the following properties:
  1176. * width - download the image at a fixed width
  1177. * height - download the image at a fixed height
  1178. * maxWidth - download the image at a maxWidth
  1179. * maxHeight - download the image at a maxHeight
  1180. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1181. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1182. */
  1183. self.getLogoImageUrl = function (item, options) {
  1184. if (!item) {
  1185. throw new Error("null item");
  1186. }
  1187. options = options || {
  1188. };
  1189. options.imageType = "logo";
  1190. var logoItemId = item.HasLogo ? item.Id : item.ParentLogoItemId;
  1191. return logoItemId ? self.getImageUrl(logoItemId, options) : null;
  1192. };
  1193. /**
  1194. * Constructs an array of backdrop image url's for an item
  1195. * If the item doesn't have any backdrops, it will inherit them from a parent
  1196. * @param {Object} item A BaseItem
  1197. * @param {Object} options
  1198. * Options supports the following properties:
  1199. * width - download the image at a fixed width
  1200. * height - download the image at a fixed height
  1201. * maxWidth - download the image at a maxWidth
  1202. * maxHeight - download the image at a maxHeight
  1203. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1204. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1205. */
  1206. self.getBackdropImageUrl = function (item, options) {
  1207. if (!item) {
  1208. throw new Error("null item");
  1209. }
  1210. options = options || {
  1211. };
  1212. options.imageType = "backdrop";
  1213. var backdropItemId;
  1214. var backdropCount;
  1215. if (!item.BackdropCount) {
  1216. backdropItemId = item.ParentBackdropItemId;
  1217. backdropCount = item.ParentBackdropCount || 0;
  1218. } else {
  1219. backdropItemId = item.Id;
  1220. backdropCount = item.BackdropCount;
  1221. }
  1222. if (!backdropItemId) {
  1223. return [];
  1224. }
  1225. var files = [];
  1226. for (var i = 0; i < backdropCount; i++) {
  1227. options.imageIndex = i;
  1228. files[i] = self.getImageUrl(backdropItemId, options);
  1229. }
  1230. return files;
  1231. };
  1232. /**
  1233. * Authenticates a user
  1234. * @param {String} userId
  1235. * @param {String} password
  1236. */
  1237. self.authenticateUser = function (userId, password) {
  1238. if (!userId) {
  1239. throw new Error("null userId");
  1240. }
  1241. var url = self.getUrl("Users/" + userId + "/authenticate");
  1242. var postData = {
  1243. password: MediaBrowser.SHA1(password || "")
  1244. };
  1245. return self.ajax({
  1246. type: "POST",
  1247. url: url,
  1248. data: JSON.stringify(postData),
  1249. contentType: "application/json"
  1250. });
  1251. };
  1252. /**
  1253. * Updates a user's password
  1254. * @param {String} userId
  1255. * @param {String} currentPassword
  1256. * @param {String} newPassword
  1257. */
  1258. self.updateUserPassword = function (userId, currentPassword, newPassword) {
  1259. if (!userId) {
  1260. throw new Error("null userId");
  1261. }
  1262. var url = self.getUrl("Users/" + userId + "/Password");
  1263. var postData = {
  1264. };
  1265. postData.currentPassword = MediaBrowser.SHA1(currentPassword);
  1266. if (newPassword) {
  1267. postData.newPassword = newPassword;
  1268. }
  1269. return self.ajax({
  1270. type: "POST",
  1271. url: url,
  1272. data: postData
  1273. });
  1274. };
  1275. /**
  1276. * Resets a user's password
  1277. * @param {String} userId
  1278. */
  1279. self.resetUserPassword = function (userId) {
  1280. if (!userId) {
  1281. throw new Error("null userId");
  1282. }
  1283. var url = self.getUrl("Users/" + userId + "/Password");
  1284. var postData = {
  1285. };
  1286. postData.resetPassword = true;
  1287. return self.ajax({
  1288. type: "POST",
  1289. url: url,
  1290. data: postData
  1291. });
  1292. };
  1293. /**
  1294. * Updates the server's configuration
  1295. * @param {Object} configuration
  1296. */
  1297. self.updateServerConfiguration = function (configuration) {
  1298. if (!configuration) {
  1299. throw new Error("null configuration");
  1300. }
  1301. var url = self.getUrl("System/Configuration");
  1302. return self.ajax({
  1303. type: "POST",
  1304. url: url,
  1305. data: JSON.stringify(configuration),
  1306. contentType: "application/json"
  1307. });
  1308. };
  1309. self.updateItem = function (item) {
  1310. if (!item) {
  1311. throw new Error("null item");
  1312. }
  1313. var url = self.getUrl("Items/" + item.Id);
  1314. return self.ajax({
  1315. type: "POST",
  1316. url: url,
  1317. data: JSON.stringify(item),
  1318. contentType: "application/json"
  1319. });
  1320. };
  1321. /**
  1322. * Updates plugin security info
  1323. */
  1324. self.updatePluginSecurityInfo = function (info) {
  1325. var url = self.getUrl("Plugins/SecurityInfo");
  1326. return self.ajax({
  1327. type: "POST",
  1328. url: url,
  1329. data: JSON.stringify(info),
  1330. contentType: "application/json"
  1331. });
  1332. };
  1333. /**
  1334. * Creates a user
  1335. * @param {Object} user
  1336. */
  1337. self.createUser = function (user) {
  1338. if (!user) {
  1339. throw new Error("null user");
  1340. }
  1341. var url = self.getUrl("Users");
  1342. return self.ajax({
  1343. type: "POST",
  1344. url: url,
  1345. data: JSON.stringify(user),
  1346. dataType: "json",
  1347. contentType: "application/json"
  1348. });
  1349. };
  1350. /**
  1351. * Updates a user
  1352. * @param {Object} user
  1353. */
  1354. self.updateUser = function (user) {
  1355. if (!user) {
  1356. throw new Error("null user");
  1357. }
  1358. var url = self.getUrl("Users/" + user.Id);
  1359. return self.ajax({
  1360. type: "POST",
  1361. url: url,
  1362. data: JSON.stringify(user),
  1363. contentType: "application/json"
  1364. });
  1365. };
  1366. /**
  1367. * Updates the Triggers for a ScheduledTask
  1368. * @param {String} id
  1369. * @param {Object} triggers
  1370. */
  1371. self.updateScheduledTaskTriggers = function (id, triggers) {
  1372. if (!id) {
  1373. throw new Error("null id");
  1374. }
  1375. if (!triggers) {
  1376. throw new Error("null triggers");
  1377. }
  1378. var url = self.getUrl("ScheduledTasks/" + id + "/Triggers");
  1379. return self.ajax({
  1380. type: "POST",
  1381. url: url,
  1382. data: JSON.stringify(triggers),
  1383. contentType: "application/json"
  1384. });
  1385. };
  1386. /**
  1387. * Updates a plugin's configuration
  1388. * @param {String} Id
  1389. * @param {Object} configuration
  1390. */
  1391. self.updatePluginConfiguration = function (id, configuration) {
  1392. if (!id) {
  1393. throw new Error("null Id");
  1394. }
  1395. if (!configuration) {
  1396. throw new Error("null configuration");
  1397. }
  1398. var url = self.getUrl("Plugins/" + id + "/Configuration");
  1399. return self.ajax({
  1400. type: "POST",
  1401. url: url,
  1402. data: JSON.stringify(configuration),
  1403. contentType: "application/json"
  1404. });
  1405. };
  1406. /**
  1407. * Gets items based on a query, typically for children of a folder
  1408. * @param {String} userId
  1409. * @param {Object} options
  1410. * Options accepts the following properties:
  1411. * itemId - Localize the search to a specific folder (root if omitted)
  1412. * startIndex - Use for paging
  1413. * limit - Use to limit results to a certain number of items
  1414. * filter - Specify one or more ItemFilters, comma delimeted (see server-side enum)
  1415. * sortBy - Specify an ItemSortBy (comma-delimeted list see server-side enum)
  1416. * sortOrder - ascending/descending
  1417. * fields - additional fields to include aside from basic info. This is a comma delimited list. See server-side enum ItemFields.
  1418. * index - the name of the dynamic, localized index function
  1419. * dynamicSortBy - the name of the dynamic localized sort function
  1420. * recursive - Whether or not the query should be recursive
  1421. * searchTerm - search term to use as a filter
  1422. */
  1423. self.getItems = function (userId, options) {
  1424. if (!userId) {
  1425. throw new Error("null userId");
  1426. }
  1427. var url = self.getUrl("Users/" + userId + "/Items", options);
  1428. return self.ajax({
  1429. type: "GET",
  1430. url: url,
  1431. dataType: "json"
  1432. });
  1433. };
  1434. /**
  1435. Gets artists from an item
  1436. */
  1437. self.getArtists = function (userId, options) {
  1438. if (!userId) {
  1439. throw new Error("null userId");
  1440. }
  1441. options = options || {};
  1442. options.userId = userId;
  1443. var url = self.getUrl("Artists", options);
  1444. return self.ajax({
  1445. type: "GET",
  1446. url: url,
  1447. dataType: "json"
  1448. });
  1449. };
  1450. /**
  1451. Gets genres from an item
  1452. */
  1453. self.getGenres = function (userId, options) {
  1454. if (!userId) {
  1455. throw new Error("null userId");
  1456. }
  1457. options = options || {};
  1458. options.userId = userId;
  1459. var url = self.getUrl("Genres", options);
  1460. return self.ajax({
  1461. type: "GET",
  1462. url: url,
  1463. dataType: "json"
  1464. });
  1465. };
  1466. /**
  1467. Gets people from an item
  1468. */
  1469. self.getPeople = function (userId, options) {
  1470. if (!userId) {
  1471. throw new Error("null userId");
  1472. }
  1473. options = options || {};
  1474. options.userId = userId;
  1475. var url = self.getUrl("Persons", options);
  1476. return self.ajax({
  1477. type: "GET",
  1478. url: url,
  1479. dataType: "json"
  1480. });
  1481. };
  1482. /**
  1483. Gets studios from an item
  1484. */
  1485. self.getStudios = function (userId, options) {
  1486. if (!userId) {
  1487. throw new Error("null userId");
  1488. }
  1489. options = options || {};
  1490. options.userId = userId;
  1491. var url = self.getUrl("Studios", options);
  1492. return self.ajax({
  1493. type: "GET",
  1494. url: url,
  1495. dataType: "json"
  1496. });
  1497. };
  1498. /**
  1499. * Gets local trailers for an item
  1500. */
  1501. self.getLocalTrailers = function (userId, itemId) {
  1502. if (!userId) {
  1503. throw new Error("null userId");
  1504. }
  1505. if (!itemId) {
  1506. throw new Error("null itemId");
  1507. }
  1508. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/LocalTrailers");
  1509. return self.ajax({
  1510. type: "GET",
  1511. url: url,
  1512. dataType: "json"
  1513. });
  1514. };
  1515. /**
  1516. * Gets theme songs for an item
  1517. */
  1518. self.getThemeSongs = function (userId, itemId) {
  1519. if (!itemId) {
  1520. throw new Error("null itemId");
  1521. }
  1522. var options = {};
  1523. if (userId) {
  1524. options.userId = userId;
  1525. }
  1526. var url = self.getUrl("Items/" + itemId + "/ThemeSongs", options);
  1527. return self.ajax({
  1528. type: "GET",
  1529. url: url,
  1530. dataType: "json"
  1531. });
  1532. };
  1533. self.getThemeVideos = function (userId, itemId) {
  1534. if (!itemId) {
  1535. throw new Error("null itemId");
  1536. }
  1537. var options = {};
  1538. if (userId) {
  1539. options.userId = userId;
  1540. }
  1541. var url = self.getUrl("Items/" + itemId + "/ThemeVideos", options);
  1542. return self.ajax({
  1543. type: "GET",
  1544. url: url,
  1545. dataType: "json"
  1546. });
  1547. };
  1548. self.getSearchHints = function (options) {
  1549. var url = self.getUrl("Search/Hints", options);
  1550. return self.ajax({
  1551. type: "GET",
  1552. url: url,
  1553. dataType: "json"
  1554. });
  1555. };
  1556. /**
  1557. * Gets special features for an item
  1558. */
  1559. self.getSpecialFeatures = function (userId, itemId) {
  1560. if (!userId) {
  1561. throw new Error("null userId");
  1562. }
  1563. if (!itemId) {
  1564. throw new Error("null itemId");
  1565. }
  1566. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/SpecialFeatures");
  1567. return self.ajax({
  1568. type: "GET",
  1569. url: url,
  1570. dataType: "json"
  1571. });
  1572. };
  1573. /**
  1574. * Marks an item as played or unplayed
  1575. * This should not be used to update playstate following playback.
  1576. * There are separate playstate check-in methods for that. This should be used for a
  1577. * separate option to reset playstate.
  1578. * @param {String} userId
  1579. * @param {String} itemId
  1580. * @param {Boolean} wasPlayed
  1581. */
  1582. self.updatePlayedStatus = function (userId, itemId, wasPlayed) {
  1583. if (!userId) {
  1584. throw new Error("null userId");
  1585. }
  1586. if (!itemId) {
  1587. throw new Error("null itemId");
  1588. }
  1589. var url = self.getUrl("Users/" + userId + "/PlayedItems/" + itemId);
  1590. var method = wasPlayed ? "POST" : "DELETE";
  1591. return self.ajax({
  1592. type: method,
  1593. url: url
  1594. });
  1595. };
  1596. /**
  1597. * Updates a user's favorite status for an item.
  1598. * @param {String} userId
  1599. * @param {String} itemId
  1600. * @param {Boolean} isFavorite
  1601. */
  1602. self.updateFavoriteStatus = function (userId, itemId, isFavorite) {
  1603. if (!userId) {
  1604. throw new Error("null userId");
  1605. }
  1606. if (!itemId) {
  1607. throw new Error("null itemId");
  1608. }
  1609. var url = self.getUrl("Users/" + userId + "/FavoriteItems/" + itemId);
  1610. var method = isFavorite ? "POST" : "DELETE";
  1611. return self.ajax({
  1612. type: method,
  1613. url: url
  1614. });
  1615. };
  1616. /**
  1617. * Updates a user's personal rating for an item
  1618. * @param {String} userId
  1619. * @param {String} itemId
  1620. * @param {Boolean} likes
  1621. */
  1622. self.updateUserItemRating = function (userId, itemId, likes) {
  1623. if (!userId) {
  1624. throw new Error("null userId");
  1625. }
  1626. if (!itemId) {
  1627. throw new Error("null itemId");
  1628. }
  1629. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating", {
  1630. likes: likes
  1631. });
  1632. return self.ajax({
  1633. type: "POST",
  1634. url: url
  1635. });
  1636. };
  1637. /**
  1638. * Updates a user's favorite status for an item by name.
  1639. * @param {String} userId
  1640. * @param {String} name
  1641. * @param {Boolean} isFavorite
  1642. */
  1643. self.updateFavoriteArtistStatus = function (userId, name, isFavorite) {
  1644. if (!userId) {
  1645. throw new Error("null userId");
  1646. }
  1647. if (!name) {
  1648. throw new Error("null name");
  1649. }
  1650. var url = self.getUrl("Users/" + userId + "/Favorites/Artists/" + self.encodeName(name));
  1651. var method = isFavorite ? "POST" : "DELETE";
  1652. return self.ajax({
  1653. type: method,
  1654. url: url
  1655. });
  1656. };
  1657. self.updateFavoritePersonStatus = function (userId, name, isFavorite) {
  1658. if (!userId) {
  1659. throw new Error("null userId");
  1660. }
  1661. if (!name) {
  1662. throw new Error("null name");
  1663. }
  1664. var url = self.getUrl("Users/" + userId + "/Favorites/Persons/" + self.encodeName(name));
  1665. var method = isFavorite ? "POST" : "DELETE";
  1666. return self.ajax({
  1667. type: method,
  1668. url: url
  1669. });
  1670. };
  1671. self.updateFavoriteStudioStatus = function (userId, name, isFavorite) {
  1672. if (!userId) {
  1673. throw new Error("null userId");
  1674. }
  1675. if (!name) {
  1676. throw new Error("null name");
  1677. }
  1678. var url = self.getUrl("Users/" + userId + "/Favorites/Studios/" + self.encodeName(name));
  1679. var method = isFavorite ? "POST" : "DELETE";
  1680. return self.ajax({
  1681. type: method,
  1682. url: url
  1683. });
  1684. };
  1685. self.updateFavoriteGenreStatus = function (userId, name, isFavorite) {
  1686. if (!userId) {
  1687. throw new Error("null userId");
  1688. }
  1689. if (!name) {
  1690. throw new Error("null name");
  1691. }
  1692. var url = self.getUrl("Users/" + userId + "/Favorites/Genres/" + self.encodeName(name));
  1693. var method = isFavorite ? "POST" : "DELETE";
  1694. return self.ajax({
  1695. type: method,
  1696. url: url
  1697. });
  1698. };
  1699. /**
  1700. * Updates a user's rating for an item by name.
  1701. * @param {String} userId
  1702. * @param {String} name
  1703. * @param {Boolean} likes
  1704. */
  1705. self.updateArtistRating = function (userId, name, likes) {
  1706. if (!userId) {
  1707. throw new Error("null userId");
  1708. }
  1709. if (!name) {
  1710. throw new Error("null name");
  1711. }
  1712. var url = self.getUrl("Users/" + userId + "/Ratings/Artists/" + self.encodeName(name), {
  1713. likes: likes
  1714. });
  1715. return self.ajax({
  1716. type: "POST",
  1717. url: url
  1718. });
  1719. };
  1720. self.updatePersonRating = function (userId, name, likes) {
  1721. if (!userId) {
  1722. throw new Error("null userId");
  1723. }
  1724. if (!name) {
  1725. throw new Error("null name");
  1726. }
  1727. var url = self.getUrl("Users/" + userId + "/Ratings/Persons/" + self.encodeName(name), {
  1728. likes: likes
  1729. });
  1730. return self.ajax({
  1731. type: "POST",
  1732. url: url
  1733. });
  1734. };
  1735. self.updateStudioRating = function (userId, name, likes) {
  1736. if (!userId) {
  1737. throw new Error("null userId");
  1738. }
  1739. if (!name) {
  1740. throw new Error("null name");
  1741. }
  1742. var url = self.getUrl("Users/" + userId + "/Ratings/Studios/" + self.encodeName(name), {
  1743. likes: likes
  1744. });
  1745. return self.ajax({
  1746. type: "POST",
  1747. url: url
  1748. });
  1749. };
  1750. self.updateGenreRating = function (userId, name, likes) {
  1751. if (!userId) {
  1752. throw new Error("null userId");
  1753. }
  1754. if (!name) {
  1755. throw new Error("null name");
  1756. }
  1757. var url = self.getUrl("Users/" + userId + "/Ratings/Genres/" + self.encodeName(name), {
  1758. likes: likes
  1759. });
  1760. return self.ajax({
  1761. type: "POST",
  1762. url: url
  1763. });
  1764. };
  1765. /**
  1766. * Clears a user's rating for an item by name.
  1767. * @param {String} userId
  1768. * @param {String} name
  1769. */
  1770. self.clearArtistRating = function (userId, name) {
  1771. if (!userId) {
  1772. throw new Error("null userId");
  1773. }
  1774. if (!name) {
  1775. throw new Error("null name");
  1776. }
  1777. var url = self.getUrl("Users/" + userId + "/Ratings/Artists/" + self.encodeName(name));
  1778. return self.ajax({
  1779. type: "DELETE",
  1780. url: url
  1781. });
  1782. };
  1783. self.clearPersonRating = function (userId, name) {
  1784. if (!userId) {
  1785. throw new Error("null userId");
  1786. }
  1787. if (!name) {
  1788. throw new Error("null name");
  1789. }
  1790. var url = self.getUrl("Users/" + userId + "/Ratings/Persons/" + self.encodeName(name));
  1791. return self.ajax({
  1792. type: "DELETE",
  1793. url: url
  1794. });
  1795. };
  1796. self.clearStudioRating = function (userId, name) {
  1797. if (!userId) {
  1798. throw new Error("null userId");
  1799. }
  1800. if (!name) {
  1801. throw new Error("null name");
  1802. }
  1803. var url = self.getUrl("Users/" + userId + "/Ratings/Studios/" + self.encodeName(name));
  1804. return self.ajax({
  1805. type: "DELETE",
  1806. url: url
  1807. });
  1808. };
  1809. self.clearGenreRating = function (userId, name) {
  1810. if (!userId) {
  1811. throw new Error("null userId");
  1812. }
  1813. if (!name) {
  1814. throw new Error("null name");
  1815. }
  1816. var url = self.getUrl("Users/" + userId + "/Ratings/Genres/" + self.encodeName(name));
  1817. return self.ajax({
  1818. type: "DELETE",
  1819. url: url
  1820. });
  1821. };
  1822. self.getItemCounts = function (userId) {
  1823. var options = {};
  1824. if (userId) {
  1825. options.userId = userId;
  1826. }
  1827. var url = self.getUrl("Items/Counts", options);
  1828. return self.ajax({
  1829. type: "GET",
  1830. url: url,
  1831. dataType: "json"
  1832. });
  1833. };
  1834. /**
  1835. Gets a variety of item counts that a person appears in
  1836. */
  1837. self.getPersonItemCounts = function (userId, name) {
  1838. if (!userId) {
  1839. throw new Error("null userId");
  1840. }
  1841. if (!name) {
  1842. throw new Error("null name");
  1843. }
  1844. var url = self.getUrl("Persons/" + self.encodeName(name) + "/Counts", {
  1845. userId: userId
  1846. });
  1847. return self.ajax({
  1848. type: "GET",
  1849. url: url,
  1850. dataType: "json"
  1851. });
  1852. };
  1853. /**
  1854. Gets a variety of item counts that a genre appears in
  1855. */
  1856. self.getGenreItemCounts = function (userId, name) {
  1857. if (!userId) {
  1858. throw new Error("null userId");
  1859. }
  1860. if (!name) {
  1861. throw new Error("null name");
  1862. }
  1863. var url = self.getUrl("Genres/" + self.encodeName(name) + "/Counts", {
  1864. userId: userId
  1865. });
  1866. return self.ajax({
  1867. type: "GET",
  1868. url: url,
  1869. dataType: "json"
  1870. });
  1871. };
  1872. /**
  1873. Gets a variety of item counts that an artist appears in
  1874. */
  1875. self.getArtistItemCounts = function (userId, name) {
  1876. if (!userId) {
  1877. throw new Error("null userId");
  1878. }
  1879. if (!name) {
  1880. throw new Error("null name");
  1881. }
  1882. var url = self.getUrl("Artists/" + self.encodeName(name) + "/Counts", {
  1883. userId: userId
  1884. });
  1885. return self.ajax({
  1886. type: "GET",
  1887. url: url,
  1888. dataType: "json"
  1889. });
  1890. };
  1891. /**
  1892. Gets a variety of item counts that a studio appears in
  1893. */
  1894. self.getStudioItemCounts = function (userId, name) {
  1895. if (!userId) {
  1896. throw new Error("null userId");
  1897. }
  1898. if (!name) {
  1899. throw new Error("null name");
  1900. }
  1901. var url = self.getUrl("Studios/" + self.encodeName(name) + "/Counts", {
  1902. userId: userId
  1903. });
  1904. return self.ajax({
  1905. type: "GET",
  1906. url: url,
  1907. dataType: "json"
  1908. });
  1909. };
  1910. /**
  1911. * Clears a user's personal rating for an item
  1912. * @param {String} userId
  1913. * @param {String} itemId
  1914. */
  1915. self.clearUserItemRating = function (userId, itemId) {
  1916. if (!userId) {
  1917. throw new Error("null userId");
  1918. }
  1919. if (!itemId) {
  1920. throw new Error("null itemId");
  1921. }
  1922. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating");
  1923. return self.ajax({
  1924. type: "DELETE",
  1925. url: url
  1926. });
  1927. };
  1928. /**
  1929. * Reports the user has started playing something
  1930. * @param {String} userId
  1931. * @param {String} itemId
  1932. */
  1933. self.reportPlaybackStart = function (userId, itemId) {
  1934. if (!userId) {
  1935. throw new Error("null userId");
  1936. }
  1937. if (!itemId) {
  1938. throw new Error("null itemId");
  1939. }
  1940. if (self.isWebSocketOpen()) {
  1941. var deferred = $.Deferred();
  1942. self.sendWebSocketMessage("PlaybackStart", itemId);
  1943. deferred.resolveWith(null, []);
  1944. return deferred.promise();
  1945. }
  1946. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId);
  1947. return self.ajax({
  1948. type: "POST",
  1949. url: url
  1950. });
  1951. };
  1952. /**
  1953. * Reports progress viewing an item
  1954. * @param {String} userId
  1955. * @param {String} itemId
  1956. */
  1957. self.reportPlaybackProgress = function (userId, itemId, positionTicks, isPaused) {
  1958. if (!userId) {
  1959. throw new Error("null userId");
  1960. }
  1961. if (!itemId) {
  1962. throw new Error("null itemId");
  1963. }
  1964. if (self.isWebSocketOpen()) {
  1965. var deferred = $.Deferred();
  1966. self.sendWebSocketMessage("PlaybackProgress", itemId + "|" + (positionTicks == null ? "" : positionTicks) + "|" + (isPaused == null ? "" : isPaused));
  1967. deferred.resolveWith(null, []);
  1968. return deferred.promise();
  1969. }
  1970. var params = {
  1971. };
  1972. if (positionTicks) {
  1973. params.positionTicks = positionTicks;
  1974. }
  1975. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId + "/Progress", params);
  1976. return self.ajax({
  1977. type: "POST",
  1978. url: url
  1979. });
  1980. };
  1981. /**
  1982. * Reports a user has stopped playing an item
  1983. * @param {String} userId
  1984. * @param {String} itemId
  1985. */
  1986. self.reportPlaybackStopped = function (userId, itemId, positionTicks) {
  1987. if (!userId) {
  1988. throw new Error("null userId");
  1989. }
  1990. if (!itemId) {
  1991. throw new Error("null itemId");
  1992. }
  1993. if (self.isWebSocketOpen()) {
  1994. var deferred = $.Deferred();
  1995. self.sendWebSocketMessage("PlaybackStopped", itemId + "|" + (positionTicks == null ? "" : positionTicks));
  1996. deferred.resolveWith(null, []);
  1997. return deferred.promise();
  1998. }
  1999. var params = {
  2000. };
  2001. if (positionTicks) {
  2002. params.positionTicks = positionTicks;
  2003. }
  2004. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId, params);
  2005. return self.ajax({
  2006. type: "DELETE",
  2007. url: url
  2008. });
  2009. };
  2010. }
  2011. }(jQuery, navigator, window.JSON, window.WebSocket, setTimeout);
  2012. /**
  2013. * Provides a friendly way to create an api client instance using information from the browser's current url
  2014. */
  2015. MediaBrowser.ApiClient.create = function (clientName) {
  2016. var loc = window.location;
  2017. return new MediaBrowser.ApiClient(loc.protocol, loc.hostname, loc.port, clientName);
  2018. };
  2019. /**
  2020. *
  2021. * Secure Hash Algorithm (SHA1)
  2022. * http://www.webtoolkit.info/
  2023. *
  2024. **/
  2025. MediaBrowser.SHA1 = function (msg) {
  2026. function rotate_left(n, s) {
  2027. var t4 = (n << s) | (n >>> (32 - s));
  2028. return t4;
  2029. }
  2030. function lsb_hex(val) {
  2031. var str = "";
  2032. var i;
  2033. var vh;
  2034. var vl;
  2035. for (i = 0; i <= 6; i += 2) {
  2036. vh = (val >>> (i * 4 + 4)) & 0x0f;
  2037. vl = (val >>> (i * 4)) & 0x0f;
  2038. str += vh.toString(16) + vl.toString(16);
  2039. }
  2040. return str;
  2041. }
  2042. function cvt_hex(val) {
  2043. var str = "";
  2044. var i;
  2045. var v;
  2046. for (i = 7; i >= 0; i--) {
  2047. v = (val >>> (i * 4)) & 0x0f;
  2048. str += v.toString(16);
  2049. }
  2050. return str;
  2051. }
  2052. function Utf8Encode(string) {
  2053. string = string.replace(/\r\n/g, "\n");
  2054. var utftext = "";
  2055. for (var n = 0; n < string.length; n++) {
  2056. var c = string.charCodeAt(n);
  2057. if (c < 128) {
  2058. utftext += String.fromCharCode(c);
  2059. }
  2060. else if ((c > 127) && (c < 2048)) {
  2061. utftext += String.fromCharCode((c >> 6) | 192);
  2062. utftext += String.fromCharCode((c & 63) | 128);
  2063. }
  2064. else {
  2065. utftext += String.fromCharCode((c >> 12) | 224);
  2066. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  2067. utftext += String.fromCharCode((c & 63) | 128);
  2068. }
  2069. }
  2070. return utftext;
  2071. }
  2072. var blockstart;
  2073. var i, j;
  2074. var W = new Array(80);
  2075. var H0 = 0x67452301;
  2076. var H1 = 0xEFCDAB89;
  2077. var H2 = 0x98BADCFE;
  2078. var H3 = 0x10325476;
  2079. var H4 = 0xC3D2E1F0;
  2080. var A, B, C, D, E;
  2081. var temp;
  2082. msg = Utf8Encode(msg);
  2083. var msg_len = msg.length;
  2084. var word_array = new Array();
  2085. for (i = 0; i < msg_len - 3; i += 4) {
  2086. j = msg.charCodeAt(i) << 24 | msg.charCodeAt(i + 1) << 16 |
  2087. msg.charCodeAt(i + 2) << 8 | msg.charCodeAt(i + 3);
  2088. word_array.push(j);
  2089. }
  2090. switch (msg_len % 4) {
  2091. case 0:
  2092. i = 0x080000000;
  2093. break;
  2094. case 1:
  2095. i = msg.charCodeAt(msg_len - 1) << 24 | 0x0800000;
  2096. break;
  2097. case 2:
  2098. i = msg.charCodeAt(msg_len - 2) << 24 | msg.charCodeAt(msg_len - 1) << 16 | 0x08000;
  2099. break;
  2100. case 3:
  2101. i = msg.charCodeAt(msg_len - 3) << 24 | msg.charCodeAt(msg_len - 2) << 16 | msg.charCodeAt(msg_len - 1) << 8 | 0x80;
  2102. break;
  2103. }
  2104. word_array.push(i);
  2105. while ((word_array.length % 16) != 14) word_array.push(0);
  2106. word_array.push(msg_len >>> 29);
  2107. word_array.push((msg_len << 3) & 0x0ffffffff);
  2108. for (blockstart = 0; blockstart < word_array.length; blockstart += 16) {
  2109. for (i = 0; i < 16; i++) W[i] = word_array[blockstart + i];
  2110. for (i = 16; i <= 79; i++) W[i] = rotate_left(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1);
  2111. A = H0;
  2112. B = H1;
  2113. C = H2;
  2114. D = H3;
  2115. E = H4;
  2116. for (i = 0; i <= 19; i++) {
  2117. temp = (rotate_left(A, 5) + ((B & C) | (~B & D)) + E + W[i] + 0x5A827999) & 0x0ffffffff;
  2118. E = D;
  2119. D = C;
  2120. C = rotate_left(B, 30);
  2121. B = A;
  2122. A = temp;
  2123. }
  2124. for (i = 20; i <= 39; i++) {
  2125. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0x6ED9EBA1) & 0x0ffffffff;
  2126. E = D;
  2127. D = C;
  2128. C = rotate_left(B, 30);
  2129. B = A;
  2130. A = temp;
  2131. }
  2132. for (i = 40; i <= 59; i++) {
  2133. temp = (rotate_left(A, 5) + ((B & C) | (B & D) | (C & D)) + E + W[i] + 0x8F1BBCDC) & 0x0ffffffff;
  2134. E = D;
  2135. D = C;
  2136. C = rotate_left(B, 30);
  2137. B = A;
  2138. A = temp;
  2139. }
  2140. for (i = 60; i <= 79; i++) {
  2141. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0xCA62C1D6) & 0x0ffffffff;
  2142. E = D;
  2143. D = C;
  2144. C = rotate_left(B, 30);
  2145. B = A;
  2146. A = temp;
  2147. }
  2148. H0 = (H0 + A) & 0x0ffffffff;
  2149. H1 = (H1 + B) & 0x0ffffffff;
  2150. H2 = (H2 + C) & 0x0ffffffff;
  2151. H3 = (H3 + D) & 0x0ffffffff;
  2152. H4 = (H4 + E) & 0x0ffffffff;
  2153. }
  2154. var temp = cvt_hex(H0) + cvt_hex(H1) + cvt_hex(H2) + cvt_hex(H3) + cvt_hex(H4);
  2155. return temp.toLowerCase();
  2156. };
  2157. (function (jQuery, window, undefined) {
  2158. "use strict";
  2159. var matched, browser;
  2160. jQuery.uaMatch = function (ua) {
  2161. ua = ua.toLowerCase();
  2162. var match = /(chrome)[ \/]([\w.]+)/.exec(ua) ||
  2163. /(webkit)[ \/]([\w.]+)/.exec(ua) ||
  2164. /(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
  2165. /(msie) ([\w.]+)/.exec(ua) ||
  2166. ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) ||
  2167. [];
  2168. var platform_match = /(ipad)/.exec(ua) ||
  2169. /(iphone)/.exec(ua) ||
  2170. /(android)/.exec(ua) ||
  2171. [];
  2172. return {
  2173. browser: match[1] || "",
  2174. version: match[2] || "0",
  2175. platform: platform_match[0] || ""
  2176. };
  2177. };
  2178. matched = jQuery.uaMatch(window.navigator.userAgent);
  2179. browser = {};
  2180. if (matched.browser) {
  2181. browser[matched.browser] = true;
  2182. browser.version = matched.version;
  2183. }
  2184. if (matched.platform) {
  2185. browser[matched.platform] = true
  2186. }
  2187. // Chrome is Webkit, but Webkit is also Safari.
  2188. if (browser.chrome) {
  2189. browser.webkit = true;
  2190. } else if (browser.webkit) {
  2191. browser.safari = true;
  2192. }
  2193. jQuery.browser = browser;
  2194. })(jQuery, window);