]> Joshua Wise's Git repositories - patchfork.git/blob - inc/base.php
Add support for view-only mode.
[patchfork.git] / inc / base.php
1 <?php
2 if(function_exists("mb_internal_encoding"))
3         mb_internal_encoding("UTF-8");
4 define('HASH_PASS', "********");
5 define('SALT_LENGTH', 15);
6
7 session_start();
8
9 /* make sure . is first in include path */
10 set_include_path("." . PATH_SEPARATOR . get_include_path());
11 require_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
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>");
33         exit(); 
34 }
35
36 $language = get_config("lang", "en");
37
38
39 $selected_theme = get_config("theme", "default");
40 if(!is_theme_dir_ok($theme_dir . $selected_theme)) 
41         $selected_theme = "default";
42
43 $lpass = get_config('login_pass');
44
45 $rw = true;
46 $ro = false;
47 if(isset($_SESSION['logged_in']) && ($_SESSION['logged_in'] == "ro")) {
48         $rw = false;
49         $ro = true;
50 }
51
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";
57                         exit();
58                 }
59         }
60 }
61
62
63 function get_config($name, $default = null) {
64         global $config;
65         if(isset($config->$name)) {
66                 if(trim($config->$name)=="")
67                         return $default;
68                 return ((string)$config->$name);
69         }
70         else {
71                 return $default;
72         }
73 }
74
75 function get_selected_plfields() {
76         global $config, $pl_fields;
77         $plentry = false;
78         if(isset($config->plentry)) 
79                 $plentry = $config->plentry;
80         $ret = array();
81         $i = 0;
82         foreach($pl_fields as $field) {
83                 if($plentry) {
84                         $ret[] = ((string)$plentry->$field)!="";
85                 }
86                 else {
87                         $ret[] = $i++<3;
88                 }
89         }
90         return $ret;
91 }
92
93 function set_config($name, $value) {
94
95 }
96 /* if a key is found to be numeric it will be replaced by numb_replace 
97 */
98 function array_to_xml($arr, $xml = null, $numb_replace = "elem") {
99         if(is_null($xml)) {
100                 $xml = simplexml_load_string("<?xml version='1.0' ?><root/>");
101         }
102         foreach($arr as $key => $value) {
103                 if(is_numeric($key)) 
104                         $key = $numb_replace;
105                 if(is_array($value)) {
106                         $tmp = $xml->addChild($key);
107                         array_to_xml($value, $tmp, $numb_replace);
108                 }
109                 else {
110                         $xml->addChild($key, htmlspecialchars($value));
111                 }
112         }
113         return $xml;
114 }
115
116 function get_mpd($type) {
117         try {
118                 $port = 6600;
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"));
122                 $ret->connect();
123                 return $ret;
124         }
125         catch(PEAR_Exception $e) {
126                 return false;
127         }
128 }
129 function get_playlist() {
130         return get_mpd("Playlist");
131 }
132 function get_playback() {
133         return get_mpd("Playback");
134 }
135 function get_database() {
136         return get_mpd("Database");
137 }
138 function get_admin() {
139         return get_mpd("Admin");
140 }
141
142 /* mimic behaviour of java System.currentTimeMillis() */
143 // ex: 1172151695935.8
144 function current_time_millis() {
145         return microtime(true)*1000;
146 }
147
148 // returns array with available themes
149 function get_available_themes() {
150         global $theme_dir;
151         $dirs = scandir($theme_dir);
152         $themes = array();
153         foreach($dirs as $dir) {
154                 if($dir=="."||$dir=="..")
155                         continue;
156
157                 if(is_theme_dir_ok($theme_dir . $dir)) {
158                         $themes[] = $dir;
159                 }
160         }
161         return $themes;
162 }
163
164 function is_theme_dir_ok($tdir) {
165         return is_dir($tdir) && file_exists($tdir . "/theme.css")  && file_exists($tdir . "/theme.js");
166 }
167
168 function generate_hash($val, $salt = false) {
169         if(function_exists("hash")) {
170                 if($salt===false)
171                         $salt = substr(md5(uniqid(rand(), true)), 0, SALT_LENGTH);
172                 
173                 $p = hash("sha256", $val . $salt);
174                 return "sha:" . $p . $salt;
175         }
176         else return $val;
177 }
178 function check_hash($proper, $check) {
179         $len = strlen($proper);
180         $nhash = generate_hash($check, substr($proper, $len-SALT_LENGTH));
181         if($proper==$nhash) {
182                 return true;
183         }
184         return false;
185 }
186
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");
193         }
194         else {
195                 return strlen($data);
196         }
197 }
198 ?>
This page took 0.027351 seconds and 4 git commands to generate.