commit 0c6ddd06bba52c0ff780a7b3983cfeef57903109 Author: Antonio de la Rosa Date: Thu Apr 28 14:52:30 2022 +0200 Added files to initial project diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cc5a674 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +*~ +composer.json +composer.lock +vendor/* +!modules/welcome +settings/config.php diff --git a/application/index.php b/application/index.php new file mode 100644 index 0000000..9fa7178 --- /dev/null +++ b/application/index.php @@ -0,0 +1,6 @@ + /module +// Second deep element in path modules/{module}/controllers/{controller}.php -> /module/controller +// Third deep element and more in path are variables into controller function +// Filter the elements path with slugify function. + +Utils::load_config('settings/config'); + +if($path_info!='') { + + $arr_path=explode('/', $path_info); + + if($arr_path[count($arr_path)-1]=='') { + + unset($arr_path[count($arr_path)-1]); + + } + + unset($arr_path[0]); + + foreach($arr_path as $k => $info) { + + $arr_path[$k]=Utils::slugify($info); + } + + switch (count($arr_path)) { + + case 0: + + $controller='modules/'.Config::$home_module.'/controllers/index.php'; + + get_controller($controller, []); + + break; + + case 1: + + $controller='modules/'.$arr_path[1].'/controllers/index.php'; + + get_controller($controller, []); + + break; + + case 2: + + $controller='modules/'.$arr_path[1].'/controllers/'.$arr_path[2].'.php'; + + get_controller($controller, []); + + break; + + default: + + $controller='modules/'.$arr_path[1].'/controllers/'.$arr_path[2].'.php'; + + $args=array_slice($arr_path, 2, count($arr_path)); + + get_controller($controller, $args); + + break; + + } + +} +else { + + $controller='modules/'.Config::$home_module.'/controllers/index.php'; + + get_controller($controller, []); + +} + +ob_end_flush(); + +function get_controller($controller, $args=[]) { + + if(is_file($controller)) { + + include($controller); + + echo call_user_func_array('controller', $args); + + } + else { + + echo 'Page not found'; + + http_response_code(404); + + exit(1); + + } + +} diff --git a/libraries/Utils.php b/libraries/Utils.php new file mode 100644 index 0000000..b73157d --- /dev/null +++ b/libraries/Utils.php @@ -0,0 +1,708 @@ +`çÇ{}@~=^:´[]'; + $to= 'aaaaaaaceeeeiiiidoooooouuuybyrnAAAAAACEEEEIIIIDOOOOOOUUUYYYBRN---------------------------------'; + + if($replace_dot==1) + { + + $from.='.'; + $to.='-'; + + } + + if($replace_barr==1) + { + + $from.="/"; + $to.="-"; + + } + + $text = utf8_decode(urldecode($text)); + + $text=trim(str_replace(" ", $replace_space, $text)); + + $text = strtr($text, utf8_decode($from), $to); + + //Used for pass base64 via GET that use upper, for example. + + if($respect_upper==0) + { + + $text = strtolower($text); + + } + + $text=trim(preg_replace('~'.$replace_space.'+~', $replace_space, $text), '-'); + + $text=preg_replace('~[^-\w]+~', '', $text); + + return utf8_encode($text); + + } + + /** + * This function is used to clean up the text of undesirable elements + * @param string $text Text to clean + * @param string $br Boolean variable used for control if you want br tags or \n symbon on input text + */ + + static public function form_text( $text ,$br=1) + { + + settype( $text, "string" ); + + $text = trim( $text ); + + $arr_tags=array('//', '/"/', '/\'/', "/ /"); + $arr_entities=array('<', '>', '"', ''', ' '); + + if($br==1) + { + + $text = preg_replace($arr_tags, $arr_entities, $text); + + $arr_text = explode("\n\r\n", $text); + + $c=count($arr_text); + + if($c>1) + { + for($x=0;$x<$c;$x++) + { + + $arr_text[$x]='

'.trim($arr_text[$x]).' 

'; + + } + } + + + $text=implode('', $arr_text); + + $arr_text = explode("\n", $text); + + $c=count($arr_text); + + if($c>1) + { + for($x=0;$x<$c;$x++) + { + + $arr_text[$x]=trim($arr_text[$x]).'
'; + + } + } + + $text=implode('', $arr_text); + + } + + + $text = Utils::make_slashes( $text ); + + return $text; + + } + + /** + * Function used for add slashes from _POST and _GET variables. + * + * + * @param string $string String for add slashes + */ + + static public function make_slashes( $string ) + { + return addslashes( $string ); + } + + /** + * Function used for strip slashes from _POST and _GET variables. + * + * + * @param string $string String for strip slashes + */ + + static public function unmake_slashes( $string ) + { + return stripslashes( $string ); + } + + /** + * This function is used to clean up the text of undesirable html tags + * + * @param string $text Input text for clean undesirable html tags + * @param array $allowedtags An array with allow tags on the text. + */ + + static public function form_text_html( $text , $allowedtags=array()) + { + + settype( $text, "string" ); + + //If no html editor \r\n=

+ + /*$text=preg_replace("//", "\n", $text);*/ + + /*if(Utils::$textbb_type!='') + { + + $text=str_replace("\r", '', $text); + $text=str_replace("\n", '', $text); + + } + else + { + + //Make

+ + $arr_text = explode("\n\r\n", $text); + + $c=count($arr_text); + + if($c>1) + { + for($x=0;$x<$c;$x++) + { + + $arr_text[$x]='

'.trim($arr_text[$x]).' 

'; + + } + } + + + $text=implode('', $arr_text); + + $arr_text = explode("\n", $text); + + $c=count($arr_text); + + if($c>1) + { + for($x=0;$x<$c;$x++) + { + + $arr_text[$x]=trim($arr_text[$x]).'
'; + + } + } + + $text=implode('', $arr_text); + + }*/ + /*echo htmlentities($text); + die;*/ + + //Check tags + + //Bug : tags deleted ocuppied space. + + //First strip_tags + + $text = trim( $text ); + + //Trim html + + /*$text=str_replace(' ', ' ', $text); + + while(preg_match('/

\s+<\/p>$/s', $text)) + { + + $text=preg_replace('/

\s+<\/p>$/s', '', $text); + + }*/ + + //Now clean undesirable html tags + + if(count($allowedtags)>0) + { + + $text=strip_tags($text, '<'.implode('><', array_keys($allowedtags)).'>' ); + + $arr_tags=array('//', '/"/', '/\'/', "/ /"); + $arr_entities=array('<', '>', '"', ''', ' '); + + $text=preg_replace($arr_tags, $arr_entities, $text); + + $text=str_replace(' ', '  ', $text); + + $arr_tags_clean=array(); + $arr_tags_empty_clean=array(); + + //Close tags. + + //Filter tags + + $final_allowedtags=array(); + + foreach($allowedtags as $tag => $parameters) + { + //If mark how recursive, make a loop + + settype($parameters['recursive'], 'integer'); + + $c_count=0; + $x=0; + + if($parameters['recursive']==1) + { + + $c_count = substr_count( $text, '<'.$tag.'>'); + + } + + for($x=0;$x<=$c_count;$x++) + { + + $text=preg_replace($parameters['pattern'], $parameters['replace'], $text); + + } + + $pos_=strpos($tag, '_'); + + if($pos_!==false) + { + + $tag=substr($tag, 0, $pos_); + + } + + $final_allowedtags[]=$tag.'_tmp'; + + //Destroy open tags. + + $arr_tags_clean[]='/<(.*?)'.$tag.'(.*?)>/'; + + $arr_tags_empty_clean[]=''; + $arr_tags_empty_clean[]=''; + + } + + $text=preg_replace($arr_tags_clean, $arr_tags_empty_clean, $text); + } + + //With clean code, modify (.*?)<\/p>/s", "$1\n\r\n", $text ); + $text = str_replace( "
", "", $text ); + + return $text; + + } + + + /** + * A function for generate a rand token used on sessions. + * + * @param string $length_token A variable used for set the character's length the password. More length, password more secure + */ + + static public function get_token($length_token=24) + { + + $rand_prefix=Utils::generate_random_password($length_token); + + return $rand_prefix; + + } + + /** + * Function used for generate a simple random password. Used RamdomLib from Ircmaxell + * + * @param string $length_pass A variable used for set the character's length the password. More length, password more secure + * + */ + + static public function generate_random_password($length_pass=32) + { + + $x=0; + $z=0; + + $abc=array( 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '*', '+', '!', '-', '_', '@', '#', '$'); + + $password_final=''; + + $c=count($abc); + + for($x=0;$x<$length_pass;$x++) { + + $num_element_pass=random_int(0, $c-1); + + $password_final.=$abc[$num_element_pass]; + + } + + /*$password_final=bin2hex(random_bytes(round($length_pass/2))); + + $num_element_pass=random_int(0, 5); + + $abc=['*', '+', '!', '-', '_', '@', '#', '$']; + + for($x=0;$x<$num_element_pass;$x++) { + + $num_change_char=random_int(0, len($abc)); + + }*/ + + //Add strange characters + + return $password_final; + + } + + + /** + * Load libraries, well, simply an alternative include + * + * Very important function used for load the functions and method necessaries on your projects. Is simple, you create a file php and put in a libraries folder. Use the name without php used in file and magically the file is loaded. You can use this function in many places, phango use a little cache for know who file is loaded. + * + * @param string $names The name of php file without .php extension. If you want specific many libraries you can use an array + * @param string $path The base path where search the library if is not in standard path. By default the path is on Utils::$base_path/libraries/ or Utils::$base_path/modules/$module/libraries/ + * @deprecated Use namespaces and composer instead. + */ + + static public function load_libraries($names, $path='') + { + + if(gettype($names)!='array') + { + + $arr_names[]=$names; + + } + else + { + + $arr_names=&$names; + + } + + if($path=='') + { + + $path=Routes::$base_path.'/vendor/'.Routes::$app.'/libraries/'; + + } + else + { + + $path=Routes::$base_path.'/'.$path.'/'; + + } + + + foreach($arr_names as $library) + { + + + if(!isset(Utils::$cache_libraries[$library])) + { + + $old_path=$path; + + if(is_file($path.'/'.$library.'.php')) + { + include($path.$library.'.php'); + + Utils::$cache_libraries[$library]=1; + + } + else + { + //Libraries path + + $path=Routes::$base_path.'/libraries/'; + + if(!include($path.$library.'.php')) + { + + + die(); + + } + else + { + + Utils::$cache_libraries[$library]=1; + + } + + } + + } + + } + + return true; + + } + + /** + * Simple function for replate real quotes for quote html entities. + * + * @param string $text Text to clean. + */ + + static public function replace_quote_text( $text ) + { + + $text = str_replace( "\"", """, $text ); + + return $text; + + } + + /** + * Internal function for set array values without keys inside $array_strip + * + * @param array $array_strip The array with key values for set + * @param array $array_source The array that i want fill with default values + * + */ + + static public function filter_fields_array($array_strip, $array_source) + { + + $array_final=array(); + + if(count($array_strip)>0) + { + foreach($array_strip as $field_strip) + { + + $array_final[$field_strip]=@$array_source[$field_strip]; + + } + + return $array_final; + + } + else + { + + return $array_source; + + } + } + + /** + * Function used for show on stdout a csrf_token used by POST phango controllers for check if is a real POST from phango. + * + * @param string $name_token The name of the token key in session + * @param int $length_token The length of + */ + + static public function set_csrf_key($name_token='csrf_token', $length_token=80) + { + + /*if(!isset($_SESSION['csrf_token'])) + {*/ + + $_SESSION['csrf_token']=Utils::get_token($length_token); + + //} + + return "\n".\PhangoApp\PhaModels\CoreForms::HiddenForm($name_token, '', $_SESSION['csrf_token'])."\n"; + + } + + /** + * Function used for show on stdout a csrf_token used by POST phango controllers for check if is a real POST from phango. + * + */ + + static public function generate_csrf_key($length_token=80) + { + + $_SESSION['csrf_token']=Utils::get_token($length_token); + + return $_SESSION['csrf_token']; + + } + + /** + * Function for load config for modules. + * + * + * @param $name_config Name of the config file, optional. Normally load config.php file on folder config. + * @param $path Path where search the config. + */ + + static public function load_config($name_config, $path="./settings") + { + + //load_libraries(array($name_config), PhangoVar::$base_path.'/modules/'.$module.'/config/'); + + $file=$path.'/'.$name_config.'.php'; + + if(is_file($file) && !isset(Utils::$cache_config[$file])) + { + include($file); + + Utils::$cache_config[$file]=1; + + return true; + } + else + { + + return false; + + } + + } + + /** + * Function for reload config. + * + * @warning WARNING, use this method only if you don't have any alternative + * + * @param $module Name of the module + * @param $name_config Name of the config file, optional. Normally load config.php file on folder config. + */ + + static public function reload_config($name_config, $path="./settings") + { + + //load_libraries(array($name_config), PhangoVar::$base_path.'/modules/'.$module.'/config/'); + + $file=$path.'/'.$name_config.'.php'; + + if(is_file($file)) + { + + $cont_file_config=file_get_contents($file); + + $cont_file_config=str_replace('', '', $cont_file_config); + + if(eval($cont_file_config)===FALSE) + { + + throw new \Exception('Error: cannot reload the config file: '.$file); + + } + + } + + } + + /** + * Method for wrap words of a string + * + * @param string $text The text to wrap + * @param integer $num_words The number of words of returned text + * @param string $text_explain The string used for show that the texts is more longer + * @param boolean $yes_more_ever Boolean used for activate or disable $text_explain if the size of real string is the same of the new string + */ + + static public function wrap_words($text, $num_words, $text_explain='...', $yes_more_ever=0) + { + + $arr_text=explode(' ', $text); + + $final_text=''; + + $total_num_text=count($arr_text); + + if($total_num_text<$num_words) + { + + $num_words=$total_num_text; + + if($yes_more_ever==0) + { + $text_explain=''; + } + + } + + for($x=0;$x<$num_words;$x++) + { + + $arr_final_text[]=$arr_text[$x]; + + } + + return implode(' ', $arr_final_text).' '.$text_explain; + + } + + +} + +?> diff --git a/modules/welcome/controllers/home.php b/modules/welcome/controllers/home.php new file mode 100644 index 0000000..cf3efbe --- /dev/null +++ b/modules/welcome/controllers/home.php @@ -0,0 +1,8 @@ + 'Cool']; + + header('Content-Type: application/json; charset=utf-8'); + + return json_encode($data); + + break; + + } + +}