Dotclear

source: plugins/popularityContest/inc/lib.popularityContest.php @ 1779

Revision 1779, 7.0 KB checked in by kozlika, 14 years ago (diff)

popularityContest: Typo. Suppression ligne vide en fin de fichier PHP.

Line 
1<?php 
2# ***** BEGIN LICENSE BLOCK *****
3#
4# This file is part of Popularity Contest.
5# Copyright 2007,2009 Moe (http://gniark.net/)
6#
7# Popularity Contest is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 3 of the License, or
10# (at your option) any later version.
11#
12# Popularity Contest is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19#
20# Icon (icon.png) is from Silk Icons : http://www.famfamfam.com/lab/icons/silk/
21#
22# ***** END LICENSE BLOCK *****
23
24class popularityContest
25{
26     public static $send_url = 'http://popcon.gniark.net/send.php';
27     public static $plugins_xml_url = 'http://popcon.gniark.net/raw.xml';
28     
29     public static function getComboOptions()
30     {
31          $day_seconds = 24*3600;
32          $array = array(
33               __('weekly') => (7*$day_seconds),
34               __('every 3 days') => (3*$day_seconds),
35               __('daily') => ($day_seconds),
36          );
37          return($array);
38     }
39
40     # use : getPluginsArray(array('name','root','version')
41     public static function getPluginsArray($array_keys,$array_hidden_plugins=array())
42     {
43          $modules = $GLOBALS['core']->plugins->getModules();
44          $array = array();
45
46          foreach ($modules as $module => $module_values)
47          {
48               if (!in_array($module,$array_hidden_plugins))
49               {
50                    foreach ($array_keys as $key)
51                    {
52                         $array[$module][$key] = $module_values[$key];
53                    }
54               }
55          }
56
57          return($array);
58     }
59               
60     # send to Dotclear Popularity Contest
61     public static function send()
62     {
63          $settings =& $GLOBALS['core']->blog->settings;
64
65          $time_interval_last_try =
66               $_SERVER['REQUEST_TIME'] - $settings->popularityContest_last_try;
67          if ($time_interval_last_try < (30*60))
68          {
69               return(false);
70          }
71
72          $hidden_plugins = array();
73          if (strlen($settings->popularityContest_hidden_plugins) > 0)
74          {
75               $hidden_plugins = unserialize(base64_decode($settings->popularityContest_hidden_plugins));
76          }
77          if (!is_array($hidden_plugins)) {$hidden_plugins = array();}
78         
79          $settings->setNameSpace('popularitycontest');
80          # Popularity Contest last try
81          $settings->put('popularityContest_last_try',
82               $_SERVER['REQUEST_TIME'],'integer','Popularity Contest last try (Unix timestamp)',
83               true,true);
84
85          $url = self::$send_url;
86
87          # inspired by /dotclear/inc/core/class.dc.trackback.php
88          $client = netHttp::initClient($url,$path);
89          $client->setUserAgent('Dotclear - http://www.dotclear.net/');
90          $client->setPersistReferers(false);
91          $infos = array();
92          $infos['id'] = md5(DC_ADMIN_URL);
93          $infos['dc_version'] = DC_VERSION;
94          $data = self::getPluginsArray(array('name'),$hidden_plugins);
95          $data = array_merge($infos,$data);
96          $client->post($url,$data);
97          $content = $client->getContent();
98         
99          if (substr($content,0,4) == 'ok: ')
100          {
101               # ok
102               if (substr($content,4,4) == 'sent')
103               {
104                    $settings->setNameSpace('popularitycontest');
105                    # success : update Popularity Contest last report
106                    $settings->put('popularityContest_last_report',
107                         $_SERVER['REQUEST_TIME'],'integer',
108                         'Popularity Contest last report (Unix timestamp)',true,true);
109                    return(true);
110               }
111               return(false);
112          }
113         
114          return(false);
115     }
116
117     # create table
118     public static function getPluginsTable($editable=false)
119     {
120          global $hidden_plugins;
121
122          if (!is_array($hidden_plugins)) {$hidden_plugins = array();}
123         
124          $plugins_XML = self::getPluginsXML();
125         
126          $show_popularity = false;
127         
128          if (($plugins_XML !== false))
129          {
130               $show_popularity = true;
131               
132               $attr = $plugins_XML->attributes();
133               
134               $plugins_popularity = array();
135               
136               # inspired by daInstaller/inc/class.da.modules.parser.php
137               foreach ($plugins_XML->plugin as $p)
138               {
139                    $attrs = $p->attributes();
140                   
141                    $id = (string) $attrs['id'];
142                    $name = (string) $attrs['name'];
143                    $popularity = (string) $attrs['popularity'];
144                   
145                    $plugins_popularity[$id] = array(
146                         'name' => $name,
147                         'popularity' => $popularity
148                    );
149               }
150          }
151         
152          # don't select hidden plugins or select all of it's editable
153          $array = self::getPluginsArray(array('name','root','version'),
154               (($editable) ? array() : $hidden_plugins));
155
156          $table = new table('class="clear" summary="'.
157               __('Installed plugins:').'"');
158          $table->part('head');
159          $table->row();
160          if ($editable)
161          {
162               $table->header(__('Hide'),'class="nowrap"');
163          }
164          $table->header(__('Icon'),'class="nowrap"');
165          $table->header(__('Plugin'),'class="nowrap"');
166          $table->header(__('Name'),'class="nowrap"');
167          $table->header(__('Version'),'class="nowrap"');
168          if ($show_popularity) {$table->header(__('Popularity'),'class="nowrap"');}
169
170          $table->part('body');
171
172          foreach ($array as $k => $v)
173          {
174               $table->row();
175               if ($editable)
176               {
177                    $table->cell(form::checkbox(array('hidden_plugins[]'),$k,
178                         in_array($k,$hidden_plugins)));
179               }
180               $icon = (file_exists($v['root'].'/icon.png')) ? 
181                    '<img src="index.php?pf='.$k.'/icon.png" style="height:16px;" alt="" />' : '';
182               $table->cell($icon);
183               $table->cell($k);
184               $table->cell($v['name']);
185               $table->cell($v['version']);
186               
187               if ($show_popularity) {
188                    $popularity = '&nbsp;';
189                    if (array_key_exists($k,$plugins_popularity))
190                    {
191                         $popularity = $plugins_popularity[$k]['popularity'].'%';
192                    }
193                    $table->cell($popularity);
194               }
195          }
196
197          return($table->get());
198     }
199
200     # return an human readable string of $diff
201     public static function getDiff($diff)
202     {
203          if ($diff == 0) {return('0'.' '.__('second'));}
204         
205          $diff = abs($diff);
206         
207          $times = array();
208
209          $intervals = array
210          (
211               (3600*24*365.24) => array('one'=>__('year'),'more'=>__('years')),
212               (3600*24*30.4) => array('one'=>__('month'),'more'=>__('months')),
213               (3600*24) => array('one'=>__('day'),'more'=>__('days')),
214               (3600) => array('one'=>__('hour'),'more'=>__('hours')),
215               (60) => array('one'=>__('minute'),'more'=>__('minutes')),
216               (1) => array('one'=>__('second'),'more'=>__('seconds')),
217          );
218
219          foreach ($intervals as $k => $v)
220          {
221               if ($diff >= $k)
222               {
223                    $time = floor($diff/$k);
224                    $times[] = $time.' '.(($time <= 1) ? $v['one'] : $v['more']);
225                    $diff = $diff%$k;
226               }
227          }
228
229          # get times and make a string
230          if (count($times) > 1)
231          {
232               $last = array_pop($times);
233               $str = implode(__(', '),$times).' '.__('and').' '.$last;
234          }
235          else {$str = implode('',$times);}
236
237          return($str);
238     }
239     
240     public static function getPluginsXML()
241     {
242          $file = dirname(__FILE__).'/../xml/plugins.xml';
243          $dir = dirname($file);
244         
245          if (!is_writable($dir))
246          {
247               return(false);
248          }
249         
250          # update the file if it's older than one day
251          if ((!file_exists($file)) OR 
252               (filemtime($file) < ($_SERVER['REQUEST_TIME'] - 3600*24)))
253          {
254               try
255               {
256                    netHttp::quickGet(self::$plugins_xml_url,$file);
257               }
258               catch (Exception $e) {}
259          }
260         
261          if (file_exists($file) && is_readable($file))
262          {
263               $simple_xml = simplexml_load_file($file);
264               
265               return($simple_xml);
266          }
267          else
268          {
269               return(false);
270          }
271     }
272}
273
274?>
Note: See TracBrowser for help on using the repository browser.

Sites map