Warning: Cannot use a scalar value as an array in /home/admin/public_html/forum/include/fm.class.php on line 757

Warning: Invalid argument supplied for foreach() in /home/admin/public_html/forum/include/fm.class.php on line 770

Warning: Invalid argument supplied for foreach() in /home/admin/public_html/forum/topic.php on line 737
Форумы портала PHP.SU :: Несколько классов может кому-нить помогут.

 PHP.SU

Программирование на PHP, MySQL и другие веб-технологии
PHP.SU Портал     На главную страницу форума Главная     Помощь Помощь     Поиск Поиск     Поиск Яндекс Поиск Яндекс     Вакансии  Пользователи Пользователи


 Страниц (1): [1]   

> Описание: работа с CURL, Encryption, Cookie, Database
-SCHATTEN-
Отправлено: 09 Апреля, 2008 - 10:29:33
Post Id



Пользователь


Покинул форум
Сообщений всего: 615
Дата рег-ции: Июль 2006  
Откуда: Оттуда !


Помог: 0 раз(а)




Начнем пожалуй с
PHP:
скопировать код в буфер обмена
  1.  
  2. <?PHP
  3. class Encryption {
  4.        
  5.         static $cypher   = 'blowfish';
  6.         static $mode    = 'cfb';
  7.         static $key     = 'Тут пишем ключ для кодировки';
  8.        
  9.         public static function encrypt( $plantext ) {
  10.                 $td = mcrypt_module_open( self::$cypher, '', self::$mode, '' );
  11.                 $iv = mcrypt_create_iv( mcrypt_enc_get_iv_size( $td ), MCRYPT_RAND );
  12.                 mcrypt_generic_init( $td, self::$key, $iv );
  13.                 $crypttext = mcrypt_generic( $td, $plantext );
  14.                 mcrypt_generic_deinit( $td );
  15.                 return $iv.$crypttext;
  16.         }
  17.        
  18.         public static function decrypt( $crypttext ) {
  19.                 $td = mcrypt_module_open( self::$cypher, '', self::$mode, '' );
  20.                 $ivsize = mcrypt_enc_get_iv_size( $td );
  21.                 $iv = substr( $crypttext, 0, $ivsize );
  22.                 $crypttext = substr( $crypttext, $ivsize );
  23.                 $plaintext = "";
  24.                 if ( $iv ) {
  25.                         mcrypt_generic_init( $td, self::$key, $iv );
  26.                         $plaintext = mdecrypt_generic( $td, $crypttext );
  27.                         mcrypt_generic_deinit( $td );
  28.                 }
  29.                 return $plaintext;
  30.         }
  31.        
  32. }
  33. ?>
  34.  

(Добавление)
PHP:
скопировать код в буфер обмена
  1.  
  2. <?PHP
  3.  
  4. class CURL {
  5.        
  6.         private $curl = NULL;
  7.         private $url  = NULL;
  8.        
  9.         function __construct( $url ) {
  10.                 $this->url = $url;
  11.                 $this->curl = curl_init( $this->url );
  12.         }
  13.        
  14.         function __destruct() {
  15.                 curl_close( $this->curl );
  16.         }
  17.        
  18.         /**
  19.          * This function use's for installing CURL
  20.          * parameters. If You need to use other
  21.          * params you can modify this function.
  22.          *
  23.          * All function You can see below.
  24.          */
  25.         private function setOptions() {
  26.                 $this->setParams();
  27.                 $this->showHeader();
  28.                 $this->followLocation();
  29.                 $this->setTimeOut(0);
  30.         }
  31.        
  32.        
  33.         public function setParams() {
  34.                 curl_setopt ($this->curl, CURLOPT_SSL_VERIFYPEER, 0);  
  35.                 curl_setopt ($this->curl, CURLOPT_RETURNTRANSFER, 1);
  36.                 curl_setopt ($this->curl, CURLOPT_SSL_VERIFYHOST, 1);
  37.         }
  38.        
  39.         public function showHeader( $show = false ) {
  40.                 if ( $show ) {
  41.                         curl_setopt ($this->curl, CURLOPT_HEADER, 1);
  42.                 } else {
  43.                         curl_setopt ($this->curl, CURLOPT_HEADER, 0);
  44.                 }
  45.         }
  46.        
  47.         public function followLocation( $follow = false ) {
  48.                 if ( $follow ) {
  49.                         curl_setopt ($this->curl, CURLOPT_FOLLOWLOCATION, 1);
  50.                 } else {
  51.                         curl_setopt ($this->curl, CURLOPT_FOLLOWLOCATION, 0);
  52.                 }
  53.         }
  54.        
  55.         public function setPost( $row ) {
  56.                 $postdata = self::parse( $row );
  57.                 curl_setopt ($this->curl, CURLOPT_POST, 1);
  58.                 curl_setopt ($this->curl, CURLOPT_POSTFIELDS, $postdata);
  59.         }
  60.        
  61.         public function setTimeOut( $time ) {
  62.                 $time = intval( $time );
  63.                 curl_setopt($this->curl, CURLOPT_TIMEOUT, $time);
  64.         }
  65.        
  66.         public function getUrl() {
  67.                 return $this->url;     
  68.         }
  69.        
  70.         public function getResource() {
  71.                 return $this->curl;
  72.         }
  73.        
  74.         public function send() {
  75.                 $html = curl_exec( $this->curl );
  76.                 return $html;
  77.         }
  78.        
  79.         private static function parse( $row ) {
  80.                 $str = "";
  81.                 foreach ( $row as $key => $value ) {
  82.                         $str .= $key."=".$value."&";
  83.                 }
  84.                 return $str;
  85.         }
  86. }
  87.  
  88. ?>
  89.  
  90.  

