PageRenderTime 49ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/1/enp_baza_bezado/includes/function.php

http://htdocstest.googlecode.com/
PHP | 527 lines | 329 code | 80 blank | 118 comment | 72 complexity | b01e6a0ea2f96df532fb58810299136a MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, BSD-2-Clause
  1. <?php
  2. define('ERROR_UNKNOWN', 0);
  3. define('ERROR_DATABASE', 1);
  4. define('ERROR_SYSTEM', 2);
  5. define('ERROR_PHP', 3);
  6. /**
  7. * Zwraca nazwe sesji i identyfikator gotowy do doklejenia gdzie trzeba
  8. *
  9. * @param brak
  10. * @return nazwa i id sesji w postaci NAZWA=IDENTYFIKATOR (np. PHPSESSID=cb2a0328a69e83e51aaee651a94fb3b8)
  11. */
  12. function Session(){ return session_name().'='.session_id();}
  13. /**
  14. * Funkcja przekierowuje usera na adres przekazany w zmiennej $url
  15. *
  16. * @param $url - adres na ktory przekierowujemy
  17. * @return funkcja nic nie zwraca
  18. */
  19. function Redirect($url)
  20. {
  21. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  22. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  23. header("Cache-Control: no-store, no-cache, must-revalidate");
  24. header("Cache-Control: post-check=0, pre-check=0", false);
  25. header("Pragma: no-cache");
  26. header("Location: ".$url);
  27. exit();
  28. }
  29. /**
  30. * Wyswietla zawartosc zmiennej w tagach <pre></pre>
  31. *
  32. * @param $var - zmienna do wyswietlenia
  33. * @return funkcja nic nie zwraca
  34. */
  35. function Debug($var)
  36. {
  37. if( $_SERVER['REMOTE_ADDR'] == '127.0.0.1')
  38. {
  39. echo '<pre align=left style="text-align:left;">';print_r($var);echo '</pre>';
  40. }
  41. }
  42. /**
  43. * Zwraca tablice do generacji selecta 1/0
  44. */
  45. function pobierzTakNie()
  46. {
  47. global $lang;
  48. $ret = array( 0 => $lang[ 'nie' ], 1 => $lang[ 'tak' ] );
  49. return $ret;
  50. }
  51. /**
  52. * Zwraca tablice z nawigacja w postaci [1] [2] [3]
  53. *
  54. * @param integer $intWszystkich - ilosc wszystkich rekordow
  55. * @param integer $intNaStronie - ile pozycji na stronie
  56. * @param string $strLink - link do ktorego doklejamy wartosc parametru (np. $_SERVER['PHP_SELF'].'?page=produkty&start='
  57. * @return array - tablica z wynikami
  58. */
  59. function getNav( $intWszystkich, $intNaStronie, $strLink, $intGrupa=15 )
  60. {
  61. if( $intWszystkich==0 || $intNaStronie==0 )
  62. return;
  63. $iloscStron = ceil( $intWszystkich/ $intNaStronie );
  64. $aktualnaStrona = ($_GET['start']/$intNaStronie)+1;
  65. $tab = array();
  66. # od ktorej strony zaczynamy wyswietlanie listy stron
  67. $zacznijOd = ceil( ($aktualnaStrona-(($intGrupa-1)/2)-1) );
  68. if( $zacznijOd<0 )
  69. $zacznijOd = 0;
  70. else if( $aktualnaStrona+((($intGrupa-1)/2)-1)>$iloscStron )
  71. $zacznijOd = $iloscStron-($intGrupa-1);
  72. # wyswietlamy do strony...
  73. $zakonczNa = ($zacznijOd+$intGrupa);//debug($aktualnaStrona);
  74. if( $zakonczNa>$iloscStron )
  75. $zakonczNa = $iloscStron;
  76. for( $i=$zacznijOd; $i<$zakonczNa; $i++ )
  77. {
  78. $start = $i * $intNaStronie;
  79. if( $_GET['start']==$start )
  80. $aktualny = 1;
  81. if( $_GET['start']-$intNaStronie>0 )
  82. $poprzednia = $_GET['start']-$intNaStronie;
  83. else
  84. $poprzednia = 0;
  85. if( $_GET['start']+$intNaStronie<=$intWszystkich )
  86. $nastepna = $_GET['start']+$intNaStronie;
  87. else
  88. $nastepna = floor($intWszystkich/$intNaStronie)*$intNaStronie ;
  89. if( $i+1 > 0 )
  90. {
  91. $tab[] = array(
  92. 'link' => $strLink.$start,
  93. 'start' => $start,
  94. 'strona' => $i+1,
  95. 'aktualny' => $aktualny,
  96. 'poprzednia' => $strLink.$poprzednia,
  97. 'nastepna' => $strLink.$nastepna,
  98. 'iloscStron' => $iloscStron,
  99. 'aktualnaStrona' => $aktualnaStrona
  100. );
  101. }
  102. }
  103. return $tab;
  104. }
  105. /**
  106. *
  107. * @param integer $startItem - od ktorego elementu zaczynamy aktualna strone
  108. * @param integer $itemsQty - ilosc elementow w wyniku
  109. * @param integer $perPage - ilosc elementow na stronie
  110. * @param integer $pagesQty - ilosc linkow do stron (powinno byc parzyste)
  111. */
  112. function getNavBar( $startItem, $itemsQty, $perPage, $pagesQty )
  113. {
  114. $tabData = array();
  115. if( $pagesQty%2 == 0 )
  116. $pagesQty++;
  117. $tabData['itemsQty'] = $itemsQty;
  118. $tabData['pageCount'] = ceil( $itemsQty / $perPage );
  119. $tabData['currentPage'] = ( $startItem / $perPage )+1;
  120. $tabData['currentFrom'] = $startItem + 1;
  121. $tabData['currentTo'] = $startItem + $perPage <= $itemsQty ? $startItem + $perPage : $itemsQty;
  122. $tabData['nextPageStart'] = ( $startItem + $perPage <= $itemsQty ? $startItem + $perPage :( $tabData['pageCount'] - 1 ) * $perPage );
  123. $tabData['prevPageStart'] = ( $startItem - $perPage >= 0 ? $startItem - $perPage : 0 );
  124. $tabData['firstPageStart'] = 0;
  125. $tabData['lastPageStart'] = ( $tabData['pageCount'] - 1 ) * $perPage;
  126. $pageBegin = $tabData['currentPage']-floor($pagesQty/2)-1;
  127. if( $pageBegin < 0 )
  128. $pageBegin = 0;
  129. else if( $tabData['pageCount'] - $pageBegin < $pagesQty && $tabData['pageCount'] - $pagesQty >= 0 )
  130. $pageBegin = $tabData['pageCount'] - $pagesQty;
  131. $pageEnd = $pageBegin + $pagesQty;
  132. if( $pageEnd > $tabData['pageCount'] )
  133. $pageEnd = $tabData['pageCount'];
  134. for( $i = $pageBegin; $i < $pageEnd; $i++ )
  135. {
  136. $tabData['pagesData'][$i]['startItem'] = $i * $perPage;
  137. $tabData['pagesData'][$i]['pageNumber'] = $i+1;
  138. }
  139. return $tabData;
  140. }
  141. function getTime()
  142. {
  143. list($usec, $sec) = explode(" ",microtime());
  144. return ((float)$usec + (float)$sec);
  145. }
  146. function convert_to_utf8 ( $arrText, $strCharset )
  147. {
  148. if( !is_array($arrText) )
  149. die('$arrLang is not an array!');
  150. while( list($key, $val) = each($arrText) )
  151. {
  152. if( function_exists('iconv') )
  153. $arrText[ $key ] = iconv( $strCharset, 'UTF-8', $val );
  154. else if( function_exists('mb_convert_encoding') )
  155. $arrText[ $key ] = mb_convert_encoding($val, 'UTF-8', $strCharset);
  156. else
  157. $arrText[ $key ] = $val;
  158. }
  159. return $arrText;
  160. }
  161. function pobierzJezyki()
  162. {
  163. $ret = array('en' => 'en', 'de' => 'de', 'it' => 'it' );
  164. return $ret;
  165. }
  166. /**
  167. * Wstawia element na poczatek tablicy
  168. */
  169. function array_on_top( $array, $item )
  170. {
  171. $array = array_reverse($array , true);
  172. $array[] = $item;
  173. $array = array_reverse($array , true);
  174. return $array;
  175. }
  176. /**
  177. * Sprawdza poprawnosc daty
  178. */
  179. function dateCheck($date, $yearepsilon=5000) { // inputs format is "YYYY-MM-DD" ONLY !
  180. if (count($datebits=explode('-',$date))!=3) return false;
  181. $year = intval($datebits[0]);
  182. $month = intval($datebits[1]);
  183. $day = intval($datebits[2]);
  184. if ((abs($year-date('Y'))>$yearepsilon) || // year outside given range
  185. ($month<1) || ($month>12) || ($day<1) ||
  186. (($month==2) && ($day>28+(!($year%4))-(!($year%100))+(!($year%400)))) ||
  187. ($day>30+(($month>7)^($month&1)))) return false; // date out of range
  188. return checkdate($month,$day,$year );
  189. }
  190. /**
  191. * Sprawdza poprawnosc czasu hh:mm:ss
  192. */
  193. function timeCheck($time)
  194. {
  195. if (count($timebits=explode(':',$time))!=3) return false;
  196. $hours = intval($datebits[0]);
  197. $minutes = intval($datebits[1]);
  198. $seconds = intval($datebits[2]);
  199. if( $hours<0 || $hours>23 || $minutes<0 || $minutes>59 || $seconds<0 || $seconds>59 )
  200. return false;
  201. return true;
  202. }
  203. function getPDF($html)
  204. {
  205. $pdf=new HTML2FPDF();
  206. $pdf->AddFont('times', '', 'times.php');
  207. $pdf->AddFont('times', 'B', 'timesbd.php');
  208. $pdf->AddFont('times', 'I', 'timesi.php');
  209. $pdf->SetFont('times', '', 11);
  210. $pdf->AddPage();
  211. $pdf->WriteHTML($html);
  212. $pdf->Output();
  213. }
  214. /**
  215. * Czysci ciag lub tablice z potencjalnie niebezpiecznych znakow
  216. *
  217. * @param string|array $dirty
  218. * @return string|array
  219. */
  220. function clean_input($dirty)
  221. {
  222. //$input = get_magic_quotes_gpc() ? $dirty : addslashes($dirty);
  223. if( !is_array($dirty) )
  224. {
  225. $dirty = trim( strip_tags( htmlspecialchars($dirty, ENT_QUOTES), '' ) );
  226. }else if( is_array($dirty) )
  227. {
  228. foreach($dirty as $k=>$v)
  229. $dirty[ $k ] = trim( strip_tags( htmlspecialchars($v, ENT_QUOTES), '' ) );
  230. }
  231. return $dirty;
  232. }
  233. function is_valid_email($email) {
  234. $result = TRUE;
  235. if(!eregi("^[a-z0-9_-]+(\.[a-z0-9_-]+)*@[a-z0-9_-]+(\.[a-z0-9_-]+)*(\.[a-z]{2,4})$", $email)) {
  236. $result = FALSE;
  237. }
  238. return $result;
  239. }
  240. function filtr($filtr)
  241. {
  242. $filtr_out = '';
  243. if( !is_array($filtr) )
  244. return $filtr_out;
  245. while( list($key, $val)=each($filtr) )
  246. {
  247. if( $key!='mod' && $key!='sort' && $key!='order' && $key!='act' )
  248. $filtr_out.= '&filtr['.$key.']='.$val;
  249. }
  250. return $filtr_out;
  251. }
  252. function roundValuation($val)
  253. {
  254. if( $val == 0 ) return 0;
  255. if( $val == 100 ) return 100;
  256. $newVal = $val/10;
  257. $split = explode('.', $newVal);
  258. if( $split[1] == 0 )
  259. {
  260. $split[1] = 0;
  261. }else if( $split[1] <= 5 )
  262. {
  263. $split[1] = 5;
  264. }else{
  265. $split[1] = 0;
  266. $split[0]++;
  267. }
  268. return implode('.', $split) * 10;
  269. }
  270. /**
  271. * compares 2 arrays and return true if they has different values
  272. *
  273. * @param array $old
  274. * @param array $new
  275. * @return bool
  276. */
  277. function compareRec( $old, $new )
  278. {
  279. while( list($k,$v) = each($old) )
  280. {
  281. if( array_key_exists($k, $new) && $new[$k] != $old[$k] )
  282. {
  283. return true;
  284. }
  285. }
  286. return false;
  287. }
  288. function modifier_url($string)
  289. {
  290. $addr = strtr( html_entity_decode($string), array("š"=>"a","?"=>"c","?"=>"e","?"=>"l","?"=>"n","ó"=>"o","&#x153;"=>"s","?"=>"z","&#x;"=>"z","?"=>"a","?"=>"c","?"=>"e","?"=>"l","?"=>"n","Ó"=>"o","&#x152;"=>"s","?"=>"z","?"=>"z"," "=>"-","/"=>"-","("=>"-",")"=>"-","."=>"-",","=>"-",":"=>"-","ö"=>"o","ä"=>"a","ü"=>"u","ß"=>"ss","Ö"=>"o","Ä"=>"a","Ü"=>"u"));
  291. return strtolower( ereg_replace( '-+', '-', $addr ) );
  292. }
  293. function url($string)
  294. {
  295. return modifier_url($string);
  296. }
  297. function weekDayToTime($week, $year, $dayOfWeek = 1) {
  298. $dayOfWeekRef = date("w", mktime (0,0,0,1,4,$year));
  299. if ($dayOfWeekRef == 0) $dayOfWeekRef = 7;
  300. $resultTime = mktime(0,0,0,1,4,$year) + ((($week - 1) * 7 + ($dayOfWeek - $dayOfWeekRef)) * 86400);
  301. $resultTime = cleanTime($resultTime);
  302. return $resultTime;
  303. };
  304. function cleanTime($time) {
  305. $cleanTime = mktime(0,0,0,date("m", $time),date("d", $time),date("Y", $time));
  306. return $cleanTime;
  307. }
  308. /**
  309. * Zwraca tablice z data poczatku tygodnia oraz data konca tygodnia dla podanego numeru tygodnia 1-53
  310. *
  311. * @param int $week_number
  312. */
  313. function week( $week_number )
  314. {
  315. $start = weekDayToTime($week_number, 2008);
  316. $end = cleanTime(604800 + $start - 86400);
  317. return array( 'poczatek'=>date("Y-m-d", $start), 'koniec'=>date("Y-m-d", $end) );
  318. }
  319. /*!
  320. * Zamienia polskie znaiki na ich angielskie odpowiedniki
  321. */
  322. function strip_polish($string)
  323. {
  324. $addr = strtr( html_entity_decode($string), array("š"=>"a","?"=>"c","?"=>"e","?"=>"l","?"=>"n","ó"=>"o","&#x153;"=>"s","?"=>"z","&#x;"=>"z","?"=>"a","?"=>"c","?"=>"e","?"=>"l","?"=>"n","Ó"=>"o","&#x152;"=>"s","?"=>"z","?"=>"z"));
  325. return $addr;
  326. }
  327. /**
  328. * debug output for Excel in CSV format
  329. */
  330. function debug_csv($out){
  331. die( "<table border=1><tr><td>".strtr( $out, array(";"=>"&nbsp;<td>","\n"=>"<tr><td>"))."</table>" );
  332. }
  333. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  334. //FUNKCJE B??DÓW
  335. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  336. /**
  337. * newErrorHandler( intErrNo, strErrStr, strErrFile, strErrLine, strErrContext )
  338. * ======
  339. * funkcja generujaca komunikat o bledzie
  340. *
  341. * Lista parametrow:
  342. * intErrNo - numer bledu
  343. * strErrStr - opis bledu
  344. * strErrFile - nazwa pliku
  345. * strErrLine - numer lini, w ktorej wystapil blad
  346. * strErrContext - dodatkowe informacje
  347. *
  348. */
  349. function newErrorHandler( $intErrNo, $strErrStr, $strErrFile, $strErrLine, $strErrContext )
  350. {
  351. global $strGlobalError;
  352. $strGlobalError = $strErrStr;
  353. genErrorMsg( ERROR_PHP, $intErrNo . "-" . $strErrStr, $strErrFile, NULL, $strErrLine, $strErrContext );
  354. }
  355. /**
  356. * array genErrorMsg( intErrId, strError, strFile, strFunction, strLine, strDetails )
  357. * ======
  358. * funkcja generujaca komunikat o bledzie
  359. *
  360. * Lista parametrow:
  361. * intErrId - identyfikator rodzaju bledu
  362. * strError - opis bledu
  363. * strFile - nazwa pliku
  364. * strFunction - nazwa funkcji
  365. * strLine - numer lini, w ktorej wystapil blad
  366. * strDetails - dodatkowe informacje
  367. *
  368. */
  369. function genErrorMsg( $intErrId, $strError=NULL, $strFile=NULL, $strFunction=NULL, $strLine=NULL, $strDetails=NULL )
  370. {
  371. switch( $intErrId )
  372. {
  373. // blad bazy danych
  374. case ERROR_DATABASE :
  375. $arrError['errorNo'] = ERROR_DATABASE;
  376. $strErrMsg = "========================================\n";
  377. $strErrMsg .= "DATETIME : " . date( "Y-m-d H:i:s" ) . "\n";
  378. $strErrMsg .= "FILE : " . $strFile . "\n";
  379. $strErrMsg .= "FUNCTION : " . $strFunction . "\n";
  380. $strErrMsg .= "LINE : " . $strLine . "\n";
  381. $strErrMsg .= "ERROR : " . print_r($strError,true) . "\n";
  382. //$strErrMsg .= "GET : " . print_r($_GET,true) . "\n";
  383. //$strErrMsg .= "POST : " . print_r($_POST,true) . "\n";
  384. //$strErrMsg .= "SESSION : " . print_r($_SESSION,true) . "\n";
  385. // $strErrMsg .= "COOKIES : " . print_r($_COOKIE,true) . "\n";
  386. //$strErrMsg .= "FILES : " . print_r($_FILES,true) . "\n";
  387. if( isset( $strDetails ) && ( is_array($strDetails) || is_object($strDetails) ) )
  388. {
  389. $strErrMsg .= "DETAILS : " . print_r($strDetails, true) . "\n";
  390. }
  391. if( isset( $strDetails ) && !is_array($strDetails) )
  392. {
  393. $strErrMsg .= "DETAILS : " . $strDetails . "\n";
  394. }
  395. error_log( $strErrMsg, ERROR_PHP, 'niewiem');
  396. break;
  397. // blad ogolny aplikacji
  398. case ERROR_SYSTEM :
  399. $arrError['errorNo'] = $strError;
  400. break;
  401. // blad php
  402. case ERROR_PHP :
  403. $arrError['errorNo'] = ERROR_PHP;
  404. $strErrMsg = "========================================\n";
  405. $strErrMsg .= "DATETIME : " . date( "Y-m-d H:i:s" ) . "\n";
  406. $strErrMsg .= "FILE : " . $strFile . "\n";
  407. $strErrMsg .= "FUNCTION : " . $strFunction . "\n";
  408. $strErrMsg .= "LINE : " . $strLine . "\n";
  409. $strErrMsg .= "ERROR : " . print_r($strError,true) . "\n";
  410. //$strErrMsg .= "GET : " . print_r($_GET,true) . "\n";
  411. //$strErrMsg .= "POST : " . print_r($_POST,true) . "\n";
  412. //$strErrMsg .= "SESSION : " . print_r($_SESSION,true) . "\n";
  413. //$strErrMsg .= "COOKIES : " . print_r($_COOKIE,true) . "\n";
  414. // $strErrMsg .= "FILES : " . print_r($_FILES,true) . "\n";
  415. if( isset( $strDetails ) && ( is_array($strDetails) || is_object($strDetails) ) )
  416. {
  417. $strErrMsg .= "DETAILS : " . print_r($strDetails, true) . "\n";
  418. }
  419. if( isset( $strDetails ) && !is_array($strDetails) )
  420. {
  421. $strErrMsg .= "DETAILS : " . $strDetails . "\n";
  422. }
  423. $intErrNo = explode( "-", $strError );
  424. if( $intErrNo[0] != E_NOTICE && $intErrNo[0] != E_STRICT )
  425. {
  426. error_log( $strErrMsg, ERROR_PHP, _ERROR_LOG );
  427. }
  428. break;
  429. }
  430. $arrError['errorId'] = $intErrId;
  431. return false;
  432. }
  433. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  434. //MOJE FUNKCJE
  435. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  436. function __autoload($classname)
  437. {
  438. require_once("./includes/classes/class.$classname.php");
  439. }
  440. function dump($args)
  441. {
  442. $args = func_get_args();
  443. foreach($args as $arg) {
  444. Dumper::dump($arg);
  445. }
  446. }
  447. ?>