Wisepricer_Syncer - Version 1.1.2.5

Version Notes

wisepricer

Download this release

Release Info

Developer Moshe
Extension Wisepricer_Syncer
Version 1.1.2.5
Comparing to
See all releases


Version 1.1.2.5

Files changed (48) hide show
  1. app/code/local/Wisepricer/Syncer/Block/Adminhtml/Mapping.php +136 -0
  2. app/code/local/Wisepricer/Syncer/Helper/Data.php +11 -0
  3. app/code/local/Wisepricer/Syncer/Model/Adminhtml/Attributes.php +70 -0
  4. app/code/local/Wisepricer/Syncer/Model/Config.php +13 -0
  5. app/code/local/Wisepricer/Syncer/Model/Mapping.php +17 -0
  6. app/code/local/Wisepricer/Syncer/Model/Mysql4/Config.php +14 -0
  7. app/code/local/Wisepricer/Syncer/Model/Mysql4/Config/Collection.php +15 -0
  8. app/code/local/Wisepricer/Syncer/Model/Mysql4/Mapping.php +31 -0
  9. app/code/local/Wisepricer/Syncer/Model/Mysql4/Mapping/Collection.php +15 -0
  10. app/code/local/Wisepricer/Syncer/controllers/Adminhtml/SyncerController.php +182 -0
  11. app/code/local/Wisepricer/Syncer/controllers/ProductsController.php +430 -0
  12. app/code/local/Wisepricer/Syncer/etc/config.xml +126 -0
  13. app/code/local/Wisepricer/Syncer/lib/phpseclib/Crypt/AES.php +479 -0
  14. app/code/local/Wisepricer/Syncer/lib/phpseclib/Crypt/DES.php +945 -0
  15. app/code/local/Wisepricer/Syncer/lib/phpseclib/Crypt/Hash.php +816 -0
  16. app/code/local/Wisepricer/Syncer/lib/phpseclib/Crypt/RC4.php +493 -0
  17. app/code/local/Wisepricer/Syncer/lib/phpseclib/Crypt/RSA.php +2119 -0
  18. app/code/local/Wisepricer/Syncer/lib/phpseclib/Crypt/Random.php +129 -0
  19. app/code/local/Wisepricer/Syncer/lib/phpseclib/Crypt/Rijndael.php +1242 -0
  20. app/code/local/Wisepricer/Syncer/lib/phpseclib/Crypt/TripleDES.php +690 -0
  21. app/code/local/Wisepricer/Syncer/lib/phpseclib/Math/BigInteger.php +3545 -0
  22. app/code/local/Wisepricer/Syncer/lib/phpseclib/Net/SFTP.php +1461 -0
  23. app/code/local/Wisepricer/Syncer/lib/phpseclib/Net/SSH1.php +1159 -0
  24. app/code/local/Wisepricer/Syncer/lib/phpseclib/Net/SSH2.php +2302 -0
  25. app/code/local/Wisepricer/Syncer/lib/phpseclib/PHP/Compat/Function/array_fill.php +41 -0
  26. app/code/local/Wisepricer/Syncer/lib/phpseclib/PHP/Compat/Function/bcpowmod.php +66 -0
  27. app/code/local/Wisepricer/Syncer/lib/phpseclib/PHP/Compat/Function/str_split.php +59 -0
  28. app/code/local/Wisepricer/Syncer/sql/syncer_setup/mysql4-install-0.1.php +27 -0
  29. app/code/local/Wisepricer/Syncer/sql/syncer_setup/mysql4-install-1.0.0.php +27 -0
  30. app/code/local/Wisepricer/Syncer/sql/syncer_setup/mysql4-install-1.0.2.php +27 -0
  31. app/code/local/Wisepricer/Syncer/sql/syncer_setup/mysql4-upgrade-0.1-0.2.php +29 -0
  32. app/code/local/Wisepricer/Syncer/sql/syncer_setup/mysql4-upgrade-1.0.0-1.1.0.php +29 -0
  33. app/code/local/Wisepricer/Syncer/sql/syncer_setup/mysql4-upgrade-1.0.2-1.1.1.php +31 -0
  34. app/design/adminhtml/default/default/layout/syncer.xml +15 -0
  35. app/design/adminhtml/default/default/template/wisepricer/mapping.phtml +255 -0
  36. app/etc/modules/Wisepricer_Syncer.xml +23 -0
  37. package.xml +18 -0
  38. skin/adminhtml/default/default/images/wisepricer/bullet-green.png +0 -0
  39. skin/adminhtml/default/default/images/wisepricer/validation_advice_bg.gif +0 -0
  40. skin/adminhtml/default/default/images/wisepricer/wp-alert-icon.png +0 -0
  41. skin/adminhtml/default/default/images/wisepricer/wp-logo.png +0 -0
  42. skin/adminhtml/default/default/images/wisepricer/wp-save-btn.png +0 -0
  43. skin/adminhtml/default/default/wisepricer/chosen-sprite.png +0 -0
  44. skin/adminhtml/default/default/wisepricer/chosen.css +396 -0
  45. skin/adminhtml/default/default/wisepricer/chosen.proto.js +1007 -0
  46. skin/adminhtml/default/default/wisepricer/myprototype.js +6082 -0
  47. skin/adminhtml/default/default/wisepricer/prototype17.js +6082 -0
  48. skin/adminhtml/default/default/wisepricer/wisepricer.css +82 -0
