Category Archives: script php

upload di files in stile web 2.0 con php

Oggi vi presento due modi in stile web 2.0 per upload di immagini o di files in generale.

upload di file con barra di caricamento

Questo script permette di far apparire una barra di caricamento mentre si sta caricando un files in modo da far vedere in tempo reale lo sati di caricamento

http://digitarald.de/project/fancyupload/

Upload di file con trascinamento

Questa metodologia, che usa una applet java, permette di caricare un file trascinandolo direttamente dentro l'applet stessa e togliendo l'incombenza di dover cliccare sul tasto sfoglia.

http://www.jfileupload.com/products/jfileupload/index.html

validare un’ email tramite smtp con php

Questa interessante classe php permette di validare un' email tramite smtp con php

  1.  
  2. < ?php
  3. /**
  4.  * Validate Email Addresses Via SMTP
  5.  * This queries the SMTP server to see if the email address is accepted.
  6.  * @copyright http://creativecommons.org/licenses/by/2.0/ - Please keep this comment intact
  7.  * @author gabe@fijiwebdesign.com
  8.  * @contributers adnan@barakatdesigns.net
  9.  * @version 0.1a
  10.  */
  11. class SMTP_validateEmail {
  12.  
  13. /**
  14.   * PHP Socket resource to remote MTA
  15.   * @var resource $sock
  16.   */
  17. var $sock;
  18.  
  19. /**
  20.   * Current User being validated
  21.   */
  22. var $user;
  23. /**
  24.   * Current domain where user is being validated
  25.   */
  26. var $domain;
  27. /**
  28.   * List of domains to validate users on
  29.   */
  30. var $domains;
  31. /**
  32.   * SMTP Port
  33.   */
  34. var $port = 25;
  35. /**
  36.   * Maximum Connection Time to wait for connection establishment per MTA
  37.   */
  38. var $max_conn_time = 30;
  39. /**
  40.   * Maximum time to read from socket before giving up
  41.   */
  42. var $max_read_time = 5;
  43.  
  44. /**
  45.   * username of sender
  46.   */
  47. var $from_user = 'user';
  48. /**
  49.   * Host Name of sender
  50.   */
  51. var $from_domain = 'localhost';
  52.  
  53. /**
  54.   * Nameservers to use when make DNS query for MX entries
  55.   * @var Array $nameservers
  56.   */
  57. var $nameservers = array(
  58. '192.168.0.1'
  59. );
  60.  
  61. var $debug = false;
  62.  
  63. /**
  64.   * Initializes the Class
  65.   * @return SMTP_validateEmail Instance
  66.   * @param $email Array[optional] List of Emails to Validate
  67.   * @param $sender String[optional] Email of validator
  68.   */
  69. function SMTP_validateEmail($emails = false, $sender = false) {
  70. if ($emails) {
  71. $this->setEmails($emails);
  72. }
  73. if ($sender) {
  74. $this->setSenderEmail($sender);
  75. }
  76. }
  77.  
  78. function _parseEmail($email) {
  79. $parts = explode('@', $email);
  80. $domain = array_pop($parts);
  81. $user= implode('@', $parts);
  82. return array($user, $domain);
  83. }
  84.  
  85. /**
  86.   * Set the Emails to validate
  87.   * @param $emails Array List of Emails
  88.   */
  89. function setEmails($emails) {
  90. foreach($emails as $email) {
  91. list($user, $domain) = $this->_parseEmail($email);
  92. if (!isset($this->domains[$domain])) {
  93. $this->domains[$domain] = array();
  94. }
  95. $this->domains[$domain][] = $user;
  96. }
  97. }
  98.  
  99. /**
  100.   * Set the Email of the sender/validator
  101.   * @param $email String
  102.   */
  103. function setSenderEmail($email) {
  104. $parts = $this->_parseEmail($email);
  105. $this->from_user = $parts[0];
  106. $this->from_domain = $parts[1];
  107. }
  108.  
  109. /**
  110.  * Validate Email Addresses
  111.  * @param String $emails Emails to validate (recipient emails)
  112.  * @param String $sender Sender's Email
  113.  * @return Array Associative List of Emails and their validation results
  114.  */
  115. function validate($emails = false, $sender = false) {
  116.  
  117. $results = array();
  118.  
  119. if ($emails) {
  120. $this->setEmails($emails);
  121. }
  122. if ($sender) {
  123. $this->setSenderEmail($sender);
  124. }
  125.  
  126. // query the MTAs on each Domain
  127. foreach($this->domains as $domain=>$users) {
  128.  
  129. $mxs = array();
  130.  
  131. // current domain being queried
  132. $this->domain = $domain;
  133.  
  134. // retrieve SMTP Server via MX query on domain
  135. list($hosts, $mxweights) = $this->queryMX($domain);
  136.  
  137. // retrieve MX priorities
  138. for($n=0; $n < count($hosts); $n++){
  139. $mxs[$hosts[$n]] = $mxweights[$n];
  140. }
  141. asort($mxs);
  142.  
  143. // last fallback is the original domain
  144. $mxs[$this->domain] = 0;
  145.  
  146. $this->debug(print_r($mxs, 1));
  147.  
  148. $timeout = $this->max_conn_time;
  149.  
  150. // try each host
  151. while(list($host) = each($mxs)) {
  152. // connect to SMTP server
  153. $this->debug("try $host:$this->port\n");
  154. if ($this->sock = fsockopen($host, $this->port, $errno, $errstr, (float) $timeout)) {
  155. stream_set_timeout($this->sock, $this->max_read_time);
  156. break;
  157. }
  158. }
  159.  
  160. // did we get a TCP socket
  161. if ($this->sock) {
  162. $reply = fread($this->sock, 2082);
  163. $this->debug("< <<\n$reply");
  164.  
  165. preg_match('/^([0-9]{3}) /ims', $reply, $matches);
  166. $code = isset($matches[1]) ? $matches[1] : '';
  167.  
  168. if($code != '220') {
  169. // MTA gave an error...
  170. foreach($users as $user) {
  171. $results[$user.'@'.$domain] = false;
  172. }
  173. continue;
  174. }
  175.  
  176. // say helo
  177. $this->send("HELO ".$this->from_domain);
  178. // tell of sender
  179. $this->send("MAIL FROM: < ".$this->from_user.'@'.$this->from_domain.">");
  180.  
  181. // ask for each recepient on this domain
  182. foreach($users as $user) {
  183.  
  184. // ask of recepient
  185. $reply = $this->send("RCPT TO: < ".$user.'@'.$domain.">");
  186.  
  187. // get code and msg from response
  188. preg_match('/^([0-9]{3}) /ims', $reply, $matches);
  189. $code = isset($matches[1]) ? $matches[1] : '';
  190.  
  191. if ($code == '250') {
  192. // you received 250 so the email address was accepted
  193. $results[$user.'@'.$domain] = true;
  194. } elseif ($code == '451' || $code == '452') {
  195. // you received 451 so the email address was greylisted (or some temporary error occured on the MTA) - so assume is ok
  196. $results[$user.'@'.$domain] = true;
  197. } else {
  198. $results[$user.'@'.$domain] = false;
  199. }
  200.  
  201. }
  202.  
  203. // reset before quit
  204. $this->send("RSET");
  205.  
  206. // quit
  207. $this->send("quit");
  208. // close socket
  209. fclose($this->sock);
  210.  
  211. }
  212. }
  213. return $results;
  214. }
  215.  
  216.  
  217.  
  218. function send($msg) {
  219. fwrite($this->sock, $msg."\r\n");
  220.  
  221. $reply = fread($this->sock, 2082);
  222.  
  223. $this->debug(">>>\n$msg\n");
  224. $this->debug("< <<\n$reply");
  225.  
  226. return $reply;
  227. }
  228.  
  229. /**
  230.   * Query DNS server for MX entries
  231.   * @return
  232.   */
  233. function queryMX($domain) {
  234. $hosts = array();
  235. $mxweights = array();
  236. if (function_exists('getmxrr')) {
  237. getmxrr($domain, $hosts, $mxweights);
  238. } else {
  239. // windows, we need Net_DNS
  240. require_once 'Net/DNS.php';
  241.  
  242. $resolver = new Net_DNS_Resolver();
  243. $resolver->debug = $this->debug;
  244. // nameservers to query
  245. $resolver->nameservers = $this->nameservers;
  246. $resp = $resolver->query($domain, 'MX');
  247. if ($resp) {
  248. foreach($resp->answer as $answer) {
  249. $hosts[] = $answer->exchange;
  250. $mxweights[] = $answer->preference;
  251. }
  252. }
  253.  
  254. }
  255. return array($hosts, $mxweights);
  256. }
  257.  
  258. /**
  259.   * Simple function to replicate PHP 5 behaviour. http://php.net/microtime
  260.   */
  261. function microtime_float() {
  262. list($usec, $sec) = explode(" ", microtime());
  263. return ((float)$usec + (float)$sec);
  264. }
  265.  
  266. function debug($str) {
  267. if ($this->debug) {
  268. echo '
'.htmlentities($str).'

';
}
}
}
?>