Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
CmsuserauthenticationModule.php
1 <?php
2 /**
3  * module for cms user authentication
4  */
5 class CmsuserauthenticationModule extends CWebModule
6 {
7  public $authUrl = '';
8  public $salt_secretkey = '';
9  public $username_sessionattr = '';
10  public $cmsBackendUrl = '';
11  public $crypt_secretkey = '';
12  private $key = "secretKey123#";
13  private $CMS_sid_object;
14 
15  /**
16  * starts the authentication request
17  */
18  public function startRequest()
19  {
20  $username = '';
21  $password = '';
22 
23  $user_id = Yii::app()->user->id;
24 
25  $user = User::model()->findByAttributes(array(
26  'id' => $user_id
27  ));
28 
29  if (isset($this->username_sessionattr) && ($this->username_sessionattr == 'username')) {
30  $username = $user->username;
31  } else {
32  $username = $user->email;
33  }
34 
35  /**
36  if(isset(Yii::app()->user->{$this->username_sessionattr})){
37  $username = Yii::app()->user->{$this->username_sessionattr};
38  }
39  **/
40 
41  $password = md5($this->salt_secretkey . $username);
42  $CMS_sid = $this->sendRequest($username, $password);
43 
44  if (isset($CMS_sid) && isset($CMS_sid->{'sid'})) {
45  YII::app()->session->add('feSid', $CMS_sid->{'sid'});
46  $this->CMS_sid_object = $CMS_sid;
47  }
48  }
49 
50  /**
51  * print out the path to backend cms with sid and encrypted cookie data
52  */
53  public function getProfileAttr($attributename = "")
54  {
55  $attributevalue = "";
56 
57  $attributevalue = Yii::app()->getModule('user')->user()->profile->getAttribute($attributename);
58 
59  return $attributevalue;
60  }
61 
62  public function printBackendPath()
63  {
64  $username = $password = '';
65 
66  include_once 'encryption_class.php';
67  $user_id = Yii::app()->user->id;
68 
69  $user = User::model()->findByAttributes(array(
70  'id' => $user_id
71  ));
72 
73  if (isset($this->username_sessionattr) && ($this->username_sessionattr == 'username')) {
74  $username = $user->username;
75  } else {
76  $username = $user->email;
77  }
78 
79  $profile_firstname = $this->getProfileAttr('firstname');
80  $profile_lastname = $this->getProfileAttr('lastname');
81  $profile_cmseditor = $this->getProfileAttr('cmseditor');
82 
83  $password = md5($this->salt_secretkey . $username);
84  if (!isset($CMS_sid)) {
85  $CMS_sid = $this->sendRequest($username, $password);
86  } else {
87  $CMS_sid = $this->CMS_sid_object;
88  }
89 
90  if (isset($CMS_sid->{'sid'}))
91  YII::app()->session->add('feSid', $CMS_sid->{'sid'});
92 
93  if (isset($CMS_sid->{'secret'}) && isset($CMS_sid->{'sid'})) {
94  echo $this->cmsBackendUrl . '?sid=' . $CMS_sid->{'sid'} . '&value=' . $this->encrypt($CMS_sid->{'secret'});
95 
96  } elseif (isset($profile_cmseditor) && $profile_cmseditor == 1) {
97  if (isset($this->crypt_secretkey) && $this->crypt_secretkey != '') {
98  } else {
99  $this->crypt_secretkey = $this->key;
100  }
101  echo $this->cmsBackendUrl . '?u=' . $this->encrypt($user->username) . '&f=' . $this->encrypt($profile_firstname) . '&l=' . $this->encrypt($profile_lastname) . '&e=' . $this->encrypt($user->email) . '&p=' . $this->encrypt($password) . '&c=' . $this->encrypt($this->crypt_secretkey) . '&ch=' . $this->username_sessionattr;
102  } else {
103  // cms auth failed;
104  echo "#";
105  }
106  }
107 
108  /**
109  * sends a POST request to the GCN Rest API login service (http://www.gentics.com/Content.Node/guides/restapi/resource_AuthenticationResource.html#path__auth_login.html)
110  * sets the SessionSecret Cookie for the Client
111  * returns a valid CMS User Session
112  */
113  private function sendRequest($username, $password)
114  {
115  $url = $this->authUrl;
116  $data = json_encode(array(
117  "login" => $username,
118  "password" => $password
119  ));
120 
121  if (!in_array('curl', get_loaded_extensions())) {
122  Yii::log('Error: curl not installed', 'trace', 'exception.CDbException');
123  echo '<script language = "JavaScript" type = "text/JavaScript">alert("Error: curl not installed");</script>';
124  return;
125  }
126 
127  $curl = curl_init();
128  curl_setopt($curl, CURLOPT_URL, $url);
129  curl_setopt($curl, CURLOPT_POST, 1);
130  curl_setopt($curl, CURLOPT_HEADER, 1);
131  curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
132  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
133  curl_setopt($curl, CURLOPT_HTTPHEADER, array(
134  "Content-Type: application/json; charset=utf-8"
135  ));
136  curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
137 
138  $response = curl_exec($curl);
139 
140 
141  //echo "\">"; print_r($response); echo $username . "<br>" . $password . "<br>" . $url . "</a><a href=\"";
142 
143 
144  $info = curl_getinfo($curl);
145  $header = substr($response, 0, $info['header_size']);
146  $headers = ($this->http_parse_headers($header));
147  $body = substr($response, $info['header_size'], strlen($response) - 1);
148  $body = json_decode($body);
149  curl_close($curl);
150  if (isset($headers["Set-Cookie"])) {
151  $cookie = $headers["Set-Cookie"];
152  $key = substr($cookie, 0, strpos($cookie, '='));
153  $value = substr($cookie, strpos($cookie, '=') + 1, strlen($cookie) - 1);
154  $value = str_replace("; Path=/", "", $value);
155  // new !!! KMS
156  $value = str_replace("; HttpOnly", "", $value);
157  setcookie($key, $value, time() + 60 * 60 * 24, '/');
158  //setcookie ($key, $value,time()+60*60*24, '/',$this->cmsBackendDomain);
159  //return $body['sid'];
160  $body->{'secret'} = $value;
161  return $body;
162  } else {
163  Yii::log('Error: Authentication on CMS failed', 'trace', 'exception.CDbException');
164  echo '';
165  return;
166  }
167  }
168 
169  /**
170  * http_parse_headers function from pecl documentation
171  *
172  */
173  private function http_parse_headers($header)
174  {
175  $retVal = array();
176  $fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $header));
177  foreach ($fields as $field) {
178  if (preg_match('/([^:]+): (.+)/m', $field, $match)) {
179  $match[1] = preg_replace('/(?<=^|[\x09\x20\x2D])./e', 'strtoupper("\0")', strtolower(trim($match[1])));
180 
181  if (isset($retVal[$match[1]])) {
182  $retVal[$match[1]] = array(
183  $retVal[$match[1]],
184  $match[2]
185  );
186  } else {
187  $retVal[$match[1]] = trim($match[2]);
188  }
189  }
190  }
191  return $retVal;
192  }
193  /*
194  * encrypt data with secret Key
195  */
196  private function encrypt($val)
197  {
198  if (isset($this->crypt_secretkey) && $this->crypt_secretkey != "")
199  $this->key = $this->crypt_secretkey;
200  $crypt = new encryption_class();
201  $min_length = 8;
202  $encrypt_result = $crypt->encrypt($this->key, $val, $min_length);
203  return urlencode($encrypt_result);
204  }
205 }
206 ?>