Help for developers

How to write custom command * Hello World * Input processing * Command chaining * Operations with files * Operations with strings * Operations with groups

How to write custom command

Hello World

The principle is simple! You just sent text on the output. The command Hellow World looks like this: namespace ku; function hw__ ($shell_option, $shell_data, $input, $step, $echo) { $return["output"] = "Hello World!"; return $return; } The name of the function is always followed by two underscores. This enables use of reserved words (keywords) for function names. The file is then saved into the directory /bin/commands here without underscores in this case the full path of the file is /bin/commands/hw.php

Input processing

Input processing from a command (link). Example: http://domain.tld?shell=square 3 The code processes output of the command and returns the square of the number: namespace ku; function square__ ($shell_option, $shell_data, $input, $step, $echo) { $return["output"] = $shell_data * $shell_data; return $return; } The string following the command is stored in the variable $shell_data But if we write in exapmle this: Example http://domain.tld?shell=square -h Therefore the dash is on the beginning, data will be stodred in the variable $shell_option Example with square and help function: namespace ku; function square__ ($shell_option, $shell_data, $input, $step, $echo) { if ($shell_option == "h") { $return["output"] = "Insert a number and the command will return this number squared."; } elseif (!$shell_option) { $return["output"] = $shell_data * $shell_data; } else { $return["output"] = "Unknown option"; } return $return; } If we input a positive number, its square is returned. if we input -h, the help is shown. and if we input any other option than -h, the command will return error message. The variable $return["step"] is used. If we neeed more steps for example login We add new step in the variable $return["step"] in example: ... $return["output"] = "first_step"; $return["step"] = "second_step"; return $return; ... The variable $return["echo"] is currently not used, it was created to mute output of commands in a case of need.

Command chaining

To enable command chaining with sending outputs of one command into input of another command the pipe is used: | Example http://domain.tld?shell=square 3 | square Output of previous command is returned in the variable $input. We will change our example square again. This time if there is an output of the first command, the square of previus square will be created: namespace ku; function square__ ($shell_option, $shell_data, $input, $step, $echo) { if ($shell_option == "h") { $return["output"] = "Insert a number and the command will return this number squared."; } elseif (!$shell_option) { if ($input) { $return["output"] = $input * $input; } else { $return["output"] = $shell_data * $shell_data; } } else { $return["output"] = "Unknown option."; } return $return; }

Operations with files

Commands starting with X_ are used to operate with files and rights safely. These functions have automatic controll of access rights // chmod with test whether the rights really changed function X_chmod ($file_name, $permissions) { // Echo s conversion of <> into HTML entities function X_echo ($string) { // Test whether the file exists function X_file_exists ($file_name) { // Returns the content of file with a test whether the file was loaded function X_file_get_contents ($file_name) { // fopen wit thes whether the file was opened function X_fopen ($file_name, $mode) { // Is the file an audio file? function X_is_audio ($file_name) { // Is it a directory? function X_is_dir ($path) { // Is it a file? function X_is_file ($path) { // Is it an image? function X_is_image ($file_name) { // Is a file in KUF format? function X_is_kuf ($file_name) { // Test whether I have rights to read function X_is_readable ($file_name) { // Is file a video file? function X_is_video ($file_name) { // Test wheter I have rights to write into a file function X_is_writable ($file_name) { // Creates a directory and control wheter the directory was created function X_mkdir ($dir_name) { //Do we have rights to read a file or a directory? function X_perm_read ($file_name) { // Tests persmission rights function X_perm_right ($permissions) { // Do we have rights to write a file or a directory? function X_perm_write ($file_name) { // Scans directory with a test of perpission rights function X_scandir ($directory) { // Scans directory without files or with a dot at the beginning // by the time of creation function X_scandir_by_mtime ($directory) { // Scans directories recursively function X_scandir_recursive ($directory) { // Returns how many bytes the directory has (recursively) function dir_size_rec ($directory) { // Returns whether we have rights to move or delete a file function file_allowed_owner ($file) { // Returns the content of KUF file in an associative array function get_ku_data ($file_name) { // Returns mime type of a file function get_mime ($file_name) { // Returns absolute path to a file function get_path ($string) { // Edits the string with a path // Removes /./ and /../ // Example /1/2/../3 changes into /1/3 a path /./1 into /1 function get_real_path ($string) { // Returns free space in the home directory function home_free () { // Resamples an image in commands 'cat' and 'ls' function resample_image ($image_file, $shell_data) { // Works similarly as 'resample_image' function resample_image_in_text ($output, $shell_data) { // Saves the log function save_log ($path, $mark) {

Operations with strings

// Adds backslash afore the slash function add_back_slash ($string) { // Converts bytes into kilobytes, megabytes, gigabytes and terabytes // options are B, K, M, G a T. // If we input human, byty are converted int human readable format function bytes_to_units ($bytes, $unit) { // Returns bytes in human readable format function human_bytes ($bytes) { // Returns seconds in format Hours:minutes:seconds function human_seconds ($seconds) { // Converts bytes in human readable format into bytes function human_to_bytes ($from) { // If a string is IPv4 format returns 4 // If a string is IPv6 format returns 6 // Else returns FALSE function is_ip ($ip) { // Saves asssociative array into a file in KUF format function put_ku_data ($file_name, $kuf_content) { // Returns random string containg chars 0-9 a-z A-Z function random_string ($length) { // Removes diacritics function rm_diacritics ($string) { // Removes diacritics and symbols function rm_diacritics_symbols ($string) { // Validates e-mail address function validate_email ($email) {

Operations with groups

// Does this group exist? function group_exist ($group) { // Does the name of a group contains only allowed signs? function group_name_right ($group_name) { // Does this user exist? function user_exists ($user) { // Is the logged user the owner of the group? function user_is_owner_group ($group) { // Does the logged user have pesmission to modify the group? function user_is_perm_group ($group) { // Is a user member of a group? function user_in_group ($group, $user) { The english version written by : Miriam from Licence GPLv2+: GNU GPL version 2 or the later