newsblog
Thu, 30 Mar 2017, 18:33 *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Hi gusy!
 
   Home   Help Search Login Register  
Pages: [1] 2 3   Go Down
  Print  
Author Topic: PHP SNIPPETS  (Read 10127 times)
0 Members and 1 Guest are viewing this topic.
punkt
personal space invader
Baistruc

Offline Offline

Gender: Male
Posts: 1 458



View Profile
« on: Fri, 08 Jun 2007, 14:46 »

MOTOR DE TEMPLATES

Scop: de a separa codul php de cel html. Might be useful grin

FISIERE NECESARE:
index.php
template.inc.php
themes/test/index.theme.php

index.php
Code:
<?php
global $SETTINGS;
include 
"template.inc.php";
$SETTINGS[&#39;theme&#39;]=&#39;test&#39;;

$vars = array(
&#39;middle&#39; => &#39;HELLO WORLD&#39;,
&#39;left&#39; => "[s f=arial s=30px c=#00F]test[/s]"
);

$vars2 = array(
&#39;middle&#39; => &#39;<br />FOO BAR&#39;,
&#39;left&#39; => "<br />[s f=arial s=30px c=#F00]test2[/s]"
);

$TPL = new cTemplate();

$TPL->LOAD();
$TPL->ADD($vars);
$TPL->ADD($vars2);
$TPL->DRAW();
?>


template.inc.php
Code:
<?php
class cTemplate{
var $T_OUTPUT="";
var $ERR="";
function LOAD($T_NAME="null"){
if($T_NAME=="null"){
$thisname basename($_SERVER[&#39;SCRIPT_FILENAME&#39;]);
$thisname explode(".",$thisname);
$thisname $thisname[0];
$T_NAME $thisname;
}
global $SETTINGS;
$T_THEMEDIR "themes/".$SETTINGS[&#39;theme&#39;]."/";
$T_THEMEFILE $T_THEMEDIR.$T_NAME.".theme.php";
if(!file_exists($T_THEMEFILE)){
?>

                <span style="border: 1px solid black; background-color:#CC0000; color:#FFF;
                font-family:Verdana, Arial, Helvetica, sans-serif; font-size:9px; padding:2px;">
                <strong>TEMPLATE ERROR:</strong> Template file doesn't exist!(<?=$T_THEMEFILE?>)
                </span>
               
                <?php
}else{
$T_FILE file_get_contents($T_THEMEFILE);
$this->T_OUTPUT $T_FILE;
}
}

function ADD($T_VARS){
foreach($T_VARS as $k=> $v){
$this->T_OUTPUT str_replace("{{$k}}"$v."{{$k}}"$this->T_OUTPUT);
}
}

function STORE(){
$T_OUTPUT $this->T_OUTPUT;
$T_OUTPUT preg_replace(&#39;/\{(.*?)\}/is&#39;,"", $T_OUTPUT);
return $T_OUTPUT;

}

function DRAW(){
$T_OUTPUT $this->T_OUTPUT;
$T_OUTPUT preg_replace(&#39;/\[table](.*?)\[\/table\]/is&#39;, &#39;$1&#39;,$T_OUTPUT);
$T_OUTPUT preg_replace(&#39;/\{(.*?)\}/is&#39;,"&nbsp;", $T_OUTPUT);

$tags = array(
//TABLE TAGS
&#39;/\[table class=(.*?)\](.*?)\[\/table\]/is&#39;  =>  &#39;CLASS-TABLE-TEMPLATE-HERE&#39;,
&#39;/\[table style=\&#39;(.*?)\&#39;\](.*?)\[\/table\]/is&#39;  =>  &#39;STYLE-TABLE-TEMPLATE-HERE&#39;,
&#39;/\[table](.*?)\[\/table\]/is&#39;  =>  &#39;SIMPLE-TABLE STYLE&#39;,

//STYLING
&#39;/\[b](.*?)\[\/b\]/is&#39;  =>  &#39;<strong>$1</strong>&#39;,
&#39;/\[col=(.*?)\](.*?)\[\/col\]/is&#39;  =>  &#39;<span style="color:#$1">$2</span>&#39;,
&#39;/\[size=(.*?)\](.*?)\[\/size\]/is&#39;  =>  &#39;<span style="font-size:$1">$2</span>&#39;,
&#39;/\[s f=(.*?)\ s=(.*?)\ c=(.*?)\](.*?)\[\/s\]/is&#39;  =>  &#39;<span style="font-family:$1; font-size:$2; color:$3">$4</span>&#39;,

//EMPTY TEMPLATE TAGS
&#39;/\{(.*?)\}/is&#39;  =>  "",

);

foreach($tags as $tk=>$tv){
$T_OUTPUT preg_replace($tk,$tv,$T_OUTPUT);
}

echo $T_OUTPUT;

}
}
?>


themes/test/index.theme.php
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<table width="800" border="1">
  <tr>
    <td height="100">&nbsp;</td>
    <td><div align="center">THEME EXAMPLE</div></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="500">{left}</td>
    <td width="500">{middle}</td>
    <td>{right}</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
</body>
</html>

« Last Edit: Wed, 15 Aug 2007, 14:57 by punkt » Logged
barkayal
Iguana Tamer
Baistruc

Offline Offline

Gender: Male
Posts: 174


View Profile
« Reply #1 on: Fri, 08 Jun 2007, 14:59 »

try smarty template engine http://www.smarty.net/
it's easy to use and has a lot of features, inclusiv un sistem de caching pentru templateturi.
Logged
wooptoo
Baistruc

Offline Offline

Posts: 6 201


◕ ◡ ◕


View Profile WWW
« Reply #2 on: Fri, 08 Jun 2007, 15:31 »

thx punkt.
Cand voi avea timp voi trece revolushii pe Drupal (sper...). Are un sistem de templates foarte asemanator cu smarty engine si ce ai scris tu, stie caching cum trebuie, nu ca fsckin wordpress, e foarte performant si curat scris si suporta o gramada de plug-ins.
Logged
punkt
personal space invader
Baistruc

Offline Offline

Gender: Male
Posts: 1 458



View Profile
« Reply #3 on: Fri, 08 Jun 2007, 17:40 »

l-am scris azi, i was too bored at work, si m-am gandit sa fac ceva sa-mi separ codul html de php (de obicei tind sa amestec html-ul cu php-ul pana nu mai inteleg nimic).
Nu prea imi place sa lucrez cu script-uri luate din alte parti, si mai degraba ma chinui sa-mi scriu eu codul de care am nevoie. Mai stii, daca ma tin de capul lui poate ii adaug si eu caching & stuff dar e putin probabil sa-l continui. grin

edit: 'bbcodes' pt tables nu merg grin sunt puse acolo de forma.
« Last Edit: Fri, 08 Jun 2007, 17:43 by punkt » Logged
Stokky
Baistruc

Offline Offline

Gender: Male
Posts: 662



View Profile WWW
« Reply #4 on: Fri, 08 Jun 2007, 19:00 »

try smarty template engine http://www.smarty.net/
it's easy to use and has a lot of features, inclusiv un sistem de caching pentru templateturi.
qtf, smarty template engine ftw
Logged
punkt
personal space invader
Baistruc

Offline Offline

Gender: Male
Posts: 1 458



View Profile
« Reply #5 on: Wed, 15 Aug 2007, 15:06 »

Constante ce contin un interval de timp in secunde.
Not much, dar destul de utile cand lucrezi tu timestamp-uri UNIX tongue.

Code:
<?php
define
("SECONDS",1);
define("MINUTES",60);
define("HOURS",3600);
define("DAYS",86400);
define("WEEKS",604800);
define("MONTHS",2629743.83);
define("YEARS",31556926);
?>


EXEMPLU:

Code:
<?php
$start_execution_time 
date("U");
...do-
something for a long time...
$end_execution_time date("U");
$diff = ($end_execution_time-$start_execution_time)

if(
$diff>1*HOUR){echo "It took me ".($diff/MINUTES)." minutes to do that! oO";}
?>

« Last Edit: Wed, 15 Aug 2007, 15:42 by punkt » Logged
wooptoo
Baistruc

Offline Offline

Posts: 6 201


◕ ◡ ◕


View Profile WWW
« Reply #6 on: Fri, 26 Oct 2007, 01:42 »

Snippet pt validare email cu regular expressions:
Code:
eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $mail)

Stolen from: http://www.zend.com/zend/spotlight/ev12apr.php tongue Functioneaza cel mai bine din cate am gasit pana acuma... nu stiu regex atat de bine incat sa-mi fac eu unul. Pe zend.com e {2,3})$ in loc de {2,4})$, dar articolul e mai vechi (2001) si acum au aparut si domenii din 4 litere ca .info, .mobi etc
Logged
punkt
personal space invader
Baistruc

Offline Offline

Gender: Male
Posts: 1 458



View Profile
« Reply #7 on: Wed, 31 Oct 2007, 17:25 »

http://sol-center.ro/www/r.php (sursa);

"pseudo-widget" tongue pentru ... stuff.. smile

Eu il folosesc pentru Netvibes (inainte sa puneti 'blog-ul' exista chestie RSS pentru FORUM, dar n-am mai gasit o_O)
Face refresh la fiecare 10 min. E facut in 15 min (bored), so no complains 'bout caching, aaight? tongue

netvibes:
« Last Edit: Wed, 31 Oct 2007, 17:31 by punkt » Logged
punkt
personal space invader
Baistruc

Offline Offline

Gender: Male
Posts: 1 458



View Profile
« Reply #9 on: Wed, 31 Oct 2007, 17:37 »

well... daca e ascuns link-ul nu-l stie nimeni smile... nu am pb cu RSS pe forum, doar ca nu stiam de unde sa-l accesez... Inainte era ceva iconita RSS pe forum >.>
Logged
wooptoo Pai detecteaza Opera sau FF daca site-ul are RSS si-ti afiseaza o iconita pe address bar. Click on it.
yoshi mie opera nu-mi arata rss feed pt forum/site :\
wooptoo
Baistruc

Offline Offline

Posts: 6 201


◕ ◡ ◕


View Profile WWW
« Reply #12 on: Thu, 24 Jan 2008, 16:52 »

M-am enervat ieri ca vroia sa-mi trimita un prieten ceva si nu merge receive files in pidgin, sooo:

filebin
Un php script mic care iti permite sa uploadezi doar anumite tipuri de fisiere intr-un director. Poti seta limita de dimensiune.
Se integreaza cu modulul autoindex al apache pentru afisarea fisierelor. E compus din 2 fisiere: HEADER.html - pe care apache il include in paginile generate de autoindex si .up.php care se ocupa de uploadul efectiv (are . in fa?? ca sa nu apara in lista fisierelor din director).

Code: (HEADER.html)
<h1>filebin</h1>

<style type="text/css">
form {
font: normal 1em Verdana, sans-serif;
background: #fff;
border: solid 1px #8D8D8D;
width: 22em;
padding: .8em 1em;
position: fixed;
top: 10px;
right: 8px;
}
h2 {
font-size: 1.4em;
margin: 0 0 .4em 0;
}
#allowed {
font-size: .8em;
}
</style>

<form action=".up.php" method="post" enctype="multipart/form-data">
<h2>Upload file</h2>
<input type="file" name="file" />
<input type="submit" name="submit" value="Upload" />
<p id="allowed">Allowed: png, gif, jpg, mp3, zip, gz</p>
</form>
Tag-ul <style> nu e valid daca e pus acolo, dar asta e. E o limitare a autoindex-ului - nu poti modifica <head>-ul paginilor.

Code: (.up.php)
<?php
if ((($_FILES["file"]["type"] == "image/png")
|| (
$_FILES["file"]["type"] == "image/gif")
|| (
$_FILES["file"]["type"] == "image/jpeg")
|| (
$_FILES["file"]["type"] == "image/pjpeg")
|| (
$_FILES["file"]["type"] == "audio/mpeg")
|| (
$_FILES["file"]["type"] == "application/zip")
|| (
$_FILES["file"]["type"] == "application/x-gzip"))
&& (
$_FILES["file"]["size"] < 11000000)
&& (
$_FILES["file"]["error"] == 0)
&& (
file_exists("" $_FILES["file"]["name"]) == 0))
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"" $_FILES["file"]["name"]);
}
header("Location:.");
?>

silence is golden grin


Live demo here: http://wooptoo.hopto.org/filebin/ yes, you can upload shit tongue
Also, daca poate cineva sa-si dea o parere in legatura cu securitatea scriptului ar fi super. Ma gandeam sa fac un .htaccess care sa dea deny la orice php din director in afara de .up.php... dar trebuie sa mai invat regex tongue
Logged
punkt
personal space invader
Baistruc

Offline Offline

Gender: Male
Posts: 1 458



View Profile
« Reply #13 on: Thu, 24 Jan 2008, 17:53 »

o alternativa la codul tau + respingere fisiere cu extensia php sau exe
Code:
<?php
$allowedtypes 
= array("image/png","image/gif","image/jpeg"."image/pjpeg","audio/mpeg","application/zip","application/x-gzip");
$deniedext = array("php","exe");
$filetype $_FILES["file"]["type"];
$filename =  $_FILES["file"]["name"];
$fileext substr($filename,-3,3); //intoarce ultimele 3 litere din $filename
if(!file_exists($filename)):
   if((
in_array($filetype,$allowedtypes)) && (!in_array($fileext,$deniedext))): //in_array() rox!
move_uploaded_file($_FILES["file"]["tmp_name"],
"" $_FILES["file"]["name"]);
   endif;
endif;
?>


Also securitatea nu prea trebuie sa fie o grija in cazul asta, pentru ca din cate stiu eu nu prea poti sa patesti nimic smile
Poti sa parsezi numele final ($_FILES['file']['name']) pentru caractere interzise (urlencode(), functii custom, etc)
Poti sa scanezi fisierele inainte de upload impotriva virusilor apeland antivirusul de pe sistem (daca ai antivirus) cu exec();
« Last Edit: Thu, 24 Jan 2008, 18:16 by punkt » Logged
yoshi upload on website then download is a waste of time. use HFS http://www.rejetto.com/hfs/
Pages: [1] 2 3   Go Up
  Print  
 
Jump to:  

Designed by | Licensed under Creative Commons BY-NC-ND 3.0 | Powered by SMF 1.1.21 | SMF © 2015, Simple Machines