2 if(function_exists("mb_internal_encoding"))
3 mb_internal_encoding("UTF-8");
4 define('HASH_PASS', "********");
5 define('SALT_LENGTH', 15);
9 /* make sure . is first in include path */
10 set_include_path("." . PATH_SEPARATOR . get_include_path());
11 require_once('Net/MPD.php');
13 $release_version = "0.5.5";
14 $release_date = "18-01-2008";
16 $config_dir = "../config";
18 /* should be metadata_dir, no time to change now TODO */
19 $cover_dir = "$config_dir/metadata/";
20 $metadata_dir = $cover_dir;
23 $theme_dir = "../theme/";
25 $config = @simplexml_load_file("../config/config.xml");
29 $pl_fields = array("Title", "Album", "Artist", "Track", "Name", "Genre", "Date", "Composer", "Performer", "Comment", "Disc");
31 if(!$config&&!isset($_GET['new_config'])) {
32 header("Location: config.php?new_config=true") or die("Unable to redirect, go here <a href='config.php?new_config=true'>config</a>");
36 $language = get_config("lang", "en");
39 $selected_theme = get_config("theme", "default");
40 if(!is_theme_dir_ok($theme_dir . $selected_theme))
41 $selected_theme = "default";
43 $lpass = get_config('login_pass');
47 if(isset($_SESSION['logged_in']) && ($_SESSION['logged_in'] == "ro")) {
52 if(!is_null($lpass)&&$lpass!="") {
53 if(!isset($_SESSION['logged_in'])||!$_SESSION['logged_in'] || ($need_rw && !$rw)) {
54 if(!isset($no_require_login)) {
55 header("Location: login.php");
56 echo "Wrong password";
63 function get_config($name, $default = null) {
65 if(isset($config->$name)) {
66 if(trim($config->$name)=="")
68 return ((string)$config->$name);
75 function get_selected_plfields() {
76 global $config, $pl_fields;
78 if(isset($config->plentry))
79 $plentry = $config->plentry;
82 foreach($pl_fields as $field) {
84 $ret[] = ((string)$plentry->$field)!="";
93 function set_config($name, $value) {
96 /* if a key is found to be numeric it will be replaced by numb_replace
98 function array_to_xml($arr, $xml = null, $numb_replace = "elem") {
100 $xml = simplexml_load_string("<?xml version='1.0' ?><root/>");
102 foreach($arr as $key => $value) {
104 $key = $numb_replace;
105 if(is_array($value)) {
106 $tmp = $xml->addChild($key);
107 array_to_xml($value, $tmp, $numb_replace);
110 $xml->addChild($key, htmlspecialchars($value));
116 function get_mpd($type) {
119 if(is_numeric(get_config("mpd_port")))
120 $port = (int) get_config("mpd_port");
121 $ret = Net_MPD::factory($type, get_config('mpd_host'), intval($port), get_config("mpd_pass"));
125 catch(PEAR_Exception $e) {
129 function get_playlist() {
130 return get_mpd("Playlist");
132 function get_playback() {
133 return get_mpd("Playback");
135 function get_database() {
136 return get_mpd("Database");
138 function get_admin() {
139 return get_mpd("Admin");
142 /* mimic behaviour of java System.currentTimeMillis() */
143 // ex: 1172151695935.8
144 function current_time_millis() {
145 return microtime(true)*1000;
148 // returns array with available themes
149 function get_available_themes() {
151 $dirs = scandir($theme_dir);
153 foreach($dirs as $dir) {
154 if($dir=="."||$dir=="..")
157 if(is_theme_dir_ok($theme_dir . $dir)) {
164 function is_theme_dir_ok($tdir) {
165 return is_dir($tdir) && file_exists($tdir . "/theme.css") && file_exists($tdir . "/theme.js");
168 function generate_hash($val, $salt = false) {
169 if(function_exists("hash")) {
171 $salt = substr(md5(uniqid(rand(), true)), 0, SALT_LENGTH);
173 $p = hash("sha256", $val . $salt);
174 return "sha:" . $p . $salt;
178 function check_hash($proper, $check) {
179 $len = strlen($proper);
180 $nhash = generate_hash($check, substr($proper, $len-SALT_LENGTH));
181 if($proper==$nhash) {
187 /* someone find me a php equiv */
188 function str_byte_count($data) {
189 if(function_exists("mb_strlen")) {
190 /* count bytes, not characters if utf-8,
191 * I imagine this works, but hard to actually test */
192 return mb_strlen($data, "ascii");
195 return strlen($data);