| 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 | function get_url_data_array() |
|---|
| 13 | { |
|---|
| 14 | $yaml= spyc_load_file( dirname(__FILE__) . "/../settings.yml");// ☆の部分にはyamlファイルがあるパスを入れます |
|---|
| 15 | return $yaml; |
|---|
| 16 | } |
|---|
| 17 | |
|---|
| 18 | function get_url_data_array0() |
|---|
| 19 | { |
|---|
| 20 | $array = array( |
|---|
| 21 | 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') ); |
|---|
| 22 | |
|---|
| 23 | return $array; |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | function get_url_data( $num ){ |
|---|
| 27 | $array = get_url_data_array(); |
|---|
| 28 | return $array[$num]; |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | function dump_url_data( $num ){ |
|---|
| 32 | print_r( get_url_data( $num )); |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | function get_path_from_url_data( $num , $part){ |
|---|
| 36 | switch($part){ |
|---|
| 37 | case 'data': |
|---|
| 38 | $ret = get_url_data( $num )['path']; |
|---|
| 39 | break; |
|---|
| 40 | case 'setting': |
|---|
| 41 | $ret = get_url_data( $num )['setting_path']; |
|---|
| 42 | break; |
|---|
| 43 | case 'output': |
|---|
| 44 | $ret = get_url_data( $num )['output_path']; |
|---|
| 45 | break; |
|---|
| 46 | default: |
|---|
| 47 | //exit(200); |
|---|
| 48 | echo "get_path_from_url_data " . $part . "<br>"; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | return $ret; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | function get_path_list_by_url( $num , $part){ |
|---|
| 55 | $top_path = get_path_from_url_data($num, $part); |
|---|
| 56 | |
|---|
| 57 | switch($part){ |
|---|
| 58 | case 'data': |
|---|
| 59 | $pattern = "*.md"; |
|---|
| 60 | $cmd_line_items = array("find" , $top_path , "-name", $pattern); |
|---|
| 61 | break; |
|---|
| 62 | case 'setting': |
|---|
| 63 | $cmd_line_items = array("find" , $top_path , "-type", "f"); |
|---|
| 64 | break; |
|---|
| 65 | default: |
|---|
| 66 | echo "get_path_list_by_url=" . $part . "<br>"; |
|---|
| 67 | exit(100); |
|---|
| 68 | } |
|---|
| 69 | $cmd_line = implode(" " , $cmd_line_items); |
|---|
| 70 | |
|---|
| 71 | $array = array(); |
|---|
| 72 | $handle = popen( $cmd_line , "r"); |
|---|
| 73 | while (( $buffer = fgets($handle, 4096)) !== false){ |
|---|
| 74 | $line = trim($buffer); |
|---|
| 75 | $data = explode($top_path, $line)[1]; |
|---|
| 76 | array_push($array , $data); |
|---|
| 77 | } |
|---|
| 78 | if (!feof($handle)) { |
|---|
| 79 | echo "Error: unexpected fgets() fail\n"; |
|---|
| 80 | } |
|---|
| 81 | pclose($handle); |
|---|
| 82 | |
|---|
| 83 | return array( "top_path" => $top_path, "file_list" => $array); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | function dump_hier_list($pathinfo){ |
|---|
| 88 | foreach( array_keys($pathinfo->hier_hash) as $v ){ |
|---|
| 89 | $v2 = trim($v); |
|---|
| 90 | if(is_null($v2)){ |
|---|
| 91 | echo "found null\n"; |
|---|
| 92 | exit(0); |
|---|
| 93 | } |
|---|
| 94 | echo $v . "||(" . strlen($v) . ")\n"; |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | function dump_hier($pathinfo) { |
|---|
| 99 | foreach( array_keys($pathinfo->hier_hash) as $v ){ |
|---|
| 100 | echo $v . "\n"; |
|---|
| 101 | $item = $pathinfo->hier_hash[$v]; |
|---|
| 102 | $item->dump(); |
|---|
| 103 | echo "--dump_children\n"; |
|---|
| 104 | $item->dump_children(); |
|---|
| 105 | echo "#==========\n"; |
|---|
| 106 | } |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | function get_path_info( $num , $part){ |
|---|
| 110 | $path_info = get_path_list_by_url( $num , $part); |
|---|
| 111 | $pathinfo = new ns\PathInfo($path_info["top_path"]); |
|---|
| 112 | foreach($path_info["file_list"] as $path){ |
|---|
| 113 | $pathinfo->register($path); |
|---|
| 114 | } |
|---|
| 115 | return $pathinfo->pathinfo_list_in_json(); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | function get_setting_path_info( $num ){ |
|---|
| 119 | $path_info = get_setting_path_list_by_url( $num ); |
|---|
| 120 | $pathinfo = new ns\PathInfo($path_info["top_path"]); |
|---|
| 121 | foreach($path_info["file_list"] as $path){ |
|---|
| 122 | $pathinfo->register($path); |
|---|
| 123 | } |
|---|
| 124 | return $pathinfo->pathinfo_list_in_json(); |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | function print_get_path_info( $num ,$part){ |
|---|
| 128 | echo get_path_info( $num , $part); |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | function print_get_setting_path_info( $num ){ |
|---|
| 132 | echo get_setting_path_info( $num ); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | function ensure_dir( $dir ) { |
|---|
| 136 | if( !file_exists( $dir ) ){ |
|---|
| 137 | mkdir( $dir ); |
|---|
| 138 | } |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | function ensure_storage_dir( $storage_dir , $num, $filename ) { |
|---|
| 142 | $dir = implode( "/" , array( $storage_dir , $num) ); |
|---|
| 143 | mkdir($dir); |
|---|
| 144 | $path = implode( "/" , array( $dir , $filename) ); |
|---|
| 145 | ensure_dir( $dir ); |
|---|
| 146 | |
|---|
| 147 | return $path; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | function get_content( $num , $path , $part) { |
|---|
| 151 | $top_dir = get_path_from_url_data( $num , $part); |
|---|
| 152 | $full_path = implode( '/', [$top_dir, $path]); |
|---|
| 153 | $result = input_file( $full_path ); |
|---|
| 154 | return $result; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | function cmdx(){ |
|---|
| 158 | $filelist_filename = array("data"=> "filelist.json", "setting"=> "setting-filelist.json"); |
|---|
| 159 | $storage_dir = dirname(__FILE__) . '/../storage'; |
|---|
| 160 | |
|---|
| 161 | $content = NULL; |
|---|
| 162 | if (isset($_GET['cmd'])) { |
|---|
| 163 | $cmd = $_GET['cmd']; |
|---|
| 164 | if (isset($_GET['num'])) { |
|---|
| 165 | $num = $_GET['num']; |
|---|
| 166 | if (isset($_GET['part'])) { |
|---|
| 167 | $part = $_GET['part']; |
|---|
| 168 | |
|---|
| 169 | switch ($cmd) { |
|---|
| 170 | case 'update_setting_filelist': |
|---|
| 171 | $path = ensure_storage_dir( $storage_dir , $num, $setting_filelist_filename ); |
|---|
| 172 | $content = get_setting_path_info( $num ); |
|---|
| 173 | output_file( $path , $content ); |
|---|
| 174 | break; |
|---|
| 175 | case 'get_setting_filelist': |
|---|
| 176 | $path = ensure_storage_dir( $storage_dir , $num, $setting_filelist_filename ); |
|---|
| 177 | if( file_exists($path) ){ |
|---|
| 178 | $content = input_file( $path ); |
|---|
| 179 | } |
|---|
| 180 | else { |
|---|
| 181 | $content = get_setting_path_info( $num ); |
|---|
| 182 | output_file( $path , $content ); |
|---|
| 183 | } |
|---|
| 184 | break; |
|---|
| 185 | case 'get_setting_content': |
|---|
| 186 | if (isset($_GET['path'])) { |
|---|
| 187 | $path = $_GET['path']; |
|---|
| 188 | $content = get_setting_content( $num , $path ); |
|---|
| 189 | } |
|---|
| 190 | break; |
|---|
| 191 | case 'build': |
|---|
| 192 | break; |
|---|
| 193 | case 'get_content': |
|---|
| 194 | if (isset($_GET['path'])) { |
|---|
| 195 | $path = $_GET['path']; |
|---|
| 196 | $content = get_content( $num , $path , $part ); |
|---|
| 197 | } |
|---|
| 198 | break; |
|---|
| 199 | case 'update_filelistx': |
|---|
| 200 | $path = ensure_storage_dir( $storage_dir , $num, $filelist_filename[$part] ); |
|---|
| 201 | $content = get_path_info( $num , $part); |
|---|
| 202 | output_file( $path , $content ); |
|---|
| 203 | break; |
|---|
| 204 | case 'get_filelistx': |
|---|
| 205 | $path = ensure_storage_dir( $storage_dir , $num, $filelist_filename[$part] ); |
|---|
| 206 | if( file_exists($path) ){ |
|---|
| 207 | $content = input_file( $path ); |
|---|
| 208 | } |
|---|
| 209 | else { |
|---|
| 210 | $content = get_path_info( $num , $part); |
|---|
| 211 | output_file( $path , $content ); |
|---|
| 212 | } |
|---|
| 213 | break; |
|---|
| 214 | default: |
|---|
| 215 | break; |
|---|
| 216 | } |
|---|
| 217 | } |
|---|
| 218 | } |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | elseif (isset($_POST['cmd'])) { |
|---|
| 222 | $cmd = $_POST['cmd']; |
|---|
| 223 | if (isset($_POST['num'])) { |
|---|
| 224 | $num = $_POST['num']; |
|---|
| 225 | if (isset($_POST['part'])) { |
|---|
| 226 | $part = $_POST['part']; |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | switch ($cmd) { |
|---|
| 230 | case 'upload_content': |
|---|
| 231 | $array = get_lines_from_formdata( "mytext" ); |
|---|
| 232 | $upload_content = implode( "\n" , $array ); |
|---|
| 233 | if (isset($_POST['path'])) { |
|---|
| 234 | $path = $_POST['path']; |
|---|
| 235 | $top_path = get_path_from_url_data( $num , $part); |
|---|
| 236 | $filename_abs_path = $top_path . DIRECTORY_SEPARATOR . $path; |
|---|
| 237 | |
|---|
| 238 | output_file( $filename_abs_path , $upload_content ); |
|---|
| 239 | $content = "P-UPLOAD_CONTENT-OK=" . $filename_abs_path; |
|---|
| 240 | } |
|---|
| 241 | else{ |
|---|
| 242 | $content = "P-UPLOAD_CONTENT-NO-PATH"; |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | default: |
|---|
| 246 | debug_x("P-DEFAULT"); |
|---|
| 247 | /* $content = "P-DEFAULT";*/ |
|---|
| 248 | break; |
|---|
| 249 | } |
|---|
| 250 | } |
|---|
| 251 | } |
|---|
| 252 | else{ |
|---|
| 253 | $content = "P-NO_NUM"; |
|---|
| 254 | } |
|---|
| 255 | } |
|---|
| 256 | else{ |
|---|
| 257 | debug_x('DEFAULT'); |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | if(!is_null($content)){ |
|---|
| 261 | echo( $content ); |
|---|
| 262 | } |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | function debug_x($mes) { |
|---|
| 266 | ob_start(); |
|---|
| 267 | echo $mes . "\n"; |
|---|
| 268 | var_dump($_GET); |
|---|
| 269 | var_dump($_POST); |
|---|
| 270 | ob_end_clean(); |
|---|
| 271 | $content = ob_get_contents(); |
|---|
| 272 | error_log($content,3, './debug.txt'); |
|---|
| 273 | } |
|---|
| 274 | #print_get_path_info( 0 ); |
|---|
| 275 | #print_get_path_info( 1 ); |
|---|
| 276 | #dump_url_data( 0 ); |
|---|
| 277 | #echo( get_setting_path_from_url_data( 0 ) ); |
|---|
| 278 | #var_dump( get_setting_path_list_by_url( 0 ) ); |
|---|
| 279 | #echo( get_setting_path_info( 0 ) ); |
|---|
| 280 | #print_get_setting_path_info( 0 ); |
|---|
| 281 | |
|---|
| 282 | cmdx(); |
|---|
| 283 | |
|---|
| 284 | ?> |
|---|