(Добавление)
PHP:
скопировать код в буфер обмена
  1.  
  2. <?PHP
  3.  
  4. class Database {
  5.        
  6.         public $_dbLocation    = "localhost";
  7.         public $_dbUser        = "user";
  8.         public $_dbPass        = "pass";
  9.         public $_dbName        = "dbname";
  10.         public $_dbr           = null;
  11.         public $_sql           = "";
  12.         public $_sql_res       = null;
  13.         public $_resource      = null;
  14.         public $_error         = null;
  15.  
  16.         public function __construct( $new = false ) {
  17.                
  18.                 if ($this->_resource = @mysql_connect($this->_dbLocation, $this->_dbUser, $this->_dbPass, $new)) {
  19.                         if (!$this->_dbr = @mysql_select_db($this->_dbName, $this->_resource))  {
  20.                                 $this->_error = " Could not connect to database !";
  21.                                 return NULL;
  22.                         }                      
  23.                 }
  24.                 $this->_error = NULL;
  25.                
  26.         }
  27.        
  28.         public function __destruct()
  29.         {
  30.                 mysql_close($this->_resource);
  31.         }
  32.                
  33.         public function haveError() {
  34.                 if ( $this->_error == NULL ) {
  35.                         return FALSE;
  36.                 }
  37.                 return TRUE;
  38.         }
  39.        
  40.         public function getLastId()
  41.         {
  42.                
  43.                 $rows = null;
  44.                 $rows = @mysql_insert_id();
  45.                
  46.                 return $rows;
  47.         }
  48.        
  49.         /**
  50.          * selecting database
  51.          *
  52.          * @param string $database
  53.          */
  54.         public function selectDb ($database) {
  55.                 $this->_dbr = @mysql_select_db($database, $this->_resource);
  56.         }
  57.        
  58.         private function escape( $var ) {
  59.         if ( get_magic_quotes_gpc() ) {
  60.             $var = stripslashes( $var );
  61.         }  if (version_compare(phpversion(), '4.3.0', '<')) {
  62.                 $var = @mysql_escape_string($var);
  63.         } else {
  64.                 //$var = @mysql_real_escape_string($var);
  65.         }
  66.         return $var;
  67.     }
  68.        
  69.    
  70.     /**
  71.      * Enter description here...
  72.      *
  73.      * @param SQL query $strQuery
  74.      * @param Database (optional) $database
  75.      * @return Error massage if exist
  76.      */
  77.     public function query($strQuery, $database = '') {
  78.                 $strQuery = $this->escape($strQuery);
  79.                 $this->_sql_res = $strQuery;
  80.                 $sql = false;
  81.                 if (!empty($database)) {
  82.                         $this->_dbr = @mysql_select_db($database, $this->_resource);
  83.                 }
  84.                 if ($this->_sql = @mysql_query($this->_sql_res)) {
  85.                         $this->_error = null;
  86.                         return TRUE;
  87.                 } else  {      
  88.                         $this->_sql = null;
  89.                         $this->_error = mysql_error();
  90.                         return FALSE;
  91.                 }
  92.         }
  93.        
  94.         public function gerErrorMsg()
  95.         {
  96.                 return $this->_error;
  97.                
  98.         }
  99.         /**
  100.          * Enter description here...
  101.          *
  102.          * @return Rows of query function ($this->query)
  103.          */
  104.         public function fetchRow() {
  105.                 if ( !$this->_sql || $this->_sql == null ) {
  106.                         return null;
  107.                 }
  108.                
  109.                 $rows = null;
  110.                 $rows = @mysql_fetch_row($this->_sql);
  111.                
  112.                 return $rows;
  113.         }
  114.        
  115.         /**
  116.          * Enter description here...
  117.          *
  118.          * @return List Array
  119.          */
  120.         public function fetchArrayList() {
  121.                 if ( !$this->_sql || $this->_sql == null ) {
  122.                         return null;
  123.                 }
  124.                
  125.                 $array = array();
  126.                 while ($row = mysql_fetch_array( $this->_sql )) {
  127.                         $array[] = $row;
  128.                 }
  129.                        
  130.                 return $array;
  131.         }
  132.        
  133.         /**
  134.          * Enter description here...
  135.          *
  136.          * @return Array of query function
  137.          */
  138.         public function fetchArray() {
  139.                 if ( !$this->_sql || $this->_sql == null ) {
  140.                         return null;
  141.                 }
  142.                
  143.                 $row = mysql_fetch_array( $this->_sql );
  144.                        
  145.                 return $row;
  146.         }
  147.        
  148.         /**
  149.          * Enter description here...
  150.          *
  151.          * @return One value of query function
  152.          */
  153.         public function fetchOne() {
  154.                 if ( !$this->_sql || $this->_sql == null ) {
  155.                         return null;
  156.                 }
  157.                
  158.                 $res = null;
  159.                 if ($row = mysql_fetch_row( $this->_sql )) {
  160.                         $res = $row[0];
  161.                 }
  162.                
  163.                
  164.                 return $res;
  165.         }
  166.        
  167.         /**
  168.          * Enter description here...
  169.          *
  170.          * @return Object array of query function
  171.          */
  172.         public function fetchObjectList() {
  173.                 if ( !$this->_sql || $this->_sql == null ) {
  174.                         return null;
  175.                 }
  176.                
  177.                 $array = array();
  178.                 while ($row = mysql_fetch_object( $this->_sql )) {
  179.                         $array[] = $row;
  180.                 }
  181.                
  182.                 return $array;
  183.         }
  184.        
  185.         public function fetchObject()
  186.         {
  187.                 if ( !$this->_sql || $this->_sql == null ) {
  188.                         return null;
  189.                 }
  190.                
  191.                 $row = mysql_fetch_object( $this->_sql );
  192.                
  193.                 return $row;
  194.         }
  195.        
  196.         /**
  197.          * Enter description here...
  198.          *
  199.          * @return String of query yau have set if query function
  200.          */
  201.         public function returnSql() {
  202.                 return "<pre>".htmlspecialchars( $this->_sql_res )."</pre>";
  203.         }
  204.  
  205. }
  206.  
  207.  
  208. ?>
  209.  
  210.  

