From f79a2489c1f18f2bf1436fa5ec4c8fd9c903fd61 Mon Sep 17 00:00:00 2001 From: Karl 'vollmerk' Vollmer Date: Mon, 9 Oct 2006 09:00:47 +0000 Subject: updates, ajax kind of broken right now its late --- docs/CHANGELOG | 5 +- index.php | 11 +- lib/class/localplay.class.php | 25 ++- lib/ui.lib.php | 24 ++ modules/kajax/ajax.js | 10 +- modules/sajax/Sajax.php | 373 ------------------------------- server/ajax.server.php | 39 +++- templates/show_localplay_control.inc.php | 19 +- templates/show_localplay_status.inc.php | 27 ++- templates/sidebar.inc.php | 8 +- 10 files changed, 138 insertions(+), 403 deletions(-) delete mode 100644 modules/sajax/Sajax.php diff --git a/docs/CHANGELOG b/docs/CHANGELOG index fd540d0f..67dbe86c 100755 --- a/docs/CHANGELOG +++ b/docs/CHANGELOG @@ -4,7 +4,10 @@ -------------------------------------------------------------------------- v.3.3.3-Alpha1 - - Fixed display of disabled localplay methods, Preferences wil + - Tweaked Kajax, now accepts an array of elements to replace + from a passed xml document. allows for multiple targets + on a single ajax request + - Fixed display of disabled localplay methods, Preferences will now only display active ones. - Fixed MPD Controller to attempt to find files based on filename if they were added outside of ampache diff --git a/index.php b/index.php index df7c693d..8688fffa 100644 --- a/index.php +++ b/index.php @@ -40,12 +40,17 @@ $action = scrub_in($_REQUEST['action']); if (conf('refresh_limit') > 5) { $ajax_url = conf('web_path') . '/server/ajax.server.php?action=reloadnp&user_id=' . $GLOBALS['user']->id . '&sessid=' . session_id(); - $ajax_object = 'nowplaying'; + $ajax_object = 'np_refresh'; require_once(conf('prefix') . '/templates/javascript_refresh.inc.php'); } ?> - -
+ +
diff --git a/lib/class/localplay.class.php b/lib/class/localplay.class.php index b9f0d8cc..48097882 100644 --- a/lib/class/localplay.class.php +++ b/lib/class/localplay.class.php @@ -534,7 +534,7 @@ class Localplay { * of the current player state */ function get_user_state($state) { - + switch ($state) { case 'play': return _('Now Playing'); @@ -552,6 +552,29 @@ class Localplay { } // get_user_state + /** + * get_user_playing + * This attempts to return a nice user friendly + * currently playing string + */ + function get_user_playing() { + + $status = $this->status(); + + /* Format the track name */ + $track_name = $status['track_artist'] . ' - ' . $status['track_album'] . ' - ' . $status['track_title']; + + /* This is a cheezball fix for when we were unable to find a + * artist/album (or one wasn't provided) + */ + $track_name = ltrim(ltrim($track_name,' - '),' - '); + + $track_name = "[" . $status['track'] . "] - " . $track_name; + + return $track_name; + + } // get_user_playing + } //end localplay class ?> diff --git a/lib/ui.lib.php b/lib/ui.lib.php index e30db5ec..7b93b37e 100644 --- a/lib/ui.lib.php +++ b/lib/ui.lib.php @@ -1334,5 +1334,29 @@ function get_user_icon($name) { } // show_icon +/** + * xml_from_array + * This takes a one dimensional array and + * creates a XML document form it for use + * primarly by the ajax mojo + */ +function xml_from_array($array) { + + $escape_array = array ('<','&'); + $replace_array = array('<','&'); + + $string = "\n"; + foreach ($array as $key=>$value) { + /* We need to escape the value */ + $value = str_replace($escape_array,$replace_array,$value); + $string .= "\t<$key>$value\n"; + } + $string .= "\n"; + + return $string; + +} // xml_from_array + + ?> diff --git a/modules/kajax/ajax.js b/modules/kajax/ajax.js index cbe86c78..9a7bea50 100755 --- a/modules/kajax/ajax.js +++ b/modules/kajax/ajax.js @@ -25,7 +25,8 @@ http_request.open('GET', url, true); http_request.send(null); } - + + // uid is an array of uids that need to be replaced function ajaxPut(url,uid) { if (window.ActiveXObject) { // IE try { @@ -52,7 +53,10 @@ function getContents(http_request,uid) { if (http_request.readyState == 4) { - data = http_request.responseText; - document.getElementById(uid).innerHTML = data; + data = http_request.responseXML; + for(i=0;i$v) { - $esc_key = sajax_esc($k); - if (is_numeric($k)) - $s .= "$k: " . sajax_get_js_repr($v) . ", "; - else - $s .= "\"$esc_key\": " . sajax_get_js_repr($v) . ", "; - } - if (count($value)) - $s = substr($s, 0, -2); - return $s . " }"; - } - else { - $esc_val = sajax_esc($value); - $s = "'$esc_val'"; - return $s; - } - } - - function sajax_handle_client_request() { - global $sajax_export_list; - - $mode = ""; - - if (! empty($_GET["rs"])) - $mode = "get"; - - if (!empty($_POST["rs"])) - $mode = "post"; - - if (empty($mode)) - return; - - $target = ""; - - if ($mode == "get") { - // Bust cache in the head - header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past - header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); - // always modified - header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 - header ("Pragma: no-cache"); // HTTP/1.0 - $func_name = $_GET["rs"]; - if (! empty($_GET["rsargs"])) - $args = $_GET["rsargs"]; - else - $args = array(); - } - else { - $func_name = $_POST["rs"]; - if (! empty($_POST["rsargs"])) - $args = $_POST["rsargs"]; - else - $args = array(); - } - - if (! in_array($func_name, $sajax_export_list)) - echo "-:$func_name not callable"; - else { - echo "+:"; - $result = call_user_func_array($func_name, $args); - echo "var res = " . trim(sajax_get_js_repr($result)) . "; res;"; - } - exit; - } - - function sajax_get_common_js() { - global $sajax_debug_mode; - global $sajax_request_type; - global $sajax_remote_uri; - global $sajax_failure_redirect; - - $t = strtoupper($sajax_request_type); - if ($t != "" && $t != "GET" && $t != "POST") - return "// Invalid type: $t.. \n\n"; - - ob_start(); - ?> - - // remote scripting library - // (c) copyright 2005 modernmethod, inc - var sajax_debug_mode = ; - var sajax_request_type = ""; - var sajax_target_id = ""; - var sajax_failure_redirect = ""; - - function sajax_debug(text) { - if (sajax_debug_mode) - alert(text); - } - - function sajax_init_object() { - sajax_debug("sajax_init_object() called..") - - var A; - - var msxmlhttp = new Array( - 'Msxml2.XMLHTTP.5.0', - 'Msxml2.XMLHTTP.4.0', - 'Msxml2.XMLHTTP.3.0', - 'Msxml2.XMLHTTP', - 'Microsoft.XMLHTTP'); - for (var i = 0; i < msxmlhttp.length; i++) { - try { - A = new ActiveXObject(msxmlhttp[i]); - } catch (e) { - A = null; - } - } - - if(!A && typeof XMLHttpRequest != "undefined") - A = new XMLHttpRequest(); - if (!A) - sajax_debug("Could not create connection object."); - return A; - } - - var sajax_requests = new Array(); - - function sajax_cancel() { - for (var i = 0; i < sajax_requests.length; i++) - sajax_requests[i].abort(); - } - - function sajax_do_call(func_name, args) { - var i, x, n; - var uri; - var post_data; - var target_id; - - sajax_debug("in sajax_do_call().." + sajax_request_type + "/" + sajax_target_id); - target_id = sajax_target_id; - if (typeof(sajax_request_type) == "undefined" || sajax_request_type == "") - sajax_request_type = "GET"; - - uri = ""; - if (sajax_request_type == "GET") { - - if (uri.indexOf("?") == -1) - uri += "?rs=" + escape(func_name); - else - uri += "&rs=" + escape(func_name); - uri += "&rst=" + escape(sajax_target_id); - uri += "&rsrnd=" + new Date().getTime(); - - for (i = 0; i < args.length-1; i++) - uri += "&rsargs[]=" + escape(args[i]); - - post_data = null; - } - else if (sajax_request_type == "POST") { - post_data = "rs=" + escape(func_name); - post_data += "&rst=" + escape(sajax_target_id); - post_data += "&rsrnd=" + new Date().getTime(); - - for (i = 0; i < args.length-1; i++) - post_data = post_data + "&rsargs[]=" + escape(args[i]); - } - else { - alert("Illegal request type: " + sajax_request_type); - } - - x = sajax_init_object(); - if (x == null) { - if (sajax_failure_redirect != "") { - location.href = sajax_failure_redirect; - return false; - } else { - sajax_debug("NULL sajax object for user agent:\n" + navigator.userAgent); - return false; - } - } else { - x.open(sajax_request_type, uri, true); - // window.open(uri); - - sajax_requests[sajax_requests.length] = x; - - if (sajax_request_type == "POST") { - x.setRequestHeader("Method", "POST " + uri + " HTTP/1.1"); - x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); - } - - x.onreadystatechange = function() { - if (x.readyState != 4) - return; - - sajax_debug("received " + x.responseText); - - var status; - var data; - var txt = x.responseText.replace(/^\s*|\s*$/g,""); - status = txt.charAt(0); - data = txt.substring(2); - - if (status == "") { - // let's just assume this is a pre-response bailout and let it slide for now - } else if (status == "-") - alert("Error: " + data); - else { - if (target_id != "") - document.getElementById(target_id).innerHTML = eval(data); - else { - try { - var callback; - var extra_data = false; - if (typeof args[args.length-1] == "object") { - callback = args[args.length-1].callback; - extra_data = args[args.length-1].extra_data; - } else { - callback = args[args.length-1]; - } - callback(eval(data), extra_data); - } catch (e) { - sajax_debug("Caught error " + e + ": Could not eval " + data ); - } - } - } - } - } - - sajax_debug(func_name + " uri = " + uri + "/post = " + post_data); - x.send(post_data); - sajax_debug(func_name + " waiting.."); - delete x; - return true; - } - - - - // wrapper for - - function x_() { - sajax_do_call("", - x_.arguments); - } - - diff --git a/server/ajax.server.php b/server/ajax.server.php index aa519c3f..76e78102 100644 --- a/server/ajax.server.php +++ b/server/ajax.server.php @@ -33,6 +33,9 @@ if (!session_exists($_REQUEST['sessid'])) { exit(); } $GLOBALS['user'] = new User($_REQUEST['user_id']); $action = scrub_in($_REQUEST['action']); +/* Set the correct headers */ +header("Content-type: application/xhtml+xml"); + switch ($action) { case 'localplay': init_preferences(); @@ -40,7 +43,32 @@ switch ($action) { $localplay->connect(); $function = scrub_in($_GET['cmd']); $value = scrub_in($_GET['value']); + /* Return information based on function */ + switch($function) { + case 'play': + case 'stop': + case 'pause': + $results['lp_state'] = $localplay->get_user_state($function); + $results['lp_playing'] = $localplay->get_user_playing(); + break; + case 'next': + case 'prev': + $results['lp_state'] = $localplay->get_user_state('play'); + $results['lp_playing'] = $localplay->get_user_playing(); + break; + case 'volume_up': + case 'volume_down': + case 'volume_mute': + $status = $localplay->status(); + $results['lp_volume'] = $status['volume']; + break; + default: + $results = array(); + break; + } // end switch on cmd $localplay->$function($value); + $xml_doc = xml_from_array($results); + echo $xml_doc; break; case 'change_play_type': init_preferences(); @@ -54,11 +82,20 @@ switch ($action) { $ajax_url = conf('web_path') . '/server/ajax.server.php'; $required_info = "&user_id=" . $GLOBALS['user']->id . "&sessid=" . session_id(); ${$_GET['type']} = 'id="pt_active"'; - + ob_start(); require_once(conf('prefix') . '/templates/show_localplay_switch.inc.php'); + $results['play_type'] = ob_get_contents(); + ob_end_clean(); + $xml_doc = xml_from_array($results); + echo $xml_doc; break; case 'reloadnp': + ob_start(); show_now_playing(); + $results['np_data'] = ob_get_contents(); + ob_end_clean(); + $xml_doc = xml_from_array($results); + echo $xml_doc; break; default: echo "Default Action"; diff --git a/templates/show_localplay_control.inc.php b/templates/show_localplay_control.inc.php index b45cd887..a0177603 100644 --- a/templates/show_localplay_control.inc.php +++ b/templates/show_localplay_control.inc.php @@ -23,29 +23,36 @@ $web_path = conf('web_path'); $localplay = init_localplay(); -$required_info = "&user_id=" . $GLOBALS['user']->id . "&sessid=" . session_id(); +$required_info = "&user_id=" . $GLOBALS['user']->id . "&sessid=" . session_id(); $ajax_url = $web_path . '/server/ajax.server.php'; ?> +
has_function('prev')) { ?> - + prev - + stop has_function('pause')) { ?> - + pause - + play has_function('next')) { ?> - + next diff --git a/templates/show_localplay_status.inc.php b/templates/show_localplay_status.inc.php index 2fab62de..0e250923 100644 --- a/templates/show_localplay_status.inc.php +++ b/templates/show_localplay_status.inc.php @@ -23,40 +23,39 @@ $web_path = conf('web_path'); $localplay = init_localplay(); -$required_info = "&user_id=" . $GLOBALS['user']->id . "&sessid=" . session_id(); +$required_info = "&user_id=" . $GLOBALS['user']->id . "&sessid=" . session_id(); $ajax_url = $web_path . '/server/ajax.server.php'; $status = $localplay->status(); -/* Format the track name */ -$track_name = $status['track_artist'] . ' - ' . $status['track_album'] . ' - ' . $status['track_title']; - -/* This is a cheezball fix for when we were unable to find a - * artist/album (or one wasn't provided) - */ -$track_name = ltrim(ltrim($track_name,' - ')); ?> -get_user_state($status['state']) ?>
-  '; ?> +get_user_state($status['state']) ?>
+  get_user_playing(); ?>
+ has_function('volume_up')) { ?> - + volume up has_function('volume_down')) { ?> - + volume down has_function('volume_mute')) { ?> - + volume mute
- + :

has_function('repeat')) { ?> diff --git a/templates/sidebar.inc.php b/templates/sidebar.inc.php index d4b5960a..38fef93d 100644 --- a/templates/sidebar.inc.php +++ b/templates/sidebar.inc.php @@ -184,9 +184,15 @@ $web_path = conf('web_path');
id . "&sessid=" . session_id(); + $required_info = "&user_id=" . $GLOBALS['user']->id . "&sessid=" . session_id(); $ajax_url = $web_path . '/server/ajax.server.php'; ?> + -- cgit