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
<?php
class HashJob extends BEAppModel {
private $hashString = "abcdefghijklmnopqrstuvwxyz";
public function beforeSave() {
if (empty($this->data["HashJob"]) && empty($this->data)) {
return false;
} elseif (empty($this->data["HashJob"]) && !empty($this->data)) {
$this->data["HashJob"] = $this->data;
}
if (empty($this->data["HashJob"]["hash"])) {
$this->data["HashJob"]["hash"] = $this->generateHash();
}
if (empty($this->data["HashJob"]["params"])) {
$columnTypes = $this->getColumnTypes();
$params = array();
foreach ($this->data["HashJob"] as $key => $val) {
if (!array_key_exists($key, $columnTypes)) {
$params[$key] = $val;
}
}
$this->data['HashJob']['params'] = json_encode($params, true);
}
return true;
}
public function afterFind($results) {
if (!empty($results)) {
foreach ($results as $key => $val) {
if ( !empty($val["HashJob"]["status"]) && ($val["HashJob"]["status"] != 'closed' && $val["HashJob"]["status"] != 'failed' ) &&
(!empty($val["HashJob"]["expired"]) && $val["HashJob"]["expired"] < date("Y-m-d H:i:s", time()))) {
$this->id = $val["HashJob"]["id"];
$this->saveField("status", "expired");
$results[$key]["HashJob"]["status"] = "expired";
}
if (!empty($val['HashJob']['params'])) {
$params = json_decode($val['HashJob']['params'], true);
if (!(is_array($params) && (json_last_error() === JSON_ERROR_NONE))) {
$params = @unserialize($val['HashJob']['params']);
}
if ($params !== false && is_array($params)) {
$results[$key]['HashJob'] = array_merge($results[$key]['HashJob'], $params);
}
}
if (!empty($val['HashJob']['result'])) {
$res = json_decode($val['HashJob']['result'], true);
if ($res !== false && is_array($res)) {
$results[$key]['HashJob'] = array_merge($results[$key]['HashJob'], $res);
}
}
}
}
return $results;
}
public function generateHash($authKey = false) {
return $authKey ? Security::generateAuthKey() : md5(str_shuffle($this->hashString) . microtime());
}
public function getExpirationDate($hashExpiredTime=null) {
if (empty($hashExpiredTime)) {
$hashExpiredTime = Configure::read("hashExpiredTime");
}
return date("Y-m-d H:i:s", time() + $hashExpiredTime);
}
}
?>