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