3 * Table Definition for Images
5 require_once 'DB/DataObject.php';
7 class Pman_Core_DataObjects_Images extends DB_DataObject
10 /* the code below is auto generated do not remove the above tag */
12 public $__table = 'Images'; // table name
13 public $id; // int(11) not_null primary_key auto_increment
14 public $filename; // string(255) not_null
15 public $ontable; // string(32) not_null multiple_key
16 public $onid; // int(11) not_null
17 public $mimetype; // string(64) not_null
18 public $width; // int(11) not_null
19 public $height; // int(11) not_null
20 public $filesize; // int(11) not_null
21 public $displayorder; // int(11) not_null
22 public $language; // string(6) not_null
23 public $parent_image_id; // int(11) not_null
24 public $created; // datetime(19) not_null binary
25 public $imgtype; // string(32) not_null
26 public $linkurl; // string(254) not_null
27 public $descript; // blob(65535) not_null blob
28 public $title; // string(128) not_null
30 /* the code above is auto generated do not remove the tag below */
33 function checkPerm($lvl, $au)
35 // default permissons are to
36 // allow create / edit / if the user has
44 if (method_exists($o, 'checkPerm')) {
45 // edit permissions on related object needed...
46 return $o->checkPerm( $lvl == 'S' ? 'S' : 'E' , $au);
50 return true; //// ??? not really that safe...
54 function beforeInsert($q, $roo)
56 print_r('beforeInsert');exit;
58 if (isset($q['_remote_upload'])) {
59 require_once 'System.php';
61 $tmpdir = System::mktemp("-d remote_upload");
63 $path = $tmpdir . '/' . basename($q['_remote_upload']);
65 if(!file_exists($path)){
66 file_put_contents($path, file_get_contents($q['_remote_upload']));
69 $imageInfo = getimagesize($path);
71 require_once 'File/MimeType.php';
72 $y = new File_MimeType();
73 $ext = $y->toExt(trim((string) $imageInfo['mime'] ));
75 if (!preg_match("/\." . $ext."$/", $path, $matches)) {
76 rename($path,$path.".".$ext);
80 if (!$this->createFrom($path)) {
81 $roo->jerr("erro making image" . $q['_remote_upload']);
84 if(!empty($q['_return_after_create'])){
88 $roo->addEvent("ADD", $this, $this->toEventString());
90 $r = DB_DataObject::factory($this->tableName());
95 $roo->jok($r->URL(-1,'/Images') . '#attachment-'. $r->id);
102 * create an email from file.
103 * these must have been set first.
107 function createFrom($file, $filename=false)
109 print_r('createFrom');exit;
110 // copy the file into the storage area..
111 if (!file_exists($file) || !filesize($file)) {
112 $this->err = "File $file did not exist or is 0 size";
116 $filename = empty($filename) ? $file : $filename;
118 if (empty($this->mimetype)) {
119 require_once 'File/MimeType.php';
120 $y = new File_MimeType();
121 $this->mimetype = $y->fromFilename($filename);
124 $this->mimetype= strtolower($this->mimetype);
126 if (array_shift(explode('/', $this->mimetype)) == 'image') {
128 $imgs = @getimagesize($file);
130 if (empty($imgs) || empty($imgs[0]) || empty($imgs[1])) {
133 list($this->width , $this->height) = $imgs;
137 $this->filesize = filesize($file);
138 $this->created = date('Y-m-d H:i:s');
141 if (empty($this->filename)) {
142 $this->filename = basename($filename);
145 //DB_DataObject::debugLevel(1);
154 $f = $this->getStoreName();
156 if (!file_exists($dest)) {
157 // currently this is 0775 due to problems using shared hosing (FTP)
158 // it makes all the files unaccessable..
159 // you can normally solve this by giving the storedirectory better perms
160 // if needed on a dedicated server..
161 $oldumask = umask(0);
162 mkdir($dest, 0775, true);
173 // $this->createThumbnail(0,50);
179 * Calculate target file name
181 * @return - target file name
183 function getStoreName()
185 $opts = HTML_FlexyFramework::get()->Pman;
186 $fn = preg_replace('/[^a-z0-9\.]+/i', '_', $this->filename);
187 return implode( '/', array(
188 $opts['storedir'], '_images_', date('Y/m', strtotime($this->created)), $this->id . '-'. $fn
194 * deletes all the image instances of it...
198 function beforeDelete()
200 $fn = $this->getStoreName();
201 if (file_exists($fn)) {
207 if (file_exists($d)) {
210 while (false !== ($fn = readdir($dh))) {
211 if (substr($fn, 0, strlen($b)) == $b) {
212 unlink($d. '/'. $fn);
219 * check mimetype against type
220 * - eg. img.is(#image#)
225 if (empty($this->mimetype)) {
228 return 0 === strcasecmp($type, array_shift(explode('/',$this->mimetype)));
232 * onUpload (singlely attached image to a table)
235 function onUploadWithTbl($tbl, $fld)
237 if ( $tbl->__table == 'Images') {
238 return; // not upload to self...
240 if (empty($_FILES['imageUpload']['tmp_name']) ||
241 empty($_FILES['imageUpload']['name']) ||
242 empty($_FILES['imageUpload']['type'])
247 $image = DB_DataObject::factory('Images');
248 $image->get($tbl->$fld);
249 $image->beforeDelete();
253 $image = DB_DataObject::factory('Images');
254 $image->onid = $tbl->id;
255 $image->ontable = $tbl->__table;
256 $image->filename = $_FILES['imageUpload']['name'];
257 $image->mimetype = $_FILES['imageUpload']['type'];
259 if (!$image->createFrom($_FILES['imageUpload']['tmp_name'])) {
263 $tbl->$fld = $image->id;
270 function onUpload($roo)
272 print_r('onUpload');exit;
273 //print_r($_FILES); echo $_FILES['imageUpload']['type'];exit;
274 if (empty($_FILES['imageUpload']['tmp_name']) ||
275 empty($_FILES['imageUpload']['name']) ||
276 empty($_FILES['imageUpload']['type'])
280 0=>"There is no error, the file uploaded with success",
281 1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini",
282 2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form" ,
283 3=>"The uploaded file was only partially uploaded",
284 4=>"No file was uploaded",
285 6=>"Missing a temporary folder"
287 $estr = (empty($_FILES['imageUpload']['error']) ? '?': $emap[$_FILES['imageUpload']['error']]);
288 $this->err = "Missing file details : Error=". $estr;
293 $this->beforeDelete();
295 if ( empty($this->ontable)) {
296 $this->err = "Missing ontable";
300 if (!empty($this->imgtype) && $this->imgtype[0] == '-' && !empty($this->onid)) {
301 // then its an upload
302 $img = DB_DataObject::factory('Images');
303 $img->onid = $this->onid;
304 $img->ontable = $this->ontable;
305 $img->imgtype = $this->imgtype;
308 while ($img->fetch()) {
309 $img->beforeDelete();
317 require_once 'File/MimeType.php';
318 $y = new File_MimeType();
319 $this->mimetype = $_FILES['imageUpload']['type'];
320 if (in_array($this->mimetype, array(
322 'application/octet-stream',
323 'image/x-png', // WTF does this?
324 'image/pjpeg', // WTF does this?
325 'application/x-apple-msg-attachment', /// apple doing it's magic...
326 'application/vnd.ms-excel', /// sometimes windows reports csv as excel???
327 'application/csv-tab-delimited-table', // windows again!!?
328 ))) { // weird tyeps..
329 $inf = pathinfo($_FILES['imageUpload']['name']);
330 $this->mimetype = $y->fromExt($inf['extension']);
334 $ext = $y->toExt(trim((string) $this->mimetype ));
336 $this->filename = empty($this->filename) ?
337 $_FILES['imageUpload']['name'] : ($this->filename .'.'. $ext);
341 if (!$this->createFrom($_FILES['imageUpload']['tmp_name'])) {
342 $this->err = isset($this->err) ? $this->err : "createFrom Image failed";
352 * return a list of images for an object, optionally with a mime regex.
353 * eg. '%/pdf' or 'image/%'
357 * $i = DB_DataObject::factory('Images');
358 * $i->imgtype = 'LOGO';
359 * $ar = $i->gather($somedataobject, 'image/%');
361 * @param {DB_DataObject} dataobject = the object to gather data on.
362 * @param {String} mimelike LIKE query to use for search
365 function gather($obj, $mime_like='', $opts=array())
367 //DB_DataObject::debugLevel(1);
368 if (empty($obj->id)) {
373 $c->whereAddIn($this->tableName() . '.ontable', array( $obj->tableName(), $obj->__table) , 'string');
376 if (!empty($mime_like)) {
377 $c->whereAdd("Images.mimetype LIKE '". $c->escape($mime_like) ."'");
379 $c->orderBy('created DESC');
381 return $c->fetchAll();
386 * set or get the dataobject this image is associated with
387 * @param DB_DataObject $obj An object to associate this image with
388 * (does not store it - you need to call update() to do that)
389 * @return DB_DataObject the dataobject this image is attached to.
391 function object($obj=false)
393 if ($obj === false) {
394 if (empty($this->ontable) || empty($this->onid)) {
397 $ret = DB_DataObject::factory($this->ontable);
398 $ret->get($this->onid);
403 $this->ontable = $obj->tableName();
404 $this->onid = $obj->id; /// assumes our nice standard of using ids..
409 function toRooArray($req) {
411 $ret= $this->toArray();
415 $ff = HTML_FlexyFramework::get();
418 $ret['public_baseURL'] = isset($ff->Pman_Images['public_baseURL']) ?
419 $ff->Pman_Images['public_baseURL'] : $ff->baseURL;
421 if (!empty($req['query']['imagesize'])) {
422 // query/imageBaseURL ... depricated...? -- set it in config?
424 $baseURL = isset($req['query']['imageBaseURL']) ? $req['query']['imageBaseURL'] : $ret['public_baseURL'];
426 $ret['url'] = $this->URL(-1, '/Images/Download',$baseURL);
428 $ret['url_view'] = $this->URL(-1, '/Images',$baseURL);
430 if (!empty($req['query']['imagesize'])) {
431 $ret['url_thumb'] = $this->URL($req['query']['imagesize'], '/Images/Thumb',$baseURL);
441 * URL - create a url for the image.
442 * size - use -1 to show full size.
443 * provier = baseURL + /Images/Thumb ... use '/Images/' for full
447 function URL($size , $provider = '/Images/Thumb', $baseURL=false)
450 return 'about:blank';
454 $ff = HTML_FlexyFramework::get();
455 $baseURL = $baseURL ? $baseURL : $ff->baseURL ;
456 if (preg_match('#^http[s]*://#', $provider)) {
461 $provider = preg_replace('#/Thumb$#', '', $provider);
463 return $baseURL . $provider . "/{$this->id}/{$this->filename}";
466 //$size = max(100, (int) $size);
467 //$size = min(1024, (int) $size);
468 // the size should 200x150 to convert
469 $sizear = preg_split('/(x|c)/', $size);
470 if(empty($sizear[1])){
473 $size = implode(strpos($size,'c') > -1 ? 'c' : 'x', $sizear);
475 $fc = $this->toFileConvert();
478 $mt = $this->mimetype;
479 if (!preg_match('#^image/#i',$mt)) {
483 $fc->convert($mt, $size);
485 return $baseURL . $provider . "/$size/{$this->id}/{$this->filename}";
488 * size could be 123x345
492 function toHTML($size, $provider = '/Images/Thumb')
497 $sz = explode('x', $size);
500 if (!$this->id || empty($this->width)) {
502 $this->width = empty($sz[1]) ? $sx : $sz[1];
506 $ratio = empty($this->width) ? 1 : $this->height/ ($this->width *1.0);
513 if (strlen($this->title)) {
514 $extra = ' title="'. htmlspecialchars($this->title) . '"';
517 return '<img src="' . $this->URL($size, $provider) . '"' .
519 ' width="'. $sx . '"' .
520 ' height="'. $sy . '">';
527 * #2142 [new] CMS - image link urls
532 function toLinkHTML($size, $provider = '/Images/Thumb')
534 if(empty($this->linkurl)){
535 return $this->toHTML($size, $provider = '/Images/Thumb');
538 return '<a href="'.$this->linkurl.'" target="_blank">'.$this->toHTML($size, $provider = '/Images/Thumb').'</a>';
544 * to Fileconvert object..
549 function toFileConvert()
551 require_once 'File/Convert.php';
552 $fc = new File_Convert($this->getStoreName(), $this->mimetype);
559 require_once 'File/MimeType.php';
561 $y = new File_MimeType();
562 return $y->toExt($this->mimetype);
574 function setFromRoo($ar, $roo)
576 // not sure why we do this..
578 // if imgtype starts with '-' ? then we set the 'old' (probably to delete later)
579 if (!empty($ar['imgtype']) && !empty($ar['ontable']) && !empty($ar['onid']) && ($ar['imgtype'][0] == '-')) {
582 if ($this->find(true)) {
583 $roo->old = clone($this);
588 if (!empty($ar['_copy_from'])) {
590 if (!$this->checkPerm( 'A' , $roo->authUser)) {
591 $roo->jerr("IMAGE UPLOAD PERMISSION DENIED");
594 $copy = DB_DataObject::factory('Images');
595 $copy->get($ar['_copy_from']);
596 $this->setFrom($copy->toArray());
598 $this->createFrom($copy->getStoreName());
600 $roo->addEvent("ADD", $this, $this->toEventString());
602 $r = DB_DataObject::factory($this->tableName());
607 $roo->jok($r->toArray());
614 // FIXME - we should be checking perms here...
616 // this should be doign update
619 if (!$this->checkPerm($this->id ? 'A' : 'E', $roo->authUser)) {
620 $roo->jerr("IMAGE UPLOAD PERMISSION DENIED");
625 if (!isset($_FILES['imageUpload'])) {
626 return; // standard update...
630 // print_r(!$this->onUpload($this));
632 if ( !$this->onUpload($this)) {
633 $roo->jerr("File upload failed : error = ". (!empty($this->err) ? $this->err : ''));
636 $roo->addEvent("ADD", $this, $this->toEventString());
638 $r = DB_DataObject::factory($this->tableName());
643 $roo->jok($r->toArray());
647 function toEventString()
650 //$p = DB_DataObject::factory($this->ontable);
652 // return "ERROR unknown table? {$this->ontable}";
656 return $this->filename .' - on ' . $this->ontable . ':' . $this->onid;
657 //$p->toEventString();
660 function onUploadFromData($data, $roo)
663 $this->err = "Missing file details";
668 $this->beforeDelete();
671 if (empty($this->ontable)) {
672 $this->err = "Missing ontable";
676 if (!empty($this->imgtype) && $this->imgtype[0] == '-' && !empty($this->onid)) {
677 // then its an upload
678 $img = DB_DataObject::factory('Images');
679 $img->onid = $this->onid;
680 $img->ontable = $this->ontable;
681 $img->imgtype = $this->imgtype;
684 while ($img->fetch()) {
685 $img->beforeDelete();
691 require_once 'File/MimeType.php';
692 $y = new File_MimeType();
694 if (in_array($this->mimetype, array(
696 'application/octet-stream',
697 'image/x-png', // WTF does this?
698 'image/pjpeg', // WTF does this?
699 'application/x-apple-msg-attachment', /// apple doing it's magic...
700 'application/vnd.ms-excel', /// sometimes windows reports csv as excel???
701 'application/csv-tab-delimited-table', // windows again!!?
702 ))) { // weird tyeps..
703 $inf = pathinfo($this->filename);
704 $this->mimetype = $y->fromExt($inf['extension']);
707 $ext = $y->toExt(trim((string) $this->mimetype ));
709 if(array_pop(explode('.', $this->filename)) != $ext){
710 $this->filename = $this->filename .'.'. $ext;
713 if (!$this->createFromData($data)) {
721 function createFromData($data)
724 $this->mimetype= strtolower($this->mimetype);
726 if (array_shift(explode('/', $this->mimetype)) == 'image') {
728 $imgs = @getimagesize($data);
730 if (!empty($imgs) && !empty($imgs[0]) && !empty($imgs[1])) {
731 list($this->width , $this->height) = $imgs;
735 $this->created = date('Y-m-d H:i:s');
743 $f = $this->getStoreName();
745 if (!file_exists($dest)) {
746 $oldumask = umask(0);
747 mkdir($dest, 0775, true);
751 file_put_contents($f, file_get_contents("data://" . $data));
755 $this->filesize = filesize($f);