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

Last change on this file since 31844 was 31491, checked in by ddtddt, 8 years ago

[extensions] - meta - 2.8

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 9.3 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                                                |
13// +-----------------------------------------------------------------------+
14// | Copyright(C) 2008-2016 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;
66  $template->set_prefilter('header', 'upmata');
67  $PAED = pwg_db_fetch_assoc(pwg_query("SELECT state FROM " . PLUGINS_TABLE . " WHERE id = 'ExtendedDescription';"));
68  if ($PAED['state'] == 'active')
69    add_event_handler('AP_render_content', 'get_user_language_desc');
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;
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  $MPC = pwg_db_fetch_assoc(pwg_query("SELECT state FROM " . PLUGINS_TABLE . " WHERE id = 'ContactForm';"));
140  if ($MPC['state'] == 'active'){
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  $MAP = pwg_db_fetch_assoc(pwg_query("SELECT state FROM " . PLUGINS_TABLE . " WHERE id = 'AdditionalPages';"));
156  if ($MAP['state'] == 'active') {
157    if (!empty($page['additional_page']['id'])) {
158      $lire = $page['additional_page']['id'];
159      $query = 'SELECT id, metaKeyap, metadesap FROM ' . META_AP_TABLE . ' WHERE id = \'' . $lire . '\';';
160          $result = pwg_query($query);
161          $row = pwg_db_fetch_assoc($result);
162          $metaKeyapap = $row['metaKeyap'];
163          $metadesapap = $row['metadesap'];
164          $metaKeyapapED = trigger_change('AP_render_content', $metaKeyapap);
165          $metadesapED = trigger_change('AP_render_content', $metadesapap);
166    }
167        if (isset($page['section']) and $page['section'] == 'additional_page') {
168          if (!empty($metaKeyapapED)) {
169                $template->append('related_tags', array('name' => $metaKeyapapED));
170          }
171          if (!empty($metadesapED)) {
172                $template->assign('PLUG_META', $metadesapED);
173          }
174        }
175  }
176}
177
178function add_metacat() {
179  global $template, $page, $meta_infos;
180  if (!empty($page['category']['id'])) {
181    $query = 'SELECT id,metaKeycat,metadescat FROM ' . meta_cat_TABLE . ' WHERE id = \'' . $page['category']['id'] . '\';';
182        $result = pwg_query($query);
183        $row = pwg_db_fetch_assoc($result);
184        $albumKeyED = trigger_change('AP_render_content', $row['metaKeycat']);
185        if (!empty($row['metaKeycat'])) {
186          $template->append('related_tags', array('name' => $albumKeyED));
187        }
188        $albumDesED = trigger_change('AP_render_content', $row['metadescat']);
189        if (!empty($row['metadescat'])) {
190          $template->assign('PLUG_META', $albumDesED);
191        }
192  }
193}
194
195function add_metaimg(){
196  global $template, $page, $meta_infos;
197  if (!empty($page['image_id'])) {
198    $query = 'SELECT id,metaKeyimg,metadesimg FROM ' . meta_img_TABLE . ' WHERE id = \'' . $page['image_id'] . '\';';
199        $result = pwg_query($query);
200        $row = pwg_db_fetch_assoc($result);
201        $photoKeyED = trigger_change('AP_render_content', $row['metaKeyimg']);
202        if (!empty($row['metaKeyimg'])) {
203          $template->append('related_tags', array('name' => $photoKeyED));
204        }
205        $photoDesED = trigger_change('AP_render_content', $row['metadesimg']);
206        if (!empty($row['metadesimg'])) {
207          $template->assign('PLUG_META', $photoDesED);
208        }else{
209          $meta_infosph = array();
210          $meta_infosph['title'] = $template->get_template_vars('PAGE_TITLE');
211          $meta_infosph['gt'] = $template->get_template_vars('GALLERY_TITLE');
212          $meta_infosph['descimg'] = $template->get_template_vars('COMMENT_IMG');
213          if (!empty($meta_infosph['descimg'])) {
214                $template->assign('PLUG_META', strip_tags($meta_infosph['descimg']) . ' - ' . $meta_infosph['title']);
215          }else{
216                $template->assign('PLUG_META', $meta_infosph['title'] . ' - ' . $meta_infosph['gt']);
217          }
218        }
219  }
220}
221
222function set_meta_back(){
223  global $template, $meta_infos;
224  $template->assign(
225    array(
226      'INFO_AUTHOR' => $meta_infos['author'],
227      'related_tags' => $meta_infos['related_tags'],
228      'INFO_FILE' => $meta_infos['info'],
229    )
230   );
231}
232
233?>
Note: See TracBrowser for help on using the repository browser.