PageRenderTime 56ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/library/XFeeds.php

https://bitbucket.org/baruffaldi/website-2008-computer-shopping-3
PHP | 365 lines | 270 code | 58 blank | 37 comment | 26 complexity | 0ae69c4af500c90671e64183ea40cfe2 MD5 | raw file
  1. <?php
  2. class XFeeds
  3. {
  4. public $mimetype = NULL;
  5. public $mime = NULL;
  6. public $type = NULL;
  7. public $service = NULL;
  8. public $protocol = NULL;
  9. public $feed = NULL;
  10. public $feed_type = 'rss';
  11. public $feed_handler = NULL;
  12. public $title = NULL;
  13. public $description = NULL;
  14. public function XFeeds( $options = array( ) )
  15. {
  16. foreach ( $options as $option => $value )
  17. $this->$option = $value;
  18. }
  19. public function getUrlTubes( )
  20. {
  21. global $_SITE;
  22. switch( $this->type )
  23. {
  24. default:
  25. $_GET['resetFilters'] = 1;
  26. $d = new Seller( $_SITE['database']['handler'] );
  27. return $d->getProducts( array( 's' => 'update', 'sd' => 'DESC' ) );
  28. break;
  29. case 'gallery':
  30. $q = $_SITE['database']['handler']->select( )
  31. ->from( 'gallery' )
  32. ->where( 'status = ?', 'Y' )
  33. ->order( 'id DESC' )
  34. ->limit( 16 );
  35. $h = $q->query( );
  36. return $h->fetchAll( );
  37. case 'updates':
  38. $q = $_SITE['database']['handler']->select( )
  39. ->from( 'updates' )
  40. ->order( 'id DESC' )
  41. ->limit( 16 );
  42. $h = $q->query( );
  43. return $h->fetchAll( );
  44. case 'newsletters':
  45. $q = $_SITE['database']['handler']->select( )
  46. ->from( 'newsletters' )
  47. ->order( 'id DESC' )
  48. ->limit( 16 );
  49. $h = $q->query( );
  50. return $h->fetchAll( );
  51. break;
  52. }
  53. }
  54. public function getFeed( $type = NULL )
  55. {
  56. if ( ! is_null( $type ) ) $this->feed_type = $type;
  57. $UrlTubes = $this->getFeedArrayByUrlTubes( $this->getUrlTubes( ), $this->service );
  58. switch( $this->feed_type )
  59. {
  60. default:
  61. case 'rss':
  62. $this->feed_handler = Zend_Feed::importArray( $UrlTubes, 'rss' );
  63. break;
  64. case 'atom':
  65. $this->feed_handler = Zend_Feed::importArray( $UrlTubes, 'atom' );
  66. break;
  67. }
  68. $this->feed = $this->feed_handler->saveXML( );
  69. return $this->feed;
  70. }
  71. public function printFeed( )
  72. {
  73. if ( is_null( $this->feed_handler ) ) $this->getFeed( $this->feed_type );
  74. $this->feed_handler->send( );
  75. return $this->feed_handler;
  76. }
  77. public function getFeedInstance( )
  78. {
  79. if ( is_null( $this->feed_handler ) ) $this->getFeed( $this->feed_type );
  80. return $this->feed_handler;
  81. }
  82. public function getFeedArrayByUrlTubes( $urls = NULL, $service = NULL )
  83. {
  84. global $_SITE;
  85. if ( is_null( $urls ) ) return FALSE;
  86. $feedArray = $this->getFeedArray( );
  87. $feedArray['lastUpdate'] = $urls[0]['creation_date'];
  88. $feedArray['published'] = '454475222';
  89. $feedArray['itunes'] = array(
  90. 'author' => trim( $_SITE['config']['handler']->site->admin ), // optional, default to the main author value
  91. 'image' => 'http://' . $_SERVER['HTTP_HOST'] . ':' . $_SERVER['SERVER_PORT'] . '/public/themes/default/images/logo.png', // optional, default to the main image value
  92. 'subtitle' => trim( $this->description ), // optional, default to the main description value
  93. 'summary' => trim( $this->description ), // optional, default to the main description value
  94. 'explicit' => 'clean', // optional
  95. 'category' => array(
  96. array('main' => 'Computer Shopping 3', // required
  97. 'sub' => 'Vendita materiale tecnologico' // optional
  98. )
  99. // up to 3 rows
  100. ) // 'Category column and in iTunes Music Store Browse' // required*/
  101. /*'owner' => array(
  102. 'name' => 'name of the owner', // optional, default to main author value
  103. 'email' => 'email of the owner' // optional, default to main email value
  104. ), */// Owner of the podcast // optional
  105. //'block' => 'Prevent an episode from appearing (yes|no)', // optional
  106. //'keywords' => 'a comma separated list of 12 keywords maximum', // optional
  107. //'new-feed-url' => 'used to inform iTunes of new feed URL location' // optional
  108. );
  109. $feedArray['entries'] = array( );
  110. if ( ! empty( $urls[0] ) )
  111. foreach( $urls as $id => $url )
  112. {
  113. if ( ! empty( $url ) )
  114. {
  115. unset( $get );
  116. switch( $this->type )
  117. {
  118. default:
  119. $get = "i={$url['id_product']}";
  120. $image = md5( $url['id_product'] ) . '.jpg';
  121. $url['title'] = ( is_null( $url['title'] ) ) ? __LANG_NO_TITLE__ : str_replace( "\n", '<br />', wordwrap( $url['title'], 25 ) );
  122. $url['description'] = ( is_null( $url['description'] ) ) ? __LANG_NO_DESCRIPTION__ : str_replace( "\n", '<br />', wordwrap( $url['description'], 35 ) );
  123. $dt = explode( ' ', $url['update'] );
  124. $d = explode( '-', $dt[0] );
  125. $t = explode( ':', $dt[1] );
  126. $feedArray['entries'][$id] = array('title' => trim( strip_tags( "{$url['manufacturer']}: ". substr( $url['description'], 0, 32 ) ) ),
  127. 'link' => "http://{$_SERVER['HTTP_HOST']}/browse/{$this->type}?$get",
  128. 'description' => "<h2>" .trim( strip_tags( $url['description'] ) ) . "</h2> <br /> <br /><img src='http://{$_SERVER['HTTP_HOST']}/public/images/seller/thumbs/$image' alt='Thumbnail' />", // only text, no html, required
  129. 'lastUpdate' => mktime( $t[0], $t[1], $t[2], $d[1], $d[2], $d[0] ), // optional
  130. 'pubDate' => mktime( $t[0], $t[1], $t[2], $d[1], $d[2], $d[0] ), // optional
  131. 'source' => array(
  132. 'title' => trim( strip_tags( "{$url['manufacturer']}: ". substr( $url['description'], 0, 32 ) ) ), // required,
  133. 'url' => "http://{$_SERVER['HTTP_HOST']}/browse/{$this->type}?$get" // required
  134. ), // original source of the feed entry // optional
  135. 'guid' => trim( "http://{$_SERVER['HTTP_HOST']}/browse/{$this->type}?$get" ),
  136. 'content' => trim( strip_tags( $url['description'] ) ) . '&nbsp;',
  137. 'enclosure' => array( array( 'lenght' => 0, 'type' => 'image/jpeg', 'url' => "http://{$_SERVER['HTTP_HOST']}/public/images/seller/thumbs/$image" ) )/*,
  138. //'comments' => 'comments page of the feed entry', // optional
  139. //'commentRss' => 'the feed url of the associated comments', // optional
  140. */ );
  141. break;
  142. case 'gallery':
  143. $image = $url['filename'];
  144. $imageInfo = filemtime( $_SERVER['DOCUMENT_ROOT'] . '/public/images/gallery/data/' . $image );
  145. $url['title'] = ( is_null( $url['title'] ) ) ? __LANG_NO_TITLE__ : str_replace( "\n", '<br />', wordwrap( $url['title'], 25 ) );
  146. $url['description'] = ( is_null( $url['title'] ) ) ? __LANG_NO_DESCRIPTION__ : str_replace( "\n", '<br />', wordwrap( $url['title'], 35 ) );
  147. $feedArray['entries'][$id] = array('title' => trim( strip_tags( "{$url['title']}: ". substr( $url['description'], 0, 32 ) ) ),
  148. 'link' => "http://{$_SERVER['HTTP_HOST']}/browse/{$this->type}",
  149. 'description' => "<h2>" .trim( strip_tags( $url['description'] ) ) . "</h2> <br /> <br /><img src='http://{$_SERVER['HTTP_HOST']}/public/images/{$this->type}/thumbs/$image' alt='Thumbnail' />", // only text, no html, required
  150. 'lastUpdate' => $imageInfo, // optional
  151. 'pubDate' => $imageInfo, // optional
  152. 'source' => array(
  153. 'title' => trim( strip_tags( "{$url['title']}" ) ), // required,
  154. 'url' => "http://gallery/browse/gallery" // required
  155. ), // original source of the feed entry // optional
  156. 'guid' => trim( "http://{$_SERVER['HTTP_HOST']}/public/images/gallery/data/$image?".time() ),
  157. 'content' => trim( strip_tags( $url['description'] ) ) . '&nbsp;',
  158. 'enclosure' => array( array( 'lenght' => 0, 'type' => 'image/jpeg', 'url' => "http://{$_SERVER['HTTP_HOST']}/public/images/{$this->type}/data/$image" ) )/*,
  159. //'comments' => 'comments page of the feed entry', // optional
  160. //'commentRss' => 'the feed url of the associated comments', // optional
  161. */ );
  162. break;
  163. case 'updates':
  164. $dt = explode( ' ', $url['update'] );
  165. $d = explode( '-', $dt[0] );
  166. $t = explode( ':', $dt[1] );
  167. $ret = "<dl>";
  168. if ( $url['added_sources'] >= 1 ) $ret .= "<dd>{$url['added_sources']} " .__LANG_NEW_SOURCES__ . "</dd>";
  169. if ( $url['added_products'] >= 1 ) $ret .= "<dd><a href='http://{$_SERVER['HTTP_HOST']}/browse/products?q=ap&amp;ui={$url['id']}&amp;resetFilters=1' title='Vai agli articoli'>{$url['added_products']}</a> " . __LANG_NEW_PRODUCTS__ . "</dd>";
  170. if ( $url['changed_prices'] >= 1 ) $ret .= "<dd><a href='http://{$_SERVER['HTTP_HOST']}/browse/products?q=cpr&amp;ui={$url['id']}&amp;resetFilters=1' title='Vai agli articoli'>{$url['changed_prices']}</a> " . __LANG_NEW_PRICES__ . "</dd>";
  171. if ( $url['changed_products'] >= 1 ) $ret .= "<dd><a href='http://{$_SERVER['HTTP_HOST']}/browse/products?q=cp&amp;ui={$url['id']}&amp;resetFilters=1' title='Vai agli articoli'>{$url['changed_products']}</a> " . __LANG_CHANGED_PRODUCTS__ . '</dd>';
  172. $ret .= "</dl>";
  173. $image = $url['filename'];
  174. $url['title'] = date( 'd F, Y', mktime( $t[0], $t[1], $t[2], $d[1], $d[2], $d[0] ));
  175. $feedArray['entries'][$id] = array('title' => trim( strip_tags( "{$url['update']}") ),
  176. 'link' => "http://{$_SERVER['HTTP_HOST']}/browse/products?ui={$url['id']}&amp;resetFilters=1",
  177. 'description' => "$ret", // only text, no html, required
  178. 'lastUpdate' => mktime( $t[0], $t[1], $t[2], $d[1], $d[2], $d[0] ), // optional
  179. 'pubDate' => mktime( $t[0], $t[1], $t[2], $d[1], $d[2], $d[0] ), // optional
  180. 'source' => array(
  181. 'title' => trim( strip_tags( "{$url['title']}" ) ), // required,
  182. 'url' => "http://{$_SERVER['HTTP_HOST']}/browse/products?ui={$url['id']}&amp;resetFilters=1" // required
  183. ), // original source of the feed entry // optional
  184. 'guid' => trim( "http://{$_SERVER['HTTP_HOST']}/browse/products?ui={$url['id']}&amp;resetFilters=1" ),
  185. 'content' => trim( strip_tags( $url['description'] ) ) . '&nbsp;'/*
  186. //'comments' => 'comments page of the feed entry', // optional
  187. //'commentRss' => 'the feed url of the associated comments', // optional
  188. */ );
  189. break;
  190. case 'newsletters':
  191. $dt = explode( ' ', $url['date'] );
  192. $d = explode( '-', $dt[0] );
  193. $t = explode( ':', $dt[1] );
  194. $image = $url['filename'];
  195. $url['title'] = ( is_null( $url['title'] ) ) ? __LANG_NO_TITLE__ : str_replace( "\n", '<br />', wordwrap( $url['title'], 25 ) );
  196. $url['description'] = ( is_null( $url['title'] ) ) ? __LANG_NO_DESCRIPTION__ : str_replace( "\n", '<br />', wordwrap( $url['title'], 35 ) );
  197. // $url['creation_date'] = date( DATE_RFC822, $url['creation_date'] );
  198. //
  199. // print "CCC".$url['creation_date']."CCC";
  200. $feedArray['entries'][$id] = array('title' => trim( strip_tags( mktime( $t[0], $t[1], $t[2], $d[1], $d[2], $d[0] ) . ": ". $url['title'] ) ),
  201. 'link' => "http://{$_SERVER['HTTP_HOST']}/browse/products?".time(),
  202. 'description' => "<h2>" .trim( strip_tags( $url['title'] ) ) . "</h2> <br /> <br />{$url['content_up']}<br /><br />{$url['products']}<br /><br />{$url['content_down']}", // only text, no html, required
  203. 'lastUpdate' => mktime( $t[0], $t[1], $t[2], $d[1], $d[2], $d[0] ), // optional
  204. 'pubDate' => mktime( $t[0], $t[1], $t[2], $d[1], $d[2], $d[0] ), // optional
  205. 'source' => array(
  206. 'title' => trim( strip_tags( "{$url['title']}" ) ), // required,
  207. 'url' => "http://{$_SERVER['HTTP_HOST']}/browse/products?".time() // required
  208. ), // original source of the feed entry // optional
  209. 'guid' => "http://{$_SERVER['HTTP_HOST']}/browse/products?".time(),
  210. 'content' => trim( strip_tags( $url['description'] ) ) . '&nbsp;'
  211. /*,
  212. //'comments' => 'comments page of the feed entry', // optional
  213. //'commentRss' => 'the feed url of the associated comments', // optional
  214. */ );
  215. break;
  216. }
  217. }
  218. }
  219. return $feedArray;
  220. }
  221. public function getFeedArray( )
  222. {
  223. global $_SITE;
  224. $feedArray = array(
  225. 'title' => $this->title,
  226. 'link' => 'http://' . $_SERVER['HTTP_HOST'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'],
  227. 'charset' => trim( $_SITE['config']['handler']->site->charset ),
  228. 'description' => 'Assemblaggio, Configurazione, Assistenza e Vendita apparati tecnologici e pc',
  229. 'author' => trim( $_SITE['config']['handler']->site->admin ),
  230. 'email' => trim( $_SITE['config']['handler']->site->admin ),
  231. 'webmaster' => trim( $_SITE['config']['handler']->site->admin ),
  232. 'image' => 'http://' . $_SERVER['HTTP_HOST'] . ':' . $_SERVER['SERVER_PORT'] . '/public/themes/default/images/logo.jpg',
  233. 'generator' => trim( __GENERATOR__ ),
  234. 'language' => trim( $_SITE['config']['env']['language']['short'] ), // optional
  235. 'ttl' => '3600',
  236. 'copyright' => '@'.date('Y').' Computer Shopping 3'
  237. //'rating' => 'The PICS rating for the channel.',
  238. /*'textInput' => array(
  239. 'title' => 'the label of the Submit button in the text input area', // required,
  240. 'description' => 'explains the text input area', // required
  241. 'name' => 'the name of the text object in the text input area', // required
  242. 'link' => 'the URL of the CGI script that processes text input requests', // required
  243. ),*/ // a text input box that can be displayed with the feed // optional, ignored if atom is used
  244. );
  245. return $feedArray;
  246. }
  247. public function getSitemap( $categoryAlso = TRUE )
  248. {
  249. global $_SITE;
  250. //_dump($_SITE);
  251. $links = $_SITE['database']['handler']->select( )
  252. ->from( 'sitemap' )
  253. ->order( array( 'lastmod DESC', 'priority DESC' ) );
  254. $handle = $links->query( );
  255. $links = $handle->fetchAll( );
  256. foreach( $links as $link )
  257. if ( ! @in_array( $linkz[$link['controller']], $link['action'] ) )
  258. $linkz[$link['controller']][$link['action']] = $link;
  259. foreach( $links as $link )
  260. if ( ! @in_array( $link['controller'], $links_controllers ) )
  261. $links_controllers[] = $link['controller'];
  262. foreach( $links as $link )
  263. if ( ! @in_array( $link['action'], $actions[$link['controller']] ) )
  264. $actions[$link['controller']][] = $link['action'];
  265. // _dump($links_controllers);
  266. // _dump($actions);
  267. $host = ( $_SERVER['SERVER_PORT'] == '80' )
  268. ? $_SERVER['HTTP_HOST'] : "{$_SERVER['HTTP_HOST']}:{$_SERVER['SERVER_PORT']}";
  269. foreach ( glob( $_SITE['config']['fs']['path_view'] . DIRECTORY_SEPARATOR . '*' ) as $controller )
  270. {
  271. $controller = str_replace( $_SITE['config']['fs']['path_view'] . DIRECTORY_SEPARATOR, NULL, $controller );
  272. //_dump('C'.$controller);
  273. // *** Check if the controller exists on database
  274. if ( @in_array( $controller, $links_controllers ) !== FALSE )
  275. {
  276. foreach( glob( $_SITE['config']['fs']['path_view'] . DIRECTORY_SEPARATOR .
  277. $controller . DIRECTORY_SEPARATOR . '*' ) as $action )
  278. {
  279. $patterns[0] = '/'.str_replace( '/', '\\/', $_SITE['config']['fs']['path_view'] . DIRECTORY_SEPARATOR .
  280. $controller . DIRECTORY_SEPARATOR ).'/';
  281. $patterns[1] = '/.phtml/';
  282. $replacements[0] = NULL;
  283. $replacements[1] = NULL;
  284. $action = preg_replace( $patterns, $replacements, $action );
  285. // _dump($action);
  286. // *** Check if the action exists on database
  287. if ( @in_array( $action, $actions[$controller] ) !== FALSE )
  288. {
  289. unset( $url );
  290. if ( $action != 'index' ) $url = "$controller/$action";
  291. elseif ( $controller != 'index' && $action == 'index' ) $url = $controller;
  292. // _dump($linkz[$controller][$action]);
  293. $linkz[$controller][$action]['lastmod'] = array_pop( array_reverse( explode( ' ', $linkz[$controller][$action]['lastmod'] ) ) );
  294. if ( $linkz[$controller][$action]['published'] == 'Y' && ! @array_search( "http://$host/$url", $urls[$controller] ) )
  295. $urls[$controller][] = array( 'loc' => "http://$host/$url",
  296. 'lastmod' => date( 'Y-m-' ) . ( date( 'd' ) - 2 ),//$linkz[$controller][$action]['lastmod'],
  297. 'changefreq' => $linkz[$controller][$action]['changefreq'],
  298. 'priority' => $linkz[$controller][$action]['priority'] );
  299. // *** If not present we create a new database entry
  300. } else $_SITE['database']['handler']->insert( 'sitemap', array( 'controller' => $controller, 'action' => $action ) );
  301. }
  302. // *** If not present we create a new database entry
  303. } else $_SITE['database']['handler']->insert( 'sitemap', array( 'controller' => $controller, 'action' => 'index' ) );
  304. }
  305. return $urls;
  306. }
  307. }