* a copy of the License at <http://www.apache.org/licenses/LICENSE-2.0>
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * $Id: myproxy_renew.php 2378 2009-07-14 14:00:34Z pieterb $
 **************************************************************************/
/**
 * File documentation.
 * @package Portal
 */
require_once 'include/global.php';
if (Portal::user_dn() != @$_SERVER['SSL_SERVER_S_DN']) {
    REST::fatal(REST::HTTP_UNAUTHORIZED);
}
REST::require_method('GET');
foreach (glob(Portal::PROXY_DIR . '*.pem') as $fullfilename) {
    $escfullfilename = escapeshellarg($fullfilename);
    exec("grid-proxy-info -f {$escfullfilename} -exists -valid 1:00", $output, $returnval);
    if (!$returnval) {
        continue;
    }
    // The proxy is valid for at least another hour
    $user_dn_md5 = Portal_MySQL::escape_string(basename($fullfilename, '.pem'));
    $result = Portal_MySQL::query(<<<EOS
SELECT `proxy_server`, `proxy_username`, `proxy_password` FROM `User`
 WHERE `user_dn_md5` = {$user_dn_md5};
EOS
 * a copy of the License at <http://www.apache.org/licenses/LICENSE-2.0>
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * $Id: proxy.php 2378 2009-07-14 14:00:34Z pieterb $
 **************************************************************************/
/**
 * File documentation.
 * @package Portal
 */
require_once 'include/global.php';
$userdnmd5 = md5(Portal::user_dn());
$proxy = Portal::PROXY_DIR . $userdnmd5 . '.pem';
$escproxy = str_replace("'", "\\'", $proxy);
if ($_SERVER['REQUEST_METHOD'] == 'PUT') {
    $input = REST::inputhandle();
    $output = fopen($proxy, 'w');
    while (!feof($input)) {
        if (($block = fread($input, 8192)) === false) {
            REST::fatal(REST::HTTP_INTERNAL_SERVER_ERROR, 'Error while reading PUT data');
        }
        fwrite($output, $block);
    }
    fclose($output);
    fclose($input);
    REST::header(array('status' => REST::HTTP_NO_CONTENT));
    exit;