source: extensions/hotlink_compatibility/admin.php @ 31405

Last change on this file since 31405 was 16274, checked in by mistic100, 12 years ago

create Hotlink Compatibility plugin

File size: 3.7 KB
Line 
1<?php
2if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
3
4global $template, $page;
5load_language('plugin.lang', PHPWG_PLUGINS_PATH.basename(dirname(__FILE__)).'/');
6
7// file contents
8if (!isset($conf['prefix_thumbnail'])) $conf['prefix_thumbnail'] = 'TN-';
9if (!isset($conf['dir_thumbnail']))    $conf['dir_thumbnail'] = 'thumbnail';
10
11$htaccess = PHPWG_ROOT_PATH.'/.htaccess';
12$htaccess_content = '
13<IfModule mod_rewrite.c>
14RewriteEngine on
15## redirect <2.4 thumbnails hotlinks to i.php
16RewriteRule ^upload/(.*)/'.preg_quote($conf['dir_thumbnail']).'/'.preg_quote($conf['prefix_thumbnail']).'(.*)\.(jpg|jpeg|png|gif)$ i.php?/upload/$1/$2-th.$3 [NC,L]
17RewriteRule ^galleries/(.*)/'.preg_quote($conf['dir_thumbnail']).'/'.preg_quote($conf['prefix_thumbnail']).'(.*)\.(jpg|jpeg|png|gif)$ i.php?/galleries/$1/$2-th.$3 [NC,L]
18
19## redirect <2.4 high-def hotlinks to original file
20RewriteRule ^upload/(.*)/pwg_high/(.*)\.(jpg|jpeg|png|gif)$ upload/$1/$2.$3 [NC,L]
21RewriteRule ^galleries/(.*)/pwg_high/(.*)\.(jpg|jpeg|png|gif)$ galleries/$1/$2.$3 [NC,L]
22
23## redirect <2.4 low-def hotlinks to i.php
24RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?'.preg_quote($_SERVER['SERVER_NAME']).'/.*$ [NC]
25RewriteRule ^upload/(.*)/(.*)\.(jpg|jpeg|png|gif)$ i.php?/upload/$1/$2-me.$3 [NC,L]
26RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?'.preg_quote($_SERVER['SERVER_NAME']).'/.*$ [NC]
27RewriteRule ^galleries(.*)/(.*)\.(jpg|jpeg|png|gif)$ i.php?/galleries/$1/$2-me.$3 [NC,L]
28</IfModule>';
29
30
31// check if the file is writable
32$writable = true;
33if (file_exists($htaccess))
34{
35  if (!is_readable($htaccess) || !is_writable($htaccess))
36  {
37    $writable = false;
38  }
39}
40else
41{
42  $writable = is_writable(PHPWG_ROOT_PATH);
43}
44
45if (!$writable)
46{
47  array_push($page['errors'], l10n('.htaccess file is not writable'));
48  $template->assign('FILE_ERROR', true);
49  $template->assign('FILE_CONTENT', $htaccess_content);
50  $template->assign('DISPLAY_DESC', true);
51}
52else
53{
54  $continue = true;
55  $content = file_exists($htaccess) ? file_get_contents($htaccess) : null;
56
57  // check if RewriteEngine off
58  if (preg_match('#RewriteEngine\s+off#', $content))
59  {
60    array_push($page['errors'], l10n('Unable to activate RewriteEngine'));
61    $template->assign('REWRITE_ERROR', true);
62    $template->assign('DISPLAY_DESC', true);
63    $continue = false;
64  }
65 
66  // check if already updated
67  if (strpos($content, '## redirect <2.4') !== false)
68  {
69    array_push($page['infos'], l10n('Upgrade already applied, please remove this plugin'));
70    $template->assign('ALREADY_UPDATED', true);
71    $continue = false;
72  }
73
74  if ($continue)
75  {
76    if (isset($_GET['init_migration']))
77    {
78      if (strpos($content, 'RewriteEngine on') !== false)
79      {
80        $htaccess_content = str_replace('RewriteEngine on', null, $htaccess_content);
81      }
82     
83      file_put_contents($htaccess, "\n".$htaccess_content, FILE_APPEND);
84     
85      array_push($page['infos'], l10n('Upgrade successfully applied, please remove this plugin'));
86    }
87    else
88    {
89      // check if the host is incompatible
90      if (preg_match('#free\.fr/?$#', $_SERVER['SERVER_NAME']))
91      {
92        array_push($page['warnings'], l10n('Your host may be incompatible with URL rewriting'));
93        $template->assign('HOST_WARNING', true);
94      }
95     
96      $template->assign('DISPLAY_DESC', true);
97      $template->assign('NO_ERROR', true);
98    }
99  }
100}
101
102$template->assign('DESC_CONTENT', load_language('desc_content.html', PHPWG_PLUGINS_PATH.basename(dirname(__FILE__)).'/', array('return'=>true)));
103$template->assign('URL_INIT', get_root_url().'admin.php?page=plugin-'.basename(dirname(__FILE__)).'&amp;init_migration');
104
105$template->set_filename('addht_content', dirname(__FILE__).'/admin.tpl');
106$template->assign_var_from_handle('ADMIN_CONTENT', 'addht_content');
107
108?> 
Note: See TracBrowser for help on using the repository browser.