1   /**
2    * Copyright (c) 2000-2007 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  
28  import java.io.IOException;
29  import java.io.PrintWriter;
30  
31  import javax.portlet.ActionRequest;
32  import javax.portlet.ActionResponse;
33  import javax.portlet.GenericPortlet;
34  import javax.portlet.PortletException;
35  import javax.portlet.PortletURL;
36  import javax.portlet.RenderRequest;
37  import javax.portlet.RenderResponse;
38  
39  import javax.servlet.ServletConfig;
40  import javax.servlet.http.HttpServlet;
41  import javax.servlet.http.HttpServletRequest;
42  import javax.servlet.http.HttpServletResponse;
43  
44  import org.apache.commons.logging.Log;
45  import org.apache.commons.logging.LogFactory;
46  import org.apache.portals.bridges.common.ScriptPostProcess;
47  
48  /**
49   * <a href="PHPPortlet.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Jorge Ferrer
52   *
53   */
54  public class PHPPortlet extends GenericPortlet {
55  
56      public static final String PHP_URI_PARAM = "phpURI";
57  
58      public void init() throws PortletException {
59          editUri = getInitParameter("edit-uri");
60          helpUri = getInitParameter("help-uri");
61          viewUri = getInitParameter("view-uri");
62      }
63  
64      public void doDispatch(RenderRequest req, RenderResponse res)
65          throws IOException, PortletException {
66  
67          String phpUri = req.getParameter(PHP_URI_PARAM);
68  
69          if (phpUri != null) {
70              processPHP(phpUri, req, res);
71          }
72          else {
73              super.doDispatch(req, res);
74          }
75      }
76  
77      public void doEdit(RenderRequest req, RenderResponse res)
78          throws IOException, PortletException {
79  
80          if (req.getPreferences() == null) {
81              super.doEdit(req, res);
82          }
83          else {
84              processPHP(editUri, req, res);
85          }
86      }
87  
88      public void doHelp(RenderRequest req, RenderResponse res)
89          throws IOException, PortletException {
90  
91          processPHP(helpUri, req, res);
92      }
93  
94      public void doView(RenderRequest req, RenderResponse res)
95          throws IOException, PortletException {
96  
97          processPHP(viewUri, req, res);
98      }
99  
100     public void processAction(ActionRequest req, ActionResponse res)
101         throws IOException, PortletException {
102 
103         String phpURI = req.getParameter(PHP_URI_PARAM);
104 
105         if (phpURI != null) {
106             res.setRenderParameter(PHP_URI_PARAM, phpURI);
107         }
108     }
109 
110     public void destroy() {
111         if (quercusServlet != null) {
112             quercusServlet.destroy();
113         }
114     }
115 
116     protected synchronized void initQuercus(ServletConfig config)
117         throws PortletException {
118 
119         if (quercusServlet == null) {
120             try {
121                 quercusServlet = (HttpServlet)Class.forName(
122                     _QUERCUS_SERVLET).newInstance();
123 
124                 quercusServlet.init(config);
125             }
126             catch (Exception e) {
127                 throw new PortletException(e);
128             }
129         }
130     }
131 
132     protected void processPHP(
133             String phpURI, RenderRequest req, RenderResponse res)
134         throws IOException, PortletException {
135 
136         try {
137             ServletConfig config = (ServletConfig)req.getAttribute(
138                 PortletServlet.PORTLET_SERVLET_CONFIG);
139 
140             initQuercus(config);
141 
142             HttpServletRequest httpReq = (HttpServletRequest)req.getAttribute(
143                 PortletServlet.PORTLET_SERVLET_REQUEST);
144             HttpServletResponse httpRes = (HttpServletResponse)req.getAttribute(
145                 PortletServlet.PORTLET_SERVLET_RESPONSE);
146 
147             PHPServletRequest phpReq = new PHPServletRequest(
148                 httpReq, config, req, res, getPortletConfig(), phpURI);
149 
150             StringServletResponse phpRes = new StringServletResponse(httpRes);
151 
152             quercusServlet.service(phpReq, phpRes);
153 
154             String result = phpRes.getString();
155 
156             if (phpRes.getContentType().startsWith("text/")) {
157                 result = rewriteURLs(result, res.createRenderURL());
158             }
159 
160             PrintWriter writer = httpRes.getWriter();
161 
162             writer.write(result.toCharArray());
163         }
164         catch (Exception e) {
165             _log.error("Error processing PHP", e);
166         }
167     }
168 
169     protected String rewriteURLs(String page, PortletURL portletURL) {
170         ScriptPostProcess processor = new ScriptPostProcess();
171 
172         processor.setInitalPage(new StringBuffer(page));
173         processor.postProcessPage(portletURL, PHP_URI_PARAM);
174 
175         return processor.getFinalizedPage();
176     }
177 
178     private static final String _QUERCUS_SERVLET =
179         "com.caucho.quercus.servlet.QuercusServlet";
180 
181     private static Log _log = LogFactory.getLog(PHPPortlet.class);
182 
183     protected String editUri;
184     protected String helpUri;
185     protected String viewUri;
186     protected HttpServlet quercusServlet;
187 
188 }