00001 <?php
00002
00019 class Pictcha {
00020
00021 private $pictchaArray = array();
00022 private $maxPictcha = 0;
00023 private $pictchaValue = array();
00024 private $imagesFolder = '';
00025
00030 public function __construct() {
00031 $this->imagesFolder = SPath::getRelPath('Common') . '/XForms/pictcha/getImage.php5?id=';
00032 }
00033
00041 public function getPictcha() {
00042
00043
00044
00045 $this->getPictchaData();
00046
00047
00048
00049 return ($this->getRandomizedPictcha());
00050 }
00051
00059 protected function getPictchaData() {
00060
00061
00062 $xmlData = file_get_contents(dirname(__FILE__) . '/pictcha.xml');
00063
00064 if ($xmlData) {
00065 # initialize the pictcha array
00066 if (!is_array($this->pictchaArray)) {
00067 $this->pictchaArray = array();
00068 }
00069
00070 # Now that we have the xml data, let's parse it for a list of errors
00071 $xmlData = simplexml_load_string($xmlData);
00072
00073 # Each of the pictcha's are categorized into different groups called series.
00074 # A series contains a listing of all items that look visually similar. Therefore,
00075 # only one item will be selected from a series.
00076 foreach ($xmlData AS $series) {
00077
00078 # We will now save the root node of the array to be the series name
00079 $this->pictchaArray[(String)$series->seriesName] = array();
00080 $this->pictchaArray[(String)$series->seriesName]["folder"] = (String)$series->folder;
00081 $this->pictchaArray[(String)$series->seriesName]["location"] = array();
00082
00083 foreach($series->pictcha AS $pictureData) {
00084 array_push($this->pictchaArray[(String)$series->seriesName]["location"], (String)$pictureData->location);
00085 }
00086 }
00087 }
00088 }
00089
00097 protected function getRandomizedPictcha() {
00098
00099 # We will randomly select "n" values to generate a Pictcha frame
00100 # Assuring the each selection has not been previous made
00101 #
00102 # First let's store the available series in an array
00103 $pictchaSeries = array();
00104 $dOut = "";
00105
00106 foreach($this->pictchaArray AS $name => $value) {
00107 array_push($pictchaSeries, $name);
00108 }
00109
00110 # We will make a copy of the pictcha Array for later use
00111 $tempPictchaSeries = $pictchaSeries;
00112
00113 if (count($tempPictchaSeries) < $this->getMaxPictcha()) {
00114 $numPictcha = count($tempPictchaSeries);
00115 } else {
00116 $numPictcha = $this->getMaxPictcha();
00117 }
00118
00119 $pictchaList = array();
00120
00121 # Now we will choose a random series, remove it from our
00122 # comparision array, and then choose the next one.
00123 # The results will be stored in the pictchaList
00124 while ($numPictcha > 0) {
00125 $randValue = rand(0, $numPictcha-1);
00126 array_push($pictchaList, $tempPictchaSeries[$randValue]);
00127
00128 $temp = $tempPictchaSeries[count($tempPictchaSeries)-1];
00129 $tempPictchaSeries[$randValue] = $temp;
00130 array_pop($tempPictchaSeries);
00131 $numPictcha--;
00132 }
00133
00134 # We need to also choose two series to ask the user a question about
00135 $randA = rand(0, count($pictchaList)-1);
00136 $randB = "";
00137
00138 while ($randB == "") {
00139 $newRand = rand(0, count($pictchaList)-1);
00140 if ($newRand != $randA) {
00141 $randB = $newRand;
00142 }
00143 }
00144
00145 $qA = $pictchaList[$randA];
00146 $qB = $pictchaList[$randB];
00147 $this->setPictchaData($qA);
00148 $this->setPictchaData($qB);
00149
00150 # Now that we have our random pictcha, we will now mask their
00151 # identities and select an image from each
00152 $dOut .= ("<table class=\"pchaTable\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n");
00153 $dOut .= ("<tr class=\"pchaRow\">\n");
00154
00155 for ($i = 0; $i < count($pictchaList); $i++) {
00156
00157 $seed = substr(MD5(time()), 0, 4);
00158 $img = $this->getRandomSeriesPictcha($pictchaList[$i]);
00159 $folder = substr(MD5($seed . $pictchaList[$i]), 0, 15);
00160 $file = substr(MD5($seed . $img), 0, 15);
00161 $pictchaList[$i] = $folder . $file . $seed;
00162
00163 if ($i == (int)(count($pictchaList)/2-1)) {
00164 $dOut .= ("<td class=\"pchaData\" id=\"pchaData_" . ($i+1) . "\"><img width=\"50\" height=\"50\" src=\"" . $this->imagesFolder . "$pictchaList[$i]\" onclick=\"javascript:selectPictcha('$pictchaList[$i]', '" . ($i+1) . "');\" alt=\"pictcha\" /></td>\n");
00165 $dOut .= ("</tr><tr class=\"pchaRow\">");
00166 } else {
00167 $dOut .= ("<td class=\"pchaData\" id=\"pchaData_" . ($i+1) . "\"><img width=\"50\" height=\"50\" src=\"" . $this->imagesFolder . "$pictchaList[$i]\" onclick=\"javascript:selectPictcha('$pictchaList[$i]', '" . ($i+1) . "');\" alt=\"pictcha\" /></td>\n");
00168 }
00169 }
00170
00171 $dOut .= ("<td style=\"display:none\">");
00172 $dOut .= ("<input type=\"hidden\" name=\"series_a\" value=\"$qA\"/>\n");
00173 $dOut .= ("<input type=\"hidden\" name=\"series_b\" value=\"$qB\"/>\n");
00174 $dOut .= ("<input type=\"hidden\" name=\"pictcha_a\" value=\"\" id=\"pictcha_a\" />\n");
00175 $dOut .= ("<input type=\"hidden\" name=\"pictcha_b\" value=\"\" id=\"pictcha_b\" />\n");
00176 $dOut .= ("</td>");
00177 $dOut .= ("</tr>\n");
00178 $dOut .= ("</table>\n");
00179
00180 return ($dOut);
00181 }
00182
00190 public function getMaxPictcha() {
00191 if ($this->maxPictcha != 0) {
00192 return ($this->maxPictcha);
00193 } else {
00194 return (9);
00195 }
00196 }
00197
00206 public function setMaxPictcha($picIn) {
00207 $this->maxPictcha = $picIn;
00208 }
00209
00218 protected function getRandomSeriesPictcha($series) {
00219
00220 $seriesItem = $this->pictchaArray[$series]["location"];
00221
00222 $item = rand(0, count($seriesItem)-1);
00223
00224 return ($this->pictchaArray[$series]["folder"] . "/" . $this->pictchaArray[$series]["location"][$item]);
00225 }
00226
00235 public function getPictchaImage($image) {
00236
00237 $this->getPictchaData();
00238
00239
00240
00241
00242 $seed = substr($image, strlen($image)-4, strlen($image));
00243
00244
00245
00246
00247
00248 $type = "";
00249
00250 foreach($this->pictchaArray AS $name => $value) {
00251 if (substr(MD5($seed. $name), 0, 15) == substr($image, 0, 15)) {
00252 $type = $name;
00253 break;
00254 }
00255 }
00256
00257
00258
00259
00260
00261 for ($i = 0; $i < count(@$this->pictchaArray[$type]["location"]); $i++) {
00262
00263 if (substr($image, 15, 15) == substr(MD5($seed . $this->pictchaArray[$type]["folder"] . "/" . $this->pictchaArray[$type]["location"][$i]), 0, 15)) {
00264 header("Content-type: image/jpeg");
00265 print file_get_contents(dirname(__FILE__) . "/pictcha/data/" . $this->pictchaArray[$type]["folder"] . "/" . $this->pictchaArray[$type]["location"][$i]);
00266
00267
00268
00269 break;
00270 }
00271 }
00272
00273 }
00274
00284 protected function getValidImage($series, $image) {
00285
00286 $this->getPictchaData();
00287
00288
00289
00290
00291 $seed = substr($image, strlen($image)-4, strlen($image));
00292
00293
00294
00295
00296
00297 $type = "";
00298
00299 foreach($this->pictchaArray AS $name => $value) {
00300 if (substr(MD5($seed. $name), 0, 15) == substr($image, 0, 15)) {
00301 $type = $name;
00302 break;
00303 }
00304 }
00305
00306
00307
00308
00309
00310 for ($i = 0; $i < count(@$this->pictchaArray[$type]["location"]); $i++) {
00311
00312 if (substr($image, 15, 15) == substr(MD5($seed . $this->pictchaArray[$type]["folder"] . "/" . $this->pictchaArray[$type]["location"][$i]), 0, 15)) {
00313 if ($type == $series) {
00314 return (true);
00315 }
00316 }
00317 }
00318
00319 return (false);
00320 }
00321
00333 public function getValidPictcha($seriesA, $hashA, $seriesB, $hashB) {
00334 if ($this->getValidImage($seriesA, $hashA) && $this->getValidImage($seriesB, $hashB)) return (true);
00335 else return (false);
00336 }
00337
00345 public function getJavascript() {
00346
00347 $data =<<< END_JS
00348 <script type="text/javascript" charset="utf-8">
00349 <!--
00350 var sp = 1; var i;
00351 var color, letter = '';
00352 var defaultColor = 'green';
00353 function selectPictcha(hash, id)
00354 {
00355
00356 if (document.getElementById('pchaData_' + id).style.backgroundColor != "" && document.getElementById('pchaData_' + id).style.backgroundColor != defaultColor) {
00357 reset();
00358 }
00359
00360 else if (sp < 3){
00361
00362 color = (sp == 1) ? 'red' : 'blue';
00363 letter = (sp == 1) ? 'a' : 'b';
00364 document.getElementById('pchaData_' + id).style.background=color;
00365 document.getElementById('pictcha_' + letter).value = hash;
00366 sp++;
00367 } else reset();
00368 }
00369 function reset()
00370 {
00371 sp = 1;
00372 for (i = 1; i <= 8; i++) document.getElementById('pchaData_' + i).style.background=defaultColor;
00373 for (i = 0; i < 2; i++) {
00374 letter = (i == 1) ? 'a' : 'b';
00375 document.getElementById('pictcha_' + letter).value = "";
00376 }
00377 }
00378 -->
00379 </script>
00380 END_JS;
00381
00382 return ($data);
00383 }
00384
00393 protected function setPictchaData($data) {
00394 array_push($this->pictchaValue, $data);
00395 }
00396
00404 public function getPictchaValue() {
00405 array_push($this->pictchaValue, "yo Mama");
00406 return ($this->pictchaValue);
00407 }
00408 }
00409 ?>