AMuleWeb PHP

From AMule Project FAQ
Revision as of 13:11, 4 June 2009 by Woutermense (Talk | contribs)

Jump to: navigation, search

Introduction

aMuleWeb is an add-on program, used together with aMule and serves as webserver, allowing user to control aMule through browser interface. aMuleWeb works very similar to regular and wide-known webservers such as Apache[1] - set of HTML pages stored on server side (called "template"). Those pages contains embedded server-side script, which is running when user requests the page. Resulting page is returned to user.

The scripting language, aMuleWeb using is very similar to widely known "PHP", developed and distributed by Zend company [2]. Since aMuleWeb implementation is differ from original PHP, I will call it "aMuleWeb PHP" for reader convenience. "The PHP" language, will be referred as "real PHP" or just "PHP" to make difference between the two.

Conventions

Language elements and reserved words are marked bold: keyword. Things that are not part of syntax, but are reserved or predefined are marked bold italic: special_var. Language limitations are marked like:

  • Making cofee not supported

Interaction with PHP and webserver

Similarly to real PHP, script is embedded into regular HTML pages using <?php ?> marks. Page must have .php extension in order to be searched for script. Note, that markups can not be nested, i.e. placing <html> marks inside of <?php ?> will result in syntax error.

Core language constructs

aMuleWeb PHP have same basic language elements as real PHP version 5. Several things supported or implemented differently: OO constructs (classes) may pass syntax check, but they are not supported in any way. include construct is parse-time directive (in contrary to PHP, where you can do it in run-time and check result of "include" operation) There's no list at all.

Variables

There's scalar (referred as $var) and array (referred as @var) variables. Similarly to real PHP all variables are local, unless defined otherwise. Variables, marked as global must be declated before this definition. Example:

$a = 1;
function foo()
{
	global $a;
}

References to variable and to array members are fully supported:

$a = &$b;
$c = &$c[5];
  • Reference of variable by name like $$var is not supported.
  • Integer variables are 64-bit unsigned integers
  • Variable substitution inside of "" is not supported.

Operators

All PHP operators supported. Implicit type conversion is supported too, as common in scripting languages. This way 1 + "2" = 3.

  • Prefix and postfix ++ and -- are not distinguished (prefix form used)
  • Using operator + for array mean "join" in real PHP. Here array is interpreted as integer equal to number of elements.

Subroutines

function construct is fully supported. In contrary to regular PHP function must be defined prior calling. It is error to do otherwise. Functions can be recursive, thou. Real PHP allows to omit parameters for function calls (default parameters). aMuleWeb PHP does not support this conventions, which mean that all parameters must be present when function is called. This is not an error, and parameters will receive predefined values when omit: 0 for numbers, "" (empty string) for strings. Parameters can be references or arrays with usual meaning.

  • Function can not return reference.

Library

PHP language is known to have huge library of useful subroutines. Those subroutines are used to perform various tasks, varying from simple string manipulation to complex databases and OS binding. aMuleWeb PHP on the other hand, have very limited library. There's several reasons behind this fact. First of all, it is out of question to completely re-create PHP library, given it's size and complexity. Second, and most important, it's not really needed. aMuleWeb PHP is built to serve very specific application, and used to create very specific kind of pages. It will not be used to create e-commerce site or webmail. List below summarize supported functions:

If the aMule webserver was built with native language support enabled:

  • _($message) and gettext($message): gettext()
  • gettext_noop($message)
  • ngettext($msgid1, $msgid2, $n): ngettext()

Predefined global variables

Similarly to real PHP, there's set of special, or predefined variables, which have special meaning. Those variables may indicate input received from user, or they may control script execution. Those variables have same name as in real PHP, but functionality may be limited.

$_SESSION: Hash of values that survive between engine invocation per session. Some of it's values are predefined by webserver itself.

  1. $_SESSION["guest_login"]: set to 1 when password supplied belongs to low-right user, 0 otherwise
  2. $_SESSION["login_error"]: set when bad password supplied, normally is an empty string.
  3. $_SESSION["auto_refresh"]: always set; indicate time to autorefresh (in seconds). User can supply a zero. Can be used in http-equiv:
