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

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

remove php/x.php

File size: 12.7 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$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;
49    case 'conf':
50        $ret = get_url_data( $num )['conf_path'];
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;
74    case 'conf':
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    exec($cmd_line, $results, $ret_code);
89   
90    if( $ret_code === 0 ){
91        $array = array();
92        foreach($results as $buffer){
93            $line = trim($buffer);
94            $data = explode($top_path,  $line)[1];
95            array_push($array , $data);
96        }
97        $array_x = array( "top_path" => $top_path, "file_list" => $array);
98   }
99    else{
100        $array_x = array( );
101    }
102    return $array_x;
103}
104
105function restrict_filelist( $num, $path_info , $part ) {
106    if( $part === "conf" ){
107        $pathinfox = new ns\PathInfo($part, $path_info["top_path"]);
108    }
109    else{
110        $output_htmls = get_restrict_basenames( $num );
111        $pathinfox = new ns\PathInfo($part, $path_info["top_path"] , $output_htmls);
112
113        foreach($path_info["file_list"] as $path){
114            $pathinfox->scan($path);
115        }
116    }
117
118    foreach($path_info["file_list"] as $path){
119        $pathinfox->register($path);
120    }
121
122    return $pathinfox->pathinfo_list_in_json();
123}
124
125
126function get_path_info( $num , $part){
127    $path_info = get_path_list_by_url( $num , $part);
128
129    return restrict_filelist( $num, $path_info , $part);
130 }
131
132function ensure_dir( $dir ) {
133    if( !file_exists( $dir ) ){
134        mkdir( $dir );
135    }
136}
137
138function ensure_storage_dir( $storage_dir , $num, $filename ) {
139    $dir = implode( "/" , array( $storage_dir , $num) );
140    mkdir($dir);
141    $path = implode( "/" , array( $dir , $filename) );
142    ensure_dir( $dir );
143
144    return $path;
145}
146
147function get_content( $num , $path , $part) {
148    $top_dir = get_path_from_url_data( $num , $part);
149    $full_path = implode( '/', [$top_dir, $path]);
150    $result = input_file( $full_path );
151    return $result;
152}
153
154function build_one_html( $exec_path, $num , $md_path , $part) {
155    $pathinfo = pathinfo( $md_path );
156    $md_fname = $pathinfo['basename'];
157   
158    $base = $pathinfo['filename'];
159
160    $list = get_site_tsv( $num );
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   
176    exec($cmd_line, $results, $ret_code);
177    if( $ret_code === 0 ){
178        $full_download_url = implode( "/" , array( $output_url , $output_html ) );
179
180        $html_fname_tmp = $output_html  . ".tmp";
181        $partial_download_url = implode( "/" , array( $output_url , $html_fname_tmp) );
182
183        $info = array( "full_download_url" => $full_download_url,
184                   "partial_download_url" => $partial_download_url );
185    }
186    else{
187        $info = array();
188    }
189    return $info;
190}
191
192function cmdx(){
193    global $exec_path;
194
195    $filelist_filename = array("data"=> "filelist.json", "conf"=> "conf-filelist.json", "output" => "output-filelist.json");
196    #    $storage_dir = dirname(__FILE__) . '/../storage';
197    $storage_dir = dirname(__FILE__) . '/../storage-devconf';
198
199    $content = NULL;
200    if (isset($_GET['cmd'])) {
201        $cmd = $_GET['cmd'];
202        if (isset($_GET['num'])) {
203            $num = $_GET['num'];
204            if (isset($_GET['part'])) {
205                $part = $_GET['part'];
206
207                switch ($cmd) {
208                case 'get_content':
209                    if (isset($_GET['path'])) {
210                        $path = $_GET['path'];
211                        $content = get_content( $num , $path , $part );
212                    }
213                    break;
214                case 'update_filelistx':
215                    $path = ensure_storage_dir( $storage_dir , $num, $filelist_filename[$part] );
216                    $content = get_path_info( $num , $part);
217                    output_file( $path , $content );
218                    break;
219                case 'get_filelistx':
220                    $path = ensure_storage_dir( $storage_dir , $num, $filelist_filename[$part] );
221                    if( file_exists($path) ){
222                        $content = input_file( $path );
223                    }
224                    else {
225                        $content = get_path_info( $num , $part);
226                        output_file( $path , $content );
227                    }
228                    break;
229                default:
230                    break;
231                }
232            }
233            else{
234                switch ($cmd) {
235                case 'get_output_url':
236                    $info = array('output_url' => get_output_url($num));
237                    $content = json_encode($info);
238                    break;
239                default:
240                    break;
241                }
242            }
243        }
244    }
245
246    elseif (isset($_POST['cmd'])) {
247        $cmd = $_POST['cmd'];
248        if (isset($_POST['num'])) {
249            $num = $_POST['num'];
250            if (isset($_POST['part'])) {
251                $part = $_POST['part'];
252
253                switch ($cmd) {
254                case 'build_one_html':
255                    $array = get_lines_from_formdata( "mytext" );
256                    $upload_content = implode( "\n" , $array );
257                    if (isset($_POST['path'])) {
258                        $path = $_POST['path'];
259                        $top_path = get_path_from_url_data( $num , $part);
260                        $filename_abs_path = $top_path . DIRECTORY_SEPARATOR . $path;
261                        $ret = build_one_html( $exec_path , $num, $path, $part );
262                    }
263                    else{
264#                        output_file( dirname(__FILE__) . "/debug-F.txt" , "ABCDEFG" );
265                        $ret = "P-UPLOAD_CONTENT-NO-PATH";
266                    }
267                    $content = json_encode( $ret );
268                    break;
269                case 'upload_content':
270                    $array = get_lines_from_formdata( "mytext" );
271                    $upload_content = implode( "\n" , $array );
272                    if (isset($_POST['path'])) {
273                        $path = $_POST['path'];
274                        $top_path = get_path_from_url_data( $num , $part);
275                        $filename_abs_path = $top_path . DIRECTORY_SEPARATOR . $path;
276                       
277                        output_file( $filename_abs_path , $upload_content );
278                        $content = $upload_content;
279                    }
280                    else{
281                        $content = json_encode("P-UPLOAD_CONTENT-NO-PATH");
282                    }
283                   
284                default:
285#                    debug_x("P-DEFAULT");
286                    /* $content = "P-DEFAULT";*/
287                    break;
288                }
289            }
290        }
291        else{
292            $content = json_encode("P-NO_NUM");
293#            $content = json_encode("{pxyz:123}");
294        }
295    }
296    else{
297#        debug_x('DEFAULT');
298    }
299
300    if(!is_null($content)){
301#        $content = "PABCEDFG";
302        echo($content);
303#        debug_x("CONTENT");
304    }
305    else{
306        echo($content);
307    }
308}
309
310function debug_x($mes, $fname = null) {
311    ob_start();
312    echo $mes . "\n";
313    /*    var_dump($_GET);*/
314    /*    var_dump($_POST);*/
315    ob_end_clean();
316    $contentx = ob_get_contents();
317    if( $fname === null ){
318        $fname = './debug.txt';
319    }
320
321    error_log($contentx, 3, $fname );
322}
323
324function debug_vd($var) {
325    ob_start();
326    var_dump( $var );
327    ob_end_clean();
328    $content = ob_get_contents();
329    output_file( "debug.txt" , $content );
330}
331
332function get_full_path($num, $key, $fname){
333    $array = get_url_data_array();
334    return $array[$num][$key] . "/" . $fname;
335}
336
337function get_restrict_from_settings_yml() {
338    $settings = get_url_data_array();
339    $array = $settings[0]['restrict'];
340
341    $r_keys = array_keys( $array );
342    $restrict_array = array();
343    foreach( $array as $k => $v ){
344        $restrict_array[$k] = array();
345        foreach( $v as $k2 => $v2 ){
346            $restrict_array[$k][$k2] = array();
347            foreach( $v2 as $k3 => $v3 ){
348                foreach( $v3 as $k4 => $v4 ){
349                    $restrict_array[$k][$k2][$v4] = "";
350                }
351            }
352        }
353    }
354
355    return $restrict_array;
356}
357
358function get_restrict_basenames( $num ) {
359    $output_htmls = array();
360
361    $list = get_site_tsv($num );
362
363    $restrict_array = get_restrict_from_settings_yml();
364
365    foreach( $list as $k => $v ){
366        if( isset( $restrict_array[ $v['category'] ][ $v['t'] ][ $v['s'] ] ) ) {
367            $output_htmls[$k] = $v['output_html'];
368        }
369    }
370
371    return $output_htmls;
372}
373
374function get_site_tsv($num){
375    $list = array();
376
377    $fname = get_full_path( $num, 'conf_path' , 'site.tsv');
378    if (($f = fopen( $fname , "r" )) !== FALSE) {
379        while (($data = fgetcsv($f, 1000, "\t")) !== FALSE) {
380            if( !preg_match("/^#/" , $data[0]) ){
381                $t = $data[0];
382                $s = $data[1];
383                $category = $data[2];
384                $output_html = $data[3];
385                if( count($data) > 4 ){
386                    $alias_html = $data[4];
387                }
388                else{
389                    $alias_html = "";
390                }
391                $basename = basename( $output_html , ".html");
392                $list[$basename] = array( 't'=> $t, 's'=> $s, 'category'=>$category, 'output_html' => $output_html, 'alias_html'=> $alias_html );
393            }
394        }
395        fclose($f);
396    }
397
398    return $list;
399}
400
401function get_output_url($num){
402    $settings = get_url_data_array();
403    return $settings[$num]['output_url'];
404}
405
406function get_restrict($num){
407    $settings = get_url_data_array();
408    return $settings[$num]['restrict'];
409}
410
411function get_item($num, $name){
412    $settings = get_url_data_array();
413    return $settings[$num][$name];
414}
415
416function get_settings_file( $name ){
417    $dir = get_item( 0 , "conf_path" );
418    $path = implode( "/" , [$dir, $name] );
419
420    return $path;
421}
422
423function do_exec(){
424    $cmd = "ls -l /z";
425
426    exec($cmd, $opt, $ret);
427
428#    print_r($opt);
429#    echo "ret=" . $ret . "\n";
430}
431
432cmdx();
433/*
434do_exec();
435*/
436/*
437$num = 0;
438$path = "/attempt/devconf2018/data/devconf2018.md";
439$part = "data";
440$lines = build_one_html( $exec_path, $num , $path , $part );
441
442var_dump( $lines );
443
444foreach( $lines as $k => $v ){
445    if( is_array( $v )){
446        echo implode( "\n" , $v );
447    }
448    else{
449        echo $v . "\n";
450    }
451}
452*/
453?>
Note: See TracBrowser for help on using the repository browser.