Advertisement
yhoezt_27

Untitled

Aug 7th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.14 KB | None | 0 0
  1. Untuk membersihkan data dari Form_Input
  2.  
  3. (1) tools_helper
  4. function id_clean($id,$size=11){
  5. return intval(substr($id,0,$size));
  6. }
  7.  
  8. function db_clean($string,$size=255){
  9. return xss_clean(substr($string,0,$size));
  10. }
  11.  
  12.  
  13. function createfoldername($string){
  14. $string = mb_strtolower($string,'utf-8');
  15. $regexp = '/( |g)/iU';
  16. // $regexp = '/( |å|ø|æ|Å|Ø|Æ|Ã¥|ø|æ|Ã…|Ø|Æ)/iU';
  17. $replace_char = '_';
  18. $data = preg_replace($regexp, $replace_char, $string);
  19. return $data;
  20. }
  21.  
  22. /*
  23. * This will replace non English to similar letter in English
  24. *
  25. */
  26. function createdirname($string){
  27.  
  28. $forbidden = array(" ", "å", "Å","ø", "Ø", "æ", "Æ", "ã…", "ã˜","ã†", "ã¥", "ã¸", "ã¦" );
  29. // order is space, å, Å,ø, Ø,æ, Æ, and Å, Ø, Æ, å,ø,æ
  30. $normal = array("_", "aa", "aa", "o", "o", "ae", "ae","aa","o", "ae", "aa", "o", "ae" );
  31. $string = str_replace($forbidden, $normal, $string);
  32. $data = mb_strtolower($string,'utf-8');
  33. return $data;
  34. }
  35.  
  36.  
  37. function create_path($folder)
  38. {
  39. // create dir if not exists
  40. $folder = explode( "/" , $folder );
  41. $mkfolder = "";
  42. //sets the complete directory path
  43. for( $i=0 ; isset( $folder[$i] ) ; $i++ )
  44. {
  45. $mkfolder .= $folder[$i] . '/';
  46. if(!is_dir($mkfolder )) {
  47. mkdir("$mkfolder");
  48. mkdir("$mkfolder/thumbnails");
  49. }
  50. }
  51. }
  52.  
  53. function recursive_remove_directory($directory, $empty=FALSE)
  54. {
  55. // if the path has a slash at the end we remove it here
  56. if(substr($directory,-1) == '/')
  57. {
  58. $directory = substr($directory,0,-1);
  59. }
  60.  
  61. // if the path is not valid or is not a directory ...
  62. if(!file_exists($directory) || !is_dir($directory))
  63. {
  64. // ... we return false and exit the function
  65. return FALSE;
  66.  
  67. // ... if the path is not readable
  68. }elseif(!is_readable($directory))
  69. {
  70. // ... we return false and exit the function
  71. return FALSE;
  72.  
  73. // ... else if the path is readable
  74. }else{
  75.  
  76. // we open the directory
  77. $handle = opendir($directory);
  78.  
  79. // and scan through the items inside
  80. while (FALSE !== ($item = readdir($handle)))
  81. {
  82. // if the filepointer is not the current directory
  83. // or the parent directory
  84. if($item != '.' && $item != '..')
  85. {
  86. // we build the new path to delete
  87. $path = $directory.'/'.$item;
  88.  
  89. // if the new path is a directory
  90. if(is_dir($path))
  91. {
  92. // we call this function with the new path
  93. // you need to change to $this->recursive_remove_directory($path);
  94. // in controller.
  95. recursive_remove_directory($path);
  96.  
  97. // if the new path is a file
  98. }else{
  99. // we remove the file
  100. unlink($path);
  101. }
  102. }
  103. }
  104. // close the directory
  105. closedir($handle);
  106.  
  107. // if the option to empty is not set to true
  108. if($empty == FALSE)
  109. {
  110. // try to delete the now empty directory
  111. if(!rmdir($directory))
  112. {
  113. // return false if not possible
  114. return FALSE;
  115. }
  116. }
  117. // return success
  118. return TRUE;
  119. }
  120. }
  121. function findOrphans($id, $orphan_id, $db_table){
  122. // delete a customer from omc_customer table
  123. // if $db_table is omc_customer, this will create customer_id
  124. // then find customer_id in omc_order table to find orphans
  125. /**
  126. * delete an order from omc_order table. this will create order_items orphans in omc_order_item
  127. * find order_item where order_id is
  128. *
  129. *
  130. */
  131. $tablename = explode("-", $db_table);
  132. $tableid = $tablename[1]."_id";
  133.  
  134. // or
  135. // $id_name = preg_replace('/.*_(.*)/', '${1}_id', $db_table);
  136.  
  137. $data = array();
  138. // $this->db->select($tableid.',name');
  139. $this->db->select($tableid,'name');
  140. $this->db->where($orphan_id,id_clean($id));
  141. $Q = $this->db->get($db_table);
  142. if ($Q->num_rows() > 0){
  143. foreach ($Q->result_array() as $row){
  144. $data[$row['id']] = $row['name'];
  145. }
  146. }
  147. $Q->free_result();
  148. return $data;
  149.  
  150. }
  151.  
  152. function convert_image_path ($imageinfo){
  153. $str = $imageinfo;
  154. $m = array();
  155. if (preg_match('#<.*?/([^\.]+\.(jpg|jpeg|gif|png))"#', $str, $m)) {
  156. $image = $m[1];
  157. }else{
  158. $image = $imageinfo;
  159. }
  160. $tags = array("<p>", "</p>");
  161. $image = str_replace($tags, "", $image);
  162. return $image;
  163. }
  164.  
  165. function multiple_img ($image_path){
  166. $search = array("../../../");
  167. $new_images = str_replace($search,"",$image_path);
  168. return $new_images;
  169. }
  170.  
  171. (2) Hal tersebut, di-dasarkan dari security php
  172.  
  173. if ( ! function_exists('xss_clean'))
  174. {
  175. function xss_clean($str, $is_image = FALSE)
  176. {
  177. $CI =& get_instance();
  178. return $CI->security->xss_clean($str, $is_image);
  179. }
  180. }
  181.  
  182. // ------------------------------------------------------------------------
  183.  
  184. /**
  185. * Sanitize Filename
  186. *
  187. * @access public
  188. * @param string
  189. * @return string
  190. */
  191. if ( ! function_exists('sanitize_filename'))
  192. {
  193. function sanitize_filename($filename)
  194. {
  195. $CI =& get_instance();
  196. return $CI->security->sanitize_filename($filename);
  197. }
  198. }
  199.  
  200. // --------------------------------------------------------------------
  201.  
  202. /**
  203. * Hash encode a string
  204. *
  205. * @access public
  206. * @param string
  207. * @return string
  208. */
  209. if ( ! function_exists('do_hash'))
  210. {
  211. function do_hash($str, $type = 'sha1')
  212. {
  213. if ($type == 'sha1')
  214. {
  215. return sha1($str);
  216. }
  217. else
  218. {
  219. return md5($str);
  220. }
  221. }
  222. }
  223.  
  224. // ------------------------------------------------------------------------
  225.  
  226. /**
  227. * Strip Image Tags
  228. *
  229. * @access public
  230. * @param string
  231. * @return string
  232. */
  233. if ( ! function_exists('strip_image_tags'))
  234. {
  235. function strip_image_tags($str)
  236. {
  237. $str = preg_replace("#<img\s+.*?src\s*=\s*[\"'](.+?)[\"'].*?\>#", "\\1", $str);
  238. $str = preg_replace("#<img\s+.*?src\s*=\s*(.+?).*?\>#", "\\1", $str);
  239.  
  240. return $str;
  241. }
  242. }
  243.  
  244. // ------------------------------------------------------------------------
  245.  
  246. /**
  247. * Convert PHP tags to entities
  248. *
  249. * @access public
  250. * @param string
  251. * @return string
  252. */
  253. if ( ! function_exists('encode_php_tags'))
  254. {
  255. function encode_php_tags($str)
  256. {
  257. return str_replace(array('<?php', '<?PHP', '<?', '?>'), array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
  258. }
  259. }
  260.  
  261.  
  262. /* End of file security_helper.php */
  263. /* Location: ./system/helpers/security_helper.php */
  264.  
  265. (3) contoh penggunaan :
  266.  
  267. function tambah()
  268. {
  269. if(isset($_POST['simpan']))
  270. {
  271. $client = db_clean($this->input->post('client'));
  272. $type = db_clean($this->input->post('id_type_client'));
  273. $jenis = db_clean($this->input->post('id_jenis_client'));
  274.  
  275. if ((!is_null($client)) && (!is_null($type)) && (!is_null($jenis)))
  276. $proses = TRUE;
  277. else $proses = FALSE;
  278.  
  279. if ($proses)
  280. {
  281. $propinsi = db_clean($this->input->post('provinsi_id'));
  282.  
  283. $kota = db_clean($this->input->post('kota_id'));
  284.  
  285. $carikota = $this->m_all->cari($kota, $propinsi, NULL, NULL, NULL, 'kota_id', NULL, NULL, 'kota_kabupaten')->result();
  286.  
  287. foreach ($carikota as $row)
  288. {
  289. $newkota = $row->kota_id;
  290. }
  291.  
  292. $this->_set_rules_tambah();
  293.  
  294. if($this->form_validation->run() == true)
  295. {
  296.  
  297. $arr = array($type, $jenis, $newkota, $propinsi, $client);
  298.  
  299. $telp_1 = db_clean($this->input->post('telp_client_1'));
  300. if (empty($telp_1))
  301. {
  302. $telp_1 = NULL;
  303. }
  304. $telp_2 = db_clean($this->input->post('telp_client_2'));
  305. if(empty($telp_2))
  306. {
  307. $telp_2 = NULL;
  308. }
  309. $pinbbm = db_clean($this->input->post('pin_bbm'));
  310. if(empty($pinbbm))
  311. {
  312. $pinbbm = NULL;
  313. }
  314. $alamat = db_clean($this->input->post('alamat'));
  315. if(empty($alamat))
  316. {
  317. $alamat = NULL;
  318. }
  319.  
  320. $tgl = date('Y/m/d');
  321. $user = $this->session->userdata('username');
  322.  
  323. $info = array(
  324. 'id' => $this->db->insert_id(),
  325. 'id_client' => join('_', $arr),
  326. 'client' => $client,
  327. 'telp_client_1' => $telp_1,
  328. 'telp_client_2' => $telp_2,
  329. 'pin_bbm' => $pinbbm,
  330. 'alamat' => $alamat,
  331. 'updated' => $tgl,
  332. 'username' => $user,
  333. 'keterangan' => db_clean($this->input->post('keterangan')),
  334. 'hapus' => '0'
  335. );
  336.  
  337. $this->m_all->simpan($info, $this->table);
  338. echo "<meta http-equiv='refresh' content='0; url=".site_url('data_client/index/add_success')."'>";
  339. }
  340. else
  341. {
  342. ?>
  343. <script type="text/javascript">
  344. alert("Harap Periksa Validitas Data");
  345. </script>
  346. <?php
  347. echo "<meta http-equiv='refresh' content='0; url=".site_url('data_client/tambah')."'>";
  348. }
  349. }
  350. else
  351. {
  352. ?>
  353. <script type="text/javascript">
  354. alert("Penambahan data tidak boleh kosong");
  355. </script>
  356. <?php
  357. echo "<meta http-equiv='refresh' content='0; url=".site_url('data_client/tambah')."'>";
  358. }
  359. }
  360.  
  361. if (isset($_POST['cancel']))
  362. {
  363. echo "<meta http-equiv='refresh' content='0; url=".site_url('data_client/index')."'>";
  364. }
  365.  
  366. $ktgr_alamat = $this->auth->find_ktgr_alamat('data_client/index', 'menu_uri', 'menu', 'id_kategori', 'kategori_menu');
  367. $var = explode('+', $ktgr_alamat);
  368.  
  369. $data['kategori'] = $var['0'];
  370. $data['alamat'] = $var['1'];
  371. $data['alamat1'] = 'data_client/index';
  372. $data['title']="Index Data Client";
  373. $data['alamat2'] = 'data_client/tambah';
  374. $data['title1'] = 'Tambah Data';
  375. $data['list_type_client'] = $this->m_all->getList('tb_type_client','id_type_client', null,'0');
  376. $data['list_jns_client'] = $this->m_all->getList('tb_jenis_client','id_jenis_client', null, '0');
  377. $data['list_propinsi'] = $this->m_all->getList('propinsi','propinsi_id', null, null);
  378.  
  379. $this->template->display('data_client/tambah', $data);
  380. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement