source: extensions/AStat/astat_aip.class.inc.php @ 27726

Last change on this file since 27726 was 27065, checked in by plg, 10 years ago

compatibility with Piwigo 2.6: jQuery 1.9+, simpler use of datepicker, no more is_adviser()

  • Property svn:executable set to *
File size: 81.5 KB
Line 
1<?php
2/* -----------------------------------------------------------------------------
3  Plugin     : AStat.2
4  Author     : Grum
5    email    : grum@piwigo.org
6    website  : http://photos.grum.dnsalias.com
7
8    << May the Little SpaceFrog be with you ! >>
9  ------------------------------------------------------------------------------
10  See main.inc.php for release information
11
12  AI classe => manage integration in administration interface
13
14  --------------------------------------------------------------------------- */
15if (!defined('PHPWG_ROOT_PATH')) { die('Hacking attempt!'); }
16
17include_once('astat_root.class.inc.php');
18include_once(PHPWG_ROOT_PATH.'admin/include/tabsheet.class.php');
19include_once(PHPWG_PLUGINS_PATH.'GrumPluginClasses/classes/GPCAjax.class.inc.php');
20
21class AStat_AIP extends AStat_root
22{
23  protected $tabsheet;
24  protected $list_periods = array('global', 'all', 'year', 'month', 'day');
25  protected $list_sortcat = array('page', 'picture', 'nbpicture');
26  protected $list_sortimg = array('picture', 'catname');
27  protected $list_sortip = array('page', 'picture', 'ip');
28
29  protected $catfilter;    //filter on categories
30  protected $max_width;
31  protected $seetimerequest;
32
33  function AStat_AIP($prefixeTable, $filelocation)
34  {
35    parent::__construct($prefixeTable, $filelocation);
36
37    $this->loadConfig();
38    $this->initEvents();
39
40    $this->tabsheet = new tabsheet();
41    $this->tabsheet->add('stats_by_period',
42                          l10n('AStat_by_period'),
43                          $this->getAdminLink().'-stats_by_period');
44    $this->tabsheet->add('stats_by_ip',
45                          l10n('AStat_by_ip'),
46                          $this->getAdminLink().'-stats_by_ip');
47    $this->tabsheet->add('stats_by_category',
48                          l10n('AStat_by_category'),
49                          $this->getAdminLink().'-stats_by_category');
50    $this->tabsheet->add('stats_by_image',
51                          l10n('AStat_by_image'),
52                          $this->getAdminLink().'-stats_by_image');
53    $this->tabsheet->add('config',
54                          l10n('AStat_config'),
55                          $this->getAdminLink().'-config');
56    $this->tabsheet->add('tools',
57                          l10n('AStat_tools'),
58                          $this->getAdminLink().'-tools');
59
60  }
61
62
63  /* ---------------------------------------------------------------------------
64  Public classe functions
65  --------------------------------------------------------------------------- */
66
67
68
69  /*
70    manage plugin integration into piwigo's admin interface
71  */
72  public function manage()
73  {
74    global $template;
75
76    $this->return_ajax_content();
77
78    $template->set_filename('plugin_admin_content', dirname(__FILE__)."/admin/astat_admin.tpl");
79
80    $this->init_request();
81
82    $this->make_filter_list($_REQUEST['fAStat_catfilter']);
83    if($_REQUEST['fAStat_catfilter']!="")
84    {
85      $this->setAdminLink($this->getAdminLink()."&amp;fAStat_catfilter=".$_REQUEST['fAStat_catfilter']);
86    }
87
88    if($_GET['tab']=='stats_by_period')
89    {
90      $this->display_stats_by_period(
91          $_REQUEST['fAStat_all'],
92          $_REQUEST['fAStat_year'],
93          $_REQUEST['fAStat_month'],
94          $_REQUEST['fAStat_day'],
95          $this->config['AStat_MaxBarWidth'],
96          $this->config['AStat_SeeTimeRequests']
97      );
98    }
99    elseif($_GET['tab']=='stats_by_ip')
100    {
101      if(isset($_REQUEST['fAStat_IP_BL']))
102      {
103        $this->add_ip_to_filter($_REQUEST['fAStat_IP_BL']);
104      }
105
106      $this->display_stats_by_ip(
107          $_REQUEST['fAStat_year'],
108          $_REQUEST['fAStat_month'],
109          $_REQUEST['fAStat_day'],
110          $this->config['AStat_MaxBarWidth'],
111          $this->config['AStat_NpIPPerPages'],
112          $_REQUEST['fAStat_page_number'],
113          $_REQUEST['fAStat_SortIP'],
114          $this->config['AStat_SeeTimeRequests']
115      );
116    }
117    elseif($_GET['tab']=='stats_by_category')
118    {
119      $this->display_stats_by_category(
120          $_REQUEST['fAStat_year'],
121          $_REQUEST['fAStat_month'],
122          $_REQUEST['fAStat_day'],
123          $this->config['AStat_MaxBarWidth'],
124          $this->config['AStat_NpCatPerPages'],
125          $_REQUEST['fAStat_page_number'],
126          $this->config['AStat_ShowThumbCat'],
127          $_REQUEST['fAStat_SortCat'],
128          $this->config['AStat_SeeTimeRequests']
129      );
130    }
131    elseif($_GET['tab']=='stats_by_image')
132    {
133      $this->display_stats_by_image(
134          $_REQUEST['fAStat_year'],
135          $_REQUEST['fAStat_month'],
136          $_REQUEST['fAStat_day'],
137          $this->config['AStat_MaxBarWidth'],
138          $this->config['AStat_NbImgPerPages'],
139          $_REQUEST['fAStat_page_number'],
140          $this->config['AStat_ShowThumbImg'],
141          $_REQUEST['fAStat_SortImg'],
142          $_REQUEST['fAStat_IP'],
143          $this->config['AStat_SeeTimeRequests']
144      );
145    }
146    elseif($_GET['tab']=='config')
147    {
148      $this->display_config();
149    }
150    elseif($_GET['tab']=='tools')
151    {
152      $this->display_tools();
153    }
154
155    $this->tabsheet->select($_GET['tab']);
156    $this->tabsheet->assign();
157    $selected_tab=$this->tabsheet->get_selected();
158    $template->assign($this->tabsheet->get_titlename(), "[".$selected_tab['caption']."]");
159
160    $template_plugin["ASTAT_VERSION"] = "<i>AStat</i> ".l10n('AStat_version').ASTAT_VERSION;
161    $template_plugin["ASTAT_PAGE"] = $_GET['tab'];
162
163    $template->assign('plugin', $template_plugin);
164    $template->assign_var_from_handle('ADMIN_CONTENT', 'plugin_admin_content');
165  }
166
167  /* ---------------------------------------------------------------------------
168  Private classe functions
169  --------------------------------------------------------------------------- */
170
171  /*
172    return ajax content
173  */
174  protected function return_ajax_content()
175  {
176    global $ajax, $template;
177
178    if(isset($_REQUEST['ajaxfct']))
179    {
180      //$this->debug("AJAXFCT:".$_REQUEST['ajaxfct']);
181      $result="<p class='errors'>An error has occured</p>";
182      switch($_REQUEST['ajaxfct'])
183      {
184        case 'astat_listip':
185          if(!isset($_REQUEST['ipfilter'])) $_REQUEST['ipfilter']="";
186          if(!isset($_REQUEST['exclude'])) $_REQUEST['exclude']="";
187          $result=$this->ajax_listip($_REQUEST['ipfilter'], $_REQUEST['exclude']);
188          break;
189      }
190      GPCAjax::returnResult($result);
191    }
192  }
193
194
195
196
197
198
199
200  private function init_request()
201  {
202    $default_request = array('all'=>'Y', 'year'=>'', 'month'=>'', 'day'=>'');
203
204    //initialise $REQUEST values if not defined
205    if(!array_key_exists('tab', $_GET))
206    {
207      $_GET['tab']='stats_by_period';
208    }
209    if(!array_key_exists('fAStat_defper', $_REQUEST))
210    {
211      $_REQUEST['fAStat_defper']='Y';
212    }
213    if(!array_key_exists('fAStat_SortCat', $_REQUEST))
214    {
215      $_REQUEST['fAStat_SortCat']=$this->config['AStat_DefaultSortCat'];
216    }
217    if(!array_key_exists('fAStat_SortImg', $_REQUEST))
218    {
219      $_REQUEST['fAStat_SortImg']=$this->config['AStat_DefaultSortImg'];
220    }
221    if(!array_key_exists('fAStat_SortIP', $_REQUEST))
222    {
223      $_REQUEST['fAStat_SortIP']=$this->config['AStat_DefaultSortIP'];
224    }
225    if(!array_key_exists('fAStat_page_number', $_REQUEST))
226    {
227      $_REQUEST['fAStat_page_number']='1';
228    }
229    if(!array_key_exists('fAStat_IP', $_REQUEST))
230    {
231      $_REQUEST['fAStat_IP']="";
232    }
233    if(!array_key_exists('fAStat_purge_history_date', $_REQUEST))
234    {
235      $_REQUEST['fAStat_purge_history_date']="";
236    }
237    if(!array_key_exists('fAStat_catfilter', $_REQUEST))
238    {
239      $_REQUEST['fAStat_catfilter']="";
240    }
241
242    if(($_GET['tab']=='stats_by_period')&&($_REQUEST['fAStat_defper']=='Y'))
243    {
244      if($this->config['AStat_default_period']!='global')
245      {
246        $default_request['all'] = 'N';
247      }
248
249      if(($this->config['AStat_default_period']=='year')||
250        ($this->config['AStat_default_period']=='month')||
251        ($this->config['AStat_default_period']=='day'))
252      {
253        $default_request['year'] = date('Y');
254      }
255
256      if(($this->config['AStat_default_period']=='month')||
257        ($this->config['AStat_default_period']=='day'))
258      {
259        $default_request['month'] = date('n');
260      }
261
262      if($this->config['AStat_default_period']=='day')
263      {
264        $default_request['day'] = date('j');
265      }
266    }
267
268    if(!array_key_exists('fAStat_all', $_REQUEST))
269    {
270      $_REQUEST['fAStat_all']=$default_request['all'];
271    }
272    if(!array_key_exists('fAStat_year', $_REQUEST))
273    {
274      $_REQUEST['fAStat_year']=$default_request['year'];
275    }
276    if(!array_key_exists('fAStat_month', $_REQUEST))
277    {
278      $_REQUEST['fAStat_month']=$default_request['month'];
279    }
280    if(!array_key_exists('fAStat_day', $_REQUEST))
281    {
282      $_REQUEST['fAStat_day']=$default_request['day'];
283    }
284
285    $this->catfilter=$_REQUEST['fAStat_catfilter'];
286
287  } //init_request
288
289
290  /*
291    request functions
292
293    theses functions made SQL requests and returns datas
294  */
295
296
297  /*
298    Stat by period
299      Number of Pages
300      Number IP
301      Number of Images
302      by :
303        total
304        years
305        years/months
306        years/months/days
307        years/months/days/hours
308  */
309  private function stats_by_period($total, $year, $month, $day)
310  {
311    $returned = array();
312    $delta = 0;
313
314    $sql = " count(id) as NbPages, count(distinct IP) as NbIP,
315            count(image_id) as NbImg, count(distinct category_id) as NbCat ";
316    $sql_nfomax = ", MaxPages, MaxIP, MaxImg";
317    $sql_from = " from ".HISTORY_TABLE." ";
318    $sql_where = "";
319    $sql_order = "";
320    $sql_select = "";
321    $sql_group = "";
322    $sql_groupdef="";
323
324    if($day!="")
325    {
326      $sql_groupdef="HOUR(time) as GId,";
327      $sql_select="select HOUR(time) as GId, ";
328      $sql_where=" where YEAR(date) = $year and MONTH(date) = $month and DAY(date) = $day ";
329      $sql_group=" group by GId ";
330      $sql_order=" order by GId asc";
331
332      for($i=0;$i<24;$i++)
333      {
334        $returned[$i] = array(
335                          "GId" => $i,
336                          "NbPages" => 0,
337                          "NbIP" => 0,
338                          "NbImg" => 0,
339                          "NbCat"=>0,
340                          "MaxPages" => 0,
341                          "MaxIP" => 0,
342                          "MaxImg" => 0
343                        );
344      }
345    }
346    elseif($month!="")
347    {
348      $sql_groupdef="DAY(date) as GId,";
349      $sql_select="select DAY(date) as GId, ";
350      $sql_where=" where YEAR(date) = $year and MONTH(date) = $month ";
351      $sql_group=" group by GId ";
352      $sql_order=" order by GId asc";
353
354      $delta = 1;
355      $NbDays = strftime('%d', mktime(0,0,0,$month+1,0,$year));
356      for($i=0;$i<$NbDays;$i++)
357      {
358        $returned[$i] = array(
359                          "GId" => $i+1,
360                          "NbPages" => 0,
361                          "NbIP" => 0,
362                          "NbImg" => 0,
363                          "NbCat"=>0,
364                          "MaxPages" => 0,
365                          "MaxIP" => 0,
366                          "MaxImg" => 0
367                        );
368      }
369    }
370    elseif($year!="")
371    {
372      $sql_groupdef="MONTH(date) as GId,";
373      $sql_select="select MONTH(date) as GId, ";
374      $sql_where=" where YEAR(date) = $year ";
375      $sql_group=" group by GId ";
376      $sql_order=" order by GId asc ";
377
378      $delta = 1;
379      for($i=0;$i<12;$i++)
380      {
381        $returned[$i] = array(
382                          "GId" => $i+1,
383                          "NbPages" => 0,
384                          "NbIP" => 0,
385                          "NbImg" => 0,
386                          "NbCat"=>0,
387                          "MaxPages" => 0,
388                          "MaxIP" => 0,
389                          "MaxImg" => 0
390                        );
391      }
392    }
393    elseif($total!="Y")
394    {
395      $sql_groupdef="YEAR(date) as GId,";
396      $sql_select="select YEAR(date) as GId, ";
397      $sql_group=" group by GId ";
398      $sql_order=" order by GId desc";
399    }
400    else
401    {
402      $sql_select="select 'Tout' as GId, "; $sql_nfomax=", 0 as MaxPages, 0 as MaxIP, 0 as MaxImg";
403    }
404
405    if($this->catfilter!="")
406    {
407      $catfilter=$this->make_where_clause($this->catfilter);
408      ($sql_where=="")?$sql_where=" where ":$sql_where.=" and ";
409      $sql_where.=" category_id IN (".$catfilter.")";
410    }
411
412    if(($this->config['AStat_UseBlackList']!="false")&&($this->config['AStat_BlackListedIP']!=""))
413    {
414      ($sql_where=="")?$sql_where=" where ":$sql_where.=" AND ";
415      ($this->config['AStat_UseBlackList']=="true")?$sql_where .= " NOT ":"";
416      $sql_where .= $this->make_IP_where_clause($this->config['AStat_BlackListedIP']);
417    }
418
419    $sql_max=", (select max(n.MaxPages) as MaxPages, max(n.MaxIP) as MaxIP, max(n.MaxImg) as MaxImg
420        from (select ".$sql_groupdef." count(id) as MaxPages, count(distinct IP) as MaxIP, count(image_id) as MaxImg
421            from ".HISTORY_TABLE.$sql_where.$sql_group.") as n) as n ";
422    $sql=$sql_select.$sql.$sql_nfomax.$sql_from.$sql_max.$sql_where.$sql_group.$sql_order;
423
424    $result = pwg_query($sql);
425
426    $i=0;
427    while ($row = pwg_db_fetch_assoc($result))
428    {
429      if($year.$month.$day=="")
430      { $returned[$i] = $row; $i++; }
431      else
432      { $returned[$row["GId"]-$delta] = $row; }
433    }
434
435    return($returned);
436  } //stat by period
437
438  /*
439    Stats by IP
440      Number of Pages
441      Number of Images
442      by :
443        IP
444        IP/years
445        IP/years/months
446        IP/years/months/days
447  */
448  private function stats_by_ip($year, $month, $day, $nbipperpage, $pagenumber, $sortip)
449  {
450    $returned0 = array();
451    $sortlist = array(
452                  "page" => "NbPages desc",
453                  "picture" => "NbImg desc",
454                  "ip" => "IP_USER asc"
455                );
456
457    $sql_select="select SQL_CALC_FOUND_ROWS ";
458    $sql= " if(".HISTORY_TABLE.".user_id = 2, IP, if(".USERS_TABLE.".username is null, ' [".l10n("AStat_deleted_user")."]', CONCAT(' ', ".USERS_TABLE.".username))) as IP_USER, count(".HISTORY_TABLE.".id) as NbPages, count(image_id) as NbImg ";
459          $sql_nfomax = ", MaxPages, MaxImg";
460    $sql_from = " from ".HISTORY_TABLE." LEFT JOIN ".USERS_TABLE." ON ".HISTORY_TABLE.".user_id = ".USERS_TABLE.".id ";
461    $sql_where = "";
462    $sql_group=" group by IP_USER ";
463    $sql_order=" order by ".$sortlist[$sortip]." ";
464    $sql_limit=" limit ".(($pagenumber-1)* $nbipperpage).", ".$nbipperpage;
465
466
467    if($day!="")
468    {
469      $sql_where=" where YEAR(date) = $year and MONTH(date) = $month and DAY(date) = $day ";
470    }
471    elseif($month!="")
472    {
473      $sql_where=" where YEAR(date) = $year and MONTH(date) = $month ";
474    }
475    elseif($year!="")
476    {
477      $sql_where=" where YEAR(date) = $year ";
478    }
479    else
480    {
481      $sql_nfomax = ", 0 as MaxPages, 0 as MaxImg";
482    }
483
484    if($this->catfilter!="")
485    {
486      $catfilter=$this->make_where_clause($this->catfilter);
487      ($sql_where=="")?$sql_where=" where ":$sql_where.=" and ";
488      $sql_where.=" category_id IN (".$catfilter.")";
489    }
490
491    if(($this->config['AStat_UseBlackList']!="false")&&($this->config['AStat_BlackListedIP']!=""))
492    {
493      ($sql_where=="")?$sql_where=" where ":$sql_where.=" AND ";
494      ($this->config['AStat_UseBlackList']=="true")?$sql_where .= " NOT ":"";
495      $sql_where .= $this->make_IP_where_clause($this->config['AStat_BlackListedIP']);
496      $sql.=" , 'N' AS blacklist";
497    }
498    else
499    {
500      if($this->config['AStat_BlackListedIP']=='')
501      {
502        $sql.=" , 'N' AS blacklist";
503      }
504      else
505      {
506        $sql.=" , (CASE ";
507        $tmp=explode(',', $this->config['AStat_BlackListedIP']);
508        foreach($tmp as $key=>$val)
509        {
510          $sql.=" WHEN IP LIKE '".$val."' THEN 'Y' ";
511        }
512        $sql.="ELSE 'N' END) AS blacklist ";
513      }
514    }
515
516    $sql_max=", (select max(n.MaxPages) as MaxPages, max(n.MaxImg) as MaxImg
517        from (select if(".HISTORY_TABLE.".user_id = 2, IP, if(".USERS_TABLE.".username is null, '[".l10n("AStat_deleted_user")."]', ".USERS_TABLE.".username)) as IP_USER, count(".HISTORY_TABLE.".id) as MaxPages, count(image_id) as MaxImg
518            from ".HISTORY_TABLE." LEFT JOIN ".USERS_TABLE." ON ".HISTORY_TABLE.".user_id = ".USERS_TABLE.".id ".$sql_where.$sql_group.") as n) as n ";
519    $sql=$sql_select.$sql.$sql_nfomax.$sql_from.$sql_max.$sql_where.$sql_group.$sql_order.$sql_limit;
520
521
522    $result = pwg_query($sql);
523    $sql="select FOUND_ROWS()";
524
525    $i=0;
526    while ($row = pwg_db_fetch_assoc($result))
527    {
528      $returned0[$i] = $row; $i++;
529    }
530    $returned[0] = $returned0;
531
532    $result = pwg_query($sql);
533    if($result)
534    {
535      $row = pwg_db_fetch_row($result); $returned[1] = $row[0];
536    }
537    else
538    {
539      $returned[1] = -1;
540    }
541
542    return($returned);
543  } //stat by ip
544
545  /*
546    Stats per categories
547      %Pages
548      %Images
549      by :
550        Categories
551        Categories/years
552        Categories/years/months
553        Categories/years/months/day
554  */
555  private function stats_by_category($year, $month, $day, $nbipperpage, $pagenumber, $show_thumb, $sortcat)
556  {
557    $sortlist = array(
558                  "page" => "PctPages desc, PctImg desc",
559                  "picture" => "PctImg desc, PctPages desc",
560                  "nbpicture" => "RatioImg desc, PctPages desc"
561                );
562    $returned0 = array();
563
564    $sql_select="SELECT SQL_CALC_FOUND_ROWS ";
565
566    $sql= "category_id, IF(category_id > 0, ".CATEGORIES_TABLE.".name, section) AS IdCat,
567  COUNT(".HISTORY_TABLE.".id) AS NbPages, MaxPages.somme, 100*(count(".HISTORY_TABLE.".id)/MaxPages.somme) AS PctPages,
568  COUNT(".HISTORY_TABLE.".image_id) AS NbImg, MaxImg.somme, 100*(count(".HISTORY_TABLE.".image_id)/MaxImg.somme) AS PctImg, ic2.nb_images as NbImgCat, (COUNT(".HISTORY_TABLE.".image_id)/ic2.nb_images) AS RatioImg, greatest(100*(COUNT(".HISTORY_TABLE.".id)/MaxPages.somme), 100*(COUNT(".HISTORY_TABLE.".image_id)/MaxImg.somme)) AS MaxPct ";
569
570    if($show_thumb=='true')
571    {
572      $sql_thumb = ', '.IMAGES_TABLE.'.path as ImgPath, '.IMAGES_TABLE.'.file as ThumbFile, '.IMAGES_TABLE.'.id as ImgId';
573      $sql_fromthumb = "LEFT JOIN ".IMAGES_TABLE." ON ic2.representative_picture_id = ".IMAGES_TABLE.".id  ";
574    }
575    else
576    {
577      $sql_thumb = "";
578      $sql_fromthumb = "";
579    }
580
581    $sql_from = " FROM (".HISTORY_TABLE." LEFT JOIN ".CATEGORIES_TABLE." ON ".CATEGORIES_TABLE.".id = ".HISTORY_TABLE.".category_id),
582(SELECT category_id AS catid, COUNT(image_id) AS nb_images, representative_picture_id
583 FROM ".IMAGE_CATEGORY_TABLE.", ".CATEGORIES_TABLE."
584 WHERE ".CATEGORIES_TABLE.".id = ".IMAGE_CATEGORY_TABLE.".category_id group by category_id) AS ic2 ";
585    $sql_where = "";
586    $sql_group=" GROUP BY category_id, section ";
587    $sql_group2="";
588    $sql_order=" ORDER BY ".$sortlist[$sortcat];
589    $sql_limit=" LIMIT ".(($pagenumber-1)* $nbipperpage).", ".$nbipperpage;
590
591    if($day!="")
592    {
593      $sql_where=" WHERE YEAR(date) = $year AND MONTH(date) = $month AND DAY(date)= $day ";
594    }
595    elseif($month!="")
596    {
597      $sql_where=" WHERE YEAR(date) = $year AND MONTH(date) = $month ";
598    }
599    elseif($year!="")
600    {
601      $sql_where=" WHERE YEAR(date) = $year ";
602    }
603    else { }
604
605    if($this->catfilter!="")
606    {
607      $catfilter=$this->make_where_clause($this->catfilter);
608      ($sql_where=="")?$sql_where=" where ":$sql_where.=" and ";
609      $sql_where.=" category_id IN (".$catfilter.")";
610    }
611
612    $sql_max=", (SELECT COUNT(id) AS somme FROM ".HISTORY_TABLE.$sql_where.$sql_group2.") AS MaxPages,
613          (SELECT COUNT(image_id) AS somme FROM ".HISTORY_TABLE.$sql_where.$sql_group2.") AS MaxImg ";
614
615    ($sql_where=="")?$sql_where=" WHERE ":$sql_where.=" AND ";
616    $sql_where .= "  ic2.catid = ".HISTORY_TABLE.".category_id ";
617
618    if(($this->config['AStat_UseBlackList']!="false")&&($this->config['AStat_BlackListedIP']!=""))
619    {
620      ($this->config['AStat_UseBlackList']=="true")?$sql_where .= " AND NOT ":"";
621      $sql_where .= $this->make_IP_where_clause($this->config['AStat_BlackListedIP']);
622    }
623
624    $sql=$sql_select.$sql.$sql_thumb.$sql_from.$sql_fromthumb.$sql_max.$sql_where.$sql_group.$sql_order.$sql_limit;
625
626    $result = pwg_query($sql);
627    $sql="SELECT FOUND_ROWS()";
628
629    $i=0;
630    while ($row = pwg_db_fetch_assoc($result))
631    {
632      $returned0[$i] = $row; $i++;
633    }
634    $returned[0] = $returned0;
635
636    $result = pwg_query($sql);
637    if($result)
638    {
639      $row = pwg_db_fetch_row($result); $returned[1] = $row[0];
640    }
641    else
642    {
643      $returned[1] = -1;
644    }
645
646    return($returned);
647  } // stats per categories
648
649  /*
650    Stats by image
651      Num of view per image
652      %view on period
653      by :
654        Images
655        Images/years
656        Images/years/months
657        Images/years/months/days
658  */
659  private function stats_by_image($year, $month, $day, $nbipperpage, $pagenumber, $sortimg, $ip)
660  {
661    $sortlist = array(
662                  "picture" => "NbVues desc, CatName asc, ImgName asc, ImgId asc",
663                  "catname" => "CatName asc, ImgName asc, ImgId asc"
664                );
665    $returned0 = array();
666
667    $sql_select="select SQL_CALC_FOUND_ROWS ";
668
669
670    $sql=" image_id as ImgId, ".IMAGES_TABLE.".name as ImgName,
671        if(category_id > 0, ".CATEGORIES_TABLE.".name, section) as CatName,
672        ".HISTORY_TABLE.".category_id as IdCat, count(".HISTORY_TABLE.".image_id) as NbVues,
673        MaxImg.somme, 100*(count(".HISTORY_TABLE.".image_id)/MaxImg.somme) as PctImg,
674        ".IMAGES_TABLE.".path as ImgPath, ".IMAGES_TABLE.".file as ThumbFile,
675        MaxImg2.somme as NbVuesMax ";
676
677    $sql_from = " from ((".HISTORY_TABLE." LEFT JOIN ".IMAGES_TABLE." ON
678  ".IMAGES_TABLE.".id = ".HISTORY_TABLE.".image_id) LEFT JOIN ".CATEGORIES_TABLE."
679  ON ".CATEGORIES_TABLE.".id = ".HISTORY_TABLE.".category_id) ";
680    $sql_from_ip="";
681
682    $sql_where = " where ".HISTORY_TABLE.".image_id is not null ";
683    $sql_group=" group by image_id, category_id ";
684    $sql_order=" order by ".$sortlist[$sortimg];
685    $sql_limit=" limit ".(($pagenumber-1)* $nbipperpage).", ".$nbipperpage;
686
687    if($day!="")
688    {
689      $sql_where.=" and YEAR(date) = $year and MONTH(date) = $month and DAY(date)= $day ";
690    }
691    elseif($month!="")
692    {
693      $sql_where.=" and YEAR(date) = $year and MONTH(date) = $month ";
694    }
695    elseif($year!="")
696    {
697      $sql_where.=" and YEAR(date) = $year ";
698    }
699    else { }
700
701    if($ip!="")
702    {
703      $sql_where.=" and ( ((IP = '$ip') and ( user_id = 2 ))  or (".USERS_TABLE.".username = '$ip') )";
704      $sql_from_ip=" LEFT JOIN ".USERS_TABLE." ON ".USERS_TABLE.".id = ".HISTORY_TABLE.".user_id ";
705    }
706
707    if($this->catfilter!="")
708    {
709      $catfilter=$this->make_where_clause($this->catfilter);
710      ($sql_where=="")?$sql_where=" where ":$sql_where.=" and ";
711      $sql_where.=" category_id IN (".$catfilter.")";
712    }
713
714    if(($this->config['AStat_UseBlackList']!="false")&&($this->config['AStat_BlackListedIP']!=""))
715    {
716      ($sql_where=="")?$sql_where=" where ":$sql_where.=" AND ";
717      ($this->config['AStat_UseBlackList']=="true")?$sql_where .= " NOT ":"";
718      $sql_where .= $this->make_IP_where_clause($this->config['AStat_BlackListedIP']);
719    }
720
721
722    $sql_max=", (select count(image_id) as somme from ".HISTORY_TABLE.$sql_from_ip.$sql_where.") as MaxImg, (select count(image_id) as somme from ".HISTORY_TABLE.$sql_from_ip.$sql_where." and ".HISTORY_TABLE.".image_id is not null group by image_id order by somme desc limit 0,1) as MaxImg2 ";
723
724    $sql=$sql_select.$sql.$sql_from.$sql_from_ip.$sql_max.$sql_where.$sql_group.$sql_order.$sql_limit;
725
726    $result = pwg_query($sql);
727    $sql="select FOUND_ROWS()";
728
729    $i=0;
730    while ($row = pwg_db_fetch_assoc($result))
731    {
732      $returned0[$i] = $row; $i++;
733    }
734    $returned[0] = $returned0;
735
736    $result = pwg_query($sql);
737    if($result)
738    {
739      $row = pwg_db_fetch_row($result); $returned[1] = $row[0];
740    }
741    else
742    {
743      $returned[1] = -1;
744    }
745
746    return($returned);
747  } //stat by images
748
749
750  /*
751    template related functions
752
753    theses functions call stat function, and display result with template usage
754  */
755
756
757
758  private function display_stats_by_period($total, $year, $month, $day, $max_width, $seetimerequest)
759  {
760    global $template;
761
762    $template->set_filename('body_page', dirname(__FILE__)."/admin/astat_by_period.tpl");
763
764
765    $template_datas=array();
766    $template_datarows=array();
767
768
769    /* requete */
770    //calc_time(true);
771    $stats = $this->stats_by_period($total, $year, $month, $day);
772    //$timerequest=calc_time(false);
773
774    $dir_links = "";
775    $a_links=array("global" => $this->getAdminLink()."&amp;fAStat_defper=N&amp;fAStat_all=Y",
776        "all" => $this->getAdminLink()."&amp;fAStat_defper=N&amp;fAStat_all=N",
777        "year" => $this->getAdminLink()."&amp;fAStat_defper=N&amp;fAStat_all=N&amp;fAStat_year=$year",
778        "month" => $this->getAdminLink()."&amp;fAStat_defper=N&amp;fAStat_all=N&amp;fAStat_year=$year&amp;fAStat_month=$month",
779        "day" => $this->getAdminLink()."&amp;fAStat_defper=N&amp;fAStat_all=N&amp;fAStat_year=$year&amp;fAStat_month=$month&amp;fAStat_day=$day");
780
781    $ip_links=array("all" => $this->getAdminLink()."-stats_by_ip",
782        "year" => $this->getAdminLink()."-stats_by_ip&amp;fAStat_year=",
783        "month" => $this->getAdminLink()."-stats_by_ip&amp;fAStat_year=$year&amp;fAStat_month=",
784        "day" => $this->getAdminLink()."-stats_by_ip&amp;fAStat_year=$year&amp;fAStat_month=$month&amp;fAStat_day=");
785
786    $cat_links=array("all" => $this->getAdminLink()."-stats_by_category",
787        "year" => $this->getAdminLink()."-stats_by_category&amp;fAStat_year=",
788        "month" => $this->getAdminLink()."-stats_by_category&amp;fAStat_year=$year&amp;fAStat_month=",
789        "day" => $this->getAdminLink()."-stats_by_category&amp;fAStat_year=$year&amp;fAStat_month=$month&amp;fAStat_day=");
790
791    $img_links=array("all" => $this->getAdminLink()."-stats_by_image",
792        "year" => $this->getAdminLink()."-stats_by_image&amp;fAStat_year=",
793        "month" => $this->getAdminLink()."-stats_by_image&amp;fAStat_year=$year&amp;fAStat_month=",
794        "day" => $this->getAdminLink()."-stats_by_image&amp;fAStat_year=$year&amp;fAStat_month=$month&amp;fAStat_day=");
795
796
797    /* period label + navigation links */
798    if($day!="")
799    {
800      $template_datas["PERIOD_LABEL"] = l10n("AStat_period_label_hours");
801      $template_datas["L_STAT_TITLE"] = $this->format_link(l10n("AStat_period_label_global"), $a_links["global"])." / ".$this->format_link(l10n("AStat_period_label_all"), $a_links["all"])." / ".$this->format_link($year, $a_links["year"])." / ".$this->format_link(l10n("AStat_month_of_year_".$month), $a_links["month"])." / ".l10n("AStat_day_of_week_".date("w",mktime(0, 0, 0, $month, $day, $year)))." $day";
802    }
803    elseif($month!="")
804    {
805      $template_datas["PERIOD_LABEL"] = l10n("AStat_period_label_days");
806      $template_datas["L_STAT_TITLE"] = $this->format_link(l10n("AStat_period_label_global"), $a_links["global"])." / ".$this->format_link(l10n("AStat_period_label_all"), $a_links["all"])." / ".$this->format_link($year, $a_links["year"])." / ".l10n("AStat_month_of_year_".$month);
807    }
808    elseif($year!="")
809    {
810      $template_datas["PERIOD_LABEL"] = l10n("AStat_period_label_months");
811      $template_datas["L_STAT_TITLE"] = $this->format_link(l10n("AStat_period_label_global"), $a_links["global"])." / ".$this->format_link(l10n("AStat_period_label_all"), $a_links["all"])." / ".$year;
812    }
813    elseif($total!="Y")
814    {
815      $template_datas["PERIOD_LABEL"] = l10n("AStat_period_label_years");
816      $template_datas["L_STAT_TITLE"] = $this->format_link(l10n("AStat_period_label_global"), $a_links["global"])." / ".l10n("AStat_period_label_all");
817    }
818    else
819    {
820      $template_datas["PERIOD_LABEL"] = l10n("AStat_period_label_all");
821      $template_datas["L_STAT_TITLE"] = l10n("AStat_period_label_global");
822    }
823    $template_datas["MAX_WIDTH"] = $max_width+10;
824    $template_datas["ASTAT_NFO_STAT"] = l10n("AStat_Nfo_Period");
825
826    if($seetimerequest=='true')
827    {
828      $template_datas["ASTAT_TIME_REQUEST"] = l10n('AStat_time_request_label').' '.$timerequest.'s';
829    }
830
831    /* tableau de stat */
832    for($i=0;$i<count($stats);$i++)
833    {
834      if($stats[$i]["MaxPages"] > 0)
835      {
836        $width_pages = ceil(($stats[$i]["NbPages"] * $max_width) / $stats[$i]["MaxPages"] );
837        $width_img = ceil(($stats[$i]["NbImg"] * $max_width) / $stats[$i]["MaxPages"] );
838        $width_ip = ceil(($stats[$i]["NbIP"] * $max_width) / $stats[$i]["MaxPages"] );
839        $width_cat = ceil(($stats[$i]["NbCat"] * $max_width) / $stats[$i]["MaxPages"] );
840      }
841      else
842      {
843        $width_pages = 0;
844        $width_img = 0;
845        $width_ip = 0;
846        $width_cat = 0;
847      }
848
849
850      if($day!="")
851      { // si jours sélectionné, heures affichées
852        $value=$stats[$i]["GId"];
853        $link="";
854        $value_ip="";
855        $value_cat="";
856        $value_img="";
857      }
858      elseif($month!="")
859      { // si mois sélectionné, jours affichés
860        $value = $stats[$i]["GId"]." (".l10n("AStat_day_of_week_".date("w",mktime(0, 0, 0, $month, $stats[$i]["GId"], $year))).")";
861        $link=$this->getAdminLink()."&amp;fAStat_defper=N&amp;fAStat_all=N&amp;fAStat_year=$year&amp;fAStat_month=$month&amp;fAStat_day=".$stats[$i]["GId"];
862        $value_ip=$ip_links["day"].$stats[$i]["GId"];
863        $value_cat=$cat_links["day"].$stats[$i]["GId"];
864        $value_img=$img_links["day"].$stats[$i]["GId"];
865      }
866      elseif($year!="")
867      { // si année sélectionnée, mois affichés
868        $value = l10n("AStat_month_of_year_".$stats[$i]["GId"]);
869        $link=$this->getAdminLink()."&amp;fAStat_defper=N&amp;fAStat_all=N&amp;fAStat_year=$year&amp;fAStat_month=".$stats[$i]["GId"];
870        $value_ip=$ip_links["month"].$stats[$i]["GId"];
871        $value_cat=$cat_links["month"].$stats[$i]["GId"];
872        $value_img=$img_links["month"].$stats[$i]["GId"];
873      }
874      elseif($total!="Y")
875      { // si total sélectionné, années affichées
876        $value = $stats[$i]["GId"];
877        $link=$this->getAdminLink()."&amp;fAStat_defper=N&amp;fAStat_all=N&amp;fAStat_year=".$stats[$i]["GId"];
878        $value_ip=$ip_links["year"].$stats[$i]["GId"];
879        $value_cat=$cat_links["year"].$stats[$i]["GId"];
880        $value_img=$img_links["year"].$stats[$i]["GId"];
881      }
882      else
883      {
884        $value=l10n("AStat_period_label_all");
885        $link=$this->getAdminLink()."&amp;fAStat_defper=N&amp;fAStat_all=N";
886        $value_ip=$ip_links["all"];
887        $value_cat=$cat_links["all"];
888        $value_img=$img_links["all"];
889      }
890
891
892      if((($stats[$i]["NbPages"] + $stats[$i]["NbImg"] + $stats[$i]["NbIP"])>0)&&($link!=""))
893      { $value=$this->format_link($value, $link); }
894
895      if((($stats[$i]["NbIP"])>0)&&($value_ip!=""))
896      { $value_ip=$this->format_link($stats[$i]["NbIP"], $value_ip); }
897      else
898      { $value_ip = $stats[$i]["NbIP"]; }
899
900      if((($stats[$i]["NbCat"])>0)&&($value_cat!=""))
901      { $value_cat=$this->format_link($stats[$i]["NbCat"], $value_cat); }
902      else
903      { $value_cat= $stats[$i]["NbCat"]; }
904
905      if((($stats[$i]["NbImg"])>0)&&($value_img!=""))
906      { $value_img=$this->format_link($stats[$i]["NbImg"], $value_img); }
907      else
908      { $value_img= $stats[$i]["NbImg"]; }
909
910
911
912      $template_datarows[]=array(
913        'VALUE' => $value,
914        'PAGES' => $stats[$i]["NbPages"],
915        'PICTURES' => $value_img,
916        'CATEGORIES' => $value_cat,
917        'IPVISIT' => $value_ip,
918        'WIDTH1' => $width_ip,
919        'WIDTH2' => $width_img,
920        'WIDTH3' => $width_pages,
921        'WIDTH4' => $width_cat,
922      );
923
924    }
925
926    $template->assign('datas', $template_datas);
927    $template->assign('datarows', $template_datarows);
928    $template->assign_var_from_handle('ASTAT_BODY_PAGE', 'body_page');
929  } //display_stats_by_period
930
931
932
933  /*
934    display stat by ip
935  */
936  private function display_stats_by_ip($year, $month, $day, $max_width, $nbipperpage, $pagenumber, $sortip, $seetimerequest)
937  {
938    global $template;
939
940    $template->set_filename('body_page', dirname(__FILE__)."/admin/astat_by_ip.tpl");
941
942
943    $template_datas=array();
944    $template_datarows=array();
945
946
947    /* requete */
948    //calc_time(true);
949    $returned = $this->stats_by_ip($year, $month, $day, $nbipperpage, $pagenumber, $sortip);
950    //$timerequest=calc_time(false);
951    $stats=$returned[0];
952    $nbfoundrows=$returned[1];
953    $nbpages=ceil($nbfoundrows / $nbipperpage);
954
955
956    $dir_links = "";
957    $page_link = "";
958    $a_links=array("global" => $this->getAdminLink()."&amp;fAStat_defper=N&amp;fAStat_all=Y",
959        "all" => $this->getAdminLink()."&amp;fAStat_defper=N&amp;fAStat_all=N",
960        "year" => $this->getAdminLink()."&amp;fAStat_defper=N&amp;fAStat_all=N&amp;fAStat_year=$year",
961        "month" => $this->getAdminLink()."&amp;fAStat_defper=N&amp;fAStat_all=N&amp;fAStat_year=$year&amp;fAStat_month=$month",
962        "day" => $this->getAdminLink()."&amp;fAStat_defper=N&amp;fAStat_all=N&amp;fAStat_year=$year&amp;fAStat_month=$month&amp;fAStat_day=$day");
963
964    $ip_links=array("all" => $this->getAdminLink()."-stats_by_ip",
965        "year" => $this->getAdminLink()."-stats_by_ip&amp;fAStat_year=$year",
966        "month" => $this->getAdminLink()."-stats_by_ip&amp;fAStat_year=$year&amp;fAStat_month=$month",
967        "day" => $this->getAdminLink()."-stats_by_ip&amp;fAStat_year=$year&amp;fAStat_month=$month&amp;fAStat_day=$day");
968
969    $img_links=array("all" => $this->getAdminLink()."-stats_by_image",
970        "year" => $this->getAdminLink()."-stats_by_image&amp;fAStat_year=$year",
971        "month" => $this->getAdminLink()."-stats_by_image&amp;fAStat_year=$year&amp;fAStat_month=$month",
972        "day" => $this->getAdminLink()."-stats_by_image&amp;fAStat_year=$year&amp;fAStat_month=$month&amp;fAStat_day=$day");
973
974
975    /* periode label + navigation links */
976    if($day!="")
977    {
978      $dir_links = $this->format_link(l10n("AStat_period_label_global"), $a_links["global"])." / ".$this->format_link(l10n("AStat_period_label_all"), $a_links["all"])." / ".$this->format_link($year, $a_links["year"])." / ".$this->format_link(l10n("AStat_month_of_year_".$month), $a_links["month"])." / ".l10n("AStat_day_of_week_".date("w",mktime(0, 0, 0, $month, $day, $year)))." $day";
979      $page_link=$ip_links["day"];
980      $img_link=$img_links["day"];
981    }
982    elseif($month!="")
983    {
984      $dir_links = $this->format_link(l10n("AStat_period_label_global"), $a_links["global"])." / ".$this->format_link(l10n("AStat_period_label_all"), $a_links["all"])." / ".$this->format_link($year, $a_links["year"])." / ".l10n("AStat_month_of_year_".$month);
985      $page_link=$ip_links["month"];
986      $img_link=$img_links["month"];
987    }
988    elseif($year!="")
989    {
990      $dir_links = $this->format_link(l10n("AStat_period_label_global"), $a_links["global"])." / ".$this->format_link(l10n("AStat_period_label_all"), $a_links["all"])." / ".$year;
991      $page_link=$ip_links["year"];
992      $img_link=$img_links["year"];
993    }
994    else
995    {
996      $dir_links = $this->format_link(l10n("AStat_period_label_global"), $a_links["global"])." / ".l10n("AStat_period_label_all");
997      $page_link=$ip_links["all"];
998      $img_link=$img_links["all"];
999    }
1000
1001
1002    if($nbpages>1) { $plural="s"; } else { $plural=""; }
1003    $pages_links=l10n("AStat_page".$plural."_label")." : ";
1004    for($i=1;$i<=$nbpages;$i++)
1005    {
1006      if($i==$pagenumber)
1007      { $pages_links.=" $i "; }
1008      else
1009      {
1010        $pages_links.=$this->format_link(" $i ", $page_link."&amp;fAStat_page_number=$i&amp;fAStat_SortIP=$sortip");
1011      }
1012    }
1013
1014    $template_datas["L_STAT_TITLE"] = $dir_links;
1015    $template_datas["MAX_WIDTH"]=$max_width+10;
1016    $template_datas["NB_TOTAL_IP"] = l10n('AStat_nb_total_ip')." : $nbfoundrows";
1017    $template_datas["PAGES_LINKS"] = $pages_links;
1018    $template_datas["ASTAT_NFO_STAT"]= l10n("AStat_Nfo_IP");
1019
1020    $template_datas["ASTAT_SORT_LABEL"] = l10n("AStat_SortIPLabel").strtolower(l10n("AStat_sortip_$sortip"));
1021
1022    $template_datas["ASTAT_LABEL_IP_label"] = $this->format_link(l10n("AStat_RefIPLabel"), $page_link."&amp;fAStat_SortIP=ip");
1023    $template_datas["ASTAT_LABEL_Pages_seen"] = $this->format_link(l10n("Pages seen"), $page_link."&amp;fAStat_SortIP=page");
1024    $template_datas["ASTAT_LABEL_Pictures_seen"] = $this->format_link(l10n("Pictures_seen"), $page_link."&amp;fAStat_SortIP=picture");
1025
1026    if($seetimerequest=='true')
1027    {
1028      $template_datas["ASTAT_TIME_REQUEST"] = l10n('AStat_time_request_label').' '.$timerequest.'s';
1029    }
1030
1031    for($i=0;$i<count($stats);$i++)
1032    {
1033        if($stats[$i]["MaxPages"] > 0)
1034      {
1035        $width_pages = ceil(($stats[$i]["NbPages"] * $max_width) / $stats[$i]["MaxPages"] );
1036        $width_img = ceil(($stats[$i]["NbImg"] * $max_width) / $stats[$i]["MaxPages"] );
1037      }
1038      else
1039      {
1040        $width_pages = 0;
1041        $width_img = 0;
1042      }
1043
1044
1045      if($this->is_ip($stats[$i]["IP_USER"]))
1046      {
1047        $ip_geolocalisation='<a href="http://www.geoiptool.com/fr/?IP='.$stats[$i]["IP_USER"].'" title="Geo IP localisation" target="_blank">['.l10n('AStat_IP_geolocalisation').'] </a>';
1048        $ip_adress='<a href="http://www.ripe.net/whois?form_type=simple&amp;full_query_string=&amp;searchtext='.$stats[$i]["IP_USER"].'+&amp;do_search=Search" title="Ripe Whois" target="_blank">'.$stats[$i]["IP_USER"].'</a>';
1049        $ip_blacklist=$page_link."&amp;fAStat_IP_BL=".$stats[$i]["IP_USER"];
1050
1051        if($pagenumber>1)
1052        {
1053          $ip_blacklist.="&amp;fAStat_page_number=$pagenumber";
1054        }
1055
1056        if($sortip!="page")
1057        {
1058          $ip_blacklist.="&amp;fAStat_SortIP=$sortip";
1059        }
1060
1061        if($stats[$i]["blacklist"]=='Y')
1062        {
1063          $ip_blacklist="[".l10n('AStat_IP_blacklist')."]";
1064        }
1065        else
1066        {
1067          $ip_blacklist=$this->format_link("[".l10n('AStat_IP_blacklist')."]", $ip_blacklist);
1068        }
1069      }
1070      else
1071      {
1072        $ip_geolocalisation='';
1073        $ip_adress=$stats[$i]["IP_USER"];
1074        $ip_blacklist='';
1075      }
1076
1077
1078
1079      $template_datarows[]=array(
1080        'ASTAT_IP_BLACKLIST' => $ip_blacklist,
1081        'ASTAT_IP_GEOLOCALISATION' => $ip_geolocalisation,
1082        'ASTAT_IP_ADRESS' => $ip_adress,
1083        'PAGES' => $stats[$i]["NbPages"],
1084        'PICTURES' => $this->format_link($stats[$i]["NbImg"], $img_link."&amp;fAStat_IP=".trim($stats[$i]["IP_USER"])),
1085        'WIDTH1' => $width_img,
1086        'WIDTH2' => $width_pages,
1087      );
1088    }
1089
1090    $template->assign('datas', $template_datas);
1091    $template->assign('datarows', $template_datarows);
1092    $template->assign_var_from_handle('ASTAT_BODY_PAGE', 'body_page');
1093  } // display_stat_by_ip
1094
1095
1096
1097
1098
1099  /*
1100    display stat by category
1101  */
1102  private function display_stats_by_category($year, $month, $day, $max_width, $nbipperpage, $pagenumber,$showthumb, $sortcat, $seetimerequest)
1103  {
1104    global $template, $conf;
1105
1106    $template->set_filename('body_page', dirname(__FILE__)."/admin/astat_by_category.tpl");
1107
1108    $template_datas=array();
1109    $template_datarows=array();
1110
1111    /* requete */
1112    //calc_time(true);
1113    $returned = $this->stats_by_category($year, $month, $day, $nbipperpage, $pagenumber, $showthumb, $sortcat);
1114    //$timerequest=calc_time(false);
1115    $stats=$returned[0];
1116    $nbfoundrows=$returned[1];
1117    $nbpages=ceil($nbfoundrows / $nbipperpage);
1118
1119
1120    $dir_links = "";
1121    $page_link = "";
1122    $a_links=array("global" => $this->getAdminLink()."&amp;fAStat_defper=N&amp;fAStat_all=Y",
1123        "all" => $this->getAdminLink()."&amp;fAStat_defper=N&amp;fAStat_all=N",
1124        "year" => $this->getAdminLink()."&amp;fAStat_defper=N&amp;fAStat_all=N&amp;fAStat_year=$year",
1125        "month" => $this->getAdminLink()."&amp;fAStat_defper=N&amp;fAStat_all=N&amp;fAStat_year=$year&amp;fAStat_month=$month",
1126        "day" => $this->getAdminLink()."&amp;fAStat_defper=N&amp;fAStat_all=N&amp;fAStat_year=$year&amp;fAStat_month=$month&amp;fAStat_day=$day");
1127
1128    $cat_links=array("all" => $this->getAdminLink()."-stats_by_category",
1129        "year" => $this->getAdminLink()."-stats_by_category&amp;fAStat_year=$year",
1130        "month" => $this->getAdminLink()."-stats_by_category&amp;fAStat_year=$year&amp;fAStat_month=$month",
1131        "day" => $this->getAdminLink()."-stats_by_category&amp;fAStat_year=$year&amp;fAStat_month=$month&amp;fAStat_day=$day");
1132
1133    /* make navigation links */
1134    if($day!="")
1135    {
1136      $dir_links = $this->format_link(l10n("AStat_period_label_global"), $a_links["global"])." / ".$this->format_link(l10n("AStat_period_label_all"), $a_links["all"])." / ".$this->format_link($year, $a_links["year"])." / ".$this->format_link(l10n("AStat_month_of_year_".$month), $a_links["month"])." / ".l10n("AStat_day_of_week_".date("w",mktime(0, 0, 0, $month, $day, $year)))." $day";
1137      $page_link=$cat_links["day"];
1138    }
1139    elseif($month!="")
1140    {
1141      $dir_links = $this->format_link(l10n("AStat_period_label_global"), $a_links["global"])." / ".$this->format_link(l10n("AStat_period_label_all"), $a_links["all"])." / ".$this->format_link($year, $a_links["year"])." / ".l10n("AStat_month_of_year_".$month);
1142      $page_link=$cat_links["month"];
1143    }
1144    elseif($year!="")
1145    {
1146      $dir_links = $this->format_link(l10n("AStat_period_label_global"), $a_links["global"])." / ".$this->format_link(l10n("AStat_period_label_all"), $a_links["all"])." / ".$year;
1147      $page_link=$cat_links["year"];
1148    }
1149    else
1150    {
1151      $dir_links = $this->format_link(l10n("AStat_period_label_global"), $a_links["global"])." / ".l10n("AStat_period_label_all");
1152      $page_link=$cat_links["all"];
1153    }
1154
1155
1156    if($nbpages>1)
1157    {
1158      $plural="s";
1159    }
1160    else
1161    {
1162      $plural="";
1163    }
1164
1165    $pages_links=l10n("AStat_page".$plural."_label")." : ";
1166    for($i=1;$i<=$nbpages;$i++)
1167    {
1168      if($i==$pagenumber)
1169      {
1170        $pages_links.=" $i ";
1171      }
1172      else
1173      {
1174        $pages_links.=$this->format_link(" $i ", $page_link."&amp;fAStat_SortCat=$sortcat&amp;fAStat_page_number=$i");
1175      }
1176    }
1177
1178
1179    /* navigation */
1180    $template_datas["L_STAT_TITLE"] = $dir_links;
1181    $template_datas["MAX_WIDTH"] = $max_width;
1182    $template_datas["NB_TOTAL_CATEGORY"] = l10n('AStat_nb_total_category')." : $nbfoundrows";
1183    $template_datas["PAGES_LINKS"] = $pages_links;
1184    $template_datas["ASTAT_NFO_STAT"] = l10n("AStat_Nfo_Category");
1185    $template_datas["ASTAT_SORT_LABEL"] = l10n("AStat_SortCatLabel").strtolower(l10n("AStat_sortcat_$sortcat"));
1186
1187    $template_datas["ASTAT_LABEL_pct_Pages_seen"] = $this->format_link(l10n("pct_Pages_seen"), $page_link."&amp;fAStat_SortCat=page");
1188    $template_datas["ASTAT_LABEL_pct_Pictures_seen"] = $this->format_link(l10n("pct_Pictures_seen"), $page_link."&amp;fAStat_SortCat=picture");
1189    $template_datas["ASTAT_LABEL_ratio_Pictures_seen"] = $this->format_link(l10n("ratio_Pictures_seen"), $page_link."&amp;fAStat_SortCat=nbpicture");
1190
1191    if($seetimerequest=='true')
1192    {
1193      $template_datas["ASTAT_TIME_REQUEST"] = l10n('AStat_time_request_label').' '.$timerequest.'s';
1194    }
1195
1196    for($i=0;$i<count($stats);$i++)
1197    {
1198      $width_pages = ceil(($stats[$i]["PctPages"] * $max_width)/100);
1199      $width_img = ceil(($stats[$i]["PctImg"] * $max_width)/100 );
1200
1201      if($showthumb=='true')
1202      {
1203        $filethumb=DerivativeImage::thumb_url(array('id'=>$stats[$i]["ImgId"], 'path'=>$stats[$i]["ImgPath"]));
1204      }
1205      else
1206      {
1207        $filethumb='';
1208      }
1209
1210      if($stats[$i]["category_id"]>0)
1211      { $category = $this->format_link($stats[$i]["IdCat"], PHPWG_ROOT_PATH."index.php?/category/".$stats[$i]["category_id"]); }
1212      else
1213      {
1214        $category = "<i>".l10n('AStat_section_label').' : ';
1215        if(l10n('AStat_section_'.$stats[$i]["IdCat"])!='AStat_section_'.$stats[$i]["IdCat"])
1216        {
1217          $category.=l10n('AStat_section_'.$stats[$i]["IdCat"]);
1218        }
1219        else
1220        {
1221          $category.=sprintf(l10n('AStat_section_unknown'), $stats[$i]["IdCat"]);
1222        }
1223
1224        $category.="</i>";
1225      }
1226
1227      $template_datarows[]=array(
1228        'THUMBJPG' => $filethumb,
1229        'CATEGORY' => $category,
1230        'PCTPAGES' => $stats[$i]["PctPages"],
1231        'PCTPICTURES' => $stats[$i]["PctImg"],
1232        'RATIOPICTURES' => $stats[$i]["RatioImg"],
1233        'WIDTH1' => $width_img,
1234        'WIDTH2' => $width_pages,
1235      );
1236    }
1237
1238    $template->assign('datas', $template_datas);
1239    $template->assign('datarows', $template_datarows);
1240    $template->assign_var_from_handle('ASTAT_BODY_PAGE', 'body_page');
1241  } // display_stat_by_category
1242
1243
1244
1245  /* ------------------------------------------------------------------------------------------
1246      display stats for images
1247      ------------------------------------------------------------------------------------------ */
1248  function display_stats_by_image($year, $month, $day, $max_width, $nbipperpage, $pagenumber,$showthumb, $sortimg, $ip, $seetimerequest)
1249  {
1250    global $template, $conf;
1251
1252    $template->set_filename('body_page', dirname(__FILE__)."/admin/astat_by_image.tpl");
1253
1254
1255    $template_datas=array();
1256    $template_datarows=array();
1257
1258
1259    //calc_time(true);
1260    $returned = $this->stats_by_image($year, $month, $day, $nbipperpage, $pagenumber, $sortimg, $ip);
1261    //$timerequest=calc_time(false);
1262    $stats=$returned[0];
1263    $nbfoundrows=$returned[1];
1264    $nbpages=ceil($nbfoundrows / $nbipperpage);
1265
1266    $dir_links = "";
1267    $page_link = "";
1268    $a_links=array("global" => $this->getAdminLink()."&amp;fAStat_defper=N&amp;fAStat_all=Y",
1269        "all" => $this->getAdminLink()."&amp;fAStat_defper=N&amp;fAStat_all=N",
1270        "year" => $this->getAdminLink()."&amp;fAStat_defper=N&amp;fAStat_all=N&amp;fAStat_year=$year",
1271        "month" => $this->getAdminLink()."&amp;fAStat_defper=N&amp;fAStat_all=N&amp;fAStat_year=$year&amp;fAStat_month=$month",
1272        "day" => $this->getAdminLink()."&amp;fAStat_defper=N&amp;fAStat_all=N&amp;fAStat_year=$year&amp;fAStat_month=$month&amp;fAStat_day=$day");
1273
1274    $img_links=array("all" => $this->getAdminLink()."-stats_by_image",
1275        "year" => $this->getAdminLink()."-stats_by_image&amp;fAStat_year=$year",
1276        "month" => $this->getAdminLink()."-stats_by_image&amp;fAStat_year=$year&amp;fAStat_month=$month",
1277        "day" => $this->getAdminLink()."-stats_by_image&amp;fAStat_year=$year&amp;fAStat_month=$month&amp;fAStat_day=$day");
1278
1279    /* navigation links */
1280    if($day!="")
1281    {
1282      $dir_links = $this->format_link(l10n("AStat_period_label_global"), $a_links["global"])." / ".$this->format_link(l10n("AStat_period_label_all"), $a_links["all"])." / ".$this->format_link($year, $a_links["year"])." / ".$this->format_link(l10n("AStat_month_of_year_".$month), $a_links["month"])." / ".l10n("AStat_day_of_week_".date("w",mktime(0, 0, 0, $month, $day, $year)))." $day";
1283      $page_link=$img_links["day"];
1284    }
1285    elseif($month!="")
1286    {
1287      $dir_links = $this->format_link(l10n("AStat_period_label_global"), $a_links["global"])." / ".$this->format_link(l10n("AStat_period_label_all"), $a_links["all"])." / ".$this->format_link($year, $a_links["year"])." / ".l10n("AStat_month_of_year_".$month);
1288      $page_link=$img_links["month"];
1289    }
1290    elseif($year!="")
1291    {
1292      $dir_links = $this->format_link(l10n("AStat_period_label_global"), $a_links["global"])." / ".$this->format_link(l10n("AStat_period_label_all"), $a_links["all"])." / ".$year;
1293      $page_link=$img_links["year"];
1294    }
1295    else
1296    {
1297      $dir_links = $this->format_link(l10n("AStat_period_label_global"), $a_links["global"])." / ".l10n("AStat_period_label_all");
1298      $page_link=$img_links["all"];
1299    }
1300
1301    if($ip!="")
1302    {
1303      $dir_links.= " [IP : $ip]";
1304    }
1305
1306
1307    if($nbpages>1) { $plural="s"; } else { $plural=""; }
1308    $pages_links=l10n("AStat_page".$plural."_label")." : ";
1309    for($i=1;$i<=$nbpages;$i++)
1310    {
1311      if($i==$pagenumber)
1312      { $pages_links.=" $i "; }
1313      else
1314      {
1315        if($ip!="") { $ip_link="&amp;fAStat_IP=$ip"; } else { $ip_link=""; }
1316        $pages_links.=$this->format_link(" $i ", $page_link."&amp;fAStat_SortImg=$sortimg&amp;fAStat_page_number=$i".$ip_link);
1317      }
1318    }
1319
1320
1321    /* navigation */
1322    $template_datas["L_STAT_TITLE"] = $dir_links;
1323    $template_datas["MAX_WIDTH"] = $max_width;
1324    $template_datas["NB_TOTAL_IMAGE"] = l10n('AStat_nb_total_image')." : $nbfoundrows";
1325    $template_datas["PAGES_LINKS"] = $pages_links;
1326    $template_datas["ASTAT_NFO_STAT"] = l10n("AStat_Nfo_Image");
1327    $template_datas["ASTAT_SORT_LABEL"] = l10n("AStat_SortImgLabel").strtolower(l10n("AStat_sortimg_$sortimg"));
1328
1329    $template_datas["ASTAT_LABEL_AStat_RefImageLabel"] = $this->format_link(l10n("AStat_RefImageLabel"), $page_link."&amp;fAStat_SortImg=catname");
1330    $template_datas["ASTAT_LABEL_Pictures_seen"] = $this->format_link(l10n("Pictures_seen"), $page_link."&amp;fAStat_SortImg=picture");
1331    $template_datas["ASTAT_LABEL_pct_Pictures_seen"] = l10n("pct_Pictures_seen");
1332
1333    if($seetimerequest=='true')
1334    {
1335      $template_datas["ASTAT_TIME_REQUEST"] = l10n('AStat_time_request_label').' '.$timerequest.'s';
1336    }
1337
1338    for($i=0;$i<count($stats);$i++)
1339    {
1340      $width_pages = ceil(($stats[$i]["NbVues"] * $max_width)/$stats[$i]["NbVuesMax"] );
1341      $width_img = ceil(($stats[$i]["PctImg"] * $max_width)/100 );
1342
1343      if($showthumb=='true')
1344      {
1345        $filethumb=DerivativeImage::thumb_url(array('id'=>$stats[$i]["ImgId"], 'path'=>$stats[$i]["ImgPath"]));
1346      }
1347      else
1348      {
1349        $filethumb='';
1350      }
1351
1352
1353      if($stats[$i]["IdCat"]>0)
1354      {
1355        $image_links = $this->format_link($stats[$i]["CatName"], PHPWG_ROOT_PATH."index.php?/category/".$stats[$i]["IdCat"])." / ";
1356        if($stats[$i]["ImgName"]!="")
1357        {
1358          $image_links.=$this->format_link($stats[$i]["ImgName"], PHPWG_ROOT_PATH."picture.php?/".$stats[$i]["ImgId"]."/category/".$stats[$i]["IdCat"]);
1359        }
1360        else
1361        {
1362          if($stats[$i]["ThumbFile"]!="")
1363          {
1364            $image_links.=$this->format_link("[ ".$stats[$i]["ThumbFile"]." ]", PHPWG_ROOT_PATH."picture.php?/".$stats[$i]["ImgId"]."/category/".$stats[$i]["IdCat"]);
1365          }
1366          else
1367          {
1368            $image_links=l10n('AStat_deleted_picture')." [ Id #".$stats[$i]["ImgId"]." ]";
1369          }
1370        }
1371      }
1372      else
1373      {
1374        $image_links = "<i>".l10n('AStat_section_label').' : ';
1375        if(l10n('AStat_section_'.$stats[$i]["CatName"])!='AStat_section_'.$stats[$i]["CatName"])
1376        {
1377          $image_links.=l10n('AStat_section_'.$stats[$i]["CatName"]);
1378        }
1379        else
1380        {
1381          $image_links.=sprintf(l10n('AStat_section_unknown'), $stats[$i]["CatName"]);
1382        }
1383
1384        $image_links.="</i> / ";
1385
1386        if($stats[$i]["ImgName"]!="")
1387        {
1388          $image_links.=$stats[$i]["ImgName"];
1389        }
1390        else
1391        {
1392          if($stats[$i]["ThumbFile"]!="")
1393          {
1394            $image_links.="[ ".$stats[$i]["ThumbFile"]." ]";
1395          }
1396          else
1397          {
1398            $image_links=l10n('AStat_deleted_picture')." [ Id #".$stats[$i]["ImgId"]." ]";
1399          }
1400        }
1401      }
1402
1403
1404
1405      $template_datarows[]=array(
1406        'THUMBJPG' => $filethumb,
1407        'IMAGE' => $image_links,
1408        'NBPICTURES' => $stats[$i]["NbVues"],
1409        'PCTPICTURES' => $stats[$i]["PctImg"],
1410        'WIDTH1' => $width_img,
1411        'WIDTH2' => $width_pages,
1412      );
1413    }
1414
1415    $template->assign('datas', $template_datas);
1416    $template->assign('datarows', $template_datarows);
1417    $template->assign_var_from_handle('ASTAT_BODY_PAGE', 'body_page');
1418  } // display_stat_by_image
1419
1420
1421
1422
1423  private function add_ip_to_filter($ip)
1424  {
1425    if(strpos($this->config['AStat_BlackListedIP'].",", $ip.",")===false)
1426    {
1427      ($this->config['AStat_BlackListedIP']!='')?$this->config['AStat_BlackListedIP'].=",":"";
1428      $this->config['AStat_BlackListedIP'].=$ip;
1429      $this->saveConfig();
1430    }
1431  }
1432
1433
1434  /*
1435    display config page
1436  */
1437  private function display_config()
1438  {
1439    global $template, $page;
1440
1441    $save_status=false;
1442
1443    if(isset($_POST['submit']))
1444    {
1445      if (true) // if(!is_adviser())
1446      {
1447        reset($this->config);
1448        while (list($key, $val) = each($this->config))
1449        {
1450          if(isset($_POST['f_'.$key]))
1451          {
1452            $this->config[$key] = $_POST['f_'.$key];
1453          }
1454        }
1455        if($this->saveConfig())
1456        {
1457          array_push($page['infos'], l10n('AStat_config_saved'));
1458        }
1459        else
1460        {
1461          array_push($page['errors'], l10n('AStat_adviser_not_authorized'));
1462        }
1463      }
1464      else
1465      {
1466        array_push($page['errors'], l10n('AStat_adviser_not_authorized'));
1467      }
1468    }
1469
1470    $template->set_filename('body_page', dirname(__FILE__).'/admin/astat_config.tpl');
1471
1472    $template_datas=array();
1473    $template_list_values=array();
1474    $template_list_labels=array();
1475
1476    //standards inputs zones
1477    reset($this->config);
1478    while (list($key, $val) = each($this->config))
1479    {
1480      $template_datas["f_".$key]=$val;
1481    }
1482
1483    //
1484    $template_datas['ajaxurl']=$this->getAdminLink();
1485
1486    // define selected item for lists zones
1487    $template_datas['AStat_periods_selected']=$this->config['AStat_default_period'];
1488    $template_datas['AStat_defaultsortcat_selected']=$this->config['AStat_DefaultSortCat'];
1489    $template_datas['AStat_defaultsortip_selected']=$this->config['AStat_DefaultSortIP'];
1490    $template_datas['AStat_defaultsortimg_selected']=$this->config['AStat_DefaultSortImg'];
1491
1492    $template_datas['AStat_showthumbcat_selected']=$this->config['AStat_ShowThumbCat'];
1493    $template_datas['AStat_showthumbimg_selected']=$this->config['AStat_ShowThumbImg'];
1494    $template_datas['AStat_UseBlackList_selected']=$this->config['AStat_UseBlackList'];
1495
1496    // making lists zones
1497    // default period
1498    reset($this->list_periods);
1499    while (list($key, $val) = each($this->list_periods))
1500    {
1501      $template_list_values['periods'][]=$val;
1502      $template_list_labels['periods'][]=l10n('AStat_PeriodPerDefault_'.$val);
1503    }
1504    // default category order
1505    reset($this->list_sortcat);
1506    while (list($key, $val) = each($this->list_sortcat))
1507    {
1508      $template_list_values['sortcat'][]=$val;
1509      $template_list_labels['sortcat'][]=l10n('AStat_sortcat_'.$val);
1510    }
1511    // default ip order
1512    reset($this->list_sortip);
1513    while (list($key, $val) = each($this->list_sortip))
1514    {
1515      $template_list_values['sortip'][]=$val;
1516      $template_list_labels['sortip'][]=l10n('AStat_sortip_'.$val);
1517    }
1518    // default picture order
1519    reset($this->list_sortimg);
1520    while (list($key, $val) = each($this->list_sortimg))
1521    {
1522      $template_list_values['sortimg'][]=$val;
1523      $template_list_labels['sortimg'][]=l10n('AStat_sortimg_'.$val);
1524    }
1525
1526    /* yes/no lists */
1527    $template_list_values['yesno'][]='true';
1528    $template_list_labels['yesno'][]=l10n('AStat_yesno_true');
1529    $template_list_values['yesno'][]='false';
1530    $template_list_labels['yesno'][]=l10n('AStat_yesno_false');
1531
1532    $template_list_values['enableddisabled'][]='true';
1533    $template_list_values['enableddisabled'][]='false';
1534    $template_list_values['enableddisabled'][]='invert';
1535    $template_list_labels['enableddisabled'][]=l10n('AStat_enableddisabled_true');
1536    $template_list_labels['enableddisabled'][]=l10n('AStat_enableddisabled_false');
1537    $template_list_labels['enableddisabled'][]=l10n('AStat_enableddisabled_invert');
1538
1539    $template_datas["L_STAT_TITLE"]=l10n('AStat_config_title');
1540
1541    $template->assign("datas", $template_datas);
1542    $template->assign("AStat_periods_list_values", $template_list_values['periods']);
1543    $template->assign("AStat_periods_list_labels", $template_list_labels['periods']);
1544    $template->assign("AStat_defaultsortip_list_values", $template_list_values['sortip']);
1545    $template->assign("AStat_defaultsortip_list_labels", $template_list_labels['sortip']);
1546    $template->assign("AStat_defaultsortcat_list_values", $template_list_values['sortcat']);
1547    $template->assign("AStat_defaultsortcat_list_labels", $template_list_labels['sortcat']);
1548    $template->assign("AStat_defaultsortimg_list_values", $template_list_values['sortimg']);
1549    $template->assign("AStat_defaultsortimg_list_labels", $template_list_labels['sortimg']);
1550    $template->assign("AStat_yesno_list_values", $template_list_values['yesno']);
1551    $template->assign("AStat_yesno_list_labels", $template_list_labels['yesno']);
1552    $template->assign("AStat_enableddisabled_list_values", $template_list_values['enableddisabled']);
1553    $template->assign("AStat_enableddisabled_list_labels", $template_list_labels['enableddisabled']);
1554
1555    $template->assign_var_from_handle('ASTAT_BODY_PAGE', 'body_page');
1556  } // display_config
1557
1558
1559
1560  /*
1561    display tools page
1562  */
1563  private function display_tools()
1564  {
1565    global $template, $page;
1566
1567    $action_result=array('result' => '', 'action' => '', 'msg' => '', 'nfo' => '');
1568
1569    $template->set_filename('body_page', dirname(__FILE__).'/admin/astat_tools.tpl');
1570
1571    $template_datas=array();
1572
1573    // >> PURGE HISTORY --------------------------------------------------------
1574    if(isset($_POST['apply_tool_purge_history']))
1575    {
1576      if (true) // if(!is_adviser())
1577      {
1578        $action_result['action']='AStat_tools_purge_history';
1579        $action_result['result']='false';
1580        $action_result['msg']='AStat_tools_result_ko';
1581
1582        $ok_to_purge=true;
1583        if($_REQUEST['fAStat_purge_history_type']=='bydate')
1584        {
1585          // may be this functionnality could be optimized with pcre functions
1586          $date=explode('/', $_REQUEST['fAStat_purge_history_date']);
1587          if(!isset($date[0])) { $date[0]=0; }
1588          if(!isset($date[1])) { $date[1]=0; }
1589          if(!isset($date[2])) { $date[2]=0; }
1590
1591          $purge_date=mktime(0,0,0,$date[1],$date[0],$date[2]);
1592          $fparam=date("Y-m-d", mktime(0,0,0,$date[1],$date[0],$date[2]));
1593
1594          if(date("d/m/Y", $purge_date)!=$_REQUEST['fAStat_purge_history_date'])
1595          {
1596            $action_result['nfo']='AStat_tools_invalid_date';
1597            $ok_to_purge=false;
1598          }
1599          elseif(date("Ymd", $purge_date)>=date("Ymd"))
1600          {
1601            $action_result['nfo']='AStat_tools_invalid_date2';
1602            $ok_to_purge=false;
1603          }
1604        }
1605        elseif($_REQUEST['fAStat_purge_history_type']=='byipid0')
1606        {
1607          $fparam=$this->config['AStat_BlackListedIP'];
1608        }
1609        else
1610        {
1611          $fparam="";
1612        }
1613
1614        if($ok_to_purge)
1615        {
1616          $result=$this->do_purge_history( $fparam, $_REQUEST['fAStat_purge_history_type']);
1617          if($result)
1618          {
1619            $action_result['result']='true';
1620            $action_result['msg']='AStat_tools_result_ok';
1621          }
1622        }
1623      }
1624      else
1625      {
1626        array_push($page['errors'], l10n('AStat_adviser_not_authorized'));
1627      }
1628    }
1629    // << PURGE HISTORY --------------------------------------------------------
1630
1631    // >> DELETED_PICTURE ------------------------------------------------------
1632    elseif(isset($_POST['apply_tool_deleted_picture_resync']))
1633    {
1634      if (true) // if(!is_adviser())
1635      {
1636        $action_result['action']='AStat_tools_deleted_picture';
1637        $action_result['result']='false';
1638        $action_result['msg']='AStat_tools_result_ko';
1639
1640        if($_POST['fAStat_tools_deleted_picture_action']=='prepare')
1641        {
1642          if($this->prepare_AStat_picture_table())
1643          {
1644            $action_result['result']='true';
1645            $action_result['msg']='AStat_tools_result_ok';
1646            $action_result['nfo']='AStat_tools_deleted_picture_ok0';
1647          }
1648          else
1649          {
1650            $action_result['nfo']='AStat_tools_deleted_picture_error0';
1651          }
1652        }
1653        elseif($_POST['fAStat_tools_deleted_picture_action']=='apply')
1654        {
1655          if($this->apply_AStat_picture_table())
1656          {
1657            $action_result['result']='true';
1658            $action_result['msg']='AStat_tools_result_ok';
1659            $action_result['nfo']='AStat_tools_deleted_picture_ok1';
1660          }
1661          else
1662          {
1663            $action_result['nfo']='AStat_tools_deleted_picture_error1';
1664          }
1665        }
1666      }
1667      else
1668      {
1669        array_push($page['errors'], l10n('AStat_adviser_not_authorized'));
1670      }
1671    }
1672    //
1673    elseif(isset($_POST['apply_tool_deleted_picture']))
1674    {
1675      if (true) // if(!is_adviser())
1676      {
1677        $action_result['action']='AStat_tools_deleted_picture';
1678        $action_result['result']='false';
1679        $action_result['msg']='AStat_tools_result_ko';
1680
1681        $nfo = $this->tools_deleted_picture('to0');
1682        if($nfo[0]==1)
1683        {
1684          $action_result['result']='true';
1685          $action_result['msg']='AStat_tools_result_ok';
1686        }
1687      }
1688      else
1689      {
1690        array_push($page['errors'], l10n('AStat_adviser_not_authorized'));
1691      }
1692    }
1693    // << DELETED_PICTURE ------------------------------------------------------
1694
1695    // >> DELETED_CATEGORY -----------------------------------------------------
1696    elseif(isset($_POST['apply_tool_deleted_category']))
1697    {
1698      if (true) // if(!is_adviser())
1699      {
1700        $action_result['action']='AStat_tools_deleted_category';
1701        $action_result['result']='false';
1702        $action_result['msg']='AStat_tools_result_ko';
1703
1704        $nfo = $this->tools_deleted_category('to0');
1705        if($nfo[0]==1)
1706        {
1707          $action_result['result']='true';
1708          $action_result['msg']='AStat_tools_result_ok';
1709        }
1710      }
1711      else
1712      {
1713        array_push($page['errors'], l10n('AStat_adviser_not_authorized'));
1714      }
1715    }
1716    // << DELETED_CATEGORY -----------------------------------------------------
1717
1718    // >> DELETED USER ---------------------------------------------------------
1719    elseif(isset($_POST['apply_tool_deleted_user']))
1720    {
1721      if (true) // if(!is_adviser())
1722      {
1723        $action_result['action']='AStat_tools_deleted_user';
1724        $action_result['result']='false';
1725        $action_result['msg']='AStat_tools_result_ko';
1726
1727        $nfo = $this->tools_deleted_user('resynchro');
1728        if($nfo[0]==1)
1729        {
1730          $action_result['result']='true';
1731          $action_result['msg']='AStat_tools_result_ok';
1732        }
1733      }
1734      else
1735      {
1736        array_push($page['errors'], l10n('AStat_adviser_not_authorized'));
1737      }
1738    }
1739    // << DELETED USER ---------------------------------------------------------
1740
1741
1742    // >> DISPLAY DELETED_PICTURE NFO ------------------------------------------
1743    $table_exists=$this->verify_AStat_picture_table_status();
1744    if($table_exists==true)
1745    {
1746      $template_datas["ASTAT_DELETED_PICTURE_DO_ACTION"] = "checked";
1747      $template_datas["ASTAT_DELETED_PICTURE_PREPARE"] = "disabled";
1748    }
1749    else
1750    {
1751      $template_datas["ASTAT_DELETED_PICTURE_PREPARE"]="checked";
1752      $template_datas["ASTAT_DELETED_PICTURE_DO_ACTION"]="disabled";
1753    }
1754
1755    $nfo=$this->count_AStat_picture_table();
1756    $template_datas["ASTAT_DELETED_PICTURE_NFO_NB"] = sprintf(l10n('AStat_tools_deleted_picture_nfo_nb'), $nfo[1], $nfo[2]);
1757
1758    $nfo = $this->tools_deleted_picture('analyse');
1759    if($nfo[0]>0)
1760    {
1761      $list='';
1762      for($i=0;$i<count($nfo[2]);$i++)
1763      {
1764        if($nfo[2][$i][0]>1) { $s='s'; } else { $s=''; }
1765        $list.="<li>image_id #".$nfo[2][$i][1]." : ".$nfo[2][$i][0]." ".l10n("AStat_event$s")."</li>";
1766      }
1767      $template_datas["ASTAT_DELETED_PICTURE_NFO"] = sprintf(l10n('AStat_tools_deleted_picture_nfo1'), $nfo[0], $nfo[1], $list);
1768      //function not yet implemented
1769      $template_datas['AStat_deleted_picture_submit0'] = 'yes';
1770    }
1771    else
1772    {
1773      $template_datas["ASTAT_DELETED_PICTURE_NFO"] = l10n('AStat_tools_deleted_picture_nfo2');
1774    }
1775    // << DISPLAY DELETED_PICTURE NFO ------------------------------------------
1776
1777    // >> DISPLAY DELETED_CATEGORY NFO -----------------------------------------
1778    $nfo = $this->tools_deleted_category('analyse');
1779    if($nfo[0]>0)
1780    {
1781      $list='';
1782      for($i=0;$i<count($nfo[2]);$i++)
1783      {
1784        if($nfo[2][$i][0]>1) { $s='s'; } else { $s=''; }
1785        $list.="<li>category_id #".$nfo[2][$i][1]." : ".$nfo[2][$i][0]." ".l10n("AStat_event$s")."</li>";
1786      }
1787      $template_datas["ASTAT_DELETED_CATEGORY_NFO"] = sprintf(l10n('AStat_tools_deleted_category_nfo1'), $nfo[0], $nfo[1], $list);
1788      //function not yet implemented
1789      $template_datas['AStat_deleted_category_submit0'] = 'yes';
1790    }
1791    else
1792    {
1793      $template_datas["ASTAT_DELETED_CATEGORY_NFO"] = l10n('AStat_tools_deleted_category_nfo2');
1794    }
1795    // << DISPLAY DELETED_CATEGORY NFO -----------------------------------------
1796
1797    // >> DISPLAY DELETED USER NFO ---------------------------------------------
1798    $nfo = $this->tools_deleted_user('analyse');
1799    if($nfo[0]>0)
1800    {
1801      $list='';
1802      for($i=0;$i<count($nfo[2]);$i++)
1803      {
1804        if($nfo[2][$i][0]>1) { $s='s'; } else { $s=''; }
1805        $list.="<li>user_id #".$nfo[2][$i][1]." : ".$nfo[2][$i][0]." ".l10n("AStat_event$s")."</li>";
1806      }
1807      $template_datas["ASTAT_DELETED_USER_NFO"] = sprintf(l10n('AStat_tools_deleted_user_nfo1'), $nfo[0], $nfo[1], $list);
1808      $template_datas['AStat_deleted_user_submit'] = 'yes';
1809    }
1810    else
1811    {
1812      $template_datas["ASTAT_DELETED_USER_NFO"] = l10n('AStat_tools_deleted_user_nfo2');
1813    }
1814    // << DISPLAY DELETED USER NFO ---------------------------------------------
1815
1816
1817    // >> DISPLAY GENERAL NFO --------------------------------------------------
1818    $nfo = $this->tools_general_nfo();
1819    if($nfo[0]>0)
1820    {
1821      $template_datas["ASTAT_GENERAL_NFO"] = sprintf(l10n('AStat_tools_general_nfo_nfo'),
1822              $nfo[0],
1823              $this->formatoctet($nfo[3]+$nfo[4], "A", " ", 2, true),
1824              $this->formatoctet($nfo[3], "A", " ", 2, true),
1825              $this->formatoctet($nfo[4], "A", " ", 2, true),
1826              date(l10n('AStat_date_time_format'), strtotime($nfo[2])),
1827              date(l10n('AStat_date_time_format'), strtotime($nfo[1])) );
1828      $template_datas["ASTAT_MINDATE"]=date("m/d/Y",strtotime($nfo[2]));
1829    }
1830
1831    $nfo=$this->purge_history_count_imageid0();
1832    $template_datas["ASTAT_PURGE_HISTORY_IMAGE_NFO"] = sprintf(l10n('AStat_tools_purge_history_imageid0'), $nfo);
1833    if($nfo==0)
1834    {
1835      $template_datas["ASTAT_PURGE_HISTORY_IMAGE_DISABLED"] = " disabled ";
1836    }
1837    else
1838    {
1839      $template_datas["ASTAT_PURGE_HISTORY_IMAGE_DISABLED"] = '';
1840    }
1841    $nfo=$this->purge_history_count_categoryid0();
1842    $template_datas["ASTAT_PURGE_HISTORY_CATEGORY_NFO"] = sprintf(l10n('AStat_tools_purge_history_categoryid0'), $nfo);
1843    if($nfo==0)
1844    {
1845      $template_datas["ASTAT_PURGE_HISTORY_CATEGORY_DISABLED"] = " disabled ";
1846    }
1847    else
1848    {
1849      $template_datas["ASTAT_PURGE_HISTORY_CATEGORY_DISABLED"] = '';
1850    }
1851    $nfo=$this->purge_history_count_ipid0();
1852    $template_datas["ASTAT_PURGE_HISTORY_IP_NFO"] = sprintf(l10n('AStat_tools_purge_history_ipid0'), $nfo[1], $nfo[0]);
1853    if($nfo[0]==0)
1854    {
1855      $template_datas["ASTAT_PURGE_HISTORY_IP_DISABLED"] = " disabled ";
1856    }
1857    else
1858    {
1859      $template_datas["ASTAT_PURGE_HISTORY_IP_DISABLED"] = '';
1860    }
1861    // << GENERAL NFO ----------------------------------------------------------
1862
1863
1864    if($action_result['result']!='')
1865    {
1866      if($action_result['result']=='true')
1867      {
1868        $value="<p class='infos'>";
1869      }
1870      else
1871      {
1872        $value="<p class='errors'>";
1873      }
1874      $value.="<i>".l10n($action_result['action'])."</i><br>".l10n($action_result['msg'])."<br>";
1875
1876      if($action_result['nfo']!='')
1877      {
1878        $value.="[".l10n($action_result['nfo'])."]</p>";
1879      }
1880
1881      $template_datas["ASTAT_RESULT_OK"] = $value;
1882    }
1883
1884    $template_datas["L_STAT_TITLE"] = l10n('AStat_tools_title');
1885
1886    $template->assign('datas', $template_datas);
1887    $template->assign_var_from_handle('ASTAT_BODY_PAGE', 'body_page');
1888  } // display_tools
1889
1890
1891  /*
1892    tools functions
1893  */
1894
1895
1896
1897  /*
1898    tools : deleted_user
1899    allow to force HISTORY_TABLE.user_id at  2 (guest) for records with user ident
1900    doesn't exist anymore in the USERS_TABLE
1901
1902    Two usages :
1903      - analyse : return infos about records wich need to be updated
1904          * number of users
1905          * number of records in HISTORY_TABLE
1906          * the users list
1907      - resynchro : update table
1908  */
1909  private function tools_deleted_user($mode)
1910  {
1911    $returned = array(-1,0,'');
1912
1913    if($mode=='analyse')
1914    {
1915      $sql="SELECT count(id) as NbRecord, user_id ";
1916      $sql.=" FROM ".HISTORY_TABLE;
1917      $sql.=" WHERE ".HISTORY_TABLE.".user_id NOT IN (SELECT id FROM ".USERS_TABLE.") ";
1918      $sql.=" GROUP BY user_id ORDER BY NbRecord";
1919      $result=pwg_query($sql);
1920      if($result)
1921      {
1922        $returned[0]=0;
1923        while ($row = pwg_db_fetch_row($result))
1924        {
1925          $returned[2][$returned[0]][0] = $row[0];
1926          $returned[2][$returned[0]][1] = $row[1];
1927          $returned[0]++;
1928          $returned[1]+=$row[0];
1929        }
1930
1931      }
1932    }
1933    elseif($mode=='resynchro')
1934    {
1935      $sql="UPDATE ".HISTORY_TABLE." SET user_id = 2 ";
1936      $sql.=" WHERE ".HISTORY_TABLE.".user_id NOT IN (SELECT id FROM ".USERS_TABLE.") ";
1937      $result=pwg_query($sql);
1938      if($result)
1939      { $returned[0]=1; }
1940    }
1941    return($returned);
1942  }
1943
1944
1945  /*
1946    tools : deleted_picture
1947    analyse history to find deleted pictures
1948    Two functions :
1949      - analyse : return infos
1950          * number of pictures
1951          * number of record in HISTORY_TABLE
1952          * pictures list
1953      - to0 : update picture ident to  0
1954  */
1955  private function tools_deleted_picture($mode)
1956  {
1957    $returned = array(-1,0,'');
1958
1959    if($mode=='analyse')
1960    {
1961      $sql="SELECT count(id) as NbRecord, image_id ";
1962      $sql.=" FROM ".HISTORY_TABLE;
1963      $sql.=" WHERE ".HISTORY_TABLE.".image_id NOT IN (SELECT id FROM ".IMAGES_TABLE.") and image_id > 0 ";
1964      $sql.=" GROUP BY image_id ORDER BY NbRecord";
1965      $result=pwg_query($sql);
1966
1967      if($result)
1968      {
1969        $returned[0]=0;
1970        while ($row = pwg_db_fetch_row($result))
1971        {
1972          $returned[2][$returned[0]][0] = $row[0];
1973          $returned[2][$returned[0]][1] = $row[1];
1974          $returned[0]++;
1975          $returned[1]+=$row[0];
1976        }
1977
1978      }
1979    }
1980    elseif($mode=='to0')
1981    {
1982      $sql="UPDATE ".HISTORY_TABLE."
1983        SET image_id = 0
1984        WHERE ".HISTORY_TABLE.".image_id > 0
1985          AND ".HISTORY_TABLE.".image_id NOT IN (SELECT id FROM ".IMAGES_TABLE.")";
1986      $result=pwg_query($sql);
1987
1988      if($result)
1989      {
1990        $returned[0]=1;
1991      }
1992    }
1993    return($returned);
1994  }
1995
1996
1997  /*
1998    tools : deleted_category
1999    analyse history to find deleted categories
2000    Two functions :
2001      - analyse : return infos
2002          * number of category
2003          * number of record in HISTORY_TABLE
2004          * catgories list
2005      - to0 : update categories ident to 0
2006  */
2007  private function tools_deleted_category($mode)
2008  {
2009    $returned = array(-1,0,'');
2010
2011    if($mode=='analyse')
2012    {
2013      $sql="SELECT count(id) as NbRecord, category_id ";
2014      $sql.=" FROM ".HISTORY_TABLE;
2015      $sql.=" WHERE ".HISTORY_TABLE.".category_id NOT IN (SELECT id FROM ".CATEGORIES_TABLE.") and category_id > 0";
2016      $sql.=" GROUP BY category_id ORDER BY NbRecord";
2017      $result=pwg_query($sql);
2018
2019      if($result)
2020      {
2021        $returned[0]=0;
2022        while ($row = pwg_db_fetch_row($result))
2023        {
2024          $returned[2][$returned[0]][0] = $row[0];
2025          $returned[2][$returned[0]][1] = $row[1];
2026          $returned[0]++;
2027          $returned[1]+=$row[0];
2028        }
2029
2030      }
2031    }
2032    elseif($mode=='to0')
2033    {
2034      $sql="UPDATE ".HISTORY_TABLE."
2035        SET category_id = NULL, section = 'deleted_cat'
2036        WHERE ".HISTORY_TABLE.".category_id > 0
2037          AND ".HISTORY_TABLE.".category_id NOT IN (SELECT id FROM ".CATEGORIES_TABLE.")";
2038      $result=pwg_query($sql);
2039
2040      if($result)
2041      {
2042        $returned[0]=1;
2043      }
2044    }
2045    return($returned);
2046  }
2047
2048
2049  /*
2050    tools : general_nfo
2051    return infos about historic
2052      0 : nulber of records
2053      1 : date of newest record
2054      2 : date of oldesr record
2055      3 : table size
2056      4 : index size
2057  */
2058  private function tools_general_nfo()
2059  {
2060    $returned = array(-1,'','',0,0);
2061
2062    $sql="SELECT count(id) AS NbRecord, MAX(concat(date,' ', time)) AS LastDate, MIN(concat(date,' ', time)) AS FirstDate ";
2063    $sql.=" FROM ".HISTORY_TABLE;
2064    $result=pwg_query($sql);
2065    if($result)
2066    {
2067      $row = pwg_db_fetch_row($result);
2068      if(is_array($row))
2069      {
2070        $returned = $row;
2071        $sql="SHOW TABLE STATUS LIKE '".HISTORY_TABLE."';";
2072        $result=pwg_query($sql);
2073        if($result)
2074        {
2075          $row2=pwg_db_fetch_assoc($result);
2076          array_push($returned, $row2['Data_length'], $row2['Index_length']);
2077        }
2078      }
2079    }
2080    return($returned);
2081  }
2082
2083
2084  /*
2085    tools : do_purge_history
2086    do a purge of history table :
2087    - $purgetype='bydate' : purge all record wich date is less than given date
2088    - $purgetype='byimageid0' : with and image_id = 0
2089  ------------------------------------------------------------------------------------ */
2090  private function do_purge_history($param, $purgetype)
2091  {
2092    if($purgetype=='bydate')
2093    {
2094      $sql="DELETE FROM ".HISTORY_TABLE." WHERE date < '$param'";
2095    }
2096    elseif($purgetype=='byimageid0')
2097    {
2098      $sql="DELETE FROM ".HISTORY_TABLE." WHERE image_id = 0";
2099    }
2100    elseif($purgetype=='bycategoryid0')
2101    {
2102      $sql="DELETE FROM ".HISTORY_TABLE." WHERE category_id is null and section='deleted_cat'";
2103    }
2104    elseif($purgetype=='byipid0')
2105    {
2106      $sql="DELETE FROM ".HISTORY_TABLE." WHERE ".$this->make_IP_where_clause($param);
2107    }
2108    else
2109    {
2110      return(false);
2111    }
2112
2113    $result=pwg_query($sql);
2114    if($result)
2115    {
2116      $sql="OPTIMIZE TABLE ".HISTORY_TABLE;
2117      $result=pwg_query($sql);
2118      return($result);
2119    }
2120    return(false);
2121  }
2122
2123  private function purge_history_count_imageid0()
2124  {
2125    $sql="SELECT COUNT(id) FROM ".HISTORY_TABLE." WHERE image_id = 0";
2126    $result=pwg_query($sql);
2127    if($result)
2128    {
2129      $row=pwg_db_fetch_row($result);
2130      return($row[0]);
2131    }
2132    return(0);
2133  }
2134
2135  private function purge_history_count_categoryid0()
2136  {
2137    $sql="SELECT COUNT(id) FROM ".HISTORY_TABLE." WHERE category_id is null and section = 'deleted_cat'" ;
2138    $result=pwg_query($sql);
2139    if($result)
2140    {
2141      $row=pwg_db_fetch_row($result);
2142      return($row[0]);
2143    }
2144    return(0);
2145  }
2146
2147  private function purge_history_count_ipid0()
2148  {
2149    if($this->config['AStat_BlackListedIP']!="")
2150    {
2151      $list=explode(',', $this->config['AStat_BlackListedIP']);
2152    }
2153    else
2154    {
2155      $list=array();
2156    }
2157
2158    $returned=array(0,count($list));
2159
2160    if($this->config['AStat_BlackListedIP']!='')
2161    {
2162      $sql="SELECT COUNT(id)
2163            FROM ".HISTORY_TABLE."
2164            WHERE ".$this->make_IP_where_clause($this->config['AStat_BlackListedIP']);
2165      $result=pwg_query($sql);
2166      if($result)
2167      {
2168        $row=pwg_db_fetch_row($result);
2169        $returned[0]=$row[0];
2170      }
2171    }
2172    return($returned);
2173  }
2174
2175
2176  /*
2177    tools : deleted_picture
2178    > verify_AStat_picture_table_status :
2179    > prepare_AStat_picture_table :
2180  */
2181  private function verify_AStat_picture_table_status()
2182  {
2183    global $prefixeTable;
2184
2185    $sql="SHOW TABLE STATUS LIKE '".$prefixeTable."AStat_picture'";
2186    $result=pwg_query($sql);
2187    if($result)
2188    {
2189      if(pwg_db_num_rows($result)==1)
2190      {
2191        return(true);
2192      }
2193    }
2194    return(false);
2195  }
2196
2197  private function prepare_AStat_picture_table()
2198  {
2199    global $prefixeTable;
2200
2201    $sql="CREATE TABLE ".$prefixeTable."AStat_picture (PRIMARY KEY (id), KEY ifile(file))
2202      SELECT id, file
2203      FROM ".IMAGES_TABLE;
2204    $result=pwg_query($sql);
2205    if($result)
2206    {
2207      return(true);
2208    }
2209    return(false);
2210  }
2211
2212  private function count_AStat_picture_table()
2213  {
2214    global $prefixeTable;
2215    $returned=array(false,0,0);
2216
2217    if($this->verify_AStat_picture_table_status())
2218    {
2219      $sql="SELECT count(DISTINCT ".$prefixeTable."AStat_picture.id), count(".HISTORY_TABLE.".id)
2220        FROM ".HISTORY_TABLE.", ".$prefixeTable."AStat_picture
2221        WHERE ".HISTORY_TABLE.".image_id = ".$prefixeTable."AStat_picture.id
2222        AND ".HISTORY_TABLE.".image_id NOT IN (SELECT id FROM ".IMAGES_TABLE.")
2223        ORDER BY ".$prefixeTable."AStat_picture.id";
2224      $result=pwg_query($sql);
2225      if($result)
2226      {
2227        if(pwg_db_num_rows($result)>0)
2228        {
2229          $row=pwg_db_fetch_row($result);
2230          $returned[0]=true;
2231          $returned[1]=$row[0];
2232          $returned[2]=$row[1];
2233        }
2234      }
2235    }
2236    return($returned);
2237  }
2238
2239  private function apply_AStat_picture_table()
2240  {
2241    global $prefixeTable;
2242    $returned=false;
2243
2244    $sql="CREATE TABLE ".$prefixeTable."AStat_picture2 (PRIMARY KEY (OldId))
2245      SELECT AStat_tmp.id as OldId , ".IMAGES_TABLE.".id as NewId, ".IMAGES_TABLE.".storage_category_id as NewCatId
2246      FROM (SELECT DISTINCT ".$prefixeTable."AStat_picture.*
2247        FROM ".HISTORY_TABLE.", ".$prefixeTable."AStat_picture
2248        WHERE ".HISTORY_TABLE.".image_id = ".$prefixeTable."AStat_picture.id
2249        AND ".HISTORY_TABLE.".image_id NOT IN (SELECT id FROM ".IMAGES_TABLE.")
2250        ORDER BY ".$prefixeTable."AStat_picture.id
2251      ) as AStat_tmp
2252      LEFT JOIN ".IMAGES_TABLE." ON AStat_tmp.file = ".IMAGES_TABLE.".file";
2253    $result=pwg_query($sql);
2254    if($result)
2255    {
2256      $sql="UPDATE ".HISTORY_TABLE.", ".$prefixeTable."AStat_picture2, ".IMAGES_TABLE."
2257        SET ".HISTORY_TABLE.".image_id = ".$prefixeTable."AStat_picture2.NewId,
2258            ".HISTORY_TABLE.".category_id = ".$prefixeTable."AStat_picture2.NewCatId
2259        WHERE ".$prefixeTable."AStat_picture2.OldId = ".HISTORY_TABLE.".image_id";
2260      $result=pwg_query($sql);
2261      if($result)
2262      {
2263        $returned=true;
2264      }
2265      $sql="DROP TABLE IF EXISTS ".$prefixeTable."AStat_picture2";
2266      $result=pwg_query($sql);
2267      $sql="DROP TABLE IF EXISTS ".$prefixeTable."AStat_picture";
2268      $result=pwg_query($sql);
2269    }
2270    return($returned);
2271  }
2272
2273
2274
2275
2276  /*
2277    generics functions
2278  */
2279
2280  /*
2281    this function make filter list and apply template
2282
2283      $selected : category_id of selected item ("" if none)
2284  */
2285  private function make_filter_list($selected="")
2286  {
2287    global $template;
2288
2289    $template_datarows_values=array();
2290    $template_datarows_labels=array();
2291    $template_hiddenrows=array();
2292
2293
2294    $template_datarows_values[]="";
2295    $template_datarows_labels[]=l10n("AStat_nofilter");
2296
2297    $sql="SELECT id, name, global_rank FROM ".CATEGORIES_TABLE." order by global_rank";
2298    $result = pwg_query($sql);
2299    if($result)
2300    {
2301      while ($row = pwg_db_fetch_assoc($result))
2302      {
2303        $text=str_repeat('&nbsp;&nbsp;', substr_count($row['global_rank'], '.'));
2304        $template_datarows_values[]=$row['id'];
2305        $template_datarows_labels[]=$text.$row['name'];
2306      }
2307    }
2308
2309    $query=explode('&', $_SERVER['QUERY_STRING']);
2310    foreach($query as $key => $value)
2311    {
2312      $nfo=explode('=', $value);
2313      $template_hiddenrows[]=array(
2314          'VALUE' => urldecode($nfo[1]),
2315          'NAME' => $nfo[0]
2316      );
2317    }
2318
2319    $template->assign('ASTAT_LINK', $_SERVER['SCRIPT_NAME']);
2320    $template->assign('f_AStat_catfilter_list_values', $template_datarows_values);
2321    $template->assign('f_AStat_catfilter_list_labels', $template_datarows_labels);
2322    $template->assign('f_AStat_catfilter_selected', $selected);
2323    $template->assign('f_AStat_parameters', $template_hiddenrows);
2324  } //make_filter_list
2325
2326
2327  /*
2328    this function make SELECT "WHERE" clause for filter list
2329
2330      $selected : category_id of selected item ("" if none)
2331  */
2332  private function make_where_clause($catfilter)
2333  {
2334    $returned=array();
2335    $sql="SELECT id, ".CATEGORIES_TABLE.".global_rank
2336          FROM ".CATEGORIES_TABLE.",
2337               (SELECT global_rank FROM ".CATEGORIES_TABLE." WHERE id = '".$catfilter."') as tmp1
2338          WHERE ".CATEGORIES_TABLE.".global_rank LIKE CONCAT(tmp1.global_rank, '%')
2339          ORDER BY ".CATEGORIES_TABLE.".global_rank";
2340    $result = pwg_query($sql);
2341
2342    if($result)
2343    {
2344      while ($row = pwg_db_fetch_row($result))
2345      {
2346        $returned[]=$row[0];
2347      }
2348    }
2349
2350    return(implode(',', $returned));
2351  }
2352
2353
2354  /*
2355    format text : <a href="$link">$value</a>
2356  */
2357  private function format_link($value, $link)
2358  {
2359    return("<a href='$link'>$value</a>");
2360  }
2361
2362  /*
2363    return true if IP adress is given
2364  */
2365  private function is_IP($ip)
2366  {  //basic test, maybe a pcre will be more appropriate...
2367    $tmp=explode('.', $ip);
2368    if(count($tmp)!=4)
2369    { return (false); }
2370
2371    for($i=0;$i<4;$i++)
2372    {
2373      if(!is_numeric($tmp[$i])) { return (false); }
2374    }
2375    return (true);
2376  }
2377
2378  /*
2379      format number $octets with unit
2380      $format = "A" : auto
2381                "O" : o
2382                "K" : Ko
2383                "M" : Mo
2384                "G" : Go
2385      $thsep = thousand separator
2386      $prec = number of decimal
2387      $unitevis = true/false > renvoi l'unité ou non dans le résultat
2388  */
2389  private function formatoctet($octets, $format="A", $thsep=" ", $prec=2, $unitevis=true)
2390  {
2391    if($format=="A")
2392    {
2393    if($octets<1024)
2394    { $format="O"; }
2395    elseif($octets<1024000)
2396    { $format="K"; }
2397    elseif($octets<1024000000)
2398    { $format="M"; }
2399    else
2400    { $format="G"; }
2401    }
2402    switch($format)
2403    {
2404    case "O":
2405      $unite="o"; $div=1;
2406      break;
2407    case "K":
2408      $unite="Ko"; $div=1024;
2409      break;
2410    case "M":
2411      $unite="Mo"; $div=1024000;
2412      break;
2413    case "G":
2414      $unite="Go"; $div=1024000000;
2415      break;
2416    }
2417
2418    $retour=number_format($octets/$div, $prec, '.', $thsep);
2419    if($unitevis)
2420    { $retour.=" ".$unite; }
2421    return($retour);
2422  }
2423
2424  private function make_IP_where_clause($list)
2425  {
2426    $returned="";
2427
2428    $tmp=explode(",", $list);
2429    foreach($tmp as $key=>$val)
2430    {
2431      if($returned!="") { $returned.=" OR "; }
2432      $returned.=" IP LIKE '".$val."' ";
2433    }
2434    if($returned!="")
2435    {
2436      $returned ="(".$returned.")";
2437    }
2438    return($returned);
2439  }
2440
2441
2442  /* ---------------------------------------------------------------------------
2443   * AJAX functions
2444   * ------------------------------------------------------------------------- */
2445  protected function ajax_listip($filter, $exclude)
2446  {
2447    $sql="SELECT IP, COUNT(id) as NbEvents FROM ".HISTORY_TABLE;
2448
2449    $where=array();
2450    if($filter!="")
2451    {
2452      $where[]=" IP LIKE '".$filter."' ";
2453    }
2454    if($exclude!="")
2455    {
2456      $where[]=" NOT ".$this->make_IP_where_clause($exclude);
2457    }
2458    if(count($where)>0)
2459    {
2460      $sql.=" WHERE ".implode(" AND ", $where);
2461    }
2462    $sql.=" GROUP BY IP ORDER BY NbEvents desc, IP asc LIMIT 0,100";
2463
2464    $list="<select multiple id='iipsellist'>";
2465    $result=pwg_query($sql);
2466    if($result)
2467    {
2468      while($row=pwg_db_fetch_assoc($result))
2469      {
2470        $list.="<option value='".$row['IP']."'>".$row['IP'].str_repeat("&nbsp;", 15-strlen($row['IP']))."&nbsp;&nbsp;&nbsp;&nbsp;(".$row['NbEvents'].")</option>";
2471      }
2472    }
2473    $list.="</select>";
2474
2475    return($list);
2476  }
2477
2478
2479} // AStat_AI class
2480
2481
2482?>
Note: See TracBrowser for help on using the repository browser.