PageRenderTime 36ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/general.lib.php

https://github.com/jinzora/jinzora3
PHP | 2704 lines | 1636 code | 326 blank | 742 comment | 426 complexity | 67c4ad2ce1378ed2f99665a131920a30 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. * Jinzora Author:
  11. * Ross Carlson: ross@jasbone.com
  12. * 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: This page contains all the "general" related functions
  22. * Created: 9.24.03 by Ross Carlson
  23. *
  24. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  25. /**
  26. * Setup the Smarty template system
  27. *
  28. * @author Ross Carlson
  29. * @since 2.27.06
  30. *
  31. **/
  32. function smartySetup(){
  33. global $web_root, $root_dir, $include_path;
  34. $d = dirname(__FILE__);
  35. $root = substr($d, 0, strlen($d)-4) . "/";
  36. defined('SMARTY_DIR') or define('SMARTY_DIR', $root. 'lib/smarty/');
  37. defined('SMARTY_ROOT') or define('SMARTY_ROOT', $root);
  38. require_once(SMARTY_DIR . 'Smarty.class.php');
  39. $smarty = new Smarty;
  40. $smarty->compile_dir = $root. 'temp/';
  41. $smarty->cache_dir = $root. 'temp/';
  42. return $smarty;
  43. }
  44. /*
  45. * Gets a global variable without
  46. * having to declare global.
  47. * Useful for settings.
  48. *
  49. * @author Ben Dodson
  50. * @since 1/30/07
  51. */
  52. function conf($name) {
  53. global $$name;
  54. if (isset($$name)) {
  55. return $$name;
  56. } else {
  57. return null;
  58. }
  59. }
  60. /**
  61. * Verifies that a directory exists and if not creates it
  62. *
  63. * @author Ross Carlson
  64. * @since 11/02/2005
  65. * @param $dir Name of the directory to create
  66. *
  67. **/
  68. function makedir($dir){
  69. $dArr = explode("/",$dir);
  70. $prevDir = "";
  71. foreach($dArr as $dir){
  72. $prevDir .= $dir. "/";
  73. if (!is_dir($prevDir)){
  74. mkdir($prevDir);
  75. }
  76. }
  77. }
  78. /**
  79. * Cleans a potential filename of back characters
  80. *
  81. * @author Ross Carlson
  82. * @since 11/02/2005
  83. * @param $name Name of the track to clean
  84. * @return $ret Cleaned track name
  85. *
  86. **/
  87. function cleanFileName($name){
  88. $badArray = explode(";",';?;";/;\;|;*;<;>;:');
  89. for ($e=0;$e<count($badArray);$e++){
  90. $name = str_replace($badArray[$e],"",$name);
  91. }
  92. return $name;
  93. }
  94. /**
  95. * Grabs the data that was parsed from a Podcast
  96. *
  97. * @author Ross Carlson
  98. * @since 11/02/2005
  99. * @param $item An array with all the values to grab
  100. * @param $folder The subfolder to store the file in
  101. * @return boolean true|false
  102. *
  103. **/
  104. function getPodcastData($item, $folder){
  105. global $include_path, $podcast_folder;
  106. if ($item['file'] == ""){return false;}
  107. $be = new jzBackend();
  108. $display = new jzDisplay();
  109. // Let's clean the new folder name
  110. $folder = trim(cleanFileName($folder));
  111. // Let's grab the file and save it to disk
  112. $ext = substr($item['file'],strlen($item['file'])-3,3);
  113. $track = trim(cleanFileName($item['title']. ".". $ext));
  114. if (substr($podcast_folder,0,1) <> "/"){
  115. $dir = str_replace("\\","/",getcwd()). "/". $podcast_folder. "/". $folder;
  116. } else {
  117. $dir = $podcast_folder. "/". $folder;
  118. }
  119. $track = $dir. "/". $track;
  120. // Now let's create the directory we need
  121. makedir($dir);
  122. // Now let's see if the file already exists
  123. if (!is_file($track)){
  124. ?>
  125. <script language="javascript">
  126. t.innerHTML = '<?php echo word("Downloading"). ": ". $display->returnShortName($item['title'],45); ?>';
  127. -->
  128. </SCRIPT>
  129. <?php
  130. flushdisplay();
  131. // Now let's grab the file and write it out
  132. $fName = str_replace("&amp;","&",$item['file']);
  133. $data = file_get_contents($fName);
  134. $handle = fopen($track, "w");
  135. fwrite($handle,$data);
  136. fclose ($handle);
  137. ?>
  138. <script language="javascript">
  139. t.innerHTML = '<?php echo word("Download Complete!"); ?>';
  140. -->
  141. </SCRIPT>
  142. <?php
  143. flushdisplay();
  144. } else {
  145. ?>
  146. <script language="javascript">
  147. t.innerHTML = '<?php echo word("Exists - moving to next track..."); ?>';
  148. -->
  149. </SCRIPT>
  150. <?php
  151. flushdisplay();
  152. }
  153. return $track;
  154. }
  155. /**
  156. * Parses a given Podcast URL to retrieve all the enclosures
  157. *
  158. * @author Ross Carlson
  159. * @since 11/02/2005
  160. * @param $url The URL to parse
  161. * @return array of the files and descriptions in the XML file
  162. *
  163. **/
  164. function parsePodcastXML($url){
  165. // Let's get the data from the URL
  166. $data = file_get_contents($url);
  167. $c=0;
  168. // Now let's parse out the basics about the feed
  169. $title = substr($data,strpos($data,"<title>")+strlen("<title>"));
  170. $title = substr($title,0,strpos($title,"</title>"));
  171. $desc = substr($data,strpos($data,"<description>")+strlen("<description>"));
  172. $desc = substr($desc,0,strpos($desc,"</description>"));
  173. $pubDate = substr($data,strpos($data,"<pubDate>")+strlen("<pubDate>"));
  174. $pubDate = substr($pubDate,0,strpos($pubDate,"</pubDate>"));
  175. $image = "";
  176. if (stristr($data,"<itunes:image")){
  177. $image = substr($data,strpos($data,"<itunes:image"));
  178. $image = substr($image,strpos($image,'href="')+6);
  179. $image = substr($image,0,strpos($image,'"'));
  180. }
  181. // Now let's set the array
  182. $retArray['title'] = $title;
  183. $retArray['desc'] = $desc;
  184. $retArray['pubDate'] = $pubDate;
  185. $retArray['image'] = $image;
  186. $c++;
  187. // Now let's get the individual items
  188. $items = substr($data,strpos($data,"<item>"));
  189. $iArr = explode("</item>",$items);
  190. // Now let's loop
  191. foreach ($iArr as $item){
  192. // Now let's parse that out
  193. $title = substr($item,strpos($item,"<title>")+strlen("<title>"));
  194. $title = substr($title,0,strpos($title,"</title>"));
  195. $desc = substr($item,strpos($item,"<description>")+strlen("<description>"));
  196. $desc = substr($desc,0,strpos($desc,"</description>"));
  197. $desc = str_replace("]]>","",str_replace("<![CDATA[","",$desc));
  198. $link = substr($item,strpos($item,"<link>")+strlen("<link>"));
  199. $link = substr($link,0,strpos($link,"</link>"));
  200. $pubDate = substr($item,strpos($item,"<pubDate>")+strlen("<pubDate>"));
  201. $pubDate = substr($pubDate,0,strpos($pubDate,"</pubDate>"));
  202. $file = substr($item,strpos($item,'url="')+strlen('url="'));
  203. $file = substr($file,0,strpos($file,'"'));
  204. $length = substr($item,strpos($item,'length="')+strlen('length="'));
  205. $length = substr($length,0,strpos($length,'"'));
  206. // Now let's set the return array
  207. $retArray[$c]['title'] = $title;
  208. $retArray[$c]['link'] = $link;
  209. $retArray[$c]['pubDate'] = $pubDate;
  210. $retArray[$c]['file'] = $file;
  211. $retArray[$c]['desc'] = $desc;
  212. $retArray[$c]['length'] = $length;
  213. $c++;
  214. }
  215. // Now let's return
  216. return $retArray;
  217. }
  218. /*
  219. * Fallback function for stripos
  220. *
  221. * @author Ben Dodson, from PHP.net
  222. * @since 8/31/06
  223. */
  224. if (!function_exists("stripos")) {
  225. function stripos($str,$needle,$offset=0) {
  226. return strpos(strtolower($str),strtolower($needle),$offset);
  227. }
  228. }
  229. /**
  230. * Allows you to write 1 setting to a settings file
  231. *
  232. * @author Ross Carlson
  233. * @since 7/04/2005 - Happy 4th all you Americans!!!
  234. * @param $setting The setting to write
  235. * @param $val The value to write to the setting
  236. * @param $file The file to write too (the full path and filename)
  237. * @return true|false wether the action was successful
  238. *
  239. **/
  240. function writeSetting($setting, $val, $file){
  241. // Now let's open the file and read it in
  242. $fArr = file($file);
  243. // Let's start our new file
  244. $newFile = "";
  245. // Now let's loop through and find the setting
  246. foreach($fArr as $line){
  247. // Now does this line have the setting we want?
  248. if (stristr($line,$setting)){
  249. // Ok we found it, let's change it
  250. $newFile .= substr($line,0,strpos($line," =")). ' = "'. $val. '";'. "\n";
  251. } else {
  252. // Nope, not the line
  253. $newFile .= $line;
  254. }
  255. }
  256. // Now we need to write the file back out
  257. if (is_writable($file)){
  258. $handle = fopen($file, "w");
  259. fwrite($handle,$newFile);
  260. fclose ($handle);
  261. return true;
  262. } else {
  263. return false;
  264. }
  265. }
  266. /**
  267. * Checks the site's general security.
  268. *
  269. * @author Ben Dodson
  270. * @since 6/30/05
  271. *
  272. **/
  273. function checkSecurity() {
  274. // TODO: Check the include file; make sure it is index.php
  275. // or a CMS-specified file.
  276. return;
  277. }
  278. /**
  279. * Makes sure a file is safe to include.
  280. *
  281. * @author Ben Dodson
  282. * @since 8/15/05
  283. *
  284. **/
  285. function includeable_file($fname, $dir = false) {
  286. global $include_path;
  287. if (stristr($fname,'/') !== false) {
  288. return false;
  289. }
  290. if (stristr($fname,"\\") !== false) {
  291. return false;
  292. }
  293. if ($dir !== false) {
  294. $dir = $include_path . $dir;
  295. // make sure it's in $dir.
  296. if (!is_dir($dir)) {
  297. die("$dir is not a valid directory.");
  298. }
  299. $d = dir($dir);
  300. while ($entry = $d->read()) {
  301. if ($entry == $fname) {
  302. return true;
  303. }
  304. }
  305. return false;
  306. }
  307. return true;
  308. }
  309. function emu_getallheaders() {
  310. foreach ($_SERVER as $name => $value)
  311. {
  312. if (substr($name, 0, 5) == 'HTTP_')
  313. {
  314. $name = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))));
  315. $headers[$name] = $value;
  316. } else if ($name == "CONTENT_TYPE") {
  317. $headers["Content-Type"] = $value;
  318. } else if ($name == "CONTENT_LENGTH") {
  319. $headers["Content-Length"] = $value;
  320. }
  321. }
  322. return $headers;
  323. }
  324. if (!function_exists('getallheaders')) {
  325. function getallheaders() {
  326. return emu_getallheaders();
  327. }
  328. }
  329. /**
  330. *
  331. * Sets a global variable.
  332. *
  333. * @author Ben Dodson
  334. * @since 6/7/05
  335. *
  336. **/
  337. function setGlobal($var,$val) {
  338. $GLOBALS[$var] = $val;
  339. }
  340. /**
  341. *
  342. * Gets a global variable.
  343. *
  344. * @author Ben Dodson
  345. * @since 6/7/05
  346. *
  347. **/
  348. function getGlobal($var) {
  349. return isset($GLOBALS[$var]) ? $GLOBALS[$var] : false;
  350. }
  351. /**
  352. *
  353. * Takes binary data and writes it to a file
  354. *
  355. * @author Ross Carlson
  356. * @since 3/31/05
  357. * @param $file the name of the file (the full path)
  358. * @param $data the binary data to write to the file
  359. * @param return returns true or false (bolean)
  360. *
  361. **/
  362. function writeImage($file, $data){
  363. global $bad_chars;
  364. foreach ($bad_chars as $item){
  365. str_replace($item,"",$file);
  366. }
  367. if (is_file($file)){ unlink($file); }
  368. $handle = fopen($file, "w");
  369. if (fwrite($handle,$data)){
  370. fclose ($handle);
  371. return true;
  372. } else {
  373. fclose ($handle);
  374. return false;
  375. }
  376. }
  377. /**
  378. * Makes a string safe for XML
  379. * @author php.net
  380. * @since 8/11/05
  381. **/
  382. function htmlnumericentities($str){
  383. return preg_replace('/[^!-%\x27-;=?-~ ]/e', '"&#".ord("$0").chr(59)', $str);
  384. }
  385. function xmlentities($string) {
  386. return str_replace ( array ( '&', '"', "'", '<', '>', '�' ), array ( '&amp;' , '&quot;', '&apos;' , '&lt;' , '&gt;', '&apos;' ), $string );
  387. }
  388. /**
  389. * @author Ben Dodson
  390. *
  391. **/
  392. function sendFileBundle($myfiles, $name) {
  393. global $multiple_download_mode, $download_speed;
  394. $files = array();
  395. foreach ($myfiles as $file) {
  396. if ($file != '' && is_file($file)) {
  397. $files[] = $file;
  398. }
  399. }
  400. if ($files == array()) {
  401. exit();
  402. }
  403. if ($multiple_download_mode == 'tar') {
  404. $reader = &new jzStreamTar($files);
  405. header ('Content-Type: application/x-tar');
  406. header ('Content-Disposition: attachment; filename="' . $name . '.tar"');
  407. } else { // assume zip
  408. $reader = &new jzStreamZip($files);
  409. header ('Content-Type: application/zip');
  410. header ('Content-Disposition: attachment; filename="' . $name . '.zip"');
  411. }
  412. // content length header if supported
  413. if (($size = $reader->FinalSize()) != 0) {
  414. $range = getContentRange($size);
  415. if ($range !== false) {
  416. $range_from = $range[0];
  417. $range_to = $range[1];
  418. } else {
  419. $range_from = 0;
  420. $range_to = $size-1;
  421. }
  422. // BJD 6/30/06:
  423. // HACK: range_to is not supported- only resuming and requesting entire
  424. // remains of file
  425. $range_to = $size-1;
  426. if ($range === false) {
  427. // Content length has already been sent
  428. header("Content-length: ".(string)$size);
  429. } else {
  430. header("HTTP/1.1 206 Partial Content");
  431. header("Accept-Range: bytes");
  432. header("Content-Length: " . ($size - $range_from));
  433. header("Content-Range: bytes $range_from" . "-" . ($range_to) . "/$size");
  434. }
  435. } else {
  436. $range = false;
  437. $range_from = $range_to = 0;
  438. }
  439. // caching headers
  440. header("Cache-control: private");
  441. header("Expires: " . gmdate("D, d M Y H:i:s", mktime(date("H") + 8, date("i"), date("s"), date("m"), date("d"), date("Y"))) . " GMT");
  442. //header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  443. $lastmod = 0;
  444. foreach ($files as $f) {
  445. if (($t = filemtime($f)) > $lastmod) {
  446. $lastmod = $t;
  447. }
  448. }
  449. header("Last-Modified: " . gmdate("D, d M Y H:i:s", $lastmod) . " GMT");
  450. header("Pragma: no-cache");
  451. // Let's up the max_execution_time
  452. //ini_set('max_execution_time','6000');
  453. @set_time_limit(0);
  454. // open reader
  455. $reader->Open();
  456. // Are we resuming?
  457. if ($range_from > 0) {
  458. $reader->Read($range_from);
  459. }
  460. // let's send, no speed limit
  461. if ($download_speed == 0 || $download_speed == "") {
  462. while ( ($data=$reader->Read(4096))!='' and connection_status()==0 ) {
  463. echo $data;
  464. flush();
  465. }
  466. } else {
  467. // let's send, looking at the speed
  468. $sent = 0;
  469. $begin_time = get_time();
  470. while ( ($data=$reader->Read(4096))!='' and connection_status()==0 ) {
  471. echo $data;
  472. flush();
  473. $sent += 4;
  474. // if current speed is too high, let's wait a bit
  475. while ($sent/(get_time()-$begin_time) > $download_speed) {
  476. sleep(1);
  477. }
  478. }
  479. }
  480. // close reader
  481. $reader->Close();
  482. @set_time_limit(30);
  483. //write the result in log
  484. if (connection_status() == 0) {
  485. writeLogData('download','download of '.$name.' sucessful');
  486. }
  487. else {
  488. writeLogData('download','download of '.$name.' failed');
  489. }
  490. }
  491. /**
  492. * Displays the image in a browser.
  493. *
  494. *
  495. * @author Ben Dodson
  496. * @version 11/10/04
  497. * @since 11/10/04
  498. */
  499. function showImage ($path) {
  500. global $include_path;
  501. // Now let's see if this is an ID3 image or not
  502. if (stristr($path,"ID3:")){
  503. // Now let's get the data we need
  504. include_once($include_path. 'services/class.php');
  505. $jzSERVICES = new jzServices();
  506. $jzSERVICES->loadStandardServices();
  507. // Now let's fix the path
  508. $path = substr($path,4);
  509. $meta = $jzSERVICES->getTagData($path);
  510. // Now let's set the header
  511. header("Content-Type: ". $meta['pic_mime']);
  512. sendID3Image($path, $meta['pic_name'],$meta['pic_data']);
  513. } else {
  514. $arr = explode("/",$path);
  515. if (sizeof($arr > 0))
  516. $name = $arr[sizeof($arr)-1];
  517. else
  518. $name = $path;
  519. if (substr($path, -3) == "jpg" || substr($path, -4) == "jpeg" || substr($path, -3) == "jpe")
  520. header("Content-Type: image/jpeg");
  521. else if (substr($path, -3) == "gif")
  522. header("Content-Type: image/gif");
  523. else if (substr($path, -3) == "png")
  524. header("Content-Type: image/png");
  525. else if (substr($path, -3) == "bmp")
  526. header("Content-Type: image/bmp");
  527. }
  528. // TODO: GD stuff; synchronize args with jzDisplay::image.
  529. // images are small, so don't worry about handling stream stuff.
  530. streamFile($path,$name);
  531. }
  532. /**
  533. * Sends the content-type header
  534. *
  535. * @author Ben Dodson
  536. * @version 8/17/05
  537. * @since 8/17/05
  538. **/
  539. function sendContentType($ext) {
  540. switch ($ext){
  541. case "mp3":
  542. header("Content-Type: audio/mpeg");
  543. break;
  544. case "wav":
  545. header("Content-Type: audio/x-wav");
  546. break;
  547. case "mpc":
  548. header("Content-Type: application/mpc");
  549. break;
  550. case "wv":
  551. header("Content-Type: application/wv");
  552. break;
  553. case ".ra":
  554. case ".rm":
  555. header("Content-Type: audio/x-pn-realaudio");
  556. break;
  557. case "flac":
  558. header("Content-Type: application/flac");
  559. break;
  560. case "ogg":
  561. header("Content-Type: application/ogg");
  562. break;
  563. case "avi":
  564. header("Content-Type: video/x-msvideo");
  565. break;
  566. case "mpg":
  567. case "mpeg":
  568. header("Content-Type: video/x-mpeg");
  569. break;
  570. case "asf":
  571. case "asx":
  572. case "wma":
  573. case "wmv":
  574. header("Content-Type: video/x-ms-asf");
  575. break;
  576. case "mov":
  577. header("Content-Type: application/x-quicktimeplayer");
  578. break;
  579. case "flv":
  580. header("Content-Type: video/x-flv");
  581. break;
  582. case "mid":
  583. case "midi":
  584. header("Content-Type: audio/midi");
  585. break;
  586. case "aac":
  587. case "mp4":
  588. header("Content-Type: application/x-quicktimeplayer");
  589. break;
  590. }
  591. }
  592. /**
  593. * Sends a clip of a media file.
  594. *
  595. * @author Ben Dodson
  596. * @version 8/17/05
  597. * @since 8/17/05
  598. */
  599. function sendClip($el) {
  600. global $clip_length, $clip_start;
  601. if (!$el->isLeaf()) {
  602. return false;
  603. }
  604. $fname = $el->getFilename("host");
  605. $ext = substr($fname,strrpos($fname,".")+1);
  606. $clipfile = substr($fname,0,-4).".clip." . $ext;
  607. if (is_file($clipfile)) {
  608. sendMedia($clipfile, $el->getName() . " Clip." . $ext);
  609. exit();
  610. }
  611. $meta = $el->getMeta();
  612. $bitrate = $meta['bitrate'];
  613. if (!is_numeric($bitrate)) {
  614. // Let's assume 160.
  615. $bitrate = 160;
  616. }
  617. $cstart = $clip_start * ($bitrate * 1024 / 8);
  618. $clength = $clip_length * ($bitrate * 1024 / 8);
  619. $contents = substr(file_get_contents($fname),$cstart,$clength);
  620. sendContentType($ext);
  621. header("Content-length: " . $clength);
  622. header("Content-Disposition: inline; filename=\"".$el->getName() . " Clip." . $ext."\"");
  623. header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
  624. header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
  625. header("Cache-Control: no-cache, must-revalidate");
  626. header("Pragma: no-cache");
  627. print $contents;
  628. exit();
  629. }
  630. /**
  631. * Sends the actual media file
  632. * and possibly resamples it.
  633. *
  634. *
  635. * @author Ben Dodson
  636. * @version 11/11/04
  637. * @since 11/11/04
  638. */
  639. function sendMedia($path, $name, $resample=false, $download = false) {
  640. // Let's get the extension from the file
  641. $extArr = explode(".",$path);
  642. $ext = $extArr[count($extArr)-1];
  643. // Now let's fix up the name
  644. if (substr($name,0-strlen($ext)-1) != "." . $ext) {
  645. $name .= "." . $ext;
  646. }
  647. if ($resample != "") {
  648. $ext = false;
  649. }
  650. // TODO: resample.
  651. // probably make a different streamFile (streamResampled)
  652. streamFile($path,$name,false,$resample,$download, true, $ext);
  653. }
  654. /**
  655. * Sends the ID3 image
  656. *
  657. * @author Ben Dodson
  658. * @version 7/20/05
  659. * @since 7/20/05
  660. **/
  661. function sendID3Image($path,$name,$id3) {
  662. header("Content-length: ".(string)(strlen($id3)));
  663. header("Content-Disposition: inline; filename=\"".$name."\"");
  664. header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
  665. header("Last-Modified: ".gmdate("D, d M Y H:i:s", filemtime($path))." GMT");
  666. header("Cache-Control: no-cache, must-revalidate");
  667. header("Pragma: no-cache");
  668. print($id3);
  669. return true;
  670. }
  671. /**
  672. * Actually sends the data in the specified file.
  673. *
  674. *
  675. * @author Ben Dodson, PHP.net
  676. * @version 11/11/04
  677. * @since 11/11/04
  678. */
  679. function streamFile($path,$name,$limit=false,$resample="",$download = false, $contentTypeFor = false) {
  680. global $always_resample, $allow_resample, $always_resample_rate, $jzUSER;
  681. // Let's ignore if they abort, that way we'll know when the track stops...
  682. ignore_user_abort(TRUE);
  683. $jzSERVICES = new jzServices();
  684. $jzSERVICES->loadStandardServices();
  685. $status = false;
  686. if ($limit === false)
  687. $speed_limit = 1*1024; // from system.php?
  688. else
  689. $speed_limit = $limit;
  690. // limit is passed as a param because we may want to limit it for downloads
  691. // but not for streaming / image viewing.
  692. // Also, we may want to write a different function for resampling,
  693. // but I don't know yet.
  694. // IF NO SPEED LIMIT:
  695. // the 'speed_limit' from above is the amount
  696. // of buffer used while sending the file.
  697. // but with no speed limit, there is no 'sleep' issued.
  698. // this makes seeking in a file much faster.
  699. // Let's get the extension of the real file
  700. $extArr = explode(".",$path);
  701. $ext = $extArr[count($extArr)-1];
  702. if (!is_file($path) || connection_status() != 0) return (false);
  703. $meta = $jzSERVICES->getTagData($path);
  704. $do_resample = false;
  705. if (!isNothing($resample)) {
  706. $do_resample = true;
  707. }
  708. if (($allow_resample == "true") && stristr($always_resample,$ext)) {
  709. $do_resample = true;
  710. }
  711. if ($meta['type'] == "mp3") {
  712. if (!isNothing($resample) && $resample >= $meta['bitrate']) {
  713. $do_resample = false;
  714. }
  715. }
  716. if ($download) {
  717. $do_resample = false;
  718. }
  719. // Are they resampling or transcoding?
  720. if ($do_resample){
  721. // Ok, if resampling isn't set let's go with the default
  722. if ($resample == ""){
  723. $resample = $always_resample_rate;
  724. }
  725. // Now let's load up the resampling service
  726. $jzSERVICES = new jzServices();
  727. $jzSERVICES->loadService("resample","resample");
  728. $jzSERVICES->resampleFile($path,$name,$resample);
  729. // Now let's unset what they are playing
  730. $be = new jzBackend();
  731. $be->unsetPlaying($_GET['jz_user'],$_GET['sid']);
  732. return;
  733. }
  734. // Now we need to know if this is an ID3 image or not
  735. // First let's get their limit
  736. $limit = "7";
  737. $size = filesize($path);
  738. $range = getContentRange($size);
  739. if ($range !== false) {
  740. $range_from = $range[0];
  741. $range_to = $range[1];
  742. } else {
  743. $range_from = 0;
  744. $range_to = $size-1;
  745. }
  746. $ps3 = false;
  747. $allheaders = getallheaders();
  748. if (isset($allheaders['User-Agent']) && $allheaders['User-Agent'] == "PLAYSTATION 3") {
  749. $ps3 = true;
  750. }
  751. if ($ps3) {
  752. // ps3 is picky and bizarre.
  753. if ($range_from > $size) {
  754. // This happens if the ps3 thinks the file
  755. // is larger than it is, and sending a
  756. // This is supposed to be a read of the id3v1 tag at
  757. // the end of a file (128 bytes), or the lyrics tag
  758. // (138 bytes)
  759. $requested = $range_to - $range_from + 1;
  760. $range_from = $size - $requested;
  761. $range_to = $size - 1;
  762. header("HTTP/1.1 206 Partial content");
  763. } else if ($range_from == 0 && $size == $range_to + 1) {
  764. header("HTTP/1.1 200 OK");
  765. } else {
  766. header("HTTP/1.1 206 Partial content");
  767. header("CONTENT-RANGE: bytes ${range_from}-${range_to}/${size}");
  768. }
  769. header("transferMode.dlna.org: Streaming");
  770. header("contentFeatures.dlna.org: DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS=017000 00000000000000000000000000");
  771. header("Accept-Ranges: bytes");
  772. header("Connection: keep-alive");
  773. header("Content-Length: " . ($size - $range_from));
  774. } else if ($range === false) {
  775. // Content length has already been sent
  776. header("Content-Length: ".(string)$size);
  777. } else {
  778. header("HTTP/1.1 206 Partial Content");
  779. header("Accept-Range: bytes");
  780. header("Content-Length: " . ($size - $range_from));
  781. header("Content-Range: bytes $range_from" . "-" . ($range_to) . "/$size");
  782. }
  783. if ($contentTypeFor !== false) {
  784. sendContentType($contentTypeFor);
  785. }
  786. if (!$ps3) {
  787. header("Content-Disposition: inline; filename=\"".$name."\"");
  788. header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
  789. header("Last-Modified: ".gmdate("D, d M Y H:i:s", filemtime($path))." GMT");
  790. header("Cache-Control: no-cache, must-revalidate");
  791. header("Pragma: no-cache");
  792. }
  793. if ($file = fopen($path, 'rb')) {
  794. @set_time_limit(0);
  795. fseek($file, $range_from);
  796. while(!feof($file) and (connection_status()==0) and ($cur_pos = ftell($file)) < $range_to+1) {
  797. print(fread($file, min(1024*$speed_limit, $range_to + 1 - $cur_pos)));
  798. flush();
  799. if ($limit !== false) {
  800. sleep(1);
  801. }
  802. }
  803. $status = (connection_status()==0);
  804. fclose($file);
  805. @set_time_limit(30);
  806. }
  807. // Now let's unset what they are playing
  808. $be = new jzBackend();
  809. $be->unsetPlaying($_GET['jz_user'],$_GET['sid']);
  810. return($status);
  811. }
  812. /**
  813. * Converts an associative array to a link.
  814. * Function can take 2 arrays and treats them both the same.
  815. * Useful if you have a set of variables that you use a lot
  816. * and another set you use only once or twice.
  817. *
  818. * This function also remembers things like GET-based frontend/theme settings.
  819. *
  820. * @author Ben Dodson
  821. * @version 11/6/04
  822. * @since 11/5/04
  823. */
  824. function urlize($arr1 = array(), $arr2 = array()) {
  825. global $this_page, $root_dir, $link_root, $include_path, $ssl_stream, $secure_urls,$jzUSER;
  826. $this_page = setThisPage();
  827. $arr = $arr1 + $arr2;
  828. if (!isset($arr['action'])) {
  829. $action="";
  830. } else {
  831. $action = $arr['action'];
  832. }
  833. if (isset($arr['jz_path']) && $arr['jz_path'] == "") {
  834. unset($arr['jz_path']);
  835. }
  836. if ($action != "play" && $action != "playlist") {
  837. if (isset($_GET['frontend']) && !isset($arr['frontend'])) {
  838. $arr['frontend'] = $_GET['frontend'];
  839. } else if (isset($_POST['frontend']) && !isset($arr['frontend'])) {
  840. $arr['frontend'] = $_POST['frontend'];
  841. }
  842. if (isset($_GET['theme']) && $_GET['theme'] != '' && !isset($arr['theme'])) {
  843. $arr['theme'] = $_GET['theme'];
  844. } else if (isset($_POST['theme']) && $_POST['theme'] != '' && !isset($arr['theme'])) {
  845. $arr['theme'] = $_POST['theme'];
  846. }
  847. }
  848. switch ($action) {
  849. case "play":
  850. $link = "";
  851. if ($ssl_stream == "true") {
  852. $link .= "https://";
  853. } else {
  854. $link .= "http://";
  855. }
  856. $link .= $_SERVER['HTTP_HOST'];
  857. if ($_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 443 && (strpos($link,":") === false)) {
  858. $link .= ":" . $_SERVER['SERVER_PORT'];
  859. }
  860. $link = str_replace(":443","",$link);
  861. $link .= $root_dir . "/mediabroadcast.php?";
  862. $ENC_FUNC = "jz_track_encode";
  863. // fairly gross hack for non-mp3 files:
  864. if (isset($arr['ext'])) {
  865. $extension = $arr['ext'];
  866. unset($arr['ext']);
  867. } else {
  868. $extension = false;
  869. }
  870. break;
  871. case "image":
  872. //case "download":
  873. $link = "";
  874. if ($_SERVER['SERVER_PORT'] == 443) {
  875. $link .= "https://";
  876. } else {
  877. $link .= "http://";
  878. }
  879. $link .= $_SERVER['HTTP_HOST'];
  880. if ($_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 443 && (strpos($link,":") === false)) {
  881. $link .= ":" . $_SERVER['SERVER_PORT'];
  882. }
  883. $link = str_replace(":443","",$link);
  884. $link .= $root_dir . "/mediabroadcast.php?image=tr&";
  885. $ENC_FUNC = "jz_encode";
  886. // fairly gross hack for non-mp3 files:
  887. if (isset($arr['ext'])) {
  888. $extension = $arr['ext'];
  889. unset($arr['ext']);
  890. } else {
  891. $extension = false;
  892. }
  893. break;
  894. case "popup":
  895. $link = $root_dir . "/popup.php?";
  896. $ENC_FUNC = "jz_encode";
  897. $extension = false;
  898. break;
  899. default:
  900. $link = $this_page; // setThisPage handles CMS stuff and adds & or ?.
  901. $ENC_FUNC = "jz_encode";
  902. $extension = false;
  903. }
  904. if ($action == "playlist") {
  905. $extension = $jzUSER->getSetting('playlist_type');
  906. }
  907. //$jz_secure = "true"; // for now.
  908. $vars="";
  909. if ($secure_urls == "true" && !url_full_exception($arr)) {
  910. foreach ($arr as $key => $val) {
  911. // if jz_secure, jz_encode our keys and vars.
  912. if (!url_key_exception($key))
  913. $vars .= "&" . urlencode($ENC_FUNC($key)) . "=" . urlencode($ENC_FUNC($val));
  914. else
  915. $vars .= "&" . urlencode($key) . "=" . urlencode($val);
  916. }
  917. } else {
  918. foreach ($arr as $key => $val) {
  919. $vars .= "&" . urlencode($key) . "=" . urlencode($val);
  920. }
  921. }
  922. $link = $link . substr($vars,1);
  923. if (!isNothing($extension)) {
  924. $link .= "&ext." . $extension;
  925. } else {
  926. $link .= "&ext.html";
  927. }
  928. return $link;
  929. }
  930. /**
  931. * Gets the URL root from a URLized string.
  932. *
  933. * @author Ben Dodson
  934. * @since 7/16/06
  935. * @version 7/16/06
  936. */
  937. function getURLRoot($url) {
  938. return substr($url, 0, strpos($url,'?'));
  939. }
  940. /**
  941. * Gets the URL root from a URLized string.
  942. *
  943. * @author Ben Dodson
  944. * @since 7/16/06
  945. * @version 7/16/06
  946. */
  947. function getURLVars($url) {
  948. $url = substr($url, strpos($url,'?')+1);
  949. $url = explode("&",$url);
  950. $url2 = array();
  951. foreach ($url as $var){
  952. $url2[substr($var,0,strpos($var,'='))] =
  953. substr($var,strpos($var,'=')+1);
  954. }
  955. return $url2;
  956. }
  957. /**
  958. * Inverse of the above.
  959. *
  960. * @author Ben Dodson
  961. * @version 11/11/04
  962. * @since 11/11/04
  963. */
  964. function unurlize($arr) {
  965. global $secure_urls;
  966. $GET = array();
  967. if (false !== stristr($_SERVER['PHP_SELF'],"mediabroadcast.php")) {
  968. if (isset($arr['image'])) {
  969. $DEC_FUNC = "jz_decode";
  970. } else {
  971. $DEC_FUNC = "jz_track_decode";
  972. }
  973. } else {
  974. $DEC_FUNC = "jz_decode";
  975. }
  976. //$jz_secure = "true";
  977. if ($secure_urls == "true" && !url_full_exception($arr)) {
  978. foreach ($arr as $key => $val) {
  979. // if jz_secure, jz_encode our keys and vars.
  980. if (!url_key_exception($key) && !is_array($key)) {
  981. $GET[$DEC_FUNC($key)] = $DEC_FUNC(stripSlashes($val));
  982. } else if (!is_array($key)) {
  983. $GET[$key] = stripSlashes($val);
  984. } else {
  985. $GET[$key] = $val;
  986. }
  987. }
  988. } else {
  989. foreach ($arr as $key => $val) {
  990. if (!is_array($key)) {
  991. $GET[$key] = stripSlashes($val);
  992. } else {
  993. $GET[$key] = $val;
  994. }
  995. }
  996. }
  997. return $GET;
  998. }
  999. /**
  1000. * Same as above for POST variables.
  1001. *
  1002. * @author Ben Dodson
  1003. * @version 12/16/04
  1004. * @ since 12/16/04
  1005. */
  1006. function unpostize($arr) {
  1007. $POST = array();
  1008. if (url_full_exception($arr)) {
  1009. foreach($arr as $key => $val) {
  1010. $POST[$key] = stripSlashes($val);
  1011. }
  1012. }
  1013. else {
  1014. foreach ($arr as $key => $val) {
  1015. if (is_array($val)) {
  1016. $arr1 = array();
  1017. for ($i = 0; $i < sizeof($val); $i++) {
  1018. $arr1[] = stripSlashes(jz_decode($val[$i]));
  1019. }
  1020. $POST[$key] = $arr1;
  1021. } else if (post_key_exception($key)) {
  1022. $POST[$key] = stripSlashes($val);
  1023. } else {
  1024. $POST[jz_decode($key)] = jz_decode(stripSlashes($val));
  1025. }
  1026. }
  1027. }
  1028. return $POST;
  1029. }
  1030. /**
  1031. * Checks to see if a key indicates our entire string should not be scrambled.
  1032. *
  1033. * @author Ben Dodson
  1034. * @version 11/11/04
  1035. * @since 11/11/04
  1036. */
  1037. function url_full_exception($arr) {
  1038. foreach ($arr as $key => $val) {
  1039. switch ($key) {
  1040. // if we see doSearch, don't unscramble/scramble since we
  1041. // came from searchbar
  1042. case "doSearch":
  1043. if (isset($_REQUEST['action']) && $_REQUEST['action'] != '' ) {
  1044. if ($_REQUEST['action'] != "search") {
  1045. die("Not allowed.");
  1046. }
  1047. }
  1048. return true;
  1049. break;
  1050. case "update_settings":
  1051. /* This will break if you try to update_settings
  1052. * outside of a popup.
  1053. */
  1054. if (isset($_REQUEST['action'])) {
  1055. if ($_REQUEST['action'] != "popup") {
  1056. die("Not allowed.");
  1057. }
  1058. }
  1059. /* Just so you know.
  1060. */
  1061. return true;
  1062. break;
  1063. }
  1064. }
  1065. return false;
  1066. }
  1067. /**
  1068. * Checks to see if our key/val string should be scrambled.
  1069. * Things like text inputs are not scrambled.
  1070. *
  1071. * @author Ben Dodson
  1072. * @version 11/11/04
  1073. * @since 11/11/04
  1074. */
  1075. function url_key_exception($key) {
  1076. global $jzSERVICES;
  1077. $a = $jzSERVICES->cmsGETVars();
  1078. if (isset($a[$key])) return true;
  1079. switch ($key) {
  1080. case "query":
  1081. case "search_query":
  1082. case "Artist": // decoy
  1083. case "Track": // decoy
  1084. case "Title": // decoy
  1085. case "op":
  1086. case "name":
  1087. case "frame":
  1088. case "jza":
  1089. case "view":
  1090. case "style":
  1091. case "user":
  1092. case "pass":
  1093. case "playlistname":
  1094. //case "file":
  1095. return true;
  1096. }
  1097. return false;
  1098. }
  1099. /** Same as above, but for POST variables.
  1100. *
  1101. * @author Ben Dodson
  1102. * @version 12/16/04
  1103. * @since 12/16/04
  1104. */
  1105. function post_key_exception($key) {
  1106. // Let's account for some partial matches
  1107. if (stristr($key,"plTrackPos-")
  1108. or stristr($key,"plTrackDel-")
  1109. or stristr($key,"edit_")
  1110. ){
  1111. return true;
  1112. }
  1113. switch ($key) {
  1114. case "update_postsettings":
  1115. case "jz_list":
  1116. case "query":
  1117. case "search_query":
  1118. case "field1":
  1119. case "field2":
  1120. case "field3":
  1121. case "field4":
  1122. case "field5":
  1123. case "remember":
  1124. case "siteNewsData":
  1125. case "jbvol":
  1126. case "jbplaywhere":
  1127. case "jbjumpto":
  1128. case "addplat":
  1129. case "jz_playlist":
  1130. case "playlistname":
  1131. case "updateTags":
  1132. case "reGenre":
  1133. case "reArtist":
  1134. case "reAlbum":
  1135. case "reTrack":
  1136. case "reNumber":
  1137. case "plType":
  1138. case "shareWithUser":
  1139. case "updatePlaylist":
  1140. case "deletePlaylist":
  1141. case "plToEdit":
  1142. case "randomize":
  1143. return true;
  1144. default:
  1145. return false;
  1146. }
  1147. }
  1148. /**
  1149. * Scrambles a string
  1150. *
  1151. * @author Ben Dodson
  1152. * @version 11/6/04
  1153. * @since 11/6/04
  1154. */
  1155. function jz_encode($string, $key = false) {
  1156. global $secure_urls,$security_key;
  1157. if ($secure_urls == "false"){
  1158. return $string;
  1159. }
  1160. // Complex scheme.
  1161. if ($key === false) {
  1162. $key = $security_key;
  1163. if (strlen($key) < 10) {
  1164. $key = "secrets are fun to keep (so let's keep them)";
  1165. }
  1166. }
  1167. $result = '';
  1168. for($i=1; $i<=strlen($string); $i++) {
  1169. $char = substr($string, $i-1, 1);
  1170. $keychar = substr($key, ($i % strlen($key))-1, 1);
  1171. $char = chr(ord($char)+ord($keychar));
  1172. $result .= $char;
  1173. }
  1174. return str_replace("+","JZPLUS",base64_encode($result));
  1175. }
  1176. /**
  1177. * Unscrambles a string.
  1178. *
  1179. * @author Ben Dodson
  1180. * @version 11/6/04
  1181. * @since 11/5/04
  1182. */
  1183. function jz_decode($string, $key = false) {
  1184. global $secure_urls,$security_key;
  1185. if ($secure_urls == "false"){
  1186. return $string;
  1187. }
  1188. // Complex scheme.
  1189. $string = base64_decode(str_replace("JZPLUS","+",$string));
  1190. if ($key === false) {
  1191. $key = $security_key;
  1192. if (strlen($key) < 10) {
  1193. $key = "secrets are fun to keep (so let's keep them)";
  1194. }
  1195. }
  1196. $result = '';
  1197. for ($i=1; $i <= strlen($string); $i++) {
  1198. $char = substr($string,$i-1,1);
  1199. $keychar = substr($key,($i % strlen($key))-1,1);
  1200. $char = chr(ord($char)-ord($keychar));
  1201. $result .= $char;
  1202. }
  1203. return $result;
  1204. }
  1205. /*
  1206. * Encodes the URL for a track (since players break easily)
  1207. *
  1208. * @author Ben Dodson
  1209. * @since 2/2/05
  1210. * @version 2/2/05
  1211. *
  1212. **/
  1213. function jz_track_encode($string) {
  1214. $ret = "";
  1215. return strrev($string);
  1216. for ($i = 0; $i < strlen($string); $i++) {
  1217. $ret .= chr(ord($string[$i])+4);
  1218. }
  1219. return strrev($ret);
  1220. }
  1221. /*
  1222. * Decodes the URL for a track (since players break easily)
  1223. *
  1224. * @author Ben Dodson
  1225. * @since 2/2/05
  1226. * @version 2/2/05
  1227. *
  1228. **/
  1229. function jz_track_decode($string) {
  1230. $ret = "";
  1231. return strrev($string);
  1232. $string = strrev($string);
  1233. for ($i = 0; $i < strlen($string); $i++) {
  1234. $ret .= chr(ord($string[$i])-4);
  1235. }
  1236. return $ret;
  1237. }
  1238. function getTrackIdFromURL($url) {
  1239. global $secure_urls;
  1240. if ($secure_urls == "true") {
  1241. $decoded_label = jz_track_encode('jz_path');
  1242. } else {
  1243. $decoded_label = 'jz_path';
  1244. }
  1245. $args = substr($url,strpos($url,'?')+1);
  1246. $args = explode('&',$args);
  1247. foreach ($args as $arg) {
  1248. $parts = explode('=',$arg);
  1249. if ($parts[0] == $decoded_label) {
  1250. if ($secure_urls == "true") {
  1251. return jz_track_decode($parts[1]);
  1252. } else {
  1253. return $parts[1];
  1254. }
  1255. }
  1256. }
  1257. return false;
  1258. }
  1259. /**
  1260. * Scrambles a string for the cookie
  1261. *
  1262. * @author Ben Dodson
  1263. * @version 1/16/05
  1264. * @since 11/23/04
  1265. */
  1266. function jz_cookie_encode($string) {
  1267. return jz_encode($string, "this is a secret key that will be moved and improved.");
  1268. }
  1269. /**
  1270. * Unscrambles a stringf or the cookie
  1271. *
  1272. * @author Ben Dodson
  1273. * @version 1/16/05
  1274. * @since 11/23/04
  1275. */
  1276. function jz_cookie_decode($string) {
  1277. return jz_decode($string, "this is a secret key that will be moved and improved.");
  1278. }
  1279. function setThisPage() {
  1280. global $link_root, $cms_type, $cms_mode, $fe;
  1281. if (defined('JZ_URL_OVERRIDE')) {
  1282. $link = JZ_URL_OVERRIDE . '?';
  1283. } else if ($cms_mode === false || $cms_mode == "false" || $link_root == "") {
  1284. //$a = explode("/",$_SERVER['PHP_SELF']);
  1285. //$link = $a[sizeof($a)-1] . '?';
  1286. $link = $_SERVER['PHP_SELF'];
  1287. $link = substr($link,0,strrpos($link,'/')).'/index.php?';
  1288. $link = str_replace("popup.php","index.php",$link);
  1289. }
  1290. else {
  1291. //
  1292. $link = $link_root;
  1293. }
  1294. // check for things that need to be added:
  1295. $this_page = $link;
  1296. // Add additional settings to our URL:
  1297. if (isset($_GET['set_frontend'])) {
  1298. $this_page .= urlencode(jz_encode("frontend")) . "=" . urlencode(jz_encode($_GET['set_frontend'])) . "&";
  1299. $_GET['frontend'] = $_GET['set_frontend'];
  1300. } else if (isset($_POST['set_frontend'])) {
  1301. $this_page .= urlencode(jz_encode("frontend")) . "=" . urlencode(jz_encode($_POST['set_frontend'])) . "&";
  1302. $_GET['frontend'] = $_POST['set_frontend'];
  1303. } else if (isset($_GET['view'])) {
  1304. $this_page .= urlencode(jz_encode("frontend")) . "=" . urlencode(jz_encode($_GET['view'])) . "&";
  1305. $_GET['frontend'] = $_GET['view'];
  1306. } else if (isset($_GET['frontend'])) {
  1307. $this_page .= urlencode(jz_encode("frontend")) . "=" . urlencode(jz_encode($_GET['frontend'])) . "&";
  1308. }
  1309. if (isset($_GET['set_theme'])) {
  1310. $this_page .= urlencode(jz_encode("theme")) . "=" . urlencode(jz_encode($_GET['set_theme'])) . "&";
  1311. $_GET['theme'] = $_GET['set_theme'];
  1312. } else if (isset($_POST['set_theme'])) {
  1313. $this_page .= urlencode(jz_encode("theme")) . "=" . urlencode(jz_encode($_POST['set_theme'])) . "&";
  1314. $_GET['theme'] = $_POST['set_theme'];
  1315. } else if (isset($_GET['style'])) {
  1316. $this_page .= urlencode(jz_encode("theme")) . "=" . urlencode(jz_encode($_GET['style'])) . "&";
  1317. $_GET['theme'] = $_GET['style'];
  1318. } else if (isset($_GET['theme'])) {
  1319. $this_page .= urlencode(jz_encode("theme")) . "=" . urlencode(jz_encode($_GET['theme'])) . "&";
  1320. }
  1321. if (isset($_GET['set_language'])) {
  1322. $this_page .= urlencode(jz_encode("language")) . "=" . urlencode(jz_encode($_GET['set_language'])) . "&";
  1323. $_GET['language'] = $_GET['set_language'];
  1324. } else if (isset($_POST['set_language'])) {
  1325. $this_page .= urlencode(jz_encode("language")) . "=" . urlencode(jz_encode($_POST['set_language'])) . "&";
  1326. $_GET['language'] = $_POST['set_language'];
  1327. } else if (isset($_GET['language'])) {
  1328. $this_page .= urlencode(jz_encode("language")) . "=" . urlencode(jz_encode($_GET['language'])) . "&";
  1329. }
  1330. return $this_page;
  1331. }
  1332. /**
  1333. * Replace ob_flush()
  1334. *
  1335. * @category PHP
  1336. * @package PHP_Compat
  1337. * @link http://php.net/function.ob_flush
  1338. * @author Aidan Lister <aidan@php.net>
  1339. * @author Thiemo M�ttig (http://maettig.com/)
  1340. * @version $Revision$
  1341. * @since PHP 4.2.0
  1342. * @require PHP 4.0.1 (trigger_error)
  1343. */
  1344. if (!function_exists('ob_flush'))
  1345. {
  1346. function ob_flush()
  1347. {
  1348. if (@ob_end_flush()) {
  1349. return ob_start();
  1350. }
  1351. trigger_error("ob_flush() Failed to flush buffer. No buffer to flush.", E_USER_NOTICE);
  1352. return false;
  1353. }
  1354. }
  1355. /**
  1356. * Replace file_get_contents()
  1357. *
  1358. * @category PHP
  1359. * @package PHP_Compat
  1360. * @link http://php.net/function.file_get_contents
  1361. * @author Aidan Lister <aidan@php.net>
  1362. * @version $Revision$
  1363. * @internal resource_context is not supported
  1364. * @since PHP 5
  1365. * @require PHP 4.0.1 (trigger_error)
  1366. */
  1367. if (!function_exists('file_get_contents'))
  1368. {
  1369. function file_get_contents($filename, $incpath = false, $resource_context = null)
  1370. {
  1371. if (false === $fh = fopen($filename, 'rb', $incpath)) {
  1372. trigger_error('file_get_contents() failed to open stream: No such file or directory', E_USER_WARNING);
  1373. return false;
  1374. }
  1375. clearstatcache();
  1376. if ($fsize = filesize($filename)) {
  1377. $data = fread($fh, $fsize);
  1378. } else {
  1379. while (!feof($fh)) {
  1380. $data .= fread($fh, 8192);
  1381. }
  1382. }
  1383. fclose($fh);
  1384. return $data;
  1385. }
  1386. }
  1387. // microtime for PHP 4
  1388. function microtime_float()
  1389. {
  1390. list($usec, $sec) = explode(" ", microtime());
  1391. return ((float)$usec + (float)$sec);
  1392. }
  1393. /**
  1394. * Returns a list of available languages
  1395. *
  1396. * @author Ben Dodson, Ross Carlson
  1397. * @since 11/5/05
  1398. *
  1399. */
  1400. function getLanguageList() {
  1401. global $include_path;
  1402. $lang_dir = $include_path. "lang";
  1403. $retArray = readDirInfo($lang_dir,"file");
  1404. sort($retArray);
  1405. $languages = array();
  1406. for ($c=0; $c < count($retArray); $c++){
  1407. $entry = $retArray[$c];
  1408. // Let's make sure this isn't the local directory we're looking at
  1409. if ($entry == "." || $entry == ".." || $entry == "master.php") { continue;}
  1410. if (!stristr($entry,"-setup") and !stristr($entry,".html")){
  1411. if (strrpos($entry,'-') !== false) {
  1412. $languages[substr($entry,0,strrpos($entry,'-'))] = true;
  1413. } else {
  1414. $languages[$entry] = true;
  1415. }
  1416. }
  1417. }
  1418. return array_keys($languages);
  1419. }
  1420. /**
  1421. * Translates a word into a native language
  1422. *
  1423. *
  1424. * @author Ross Carlson
  1425. * @author Ben Dodson
  1426. * @version 3.22.05
  1427. * @since 3.22.05
  1428. * @param string $word The word to translate
  1429. * @param $term1, $term2, ....: replace the key %s in $word.
  1430. */
  1431. function word($word){
  1432. global $words, $jz_lang_file, $include_path;
  1433. // Let's hash that word
  1434. $hash = md5($word);
  1435. // Now let's make sure there is a translation for this
  1436. if (isset($words[$hash])){
  1437. $ret = $words[$hash];
  1438. } else {
  1439. // Ok, it didn't so let's log it and return what they put it
  1440. $fileName = $include_path. "temp/jinzora-words-". $jz_lang_file. ".log";
  1441. if (!is_file($fileName)){touch($fileName);}
  1442. $data = file_get_contents($fileName);
  1443. if (!stristr($data,$word)){
  1444. $handle = @fopen($fileName, "a");
  1445. $data = "$". "words['". $hash. "'] = ". '"'. $word. '";'. "\n";
  1446. @fwrite($handle,$data);
  1447. @fclose($handle);
  1448. }
  1449. // Now let's set up for returning:
  1450. $ret = $word;
  1451. }
  1452. // Replace %s as needed:
  1453. $i = 1;
  1454. while (($index = strpos($ret,'%s')) !== false) {
  1455. $ret = substr($ret,0,$index) . func_get_arg($i++) . substr($ret,$index+2);
  1456. }
  1457. return $ret;
  1458. }
  1459. // This function will see if there is a thumbnail for the corresponding item
  1460. // Added 3.23.04 by Ross Carlson
  1461. // It returns false or the name of the file
  1462. function searchThumbnail($searchFile){
  1463. global $ext_graphic, $web_root;
  1464. $typeArray = explode("|",$ext_graphic);
  1465. $thumb_file = "";
  1466. $fileExt = returnFileExt($searchFile);
  1467. for ($e=0; $e < count($typeArray); $e++){
  1468. $thumbFileName = str_replace(".".$fileExt,".thumb.". $typeArray[$e],$searchFile);
  1469. if (is_file($thumbFileName)){
  1470. $thumb_file = str_replace("%2F","/",rawurlencode(str_replace($web_root,"",$thumbFileName)));
  1471. }
  1472. }
  1473. // Now let's return it
  1474. if ($thumb_file <> ""){
  1475. return $thumb_file;
  1476. } else {
  1477. return false;
  1478. }
  1479. }
  1480. /**
  1481. * Display the opening tag for a table
  1482. *
  1483. * @author Ross Carlson
  1484. * @version 04/28/04
  1485. * @since 04/28/04
  1486. * @param integer $width Width of the table in percent
  1487. * @param integer $cellpadding cellpadding for the table
  1488. * @param string $class the style sheet class for the table
  1489. */
  1490. function jzTableOpen($width = "100", $cellpadding = "5", $class = "jz_track_table", $widthType = "%"){
  1491. echo '<table class="'. $class. '" width="'. $width. $widthType. '" cellpadding="'. $cellpadding. '" cellspacing="0" border="0">'. "\n";
  1492. }
  1493. /**
  1494. * Display the closing tag for a table
  1495. *
  1496. * @author Ross Carlson
  1497. * @version 04/28/04
  1498. * @since 04/28/04
  1499. */
  1500. function jzTableClose(){
  1501. echo '</table>'. "\n";
  1502. }
  1503. /**
  1504. * open a Table Row
  1505. *
  1506. * @author Ross Carlson
  1507. * @version 04/28/04
  1508. * @since 04/28/04
  1509. * @param string $class the style sheet class for the table
  1510. */
  1511. function jzTROpen($class = ""){
  1512. echo ' <tr class="'. $class. '">'. "\n";
  1513. }
  1514. /**
  1515. * close a Table Row
  1516. *
  1517. * @author Ross Carlson
  1518. * @version 04/28/04
  1519. * @since 04/28/04
  1520. */
  1521. function jzTRClose(){
  1522. echo ' </tr>'. "\n";
  1523. }
  1524. /**
  1525. * Display the opening tag for a table detail
  1526. *
  1527. * @author Ross Carlson
  1528. * @version 04/28/04
  1529. * @since 04/28/04
  1530. * @param integer $width Width of the table in percent
  1531. * @param string $align alignment for the cell
  1532. * @param string $valign verticle alignment for the cell
  1533. * @param string $class the style sheet class for the table
  1534. * @param integer $colspan how many colums should this cell span
  1535. */
  1536. function jzTDOpen($width = "100", $align = "left", $valign = "top", $class = "", $colspan = "", $widthType = "%"){
  1537. echo ' <td width="'. $width. $widthType. '" align="'. $align. '" valign="'. $valign. '" class="'. $class. '" ';
  1538. if ($colspan <> "0"){
  1539. echo 'colspan="'. $colspan. '"';
  1540. }
  1541. echo '>'. "\n";
  1542. }
  1543. /**
  1544. * Display the opening tag for a table detail
  1545. *
  1546. * @author Ross Carlson
  1547. * @version 04/28/04
  1548. * @since 04/28/04
  1549. */
  1550. function jzTDClose(){
  1551. echo ' </td>'. "\n";
  1552. }
  1553. /**
  1554. * Display an A HREF tag
  1555. *
  1556. * @author Ross Carlson
  1557. * @version 04/28/04
  1558. * @since 04/28/04
  1559. */
  1560. function jzHREF($href, $target = "", $class = "", $onclick = "", $item, $title = ""){
  1561. $retVar = '<a ';
  1562. if ($title <> ""){ $retVar .= 'title="'. $title. '" '; }
  1563. if ($class <> ""){ $retVar .= 'class="'. $class. '" '; }
  1564. if ($target <> ""){ $retVar .= 'target="'. $target. '" '; }
  1565. if ($onclick <> ""){ $retVar .= 'onclick="'. $onclick. '" '; }
  1566. $retVar .= 'href="'. $href. '" >'. $item. '</a>';
  1567. // Now let's display the link
  1568. echo $retVar;
  1569. }
  1570. /**
  1571. * returns the truncated name of an item
  1572. *
  1573. * @author Ross Carlson
  1574. * @version 05/03/04
  1575. * @since 05/03/04
  1576. * @param string $item the item to truncate
  1577. * @param string $length how long should it be?
  1578. */
  1579. function returnItemShortName($item,$length){
  1580. if (strlen($item) > $length + 3){
  1581. return substr($item,0,$length). "...";
  1582. } else {
  1583. return $item;
  1584. }
  1585. }
  1586. /**
  1587. * Hightlights (bolds) part of a string to emphasis it
  1588. *
  1589. * @author Ross Carlson (from www.php.net)
  1590. * @version 01/14/05
  1591. * @since 01/14/05
  1592. * @param string $x The Haystack
  1593. * @param string $var The needle
  1594. * @return string guessed mime type for thie filename
  1595. */
  1596. function highlight($x,$var) {//$x is the string, $var is the text to be highlighted
  1597. if ($var != "") {
  1598. $xtemp = "";
  1599. $i=0;
  1600. while($i<strlen($x)){
  1601. if((($i + strlen($var)) <= strlen($x)) && (strcasecmp($var, substr($x, $i, strlen($var))) == 0)) {
  1602. //this version bolds the text. you can replace the html tags with whatever you like.
  1603. $xtemp .= "<b>" . substr($x, $i , strlen($var)) . "</b>";
  1604. $i += strlen($var);
  1605. }
  1606. else {
  1607. $xtemp .= $x{$i};
  1608. $i++;
  1609. }
  1610. }
  1611. $x = $xtemp;
  1612. }
  1613. return $x;
  1614. }
  1615. /**
  1616. * for php < 4.3.0
  1617. *
  1618. * @author Laurent Perrin
  1619. * @version 02/06/04
  1620. * @since 02/06/04
  1621. * @param string $filename file name
  1622. * @return string guessed mime type for thie filename
  1623. */
  1624. if (!function_exists("mime_content_type")) {
  1625. function mime_content_type($filename) {
  1626. switch(strrchr($filename,'.')){
  1627. case '.mp3':
  1628. case '.mp2':
  1629. case '.mp1':
  1630. return 'audio/mpeg';
  1631. case '.wma':
  1632. return 'audio/wma';
  1633. case '.wav':
  1634. return 'audio/x-wav';
  1635. case '.avi':
  1636. return 'video/x-msvideo';
  1637. case '.qt':
  1638. case '.mov':
  1639. return 'video/quicktime';
  1640. case '.mpe':
  1641. case '.mpg':
  1642. case '.mpeg':
  1643. return 'video/mpeg';;
  1644. } // switch()
  1645. return '';
  1646. }
  1647. }
  1648. /**
  1649. * for php < 4.3.0
  1650. *
  1651. * @author Laurent Perrin
  1652. * @version 02/15/04
  1653. * @since 02/06/04
  1654. * @param string $filename filename
  1655. * @param integer $use_include_path use include path
  1656. * @return string file contents
  1657. */
  1658. if (!function_exists("file_get_contents")) {
  1659. function file_get_contents($filename, $use_include_path = 0) {
  1660. $data = ''; // just to be safe. Dunno, if this is really needed
  1661. $file = @fopen($filename, "rb", $use_include_path);
  1662. if ($file) {
  1663. $data = fread($file, filesize($filename));
  1664. fclose($file);
  1665. }
  1666. return $data;
  1667. }
  1668. }
  1669. /**
  1670. * Updates the track counter for something when it's played
  1671. *
  1672. * @author Ross Carlson
  1673. * @version 07/26/04
  1674. * @version 07/26/04
  1675. * @param string $data the file name to process
  1676. * @return string the file extension (without the .)
  1677. */
  1678. function returnFileExt($data){
  1679. $fileInfo = pathinfo($data);
  1680. if (isset($fileInfo["extension"])){
  1681. return $fileInfo["extension"];
  1682. } else {
  1683. return "";
  1684. }
  1685. }
  1686. /**
  1687. * Handles array sorting for us
  1688. *
  1689. * @author Ross Carlson
  1690. */
  1691. function track_cmp($a, $b){
  1692. if ($a == $b) {
  1693. return 0;
  1694. }
  1695. return ($a > $b) ? -1 : 1;
  1696. }
  1697. /**
  1698. * Returns the difference between 2 microtime stamps
  1699. *
  1700. * @author Ross Carlson
  1701. * @version 06/17/04
  1702. * @since 06/17/04
  1703. * @param string $value What needs to be added to the list
  1704. */
  1705. function microtime_diff($a, $b) {
  1706. list($a_dec, $a_sec) = explode(" ", $a);
  1707. list($b_dec, $b_sec) = explode(" ", $b);
  1708. return $b_sec - $a_sec + $b_dec - $a_dec;
  1709. }
  1710. /**
  1711. * Sets a session variable with the URL of the current page
  1712. *
  1713. * @author Ross Carlson
  1714. * @version 05/04/04
  1715. * @since 05/04/04
  1716. * @returns Session variable $_SESSION['prev_page']
  1717. */
  1718. function setPreviousPage(){
  1719. // Now let's set the session variable for later
  1720. $_SESSION['prev_page'] = @$_SERVER['REQUEST_URI'];
  1721. // Let's make sure it got set right and if not fix it
  1722. if ($_SESSION['prev_page'] == ""){
  1723. $_SESSION['prev_page'] = $_SERVER["URL"]. "?". $_SERVER["QUERY_STRING"];
  1724. }
  1725. }
  1726. /**
  1727. * Returns how wide the columns on the genre/artist page should be
  1728. *
  1729. * @author Ross Carlson
  1730. * @version 05/03/04
  1731. * @since 05/03/04
  1732. * @param integer $items Number of items that are to be displayed
  1733. * @returns integer returns width
  1734. */
  1735. function returnColWidth($items){
  1736. global $cols_in_genre;
  1737. // Let's find out how wide the colums will be in percent
  1738. $col_width = 100 / $cols_in_genre;
  1739. // Now let's make sure we don't divide by zero (Thanks flavio!)
  1740. if ($items < 1){ $items = 1; }
  1741. // Let's make sure we have enough items to fill the number of colums that we wanted (say we wanted 3 cols but we only have 1 item)
  1742. if ($items < $cols_in_genre){
  1743. // Ok, let's make this a better number
  1744. $col_width = 100 / $items;
  1745. }
  1746. return $col_width;
  1747. }
  1748. /**
  1749. * Convert MB to KB
  1750. *
  1751. * @author Ross Carlson
  1752. * @version 06/28/04
  1753. * @since 06/28/04
  1754. * @param integer $size Size of the item in MB (example 12.27)
  1755. * @returns returns the size in kb (example 12564.48)
  1756. */
  1757. function convertMBtoKB($size){
  1758. return $size * 1024;
  1759. }
  1760. /**
  1761. * Convert MB to KB
  1762. *
  1763. * @author Ross Carlson
  1764. * @version 06/28/04
  1765. * @since 06/28/04
  1766. * @param integer $size Size of the item in MB (example 12.27)
  1767. * @returns returns the size in kb (example 12564.48)
  1768. */
  1769. function convertKBtoMB($size){
  1770. return round($size / 1024,2);
  1771. }
  1772. /**
  1773. * Converts a long numeric value to time, 69 = 1:09:00
  1774. *
  1775. * @author Ross Carlson
  1776. * @version 04/29/04
  1777. * @since 04/29/04
  1778. * @param integer $time Amount of time in minutes
  1779. * @returns returns the time in days:hours:minutes:seconds
  1780. */
  1781. function formatTime($time){
  1782. // Let's get the days
  1783. $days=0;
  1784. while($time > (24*60*60)){
  1785. $time = $time - (24*60*60);
  1786. $days++;
  1787. }
  1788. // Now let's get hours
  1789. $hours=0;
  1790. while($time > (60*60)){
  1791. $time = $time - (60*60);
  1792. $hours++;
  1793. }
  1794. // Now let's get minutes
  1795. $mins=0;
  1796. while($time > (60)){
  1797. $time = $time - (60);
  1798. $mins++;
  1799. }
  1800. if ($time < 10){$time = "0". $time;}
  1801. return $days. ":". $hours. ":". $mins. ":". $time;
  1802. }
  1803. /**
  1804. * Convert minutes to seconds
  1805. *
  1806. * @author Ross Carlson
  1807. * @version 04/29/04
  1808. * @since 04/29/04
  1809. * @param integer $seconds Number of seconds to convert to minutes
  1810. * @returns returns the time in minutes:seconds
  1811. */
  1812. function convertMinsSecs($minutes){
  1813. // Let's make sure it was mins:sec
  1814. if (!stristr($minutes,":")){ $minutes = $minutes. ":"; }
  1815. // Now let's split it by the :
  1816. $minArray = explode(":",$minutes);
  1817. // Now let's create the time
  1818. return ($minArray[0] * 60) + $minArray[1];
  1819. }
  1820. /**
  1821. * Convert seconds to minutes
  1822. *
  1823. * @author Ross Carlson
  1824. * @version 04/29/04
  1825. * @since 04/29/04
  1826. * @param integer $seconds Number of seconds to convert to minutes
  1827. * @returns returns the time in minutes:seconds
  1828. */
  1829. function convertSecMins($seconds){
  1830. // First let's round it off
  1831. $seconds = round($seconds);
  1832. // Now let's loop through subtracking 60 each time
  1833. $ctr=0;
  1834. while ($seconds >= 60){
  1835. $seconds = $seconds - 60;
  1836. $ctr++;
  1837. }
  1838. if ($seconds < 10){
  1839. $seconds = "0". $seconds;
  1840. }
  1841. return $ctr. ":". $seconds;
  1842. }
  1843. // This function will take a filename and return the formated name of the XML (or data) file
  1844. // Added 4.6.04 by Ross Carlson
  1845. // Returns the name of the file, formated, without the leading path (so just as it was sent)
  1846. function returnFormatedFilename($fileName){
  1847. //Ok, let's set it
  1848. $fileName = str_replace("/","---",$fileName);
  1849. // Now let's make sure we don't have any files beginning with ---
  1850. while (substr($fileName,0,3) == "---"){
  1851. $fileName = substr($fileName,3,strlen($fileName));
  1852. }
  1853. return $fileName;
  1854. }
  1855. // This function forces the browser to display output
  1856. function flushDisplay() {
  1857. global $cms_type;
  1858. if ($cms_type <> "mambo" && $cms_type <> "xoops"){
  1859. @ob_flush();
  1860. @flush();
  1861. }
  1862. print str_repeat(" ", 4096); // force a flush
  1863. }
  1864. // This function just returns the directories for us
  1865. function readJustDirs($dirName, &$readCtr, &$mainArray){
  1866. global $web_root, $root_dir, $media_dir;
  1867. // Let's up the max_execution_time
  1868. ini_set('max_execution_time','600');
  1869. // Let's look at the directory we are in
  1870. if (is_dir($dirName)){
  1871. $d = dir($dirName);
  1872. while($entry = $d->read()) {
  1873. // Let's make sure we are seeing real directories
  1874. if ($entry == "." || $entry == "..") { continue;}
  1875. // Now let's read this IF it's just a folder
  1876. if (is_dir($dirName. "/". $entry)){
  1877. $mainArray[$readCtr] = str_replace($web_root. $root_dir. $media_dir. "/", "",$dirName. "/". $entry);
  1878. $readCtr++;
  1879. readJustDirs($dirName. "/". $entry, $readCtr, $mainArray);
  1880. }
  1881. }
  1882. // Now let's close the directory
  1883. $d->close();
  1884. // Now let's sort that array
  1885. @sort($mainArray);
  1886. }
  1887. // Ok, let's return the data
  1888. return $mainArray;
  1889. }
  1890. // This function takes a directory and get's all sub directories and files and puts them in an array
  1891. function readAllDirs($dirName, &$readCtr, &$mainArray, $searchExt = "false", $displayProgress = "false"){
  1892. global $audio_types, $video_types;
  1893. // Let's up the max_execution_time
  1894. ini_set('max_execution_time','600');
  1895. // Let's look at the directory we are in
  1896. if (is_dir($dirName)){
  1897. $d = dir($dirName);
  1898. while($entry = $d->read()) {
  1899. // Let's make sure we are seeing real directories
  1900. if ($entry == "." || $entry == "..") { continue;}
  1901. // Now let's see if we are looking at a directory or not
  1902. if (filetype($dirName. "/". $entry) <> "file"){
  1903. // Ok, that was a dir, so let's move to the next directory down
  1904. readAllDirs($dirName. "/". $entry, $readCtr, $mainArray, $searchExt, $displayProgress);
  1905. } else {
  1906. // Let's see if they wanted status
  1907. if ($displayProgress == "true"){
  1908. if ($readCtr % 50 == 0){ echo '.'; flushDisplay();}
  1909. }
  1910. // Let's see if we want to search a specfic extension or not
  1911. if ($searchExt == "false"){
  1912. // Ok, we found files, let's make sure they are audio or video files
  1913. if (preg_match("/\.($audio_types)$/i", $entry) or preg_match("/\.($video_types)$/i", $entry) ) {
  1914. $mainArray[$readCtr] = $dirName. "/". $entry;
  1915. $readCtr++;
  1916. }
  1917. } else {
  1918. if (stristr($entry, $searchExt) or $searchExt == "true"){
  1919. $mainArray[$readCtr] = $dirName. "/". $entry;
  1920. $readCtr++;
  1921. }
  1922. }
  1923. }
  1924. }
  1925. // Now let's close the directory
  1926. $d->close();
  1927. // Now let's sort that array
  1928. @sort($mainArray);
  1929. }
  1930. // Ok, let's return the data
  1931. return $mainArray;
  1932. }
  1933. function readAllDirs2($dirName, &$readCtr){
  1934. global $audio_types, $video_types;
  1935. // Let's up the max_execution_time
  1936. ini_set('max_execution_time','6000');
  1937. // Let's look at the directory we are in
  1938. if (@is_dir($dirName)){
  1939. $d = @dir($dirName);
  1940. if (@is_object($d)){
  1941. while($entry = $d->read()) {
  1942. // Let's make sure we are seeing real directories
  1943. if ($entry == "." || $entry == "..") { continue;}
  1944. if ($readCtr % 100 == 0){
  1945. ?>
  1946. <script language="javascript">
  1947. fc.innerHTML = '<b><?php echo word("%s files analyzed.",$readCtr); ?></b>';
  1948. -->
  1949. </SCRIPT>
  1950. <?php
  1951. @flush(); @ob_flush();
  1952. }
  1953. // Now let's see if we are looking at a directory or not
  1954. if (filetype($dirName. "/". $entry) <> "file"){
  1955. // Ok, that was a dir, so let's move to the next directory down
  1956. readAllDirs2($dirName. "/". $entry, $readCtr);
  1957. } else {
  1958. if (preg_match("/\.($audio_types|$video_types)$/i", $entry)){
  1959. $readCtr++;
  1960. $_SESSION['jz_full_counter']++;
  1961. }
  1962. }
  1963. }
  1964. // Now let's close the directory
  1965. $d->close();
  1966. }
  1967. }
  1968. }
  1969. // This function makes sure that the variable is TOTALLY clean of slashes
  1970. function jzstripslashes($variable){
  1971. // Lets loop through until the variable is clean
  1972. while (stristr($variable,"\\") <> ""){
  1973. $variable = stripslashes($variable);
  1974. }
  1975. while (stristr($variable,"//") <> ""){
  1976. $variable = str_replace("//","/",($variable));
  1977. }
  1978. // Now let's send the clean variable back
  1979. return $variable;
  1980. }
  1981. // This function reads the directory specifed and returns the results into a sorted array */
  1982. function readDirInfo($dirName, $type){
  1983. // Let's up the max_execution_time
  1984. ini_set('max_execution_time','600');
  1985. $retArray = array();
  1986. // First let's make sure this is really a dir...
  1987. if (is_dir($dirName)){
  1988. $d = dir($dirName);
  1989. while($entry = $d->read()) {
  1990. // Let's make sure this isn't the local directory we're looking at
  1991. if ($entry == "." || $entry == ".." || $entry == "CVS") { continue;}
  1992. // Let's see if they wanted to look for a directory or a file and add that to the array
  1993. if ($type == "dir" and (filetype($dirName. "/". $entry) == "dir" or filetype($dirName. "/". $entry) == "link")){
  1994. $retArray[] = $entry;
  1995. }
  1996. if ($type == "file" and stristr($entry,".") <> "" and $entry <> ""){
  1997. $retArray[] = $entry;
  1998. }
  1999. }
  2000. $d->close();
  2001. // Let's make sure we found something, and if we didn't let's not sort an empty array
  2002. if (count($retArray) <> 0){
  2003. sort($retArray);
  2004. }
  2005. }
  2006. /* Now let's return the array to them */
  2007. return $retArray;
  2008. }
  2009. function userAuthenticate($username){
  2010. global $this_site, $web_root, $root_dir, $media_dir, $cms_user_access, $default_access, $include_path, $jzUSER;
  2011. // Now let's authenticate this user
  2012. $jzUSER = new jzUser();
  2013. if ($username == "anonymous") {
  2014. $username = NOBODY;
  2015. }
  2016. return $jzUSER->login($username, "cms-user", false);
  2017. }
  2018. function check_for_numerics($str) {
  2019. for ($i = 0; $i < strlen($str); $i++) {
  2020. if (is_numeric($str[$i]))
  2021. {
  2022. return (boolean) TRUE;
  2023. }
  2024. }
  2025. }
  2026. // This function will write out error messages to the log files...
  2027. function writeLogData($logName, $data){
  2028. global $include_path, $enable_logging, $log_max_size_kb, $jzUSER;
  2029. if ($enable_logging == "false"){return;}
  2030. // Let's see what file they wanted to open and open it up!
  2031. $fileName = $include_path. "temp/" . $logName . ".log";
  2032. if (!is_file($fileName)){
  2033. @touch($fileName);
  2034. }
  2035. // First let's make sure we're not over the max size
  2036. /*
  2037. if (filesize($fileName) > $log_max_size_kb){
  2038. // Ok, we need to truncate it
  2039. $data = file($fileName);
  2040. $truncate = (count($data) * .25);
  2041. for ($i=0; $i<count($data); $i++) {
  2042. if ($i > $truncate){
  2043. $nData[] = substr($data[$i],0,strlen($data[$i])-1);
  2044. }
  2045. }
  2046. $data = implode("\n",$nData);
  2047. //$handle = @fopen($fileName, "w");
  2048. //@fwrite($handle,$data);
  2049. //@fclose($handle);
  2050. }
  2051. */
  2052. if (isset($jzUSER)){
  2053. $user = $jzUSER->getName();
  2054. } else {
  2055. $user = "anon";
  2056. }
  2057. // Let's get the microseconds
  2058. $lTime = explode(" ",microtime());
  2059. $msec = substr($lTime[0],2,2);
  2060. $handle = @fopen($fileName, "a");
  2061. $data = date("n/j/y g:i:s",time()). ".". $msec. ", user:". $user. ", ". $data. "\n";
  2062. @fwrite($handle,$data);
  2063. @fclose($handle);
  2064. }
  2065. // This function returns the installed GD version
  2066. function gd_version() {
  2067. static $gd_version_number = null;
  2068. if ($gd_version_number === null) {
  2069. ob_start();
  2070. phpinfo(INFO_MODULES);
  2071. $module_info = ob_get_contents();
  2072. ob_end_clean();
  2073. if (preg_match("/\bgd\s+version\b[^\d\n\r]+?([\d\.]+)/i",$module_info,$matches)) {
  2074. $gd_version_number = $matches[1];
  2075. } else {
  2076. $gd_version_number = 0;
  2077. }
  2078. }
  2079. return $gd_version_number;
  2080. }
  2081. /* This function will resize the JPEG's we pass into the site for us and produce PNG's */
  2082. function resizeImage($source_image, $destination_image, $destination_width, $destination_height){
  2083. global $keep_porportions;
  2084. // First we need to see if GD is installed or not...
  2085. if (gd_version() == 0){
  2086. // Ok, no GD, let's write that to the log...
  2087. writeLogData('error','Sorry, GD Libraries not found while trying to resize an image...');
  2088. return false;
  2089. }
  2090. /* get the picture and set the output picture */
  2091. $image = $source_image;
  2092. $new_image = $destination_image;
  2093. /* Let's grab the source image that was uploaded to work with it */
  2094. $src_img = @imagecreatefromjpeg($image);
  2095. if ($src_img <> ""){
  2096. /* Let's get the width and height of the source image */
  2097. $src_width = imagesx($src_img); $src_height = imagesy($src_img);
  2098. /* Let's set the width and height of the new image we'll create */
  2099. $dest_width = $destination_width; $dest_height = $destination_height;
  2100. /* Now if the picture isn't a standard resolution (like 640x480) we
  2101. need to find out what the new image size will be by figuring
  2102. out which of the two numbers is higher and using that as the scale */
  2103. // First let's make sure they wanted to keep the porportions or not
  2104. if ($keep_porportions == "true"){
  2105. if ($src_width > $src_height){
  2106. /* ok so the width is the bigger number so the width doesn't change
  2107. We need to figure out the percent of change by dividing the source width
  2108. by the dest width */
  2109. $scale = $src_width / $destination_width;
  2110. $dest_height = $src_height / $scale;
  2111. } else {
  2112. /* ok so the width is the bigger number so the width doesn't change
  2113. We need to figure out the percent of change by dividing the source width
  2114. by the dest width */
  2115. $scale = $src_height / $destination_height;
  2116. $dest_width = $src_width / $scale;
  2117. }
  2118. } else {
  2119. $dest_height = $destination_height;
  2120. $dest_width = $destination_width;
  2121. }
  2122. /* Now let's create our destination image with our new height/width */
  2123. if (gd_version() >= 2) {
  2124. $dest_img = imageCreateTrueColor($dest_width, $dest_height);
  2125. } else {
  2126. $dest_img = imageCreate($dest_width, $dest_height);
  2127. }
  2128. /* Now let's copy the data from the old picture to the new one witht the new settings */
  2129. if (gd_version() >= 2) {
  2130. imageCopyResampled($dest_img, $src_img, 0, 0, 0 ,0, $dest_width, $dest_height, $src_width, $src_height);
  2131. } else {
  2132. imageCopyResized($dest_img, $src_img, 0, 0, 0 ,0, $dest_width, $dest_height, $src_width, $src_height);
  2133. }
  2134. /* Now let's create our new image */
  2135. @imagejpeg($dest_img, $new_image);
  2136. /* Now let's clean up all our temp images */
  2137. imagedestroy($src_img);
  2138. imagedestroy($dest_img);
  2139. return true;
  2140. } else {
  2141. return false;
  2142. }
  2143. }
  2144. function createBlankImage($image, $font, $text, $color, $shadow, $drop, $maxwidth, $alignment, $valign, $padding="5"){
  2145. global $web_root, $root_dir;
  2146. // First we need to see if GD is installed or not...
  2147. if (gd_version() == 0){
  2148. // Ok, no GD, let's write that to the log...
  2149. writeLogData('error','Sorry, GD Libraries not found!');
  2150. return false;
  2151. }
  2152. /* Now let's create our destination image with our new height/width */
  2153. $src_img = imagecreatefromjpeg($image);
  2154. if (gd_version() >= 2) {
  2155. $dest_img = imageCreateTrueColor($maxwidth, $maxwidth);
  2156. } else {
  2157. $dest_img = imageCreate($maxwidth, $maxwidth);
  2158. }
  2159. // decode color arguments and allocate colors
  2160. $color_args = explode (' ',$color);
  2161. $color = imagecolorallocate($dest_img, $color_args[0], $color_args[1], $color_args[2]);
  2162. $shadow_args = explode (' ',$shadow);
  2163. $shadow = imagecolorallocate($dest_img, $shadow_args[0], $shadow_args[1], $shadow_args[2]);
  2164. /* Let's get the width and height of the source image */
  2165. $src_width = imagesx($src_img); $src_height = imagesy($src_img);
  2166. /* Now let's copy the data from the old picture to the new one witht the new settings */
  2167. if (gd_version() >= 2) {
  2168. imageCopyResampled($dest_img, $src_img, 0, 0, 0 ,0, $maxwidth, $maxwidth, $src_width, $src_height);
  2169. } else {
  2170. imageCopyResized($dest_img, $src_img, 0, 0, 0 ,0, $maxwidth, $maxwidth, $src_width, $src_height);
  2171. }
  2172. /* Now let's clean up our temp image */
  2173. imagedestroy($src_img);
  2174. $fontwidth = ImageFontWidth($font);
  2175. $fontheight = ImageFontHeight($font);
  2176. $margin = floor($padding + $drop)/2; // So that shadow is not off image on right align & bottom valign
  2177. if ($maxwidth != NULL) {
  2178. $maxcharsperline = floor( ($maxwidth - ($margin * 2)) / $fontwidth);
  2179. $text = wordwrap($text, $maxcharsperline, "\n", 1);
  2180. }
  2181. $lines = explode("\n", $text);
  2182. switch($valign){
  2183. case "center":
  2184. $y = (imageSY($dest_img) - ($fontheight * sizeof($lines)))/2;
  2185. break;
  2186. case "bottom":
  2187. $y = imageSY($dest_img) - (($fontheight * sizeof($lines)) + $margin);
  2188. break;
  2189. default:
  2190. $y = $margin;
  2191. break;
  2192. }
  2193. switch($alignment){
  2194. case "right":
  2195. while (list($numl, $line) = each($lines)) {
  2196. ImageString($dest_img, $font, (imagesx($dest_img) - $fontwidth*strlen($line))-$margin+$drop, ($y+$drop), $line, $shadow);
  2197. ImageString($dest_img, $font, (imagesx($dest_img) - $fontwidth*strlen($line))-$margin, $y, $line, $color);
  2198. $y += $fontheight;
  2199. }
  2200. break;
  2201. case "center":
  2202. while (list($numl, $line) = each($lines)) {
  2203. ImageString($dest_img, $font, floor((imagesx($dest_img) - $fontwidth*strlen($line))/2)+$drop, ($y+$drop), $line, $shadow);
  2204. ImageString($dest_img, $font, floor((imagesx($dest_img) - $fontwidth*strlen($line))/2), $y, $line, $color);
  2205. $y += $fontheight;
  2206. }
  2207. break;
  2208. default:
  2209. while (list($numl, $line) = each($lines)) {
  2210. ImageString($dest_img, $font, $margin+$drop, ($y+$drop), $line, $shadow);
  2211. ImageString($dest_img, $font, $margin, $y, $line, $color);
  2212. $y += $fontheight;
  2213. }
  2214. break;
  2215. }
  2216. /* Now let's create our new image */
  2217. $new_image = $web_root. $root_dir. "/temp/temp-image.jpg";
  2218. @touch($new_image);
  2219. // Now let's make sure that new image is writable
  2220. if (is_writable($new_image)){
  2221. imagejpeg($dest_img, $new_image);
  2222. /* Now let's clean up our temp image */
  2223. imagedestroy($dest_img);
  2224. return true;
  2225. } else {
  2226. echo "Sorry, I couldn't open the temporary image file for writing.<br>".
  2227. "looks like something is wrong with the permissions on your temp directory at:<br><br>".
  2228. $web_root. $root_dir. "/temp<br><br>".
  2229. "Sorry about that, but this is a fatal error!<br><br>".
  2230. "You could turn off auto art searching in settings.php by changing<br><br>".
  2231. '$search_album_art = "false";';
  2232. exit();
  2233. return false;
  2234. }
  2235. }
  2236. // This function will resize the album images
  2237. function jzResizeAlbum($dirToSearch, $artistImage){
  2238. global $album_img_width, $album_img_height;
  2239. // Now let's see if the artist image needs to be resized
  2240. if ($album_img_width <> "0" and $album_img_height <> "0"){
  2241. // Now let's get the image dimensions and see if it needs resizing
  2242. $imgFile = $dirToSearch. "/". $artistImage;
  2243. $imgDst = $dirToSearch. "/". $artistImage. ".new";
  2244. $imgDim = getimagesize($imgFile);
  2245. $imgWidth = $imgDim[0];
  2246. $imgHeight = $imgDim[1];
  2247. // Now let's see if either is bigger or smaller than what we want
  2248. if ($imgHeight <> $album_img_height && $imgWidth <> $album_img_width){
  2249. // Ok, we need to change the height of the image
  2250. if (resizeImage($imgFile, $imgDst, $album_img_width, $album_img_height) == true){
  2251. // Now let's make sure there's not another old file, because we don't want to replace it
  2252. if (!is_file($imgFile. ".old")){
  2253. // Now let's backup the old file
  2254. @rename ($imgFile, $imgFile. ".old");
  2255. }
  2256. // Now let's put the new file in place
  2257. @rename ($imgDst, $imgFile);
  2258. }
  2259. }
  2260. }
  2261. }
  2262. // This function will resize the artist images
  2263. function jzResizeArtist($dirToSearch, $Image){
  2264. global $artist_img_width, $artist_img_height;
  2265. // Now let's see if the artist image needs to be resized
  2266. if ($artist_img_width <> "0" and $artist_img_height <> "0"){
  2267. // Now let's get the image dimensions and see if it needs resizing
  2268. $imgFile = $dirToSearch. "/". $Image;
  2269. $imgDst = $dirToSearch. "/". $Image. ".new";
  2270. $imgDim = getimagesize($imgFile);
  2271. $imgWidth = $imgDim[0];
  2272. $imgHeight = $imgDim[1];
  2273. // Now let's see if either is bigger or smaller than what we want
  2274. if ($imgHeight <> $artist_img_height && $imgWidth <> $artist_img_width){
  2275. // Ok, we need to change the height of the image
  2276. if (resizeImage($imgFile, $imgDst, $artist_img_width, $artist_img_height) == true){
  2277. // Now let's make sure there's not another old file, because we don't want to replace it
  2278. if (!is_file($imgFile. ".old")){
  2279. // Now let's backup the old file
  2280. @rename ($imgFile, $imgFile. ".old");
  2281. }
  2282. // Now let's put the new file in place
  2283. @rename ($imgDst, $imgFile);
  2284. }
  2285. }
  2286. }
  2287. }
  2288. function deldir($dir){
  2289. $current_dir = opendir($dir);
  2290. while($entryname = readdir($current_dir)){
  2291. if(is_dir("$dir/$entryname") and ($entryname != "." and $entryname!="..")){
  2292. deldir("${dir}/${entryname}");
  2293. }elseif($entryname != "." and $entryname!=".."){
  2294. unlink("${dir}/${entryname}");
  2295. }
  2296. }
  2297. closedir($current_dir);
  2298. rmdir($dir);
  2299. }
  2300. // simple function that can help, if you want to know if a string could be UTF-8 or not
  2301. function seems_utf8($Str) {
  2302. for ($i=0; $i<strlen($Str); $i++) {
  2303. if (ord($Str[$i]) < 0x80) continue; # 0bbbbbbb
  2304. elseif ((ord($Str[$i]) & 0xE0) == 0xC0) $n=1; # 110bbbbb
  2305. elseif ((ord($Str[$i]) & 0xF0) == 0xE0) $n=2; # 1110bbbb
  2306. elseif ((ord($Str[$i]) & 0xF8) == 0xF0) $n=3; # 11110bbb
  2307. elseif ((ord($Str[$i]) & 0xFC) == 0xF8) $n=4; # 111110bb
  2308. elseif ((ord($Str[$i]) & 0xFE) == 0xFC) $n=5; # 1111110b
  2309. else return false; # Does not match any model
  2310. for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ?
  2311. if ((++$i == strlen($Str)) || ((ord($Str[$i]) & 0xC0) != 0x80))
  2312. return false;
  2313. }
  2314. }
  2315. return true;
  2316. }
  2317. /*
  2318. * Returns the content range (in bytes) of the request.
  2319. * If the range header is not set, false is returned.
  2320. * @author Ben Dodson
  2321. * @since 6/28/06
  2322. * @version 6/28/06
  2323. * @return array(from,to) or false, if the RANGE header is not set.
  2324. */
  2325. function getContentRange($size) {
  2326. $from = 0; $to = $size-1;
  2327. if (isset($_SERVER['HTTP_RANGE'])) {
  2328. $split = explode("=",$_SERVER['HTTP_RANGE']);
  2329. if (trim($split[0]) == "bytes") {
  2330. if ($split[1][0] == '-') {
  2331. if ($size !== false) {
  2332. $val = trim(substr($split[1], 1));
  2333. $from = $size - $val - 1; // TODO: VERIFY THE -1 HERE
  2334. }
  2335. }
  2336. if (strpos($split[1], '-') !== false) {
  2337. $split2 = explode("-",$split[1]);
  2338. if (isset($split2[1]) && !isNothing($split2[1])) {
  2339. $to = trim($split2[1]);
  2340. }
  2341. $from = trim($split2[0]);
  2342. } else {
  2343. $from = trim($split[1]);
  2344. }
  2345. if(empty($to)) {
  2346. $to = $size - 1; // -1 because end byte is included
  2347. //(From HTTP protocol:
  2348. // 'The last-byte-pos value gives the byte-offset of the
  2349. // last byte in the range; that is, the byte positions specified are inclusive')
  2350. }
  2351. return array($from,$to);
  2352. }
  2353. }
  2354. return false;
  2355. }
  2356. ?>