Dotclear

source: plugins/odt/_public.php @ 2490

Revision 2490, 6.1 KB checked in by abompard, 13 years ago (diff)

Suppression du passage par référence

Line 
1<?php
2# vim: set noexpandtab tabstop=5 shiftwidth=5:
3# -- BEGIN LICENSE BLOCK ----------------------------------
4# This file is part of odt, a plugin for Dotclear.
5#
6# Copyright (c) 2009 Aurélien Bompard <aurelien@bompard.org>
7#
8# Licensed under the AGPL version 3.0.
9# A copy of this license is available in LICENSE file or at
10# http://www.gnu.org/licenses/agpl-3.0.html
11# -- END LICENSE BLOCK ------------------------------------
12if (!defined('DC_RC_PATH')) { return; }
13
14$core->tpl->addValue('ODT',array('tplOdt','odtLink'));
15
16$core->addBehavior('publicHeadContent',
17     array('odtBehaviors','publicHeadContent'));
18$core->addBehavior('publicEntryBeforeContent',
19     array('odtBehaviors','publicEntryBeforeContent'));
20$core->addBehavior('publicEntryAfterContent',
21     array('odtBehaviors','publicEntryAfterContent'));
22
23require_once(dirname(__FILE__)."/inc/lib.odt.utils.php");
24require_once(dirname(__FILE__)."/inc/class.odt.dcodf.php");
25
26class urlOdt extends dcUrlHandlers
27{
28     public static function odt($args)
29     {
30          global $core;
31          if (!odtUtils::checkConfig()) {
32               throw new Exception('Configuration problem, see admin page for details');
33          }
34          if (!$args) {
35               $tpl_name = "home";
36          } else {
37               $tpl_name = self::loadPost($args);
38          }
39          # The entry
40          $core->tpl->setPath($core->tpl->getPath(),
41                              dirname(__FILE__).'/default-templates');
42          self::serveDocument($tpl_name.'.odt',
43                              'application/vnd.oasis.opendocument.text');
44          exit;
45     }
46
47     protected static function loadPost($args)
48     {
49          global $core, $_ctx;
50         
51          $core->blog->withoutPassword(false);
52         
53          $args_array = explode("/",$args);
54          $params = new ArrayObject();
55          $params['post_type'] = $args_array[0];
56          $params['post_url'] = implode("/",array_slice($args_array,1));
57          if ($params['post_type'] == "pages") $params['post_type'] = "page";
58
59          $_ctx->posts = $core->blog->getPosts($params);
60         
61          $core->blog->withoutPassword(true);
62         
63          if ($_ctx->posts->isEmpty()) {
64               # No entry
65               self::p404();
66          }
67         
68          $post_id = $_ctx->posts->post_id;
69          $post_password = $_ctx->posts->post_password;
70         
71          # Password protected entry
72          if ($post_password != '') {
73               # Get passwords cookie
74               if (isset($_COOKIE['dc_passwd'])) {
75                    $pwd_cookie = unserialize($_COOKIE['dc_passwd']);
76               } else {
77                    $pwd_cookie = array();
78               }
79               
80               # Check for match
81               if ((!empty($_POST['password']) && $_POST['password'] == $post_password)
82               || (isset($pwd_cookie[$post_id]) && $pwd_cookie[$post_id] == $post_password)) {
83                    $pwd_cookie[$post_id] = $post_password;
84                    setcookie('dc_passwd',serialize($pwd_cookie),0,'/');
85               } else {
86                    self::serveDocument('password-form.html','text/html',false);
87                    exit;
88               }
89          }
90         
91          return $_ctx->posts->post_type;   
92     }
93
94     protected static function serveDocument($tpl,$content_type='text/html',$http_cache=true,$http_etag=true)
95     {
96          global $core, $_ctx, $odf;
97
98          if ($content_type != 'application/vnd.oasis.opendocument.text') {
99               return parent::serveDocument($tpl,$content_type,$http_cache,$http_etag);
100          }
101
102          if ($_ctx->nb_entry_per_page === null) {
103               $_ctx->nb_entry_per_page = $core->blog->settings->nb_post_per_page;
104          }
105         
106          $tpl_file = $core->tpl->getFilePath($tpl);
107         
108          if (!$tpl_file) {
109               throw new Exception('Unable to find template');
110          }
111         
112          $odf = new dcOdf($tpl_file);
113          $odf->get_remote_images = $core->blog->settings->odt_import_images;
114          if ($_ctx->posts) {
115               $odf->xslparams["url"] = $_ctx->posts->getURL();
116               $odf->xslparams["heading_minus_level"] = 2;
117          } else {
118               // exporting the whole blog
119               $odf->xslparams["url"] = $core->blog->url;
120               $odf->xslparams["heading_minus_level"] = 1;
121          }
122          if ($core->blog->settings->odt_img_width) {
123               $odf->xslparams["img_default_width"] = $core->blog->settings->odt_img_width;
124          }
125          if ($core->blog->settings->odt_img_height) {
126               $odf->xslparams["img_default_height"] = $core->blog->settings->odt_img_height;
127          }
128
129          $odf->compile();
130
131          // Export the file to download
132          if ($_ctx->posts) {
133               $title = $_ctx->posts->post_title;
134          } else {
135               $title = $core->blog->name;
136          }
137          $odf->exportAsAttachedFile(str_replace('"','',$title).".odt");
138     }
139     
140}
141
142class tplOdt
143{
144
145     public static function odtLink($attr)
146     {
147          if (!odtUtils::checkConfig()) { return; } # config problem
148          return odtUtils::getButton($attr);
149     }
150
151     # Widget function
152     public static function odtWidget($w)
153     {
154          global $core, $_ctx;
155
156          if (!odtUtils::checkConfig()) { return; } # config problem
157         
158          if ($w->where == "allbuthome" && $core->url->type == 'default') {
159               return;
160          } elseif ($w->where == "home" && $core->url->type != 'default') {
161               return;
162          }
163         
164          $url = eval('return '.odtUtils::getLink().";");
165          $image_url = $core->blog->getQmarkURL().'pf=odt/img/odt.png';
166          $res =
167          '<div class="odt">'.
168          ($w->title ? '<h2>'.html::escapeHTML($w->title).'</h2>' : '').
169          '<p><a href="'.$url.'" title="'.__("Export to ODT").
170          '"><img alt="ODT" class="odt" src="'.$image_url.
171          '" style="vertical-align:middle" /></a> <a href="'.$url.'">'.
172          ($w->link_title ? html::escapeHTML($w->link_title) : __('Export to ODT')).
173          '</a></p>'.
174          '</div>';
175         
176          return $res;
177     }
178}
179
180class odtBehaviors
181{
182
183     public static function publicHeadContent($core,$_ctx)
184     {
185          if (!odtUtils::checkConfig()) { return; } # config problem
186          if (!$core->blog->settings->odt_behavior) { return; } # disabled
187          echo "\n<!-- ODT export -->\n";
188          echo (
189               '<style type="text/css" media="screen">'.
190               '@import url('.$core->blog->getQmarkURL().
191               'pf=odt/style.css);</style>'."\n"
192               );
193     }
194
195     public static function publicEntryBeforeContent($core,$_ctx)
196     {
197          if (!odtUtils::checkConfig()) { return; } # config problem
198          # Not on the home page
199          if ($core->url->type == "default" 
200                    or $core->url->type == "default-page") { 
201               return;
202          }
203          if ($core->blog->settings->odt_behavior != "top") { return; }
204          echo (odtUtils::getButton(false,"top"));
205     }
206
207     public static function publicEntryAfterContent($core,$_ctx)
208     {
209          if (!odtUtils::checkConfig()) { return; } # config problem
210          # Not on the home page
211          if ($core->url->type == "default" 
212                    or $core->url->type == "default-page") { 
213               return;
214          }
215          if ($core->blog->settings->odt_behavior != "bottom") { return; }
216          echo (odtUtils::getButton(false,"bottom"));
217     }
218}
219?>
Note: See TracBrowser for help on using the repository browser.

Sites map