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');
45 if(!is_null($lpass)&&$lpass!="") {
46 if(!isset($_SESSION['logged_in'])||!$_SESSION['logged_in']) {
47 if(!isset($no_require_login)) {
48 header("Location: login.php");
49 echo "Wrong password";
55 function get_config($name, $default = null) {
57 if(isset($config->$name)) {
58 if(trim($config->$name)=="")
60 return ((string)$config->$name);
67 function get_selected_plfields() {
68 global $config, $pl_fields;
70 if(isset($config->plentry))
71 $plentry = $config->plentry;
74 foreach($pl_fields as $field) {
76 $ret[] = ((string)$plentry->$field)!="";
85 function set_config($name, $value) {
88 /* if a key is found to be numeric it will be replaced by numb_replace
90 function array_to_xml($arr, $xml = null, $numb_replace = "elem") {
92 $xml = simplexml_load_string("<?xml version='1.0' ?><root/>");
94 foreach($arr as $key => $value) {
97 if(is_array($value)) {
98 $tmp = $xml->addChild($key);
99 array_to_xml($value, $tmp, $numb_replace);
102 $xml->addChild($key, htmlspecialchars($value));
108 function get_mpd($type) {
111 if(is_numeric(get_config("mpd_port")))
112 $port = (int) get_config("mpd_port");
113 $ret = Net_MPD::factory($type, get_config('mpd_host'), intval($port), get_config("mpd_pass"));
117 catch(PEAR_Exception $e) {
121 function get_playlist() {
122 return get_mpd("Playlist");
124 function get_playback() {
125 return get_mpd("Playback");
127 function get_database() {
128 return get_mpd("Database");
130 function get_admin() {
131 return get_mpd("Admin");
134 /* mimic behaviour of java System.currentTimeMillis() */
135 // ex: 1172151695935.8
136 function current_time_millis() {
137 return microtime(true)*1000;
140 // returns array with available themes
141 function get_available_themes() {
143 $dirs = scandir($theme_dir);
145 foreach($dirs as $dir) {
146 if($dir=="."||$dir=="..")
149 if(is_theme_dir_ok($theme_dir . $dir)) {
156 function is_theme_dir_ok($tdir) {
157 return is_dir($tdir) && file_exists($tdir . "/theme.css") && file_exists($tdir . "/theme.js");
160 function generate_hash($val, $salt = false) {
161 if(function_exists("hash")) {
163 $salt = substr(md5(uniqid(rand(), true)), 0, SALT_LENGTH);
165 $p = hash("sha256", $val . $salt);
166 return "sha:" . $p . $salt;
170 function check_hash($proper, $check) {
171 $len = strlen($proper);
172 $nhash = generate_hash($check, substr($proper, $len-SALT_LENGTH));
173 if($proper==$nhash) {
179 /* someone find me a php equiv */
180 function str_byte_count($data) {
181 if(function_exists("mb_strlen")) {
182 /* count bytes, not characters if utf-8,
183 * I imagine this works, but hard to actually test */
184 return mb_strlen($data, "ascii");
187 return strlen($data);