Skip to content

Commit

Permalink
Add warranty helper
Browse files Browse the repository at this point in the history
  • Loading branch information
bochoven committed Dec 5, 2018
1 parent 18bf4e5 commit 44be702
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 2 deletions.
136 changes: 136 additions & 0 deletions helpers/warranty_helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php

use munkireport\lib\Request;

/**
* Unfortunately we have to scrape the page as Apple discontinued the json api
*
* @param object warranty model instance
* @author AvB
**/
function check_warranty_status(&$warranty_model)
{
// Error message
$error = '';

// Check if virtual machine
// We assume vmware serials contain upper and lower chars
// There are actual macs with a serial starting with VM
// Todo: make this check more robust/support other VMs
if (strtoupper($warranty_model->serial_number) != $warranty_model->serial_number) {
$warranty_model->status = "Virtual Machine";

// Use reg_timestamp as purchase_date
$report = new Reportdata_model($warranty_model->serial_number);
$warranty_model->purchase_date = date('Y-m-d', $report->reg_timestamp);
$warranty_model->end_date = date('Y-m-d', strtotime('+10 year'));

$machine = new Machine_model($warranty_model->serial_number);
//$machine->img_url = $matches[1]; Todo: get image url for VM
$machine->machine_desc = 'VMware virtual machine';
$machine->save();
return;
}

// Previous entry
if ($warranty_model->end_date) {
if ($warranty_model->end_date < date('Y-m-d')) {
$warranty_model->status = 'Expired';
}
} else // New entry
{
// As of 19 oct 2015 warranty information is behind a Captcha
// so we can't do any automated lookup anymore
$warranty_model->status = "Can't lookup warranty";
$warranty_model->purchase_date = estimate_manufactured_date($warranty_model->serial_number);

// Calculate time to expire
$max_applecare_years = sprintf('+%s year', conf('max_applecare', 3));
$purchase_time = strtotime($warranty_model->purchase_date);
$warranty_model->end_date = date('Y-m-d', strtotime($max_applecare_years, $purchase_time));
}


// Get machine model from apple (only when not set or failed)
$machine = new Machine_model($warranty_model->serial_number);
model_description_lookup($warranty_model->serial_number);
$lookup_failed = array('model_lookup_failed', 'unknown_model');
if (! $machine->machine_desc or in_array($machine->machine_desc, $lookup_failed)) {
$machine->machine_desc = model_description_lookup($warranty_model->serial_number);
$machine->save();
}

return $error;
}

/**
* Estimates the week the machine was manfactured based off it's serial
* number
* Ported from python warranty scripts by Adam Reed <[email protected]>
*
* @return string date
* @author Arjen van Bochoven
**/
function estimate_manufactured_date($serial)
{
# See http://www.macrumors.com/2010/04/16/apple-tweaks-serial-number
# -format-with-new-macbook-pro/ for details about serial numbers

if (strlen($serial) == 11) {
$year = $serial[2];
$est_year = 2000 + strpos(' 3456789012', $year);
$week = max(1, intval(substr($serial, 3, 2)));
return formatted_manufactured_date($est_year, $week);
} elseif (strlen($serial) == 12) {
$year_code = 'cdfghjklmnpqrstvwxyz';
$year = strtolower($serial[3]);
$est_year = 2010 + intval(strpos($year_code, $year) / 2);
$est_half = strpos($year_code, $year) % 2;
$week_code = ' 123456789cdfghjklmnpqrtvwxy';
$week = strtolower(substr($serial, 4, 1));
$est_week = strpos($week_code, $week) + ($est_half * 26);
return formatted_manufactured_date($est_year, $est_week);
} else {
return 'unknown';
}
}

function formatted_manufactured_date($year, $week)
{
$strtime = sprintf('%sW%02s1', $year, $week);
return date('Y-m-d', strtotime($strtime));
}

function model_description_lookup($serial)
{
if (strpos($serial, 'VMWV') === 0) {
return 'VMware virtual machine';
}

$options = [
'query' => [
'page' => 'categorydata',
'serialnumber' => $serial
]
];

$client = new Request();
$result = $client->get('http://km.support.apple.com/kb/index', $options);

if ( ! $result) {
return 'model_lookup_failed';
}

try {
$categorydata = json_decode($result);
if(isset($categorydata->name)){
return $categorydata->name;
}
else{
return 'unknown_model';
}
} catch (Exception $e) {
return 'model_lookup_failed';
}

}
2 changes: 1 addition & 1 deletion warranty_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function estimate_manufactured_date($serial_number = '')
die('Authenticate first.'); // Todo: return json?
}

require_once(conf('application_path') . "helpers/warranty_helper.php");
require_once(__DIR__ . "/helpers/warranty_helper.php");
$out = array('date' => estimate_manufactured_date($serial_number));
$obj = new View();
$obj->view('json', array('msg' => $out));
Expand Down
2 changes: 1 addition & 1 deletion warranty_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function check_status($force = false)
$prev_status = $this->status;

// Load warranty helper
require_once(conf('application_path').'helpers/warranty_helper.php');
require_once(__DIR__ . '/helpers/warranty_helper.php');

// Update needed, check with apple
$error = check_warranty_status($this);
Expand Down

0 comments on commit 44be702

Please sign in to comment.