Refactured
[mailer.git] / 0.2.1 / inc / libs / rewrite_functions.php
1 <?php\r
2 /************************************************************************\r
3  * MXChange v0.2.1                                    Start: 10/24/2004 *\r
4  * ===============                              Last change: 11/14/2004 *\r
5  *                                                                      *\r
6  * -------------------------------------------------------------------- *\r
7  * File              : rewrite_functions.php                            *\r
8  * -------------------------------------------------------------------- *\r
9  * Short description : Special functions for rewrite extension          *\r
10  * -------------------------------------------------------------------- *\r
11  * Kurzbeschreibung  : Spezielle Funktion fuer rewrite-Erweiterung      *\r
12  * -------------------------------------------------------------------- *\r
13  *                                                                      *\r
14  * -------------------------------------------------------------------- *\r
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *\r
16  * For more information visit: http://www.mxchange.org                  *\r
17  *                                                                      *\r
18  * This program is free software; you can redistribute it and/or modify *\r
19  * it under the terms of the GNU General Public License as published by *\r
20  * the Free Software Foundation; either version 2 of the License, or    *\r
21  * (at your option) any later version.                                  *\r
22  *                                                                      *\r
23  * This program is distributed in the hope that it will be useful,      *\r
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *\r
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *\r
26  * GNU General Public License for more details.                         *\r
27  *                                                                      *\r
28  * You should have received a copy of the GNU General Public License    *\r
29  * along with this program; if not, write to the Free Software          *\r
30  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *\r
31  * MA  02110-1301  USA                                                  *\r
32  ************************************************************************/\r
33 \r
34 // Some security stuff...\r
35 if (ereg(basename(__FILE__), $_SERVER['PHP_SELF']))\r
36 {\r
37         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4)."/security.php";\r
38         require($INC);\r
39 }\r
40 \r
41 //\r
42 function REWRITE_LINKS($HTML)\r
43 {\r
44         global $_GET, $CONFIG;\r
45 \r
46         // Skip rewriting for the admin area (which search engine may enter here???)\r
47         if (ereg($GLOBALS['module'], $CONFIG['rewrite_skipped_mods'])) return $HTML;\r
48 \r
49         // Strip slashes with double-backslashes for the preg_replace() function\r
50         $URL = str_replace("/", "\\/", URL);\r
51 \r
52         // Generate target URL\r
53         $target = URL."/cms/";\r
54 \r
55         // Convert modules.php?module=...\r
56         $test = preg_replace("/".$URL."\\/modules.php\\?module=/i", $target, $HTML);\r
57 \r
58         if (eregi(SERVER_URL, $test))\r
59         {\r
60                 // Konvert URLs from my server\r
61                 $URL = str_replace("/", "\\/", SERVER_URL);\r
62                 $test = preg_replace("/".$URL."\\/modules.php\\?module=/i", SERVER_URL."/cms/", $test);\r
63         }\r
64 \r
65         // Strip slashes as above for the main URL\r
66         $target2 = str_replace("/", "\\/", $target);\r
67 \r
68         // Action variable\r
69         $act = $target."\$1/act/";\r
70 \r
71         // Convert &amp;|&action=...\r
72         $test = preg_replace("/".$target2."(.*)&amp;action=/i", $act, $test);\r
73 \r
74         // "The same procedure as last variable"... now for &amp;what=\r
75         $wht = $target."\$1/wht/";\r
76         $target2 = str_replace("/", "\\/", $target);\r
77         $test = preg_replace("/".$target2."(.*)&amp;what=/i", $wht, $test);\r
78 \r
79         if ((EXT_IS_ACTIVE("rallye")) && (eregi("rallye=", $test)))\r
80         {\r
81                 // Replace data when rallye extension is active\r
82                 // Add more if you need more like these entries\r
83                 $REPLACE = array("rallye", "activate", "auto", "notify", "sub");\r
84                 foreach ($REPLACE as $var)\r
85                 {\r
86                         // This will replace "&amp;var=" to "/var/"\r
87                         $test = preg_replace("/&amp;".$var."=/i", "/".$var."/", $test);\r
88                 }\r
89         }\r
90 \r
91         // Simple from->to replacements\r
92         $REPLACE = array(\r
93                 'search'  => array("u_id", "url", "page", "offset", "mid", "bid", "sub", "home"),\r
94                 'replace' => array("u"   , "url", "page", "offset", "m"  , "b"  , "s"  , "h")\r
95         );\r
96 \r
97         if ((EXT_IS_ACTIVE("admins")) && (eregi("admin=", $test)))\r
98         {\r
99                 // Replace &amp;admin= with "/aid/"\r
100                 $REPLACE['search'][]  = "admin";\r
101                 $REPLACE['replace'][] = "aid";\r
102         }\r
103 \r
104         // Replace all array elements through\r
105         foreach ($REPLACE['search'] as $k=>$v)\r
106         {\r
107                 if (eregi("$v=", $test))\r
108                 {\r
109                         // Replace &amp;u_id= with /u/\r
110                         $test = preg_replace("/&amp;".$v."=/i", "/".$REPLACE['replace'][$k]."/", $test);\r
111                 }\r
112         }\r
113 \r
114         // Repair missed &amp;what=??? entries\r
115         while (preg_match("/&amp;what=(.*)\/(.*)\/(.*)/i", $test))\r
116         {\r
117                 $test = preg_replace("/&amp;what=(.*)\/(.*)\/(.*)/i", "/wht/\$1/\$2/\$3", $test);\r
118         }\r
119 \r
120         // Return rewritten code\r
121         return $test;\r
122 }\r
123 //\r
124 ?>\r