Jump to content

Funciones PHP para tu web


Ryen

Recommended Posts

Hola gente, les traigo este script de funciones muy util para quellos que esten programando php y quieran ahorrrar codigo.


El script no es mio pero es el que uso yo.


 


Tampoo explicare cada funcion, es simple fijarse y saber que hace cada 1.


 



<?PHP

function calcPages($gesEin,$aktSeite,$eSeite) {
$output = array();
$esQuote = ceil(($gesEin/$eSeite));
if($aktSeite==0) {$aktSeite=1;}
$startS = ($aktSeite*$eSeite)-$eSeite;
$output[0]=$esQuote;
$output[1]=$startS;
return $output;
}

function checkAnum($wert) {
$checkit = preg_match("/^[a-zA-Z0-9]+$/",$wert);
if($checkit) {
return true;
}
else {
return false;
}
}

function checkIP($wert) {
$checkit = preg_match("/^[0-9\*]{1,3}+\.[0-9\*]{1,3}+\.[0-9\*]{1,3}+\.[0-9\*]{1,3}+$/",$wert);
if($checkit) {
return true;
}
else {
return false;
}
}

function checkName($wert) {
$checkit = preg_match("/^[a-zA-Z0-9[:space:]]+$/",$wert);
if($checkit) {
return true;
}
else {
return false;
}
}

function checkVoucher($wert) {
$checkit = preg_match("/^[0-9]+[0-9\-]+[0-9]+$/",$wert);
$wert = str_replace('-','',$wert);
if($checkit && strlen($wert)>=16 && strlen($wert)<=25) {
return true;
}
else {
return false;
}
}

function checkPwd($wert) {
$checkit = preg_match("/^[a-zA-Z0-9[:space:]]+$/",$wert);
if($checkit) {
return true;
}
else {
return false;
}
}

function checkMail($string) {
if(preg_match("/^[a-zA-Z0-9\._-]+@[a-zA-Z0-9\.-]+\.[a-zA-Z]{2,4}$/", $string)) {
return true;
}
else { return false; }
}

function checkInt($wert) {
$checkit = preg_match("/^[0-9]+$/",$wert);
if($checkit) {
return true;
}
else {
return false;
}
}

function checkRate($wert) {
//if(checkInt($wert) && $wert>0) {
if(preg_match("/^[1-9]+\.[0-9]+$/",$wert) || $wert>0) {
return true;
}
else {
return false;
}
}

function compareItems($inputID) {

$input = "./archives/items.txt";

$getFile = file($input);
$getZeilen = count($getFile);

for($x=0;$x<$getZeilen;$x++) {

$aktZeile = $getFile[$x];
$splitZeile = explode('|||',$aktZeile);
$startWert = $splitZeile[0]+0;
$endWert = $splitZeile[1]+0;
if($inputID<=$endWert AND $inputID>=$startWert) {
$maxStufe = $endWert-$startWert;
$stufe=$inputID-$startWert;
$aus=array();
$aus['item'] = $splitZeile[2];
$aus['groesse'] = trim($splitZeile[3]);
$aus['stufe'] = ($maxStufe==0) ? NULL : $stufe;
$aus['maxStufe'] = $maxStufe;
$aus['vnum']=$startWert;
return $aus;
}
}
$nItem=array();
$nItem['groesse']=1;
$nItem['item']=$inputID;
$nItem['stufe']=NULL;
$nItem['maxStufe']=0;
$nItem['vnum']=$inputID;
return $nItem;

}

function checkPos($inID) {
global $sqlServ;
$sqlCmd="SELECT pos,vnum FROM player.item WHERE owner_id='".$inID."' AND window='SAFEBOX'";
$sqlQry=mysql_query($sqlCmd,$sqlServ);

$lagerPos=array();
while($getLager=mysql_fetch_object($sqlQry)) {
$maxGr = compareItems($getLager->vnum);
$aktPos=$getLager->pos;
for($i=1;$i<=$maxGr['groesse'];$i++) {
$lagerPos[$aktPos]=$getLager->vnum;
$aktPos = $aktPos + 5;
}
}

$sqlCmd="SELECT pos,vnum FROM player.item WHERE owner_id='".$inID."' AND window='MALL'";
$sqlQry=mysql_query($sqlCmd,$sqlServ);

$islPos=array();
while($getISL=mysql_fetch_object($sqlQry)) {
$maxGr = compareItems($getISL->vnum);
$aktPos=$getISL->pos;
for($i=1;$i<=$maxGr['groesse'];$i++) {
$islPos[$aktPos]=$getISL->vnum;
$aktPos = $aktPos + 5;
}
}

$returnArray['lager']=$lagerPos;
$returnArray['islager']=$islPos;

return $returnArray;
}

