Kategori arşivi: Php

Plesk Api İle Domain Detayları

Merhabalar, Plesk panel kullanan paneller ile tüm domainin tüm bilgilerini çekmek için aşağıdaki paket örneğini kullanınız

<packet version=”1.6.0.0″>
<domain>
<get>

<filter><domain-name>mehmetmutlu.com.tr</domain-name></filter>
<dataset>
<hosting/><limits/><stat/>
</dataset>

</get>
</domain>
</packet>

GEt içerisinde dataset alanı içerisinde,

domain ile hangi bilgiler var ise ekliyoruz. örn,

<hosting/> yazarsak hosting bilgileri

<limits/> hosting’in limit bilgileri
<stat/> hosting’in durum istatistik bilgisi

IE6 derdinden kurtulmak

Web geliştiriciler için en sıkıntı veren durum şüphesiz Internet Explorer 6 tarayıcılardır. Kısa bir süre önce   Google ve Youtube gibi önemli markalar ie6 tarayıcılarla artık uğraşmayacaklarını ilan ettiler. Ben de bazı projelerde kullandığım bu ie6 tarayıcı banlama uygulamasını artık gönül rahatlığıyla kullanabileceğim.

Aşağıdaki kodları sayfanızın en başına eklerseniz Internet Explorer 6.0 versiyonlu tarayıcılarla gelen ziyaretçi görseldeki mesajla karşılaşacaktır. Kodlara geçelim;

index.php

<?php
$useragent = $_SERVER[‘HTTP_USER_AGENT’];

if (preg_match(‘|MSIE ([0-9].[0-9]{1,2})|’,$useragent,$matched)) {
$browser_version=$matched[1];
$browser = ‘IE';
}

if($browser_version==’6.0′){

?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″/>

<title>Web Tarayıcınız çok eski</title>
<style type=”text/css” media=”screen”>/**/
body { text-align:center; font-family:sans-serif; margin-top:50px; }
div.desc { margin:auto; background:#ffa; color:#110; font-size:18px; padding:20px; width:850px; }
ul.browsers { list-style:none; padding:0; margin:50px auto 0 auto; width:1000px; }
ul.browsers li { text-align:center; display:block; float:left; width:200px; height:300px; margin-left:25px; }
img { border:0; }
/**/</style>
</head>
<body>
<div>
<b>Lütfen okuyun;</b> Kullandığınız web tarayıcı, yani <b>Internet Explorer 6</b>, çok eski ve bu siteyi gösterebilecek kabiliyetlere sahip değil. <b>Artık daha iyi bir web tarayıcı yüklemenin vakti geldi</b>. aşağıdaki yeni ve başarılı web tarayıcılardan bir tanesine tıklayarak indirme sayfasına gidebilirsiniz.
</div>
<ul>
<li>
<h2>Mozilla Firefox</h2>
<a href=”http://www.mozilla-europe.org/tr/firefox/”><img src=”/firefox.jpg” width=”200″ alt=”” /></a>
</li>
<li>
<h2>Google Chrome</h2>
<a href=”http://www.google.com/chrome/index.html?hl=tr”><img src=”/chrome.jpg” width=”200″ alt=”” /></a>
</li>
<li>
<h2>Apple – Safari</h2>
<a href=”http://www.apple.com/safari”><img src=”/safari.jpg” width=”200″ alt=”” /></a>
</li>
<li>
<h2>Opera</h2>
<a href=”http://www.opera.com/”><img src=”/opera.jpg” width=”200″ alt=”” /></a>
</li>
</ul>
</body>
</html>
<?php

die();
}
?>

<!DOCTYPE HTML PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xml:lang=”en” xmlns=”http://www.w3.org/1999/xhtml” lang=”en”>

<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-9″ />
<title>serpito.com ie6 ban uygulaması</title>
</head>

<body>
index.php dosyası

</body>
</html>

Jquery ile form elemanları ve ajax

Merhabalar bu makalemde de, form submit olaylarını ve tüm form elemanları datalarını çekme işlemlerinden bahsedeceğim,

$(‘#form1′).submit(function() {

return false;

});

form submit yakalamak için event return false yazar isek, formun  durmasını sağlıyor

