[POO] Modification d'un script codé en PHP4
Bonjour,
N'ayant que très peu touché PHP5, je suis resté en "old school" (développement peu organisé, sans classes et très peu de fonctions). J'essaye de régler ce souci en apprenant le PHP5.x (en POO bien entendu).
J'ai plutôt compris la méthode, son utilisation mais j'ai encore quelques soucis coté technique. J'aimerais savoir si c'est possible de m'éclairer sur des erreurs qui seront très surement banal et bête pour un initié PHP5, mais surtout à expliquer mes erreurs :o) !!
Pour apprendre à développer, j'ai fait un TP basique : j'ai repris un script créé en PHP4, et j'ai essayé de le transformer en POO ! Mais sans succès, après avoir analysé les erreurs j'ai pas réussi à le faire fonctionner..
C'est un script qui Parse du contenu XML
SCRIPT EN PHP4 (de base, celui qui fonctionne) :
Page : char.php
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| <?php
// VARS
require "_fonction.php";
head("Reputation - Armory", "personnage");
$Cle = "blablabla"; // key
$pseudo = Decrypte($_GET['n'],$Cle); // Décryptage du pseudo
$realm = $_GET['r']; // Récuperation du nom du royaume
$r = str_replace(" ", "+", $_GET['r']); // Chaine pour ajouter les "+"
$url = "http://armory.wow-europe.com/character-sheet.xml?r=" . $r . "&n=" . $pseudo ; // Armory XML
// Début CURL /*
$ch = curl_init();
$useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_USERAGENT, $useragent);
$f = curl_exec($ch);
curl_close($ch);
// Fin CURL
$xml = new SimpleXMLElement($f);
foreach ($xml->characterInfo[0]->character as $char) {
echo '<table width="751" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="554"><p align="right"><img src="images/hk.jpg" width="9" height="13"> </p></td>
<td width="197"><font color="#FFDC19" size=1>HK : <b>';
foreach ($xml->characterInfo[0]->characterTab->pvp->lifetimehonorablekills as $hk) {
echo $hk['value'];
}
echo '</b></font></td>
</tr>
</table>
</center>
<table width="751" border="0" align="center" cellpadding="0" cellspacing="0" background="images/bar-yellow.jpg">
<tr>
<td height="27"><table width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="13%" class="black"><span class="black-bold">Char :</span></td>
<td width="12%"><img src=http://armory.wow-europe.com/images/icons/faction/icon-'.$char['factionId'].'.gif> <img src=http://armory.wow-europe.com/images/icons/race/'.$char['raceId'].'-'.$char['genderId'].'.gif> <img src=http://armory.wow-europe.com/images/icons/class/'.$char['classId'].'.gif></td>
<td width="55%" class="black">'.$char['gender'].', '.$char['race'].' '.$char['class'].' '.$char['level'].' - '.$char['faction'].' - <b>'.$char['realm'].'</b></td>';
foreach ($xml->characterInfo[0]->characterTab->professions->skill as $skill) {
echo '
<td width="3%"><img src=http://armory.wow-europe.com/images/icons/professions/'.$skill['key'].'-sm.gif></td>
<td width="7%" class="black">'.$skill['value'].'/'.$skill['max'].'</td>';
} echo '
</tr>
</table></td>
</tr>
</table>
<p align="center">';
}
echo "<br>";
foreach ($xml->characterInfo[0]->characterTab->items->item as $item) {
echo '<a class="q5" href="http://www.wowhead.com/?item='.$item['id'].'"><img src="http://eu.wowarmory.com/wow-icons/_images/43x43/'.$item['icon'].'.png" border="0"></a>';
}
echo '<table width="780" border="0" align="center" cellpadding="0" cellspacing="7">
<tr>
<td align="center"><span class="white-bold">Caracteristiques</span><br>
<table width="100%" border="0" cellspacing="0" cellpadding="0" background="images/case.jpg" style="border:1px solid #3A3A3A">
<tr>
<td height="156" valign="top" style="padding:7px">';
foreach ($xml->characterInfo[0]->characterTab->baseStats as $stats) {
echo '<b>Strength : </b>'.$stats->strength['effective'].'<br><b>Agility : </b>'.$stats->agility['effective'].'<br><b>Stamina : </b>'.$stats->stamina['effective'].'<br><b>Intellect : </b>'.$stats->intellect['effective'].'<br><b>Spirit : </b>'.$stats->spirit['effective']; }
echo '</td>
</tr>
</table></td>
<td align="center"><span class="white-bold">Melee Stats</span>
<table width="100%" border="0" cellspacing="0" cellpadding="0" background="images/case.jpg" style="border:1px solid #3A3A3A">
<tr>
<td height="156" valign="top" style="padding:7px">';
foreach ($xml->characterInfo[0]->characterTab->melee as $melee) {
echo '<b>MH : </b>'.$melee->mainHandDamage['max'].' - '.$melee->mainHandDamage['min'].'<br>
<b>Attack Power:</b> '.$melee->power['effective'].'<br>
<b>Hit Rating :</b> '.$melee->hitRating['value'].' ('.$melee->hitRating['percent'].' %)<br>
<b>Crit Chance : </b> '.$melee->critChance['rating'].' ('.$melee->critChance['percent'].' %)<br>
<b>Expertise : </b> '.$melee->expertise['value'].' ('.$melee->expertise['percent'].' %)'; }
echo '</td>
</tr>
</table></td>
<td align="center"><span class="white-bold">Ranged stats</span>
<table width="100%" border="0" cellspacing="0" cellpadding="0" background="images/case.jpg" style="border:1px solid #3A3A3A">
<tr>
<td height="156" valign="top" style="padding:7px">';
foreach ($xml->characterInfo[0]->characterTab->ranged as $ranged) {
echo '<b>Skill : </b>'.$ranged->weaponSkill['rating'].'<br>
<b>Damage :</b> '.$ranged->damage['min'].' - '.$ranged->damage['max'].'<br>
<b>Speed :</b> '.$ranged->speed['value'].'<br>
<b>Crit Chance : </b> '.$ranged->critChance['rating'].' ('.$ranged->critChance['percent'].' %)<br>
<b>Hit Rating : </b> '.$ranged->hitRating['value'].' ('.$melee->hitRating['increaseHitPercent'].' %)'; }
echo '</td>
</tr>
</table></td>
<td align="center"><span class="white-bold">Spell Stats</span>
<table width="100%" border="0" cellspacing="0" cellpadding="0" background="images/case.jpg" style="border:1px solid #3A3A3A">
<tr>
<td height="156" valign="top" style="padding:7px">';
foreach ($xml->characterInfo[0]->characterTab->spell as $spell) {
echo '<b>Spell : </b>'.$spell->bonusHealing['value'].'<br>
<b>Hit Rating :</b> '.$spell->hitRating['value'].' ('.$spell->hitRating['increasedHitPercent'].' %)<br>
<b>Crit Rating : </b> '.$spell->critChance['rating'].' <br>
<b>MP5 : </b> '.$spell->manaRegen['notCasting'].''; }
echo '</td>
</tr>
</table></td>
</tr>
</table>';
?> |
Page des fonctions qui vont avec :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| <?php
function GenerationCle($Texte,$CleDEncryptage)
{
$CleDEncryptage = md5($CleDEncryptage);
$Compteur=0;
$VariableTemp = "";
for ($Ctr=0;$Ctr<strlen($Texte);$Ctr++)
{
if ($Compteur==strlen($CleDEncryptage))
$Compteur=0;
$VariableTemp.= substr($Texte,$Ctr,1) ^ substr($CleDEncryptage,$Compteur,1);
$Compteur++;
}
return $VariableTemp;
}
function Crypte($Texte,$Cle)
{
srand((double)microtime()*1000000);
$CleDEncryptage = 545454;
$Compteur=0;
$VariableTemp = "";
for ($Ctr=0;$Ctr<strlen($Texte);$Ctr++)
{
if ($Compteur==strlen($CleDEncryptage))
$Compteur=0;
$VariableTemp.= substr($CleDEncryptage,$Compteur,1).(substr($Texte,$Ctr,1) ^ substr($CleDEncryptage,$Compteur,1) );
$Compteur++;
}
return base64_encode(GenerationCle($VariableTemp,$Cle) );
}
function Decrypte($Texte,$Cle)
{
$Texte = GenerationCle(base64_decode($Texte),$Cle);
$VariableTemp = "";
for ($Ctr=0;$Ctr<strlen($Texte);$Ctr++)
{
$md5 = substr($Texte,$Ctr,1);
$Ctr++;
$VariableTemp.= (substr($Texte,$Ctr,1) ^ $md5);
}
return $VariableTemp;
}
function head($title,$page){}
}
?> |
Codage PHP5
Char.php
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
<?php
// VARS
require "_fonction.php";
require "_class_cryptage.php";
head("Reputation - Armory", "personnage");
$cryptage = new Cryptage();
$cryptage->Cle("blablabla");
$cryptage->Pseudo($_GET['n']);
$cryptage->Realm($_GET['r']);
$cryptage->Save();
// Armory XML
foreach ($xml->characterInfo[0]->character as $char) {
// Codage écourté (le reste n'a pas été dev)
} |
PAGE : _CLASS_CRYPTAGE.PHP
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
<?php
// CLASS Cryptage:
//
class Cryptage
{
private $Cle;
public $xml;
private $Pseudo;
private $Realm;
public $url;
private $realmxml;
private $pdec;
public $Save;
public function __construct() {}
public function Cle($Cle) {
$this->Cle = $Cle;
}
public function Pseudo($Pseudo) {
$this->Pseudo = $Pseudo;
}
public function Realm($Realm) {
$this->Realm = $Realm;
}
function Decrypte($Texte,$Cle)
{
$Texte = GenerationCle(base64_decode($Texte),$Cle);
$VariableTemp = "";
for ($Ctr=0;$Ctr<strlen($Texte);$Ctr++)
{
$md5 = substr($Texte,$Ctr,1);
$Ctr++;
$VariableTemp.= (substr($Texte,$Ctr,1) ^ $md5);
} return $VariableTemp; }
public function Save() {
$this->pdec = Decrypte($this->Pseudo, $this->Cle); // Pseudo Décrypté
$this->realmxml = str_replace(" ", "+", $this->Realm);
$this->url = "http://armory.wow-europe.com/character-sheet.xml?r=" . $this->realmxml . "&n=" . $this->pdec ;
$this->ch = curl_init();
$this->useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
curl_setopt ($this->ch, CURLOPT_URL, $this->url);
curl_setopt ($this->ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($this->ch, CURLOPT_USERAGENT, $this->useragent);
$this->f = curl_exec($this->ch);
curl_close($this->ch);
// Fin CURL
$this->xml = new SimpleXMLElement($this->f);
}
} |
Donc à ce que j'ai compris, je n'arrive pas à faire executer le :
Code:
$this->xml = new SimpleXMLElement($this->f);
de la fonction Save (j'ai essayé de le faire en return et ca me met la même erreur).
Autre question : mon codage (php5 bien sur) est-il bien structuré ? D'autres problèmes de codage sont-ils visible ? (notamment sur les "private, public" j'ai pas trop trop saisi leur fonctionnement apart de "déclarer" une variable).
Merci d'avoir pris le temps de lire mon pavé :D