PageRenderTime 253ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/backend/class-track.php

https://github.com/jinzora/jinzora3
PHP | 399 lines | 235 code | 42 blank | 122 comment | 108 complexity | 477c97f39804a1d3acb387271e19ad30 MD5 | raw file
  1. <?php if (!defined(JZ_SECURE_ACCESS)) die ('Security breach detected.');
  2. /**
  3. * - JINZORA | Web-based Media Streamer -
  4. *
  5. * Jinzora is a Web-based media streamer, primarily desgined to stream MP3s
  6. * (but can be used for any media file that can stream from HTTP).
  7. * Jinzora can be integrated into a CMS site, run as a standalone application,
  8. * or integrated into any PHP website. It is released under the GNU GPL.
  9. *
  10. * - Resources -
  11. * - Jinzora Author: Ross Carlson <ross@jasbone.com>
  12. * - Web: http://www.jinzora.org
  13. * - Documentation: http://www.jinzora.org/docs
  14. * - Support: http://www.jinzora.org/forum
  15. * - Downloads: http://www.jinzora.org/downloads
  16. * - License: GNU GPL <http://www.gnu.org/copyleft/gpl.html>
  17. *
  18. * - Contributors -
  19. * Please see http://www.jinzora.org/modules.php?op=modload&name=jz_whois&file=index
  20. *
  21. * - Code Purpose -
  22. * These are the classes extended by the backend adaptors.
  23. *
  24. * @since 05.10.04
  25. * @author Ben Dodson <bdodson@seas.upenn.edu>
  26. */
  27. class jzMediaTrackClass extends jzMediaElement {
  28. var $playpath;
  29. var $meta;
  30. var $startTime;
  31. /**
  32. * Constructor for a jzMediaTrackClass
  33. *
  34. * @author
  35. * @version
  36. * @since
  37. */
  38. function jzMediaTrackClass($par = array(),$mode = "path") {
  39. $this->playpath = false;
  40. $this->meta = array();
  41. $this->startTime = 0;
  42. $this->_constructor($par,$mode);
  43. }
  44. /**
  45. * Returns the track's name (from ID3)
  46. *
  47. * @author Ben Dodson
  48. * @version 6/7/04
  49. * @since 6/7/04
  50. */
  51. function getName() {
  52. $cache = $this->readCache();
  53. return ($cache[7] == "-") ? parent::getName() : $cache[7];
  54. }
  55. /**
  56. * Returns the type of the node.
  57. *
  58. * @author Ben Dodson
  59. * @version 10/31/04
  60. * @since 10/31/04
  61. */
  62. function getType() {
  63. return "jzMediaTrack";
  64. }
  65. /**
  66. * Returns the track's complete file path (with $media_dir)
  67. *
  68. * The paramater is one of: user|host|general
  69. *
  70. * @author Ben Dodson
  71. * @version 6/7/04
  72. * @since 6/7/04
  73. */
  74. function getFileName($target = "user") {
  75. global $media_dirs, $web_dirs, $web_root, $root_dir, $protocols,
  76. $jzUSER, $allow_resample, $force_resample,
  77. $no_resample_subnets, $always_resample;
  78. if (checkPermission($jzUSER,'play',$this->getPath("String")) === false) {
  79. return false;
  80. }
  81. if ($this->playpath === false || $this->playpath == "") {
  82. $cache = $this->readCache();
  83. if ($cache[0] == "-") { // return error?
  84. $this->playpath = $this->getPath("String");
  85. }
  86. else {
  87. $this->playpath = $cache[0];
  88. }
  89. }
  90. if (isset($protocols) && isset($this->playpath)) {
  91. $parr = explode("|",$protocols);
  92. foreach ($parr as $p) {
  93. if (stristr($this->playpath,$p) !== false) {
  94. return $this->playpath;
  95. }
  96. }
  97. }
  98. /**********************/
  99. if ($target == "user" || $target == "general") {
  100. if ($target == "user" && ($local_root = $jzUSER->getSetting("localpath")) !== false) {
  101. if (stristr($media_dirs,"|") === false) {
  102. $tmp = $this->getFileName("host");
  103. if (stristr($tmp,$media_dirs) !== false) {
  104. return str_replace("//","/",str_replace($media_dirs,$local_root,$tmp));
  105. }
  106. }
  107. }
  108. $meta = $this->getMeta();
  109. $arr = array();
  110. //$arr['Artist'] = $meta['artist']; // decoy
  111. //$arr['Track'] = $meta['title']; // decoy
  112. // Resample?
  113. if (
  114. ($allow_resample == "true" || $force_resample == "true")
  115. && !(preg_match("/^${no_resample_subnets}$/", $_SERVER['REMOTE_ADDR']))
  116. ) {
  117. if ($jzUSER->getSetting('resample_lock')) {
  118. $arr["resample"] = $jzUSER->getSetting('resample_rate');
  119. }
  120. else if (isset($_SESSION['jz_resample_rate'])){
  121. if ($_SESSION['jz_resample_rate'] <> ""){
  122. // Ok already, we are resampling!!!
  123. $arr["resample"] = $_SESSION['jz_resample_rate'];
  124. }
  125. } else if (($jzUSER->getSetting('stream') === false && $jzUSER->getSetting('lofi') === true) || $force_resample == "true") {
  126. $arr["resample"] = $jzUSER->getSetting('resample_rate');
  127. }
  128. }
  129. $arr['jz_user'] = $jzUSER->getID(); // user id
  130. $arr['sid'] = $_SESSION['sid']; // unique session id
  131. if (getGlobal("CLIP_MODE")) {
  132. $arr['cl'] = 't';
  133. }
  134. // Now, if they are resampling we MUST end with an MP3
  135. $arr['ext'] = $meta['type'];
  136. if (isset($_SESSION['jz_resample_rate'])){
  137. if ($_SESSION['jz_resample_rate'] <> ""){
  138. $arr['ext'] = "mp3";
  139. }
  140. }
  141. // Now we need to see if this track is a type that always gets resampled
  142. if ($allow_resample == "true"){
  143. // Now do we have a type that is going to get resampled
  144. if (stristr($always_resample,$meta['type'])){
  145. // Ok, let's set it to MP3
  146. $arr['ext'] = "mp3";
  147. }
  148. }
  149. // Now should we send a path or ID?
  150. if ($web_dirs <> ""){
  151. return jzCreateLink($this->getFileName("host"),"track",$arr);
  152. } else {
  153. return jzCreateLink($this->getID(),"track",$arr);
  154. }
  155. } else {
  156. return $this->playpath;
  157. }
  158. }
  159. /**
  160. * Returns the track's metadata as an array with the following keys:
  161. *
  162. * title
  163. * bitrate
  164. * frequency
  165. * filename [excluding path]
  166. * size
  167. * year
  168. * comment
  169. * length
  170. * length_str
  171. * number
  172. * genre
  173. * artist
  174. * album
  175. * lyrics
  176. * type [extension]
  177. *
  178. * These are taken mostly from the ID3.
  179. *
  180. * @author Ben Dodson
  181. * @version 6/7/04
  182. * @since 6/7/04
  183. */
  184. function getMeta($mode = "cache", $installer = false) {
  185. global $track_num_seperator,$root_dir,$web_root, $include_path, $jzSERVICES;
  186. $meta = array();
  187. $cache = $this->readCache();
  188. if ($mode == "cache" && $this->meta != array()) {
  189. return $this->meta;
  190. }
  191. if ($mode == "cache") {
  192. $meta['title'] = $cache[7];
  193. $meta['bitrate'] = $cache[20];
  194. $meta['frequency'] = $cache[8];
  195. $meta['filename'] = $cache[2];
  196. $meta['size'] = $cache[13];
  197. $meta['year'] = $cache[11];
  198. $meta['comment'] = ($cache[9] == "-") ? "" : $cache[9];
  199. $meta['length'] = $len = $cache[14];
  200. $meta['number'] = $cache[21];
  201. $meta['genre'] = $cache[15];
  202. $meta['artist'] = $cache[16];
  203. $meta['album'] = $cache[17];
  204. $meta['lyrics'] = ($cache[19] == "" || $cache[19] == "-") ? "" : $cache[19];
  205. $meta['type'] = $cache[18];
  206. if (isNothing($meta['type'])) {
  207. $meta = $this->getMeta("file");
  208. $this->setMeta($meta,"cache");
  209. }
  210. }
  211. else { // Get it from the file.
  212. // other backend functions use this to get the id3 before the file is cached.
  213. if ($mode == "direct") {
  214. $fname = $this->getPath("String");
  215. } else {
  216. $fname = $this->getFileName("host");
  217. }
  218. // Do our services exist?
  219. if (!$jzSERVICES){
  220. include_once($include_path. 'services/class.php');
  221. $jzSERVICES = new jzServices();
  222. $jzSERVICES->loadStandardServices();
  223. }
  224. // Let's setup our tagdata service and return the tag data
  225. $meta = $jzSERVICES->getTagData($fname, $installer);
  226. // Hack to fix bug where first track returns null metadata.
  227. if ($meta['bitrate'] == '-') {
  228. // try again.
  229. $meta = $jzSERVICES->getTagData($fname, $installer);
  230. }
  231. }
  232. return $meta;
  233. }
  234. /**
  235. * Sets the track's meta information.
  236. * $meta is an array of meta fields to set.
  237. * $mode specifies where to update the meta,
  238. * false means do it in the cache and in the id3.
  239. *
  240. * @author Ben Dodson
  241. * @version 10/13/04
  242. * @since 10/13/04
  243. */
  244. function setMeta($meta, $mode = false, $displayOutput = false) {
  245. global $jzSERVICES, $allow_id3_modify,$backend,$hierarchy;
  246. if (is_array($hierarchy)) {
  247. $hstring = implode("/",$hierarchy);
  248. } else {
  249. $hstring = $hierarchy;
  250. }
  251. if ($mode == false) {
  252. // TODO: add variable to see if user allows ID3 updating.
  253. $this->setMeta($meta,"file");
  254. $this->setMeta($meta,"cache");
  255. }
  256. if ($mode == "cache") {
  257. $filecache = $this->readCache();
  258. if (isset($meta['title']))
  259. $filecache[7] = $meta['title'];
  260. if (isset($meta['frequency']))
  261. $filecache[8] = $meta['frequency'];
  262. if (isset($meta['comment']))
  263. $filecache[9] = $meta['comment'];
  264. if (isset($meta['year']))
  265. $filecache[11] = $meta['year'];
  266. if (isset($meta['size']))
  267. $filecache[13] = $meta['size'];
  268. if (isset($meta['length']))
  269. $filecache[14] = $meta['length'];
  270. if (isset($meta['genre']))
  271. $filecache[15] = $meta['genre'];
  272. if (isset($meta['artist']))
  273. $filecache[16] = $meta['artist'];
  274. if (isset($meta['album']))
  275. $filecache[17] = $meta['album'];
  276. if (isset($meta['type']))
  277. $filecache[18] = $meta['type'];
  278. if (isset($meta['lyrics']))
  279. $filecache[19] = $meta['lyrics'];
  280. if (isset($meta['bitrate']))
  281. $filecache[20] = $meta['bitrate'];
  282. if (isset($meta['number']))
  283. $filecache[21] = $meta['number'];
  284. $this->writeCache($filecache);
  285. return true;
  286. }
  287. else {
  288. if ($mode == "direct") {
  289. $fname = $this->getPath("String");
  290. }
  291. else {
  292. $fname = $this->getFileName("host");
  293. }
  294. if ($backend == "id3-database" || $backend == "id3-cache") {
  295. $oldmeta = $this->getMeta("file");
  296. }
  297. // Ok, now we need to write the data to this file IF it's an MP3
  298. if ($allow_id3_modify == "true"){
  299. $status = $jzSERVICES->setTagData($fname,$meta);
  300. } else {
  301. $status = true;
  302. }
  303. if (!isset($meta['genre'])) {
  304. $meta['genre'] = $oldmeta['genre'];
  305. }
  306. if (!isset($meta['artist'])) {
  307. $meta['artist'] = $oldmeta['artist'];
  308. }
  309. if (!isset($meta['album'])) {
  310. $meta['album'] = $oldmeta['album'];
  311. }
  312. if (!isset($meta['filename'])) {
  313. $meta['filename'] = $oldmeta['filename'];
  314. }
  315. if ($backend == "id3-database" || $backend == "id3-cache") {
  316. if (($oldmeta['genre'] != $meta['genre'] && stristr($hstring,"genre") !== false)||
  317. ($oldmeta['artist'] != $meta['artist'] && stristr($hstring,"artist") !== false) ||
  318. ($oldmeta['album'] != $meta['album'] && stristr($hstring,"album") !== false) ||
  319. ($oldmeta['filename'] != $meta['filename'])) {
  320. // The media needs to be moved.
  321. $arr = buildPath($meta);
  322. $newpath = implode("/",$arr);
  323. $root = new jzMediaNode();
  324. $root->moveMedia($this,$newpath);
  325. $this->reconstruct($newpath);
  326. }
  327. }
  328. return $status;
  329. }
  330. }
  331. /**
  332. * Returns the track's lyrics.
  333. *
  334. * @author Ben Dodson
  335. * @version 6/7/04
  336. * @since 6/7/04
  337. */
  338. function getLyrics() {
  339. $cache = $this->readCache();
  340. return $cache[19];
  341. }
  342. /**
  343. * Returns true, since this element is a leaf.
  344. *
  345. * @author Laurent Perrin
  346. * @version 5/10/04
  347. * @since 5/10/04
  348. */
  349. function isLeaf() { return true; }
  350. function setStartTime($time) {
  351. $this->startTime = $time;
  352. }
  353. function getStartTime() {
  354. return $this->startTime;
  355. }
  356. }
  357. ?>