/
Pictures.php
107 lines (106 loc) · 3.42 KB
/
Pictures.php
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
<?php
// vim: set et ts=4 sw=4 fdm=marker:
/*
Copyright (C) 2008 Jesse Hogan <jessehogan0@gmail.com>
All rights reserverd
*/
class Pictures{
var $_parent, $_prevDir='';
var $_dirtyDir=false;
function __construct($parent){
$this->_parent =& $parent;
}
function ParentType(){
return strtolower(get_class($this->_parent));
}
function MarkDirNameDirty(){
if (!$this->_dirtyDir && $this->_prevDir == ""){
if (!$this->_parent->IsNew()){
$this->_prevDir = $this->PictureDirectory();
$this->_dirtyDir = true;
}
}
}
public function BasePictureDirectory(){
$gs =& GlobalSingleton::GetInstance();
$dir = $gs->PictureDirectory();
if (trim($dir) == "")
throw new Exception("Picture Directory not found");
return $dir;
}
function PictureDirectory(){
$parType = $this->ParentType();
if ($parType == 'movie'){
$title = strtolower($this->_parent->Title());
$baseSubDir = 'movies';
}elseif ($parType == 'person'){
$title = strtolower($this->_parent->Name());
$baseSubDir = 'persons';
}else{
throw new Exception("Parent type '$parType' not supported");
}
if ($title != ""){
$title = str_replace(' ', '-', $title);
$initial = substr($title, 0, 1);
$baseDir = $this->BasePictureDirectory();
return "$baseDir/$baseSubDir/$initial/$title/";
}else{
return "";
}
}
function MainPicture(){
$dir = $this->PictureDirectory();
if (trim($dir) != "")
return $this->PictureDirectory() . 'main.jpg';
return "";
}
function SnarfMainPicture($url){
//TODO: Use curl when depedencies are met
if (trim($url) == ''){
return;
}
$this->MakePictureDirectory();
if (file_exists($this->MainPicture())){
if ( ! unlink($this->MainPicture())){
throw new Exception("Failed to delete existing pic");
}
}
$args = "$url -O " . $this->MainPicture();
$args = escapeshellcmd($args);
$cmd = "wget $args";
system($cmd, $retVal);
if ($retVal != 0){
throw new Exception("Snarf failed; errno:$retVal");
}
$args = escapeshellcmd($this->MainPicture());
$cmd = "touch $args";
system($cmd, $retVal);
if ($retVal != 0){
throw new Exception("Touch failed; errno:$retVal");
}
}
function MakePictureDirectory(){
$picDir = $this->PictureDirectory();
if ($picDir == "") return false;
if ($this->_prevDir == ""){
if (! file_exists($picDir)){
$ret = mkdir($picDir, 0755, true);
if (!$ret){
throw new Exception("Couldn't create picture directory: $picDir");
}
}
}else{
if (! file_exists($picDir)){
$ret = mkdir($picDir, 0755, true);
if (!$ret){
throw new Exception("Couldn't create picture directory: $picDir");
}
}
if (! rename ($this->_prevDir, $picDir)){
throw new Exception("failed: rename($this->_prevDir, $picDir)");
}
}
return true;
}
}
?>