PageRenderTime 59ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/backend/backend.php

https://github.com/jinzora/jinzora3
PHP | 2233 lines | 1640 code | 195 blank | 398 comment | 412 complexity | 9792eb6b0426ab154c2a0c03c8d317f0 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. if (!defined(JZ_SECURE_ACCESS)) die ('Security breach detected.');
  3. /**
  4. * - JINZORA | Web-based Media Streamer -
  5. *
  6. * Jinzora is a Web-based media streamer, primarily desgined to stream MP3s
  7. * (but can be used for any media file that can stream from HTTP).
  8. * Jinzora can be integrated into a CMS site, run as a standalone application,
  9. * or integrated into any PHP website. It is released under the GNU GPL.
  10. *
  11. * - Resources -
  12. * - Jinzora Author: Ross Carlson <ross@jasbone.com>
  13. * - Web: http://www.jinzora.org
  14. * - Documentation: http://www.jinzora.org/docs
  15. * - Support: http://www.jinzora.org/forum
  16. * - Downloads: http://www.jinzora.org/downloads
  17. * - License: GNU GPL <http://www.gnu.org/copyleft/gpl.html>
  18. *
  19. * - Contributors -
  20. * Please see http://www.jinzora.org/modules.php?op=modload&name=jz_whois&file=index
  21. *
  22. * - Code Purpose -
  23. * This page binds the backend to the frontend.
  24. *
  25. * @since 05.10.04
  26. * @author Ross Carlson <ross@jinzora.org>
  27. */
  28. // // //
  29. // This should be renamed to backend.lib.php
  30. //
  31. // First we have to include our files
  32. include_once($include_path. 'system.php');
  33. include_once($include_path. 'lib/general.lib.php');
  34. // Define some constants.
  35. define ("JZUNKNOWN","JZUNKNOWN");
  36. define("ALL_MEDIA_GID",0);
  37. define("NOBODY","NOBODY");
  38. define("ALL_MEDIA_GROUP","ALL_MEDIA");
  39. require_once(dirname(__FILE__) . "/backends/${backend}/header.php");
  40. // Now we can build library functions.
  41. //* * * * * * * * * * * * * * * * * *//
  42. /**
  43. * Checks to see if a users streaming limit has been hit - returns true if it has
  44. *
  45. *
  46. * @author Ross Carlson, Ben Dodson
  47. * @version 7/04/2005
  48. * @since 7/04/2005
  49. * @param $track the track they will play (or array of tracks)
  50. * @param $willPlay whether or not they are about to stream the track.
  51. * For this to work properly, this function must be the last check
  52. * before the user can play back the track.
  53. */
  54. function checkStreamLimit($track, $willPlay = true, $user = false){
  55. global $jzUSER;
  56. if ($user === false){
  57. $user = $jzUSER;
  58. }
  59. $limit = $jzUSER->getSetting('cap_limit');
  60. if (isNothing($limit) || $limit <= 0) {
  61. return true;
  62. }
  63. $cap_duration = $jzUSER->getSetting('cap_duration');
  64. $cap_method = $jzUSER->getSetting('cap_method');
  65. // Now let's see how much they've streamed today
  66. $sArr = unserialize($user->loadData("streamed"));
  67. if (!is_array($sArr)) {
  68. $sArr = array();
  69. }
  70. $stArr = array();
  71. $streamed = 0;
  72. for ($i=0; $i < count($sArr); $i++) {
  73. // Was this within the last 24 hours?
  74. $age = time() - $sArr[$i][0];
  75. if ($age < $cap_duration*24*60*60){
  76. if ($cap_method == "size") {
  77. $streamed = $streamed + $sArr[$i][1];
  78. } else {
  79. $streamed++;
  80. }
  81. $stArr[] = $sArr[$i];
  82. }
  83. }
  84. // Now that we've cleaned up the history let's write it back out so it doesn't grow forever
  85. $user->storeData("streamed", serialize($stArr));
  86. // Let's make it look like an array no matter what:
  87. if (!is_array($track)) {
  88. $arr = array();
  89. $arr[] = $track;
  90. } else {
  91. $arr = $track;
  92. }
  93. $narr = array();
  94. $tsize = 0;
  95. foreach ($arr as $t) {
  96. $path = $t->getFilename('host');
  97. $tsize += round((filesize($path)/1048576-.49));
  98. $narr[] = array(time(),$tsize);
  99. }
  100. if ($cap_method == "size") {
  101. if (($tsize + $streamed) > $limit) {
  102. $ok = false;
  103. } else {
  104. $ok = true;
  105. }
  106. } else {
  107. if ((sizeof($narr) + $streamed) > $limit) {
  108. $ok = false;
  109. } else {
  110. $ok = true;
  111. }
  112. }
  113. if (!$ok) {
  114. return false;
  115. } else {
  116. if ($willPlay) {
  117. foreach ($narr as $e) {
  118. $streamArr[] = $e;
  119. }
  120. $user->storeData("streamed", serialize($streamArr));
  121. }
  122. return true;
  123. }
  124. }
  125. /**
  126. * Checks whether or not a track
  127. * can be played right now.
  128. * This is based on media_lock_mode
  129. * and the user's permissions.
  130. *
  131. * @author Ben Dodson
  132. * @version 7/7/05
  133. * @since 7/7/05
  134. **/
  135. function canPlay($el,$user) {
  136. global $media_lock_mode;
  137. // What to return:
  138. $permissions = false;
  139. $locked = false;
  140. $valid = true;
  141. // First, is the user allowed to play it in general?
  142. if (checkPermission($user,"stream",$el->getPath("String")) === false) {
  143. return $permissions;
  144. }
  145. // Fast case:
  146. if (isNothing($media_lock_mode) || $media_lock_mode == "off") {
  147. return $valid;
  148. }
  149. $be = new jzBackend();
  150. if ($media_lock_mode == "track") {
  151. $arr = $be->getPlaying();
  152. foreach ($arr as $key=>$more) {
  153. if ($more['fpath'] == $el->getFileName("host")) {
  154. return $locked;
  155. }
  156. }
  157. return $valid;
  158. }
  159. if ($media_lock_mode == "album") {
  160. $alb = $el->getAncestor("album");
  161. if ($alb === false) {
  162. return $valid;
  163. }
  164. $arr = $be->getPlaying();
  165. foreach ($arr as $key=>$more) {
  166. $t = new jzMediaTrack($more['path']);
  167. $ta = $t->getAncestor("album");
  168. if ($ta !== false) {
  169. if ($ta->getPath("String") == $alb->getPath("String")) {
  170. return $locked;
  171. }
  172. }
  173. }
  174. return $valid;
  175. }
  176. if ($media_lock_mode == "artist") {
  177. $artist = $el->getAncestor("artist");
  178. if ($artist === false) {
  179. return $valid;
  180. }
  181. $arr = $be->getPlaying();
  182. foreach ($arr as $key=>$more) {
  183. $t = new jzMediaTrack($more['path']);
  184. $ta = $t->getAncestor("artist");
  185. if ($ta !== false) {
  186. if ($ta->getPath("String") == $artist->getPath("String")) {
  187. return $locked;
  188. }
  189. }
  190. }
  191. return $valid;
  192. }
  193. if ($media_lock_mode == "genre") {
  194. $gen = $el->getAncestor("genre");
  195. if ($gen === false) {
  196. return $valid;
  197. }
  198. $arr = $be->getPlaying();
  199. foreach ($arr as $key=>$more) {
  200. $t = new jzMediaTrack($more['path']);
  201. $ta = $t->getAncestor("genre");
  202. if ($ta !== false) {
  203. if ($ta->getPath("String") == $gen->getPath("String")) {
  204. return $locked;
  205. }
  206. }
  207. }
  208. return $valid;
  209. }
  210. return $valid;
  211. }
  212. /*
  213. * Builds a path from meta data
  214. * to fit the hierarchy.
  215. *
  216. * @author Ben Dodson
  217. * @since 8/10/05
  218. * @version 8/10/05
  219. **/
  220. function buildPath($meta) {
  221. global $hierarchy;
  222. // strip out weird characters.
  223. if (isset($meta['genre'])) {
  224. $genre = str_replace("/","-",$meta['genre']);
  225. } else {
  226. $genre = '';
  227. }
  228. if (isset($meta['artist'])) {
  229. $artist = str_replace("/","-",$meta['artist']);
  230. } else {
  231. $artist = '';
  232. }
  233. if (isset($meta['album'])) {
  234. $album = str_replace("/","-",$meta['album']);
  235. } else {
  236. $album = '';
  237. }
  238. if (isset($meta['filename'])) {
  239. $filename = str_replace("/","-",$meta['filename']);
  240. } else if (isset($meta['track'])) {
  241. $filename = str_replace("/","-",$meta['track']);
  242. }
  243. if (isNothing($genre)) {
  244. $genre = word("Unknown");
  245. }
  246. if (isNothing($artist)) {
  247. $artist = word("Unknown");
  248. }
  249. if (isNothing($album)) {
  250. $album = word("Unknown");
  251. }
  252. // TODO: 1) in inject, do a case-insensitive comparison.
  253. // 2) guess id3 fields based on filesystem...
  254. $arr = array();
  255. $norm = array($genre,$artist,$album,$filename);
  256. for ($i = 0; $i < sizeof($hierarchy); $i++) {
  257. switch ($hierarchy[$i]) {
  258. case "genre":
  259. $arr[] = $genre;
  260. break;
  261. case "artist":
  262. $arr[] = $artist;
  263. break;
  264. case "album":
  265. $arr[] = $album;
  266. break;
  267. case "track":
  268. $arr[] = $filename;
  269. break;
  270. default:
  271. $arr[] = $norm[$i];
  272. }
  273. }
  274. return $arr;
  275. }
  276. /**
  277. * Returns which media path
  278. * an element is in
  279. *
  280. * @author Ben Dodson
  281. * @since 8/12/05
  282. * @version 8/12/05
  283. **/
  284. function getMediaDir($element) {
  285. global $media_dirs;
  286. $path = $element->getFilePath();
  287. $mediapaths = explode("|",$media_dirs);
  288. for ($i = 0; $i < sizeof($mediapaths); $i++) {
  289. if (stristr($path,$mediapaths[$i])) {
  290. return $mediapaths[$i];
  291. }
  292. }
  293. return false;
  294. }
  295. function handleUserInit() {
  296. global $jzSERVICES,$jzUSER,$jz_language,$node,$skin,$include_path,
  297. $css,$image_dir,$my_frontend,$fe,$jz_path,$web_path,$USER_SETTINGS_OVERRIDE;
  298. writeLogData("messages","Index: Testing the language file for security and including");
  299. $USER_SETTINGS_OVERRIDE = array(); // for use by user agents
  300. checkUserAgent();
  301. handleSetLanguage();
  302. handleSetFrontend();
  303. writeLogData("messages","Index: Testing the theme file for security and including");
  304. handleSetTheme();
  305. handleJukeboxVars();
  306. if (!($node === false)) {
  307. if (!($dir = $jzUSER->getSetting('home_dir')) === false && $jzUSER->getSetting('home_read') === true) {
  308. if (strpos(strtolower($jz_path),strtolower($dir)) === false) {
  309. $jz_path = "";
  310. }
  311. }
  312. $node = new jzMediaNode($jz_path);
  313. if (isset($_GET['depth']))
  314. $node->setNaturalDepth($_GET['depth']);
  315. else
  316. doNaturalDepth($node);
  317. }
  318. // Let's setup our stylesheet and icons
  319. // Should this be moved to display.php:preHeader()?
  320. if (stristr($skin,"/")){
  321. $css = $include_path . "$skin/default.php";
  322. $image_dir = $web_path . "$skin/";
  323. } else {
  324. $css = $include_path . "style/$skin/default.php";
  325. $image_dir = $web_path . "style/$skin/";
  326. }
  327. }
  328. /**
  329. * Changes Jinzora settings for specific devices.
  330. *
  331. * @author Ben Dodson
  332. * @since 1/12/07
  333. * @version 1/12/07
  334. */
  335. function checkUserAgent() {
  336. global $include_path;
  337. // let's do it in a more accessible file
  338. include($include_path.'frontend/useragent.php');
  339. }
  340. /**
  341. * Sets up the frontend variable.
  342. * @author Ben Dodson
  343. * @since 4/29/05
  344. * @version 4/29/05
  345. **/
  346. function handleSetFrontend($build_fe = true) {
  347. global $jzSERVICES,$my_frontend, $jzUSER, $jinzora_skin,$fe,$skin;
  348. // Now use them.
  349. if (isset($_GET['frontend'])) {
  350. $my_frontend = $_GET['frontend'];
  351. } else if (isset($_POST['frontend'])) {
  352. $my_frontend = $_POST['frontend'];
  353. } else if (defined("JZ_FRONTEND_OVERRIDE")) {
  354. $my_frontend = JZ_FRONTEND_OVERRIDE;
  355. } else if (isset($_SESSION['frontend'])) {
  356. $my_frontend = $_SESSION['frontend'];
  357. } else if (isset($_COOKIE['frontend'])) {
  358. $my_frontend = $_COOKIE['frontend'];
  359. } else {
  360. $my_frontend = $jzUSER->getSetting('frontend');
  361. }
  362. if ($my_frontend == "andromeda") {
  363. $my_frontend = "slick";
  364. $skin = "slick";
  365. }
  366. if (!includeable_file($my_frontend, "frontend/frontends")) {
  367. die("Invalid frontend ${my_frontend}.");
  368. }
  369. if ($build_fe) {
  370. global $include_path;
  371. require($include_path. "frontend/frontends/${my_frontend}/header.php");
  372. $fe = new jzFrontend();
  373. }
  374. }
  375. /**
  376. * Sets up the theme variable.
  377. * @author Ben Dodson
  378. * @since 6/1/05
  379. * @version 6/1/05
  380. **/
  381. function handleSetTheme() {
  382. global $my_frontend, $jzUSER, $skin, $default_frontend_style, $cms_mode,$include_path;
  383. if (isset($_POST['set_theme'])){
  384. $_GET['theme'] = $_POST['set_theme'];
  385. }
  386. if (isset($_GET['set_theme'])){
  387. $_GET['theme'] = $_GET['set_theme'];
  388. }
  389. if (isset($_GET['theme'])) {
  390. $skin = $_GET['theme'];
  391. } else if (isset($_POST['theme'])) {
  392. $skin = $_POST['theme'];
  393. } else if (isset($default_frontend_style)){
  394. $skin = $default_frontend_style;
  395. } else if (defined("JZ_STYLE_OVERRIDE")) {
  396. $skin = JZ_STYLE_OVERRIDE;
  397. } else if (isset($_SESSION['theme'])) {
  398. $skin = $_SESSION['theme'];
  399. } else if (isset($_COOKIE['theme'])) {
  400. $skin = $_COOKIE['theme'];
  401. } else {
  402. $skin = $jzUSER->getSetting('theme');
  403. }
  404. // Now are the in CMS mode?
  405. if ($cms_mode == "true"){
  406. $skin = "cms-theme";
  407. }
  408. if (!includeable_file($skin,"style")) {
  409. // is it in the frontend/frontends/*/style directory?
  410. $string = $include_path . "frontend/frontends/${my_frontend}/style";
  411. if (false === ($res = stristr($skin,$string)) || $res != 0) {
  412. die("Invalid style ${skin}.");
  413. } else {
  414. if (false !== stristr($skin, "://")){
  415. die("Invalid style ${skin}.");
  416. }
  417. }
  418. }
  419. }
  420. /**
  421. * Sets up the language variable.
  422. * @author Ben Dodson
  423. * @since 6/1/05
  424. * @version 6/1/05
  425. **/
  426. function handleSetLanguage() {
  427. global $jzUSER, $jz_language, $default_frontend_style, $cms_mode;
  428. if (isset($_GET['language'])) {
  429. $jz_language = $_GET['language'];
  430. } else if (isset($_POST['language'])) {
  431. $jz_language = $_POST['language'];
  432. } else if (defined("JZ_LANGUAGE_OVERRIDE")) {
  433. $jz_language = JZ_LANGUAGE_OVERRIDE;
  434. } else if (isset($_SESSION['language'])) {
  435. $jz_language = $_SESSION['language'];
  436. } else if (isset($_COOKIE['language'])) {
  437. $jz_language = $_COOKIE['language'];
  438. } else {
  439. $jz_language = $jzUSER->getSetting('language');
  440. }
  441. if (!includeable_file($jz_language . "-simple.php","lang") and !includeable_file($jz_language . ".php","lang")) {
  442. die("Invalid language ${jz_language}.");
  443. }
  444. }
  445. /**
  446. * Initializes the jukebox variables
  447. *
  448. * @author Ben Dodson
  449. * @since 4/29/05
  450. * @version 4/29/05
  451. **/
  452. function handleJukeboxVars() {
  453. global $jukebox,$jukebox_default_addtype,$default_jukebox,$jzUSER,
  454. $home_jukebox_subnets,$home_jukebox_id;
  455. if (isset($_REQUEST['jz_player_type'])) {
  456. if ($_REQUEST['jz_player_type'] == 'stream') {
  457. $_SESSION['jb_playwhere'] = 'stream';
  458. } else if ($_REQUEST['jz_player_type'] == 'jukebox') {
  459. $_SESSION['jb_id'] = $_REQUEST['jz_player'];
  460. $_SESSION['jb_playwhere'] = 'jukebox';
  461. }
  462. }
  463. // easier call for api:
  464. if (isset($_REQUEST['jb_id'])) {
  465. if ($_REQUEST['jb_id'] == 'stream') {
  466. $_SESSION['jb_playwhere'] = 'stream';
  467. } else {
  468. $_SESSION['jb_playwhere'] = 'jukebox';
  469. $_SESSION['jb_id'] = $_REQUEST['jb_id'];
  470. }
  471. }
  472. // if (checkPermission($jzUSER,"jukebox_queue")) {
  473. if (!isset($_SESSION['jb-addtype']) || isNothing($_SESSION['jb-addtype'])){ // set all the variables.
  474. if (!isNothing($jukebox_default_addtype)) {
  475. $_SESSION['jb-addtype'] = $jukebox_default_addtype;
  476. } else {
  477. $_SESSION['jb-addtype'] = "current";
  478. }
  479. }
  480. if (!isset($_SESSION['jb_playwhere']) || isNothing($_SESSION['jb_playwhere'])) {
  481. if (isset($_GET['action']) && $_GET['action'] == 'playlist') {
  482. // hack.. stream these.
  483. $_SESSION['jb_playwhere'] = 'stream';
  484. }
  485. else if (preg_match("/^${home_jukebox_subnets}$/", $_SERVER['REMOTE_ADDR'])) {
  486. $_SESSION['jb_playwhere'] = $home_jukebox_id;
  487. }
  488. else if (!isNothing($default_jukebox)) {
  489. $_SESSION['jb_playwhere'] = $default_jukebox;
  490. } else {
  491. $_SESSION['jb_playwhere'] = "stream";
  492. }
  493. }
  494. if ($_SESSION['jb_playwhere'] == "stream" && !checkPermission($jzUSER,'stream')) {
  495. unset($_SESSION['jb_playwhere']);
  496. // We don't have $jbArr yet; handle in the block.
  497. }
  498. // }
  499. }
  500. /**
  501. * Creates an array out of the given settings file.
  502. *
  503. * @author Ben Dodson
  504. * @since 2/2/05
  505. * @version 2/2/05
  506. *
  507. **/
  508. function settingsToArray($filename) {
  509. $lines = file($filename); // each new line is an entry in the array.
  510. $arr = array();
  511. foreach ($lines as $line) {
  512. if (stristr($line,"=") === false) {
  513. continue;
  514. }
  515. $line = stripSlashes($line);
  516. $key = "";
  517. $val = "";
  518. $i = 0;
  519. while ($line[$i] != "=" && $i < strlen($line)) {
  520. if (!isBlankChar($line[$i]) && $line[$i] != "$") {
  521. $key .= $line[$i];
  522. }
  523. $i++;
  524. }
  525. if ($line[$i] == "=") {
  526. $i++;
  527. while (isBlankChar($line[$i])) {
  528. $i++;
  529. }
  530. if ($line[$i] == "\"") {
  531. $i++;
  532. }
  533. while ($i < strlen($line) && $line[$i] != ";") {
  534. $val .= $line[$i];
  535. $i++;
  536. }
  537. if ($val[strlen($val)-1] == "\"") {
  538. $val = substr($val,0,-1);
  539. }
  540. $arr[$key] = $val;
  541. }
  542. }
  543. return $arr;
  544. }
  545. /**
  546. * Creates a settings file out of an array.
  547. *
  548. * @author Ben Dodson
  549. * @since 2/2/05
  550. * @version 2/2/05
  551. *
  552. **/
  553. function arrayToSettings($array,$filename) {
  554. $file = "<?php\n";
  555. foreach ($array as $key => $val) {
  556. $file .= ' $' . str_replace(" ","",str_replace("\t","",$key)) . ' = "' . addSlashes($val) . "\";\n";
  557. }
  558. $file .= "?";
  559. $file .= ">";
  560. if (($handle = fopen($filename, "w")) === false) {
  561. echo "Could not write to $filename.";
  562. die();
  563. }
  564. fwrite($handle,$file);
  565. fclose ($handle);
  566. }
  567. function isBlankChar($char) {
  568. if ($char == ' ' || $char == '\t') {
  569. return true;
  570. } else {
  571. return false;
  572. }
  573. }
  574. /**
  575. * Checks a user permission
  576. * for permissions that require multiple
  577. * settings checks, IE admin, upload, and play.
  578. *
  579. * @author Ben Dodson
  580. * @since 3/1/05
  581. * @version 3/1/05
  582. **/
  583. function checkPermission($user, $setting, $path = false) {
  584. global $embedded_player,$jukebox,$allow_download;
  585. switch ($setting) {
  586. case "admin":
  587. if ($user->getSetting('admin') === true ||
  588. ($user->getSetting('home_admin') === true && stristr($path,$user->getSetting('home_dir')) !== false)) {
  589. return true;
  590. }
  591. else {
  592. return false;
  593. }
  594. break;
  595. case "upload":
  596. if ($user->getSetting('upload') === true ||
  597. ($user->getSetting('home_upload') === true && stristr($path,$user->getSetting('home_dir')) !== false)) {
  598. return true;
  599. } else {
  600. return checkPermission($user,"admin",$path);
  601. }
  602. break;
  603. case "play":
  604. if ($user->getSetting('jukebox_queue') === true ||
  605. $user->getSetting('jukebox_admin') === true)
  606. return true;
  607. // NO BREAK
  608. case "stream":
  609. if ($user->getSetting('stream') === true ||
  610. ($user->getSetting('home_read') === true && stristr($path,$user->getSetting('home_dir')) !== false))
  611. return true;
  612. else return false;
  613. break;
  614. case "jukebox":
  615. case "jukebox_queue":
  616. if ($jukebox == "true" &&
  617. ($user->getSetting('jukebox_queue') === true ||
  618. $user->getSetting('jukebox_admin') === true))
  619. return true;
  620. else return false;
  621. break;
  622. case "embedded_player":
  623. if (defined ('JZ_FORCE_EMBEDDED_PLAYER'))
  624. return true;
  625. if ($user->getSetting('player') != "" || ($embedded_player != "" && $embedded_player != "false"))
  626. return true;
  627. else return false;
  628. break;
  629. case "view":
  630. if ($user->getSetting('view') === true ||
  631. ($user->getSetting('home_read') === true))
  632. return true;
  633. else return false;
  634. break;
  635. case "download":
  636. if ($user->getSetting('download') === true) // no more allow_download var.
  637. return true;
  638. else return false;
  639. break;
  640. default:
  641. return $user->getSetting($setting);
  642. }
  643. }
  644. /**
  645. * Checks the current playback type.
  646. * Returns one of: stream|embedded|jukebox
  647. *
  648. * @author Ben Dodson
  649. * @since 5/26/05
  650. * @version 5/26/05
  651. * @param check_streammode True if you just want to choose between 'stream' and 'embedded'.
  652. **/
  653. function checkPlayback($check_streammode = false) {
  654. global $embedded_player,$jukebox,$jzUSER;
  655. if (!$check_streammode &&
  656. $jukebox == "true" &&
  657. checkPermission($jzUSER,'jukebox') === true &&
  658. $_SESSION['jb_playwhere'] != "stream") {
  659. return "jukebox";
  660. }
  661. if (defined('JZ_FORCE_EMBEDDED_PLAYER')) {
  662. return "embedded";
  663. }
  664. if (checkPermission($jzUSER,"embedded_player") || (!isNothing($embedded_player) && $embedded_player != "false")) {
  665. if (!isset($_REQUEST['target']) || $_REQUEST['target'] != 'raw') {
  666. return "embedded";
  667. }
  668. }
  669. return "stream";
  670. }
  671. /**
  672. * Returns the default of a setting.
  673. *
  674. * @author Ben Dodson
  675. * @version 11/22/04
  676. * @since 11/22/04
  677. */
  678. function user_default($setting) {
  679. global $frontend,$jz_lang_file,$jinzora_skin,$playlist_ext,$default_resample,$jzUSER;
  680. switch ($setting) {
  681. case "frontend":
  682. return $frontend;
  683. break;
  684. case "theme":
  685. return $jinzora_skin;
  686. break;
  687. case "localpath":
  688. return false;
  689. break;
  690. case "ratingweight":
  691. return 1;
  692. break;
  693. case "language":
  694. return $jz_lang_file;
  695. break;
  696. case "playlist_type":
  697. return $playlist_ext;
  698. break;
  699. case "home_dir":
  700. return false;
  701. break;
  702. case "edit_prefs":
  703. return ($jzUSER->lookupUID('NOBODY') != $jzUSER->getID());
  704. break;
  705. case "powersearch":
  706. case "view":
  707. case "download":
  708. case "stream":
  709. return true;
  710. break;
  711. case "resample_rate":
  712. return $default_resample;
  713. break;
  714. default:
  715. return false;
  716. }
  717. }
  718. /**
  719. * Handles all functions that should
  720. * be executed immediately before viewing a page
  721. *
  722. * @author Ben Dodson
  723. * @version 3/18/05
  724. * @since 3/18/05
  725. **/
  726. function handlePageView($node) {
  727. $node->increaseViewCount();
  728. doUserBrowsing($node);
  729. }
  730. /** Encrypts a password
  731. *
  732. * @author Ben Dodson
  733. *
  734. */
  735. function jz_password($pw) {
  736. return md5($pw);
  737. }
  738. /**
  739. * Pulls the keywords from a search string
  740. * and creates an array of them
  741. *
  742. * Returns an associative array:
  743. * $ret['search'] is the search string without keywords
  744. * $ret['keywords'] is an array of keywords
  745. * with values if needed.
  746. *
  747. * @author Ben Dodson
  748. * @version 1/17/05
  749. * @since 1/17/05
  750. *
  751. */
  752. function splitKeywords($string) {
  753. global $jzUSER, $keyword_genre, $keyword_artist, $keyword_album, $keyword_track,
  754. $keyword_play, $keyword_random, $keyword_radio, $keyword_lyrics, $keyword_limit,
  755. $keyword_id;
  756. $limit_default = 50;
  757. $ret = array();
  758. $keywords = array();
  759. if (isset($keyword_id) && (false !== stristr($string, "$keyword_id"))) {
  760. $keywords['id'] = true;
  761. $string = str_replace(" "," ",str_replace("$keyword_id","",$string));
  762. }
  763. if (isset($keyword_genre) && (false !== stristr($string, "$keyword_genre"))) {
  764. $keywords['genres'] = true;
  765. $string = str_replace(" "," ",str_replace("$keyword_genre","",$string));
  766. }
  767. if (isset($keyword_artist) && (false !== stristr($string, "$keyword_artist"))) {
  768. $keywords['artists'] = true;
  769. $string = str_replace(" "," ",str_replace("$keyword_artist","",$string));
  770. }
  771. if (isset($keyword_album) && (false !== stristr($string, "$keyword_album"))) {
  772. $keywords['albums'] = true;
  773. $string = str_replace(" "," ",str_replace("$keyword_album","",$string));
  774. }
  775. if (isset($keyword_track) && (false !== stristr($string, "$keyword_track"))) {
  776. $keywords['tracks'] = true;
  777. $string = str_replace(" "," ",str_replace("$keyword_track","",$string));
  778. }
  779. if (isset($keyword_lyrics) && (false !== stristr($string, "$keyword_lyrics"))) {
  780. $keywords['lyrics'] = true;
  781. $string = str_replace(" "," ",str_replace("$keyword_lyrics","",$string));
  782. }
  783. if (isset($keyword_play) && (false !== stristr($string, "$keyword_play") && checkPermission($jzUSER,'play') === true)) {
  784. $keywords['play'] = true;
  785. $string = str_replace(" "," ",str_replace("$keyword_play","",$string));
  786. }
  787. if (isset($keyword_radio) && (false !== stristr($string, "$keyword_radio") && checkPermission($jzUSER,'play') === true)) {
  788. $keywords['radio'] = true;
  789. $keywords['limit'] = $limit_default;
  790. $string = str_replace(" "," ",str_replace("$keyword_radio","",$string));
  791. }
  792. if (isset($keyword_limit) && (false !== stristr($string, "$keyword_random") && checkPermission($jzUSER,'play') === true)) {
  793. $keywords['random'] = true;
  794. $keywords['play'] = true;
  795. $keywords['limit'] = $limit_default;
  796. $string = str_replace(" "," ",str_replace("$keyword_random","",$string));
  797. }
  798. if (isset($keyword_limit) && (false !== stristr($string, "$keyword_limit"))) {
  799. $explode = explode(" ",$string);
  800. $str_array = array();
  801. for ($i = 0; $i < sizeof($explode)-1; $i++) {
  802. if (false !== stristr($explode[$i],"$keyword_limit")) {
  803. if (is_numeric($explode[$i+1])) {
  804. $keywords['limit'] = $explode[$i+1];
  805. $i++;
  806. }
  807. else {
  808. $keywords['limit'] = $limit_default;
  809. }
  810. } else {
  811. $str_array[] = $explode[$i];
  812. }
  813. }
  814. $string = implode(" ",$str_array);
  815. }
  816. while ($string[0] == " ") {
  817. $string = substr($string,1);
  818. }
  819. while ($string[strlen($string)] == " ") {
  820. $string = substr($string,0,-1);
  821. }
  822. $ret['keywords'] = $keywords;
  823. $ret['search'] = $string;
  824. return $ret;
  825. }
  826. /**
  827. * Determines whether or not the selected keywords
  828. * should have the output muted.
  829. *
  830. * @author Ben Dodson
  831. * @version 1/18/05
  832. * @since 1/18/05
  833. */
  834. function muteOutput($keywords) {
  835. if (isset($keywords['play'])) {
  836. return true;
  837. }
  838. if (isset($keywords['radio'])) {
  839. return true;
  840. }
  841. return false;
  842. }
  843. /**
  844. * Compares jzMediaElements by filepath for sorting.
  845. *
  846. * @author Ben Dodson
  847. * @version 6/9/04
  848. * @since 6/9/04
  849. */
  850. function compareFilename($a, $b) {
  851. global $compare_ignores_the;
  852. $an = $a->getFilePath();
  853. $bn = $b->getFilePath();
  854. return strnatcasecmp($an,$bn);
  855. }
  856. /**
  857. * Compares jzMediaElements for sorting.
  858. *
  859. * @author Ben Dodson
  860. * @version 6/9/04
  861. * @since 6/9/04
  862. */
  863. function compareNodes($a, $b) {
  864. global $compare_ignores_the;
  865. $an = $a->getName();
  866. $bn = $b->getName();
  867. if ($compare_ignores_the != "false" && strtolower(substr($an,0,4)) == "the ") {
  868. $an = substr($an,4);
  869. }
  870. if ($compare_ignores_the != "false" && strtolower(substr($bn,0,4)) == "the ") {
  871. $bn = substr($bn,4);
  872. }
  873. return strnatcasecmp($an,$bn);
  874. }
  875. /**
  876. * Compares elements by year
  877. *
  878. * @author Ben Dodson
  879. * @version 2/20/05
  880. * @since 2/20/05
  881. *
  882. **/
  883. function compareYear($a, $b) {
  884. $ay = $a->getYear();
  885. $by = $b->getYear();
  886. if ($ay == "-") {
  887. $ay = false;
  888. }
  889. if ($by == "-") {
  890. $by = false;
  891. }
  892. if ($ay === false && $by !== false) {
  893. return 1;
  894. }
  895. if ($by === false && $ay !== false) {
  896. return -1;
  897. }
  898. if ($ay == $by) {
  899. return compareNodes($a,$b);
  900. }
  901. return ($ay < $by) ? 1 : -1;
  902. }
  903. /**
  904. * Compares elements by track number
  905. *
  906. * @author Ben Dodson
  907. * @version 5/27/05
  908. * @since 5/27/05
  909. **/
  910. function compareNumber($a,$b) {
  911. if ($a->isLeaf() === false && $b->isLeaf() === false) {
  912. return compareNodes($a,$b);
  913. }
  914. $ad = $a->getAncestor("album");
  915. $bd = $b->getAncestor("album");
  916. $adArtist = $a->getAncestor("artist");
  917. $bdArtist = $b->getAncestor("artist");
  918. if(
  919. ( $ad !== false && $bd !== false && strtoupper($ad->getName()) != strtoupper($bd->getName()))
  920. ||
  921. ( $adArtist !== false && $bdArtist !== false && strtoupper($adArtist->getName()) != strtoupper($bdArtist->getName()))
  922. ) {
  923. if ($ad === false || $bd === false) {
  924. return compareNodes($a,$b);
  925. }
  926. return compareNodes($ad,$bd);
  927. }
  928. if (($ad = $a->getAncestor("disk")) != ($bd = $b->getAncestor("disk"))) {
  929. if ($ad !== false && $bd !== false) { // both from disks.
  930. return compareNodes($ad,$bd);
  931. }
  932. if ($ad === false) { // $a NOT from a disk, but b is.
  933. return 1;
  934. }
  935. return -1;
  936. }
  937. if ($a->isLeaf() === false) {
  938. return 1;
  939. }
  940. if ($b->isLeaf() === false) {
  941. return -1;
  942. }
  943. $am = $a->getMeta();
  944. $bm = $b->getMeta();
  945. if ((isNothing($am['number']) && isNothing($bm['number']))
  946. || ($am['number'] == $bm['number'])) {
  947. return compareNodes($a,$b);
  948. }
  949. if (isNothing($am['number'])) {
  950. return 1;
  951. }
  952. if (isNothing($bm['number'])) {
  953. return -1;
  954. }
  955. return ($am['number'] < $bm['number']) ? -1 : 1;
  956. }
  957. /**
  958. * Sorts a list of elements by a given parameter.
  959. *
  960. * @author Ben Dodson
  961. * @version 2/20/05
  962. * @since 2/20/05
  963. *
  964. **/
  965. function sortElements(&$list, $param = "name") {
  966. switch ($param) {
  967. case "year":
  968. $func = "compareYear";
  969. break;
  970. case "name":
  971. $func = "compareNodes";
  972. break;
  973. case "number":
  974. $func = "compareNumber";
  975. break;
  976. case "filename":
  977. $func = "compareFilename";
  978. break;
  979. }
  980. usort($list,$func);
  981. }
  982. /**
  983. * Displays the status information on screen
  984. * during an update.
  985. *
  986. * @author Ben Dodson
  987. * @version 11/13/04
  988. * @since 11/13/04
  989. */
  990. function showStatus($path = false) {
  991. global $word_importing;
  992. // Let's set our display items
  993. $media_display = str_replace("'","",$path);
  994. // Now let's truncate the media_display
  995. if (strlen($media_display) > 60){
  996. $media_display = substr($media_display,0,60). "...";
  997. }
  998. switch($_SESSION['jz_import_progress']){
  999. case "30":
  1000. $val = ".&nbsp;";
  1001. $_SESSION['jz_import_progress'] = 0;
  1002. break;
  1003. default:
  1004. $i=0;$val="";
  1005. while ($i < $_SESSION['jz_import_progress']){
  1006. $val .= ".&nbsp;";
  1007. $i++;
  1008. }
  1009. break;
  1010. }
  1011. $_SESSION['jz_import_progress']++;
  1012. if ($media_display <> ""){
  1013. ?>
  1014. <script language="javascript">
  1015. d.innerHTML = '<nobr><b><?php echo word("Directory") ?>:</b> <?php echo $media_display; ?><nobr>';
  1016. -->
  1017. </SCRIPT>
  1018. <?php
  1019. }
  1020. $status_str = word("Processing files"). ": ". $_SESSION['jz_import_full_progress'];
  1021. // Now let's figure out what's left
  1022. if ($_SESSION['jz_import_full_ammount'] <> 0){
  1023. $left = round((($_SESSION['jz_import_full_progress'] / $_SESSION['jz_import_full_ammount']) * 100));
  1024. if ($left > 0){$left = $left -1; }
  1025. // Ok, now let's figure out the time
  1026. // First how much time has elapsed
  1027. $elapsed = time() - $_SESSION['jz_import_start_time'];
  1028. if ($elapsed == 0){$elapsed = 1;}
  1029. // Ok, now how many files did we do in that time?
  1030. $perTime = round($_SESSION['jz_import_full_progress'] / $elapsed);
  1031. if ($perTime == 0){$perTime = 1;}
  1032. // Now how much is left
  1033. $ammountLeft = $_SESSION['jz_import_full_ammount'] - $_SESSION['jz_import_full_progress'];
  1034. // Now time left?
  1035. $timeLeft = convertSecMins(round($ammountLeft / $perTime));
  1036. $status_str .= " (". $left. "% - ". $timeLeft. ") &nbsp; ". $val;
  1037. }
  1038. ?>
  1039. <script language="javascript">
  1040. p.innerHTML = '<b><?php echo $status_str; ?></b>';
  1041. -->
  1042. </SCRIPT>
  1043. <?php
  1044. flushdisplay();
  1045. }
  1046. /**
  1047. * Turns a string with potentially weird characters into a valid path.
  1048. *
  1049. * @author Ben Dodson
  1050. * @version 6/9/04
  1051. * @since 6/9/04
  1052. */
  1053. function pathize($str, $char = '_') {
  1054. $str = preg_replace("/[^a-z|A-Z|0-9| |,|'|\"|(|)|.|-|_|+|=]/",$char,$str);
  1055. if ($str == "" || $str == "-") {
  1056. $str = word("Unknown");
  1057. }
  1058. return $str;
  1059. }
  1060. /**
  1061. * Returns a useable URL.
  1062. * Type is one of: image|track
  1063. * 'arr' lets you add extra variables to our URL.
  1064. *
  1065. * @author Ben Dodson
  1066. * @version 6/9/04
  1067. * @since 6/9/04
  1068. * @param string $text the data to parse
  1069. * return returns a string with the URL's fixed
  1070. */
  1071. function fixAMGUrls($text){
  1072. $text = str_replace('href="/cg/amg.dll?','target="_blank" href="http://www.allmusic.com/cg/amg.dll?',$text);
  1073. $text = str_replace("\n", "<p>", $text );
  1074. return $text;
  1075. }
  1076. /**
  1077. * Returns a useable URL.
  1078. * Type is one of: image|track
  1079. * 'arr' lets you add extra variables to our URL.
  1080. *
  1081. * @author Ben Dodson
  1082. * @version 6/9/04
  1083. * @since 6/9/04
  1084. */
  1085. function jzCreateLink($path, $type, $arr = array()) {
  1086. global $media_dirs,$web_dirs,$include_path,$this_site, $root_dir,$jzUSER;
  1087. if ($type == "image" && !($path[0] == '/' || stristr($path,":\\") || stristr($path,":/"))) {
  1088. // the link is relative; return it.
  1089. return $this_site. $root_dir. "/". str_replace("%2F","/",rawurlencode($path));
  1090. }
  1091. $media_paths = explode("|",$media_dirs);
  1092. $web_paths = @explode("|",$web_dirs);
  1093. if (strlen($web_dirs) > 0) {
  1094. for ($i = 0; $i < sizeof($media_paths); $i++) {
  1095. if (($pos = stristr($path,$media_paths[$i])) !== false) {
  1096. if ($pos == 0 && $web_paths[$i] != "") {
  1097. $path = str_replace($media_paths[$i],$web_paths[$i],$path);
  1098. if (substr($path,0,4) == "http") {
  1099. return $path;
  1100. }
  1101. if ($type == "image" && $_SERVER['SERVER_PORT'] == 443) {
  1102. $link = "https://";
  1103. } else {
  1104. $link = "http://";
  1105. }
  1106. $link .= $_SERVER['HTTP_HOST'];
  1107. if ($_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 443 && (strpos($link,":") === false)) {
  1108. $link .= ":" . $_SERVER['SERVER_PORT'];
  1109. }
  1110. $link = str_replace(":443","",$link);
  1111. $ar = explode("/",$path);
  1112. for ($i = 0; $i < sizeof($ar); $i++) {
  1113. $ar[$i] = rawurlencode($ar[$i]);
  1114. }
  1115. $path = implode("/",$ar);
  1116. $link .= '/' . $path;
  1117. return $link;
  1118. }
  1119. }
  1120. }
  1121. }
  1122. switch ($type) {
  1123. case "image":
  1124. $arr['action'] = "image";
  1125. $arr['jz_path'] = $path;
  1126. $arr['jz_user'] = $jzUSER->getId();
  1127. return urlize($arr);
  1128. break;
  1129. case "track":
  1130. if ($web_dirs <> ""){
  1131. return $path;
  1132. } else {
  1133. $ssid = session_id();
  1134. writeLogData("as_debug","jzCreateLink: ssid = ". $ssid);
  1135. $arr['jz_path'] = $path;
  1136. $arr['action'] = "play";
  1137. $arr['type'] = "track";
  1138. $arr['jza'] = $ssid;
  1139. return urlize($arr);
  1140. }
  1141. break;
  1142. }
  1143. }
  1144. /**
  1145. * Returns a blank cache;
  1146. *
  1147. * NODE:
  1148. * [0] path
  1149. * [1] art
  1150. * [2] updated
  1151. * [3] playcount
  1152. * [4] rating
  1153. * [5] ratingcount
  1154. * [6] dateadded
  1155. * [7] nodes array
  1156. * [8] tracks array
  1157. * [9] short_desc
  1158. * [10] desc
  1159. * [11] year
  1160. * [12] dlcount
  1161. * [13] filepath
  1162. * [14] links
  1163. * [15] ptype
  1164. * [16-21] unused
  1165. * [22] hidden
  1166. * [23] ID
  1167. * [24] direct play count
  1168. * [25] view count
  1169. * TRACK:
  1170. * 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
  1171. * [filepath][art][fname][playcount][rating][ratingcount][dateadded][name][frequency][short_desc][desc][year][dlcount][size][length][genre][artist][album][extension][lyrics][bitrate][tracknum][hidden][ID][dp_count][view_count][sheet_music]
  1172. *
  1173. * @author Ben Dodson
  1174. * @version 6/9/04
  1175. * @since 6/9/04
  1176. */
  1177. function blankCache($type = "node") {
  1178. $cache = array();
  1179. if ($type == "node") {
  1180. for ($i = 0; $i < 23; $i++) {
  1181. $cache[] = "-";
  1182. }
  1183. $cache[3] = $cache[4] = $cache[5] = $cache[12] = $cache[24] = $cache[25] = 0;
  1184. $cache[7] = array();
  1185. $cache[8] = array();
  1186. $cache[14] = array();
  1187. $cache[22] = 'false';
  1188. $cache[23] = uniqid("T");
  1189. return $cache;
  1190. }
  1191. else if ($type == "track") {
  1192. for ($i = 0; $i < 23; $i++) {
  1193. $cache[] = "-";
  1194. }
  1195. $cache[3] = $cache[4] = $cache[5] = $cache[12] = $cache[24] = $cache[25] = 0;
  1196. $cache[22] = 'false';
  1197. $cache[23] = uniqid("T");
  1198. $cache[26] = 0;
  1199. return $cache;
  1200. }
  1201. else {
  1202. return $cache;
  1203. }
  1204. }
  1205. /**
  1206. * Gets the desired information (genre,artist,album) from a jzMediaElement.
  1207. * This does not use meta data even for tracks-- only the $hierarchy.
  1208. *
  1209. * @author Ben Dodson
  1210. * @version 6/10/04
  1211. * @since 6/10/04
  1212. */
  1213. function getInformation(&$element,$type = "album") {
  1214. global $hierarchy;
  1215. $path = $element->getPath();
  1216. if (is_string($hierarchy))
  1217. $hierarchy = explode("/",$hierarchy);
  1218. for ($i = 0; $i < sizeof($hierarchy); $i++) {
  1219. if ($hierarchy[$i] == $type) {
  1220. return ($i < sizeof($path)) ? str_replace("_"," ",$path[$i]) : false;
  1221. }
  1222. }
  1223. return false;
  1224. }
  1225. /**
  1226. * Figures the ptype of a node from the hierarchy.
  1227. *
  1228. * @author Ben Dodson
  1229. * @version 11/3/04
  1230. * @since 11/3/04
  1231. */
  1232. function findPType(&$node) {
  1233. global $hierarchy;
  1234. // Obvious cases:
  1235. if ($node->isLeaf()) {
  1236. return "track";
  1237. }
  1238. if ($node->getLevel() == 0) {
  1239. return "root";
  1240. }
  1241. // Otherwise, we need to use the $hierarchy.
  1242. if (is_string($hierarchy))
  1243. $hier = explode("/",$hierarchy);
  1244. else
  1245. $hier = $hierarchy;
  1246. if ($node->getLevel() > sizeof($hier)) {
  1247. return "generic";
  1248. }
  1249. $guess = $hier[$node->getLevel()-1];
  1250. if ($guess == "track")
  1251. return "disk"; // isn't really a track;
  1252. // must be a disk.
  1253. else if ($guess == "loose") {
  1254. return "generic";
  1255. }
  1256. else {
  1257. return $guess;
  1258. }
  1259. }
  1260. /**
  1261. * Sets the 'natural depth' for this node based on the hierarchy.
  1262. *
  1263. * @author Ben Dodson
  1264. * @version 11/3/04
  1265. * @since 11/3/04
  1266. */
  1267. function doNaturalDepth(&$node) {
  1268. global $hierarchy;
  1269. if (is_string($hierarchy))
  1270. $hierarchy = explode("/",$hierarchy);
  1271. $level = $oldlevel = $node->getLevel();
  1272. while ($level < sizeof($hierarchy) && $hierarchy[$level] == "hidden") {
  1273. $level++;
  1274. }
  1275. $node->setNaturalDepth(1 - $oldlevel + $level);
  1276. }
  1277. /**
  1278. * Finds the distance from the node to the level type (genre,artist,album,track)
  1279. * If it is only called with 1 param, it returns the distance from the root to that type.
  1280. *
  1281. * @author Ben Dodson
  1282. * @version 6/10/04
  1283. * @since 6/10/04
  1284. */
  1285. function distanceTo($type = "album", $node = false) {
  1286. global $hierarchy;
  1287. if (is_string($hierarchy))
  1288. $hierarchy = explode("/",$hierarchy);
  1289. if ($node === false) {
  1290. $node = &new jzMediaNode();
  1291. }
  1292. if ($type == "any" || $type == "track")
  1293. return -1;
  1294. $level = $node->getLevel();
  1295. for ($i = 0; $i < sizeof($hierarchy); $i++) {
  1296. if ($hierarchy[$i] == $type) {
  1297. if ($i - $level >= 0) {
  1298. return ($i - $level + 1);
  1299. }
  1300. }
  1301. }
  1302. return false;
  1303. }
  1304. /**
  1305. * Essentially the inverse of distanceTo.
  1306. *
  1307. * @author Ben Dodson
  1308. * @version 5/1/05
  1309. * @since 5/1/05
  1310. */
  1311. function toLevel($distance, $node = false) {
  1312. global $hierarchy;
  1313. if (is_string($hierarchy))
  1314. $hierarchy = explode("/",$hierarchy);
  1315. if ($node === false) {
  1316. $node = &new jzMediaNode();
  1317. }
  1318. if ($distance < 0) {
  1319. return false;
  1320. }
  1321. $level = $node->getLevel();
  1322. $distance = $distance + $level;
  1323. if ($distance == 0) {
  1324. return "root";
  1325. }
  1326. if (0 <= $distance-1 && $distance-1 < sizeof($hierarchy)) {
  1327. return $hierarchy[$distance-1];
  1328. } else {
  1329. return false;
  1330. }
  1331. }
  1332. /**
  1333. * validates a key in our hierarchy.
  1334. *
  1335. *
  1336. * @author Ben Dodson
  1337. * @version 11/11/04
  1338. * @since 11/11/04
  1339. */
  1340. function validateLevel(&$lvl) {
  1341. $lvl = strtolower($lvl);
  1342. switch ($lvl) {
  1343. case "genre":
  1344. case "artist":
  1345. case "album":
  1346. case "track":
  1347. case "hidden":
  1348. case "generic":
  1349. case "root":
  1350. case "disk":
  1351. case "user":
  1352. case "subgenre":
  1353. return true;
  1354. break;
  1355. case "genres":
  1356. case "subgenres":
  1357. case "artists":
  1358. case "albums":
  1359. case "tracks":
  1360. case "disks":
  1361. $lvl = substr($lvl,0,-1);
  1362. return true;
  1363. }
  1364. return false;
  1365. }
  1366. /**
  1367. * Translates a path to a URL-valid one.
  1368. *
  1369. *
  1370. * @author PHP online resource
  1371. * @version
  1372. * @since
  1373. */
  1374. function translate_uri($uri) {
  1375. $parts = explode('/', $uri);
  1376. for ($i = 0; $i < count($parts); $i++) {
  1377. $parts[$i] = rawurlencode($parts[$i]);
  1378. }
  1379. return implode('/', $parts);
  1380. }
  1381. /**
  1382. * Converts seconds to a string
  1383. *
  1384. *
  1385. * @author Ben Dodson
  1386. * @version 11/17/04
  1387. * @since 11/17/04
  1388. */
  1389. function stringize_time($sec) {
  1390. $str = "";
  1391. if ($sec > 60*60*24) {
  1392. // days
  1393. $days = intval($sec / (60*60*24));
  1394. $sec -= $days*(60*60*24);
  1395. $str .= $days . " days ";
  1396. if ($sec > 60*60) { // hours
  1397. $hours = intval($sec / (60*60));
  1398. $sec -= $hours*(60*60);
  1399. $str .= $hours . " hours ";
  1400. }
  1401. if ($sec > 60) {
  1402. $mins = intval($sec / 60);
  1403. $sec -= ($mins*60);
  1404. $str .= $mins . " minutes ";
  1405. }
  1406. $str .= $sec . " seconds";
  1407. } else {
  1408. // $len -> string
  1409. if ($sec > 60*60) {
  1410. $h = intval($sec / (60*60));
  1411. $sec -= $h*60*60;
  1412. $str .= $h . ":";
  1413. }
  1414. if ($sec > 60) {
  1415. $m = intval($sec / 60);
  1416. $sec -= $m*60;
  1417. if ($str != "" && $m < 10) {
  1418. $str .= "0";
  1419. }
  1420. $str .= $m . ":";
  1421. } else {
  1422. $str .= "0:";
  1423. }
  1424. if ($sec < 10) {
  1425. $str .= "0";
  1426. }
  1427. $str .= $sec;
  1428. }
  1429. return $str;
  1430. }
  1431. /**
  1432. * Stringizes size given in megs.
  1433. *
  1434. *
  1435. * @author Ben Dodson
  1436. * @version 11/17/04
  1437. * @since 11/17/04
  1438. */
  1439. function stringize_size($size) {
  1440. if ($size > 734000) { // 70%+ of a TB
  1441. return round($size / 1024*1024,2) . " TB";
  1442. }
  1443. if ($size > 715) { // 70%+ of a GB
  1444. return round($size / 1024,2) . " GB";
  1445. }
  1446. return $size . " MB";
  1447. }
  1448. /** Updates a node's cache nonrecursively.
  1449. *
  1450. * @author Ben Dodson
  1451. */
  1452. function updateNodeCache($node, $recursive = false, $showStatus = false, $force = false, $readTags = true, $root_path = false) {
  1453. global $media_dirs,$live_update,$jzSERVICES,$hierarchy,$backend,$default_importer,$jukebox,$include_path;
  1454. $flags = array();
  1455. $flags['showstatus'] = $showStatus;
  1456. $flags['force'] = $force;
  1457. $flags['readtags'] = $readTags;
  1458. $flags['recursive'] = $recursive;
  1459. $importer = $default_importer;
  1460. // TODO: more dynamic choice of importer.
  1461. if (false !== stristr($importer,"id3tags")) {
  1462. // id3tag importer doesn't care about your hierarchy.
  1463. // TODO: seperate hierarchy for display / import.
  1464. } else {
  1465. // TODO: Remove this stuff once we have a propper way
  1466. // of getting the path from the node. Make
  1467. // the function recursive with respect to the node.
  1468. $mypath = array();
  1469. if (false !== ($val = getInformation($node,"genre"))) {
  1470. $mypath['genre'] = $val;
  1471. }
  1472. if (false !== ($val = getInformation($node,"subgenre"))) {
  1473. $mypath['subgenre'] = $val;
  1474. }
  1475. if (false !== ($val = getInformation($node,"artist"))) {
  1476. $mypath['artist'] = $val;
  1477. }
  1478. if (false !== ($val = getInformation($node,"album"))) {
  1479. $mypath['album'] = $val;
  1480. }
  1481. if (false !== ($val = getInformation($node,"disk"))) {
  1482. $mypath['disk'] = $val;
  1483. }
  1484. $flags['path'] = $mypath;
  1485. $flags['hierarchy'] = array_slice($hierarchy,sizeof($mypath),sizeof($hierarchy)-sizeof($mypath));
  1486. }
  1487. $jzSERVICES->loadService("importing",$importer);
  1488. // TODO: Move flags array into parameters of this function.
  1489. /*if ($flags['recursive']) {
  1490. @ini_set("max_execution_time","0");
  1491. @ini_set("memory_limit","64");
  1492. }*/
  1493. if ($node->getLevel() == 0 && $root_path === false) {
  1494. $mediapaths = explode("|",$media_dirs);
  1495. for ($i = 0; $i < sizeof($mediapaths); $i++) {
  1496. if (is_dir($mediapaths[$i]) && $mediapaths[$i] != "/" && $mediapaths[$i] != "") {
  1497. //$node->updateCache($recursive,$mediapaths[$i], $showStatus,$force,$readTags);
  1498. $jzSERVICES->importMedia($node, $mediapaths[$i], $flags);
  1499. }
  1500. }
  1501. } else {
  1502. //$node->updateCache($recursive,$root_path,$showStatus,$force, $readTags);
  1503. $jzSERVICES->importMedia($node, $root_path, $flags);
  1504. if ($recursive === false && $node->getSubNodeCount('tracks',-1) == 0) {
  1505. //$node->updateCache(true,$root_path,$showStatus,$force,$readTags);
  1506. $flags['recursive'] = true;
  1507. $jzSERVICES->importMedia($node, $root_path, $flags);
  1508. }
  1509. }
  1510. if ($showStatus == true) {
  1511. showStatus();
  1512. }
  1513. if ($jukebox == "true") {
  1514. include_once($include_path. "jukebox/class.php");
  1515. $jb = new jzJukebox();
  1516. $jb->updateDB($node, $recursive, $root_path);
  1517. }
  1518. }
  1519. /*
  1520. * Sets the user's browsing history, this is then used to track the user and show previous items
  1521. *
  1522. * @author Ben Dodson, Ross Carlson
  1523. * @since 2/2/05
  1524. * @version 2/2/05
  1525. *
  1526. **/
  1527. function doUserBrowsing($node) {
  1528. global $jzUSER;
  1529. $jzBackend = new jzBackend();
  1530. $oldHist = $jzUSER->loadData('history');
  1531. $jzUSER->storeData('history',$node->getPType(). "|". $node->getName(). "|". $node->getPath("String"). "|". time(). "\n". substr($oldHist,0,5000));
  1532. $oldHist = $jzBackend->loadData('history');
  1533. // Now let's find the history for this user
  1534. $dArr = explode("\n",$oldHist);
  1535. for ($i=0; $i < count($dArr); $i++){
  1536. $vArr = explode("|",$dArr[$i]);
  1537. if ($vArr[6] == $_SESSION['sid']){
  1538. unset($dArr[$i]);
  1539. }
  1540. }
  1541. $oldHist = implode("\n",$dArr);
  1542. $jzBackend->storeData('history',$node->getPType(). "|". $node->getName(). "|". $node->getPath("String"). "|". time(). "|". $jzUSER->getName(). "|". $jzUSER->getSetting('fullname'). "|". $_SESSION['sid']. "|". $_SERVER['REMOTE_ADDR']. "\n". substr($oldHist,0,50000));
  1543. }
  1544. /**
  1545. * Sees if the setting is a cookie-stored setting.
  1546. *
  1547. * @author Ben Dodson
  1548. * @version 1/21/05
  1549. * @since 1/21/05
  1550. */
  1551. function isCookieSetting($setting) {
  1552. switch ($setting) {
  1553. case "localpath":
  1554. return true;
  1555. default:
  1556. return false;
  1557. }
  1558. }
  1559. /**
  1560. * Handle a search query from the GET/POST variables.
  1561. * @author Ben Dodson
  1562. */
  1563. function handleSearch($search_string = false, $search_type = false) {
  1564. global $jzUSER;
  1565. $root = &new jzMediaNode();
  1566. $timer = microtime_float();
  1567. if ($search_string === false) {
  1568. $search_string = $_GET['search_query'];
  1569. }
  1570. if ($search_type === false) {
  1571. $search_type = $_GET['search_type'];
  1572. }
  1573. $string_array = splitKeywords($search_string);
  1574. $keywords = $string_array['keywords'];
  1575. $string = $string_array['search'];
  1576. if (isset($keywords['genres'])) {
  1577. $locations[sizeof($locations)] = 'genre';
  1578. $search_type = "genres";
  1579. }
  1580. if (isset($keywords['artists'])) {
  1581. $locations[sizeof($locations)] = 'artist';
  1582. $search_type = "artists";
  1583. }
  1584. if (isset($keywords['albums'])) {
  1585. $locations[sizeof($locations)] = 'album';
  1586. $search_type = "albums";
  1587. }
  1588. if (isset($keywords['tracks'])) {
  1589. $locations[sizeof($locations)] = 'track';
  1590. $search_type = "tracks";
  1591. }
  1592. if (isset($keywords['lyrics'])) {
  1593. $search_type = "lyrics";
  1594. }
  1595. if (isset($keywords['radio'])) {
  1596. $search_type = "artists";
  1597. $max_res = 1;
  1598. } else if (isset($keywords['limit'])) {
  1599. $max_res = $keywords['limit'];
  1600. } else {
  1601. $max_res = 100;
  1602. }
  1603. switch (strtolower($search_type)) {
  1604. case "all":
  1605. $stype = "both";
  1606. $distance = -1;
  1607. break;
  1608. case "genres":
  1609. case "genre":
  1610. $stype = "nodes";
  1611. $distance = distanceTo("genre");
  1612. break;
  1613. case "artists":
  1614. case "artist":
  1615. $stype = "nodes";
  1616. $distance = distanceTo("artist");
  1617. break;
  1618. case "albums":
  1619. case "album":
  1620. $stype = "nodes";
  1621. $distance = distanceTo("album");
  1622. break;
  1623. case "tracks":
  1624. case "track":
  1625. $stype = "tracks";
  1626. $distance = -1;
  1627. break;
  1628. case "lyrics":
  1629. case "lyric":
  1630. $stype = "lyrics";
  1631. $distance = -1;
  1632. break;
  1633. case "best":
  1634. default:
  1635. $stype = "both";
  1636. $distance = -1;
  1637. $keywords['best'] = true;
  1638. }
  1639. if ($distance === false) {
  1640. die("Could not search for ${search_type}.");
  1641. }
  1642. // Are they searching by ID explicitly?
  1643. if (isset($keywords['id'])) {
  1644. $stype = "id";
  1645. // We handle this differently than above in
  1646. // case they set @genre and @id (or whatever).
  1647. }
  1648. /* if we have 2 locations,
  1649. the closest to the root is our anchor
  1650. and the further is our return type.
  1651. */
  1652. if (sizeof($locations) > 1) {
  1653. if ($locations[1] == 'track') {
  1654. $r = 'tracks';
  1655. } else {
  1656. $r = 'nodes';
  1657. }
  1658. $limit = 1;
  1659. if (isset($keywords['limit'])) {
  1660. $limit = $keywords['limit'];
  1661. }
  1662. $results = $root->search($string,"nodes",distanceTo($locations[0]),1,'exact');
  1663. if (sizeof($results) > 0) {
  1664. $results = $results[0]->getSubNodes($r,distanceTo($locations[1],$results[0]),true,$limit);
  1665. } else {
  1666. $results = $root->search($string,"nodes",distanceTo($locations[0]),1);
  1667. if (sizeof($results) > 0) {
  1668. $results = $results[0]->getSubNodes($r,distanceTo($locations[1],$results[0]),true,$limit);
  1669. }
  1670. }
  1671. }
  1672. // Exact matches if using keywords:
  1673. else if (isset($keywords['play']) || isset($keywords['radio'])) {
  1674. $results = $root->search($string,$stype,$distance,1,'exact');
  1675. if (sizeof($results) == 0) {
  1676. $results = $root->search($string,$stype,$distance,1); // better to limit 1 or $max_res?
  1677. }
  1678. }
  1679. else if (isset($keywords['best'])) {
  1680. $results = $root->search($string,$stype,$distance,-1,'exact');
  1681. if (sizeof($results) == 0) {
  1682. $results = $root->search($string,$stype,$distance,$max_res); // better to limit 1 or $max_res?
  1683. }
  1684. }
  1685. else {
  1686. $results = $root->search($string, $stype, $distance, $max_res);
  1687. }
  1688. if (sizeof($results) == 0) {
  1689. // Maybe a search by ID will work...
  1690. $results = $root->search($string, "id", $distance, $max_res);
  1691. if (sizeof($results) == 0) {
  1692. return $results;
  1693. }
  1694. }
  1695. $timer = round(microtime_float() - $timer, 2);
  1696. writeLogData('search',"searched '${search_type}' for '${string}' in $timer seconds.");
  1697. // What about keywords?
  1698. if (isset($keywords['play']) && sizeof($results) > 0) {
  1699. $pl = new jzPlaylist;
  1700. $pl->add($results);
  1701. // if (isset($keywords['limit'])) {
  1702. // $pl->flatten();
  1703. //}
  1704. if (isset($keywords['random'])) {
  1705. $pl = $pl->getSmartPlaylist($max_res);
  1706. } else if (isset($keywords['limit'])) {
  1707. $pl->flatten();
  1708. $pl->truncate($max_res);
  1709. }
  1710. $pl->play();
  1711. exit();
  1712. } else if (isset($keywords['radio'])) {
  1713. $pl = new jzPlaylist;
  1714. $pl->add($results);
  1715. $pl = $pl->getSmartPlaylist(50, "radio");
  1716. $pl->play();
  1717. exit();
  1718. }
  1719. return $results;
  1720. }
  1721. /**
  1722. * Checks to see if tracks or nodes are
  1723. * returned from a powersearch.
  1724. *
  1725. * @author Ben Dodson
  1726. * @since 4/6/05
  1727. * @version 4/6/05
  1728. *
  1729. **/
  1730. function powerSearchType() {
  1731. if (isset($_POST['song_title']) && $_POST['song_title'] != "")
  1732. return "tracks";
  1733. if (isset($_POST['length']) && $_POST['length'] != "")
  1734. return "tracks";
  1735. if (isset($_POST['number']) && $_POST['number'] != "")
  1736. return "tracks";
  1737. if (isset($_POST['year']) && $_POST['year'] != "")
  1738. return "tracks";
  1739. if (isset($_POST['bitrate']) && $_POST['bitrate'] != "")
  1740. return "tracks";
  1741. if (isset($_POST['frequency']) && $_POST['frequency'] != "")
  1742. return "tracks";
  1743. if (isset($_POST['size']) && $_POST['size'] != "")
  1744. return "tracks";
  1745. if (isset($_POST['type']) && $_POST['type'] != "")
  1746. return "tracks";
  1747. if (isset($_POST['comment']) && $_POST['comment'] != "")
  1748. return "tracks";
  1749. if (isset($_POST['lyrics']) && $_POST['lyrics'] != "")
  1750. return "tracks";
  1751. return "nodes";
  1752. }
  1753. /**
  1754. * Prepares the meta search array
  1755. * from a powersearch.
  1756. *
  1757. * @author Ben Dodson
  1758. * @since 4/6/05
  1759. * @version 4/6/05
  1760. **/
  1761. function getSearchMeta() {
  1762. $array = array();
  1763. if (isset($_POST['length']) && $_POST['length'] != "") {
  1764. $array['length_operator'] = $_POST['length_operator'];
  1765. if ($_POST['length_type'] == "minutes") {
  1766. $array['length'] = 60*$_POST['length'];
  1767. } else {
  1768. $array['length'] = $_POST['length'];
  1769. }
  1770. }
  1771. if (isset($_POST['number']) && $_POST['number'] != "") {
  1772. $array['number'] = $_POST['number'];
  1773. $array['number_operator'] = $_POST['number_operator'];
  1774. }
  1775. if (isset($_POST['year']) && $_POST['year'] != "") {
  1776. $array['year'] = $_POST['year'];
  1777. $array['year_operator'] = $_POST['year_operator'];
  1778. }
  1779. if (isset($_POST['bitrate']) && $_POST['bitrate'] != "") {
  1780. $array['bitrate'] = $_POST['bitrate'];
  1781. $array['bitrate_operator'] = $_POST['bitrate_operator'];
  1782. }
  1783. if (isset($_POST['frequency']) && $_POST['frequency'] != "") {
  1784. $array['frequency'] = $_POST['frequency'];
  1785. $array['frequency_operator'] = $_POST['frequency_operator'];
  1786. }
  1787. if (isset($_POST['size']) && $_POST['size'] != "") {
  1788. $array['size'] = $_POST['size'];
  1789. $array['size_operator'] = $_POST['size_operator'];
  1790. }
  1791. if (isset($_POST['type']) && $_POST['type'] != "") {
  1792. $array['type'] = $_POST['type'];
  1793. }
  1794. if (isset($_POST['comment']) && $_POST['comment'] != "")
  1795. $array['comment'] = $_POST['comment'];
  1796. if (isset($_POST['lyrics']) && $_POST['lyrics'] != "")
  1797. $array['lyrics'] = $_POST['lyrics'];

Large files files are truncated, but you can click here to view the full file