function listLager($inArray,$typus=0) {
if($typus==1) {
$ueS='IS-Lager';
$cInput=$inArray['islager'];
}
else {
$ueS='Lager';
$cInput=$inArray['lager'];
}
echo'<table class="lager">';
echo'<tr>
<th class="topLine" colspan="5">'.$ueS.'</th>
</tr>';
for($i=0;$i<45;$i++) {
if($i==0) { echo'<tr>'; }
if(isset($cInput[$i])) {
$zF="tdunkel";
$getItem = compareItems($cInput[$i]);
$iStufe = (checkInt($getItem['stufe'])) ? "+".$getItem['stufe'] : '';
$lineout="<a title=\"".$getItem['item'].$iStufe."\">".$i."</a>";
}
else { $zF="thell"; $lineout=$i; }
echo '<td class="'.$zF.'">'.$lineout.'</td>';
if($i!=0 && ($i+1)%5==0) {
echo'</tr>';
if(($i+1)!=45) { echo '<tr>'; }
}
}
echo'</table>';

}

function findPos($belegtePos,$iGroesse) {
$possPos=array();
for($i=0;$i<45;$i++) {

if(empty($belegtePos[$i])) {

for($y=0;$y<$iGroesse;$y++) {

$aktPos=$i+($y*5);
$thisFits = true;
if(!isset($belegtePos[$aktPos]) && $aktPos<45) {
$thisFits = true;
}
else {
$thisFits = false;
break;
}

}
if($thisFits) { $possPos[]=$i; }

}

}
return $possPos;
}

function checkUploadSize($dateiGr,$maxGr) {
$maxGrByte = $maxGr*1024;
if($dateiGr<=$maxGrByte) {
return true;
}
else {
return false;
}
}

function imageCheckSize($datei,$xMax,$yMax) {
$erlaubteBilder = array(
'image/jpeg' => '.jpg',
'image/gif' => '.gif'
);
$getGr=getimagesize($datei);

if($getGr[0]<=$xMax && $getGr[1]<=$yMax && !empty($erlaubteBilder[$getGr['mime']]) ) {
return $erlaubteBilder[$getGr['mime']];
}
else {
return false;
}
}

function imageUpload($dateiIn,$maxDateiGr,$maxDateix,$maxDateiy) {
if($_FILES[$dateiIn]['size']>0) {
if(checkUploadSize($_FILES[$dateiIn]['size'],$maxDateiGr)) {
if($dateiEndung = imageCheckSize($_FILES[$dateiIn]['tmp_name'],$maxDateix,$maxDateiy)) {
$md5datei = md5_file($_FILES[$dateiIn]['tmp_name']).'_'.rand(10000,99999);
if(move_uploaded_file($_FILES[$dateiIn]['tmp_name'],'./is_img/'.$md5datei.$dateiEndung)) {
return $md5datei.$dateiEndung;
}
else { return false; }
}
else { return false; }
}
else { return false; }
}
else { return false; }
}

function getMapByIndex($mapIndex) {
if(checkInt($mapIndex)) {
$mapArchive = "./archives/maps.txt";
$mapContents = file($mapArchive);
$returnArray=false;
foreach($mapContents AS $aktMap) {

$splitZeile = explode("|||",$aktMap);
if(trim($splitZeile[0])==$mapIndex) {
$returnArray=array();
$returnArray=$splitZeile;
}

}

if(is_array($returnArray)) {
return $returnArray;
}
else {
return false;
}

}
else {
return false;
}
}

function listItems($iid='') {
echo'<select name="itemtyp">';
echo'<option value="">-------</option>';
$itemDatei = "./archives/items.txt";
$itemListe = file($itemDatei);
$dateiLaenge = count($itemListe);
for($i=0;$i<$dateiLaenge;$i++) {
$splitZeile = explode("|||",$itemListe[$i]);
$selected= ($iid>=$splitZeile[0] && $iid<=$splitZeile[1]) ? 'selected=selected':''; // to go
echo'<option '.$selected.' value="'.$splitZeile[0].'">'.$splitZeile[2].'</option>';
}
echo'</select>';
}

function listMaps() {
$mDatei = file('./archives/maps.txt');

echo'<ul class="menue">';

foreach($mDatei AS $aktMap) {

$splitZeile = explode("|||",$aktMap);
if(isset($splitZeile[6])) {
echo '<li><a href="index.php?s=admin&a=map&mapindex='.$splitZeile[0].'">'.$splitZeile[6].'</a></li>';
}

}

echo'</ul>';

}

function getDatum($timeStamp=0)
{
return date("d.m.Y",$timeStamp);
}

function getZeit($timeStamp=0)
{
return date("H:i",$timeStamp);
}

function x_nl2br($input)
{
$input = str_replace("\n",'<br/>',$input);
return $input;
}

function listNewsKat($mIndex=-1)
{
global $newsKategorien;
echo'<select name="kategorie">';
foreach($newsKategorien AS $katIndex => $katName)
{
$selected = ($mIndex==$katIndex) ? 'selected="selected"' : '';
echo'<option '.$selected.' value="'.$katIndex.'">'.$katName.'</option>';
}
echo'</select>';
}

function checkBetween($input,$lower=0,$upper=0)
{
if($input>=$lower && $input<=$upper) { return true; }
else { return false; }
}
?>

Tambien os dejo una API de proteccion



