source: branches/testa/php/content.php @ 25

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

use localstorage to store/restore settings

  • Property svn:executable set to *
File size: 6.3 KB
RevLine 
[23]1<?php
2
3header("Access-Control-Allow-Origin: *");
4#namespace ns;
5
6require_once( dirname(__FILE__) . '/func.php');
7
8require_once( dirname(__FILE__) . '/NorthernCross/ns/PathItem.php');
9require_once( dirname(__FILE__) . '/NorthernCross/ns/PathInfo.php');
10require_once( dirname(__FILE__) . "/spyc-0.5/spyc.php"); // ★の部分にはspyc.phpを置いたパスを入れます
11
12function get_url_data_array()
13{
14        $yaml= spyc_load_file( dirname(__FILE__) . "/../settings.yml");// ☆の部分にはyamlファイルがあるパスを入れます
15        return $yaml;
16}
17
18function 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
26function get_url_data( $num ){
27    $array = get_url_data_array();
28    return $array[$num];
29}
30
31function dump_url_data( $num ){
32    print_r( get_url_data( $num ));     
33}
34
35function get_path_from_url_data( $num ){
36    return get_url_data( $num )['path'];
37}
38
39function get_path_list_by_url( $num ){
40    $pattern = "*.md";
41    $top_path = get_path_from_url_data($num);
42    $cmd_line_items = array("find" , $top_path , "-name", $pattern);
43    $cmd_line = implode(" " , $cmd_line_items);
44
45    $array = array();
46    $handle = popen( $cmd_line , "r");
47    while (( $buffer = fgets($handle, 4096)) !== false){
48        $line = trim($buffer);
49        $data = explode($top_path,  $line)[1];
50        array_push($array , $data);
51    }
52    if (!feof($handle)) {
53            echo "Error: unexpected fgets() fail\n";
54    }
55    pclose($handle);
56   
57    return array( "top_path" => $top_path, "file_list" => $array);
58}
59
60
61function dump_hier_list($pathinfo){
62    foreach( array_keys($pathinfo->hier_hash) as $v ){
63        $v2 = trim($v);
64        if(is_null($v2)){
65            echo "found null\n";
66            exit(0);
67        }
68        echo $v . "||(" . strlen($v) . ")\n";
69   }
70}
71
72function dump_hier($pathinfo) {
73    foreach( array_keys($pathinfo->hier_hash) as $v ){
74        echo $v . "\n";
75        $item = $pathinfo->hier_hash[$v];
76        $item->dump();
77        echo "--dump_children\n";
78        $item->dump_children();
79        echo "#==========\n";
80    }
81}
82
83function get_path_info( $num ){
84    $path_info = get_path_list_by_url( $num );
85    $pathinfo = new ns\PathInfo($path_info["top_path"]);
86#    $pathinfo = new PathInfo($path_info["top_path"]);
87    foreach($path_info["file_list"] as $path){
88        $pathinfo->register($path);
89    }
90    return $pathinfo->pathinfo_list_in_json();
91#    $pathinfo->dump();
92#    $pathinfo->dump_root();
93#    dump_hier_list($pathinfo);
94}
95
96function print_get_path_info( $num ){
97    echo get_path_info( $num );
98}
99
100function ensure_dir( $dir ) {
101    if( !file_exists( $dir ) ){
102        mkdir( $dir );
103    }
104}
105
106function ensure_storage_dir( $storage_dir , $num, $filename ) {
107    $dir = implode( "/" , array( $storage_dir , $num) );
108    mkdir($dir);
109    $path = implode( "/" , array( $dir , $filename) );
110    ensure_dir( $dir );
111
112    return $path;
113}
114
115function get_content( $num , $path ) {
116    $top_dir = get_path_from_url_data( $num );
117    $full_path = implode( '/', [$top_dir, $path]);
118    $result = input_file( $full_path );
119    return $result;
120}
121
122function cmdx(){
123    $filelist_filename = "filelist.json";
[25]124    $setting_filelist_filename = "setting-filelist.json";
[23]125    $storage_dir = dirname(__FILE__) . '/../storage';
126
127    $content = NULL;
128    if (isset($_GET['cmd'])) {
129        $cmd = $_GET['cmd'];
130        if (isset($_GET['num'])) {
131            $num = $_GET['num'];
132
133            switch ($cmd) {
[25]134            case 'get_setting_filelist':
135                $path = ensure_storage_dir( $storage_dir , $num, $setting_filelist_filename );
136                if( file_exists($path) ){
137                    $content = input_file( $path );
138                }
139                else {
140                    $content = get_path_info( $num );
141                    output_file( $path , $content );
142                }
143                break;
144            case 'build':
145                break;
[23]146            case 'get_content':
147                if (isset($_GET['path'])) {
148                    $path = $_GET['path'];
149                    $content = get_content( $num , $path );
150                }
151                break;
152            case 'update_filelist':
153                $path = ensure_storage_dir( $storage_dir , $num, $filelist_filename );
154                $content = get_path_info( $num );
155                output_file( $path , $content );
156                break;
157            case 'get_filelist':
158                $path = ensure_storage_dir( $storage_dir , $num, $filelist_filename );
159                if( file_exists($path) ){
[25]160                    $content = input_file( $path );
[23]161                }
162                else {
163                    $content = get_path_info( $num );
164                    output_file( $path , $content );
165                }
166                break;
167            default:
168                break;
169            }
170        }
171    }
172    elseif (isset($_POST['cmd'])) {
173        $cmd = $_POST['cmd'];
174        if (isset($_POST['num'])) {
175            $num = $_POST['num'];
176
177            switch ($cmd) {
178            case 'upload_content':
179                $array = get_lines_from_formdata( "mytext" );
180                $upload_content = implode( "\n" , $array );
181                if (isset($_POST['path'])) {
182                    $path = $_POST['path'];
183                    $top_path = get_path_from_url_data( $num );
184                    $filename_abs_path = $top_path . DIRECTORY_SEPARATOR . $path;
185
186                    output_file( $filename_abs_path , $upload_content );
187                    $content = "P-UPLOAD_CONTENT-OK=" . $filename_abs_path;
188                }
189                else{
190                    $content = "P-UPLOAD_CONTENT-NO-PATH";
191                }
192
193            default:
194                debug_x("P-DEFAULT");
195                /* $content = "P-DEFAULT";*/
196                break;
197            }
198        }
199        else{
200            $content = "P-NO_NUM";
201        }
202    }
203    else{
204        debug_x('DEFAULT');
205    }
206
207    if(!is_null($content)){
208        echo( $content );
209    }
210}
211
212function debug_x($mes) {
213    ob_start();
214    echo $mes . "\n";
215    var_dump($_GET);
216    var_dump($_POST);
217    ob_end_clean();
218    $content = ob_get_contents();
219    error_log($content,3, './debug.txt');
220}
221#print_get_path_info( 0 );
222#print_get_path_info( 1 );
223cmdx();
224
225?>
Note: See TracBrowser for help on using the repository browser.