if($(“#pc”).attr(“checked”)==false){
var $pc = “yok”;
}else{
var $pc = “var”;
}

checked box dan seçilimi değil mi diye veriyi aldık,

var $radyo = $(“input:radio[name=RadioGroup1]:checked”).val();

radyo formdan veriyi aldık,

$.ajax({
type: “POST”, //veri gönderme tipi post get vs
url: “http://localhost/test.php”, // php
data: “adi=”+$adi+”&pc=”+$pc+”&cinsiyet=”+$cinsiyet+”&radyo=”+$radyo+”&gorusleri=”+$gorusleri+”&esktraveri=’anamm oldu'”, //datalar
success: function($phpgelen){
$(“#donenveri”).html($phpgelen); //sonucu dönen verisi div in içine güncelliyecek,
}
});

ajax ile veriyi gönderdik,

örnek için

http://www.mehmetmutlu.com.tr/script/jqueryveajaxformelemanlari/Untitled-1.html

php kaynak kodu için

http://www.mehmetmutlu.com.tr/script/jqueryveajaxformelemanlari/test.phps

Parallels Plesk Panel API RPC Protocol Developer

Comments

class ApiRequestException Extends the standard Exception class. Visit http://www.php.net/manual/en/language.exceptions.php for details.
function domainsInfoRequest() Uses the DOM model to compose a request XML packet. Returns the resulting DOM object.
function curlInit() Initializes the CURL session with necessary options as follows:
CURLOPT_URL specifies the destination URL
CURLOPT_RETURNTRANSFER set to true means that the resulting output will be returned from the server in the form of a string.
CURLOPT_POST set to true means that the packet will be sent via HTTP POST.
CURLOPT_SSL_VERIFYPEER set to false stops CURL from verifying the peer’s certificate.
CURLOPT_SSL_VERIFYHOST set to false stops CURL from verifying the host.
CURLOPT_HTTPHEADER specifies an array of HTTP header fields to set.
Returns the handler of the URL session.
function sendRequest() Sends the HTTP packet by means of CURL and gets the pure XML response packet (without the HTTP header). Closes the CURL session and returns the resulting packet (a plain text with XML tags). To learn more about the CURL engine, visit http://www.php.net/manual/en/ref.curl.php.
function parseResponse() Gets the response packet (plain text) as a parameter. Parses the packet using SimpleXML and returns the SimpleXMLElement object in which the packet is structured as a tree. To learn more about the SimpleXML extension of PHP, visit http://www.php.net/manual/en/ref.simplexml.php.
function checkResponse() Gets the response packet of type SimpleXMLEelement and checks the result node. If this node holds ‘error’, then the function throws an exception with a string passed in the errtext node.

The main() function invokes these functions in the order they are considered. First it calls domainInfoRequest() to form a packet as follows:

<?xml version="1.0" encoding="UTF-8" ?>
<packet version="1.4.1.2">
<domain>
<get>
      <filter/>
      <dataset>
             <limits/>
             <prefs/>
             <user/>
             <hosting/>
             <stat/>
             <gen_info/>
      </dataset>
</get>
</domain>
</packet>

Then curlInit() is called to initialize CURL session with options. The sendRequest function sends a packet with the HTTP header as follows:

POST /enterprise/control/agent.php HTTP/1.1
Host: 10.58.32.100:8443
HTTP_AUTH_LOGIN: login
HTTP_AUTH_PASSWD: qwedsa
HTTP_PRETTY_PRINT: TRUE
Content-Length: 294
Content-Type: text/xml

The response XML packet received from Plesk server can look as follows:

<?xml version="1.0" encoding="UTF-8" ?>
<packet version="1.4.1.2">
<domain>
<get>
   <result>
      <status>ok</status>
      <id>1234</id>
      <value>
          <gen_info>
             <cr_date>1154513574</cr_date>
             <name>technolux.co.uk</name>
             <display_name>technolux</display_name>
             <status>64</status>
             <real_size>0</real_size>
             <client_id>4610</client_id>
             <dns_ip_address>123.123.123.121</dns_ip_address>
             <htype>none</htype>
          </gen_info>
      </value>
   </result>
</get>
</domain>
</packet>

Once the response packet is received and parsed with the parseResponse() function, the foreach iterator echoes the contents of the result node.

Code sample


<?php

/**
 * Reports error during API RPC request
 */
class ApiRequestException extends Exception {}

/**
 * Returns DOM object representing request for information about all available domains
 * @return DOMDocument
 */

function domainsInfoRequest()
{
      $xmldoc = new DomDocument('1.0', 'UTF-8');
      $xmldoc->formatOutput = true;

      // <packet>
      $packet = $xmldoc->createElement('packet');
      $packet->setAttribute('version', '1.4.1.2');
      $xmldoc->appendChild($packet);

      // <packet/domain>
      $domain = $xmldoc->createElement('domain');
      $packet->appendChild($domain);

      // <packet/domain/get>
      $get = $xmldoc->createElement('get');
      $domain->appendChild($get);

      // <packet/domain/get/filter>
      $filter = $xmldoc->createElement('filter');
      $get->appendChild($filter);

      // <packet/domain/get/dataset>
      $dataset = $xmldoc->createElement('dataset');
      $get->appendChild($dataset);

      // dataset elements
      $dataset->appendChild($xmldoc->createElement('limits'));
      $dataset->appendChild($xmldoc->createElement('prefs'));
      $dataset->appendChild($xmldoc->createElement('user'));
      $dataset->appendChild($xmldoc->createElement('hosting'));
      $dataset->appendChild($xmldoc->createElement('stat'));
      $dataset->appendChild($xmldoc->createElement('gen_info'));

      return $xmldoc;
}
/**
 * Prepares CURL to perform Plesk API request
 * @return resource
 */
function curlInit($host, $login, $password)
{
      $curl = curl_init();
      curl_setopt($curl, CURLOPT_URL, "https://{$host}:8443/enterprise/control/agent.php");
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($curl, CURLOPT_POST,           true);
      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
      curl_setopt($curl, CURLOPT_HTTPHEADER,
             array("HTTP_AUTH_LOGIN: {$login}",
                    "HTTP_AUTH_PASSWD: {$password}",
                    "HTTP_PRETTY_PRINT: TRUE",
                    "Content-Type: text/xml")
      );

      return $curl;
}
/**
 * Performs a Plesk API request, returns raw API response text
 *
 * @return string
 * @throws ApiRequestException
 */
function sendRequest($curl, $packet)
{
      curl_setopt($curl, CURLOPT_POSTFIELDS, $packet);
      $result = curl_exec($curl);
      if (curl_errno($curl)) {
             $errmsg  = curl_error($curl);
             $errcode = curl_errno($curl);
             curl_close($curl);
             throw new ApiRequestException($errmsg, $errcode);
      }
      curl_close($curl);
      return $result;
}

/**
 * Looks if API responded with correct data
 *
 * @return SimpleXMLElement
 * @throws ApiRequestException
 */
function parseResponse($response_string)
{
      $xml = new SimpleXMLElement($response_string);
      if (!is_a($xml, 'SimpleXMLElement'))
             throw new ApiRequestException("Cannot parse server response: {$response_string}");
      return $xml;
}
/**
 * Check data in API response
 * @return void
 * @throws ApiRequestException
 */
function checkResponse(SimpleXMLElement $response)
{
      $resultNode = $response->domain->get->result;

      // check if request was successful
      if ('error' == (string)$resultNode->status)
             throw new ApiRequestException("Plesk API returned error: " . (string)$resultNode->result->errtext);
}

//
// int main()
//
$host = '10.58.32.100';
$login = 'admin';
$password = 'qwedsa';

$curl = curlInit($host, $login, $password);

try {

      $response = sendRequest($curl, domainsInfoRequest()->saveXML());
      $responseXml = parseResponse($response);
      checkResponse($responseXml);

} catch (ApiRequestException $e) {
      echo $e;
      die();
}

// Explore the result
foreach ($responseXml->xpath('/packet/domain/get/result') as $resultNode) {
      echo "Domain id: " . (string)$resultNode->id . " ";
      echo (string)$resultNode->data->gen_info->name . " (" . (string)$resultNode->data->gen_info->dns_ip_address . ")\n";
}

?>

PHP HTTP Authentication

Occasionally you may wish to make certain pages of your site only viewable to a select few. you can do this by using PHPs built in HTTP Authentication. The code needs to go right at the top of your php page so don’t get ‘Headers Already Sent’ errors. You can see that we’ve specified the username and password in the variables at the top of the script you can change these to reflect your own username and password.

You could easily make this authentication more dynamic by checking a database for the username and password. We can get whatever the user typed into the dropdown box by specifying the following superglobals.

//Username:
<?php echo $_SERVER['PHP_AUTH_USER'];?>
//Password:
<?php echo $_SERVER['PHP_AUTH_PW'];?>

<?php 

$config['admin_username'] = "demo";
$config['admin_password'] = "demo";

if (!($_SERVER['PHP_AUTH_USER'] == $config['admin_username'] && $_SERVER['PHP_AUTH_PW'] == $config['admin_password'])) {
    header("WWW-Authenticate: Basic realm=\"Papermashup.com Demo Admin\"");
    header("HTTP/1.0 401 Unauthorized");
	echo 'This is what happens if you press cancel';
	?>
<?php
    exit;
}
// if the username and password match show the rest of the content

?>

Be careful when coding the HTTP header lines. In order to guarantee  maximum compatibility with all browsers, the keyword “Basic” should be  written with an uppercase “B”,
the realm string must be enclosed in  double (not single) quotes, and exactly one space should precede the 401  code in the HTTP/1.0 401 header line.