268 lines
		
	
	
		
			11 KiB
		
	
	
	
		
			PHP
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			268 lines
		
	
	
		
			11 KiB
		
	
	
	
		
			PHP
		
	
	
		
			Executable File
		
	
	
	
	
<?php
 | 
						|
 | 
						|
    class WMarkDown{
 | 
						|
 | 
						|
        private static $default_settings = [
 | 
						|
            "default_value" => null,
 | 
						|
            "nulls" => true,
 | 
						|
            "default_settings_files" => __DIR__ . "/../JSON/WMarkDown.settings.json"
 | 
						|
        ];
 | 
						|
        private static $extension_mime = [];
 | 
						|
        private static $mime_extension = [];
 | 
						|
        private $custom_settings = [];
 | 
						|
        private $input;
 | 
						|
        private $wmarkdown_root = __DIR__ . "/..";
 | 
						|
        private $root = __DIR__ . "/..";
 | 
						|
        private $documentation_path = null;
 | 
						|
        private $html_files = [];
 | 
						|
        private $html_base = '';
 | 
						|
        private $public_path = __DIR__ . "/../Public";
 | 
						|
        private $hashes = [];
 | 
						|
        private $documentation_path_relative = null;
 | 
						|
        private $hash_alphabet = null;
 | 
						|
        private $data_created = [
 | 
						|
            "files" => [],
 | 
						|
            "directories" => []
 | 
						|
        ];
 | 
						|
        private $ignore_script_paths = [];
 | 
						|
        public static $string_to_pattern_special = [
 | 
						|
            "\n" => "n",
 | 
						|
            "\r" => "r",
 | 
						|
            "\t" => "t"
 | 
						|
        ];
 | 
						|
 | 
						|
        private function get_value($name, $nulls){
 | 
						|
            foreach([$this->input, $this->custom_settings, self::$default_settings] as $input)
 | 
						|
                if($input && isset($input[$name]) && ($nulls || $input[$name] !== null))
 | 
						|
                    return $input[$name];
 | 
						|
            return $this->get_value("default_value");
 | 
						|
        }
 | 
						|
 | 
						|
        private function default_value($default, $nulls){
 | 
						|
            if($nulls || $default !== null)
 | 
						|
                return $default;
 | 
						|
            return $this->get_value("default_value");
 | 
						|
        }
 | 
						|
 | 
						|
        public static function is_dictionary($value){
 | 
						|
            return is_array($value) && array_values($value) != $value;
 | 
						|
        }
 | 
						|
 | 
						|
        public function settings($names = null, $inputs = null, $default = null, $nulls = null){
 | 
						|
 | 
						|
            if(!is_bool($nulls))
 | 
						|
                $nulls = $this->get_value("nulls", false);
 | 
						|
 | 
						|
            if(!$names)
 | 
						|
                return $this->default_value($default, $nulls);
 | 
						|
 | 
						|
            if(!is_array($names))
 | 
						|
                $names = [$names];
 | 
						|
 | 
						|
            foreach(array_merge($inputs ? (is_dictionary($inputs) ? [$inputs] : (is_array($inputs) ? $inputs : [])) : [], [$this->input, $this->custom_settings, self::$default_settings]) as $input)
 | 
						|
                if(self::is_dictionary($input))
 | 
						|
                    foreach($names as $name)
 | 
						|
                        if($name && isset($input[$name]) && ($nulls || $input[$name] !== null))
 | 
						|
                            return $input[$name];
 | 
						|
            return $this->default_value($default, $nulls);
 | 
						|
        }
 | 
						|
 | 
						|
        public static function string_variables($string, $variables = null, $default = null){
 | 
						|
            return preg_replace_callback('/\{([^\{\}]+)\}/', function($values) use($variables, $default){
 | 
						|
                if(!isset($values[1]) || !$values[1])
 | 
						|
                    return $values[0];
 | 
						|
                if($variables && is_array($variables) && isset($variables[$values[1]]))
 | 
						|
                    return $variables[$values[1]];
 | 
						|
                if($default !== null)
 | 
						|
                    return $default;
 | 
						|
                return $values[0];
 | 
						|
            }, $string);
 | 
						|
        }
 | 
						|
 | 
						|
        private static function remove_directory($path){
 | 
						|
            return;
 | 
						|
 | 
						|
            if(file_exists($path)){
 | 
						|
                if(is_dir($path)){
 | 
						|
                    foreach(scandir($path) as $item){
 | 
						|
                        if(is_dir($file = $path . "/" . $item))
 | 
						|
                            self::remove_directory($file);
 | 
						|
                        else
 | 
						|
                            unlink($file);
 | 
						|
                    };
 | 
						|
                    rmdir($path);
 | 
						|
                }else
 | 
						|
                    unlink($path);
 | 
						|
            };
 | 
						|
 | 
						|
        }
 | 
						|
 | 
						|
        private function remove_current_html_files(){
 | 
						|
 | 
						|
            if(!file_exists($files = $this->root . $this->settings("html_files")))
 | 
						|
                return;
 | 
						|
 | 
						|
            $data = json_decode(file_get_contents($files), true);
 | 
						|
 | 
						|
            foreach($data["files"] as $file)
 | 
						|
                if(file_exists($file))
 | 
						|
                    unlink($file);
 | 
						|
            foreach($data["directories"] as $directory)
 | 
						|
                if(file_exists($directory))
 | 
						|
                    self::remove_directory($directory);
 | 
						|
 | 
						|
        }
 | 
						|
 | 
						|
        public static function save_file($path, $content){
 | 
						|
 | 
						|
            $directory = preg_replace('/^(\/?(([^\/]+\/)*))?\/?[^\/]+$/', "$1", $path);
 | 
						|
 | 
						|
            if(!file_exists($directory))
 | 
						|
                mkdir($directory, 0777, true);
 | 
						|
            file_put_contents($path, $content);
 | 
						|
 | 
						|
        }
 | 
						|
 | 
						|
        private function create_html_files($path = ""){
 | 
						|
 | 
						|
            if(file_exists($absolute = $this->documentation_path . $path))
 | 
						|
                foreach(scandir($absolute) as $file){
 | 
						|
                    if(in_array($file, [".", ".."]))
 | 
						|
                        continue;
 | 
						|
                    if(is_dir($directory = $absolute . "/" . $file))
 | 
						|
                        $this->create_html_files($this->data_created["directories"][] = $path . "/" . $file);
 | 
						|
                    elseif(preg_match('/^(((?!\.w).)+)(\.w)?\.md$/', $file, $matches))
 | 
						|
                        self::save_file(
 | 
						|
                            $this->data_created["files"][] = $this->public_path . $path . "/" . $matches[1] . ".html",
 | 
						|
                            self::string_variables($this->html_base, array_merge(WMarkDown\Modules::format($this, file_get_contents($absolute . "/" . $file))))
 | 
						|
                        );
 | 
						|
                };
 | 
						|
 | 
						|
        }
 | 
						|
 | 
						|
        private function request($parameters){
 | 
						|
 | 
						|
            header("content-type: application/json");
 | 
						|
            echo json_encode([
 | 
						|
                "ok" => true,
 | 
						|
                "code" => 200,
 | 
						|
                "content" => null
 | 
						|
            ]);
 | 
						|
 | 
						|
        }
 | 
						|
 | 
						|
        public function get_ignore_file_paths(){return array_merge([], $this->ignore_script_paths);}
 | 
						|
 | 
						|
        public function __construct($input = null){
 | 
						|
 | 
						|
            if(is_array($input))
 | 
						|
                $this->input = $input;
 | 
						|
 | 
						|
            foreach(["default_settings_files", "settings_files"] as $name)
 | 
						|
                if($files = $this->settings($name))
 | 
						|
                    foreach(is_array($files) ? $files : [$files] as $path)
 | 
						|
                        if($path && file_exists($path) && ($data = file_get_contents($path)) && ($json = @json_decode($data, true)))
 | 
						|
                            foreach($json as $key => $value)
 | 
						|
                                $this->custom_settings[$key] = $value;
 | 
						|
 | 
						|
            $post_key = $this->settings("variable_name");
 | 
						|
 | 
						|
            if(isset($_POST[$post_key])){
 | 
						|
                $this->request(json_decode(base64_decode($_POST[$post_key]), true));
 | 
						|
                return;
 | 
						|
            };
 | 
						|
 | 
						|
            $this->wmarkdown_root = $this->settings("root");
 | 
						|
            $this->root = $this->settings("root");
 | 
						|
            $this->documentation_path_relative = $this->settings("documentation_path");
 | 
						|
            $this->documentation_path = $this->root . $this->documentation_path_relative;
 | 
						|
            $this->public_path = $this->root . $this->settings("public_path");
 | 
						|
            $this->hash_alphabet = $this->settings("hash_alphabet");
 | 
						|
 | 
						|
            foreach($this->settings("ignore_script_paths") ?? [] as $path)
 | 
						|
                $this->ignore_script_paths[] = $path;
 | 
						|
 | 
						|
            switch($this->settings("action")){
 | 
						|
                case "update_scripts":
 | 
						|
                    (new \WMarkDown\ScriptsAnalyzer($this))->update_files();
 | 
						|
                break;
 | 
						|
                case "build_html":
 | 
						|
                default:
 | 
						|
                    $this->hash_alphabet = str_split(preg_replace('/[^a-z\d_]/i', "", is_array($this->hash_alphabet) ? implode("", $this->hash_alphabet) : $this->hash_alphabet));
 | 
						|
 | 
						|
                    if(file_exists($html_base_path = $this->settings("html_base")))
 | 
						|
                        $this->html_base = file_get_contents($html_base_path);
 | 
						|
 | 
						|
                    // $this->remove_current_html_files();
 | 
						|
                    $this->create_html_files();
 | 
						|
                    // print_r([$this->root . $this->settings("html_files"), $this->data_created]);
 | 
						|
                    $this->save_file($this->root . $this->settings("html_files"), json_encode($this->data_created));
 | 
						|
                break;
 | 
						|
            };
 | 
						|
 | 
						|
        }
 | 
						|
 | 
						|
        public function get_root(){return "" . $this->root;}
 | 
						|
        public function get_wmarkdown_root(){return "" . $this->wmarkdown_root;}
 | 
						|
 | 
						|
        public function get_hash(){
 | 
						|
 | 
						|
            $hash;
 | 
						|
            $l = count($this->hash_alphabet) - 1;
 | 
						|
            $length = $this->settings("hash_length");
 | 
						|
 | 
						|
            do{
 | 
						|
                $hash = "";
 | 
						|
                while(strlen($hash .= $this->hash_alphabet[rand(0, $l)]) < $length);
 | 
						|
            }while(
 | 
						|
                in_array($hash, $this->hashes) ||
 | 
						|
                preg_match('/^\d/', $hash)
 | 
						|
            );
 | 
						|
            $this->hashes[] = $hash;
 | 
						|
 | 
						|
            return $hash;
 | 
						|
        }
 | 
						|
 | 
						|
        public function load_file($path){
 | 
						|
            foreach(["", $this->root, $this->wmarkdown_root] as $root)
 | 
						|
                if(file_exists($file = $root . $path) && !is_dir($file))
 | 
						|
                    return file_get_contents($file);
 | 
						|
            return null;
 | 
						|
        }
 | 
						|
 | 
						|
        public function get_documentation_path(){return "" . $this->documentation_path_relative;}
 | 
						|
 | 
						|
        public function get_mime($path){
 | 
						|
            if(!$path || !preg_match('/^.+\.([^\.\/]+)$/', $path, $matches))
 | 
						|
                return $this->settings("default_mime");
 | 
						|
 | 
						|
            $extension = $matches[1];
 | 
						|
 | 
						|
            if(empty(self::$extension_mime))
 | 
						|
                self::$extension_mime = json_decode($this->load_file($this->settings("extension_mime_file")), true);
 | 
						|
 | 
						|
            if(isset(self::$extension_mime[$extension]))
 | 
						|
                return self::$extension_mime[$extension];
 | 
						|
            return $this->settings("default_mime");
 | 
						|
        }
 | 
						|
 | 
						|
        public function get_extensions($mime){
 | 
						|
            if(!$mime)
 | 
						|
                return $this->settings("default_extension");
 | 
						|
 | 
						|
            if(empty(self::$mime_extension))
 | 
						|
                self::$mime_extension = json_decode($this->load($this->settings("mime_extension_file")), true);
 | 
						|
 | 
						|
            if(isset(self::$mime_extension[$mime]))
 | 
						|
                return self::$mime_extension[$mime];
 | 
						|
            return $this->settings("default_extension");
 | 
						|
        }
 | 
						|
 | 
						|
        public static function string_to_pattern($string){
 | 
						|
            return preg_replace_callback('/[\r\n\t\.\-\:\{\[\(\)\]\}\#\@\|\!\?\*\+\^\\\\\/\&\%\~]/', function($values){
 | 
						|
                return "\\" . (isset(\WMarkDown::$string_to_pattern_special[$values[0]]) ? \WMarkDown::$string_to_pattern_special[$values[0]] : $values);
 | 
						|
            });
 | 
						|
        }
 | 
						|
 | 
						|
    };
 |