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

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

add php/x.php

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