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

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

rename setting to conf

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