initial import from pitchfork-0.5.5
[patchfork.git] / inc / base.php
CommitLineData
964dd0bc
JW
1<?php
2if(function_exists("mb_internal_encoding"))
3 mb_internal_encoding("UTF-8");
4define('HASH_PASS', "********");
5define('SALT_LENGTH', 15);
6
7session_start();
8
9/* make sure . is first in include path */
10set_include_path("." . PATH_SEPARATOR . get_include_path());
11require_once('Net/MPD.php');
12
13$release_version = "0.5.5";
14$release_date = "18-01-2008";
15
16$config_dir = "../config";
17
18/* should be metadata_dir, no time to change now TODO */
19$cover_dir = "$config_dir/metadata/";
20$metadata_dir = $cover_dir;
21
22
23$theme_dir = "../theme/";
24
25$config = @simplexml_load_file("../config/config.xml");
26$error_msg = false;
27
28/* playlist fields */
29$pl_fields = array("Title", "Album", "Artist", "Track", "Name", "Genre", "Date", "Composer", "Performer", "Comment", "Disc");
30
31if(!$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>");
33 exit();
34}
35
36$language = get_config("lang", "en");
37
38
39$selected_theme = get_config("theme", "default");
40if(!is_theme_dir_ok($theme_dir . $selected_theme))
41 $selected_theme = "default";
42
43$lpass = get_config('login_pass');
44
45if(!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";
50 exit();
51 }
52 }
53}
54
55function get_config($name, $default = null) {
56 global $config;
57 if(isset($config->$name)) {
58 if(trim($config->$name)=="")
59 return $default;
60 return ((string)$config->$name);
61 }
62 else {
63 return $default;
64 }
65}
66
67function get_selected_plfields() {
68 global $config, $pl_fields;
69 $plentry = false;
70 if(isset($config->plentry))
71 $plentry = $config->plentry;
72 $ret = array();
73 $i = 0;
74 foreach($pl_fields as $field) {
75 if($plentry) {
76 $ret[] = ((string)$plentry->$field)!="";
77 }
78 else {
79 $ret[] = $i++<3;
80 }
81 }
82 return $ret;
83}
84
85function set_config($name, $value) {
86
87}
88/* if a key is found to be numeric it will be replaced by numb_replace
89*/
90function array_to_xml($arr, $xml = null, $numb_replace = "elem") {
91 if(is_null($xml)) {
92 $xml = simplexml_load_string("<?xml version='1.0' ?><root/>");
93 }
94 foreach($arr as $key => $value) {
95 if(is_numeric($key))
96 $key = $numb_replace;
97 if(is_array($value)) {
98 $tmp = $xml->addChild($key);
99 array_to_xml($value, $tmp, $numb_replace);
100 }
101 else {
102 $xml->addChild($key, htmlspecialchars($value));
103 }
104 }
105 return $xml;
106}
107
108function get_mpd($type) {
109 try {
110 $port = 6600;
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"));
114 $ret->connect();
115 return $ret;
116 }
117 catch(PEAR_Exception $e) {
118 return false;
119 }
120}
121function get_playlist() {
122 return get_mpd("Playlist");
123}
124function get_playback() {
125 return get_mpd("Playback");
126}
127function get_database() {
128 return get_mpd("Database");
129}
130function get_admin() {
131 return get_mpd("Admin");
132}
133
134/* mimic behaviour of java System.currentTimeMillis() */
135// ex: 1172151695935.8
136function current_time_millis() {
137 return microtime(true)*1000;
138}
139
140// returns array with available themes
141function get_available_themes() {
142 global $theme_dir;
143 $dirs = scandir($theme_dir);
144 $themes = array();
145 foreach($dirs as $dir) {
146 if($dir=="."||$dir=="..")
147 continue;
148
149 if(is_theme_dir_ok($theme_dir . $dir)) {
150 $themes[] = $dir;
151 }
152 }
153 return $themes;
154}
155
156function is_theme_dir_ok($tdir) {
157 return is_dir($tdir) && file_exists($tdir . "/theme.css") && file_exists($tdir . "/theme.js");
158}
159
160function generate_hash($val, $salt = false) {
161 if(function_exists("hash")) {
162 if($salt===false)
163 $salt = substr(md5(uniqid(rand(), true)), 0, SALT_LENGTH);
164
165 $p = hash("sha256", $val . $salt);
166 return "sha:" . $p . $salt;
167 }
168 else return $val;
169}
170function check_hash($proper, $check) {
171 $len = strlen($proper);
172 $nhash = generate_hash($check, substr($proper, $len-SALT_LENGTH));
173 if($proper==$nhash) {
174 return true;
175 }
176 return false;
177}
178
179/* someone find me a php equiv */
180function 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");
185 }
186 else {
187 return strlen($data);
188 }
189}
190?>
This page took 0.032924 seconds and 4 git commands to generate.