Tutorial Extension  1.0.0
SellerDeck Extensions - Tutorial Extension
ExtensionHelper.php
1 <?php
2 
3 /**
4  * ExtensionHelper.php - Contains various global helper functions
5  *
6  * @package SellerDeck Extensions
7  *
8  * @author Péter Erdődi
9  * @copyright © SellerDeck Ltd 2015. All rights reserved.
10  */
11 
12 /**
13  * Converts float or double to SellerDeck Number
14  *
15  * @param int|float|double $dNumber a number
16  * @return int SellerDeck Number
17  */
18 function float2sdnum($dNumber)
19  {
20  return (int) round(((double) $dNumber) * 100.00);
21  }
22 
23 /**
24  * Converts SellerDeck Number to double
25  *
26  * @param int $nSellerDeckNumber A SellerDeck Number
27  * @return double Float number
28  */
29 function sdnum2float($nSellerDeckNumber)
30  {
31  return (double) ((double) $nSellerDeckNumber) / 100.00;
32  }
33 
34 /**
35  * Converts any date to SellerDeck Number
36  *
37  * @param int|float|double $dNumber a number
38  * @return int SellerDeck Number
39  */
40 function SellerDeckDate($sDate = null)
41  {
42 
43  if ($sDate == null)
44  {
45  return date(SELLERDECK_DATE_FORMAT);
46  }
47 // echo "$sDate\n";ZZZ
48  $sDate = preg_replace('/([0-9]{4}-[0-9]{2}-[0-9]{2}).([0-9]{2}:[0-9]{2}:[0-9]{2}).*/', '$1 $2', $sDate);
49 // echo "$sDate\n";
50  return date(SELLERDECK_DATE_FORMAT, strtotime($sDate));
51  }
52 
53 /**
54  * Gets a column value from the Query Result.
55  *
56  * @param PDOStatement $oQueryResult Query Result.
57  * @param mixed $key The dot-notated key or array of keys
58  * @param string $default The default value
59  * @return mixed
60  */
61 function select_get($oQueryResult, $key, $default = null)
62  {
63  if (!is_object($oQueryResult))
64  {
65  return $default;
66  }
67  return arr_get($oQueryResult->fetch(\PDO::FETCH_ASSOC), $key, $default);
68  }
69 
70 /**
71  * Gets WSDL file from URL
72  *
73  * @param string $sWsdlUrl WSDL URL
74  * @return object Wsdl File
75  */
76 function get_wsdl($sWsdlUrl = WSDL_URL)
77  {
78 
79  $oConnection = curl_init();
80  curl_setopt_array($oConnection, array(
81  CURLOPT_URL => $sWsdlUrl,
82  CURLOPT_BINARYTRANSFER => 1,
83  CURLOPT_RETURNTRANSFER => 1,
84  CURLOPT_TIMEOUT => 10,
85  ));
86  $sResponsemXml = curl_exec($oConnection);
87  if (empty($sResponsemXml))
88  {
89  throw new \SDExtension\SDException\SDException("Can't get WSDL.");
90  }
91  return 'data://text/plain;base64,' . base64_encode($sResponsemXml);
92 // return $responseXml;
93  }
94 
95 /**
96  * Creates a new instance of SDApiSoapJobs
97  *
98  * @return CSDApiSoapJobs New instance of SDApiSoapJobs
99  */
100 function CreateSDApiSoapJobs()
101  {
102  $sApiKey = \SDExtension\CConfig::get('sdd-configuration.notificationsapi.apikey', '?');
103  $sWsdlURL = \SDExtension\CConfig::get('sdd-configuration.notificationsapi.apiwsdl', 'http://localhost:54385/ActinicNotificationService?wsdl');
104  $oFileData = get_wsdl($sWsdlURL);
105  $oSyncService = new \SoapClient($oFileData, array('exceptions' => false));
106  return new \SDExtension\CSDApiSoapJobs($oSyncService, $sApiKey);
107  }
108 
109 /**
110  * Saves configuration
111  *
112  * @param array $aSdeConfigData Configuration Data
113  * @return void
114  */
115 function save_config($aSdeConfigData = [], $bMergeWithOriginal = true, $sSdeConfigFile = EXTENSION_CONFIG_FILE)
116  {
117  if ($bMergeWithOriginal && file_exists($sSdeConfigFile))
118  {
119  $aOriginalSdeConfigData = include $sSdeConfigFile;
120  $aSdeConfigData = array_merge($aOriginalSdeConfigData, $aSdeConfigData);
121  }
122  $sConfigPHPCode = sprintf(CONFIG_PHP_HEADER, basename($sSdeConfigFile)) .
123  var_export($aSdeConfigData, true) .
124  CONFIG_PHP_FOOTER;
125  file_put_contents($sSdeConfigFile, $sConfigPHPCode);
126  }
127 
128 /**
129  * Gets Table Class Folder by DB Type
130  *
131  * @return string Table Class Folder by DB Type
132  */
133 function GetTableClassFolderByDBType()
134  {
135  $sDbType = \SDExtension\CConfig::get('sdd-configuration.database.dbtype', DB_TYPE_ACCESS);
136  $sTableClassFolder = "access";
137  if (DB_TYPE_SQLSERVER == $sDbType)
138  {
139  $sTableClassFolder = "sqlserver";
140  }
141  return $sTableClassFolder;
142  }
143 
144 /**
145  * SellerDeck Encryption
146  *
147  * @param string Data to encrypt
148  * @return string Encrypted data
149  */
150 function sd_encrypt($sData)
151  {
152  return encrypt_decrypt('encrypt', $sData);
153  }
154 
155 /**
156  * SellerDeck Decryption
157  *
158  * @param string Data to decrypt
159  * @return string Decrypted data
160  */
161 function sd_decrypt($sData)
162  {
163  return encrypt_decrypt('decrypt', $sData);
164  }
165 
166 /**
167  * Get Setting
168  *
169  * @param string $sSettingKey Setting key
170  * @param mixed $vDefault Default setting value
171  * @return mixed Setting value
172  */
173 function get_setting($sSettingKey, $vDefault = null)
174  {
175  $aSettings = parse_ini_file(SETTINGS_INI);
176  return arr_get($aSettings, $sSettingKey, $vDefault);
177  }
178 
179 /**
180  * Set Setting
181  *
182  * @param string $sSettingKey Setting key
183  * @param mixed $vValue Setting value
184  * @return void
185  */
186 function set_setting($sSettingKey, $vValue)
187  {
188  $aSettings = parse_ini_file(SETTINGS_INI);
189  $aSettings[$sSettingKey] = $vValue;
190  write_ini_file(['extension' => $aSettings], SETTINGS_INI, true);
191  }
192 
193 /**
194  * Is PDO?
195  *
196  * @return bool PDO
197  */
198 function is_PDO()
199  {
200 //
201 // We always use PDO
202 //
203 // return in_array(strtolower(get_setting('pdo', '1')), ['1', 'true', 'on', 'yes']);
204 //
205  return true;
206  }
207 
208 /**
209  * encrypt_decrypt - Encrypts or decrypts data
210  *
211  * @param string $sAction Action (encrypt/decrypt)
212  * @param string $sData Data
213  * @return string Encrypted or decrypted data
214  */
215 function encrypt_decrypt($sAction, $sData)
216  {
217  $sOutput = "";
218  $sEncryptMethod = "AES-256-CBC";
219  $sSecretKey = 'This is my secret key';
220  $sSecretIV = 'This is my secret iv';
221 //
222 // hash
223 //
224  $sKey = hash('sha256', $sSecretKey);
225 //
226 // iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
227 //
228  $sIV = substr(hash('sha256', $sSecretIV), 0, 16);
229  if ($sAction == 'encrypt')
230  {
231  $sOutput = base64_encode(openssl_encrypt($sData, $sEncryptMethod, $sKey, 0, $sIV));
232  }
233  else if ($sAction == 'decrypt')
234  {
235  $sOutput = openssl_decrypt(base64_decode($sData), $sEncryptMethod, $sKey, 0, $sIV);
236  }
237  return $sOutput;
238  }
239 
240 /**
241  * valid_date - Check date validity
242  *
243  * @param string $sDate Date
244  * @return bool Valid
245  */
246 function valid_date($sDate)
247  {
248  list($nY, $sM, $sD) = explode('-', $sDate);
249  return checkdate($sM, $sD, $nY);
250  }
251 
252 /**
253  * valid_date_format - Check date format validity
254  *
255  * @param string $sDate Date
256  * @return bool Valid format
257  */
258 function valid_date_format($sDate)
259  {
260  if (preg_match("/^([0-9]{4})-(0?[1-9]|1[0-2])-(0?[1-9]|[1-2][0-9]|3[0-1])$/", $sDate))
261  {
262  return true;
263  }
264  return false;
265  }
266 
267 /**
268  * between - Check if values are in an interval
269  *
270  * @param mixed $vInput Input
271  * @param numeric $vParam1 Parameter 1
272  * @param numeric $vParam2 Parameter 2
273  * @return bool Between params
274  */
275 function between($vInput, $vParam1, $vParam2)
276  {
277  if (($vInput >= $vParam1) && ($vInput <= $vParam2))
278  {
279  return true;
280  }
281  return false;
282  }
283 
284 /**
285  * CreateLock - Creates lock file
286  *
287  * @param string $sDownloadLockFile Download Lock File
288  * @return bool Success
289  */
290 function CreateLock($sDownloadLockFile = null)
291  {
292  if ($sDownloadLockFile == null)
293  {
294  $sDownloadLockFile = get_setting('extension_program_data', "./") . LOCK_FILE;
295  }
296  if (!file_exists($sDownloadLockFile) || ((time() - filemtime($sDownloadLockFile)) > LOCK_FILE_MAX_AGE))
297  {
298  file_put_contents($sDownloadLockFile, "");
299  return true;
300  }
301  return false;
302  }
303 
304 /**
305  * RemoveLock - Removes lock file
306  *
307  * @param string $sDownloadLockFile Download Lock File
308  * @return bool Success
309  */
310 function RemoveLock($sDownloadLockFile = null)
311  {
312  if ($sDownloadLockFile == null)
313  {
314  $sDownloadLockFile = get_setting('extension_program_data', "./") . LOCK_FILE;
315  }
316  if (file_exists($sDownloadLockFile))
317  {
318  unlink($sDownloadLockFile);
319  return true;
320  }
321  return false;
322  }
static get($sName=null, $vDefault=[])
Definition: CConfig.php:47