Advertisement
Guest User

process data

a guest
Jun 1st, 2012
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.56 KB | None | 0 0
  1. <?php
  2. //codice captcha
  3.         function Random()
  4.         {
  5.         $chars = "ABCDEFGHJKLMNPQRSTUVWZYZ23456789";
  6.         srand((double)microtime()*1000000);
  7.         $i = 0;
  8.         $pass = '' ;
  9.         while ($i <= 4)
  10.             {
  11.             $num = rand() % 32;
  12.             $tmp = substr($chars, $num, 1);
  13.             $pass = $pass . $tmp;
  14.             $i++;
  15.             }
  16.         return $pass;
  17.         }
  18.     $random_code = Random();
  19.  
  20.  
  21. class Comment
  22. {
  23.     private $data = array();
  24.    
  25.     public function __construct($row)
  26.     {
  27.         /*
  28.         /   The constructor
  29.         */
  30.        
  31.         $this->data = $row;
  32.     }
  33.    
  34.     public function markup()
  35.     {
  36.         /*
  37.         /   This method outputs the XHTML markup of the comment
  38.         */
  39.        
  40.         // Setting up an alias, so we don't have to write $this->data every time:
  41.         $d = &$this->data;
  42.        
  43.         $link_open = '';
  44.         $link_close = '';
  45.        
  46.         if($d['url']){
  47.            
  48.             // If the person has entered a URL when adding a comment,
  49.             // define opening and closing hyperlink tags
  50.            
  51.             $link_open = '<a href="'.$d['url'].'" rel="nofollow">';
  52.             $link_close =  '</a>';
  53.         }
  54.        
  55.         // Converting the time to a UNIX timestamp:
  56.         $d['dt'] = strtotime($d['dt']);
  57.        
  58.         // Needed for the default gravatar image:
  59.         $url = 'http://'.dirname($_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"]).'/img/default_avatar.gif';
  60.        
  61.         return '
  62.        
  63.             <div class="comment">
  64.                 <div class="avatar">
  65.                     '.$link_open.'
  66.                     <img src="http://www.gravatar.com/avatar/'.md5($d['email']).'?size=50&amp;default='.urlencode($url).'" />
  67.                     '.$link_close.'
  68.                 </div>
  69.                
  70.                 <div class="name">'.$link_open.$d['name'].$link_close.' <i>(da/from '.$d['location'].' )</i></div>
  71.                 <div class="date" title="Added at '.date('H:i \o\n d M Y',$d['dt']).'">'.date('d M Y',$d['dt']).'</div>
  72.                 <p>'.$d['body'].'</p>
  73.                
  74.             </div>
  75.         ';
  76.     }
  77.    
  78.     public static function validate(&$arr)
  79.     {
  80.         /*
  81.         /   This method is used to validate the data sent via AJAX.
  82.         /
  83.         /   It return true/false depending on whether the data is valid, and populates
  84.         /   the $arr array passed as a paremter (notice the ampersand above) with
  85.         /   either the valid input data, or the error messages.
  86.         */
  87.        
  88.         $errors = array();
  89.         $data   = array();
  90.  
  91.         // Using the filter_input function introduced in PHP 5.2.0
  92.        
  93.         if(!($data['email'] = filter_input(INPUT_POST,'email',FILTER_VALIDATE_EMAIL)))
  94.         {
  95.             $errors['email'] = 'Please enter a valid Email.';
  96.         }
  97.        
  98.         if(!($data['url'] = filter_input(INPUT_POST,'url',FILTER_VALIDATE_URL)))
  99.         {
  100.             // If the URL field was not populated with a valid URL,
  101.             // act as if no URL was entered at all:
  102.            
  103.             $url = '';
  104.         }
  105.        
  106.         // Using the filter with a custom callback function:
  107.        
  108.         if(!($data['body'] = filter_input(INPUT_POST,'body',FILTER_CALLBACK,array('options'=>'Comment::validate_text'))))
  109.         {
  110.             $errors['body'] = 'Please enter a comment.';
  111.         }
  112.        
  113.         if(!($data['name'] = filter_input(INPUT_POST,'name',FILTER_CALLBACK,array('options'=>'Comment::validate_text'))))
  114.         {
  115.             $errors['name'] = 'Please enter a name.';
  116.         }
  117.        
  118.         if(!($data['location'] = filter_input(INPUT_POST,'location',FILTER_CALLBACK,array('options'=>'Comment::validate_text'))))
  119.         {
  120.             $errors['location'] = 'Please enter your location.';
  121.         }
  122.        
  123. print_r($_POST); die('done here');
  124.         if(!($data['security_code'] = filter_input(INPUT_POST,'security_code',FILTER_CALLBACK,array('options'=>'Comment::validate_text'))))
  125.         {
  126.         $errors['security_code'] = '<br />You did not enter the validation code.';
  127.         }
  128.         if($data['security_code'] != $randomness)
  129.         {
  130.         $errors['security_code'] = '<br />You entered '.$randomness.' the validation code incorrectly.';
  131.         }
  132.  
  133.        
  134.  
  135.         if(!empty($errors)){
  136.            
  137.             // If there are errors, copy the $errors array to $arr:
  138.            
  139.             $arr = $errors;
  140.             return false;
  141.         }
  142.        
  143.         // If the data is valid, sanitize all the data and copy it to $arr:
  144.        
  145.         foreach($data as $k=>$v){
  146.             $arr[$k] = mysql_real_escape_string($v);
  147.         }
  148.        
  149.         // Ensure that the email is lower case:
  150.        
  151.         $arr['email'] = strtolower(trim($arr['email']));
  152.        
  153.         // Prima lettera del nome e della location in maiuscolo:
  154.         $arr['name'] = ucwords(trim($arr['name']));
  155.         $arr['location'] = ucwords(trim($arr['location']));
  156.        
  157.         return true;
  158.        
  159.     }
  160.  
  161.     private static function validate_text($str)
  162.     {
  163.         /*
  164.         /   This method is used internally as a FILTER_CALLBACK
  165.         */
  166.        
  167.         if(mb_strlen($str,'utf8')<1)
  168.             return false;
  169.        
  170.         // Encode all html special characters (<, >, ", & .. etc) and convert
  171.         // the new line characters to <br> tags:
  172.        
  173.         $str = nl2br(htmlspecialchars($str));
  174.        
  175.         // Remove the new line characters that are left
  176.         $str = str_replace(array(chr(10),chr(13)),'',$str);
  177.        
  178.         return $str;
  179.     }
  180.  
  181. }
  182.  
  183. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement