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

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

[meta] compatibility piwigo12

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