PHP8 fix
[Pman.Cms] / OutputTranslations.php
1 <?php
2
3 require_once 'Pman.php'; /// needed as we might not be included from pman..
4
5 class Pman_Cms_OutputTranslations extends Pman
6 {
7     var $lang = 'en';
8     var $cachePath;
9     
10     
11     function get($lang = '',  $opts = array())
12     {
13         if(!empty($lang)){
14             $this->lang = $lang;
15         }
16         $projects = array();
17         
18         foreach ($this->modules() as $m){
19             $projects[] = "Pman.{$m}";
20         }
21         
22         $this->getTranslationsCache($projects);
23         exit;
24     }
25     
26     function getTranslations($projects = array())
27     {
28         $ret = array();
29         
30         $template = DB_DataObject::factory('cms_template');
31         $template->whereAddIn('view_name', $projects, 'string');
32         $template = $template->fetchAll('id');
33         
34         $base = DB_DataObject::factory('cms_templatestr');
35         $base->setFrom(array(
36             'lang' => '',
37             'active' => 1
38         ));
39         $base->whereAddIn('template_id', $template, 'int');
40         
41         $base = $base->fetchAll('id', 'template_id');
42         
43         $translation = DB_DataObject::factory('cms_templatestr');
44         $translation->autoJoin();
45         $translation->setFrom(array(
46             'template_id' => array_values($base),
47             'lang' => $this->lang,
48             'active' => 1
49         ));
50         $translation->whereAddIn('cms_templatestr.src_id', array_keys($base), 'int');
51         
52         $translation->selectAdd();
53         $translation->selectAdd("
54             cms_templatestr.txt AS txt,
55             join_template_id_id.template AS template_id_template,
56             join_src_id_id.mdsum AS src_id_mdsum,
57             join_src_id_id.txt AS src_id_txt
58         ");
59         
60         foreach ($translation->fetchAll() as $t){
61             if(!isset($ret[$t->template_id_template])){
62                 $ret[$t->template_id_template] = array();
63             }
64
65             $ret[$t->template_id_template]['_strings'][$t->src_id_mdsum] = (empty($t->txt)) ? $t->src_id_txt : $t->txt;
66             $ret[$t->template_id_template]['_strings_map'][$t->src_id_txt] = $t->src_id_mdsum;
67         }
68          
69         return $ret;
70     }
71     
72     function getTranslationsCache($projects = array())
73     {
74         if(empty($projects)){
75             return;
76         }
77         
78         $this->sessionState(0);
79         
80         $ff = HTML_FlexyFramework::get();
81         
82         $ui = posix_getpwuid(posix_geteuid());
83         
84         $this->cachePath = session_save_path() . '/' .
85                 $ui['name'] . '-' . $ff->project . '-templatestr/' . 
86                 $ff->project . '-' . $ff->version . '-' . $this->lang . '.fulloutput.json';
87         
88         $is_new = false;
89         
90         if(!$this->is_cached() ){
91             $is_new =true;
92             $translations = $this->getTranslations($projects);
93         
94             $output = "";
95
96             foreach ($translations as $file => $trans){
97                 $t = json_encode($trans['_strings']);
98                 $tmap = json_encode($trans['_strings_map']);
99
100                 $cls = str_replace('.bjs', '', $file);
101                 $output .= "try { ";
102                 $output .= "Roo.apply({$cls}._strings, {$t});";
103                 $output .= "Roo.apply({$cls}._strings_map, {$tmap});";
104                 $output .= " } ";
105                 $output .= "catch(e){}; ";
106             }
107
108             file_put_contents($this->cachePath, $output);
109         }
110         
111         $last_modified_time = filemtime($this->cachePath);
112         
113         if (
114             !$is_new
115             &&
116             (
117                 
118                 (
119                     isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
120                     strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time
121                 )
122                 ||
123                 (
124                     isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
125                     trim($_SERVER['HTTP_IF_NONE_MATCH']) == md5($this->cachePath)
126                 )
127             )
128         ) { 
129             
130             header("HTTP/1.1 304 Not Modified");
131             exit;
132         }
133         
134         header('Content-Type: text/javascript');
135         
136         // dont do the 'cachy thing' on dev servers...
137         if (!$this->is_dev()) { 
138             header("Pragma: public");
139             header('Content-Length: '. filesize($this->cachePath));
140             
141             header('Cache-Control: max-age=2592000, public');
142             header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 2592000));
143             header('Last-Modified: '.gmdate('D, d M Y H:i:s \G\M\T', $last_modified_time));
144             header('Etag: '. md5($this->cachePath));
145         }
146         
147         $fh = fopen($this->cachePath,'r');
148         fpassthru($fh);
149         fclose($fh);
150     }
151     
152     function is_dev()
153     {
154         if (!empty($_SERVER['HTTP_HOST']) && preg_match('/^dev/', $_SERVER['HTTP_HOST'])) {
155             return true;
156         }
157         return false;
158     }
159     
160     function is_cached()
161     {
162         
163         if (!empty($_GET['no_cache'])) {
164             return false;
165         }
166         if ($this->is_dev()) {
167             return false;
168         }
169         
170         
171         $dest = dirname($this->cachePath);
172         
173         if (!file_exists($dest)) {
174             mkdir($dest, 0700, true);
175         }
176         
177         if(!file_exists($this->cachePath)){
178             return false;
179         }
180       
181         $cms_templatestr = DB_DataObject::factory('cms_templatestr');
182         $cms_templatestr->active = 1;
183         $cms_templatestr->selectAdd();
184         $cms_templatestr->selectAdd("MAX(updated) AS latest");
185         $cms_templatestr->find(true);
186         
187       
188         
189         if(filemtime($this->cachePath) < strtotime($cms_templatestr->latest)){
190             return false;
191         }
192         
193         return true;
194     }
195 }