ApiClient.js 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850
  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. /**
  1310. * Updates plugin security info
  1311. */
  1312. self.updatePluginSecurityInfo = function (info) {
  1313. var url = self.getUrl("Plugins/SecurityInfo");
  1314. return self.ajax({
  1315. type: "POST",
  1316. url: url,
  1317. data: JSON.stringify(info),
  1318. contentType: "application/json"
  1319. });
  1320. };
  1321. /**
  1322. * Creates a user
  1323. * @param {Object} user
  1324. */
  1325. self.createUser = function (user) {
  1326. if (!user) {
  1327. throw new Error("null user");
  1328. }
  1329. var url = self.getUrl("Users");
  1330. return self.ajax({
  1331. type: "POST",
  1332. url: url,
  1333. data: JSON.stringify(user),
  1334. dataType: "json",
  1335. contentType: "application/json"
  1336. });
  1337. };
  1338. /**
  1339. * Updates a user
  1340. * @param {Object} user
  1341. */
  1342. self.updateUser = function (user) {
  1343. if (!user) {
  1344. throw new Error("null user");
  1345. }
  1346. var url = self.getUrl("Users/" + user.Id);
  1347. return self.ajax({
  1348. type: "POST",
  1349. url: url,
  1350. data: JSON.stringify(user),
  1351. contentType: "application/json"
  1352. });
  1353. };
  1354. /**
  1355. * Updates the Triggers for a ScheduledTask
  1356. * @param {String} id
  1357. * @param {Object} triggers
  1358. */
  1359. self.updateScheduledTaskTriggers = function (id, triggers) {
  1360. if (!id) {
  1361. throw new Error("null id");
  1362. }
  1363. if (!triggers) {
  1364. throw new Error("null triggers");
  1365. }
  1366. var url = self.getUrl("ScheduledTasks/" + id + "/Triggers");
  1367. return self.ajax({
  1368. type: "POST",
  1369. url: url,
  1370. data: JSON.stringify(triggers),
  1371. contentType: "application/json"
  1372. });
  1373. };
  1374. /**
  1375. * Updates a plugin's configuration
  1376. * @param {String} Id
  1377. * @param {Object} configuration
  1378. */
  1379. self.updatePluginConfiguration = function (id, configuration) {
  1380. if (!id) {
  1381. throw new Error("null Id");
  1382. }
  1383. if (!configuration) {
  1384. throw new Error("null configuration");
  1385. }
  1386. var url = self.getUrl("Plugins/" + id + "/Configuration");
  1387. return self.ajax({
  1388. type: "POST",
  1389. url: url,
  1390. data: JSON.stringify(configuration),
  1391. contentType: "application/json"
  1392. });
  1393. };
  1394. /**
  1395. * Gets items based on a query, typically for children of a folder
  1396. * @param {String} userId
  1397. * @param {Object} options
  1398. * Options accepts the following properties:
  1399. * itemId - Localize the search to a specific folder (root if omitted)
  1400. * startIndex - Use for paging
  1401. * limit - Use to limit results to a certain number of items
  1402. * filter - Specify one or more ItemFilters, comma delimeted (see server-side enum)
  1403. * sortBy - Specify an ItemSortBy (comma-delimeted list see server-side enum)
  1404. * sortOrder - ascending/descending
  1405. * fields - additional fields to include aside from basic info. This is a comma delimited list. See server-side enum ItemFields.
  1406. * index - the name of the dynamic, localized index function
  1407. * dynamicSortBy - the name of the dynamic localized sort function
  1408. * recursive - Whether or not the query should be recursive
  1409. * searchTerm - search term to use as a filter
  1410. */
  1411. self.getItems = function (userId, options) {
  1412. if (!userId) {
  1413. throw new Error("null userId");
  1414. }
  1415. var url = self.getUrl("Users/" + userId + "/Items", options);
  1416. return self.ajax({
  1417. type: "GET",
  1418. url: url,
  1419. dataType: "json"
  1420. });
  1421. };
  1422. /**
  1423. Gets artists from an item
  1424. */
  1425. self.getArtists = function (userId, options) {
  1426. if (!userId) {
  1427. throw new Error("null userId");
  1428. }
  1429. options = options || {};
  1430. options.userId = userId;
  1431. var url = self.getUrl("Artists", options);
  1432. return self.ajax({
  1433. type: "GET",
  1434. url: url,
  1435. dataType: "json"
  1436. });
  1437. };
  1438. /**
  1439. Gets genres from an item
  1440. */
  1441. self.getGenres = function (userId, options) {
  1442. if (!userId) {
  1443. throw new Error("null userId");
  1444. }
  1445. options = options || {};
  1446. options.userId = userId;
  1447. var url = self.getUrl("Genres", options);
  1448. return self.ajax({
  1449. type: "GET",
  1450. url: url,
  1451. dataType: "json"
  1452. });
  1453. };
  1454. /**
  1455. Gets people from an item
  1456. */
  1457. self.getPeople = function (userId, options) {
  1458. if (!userId) {
  1459. throw new Error("null userId");
  1460. }
  1461. options = options || {};
  1462. options.userId = userId;
  1463. var url = self.getUrl("Persons", options);
  1464. return self.ajax({
  1465. type: "GET",
  1466. url: url,
  1467. dataType: "json"
  1468. });
  1469. };
  1470. /**
  1471. Gets studios from an item
  1472. */
  1473. self.getStudios = function (userId, options) {
  1474. if (!userId) {
  1475. throw new Error("null userId");
  1476. }
  1477. options = options || {};
  1478. options.userId = userId;
  1479. var url = self.getUrl("Studios", options);
  1480. return self.ajax({
  1481. type: "GET",
  1482. url: url,
  1483. dataType: "json"
  1484. });
  1485. };
  1486. /**
  1487. * Gets local trailers for an item
  1488. */
  1489. self.getLocalTrailers = function (userId, itemId) {
  1490. if (!userId) {
  1491. throw new Error("null userId");
  1492. }
  1493. if (!itemId) {
  1494. throw new Error("null itemId");
  1495. }
  1496. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/LocalTrailers");
  1497. return self.ajax({
  1498. type: "GET",
  1499. url: url,
  1500. dataType: "json"
  1501. });
  1502. };
  1503. /**
  1504. * Gets theme songs for an item
  1505. */
  1506. self.getThemeSongs = function (userId, itemId) {
  1507. if (!itemId) {
  1508. throw new Error("null itemId");
  1509. }
  1510. var options = {};
  1511. if (userId) {
  1512. options.userId = userId;
  1513. }
  1514. var url = self.getUrl("Items/" + itemId + "/ThemeSongs", options);
  1515. return self.ajax({
  1516. type: "GET",
  1517. url: url,
  1518. dataType: "json"
  1519. });
  1520. };
  1521. self.getThemeVideos = function (userId, itemId) {
  1522. if (!itemId) {
  1523. throw new Error("null itemId");
  1524. }
  1525. var options = {};
  1526. if (userId) {
  1527. options.userId = userId;
  1528. }
  1529. var url = self.getUrl("Items/" + itemId + "/ThemeVideos", options);
  1530. return self.ajax({
  1531. type: "GET",
  1532. url: url,
  1533. dataType: "json"
  1534. });
  1535. };
  1536. self.getSearchHints = function (options) {
  1537. var url = self.getUrl("Search/Hints", options);
  1538. return self.ajax({
  1539. type: "GET",
  1540. url: url,
  1541. dataType: "json"
  1542. });
  1543. };
  1544. /**
  1545. * Gets special features for an item
  1546. */
  1547. self.getSpecialFeatures = function (userId, itemId) {
  1548. if (!userId) {
  1549. throw new Error("null userId");
  1550. }
  1551. if (!itemId) {
  1552. throw new Error("null itemId");
  1553. }
  1554. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/SpecialFeatures");
  1555. return self.ajax({
  1556. type: "GET",
  1557. url: url,
  1558. dataType: "json"
  1559. });
  1560. };
  1561. /**
  1562. * Marks an item as played or unplayed
  1563. * This should not be used to update playstate following playback.
  1564. * There are separate playstate check-in methods for that. This should be used for a
  1565. * separate option to reset playstate.
  1566. * @param {String} userId
  1567. * @param {String} itemId
  1568. * @param {Boolean} wasPlayed
  1569. */
  1570. self.updatePlayedStatus = function (userId, itemId, wasPlayed) {
  1571. if (!userId) {
  1572. throw new Error("null userId");
  1573. }
  1574. if (!itemId) {
  1575. throw new Error("null itemId");
  1576. }
  1577. var url = self.getUrl("Users/" + userId + "/PlayedItems/" + itemId);
  1578. var method = wasPlayed ? "POST" : "DELETE";
  1579. return self.ajax({
  1580. type: method,
  1581. url: url
  1582. });
  1583. };
  1584. /**
  1585. * Updates a user's favorite status for an item.
  1586. * @param {String} userId
  1587. * @param {String} itemId
  1588. * @param {Boolean} isFavorite
  1589. */
  1590. self.updateFavoriteStatus = function (userId, itemId, isFavorite) {
  1591. if (!userId) {
  1592. throw new Error("null userId");
  1593. }
  1594. if (!itemId) {
  1595. throw new Error("null itemId");
  1596. }
  1597. var url = self.getUrl("Users/" + userId + "/FavoriteItems/" + itemId);
  1598. var method = isFavorite ? "POST" : "DELETE";
  1599. return self.ajax({
  1600. type: method,
  1601. url: url
  1602. });
  1603. };
  1604. /**
  1605. * Updates a user's personal rating for an item
  1606. * @param {String} userId
  1607. * @param {String} itemId
  1608. * @param {Boolean} likes
  1609. */
  1610. self.updateUserItemRating = function (userId, itemId, likes) {
  1611. if (!userId) {
  1612. throw new Error("null userId");
  1613. }
  1614. if (!itemId) {
  1615. throw new Error("null itemId");
  1616. }
  1617. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating", {
  1618. likes: likes
  1619. });
  1620. return self.ajax({
  1621. type: "POST",
  1622. url: url
  1623. });
  1624. };
  1625. /**
  1626. * Updates a user's favorite status for an item by name.
  1627. * @param {String} userId
  1628. * @param {String} name
  1629. * @param {Boolean} isFavorite
  1630. */
  1631. self.updateFavoriteArtistStatus = function (userId, name, isFavorite) {
  1632. if (!userId) {
  1633. throw new Error("null userId");
  1634. }
  1635. if (!name) {
  1636. throw new Error("null name");
  1637. }
  1638. var url = self.getUrl("Users/" + userId + "/Favorites/Artists/" + self.encodeName(name));
  1639. var method = isFavorite ? "POST" : "DELETE";
  1640. return self.ajax({
  1641. type: method,
  1642. url: url
  1643. });
  1644. };
  1645. self.updateFavoritePersonStatus = function (userId, name, isFavorite) {
  1646. if (!userId) {
  1647. throw new Error("null userId");
  1648. }
  1649. if (!name) {
  1650. throw new Error("null name");
  1651. }
  1652. var url = self.getUrl("Users/" + userId + "/Favorites/Persons/" + self.encodeName(name));
  1653. var method = isFavorite ? "POST" : "DELETE";
  1654. return self.ajax({
  1655. type: method,
  1656. url: url
  1657. });
  1658. };
  1659. self.updateFavoriteStudioStatus = function (userId, name, isFavorite) {
  1660. if (!userId) {
  1661. throw new Error("null userId");
  1662. }
  1663. if (!name) {
  1664. throw new Error("null name");
  1665. }
  1666. var url = self.getUrl("Users/" + userId + "/Favorites/Studios/" + self.encodeName(name));
  1667. var method = isFavorite ? "POST" : "DELETE";
  1668. return self.ajax({
  1669. type: method,
  1670. url: url
  1671. });
  1672. };
  1673. self.updateFavoriteGenreStatus = function (userId, name, isFavorite) {
  1674. if (!userId) {
  1675. throw new Error("null userId");
  1676. }
  1677. if (!name) {
  1678. throw new Error("null name");
  1679. }
  1680. var url = self.getUrl("Users/" + userId + "/Favorites/Genres/" + self.encodeName(name));
  1681. var method = isFavorite ? "POST" : "DELETE";
  1682. return self.ajax({
  1683. type: method,
  1684. url: url
  1685. });
  1686. };
  1687. /**
  1688. * Updates a user's rating for an item by name.
  1689. * @param {String} userId
  1690. * @param {String} name
  1691. * @param {Boolean} likes
  1692. */
  1693. self.updateArtistRating = function (userId, name, likes) {
  1694. if (!userId) {
  1695. throw new Error("null userId");
  1696. }
  1697. if (!name) {
  1698. throw new Error("null name");
  1699. }
  1700. var url = self.getUrl("Users/" + userId + "/Ratings/Artists/" + self.encodeName(name), {
  1701. likes: likes
  1702. });
  1703. return self.ajax({
  1704. type: "POST",
  1705. url: url
  1706. });
  1707. };
  1708. self.updatePersonRating = function (userId, name, likes) {
  1709. if (!userId) {
  1710. throw new Error("null userId");
  1711. }
  1712. if (!name) {
  1713. throw new Error("null name");
  1714. }
  1715. var url = self.getUrl("Users/" + userId + "/Ratings/Persons/" + self.encodeName(name), {
  1716. likes: likes
  1717. });
  1718. return self.ajax({
  1719. type: "POST",
  1720. url: url
  1721. });
  1722. };
  1723. self.updateStudioRating = function (userId, name, likes) {
  1724. if (!userId) {
  1725. throw new Error("null userId");
  1726. }
  1727. if (!name) {
  1728. throw new Error("null name");
  1729. }
  1730. var url = self.getUrl("Users/" + userId + "/Ratings/Studios/" + self.encodeName(name), {
  1731. likes: likes
  1732. });
  1733. return self.ajax({
  1734. type: "POST",
  1735. url: url
  1736. });
  1737. };
  1738. self.updateGenreRating = function (userId, name, likes) {
  1739. if (!userId) {
  1740. throw new Error("null userId");
  1741. }
  1742. if (!name) {
  1743. throw new Error("null name");
  1744. }
  1745. var url = self.getUrl("Users/" + userId + "/Ratings/Genres/" + self.encodeName(name), {
  1746. likes: likes
  1747. });
  1748. return self.ajax({
  1749. type: "POST",
  1750. url: url
  1751. });
  1752. };
  1753. /**
  1754. * Clears a user's rating for an item by name.
  1755. * @param {String} userId
  1756. * @param {String} name
  1757. */
  1758. self.clearArtistRating = function (userId, name) {
  1759. if (!userId) {
  1760. throw new Error("null userId");
  1761. }
  1762. if (!name) {
  1763. throw new Error("null name");
  1764. }
  1765. var url = self.getUrl("Users/" + userId + "/Ratings/Artists/" + self.encodeName(name));
  1766. return self.ajax({
  1767. type: "DELETE",
  1768. url: url
  1769. });
  1770. };
  1771. self.clearPersonRating = function (userId, name) {
  1772. if (!userId) {
  1773. throw new Error("null userId");
  1774. }
  1775. if (!name) {
  1776. throw new Error("null name");
  1777. }
  1778. var url = self.getUrl("Users/" + userId + "/Ratings/Persons/" + self.encodeName(name));
  1779. return self.ajax({
  1780. type: "DELETE",
  1781. url: url
  1782. });
  1783. };
  1784. self.clearStudioRating = function (userId, name) {
  1785. if (!userId) {
  1786. throw new Error("null userId");
  1787. }
  1788. if (!name) {
  1789. throw new Error("null name");
  1790. }
  1791. var url = self.getUrl("Users/" + userId + "/Ratings/Studios/" + self.encodeName(name));
  1792. return self.ajax({
  1793. type: "DELETE",
  1794. url: url
  1795. });
  1796. };
  1797. self.clearGenreRating = function (userId, name) {
  1798. if (!userId) {
  1799. throw new Error("null userId");
  1800. }
  1801. if (!name) {
  1802. throw new Error("null name");
  1803. }
  1804. var url = self.getUrl("Users/" + userId + "/Ratings/Genres/" + self.encodeName(name));
  1805. return self.ajax({
  1806. type: "DELETE",
  1807. url: url
  1808. });
  1809. };
  1810. self.getItemCounts = function (userId) {
  1811. var options = {};
  1812. if (userId) {
  1813. options.userId = userId;
  1814. }
  1815. var url = self.getUrl("Items/Counts", options);
  1816. return self.ajax({
  1817. type: "GET",
  1818. url: url,
  1819. dataType: "json"
  1820. });
  1821. };
  1822. /**
  1823. Gets a variety of item counts that a person appears in
  1824. */
  1825. self.getPersonItemCounts = function (userId, name) {
  1826. if (!userId) {
  1827. throw new Error("null userId");
  1828. }
  1829. if (!name) {
  1830. throw new Error("null name");
  1831. }
  1832. var url = self.getUrl("Persons/" + self.encodeName(name) + "/Counts", {
  1833. userId: userId
  1834. });
  1835. return self.ajax({
  1836. type: "GET",
  1837. url: url,
  1838. dataType: "json"
  1839. });
  1840. };
  1841. /**
  1842. Gets a variety of item counts that a genre appears in
  1843. */
  1844. self.getGenreItemCounts = function (userId, name) {
  1845. if (!userId) {
  1846. throw new Error("null userId");
  1847. }
  1848. if (!name) {
  1849. throw new Error("null name");
  1850. }
  1851. var url = self.getUrl("Genres/" + self.encodeName(name) + "/Counts", {
  1852. userId: userId
  1853. });
  1854. return self.ajax({
  1855. type: "GET",
  1856. url: url,
  1857. dataType: "json"
  1858. });
  1859. };
  1860. /**
  1861. Gets a variety of item counts that an artist appears in
  1862. */
  1863. self.getArtistItemCounts = function (userId, name) {
  1864. if (!userId) {
  1865. throw new Error("null userId");
  1866. }
  1867. if (!name) {
  1868. throw new Error("null name");
  1869. }
  1870. var url = self.getUrl("Artists/" + self.encodeName(name) + "/Counts", {
  1871. userId: userId
  1872. });
  1873. return self.ajax({
  1874. type: "GET",
  1875. url: url,
  1876. dataType: "json"
  1877. });
  1878. };
  1879. /**
  1880. Gets a variety of item counts that a studio appears in
  1881. */
  1882. self.getStudioItemCounts = function (userId, name) {
  1883. if (!userId) {
  1884. throw new Error("null userId");
  1885. }
  1886. if (!name) {
  1887. throw new Error("null name");
  1888. }
  1889. var url = self.getUrl("Studios/" + self.encodeName(name) + "/Counts", {
  1890. userId: userId
  1891. });
  1892. return self.ajax({
  1893. type: "GET",
  1894. url: url,
  1895. dataType: "json"
  1896. });
  1897. };
  1898. /**
  1899. * Clears a user's personal rating for an item
  1900. * @param {String} userId
  1901. * @param {String} itemId
  1902. */
  1903. self.clearUserItemRating = function (userId, itemId) {
  1904. if (!userId) {
  1905. throw new Error("null userId");
  1906. }
  1907. if (!itemId) {
  1908. throw new Error("null itemId");
  1909. }
  1910. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating");
  1911. return self.ajax({
  1912. type: "DELETE",
  1913. url: url
  1914. });
  1915. };
  1916. /**
  1917. * Reports the user has started playing something
  1918. * @param {String} userId
  1919. * @param {String} itemId
  1920. */
  1921. self.reportPlaybackStart = function (userId, itemId) {
  1922. if (!userId) {
  1923. throw new Error("null userId");
  1924. }
  1925. if (!itemId) {
  1926. throw new Error("null itemId");
  1927. }
  1928. if (self.isWebSocketOpen()) {
  1929. var deferred = $.Deferred();
  1930. self.sendWebSocketMessage("PlaybackStart", itemId);
  1931. deferred.resolveWith(null, []);
  1932. return deferred.promise();
  1933. }
  1934. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId);
  1935. return self.ajax({
  1936. type: "POST",
  1937. url: url
  1938. });
  1939. };
  1940. /**
  1941. * Reports progress viewing an item
  1942. * @param {String} userId
  1943. * @param {String} itemId
  1944. */
  1945. self.reportPlaybackProgress = function (userId, itemId, positionTicks, isPaused) {
  1946. if (!userId) {
  1947. throw new Error("null userId");
  1948. }
  1949. if (!itemId) {
  1950. throw new Error("null itemId");
  1951. }
  1952. if (self.isWebSocketOpen()) {
  1953. var deferred = $.Deferred();
  1954. self.sendWebSocketMessage("PlaybackProgress", itemId + "|" + (positionTicks == null ? "" : positionTicks) + "|" + (isPaused == null ? "" : isPaused));
  1955. deferred.resolveWith(null, []);
  1956. return deferred.promise();
  1957. }
  1958. var params = {
  1959. };
  1960. if (positionTicks) {
  1961. params.positionTicks = positionTicks;
  1962. }
  1963. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId + "/Progress", params);
  1964. return self.ajax({
  1965. type: "POST",
  1966. url: url
  1967. });
  1968. };
  1969. /**
  1970. * Reports a user has stopped playing an item
  1971. * @param {String} userId
  1972. * @param {String} itemId
  1973. */
  1974. self.reportPlaybackStopped = function (userId, itemId, positionTicks) {
  1975. if (!userId) {
  1976. throw new Error("null userId");
  1977. }
  1978. if (!itemId) {
  1979. throw new Error("null itemId");
  1980. }
  1981. if (self.isWebSocketOpen()) {
  1982. var deferred = $.Deferred();
  1983. self.sendWebSocketMessage("PlaybackStopped", itemId + "|" + (positionTicks == null ? "" : positionTicks));
  1984. deferred.resolveWith(null, []);
  1985. return deferred.promise();
  1986. }
  1987. var params = {
  1988. };
  1989. if (positionTicks) {
  1990. params.positionTicks = positionTicks;
  1991. }
  1992. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId, params);
  1993. return self.ajax({
  1994. type: "DELETE",
  1995. url: url
  1996. });
  1997. };
  1998. }
  1999. }(jQuery, navigator, window.JSON, window.WebSocket, setTimeout);
  2000. /**
  2001. * Provides a friendly way to create an api client instance using information from the browser's current url
  2002. */
  2003. MediaBrowser.ApiClient.create = function (clientName) {
  2004. var loc = window.location;
  2005. return new MediaBrowser.ApiClient(loc.protocol, loc.hostname, loc.port, clientName);
  2006. };
  2007. /**
  2008. *
  2009. * Secure Hash Algorithm (SHA1)
  2010. * http://www.webtoolkit.info/
  2011. *
  2012. **/
  2013. MediaBrowser.SHA1 = function (msg) {
  2014. function rotate_left(n, s) {
  2015. var t4 = (n << s) | (n >>> (32 - s));
  2016. return t4;
  2017. }
  2018. function lsb_hex(val) {
  2019. var str = "";
  2020. var i;
  2021. var vh;
  2022. var vl;
  2023. for (i = 0; i <= 6; i += 2) {
  2024. vh = (val >>> (i * 4 + 4)) & 0x0f;
  2025. vl = (val >>> (i * 4)) & 0x0f;
  2026. str += vh.toString(16) + vl.toString(16);
  2027. }
  2028. return str;
  2029. }
  2030. function cvt_hex(val) {
  2031. var str = "";
  2032. var i;
  2033. var v;
  2034. for (i = 7; i >= 0; i--) {
  2035. v = (val >>> (i * 4)) & 0x0f;
  2036. str += v.toString(16);
  2037. }
  2038. return str;
  2039. }
  2040. function Utf8Encode(string) {
  2041. string = string.replace(/\r\n/g, "\n");
  2042. var utftext = "";
  2043. for (var n = 0; n < string.length; n++) {
  2044. var c = string.charCodeAt(n);
  2045. if (c < 128) {
  2046. utftext += String.fromCharCode(c);
  2047. }
  2048. else if ((c > 127) && (c < 2048)) {
  2049. utftext += String.fromCharCode((c >> 6) | 192);
  2050. utftext += String.fromCharCode((c & 63) | 128);
  2051. }
  2052. else {
  2053. utftext += String.fromCharCode((c >> 12) | 224);
  2054. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  2055. utftext += String.fromCharCode((c & 63) | 128);
  2056. }
  2057. }
  2058. return utftext;
  2059. }
  2060. var blockstart;
  2061. var i, j;
  2062. var W = new Array(80);
  2063. var H0 = 0x67452301;
  2064. var H1 = 0xEFCDAB89;
  2065. var H2 = 0x98BADCFE;
  2066. var H3 = 0x10325476;
  2067. var H4 = 0xC3D2E1F0;
  2068. var A, B, C, D, E;
  2069. var temp;
  2070. msg = Utf8Encode(msg);
  2071. var msg_len = msg.length;
  2072. var word_array = new Array();
  2073. for (i = 0; i < msg_len - 3; i += 4) {
  2074. j = msg.charCodeAt(i) << 24 | msg.charCodeAt(i + 1) << 16 |
  2075. msg.charCodeAt(i + 2) << 8 | msg.charCodeAt(i + 3);
  2076. word_array.push(j);
  2077. }
  2078. switch (msg_len % 4) {
  2079. case 0:
  2080. i = 0x080000000;
  2081. break;
  2082. case 1:
  2083. i = msg.charCodeAt(msg_len - 1) << 24 | 0x0800000;
  2084. break;
  2085. case 2:
  2086. i = msg.charCodeAt(msg_len - 2) << 24 | msg.charCodeAt(msg_len - 1) << 16 | 0x08000;
  2087. break;
  2088. case 3:
  2089. i = msg.charCodeAt(msg_len - 3) << 24 | msg.charCodeAt(msg_len - 2) << 16 | msg.charCodeAt(msg_len - 1) << 8 | 0x80;
  2090. break;
  2091. }
  2092. word_array.push(i);
  2093. while ((word_array.length % 16) != 14) word_array.push(0);
  2094. word_array.push(msg_len >>> 29);
  2095. word_array.push((msg_len << 3) & 0x0ffffffff);
  2096. for (blockstart = 0; blockstart < word_array.length; blockstart += 16) {
  2097. for (i = 0; i < 16; i++) W[i] = word_array[blockstart + i];
  2098. for (i = 16; i <= 79; i++) W[i] = rotate_left(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1);
  2099. A = H0;
  2100. B = H1;
  2101. C = H2;
  2102. D = H3;
  2103. E = H4;
  2104. for (i = 0; i <= 19; i++) {
  2105. temp = (rotate_left(A, 5) + ((B & C) | (~B & D)) + E + W[i] + 0x5A827999) & 0x0ffffffff;
  2106. E = D;
  2107. D = C;
  2108. C = rotate_left(B, 30);
  2109. B = A;
  2110. A = temp;
  2111. }
  2112. for (i = 20; i <= 39; i++) {
  2113. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0x6ED9EBA1) & 0x0ffffffff;
  2114. E = D;
  2115. D = C;
  2116. C = rotate_left(B, 30);
  2117. B = A;
  2118. A = temp;
  2119. }
  2120. for (i = 40; i <= 59; i++) {
  2121. temp = (rotate_left(A, 5) + ((B & C) | (B & D) | (C & D)) + E + W[i] + 0x8F1BBCDC) & 0x0ffffffff;
  2122. E = D;
  2123. D = C;
  2124. C = rotate_left(B, 30);
  2125. B = A;
  2126. A = temp;
  2127. }
  2128. for (i = 60; i <= 79; i++) {
  2129. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0xCA62C1D6) & 0x0ffffffff;
  2130. E = D;
  2131. D = C;
  2132. C = rotate_left(B, 30);
  2133. B = A;
  2134. A = temp;
  2135. }
  2136. H0 = (H0 + A) & 0x0ffffffff;
  2137. H1 = (H1 + B) & 0x0ffffffff;
  2138. H2 = (H2 + C) & 0x0ffffffff;
  2139. H3 = (H3 + D) & 0x0ffffffff;
  2140. H4 = (H4 + E) & 0x0ffffffff;
  2141. }
  2142. var temp = cvt_hex(H0) + cvt_hex(H1) + cvt_hex(H2) + cvt_hex(H3) + cvt_hex(H4);
  2143. return temp.toLowerCase();
  2144. };
  2145. (function (jQuery, window, undefined) {
  2146. "use strict";
  2147. var matched, browser;
  2148. jQuery.uaMatch = function (ua) {
  2149. ua = ua.toLowerCase();
  2150. var match = /(chrome)[ \/]([\w.]+)/.exec(ua) ||
  2151. /(webkit)[ \/]([\w.]+)/.exec(ua) ||
  2152. /(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
  2153. /(msie) ([\w.]+)/.exec(ua) ||
  2154. ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) ||
  2155. [];
  2156. var platform_match = /(ipad)/.exec(ua) ||
  2157. /(iphone)/.exec(ua) ||
  2158. /(android)/.exec(ua) ||
  2159. [];
  2160. return {
  2161. browser: match[1] || "",
  2162. version: match[2] || "0",
  2163. platform: platform_match[0] || ""
  2164. };
  2165. };
  2166. matched = jQuery.uaMatch(window.navigator.userAgent);
  2167. browser = {};
  2168. if (matched.browser) {
  2169. browser[matched.browser] = true;
  2170. browser.version = matched.version;
  2171. }
  2172. if (matched.platform) {
  2173. browser[matched.platform] = true
  2174. }
  2175. // Chrome is Webkit, but Webkit is also Safari.
  2176. if (browser.chrome) {
  2177. browser.webkit = true;
  2178. } else if (browser.webkit) {
  2179. browser.safari = true;
  2180. }
  2181. jQuery.browser = browser;
  2182. })(jQuery, window);