SweOptipricer - Version 0.1.4

Version Notes

Testing candidate

Download this release

Release Info

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


Code changes from version 0.1.3 to 0.1.4

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.3
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.1
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.3
10
  */
11
  class SWE_Optipricer_Block_Discount extends Mage_Core_Block_Template implements Mage_Widget_Block_Interface
12
  {
@@ -135,6 +135,7 @@ class SWE_Optipricer_Block_Discount extends Mage_Core_Block_Template implements
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
 
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
  {
135
  $data['discount_render'] = $this->renderView;
136
  $data['expiry_offset'] = $this->expiryOffset;
137
  $data['social_credentials'] = array('facebookId' => '', 'facebookToken' => '');
138
+ $data['ssid'] = $secureData::generateSessionID(Mage::getSingleton("core/session")->getEncryptedSessionId());
139
  $securedData['data'] = $secureData::secureContent($secureData::SECURE_CIPHER, json_encode($data), $this->key);
140
  $securedData['social_credentials'] = $data['social_credentials'];
141
 
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.3
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.1
10
  */
11
  class SWE_Optipricer_Helper_Data extends Mage_Core_Helper_Abstract
12
  {
app/code/community/SWE/Optipricer/Helper/Securedata.php CHANGED
@@ -7,11 +7,11 @@
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;
@@ -93,7 +93,7 @@ class SWE_Optipricer_Helper_Securedata extends Mage_Core_Helper_Abstract
93
  return false;
94
  }
95
 
96
- $data = base64_decode($content);
97
  if (!$data) {
98
  //Content is not a base64 encode object
99
  $data = $content;
@@ -179,9 +179,9 @@ class SWE_Optipricer_Helper_Securedata extends Mage_Core_Helper_Abstract
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
  }
@@ -225,8 +225,10 @@ class SWE_Optipricer_Helper_Securedata extends Mage_Core_Helper_Abstract
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
  }
@@ -298,12 +300,16 @@ class SWE_Optipricer_Helper_Securedata extends Mage_Core_Helper_Abstract
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
  /**
@@ -311,9 +317,53 @@ class SWE_Optipricer_Helper_Securedata extends Mage_Core_Helper_Abstract
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
  }
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_Helper_Securedata extends Mage_Core_Helper_Abstract
13
  {
14
+ const KEY_FILENAME = 'swe_public_key.pem';
15
 
16
  const CIPHER_ALG = MCRYPT_RIJNDAEL_128;
17
  const CIPHER_MODE = MCRYPT_MODE_CBC;
93
  return false;
94
  }
95
 
96
+ $data = base64_decode($content, true);
97
  if (!$data) {
98
  //Content is not a base64 encode object
99
  $data = $content;
179
  $cipherAlg = self::$cipherArray[$data->alg]['alg'];
180
  $cipherMode = self::$cipherArray[$data->alg]['mode'];
181
  }
 
182
  $cipher = isset($data->content) ? $data->content : $data->cipher;
183
+
184
+ $content = mcrypt_decrypt($cipherAlg, $key, base64_decode(str_replace(' ', '+',$cipher)), $cipherMode, base64_decode(str_replace(' ', '+',$data->iv)));
185
 
186
  return $content;
187
  }
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
+ $cnt = is_object($content->content) ? json_encode($content->content) : $content->content;
229
+ $result = openssl_verify($cnt, base64_decode(str_replace(" ", "+",$content->sign)), $publicKey, $signAlg);
230
+
231
+ return $result == 1;
232
  } else {
233
  return false;
234
  }
300
  $data = mcrypt_decrypt(
301
  $cipherAlg,
302
  $key,
303
+ base64_decode(str_replace(' ', '+',$cipher)),
304
  $cipherMode,
305
+ base64_decode(str_replace(' ', '+',$content->iv))
306
  );
307
 
308
+ return openssl_verify(
309
+ $data,
310
+ base64_decode(str_replace(' ', '+',$content->sign)),
311
+ $pubKey,
312
+ $signAlg) ? $data : false;
313
  }
314
 
315
  /**
317
  *
318
  * @return string
319
  */
320
+ public static function getOptipricerKey()
321
  {
322
  //ToDo: Method to get the private key from file
323
+ return file_get_contents(__DIR__ . '/' . self::KEY_FILENAME);
324
  }
325
+
326
+ /**
327
+ * Function to generate session identifier
328
+ *
329
+ * @param string $ssid generated by PHP function session_id()
330
+ * @param bool $user_agent flag which defines if user agent should be in ssid or don't
331
+ * @param bool $ip_address flag which defines if ip address should be in ssid or don't
332
+ *
333
+ * P.S. Session should be initialized (session_start())
334
+ *
335
+ * @return session identifier
336
+ */
337
+ public static function generateSessionID($ssid, $user_agent = true, $ip_address = false)
338
+ {
339
+ $sessionID = $ssid;
340
+
341
+ if($user_agent)
342
+ $sessionID .= $_SERVER['HTTP_USER_AGENT'];
343
+
344
+ if($ip_address)
345
+ $sessionID .= $_SERVER['REMOTE_ADDR'];
346
+
347
+ return sha1($sessionID);
348
+ }
349
+
350
+ /**
351
+ * Function to determine if session identifier is valid
352
+ *
353
+ * @param string $session_id session identifier present on the message (from Optipricer)
354
+ * @param string $ssid generated by PHP function session_id()
355
+ * @param bool $user_agent flag which defines if user agent should be in ssid or don't
356
+ * @param bool $ip_address flag which defines if ip address should be in ssid or don't
357
+ *
358
+ * P.S. Session should be initialized (session_start())
359
+ *
360
+ * @return bool (true or false)
361
+ */
362
+ public static function verifySessionID($session_id, $ssid, $user_agent = true, $ip_address = false)
363
+ {
364
+ $old_ssid = self::generateSessionID($ssid, $user_agent, $ip_address);
365
+
366
+ return $session_id == $old_ssid;
367
+ }
368
+
369
  }