<?php
class m2server_api {
/**
* useragent
* @var string
*/
protected $_useragent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)';
/**
* follow location
* @var boolean
*/
protected $_followlocation;
/**
* timeout
* @var int
*/
protected $_timeout;
/**
* max. redirects
* @var int
*/
protected $_maxRedirects;
/**
* file for cookies
* @var string
*/
protected $_cookieFileLocation = './cookie.txt';
/**
* activate post
* @var boolean
*/
protected $_post;
/**
* post data
* @var string
*/
protected $_postFields;
/**
* refered link
* @var string
*/
protected $_referer ="http://www.google.com";
/**
* webpage returned content
* @var string
*/
protected $_webpage;
/**
* include header
* @var boolean
*/
protected $_includeHeader;
/**
* body request
* @var boolean
*/
protected $_noBody;
/**
* curl status
* @var int
*/
protected $_status;
/**
* transfer as bin
* @var boolean
*/
protected $_binaryTransfer;
/**
* auth status
* @var int
*/
public $authentication = 0;
/**
* auth name for login
* @var string
*/
public $auth_name = '';
/**
* auth pass for login
* @var string
*/
public $auth_pass = '';
/**
* API key
* @var string
*/
public $key = '';
/**
* API return type
* @var string
*/
public $type = '';

/**
* constructor
* @param boolean $followlocation
* @param int $timeOut
* @param int $maxRedirecs
* @param boolean $binaryTransfer
* @param boolean $includeHeader
* @param boolean $noBody
*/
public function __construct($followlocation = true,$timeOut = 30,$maxRedirecs = 4,$binaryTransfer = false,$includeHeader = false,$noBody = false)
{
$this->_followlocation = $followlocation;
$this->_timeout = $timeOut;
$this->_maxRedirects = $maxRedirecs;
$this->_noBody = $noBody;
$this->_includeHeader = $includeHeader;
$this->_binaryTransfer = $binaryTransfer;
$this->_url = "http://m2-server.info/api.php";
$this->type = 'json';

$this->_cookieFileLocation = dirname(__FILE__).'/cookie.txt';

}
/**
* sends and get data via php5_curl
* @param string $request
*/
public function createCurl($request="")
{
$s = curl_init();
curl_setopt($s,CURLOPT_URL,$this->_url.'?key='.$this->key.'&request='.$request);
curl_setopt($s,CURLOPT_HTTPHEADER,array('Expect:'));
curl_setopt($s,CURLOPT_TIMEOUT,$this->_timeout);
curl_setopt($s,CURLOPT_MAXREDIRS,$this->_maxRedirects);
curl_setopt($s,CURLOPT_RETURNTRANSFER,true);
curl_setopt($s,CURLOPT_FOLLOWLOCATION,$this->_followlocation);
curl_setopt($s,CURLOPT_COOKIEJAR,$this->_cookieFileLocation);
curl_setopt($s,CURLOPT_COOKIEFILE,$this->_cookieFileLocation);
if($this->authentication == 1) curl_setopt($s, CURLOPT_USERPWD, $this->auth_name.':'.$this->auth_pass);
if($this->_post){
curl_setopt($s,CURLOPT_POST,true);
curl_setopt($s,CURLOPT_POSTFIELDS,$this->_postFields);
}
if($this->_includeHeader) curl_setopt($s,CURLOPT_HEADER,true);
if($this->_noBody) curl_setopt($s,CURLOPT_NOBODY,true);
curl_setopt($s,CURLOPT_USERAGENT,$this->_useragent);
curl_setopt($s,CURLOPT_REFERER,$this->_referer);

$this->_webpage = curl_exec($s);
$this->_status = curl_getinfo($s,CURLINFO_HTTP_CODE);
curl_close($s);
}
/**
* returns status of request
* @return int
*/
public function status() { return $this->_status; }
/**
* returns returned string of curl
* @return string
*/
public function __tostring(){ return json_decode($this->_webpage); }
/**
* setting auth usage
* @param boolean $use
*/
public function useAuth($use){ $this->authentication = 0; if($use == true) $this->authentication = 1; }
/**
* setting API Key
* @param string $key
*/
public function setKey($key){ $this->key = $key; }
/**
* setting type
* @param string $type
*/
public function setType($type){ $this->type = $type; }
/**
* setting auth name
* @param string $name
*/
public function setName($name){ $this->auth_name = $name; }
/**
* setting auth password
* @param string $pass
*/
public function setPass($pass){ $this->auth_pass = $pass; }
/**
* setting refer link
* @param string $referer
*/
public function setReferer($referer){ $this->_referer = $referer; }
/**
* setting cookie file path
* @param string $path
*/
public function setCookiFileLocation($path) { $this->_cookieFileLocation = $path; }
/**
* setting data to post
* @param string $postFields
*/
public function setPost($postFields) { $this->_post = true; $this->_postFields = $postFields; }
/**
* setting user agent
* @param string $userAgent
*/
public function setUserAgent($userAgent) { $this->_useragent = $userAgent; }
}
?>  

 


Saludos


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...