/1/enp_baza_bezado/includes/function.php
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
- <?php
- define('ERROR_UNKNOWN', 0);
- define('ERROR_DATABASE', 1);
- define('ERROR_SYSTEM', 2);
- define('ERROR_PHP', 3);
- /**
- * Zwraca nazwe sesji i identyfikator gotowy do doklejenia gdzie trzeba
- *
- * @param brak
- * @return nazwa i id sesji w postaci NAZWA=IDENTYFIKATOR (np. PHPSESSID=cb2a0328a69e83e51aaee651a94fb3b8)
- */
- function Session(){ return session_name().'='.session_id();}
- /**
- * Funkcja przekierowuje usera na adres przekazany w zmiennej $url
- *
- * @param $url - adres na ktory przekierowujemy
- * @return funkcja nic nie zwraca
- */
- function Redirect($url)
- {
- header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
- header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
- header("Cache-Control: no-store, no-cache, must-revalidate");
- header("Cache-Control: post-check=0, pre-check=0", false);
- header("Pragma: no-cache");
- header("Location: ".$url);
- exit();
- }
- /**
- * Wyswietla zawartosc zmiennej w tagach <pre></pre>
- *
- * @param $var - zmienna do wyswietlenia
- * @return funkcja nic nie zwraca
- */
- function Debug($var)
- {
- if( $_SERVER['REMOTE_ADDR'] == '127.0.0.1')
- {
- echo '<pre align=left style="text-align:left;">';print_r($var);echo '</pre>';
- }
- }
- /**
- * Zwraca tablice do generacji selecta 1/0
- */
- function pobierzTakNie()
- {
- global $lang;
- $ret = array( 0 => $lang[ 'nie' ], 1 => $lang[ 'tak' ] );
- return $ret;
- }
- /**
- * Zwraca tablice z nawigacja w postaci [1] [2] [3]
- *
- * @param integer $intWszystkich - ilosc wszystkich rekordow
- * @param integer $intNaStronie - ile pozycji na stronie
- * @param string $strLink - link do ktorego doklejamy wartosc parametru (np. $_SERVER['PHP_SELF'].'?page=produkty&start='
- * @return array - tablica z wynikami
- */
- function getNav( $intWszystkich, $intNaStronie, $strLink, $intGrupa=15 )
- {
- if( $intWszystkich==0 || $intNaStronie==0 )
- return;
- $iloscStron = ceil( $intWszystkich/ $intNaStronie );
- $aktualnaStrona = ($_GET['start']/$intNaStronie)+1;
- $tab = array();
- # od ktorej strony zaczynamy wyswietlanie listy stron
- $zacznijOd = ceil( ($aktualnaStrona-(($intGrupa-1)/2)-1) );
- if( $zacznijOd<0 )
- $zacznijOd = 0;
- else if( $aktualnaStrona+((($intGrupa-1)/2)-1)>$iloscStron )
- $zacznijOd = $iloscStron-($intGrupa-1);
- # wyswietlamy do strony...
- $zakonczNa = ($zacznijOd+$intGrupa);//debug($aktualnaStrona);
- if( $zakonczNa>$iloscStron )
- $zakonczNa = $iloscStron;
- for( $i=$zacznijOd; $i<$zakonczNa; $i++ )
- {
- $start = $i * $intNaStronie;
- if( $_GET['start']==$start )
- $aktualny = 1;
- if( $_GET['start']-$intNaStronie>0 )
- $poprzednia = $_GET['start']-$intNaStronie;
- else
- $poprzednia = 0;
- if( $_GET['start']+$intNaStronie<=$intWszystkich )
- $nastepna = $_GET['start']+$intNaStronie;
- else
- $nastepna = floor($intWszystkich/$intNaStronie)*$intNaStronie ;
- if( $i+1 > 0 )
- {
- $tab[] = array(
- 'link' => $strLink.$start,
- 'start' => $start,
- 'strona' => $i+1,
- 'aktualny' => $aktualny,
- 'poprzednia' => $strLink.$poprzednia,
- 'nastepna' => $strLink.$nastepna,
- 'iloscStron' => $iloscStron,
- 'aktualnaStrona' => $aktualnaStrona
- );
- }
- }
- return $tab;
- }
- /**
- *
- * @param integer $startItem - od ktorego elementu zaczynamy aktualna strone
- * @param integer $itemsQty - ilosc elementow w wyniku
- * @param integer $perPage - ilosc elementow na stronie
- * @param integer $pagesQty - ilosc linkow do stron (powinno byc parzyste)
- */
- function getNavBar( $startItem, $itemsQty, $perPage, $pagesQty )
- {
- $tabData = array();
- if( $pagesQty%2 == 0 )
- $pagesQty++;
- $tabData['itemsQty'] = $itemsQty;
- $tabData['pageCount'] = ceil( $itemsQty / $perPage );
- $tabData['currentPage'] = ( $startItem / $perPage )+1;
- $tabData['currentFrom'] = $startItem + 1;
- $tabData['currentTo'] = $startItem + $perPage <= $itemsQty ? $startItem + $perPage : $itemsQty;
- $tabData['nextPageStart'] = ( $startItem + $perPage <= $itemsQty ? $startItem + $perPage :( $tabData['pageCount'] - 1 ) * $perPage );
- $tabData['prevPageStart'] = ( $startItem - $perPage >= 0 ? $startItem - $perPage : 0 );
- $tabData['firstPageStart'] = 0;
- $tabData['lastPageStart'] = ( $tabData['pageCount'] - 1 ) * $perPage;
- $pageBegin = $tabData['currentPage']-floor($pagesQty/2)-1;
- if( $pageBegin < 0 )
- $pageBegin = 0;
- else if( $tabData['pageCount'] - $pageBegin < $pagesQty && $tabData['pageCount'] - $pagesQty >= 0 )
- $pageBegin = $tabData['pageCount'] - $pagesQty;
- $pageEnd = $pageBegin + $pagesQty;
- if( $pageEnd > $tabData['pageCount'] )
- $pageEnd = $tabData['pageCount'];
- for( $i = $pageBegin; $i < $pageEnd; $i++ )
- {
- $tabData['pagesData'][$i]['startItem'] = $i * $perPage;
- $tabData['pagesData'][$i]['pageNumber'] = $i+1;
- }
- return $tabData;
- }
- function getTime()
- {
- list($usec, $sec) = explode(" ",microtime());
- return ((float)$usec + (float)$sec);
- }
- function convert_to_utf8 ( $arrText, $strCharset )
- {
- if( !is_array($arrText) )
- die('$arrLang is not an array!');
- while( list($key, $val) = each($arrText) )
- {
- if( function_exists('iconv') )
- $arrText[ $key ] = iconv( $strCharset, 'UTF-8', $val );
- else if( function_exists('mb_convert_encoding') )
- $arrText[ $key ] = mb_convert_encoding($val, 'UTF-8', $strCharset);
- else
- $arrText[ $key ] = $val;
- }
- return $arrText;
- }
- function pobierzJezyki()
- {
- $ret = array('en' => 'en', 'de' => 'de', 'it' => 'it' );
- return $ret;
- }
- /**
- * Wstawia element na poczatek tablicy
- */
- function array_on_top( $array, $item )
- {
- $array = array_reverse($array , true);
- $array[] = $item;
- $array = array_reverse($array , true);
- return $array;
- }
- /**
- * Sprawdza poprawnosc daty
- */
- function dateCheck($date, $yearepsilon=5000) { // inputs format is "YYYY-MM-DD" ONLY !
- if (count($datebits=explode('-',$date))!=3) return false;
- $year = intval($datebits[0]);
- $month = intval($datebits[1]);
- $day = intval($datebits[2]);
- if ((abs($year-date('Y'))>$yearepsilon) || // year outside given range
- ($month<1) || ($month>12) || ($day<1) ||
- (($month==2) && ($day>28+(!($year%4))-(!($year%100))+(!($year%400)))) ||
- ($day>30+(($month>7)^($month&1)))) return false; // date out of range
- return checkdate($month,$day,$year );
- }
- /**
- * Sprawdza poprawnosc czasu hh:mm:ss
- */
- function timeCheck($time)
- {
- if (count($timebits=explode(':',$time))!=3) return false;
- $hours = intval($datebits[0]);
- $minutes = intval($datebits[1]);
- $seconds = intval($datebits[2]);
- if( $hours<0 || $hours>23 || $minutes<0 || $minutes>59 || $seconds<0 || $seconds>59 )
- return false;
- return true;
- }
- function getPDF($html)
- {
- $pdf=new HTML2FPDF();
- $pdf->AddFont('times', '', 'times.php');
- $pdf->AddFont('times', 'B', 'timesbd.php');
- $pdf->AddFont('times', 'I', 'timesi.php');
- $pdf->SetFont('times', '', 11);
- $pdf->AddPage();
- $pdf->WriteHTML($html);
- $pdf->Output();
- }
- /**
- * Czysci ciag lub tablice z potencjalnie niebezpiecznych znakow
- *
- * @param string|array $dirty
- * @return string|array
- */
- function clean_input($dirty)
- {
- //$input = get_magic_quotes_gpc() ? $dirty : addslashes($dirty);
- if( !is_array($dirty) )
- {
- $dirty = trim( strip_tags( htmlspecialchars($dirty, ENT_QUOTES), '' ) );
- }else if( is_array($dirty) )
- {
- foreach($dirty as $k=>$v)
- $dirty[ $k ] = trim( strip_tags( htmlspecialchars($v, ENT_QUOTES), '' ) );
- }
- return $dirty;
- }
- function is_valid_email($email) {
- $result = TRUE;
- if(!eregi("^[a-z0-9_-]+(\.[a-z0-9_-]+)*@[a-z0-9_-]+(\.[a-z0-9_-]+)*(\.[a-z]{2,4})$", $email)) {
- $result = FALSE;
- }
- return $result;
- }
- function filtr($filtr)
- {
- $filtr_out = '';
- if( !is_array($filtr) )
- return $filtr_out;
- while( list($key, $val)=each($filtr) )
- {
- if( $key!='mod' && $key!='sort' && $key!='order' && $key!='act' )
- $filtr_out.= '&filtr['.$key.']='.$val;
- }
- return $filtr_out;
- }
- function roundValuation($val)
- {
- if( $val == 0 ) return 0;
- if( $val == 100 ) return 100;
- $newVal = $val/10;
- $split = explode('.', $newVal);
- if( $split[1] == 0 )
- {
- $split[1] = 0;
- }else if( $split[1] <= 5 )
- {
- $split[1] = 5;
- }else{
- $split[1] = 0;
- $split[0]++;
- }
- return implode('.', $split) * 10;
- }
- /**
- * compares 2 arrays and return true if they has different values
- *
- * @param array $old
- * @param array $new
- * @return bool
- */
- function compareRec( $old, $new )
- {
- while( list($k,$v) = each($old) )
- {
- if( array_key_exists($k, $new) && $new[$k] != $old[$k] )
- {
- return true;
- }
- }
- return false;
- }
- function modifier_url($string)
- {
- $addr = strtr( html_entity_decode($string), array(""=>"a","?"=>"c","?"=>"e","?"=>"l","?"=>"n","ó"=>"o","œ"=>"s","?"=>"z","&#x;"=>"z","?"=>"a","?"=>"c","?"=>"e","?"=>"l","?"=>"n","Ó"=>"o","Œ"=>"s","?"=>"z","?"=>"z"," "=>"-","/"=>"-","("=>"-",")"=>"-","."=>"-",","=>"-",":"=>"-","ö"=>"o","ä"=>"a","ü"=>"u","ß"=>"ss","Ö"=>"o","Ä"=>"a","Ü"=>"u"));
- return strtolower( ereg_replace( '-+', '-', $addr ) );
- }
- function url($string)
- {
- return modifier_url($string);
- }
- function weekDayToTime($week, $year, $dayOfWeek = 1) {
- $dayOfWeekRef = date("w", mktime (0,0,0,1,4,$year));
- if ($dayOfWeekRef == 0) $dayOfWeekRef = 7;
- $resultTime = mktime(0,0,0,1,4,$year) + ((($week - 1) * 7 + ($dayOfWeek - $dayOfWeekRef)) * 86400);
- $resultTime = cleanTime($resultTime);
- return $resultTime;
- };
- function cleanTime($time) {
- $cleanTime = mktime(0,0,0,date("m", $time),date("d", $time),date("Y", $time));
- return $cleanTime;
- }
- /**
- * Zwraca tablice z data poczatku tygodnia oraz data konca tygodnia dla podanego numeru tygodnia 1-53
- *
- * @param int $week_number
- */
- function week( $week_number )
- {
- $start = weekDayToTime($week_number, 2008);
- $end = cleanTime(604800 + $start - 86400);
- return array( 'poczatek'=>date("Y-m-d", $start), 'koniec'=>date("Y-m-d", $end) );
- }
- /*!
- * Zamienia polskie znaiki na ich angielskie odpowiedniki
- */
- function strip_polish($string)
- {
- $addr = strtr( html_entity_decode($string), array(""=>"a","?"=>"c","?"=>"e","?"=>"l","?"=>"n","ó"=>"o","œ"=>"s","?"=>"z","&#x;"=>"z","?"=>"a","?"=>"c","?"=>"e","?"=>"l","?"=>"n","Ó"=>"o","Œ"=>"s","?"=>"z","?"=>"z"));
- return $addr;
- }
- /**
- * debug output for Excel in CSV format
- */
- function debug_csv($out){
- die( "<table border=1><tr><td>".strtr( $out, array(";"=>" <td>","\n"=>"<tr><td>"))."</table>" );
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //FUNKCJE B??DÓW
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /**
- * newErrorHandler( intErrNo, strErrStr, strErrFile, strErrLine, strErrContext )
- * ======
- * funkcja generujaca komunikat o bledzie
- *
- * Lista parametrow:
- * intErrNo - numer bledu
- * strErrStr - opis bledu
- * strErrFile - nazwa pliku
- * strErrLine - numer lini, w ktorej wystapil blad
- * strErrContext - dodatkowe informacje
- *
- */
- function newErrorHandler( $intErrNo, $strErrStr, $strErrFile, $strErrLine, $strErrContext )
- {
- global $strGlobalError;
- $strGlobalError = $strErrStr;
- genErrorMsg( ERROR_PHP, $intErrNo . "-" . $strErrStr, $strErrFile, NULL, $strErrLine, $strErrContext );
- }
- /**
- * array genErrorMsg( intErrId, strError, strFile, strFunction, strLine, strDetails )
- * ======
- * funkcja generujaca komunikat o bledzie
- *
- * Lista parametrow:
- * intErrId - identyfikator rodzaju bledu
- * strError - opis bledu
- * strFile - nazwa pliku
- * strFunction - nazwa funkcji
- * strLine - numer lini, w ktorej wystapil blad
- * strDetails - dodatkowe informacje
- *
- */
- function genErrorMsg( $intErrId, $strError=NULL, $strFile=NULL, $strFunction=NULL, $strLine=NULL, $strDetails=NULL )
- {
- switch( $intErrId )
- {
- // blad bazy danych
- case ERROR_DATABASE :
- $arrError['errorNo'] = ERROR_DATABASE;
- $strErrMsg = "========================================\n";
- $strErrMsg .= "DATETIME : " . date( "Y-m-d H:i:s" ) . "\n";
- $strErrMsg .= "FILE : " . $strFile . "\n";
- $strErrMsg .= "FUNCTION : " . $strFunction . "\n";
- $strErrMsg .= "LINE : " . $strLine . "\n";
- $strErrMsg .= "ERROR : " . print_r($strError,true) . "\n";
- //$strErrMsg .= "GET : " . print_r($_GET,true) . "\n";
- //$strErrMsg .= "POST : " . print_r($_POST,true) . "\n";
- //$strErrMsg .= "SESSION : " . print_r($_SESSION,true) . "\n";
- // $strErrMsg .= "COOKIES : " . print_r($_COOKIE,true) . "\n";
- //$strErrMsg .= "FILES : " . print_r($_FILES,true) . "\n";
- if( isset( $strDetails ) && ( is_array($strDetails) || is_object($strDetails) ) )
- {
- $strErrMsg .= "DETAILS : " . print_r($strDetails, true) . "\n";
- }
- if( isset( $strDetails ) && !is_array($strDetails) )
- {
- $strErrMsg .= "DETAILS : " . $strDetails . "\n";
- }
- error_log( $strErrMsg, ERROR_PHP, 'niewiem');
- break;
- // blad ogolny aplikacji
- case ERROR_SYSTEM :
- $arrError['errorNo'] = $strError;
- break;
- // blad php
- case ERROR_PHP :
- $arrError['errorNo'] = ERROR_PHP;
- $strErrMsg = "========================================\n";
- $strErrMsg .= "DATETIME : " . date( "Y-m-d H:i:s" ) . "\n";
- $strErrMsg .= "FILE : " . $strFile . "\n";
- $strErrMsg .= "FUNCTION : " . $strFunction . "\n";
- $strErrMsg .= "LINE : " . $strLine . "\n";
- $strErrMsg .= "ERROR : " . print_r($strError,true) . "\n";
- //$strErrMsg .= "GET : " . print_r($_GET,true) . "\n";
- //$strErrMsg .= "POST : " . print_r($_POST,true) . "\n";
- //$strErrMsg .= "SESSION : " . print_r($_SESSION,true) . "\n";
- //$strErrMsg .= "COOKIES : " . print_r($_COOKIE,true) . "\n";
- // $strErrMsg .= "FILES : " . print_r($_FILES,true) . "\n";
- if( isset( $strDetails ) && ( is_array($strDetails) || is_object($strDetails) ) )
- {
- $strErrMsg .= "DETAILS : " . print_r($strDetails, true) . "\n";
- }
- if( isset( $strDetails ) && !is_array($strDetails) )
- {
- $strErrMsg .= "DETAILS : " . $strDetails . "\n";
- }
- $intErrNo = explode( "-", $strError );
- if( $intErrNo[0] != E_NOTICE && $intErrNo[0] != E_STRICT )
- {
- error_log( $strErrMsg, ERROR_PHP, _ERROR_LOG );
- }
- break;
- }
- $arrError['errorId'] = $intErrId;
- return false;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //MOJE FUNKCJE
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- function __autoload($classname)
- {
- require_once("./includes/classes/class.$classname.php");
- }
- function dump($args)
- {
- $args = func_get_args();
- foreach($args as $arg) {
- Dumper::dump($arg);
- }
- }
- ?>