app/code/community/SWE/Optipricer/Helper/swe_public_key.pem ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ -----BEGIN PUBLIC KEY-----
2
+ MIIBtzCCASwGByqGSM44BAEwggEfAoGBAJsC3O2dpCKZhzIZ5MgBBa/k7+BQfBlk
3
+ Dm+c2BjX7rT+2Hvxhl0anWh/yThe/T1MUcxVB88KgTSaHPEF07YgZupYj9/nDTeI
4
+ SizGmd7K73mKCcSdW3RCTArmvMj7noAm7xqTiJ1Jg//dBiMsEN2G925Q/TwE1zra
5
+ r57jNlCX8sllAhUAxzwjRTud4UVDan1q85bwQI75sAcCgYEAh1JwywTKppKs6mbm
6
+ GVf7c4pgQPxsfn9GEKGqdTiz3Q0ccrelYE74+alql3TImx588lkxKl+2FNX69QMy
7
+ 43PvHFifsmLQ7vlIf06EclS37Mt7fP2a7MH53SO1TNgGd1STmch8Xh2X62sthdqz
8
+ Ph50BLONYEKHlfSJDSjjOpAWwmADgYQAAoGAU78c6rPh9WSDfzwBEb3pH5y+7h5K
9
+ QftDx/XAAnOHv96okVKq+4gzrrjoiZJSXttKzKLhhGYKeY47milXoPenYbtezaYJ
10
+ FNjELNKe/wA+AdxLY7PBgMIftLWDLd9zHFNtr4G7h6c3VKSGc+DHT5vu4gMzgGd/
11
+ NUhKmA38qlDI9EM=
12
+ -----END PUBLIC KEY-----
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.3
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.1
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.3
11
  */
12
  class SWE_Optipricer_Model_Observer extends Varien_Event_Observer
