source: extensions/meta/main.inc.php @ 32308

Last change on this file since 32308 was 32308, checked in by ddtddt, 3 years ago

[meta] php7.4 Notice

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 9.1 KB
Line 
1<?php
2/*
3Plugin Name: meta
4Version: auto
5Description: Allows to add metadata
6Plugin URI: http://piwigo.org/ext/extension_view.php?eid=220
7Author: ddtddt
8Author URI: http://temmii.com/piwigo/
9*/
10
11// +-----------------------------------------------------------------------+
12// | meta plugin for Piwigo by TEMMII                                      |
13// +-----------------------------------------------------------------------+
14// | Copyright(C) 2008-2020 ddtddt               http://temmii.com/piwigo/ |
15// +-----------------------------------------------------------------------+
16// | This program is free software; you can redistribute it and/or modify  |
17// | it under the terms of the GNU General Public License as published by  |
18// | the Free Software Foundation                                          |
19// |                                                                       |
20// | This program is distributed in the hope that it will be useful, but   |
21// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
22// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
23// | General Public License for more details.                              |
24// |                                                                       |
25// | You should have received a copy of the GNU General Public License     |
26// | along with this program; if not, write to the Free Software           |
27// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
28// | USA.                                                                  |
29// +-----------------------------------------------------------------------+
30
31if (!defined('PHPWG_ROOT_PATH'))
32    die('Hacking attempt!');
33
34global $prefixeTable, $page;
35
36define('meta_DIR', basename(dirname(__FILE__)));
37define('meta_PATH', PHPWG_PLUGINS_PATH . meta_DIR . '/');
38define('meta_TABLE', $prefixeTable . 'meta');
39define('meta_img_TABLE', $prefixeTable . 'meta_img');
40define('meta_cat_TABLE', $prefixeTable . 'meta_cat');
41if (!defined('METAPERSO_TABLE'))
42  define('METAPERSO_TABLE', $prefixeTable . 'metaperso');
43if (!defined('META_AP_TABLE'))
44  define('META_AP_TABLE', $prefixeTable . 'meta_ap');
45define('META_ADMIN',get_root_url().'admin.php?page=plugin-'.meta_DIR);
46
47add_event_handler('loading_lang', 'meta_loading_lang');   
48function meta_loading_lang(){
49  load_language('plugin.lang', meta_PATH);
50}
51
52// Plugin for admin
53if (script_basename() == 'admin') {
54    include_once(dirname(__FILE__) . '/initadmin.php');
55}
56
57//Gestion des meta dans le header
58add_event_handler('loc_begin_page_header', 'Change_Meta', 20);
59add_event_handler('loc_end_page_header', 'add_meta', 56);
60add_event_handler('loc_end_page_header', 'add_metacat', 61);
61add_event_handler('loc_end_page_header', 'add_metaimg', 71);
62add_event_handler('loc_after_page_header', 'set_meta_back');
63
64function Change_Meta(){
65  global $template, $pwg_loaded_plugins;
66  $template->set_prefilter('header', 'upmata');
67  if (isset($pwg_loaded_plugins['ExtendedDescription'])){
68    add_event_handler('AP_render_content', 'get_user_language_desc');
69  }
70}
71
72function upmata($content, &$smarty){
73  $search = '#<meta name="description" content=".*?">#';
74  $replacement = '<meta name="description" content="{$PLUG_META}">';
75  return preg_replace($search, $replacement, $content);
76}
77
78function add_meta(){
79  global $template, $page, $meta_infos, $pwg_loaded_plugins;
80  $meta_infos = array();
81  $meta_infos['author'] = $template->get_template_vars('INFO_AUTHOR');
82  $meta_infos['related_tags'] = $template->get_template_vars('related_tags');
83  $meta_infos['info'] = $template->get_template_vars('INFO_FILE');
84  $meta_infos['title'] = $template->get_template_vars('PAGE_TITLE');
85
86  $query = 'SELECT id,metaname,metaval FROM ' . meta_TABLE . ' WHERE metaname IN (\'author\', \'keywords\', \'Description\', \'robots\');';
87  $result = pwg_query($query);
88  $meta = array();
89  while ($row = pwg_db_fetch_assoc($result)){
90    $meta[$row['metaname']] = $row['metaval'];
91    $metaED[$row['metaname']] = trigger_change('AP_render_content', $meta[$row['metaname']]);
92  }
93
94  // Authors
95  if (!empty($meta_infos['author']) and ! empty($metaED['author'])){
96    $template->assign('INFO_AUTHOR', $meta_infos['author'] . ' - ' . $metaED['author']);
97  } elseif (!empty($metaED['author'])){
98    $template->assign('INFO_AUTHOR', $metaED['author']);
99  }
100
101  // Keywords
102  if (!empty($metaED['keywords'])){
103    $template->append('related_tags', array('name' => $metaED['keywords']));
104  }
105
106  // Description
107  if (!empty($meta_infos['title']) and ! empty($meta_infos['info']) and ! empty($metaED['Description'])) {
108    $template->assign('PLUG_META', $meta_infos['title'] . ' - ' . $meta_infos['info'] . ', ' . $metaED['Description']);
109  } elseif (!empty($meta_infos['title']) and ! empty($metaED['Description'])) {
110    $template->assign('PLUG_META', $meta_infos['title'] . ' - ' . $metaED['Description']);
111  } elseif (!empty($metaED['Description'])) {
112    $template->assign('PLUG_META', $metaED['Description']);
113  }
114
115  // Robots
116  if (!empty($meta['robots'])) {
117    $template->append('head_elements', '<meta name="robots" content="' . $meta['robots'] . '">');
118  }
119
120  //Metaperso
121  if (script_basename() !== 'admin') {
122    $metapersos = pwg_query("SELECT * FROM " . METAPERSO_TABLE . ";");
123
124    if (pwg_db_num_rows($metapersos)) {
125      while ($metaperso = pwg_db_fetch_assoc($metapersos)) {
126        $items = array(
127          'METANAME' => $metaperso['metaname'],
128          'METAVAL' => $metaperso['metaval'],
129          'METATYPE' => $metaperso['metatype']
130        );
131        $template->append('metapersos', $items);
132      }
133    }
134
135    $template->set_filename('PERSO_META', realpath(meta_PATH . 'persometa.tpl'));
136    $template->append('head_elements', $template->parse('PERSO_META', true));
137  }
138
139  if (isset($pwg_loaded_plugins['ContactForm'])){
140    global $conf;
141    if (isset($page['section']) and $page['section'] == 'contact' and isset($conf['contactmeta']) and strpos($conf['contactmeta'], ',') !== false){
142      $metacontact = explode(',', $conf['contactmeta']);
143      $metakeyED = trigger_change('AP_render_content', $metacontact[0]);
144      $metadesED = trigger_change('AP_render_content', $metacontact[1]);
145      if (!empty($metakeyED)){
146        $template->append('related_tags', array('name' => $metakeyED));
147      }
148      if (!empty($metadesED)){
149        $template->assign('PLUG_META', $metadesED);
150      }
151    }
152  }
153
154  if (isset($pwg_loaded_plugins['AdditionalPages'])){
155    if (!empty($page['additional_page']['id'])) {
156      $lire = $page['additional_page']['id'];
157      $query = 'SELECT id, metaKeyap, metadesap FROM ' . META_AP_TABLE . ' WHERE id = \'' . $lire . '\';';
158          $result = pwg_query($query);
159          $row = pwg_db_fetch_assoc($result);
160          $metaKeyapap = $row['metaKeyap'];
161          $metadesapap = $row['metadesap'];
162          $metaKeyapapED = trigger_change('AP_render_content', $metaKeyapap);
163          $metadesapED = trigger_change('AP_render_content', $metadesapap);
164    }
165        if (isset($page['section']) and $page['section'] == 'additional_page') {
166          if (!empty($metaKeyapapED)) {
167                $template->append('related_tags', array('name' => $metaKeyapapED));
168          }
169          if (!empty($metadesapED)) {
170                $template->assign('PLUG_META', $metadesapED);
171          }
172        }
173  }
174}
175
176function add_metacat() {
177  global $template, $page, $meta_infos;
178  if (!empty($page['category']['id'])) {
179    $query = 'SELECT id,metaKeycat,metadescat FROM ' . meta_cat_TABLE . ' WHERE id = \'' . $page['category']['id'] . '\';';
180        $result = pwg_query($query);
181        $row = pwg_db_fetch_assoc($result);
182        $albumKeyED = trigger_change('AP_render_content', $row['metaKeycat']);
183        if (!empty($row['metaKeycat'])) {
184          $template->append('related_tags', array('name' => $albumKeyED));
185        }
186        $albumDesED = trigger_change('AP_render_content', $row['metadescat']);
187        if (!empty($row['metadescat'])) {
188          $template->assign('PLUG_META', $albumDesED);
189        }
190  }
191}
192
193function add_metaimg(){
194  global $template, $page, $meta_infos;
195  if (!empty($page['image_id'])) {
196    $query = 'SELECT id,metaKeyimg,metadesimg FROM ' . meta_img_TABLE . ' WHERE id = \'' . $page['image_id'] . '\';';
197        $result = pwg_query($query);
198        $row = pwg_db_fetch_assoc($result);
199        $photoKeyED = trigger_change('AP_render_content', $row['metaKeyimg']);
200        if (!empty($row['metaKeyimg'])) {
201          $template->append('related_tags', array('name' => $photoKeyED));
202        }
203        $photoDesED = trigger_change('AP_render_content', $row['metadesimg']);
204        if (!empty($row['metadesimg'])) {
205          $template->assign('PLUG_META', $photoDesED);
206        }else{
207          $meta_infosph = array();
208          $meta_infosph['title'] = $template->get_template_vars('PAGE_TITLE');
209          $meta_infosph['gt'] = $template->get_template_vars('GALLERY_TITLE');
210          $meta_infosph['descimg'] = $template->get_template_vars('COMMENT_IMG');
211          if (!empty($meta_infosph['descimg'])) {
212                $template->assign('PLUG_META', strip_tags($meta_infosph['descimg']) . ' - ' . $meta_infosph['title']);
213          }else{
214                $template->assign('PLUG_META', $meta_infosph['title'] . ' - ' . $meta_infosph['gt']);
215          }
216        }
217  }
218}
219
220function set_meta_back(){
221  global $template, $meta_infos;
222  $template->assign(
223    array(
224      'INFO_AUTHOR' => $meta_infos['author'],
225      'related_tags' => $meta_infos['related_tags'],
226      'INFO_FILE' => $meta_infos['info'],
227    )
228   );
229}
230
231?>
Note: See TracBrowser for help on using the repository browser.