source: branches/testa-single-bookmark/php/content3.php @ 76

Last change on this file since 76 was 70, checked in by anonymous, 6 years ago

rename file_exist to file_exists

File size: 14.2 KB
Line 
1<?php
2#ini_set('display_errors', 0);
3#int error_reporting ([ int $レベル ] )
4#error_reporting ( ~E_ALL );
5#error_reporting ( ~E_ERROR );
6
7$settings_yml_path = "/../settings-devconf.yml";
8
9header("Access-Control-Allow-Origin: *");
10#namespace ns;
11
12require_once( dirname(__FILE__) . '/func.php');
13require_once( dirname(__FILE__) . '/NorthernCross/ns/PathItem.php');
14require_once( dirname(__FILE__) . '/NorthernCross/ns/PathInfo.php');
15require_once( dirname(__FILE__) . "/spyc-0.5/spyc.php"); // ★の部分にはspyc.phpを置いたパスを入れます
16
17$Top_path = dirname(__FILE__) . "/..";
18$Url_data = null;
19$Url_data_array = null;
20
21function get_url_data_array()
22{
23    global $settings_yml_path;
24
25    $yaml= spyc_load_file( dirname(__FILE__) . $settings_yml_path);// ☆の部分にはyamlファイルがあるパスを入れます
26    return $yaml;
27}
28
29function get_url_data( $num ){
30    global $Url_data_array;
31    return $Url_data_array[$num];
32}
33
34function dump_url_data( $num ){
35    print_r( get_url_data( $num ));     
36}
37
38function get_path_from_url_data( $num , $part){
39    global $Url_data_array;
40
41    switch($part){
42    case 'data':
43        $ret = $Url_data_array[$num]['path'];
44        break;
45    case 'conf':
46        $ret = $Url_data_array[$num]['conf_path'];
47        break;
48    case 'output':
49        $ret = $Url_data_array[$num]['output_path'];
50        break;
51    default:
52        //exit(200);
53        echo "get_path_from_url_data " . $part . "<br>";
54    }
55
56    return $ret;
57}
58
59function get_path_list_by_url( $num , $part){
60    $top_path = get_path_from_url_data($num, $part);
61
62    switch($part){
63    case 'data':
64#        $pattern = "*.md";
65        $regexp = '".*\(md\|yml\)"';
66#        $regexp = '".*\(yml\)"';
67 #       $cmd_line_items = array("find" , $top_path , "-name", $pattern);
68        $cmd_line_items = array("find" , $top_path , "-regex", $regexp);
69        break;
70    case 'conf':
71        $regexp = '".*[^~]"';
72        $cmd_line_items = array("find" , $top_path , "-type", "f", "-regex" , $regexp);
73        break;
74    case 'output':
75        $regexp = '".*htm[^~]"';
76        $cmd_line_items = array("find" , $top_path , "-type", "f", "-regex" , $regexp);
77        break;
78    default:
79        echo "get_path_list_by_url=" . $part . "<br>";
80        exit(100);
81    }
82    $cmd_line = implode(" " , $cmd_line_items);
83
84    exec($cmd_line, $results, $ret_code);
85   
86    if( $ret_code === 0 ){
87        $array = array();
88        foreach($results as $buffer){
89            $line = trim($buffer);
90            $data = explode($top_path,  $line)[1];
91            array_push($array , $data);
92        }
93        $array_x = array( "top_path" => $top_path, "file_list" => $array);
94   }
95    else{
96        $array_x = array( );
97    }
98    return $array_x;
99}
100
101function restrict_filelist( $num, $path_info , $part ) {
102    if( $part === "conf" ){
103        $pathinfox = new ns\PathInfo($part, $path_info["top_path"]);
104    }
105    else{
106        $output_htmls = get_restrict_basenames( $num );
107        $pathinfox = new ns\PathInfo($part, $path_info["top_path"] , $output_htmls);
108
109        foreach($path_info["file_list"] as $path){
110            $pathinfox->scan($path);
111        }
112    }
113
114    foreach($path_info["file_list"] as $path){
115        $pathinfox->register($path);
116    }
117
118    return $pathinfox->pathinfo_list_in_json();
119}
120
121function get_path_info( $num , $part){
122    $path_info = get_path_list_by_url( $num , $part);
123
124    return restrict_filelist( $num, $path_info , $part);
125 }
126
127function ensure_dir( $dir ) {
128    if( !file_exists( $dir ) ){
129        mkdir( $dir );
130    }
131}
132
133function ensure_storage_dir( $storage_dir , $num, $filename ) {
134    $dir = implode( "/" , array( $storage_dir , $num) );
135    if( !file_exists( $dir ) ){
136        mkdir($dir);
137    }
138    $path = implode( "/" , array( $dir , $filename) );
139    ensure_dir( $dir );
140
141    return $path;
142}
143
144function get_content( $num , $path , $part) {
145    $top_dir = get_path_from_url_data( $num , $part);
146    $full_path = implode( '/', [$top_dir, $path]);
147    $result = input_file( $full_path );
148    return $result;
149}
150
151function build_one_html( $top_path, $num , $md_path , $part) {
152    $pathinfo = pathinfo( $md_path );
153    $md_fname = $pathinfo['basename'];
154   
155    $base = $pathinfo['filename'];
156
157    $list = get_site_tsv( $num );
158
159    $t = $list[$base]["t"];
160    $s = $list[$base]["s"];
161    $output_html = $list[$base]["output_html"];
162
163    $url_data = get_url_data( $num );
164    $bundle_path = $url_data["bundle_path"];
165    $conf_path = $url_data["conf_path"];
166    $output_url = $url_data["output_url"];
167
168    $cmd_line_items = array( $top_path . "/bin/build.sh" , $bundle_path , $t, $s, $conf_path);
169
170    $cmd_line = implode(" " , $cmd_line_items);
171
172   
173    exec($cmd_line, $results, $ret_code);
174    if( $ret_code === 0 ){
175        $full_download_url = implode( "/" , array( $output_url , $output_html ) );
176
177        $html_fname_tmp = $output_html  . ".tmp";
178        $partial_download_url = implode( "/" , array( $output_url , $html_fname_tmp) );
179
180        $info = array( "full_download_url" => $full_download_url,
181                   "partial_download_url" => $partial_download_url );
182    }
183    else{
184        $info = array();
185    }
186    return $info;
187}
188
189function cmdx(){
190    global $Top_path;
191    global $Url_data;
192    global $Url_data_array;
193
194    $Url_data_array = get_url_data_array();
195
196    $filelist_filename = array("data"=> "filelist.json", "conf"=> "conf-filelist.json", "output" => "output-filelist.json");
197    $output_url_filename = "output_url.json";
198    #    $storage_dir = dirname(__FILE__) . '/../storage';
199    $storage_dir = dirname(__FILE__) . '/../storage-devconf';
200
201    $content = NULL;
202    if (isset($_GET['cmd'])) {
203        $cmd = $_GET['cmd'];
204        if (isset($_GET['num'])) {
205            $num = $_GET['num'];
206
207            $Url_data = get_url_data($num);
208
209            if (isset($_GET['part'])) {
210                $part = $_GET['part'];
211
212                switch ($cmd) {
213                case 'get_content':
214                    if (isset($_GET['path'])) {
215                        $path = $_GET['path'];
216                        $content = get_content( $num , $path , $part );
217                    }
218                    break;
219                case 'update_filelistx':
220                    $path = ensure_storage_dir( $storage_dir , $num, $filelist_filename[$part] );
221                    $content = get_path_info( $num , $part);
222                    output_file( $path , $content );
223                    break;
224                case 'get_filelistx':
225                    $path = ensure_storage_dir( $storage_dir , $num, $filelist_filename[$part] );
226                    if( file_exists($path) ){
227                        $content = input_file( $path );
228                    }
229                    else {
230                        $content = get_path_info( $num , $part);
231                        output_file( $path , $content );
232                    }
233                    break;
234                default:
235                    break;
236                }
237            }
238            else{
239                switch ($cmd) {
240                case 'get_output_url':
241                    $info = array('output_url' => get_output_url($num));
242                    $content = json_encode($info);
243                    break;
244                case 'update_output_url_listx':
245                    $content = update_output_url_list( $num );
246                case 'get_output_url_listx':
247                    $path = ensure_storage_dir( $storage_dir , $num , $output_url_filename );
248                    if( file_exists($path) ){
249                        $content = input_file( $path );
250                    }
251                    else {
252                        $content = update_output_url_list( $num );
253                    }
254                    break;
255                default:
256                    $content = "bad cmd=" . $cmd;
257                    break;
258                }
259            }
260        }
261    }
262
263    elseif (isset($_POST['cmd'])) {
264        $cmd = $_POST['cmd'];
265        if (isset($_POST['num'])) {
266            $num = $_POST['num'];
267
268            $Url_data = get_url_data($num);
269
270            if (isset($_POST['part'])) {
271                $part = $_POST['part'];
272
273                switch ($cmd) {
274                case 'build_one_html':
275                    $array = get_lines_from_formdata( "mytext" );
276                    $upload_content = implode( "\n" , $array );
277                    if (isset($_POST['path'])) {
278                        $path = $_POST['path'];
279                        $top_path = get_path_from_url_data( $num , $part);
280                        $filename_abs_path = $top_path . DIRECTORY_SEPARATOR . $path;
281                        $ret = build_one_html( $Top_path , $num, $path, $part );
282                    }
283                    else{
284#                        output_file( dirname(__FILE__) . "/debug-F.txt" , "ABCDEFG" );
285                        $ret = "P-UPLOAD_CONTENT-NO-PATH";
286                    }
287                    $content = json_encode( $ret );
288                    break;
289                case 'upload_content':
290                    $array = get_lines_from_formdata( "mytext" );
291                    $upload_content = implode( "\n" , $array );
292                    if (isset($_POST['path'])) {
293                        $path = $_POST['path'];
294                        $top_path = get_path_from_url_data( $num , $part);
295                        $filename_abs_path = $top_path . DIRECTORY_SEPARATOR . $path;
296                       
297                        output_file( $filename_abs_path , $upload_content );
298                        $content = $upload_content;
299                    }
300                    else{
301                        $content = json_encode("P-UPLOAD_CONTENT-NO-PATH");
302                    }
303                   
304                default:
305#                    debug_x("P-DEFAULT");
306                    /* $content = "P-DEFAULT";*/
307                    break;
308                }
309            }
310        }
311        else{
312            $content = json_encode("P-NO_NUM");
313#            $content = json_encode("{pxyz:123}");
314        }
315    }
316    else{
317#        debug_x('DEFAULT');
318    }
319
320    if(!is_null($content)){
321#        $content = "PABCEDFG";
322        echo($content);
323#        debug_x("CONTENT");
324    }
325    else{
326        echo("no content");
327    }
328}
329
330function debug_x($mes, $fname = null) {
331    ob_start();
332    echo $mes . "\n";
333    /*    var_dump($_GET);*/
334    /*    var_dump($_POST);*/
335    ob_end_clean();
336    $contentx = ob_get_contents();
337    if( $fname === null ){
338        $fname = './debug.txt';
339    }
340
341    error_log($contentx, 3, $fname );
342}
343
344function debug_vd($var) {
345    ob_start();
346    var_dump( $var );
347    ob_end_clean();
348    $content = ob_get_contents();
349    output_file( "debug.txt" , $content );
350}
351
352function get_full_path($num, $key, $fname){
353    global $Url_data_array;
354    return $Url_data_array[$num][$key] . "/" . $fname;
355}
356
357function get_restrict_from_settings_yml( $num ) {
358    global $Url_data_array;
359
360    /*    $settings = get_url_data_array();*/
361    $settings = $Url_data_array;
362    if( !isset($settings[$num]) ){
363        return array();
364    }
365    $array = $settings[$num]['restrict'];
366    if( !isset( $array ) ){
367        return array();
368    }
369
370    $r_keys = array_keys( $array );
371    $restrict_array = array();
372    foreach( $array as $k => $v ){
373        $restrict_array[$k] = array();
374        foreach( $v as $k2 => $v2 ){
375            $restrict_array[$k][$k2] = array();
376            foreach( $v2 as $k3 => $v3 ){
377                foreach( $v3 as $k4 => $v4 ){
378                    $restrict_array[$k][$k2][$v4] = "";
379                }
380            }
381        }
382    }
383
384    return $restrict_array;
385}
386
387function get_restrict_basenames( $num ) {
388    $output_htmls = array();
389
390    $list = get_site_tsv($num );
391
392    $restrict_array = get_restrict_from_settings_yml( $num );
393
394    foreach( $list as $k => $v ){
395        if( isset( $restrict_array[ $v['category'] ][ $v['t'] ][ $v['s'] ] ) ) {
396            $output_htmls[$k] = $v['output_html'];
397        }
398    }
399
400    return $output_htmls;
401}
402
403function get_site_tsv($num){
404    $list = array();
405
406    $fname = get_full_path( $num, 'conf_path' , 'site.tsv');
407    if (($f = fopen( $fname , "r" )) !== FALSE) {
408        while (($data = fgetcsv($f, 1000, "\t")) !== FALSE) {
409            if( !preg_match("/^#/" , $data[0]) ){
410                $t = $data[0];
411                $s = $data[1];
412                $category = $data[2];
413                $output_html = $data[3];
414                if( count($data) > 4 ){
415                    $alias_html = $data[4];
416                }
417                else{
418                    $alias_html = "";
419                }
420                $basename = basename( $output_html , ".html");
421                $list[$basename] = array( 't'=> $t, 's'=> $s, 'category'=>$category, 'output_html' => $output_html, 'alias_html'=> $alias_html );
422            }
423        }
424        fclose($f);
425    }
426
427    return $list;
428}
429
430function update_output_url_list( $num ){
431    $json_data = get_output_url_list( $num );
432    $content = json_encode($json_data);
433    output_file( $path , $content );
434
435    return $content;
436}
437
438function get_output_url_list( $num ){
439    $output_url = get_output_url($num);
440    $data = array( "output_url" => get_output_url($num),
441           "htmlfname" => array()
442    );
443
444    $list = get_site_tsv( $num );
445    $restrict_htmlnames = get_restrict_basenames( $num );
446
447    $output_path = get_path_from_url_data( $num, 'output' );
448    foreach( $list as $k => $v ){
449        $htmlfname = $v["output_html"];
450        if( $htmlfname !== null ){
451            $array = array( "full" => "", "partial" => "" );
452            if( !isset( $restrict_htmlnames[$htmlfname] ) ){
453                $fpath =  $output_path . '/' . $htmlfname;
454                if( file_exists($fpath) ){
455                    $array["full"] = $htmlfname;
456                }
457                $fpath_tmp =  $fpath . '.tmp';
458                if( file_exists($fpath_tmp) ){
459                    $array["partial"] = $htmlfname_tmp;
460                }
461                array_push( $data["htmlfname"] , $array);
462            }
463        }
464    }
465
466    return $data;
467}
468
469function get_output_url($num){
470    return $Url_data['output_url'];
471}
472
473function get_restrict($num){
474    /*    $settings = get_url_data_array();*/
475    global $Url_data_array;
476    return $Url_data_array[$num]['restrict'];
477}
478
479cmdx();
480
481#echo json_encode( get_output_url_list(0) );
482# var_dump( get_output_url_list(0) );
483#var_dump( get_output_url_list(0) );
484#echo json_encode( get_output_url_list(0) );
485?>
Note: See TracBrowser for help on using the repository browser.