13
  {
@@ -476,10 +476,32 @@ class SWE_Optipricer_Model_Observer extends Varien_Event_Observer
476
  {
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;
485
  //Get CouponToken
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
  {
476
  {
477
  $result = array();
478
  $cookie = $_COOKIE[self::COOKIE_PREFIX.$token.'_'.$productId];
479
+
480
+ @json_decode(stripslashes($cookie));
481
+
482
+ if(json_last_error() !== JSON_ERROR_NONE)
483
+ {
484
+ $couponArr = explode(':', $cookie);
485
+ $dataAux = $couponArr[0];
486
+ }
487
+ else
488
+ $dataAux = stripslashes($cookie);
489
+
490
  $secureData = Mage::helper('optipricer/Securedata');
491
+ $data = $secureData::getContent($dataAux, $this->key, $secureData::getOptipricerKey());
492
+
493
+ if(!$data)
494
+ return false;
495
+
496
+ $coupon = is_object($data) ? json_decode(json_encode($data),true) : json_decode($data, true);
497
+
498
+
499
+ if( isset($coupon['ssid']) &&
500
+ !$secureData::verifySessionID(
501
+ $coupon['ssid'],
502
+ Mage::getSingleton("core/session")->getEncryptedSessionId()))
503
+ return false;
504
+
505
  if (is_array($coupon)) {
506
  $result['productId'] = null;
507
  //Get CouponToken
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.3
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.1
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.3
11
  */
12
  -->
13
  <config>
@@ -76,8 +76,13 @@
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>
83
  <swe_pageview>1</swe_pageview>
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
  <default>
77
  <swe>
78
  <swe_group_activation>
79
+ <swe_name></swe_name>
80
+ <swe_email></swe_email>
81
+ <swe_message></swe_message>
82
+ <swe_token></swe_token>
83
+ <swe_key></swe_key>
84
+ <swe_enable>1</swe_enable>
85
  <swe_endpoint>http://www.optipricer.com/api/</swe_endpoint>
 
86
  </swe_group_activation>
87
  <swe_group_parameters>
88
  <swe_pageview>1</swe_pageview>
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.3
11
  */
12
  -->
13
  <config>
@@ -222,8 +222,8 @@
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>
@@ -232,8 +232,8 @@
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>
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.1
11
  */
12
  -->
13
  <config>
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
  <validate>color</validate>
226
+ <frontend_type>text</frontend_type>
227
  <sort_order>100</sort_order>
228
  <show_in_default>1</show_in_default>
229
  <show_in_website>1</show_in_website>
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
  <validate>color</validate>
236
+ <frontend_type>text</frontend_type>
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.3
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.1
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.3</version>
11
  </SWE_Optipricer>
12
  </modules>
13
  </config>
7
  <depends>
8
  <Mage_Cms />
9
  </depends>
10
+ <version>0.1.2</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/","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","user_posts","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(){var e;if("object"==typeof this.c){this.c.ec={fId:this.socialCredentials.facebookId};try{e=JSON.parse(this.c.content),this.c.content=e}catch(t){this.c.ec.n=this.n,this.c.ec.v=this.v}document.cookie=this.ck_n+"="+JSON.stringify(this.c)+"; expires="+this.expires+"; path=/"}else document.cookie=this.ck_n+"="+this.c+":"+this.n+":"+this.v+":"+this.socialCredentials.facebookId+"; expires="+this.expires+"; path=/"},getCookie:function(e){for(var t=e+"=",i=document.cookie.split(";"),s=0;s<i.length;s++){for(var n=i[s];" "==n.charAt(0);)n=n.substring(1);if(0==n.indexOf(t))return n.substring(t.length,n.length)}return""},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;if(e){t=JSON.parse(e.response);try{this.c=JSON.parse(t.coupon),this.v="undefined"!=typeof this.c.content.value?this.c.content.value:t.value,eTime=this.c.content.expiryTime?this.c.content.expiryTime:t.expiryTime}catch(i){this.c=t.coupon,this.v=t.value,eTime=t.expiryTime}this.expires=new Date(1e3*eTime).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 s=(this.o_p-this.v)/this.o_p*100;document.getElementById("optipricerdiscount").innerHTML="<h5>You got a <span class='discount-opti'>"+s.toFixed(0)+"%</span> discount</h5>"},readCookie:function(){this.setSpin();var e,t=this.getCookie(this.ck_n);if(""!=t){this.cookie=!0;try{if(this.c=JSON.parse(t),"object"!=typeof this.c.content)try{e=JSON.parse(this.c.content),this.n=e.product.name,this.v=e.value}catch(i){this.n=this.c.ec.n,this.v=this.c.ec.v}else this.n=this.c.content.product.name,this.v=this.c.content.value;this.socialCredentials={},this.socialCredentials.facebookId=this.c.ec.fId}catch(i){var s=t.split(":");this.c=s[0],this.n=s[1],this.v=s[2],this.socialCredentials={},this.socialCredentials.facebookId=s[3]}}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.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,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-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>
1
  <?xml version="1.0"?>
2
  <package>
3
  <name>SweOptipricer</name>
4
+ <version>0.1.4</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-05-05</date>
16
+ <time>14:13:54</time>
17
+ <contents><target name="magecommunity"><dir name="SWE"><dir name="Optipricer"><dir name="Block"><file name="Button.php" hash="2649751a4b02bc385560c43fda74a4c9"/><file name="Discount.php" hash="8d92df6ac6c967d4dd7abfe08bd95e17"/></dir><dir name="Helper"><file name="Data.php" hash="e7902afbd2304409bba65f8b62c790ba"/><file name="Securedata.php" hash="5bd25fdffe296583721f4b3addf22ecb"/><file name="swe_public_key.pem" hash="b5753918fdbd93ca45f1daa0a7deef93"/></dir><dir name="Model"><file name="Config.php" hash="0a2d0119793670f963e585ce9ed3dbd6"/><file name="Observer.php" hash="27b63db80c30adc0299f0d78bb7c2f34"/></dir><dir name="controllers"><dir name="Adminhtml"><file name="SweoptipricerController.php" hash="6751a86d6e4e098ef86bc71551395187"/></dir></dir><dir name="etc"><file name="config.xml" hash="2f8e793bcf94a0a588e193f77bf58de0"/><file name="system.xml" hash="97070a10606ce146ceb367538a8f558f"/><file name="widget.xml" hash="ed2339511b5f926dca59a9dc27a3d623"/></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="a76f3ef25a95633a227840f0284a9907"/><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>