PageRenderTime 70ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/application/helpers/common_helper.php

https://github.com/her0satr/shop
PHP | 928 lines | 777 code | 125 blank | 26 comment | 235 complexity | 0e37bccdfd4bbed88f8599659205015f MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, LGPL-2.1
  1. <?php
  2. if (! function_exists('StripArray')) {
  3. function StripArray($Array, $FieldName = array()) {
  4. $ArrayResult = array();
  5. foreach($Array as $Key => $Element) {
  6. if (in_array($Key, $FieldName) && in_array($Element, array('0', '0000-00-00', '0000-00-00 00:00:00'))) {
  7. $ArrayResult[$Key] = null;
  8. } else {
  9. $ArrayResult[$Key] = stripslashes($Element);
  10. }
  11. }
  12. return $ArrayResult;
  13. }
  14. }
  15. if (! function_exists('EscapeString')) {
  16. function EscapeString($Array) {
  17. $ArrayResult = array();
  18. foreach($Array as $Key => $Element) {
  19. $ArrayResult[$Key] = mysql_real_escape_string($Element);
  20. }
  21. return $ArrayResult;
  22. }
  23. }
  24. if (! function_exists('GetOption')) {
  25. function GetOption($OptAll, $ArrayOption, $Selected) {
  26. $temp = ($Selected == 0) ? 'selected' : '';
  27. $Content = ($OptAll) ? '<option value="0" '.$temp.'>All<option>' : '';
  28. foreach ($ArrayOption as $Value => $Title) {
  29. $temp = ($Selected == $Value) ? 'selected' : '';
  30. $Content .= '<option value="'.$Value.'" '.$temp.'>'.$Title.'</option>';
  31. }
  32. return $Content;
  33. }
  34. }
  35. if (! function_exists('ShowOption')) {
  36. function ShowOption($Param) {
  37. $Param['OptAll'] = (isset($Param['OptAll'])) ? $Param['OptAll'] : false;
  38. $Param['ArrayID'] = (isset($Param['ArrayID'])) ? $Param['ArrayID'] : 'id';
  39. $Param['WithEmptySelect'] = (isset($Param['WithEmptySelect'])) ? $Param['WithEmptySelect'] : 1;
  40. $Param['ArrayTitle'] = (isset($Param['ArrayTitle'])) ? $Param['ArrayTitle'] : 'title';
  41. $Param['Selected'] = (isset($Param['Selected'])) ? $Param['Selected'] : '';
  42. if ($Param['WithEmptySelect'] == 1) {
  43. $Content = '<option value="">-</option>';
  44. } else {
  45. $Content = '';
  46. }
  47. $Selected = '';
  48. if ($Param['OptAll']) {
  49. $Selected = ($Param['Selected'] == '0') ? 'selected' : '';
  50. $Content .= '<option value="0" ' . $Selected . '>Semua</option>';
  51. }
  52. foreach ($Param['Array'] as $Array) {
  53. $Selected = ($Param['Selected'] == $Array[$Param['ArrayID']]) ? 'selected' : '';
  54. $Content .= '<option value="'.$Array[$Param['ArrayID']].'" '.$Selected.'>'.$Array[$Param['ArrayTitle']].'</option>';
  55. }
  56. return $Content;
  57. }
  58. }
  59. if (! function_exists('ArrayToJSON')) {
  60. function ArrayToJSON($Array) {
  61. $Result = '';
  62. foreach ($Array as $Key => $Element) {
  63. $Element = mysql_real_escape_string($Element);
  64. $Result .= (empty($Result)) ? "'$Key': '$Element'" : ",'$Key':'$Element'";
  65. }
  66. $Result = '{' . $Result . '}';
  67. return $Result;
  68. }
  69. }
  70. if (! function_exists('ConvertToUnixTime')) {
  71. function ConvertToUnixTime($String) {
  72. preg_match('/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/i', $String, $Match);
  73. $UnixTime = mktime ($Match[4], $Match[5], $Match[6], $Match[2], $Match[3], $Match[1]);
  74. $UnixTime = 'new Date('.$UnixTime.')';
  75. return $UnixTime;
  76. }
  77. }
  78. if (! function_exists('ConvertDateToString')) {
  79. function ConvertDateToString($String) {
  80. preg_match('/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/i', $String, $Match);
  81. return date("d F Y", mktime (0, 0, 0, $Match[2], $Match[3], $Match[1]));
  82. }
  83. }
  84. if (! function_exists('ConvertDateToQuery')) {
  85. function ConvertDateToQuery($String) {
  86. preg_match('/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/i', $String, $Match);
  87. if (isset($Match[0]) && !empty($Match[0])) {
  88. $Result = $Match[0];
  89. return $Result;
  90. }
  91. $Array = explode('/', $String);
  92. $Result = $Array[2] . '-' . $Array[0] . '-' . $Array[1];
  93. return $Result;
  94. }
  95. }
  96. if (! function_exists('MoneyFormat')) {
  97. function MoneyFormat($Value) {
  98. return number_format($Value, 2, ',', '.');
  99. }
  100. }
  101. if (! function_exists('Upload')) {
  102. function Upload($InputName, $PathDir = 'User', $Param = array()) {
  103. $Param['AllowedExtention'] = (isset($Param['AllowedExtention'])) ? $Param['AllowedExtention'] : array('jpg', 'jpeg', 'gif', 'png', 'bmp', 'xls', 'csv');
  104. $ArrayResult = array('Result' => '0', 'FileDirName' => '');
  105. if (isset($_FILES[$InputName]) && is_array($_FILES[$InputName]) && is_array($_FILES[$InputName]['name'])) {
  106. $FileCount = count($_FILES[$InputName]['name']);
  107. for ($i = 0; $i < $FileCount; $i++) {
  108. if ($_FILES[$InputName]['error'][$i] == '0') {
  109. $Extention = F_GetExtention($_FILES[$InputName]['name'][$i]);
  110. $FileName = date("Ymd_His").'_'.rand(1000,9999).'.'.$Extention;
  111. @mkdir(IMGS_DIR.'/'.date("Y"));
  112. @mkdir(IMGS_DIR.'/'.date("Y").'/'.date("m"));
  113. @mkdir(IMGS_DIR.'/'.date("Y").'/'.date("m").'/'.date("d"));
  114. $FileLocation = IMGS_DIR.'/'.date("Y").'/'.date("m").'/'.date("d").'/'.basename($FileName);
  115. $FileRequest = date("Y").'/'.date("m").'/'.date("d").'/'.basename($FileName);
  116. if (move_uploaded_file($_FILES[$InputName]['tmp_name'][$i], $FileLocation)) {
  117. $ParamImage = array(
  118. 'FileSource' => $FileLocation,
  119. 'Width' => 456,
  120. 'Height' => 320,
  121. );
  122. F_Resize($ParamImage);
  123. $ArrayResult['Result'] = '1';
  124. $ArrayResult['ArrayImage'][] = $FileRequest;
  125. }
  126. }
  127. }
  128. }
  129. else if (isset($_FILES[$InputName]) && !empty($_FILES[$InputName]) && !empty($_FILES[$InputName]['name'])) {
  130. $Extention = GetExtention($_FILES[$InputName]['name']);
  131. $ArrayResult['Message'] = 'There was an error uploading the file, please try again!';
  132. $ArrayResult['FileDirName'] = '';
  133. if (! in_array($Extention, $Param['AllowedExtention'])) {
  134. $ArrayResult['Message'] = 'Hanya file bertipe jpg, jpeg, gif, png, bmp dan xls yang dapat di upload.';
  135. } else if ($_FILES[$InputName]['error'] == '0') {
  136. $DirYear = date("Y");
  137. $DirMonth = date("m");
  138. $DirDay = date("d");
  139. @mkdir($PathDir.'/'.$DirYear);
  140. @mkdir($PathDir.'/'.$DirYear.'/'.$DirMonth);
  141. @mkdir($PathDir.'/'.$DirYear.'/'.$DirMonth.'/'.$DirDay);
  142. $FileName = date("Ymd_His").'_'.rand(1000,9999).'.'.$Extention;
  143. $FileDirectory = $PathDir;
  144. $FileLocation = $FileDirectory.'/'.$DirYear.'/'.$DirMonth.'/'.$DirDay.'/'.basename($FileName);
  145. if (move_uploaded_file($_FILES[$InputName]['tmp_name'], $FileLocation)) {
  146. $ArrayResult['Result'] = '1';
  147. $ArrayResult['Message'] = 'Upload file berhasil.';
  148. $ArrayResult['FileDirName'] = $DirYear.'/'.$DirMonth.'/'.$DirDay.'/'.$FileName;
  149. }
  150. }
  151. }
  152. return $ArrayResult;
  153. }
  154. }
  155. if (! function_exists('UploadFtp')) {
  156. /*
  157. $ParamUpload = array(
  158. 'UploadFtp' => 1,
  159. 'WithCreateDir' => 1,
  160. 'Name' => date('YmdHis') . '_' . rand(1000,9999),
  161. 'Extention' => GetExtention($_FILES['document']['name']),
  162. 'UploadPathFtp' => SFTP_PATH,
  163. 'UploadPathLocal' => $this->config->item('base_path') . '/static/images/_temp'
  164. );
  165. $FileUpload = UploadFtp($ParamUpload, 'document');
  166. /* */
  167. function UploadFtp($File, $Name = 'Image') {
  168. $File['WithCreateDir'] = (isset($File['WithCreateDir'])) ? $File['WithCreateDir'] : 0;
  169. $File['UploadFtp'] = (isset($File['UploadFtp'])) ? $File['UploadFtp'] : 0;
  170. $ArrayResult = array('Message' => '', 'Status' => 1);
  171. if (isset($_FILES[$Name]) && !empty($_FILES[$Name])) {
  172. $FileName = basename($File['Name'] . '.' . $File['Extention']);
  173. $PathFileName = $File['UploadPathLocal'] . '/'. $FileName;
  174. @unlink($PathFileName);
  175. if (! move_uploaded_file($_FILES[$Name]['tmp_name'], $PathFileName)) {
  176. $ArrayResult['Status'] = 0;
  177. $ArrayResult['Message'] = '<span class="red">There was an error uploading the file, please try again!</span>';
  178. } else {
  179. ImageResize($PathFileName, $PathFileName, PHOTO_WIDTH, PHOTO_HEIGHT, 1);
  180. }
  181. if ($ArrayResult['Status'] == 1 && $File['UploadFtp'] == 1) {
  182. $FtpConnection = ftp_connect(SFTP_HOST);
  183. $FtpResource = ftp_login($FtpConnection, SFTP_USER, SFTP_PASS);
  184. ftp_pasv($FtpConnection, (bool)true);
  185. // Create Date Directory
  186. $DateDir = '';
  187. if ($File['WithCreateDir'] == 1) {
  188. $DirFile = array(date("Y"), date("m"), date("d"));
  189. foreach ($DirFile as $Directory) {
  190. $DateDir .= $Directory . '/';
  191. $File['UploadPathFtp'] .= '/' . $Directory;
  192. @ftp_mkdir($FtpConnection, $File['UploadPathFtp']);
  193. }
  194. }
  195. // Delete File with same name
  196. $FileNameDelete = $File['UploadPathFtp'] . '/' . basename($File['Name']);
  197. foreach (array('jpg', 'png', 'jpeg', 'pdf') as $Value) {
  198. @ftp_delete($FtpConnection, $FileNameDelete . '.' . $Value);
  199. }
  200. ftp_chdir($FtpConnection, $File['UploadPathFtp']);
  201. $FtpUpload = ftp_put($FtpConnection, $FileName, $PathFileName, FTP_BINARY);
  202. $ArrayResult['FileName'] = $DateDir . $File['Name'] . '.' . $File['Extention'];
  203. }
  204. }
  205. return $ArrayResult;
  206. }
  207. }
  208. if (! function_exists('GetExtention')) {
  209. function GetExtention($FileName) {
  210. $FileName = strtolower(trim($FileName));
  211. if (empty($FileName)) {
  212. return '';
  213. }
  214. $ArrayString = explode('.', $FileName);
  215. return $ArrayString[count($ArrayString) - 1];
  216. }
  217. }
  218. if (! function_exists('Write')) {
  219. function Write($FileLocation, $FileContent) {
  220. $Handle = @fopen($FileLocation, 'wb+');
  221. if ($Handle) {
  222. fputs($Handle, $FileContent);
  223. fclose($Handle);
  224. }
  225. }
  226. }
  227. if (! function_exists('GetStringFilter')) {
  228. // $Param = array('filter' => '[{"type":"numeric","comparison":"eq","value":"' . $company_id . '","field":"company_id"},{"type":"numeric","comparison":"eq","value":1,"field":"menu_company_active"}]');
  229. /*
  230. // overwrite field name
  231. $param['field_replace']['id'] = 'Nota.id';
  232. $param['field_replace']['nota_currency_total'] = 'Nota.nota_total';
  233. $param['field_replace']['status_nota_name'] = 'StatusNota.name';
  234. /* */
  235. function GetStringFilter($Param, $ReplaceField = array()) {
  236. $StringFilter = '';
  237. if (isset($Param['sSearch'])) {
  238. $field_replace = (isset($Param['field_replace'])) ? $Param['field_replace'] : array();
  239. $StringFilter = "";
  240. if ( $Param['sSearch'] != "" ) {
  241. $StringFilter = "AND (";
  242. $aWords = preg_split('/\s+/', $Param['sSearch']);
  243. for ($j = 0; $j < count($aWords); $j++) {
  244. if ( $aWords[$j] != "" ) {
  245. $StringFilter .= "(";
  246. for ($i = 0; $i < count($ReplaceField); $i++) {
  247. $field_name = (isset($field_replace[$ReplaceField[$i]])) ? $field_replace[$ReplaceField[$i]] : $ReplaceField[$i];
  248. if (empty($field_name)) {
  249. continue;
  250. }
  251. $StringFilter .= $field_name." LIKE '%".mysql_real_escape_string( $aWords[$j] )."%' OR ";
  252. }
  253. $StringFilter = substr_replace( $StringFilter, "", -3 );
  254. $StringFilter .= ") AND ";
  255. }
  256. }
  257. $StringFilter = substr_replace( $StringFilter, "", -5 );
  258. $StringFilter .= ")";
  259. }
  260. /* Individual column filtering */
  261. $sColumnWhere = "";
  262. for ( $i=0 ; $i<count($ReplaceField) ; $i++ ) {
  263. if ( isset($Param['sSearch_'.$i]) && !empty($Param['sSearch_'.$i]) ) {
  264. $aWords = preg_split('/\s+/', $Param['sSearch_'.$i]);
  265. $sColumnWhere .= "(";
  266. for ( $j=0 ; $j<count($aWords) ; $j++ )
  267. {
  268. if ( $aWords[$j] != "" )
  269. {
  270. $sColumnWhere .= $ReplaceField[$i]." LIKE '%".mysql_real_escape_string( $aWords[$j] )."%' OR ";
  271. }
  272. }
  273. $sColumnWhere = substr_replace( $sColumnWhere, "", -3 );
  274. $sColumnWhere .= ") AND ";
  275. }
  276. }
  277. if ( $sColumnWhere != "" ) {
  278. $sColumnWhere = substr_replace( $sColumnWhere, "", -5 );
  279. $StringFilter .= " AND ".$sColumnWhere;
  280. }
  281. }
  282. else if (isset($Param['filter']) && !empty($Param['filter'])) {
  283. $Filter = json_decode($Param['filter']);
  284. foreach ($Filter as $Array) {
  285. $Field = (isset($ReplaceField[$Array->field])) ? $ReplaceField[$Array->field] : $Array->field;
  286. if (isset($Array->field) && isset($Array->value)) {
  287. if ($Array->type == 'numeric') {
  288. if ($Array->comparison == 'eq') {
  289. $StringFilter .= "AND " . $Field." = '".$Array->value."' ";
  290. } else if ($Array->comparison == 'lt') {
  291. $StringFilter .= "AND " . $Field." < '".$Array->value."' ";
  292. } else if ($Array->comparison == 'gt') {
  293. $StringFilter .= "AND " . $Field." > '".$Array->value."' ";
  294. } else if ($Array->comparison == 'not') {
  295. $StringFilter .= "AND " . $Field." != '".$Array->value."' ";
  296. } else if ($Array->comparison == 'eq_can_empty' && !empty($Array->value)) {
  297. $StringFilter .= "AND " . $Field." = '".$Array->value."' ";
  298. } else if ($Array->comparison == 'in') {
  299. $StringFilter .= "AND " . $Field." IN (".$Array->value.") ";
  300. }
  301. } else if ($Array->type == 'date') {
  302. if ($Array->comparison == 'eq') {
  303. $StringFilter .= "AND " . $Field." = '".ConvertDateToQuery($Array->value)."' ";
  304. } else if ($Array->comparison == 'lt') {
  305. $StringFilter .= "AND " . $Field." <= '".ConvertDateToQuery($Array->value)."' ";
  306. } else if ($Array->comparison == 'gt') {
  307. $StringFilter .= "AND " . $Field." >= '".ConvertDateToQuery($Array->value)."' ";
  308. }
  309. } else if ($Array->type == 'list') {
  310. $Array->field = $Field;
  311. $StringFilter .= GetStringFromList($Array);
  312. } else if ($Array->type == 'custom') {
  313. $StringFilter .= "AND " . $Array->field . ' ';
  314. } else {
  315. $StringFilter .= "AND " . $Field." LIKE '".$Array->value."%' ";
  316. }
  317. }
  318. }
  319. }
  320. return $StringFilter;
  321. }
  322. }
  323. if (! function_exists('GetStringFromList')) {
  324. function GetStringFromList($Param) {
  325. $ArrayFieldYesNo = array('supplier_active', 'agent_active', 'car_active', 'customer_active', 'driver_active', 'guide_active');
  326. $ArrayEmptyOrExist = array('driver_photo');
  327. $StringResult = '';
  328. if (in_array($Param->field, $ArrayFieldYesNo)) {
  329. foreach ($Param->value as $Value) {
  330. if ($Value == 'Yes') {
  331. $StringResult .= (empty($StringResult)) ? "'1'" : ", '1'";
  332. } else if ($Value == 'No') {
  333. $StringResult .= (empty($StringResult)) ? "'0'" : ", '0'";
  334. }
  335. }
  336. $StringResult = (empty($StringResult)) ? '' : "AND " . $Param->field . " IN (" . $StringResult . ") ";
  337. } else if (in_array($Param->field, $ArrayEmptyOrExist)) {
  338. foreach ($Param->value as $Value) {
  339. if ($Value == 'Yes') {
  340. $StringResult .= (empty($StringResult)) ? $Param->field . " != '' " : "OR " . $Param->field . " != '' ";
  341. } else if ($Value == 'No') {
  342. $StringResult .= (empty($StringResult)) ? $Param->field . "= '' " : "OR " . $Param->field . " = '' ";
  343. }
  344. }
  345. $StringResult = (empty($StringResult)) ? '' : "AND (" . $StringResult . ") ";
  346. } else {
  347. echo 'Please create new filter spesification';
  348. exit;
  349. }
  350. return $StringResult;
  351. }
  352. }
  353. if (! function_exists('GenerateInsertQuery')) {
  354. function GenerateInsertQuery($ArrayField, $ArrayParam, $Table, $Param = array()) {
  355. $Param['AllowSymbol'] = (isset($Param['AllowSymbol'])) ? $Param['AllowSymbol'] : 0;
  356. $StringField = $StringValue = '';
  357. foreach ($ArrayField as $Column) {
  358. $StringField .= (empty($StringField)) ? '`'.$Column.'`' : ', `' . $Column.'`';
  359. $Value = (isset($ArrayParam[$Column])) ? $ArrayParam[$Column] : '';
  360. $Value = mysql_real_escape_string($Value);
  361. if ($Param['AllowSymbol'] == 0) {
  362. $Value = preg_replace('/[^\x20-\x7E|\x0A]/i', '', $Value);
  363. }
  364. $StringValue .= (empty($StringValue)) ? "'" . $Value . "'" : ", '" . $Value . "'";
  365. }
  366. $Query = "INSERT INTO `$Table` ($StringField) VALUES ($StringValue)";
  367. return $Query;
  368. }
  369. }
  370. if (! function_exists('GenerateUpdateQuery')) {
  371. function GenerateUpdateQuery($ArrayField, $ArrayParam, $Table, $Param = array()) {
  372. $Param['AllowSymbol'] = (isset($Param['AllowSymbol'])) ? $Param['AllowSymbol'] : 0;
  373. $StringQuery = '';
  374. foreach ($ArrayField as $Key => $Column) {
  375. if ($Key != 0 && isset($ArrayParam[$Column])) {
  376. $Value = $ArrayParam[$Column];
  377. if ($Param['AllowSymbol'] == 0) {
  378. $Value = preg_replace('/[^\x20-\x7E|\x0A]/i', '', $Value);
  379. }
  380. $StringQuery .= (empty($StringQuery)) ? '' : ', ';
  381. $StringQuery .= "`$Column` = '" . mysql_real_escape_string($Value) . "'";
  382. }
  383. }
  384. $Query = "UPDATE `$Table` SET $StringQuery WHERE " . $ArrayField[0] . " = '" . $ArrayParam[$ArrayField[0]] . "'";
  385. return $Query;
  386. }
  387. }
  388. if (! function_exists('GetNextAutoIncrement')) {
  389. function GetNextAutoIncrement($Table) {
  390. $NextAutoIncrement = 1;
  391. $SelectQuery = "SHOW TABLE STATUS LIKE '$Table'";
  392. $ResultQuery = mysql_query($SelectQuery) or die(mysql_error());
  393. if (false !== $Row = mysql_fetch_assoc($ResultQuery)) {
  394. $NextAutoIncrement = $Row['Auto_increment'];
  395. }
  396. return $NextAutoIncrement;
  397. }
  398. }
  399. if (! function_exists('GetStringMonth')) {
  400. function GetStringMonth($Param) {
  401. if (empty($Param['value'])) {
  402. return '';
  403. }
  404. $Param['Year'] = (isset($Param['Year'])) ? $Param['Year'] : date("Y");
  405. $StringMonth = "AND MONTH(" . $Param['field'] . ") = '" . $Param['value'] . "' AND YEAR(" . $Param['field'] . ") = '" . $Param['Year'] . "'";
  406. return $StringMonth;
  407. }
  408. }
  409. if (! function_exists('GetStringBettween')) {
  410. function GetStringBettween($Param, $Field = array()) {
  411. $StringResult = '';
  412. if (isset($Param['StartDate']) && !empty($Param['StartDate']) && isset($Param['EndDate']) && !empty($Param['EndDate'])) {
  413. foreach ($Field as $Value) {
  414. $StringResult .= (empty($StringResult)) ? '' : 'OR ';
  415. $StringResult .= "$Value between '".$Param['StartDate']."' and '".$Param['EndDate']."' ";
  416. }
  417. $StringResult = "AND (" . $StringResult . ") ";
  418. }
  419. return $StringResult;
  420. }
  421. }
  422. if (! function_exists('GetStringSorting')) {
  423. // $Param = array('sort' => '[{"property":"tanggal","direction":"DESC"}]');
  424. function GetStringSorting($param, $Field = array(), $string_default = '') {
  425. $Result = '';
  426. if (isset($param['iSortCol_0'])) {
  427. for ( $i=0 ; $i<intval( $param['iSortingCols'] ) ; $i++ ) {
  428. if ( $param[ 'bSortable_'.intval($param['iSortCol_'.$i]) ] == "true" ) {
  429. $Result .= $Field[ intval( $param['iSortCol_'.$i] )-1 ]." ".mysql_real_escape_string( $param['sSortDir_'.$i] ) .", ";
  430. }
  431. }
  432. $Result = substr_replace( $Result, "", -2 );
  433. } else if (isset($param['sort'])) {
  434. $ArrayString = json_decode($param['sort']);
  435. foreach ($ArrayString as $Array) {
  436. $FieldName = (isset($Field[$Array->property])) ? $Field[$Array->property] : $Array->property;
  437. $Query = $FieldName . ' ' . $Array->direction;
  438. $Result .= (empty($Result)) ? '' : ', ';
  439. $Result .= $Query;
  440. }
  441. } else {
  442. $Result = $string_default;
  443. }
  444. return $Result;
  445. }
  446. }
  447. if (! function_exists('json_response')) {
  448. function json_response($json, $status=200) {
  449. if ($status != 200) header('HTTP/1.1 ' . $status);
  450. header('Content-type: application/json; charset=UTF-8');
  451. echo json_encode( $json );
  452. exit;
  453. }
  454. }
  455. if (! function_exists('GetArrayFromFileUpload')) {
  456. function GetArrayFromFileUpload($FileUploadPath) {
  457. $ArrayFile = file($FileUploadPath);
  458. $ArrayRaw = array();
  459. foreach ($ArrayFile as $StringTemp) {
  460. $StringCheck = preg_replace('/\,/i', '', trim($StringTemp));
  461. if (empty($StringCheck)) {
  462. continue;
  463. }
  464. $ArrayTemp = explode(',', $StringTemp);
  465. foreach ($ArrayTemp as $Key => $Value) {
  466. $Value = preg_replace('/^\"|\"$/i', '', trim($Value));
  467. $ArrayTemp[$Key] = $Value;
  468. }
  469. $ArrayRaw[] = $ArrayTemp;
  470. }
  471. return $ArrayRaw;
  472. }
  473. }
  474. if (! function_exists('EncriptPassword')) {
  475. function EncriptPassword($Value) {
  476. return md5(sha1(SHA_SECRET . ':' . $Value));
  477. }
  478. }
  479. if (! function_exists('GetResource')) {
  480. function GetResource($Source) {
  481. $Buffer = '';
  482. $Handle = fopen($Source, "rb+");
  483. if ($Handle) {
  484. while (!feof($Handle)) {
  485. $Buffer .= fgets($Handle, 8192);
  486. }
  487. fclose($Handle);
  488. }
  489. return $Buffer;
  490. }
  491. }
  492. if (! function_exists('ImageResize')) {
  493. function ImageResize($ImageSource, $ImageOutput, $MinWidth, $MinHeight, $IsCrop = 0) {
  494. $info = @getimagesize($ImageSource);
  495. if (!empty($info)) {
  496. $Image = imagecreatefromstring(GetResource($ImageSource));
  497. $ImageWidth = imagesx($Image);
  498. $ImageHeight = imagesy($Image);
  499. // Enlarge for Small Image
  500. if ($ImageWidth < $MinWidth || $ImageHeight < $MinHeight) {
  501. $FactorWidth = $FactorHeight = 0;
  502. if ($ImageWidth < $MinWidth) {
  503. $FactorWidth = $MinWidth / $ImageWidth;
  504. }
  505. if ($ImageHeight < $MinHeight) {
  506. $FactorHeight = $MinHeight / $ImageHeight;
  507. }
  508. $FactorMultiply = ($FactorWidth > $FactorHeight) ? $FactorWidth : $FactorHeight;
  509. $ResultWidth = intval($FactorMultiply * $ImageWidth);
  510. $ResultHeight = intval($FactorMultiply * $ImageHeight);
  511. // Resize for Large Image
  512. } else {
  513. $FactorWidth = $ImageWidth / $MinWidth;
  514. $FactorHeight = $ImageHeight / $MinHeight;
  515. $FactorMultiply = ($FactorWidth < $FactorHeight) ? $FactorWidth : $FactorHeight;
  516. $ResultWidth = intval($ImageWidth / $FactorMultiply);
  517. $ResultHeight = intval($ImageHeight / $FactorMultiply);
  518. }
  519. $Result = imagecreatetruecolor($ResultWidth, $ResultHeight);
  520. imagecopyresampled($Result, $Image, 0, 0, 0, 0, $ResultWidth, $ResultHeight, $ImageWidth, $ImageHeight);
  521. imagejpeg($Result, $ImageOutput);
  522. imagedestroy($Image);
  523. imagedestroy($Result);
  524. if ($IsCrop == 1) {
  525. ImageCrop($ImageOutput, $ImageOutput, $MinWidth, $MinHeight);
  526. }
  527. }
  528. }
  529. }
  530. if (! function_exists('ImageCrop')) {
  531. function ImageCrop($source, $output, $out_x, $out_y) {
  532. $info = @getimagesize($source);
  533. if (!empty($info)){
  534. $img = imagecreatefromstring(GetResource($source));
  535. $img_x = imagesx($img);
  536. $img_y = imagesy($img);
  537. $img_top = 0;
  538. $img_left = 0;
  539. if ($img_x <= $out_x && $img_y <= $out_y){
  540. copy($source, $output);
  541. return;
  542. }
  543. $diff = round($img_y/2) - round($out_y/2);
  544. $img_top = 0;
  545. $img_y = $out_y;
  546. $out = imagecreatetruecolor($out_x, $out_y);
  547. imagecopyresampled($out, $img, 0, 0, $img_left, $img_top, $out_x, $out_y, $img_x, $img_y);
  548. imagejpeg($out, $output);
  549. imagedestroy($img);
  550. imagedestroy($out);
  551. }
  552. }
  553. }
  554. if (! function_exists('GetLengthChar')) {
  555. function GetLengthChar($String, $LengthMax, $Follower = '') {
  556. if (strlen($String) > $LengthMax) {
  557. $String = substr($String, 0, $LengthMax);
  558. $Stringpos = strrpos($String, ' ');
  559. if (false !== $Stringpos) $String = substr($String, 0, $Stringpos);
  560. if (!empty($Follower)) {
  561. $String .= $Follower;
  562. }
  563. }
  564. return $String;
  565. }
  566. }
  567. if (! function_exists('GetStringLimit')) {
  568. function GetStringLimit($Param) {
  569. $StringLimit = "0, 25";
  570. if ( isset( $Param['iDisplayStart'] ) && $Param['iDisplayLength'] != '-1' ) {
  571. $StringLimit = mysql_real_escape_string( $Param['iDisplayStart'] ).", ". mysql_real_escape_string( $Param['iDisplayLength'] );
  572. } else if (isset($Param['start']) || isset($Param['limit'])) {
  573. $PageOffset = (isset($Param['start']) && !empty($Param['start'])) ? $Param['start'] : 0;
  574. $PageLimit = (isset($Param['limit']) && !empty($Param['limit'])) ? $Param['limit'] : 25;
  575. $StringLimit = "$PageOffset, $PageLimit";
  576. }
  577. return $StringLimit;
  578. }
  579. }
  580. if (! function_exists('dt_view')) {
  581. function dt_view($row, $column, $param) {
  582. $param['is_edit'] = (isset($param['is_edit'])) ? $param['is_edit'] : 0;
  583. $param['is_delete'] = (isset($param['is_delete'])) ? $param['is_delete'] : 0;
  584. $param['is_detail'] = (isset($param['is_detail'])) ? $param['is_detail'] : 0;
  585. if ($param['is_edit'] == 1) {
  586. $temp[0] = (isset($temp[0])) ? $temp[0] : '';
  587. $temp[0] .= '<img class="cursor edit" title="Edit" src="'.base_url('static/img/button_edit.png').'"> ';
  588. $temp[0] .= '<img class="cursor delete" title="Delete" src="'.base_url('static/img/button_delete.png').'"> ';
  589. }
  590. if (isset($param['is_edit_only']) && $param['is_edit_only'] == 1) {
  591. $temp[0] = (isset($temp[0])) ? $temp[0] : '';
  592. $temp[0] .= '<img class="cursor edit" title="Edit" src="'.base_url('static/img/button_edit.png').'"> ';
  593. }
  594. if ($param['is_delete'] == 1) {
  595. $temp[0] = (isset($temp[0])) ? $temp[0] : '';
  596. $temp[0] .= '<img class="cursor delete" title="Delete" src="'.base_url('static/img/button_delete.png').'"> ';
  597. }
  598. if ($param['is_detail'] == 1) {
  599. $temp[0] = (isset($temp[0])) ? $temp[0] : '';
  600. $temp[0] .= '<img class="cursor detail" title="Detail" src="'.base_url('static/img/details_open.png').'"> ';
  601. }
  602. if (!empty($param['is_custom'])) {
  603. $temp[0] = (isset($temp[0])) ? $temp[0] : '';
  604. $temp[0] .= $param['is_custom'];
  605. }
  606. if (!empty($temp[0])) {
  607. $temp[0] .= '<span class="hide">'.json_encode($row).'</span>';
  608. }
  609. foreach ($column as $key => $value) {
  610. $temp[] = @$row[$value];
  611. }
  612. $temp['extra'] = 'hrmll';
  613. return $temp;
  614. }
  615. }
  616. if (! function_exists('get_store')) {
  617. function get_store() {
  618. $store_name = (isset($_SERVER['store_name']) && !empty($_SERVER['store_name'])) ? $_SERVER['store_name'] : '';
  619. return $store_name;
  620. }
  621. }
  622. if(! function_exists('GenerateInsertQueryByTypeData'))
  623. {
  624. function
  625. GenerateInsertQueryByTypeData($ArrayField,$ArrayParam,$Table)
  626. {
  627. $tableData = mysql_query("select COLUMN_NAME, data_type
  628. from information_schema.columns where table_schema = '".DB_NAME."' and
  629. table_name = '".$Table."'");
  630. $StringField = $StringValue = '';
  631. while($data = mysql_fetch_assoc($tableData))
  632. {
  633. foreach ($ArrayField as $Column)
  634. {
  635. if($data['COLUMN_NAME'] == $Column)
  636. {
  637. $StringField .= (empty($StringField)) ? $Column
  638. : ', ' . $Column;
  639. $Value = (isset($ArrayParam[$Column])) ?
  640. $ArrayParam[$Column] : '';
  641. $Value = mysql_real_escape_string($Value);
  642. if($data['data_type'] =='bit')
  643. {
  644. $StringValue .= (empty($StringValue)) ?
  645. $Value : ", " . $Value;
  646. }else
  647. {
  648. $StringValue .= (empty($StringValue)) ? "'"
  649. . $Value . "'" : ", '" . $Value . "'";
  650. }
  651. }
  652. }
  653. }
  654. $Query = "INSERT INTO `$Table` ($StringField) VALUES
  655. ($StringValue)";
  656. return $Query;
  657. }
  658. }
  659. if(! function_exists('GenerateUpdateQueryByTypeData'))
  660. {
  661. function
  662. GenerateUpdateQueryByTypeData($ArrayField,$ArrayParam,$Table)
  663. {
  664. $tableData = mysql_query("select COLUMN_NAME, data_type
  665. from information_schema.columns where table_schema = '".DB_NAME."' and
  666. table_name = '".$Table."'");
  667. $StringField = $StringValue = $StringQuery = '';
  668. while($data = mysql_fetch_assoc($tableData))
  669. {
  670. foreach ($ArrayField as $Key => $Column)
  671. {
  672. if($data['COLUMN_NAME'] == $Column)
  673. {
  674. if ($Key != 0 && isset($ArrayParam[$Column])) {
  675. $Value = $ArrayParam[$Column];
  676. $StringQuery .= (empty($StringQuery)) ? ''
  677. : ', ';
  678. if($data['data_type'] == 'bit')
  679. {
  680. $StringQuery .= "$Column = $Value ";
  681. }else
  682. {
  683. $StringQuery .= "$Column = '" .
  684. mysql_real_escape_string($Value) . "'";
  685. }
  686. }
  687. }
  688. }
  689. }
  690. $Query = "UPDATE `$Table` SET $StringQuery WHERE " .
  691. $ArrayField[0] . " = '" . $ArrayParam[$ArrayField[0]] . "'";
  692. return $Query;
  693. }
  694. }
  695. if (! function_exists('get_page_active')) {
  696. function get_page_active() {
  697. preg_match('/\/page_(\d+)/i', $_SERVER['REQUEST_URI'], $match);
  698. $page_no = (isset($match[1])) ? $match[1] : 1;
  699. return $page_no;
  700. }
  701. }
  702. if (! function_exists('sent_mail')) {
  703. function sent_mail($param) {
  704. $param['subject'] = (isset($param['subject'])) ? $param['subject'] : '';
  705. $param['message'] = (isset($param['message'])) ? $param['message'] : '';
  706. $param['header'] = (isset($param['header'])) ? $param['header'] : 'From: Karimun Shop <info@simetri.web.id>';
  707. if (empty($param['message'])) {
  708. return;
  709. }
  710. @mail($param['email'], $param['subject'], $param['message'], $param['header']);
  711. }
  712. }
  713. if (! function_exists('set_flash_message')) {
  714. function set_flash_message($value) {
  715. $_SESSION['flash_message'] = $value;
  716. }
  717. }
  718. if (! function_exists('get_flash_message')) {
  719. function get_flash_message() {
  720. $value = '';
  721. if (isset($_SESSION['flash_message'])) {
  722. $value = $_SESSION['flash_message'];
  723. }
  724. $_SESSION['flash_message'] = '';
  725. unset($_SESSION['flash_message']);
  726. return $value;
  727. }
  728. }
  729. if (! class_exists('CURL')) {
  730. class CURL {
  731. var $callback = false;
  732. function setCallback($func_name) {
  733. $this->callback = $func_name;
  734. }
  735. function doRequest($method, $url, $param = array()) {
  736. $ch = curl_init();
  737. curl_setopt($ch, CURLOPT_URL, $url);
  738. curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
  739. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  740. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  741. curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
  742. curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
  743. curl_setopt($ch, CURLOPT_TIMEOUT, 20);
  744. curl_setopt($ch, CURLOPT_HEADER, @$param['header']);
  745. // method
  746. if ($method == 'POST') {
  747. curl_setopt($ch, CURLOPT_POST, 1);
  748. curl_setopt($ch, CURLOPT_POSTFIELDS, $param['param']);
  749. }
  750. // referer address
  751. if (!empty($param['referer_address'])) {
  752. curl_setopt($ch, CURLOPT_REFERER, $param['referer_address']);
  753. }
  754. $data = curl_exec($ch);
  755. curl_close($ch);
  756. if ($data) {
  757. if ($this->callback) {
  758. $callback = $this->callback;
  759. $this->callback = false;
  760. return call_user_func($callback, $data);
  761. } else {
  762. return $data;
  763. }
  764. } else {
  765. if (is_resource($ch))
  766. return curl_error($ch);
  767. else
  768. return false;
  769. }
  770. }
  771. function get($url, $referer_address = '', $param = array()) {
  772. return $this->doRequest('GET', $url, $param);
  773. }
  774. function post($url, $param = array()) {
  775. return $this->doRequest('POST', $url, $param);
  776. }
  777. }
  778. }
  779. if (! function_exists('save_tinymce')) {
  780. function save_tinymce($value) {
  781. $result = $value;
  782. $result = str_replace("\"", "'", $result);
  783. $result = htmlentities($result, ENT_QUOTES);
  784. return $result;
  785. }
  786. }
  787. if (! function_exists('object_to_array')) {
  788. function object_to_array($data) {
  789. if (is_array($data) || is_object($data))
  790. {
  791. $result = array();
  792. foreach ($data as $key => $value)
  793. {
  794. $result[$key] = object_to_array($value);
  795. }
  796. return $result;
  797. }
  798. return $data;
  799. }
  800. }
  801. ?>