SweOptipricer - Version 0.1.3

Version Notes

Testing candidate

Download this release

Release Info

Developer Rui Mendes
Extension SweOptipricer
Version 0.1.3
Comparing to
See all releases


Code changes from version 0.1.2 to 0.1.3

app/code/community/SWE/Optipricer/Block/Button.php CHANGED
@@ -6,7 +6,7 @@
6
  * @author Ubiprism Lda. / be.ubi <contact@beubi.com>
7
  * @copyright 2015 be.ubi
8
  * @license GNU Lesser General Public License (LGPL)
9
- * @version v.0.1.2
10
  */
11
  class SWE_Optipricer_Block_Button extends Mage_Adminhtml_Block_System_Config_Form_Field
12
  {
6
  * @author Ubiprism Lda. / be.ubi <contact@beubi.com>
7
  * @copyright 2015 be.ubi
8
  * @license GNU Lesser General Public License (LGPL)
9
+ * @version v.0.1.3
10
  */
11
  class SWE_Optipricer_Block_Button extends Mage_Adminhtml_Block_System_Config_Form_Field
12
  {
app/code/community/SWE/Optipricer/Block/Discount.php CHANGED
@@ -6,7 +6,7 @@
6
  * @author Ubiprism Lda. / be.ubi <contact@beubi.com>
7
  * @copyright 2015 be.ubi
8
  * @license GNU Lesser General Public License (LGPL)
9
- * @version v.0.1.2
10
  */
11
  class SWE_Optipricer_Block_Discount extends Mage_Core_Block_Template implements Mage_Widget_Block_Interface
12
  {
@@ -126,6 +126,7 @@ class SWE_Optipricer_Block_Discount extends Mage_Core_Block_Template implements
126
  return $html;
127
  }
128
 
 
129
  //get ProductDetails and other relevant data to the requests
130
  $data = $this->getProductDetails();
131
  $data['min'] = $this->minDiscount;
@@ -134,7 +135,7 @@ class SWE_Optipricer_Block_Discount extends Mage_Core_Block_Template implements
134
  $data['discount_render'] = $this->renderView;
135
  $data['expiry_offset'] = $this->expiryOffset;
136
  $data['social_credentials'] = array('facebookId' => '', 'facebookToken' => '');
137
- $securedData['data'] = $this->secureContent(json_encode($data));
138
  $securedData['social_credentials'] = $data['social_credentials'];
139
 
140
  //PageView feature
@@ -312,66 +313,4 @@ class SWE_Optipricer_Block_Discount extends Mage_Core_Block_Template implements
312
  $this->maxDiscount = $this->minDiscount;
313
  }
314
  }
315
-
316
- /**
317
- * Method to secure content
318
- *
319
- * @param String $content Content
320
- * @param String $task Task to be done
321
- *
322
- * @return String
323
- */
324
- private function secureContent($content, $task = 'encrypt')
325
- {
326
- if ($task == 'decrypt') {
327
- $contentSecured = $this->decryptContent($this->key, $content);
328
- } else {
329
- $contentSecured = $this->encryptContent($this->key, $content);
330
- }
331
-
332
- return $contentSecured;
333
- }
334
-
335
- /**
336
- * Encrypt content
337
- *
338
- * @param String $key Key
339
- * @param String $content Content
340
- *
341
- * @return string
342
- */
343
- private function encryptContent($key, $content)
344
- {
345
- $ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
346
- $iv = mcrypt_create_iv($ivSize, MCRYPT_DEV_RANDOM);
347
- $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $content, MCRYPT_MODE_CBC, $iv);
348
- $ciphertextArr = array('cipher' => base64_encode($ciphertext), 'iv' => base64_encode($iv));
349
- $ciphertextArr = json_encode($ciphertextArr);
350
- $ciphertextBase64 = base64_encode($ciphertextArr);
351
-
352
- return $ciphertextBase64;
353
- }
354
-
355
- /**
356
- * Decrypt content
357
- *
358
- * @param String $key Key
359
- * @param String $cipher Cipher
360
- *
361
- * @return string
362
- */
363
- private function decryptContent($key, $cipher)
364
- {
365
- $ciphertextDec = base64_decode($cipher);
366
- $ciphertextDec = json_decode($ciphertextDec);
367
- $content = trim(mcrypt_decrypt(
368
- MCRYPT_RIJNDAEL_128,
369
- $key,
370
- base64_decode($ciphertextDec->cipher),
371
- MCRYPT_MODE_CBC,
372
- base64_decode($ciphertextDec->iv)
373
- ));
374
-
375
- return $content;
376
- }
377
  }
6
  * @author Ubiprism Lda. / be.ubi <contact@beubi.com>
7
  * @copyright 2015 be.ubi
8
  * @license GNU Lesser General Public License (LGPL)
9
+ * @version v.0.1.3
10
  */
11
  class SWE_Optipricer_Block_Discount extends Mage_Core_Block_Template implements Mage_Widget_Block_Interface
12
  {
126
  return $html;
127
  }
128
 
129
+ $secureData = Mage::helper('optipricer/Securedata');
130
  //get ProductDetails and other relevant data to the requests
131
  $data = $this->getProductDetails();
132
  $data['min'] = $this->minDiscount;
135
  $data['discount_render'] = $this->renderView;
136
  $data['expiry_offset'] = $this->expiryOffset;
137
  $data['social_credentials'] = array('facebookId' => '', 'facebookToken' => '');
138
+ $securedData['data'] = $secureData::secureContent($secureData::SECURE_CIPHER, json_encode($data), $this->key);
139
  $securedData['social_credentials'] = $data['social_credentials'];
140
 
141
  //PageView feature
313
  $this->maxDiscount = $this->minDiscount;
314
  }
315
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
316
  }
app/code/community/SWE/Optipricer/Helper/Data.php CHANGED
@@ -6,7 +6,7 @@
6
  * @author Ubiprism Lda. / be.ubi <contact@beubi.com>
7
  * @copyright 2015 be.ubi
8
  * @license GNU Lesser General Public License (LGPL)
9
- * @version v.0.1.2
10
  */
11
  class SWE_Optipricer_Helper_Data extends Mage_Core_Helper_Abstract
