Fix #7468 - updating template scanner
[Pman.Cms] / PageThumb.php
1 <?php
2
3 require_once 'Pman.php';
4
5 class Pman_Cms_PageThumb extends Pman
6 {
7     function get($id = '',  $opts = array())
8     {
9         $cms_page = DB_DataObject::factory('cms_page');
10         $cms_page->autoJoin();
11         
12         $file_broken = realpath('Pman/templates/images/file-broken.png');
13         
14         if(empty($id) || !$cms_page->get($id)){
15             $this->serve($file_broken);
16         }
17         
18         if ($cms_page->page_type_id_name == 'element') {
19             $cms_page = $cms_page->parent();
20         }
21         
22         if(empty($cms_page->page_type_id_name) || $cms_page->page_type_id_name != 'page') {
23             $this->serve($file_broken);
24         }
25         $ff = HTML_FlexyFramework::get();
26         $opts = isset($ff->Pman_Cms) ? $ff->Pman_Cms : array();
27         
28         if (!isset($opts['basePHP'])) {
29             $this->serve($file_broken);
30         }
31         
32         $basePHP = $opts['basePHP'];
33         
34         $strip = 0;
35         $pl = $cms_page->page_link;
36         if (is_array($opts['basePHP'])) {
37             foreach($opts['basePHP'] as $k=>$v) {
38                 if ($k == substr($pl,0, strlen($k))) {
39                     $pl = substr($pl, strlen($k)+1);
40                     $basePHP = $v;
41                     $matched = true;
42                     break;
43                 }
44             }
45             if (!$matched) {
46                 $this->serve($file_broken);
47             }
48         }
49         
50         switch($pl) {
51             case 'Home':
52             case 'Site':
53                 $pl = '';
54                 break;
55             default:
56                 if($pl[0] != '/') {
57                     $pl = '/' . $pl;
58                 }
59                 break;
60         }
61         
62         $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
63         
64         $url = $protocol . $_SERVER['HTTP_HOST'] . $this->rootURL .'/'. $basePHP . $pl;
65         
66         $file = $cms_page->getStoreName();
67         
68         if(file_exists($file) && strtotime($cms_page->updated) < filemtime($file)){
69             $this->serve($file);
70         }
71         
72         require_once 'System.php';
73         
74         $conv = System::which('wkhtmltoimage');
75         
76         if (!$conv) {
77             $this->jerr("missing wkhtmltoimage");
78         }
79         
80         $xvfb = System::which('xvfb-run');
81         
82         if (!$xvfb) {
83             $this->jerr("missing xvfb-run");
84         }
85         
86         $cmd = $xvfb . ' --auto-servernum '  . $conv .
87             ' --width 300 --height 300 --zoom 0.3 --disable-smart-width --enable-javascript --javascript-delay 1000 ' .
88             escapeshellarg($url) . ' ' .escapeshellarg($file);
89         
90         exec($cmd);
91         
92         if(!file_exists($file)){
93             $this->serve($file_broken);
94         }
95         
96         $this->serve($file);
97         
98         exit;
99         
100     }
101     
102     function serve($file)
103     {
104         $this->sessionState(0);
105         
106         require_once 'File/Convert.php';
107         
108         $fc = new File_Convert($file, 'image/png');
109         
110         $fc->convert('image/png');
111         
112         $fc->serve('inline');
113         
114         exit;
115         
116     }
117 }