1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.util.bridges.php;
24  
25  import com.liferay.portal.kernel.servlet.PortletServlet;
26  import com.liferay.portal.kernel.servlet.StringServletResponse;
27  import com.liferay.util.servlet.DynamicServletConfig;
28  
29  import java.io.IOException;
30  import java.io.PrintWriter;
31  
32  import java.util.Enumeration;
33  import java.util.HashMap;
34  import java.util.Map;
35  
36  import javax.portlet.ActionRequest;
37  import javax.portlet.ActionResponse;
38  import javax.portlet.GenericPortlet;
39  import javax.portlet.PortletException;
40  import javax.portlet.PortletURL;
41  import javax.portlet.RenderRequest;
42  import javax.portlet.RenderResponse;
43  
44  import javax.servlet.ServletConfig;
45  import javax.servlet.http.HttpServlet;
46  import javax.servlet.http.HttpServletRequest;
47  import javax.servlet.http.HttpServletResponse;
48  
49  import org.apache.commons.logging.Log;
50  import org.apache.commons.logging.LogFactory;
51  import org.apache.portals.bridges.common.ScriptPostProcess;
52  
53  /**
54   * <a href="PHPPortlet.java.html"><b><i>View Source</i></b></a>
55   *
56   * @author Jorge Ferrer
57   *
58   */
59  public class PHPPortlet extends GenericPortlet {
60  
61      public static final String PHP_URI_PARAM = "phpURI";
62  
63      public void init() throws PortletException {
64          editUri = getInitParameter("edit-uri");
65          helpUri = getInitParameter("help-uri");
66          viewUri = getInitParameter("view-uri");
67      }
68  
69      public void doDispatch(RenderRequest req, RenderResponse res)
70          throws IOException, PortletException {
71  
72          String phpUri = req.getParameter(PHP_URI_PARAM);
73  
74          if (phpUri != null) {
75              processPHP(phpUri, req, res);
76          }
77          else {
78              super.doDispatch(req, res);
79          }
80      }
81  
82      public void doEdit(RenderRequest req, RenderResponse res)
83          throws IOException, PortletException {
84  
85          if (req.getPreferences() == null) {
86              super.doEdit(req, res);
87          }
88          else {
89              processPHP(editUri, req, res);
90          }
91      }
92  
93      public void doHelp(RenderRequest req, RenderResponse res)
94          throws IOException, PortletException {
95  
96          processPHP(helpUri, req, res);
97      }
98  
99      public void doView(RenderRequest req, RenderResponse res)
100         throws IOException, PortletException {
101 
102         processPHP(viewUri, req, res);
103     }
104 
105     public void processAction(ActionRequest req, ActionResponse res)
106         throws IOException, PortletException {
107 
108         String phpURI = req.getParameter(PHP_URI_PARAM);
109 
110         if (phpURI != null) {
111             res.setRenderParameter(PHP_URI_PARAM, phpURI);
112         }
113     }
114 
115     public void destroy() {
116         if (quercusServlet != null) {
117             quercusServlet.destroy();
118         }
119     }
120 
121     protected synchronized void initQuercus(ServletConfig config)
122         throws PortletException {
123 
124         if (quercusServlet == null) {
125             try {
126                 quercusServlet = (HttpServlet)Class.forName(
127                     _QUERCUS_SERVLET).newInstance();
128 
129                 Map<String, String> params = new HashMap<String, String>();
130 
131                 Enumeration<String> enu = config.getInitParameterNames();
132 
133                 while (enu.hasMoreElements()) {
134                     String name = enu.nextElement();
135 
136                     if (!name.equals("portlet-class")) {
137                         params.put(name, config.getInitParameter(name));
138                     }
139                 }
140 
141                 config = new DynamicServletConfig(config, params);
142 
143                 quercusServlet.init(config);
144             }
145             catch (Exception e) {
146                 throw new PortletException(e);
147             }
148         }
149     }
150 
151     protected void processPHP(
152             String phpURI, RenderRequest req, RenderResponse res)
153         throws IOException, PortletException {
154 
155         try {
156             ServletConfig config = (ServletConfig)req.getAttribute(
157                 PortletServlet.PORTLET_SERVLET_CONFIG);
158 
159             initQuercus(config);
160 
161             HttpServletRequest httpReq = (HttpServletRequest)req.getAttribute(
162                 PortletServlet.PORTLET_SERVLET_REQUEST);
163             HttpServletResponse httpRes = (HttpServletResponse)req.getAttribute(
164                 PortletServlet.PORTLET_SERVLET_RESPONSE);
165 
166             PHPServletRequest phpReq = new PHPServletRequest(
167                 httpReq, config, req, res, getPortletConfig(), phpURI);
168 
169             StringServletResponse phpRes = new StringServletResponse(httpRes);
170 
171             quercusServlet.service(phpReq, phpRes);
172 
173             String result = phpRes.getString();
174 
175             if (phpRes.getContentType().startsWith("text/")) {
176                 result = rewriteURLs(result, res.createRenderURL());
177             }
178 
179             PrintWriter writer = httpRes.getWriter();
180 
181             writer.write(result.toCharArray());
182         }
183         catch (Exception e) {
184             _log.error("Error processing PHP", e);
185         }
186     }
187 
188     protected String rewriteURLs(String page, PortletURL portletURL) {
189         ScriptPostProcess processor = new ScriptPostProcess();
190 
191         processor.setInitalPage(new StringBuffer(page));
192         processor.postProcessPage(portletURL, PHP_URI_PARAM);
193 
194         return processor.getFinalizedPage();
195     }
196 
197     private static final String _QUERCUS_SERVLET =
198         "com.caucho.quercus.servlet.QuercusServlet";
199 
200     private static Log _log = LogFactory.getLog(PHPPortlet.class);
201 
202     protected String editUri;
203     protected String helpUri;
204     protected String viewUri;
205     protected HttpServlet quercusServlet;
206 
207 }