1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* @author Alexey Tatarinov <[email protected]>
|
4
|
|
|
* @link https://github.com/shogodev/argilla/
|
5
|
|
|
* @copyright Copyright © 2003-2014 Shogo
|
6
|
|
|
* @license http://argilla.ru/LICENSE
|
7
|
|
|
* @package
|
8
|
|
|
*/
|
9
|
|
|
Yii::import('ext.cackle.models.*');
|
10
|
|
|
Yii::import('ext.cackle.*');
|
11
|
|
|
|
12
|
|
|
class Cackle extends CApplicationComponent
|
13
|
|
|
{
|
14
|
|
|
protected $siteId;
|
15
|
|
|
|
16
|
|
|
protected $siteApiKey;
|
17
|
|
|
|
18
|
|
|
protected $singleSignOn = false;
|
19
|
|
|
|
20
|
|
View Code Duplication |
public function init()
|
|
|
|
|
21
|
|
|
{
|
22
|
|
|
parent::init();
|
23
|
|
|
|
24
|
|
|
$configPath = Yii::getPathOfAlias('frontend.config.cackle').'.php';
|
25
|
|
|
if( file_exists($configPath) )
|
26
|
|
|
{
|
27
|
|
|
$config = require($configPath);
|
28
|
|
|
$this->siteId = $config['siteId'];
|
29
|
|
|
$this->siteApiKey = $config['siteApiKey'];
|
30
|
|
|
|
31
|
|
|
if( isset($config['singleSignOn']) )
|
32
|
|
|
$this->singleSignOn = $config['singleSignOn'];
|
33
|
|
|
}
|
34
|
|
|
else
|
35
|
|
|
{
|
36
|
|
|
throw new CHttpException('500', 'Не найден кофигурационный файл cackle.php в папке config');
|
37
|
|
|
}
|
38
|
|
|
}
|
39
|
|
|
|
40
|
|
|
/**
|
41
|
|
|
* @param CActiveRecord|string $channel
|
42
|
|
|
* @param null $containerId
|
43
|
|
|
*/
|
44
|
|
View Code Duplication |
public function comments($channel, $containerId = null)
|
|
|
|
|
45
|
|
|
{
|
46
|
|
|
Yii::app()->controller->widget('CackleWidget', array(
|
47
|
|
|
'widget' => CackleWidget::COMMENT,
|
48
|
|
|
'siteId' => $this->siteId,
|
49
|
|
|
'channel' => $channel,
|
50
|
|
|
'container' => $containerId,
|
51
|
|
|
'ssoAuth' => $this->singleSignOn ? $this->getSingleSignOnString() : '',
|
52
|
|
|
));
|
53
|
|
|
}
|
54
|
|
|
|
55
|
|
|
/**
|
56
|
|
|
* @param $channel
|
57
|
|
|
* @param null $containerId
|
58
|
|
|
*/
|
59
|
|
View Code Duplication |
public function reviews($channel, $containerId = null)
|
|
|
|
|
60
|
|
|
{
|
61
|
|
|
Yii::app()->controller->widget('CackleWidget', array(
|
62
|
|
|
'widget' => CackleWidget::REVIEW,
|
63
|
|
|
'siteId' => $this->siteId,
|
64
|
|
|
'channel' => $channel,
|
65
|
|
|
'container' => $containerId,
|
66
|
|
|
'ssoAuth' => $this->singleSignOn ? $this->getSingleSignOnString() : array()
|
67
|
|
|
));
|
68
|
|
|
}
|
69
|
|
|
|
70
|
|
|
protected function getSingleSignOnString()
|
71
|
|
|
{
|
72
|
|
|
$user = array();
|
73
|
|
|
|
74
|
|
|
if( !Yii::app()->user->isGuest )
|
75
|
|
|
{
|
76
|
|
|
/**
|
77
|
|
|
* @link http://admin.cackle.me/help/integrating-sso
|
78
|
|
|
*/
|
79
|
|
|
$user = array(
|
80
|
|
|
'id' => Yii::app()->user->data->id,
|
81
|
|
|
'name' => Yii::app()->user->profile->name,
|
82
|
|
|
'email' => Yii::app()->user->getEmail(),
|
83
|
|
|
'avatar' => Yii::app()->createAbsoluteUrl('userProfile/avatar', array('id' => Yii::app()->user->data->id)),
|
84
|
|
|
);
|
85
|
|
|
}
|
86
|
|
|
$currentTime = time();
|
87
|
|
|
|
88
|
|
|
$singleSignOn = array();
|
89
|
|
|
$singleSignOn['userData'] = base64_encode(!empty($user) ? json_encode($user) : '{}');
|
90
|
|
|
$singleSignOn['sign'] = md5($singleSignOn['userData'].$this->siteApiKey.$currentTime);
|
91
|
|
|
$singleSignOn['timestamp'] = $currentTime;
|
92
|
|
|
|
93
|
|
|
return implode(' ', $singleSignOn);
|
94
|
|
|
}
|
95
|
|
|
|
96
|
|
|
protected function setSiteId($siteId)
|
97
|
|
|
{
|
98
|
|
|
$this->siteId = $siteId;
|
99
|
|
|
}
|
100
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.