<?php
       if ( $_SESSION["auto_refresh"] > 0 ) {
               echo "<meta http-equiv=\"refresh\" content=\"", $_SESSION["auto_refresh"], '">';
       }
?>

$HTTP_GET_VARS: Hash of values passed from browser on GET or POST method

Functions

The whole purpose of aMuleWeb control aMule. aMuleWeb PHP provide library functions for convinient access to underlying aMule datastructure for both queries and actions.

amule_do_shared_cmd

Description

void amule_do_shared_cmd(string $file_hash, string $command, [int $priority])

Used to change the priority of a shared file. The priority can be either increased, decreased or set directly.

Parameters

  • $file hash: identifies the file which the command is applied to.
  • $command: can be either "prioup", "priodown" or "prio". The "prioup" command increases the priority in the order very low, low, normal, high, very high, powershare, auto(, very low, ...). The "priodown" command decreases priority. When the "prio" command is used, $priority is a required parameter.
  • $priority: is used to set the priority directly. Allowed values are 4 = very low, 0 = low, 1 = normal, 2 = high, 3 = very high, 5 = powershare, 6 = auto.

amule_do_reload_shared_cmd

amule_do_reload_shared_cmd(): Used to reload the list of shared files fom disk.

amule_do_download_cmd

amule_do_download_cmd($file_hash, $command, $optional_arg): Used to change a parameter of a file in the downloads list. $command can be pause, resume, cancel, prio, prioup or priodown. In case of prio, $optional_arg needs to contain the new priority.

amule_kad_connect

amule_kad_connect($bootstrap_ip, $bootstrap_port): Used to connect to the Kad network.

amule_do_add_server_cmd

amule_do_add_server_cmd($server_addr, $server_port, $server_name): Used to add a server to the server list.

amule_do_server_cmd

amule_do_server_cmd($server_ip, $server_port, $command): Used to connect to or disconnect from a server or to remove a server. $command can be connect, disconnect or remove.

amule_get_stats

amule_get_stats(): Used to query the status of aMule. Returns a hash with the keys: id, serv_addr, serv_name, serv_users, kad_connected, kad_firewalled, speed_up, speed_down, speed_limit_up, speed_limit_down.

amule_get_categories

amule_get_categories(): return list of valid categories in form of a associative array, the key is and index starting from 0 (= all), the value is the full name of the category ('all' is the 0-indexed, others may be "film", "Docs", etc...)

$cat_list = amule_get_categories();
foreach($cat_list as $key => $cat_string) {
 echo "cat = ". $cat_string ."- key = ". $key;
}  

amule_get_options

amule_get_options(): return options and their values. Options are returned in form of hash, where keys are categories and values are hashes of name-value for each option.

Returned keys are: nick, connection, files, webserver, coretweaks.

Value of connection is a hash with keys: max_line_up_cap, max_line_down_cap, max_up_limit, max_down_limit, slot_alloc, tcp_port, udp_port, udp_dis, max_file_src, max_conn_total, autoconn_en, reconn_en.

Value of files is a hash with keys: ich_en, aich_trust, new_files_paused, new_files_auto_dl_prio, preview_prio, new_files_auto_ul_prio, upload_full_chunks, first_last_chunks_prio, start_next_paused, resume_same_cat, save_sources, extract_metadata, alloc_full, check_free_space, min_free_space.

Value of webserver is a hash with keys: use_gzip, autorefresh_time.

Value of coretweaks is a hash with key: max_conn_5sec.

amule_set_options

amule_set_options($options_hash): set aMule options. Receives hash in form returned by amule_get_options()

amule_do_search_download_cmd

amule_do_search_download_cmd($file_hash, $cat): Used to download 1 of the search results.

amule_do_search_start_cmd

amule_do_search_start_cmd($search, $ext, $filetype, $type, $avail, $min_size, $max_size):

amule_get_log

amule_get_log($rst): Returns log if $rst = "0", resets log otherwise.

$log = amule_get_log();
echo "My log:", $log;

amule_get_serverinfo

amule_get_serverinfo($rst): Request contents of server info if $rst = "0", resets contents otherwise

amule_do_ed2k_download_cmd

amule_do_ed2k_download_cmd($link, $cat): Download ed2k link. Params: link, category (default=0)