(Добавление)
PHP:
скопировать код в буфер обмена
  1. <?PHP
  2.  
  3. class Cookie {
  4.        
  5.         private $created;
  6.         private $userid;
  7.         private $version;
  8.        
  9.         static $coockiename = 'USERAUTH';
  10.         static $myversion = '1';
  11.        
  12.         static $expiration = '600';
  13.         static $resettime  = '300';
  14.         static $warning    = '300';
  15.        
  16.         static $glue       = '|';
  17.        
  18.         function __construct( $userid = false ) {
  19.                 if ( $userid ) {
  20.                         $this->userid = $userid;
  21.                         return ;
  22.                 } else {
  23.                         if ( array_key_exists( self::$coockiename , $_COOKIE ) ) {
  24.                                 $buffer = $this->_unpackage( $_COOKIE[ self::$coockiename ]);
  25.                         } else {
  26.                                 throw new Exception();
  27.                         }
  28.                 }
  29.         }
  30.        
  31.         public function set() {
  32.                 $cookie = $this->_package();
  33.                 setcookie( self::$coockiename, $cookie);
  34.         }
  35.        
  36.         public function validate() {
  37.                 if (!$this->version || !$this->created || !$this->userid ) {
  38.                         throw new Exception("Неверный cookie фаил !");
  39.                 }
  40.                
  41.                 if ( $this->version != self::$myversion ) {
  42.                         throw new Exception("Неверная версия cookie фаила !");
  43.                 }
  44.                
  45.                 if ( time() - $this->created > self::$expiration ) {
  46.                         throw new Exception("Истекло время действия cookie фаила !");
  47.                 } elseif ( time() - $this->created > self::$resettime ) {
  48.                         $this->set();
  49.                 }
  50.         }
  51.        
  52.         private function _package() {
  53.                 $parts = array( self::$myversion, time(), $this->userid );
  54.                 $coockie = implode( self::$glue, $parts );
  55.                 return Encryption::encrypt( $coockie );
  56.         }
  57.        
  58.         private function _unpackage( $cookie ) {
  59.                 $buffer = Encryption::decrypt( $cookie );
  60.                 list( $this->version, $this->created, $this->userid ) = explode( self::$glue, $buffer );
  61.                
  62.                 if ( $this->version != self::$myversion ||
  63.                          !$this->created ||
  64.                          !$this->userid ) {
  65.                         throw new Exception();
  66.                 }
  67.         }
  68.        
  69.         private function _reissue() {
  70.                 $this->created = time();
  71.         }
  72. }
  73.  
  74. ?>
  75.  

(Добавление)
Сори, документация неочень написана, чуть позже примеры напишу как какой класс юзается
 
 Top
Страниц (1): [1]
Сейчас эту тему просматривают: 0 (гостей: 0, зарегистрированных: 0)
« Пользовательские функции »


Все гости форума могут просматривать этот раздел.
Только зарегистрированные пользователи могут создавать новые темы в этом разделе.
Только зарегистрированные пользователи могут отвечать на сообщения в этом разделе.
 



Powered by PHP  Powered By MySQL  Powered by Nginx  Valid CSS  RSS

 
Powered by ExBB FM 1.0 RC1. InvisionExBB