app/code/local/Wisepricer/Syncer/Block/Adminhtml/Mapping.php ADDED
@@ -0,0 +1,136 @@
1
+ <?php
2
+
3
+ /******************************************
4
+ * wisepricer *
5
+ ******************************************/
6
+
7
+ class Wisepricer_Syncer_Block_Adminhtml_Mapping extends Mage_Adminhtml_Block_Widget_Form_Container
8
+ {
9
+
10
+ public function getHeader()
11
+ {
12
+ $header = "Wisepricer Mapping";
13
+ return $header;
14
+ }
15
+
16
+ public function renderAttributesSelect($name,$id,$value=0,$class=''){
17
+ $attributesModel=Mage::getModel('wisepricer_syncer/adminhtml_attributes');
18
+ $options=$attributesModel->toOptionArray();
19
+ $mappingModel=Mage::getModel('wisepricer_syncer/mapping');
20
+ $selectHtml='<select style="width:209px" class="select '.$class.'" name="'.$name.'" id="'.$id.'">';
21
+
22
+ $row=$mappingModel->load($id,'wsp_field');
23
+ if($row->getData()){
24
+ $value=$row->getmagento_field();
25
+ }
26
+ foreach($options as $option){
27
+
28
+ if($option['value']==$value){
29
+ $selected='selected';
30
+ }else{
31
+ $selected='';
32
+ }
33
+
34
+ if($option['value']=='enable_googlecheckout'){
35
+ continue;
36
+ }
37
+
38
+ $selectHtml.='<option value="'.$option['value'].'" '.$selected.'>';
39
+ $selectHtml.=$option['label'];
40
+ $selectHtml.='</option>';
41
+ }
42
+ $selectHtml.='</select>';
43
+
44
+ return $selectHtml;
45
+ }
46
+
47
+ public function getShippingFixedRate(){
48
+
49
+ $shipping=$this->_getMagentoFieldByWsField('shipping');
50
+ if($shipping&&is_numeric($shipping)){
51
+ return $shipping;
52
+ }
53
+
54
+ return '';
55
+ }
56
+
57
+ public function getFixedMinPrice(){
58
+
59
+ $minprice=$this->_getMagentoFieldByWsField('minprice');
60
+ if($minprice&&is_numeric($minprice)){
61
+ return $minprice;
62
+ }
63
+
64
+ return '';
65
+ }
66
+
67
+ public function renderMinPriceRuleSelects(){
68
+
69
+ $model = Mage::getModel('wisepricer_syncer/mapping');
70
+ $mappingId=$model->loadIdByWsfield('minprice');
71
+ $valuesArr=array('','');
72
+
73
+ if($mappingId){
74
+ $minpriceRow=$model->load($mappingId);
75
+
76
+ $extra=$minpriceRow->getExtra();
77
+ if($extra){
78
+ $valuesArr=explode(':',$extra);
79
+ }else{
80
+ $valuesArr=array('1','a');
81
+ }
82
+
83
+
84
+ }else{
85
+ $valuesArr=array('1','a');
86
+ }
87
+
88
+ $cur=Mage::app()->getLocale()->currency(Mage::app()->getStore()->getCurrentCurrencyCode())->getSymbol();
89
+
90
+ $typeOptions=array(
91
+ array('label'=>'%','value'=>'-1'),
92
+ array('label'=>$cur,'value'=>'1')
93
+ );
94
+
95
+ $html=$this->_getCustomSelect('mapping_form[type]',$typeOptions,$valuesArr[0],'chzn-select function-select','width:50px');
96
+
97
+ $ruleOptions=array(
98
+ array('label'=>'Above cost','value'=>'a'),
99
+ array('label'=>'Below price','value'=>'b')
100
+ );
101
+
102
+ $html.=$this->_getCustomSelect('mapping_form[rule]',$ruleOptions,$valuesArr[1],'chzn-select function-select','width:136px');
103
+
104
+ return $html;
105
+ }
106
+
107
+ private function _getCustomSelect($name,$options,$default=0,$class='',$style='',$id=''){
108
+
109
+ $select='<select name="'.$name.'" id="'.$id.'" class="'.$class.'" style="'.$style.'">';
110
+ foreach ($options as $option) {
111
+ $selected='';
112
+ if($option['value']==$default){
113
+ $selected='selected';
114
+ }
115
+ $select.='<option value="'.$option['value'].'" '.$selected.'>'.$option['label'].'</option>';
116
+ }
117
+
118
+ $select.='</select>';
119
+
120
+ return $select;
121
+ }
122
+
123
+
124
+ private function _getMagentoFieldByWsField($wsfield){
125
+
126
+ $model = Mage::getModel('wisepricer_syncer/mapping');
127
+ $mappingId=$model->loadIdByWsfield($wsfield);
128
+ if($mappingId){
129
+ $mapping=$model->load($mappingId);
130
+ return $mapping->getmagento_field();
131
+ }
132
+
133
+ return false;
134
+ }
135
+ }
136
+ ?>
app/code/local/Wisepricer/Syncer/Helper/Data.php ADDED
@@ -0,0 +1,11 @@
1
+ <?php
2
+
3
+
4
+
5
+ class Wisepricer_Syncer_Helper_Data extends Mage_Core_Helper_Abstract
6
+
7
+ {
8
+
9
+
10
+
11
+ }
app/code/local/Wisepricer/Syncer/Model/Adminhtml/Attributes.php ADDED
@@ -0,0 +1,70 @@
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Mage
22
+ * @package Mage_Adminhtml
23
+ * @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Used in creating options for Yes|No config value selection
29
+ *
30
+ */
31
+ class Wisepricer_Syncer_Model_Adminhtml_Attributes
32
+ {
33
+ public $fields = array();
34
+ /**
35
+ * Options getter
36
+ *
37
+ * @return array
38
+ */
39
+ public function toOptionArray()
40
+ {
41
+ $this->fields=$this->getOptions();
42
+ return $this->fields;
43
+ }
44
+
45
+ public function getOptions()
46
+ {
47
+ $entityType = Mage::getModel('catalog/product')->getResource()->getEntityType();
48
+ $entityTypeId=$entityType->getId();
49
+ $attributeInfo = Mage::getResourceModel('eav/entity_attribute_collection')
50
+ ->setEntityTypeFilter($entityTypeId)
51
+ ->getData();
52
+ $result=array();
53
+ $result[]=array('value'=>'','label'=>'Choose an attribute');
54
+ foreach($attributeInfo as $_key=>$_value)
55
+ {
56
+ if($_value['is_global'] != "1" || $_value['is_visible']!="1"){
57
+ // continue;
58
+ }
59
+ if(isset($_value['frontend_label'])&&($_value['frontend_label']!='')){
60
+ $result[]=array('value'=>$_value['attribute_code'],'label' => $_value['frontend_label']);
61
+ }else{
62
+ $result[]=array('value'=>$_value['attribute_code'],'label' => $_value['attribute_code']);
63
+ }
64
+
65
+ }
66
+ $result[]=array('value'=>'qty','label'=>'Qty');
67
+ return $result;
68
+ }
69
+
70
+ }
app/code/local/Wisepricer/Syncer/Model/Config.php ADDED
@@ -0,0 +1,13 @@
1
+ <?php
2
+
3
+ class Wisepricer_Syncer_Model_Config extends Mage_Core_Model_Abstract
4
+ {
5
+
6
+ public function _construct()
7
+ {
8
+ parent::_construct();
9
+ $this->_init('wisepricer_syncer/config');
10
+ }
11
+ }
12
+
13
+ ?>
app/code/local/Wisepricer/Syncer/Model/Mapping.php ADDED
@@ -0,0 +1,17 @@
1
+ <?php
2
+
3
+ class Wisepricer_Syncer_Model_Mapping extends Mage_Core_Model_Abstract
4
+ {
5
+
6
+ public function _construct()
7
+ {
8
+ parent::_construct();
9
+ $this->_init('wisepricer_syncer/mapping');
10
+ }
11
+
12
+ public function loadIdByWsfield($ws_field){
13
+ return $this->_getResource()->loadIdByWsfield($this, $ws_field);
14
+ }
15
+ }
16
+
17
+ ?>
app/code/local/Wisepricer/Syncer/Model/Mysql4/Config.php ADDED
@@ -0,0 +1,14 @@
1
+ <?php
2
+
3
+ class Wisepricer_Syncer_Model_Mysql4_Config extends Mage_Core_Model_Mysql4_Abstract
4
+ {
5
+ /**
6
+ * Initialize resource
7
+ *
8
+ */
9
+ protected function _construct()
10
+ {
11
+ $this->_init('wisepricer_syncer/wisepricer_syncer_config', 'licensekey_id');
12
+ }
13
+ }
14
+ ?>
app/code/local/Wisepricer/Syncer/Model/Mysql4/Config/Collection.php ADDED
@@ -0,0 +1,15 @@
1
+ <?php
2
+
3
+ class Wisepricer_Syncer_Model_Mysql4_Config_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
4
+ {
5
+ /**
6
+ * Initialize collection
7
+ *
8
+ */
9
+ public function _construct()
10
+ {
11
+ $this->_init('wisepricer_syncer/wisepricer_syncer_config');
12
+ }
13
+ }
14
+
15
+ ?>
app/code/local/Wisepricer/Syncer/Model/Mysql4/Mapping.php ADDED
@@ -0,0 +1,31 @@
1
+ <?php
2
+
3
+ class Wisepricer_Syncer_Model_Mysql4_Mapping extends Mage_Core_Model_Mysql4_Abstract
4
+ {
5
+ /**
6
+ * Initialize resource
7
+ *
8
+ */
9
+ protected function _construct()
10
+ {
11
+ $this->_init('wisepricer_syncer/wisepricer_syncer_mapping', 'mapping_id');
12
+ }
13
+
14
+ public function loadIdByWsfield(Wisepricer_Syncer_Model_Mapping $mapping, $wsfield, $testOnly = false)
15
+ {
16
+ $adapter = $this->_getReadAdapter();
17
+ $bind = array('wsp_field' => $wsfield);
18
+ $select = $adapter->select()
19
+ ->from('wisepricer_syncer_mapping')
20
+ ->where('wsp_field = :wsp_field');
21
+
22
+
23
+
24
+ $mappingId = $adapter->fetchOne($select, $bind);
25
+
26
+ return $mappingId;
27
+
28
+
29
+ }
30
+ }
31
+ ?>
app/code/local/Wisepricer/Syncer/Model/Mysql4/Mapping/Collection.php ADDED
@@ -0,0 +1,15 @@
1
+ <?php
2
+
3
+ class Wisepricer_Syncer_Model_Mysql4_Mapping_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
4
+ {
5
+ /**
6
+ * Initialize collection
7
+ *
8
+ */
9
+ public function _construct()
10
+ {
11
+ $this->_init('wisepricer_syncer/wisepricer_syncer_mapping');
12
+ }
13
+ }
14
+
15
+ ?>
app/code/local/Wisepricer/Syncer/controllers/Adminhtml/SyncerController.php ADDED
@@ -0,0 +1,182 @@
1
+ <?php
2
+ /**
3
+ * Performs integration between Wisepricer and Magento
4
+ *
5
+ */
6
+ class Wisepricer_Syncer_Adminhtml_SyncerController extends Mage_Adminhtml_Controller_Action
7
+ {
8
+ public function registrateAction()
9
+ {
10
+ $model = Mage::getModel('wisepricer_syncer/config');
11
+ $lisenceData=$model->load(1);
12
+ if(count($lisenceData->getData())>0){
13
+ if($lisenceData->getis_confirmed()==0){
14
+ Mage::getSingleton('adminhtml/session')->addError('Integration not complete.');
15
+ }
16
+ }else{
17
+ Mage::getSingleton('adminhtml/session')->addError('Integration not complete.');
18
+ }
19
+
20
+ $this->loadLayout()->_setActiveMenu('wisepricer');
21
+ $this->renderLayout();
22
+ }
23
+ public function mappingAction()
24
+ {
25
+ $model = Mage::getModel('wisepricer_syncer/config');
26
+ $lisenceData=$model->load(1);
27
+ if(count($lisenceData->getData())>0){
28
+ if($lisenceData->getis_confirmed()==0){
29
+ Mage::getSingleton('adminhtml/session')->addError('Integration not complete.');
30
+ }
31
+ }else{
32
+ Mage::getSingleton('adminhtml/session')->addError('Integration not complete.');
33
+ }
34
+
35
+ $this->loadLayout()->_setActiveMenu('wisepricer');
36
+ $this->renderLayout();
37
+ }
38
+ public function _savekey()
39
+ {
40
+ $post = $this->getRequest()->getPost('register_form');
41
+ $lisensekey = $post['licensekey'];
42
+ try {
43
+ if (empty($lisensekey)) {
44
+ Mage::throwException($this->__('Invalid form data. The license key is missing!'));
45
+ }
46
+ $model = Mage::getModel('wisepricer_syncer/config');
47
+ $lisenceData=$model->load(1);
48
+ if(count($lisenceData->getData())>0){
49
+ $lisenceData->setLicensekey($lisensekey);
50
+ $lisenceData->save();
51
+ }else{
52
+ $model->setLicensekey($lisensekey)->save();
53
+ }
54
+
55
+ }
56
+ catch (Exception $e) {
57
+ Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
58
+ }
59
+ }
60
+ protected function _validateMapping($post){
61
+
62
+ $isValid=false;
63
+ if(
64
+ $post['upc']||
65
+ $post['asin']||
66
+ ($post['brand']&&$post['model'])||
67
+ ($post['brand']&&$post['mpn'])
68
+ ){
69
+ $isValid=true;
70
+ }
71
+ if($post['sku']==''||$post['title']==''||$post['price']==''){
72
+ $isValid=false;
73
+ }
74
+
75
+ if(($post['cost']==''||$post['minprice_man']=='')&&$post['minprice']==''){
76
+ $isValid=false;
77
+ }
78
+
79
+ return $isValid;
80
+ }
81
+ public function savemappingAction(){
82
+ $post= $this->getRequest()->getPost('mapping_form');
83
+ $this->_savekey();
84
+
85
+ $isValid=$this->_validateMapping($post);
86
+ if(!$post||!$isValid){
87
+ Mage::getSingleton('adminhtml/session')->addError('Your mapping is not valid.Please fill the data according to the instruction below.');
88
+ $this->_redirect('*/*/mapping');
89
+ return;
90
+ }
91
+ $data=array();
92
+ $model = Mage::getModel('wisepricer_syncer/mapping');
93
+ $allEntered=true;
94
+
95
+ foreach($post as $name=>$value){
96
+
97
+ if(!$value||$name=='type'||$name=='rule'){
98
+ continue;
99
+ }
100
+
101
+ $fieldName=$name;
102
+
103
+ if($name=='shipping_man'){
104
+ $fieldName='shipping';
105
+ }
106
+
107
+
108
+ $data=array('wsp_field'=>$fieldName,'magento_field'=>$value);
109
+
110
+ if($name=='minprice_man'){
111
+ $fieldName='minprice';
112
+ $function=$post['type'].':'.$post['rule'];
113
+ $data=array('wsp_field'=>$fieldName,'magento_field'=>$value,'extra'=>$function);
114
+ }
115
+
116
+ $mappingId=$model->loadIdByWsfield($fieldName);
117
+
118
+ if($mappingId){
119
+
120
+ $mapping=$model->load($mappingId);
121
+ $mapping->setmagento_field($value);
122
+
123
+ if($name=='minprice_man'){
124
+ $mapping->setExtra($function);
125
+ }
126
+
127
+ try {
128
+ $mapping->save()->getId();
129
+ } catch (Exception $e){
130
+ Mage::getSingleton('adminhtml/session')->addError('Insert of the field "'.$fieldName.'" failed with a following message: '.$e->getMessage());
131
+ $allEntered=false;
132
+ }
133
+
134
+ }else{
135
+ $model->setData($data);
136
+ try {
137
+ $model->save()->getId();
138
+ } catch (Exception $e){
139
+ Mage::getSingleton('adminhtml/session')->addError('Insert of the field "'.$fieldName.'" failed with a following message: '.$e->getMessage());
140
+ $allEntered=false;
141
+ }
142
+ }
143
+
144
+
145
+
146
+ }
147
+
148
+ $this->_redirect('*/*/mapping');
149
+ if($allEntered){
150
+ $lisenceModel=Mage::getModel('wisepricer_syncer/config')->load(1);
151
+ if($lisenceModel->getData()){
152
+ if($lisenceModel->getis_confirmed()==0){
153
+ $this->_createKeys($lisenceModel);
154
+ $lisenceModel->setis_confirmed(1)->save();
155
+ Mage::getSingleton('adminhtml/session')->addSuccess('Your integration with Wisepricer now is complete!');
156
+ }
157
+ }else{
158
+ Mage::throwException($this->__('Invalid form data. The license key is missing!'));
159
+ }
160
+ }
161
+
162
+ }
163
+
164
+ public function checkcompatAction(){
165
+
166
+ }
167
+
168
+ protected function _createKeys($lisenceModel){
169
+
170
+ set_include_path(get_include_path().PS .BP.DS . 'lib'.DS.'phpseclib' . PS.BP.DS.'app'.DS.'code'.DS.'local'.DS.'Wisepricer'.DS.'Syncer'.DS.'lib'.DS.'phpseclib' );
171
+ include('Crypt'.DS.'RSA.php');
172
+
173
+ $rsa = new Crypt_RSA();
174
+
175
+ // Create the keypair
176
+ $keyArr = $rsa->createKey();
177
+ $lisenceModel->setpublickey($keyArr['publickey']);
178
+ $lisenceModel->setprivatekey($keyArr['privatekey']);
179
+ }
180
+
181
+ }
182
+ ?>
app/code/local/Wisepricer/Syncer/controllers/ProductsController.php ADDED
@@ -0,0 +1,430 @@
1
+ <?php
2
+ /**
3
+ * Performs integration between Wisepricer and Magento
4
+ *
5
+ */
6
+ class Wisepricer_Syncer_ProductsController extends Mage_Core_Controller_Front_Action
7
+ {
8
+
9
+ var $productOrigData=array();
10
+
11
+ public function sendAction(){
12
+ // Mage::log('start sending',null,'wplog.log');
13
+ set_time_limit (1800);
14
+ $licenseData =Mage::getModel('wisepricer_syncer/config')->load(1);
15
+ if(!$licenseData->getData()||$licenseData->getIs_confirmed()==0){
16
+
17
+ $returnArr=array(
18
+ 'status'=>'failure',
19
+ 'error_code'=>'769',
20
+ 'error_details'=>'The user has not completed the integration.'
21
+ );
22
+ echo json_encode($returnArr);
23
+ die;
24
+ }
25
+
26
+ $post = $this->getRequest()->getParams();
27
+
28
+ $magentoSessionId=Mage::getModel('core/cookie')->get('wpsession');
29
+
30
+ if($magentoSessionId!=$post['sesssionid']){
31
+
32
+ $returnArr=array(
33
+ 'status'=>'failure',
34
+ 'error_code'=>'771',
35
+ 'error_details'=>'Unauthorized access.'
36
+ );
37
+ echo json_encode($returnArr);
38
+ die;
39
+ }
40
+
41
+ //decryption process
42
+
43
+ $startInd = $post['start'];
44
+ if(!$startInd){
45
+ $startInd=0;
46
+ }
47
+
48
+ $count = $post['count'];
49
+ if(!$count||$count>200){
50
+ $count=200;
51
+ }
52
+ $fieldsEncoded= $post['params'];
53
+ $fields =json_decode(urldecode ($fieldsEncoded));
54
+
55
+ $mappings =Mage::getModel('wisepricer_syncer/mapping')->getCollection()->getData();
56
+
57
+ $collection=Mage::getModel('catalog/product')->getCollection();
58
+
59
+ $requiredFields=array();
60
+ foreach ($mappings as $fieldsArr) {
61
+ if($fields[0]=='all'||in_array($fieldsArr['wsp_field'],$fields)){
62
+ $requiredFields[]=$fieldsArr;
63
+ }
64
+
65
+ if(!is_numeric($fieldsArr['magento_field'])){
66
+ $collection->addAttributeToSelect($fieldsArr['magento_field']);
67
+ }
68
+
69
+ }
70
+
71
+
72
+ $productsOutput=array();
73
+
74
+
75
+
76
+ foreach ($collection as $product) {
77
+ // echo '<pre>'.print_r($product->getData(),true).'</pre>';die;
78
+ $productCollData=$product->getData();
79
+ $productModel=Mage::getModel('catalog/product')->load($productCollData['entity_id']);
80
+
81
+ //TODO add user control here - dont bring not saleble products
82
+ if(!$productModel->isSaleable()){
83
+ continue;
84
+ }
85
+ $this->productOrigData=$productModel->getData();
86
+ $productData=array();
87
+
88
+ foreach ($requiredFields as $field) {
89
+ if($field['wsp_field']=='stock'){
90
+
91
+ $qtyStock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->getQty();
92
+ $productData['stock']=(int)$qtyStock;
93
+
94
+ }elseif($field['wsp_field']=='productimage'){
95
+
96
+ if($field['magento_field']=='image'||$field['magento_field']=='small_image'){
97
+ $siteUrl=substr_replace(Mage::getUrl('media/catalog/product') ,"",-1);
98
+ $productData['productimage']=$siteUrl.$this->productOrigData['image'];
99
+ }else{
100
+ $productData['productimage']=$this->productOrigData['image'];
101
+ }
102
+
103
+
104
+ }elseif($field['wsp_field']=='shipping'){
105
+
106
+ if(is_numeric($productOrigData['shipping'])){
107
+ $productData['shipping']=$field['magento_field'];
108
+ }else{
109
+ $productData['shipping']=$this->productOrigData[$field['magento_field']];
110
+ }
111
+
112
+ }elseif($field['wsp_field']=='minprice'){
113
+ if(is_numeric($field['magento_field'])){
114
+
115
+ $productData[$field['wsp_field']]=$this->_calculateMinPrice($field);
116
+
117
+ }else{
118
+ $productData[$field['wsp_field']]=$this->productOrigData[$field['magento_field']];
119
+ }
120
+ }else{
121
+ $attributeInfo = Mage::getResourceModel('eav/entity_attribute_collection')
122
+ ->setCodeFilter($field['magento_field'])
123
+ ->getFirstItem();
124
+
125
+
126
+ if($attributeInfo->getfrontend_input()=='select'){
127
+ $attrLabel=$productModel->getAttributeText($field['magento_field']);
128
+ $productData[$field['wsp_field']]=$attrLabel;
129
+ // echo '<pre>'.print_r($attr,true).'</pre>';//die;
130
+ }else{
131
+ $productData[$field['wsp_field']]=$this->productOrigData[$field['magento_field']];
132
+ }
133
+
134
+
135
+ }
136
+
137
+
138
+ }
139
+
140
+ $productData['producturl']=Mage::helper('catalog/product')->getProductUrl($productCollData['entity_id']);
141
+
142
+ $productsOutput[]=$productData;
143
+ }
144
+ $productsOutput= array_splice($productsOutput, $startInd, $count);
145
+ // echo '<pre>'.print_r($productsOutput,true).'</pre>'; die;
146
+ $returnArr=array(
147
+ 'status'=>'success',
148
+ 'error_code'=>'0',
149
+ 'data'=>$productsOutput
150
+ );
151
+ // Mage::log('sending '.count($productsOutput).' products',null,'wplog.log');
152
+ echo json_encode($returnArr);
153
+
154
+ }
155
+
156
+ private function _calculateMinPrice($field){
157
+
158
+ $ruleArr= explode(':',$field['extra']);
159
+
160
+ if($ruleArr[1]=='a'){
161
+ $costField=$this->_getMagentoFieldByWsField('cost');
162
+ if($costField){
163
+ $cost=$this->productOrigData[$costField];
164
+ if($ruleArr[0]=='-1'){
165
+ $minPrice=($cost*$field['magento_field'])/100+$cost;
166
+ }else{
167
+ $minPrice=$cost+$field['magento_field'];
168
+ }
169
+ }
170
+ }else{
171
+ $priceField=$this->_getMagentoFieldByWsField('price');
172
+ $price=$this->productOrigData[$priceField];
173
+ if($ruleArr[0]=='-1'){
174
+ $minPrice=$price-($price*$field['magento_field'])/100;
175
+ }else{
176
+ $minPrice=$price-$field['magento_field'];
177
+ }
178
+ }
179
+ return $minPrice;
180
+
181
+ }
182
+
183
+ private function _getMagentoFieldByWsField($wsfield){
184
+
185
+ $model = Mage::getModel('wisepricer_syncer/mapping');
186
+ $mappingId=$model->loadIdByWsfield($wsfield);
187
+ if($mappingId){
188
+ $mapping=$model->load($mappingId);
189
+ return $mapping->getmagento_field();
190
+ }
191
+
192
+ return false;
193
+ }
194
+
195
+ private function _randString( $length ) {
196
+ $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
197
+ $str='';
198
+ $size = strlen( $chars );
199
+ for( $i = 0; $i < $length; $i++ ) {
200
+ $str .= $chars[ rand( 0, $size - 1 ) ];
201
+ }
202
+
203
+ return $str;
204
+ }
205
+
206
+ public function loginAction(){
207
+
208
+ $licenseData =Mage::getModel('wisepricer_syncer/config')->load(1);
209
+ if(!$licenseData->getData()||$licenseData->getIs_confirmed()==0){
210
+
211
+ $returnArr=array(
212
+ 'status'=>'failure',
213
+ 'error_code'=>'769',
214
+ 'error_details'=>'The user has not completed the integration.'
215
+ );
216
+ echo json_encode($returnArr);
217
+ die;
218
+ }
219
+
220
+ $post = $this->getRequest()->getParams();
221
+
222
+ $lisensekeyEncr = $post['licensekey'];
223
+ Mage::log(print_r($lisensekeyEncr,true),null,'wplog.log');
224
+ $lisensekeyEncr=pack('H*', $lisensekeyEncr);
225
+ Mage::log(print_r($lisensekeyEncr,true),null,'wplog.log');
226
+ $lisensekey=$this->_decryptstring($lisensekeyEncr);
227
+ Mage::log(print_r($lisensekey,true),null,'wplog.log');
228
+ if($licenseData->getLicensekey()!=$lisensekey){
229
+
230
+ $returnArr=array(
231
+ 'status'=>'failure',
232
+ 'error_code'=>'771',
233
+ 'error_details'=>'Unauthorized access.'
234
+ );
235
+ echo json_encode($returnArr);
236
+ die;
237
+ }
238
+
239
+ $sessionId=$this->_randString(8);
240
+
241
+ Mage::getModel('core/cookie')->set('wpsession',$sessionId , true);
242
+
243
+ $returnArr=array(
244
+ 'status'=>'success',
245
+ 'error_code'=>'0',
246
+ 'session_id'=>$sessionId
247
+ );
248
+ Mage::log(print_r('=========================================================',true),null,'wplog.log');
249
+ echo json_encode($returnArr);
250
+ }
251
+
252
+ public function logoutAction(){
253
+
254
+ $post = $this->getRequest()->getParams();
255
+
256
+ $magentoSessionId=Mage::getModel('core/cookie')->get('wpsession');
257
+
258
+ if($magentoSessionId!=$post['sesssionid']){
259
+
260
+ $returnArr=array(
261
+ 'status'=>'failure',
262
+ 'error_code'=>'771',
263
+ 'error_details'=>'Unauthorized access.'
264
+ );
265
+ echo json_encode($returnArr);
266
+ die;
267
+ }
268
+
269
+ Mage::getModel('core/cookie')->delete('wpsession');
270
+ $returnArr=array(
271
+ 'status'=>'success',
272
+ 'error_code'=>'0'
273
+ );
274
+
275
+ echo json_encode($returnArr);
276
+ }
277
+ public function getpublicAction(){
278
+
279
+ $publickey=$this->_getpublickey();
280
+
281
+ $returnArr=array(
282
+ 'status'=>'success',
283
+ 'error_code'=>'0',
284
+ 'publickey'=>$publickey
285
+ );
286
+
287
+ echo json_encode($returnArr);
288
+ }
289
+
290
+ protected function _getprivatekey(){
291
+
292
+ $licenseData =Mage::getModel('wisepricer_syncer/config')->load(1);
293
+ if(!$licenseData->getData()||$licenseData->getIs_confirmed()==0){
294
+
295
+ $returnArr=array(
296
+ 'status'=>'failure',
297
+ 'error_code'=>'769',
298
+ 'error_details'=>'The user has not completed the integration.'
299
+ );
300
+ echo json_encode($returnArr);
301
+ die;
302
+ }
303
+
304
+ return $licenseData->getprivatekey();
305
+ }
306
+
307
+ protected function _getpublickey(){
308
+
309
+ $licenseData =Mage::getModel('wisepricer_syncer/config')->load(1);
310
+ if(!$licenseData->getData()||$licenseData->getIs_confirmed()==0){
311
+
312
+ $returnArr=array(
313
+ 'status'=>'failure',
314
+ 'error_code'=>'769',
315
+ 'error_details'=>'The user has not completed the integration.'
316
+ );
317
+ echo json_encode($returnArr);
318
+ die;
319
+ }
320
+
321
+ return $licenseData->getpublickey();
322
+ }
323
+
324
+ protected function _decryptstring($str){
325
+
326
+ set_include_path(get_include_path().PS .BP.DS . 'lib'.DS.'phpseclib' . PS.BP.DS.'app'.DS.'code'.DS.'local'.DS.'Wisepricer'.DS.'Syncer'.DS.'lib'.DS.'phpseclib' );
327
+ include('Crypt'.DS.'RSA.php');
328
+
329
+ $rsa = new Crypt_RSA();
330
+ $rsa->loadKey($this->_getprivatekey());
331
+ return $rsa->decrypt($str);
332
+ }
333
+
334
+ public function repriceAction(){
335
+
336
+ $licenseData =Mage::getModel('wisepricer_syncer/config')->load(1);
337
+ if(!$licenseData->getData()||$licenseData->getIs_confirmed()==0){
338
+
339
+ $returnArr=array(
340
+ 'status'=>'failure',
341
+ 'error_code'=>'769',
342
+ 'error_details'=>'The user has not completed the integration.'
343
+ );
344
+ echo json_encode($returnArr);
345
+ die;
346
+ }
347
+
348
+ $post = $this->getRequest()->getParams();
349
+
350
+ $lisensekeyEncr = $post['licensekey'];
351
+
352
+ $lisensekey=$this->_decryptstring($lisensekeyEncr);
353
+
354
+ if($licenseData->getLicensekey()!=$lisensekey){
355
+
356
+ $returnArr=array(
357
+ 'status'=>'failure',
358
+ 'error_code'=>'771',
359
+ 'error_details'=>'Unauthorized access.'
360
+ );
361
+ echo json_encode($returnArr);
362
+ die;
363
+ }
364
+
365
+ $productsEncoded= $post['products'];
366
+ $products =json_decode(urldecode ($productsEncoded));
367
+ $responseArr=array();
368
+ $sucessCounter=0;
369
+ $failedCounter=0;
370
+ foreach ($products as $prodArr) {
371
+ $model = Mage::getModel('catalog/product');
372
+ $product = $model->load($model->getIdBySku($prodArr->sku));
373
+
374
+ try {
375
+ if ($product != null) {
376
+ if (isset($prodArr->price)) {
377
+ $priceOld = $product->setPrice($prodArr->price);
378
+ }
379
+
380
+ /*if (isset($spPrice)) {
381
+ $priceOld = $product->setSpecialPrice($spPrice);
382
+ }*/
383
+
384
+ $product->save();
385
+ $responseArr[]=array('sku'=>$prodArr->sku,'error_code'=>'0');
386
+ $sucessCounter++;
387
+ }else{
388
+ $responseArr[]=array('sku'=>$prodArr->sku,'error_code'=>'333','error_details'=>'SKU could not be loaded');
389
+ $failedCounter++;
390
+ }
391
+ } catch (Exception $exc) {
392
+ $responseArr[]=array('sku'=>$prodArr->sku,'error_code'=>'444','error_details'=>$exc->getMessage());
393
+ $failedCounter++;
394
+ }
395
+
396
+
397
+ }
398
+
399
+ $returnArr=array();
400
+ $returnArr['ResultData']= $responseArr;
401
+ $returnArr['Succeeded']=$sucessCounter;
402
+ $returnArr['Failed']=$failedCounter;
403
+ echo json_encode($returnArr);
404
+ die;
405
+ }
406
+
407
+ public function generatejasonAction(){
408
+ $result['products']=array(
409
+ array(
410
+ 'sku'=>'zol_r_sm',
411
+ 'price'=>'303.3'
412
+ ),
413
+ array(
414
+ 'sku'=>'4fasd5f5',
415
+ 'price'=>'770.0'
416
+ )
417
+ );
418
+ echo json_encode($result['products']);
419
+ die;
420
+ }
421
+
422
+ public function pingAction(){
423
+
424
+ $result['storeurl']=Mage::getUrl();
425
+ echo json_encode($result);
426
+ die;
427
+ }
428
+
429
+ }
430
+ ?>
app/code/local/Wisepricer/Syncer/etc/config.xml ADDED
@@ -0,0 +1,126 @@
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <modules>
4
+ <Wisepricer_Syncer>
5
+ <version>1.1.2.1</version>
6
+ <url>http://www.wisepricer.com/index.php</url>
7
+ <modulename>Wisepricer Syncer</modulename>
8
+ </Wisepricer_Syncer>
9
+ </modules>
10
+ <syncer>
11
+ <version>1.2</version>
12
+ </syncer>
13
+ <global>
14
+ <models>
15
+ <wisepricer_syncer>
16
+ <class>Wisepricer_Syncer_Model</class>
17
+ <resourceModel>wisepricer_syncer_mysql4</resourceModel>
18
+ </wisepricer_syncer>
19
+ <wisepricer_syncer_mysql4>
20
+ <class>Wisepricer_Syncer_Model_Mysql4</class>
21
+ <entities>
22
+ <wisepricer_syncer_config>
23
+ <table>wisepricer_syncer_config</table>
24
+ </wisepricer_syncer_config>
25
+ <wisepricer_syncer_mapping>
26
+ <table>wisepricer_syncer_mapping</table>
27
+ </wisepricer_syncer_mapping>
28
+ </entities>
29
+ </wisepricer_syncer_mysql4>
30
+ </models>
31
+ <resources>
32
+ <syncer_setup>
33
+ <setup>
34
+ <module>Wisepricer_Syncer</module>
35
+ </setup>
36
+ <connection>
37
+ <use>core_setup</use>
38
+ </connection>
39
+ </syncer_setup>
40
+ <syncer_write>
41
+ <connection>
42
+ <use>core_write</use>
43
+ </connection>
44
+ </syncer_write>
45
+ <syncer_read>
46
+ <connection>
47
+ <use>core_read</use>
48
+ </connection>
49
+ </syncer_read>
50
+ </resources>
51
+ <blocks>
52
+ <syncer>
53
+ <class>Wisepricer_Syncer_Block</class>
54
+ </syncer>
55
+ </blocks>
56
+ <helpers>
57
+ <syncer>
58
+ <class>Wisepricer_Syncer_Helper</class>
59
+ </syncer>
60
+ </helpers>
61
+ </global>
62
+ <frontend>
63
+ <routers>
64
+ <wisepricer>
65
+ <use>standard</use>
66
+ <args>
67
+ <module>Wisepricer_Syncer</module>
68
+ <frontName>wisepricer</frontName>
69
+ </args>
70
+ </wisepricer>
71
+ </routers>
72
+ </frontend>
73
+ <admin>
74
+ <routers>
75
+ <adminhtml>
76
+ <args>
77
+ <modules>
78
+ <Wisepricer_Syncer_Adminhtml before="Mage_Adminhtml">Wisepricer_Syncer_Adminhtml</Wisepricer_Syncer_Adminhtml>
79
+ </modules>
80
+ </args>
81
+ </adminhtml>
82
+ </routers>
83
+ </admin>
84
+ <adminhtml>
85
+ <acl>
86
+ <resources>
87
+ <admin>
88
+ <children>
89
+ <system>
90
+ <children>
91
+ <config>
92
+ <children>
93
+ <syncer>
94
+ <title>Wisepricer Configuration</title>
95
+ </syncer>
96
+ </children>
97
+ </config>
98
+ </children>
99
+ </system>
100
+ </children>
101
+ </admin>
102
+ </resources>
103
+ </acl>
104
+ <layout>
105
+ <updates>
106
+ <wisepricer_syncer>
107
+ <file>syncer.xml</file>
108
+ </wisepricer_syncer>
109
+ </updates>
110
+ </layout>
111
+ <menu>
112
+ <wisepricer>
113
+ <title>Wisepricer</title>
114
+ <sort_order>70</sort_order>
115
+ <action>adminhtml/syncer/mapping</action>
116
+ </wisepricer>
117
+ </menu>
118
+ </adminhtml>
119
+ <default>
120
+ <syncer>
121
+ <config>
122
+ <ftpport>21</ftpport>
123
+ </config>
124
+ </syncer>
125
+ </default>
126
+ </config>
app/code/local/Wisepricer/Syncer/lib/phpseclib/Crypt/AES.php ADDED
@@ -0,0 +1,479 @@
1
+ <?php
2
+ /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
+
4
+ /**
5
+ * Pure-PHP implementation of AES.
6
+ *
7
+ * Uses mcrypt, if available, and an internal implementation, otherwise.
8
+ *
9
+ * PHP versions 4 and 5
10
+ *
11
+ * If {@link Crypt_AES::setKeyLength() setKeyLength()} isn't called, it'll be calculated from
12
+ * {@link Crypt_AES::setKey() setKey()}. ie. if the key is 128-bits, the key length will be 128-bits. If it's 136-bits
13
+ * it'll be null-padded to 160-bits and 160 bits will be the key length until {@link Crypt_Rijndael::setKey() setKey()}
14
+ * is called, again, at which point, it'll be recalculated.
15
+ *
16
+ * Since Crypt_AES extends Crypt_Rijndael, some functions are available to be called that, in the context of AES, don't
17
+ * make a whole lot of sense. {@link Crypt_AES::setBlockLength() setBlockLength()}, for instance. Calling that function,
18
+ * however possible, won't do anything (AES has a fixed block length whereas Rijndael has a variable one).
19
+ *
20
+ * Here's a short example of how to use this library:
21
+ * <code>
22
+ * <?php
23
+ * include('Crypt/AES.php');
24
+ *
25
+ * $aes = new Crypt_AES();
26
+ *
27
+ * $aes->setKey('abcdefghijklmnop');
28
+ *
29
+ * $size = 10 * 1024;
30
+ * $plaintext = '';
31
+ * for ($i = 0; $i < $size; $i++) {
32
+ * $plaintext.= 'a';
33
+ * }
34
+ *
35
+ * echo $aes->decrypt($aes->encrypt($plaintext));
36
+ * ?>
37
+ * </code>
38
+ *
39
+ * LICENSE: This library is free software; you can redistribute it and/or
40
+ * modify it under the terms of the GNU Lesser General Public
41
+ * License as published by the Free Software Foundation; either
42
+ * version 2.1 of the License, or (at your option) any later version.
43
+ *
44
+ * This library is distributed in the hope that it will be useful,
45
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
46
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
47
+ * Lesser General Public License for more details.
48
+ *
49
+ * You should have received a copy of the GNU Lesser General Public
50
+ * License along with this library; if not, write to the Free Software
51
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
52
+ * MA 02111-1307 USA
53
+ *
54
+ * @category Crypt
55
+ * @package Crypt_AES
56
+ * @author Jim Wigginton <terrafrost@php.net>
57
+ * @copyright MMVIII Jim Wigginton
58
+ * @license http://www.gnu.org/licenses/lgpl.txt
59
+ * @version $Id: AES.php,v 1.7 2010/02/09 06:10:25 terrafrost Exp $
60
+ * @link http://phpseclib.sourceforge.net
61
+ */
62
+
63
+ /**
64
+ * Include Crypt_Rijndael
65
+ */
66
+ require_once 'Rijndael.php';
67
+
68
+ /**#@+
69
+ * @access public
70
+ * @see Crypt_AES::encrypt()
71
+ * @see Crypt_AES::decrypt()
72
+ */
73
+ /**
74
+ * Encrypt / decrypt using the Counter mode.
75
+ *
76
+ * Set to -1 since that's what Crypt/Random.php uses to index the CTR mode.
77
+ *
78
+ * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Counter_.28CTR.29
79
+ */
80
+ define('CRYPT_AES_MODE_CTR', -1);
81
+ /**
82
+ * Encrypt / decrypt using the Electronic Code Book mode.
83
+ *
84
+ * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Electronic_codebook_.28ECB.29
85
+ */
86
+ define('CRYPT_AES_MODE_ECB', 1);
87
+ /**
88
+ * Encrypt / decrypt using the Code Book Chaining mode.
89
+ *
90
+ * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher-block_chaining_.28CBC.29
91
+ */
92
+ define('CRYPT_AES_MODE_CBC', 2);
93
+ /**#@-*/
94
+
95
+ /**#@+
96
+ * @access private
97
+ * @see Crypt_AES::Crypt_AES()
98
+ */
99
+ /**
100
+ * Toggles the internal implementation
101
+ */
102
+ define('CRYPT_AES_MODE_INTERNAL', 1);
103
+ /**
104
+ * Toggles the mcrypt implementation
105
+ */
106
+ define('CRYPT_AES_MODE_MCRYPT', 2);
107
+ /**#@-*/
108
+
109
+ /**
110
+ * Pure-PHP implementation of AES.
111
+ *
112
+ * @author Jim Wigginton <terrafrost@php.net>
113
+ * @version 0.1.0
114
+ * @access public
115
+ * @package Crypt_AES
116
+ */
117
+ class Crypt_AES extends Crypt_Rijndael {
118
+ /**
119
+ * mcrypt resource for encryption
120
+ *
121
+ * The mcrypt resource can be recreated every time something needs to be created or it can be created just once.
122
+ * Since mcrypt operates in continuous mode, by default, it'll need to be recreated when in non-continuous mode.
123
+ *
124
+ * @see Crypt_AES::encrypt()
125
+ * @var String
126
+ * @access private
127
+ */
128
+ var $enmcrypt;
129
+
130
+ /**
131
+ * mcrypt resource for decryption
132
+ *
133
+ * The mcrypt resource can be recreated every time something needs to be created or it can be created just once.
134
+ * Since mcrypt operates in continuous mode, by default, it'll need to be recreated when in non-continuous mode.
135
+ *
136
+ * @see Crypt_AES::decrypt()
137
+ * @var String
138
+ * @access private
139
+ */
140
+ var $demcrypt;
141
+
142
+ /**
143
+ * Default Constructor.
144
+ *
145
+ * Determines whether or not the mcrypt extension should be used. $mode should only, at present, be
146
+ * CRYPT_AES_MODE_ECB or CRYPT_AES_MODE_CBC. If not explictly set, CRYPT_AES_MODE_CBC will be used.
147
+ *
148
+ * @param optional Integer $mode
149
+ * @return Crypt_AES
150
+ * @access public
151
+ */
152
+ function Crypt_AES($mode = CRYPT_AES_MODE_CBC)
153
+ {
154
+ if ( !defined('CRYPT_AES_MODE') ) {
155
+ switch (true) {
156
+ case extension_loaded('mcrypt'):
157
+ // i'd check to see if aes was supported, by doing in_array('des', mcrypt_list_algorithms('')),
158
+ // but since that can be changed after the object has been created, there doesn't seem to be
159
+ // a lot of point...
160
+ define('CRYPT_AES_MODE', CRYPT_AES_MODE_MCRYPT);
161
+ break;
162
+ default:
163
+ define('CRYPT_AES_MODE', CRYPT_AES_MODE_INTERNAL);
164
+ }
165
+ }
166
+
167
+ switch ( CRYPT_AES_MODE ) {
168
+ case CRYPT_AES_MODE_MCRYPT:
169
+ switch ($mode) {
170
+ case CRYPT_AES_MODE_ECB:
171
+ $this->mode = MCRYPT_MODE_ECB;
172
+ break;
173
+ case CRYPT_AES_MODE_CTR:
174
+ // ctr doesn't have a constant associated with it even though it appears to be fairly widely
175
+ // supported. in lieu of knowing just how widely supported it is, i've, for now, opted not to
176
+ // include a compatibility layer. the layer has been implemented but, for now, is commented out.
177
+ $this->mode = 'ctr';
178
+ //$this->mode = in_array('ctr', mcrypt_list_modes()) ? 'ctr' : CRYPT_AES_MODE_CTR;
179
+ break;
180
+ case CRYPT_AES_MODE_CBC:
181
+ default:
182
+ $this->mode = MCRYPT_MODE_CBC;
183
+ }
184
+
185
+ break;
186
+ default:
187
+ switch ($mode) {
188
+ case CRYPT_AES_MODE_ECB:
189
+ $this->mode = CRYPT_RIJNDAEL_MODE_ECB;
190
+ break;
191
+ case CRYPT_AES_MODE_CTR:
192
+ $this->mode = CRYPT_RIJNDAEL_MODE_CTR;
193
+ break;
194
+ case CRYPT_AES_MODE_CBC:
195
+ default:
196
+ $this->mode = CRYPT_RIJNDAEL_MODE_CBC;
197
+ }
198
+ }
199
+
200
+ if (CRYPT_AES_MODE == CRYPT_AES_MODE_INTERNAL) {
201
+ parent::Crypt_Rijndael($this->mode);
202
+ }
203
+ }
204
+
205
+ /**
206
+ * Dummy function
207
+ *
208
+ * Since Crypt_AES extends Crypt_Rijndael, this function is, technically, available, but it doesn't do anything.
209
+ *
210
+ * @access public
211
+ * @param Integer $length
212
+ */
213
+ function setBlockLength($length)
214
+ {
215
+ return;
216
+ }
217
+
218
+ /**
219
+ * Encrypts a message.
220
+ *
221
+ * $plaintext will be padded with up to 16 additional bytes. Other AES implementations may or may not pad in the
222
+ * same manner. Other common approaches to padding and the reasons why it's necessary are discussed in the following
223
+ * URL:
224
+ *
225
+ * {@link http://www.di-mgt.com.au/cryptopad.html http://www.di-mgt.com.au/cryptopad.html}
226
+ *
227
+ * An alternative to padding is to, separately, send the length of the file. This is what SSH, in fact, does.
228
+ * strlen($plaintext) will still need to be a multiple of 16, however, arbitrary values can be added to make it that
229
+ * length.
230
+ *
231
+ * @see Crypt_AES::decrypt()
232
+ * @access public
233
+ * @param String $plaintext
234
+ */
235
+ function encrypt($plaintext)
236
+ {
237
+ if ( CRYPT_AES_MODE == CRYPT_AES_MODE_MCRYPT ) {
238
+ $this->_mcryptSetup();
239
+ /*
240
+ if ($this->mode == CRYPT_AES_MODE_CTR) {
241
+ $iv = $this->encryptIV;
242
+ $xor = mcrypt_generic($this->enmcrypt, $this->_generate_xor(strlen($plaintext), $iv));
243
+ $ciphertext = $plaintext ^ $xor;
244
+ if ($this->continuousBuffer) {
245
+ $this->encryptIV = $iv;
246
+ }
247
+ return $ciphertext;
248
+ }
249
+ */
250
+
251
+ if ($this->mode != 'ctr') {
252
+ $plaintext = $this->_pad($plaintext);
253
+ }
254
+
255
+ $ciphertext = mcrypt_generic($this->enmcrypt, $plaintext);
256
+
257
+ if (!$this->continuousBuffer) {
258
+ mcrypt_generic_init($this->enmcrypt, $this->key, $this->iv);
259
+ }
260
+
261
+ return $ciphertext;
262
+ }
263
+
264
+ return parent::encrypt($plaintext);
265
+ }
266
+
267
+ /**
268
+ * Decrypts a message.
269
+ *
270
+ * If strlen($ciphertext) is not a multiple of 16, null bytes will be added to the end of the string until it is.
271
+ *
272
+ * @see Crypt_AES::encrypt()
273
+ * @access public
274
+ * @param String $ciphertext
275
+ */
276
+ function decrypt($ciphertext)
277
+ {
278
+ if ( CRYPT_AES_MODE == CRYPT_AES_MODE_MCRYPT ) {
279
+ $this->_mcryptSetup();
280
+ /*
281
+ if ($this->mode == CRYPT_AES_MODE_CTR) {
282
+ $iv = $this->decryptIV;
283
+ $xor = mcrypt_generic($this->enmcrypt, $this->_generate_xor(strlen($ciphertext), $iv));
284
+ $plaintext = $ciphertext ^ $xor;
285
+ if ($this->continuousBuffer) {
286
+ $this->decryptIV = $iv;
287
+ }
288
+ return $plaintext;
289
+ }
290
+ */
291
+
292
+ if ($this->mode != 'ctr') {
293
+ // we pad with chr(0) since that's what mcrypt_generic does. to quote from http://php.net/function.mcrypt-generic :
294
+ // "The data is padded with "\0" to make sure the length of the data is n * blocksize."
295
+ $ciphertext = str_pad($ciphertext, (strlen($ciphertext) + 15) & 0xFFFFFFF0, chr(0));
296
+ }
297
+
298
+ $plaintext = mdecrypt_generic($this->demcrypt, $ciphertext);
299
+
300
+ if (!$this->continuousBuffer) {
301
+ mcrypt_generic_init($this->demcrypt, $this->key, $this->iv);
302
+ }
303
+
304
+ return $this->mode != 'ctr' ? $this->_unpad($plaintext) : $plaintext;
305
+ }
306
+
307
+ return parent::decrypt($ciphertext);
308
+ }
309
+
310
+ /**
311
+ * Setup mcrypt
312
+ *
313
+ * Validates all the variables.
314
+ *
315
+ * @access private
316
+ */
317
+ function _mcryptSetup()
318
+ {
319
+ if (!$this->changed) {
320
+ return;
321
+ }
322
+
323
+ if (!$this->explicit_key_length) {
324
+ // this just copied from Crypt_Rijndael::_setup()
325
+ $length = strlen($this->key) >> 2;
326
+ if ($length > 8) {
327
+ $length = 8;
328
+ } else if ($length < 4) {
329
+ $length = 4;
330
+ }
331
+ $this->Nk = $length;
332
+ $this->key_size = $length << 2;
333
+ }
334
+
335
+ switch ($this->Nk) {
336
+ case 4: // 128
337
+ $this->key_size = 16;
338
+ break;
339
+ case 5: // 160
340
+ case 6: // 192
341
+ $this->key_size = 24;
342
+ break;
343
+ case 7: // 224
344
+ case 8: // 256
345
+ $this->key_size = 32;
346
+ }
347
+
348
+ $this->key = substr($this->key, 0, $this->key_size);
349
+ $this->encryptIV = $this->decryptIV = $this->iv = str_pad(substr($this->iv, 0, 16), 16, chr(0));
350
+
351
+ if (!isset($this->enmcrypt)) {
352
+ $mode = $this->mode;
353
+ //$mode = $this->mode == CRYPT_AES_MODE_CTR ? MCRYPT_MODE_ECB : $this->mode;
354
+
355
+ $this->demcrypt = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', $mode, '');
356
+ $this->enmcrypt = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', $mode, '');
357
+ } // else should mcrypt_generic_deinit be called?
358
+
359
+ mcrypt_generic_init($this->demcrypt, $this->key, $this->iv);
360
+ mcrypt_generic_init($this->enmcrypt, $this->key, $this->iv);
361
+
362
+ $this->changed = false;
363
+ }
364
+
365
+ /**
366
+ * Encrypts a block
367
+ *
368
+ * Optimized over Crypt_Rijndael's implementation by means of loop unrolling.
369
+ *
370
+ * @see Crypt_Rijndael::_encryptBlock()
371
+ * @access private
372
+ * @param String $in
373
+ * @return String
374
+ */
375
+ function _encryptBlock($in)
376
+ {
377
+ $state = unpack('N*word', $in);
378
+
379
+ $Nr = $this->Nr;
380
+ $w = $this->w;
381
+ $t0 = $this->t0;
382
+ $t1 = $this->t1;
383
+ $t2 = $this->t2;
384
+ $t3 = $this->t3;
385
+
386
+ // addRoundKey and reindex $state
387
+ $state = array(
388
+ $state['word1'] ^ $w[0][0],
389
+ $state['word2'] ^ $w[0][1],
390
+ $state['word3'] ^ $w[0][2],
391
+ $state['word4'] ^ $w[0][3]
392
+ );
393
+
394
+ // shiftRows + subWord + mixColumns + addRoundKey
395
+ // we could loop unroll this and use if statements to do more rounds as necessary, but, in my tests, that yields
396
+ // only a marginal improvement. since that also, imho, hinders the readability of the code, i've opted not to do it.
397
+ for ($round = 1; $round < $this->Nr; $round++) {
398
+ $state = array(
399
+ $t0[$state[0] & 0xFF000000] ^ $t1[$state[1] & 0x00FF0000] ^ $t2[$state[2] & 0x0000FF00] ^ $t3[$state[3] & 0x000000FF] ^ $w[$round][0],
400
+ $t0[$state[1] & 0xFF000000] ^ $t1[$state[2] & 0x00FF0000] ^ $t2[$state[3] & 0x0000FF00] ^ $t3[$state[0] & 0x000000FF] ^ $w[$round][1],
401
+ $t0[$state[2] & 0xFF000000] ^ $t1[$state[3] & 0x00FF0000] ^ $t2[$state[0] & 0x0000FF00] ^ $t3[$state[1] & 0x000000FF] ^ $w[$round][2],
402
+ $t0[$state[3] & 0xFF000000] ^ $t1[$state[0] & 0x00FF0000] ^ $t2[$state[1] & 0x0000FF00] ^ $t3[$state[2] & 0x000000FF] ^ $w[$round][3]
403
+ );
404
+
405
+ }
406
+
407
+ // subWord
408
+ $state = array(
409
+ $this->_subWord($state[0]),
410
+ $this->_subWord($state[1]),
411
+ $this->_subWord($state[2]),
412
+ $this->_subWord($state[3])
413
+ );
414
+
415
+ // shiftRows + addRoundKey
416
+ $state = array(
417
+ ($state[0] & 0xFF000000) ^ ($state[1] & 0x00FF0000) ^ ($state[2] & 0x0000FF00) ^ ($state[3] & 0x000000FF) ^ $this->w[$this->Nr][0],
418
+ ($state[1] & 0xFF000000) ^ ($state[2] & 0x00FF0000) ^ ($state[3] & 0x0000FF00) ^ ($state[0] & 0x000000FF) ^ $this->w[$this->Nr][1],
419
+ ($state[2] & 0xFF000000) ^ ($state[3] & 0x00FF0000) ^ ($state[0] & 0x0000FF00) ^ ($state[1] & 0x000000FF) ^ $this->w[$this->Nr][2],
420
+ ($state[3] & 0xFF000000) ^ ($state[0] & 0x00FF0000) ^ ($state[1] & 0x0000FF00) ^ ($state[2] & 0x000000FF) ^ $this->w[$this->Nr][3]
421
+ );
422
+
423
+ return pack('N*', $state[0], $state[1], $state[2], $state[3]);
424
+ }
425
+
426
+ /**
427
+ * Decrypts a block
428
+ *
429
+ * Optimized over Crypt_Rijndael's implementation by means of loop unrolling.
430
+ *
431
+ * @see Crypt_Rijndael::_decryptBlock()
432
+ * @access private
433
+ * @param String $in
434
+ * @return String
435
+ */
436
+ function _decryptBlock($in)
437
+ {
438
+ $state = unpack('N*word', $in);
439
+
440
+ $Nr = $this->Nr;
441
+ $dw = $this->dw;
442
+ $dt0 = $this->dt0;
443
+ $dt1 = $this->dt1;
444
+ $dt2 = $this->dt2;
445
+ $dt3 = $this->dt3;
446
+
447
+ // addRoundKey and reindex $state
448
+ $state = array(
449
+ $state['word1'] ^ $dw[$this->Nr][0],
450
+ $state['word2'] ^ $dw[$this->Nr][1],
451
+ $state['word3'] ^ $dw[$this->Nr][2],
452
+ $state['word4'] ^ $dw[$this->Nr][3]
453
+ );
454
+
455
+
456
+ // invShiftRows + invSubBytes + invMixColumns + addRoundKey
457
+ for ($round = $this->Nr - 1; $round > 0; $round--) {
458
+ $state = array(
459
+ $dt0[$state[0] & 0xFF000000] ^ $dt1[$state[3] & 0x00FF0000] ^ $dt2[$state[2] & 0x0000FF00] ^ $dt3[$state[1] & 0x000000FF] ^ $dw[$round][0],
460
+ $dt0[$state[1] & 0xFF000000] ^ $dt1[$state[0] & 0x00FF0000] ^ $dt2[$state[3] & 0x0000FF00] ^ $dt3[$state[2] & 0x000000FF] ^ $dw[$round][1],
461
+ $dt0[$state[2] & 0xFF000000] ^ $dt1[$state[1] & 0x00FF0000] ^ $dt2[$state[0] & 0x0000FF00] ^ $dt3[$state[3] & 0x000000FF] ^ $dw[$round][2],
462
+ $dt0[$state[3] & 0xFF000000] ^ $dt1[$state[2] & 0x00FF0000] ^ $dt2[$state[1] & 0x0000FF00] ^ $dt3[$state[0] & 0x000000FF] ^ $dw[$round][3]
463
+ );
464
+ }
465
+
466
+ // invShiftRows + invSubWord + addRoundKey
467
+ $state = array(
468
+ $this->_invSubWord(($state[0] & 0xFF000000) ^ ($state[3] & 0x00FF0000) ^ ($state[2] & 0x0000FF00) ^ ($state[1] & 0x000000FF)) ^ $dw[0][0],
469
+ $this->_invSubWord(($state[1] & 0xFF000000) ^ ($state[0] & 0x00FF0000) ^ ($state[3] & 0x0000FF00) ^ ($state[2] & 0x000000FF)) ^ $dw[0][1],
470
+ $this->_invSubWord(($state[2] & 0xFF000000) ^ ($state[1] & 0x00FF0000) ^ ($state[0] & 0x0000FF00) ^ ($state[3] & 0x000000FF)) ^ $dw[0][2],
471
+ $this->_invSubWord(($state[3] & 0xFF000000) ^ ($state[2] & 0x00FF0000) ^ ($state[1] & 0x0000FF00) ^ ($state[0] & 0x000000FF)) ^ $dw[0][3]
472
+ );
473
+
474
+ return pack('N*', $state[0], $state[1], $state[2], $state[3]);
475
+ }
476
+ }
477
+
478
+ // vim: ts=4:sw=4:et:
479
+ // vim6: fdl=1:
app/code/local/Wisepricer/Syncer/lib/phpseclib/Crypt/DES.php ADDED
@@ -0,0 +1,945 @@
1
+ <?php
2
+ /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
+
4
+ /**
5
+ * Pure-PHP implementation of DES.
6
+ *
7
+ * Uses mcrypt, if available, and an internal implementation, otherwise.
8
+ *
9
+ * PHP versions 4 and 5
10
+ *
11
+ * Useful resources are as follows:
12
+ *
13
+ * - {@link http://en.wikipedia.org/wiki/DES_supplementary_material Wikipedia: DES supplementary material}
14
+ * - {@link http://www.itl.nist.gov/fipspubs/fip46-2.htm FIPS 46-2 - (DES), Data Encryption Standard}
15
+ * - {@link http://www.cs.eku.edu/faculty/styer/460/Encrypt/JS-DES.html JavaScript DES Example}
16
+ *
17
+ * Here's a short example of how to use this library:
18
+ * <code>
19
+ * <?php
20
+ * include('Crypt/DES.php');
21
+ *
22
+ * $des = new Crypt_DES();
23
+ *
24
+ * $des->setKey('abcdefgh');
25
+ *
26
+ * $size = 10 * 1024;
27
+ * $plaintext = '';
28
+ * for ($i = 0; $i < $size; $i++) {
29
+ * $plaintext.= 'a';
30
+ * }
31
+ *
32
+ * echo $des->decrypt($des->encrypt($plaintext));
33
+ * ?>
34
+ * </code>
35
+ *
36
+ * LICENSE: This library is free software; you can redistribute it and/or
37
+ * modify it under the terms of the GNU Lesser General Public
38
+ * License as published by the Free Software Foundation; either
39
+ * version 2.1 of the License, or (at your option) any later version.
40
+ *
41
+ * This library is distributed in the hope that it will be useful,
42
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
43
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
44
+ * Lesser General Public License for more details.
45
+ *
46
+ * You should have received a copy of the GNU Lesser General Public
47