PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/application/helpers/utility_helper.php

https://bitbucket.org/manisha2018/love-undivided
PHP | 334 lines | 216 code | 54 blank | 64 comment | 28 complexity | 0a4330fd9e92be30da5e30d9fd3e6ea9 MD5 | raw file
  1. <?php
  2. if (!defined('BASEPATH'))
  3. exit('No direct script access allowed');
  4. /* Function check for password hash
  5. * @param string
  6. * return TRUE on success FALSE if fails
  7. */
  8. function verifyPasswordHash($password, $hash_and_salt) {
  9. if (password_verify($password, $hash_and_salt)) {
  10. return TRUE;
  11. } else {
  12. return FALSE;
  13. }
  14. }
  15. /* Function create hash and salt password
  16. * @param string
  17. * return string
  18. */
  19. function createHashAndSalt($user_provided_password) {
  20. $options = [
  21. 'cost' => 12,
  22. ];
  23. $hash = password_hash($user_provided_password, PASSWORD_BCRYPT, $options);
  24. return $hash;
  25. }
  26. /* Function To Send An Email To User
  27. */
  28. function sendMail($to, $subject, $message) {
  29. // print_r($to);die();
  30. $from = 'no-reply@LoveUndivided.com';
  31. include 'email_library.php'; // include the library file
  32. include "email_classes/class.phpmailer.php"; // include the class name
  33. $mail = new PHPMailer; // call the class
  34. $mail->IsSMTP();
  35. $mail->Host = SMTP_HOST; //Hostname of the mail server
  36. $mail->Port = SMTP_PORT; //Port of the SMTP like to be 25, 80, 465 or 587
  37. $mail->SMTPAuth = true; //Whether to use SMTP authentication
  38. $mail->Username = SMTP_UNAME; //Username for SMTP authentication any valid email created in your domain
  39. $mail->Password = SMTP_PWORD; //Password for SMTP authentication
  40. $mail->AddReplyTo($from); //reply-to address
  41. $mail->SetFrom($from, "LoveUndivided"); //From address of the mail
  42. // put your while loop here like below,
  43. $mail->Subject = $subject; //Subject od your mail
  44. // echo count($to);die;
  45. if(count($to)==1)
  46. {
  47. //echo "Manu";
  48. if(gettype($to)=="array"){
  49. foreach ($to as $value) {
  50. $mail->AddAddress($value, "");
  51. }
  52. }else{
  53. $mail->AddAddress($to, "");
  54. }
  55. //$mail->AddAddress($to, ""); //To address who will receive this email
  56. $mail->MsgHTML($message); //Put your body of the message you can place html code here
  57. //$mail->AddAttachment("images/asif18-logo.png"); //Attach a file here if any or comment this line,
  58. $send = $mail->Send();
  59. }
  60. else
  61. {
  62. // print_r($to);
  63. if(is_array($message))
  64. {
  65. foreach ($to as $key1 => $value1) {
  66. $mail->ClearAddresses(); //It removes adress history
  67. $mail->AddAddress($value1, "");
  68. $mail->MsgHTML($message[$key1]);
  69. $send = $mail->Send();
  70. }
  71. }
  72. else
  73. {
  74. foreach ($to as $key1 => $value1) {
  75. $mail->ClearAddresses(); //It removes adress history
  76. $mail->AddAddress($value1, "");
  77. $mail->MsgHTML($message);
  78. $send = $mail->Send();
  79. }
  80. }
  81. }
  82. //die;
  83. if ($send) {
  84. return true;
  85. } else {
  86. return false;
  87. }
  88. }
  89. function random_password($cnt = 8) {
  90. $alphabet = "ABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
  91. $pass = array(); //remember to declare $pass as an array
  92. $alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
  93. for ($i = 0; $i < $cnt; $i++) {
  94. $n = rand(0, $alphaLength);
  95. $pass[] = $alphabet[$n];
  96. }
  97. return implode($pass); //turn the array into a string
  98. }
  99. /*
  100. * funciton for upload image without compress
  101. * created on :
  102. * modified on:
  103. * created by :Akshay Patil
  104. *
  105. */
  106. function upload_image($destination, $fieldName, $prefix) {
  107. $CI = & get_instance();
  108. $file_ext = substr(strrchr($_FILES[$fieldName]['name'], '.'), 1);
  109. $name = $prefix . "_" . time() . "_" . random_password();
  110. $config = array(
  111. 'upload_path' => "./$destination",
  112. 'allowed_types' => "jpeg|jpg|png|gif",
  113. 'overwrite' => TRUE,
  114. 'file_name' => $name . "." . $file_ext
  115. );
  116. $CI->load->helper('file');
  117. $CI->load->library('upload', $config);
  118. $CI->upload->initialize($config);
  119. if ($CI->upload->do_upload($fieldName)) {
  120. $data = array('upload_data' => $CI->upload->data());
  121. return $data['upload_data']['file_name'];
  122. } else {
  123. $error = array('error' => $CI->upload->display_errors());
  124. return 0; //$error;
  125. }
  126. }
  127. /*
  128. * funciton for upload image with compress
  129. * created on :
  130. * modified on:
  131. * created by :Akshay Patil
  132. *
  133. */
  134. function image_compress($source, $destination, $quality) {
  135. $info = getimagesize($source);
  136. if ($info['mime'] == 'image/jpeg')
  137. $image = imagecreatefromjpeg($source);
  138. elseif ($info['mime'] == 'image/gif')
  139. $image = imagecreatefromgif($source);
  140. elseif ($info['mime'] == 'image/png')
  141. $image = imagecreatefrompng($source);
  142. imagejpeg($image, $destination, $quality);
  143. imagedestroy($image);
  144. return $destination;
  145. }
  146. /*
  147. * funciton for thumb image
  148. * created on :
  149. * modified on:
  150. * created by :Akshay Patil
  151. *
  152. */
  153. function image_thumb($source, $destination, $new_width = 150, $new_height = 150) {
  154. list($old_width, $old_height) = getimagesize($source);
  155. $new_image = imagecreatetruecolor($new_width, $new_height);
  156. $old_image = imagecreatefromjpeg($source);
  157. imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);
  158. // save thumbnail into a file
  159. imagejpeg($new_image, "{$destination}");
  160. imagedestroy($old_image);
  161. imagedestroy($new_image);
  162. return true;
  163. }
  164. /*
  165. * funciton for upload video files
  166. * created on :
  167. * modified on:
  168. * created by :Akshay Patil
  169. *
  170. */
  171. function upload_video($destination, $fieldName, $prefix) {
  172. $CI = & get_instance();
  173. $file_ext = substr(strrchr($_FILES[$fieldName]['name'], '.'), 1);
  174. $name = $prefix . "_" . time() . "_" . random_password();
  175. $config = array(
  176. 'upload_path' => "./$destination",
  177. 'allowed_types' => "*",
  178. 'overwrite' => TRUE,
  179. 'file_name' => $name . "." . $file_ext
  180. );
  181. //$config['max_size'] = '100';
  182. $CI->load->helper('file');
  183. $CI->load->library('upload', $config);
  184. $CI->upload->initialize($config); // Make sure it has been initialized
  185. if ($CI->upload->do_upload($fieldName)) {
  186. $data = array('upload_data' => $CI->upload->data());
  187. return $data['upload_data']['file_name'];
  188. } else {
  189. $error = array('error' => $CI->upload->display_errors());
  190. return $error;
  191. }
  192. }
  193. /*
  194. function for generating Slug
  195. created on : 22 Aug 2017
  196. created by : Pratik Guthalkar
  197. */
  198. function create_slug($text,$tbl_name,$col_name,$user_id="")
  199. {
  200. $text = preg_replace('~[^\pL\d]+~u', '-', $text);
  201. // transliterate
  202. $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
  203. // remove unwanted characters
  204. $text = preg_replace('~[^-\w]+~', '', $text);
  205. // trim
  206. $text = trim($text, '-');
  207. // remove duplicate -
  208. $text = preg_replace('~-+~', '-', $text);
  209. // lowercase
  210. $text = strtolower($text);
  211. if (empty($text)) {
  212. return 'n-a';
  213. }
  214. $slug = $text;
  215. $i = 1;
  216. if(empty($user_id))
  217. {
  218. if(slug_exist($text,$tbl_name,$col_name)){
  219. $slug = $text . "-" . $i++;
  220. }
  221. }
  222. else{
  223. if(slug_exist($text,$tbl_name,$col_name,$user_id)){
  224. $slug = $text . "-" . $i++;
  225. }
  226. }
  227. return $slug;
  228. }
  229. function check_subscription($user_id)
  230. {
  231. if(!empty($user_id))
  232. {
  233. $CI = & get_instance();
  234. // $sql = "SELECT user_id,CURRENT_DATE FROM user_subscription where user_id = $user_id AND CURRENT_DATE BETWEEN DATE(start_date) AND DATE(end_date)";
  235. $sql = "SELECT user_id,(IF(end_date='0000-00-00 00:00:00', (SELECT user_id FROM user_subscription WHERE user_id = $user_id ),(SELECT CURRENT_DATE FROM user_subscription WHERE user_id = $user_id AND CURRENT_DATE BETWEEN DATE(start_date) AND DATE(end_date)) ) ) as data FROM user_subscription where user_id=$user_id";
  236. // echo $sql ; die;
  237. $query = $CI->db->query($sql);
  238. $res = $query->num_rows();
  239. //print_r($res); die;
  240. if($res > 0)
  241. return false;
  242. else
  243. return true;
  244. }
  245. else
  246. return false;
  247. }
  248. function slug_exist($slug,$tbl_name,$col_name,$user_id="")
  249. {
  250. $CI = & get_instance();
  251. $sql = "SELECT $col_name FROM $tbl_name where $col_name = '$slug'";
  252. if(!empty($user_id))
  253. $sql .= " AND user_id != $user_id ";
  254. //echo $sql ; die;
  255. $query = $CI->db->query($sql);
  256. $res = $query->num_rows();
  257. if($res > 0)
  258. return true;
  259. else
  260. return false;
  261. }
  262. function check_expiration($user_id)
  263. {
  264. if(!empty($user_id))
  265. {
  266. $CI = & get_instance();
  267. $sql = "SELECT user_subscription.* , TIMESTAMPDIFF(DAY , CURRENT_DATE, end_date ) AS days
  268. FROM user_subscription WHERE user_id = $user_id";
  269. // echo $sql ; die;
  270. $query = $CI->db->query($sql);
  271. $res = $query->result_array();
  272. // print_r($res); die;
  273. if(!empty($res))
  274. return $res;
  275. else
  276. return false;
  277. }
  278. else
  279. return false;
  280. }