PageRenderTime 124ms CodeModel.GetById 70ms app.highlight 13ms RepoModel.GetById 38ms app.codeStats 0ms

/library/UrlTube/Parse.php

https://bitbucket.org/baruffaldi/webapp-urltube
PHP | 255 lines | 200 code | 53 blank | 2 comment | 25 complexity | c9de89d339e4f016ef3fcc5529198d3c MD5 | raw file
  1<?php
  2
  3class UrlTube_Parse
  4{
  5     public $patterns = array( 'uri' => '^([a-zA-Z]+)\:\/\/([a-zA-Z0-9\-\_\.]+)\.([a-zA-Z]+)((\/.*)|)^',
  6                               'services' => '^([a-zA-Z]+)\:\/\/(.*)\.(.*)^',
  7                               'mimetype' => '^.*\/.*^' );
  8    
  9    public function setPattern( $type, $pattern )
 10    {
 11         $this->patterns[$type] = $pattern;
 12    }
 13    
 14    public function getDataByMimetype( $mimetype )
 15    {
 16         @preg_match( $this->patterns['mimetype'], $mimetype, $mime );
 17
 18         if ( empty( $mime[1] ) )
 19         {
 20              $mime = explode( '/', $mimetype );
 21              return array( 1 => $mime[0], 2 => $mime[1] );
 22         }
 23
 24          if ( ! empty( $mime[1] ) ) return $mime;
 25          else return FALSE;
 26    }
 27     
 28    public function getDataByURI( $uri )
 29    {
 30          @preg_match( $this->patterns['uri'], $uri, $host );
 31          
 32          if ( ! empty( $host[1] ) ) return $host;
 33          else return FALSE;
 34    }
 35     
 36     public function getServiceByURI( $url )
 37     {
 38          @preg_match( $this->patterns['uri'], $url, $return );
 39          
 40          if ( empty( $return[1] ) ) return FALSE;
 41
 42          $proto  = $return[1];
 43          $host   = $return[2];
 44          $tld    = $return[3];
 45          $domain = array_pop( explode( '.', $host ) );
 46
 47          if ( stristr( $host, 'maps.' ) ) return 'maps';
 48          if ( $proto == 'rtsp' || $proto == 'ftp' ) return $proto;
 49          
 50          switch( $domain ) 
 51          {
 52               default:
 53                    return 'web';
 54                    break;
 55                    
 56               case 'google':
 57                         return 'google';
 58                    break;
 59                    
 60               case 'facebook':
 61                         return 'facebook';
 62                    break;
 63                    
 64               case 'youtube':
 65                         return 'youtube';
 66                    break;
 67                    
 68               case 'flickr':
 69                         return 'flickr';
 70                    break;
 71          }
 72     }
 73     
 74    public function getProtocolByURI( $uri )
 75    {
 76          @preg_match( $this->patterns['uri'], $uri, $host );
 77          
 78          if ( ! empty( $host[1] ) ) return $host[1];
 79          else return FALSE;
 80    }
 81     
 82     public function getCategoryName( $mimetype, $url = NULL )
 83     {
 84          global $_SITE;
 85          
 86          $mime = $_SITE['UrlTube']['Parser']->getDataByMimetype( $mimetype );
 87          
 88          switch( $mime[2] )
 89          {
 90               case 'x-flv':
 91               case 'x-shockwave-flash':
 92               case 'futuresplash':
 93               case 'flash':
 94                    return 'flash';
 95                    break;
 96                    
 97               case 'xml':
 98               case 'xhtml':
 99               case 'html':
100               case 'wml':
101                    if ( ! is_null( $url ) )
102                         return $_SITE['UrlTube']['Parser']->getServiceByURI( $url );
103                    else return 'web';
104                    break;
105                    
106               case 'pdf':
107                    return $mime[2];
108                    break;
109                    
110               case 'quicktime':
111                    return $mime[2];
112                    break;
113          }
114          
115          switch( $mime[1] )
116          {
117               default:
118                    return 'web';
119                    break;
120                    
121               case 'text':
122                    return $mime[1];
123                    break;
124                    
125               case 'audio':
126                    return $mime[1];
127                    break;
128                    
129               case 'video':
130                    return $mime[1];
131                    break;
132                    
133               case 'image':
134                    return $mime[1];
135                    break;
136          }
137     }
138     
139     public function getImageByMimetype( $mimetype, $url = NULL )
140     {
141          return UrlTube_Parse::getCategoryName( $mimetype, $url ) . '.png';
142     }
143     
144     public function getMimetypeByCategory( $category )
145     {
146          switch( $category )
147          {
148               default:
149                    return NULL;
150                    break;
151                    
152               case 'ftp':
153               case 'rtsp':
154                    return array( '*/*' );
155                    break;
156                    
157               case 'flash':
158                    return array( '*/x-flv', '*/x-shockwave-flash', '*/futuresplash', '*/flash' );
159                    break;
160                    
161               case 'web':
162                    return array( '*/xml', '*/xhtml', '*/html' );
163                    break;
164                    
165               case 'pdf':
166                    return array( '*/pdf' );
167                    break;
168                    
169               case 'quicktime':
170                    return array( 'video/quicktime' );
171                    break;
172                    
173               case 'text':
174                    return array( 'text/plain' );
175                    break;
176                    
177               case 'audio':
178                    return array( 'audio/*' );
179                    break;
180                    
181               case 'video':
182                    return array( 'video/*' );
183                    break;
184                    
185               case 'image':
186                    return array( 'image/*' );
187                    break;
188                    
189               case 'facebook':
190                    return array( '*/xml', '*/xhtml', '*/html' );
191                    break;
192                    
193               case 'flickr':
194                    return array( '*/xml', '*/xhtml', '*/html' );
195                    break;
196                    
197               case 'google':
198                    return array( '*/xml', '*/xhtml', '*/html' );
199                    break;
200                    
201               case 'maps':
202                    return array( '*/xml', '*/xhtml', '*/html' );
203                    break;
204          }
205     }
206     
207     public function cleanUrlResults( $url )
208     {
209          foreach( $url as $key => $value )
210          {
211               switch ( $key )
212               {
213                    default:
214                         $url[$key] = ( substr( $key, strlen( $key ) - 4 ) == 'date' && $value != '0' ) 
215                                    ? date( 'y.m.d H:i', $value ) : $value;
216          
217                         if ( is_null( $value ) ) $url[$key] = __LANG_NOT_AVAILABLE__; 
218                         break;
219                         
220                    case 'ip':
221                         $url[$key] = explode( '.', $url[$key] );
222                         $url[$key] = "{$url[$key][0]}.x.x.{$url[$key][3]}";
223                         break;
224                         
225                    case 'max_visits':
226                         $url[$key] = ( $value != 0 ) ? $value : __LANG_ADDURLTUBE_INFVISITS__;
227                         break;
228                         
229                    case 'visits_counter':
230                         $url[$key] = ( $value != 0 ) ? $value : __LANG_NO_ONE_F__;
231                         break;
232                         
233                    case 'title':
234                         $url[$key] = $value;
235                         //$url[$key] = ( ! empty( $value ) ) ? wordwrap( $value, 25, '<br />', true ) : __LANG_NO_TITLE__;
236                         break;
237                         
238                    case 'description':
239                         $url[$key] = $value;
240                         //$url[$key] = ( ! empty( $value ) ) ? wordwrap( $value, 40, '<br />', true ) : __LANG_NO_DESCRIPTION__;
241                         break;
242                         
243                    case 'image':
244                         $category = UrlTube_Parse::getCategoryName( $url['mimetype'], $url['url'] );
245                         if ( $category == 'web' ) $url[$key] = 'http://images.websnapr.com/?url=' . urlencode( $url['url'] ) . '&amp;size=s&amp;nocache=58';
246                         elseif ( file_exists( $_SERVER['DOCUMENT_ROOT'] . "public/images/thumbnails/".md5( $url['id_url'] ).".jpg" ) ) $url[$key] = "/public/images/thumbnails/".md5( $url['id_url'] ).".jpg";
247                         else $url[$key] = '/public/images/categories/' . UrlTube_Parse::getImageByMimetype( $url['mimetype'] );
248                         break;
249               }
250          }
251          
252          return $url;
253     }
254}
255$_SITE['UrlTube']['Parser'] = new UrlTube_Parse();