source: extensions/Random_Header/main.inc.php @ 6299

Last change on this file since 6299 was 5499, checked in by repie38, 14 years ago

Test for get_file_contents() existence

File size: 4.0 KB
Line 
1<?php
2/*
3Plugin Name: Random Header
4Version: 2.0.e
5Description: Random Header allow you to show in the header a random picture from the choosen categorie, as a normal image, or as a background
6Plugin URI: http://phpwebgallery.net/ext/extension_view.php?eid=188
7Author: repie38
8Author URI: http://www.pierre-b.com
9*/
10
11if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
12DEFINE('RH_VERSION','v2.0.e');
13define('RH_PATH' , PHPWG_PLUGINS_PATH . basename(dirname(__FILE__)) . '/');
14
15if (! function_exists('get_file_contents')) {
16  function get_file_contents($url, $totalTries = 5)
17         {
18                $Tries = 0;
19                do
20                 {
21                                if ($Tries > 0) sleep(1); # Wait for a sec before retrieving again
22                                $contents = @file_get_contents($url);
23                                $Tries++;
24                 } while ($Tries <= $totalTries && $contents === FALSE);
25                 if ($contents == "") $contents = FALSE;
26                 return $contents;
27         }
28}
29
30class RandomHeader
31{
32    var $rh_config;
33       
34        function load_config()
35    {
36        $x = get_file_contents( dirname(__FILE__).'/data.dat' );
37               
38        if ($x!==false)
39                $this->rh_config = unserialize($x);
40
41                foreach (str_replace(" ", "_", get_pwg_themes()) as $pwg_template) {
42                          if (empty($this->rh_config[$pwg_template]['selected_cat']))  {
43
44                                $this->rh_config[$pwg_template]['selected_cat'] = '0';
45                $this->rh_config[$pwg_template]['active_on_picture']='off';
46                                $this->rh_config[$pwg_template]['head_css']='';
47                                $this->rh_config[$pwg_template]['img_css']='';
48                                $this->rh_config[$pwg_template]['mode_background']='off';
49                                $this->rh_config[$pwg_template]['concat_before'] ='off' ;
50                                $this->rh_config[$pwg_template]['concat_after'] ='off' ;
51
52                $this->save_config();
53                }
54                }
55
56    }
57    function save_config()
58    {
59        $file = fopen( dirname(__FILE__).'/data.dat', 'w' );
60        fwrite($file, serialize($this->rh_config) );
61        fclose( $file );
62    }
63    function plugin_admin_menu($menu)
64    {
65        array_push($menu,
66            array(
67                'NAME' => 'Random Header',
68                'URL' => get_admin_plugin_menu_link(dirname(__FILE__).'/admin/rh_admin.php')
69            ));
70        return $menu;
71    }
72
73        function randombanner(){
74                global $page;
75                global $template;
76                global $conf;
77                global $user;
78                $usertheme=str_replace(" ", "_", $user['template'].'/'.$user['theme'] );
79                if ( !defined('IN_ADMIN') && isset($page['body_id']) && ($page['body_id']!='thePicturePage' || $this->rh_config[$usertheme]['active_on_picture']=='on') ) {
80
81                        $result = pwg_query('SELECT '.IMAGES_TABLE.'.path  FROM '.IMAGES_TABLE.' , '.IMAGE_CATEGORY_TABLE.' WHERE '.IMAGES_TABLE.'.`id` = '.IMAGE_CATEGORY_TABLE.'.`image_id` AND '.IMAGE_CATEGORY_TABLE.'.category_id = ' . $this->rh_config[$usertheme]['selected_cat'] . ' ORDER BY RAND() LIMIT 0,1');
82                        if (mysql_num_rows($result) > 0) {
83                                $toto = mysql_fetch_row($result);
84
85                                if ($this->rh_config[$usertheme]['mode_background']=='on') {
86                                        $template->append('head_elements','<style>#theHeader{background: url('.$toto[0].') no-repeat;'. $this->rh_config[$usertheme]['head_css'] .'}</style>');
87                                }
88                                else {
89                                        if ($this->rh_config[$usertheme]['img_css']!='' || $this->rh_config[$usertheme]['head_css']!='')
90                                                $template->append('head_elements','<style>#theHeader{'. $this->rh_config[$usertheme]['head_css'] .'}#theHeader #RandomImage{'. $this->rh_config[$usertheme]['img_css'] .'}</style>');
91                                        $page['page_banner'] = ($this->rh_config[$usertheme]['concat_before']=='on') ? $conf['page_banner'] : '';
92                                        $page['page_banner'].= '<img id="RandomImage" src="'.$toto[0].'">';
93                                        $page['page_banner'].= ($this->rh_config[$usertheme]['concat_after']=='on') ? $conf['page_banner'] : '';
94                                }
95                        }
96                }
97        }
98
99
100}
101$obj = new RandomHeader();
102$obj->load_config();
103
104add_event_handler('loc_begin_page_header', array(&$obj, 'randombanner') );
105add_event_handler('get_admin_plugin_menu_links', array(&$obj, 'plugin_admin_menu') );
106set_plugin_data($plugin['id'], $obj)
107
108
109?>
Note: See TracBrowser for help on using the repository browser.