| [23] | 1 | <?php |
|---|
| 2 | |
|---|
| 3 | header("Access-Control-Allow-Origin: *"); |
|---|
| 4 | #namespace ns; |
|---|
| 5 | |
|---|
| 6 | require_once( dirname(__FILE__) . '/func.php'); |
|---|
| 7 | |
|---|
| 8 | require_once( dirname(__FILE__) . '/NorthernCross/ns/PathItem.php'); |
|---|
| 9 | require_once( dirname(__FILE__) . '/NorthernCross/ns/PathInfo.php'); |
|---|
| 10 | require_once( dirname(__FILE__) . "/spyc-0.5/spyc.php"); // ★の部分にはspyc.phpを置いたパスを入れます |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | function get_url_data_array() |
|---|
| 14 | { |
|---|
| 15 | $yaml= spyc_load_file( dirname(__FILE__) . "/../settings.yml");// ☆の部分にはyamlファイルがあるパスを入れます |
|---|
| 16 | return $yaml; |
|---|
| 17 | } |
|---|
| 18 | |
|---|
| 19 | function get_url_data_array0() |
|---|
| 20 | { |
|---|
| 21 | $array = array( |
|---|
| 22 | array('url' => 'https://toppers.jp', 'path' => '/var/www/vhosts/devconf.northern-cross.info/httpdocs/a/tmp/devconf-guest-4/tmp/md2site-x/_xtest/wtjx/t12/src') ); |
|---|
| 23 | |
|---|
| 24 | return $array; |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| 27 | function get_url_data( $num ){ |
|---|
| 28 | $array = get_url_data_array(); |
|---|
| 29 | return $array[$num]; |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | function dump_url_data( $num ){ |
|---|
| 33 | print_r( get_url_data( $num )); |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | function get_path_from_url_data( $num ){ |
|---|
| 37 | return get_url_data( $num )['path']; |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | function get_path_list_by_url( $num ){ |
|---|
| 41 | $pattern = "*.md"; |
|---|
| 42 | $top_path = get_path_from_url_data($num); |
|---|
| 43 | $cmd_line_items = array("find" , $top_path , "-name", $pattern); |
|---|
| 44 | $cmd_line = implode(" " , $cmd_line_items); |
|---|
| 45 | |
|---|
| 46 | $array = array(); |
|---|
| 47 | $handle = popen( $cmd_line , "r"); |
|---|
| 48 | while (( $buffer = fgets($handle, 4096)) !== false){ |
|---|
| 49 | $line = trim($buffer); |
|---|
| 50 | $data = explode($top_path, $line)[1]; |
|---|
| 51 | array_push($array , $data); |
|---|
| 52 | } |
|---|
| 53 | if (!feof($handle)) { |
|---|
| 54 | echo "Error: unexpected fgets() fail\n"; |
|---|
| 55 | } |
|---|
| 56 | pclose($handle); |
|---|
| 57 | |
|---|
| 58 | return array( "top_path" => $top_path, "file_list" => $array); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | function dump_hier_list($pathinfo){ |
|---|
| 63 | foreach( array_keys($pathinfo->hier_hash) as $v ){ |
|---|
| 64 | $v2 = trim($v); |
|---|
| 65 | if(is_null($v2)){ |
|---|
| 66 | echo "found null\n"; |
|---|
| 67 | exit(0); |
|---|
| 68 | } |
|---|
| 69 | echo $v . "||(" . strlen($v) . ")\n"; |
|---|
| 70 | } |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | function dump_hier($pathinfo) { |
|---|
| 74 | foreach( array_keys($pathinfo->hier_hash) as $v ){ |
|---|
| 75 | echo $v . "\n"; |
|---|
| 76 | $item = $pathinfo->hier_hash[$v]; |
|---|
| 77 | $item->dump(); |
|---|
| 78 | echo "--dump_children\n"; |
|---|
| 79 | $item->dump_children(); |
|---|
| 80 | echo "#==========\n"; |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | function get_path_info( $num ){ |
|---|
| 85 | $path_info = get_path_list_by_url( $num ); |
|---|
| 86 | $pathinfo = new ns\PathInfo($path_info["top_path"]); |
|---|
| 87 | # $pathinfo = new PathInfo($path_info["top_path"]); |
|---|
| 88 | foreach($path_info["file_list"] as $path){ |
|---|
| 89 | $pathinfo->register($path); |
|---|
| 90 | } |
|---|
| 91 | return $pathinfo->pathinfo_list_in_json(); |
|---|
| 92 | # $pathinfo->dump(); |
|---|
| 93 | # $pathinfo->dump_root(); |
|---|
| 94 | # dump_hier_list($pathinfo); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | function print_get_path_info( $num ){ |
|---|
| 98 | echo get_path_info( $num ); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | function ensure_dir( $dir ) { |
|---|
| 102 | if( !file_exists( $dir ) ){ |
|---|
| 103 | mkdir( $dir ); |
|---|
| 104 | } |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | function ensure_storage_dir( $storage_dir , $num, $filename ) { |
|---|
| 108 | $dir = implode( "/" , array( $storage_dir , $num) ); |
|---|
| 109 | mkdir($dir); |
|---|
| 110 | $path = implode( "/" , array( $dir , $filename) ); |
|---|
| 111 | ensure_dir( $dir ); |
|---|
| 112 | |
|---|
| 113 | return $path; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | function get_content( $num , $path ) { |
|---|
| 117 | $top_dir = get_path_from_url_data( $num ); |
|---|
| 118 | $full_path = implode( '/', [$top_dir, $path]); |
|---|
| 119 | $result = input_file( $full_path ); |
|---|
| 120 | return $result; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | function cmdx(){ |
|---|
| 124 | $filelist_filename = "filelist.json"; |
|---|
| 125 | $storage_dir = dirname(__FILE__) . '/../storage'; |
|---|
| 126 | |
|---|
| 127 | $content = NULL; |
|---|
| 128 | if (isset($_GET['cmd'])) { |
|---|
| 129 | $cmd = $_GET['cmd']; |
|---|
| 130 | if (isset($_GET['num'])) { |
|---|
| 131 | $num = $_GET['num']; |
|---|
| 132 | |
|---|
| 133 | switch ($cmd) { |
|---|
| 134 | case 'get_content': |
|---|
| 135 | if (isset($_GET['path'])) { |
|---|
| 136 | $path = $_GET['path']; |
|---|
| 137 | $content = get_content( $num , $path ); |
|---|
| 138 | } |
|---|
| 139 | break; |
|---|
| 140 | case 'update_filelist': |
|---|
| 141 | $path = ensure_storage_dir( $storage_dir , $num, $filelist_filename ); |
|---|
| 142 | $content = get_path_info( $num ); |
|---|
| 143 | output_file( $path , $content ); |
|---|
| 144 | break; |
|---|
| 145 | case 'get_filelist': |
|---|
| 146 | $path = ensure_storage_dir( $storage_dir , $num, $filelist_filename ); |
|---|
| 147 | if( file_exists($path) ){ |
|---|
| 148 | $content = input_file( $filename ); |
|---|
| 149 | } |
|---|
| 150 | else { |
|---|
| 151 | $content = get_path_info( $num ); |
|---|
| 152 | output_file( $path , $content ); |
|---|
| 153 | } |
|---|
| 154 | break; |
|---|
| 155 | default: |
|---|
| 156 | break; |
|---|
| 157 | } |
|---|
| 158 | } |
|---|
| 159 | } |
|---|
| 160 | elseif (isset($_POST['cmd'])) { |
|---|
| 161 | $cmd = $_POST['cmd']; |
|---|
| 162 | if (isset($_POST['num'])) { |
|---|
| 163 | $num = $_POST['num']; |
|---|
| 164 | |
|---|
| 165 | switch ($cmd) { |
|---|
| 166 | case 'upload_content': |
|---|
| 167 | $array = get_lines_from_formdata( "mytext" ); |
|---|
| 168 | $upload_content = implode( "\n" , $array ); |
|---|
| 169 | if (isset($_POST['path'])) { |
|---|
| 170 | $path = $_POST['path']; |
|---|
| 171 | $top_path = get_path_from_url_data( $num ); |
|---|
| 172 | $filename_abs_path = $top_path . DIRECTORY_SEPARATOR . $path; |
|---|
| 173 | |
|---|
| 174 | output_file( $filename_abs_path , $upload_content ); |
|---|
| 175 | $content = "P-UPLOAD_CONTENT-OK=" . $filename_abs_path; |
|---|
| 176 | } |
|---|
| 177 | else{ |
|---|
| 178 | $content = "P-UPLOAD_CONTENT-NO-PATH"; |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | default: |
|---|
| 182 | debug_x("P-DEFAULT"); |
|---|
| 183 | /* $content = "P-DEFAULT";*/ |
|---|
| 184 | break; |
|---|
| 185 | } |
|---|
| 186 | } |
|---|
| 187 | else{ |
|---|
| 188 | $content = "P-NO_NUM"; |
|---|
| 189 | } |
|---|
| 190 | } |
|---|
| 191 | else{ |
|---|
| 192 | debug_x('DEFAULT'); |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | if(!is_null($content)){ |
|---|
| 196 | echo( $content ); |
|---|
| 197 | } |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | function debug_x($mes) { |
|---|
| 201 | ob_start(); |
|---|
| 202 | echo $mes . "\n"; |
|---|
| 203 | var_dump($_GET); |
|---|
| 204 | var_dump($_POST); |
|---|
| 205 | ob_end_clean(); |
|---|
| 206 | $content = ob_get_contents(); |
|---|
| 207 | error_log($content,3, './debug.txt'); |
|---|
| 208 | } |
|---|
| 209 | #print_get_path_info( 0 ); |
|---|
| 210 | #print_get_path_info( 1 ); |
|---|
| 211 | cmdx(); |
|---|
| 212 | |
|---|
| 213 | ?> |
|---|