amule_load_vars

amule_load_vars($name_of_variable): Query aMule datastructure. Result is returned as array of hashes, where each hash represent item in aMule datastructure (upload, download, client, item in search result). Following are possible category names:

  1. 'downloads'
  2. 'uploads'
  3. 'searchresult'
  4. 'shared'
  5. 'servers'
  6. 'stats_graph'
  7. 'stats_tree'
$var = amule_load_vars($name_of_variable)
foreach ($var as $i) {
}

Some info about data structures retrieved via amule_load_vars

downloads: return an array of Object(AmuleDownloadFile). The fields of this are:

  1. name (full name with extension)
  2. short_name (the full name trucated after some chars and ended with three periods)
  3. hash (univoque id of the file, is used for other commands)
  4. progress
  5. link (full ed2k link, be aware: it included you ip as sources)
  6. category (id of the category of the file, where 0 = all, 1 = first category, etc.. see amule_get_categories)
  7. status (where 7=stopped / paused, for other status valute the transfer rate or src_count_xfer, if it's 0 status=waiting, else downloading'. Actually I don't know how distinguish from 'stopped' and 'paused')
  8. size (in byte)
  9. size_done (completed bytes)
  10. size_xfer (transfered bytes)
  11. speed (actually download speed, in bytes/sec)
  12. src_count (total number of source found)
  13. src_count_not_curr (number of sources not fully identified and connected to, so for total of valid source you use src_count - src_count_not_curr)
  14. src_count_a4af (sources not used because used for another file)
  15. src_count_xfer (source transfering in this moment)
  16. prio (priority of downloading where 0 => "Low", 1 => "Normal", 2 => "High", 3 => "Very high", 4 => "Very low", 5=> "Auto", 6 => "Powershare")
  17. prio_auto (boolean, 0 = false, 1 = true)
  18. last_seen_complete show last seen date / time, timestamp format

uploads: return an array of Object(AmuleUploadFile). The fields of this are:

  1. name (full file name with extension)
  2. short_name (the full name trucated after some chars and ended with three periods)
  3. xfer_up (total of uploaded bytes of this file to this client)
  4. xfer_down (total of downloaded bytes of this file from this client)
  5. xfer_speed (actual upload speed of this file to this client, in bytes/sec)

shared: returns an array of Object(AmuleSharedFile). The fields of this are:

  1. name
  2. short_name:
  3. hash:
  4. size
  5. link
  6. xfer
  7. xfer_all
  8. req
  9. req_all
  10. accept:
  11. accept_all:
  12. prio
  13. prio_auto:

searchresult: returns an array of Object(AmuleSearchResult). The fields of this are:

  1. name
  2. short_name
  3. hash
  4. size
  5. sources
  6. present

servers:

  1. name
  2. desc
  3. addr
  4. users
  5. ip
  6. port
  7. maxusers
  8. files

stats_graph: returns stats graphs images (?)

stats_tree: returns stats tree (?)

amule_get_version

amule_get_version(): Returns version number string.

Authentication

Real PHP have wide range of authentication and session management possibilities. In aMuleWeb PHP, status is different. Both authentication and session management are not part of scripting language. Instead, they are implemented as part of in webserver itself, and running invisibly for script engine.

Errors and debugging

As in any other language, aMuleWeb PHP have 3 types of errors: compile-time, run-time and logic errors. All error messages produced by engine are printed to terminal. This effectively means, that aMuleWeb should be started manually from terminal (and not automatically by aMule) in order to see those messages. Alghouth engine perform some effort to recover from syntax errors, it's known to crash in some corner-cases. Beware.

  • Compile-time errors means error in script syntax as misspelled names, missing ";" or mismatched (). In this case, error message from parser is printed and execution is not performed.
  • Run-time errors like division by zero, incorrect or missing parameters etc. When encountered, appropriate message is printed, and execution aborted in point of problem.
  • Logic errors: everything is OK, but result is different from expected. Since aMuleWeb PHP offers no debugger, the only way to trace program is to add var_dump() in suspicious places;

It is possible to compile aMuleWeb PHP engine standalone. This compilation mode is used to debug engine itself. Since aMule binding is not possible in this case, it have little use for template debugging.