1<?php
2
3namespace dokuwiki\plugin\blogtng\entities;
4
5use helper_plugin_blogtng_tools;
6
7/**
8 * Simple wrapper class for a single comment object
9 */
10class Comment{
11
12    /** @var array @deprecated 2022-08-13 */
13    public $data;
14    /** @var int */
15    private $num;
16    /**
17     * @var helper_plugin_blogtng_tools
18     */
19    private $tools;
20
21    //data, corresponds to comment entry in database
22    /** @var string */
23    private $cid;
24    /** @var string page id */
25    private $pid;
26    /** @var string */
27    private $source;
28    /** @var string */
29    private $status;
30    /** @var string */
31    private $text;
32    /** @var string */
33    private $name;
34    /** @var string */
35    private $web;
36    /** @var string */
37    private $mail;
38    /** @var string */
39    private $type;
40    /** @var string */
41    private $created;
42    /** @var string */
43    private $avatar;
44    /** @var string */
45    private $ip;
46
47    //extra temporary stored data
48    /** @var string */
49    private $subscribe;
50
51    /**
52     * Resets the internal data with a given row
53     *
54     * @param array $row associated array as returned from database row of table 'comments'
55     */
56    public function __construct($row = null){
57        $this->tools = new helper_plugin_blogtng_tools();
58        if(is_array($row)) {
59            $this->init($row);
60        }
61    }
62
63    /**
64     * @param array $row row of table 'comments'
65     */
66    public function init($row){
67        foreach ($row as $key => $item) {
68            if(property_exists($this, $key)) {
69                $this->{$key} = $item;
70            }
71        }
72        //old data row, for backward compatibility in existing templates
73        $this->data = $row;
74    }
75
76    /**
77     * Render a comment using the templates comments file.
78     *
79     * @param string $name of template
80     * @return bool
81     */
82    public function output($name){
83        global $INFO;
84        $name = preg_replace('/[^a-zA-Z_\-]+/','',$name);
85        $tpl = $this->tools->getTplFile($name,'comments');
86        if($tpl === false){
87            return false;
88        }
89
90        $comment = $this;
91        if($comment->status == 'visible' || ($comment->status == 'hidden' && $INFO['isadmin'])) {
92            $comment->num++;
93            include($tpl);
94        }
95        return true;
96    }
97
98    /**
99     * Get translated string for @$name
100     *
101     * @param   string  $name     id of the string to be retrieved
102     * @return  string  string in appropriate language or english if not available
103     */
104    public function getLang($name){
105        return $this->tools->getLang($name);
106    }
107
108    /**
109     * Render the text/content of a single comment
110     */
111    public function tpl_comment(){
112        //FIXME add caching
113
114        $inst = p_get_instructions($this->text);
115        echo p_render('blogtng_comment',$inst,$info);
116    }
117
118    /**
119     * Render the cid of a single comment
120     */
121    public function tpl_cid(){
122        echo $this->cid;
123    }
124
125    /**
126     * Render the number of a comment.
127     *
128     * @param bool   $link  whether wrapped with link element
129     * @param string $fmt   format of number
130     * @param string $title null or alternative title
131     */
132    public function tpl_number($link = true, $fmt = '%d', $title = null) {
133        if($title === null) $title = sprintf($fmt, $this->num);
134
135        if($link) echo '<a href="#comment_' . $this->cid . '" class="blogtng_num">';
136        echo $title;
137        if($link) echo '</a>';
138    }
139
140    /**
141     * Render the hcard/userdata of a single comment
142     */
143    public function tpl_hcard(){
144        echo '<div class="vcard">';
145        if($this->web){
146            echo '<a href="'.hsc($this->web).'" class="fn nickname">';
147            echo hsc($this->name);
148            echo '</a>';
149        }else{
150            echo '<span class="fn nickname">';
151            echo hsc($this->name);
152            echo '</span>';
153        }
154        echo '</div>';
155    }
156
157    /**
158     * Render the name of a single comment
159     */
160    public function tpl_name(){
161        echo hsc($this->name);
162    }
163
164    /**
165     * Render the type of a single comment
166     */
167    public function tpl_type(){
168        echo hsc($this->type);
169    }
170
171    /**
172     * Render the mail of a single comment
173     */
174    public function tpl_mail(){
175        echo hsc($this->mail);
176    }
177
178    /**
179     * Render the web address of a single comment
180     */
181    public function tpl_web(){
182        echo hsc($this->web);
183    }
184
185    /**
186     * Render the creation date of a single comment
187     *
188     * @param string $fmt date format, empty string default to $conf['dformat']
189     */
190    public function tpl_created($fmt=''){
191        echo hsc(dformat($this->created,$fmt));
192    }
193
194    /**
195     * Render the status of a single comment
196     */
197    public function tpl_status() {
198        echo $this->status;
199    }
200
201    /**
202     * Render the avatar of a single comment
203     *
204     * @param int $w avatar width
205     * @param int $h avatar height
206     * @param bool $return whether the url is returned or printed
207     * @return void|string url of avatar
208     */
209    public function tpl_avatar($w=0,$h=0,$return=false){
210        global $conf;
211        $img = '';
212        if($this->avatar) {
213            $img = $this->avatar;
214            //FIXME add hook for additional methods
215        } elseif ($this->mail) {
216            $dfl = $conf['plugin']['blogtng']['comments_gravatar_default'];
217            if(!isset($dfl) || $dfl == 'blank') $dfl = DOKU_URL . 'lib/images/blank.gif';
218
219            $img = 'https://gravatar.com/avatar.php'
220                . '?gravatar_id=' . md5($this->mail)
221                . '&size=' . $w
222                . '&rating=' . $conf['plugin']['blogtng']['comments_gravatar_rating']
223                . '&default='.rawurlencode($dfl)
224                . '&.png';
225        } elseif ($this->web){
226            $img = 'https://getfavicon.appspot.com/'.rawurlencode($this->web).'?.png';
227        }
228
229
230        //use fetch for caching and resizing
231        if($img){
232            $img = ml($img,array('w'=>$w,'h'=>$h,'cache'=>'recache'));
233        }
234        if($return) {
235            return $img;
236        } else {
237            print $img;
238        }
239    }
240
241    /**
242     * @return string
243     */
244    public function getStatus()
245    {
246        return $this->status;
247    }
248
249    /**
250     * @param string $status
251     */
252    public function setStatus($status)
253    {
254        $this->status = $status;
255    }
256
257    /**
258     * @return string
259     */
260    public function getText()
261    {
262        return $this->text;
263    }
264
265    /**
266     * @param string $text
267     */
268    public function setText($text)
269    {
270        $this->text = $text;
271    }
272
273    /**
274     * @return string
275     */
276    public function getCid()
277    {
278        return $this->cid;
279    }
280
281    /**
282     * @param string $cid
283     */
284    public function setCid($cid)
285    {
286        $this->cid = $cid;
287    }
288
289    /**
290     * @return string
291     */
292    public function getName()
293    {
294        return $this->name;
295    }
296
297    /**
298     * @param string $name
299     */
300    public function setName($name)
301    {
302        $this->name = $name;
303    }
304
305    /**
306     * @return string
307     */
308    public function getWeb()
309    {
310        return $this->web;
311    }
312
313    /**
314     * @param string $web
315     */
316    public function setWeb($web)
317    {
318        $this->web = $web;
319    }
320
321    /**
322     * @return string
323     */
324    public function getMail()
325    {
326        return $this->mail;
327    }
328
329    /**
330     * @param string $mail
331     */
332    public function setMail($mail)
333    {
334        $this->mail = $mail;
335    }
336
337    /**
338     * @return string
339     */
340    public function getType()
341    {
342        return $this->type;
343    }
344
345    /**
346     * @param string $type
347     */
348    public function setType($type)
349    {
350        $this->type = $type;
351    }
352
353    /**
354     * @return string
355     */
356    public function getCreated()
357    {
358        return $this->created;
359    }
360
361    /**
362     * @param string $created
363     */
364    public function setCreated($created)
365    {
366        $this->created = $created;
367    }
368
369    /**
370     * @return string
371     */
372    public function getAvatar()
373    {
374        return $this->avatar;
375    }
376
377    /**
378     * @param string $avatar
379     */
380    public function setAvatar($avatar)
381    {
382        $this->avatar = $avatar;
383    }
384
385    /**
386     * @return string
387     */
388    public function getSource()
389    {
390        return $this->source;
391    }
392
393    /**
394     * @param string $source
395     */
396    public function setSource($source)
397    {
398        $this->source = $source;
399    }
400
401    /**
402     * @return string
403     */
404    public function getIp()
405    {
406        return $this->ip;
407    }
408
409    /**
410     * @param string $ip
411     */
412    public function setIp($ip)
413    {
414        $this->ip = $ip;
415    }
416
417    /**
418     * @return string
419     */
420    public function getPid()
421    {
422        return $this->pid;
423    }
424
425    /**
426     * @param string $pid
427     */
428    public function setPid($pid)
429    {
430        $this->pid = $pid;
431    }
432
433    /**
434     * @return string
435     */
436    public function getSubscribe()
437    {
438        return $this->subscribe;
439    }
440
441    /**
442     * @param string $subscribe
443     */
444    public function setSubscribe($subscribe)
445    {
446        $this->subscribe = $subscribe;
447    }
448}
449