88828eebc42024c0c3e367976f3b83effdb4ff18
[Pman.Builder] / DataObjects / Builder_modules.php
1 <?php
2 /**
3  * Table Definition for builder_app
4  */
5 require_once 'DB/DataObject.php';
6
7 class Pman_Builder_DataObjects_Builder_modules extends DB_DataObject 
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11
12     public $__table = 'builder_modules';                     // table name
13     public $id;                              // int(11)  not_null primary_key auto_increment
14     public $name;                             // string(64)  not_null
15     public $path;                          // string(128)  not_null
16     public $public;                        // int(2)  not_null
17
18     
19     /* the code above is auto generated do not remove the tag below */
20     ###END_AUTOCODE
21     // * - beforeDelete() -- return false for fail and set DO->err;
22
23     function beforeDelete()
24     {
25         return;
26         // fixme - check builder components.
27         
28     }
29     
30     
31     function applyFilters($q, $au)
32     {
33         if (!empty($q['query']['_sync'])) {
34             
35             // use the basic builder modules for this project based on configurion.
36             // what do we have..
37             //DB_DataObject::DebugLevel(1);
38             $x = DB_DataObject::factory('builder_modules');
39             $modpaths = $x->fetchAll('name', 'path');
40             
41             $ff = HTML_FlexyFramework::get();
42             
43             
44             foreach($ff->enableArray as $m) {
45                 if (isset($modpaths[$m])) {
46                     continue;
47                 }
48                 if (file_exists($ff->baseDir .'/'. $m)) {
49                     $x = DB_DataObject::factory('builder_modules');
50                     $x->setFrom(array(
51                         'name' =>$m,
52                         'path' => $ff->baseDir .'/'. $m,
53                         'public' => 0,
54                     ));
55                     $x->insert();
56                     
57                     
58                 }
59                
60                 
61                 
62                 
63             }
64              $ff->page->jok("Synced");
65             //$mods = $ff->enableArray;
66             echo '<PRE>';print_R($ff);exit;
67             
68             
69             
70         }
71     }
72       
73     function scanDir() // return name => mtime for files in path..
74     {
75         $gd = $this->gitDir();
76         if (!$gd) {
77             return false;
78         }
79         $working = $this->gitWorking($gd['url']);
80         $path = strlen($gd['path']) ? $gd['path'] . '/' : '';
81         
82         // list of bjs files...
83         // or should this be totally database related...
84         
85         $ret = array();
86         foreach(glob($working . '/'. $path .'*.bjs') as $bjs) {
87             $js = preg_replace('/\.bjs$/', '.js', $bjs);
88             if (!file_exists($js)) {
89                 continue;
90             }
91             $n = preg_replace('/\.bjs$/', '', basename($bjs));
92             
93             $ret[$n] = filemtime( $bjs);
94         }
95         return $ret;
96     }
97     /**
98      * This updates the contents of a DataObjects with the file contents if they are newer..
99      * 
100      * 
101      */
102     function syncParts()
103     {
104         
105         
106         $files = $this->scanDir();
107         
108         $gd = $this->gitDir();
109         if (!$gd) {
110             return false;
111         }
112         $working = $this->gitWorking($gd['url']);
113         $path = strlen($gd['path']) ? $gd['path'] . '/' : '';
114         
115         
116         $d = DB_DataObject::factory('builder_part');
117         $d->module_id = $this->id;
118        // DB_DataObject::debugLevel(1);
119         $cur = $d->fetchAll();
120         foreach($cur  as $d) {
121             if (isset($files[$d->name]) && strtotime($d->updated) < $files[$d->name]) {
122                 //file mtime is greater than db. -- replace!
123                 $d->json = file_get_contents($working . '/'. $path . $d->name . '.bjs');
124                 $d->jsource = file_get_contents($working . '/'. $path . $d->name . '.js');
125                 $d->update();
126                 // do not need to create it...
127                 unset($files[$d->name]);
128             }
129             if (isset($files[$d->name])) {
130                 unset($files[$d->name]); 
131             }
132         }
133         // we do not delete anything...
134         // next create stuff..
135         foreach($files as $f=>$mt) {
136             $d = DB_DataObject::factory('builder_part');
137             $d->name = $f;
138             $d->json = file_get_contents($working . '/'. $path . $f . '.bjs');
139             $d->jsource = file_get_contents($working . '/'. $path . $f . '.js');
140             $d->updated = date('Y-m-d H:i:s', $mt);
141             $d->module_id = $this->id;
142             $d->insert();
143         }
144         
145         
146     }
147     /**
148      * get the git configuration...
149      * @return {Array|false} false if it can not find path..
150      */
151     function gitDir()
152     {
153         
154         static $paths= array();
155         if (isset($paths[$this->path])) {
156             return $paths[$this->path]; 
157         }
158         // using the path determine 
159         $path = $this->path;
160         
161         
162         
163         //var_dump($path);
164         //var_dump(file_exists($path.'/.git'));
165         
166         while (!file_exists($path.'/.git')) {
167             $path = dirname($path);
168             //var_dump($path);
169             if (empty($path) || $path == '/') {
170                 return false;
171             }
172         }
173         //$gpath = $path.'/.git';
174         
175         $sub = substr($this->path, strlen($path) + 1);
176         
177         require_once 'System.php';
178         $git = System::which('git');
179         
180         @chdir($this->path);
181         
182         $url = trim(`$git config --get remote.origin.url`);
183         
184         $ret=  array(
185             'url' => $url,
186             'path' => $sub
187         );
188         
189         $paths[$this->path] = $ret;
190         //var_Dump($ret); //exit;
191         return $ret;
192         
193     }
194     
195     function gitWorking($url)
196     {
197         
198         
199         $pg = HTML_FlexyFramework::get()->page;
200         $working = ini_get('session.save_path'). '/' .
201                 urlencode($pg->authUser->email) . '-' .
202                 urlencode($url);
203         
204         require_once 'System.php';
205         $git = System::which('git');
206         
207         
208         if (!$this->checkURL($url)) {
209             $pg->jerr("netrc authentication not set up for www-data to access  $url");
210         }
211         // var_dump($working);exit;
212         if (file_exists($working)) {
213             chdir($working);
214             
215             `$git pull`;
216             return $working;
217                 
218         }
219         // might take some time..
220         chdir (ini_get('session.save_path'));
221         
222         $cmd = "$git clone ". escapeshellarg($url) ." " . basename($working);
223         
224         `$cmd`;
225         return $working;
226     }
227     /**
228      * dumb http check to see if we are authenticated..
229      */
230     function checkURL($u)
231     {
232         if (!preg_match('#^(http|https):/#i', $u)) {
233             //var_dump($u);exit;
234             return true;
235         }
236          $curl = System::which('curl');
237          $cmd = "$curl -n -I " . escapeshellarg($u);
238         $res = `$cmd`;
239         //var_dump($res);exit;
240         $lines = explode("\n", $res);
241         if (!preg_match('/401/', $lines[0])) {
242             return true;
243         }
244         return false;
245
246     }
247     
248     function gitCommit($file,$data)
249     {
250         $gd = $this->gitDir();
251         if (!$gd) {
252             
253             // directory is not git..
254             if (empty($this->path)) {
255                 return false;
256             }
257             if (!file_exists($this->path)) {
258                 HTML_FlexyFramework::get()->page->jerr("configured url is not git or writable: {$this->path}");
259                 
260             }
261             file_put_contents(rtrim($this->path,'/') .'/'. $file, $data);
262             
263             
264             return false;
265         }
266         $working = $this->gitWorking($gd['url']);
267         chdir($working);
268         $path = strlen($gd['path']) ? $gd['path'] . '/' : '';
269         $target = $working.'/'.$path . $file;
270         $exist = file_exists($target);
271         file_put_contents($working.'/'.$path . $file, $data);
272         
273         require_once 'System.php';
274         $git = System::which('git');
275        
276         chdir($working);
277         if (!$exist) {
278             $cmd = "$git add {$path}{$file}";
279             `$cmd`;
280         }
281         
282         $cmd = "$git commit -m '{$path}{$file} - Commit from online editor' {$path}{$file}";
283         `$cmd`;
284         `git push`;
285         
286         
287         
288     }
289     
290     function gitCommitDelete($file)
291     {
292         $gd = $this->gitDir();
293         if (!$gd) {
294             return false;
295         }
296         $working = $this->gitWorking($gd['url']);
297         
298         
299         $path = strlen($gd['path']) ? $gd['path'] . '/' : '';
300         $target = $working.'/'.$path . $file;
301         //var_dump($target);exit;
302         if (!file_exists($target)) {
303             return; // no need to do anything..
304         }
305        
306         
307         require_once 'System.php';
308         $git = System::which('git');
309         chdir($working);
310         
311         `$git rm {$path}{$file}`;
312         
313         $cmd = "$git commit -m '{$path}{$file} - Commit (DELETE) from online editor' ";
314         `$cmd`;
315         `git push`;
316          
317     }
318     
319     
320     
321 }