PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/engine/class/config.class.php

https://github.com/nezbryk/newhealthkeeper
PHP | 588 lines | 438 code | 101 blank | 49 comment | 92 complexity | b887871df1b87764d719963e95e24ee5 MD5 | raw file
  1. <?php
  2. require_once(ENGINE_PATH.'class/error.class.php');
  3. class Config{
  4. private $_pdo;
  5. private $_dbname;
  6. private $_dbuser;
  7. private $_dbpass;
  8. function __construct($op=0,$name=DB_HEALTHKEEP_NAME,$user=DB_HEALTHKEEP_USER,$pass=DB_HEALTHKEEP_PW)
  9. {
  10. if(defined("USER_DB_NAME") && defined("USER_DB_USER") && defined("USER_DB_PASS") && $op==0)
  11. {
  12. $this->_dbname=USER_DB_NAME;
  13. $this->_dbuser=USER_DB_USER;
  14. $this->_dbpass=USER_DB_PASS;
  15. }else{
  16. $this->_dbname=$name;
  17. $this->_dbuser=$user;
  18. $this->_dbpass=$pass;
  19. }
  20. $this->_pdo = $this->pdo();
  21. }
  22. public function pdo()
  23. {
  24. try
  25. {
  26. return new PDO("mysql:host=".DB_HEALTHKEEP_HOST.";dbname=".$this->_dbname,
  27. $this->_dbuser,$this->_dbpass,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'",
  28. PDO::ATTR_EMULATE_PREPARES => false,
  29. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
  30. }
  31. catch(PDOException $err)
  32. {
  33. $myError = new Error();
  34. $myError->exceptionHandling($err);
  35. }
  36. catch (Exception $err)
  37. {
  38. $myError = new Error();
  39. $myError->exceptionHandling($err);
  40. }
  41. }
  42. public function query($sql,$params=array(),$modo="")
  43. {
  44. try
  45. {
  46. if($modo == "")
  47. {
  48. $query = $this->_pdo->prepare($sql);
  49. if(preg_match("/^select/", strtolower($sql))){
  50. $query->execute($params);
  51. $res = $query->fetchAll(PDO::FETCH_ASSOC);
  52. $res["result"]=count($res);
  53. return $res;
  54. }else{
  55. return $query->execute($params);
  56. }
  57. }
  58. elseif ($modo == "exec") {
  59. return $this->_pdo->exec($sql);
  60. }
  61. }
  62. catch(PDOException $err)
  63. {
  64. $myError = new Error();
  65. $myError->exceptionHandling($err);
  66. }
  67. catch (Exception $err)
  68. {
  69. $myError = new Error();
  70. $myError->exceptionHandling($err);
  71. }
  72. }
  73. public function name($array,$param0=true){
  74. if($param0){
  75. if($array[0]["type_profile"]==1){
  76. return $array[0]["username_profile"];
  77. }else{
  78. return $array[0]["name_profile"];
  79. }
  80. }else{
  81. if($array["type_profile"]==1){
  82. return $array["username_profile"];
  83. }else{
  84. return $array["name_profile"];
  85. }
  86. }
  87. }
  88. public function endEmailText(){
  89. return "<br /><br />-----------------------------------------------------------<br /><br />You received this message because your <a href=\"".WEB_URL."\">HealthKeep</a> account is set to allow it.<br />If you wish to unsubscribe from this type of email please visit ".WEB_URL."account/notifications";
  90. }
  91. public function formatPhoneNumber($number){
  92. return "(".substr($number, 0, 3).") ".substr($number, 3, 3)."-".substr($number,6);
  93. }
  94. public function getRealIpAddr()
  95. {
  96. if (!empty($_SERVER['HTTP_CLIENT_IP'])){ //check ip from share internet
  97. return $_SERVER['HTTP_CLIENT_IP'];
  98. }else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){ //to check ip is pass from proxy
  99. return $_SERVER['HTTP_X_FORWARDED_FOR'];
  100. }else{
  101. return $_SERVER['REMOTE_ADDR'];
  102. }
  103. }
  104. public function escapeOddChars($string){
  105. $string=str_replace('‘', '\'', $string);
  106. $string=str_replace('’', '\'', $string);
  107. return $string;
  108. }
  109. public function br2nl($string)
  110. {
  111. RETURN PREG_REPLACE('#<br\s*?/?>#i', "", $string);
  112. }
  113. public function processPostText($text){
  114. $text=strip_tags($text);
  115. $text=$this->haveLink($text);
  116. $text=nl2br(trim($text));
  117. /*$text=preg_replace("/(<br\s*\/?>\s*)+/", "<br/><br/>", $text);*/
  118. return $text;
  119. }
  120. public function haveLink($text){
  121. $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
  122. if(preg_match($reg_exUrl, $text, $url)) {
  123. $parsed=parse_url($url[0]);
  124. //return preg_replace($reg_exUrl, "<a href=\"".$url[0]."\" rel=\"nofollow\" target=\"_blank\">".$parsed["host"]."</a> ", $text);
  125. //as requested by Lyle, the URL displayed should be the full URL and not just the domain
  126. return preg_replace($reg_exUrl, "<a href=\"".$url[0]."\" rel=\"nofollow\" target=\"_blank\">".$url[0]."</a> ", $text);
  127. } else {
  128. return $text;
  129. }
  130. }
  131. public function ago($time){
  132. $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
  133. $lengths = array("60","60","24","7","4.35","12","10");
  134. $now = time();
  135. $difference = $now - $time;
  136. $tense = "ago";
  137. for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
  138. $difference /= $lengths[$j];
  139. }
  140. $difference = round($difference);
  141. if($difference<1){
  142. return "just now";
  143. }
  144. if($difference != 1) {
  145. $periods[$j].= "s";
  146. }
  147. return "$difference $periods[$j] ago ";
  148. }
  149. public function safeUri($string){
  150. $string = trim($string);
  151. if ( ctype_digit($string) ) {
  152. return $string;
  153. }
  154. else {
  155. // replace accented chars
  156. $accents = '/&([A-Za-z]{1,2})(grave|acute|circ|cedil|uml|lig|tilde);/';
  157. $string_encoded = htmlentities($string,ENT_NOQUOTES,'UTF-8');
  158. $string = preg_replace($accents,'$1',$string_encoded);
  159. // clean out the rest
  160. $replace = array('([\40])','([^a-zA-Z0-9-])','(-{2,})','/[^[:print:]]+/');
  161. $with = array('-','','-','');
  162. $string = preg_replace($replace,$with,$string);
  163. }
  164. $string = ltrim($string, "-");
  165. $string = rtrim($string, "-");
  166. $string = strtolower($string);
  167. return urlencode($string);
  168. }
  169. public function getPagingQuery($sql,$page, $itemPerPage = 10)
  170. {
  171. if ($page > 0) {
  172. $page = $page;
  173. } else {
  174. $page = 1;
  175. }
  176. // start fetching from this row number
  177. $offset = ($page - 1) * $itemPerPage;
  178. return $sql . " LIMIT $offset, $itemPerPage";
  179. }
  180. public function pagination($total,$actual,$url,$symbol,$num=15)
  181. {
  182. $actual = (int)$actual;
  183. if($actual == 0)
  184. {
  185. $actual = 1;
  186. }
  187. $start = (($actual-1)*$num);
  188. $end = $num;
  189. $total_pages = ceil($total/$num);
  190. $html = "";
  191. if($total_pages > 1)
  192. {
  193. if($actual==1){
  194. $html.='<button disabled class="btn">first</button>';
  195. }else{
  196. $html.='<a href="'.$url.'" class="btn">first</a>';
  197. }
  198. if($actual>5){
  199. $z=$actual-4;
  200. }else{
  201. $z=1;
  202. }
  203. $quantos=1;
  204. for ($i=$z;$total_pages>=$i && $quantos<=10;$i++)
  205. {
  206. if($i==$actual){
  207. $html.= ' | <button disabled class="btn">'.$i."</button>";
  208. }else if($i==1)
  209. {
  210. $html.= ' | <a href="'.$url.'" class="btn">'.$i.'</a>';
  211. }
  212. else
  213. {
  214. $html.= ' | <a href="'.$url.$symbol.'page='.$i.'" class="btn">'.$i.'</a>';
  215. }
  216. $quantos++;
  217. }
  218. if($actual==$total_pages){
  219. $html.='<button disabled class="btn">last</button>';
  220. }else{
  221. $html.=' | <a href="'.$url.$symbol.'page='.$total_pages.'" class="btn">last</a>';
  222. }
  223. //$html = ltrim($html," |");
  224. }
  225. return array("start"=>$start,"end"=>$end,"total_pages"=>$total_pages,"html"=>$html);
  226. }
  227. public function uploadFile($inputName,$uploadDir){
  228. $filename = $_FILES[$inputName];
  229. $filePath = '';
  230. if (trim($filename['tmp_name']) != '') {
  231. $ext = substr(strrchr($filename['name'], "."), 1);
  232. $ext = strtolower($ext);
  233. $filePath = md5(rand() * time()) . ".".$ext;
  234. if(!move_uploaded_file($filename['tmp_name'], $uploadDir.$filePath)) {
  235. $filePath = '';
  236. }
  237. }
  238. return array('file' => $filePath);
  239. }
  240. /*
  241. Create a thumbnail of $srcFile and save it to $destFile.
  242. The thumbnail will be $width pixels.
  243. */
  244. private function createThumbnail($srcFile, $destFile, $width, $quality = 90)
  245. {
  246. $thumbnail = '';
  247. //HACKED TO ALLOW REMOTE URL UPLOAD
  248. /*if (file_exists($srcFile) && isset($destFile))
  249. {*/
  250. $size = getimagesize($srcFile);
  251. $w = number_format($width, 0, ',', '');
  252. $h = number_format(($size[1] / $size[0]) * $width, 0, ',', '');
  253. $thumbnail = $this->copyImage($srcFile, $destFile, $w, $h, $quality);
  254. //}
  255. // return the thumbnail file name on sucess or blank on fail
  256. return basename($thumbnail);
  257. }
  258. /*
  259. Copy an image to a destination file. The destination
  260. image size will be $w X $h pixels
  261. */
  262. private function copyImage($srcFile, $destFile, $w, $h, $quality = 90)
  263. {
  264. $tmpSrc = pathinfo(strtolower($srcFile));
  265. $tmpDest = pathinfo(strtolower($destFile));
  266. $size = getimagesize($srcFile);
  267. if ($tmpDest['extension'] == "gif" || $tmpDest['extension'] == "jpg" || $tmpDest['extension'] == "jpeg")
  268. {
  269. //$destFile = substr_replace($destFile, 'jpg', -3);
  270. $dest = imagecreatetruecolor($w, $h);
  271. imageantialias($dest, TRUE);
  272. } elseif ($tmpDest['extension'] == "png") {
  273. $dest = imagecreatetruecolor($w, $h);
  274. imageantialias($dest, TRUE);
  275. } else {
  276. return false;
  277. }
  278. if($size[2]==1 || $size[2]==3){
  279. imagecolortransparent($dest, imagecolorallocatealpha($dest, 0, 0, 0, 127));
  280. imagealphablending($dest, false);
  281. imagesavealpha($dest, true);
  282. }
  283. switch($size[2])
  284. {
  285. case 1: //GIF
  286. $src = imagecreatefromgif($srcFile);
  287. break;
  288. case 2: //JPEG
  289. $src = imagecreatefromjpeg($srcFile);
  290. break;
  291. case 3: //PNG
  292. $src = imagecreatefrompng($srcFile);
  293. break;
  294. default:
  295. return false;
  296. break;
  297. }
  298. imagecopyresampled($dest, $src, 0, 0, 0, 0, $w, $h, $size[0], $size[1]);
  299. switch($size[2])
  300. {
  301. case 1:
  302. imagegif($dest,$destFile,$quality);
  303. break;
  304. case 2:
  305. imagejpeg($dest,$destFile,$quality);
  306. break;
  307. case 3:
  308. imagepng($dest,$destFile);
  309. }
  310. return $destFile;
  311. }
  312. public function uploadImage($inputName, $uploadDir, $orgsize=960, $medsize=320, $tbsize=100)
  313. {
  314. $image = $_FILES[$inputName];
  315. $imagePath = '';
  316. // if a file is given
  317. if (trim($image['tmp_name']) != '') {
  318. $ext = substr(strrchr($image['name'], "."), 1); //$extensions[$image['type']];
  319. // generate a random new file name to avoid name conflict
  320. $ext = strtolower($ext);
  321. $imagePath = md5(rand() * time()) . ".".$ext;
  322. //$imagePath = md5(rand() * time()) . ".jpg";
  323. list($width, $height, $type, $attr) = getimagesize($image['tmp_name']);
  324. if (($image["type"] == "image/jpeg" || $image["type"] == "image/pjpeg" || $image["type"] == "image/gif" || $image["type"] == "image/x-png" || $image["type"] == "image/png") && ($image["size"] < 2097152)){
  325. // make sure the image width does not exceed the
  326. // maximum allowed width
  327. if ($width < $orgsize) {
  328. $orgsize=$width;
  329. }
  330. $result = $this->createThumbnail($image['tmp_name'], $uploadDir . "org/" . $imagePath, $orgsize);
  331. if(!$result){
  332. $imagePath = '';
  333. }else{
  334. $imagePath = $result;
  335. if ($width < $medsize) {
  336. $medsize=$width;
  337. }
  338. $result = $this->createThumbnail($uploadDir . "org/" . $imagePath, $uploadDir . "med/" . $imagePath, $medsize);
  339. if (!$result) {
  340. // the product cannot be upload / resized
  341. unlink($uploadDir . "org/" . $imagePath);
  342. $imagePath = '';
  343. }else{
  344. if ($width < $tbsize) {
  345. $tbsize=$width;
  346. }
  347. if($width==$height){
  348. $result = $this->createThumbnail($uploadDir . "med/" . $imagePath, $uploadDir . "tb/" . $imagePath, $tbsize);
  349. }else{
  350. $result = $this->square_crop($uploadDir . "med/" . $imagePath, $uploadDir . "tb/" . $imagePath, $tbsize);
  351. }
  352. if (!$result) {
  353. // the product cannot be upload / resized
  354. unlink($uploadDir . "org/" . $imagePath);
  355. unlink($uploadDir . "med/" . $imagePath);
  356. $imagePath = '';
  357. }
  358. }
  359. }
  360. //echo "ok";
  361. }else{
  362. //echo "erro";
  363. $imagePath='';
  364. }
  365. }
  366. return array('image' => $imagePath);
  367. }
  368. private function square_crop($src_image, $dest_image, $thumb_size = 75, $jpg_quality = 90) {
  369. // Get dimensions of existing image
  370. $image = getimagesize($src_image);
  371. // Check for valid dimensions
  372. if( $image[0] <= 0 || $image[1] <= 0 ) return false;
  373. // Determine format from MIME-Type
  374. $image['format'] = strtolower(preg_replace('/^.*?\//', '', $image['mime']));
  375. // Import image
  376. if($image['format']=="png"){
  377. $image_data = imagecreatefrompng($src_image);
  378. }else if($image['format']=="gif"){
  379. $image_data = imagecreatefromgif($src_image);
  380. }else{
  381. $image_data = imagecreatefromjpeg($src_image);
  382. }
  383. // Verify import
  384. if( $image_data == false ) {return false;};
  385. // Calculate measurements
  386. if( $image[0] > $image[1] ) {
  387. // For landscape images
  388. $x_offset = ($image[0] - $image[1]) / 2;
  389. $y_offset = 0;
  390. $square_size = $image[0] - ($x_offset * 2);
  391. } else {
  392. // For portrait and square images
  393. $x_offset = 0;
  394. $y_offset = ($image[1] - $image[0]) / 2;
  395. $square_size = $image[1] - ($y_offset * 2);
  396. }
  397. // Resize and crop
  398. $canvas = imagecreatetruecolor($thumb_size, $thumb_size);
  399. if($image['format']=="png" || $image['format']=="gif"){
  400. imagecolortransparent($canvas, imagecolorallocatealpha($canvas, 0, 0, 0, 127));
  401. imagealphablending($canvas, false);
  402. imagesavealpha($canvas, true);
  403. }
  404. if( imagecopyresampled(
  405. $canvas,
  406. $image_data,
  407. 0,
  408. 0,
  409. $x_offset,
  410. $y_offset,
  411. $thumb_size,
  412. $thumb_size,
  413. $square_size,
  414. $square_size
  415. )) {
  416. if($image['format']=="png"){
  417. return imagepng($canvas, $dest_image, 0);
  418. }else if($image['format']=="gif"){
  419. return imagegif($canvas, $dest_image);
  420. }else{
  421. return imagejpeg($canvas, $dest_image, $jpg_quality);
  422. }
  423. } else {
  424. return false;
  425. }
  426. }
  427. public function uploadImageURL($url, $uploadDir, $orgsize=960, $medsize=320, $tbsize=100)
  428. {
  429. $image = $url;
  430. $imagePath = '';
  431. // if a file is given
  432. if (trim($image) != '') {
  433. $ext = substr(strrchr($image, "."), 1); //$extensions[$image['type']];
  434. // generate a random new file name to avoid name conflict
  435. $ext = strtolower($ext);
  436. $imagePath = md5(rand() * time()) . ".jpg";
  437. if(!@getimagesize($image)){
  438. return array('image' => '');
  439. }
  440. list($width, $height, $type, $attr) = getimagesize($image);
  441. //print_r($image);exit;
  442. if (1==1/*($image["type"] == "image/jpeg" || $image["type"] == "image/pjpeg" || $image["type"] == "image/gif" || $image["type"] == "image/x-png") && ($image["size"] < 1000000)*/){
  443. // make sure the image width does not exceed the
  444. // maximum allowed width
  445. if ($width < $orgsize) {
  446. $orgsize=$width;
  447. }
  448. $result = $this->createThumbnail($image, $uploadDir . "org/" . $imagePath, $orgsize);
  449. if(!$result){
  450. $imagePath = '';
  451. }else{
  452. //$imagePath = $result;
  453. $imagePath = $result;
  454. if ($width < $medsize) {
  455. $medsize=$width;
  456. }
  457. $result = $this->createThumbnail($uploadDir . "org/" . $imagePath, $uploadDir . "med/" . $imagePath, $medsize);
  458. if (!$result) {
  459. // the product cannot be upload / resized
  460. unlink($uploadDir . "org/" . $imagePath);
  461. $imagePath = '';
  462. }else{
  463. if ($width < $tbsize) {
  464. $tbsize=$width;
  465. }
  466. if($width==$height){
  467. $result = $this->createThumbnail($uploadDir . "org/" . $imagePath, $uploadDir . "tb/" . $imagePath, $tbsize);
  468. }else{
  469. $result = $this->square_crop($uploadDir . "org/" . $imagePath, $uploadDir . "tb/" . $imagePath, $tbsize);
  470. }
  471. if (!$result) {
  472. // the product cannot be upload / resized
  473. unlink($uploadDir . "org/" . $imagePath);
  474. unlink($uploadDir . "med/" . $imagePath);
  475. $imagePath = '';
  476. }
  477. }
  478. }
  479. //echo "ok";
  480. }else{
  481. //echo "erro";
  482. $imagePath='';
  483. }
  484. }
  485. return array('image' => $imagePath);
  486. }
  487. }