12
  {
6
  * @author Ubiprism Lda. / be.ubi <contact@beubi.com>
7
  * @copyright 2015 be.ubi
8
  * @license GNU Lesser General Public License (LGPL)
9
+ * @version v.0.1.3
10
  */
11
  class SWE_Optipricer_Helper_Data extends Mage_Core_Helper_Abstract
12
  {
app/code/community/SWE/Optipricer/Helper/Securedata.php ADDED
@@ -0,0 +1,319 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * Widget to share the product in Facebook and get a personalized discount
5
+ *
6
+ * @package SWE_Optipricer
7
+ * @author Ubiprism Lda. / be.ubi <contact@beubi.com>
8
+ * @copyright 2015 be.ubi
9
+ * @license GNU Lesser General Public License (LGPL)
10
+ * @version v.0.1.3
11
+ */
12
+ class SWE_Optipricer_Helper_Securedata extends Mage_Core_Helper_Abstract
13
+ {
14
+ const KEY_FILENAME = 'swe_private_key.pem';
15
+
16
+ const CIPHER_ALG = MCRYPT_RIJNDAEL_128;
17
+ const CIPHER_MODE = MCRYPT_MODE_CBC;
18
+ const CIPHER_ALG_MODE = "AES-128-CBC";
19
+
20
+ const SIGN_ALG = OPENSSL_ALGO_SHA256;
21
+
22
+ const SECURE_CIPHER = 1;
23
+ const SECURE_SIGN = 2;
24
+ const SECURE_CIPHER_SIGN = 3;
25
+
26
+ private static $cipherArray = array(
27
+ "AES-128-CBC" => array("alg" => MCRYPT_RIJNDAEL_128, "mode" => MCRYPT_MODE_CBC),
28
+ "AES-128-CFB" => array("alg" => MCRYPT_RIJNDAEL_128, "mode" => MCRYPT_MODE_CFB),
29
+ "AES-128-OFB" => array("alg" => MCRYPT_RIJNDAEL_128, "mode" => MCRYPT_MODE_OFB),
30
+ "AES-128-ECB" => array("alg" => MCRYPT_RIJNDAEL_128, "mode" => MCRYPT_MODE_ECB),
31
+ "AES-192-CBC" => array("alg" => MCRYPT_RIJNDAEL_192, "mode" => MCRYPT_MODE_CBC),
32
+ "AES-192-CFB" => array("alg" => MCRYPT_RIJNDAEL_192, "mode" => MCRYPT_MODE_CFB),
33
+ "AES-192-OFB" => array("alg" => MCRYPT_RIJNDAEL_192, "mode" => MCRYPT_MODE_OFB),
34
+ "AES-192-ECB" => array("alg" => MCRYPT_RIJNDAEL_192, "mode" => MCRYPT_MODE_ECB),
35
+ "AES-256-CBC" => array("alg" => MCRYPT_RIJNDAEL_256, "mode" => MCRYPT_MODE_CBC),
36
+ "AES-256-CFB" => array("alg" => MCRYPT_RIJNDAEL_256, "mode" => MCRYPT_MODE_CFB),
37
+ "AES-256-OFB" => array("alg" => MCRYPT_RIJNDAEL_256, "mode" => MCRYPT_MODE_OFB),
38
+ "AES-256-ECB" => array("alg" => MCRYPT_RIJNDAEL_256, "mode" => MCRYPT_MODE_ECB),
39
+ "BF-CBC" => array("alg" => MCRYPT_BLOWFISH, "mode" => MCRYPT_MODE_CBC),
40
+ "BF-CFB" => array("alg" => MCRYPT_BLOWFISH, "mode" => MCRYPT_MODE_CFB),
41
+ "BF-OFB" => array("alg" => MCRYPT_BLOWFISH, "mode" => MCRYPT_MODE_OFB),
42
+ "BF-ECB" => array("alg" => MCRYPT_BLOWFISH, "mode" => MCRYPT_MODE_ECB),
43
+ "CAST-128-CBC" => array("alg" => MCRYPT_CAST_128, "mode" => MCRYPT_MODE_CBC),
44
+ "CAST-128-CFB" => array("alg" => MCRYPT_CAST_128, "mode" => MCRYPT_MODE_CFB),
45
+ "CAST-128-OFB" => array("alg" => MCRYPT_CAST_128, "mode" => MCRYPT_MODE_OFB),
46
+ "CAST-128-ECB" => array("alg" => MCRYPT_CAST_128, "mode" => MCRYPT_MODE_ECB),
47
+ "CAST-256-CBC" => array("alg" => MCRYPT_CAST_256, "mode" => MCRYPT_MODE_CBC),
48
+ "CAST-256-CFB" => array("alg" => MCRYPT_CAST_256, "mode" => MCRYPT_MODE_CFB),
49
+ "CAST-256-OFB" => array("alg" => MCRYPT_CAST_256, "mode" => MCRYPT_MODE_OFB),
50
+ "CAST-256-ECB" => array("alg" => MCRYPT_CAST_256, "mode" => MCRYPT_MODE_ECB),
51
+ );
52
+
53
+ /**
54
+ * Method to secure content (can use different security methods)
55
+ *
56
+ * @param Int $task Task
57
+ * @param string $content Content
58
+ * @param bool|string $key Key
59
+ * @param bool|string $privKey PrivateKey
60
+ *
61
+ * @return array|bool|string
62
+ */
63
+ public static function secureContent($task, $content, $key = false, $privKey = false)
64
+ {
65
+ switch($task)
66
+ {
67
+ case self::SECURE_CIPHER:
68
+ return self::encryptContent($key,$content);
69
+ break;
70
+ case self::SECURE_SIGN:
71
+ return self::signContent($privKey, $content);
72
+ break;
73
+ case self::SECURE_CIPHER_SIGN:
74
+ return self::encryptSignContent($key, $privKey, $content);
75
+ break;
76
+ default:
77
+ return false;
78
+ }
79
+ }
80
+
81
+ /**
82
+ * Get content of a secured data object
83
+ *
84
+ * @param string $content Content
85
+ * @param bool|string $key Key flag
86
+ * @param bool|string $publicKey PublicKey flag
87
+ *
88
+ * @return bool|string
89
+ */
90
+ public static function getContent($content, $key = false, $publicKey = false)
91
+ {
92
+ if (is_array($content)) {
93
+ return false;
94
+ }
95
+
96
+ $data = base64_decode($content);
97
+ if (!$data) {
98
+ //Content is not a base64 encode object
99
+ $data = $content;
100
+ }
101
+
102
+ $data = json_decode($data);
103
+ $mode = isset($data->smode) ? $data->smode : self::SECURE_CIPHER;
104
+ if ($mode == self::SECURE_CIPHER && !isset($data->iv)) {
105
+ return false;
106
+ }
107
+ $obj = isset($data->content) ? $data->content : $data->cipher;
108
+
109
+ switch($mode)
110
+ {
111
+ case self::SECURE_CIPHER:
112
+ return self::decryptContent($key, $data);
113
+ break;
114
+ case self::SECURE_SIGN:
115
+ if(self::verifySignature($publicKey, $data))
116
+ return $obj;
117
+ else
118
+ return false;
119
+ break;
120
+ case self::SECURE_CIPHER_SIGN:
121
+ return self::decryptVerifyContent($key, $publicKey, $data);
122
+ break;
123
+ default:
124
+ return false;
125
+ }
126
+ }
127
+
128
+ /**
129
+ * Encrypt content
130
+ *
131
+ * @param string $key Key
132
+ * @param string $content Content
133
+ *
134
+ * @return string
135
+ */
136
+ private static function encryptContent($key, $content)
137
+ {
138
+ $ivSize = mcrypt_get_iv_size(self::CIPHER_ALG, self::CIPHER_MODE);
139
+ $iv = '';
140
+ if($ivSize > 0)
141
+ $iv = mcrypt_create_iv($ivSize, MCRYPT_DEV_URANDOM);
142
+
143
+ // creates a cipher text compatible with AES (Rijndael block size = 128) to keep the text confidential
144
+ // only suitable for encoded input that never ends with value 00h (because of default zero padding)
145
+ $cipherText = mcrypt_encrypt(self::CIPHER_ALG, $key, $content, self::CIPHER_MODE, $iv);
146
+
147
+ // prepend the IV for it to be available for decryption
148
+ $cipherTextArray = array(
149
+ 'content' => base64_encode($cipherText),
150
+ 'iv' => base64_encode($iv),
151
+ 'alg' => self::CIPHER_ALG_MODE,
152
+ 'smode' => self::SECURE_CIPHER
153
+ );
154
+
155
+ $cipherTextArray = json_encode($cipherTextArray);
156
+
157
+ // encode the resulting cipher text so it can be represented by a string
158
+ // could be commented...
159
+ $cipherTextArray = base64_encode($cipherTextArray);
160
+
161
+ return $cipherTextArray;
162
+ }
163
+
164
+ /**
165
+ * Decrypt content
166
+ *
167
+ * @param string $key Key
168
+ * @param object $data Data
169
+ *
170
+ * @return string
171
+ */
172
+ private static function decryptContent($key, $data)
173
+ {
174
+ $cipherAlg = self::CIPHER_ALG;
175
+ $cipherMode = self::CIPHER_MODE;
176
+
177
+ if(isset($data->alg))
178
+ {
179
+ $cipherAlg = self::$cipherArray[$data->alg]['alg'];
180
+ $cipherMode = self::$cipherArray[$data->alg]['mode'];
181
+ }
182
+
183
+ $cipher = isset($data->content) ? $data->content : $data->cipher;
184
+ $content = mcrypt_decrypt($cipherAlg, $key, base64_decode($cipher), $cipherMode, base64_decode($data->iv));
185
+
186
+ return $content;
187
+ }
188
+
189
+
190
+ /**
191
+ * Method to sign content
192
+ *
193
+ * @param string $privateKey Private Key
194
+ * @param string $content Content
195
+ *
196
+ * @return array|string
197
+ */
198
+ private static function signContent($privateKey, $content)
199
+ {
200
+ openssl_sign($content, $signature, $privateKey, self::SIGN_ALG);
201
+ $obj = array(
202
+ 'content' => $content,
203
+ 'alg' => self::SIGN_ALG,
204
+ 'smode' => self::SECURE_SIGN,
205
+ 'sign' => base64_encode($signature)
206
+ );
207
+
208
+ $obj = json_encode($obj);
209
+ //$obj = base64_encode($obj);
210
+
211
+ return $obj;
212
+ }
213
+
214
+ /**
215
+ * Method to verify signature
216
+ *
217
+ * @param string $publicKey Public Key
218
+ * @param object $content Content
219
+ *
220
+ * @return bool
221
+ */
222
+ private static function verifySignature($publicKey, $content)
223
+ {
224
+ $signAlg = isset($content->alg) ? $content->alg : self::SIGN_ALG;
225
+
226
+ if (isset($content->content) && isset($content->sign)) {
227
+ //int 1 if the signature is correct, 0 if it is incorrect, and -1 on error.
228
+ $result = openssl_verify($content->content, base64_decode($content->sign), $publicKey, $signAlg);
229
+ return $result != 1;
230
+ } else {
231
+ return false;
232
+ }
233
+ }
234
+
235
+ /**
236
+ * Method to encrypt and sign content
237
+ *
238
+ * @param string $key Shared Key
239
+ * @param string $privKey Private Key
240
+ * @param string $content Content
241
+ *
242
+ * @return array|string
243
+ */
244
+ private static function encryptSignContent($key, $privKey, $content)
245
+ {
246
+ openssl_sign($content, $signature, $privKey, self::SIGN_ALG);
247
+
248
+ $ivSize = mcrypt_get_iv_size(self::CIPHER_ALG, self::CIPHER_MODE);
249
+
250
+ $iv = '';
251
+ if($ivSize > 0)
252
+ $iv = mcrypt_create_iv($ivSize, MCRYPT_DEV_URANDOM);
253
+
254
+ // creates a cipher text compatible with AES (Rijndael block size = 128) to keep the text confidential
255
+ // only suitable for encoded input that never ends with value 00h (because of default zero padding)
256
+ $ciphertext = mcrypt_encrypt(self::CIPHER_ALG, $key, $content, self::CIPHER_MODE, $iv);
257
+
258
+ $obj = array(
259
+ 'content' => base64_encode($ciphertext),
260
+ 'iv' => base64_encode($iv),
261
+ 'alg' => self::CIPHER_ALG_MODE . '|' . self::SIGN_ALG,
262
+ 'smode' => self::SECURE_CIPHER_SIGN,
263
+ 'sign' => base64_encode($signature)
264
+ );
265
+
266
+ $obj = json_encode($obj);
267
+
268
+ //is it really necessary?
269
+ //$obj = base64_encode($obj);
270
+
271
+ return $obj;
272
+ }
273
+
274
+ /**
275
+ * Method to decrypt and verify content signature
276
+ *
277
+ * @param string $key Shared Key
278
+ * @param string $pubKey Public Key
279
+ * @param object $content Content
280
+ *
281
+ * @return bool|string
282
+ */
283
+ private static function decryptVerifyContent($key, $pubKey, $content)
284
+ {
285
+ $cipherAlg = self::CIPHER_ALG;
286
+ $cipherMode = self::CIPHER_MODE;
287
+ $signAlg = self::SIGN_ALG;
288
+
289
+ if(isset($content->alg))
290
+ {
291
+ $algs = explode("|", $content->alg);
292
+ $cipherAlg = self::$cipherArray[$algs[0]]['alg'];
293
+ $cipherMode = self::$cipherArray[$algs[0]]['mode'];
294
+ $signAlg = $algs[1];
295
+ }
296
+
297
+ $cipher = isset($content->content) ? $content->content : $content->cipher;
298
+ $data = mcrypt_decrypt(
299
+ $cipherAlg,
300
+ $key,
301
+ base64_decode($cipher),
302
+ $cipherMode,
303
+ base64_decode($content->iv)
304
+ );
305
+
306
+ return openssl_verify($data, base64_decode($content->sign), $pubKey, $signAlg) ? $data : false;
307
+ }
308
+
309
+ /**
310
+ * Get Optipricer Key
311
+ *
312
+ * @return string
313
+ */
314
+ private static function getOptipricerKey()
315
+ {
316
+ //ToDo: Method to get the private key from file
317
+ return file_get_contents(self::KEY_FILENAME);
318
+ }
319
+ }
app/code/community/SWE/Optipricer/Model/Config.php CHANGED
@@ -6,7 +6,7 @@
6
  * @author Ubiprism Lda. / be.ubi <contact@beubi.com>
7
  * @copyright 2015 be.ubi
8
  * @license GNU Lesser General Public License (LGPL)
9
- * @version v.0.1.2
10
  */
11
  class SWE_Optipricer_Model_Config
12
  {
6
  * @author Ubiprism Lda. / be.ubi <contact@beubi.com>
7
  * @copyright 2015 be.ubi
8
  * @license GNU Lesser General Public License (LGPL)
9
+ * @version v.0.1.3
10
  */
11
  class SWE_Optipricer_Model_Config
12
  {
app/code/community/SWE/Optipricer/Model/Observer.php CHANGED
@@ -7,7 +7,7 @@
7
  * @author Ubiprism Lda. / be.ubi <contact@beubi.com>
8
  * @copyright 2015 be.ubi
9
  * @license GNU Lesser General Public License (LGPL)
10
- * @version v.0.1.2
11
  */
12
  class SWE_Optipricer_Model_Observer extends Varien_Event_Observer
13
  {
@@ -237,7 +237,8 @@ class SWE_Optipricer_Model_Observer extends Varien_Event_Observer
237
  'notes' => $this->products,
238
  'location' => ''
239
  );
240
- $securedData['data'] = $this->secureContent(json_encode($data));
 
241
  $client->setRawData(json_encode($securedData));
242
  try {
243
  $response = $client->request('PUT');
@@ -476,7 +477,8 @@ class SWE_Optipricer_Model_Observer extends Varien_Event_Observer
476
  $result = array();
477
  $cookie = $_COOKIE[self::COOKIE_PREFIX.$token.'_'.$productId];
478
  $dataAux = explode(':', $cookie);
479
- $data = $this->secureContent(json_encode($dataAux[0]), 'decrypt');
 
480
  $coupon = json_decode($data, true);
481
  if (is_array($coupon)) {
482
  $result['productId'] = null;
@@ -530,65 +532,4 @@ class SWE_Optipricer_Model_Observer extends Varien_Event_Observer
530
  {
531
  Mage::log($message, $level, 'OptipricerLogFile.log');
532
  }
533
-
534
- /**
535
- * Method to manage secure content
536
- *
537
- * @param string $content Content
538
- * @param string $task Task
539
- *
540
- * @return string
541
- */
542
- private function secureContent($content, $task = 'encrypt')
543
- {
544
- if ($task == 'decrypt') {
545
- $contentSecured = $this->decryptContent($this->key, $content);
546
- } else {
547
- $contentSecured = $this->encryptContent($this->key, $content);
548
- }
549
-
550
- return $contentSecured;
551
- }
552
-
553
- /**
554
- * Encrypt content
555
- *
556
- * @param String $key Key
557
- * @param String $content Content
558
- *
559
- * @return String
560
- */
561
- private function encryptContent($key, $content)
562
- {
563
- $ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
564
- $iv = mcrypt_create_iv($ivSize, MCRYPT_DEV_RANDOM);
565
- $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $content, MCRYPT_MODE_CBC, $iv);
566
- $ciphertextArr = array('cipher' => base64_encode($ciphertext), 'iv' => base64_encode($iv));
567
- $ciphertextArr = json_encode($ciphertextArr);
568
- $ciphertextBase64 = base64_encode($ciphertextArr);
569
-
570
- return $ciphertextBase64;
571
- }
572
-
573
- /**
574
- * Decrypt content
575
- *
576
- * @param String $key Key
577
- * @param String $cipher Cipher
578
- *
579
- * @return String
580
- */
581
- private function decryptContent($key, $cipher)
582
- {
583
- $ciphertextDec = base64_decode($cipher);
584
- $ciphertextDec = json_decode($ciphertextDec);
585
- $content = trim(mcrypt_decrypt(
586
- MCRYPT_RIJNDAEL_128,
587
- $key,
588
- base64_decode($ciphertextDec->cipher),
589
- MCRYPT_MODE_CBC,
590
- base64_decode($ciphertextDec->iv)
591
- ));
592
- return $content;
593
- }
594
  }
7
  * @author Ubiprism Lda. / be.ubi <contact@beubi.com>
8
  * @copyright 2015 be.ubi
9
  * @license GNU Lesser General Public License (LGPL)
10
+ * @version v.0.1.3
11
  */
12
  class SWE_Optipricer_Model_Observer extends Varien_Event_Observer
13
  {
237
  'notes' => $this->products,
238
  'location' => ''
239
  );
240
+ $secureData = Mage::helper('optipricer/Securedata');
241
+ $securedData['data'] = $secureData::secureContent($secureData::SECURE_CIPHER, json_encode($data), $this->key);
242
  $client->setRawData(json_encode($securedData));
243
  try {
244
  $response = $client->request('PUT');
477
  $result = array();
478
  $cookie = $_COOKIE[self::COOKIE_PREFIX.$token.'_'.$productId];
479
  $dataAux = explode(':', $cookie);
480
+ $secureData = Mage::helper('optipricer/Securedata');
481
+ $data = $secureData::getContent($dataAux[0], $this->key);
482
  $coupon = json_decode($data, true);
483
  if (is_array($coupon)) {
484
  $result['productId'] = null;
532
  {
533
  Mage::log($message, $level, 'OptipricerLogFile.log');
534
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
535
  }
app/code/community/SWE/Optipricer/controllers/Adminhtml/SweoptipricerController.php CHANGED
@@ -6,7 +6,7 @@
6
  * @author Ubiprism Lda. / be.ubi <contact@beubi.com>
7
  * @copyright 2015 be.ubi
8
  * @license GNU Lesser General Public License (LGPL)
9
- * @version v.0.1.2
10
  */
11
  class SWE_Optipricer_Adminhtml_SweoptipricerController extends Mage_Adminhtml_Controller_Action
12
  {
6
  * @author Ubiprism Lda. / be.ubi <contact@beubi.com>
7
  * @copyright 2015 be.ubi
8
  * @license GNU Lesser General Public License (LGPL)
9
+ * @version v.0.1.3
10
  */
11
  class SWE_Optipricer_Adminhtml_SweoptipricerController extends Mage_Adminhtml_Controller_Action
12
  {
app/code/community/SWE/Optipricer/etc/config.xml CHANGED
@@ -7,7 +7,7 @@
7
  * @author Ubiprism Lda. / be.ubi <contact@beubi.com>
8
  * @copyright 2015 be.ubi
9
  * @license GNU Lesser General Public License (LGPL)
10
- * @version v.0.1.2
11
  */
12
  -->
13
  <config>
@@ -76,7 +76,7 @@
76
  <default>
77
  <swe>
78
  <swe_group_activation>
79
- <swe_endpoint>http://lh.optipricer.com:4437/api/</swe_endpoint>
80
  <swe_enable>0</swe_enable>
81
  </swe_group_activation>
82
  <swe_group_parameters>
@@ -85,8 +85,8 @@
85
  <swe_expiryoffset>2880</swe_expiryoffset>
86
  <swe_min>5</swe_min>
87
  <swe_max>30</swe_max>
88
- <swe_background_color>660066</swe_background_color>
89
- <swe_font_color>FFFFFF</swe_font_color>
90
  </swe_group_parameters>
91
  </swe>
92
  </default>
7
  * @author Ubiprism Lda. / be.ubi <contact@beubi.com>
8
  * @copyright 2015 be.ubi
9
  * @license GNU Lesser General Public License (LGPL)
10
+ * @version v.0.1.3
11
  */
12
  -->
13
  <config>
76
  <default>
77
  <swe>
78
  <swe_group_activation>
79
+ <swe_endpoint>http://www.optipricer.com/api/</swe_endpoint>
80
  <swe_enable>0</swe_enable>
81
  </swe_group_activation>
82
  <swe_group_parameters>
85
  <swe_expiryoffset>2880</swe_expiryoffset>
86
  <swe_min>5</swe_min>
87
  <swe_max>30</swe_max>
88
+ <swe_background_color><![CDATA[660066]]></swe_background_color>
89
+ <swe_font_color><![CDATA[FFFFFF]]></swe_font_color>
90
  </swe_group_parameters>
91
  </swe>
92
  </default>
app/code/community/SWE/Optipricer/etc/system.xml CHANGED
@@ -7,7 +7,7 @@
7
  * @author Ubiprism Lda. / be.ubi <contact@beubi.com>
8
  * @copyright 2015 be.ubi
9
  * @license GNU Lesser General Public License (LGPL)
10
- * @version v.0.1.2
11
  */
12
  -->
13
  <config>
@@ -27,6 +27,57 @@
27
  <show_in_store>1</show_in_store>
28
 
29
  <groups>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  <swe_group_activation translate="label" module="optipricer">
31
  <label>Optipricer - Activation</label>
32
  <sort_order>10</sort_order>
@@ -72,27 +123,27 @@
72
  </swe_contact>
73
  <swe_token translate="label">
74
  <label>Store Token </label>
75
- <comment>Store Token (given by be.ubi)</comment>
76
  <frontend_type>text</frontend_type>
77
  <validate>required-entry</validate>
78
  <sort_order>24</sort_order>
79
  <show_in_default>1</show_in_default>
80
  <show_in_website>1</show_in_website>
81
  <show_in_store>1</show_in_store>
 
82
  </swe_token>
83
  <swe_key translate="label">
84
  <label>Store Key </label>
85
- <comment>Key for encrypt/decrypt data (given by be.ubi)</comment>
86
  <frontend_type>text</frontend_type>
87
  <validate>required-entry</validate>
88
  <sort_order>30</sort_order>
89
  <show_in_default>1</show_in_default>
90
  <show_in_website>1</show_in_website>
91
  <show_in_store>1</show_in_store>
 
92
  </swe_key>
93
  <swe_enable translate="label">
94
  <label>Enable Service </label>
95
- <comment>Enable or Disable the service.</comment>
96
  <frontend_type>select</frontend_type>
97
  <sort_order>40</sort_order>
98
  <show_in_default>1</show_in_default>
@@ -121,7 +172,7 @@
121
  <fields>
122
  <swe_expiryoffset translate="label">
123
  <label>Expiry Offset (in minutes)</label>
124
- <comment>Time limit for the Coupon. Example: 2880=2 days</comment>
125
  <frontend_type>text</frontend_type>
126
  <sort_order>50</sort_order>
127
  <show_in_default>1</show_in_default>
@@ -130,7 +181,7 @@
130
  </swe_expiryoffset>
131
  <swe_min translate="label">
132
  <label>Minimum Discount </label>
133
- <comment>Minimum value of the discount range.</comment>
134
  <frontend_type>text</frontend_type>
135
  <sort_order>60</sort_order>
136
  <show_in_default>1</show_in_default>
@@ -139,7 +190,7 @@
139
  </swe_min>
140
  <swe_max translate="label">
141
  <label>Maximum Discount </label>
142
- <comment>Maximum value of the discount range.</comment>
143
  <frontend_type>text</frontend_type>
144
  <sort_order>70</sort_order>
145
  <show_in_default>1</show_in_default>
@@ -148,7 +199,7 @@
148
  </swe_max>
149
  <swe_pageview translate="label">
150
  <label>Enable Page Views </label>
151
- <comment>Enable or Disable the feature "Page Views".</comment>
152
  <frontend_type>select</frontend_type>
153
  <sort_order>80</sort_order>
154
  <show_in_default>1</show_in_default>
@@ -158,18 +209,21 @@
158
  </swe_pageview>
159
  <swe_renderview translate="label">
160
  <label>Render Template </label>
161
- <comment>Render templates from local or remote sources.</comment>
 
162
  <frontend_type>radios</frontend_type>
163
  <source_model>optipricer/config</source_model>
164
  <sort_order>90</sort_order>
165
  <show_in_default>1</show_in_default>
166
  <show_in_website>1</show_in_website>
167
  <show_in_store>1</show_in_store>
 
168
  </swe_renderview>
169
  <swe_background_color translate="label">
170
  <label>Background color (Button)</label>
171
- <comment>By default, the color 660066 (purple) will be used</comment>
172
  <frontend_type>text</frontend_type>
 
173
  <sort_order>100</sort_order>
174
  <show_in_default>1</show_in_default>
175
  <show_in_website>1</show_in_website>
@@ -177,8 +231,9 @@
177
  </swe_background_color>
178
  <swe_font_color translate="label">
179
  <label>Font color (Button)</label>
180
- <comment>By default, the color FFFFFF (white) will be used</comment>
181
  <frontend_type>text</frontend_type>
 
182
  <sort_order>110</sort_order>
183
  <show_in_default>1</show_in_default>
184
  <show_in_website>1</show_in_website>
7
  * @author Ubiprism Lda. / be.ubi <contact@beubi.com>
8
  * @copyright 2015 be.ubi
9
  * @license GNU Lesser General Public License (LGPL)
10
+ * @version v.0.1.3
11
  */
12
  -->
13
  <config>
27
  <show_in_store>1</show_in_store>
28
 
29
  <groups>
30
+ <swe_group_about>
31
+ <label>Optipricer - About</label>
32
+ <frontend_type>text</frontend_type>
33
+ <sort_order>10</sort_order>
34
+ <show_in_default>1</show_in_default>
35
+ <show_in_website>1</show_in_website>
36
+ <show_in_store>1</show_in_store>
37
+ <expanded>1</expanded>
38
+ <comment><![CDATA[ <ol>
39
+ <li><h3>About</h3></li>
40
+ <li>Personalized discounts for your store. Enhanced social reach via Facebook ™ shares. </li>
41
+ <li>Installation guide <a target="_blank" href="http://www.optipricer.com/external/OptipricerWidget-InstallationandConfigurationguide.pdf">here</a>.</li>
42
+ <li>For more information please visit us @<a target="_blank" href="http://www.optipricer.com" >Optipricer</a>.</li>
43
+ <li>&nbsp;</li>
44
+ <li><h3>Systems Check</h3></li>
45
+ <li>SWE Optipricer API Status: <strong id="swe-api-status"></span></strong>
46
+ <li>Account status: <strong id="swe-account-status"></span></strong>
47
+ <li>&nbsp;</li>
48
+ <script>
49
+ window.onload = function(){
50
+ var swe_api_status_div = document.getElementById('swe-api-status');
51
+ var swe_account_status_div = document.getElementById('swe-account-status');
52
+ var swe_endpoint = document.getElementById('swe_swe_group_activation_swe_endpoint').value ;
53
+ var store_token = document.getElementById('swe_swe_group_activation_swe_token').value ;
54
+
55
+ if (swe_endpoint == '') {
56
+ swe_api_status_div.innerHTML = '<span style="color: red;">Offline</span>';
57
+ swe_account_status_div.innerHTML = '<span style="color: red;">Inactive</span>';
58
+ return;
59
+ }
60
+
61
+ swe_endpoint += 'service/validate';
62
+ var xhr = new XMLHttpRequest();
63
+ xhr.open('GET', encodeURI(swe_endpoint), true);
64
+ xhr.setRequestHeader("Authorization", "Token "+store_token);
65
+ xhr.onload = function() { swe_api_status_div.innerHTML = '<span style="color: green;">Online</span>'; }
66
+ xhr.onerror = function() { swe_api_status_div.innerHTML = '<span style="color: red;">Offline</span>'; }
67
+ xhr.onreadystatechange = function() {
68
+ if (xhr.status != 200) {
69
+ swe_account_status_div.innerHTML = '<span style="color: red;">Inactive</span><br /><small>&nbsp;&nbsp;&nbsp;Please, send use the option below to activate your account!</small>';
70
+ } else {
71
+ swe_account_status_div.innerHTML = '<span style="color: green;">Active</span>';
72
+ }
73
+ };
74
+ xhr.send();
75
+ };
76
+ </script>
77
+ </ol> ]]></comment>
78
+ <expanded>1</expanded>
79
+ </swe_group_about>
80
+
81
  <swe_group_activation translate="label" module="optipricer">
82
  <label>Optipricer - Activation</label>
83
  <sort_order>10</sort_order>
123
  </swe_contact>
124
  <swe_token translate="label">
125
  <label>Store Token </label>
 
126
  <frontend_type>text</frontend_type>
127
  <validate>required-entry</validate>
128
  <sort_order>24</sort_order>
129
  <show_in_default>1</show_in_default>
130
  <show_in_website>1</show_in_website>
131
  <show_in_store>1</show_in_store>
132
+ <tooltip>Store Token (provided by be.ubi)</tooltip>
133
  </swe_token>
134
  <swe_key translate="label">
135
  <label>Store Key </label>
 
136
  <frontend_type>text</frontend_type>
137
  <validate>required-entry</validate>
138
  <sort_order>30</sort_order>
139
  <show_in_default>1</show_in_default>
140
  <show_in_website>1</show_in_website>
141
  <show_in_store>1</show_in_store>
142
+ <tooltip>Key for encrypt/decrypt data (provided by be.ubi)</tooltip>
143
  </swe_key>
144
  <swe_enable translate="label">
145
  <label>Enable Service </label>
146
+ <tooltip>Enable or Disable the service.</tooltip>
147
  <frontend_type>select</frontend_type>
148
  <sort_order>40</sort_order>
149
  <show_in_default>1</show_in_default>
172
  <fields>
173
  <swe_expiryoffset translate="label">
174
  <label>Expiry Offset (in minutes)</label>
175
+ <tooltip>Time limit for the Coupon. Example: 2880=2 days</tooltip>
176
  <frontend_type>text</frontend_type>
177
  <sort_order>50</sort_order>
178
  <show_in_default>1</show_in_default>
181
  </swe_expiryoffset>
182
  <swe_min translate="label">
183
  <label>Minimum Discount </label>
184
+ <tooltip>Minimum value of the discount range.</tooltip>
185
  <frontend_type>text</frontend_type>
186
  <sort_order>60</sort_order>
187
  <show_in_default>1</show_in_default>
190
  </swe_min>
191
  <swe_max translate="label">
192
  <label>Maximum Discount </label>
193
+ <tooltip>Maximum value of the discount range.</tooltip>
194
  <frontend_type>text</frontend_type>
195
  <sort_order>70</sort_order>
196
  <show_in_default>1</show_in_default>
199
  </swe_max>
200
  <swe_pageview translate="label">
201
  <label>Enable Page Views </label>
202
+ <tooltip>Enable or Disable the feature "Page Views".</tooltip>
203
  <frontend_type>select</frontend_type>
204
  <sort_order>80</sort_order>
205
  <show_in_default>1</show_in_default>
209
  </swe_pageview>
210
  <swe_renderview translate="label">
211
  <label>Render Template </label>
212
+ <comment><![CDATA[<i>Not currently supported!</i> ]]></comment>
213
+ <tooltip>Render templates from local or remote sources.</tooltip>
214
  <frontend_type>radios</frontend_type>
215
  <source_model>optipricer/config</source_model>
216
  <sort_order>90</sort_order>
217
  <show_in_default>1</show_in_default>
218
  <show_in_website>1</show_in_website>
219
  <show_in_store>1</show_in_store>
220
+ <disabled>1</disabled>
221
  </swe_renderview>
222
  <swe_background_color translate="label">
223
  <label>Background color (Button)</label>
224
+ <tooltip>By default, the color 660066 (purple) will be used</tooltip>
225
  <frontend_type>text</frontend_type>
226
+ <validate>color</validate>
227
  <sort_order>100</sort_order>
228
  <show_in_default>1</show_in_default>
229
  <show_in_website>1</show_in_website>
231
  </swe_background_color>
232
  <swe_font_color translate="label">
233
  <label>Font color (Button)</label>
234
+ <tooltip>By default, the color FFFFFF (white) will be used</tooltip>
235
  <frontend_type>text</frontend_type>
236
+ <validate>color</validate>
237
  <sort_order>110</sort_order>
238
  <show_in_default>1</show_in_default>
239
  <show_in_website>1</show_in_website>
app/code/community/SWE/Optipricer/etc/widget.xml CHANGED
@@ -7,7 +7,7 @@
7
  * @author Ubiprism Lda. / be.ubi <contact@beubi.com>
8
  * @copyright 2015 be.ubi
9
  * @license GNU Lesser General Public License (LGPL)
10
- * @version v.0.1.2
11
  */
12
  -->
13
  <widgets>
7
  * @author Ubiprism Lda. / be.ubi <contact@beubi.com>
8
  * @copyright 2015 be.ubi
9
  * @license GNU Lesser General Public License (LGPL)
10
+ * @version v.0.1.3
11
  */
12
  -->
13
  <widgets>
app/etc/modules/SWE_Optipricer.xml CHANGED
@@ -7,7 +7,7 @@
7
  <depends>
8
  <Mage_Cms />
9
  </depends>
10
- <version>0.1.2</version>
11
  </SWE_Optipricer>
12
  </modules>
13
  </config>
7
  <depends>
8
  <Mage_Cms />
9
  </depends>
10
+ <version>0.1.3</version>
11
  </SWE_Optipricer>
12
  </modules>
13
  </config>
js/swe/optipricer.min.js CHANGED
@@ -1 +1 @@
1
- function Coupon(e,t,i,s,n,o,r,a){var c={n:e,d:t,st:i,p:s,o_p:n,o_pf:o,url:r,ps:a};this._initialize(c),this.setSpin();var h=this;document.getElementById("optibutton").addEventListener("click",function(e){e.preventDefault(),e.stopPropagation(),h.socialCredentials&&h.socialCredentials.facebookToken?(h.sendForm(),h.lockPermissions=!0):(h.checkLogin(h.perms,!0),h.lockPermissions=!1)})}function treatError(e,t,i){var s='{"msg":"'+e+'", "url":"'+t+'", "line":'+i+"}";sendRequest("logs/create","post",s,500,1,function(){})}function sendRequest(e,t,i,s,n,o){var r=cp.url+""+e,a="Token "+cp.st,c=null,h=new XMLHttpRequest;h.open(t.toUpperCase(),encodeURI(r),!0),h.setRequestHeader("Authorization",a),h.onreadystatechange=function l(){200!=h.status&&201!=h.status&&204!=h.status&&304!=h.status?(n++,s=4*s,6>n&&(c=window.setTimeout(function(){h.open(t.toUpperCase(),encodeURI(r),!0),h.onreadystatechange=l(),h.send(i)},s))):(c&&clearTimeout(c),o(h))},null!==i&&h.send(i)}var coupon={_initialize:function(e){this.d=e.d,this.n=e.n,this.st=e.st,this.p=e.p,this.ck_n="swe_"+this.st+"_"+this.p,this.url=e.url,this.o_p=e.o_p,this.o_pf=e.o_pf,this.p_s=e.ps,this.v=null,this.html=null,this.opts={lines:13,length:10,width:5,radius:10,corners:1,rotate:0,direction:1,color:"#010046",speed:1,trail:60,shadow:!0,hwaccel:!1,className:"spinner",zIndex:2e9,top:"43%",left:"54%"},this.spinner=new Spinner(this.opts),this.perms=["email","read_stream","user_interests","user_relationships","user_birthday","user_likes","user_work_history","user_education_history","publish_actions"]},_findRiskPrice:function(e){var t=this._htmlTree(document.getElementById("main-optipricer"),this.n);t&&this._riskPrice(t,e)},_htmlTree:function(e,t){var i=null;if(e.parentNode){var s=e.parentNode;s.childNodes.length>1&&(i=this._searchParent(s,t)),null===i&&this._htmlTree(s.parentNode,t)}return i},_replaceDiv:function(e,t,i){var s=document.getElementById(e);if(i)s.innerHTML="",s.innerHTML=t;else{var n=document.getElementById("opti-fbmessage");n?n.innerText=t:(n=document.createElement("h4"),n.id="opti-fbmessage",n.innerText=t,s.appendChild(n))}},_riskPrice:function(e,t){var i,s,n,o,r=e.childNodes,a=this.o_p.toString().replace(".",",");for(i=0;i<r.length;i++)r[i].hasChildNodes()&&r[i].innerHTML?this._riskPrice(r[i],t):(o=void 0!==r[i].innerHTML?r[i].innerHTML:r[i].data,n=e.parentNode,-1===o.indexOf(this.p_s)||-1===o.indexOf(this.o_pf)&&-1===o.indexOf(a)||-1!==n.className.indexOf("opti-discount")||(this.p_symbol=o.indexOf(this.p_s)>3?!0:!1,s=e.cloneNode(!0),s.innerHTML=this.getPriceToPrint(-1!==o.indexOf(".")?!0:!1),s.style.display="block",n.className=n.className+" opti-discount",n.appendChild(s),e.style.textDecoration="line-through"))},_searchParent:function(e,t){var i,s=e.childNodes;for(i=0;i<s.length;i++)if(void 0!==s[i].tagName&&"main-optipricer"!==s[i].id){if(-1!==s[i].innerHTML.indexOf(t))return e;s[i].hasChildNodes()&&this._searchParent(s[i],t)}return null},_toHtml:function(){var e=document.createElement("h2"),t=document.createElement("div");return t.id="optipricerdiscount"," "+e.outerHTML+" "+t.outerHTML},_missingPermissions:function(e){var t,i,s=[];if(e){for(i=0;i<this.perms.length;i++){var n=!1;for(t=0;t<e.length;t++)e[t].permission===this.perms[i]&&"granted"===e[t].status&&(n=!0,t=e.length);n||s.push(this.perms[i])}return s}return void 0},checkLogin:function(e,t){var i=this;FB.login(function(e){i.getLoginStatus(e)&&(i.cookie?i.handleRequest(null):FB.api("/v2.2/"+i.socialCredentials.facebookId+"/permissions","GET",function(e){var s=i._missingPermissions(e.data);0===s.length?i.sendForm():t&&i.sendForm()}))},{scope:e.join(",")})},checkPermissions:function(e){if(this.lockPermissions){var t=this,i=[];FB.api("/v2.2/"+t.socialCredentials.facebookId+"/permissions","GET",function(s){i=t._missingPermissions(s.data),void 0===i?t.checkLogin(t.perms,!0):i.length>0&&(t._replaceDiv("background1",e,!1),t.checkLogin(i,!1))}),this.removeSpin()}else this._replaceDiv("background1",e,!1),this.removeSpin()},createCookie:function(){document.cookie=this.ck_n+"="+this.c+":"+this.n+":"+this.v+":"+this.socialCredentials.facebookId+"; expires="+this.expires+"; path=/"},getLoginStatus:function(e){if("connected"===e.status){var t={facebookId:e.authResponse.userID,facebookToken:e.authResponse.accessToken};return this.setSocialCredentials(t),!0}return!1},getPriceToPrint:function(e){var t=e?this.v.toString():this.v.toString().replace(".",",");return this.p_symbol?t+" "+this.p_s:this.p_s+" "+t},handleErrors:function(e){var t=JSON.parse(e.responseText);switch(t.errorCode){case 1:case 2:case 3:case 4:case 6:this.removeSpin(),this._replaceDiv("main-optipricer",t.html,!0);break;case 5:this.checkPermissions(t.message)}},handleRequest:function(e){var t;e&&(t=JSON.parse(e.response),this.c=t.coupon,this.v=t.value,this.expires=new Date(1e3*t.expiryTime).toString(),this.createCookie()),this._findRiskPrice(this.v),this.removeSpin(),this.html=t&&t.html?t.html:this.html?this.html:this._toHtml(),this._replaceDiv("background1",this.html,!0);var i=(this.o_p-this.v)/this.o_p*100;document.getElementById("optipricerdiscount").innerHTML="<h5>You got a <span class='discount-opti'>"+i.toFixed(0)+"%</span> discount</h5>"},readCookie:function(){this.setSpin();var e="; "+document.cookie,t=e.split("; "+this.ck_n+"=");if(2===t.length){var i=t[1].split(":");this.cookie=!0,this.c=i[0],this.n=i[1],this.v=i[2],this.socialCredentials={},this.socialCredentials.facebookId=i[3].split(";")[0]}else this.cookie=!1},removeSpin:function(){var e=document.getElementsByClassName("fade")[0];e.style.display="none",this.spinner.stop()},sendForm:function(){this.setComment(document.getElementById("form_comment").value);var e=this.toJson();if(this.comment.length>2){document.getElementById("form_comment").setCustomValidity("");var t=this.url+"coupon/",i=[["Authorization","Token "+this.st]];i.push(["Accept","application/json"]),this.setSpin(),this.sendRequest(t,"POST",i,e,this)}else alertify.custom=alertify.extend("custom"),alertify.custom("You must comment something about the product..")},sendRequest:function(e,t,i,s,n){var o,r=new XMLHttpRequest,a=!0;for(r.open(t.toUpperCase(),encodeURI(e),!0),o=0;o<i.length;o++)2===i[o].length?r.setRequestHeader(i[o][0],i[o][1]):a=!1;if(a){var c=0;r.onreadystatechange=function(){if(200!=r.status&&201!=r.status&&204!=r.status&&304!=r.status)try{JSON.parse(r.responseText),""!==r.responseText&&0===c&&(c++,n.handleErrors(r))}catch(t){n.removeSpin(),treatError("[widget-magento]"+t.message,e,0)}else n.handleRequest(r)},null!==s&&r.send(new Blob([s],{type:"application/json"}))}else console.log("The input headers are defined wrong, must be a bi dimensional array, header[x][2]")},setComment:function(e){this.comment=e},setSocialCredentials:function(e){this.socialCredentials?this.socialCredentials.facebookId===e.facebookId?this.socialCredentials.facebookToken=e.facebookToken:(this.cookie=!1,this.socialCredentials.facebookId=e.facebookId,this.socialCredentials.facebookToken=e.facebookToken):(this.socialCredentials={},this.socialCredentials.facebookId=e.facebookId,this.socialCredentials.facebookToken=e.facebookToken)},setSpin:function(){var e=document.getElementsByClassName("fade")[0],t=document.getElementById("main-optipricer");e.style.display="block",e.style.width=t.offsetWidth+"px",e.style.height=t.offsetHeight+"px",this.spinner.spin(e)},toJson:function(){var e={data:this.d,social_credentials:this.socialCredentials,comment:this.comment};return JSON.stringify(e)}};Coupon.prototype=coupon,window.onerror=function(e,t,i){treatError(e,t,i)};
1
+ function Coupon(e,t,i,s,n,o,r,a){var c={n:e,d:t,st:i,p:s,o_p:n,o_pf:o,url:r,ps:a};this._initialize(c),this.setSpin();var h=this;document.getElementById("optibutton").addEventListener("click",function(e){e.preventDefault(),e.stopPropagation(),h.socialCredentials&&h.socialCredentials.facebookToken?(h.sendForm(),h.lockPermissions=!0):(h.checkLogin(h.perms,!0),h.lockPermissions=!1)})}function treatError(e,t,i){var s='{"msg":"'+e+'", "url":"'+t+'", "line":'+i+"}";sendRequest("logs/","post",s,500,1,function(){})}function sendRequest(e,t,i,s,n,o){var r=cp.url+""+e,a="Token "+cp.st,c=null,h=new XMLHttpRequest;h.open(t.toUpperCase(),encodeURI(r),!0),h.setRequestHeader("Authorization",a),h.onreadystatechange=function l(){200!=h.status&&201!=h.status&&204!=h.status&&304!=h.status?(n++,s=4*s,6>n&&(c=window.setTimeout(function(){h.open(t.toUpperCase(),encodeURI(r),!0),h.onreadystatechange=l(),h.send(i)},s))):(c&&clearTimeout(c),o(h))},null!==i&&h.send(i)}var coupon={_initialize:function(e){this.d=e.d,this.n=e.n,this.st=e.st,this.p=e.p,this.ck_n="swe_"+this.st+"_"+this.p,this.url=e.url,this.o_p=e.o_p,this.o_pf=e.o_pf,this.p_s=e.ps,this.v=null,this.html=null,this.opts={lines:13,length:10,width:5,radius:10,corners:1,rotate:0,direction:1,color:"#010046",speed:1,trail:60,shadow:!0,hwaccel:!1,className:"spinner",zIndex:2e9,top:"43%",left:"54%"},this.spinner=new Spinner(this.opts),this.perms=["email","read_stream","user_interests","user_relationships","user_birthday","user_likes","user_work_history","user_education_history","publish_actions"]},_findRiskPrice:function(e){var t=this._htmlTree(document.getElementById("main-optipricer"),this.n);t&&this._riskPrice(t,e)},_htmlTree:function(e,t){var i=null;if(e.parentNode){var s=e.parentNode;s.childNodes.length>1&&(i=this._searchParent(s,t)),null===i&&this._htmlTree(s.parentNode,t)}return i},_replaceDiv:function(e,t,i){var s=document.getElementById(e);if(i)s.innerHTML="",s.innerHTML=t;else{var n=document.getElementById("opti-fbmessage");n?n.innerText=t:(n=document.createElement("h4"),n.id="opti-fbmessage",n.innerText=t,s.appendChild(n))}},_riskPrice:function(e,t){var i,s,n,o,r=e.childNodes,a=this.o_p.toString().replace(".",",");for(i=0;i<r.length;i++)r[i].hasChildNodes()&&r[i].innerHTML?this._riskPrice(r[i],t):(o=void 0!==r[i].innerHTML?r[i].innerHTML:r[i].data,n=e.parentNode,-1===o.indexOf(this.p_s)||-1===o.indexOf(this.o_pf)&&-1===o.indexOf(a)||-1!==n.className.indexOf("opti-discount")||(this.p_symbol=o.indexOf(this.p_s)>3?!0:!1,s=e.cloneNode(!0),s.innerHTML=this.getPriceToPrint(-1!==o.indexOf(".")?!0:!1),s.style.display="block",n.className=n.className+" opti-discount",n.appendChild(s),e.style.textDecoration="line-through"))},_searchParent:function(e,t){var i,s=e.childNodes;for(i=0;i<s.length;i++)if(void 0!==s[i].tagName&&"main-optipricer"!==s[i].id){if(-1!==s[i].innerHTML.indexOf(t))return e;s[i].hasChildNodes()&&this._searchParent(s[i],t)}return null},_toHtml:function(){var e=document.createElement("h2"),t=document.createElement("div");return t.id="optipricerdiscount"," "+e.outerHTML+" "+t.outerHTML},_missingPermissions:function(e){var t,i,s=[];if(e){for(i=0;i<this.perms.length;i++){var n=!1;for(t=0;t<e.length;t++)e[t].permission===this.perms[i]&&"granted"===e[t].status&&(n=!0,t=e.length);n||s.push(this.perms[i])}return s}return void 0},checkLogin:function(e,t){var i=this;FB.login(function(e){i.getLoginStatus(e)&&(i.cookie?i.handleRequest(null):FB.api("/v2.2/"+i.socialCredentials.facebookId+"/permissions","GET",function(e){var s=i._missingPermissions(e.data);0===s.length?i.sendForm():t&&i.sendForm()}))},{scope:e.join(",")})},checkPermissions:function(e){if(this.lockPermissions){var t=this,i=[];FB.api("/v2.2/"+t.socialCredentials.facebookId+"/permissions","GET",function(s){i=t._missingPermissions(s.data),void 0===i?t.checkLogin(t.perms,!0):i.length>0&&(t._replaceDiv("background1",e,!1),t.checkLogin(i,!1))}),this.removeSpin()}else this._replaceDiv("background1",e,!1),this.removeSpin()},createCookie:function(){document.cookie=this.ck_n+"="+this.c+":"+this.n+":"+this.v+":"+this.socialCredentials.facebookId+"; expires="+this.expires+"; path=/"},getLoginStatus:function(e){if("connected"===e.status){var t={facebookId:e.authResponse.userID,facebookToken:e.authResponse.accessToken};return this.setSocialCredentials(t),!0}return!1},getPriceToPrint:function(e){var t=e?this.v.toString():this.v.toString().replace(".",",");return this.p_symbol?t+" "+this.p_s:this.p_s+" "+t},handleErrors:function(e){var t=JSON.parse(e.responseText);switch(t.errorCode){case 1:case 2:case 3:case 4:case 6:this.removeSpin(),this._replaceDiv("main-optipricer",t.html,!0);break;case 5:this.checkPermissions(t.message)}},handleRequest:function(e){var t;e&&(t=JSON.parse(e.response),this.c=t.coupon,this.v=t.value,this.expires=new Date(1e3*t.expiryTime).toString(),this.createCookie()),this._findRiskPrice(this.v),this.removeSpin(),this.html=t&&t.html?t.html:this.html?this.html:this._toHtml(),this._replaceDiv("background1",this.html,!0);var i=(this.o_p-this.v)/this.o_p*100;document.getElementById("optipricerdiscount").innerHTML="<h5>You got a <span class='discount-opti'>"+i.toFixed(0)+"%</span> discount</h5>"},readCookie:function(){this.setSpin();var e="; "+document.cookie,t=e.split("; "+this.ck_n+"=");if(2===t.length){var i=t[1].split(":");this.cookie=!0,this.c=i[0],this.n=i[1],this.v=i[2],this.socialCredentials={},this.socialCredentials.facebookId=i[3].split(";")[0]}else this.cookie=!1},removeSpin:function(){var e=document.getElementsByClassName("fade")[0];e.style.display="none",this.spinner.stop()},sendForm:function(){this.setComment(document.getElementById("form_comment").value);var e=this.toJson();if(this.comment.length>2){document.getElementById("form_comment").setCustomValidity("");var t=this.url+"coupon/",i=[["Authorization","Token "+this.st]];i.push(["Accept","application/json"]),this.setSpin(),this.sendRequest(t,"POST",i,e,this)}else alertify.custom=alertify.extend("custom"),alertify.custom("You must comment something about the product..")},sendRequest:function(e,t,i,s,n){var o,r=new XMLHttpRequest,a=!0;for(r.open(t.toUpperCase(),encodeURI(e),!0),o=0;o<i.length;o++)2===i[o].length?r.setRequestHeader(i[o][0],i[o][1]):a=!1;if(a){var c=0;r.onreadystatechange=function(){if(200!=r.status&&201!=r.status&&204!=r.status&&304!=r.status)try{JSON.parse(r.responseText),""!==r.responseText&&0===c&&(c++,n.handleErrors(r))}catch(t){n.removeSpin(),treatError("[widget-magento]"+t.message,e,0)}else n.handleRequest(r)},null!==s&&r.send(new Blob([s],{type:"application/json"}))}else console.log("The input headers are defined wrong, must be a bi dimensional array, header[x][2]")},setComment:function(e){this.comment=e},setSocialCredentials:function(e){this.socialCredentials?this.socialCredentials.facebookId===e.facebookId?this.socialCredentials.facebookToken=e.facebookToken:(this.cookie=!1,this.socialCredentials.facebookId=e.facebookId,this.socialCredentials.facebookToken=e.facebookToken):(this.socialCredentials={},this.socialCredentials.facebookId=e.facebookId,this.socialCredentials.facebookToken=e.facebookToken)},setSpin:function(){var e=document.getElementsByClassName("fade")[0],t=document.getElementById("main-optipricer");e.style.display="block",e.style.width=t.offsetWidth+"px",e.style.height=t.offsetHeight+"px",this.spinner.spin(e)},toJson:function(){var e={data:this.d,social_credentials:this.socialCredentials,comment:this.comment};return JSON.stringify(e)}};Coupon.prototype=coupon,window.onerror=function(e,t,i){treatError(e,t,i)};
package.xml CHANGED
@@ -1,7 +1,7 @@
1
  <?xml version="1.0"?>
2
  <package>
3
  <name>SweOptipricer</name>
4
- <version>0.1.2</version>
5
  <stability>stable</stability>
6
  <license uri="http://opensource.org/licenses/LGPL-3.0">GNU Lesser General Public License (LGPL)</license>
7
  <channel>community</channel>
@@ -12,9 +12,9 @@ Customers will receive a discount after a successful product share on Facebook.&
12
  For more information www.optipricer.com</description>
13
  <notes>Testing candidate</notes>
14
  <authors><author><name>Rui Mendes</name><user>ruidamendes</user><email>rui.mendes@beubi.com</email></author><author><name>Ant&#xF3;nio Ferreira</name><user>beubi</user><email>swe@beubi.com</email></author></authors>
15
- <date>2015-03-24</date>
16
- <time>17:07:05</time>
17
- <contents><target name="magecommunity"><dir name="SWE"><dir name="Optipricer"><dir name="Block"><file name="Button.php" hash="a396da802f3218b846983e0edc1612c2"/><file name="Discount.php" hash="ea7fb0043c06352c5a230677ffa58174"/></dir><dir name="Helper"><file name="Data.php" hash="382b06d5bdb2ccca8b2eca14dfbcd423"/></dir><dir name="Model"><file name="Config.php" hash="51220275c7f817d6e34b756679e949a1"/><file name="Observer.php" hash="5f65cf218c21409e799782d5f6860824"/></dir><dir name="controllers"><dir name="Adminhtml"><file name="SweoptipricerController.php" hash="632b100eba3ab7344429ed7b31952bc6"/></dir></dir><dir name="etc"><file name="config.xml" hash="9c02bfd523b1341bc1211dfbd086cf60"/><file name="system.xml" hash="a48519da9f2529de9dff369061ba2953"/><file name="widget.xml" hash="113da72e38ce3a230a1ea8e4cf0a1340"/></dir></dir></dir></target><target name="magedesign"><dir name="frontend"><dir name="base"><dir name="default"><dir name="template"><dir name="swe"><dir name="optipricer"><file name="widget.phtml" hash="2cb14a351ce2ec686afe228c326caf9e"/></dir></dir></dir></dir></dir></dir><dir name="adminhtml"><dir name="default"><dir name="default"><dir name="template"><dir name="swe"><dir name="optipricer"><dir name="system"><dir name="config"><file name="button.phtml" hash="24a7d6503d556b5a068b03fd21b2aba5"/></dir></dir></dir></dir></dir></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="SWE_Optipricer.xml" hash="fef9106f9ed27d9e69a1abcc22f2063e"/></dir></target><target name="magelocale"><dir name="en_US"><file name="SWE_Optipricer.csv" hash="2c27e6a50f20ae1318274d6fa8b17bfd"/></dir><dir name="pt_PT"><file name="SWE_Optipricer.csv" hash="7ac9dd9cf8e434d982a003789a0cd826"/></dir></target><target name="mage"><dir name="js"><dir name="swe"><file name="optialert.min.js" hash="d819c38f0c3c8ef8a084154da96667eb"/><file name="optipricer.min.js" hash="d1dc47bba0b6a8d1bb4171196944c051"/><file name="optispin.min.js" hash="f2b0a61b3a739d03e88401e2a1163588"/></dir></dir></target></contents>
18
  <compatible/>
19
  <dependencies><required><php><min>5.3.10</min><max>6.0.0</max></php></required></dependencies>
20
  </package>
1
  <?xml version="1.0"?>
2
  <package>
3
  <name>SweOptipricer</name>
4
+ <version>0.1.3</version>
5
  <stability>stable</stability>
6
  <license uri="http://opensource.org/licenses/LGPL-3.0">GNU Lesser General Public License (LGPL)</license>
7
  <channel>community</channel>
12
  For more information www.optipricer.com</description>
13
  <notes>Testing candidate</notes>
14
  <authors><author><name>Rui Mendes</name><user>ruidamendes</user><email>rui.mendes@beubi.com</email></author><author><name>Ant&#xF3;nio Ferreira</name><user>beubi</user><email>swe@beubi.com</email></author></authors>
15
+ <date>2015-04-17</date>
16
+ <time>13:46:47</time>
17
+ <contents><target name="magecommunity"><dir name="SWE"><dir name="Optipricer"><dir name="Block"><file name="Button.php" hash="e59a2b0911c431431024ab4f6472f3a0"/><file name="Discount.php" hash="89bbd3742c77e8f7b78eaaed18ec01cf"/></dir><dir name="Helper"><file name="Data.php" hash="b276d4a6a0c9ee37379bd822d44874d7"/><file name="Securedata.php" hash="c57c16433e7ec2d4c967baab3e6a97c5"/></dir><dir name="Model"><file name="Config.php" hash="5767d17e3d570337af6eb91b9be4e886"/><file name="Observer.php" hash="4b687dd0c27a1c0598422de2fa25a008"/></dir><dir name="controllers"><dir name="Adminhtml"><file name="SweoptipricerController.php" hash="ab803da796a6a605caefbdeb16ce6cb0"/></dir></dir><dir name="etc"><file name="config.xml" hash="86719a0fadb8b3a2df862810cdabf213"/><file name="system.xml" hash="2e3b9713df380720585a4c3d4c2c6dc1"/><file name="widget.xml" hash="5945d2127dbb3a6a0f26de71d3b0099e"/></dir></dir></dir></target><target name="magedesign"><dir name="frontend"><dir name="base"><dir name="default"><dir name="template"><dir name="swe"><dir name="optipricer"><file name="widget.phtml" hash="2cb14a351ce2ec686afe228c326caf9e"/></dir></dir></dir></dir></dir></dir><dir name="adminhtml"><dir name="default"><dir name="default"><dir name="template"><dir name="swe"><dir name="optipricer"><dir name="system"><dir name="config"><file name="button.phtml" hash="24a7d6503d556b5a068b03fd21b2aba5"/></dir></dir></dir></dir></dir></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="SWE_Optipricer.xml" hash="a64709dc9fd1693a82df3dc503fda07e"/></dir></target><target name="magelocale"><dir name="en_US"><file name="SWE_Optipricer.csv" hash="2c27e6a50f20ae1318274d6fa8b17bfd"/></dir><dir name="pt_PT"><file name="SWE_Optipricer.csv" hash="7ac9dd9cf8e434d982a003789a0cd826"/></dir></target><target name="mage"><dir name="js"><dir name="swe"><file name="optialert.min.js" hash="d819c38f0c3c8ef8a084154da96667eb"/><file name="optipricer.min.js" hash="c14ff76e8abf11a98d86eac149a75c6b"/><file name="optispin.min.js" hash="f2b0a61b3a739d03e88401e2a1163588"/></dir></dir></target></contents>
18
  <compatible/>
19
  <dependencies><required><php><min>5.3.10</min><max>6.0.0</max></php></required></dependencies>
20
  </package>