/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
- function createFrom($file)
+ function checkPerm($perm, $au)
+ {
+ // default permissons are to
+ // allow create / edit / if the user has
+ if (!$au) {
+
+
+
+ return false;
+ }
+
+
+
+ $o = $this->object();
+ //print_r($o);
+ if (method_exists($o, 'hasPerm')) {
+ // edit permissions on related object needed...
+ return $o->hasPerm( $perm == 'S' ? 'S' : 'E' , $au);
+
+ }
+
+ return true; //// ??? not really that safe...
+
+ }
+
+
+
+
+
+
+ /**
+ * create an email from file.
+ * these must have been set first.
+ * ontable / onid.
+ *
+ */
+ function createFrom($file, $filename=false)
{
// copy the file into the storage area..
if (!file_exists($file) || !filesize($file)) {
return false;
}
+ $filename = empty($filename) ? $file : $filename;
- $imgs = @getimagesize($file);
+ if (empty($this->mimetype)) {
+ require_once 'File/MimeType.php';
+ $y = new File_MimeType();
+ $this->mimetype = $y->fromFilename($filename);
+ }
- if (empty($imgs) || empty($imgs[0]) || empty($imgs[1])) {
- // it's a file!!!!
- } else {
- list($this->width , $this->height) = $imgs;
+ $this->mimetype= strtolower($this->mimetype);
+
+ if (array_shift(explode('/', $this->mimetype)) == 'image') {
+
+ $imgs = @getimagesize($file);
+
+ if (empty($imgs) || empty($imgs[0]) || empty($imgs[1])) {
+ // it's a file!!!!
+ } else {
+ list($this->width , $this->height) = $imgs;
+ }
}
$this->filesize = filesize($file);
$this->created = date('Y-m-d H:i:s');
+
+
+ if (empty($this->filename)) {
+ $this->filename = basename($filename);
+ }
+
//DB_DataObject::debugLevel(1);
if (!$this->id) {
$this->insert();
$f = $this->getStoreName();
$dest = dirname($f);
if (!file_exists($dest)) {
-
+ // currently this is 0775 due to problems using shared hosing (FTP)
+ // it makes all the files unaccessable..
+ // you can normally solve this by giving the storedirectory better perms
+ // if needed on a dedicated server..
$oldumask = umask(0);
- mkdir($dest, 0770, true);
+ mkdir($dest, 0775, true);
umask($oldumask);
}
*/
function getStoreName()
{
- $opts = PEAR::getStaticProperty('Pman', 'options');
+ $opts = HTML_FlexyFramework::get()->Pman;
$fn = preg_replace('/[^a-z0-9\.]+/i', '_', $this->filename);
return implode( '/', array(
$opts['storedir'], '_images_', date('Y/m', strtotime($this->created)), $this->id . '-'. $fn
if (file_exists($fn)) {
unlink($fn);
}
-
+ // delete thumbs..
$b = basename($fn);
$d = dirname($fn);
- $dh = opendir($d);
- while (false !== ($fn = readdir($dh))) {
- if (substr($fn, 0, strlen($b)) == $b) {
- unlink($d. '/'. $fn);
+ if (file_exists($d)) {
+
+ $dh = opendir($d);
+ while (false !== ($fn = readdir($dh))) {
+ if (substr($fn, 0, strlen($b)) == $b) {
+ unlink($d. '/'. $fn);
+ }
}
}
-
}
-
+ /**
+ * check mimetype against type
+ * - eg. img.is(#image#)
+ *
+ */
+ function is($type)
+ {
+ if (empty($this->mimetype)) {
+ return false;
+ }
+ return 0 === strcasecmp($type, array_shift(explode('/',$this->mimetype)));
+ }
/**
* onUpload (singlely attached image to a table)
/**
* return a list of images for an object, optionally with a mime regex.
* eg. '%/pdf' or 'image/%'
+ *
+ * usage:
+ *
+ * $i = DB_DataObject::factory('Images');
+ * $i->imgtype = 'LOGO';
+ * $ar = $i->gather($somedataobject, 'image/%');
+ *
+ * @param {DB_DataObject} dataobject = the object to gather data on.
+ * @param {String} mimelike LIKE query to use for search
+
*/
- function gather($obj, $mime_like='')
+ function gather($obj, $mime_like='', $opts=array())
{
- DB_DataObject::debugLevel(1);
+ //DB_DataObject::debugLevel(1);
if (empty($obj->id)) {
return array();
}
+
$c = clone($this);
$c->ontable = $obj->tableName();
$c->onid = $obj->id;
+ $c->autoJoin();
if (!empty($mime_like)) {
- $c->whereAdd("mimetype LIKE '". $c->escape($mime_like) ."'");
+ $c->whereAdd("Images.mimetype LIKE '". $c->escape($mime_like) ."'");
}
return $c->fetchAll();
}
+
+
+ /**
+ * set or get the dataobject this image is associated with
+ * @param DB_DataObject $obj An object to associate this image with
+ * (does not store it - you need to call update() to do that)
+ * @return DB_DataObject the dataobject this image is attached to.
+ */
+ function object($obj=false)
+ {
+ if ($obj === false) {
+ if (empty($this->ontable) || empty($this->onid)) {
+ return false;
+ }
+ $ret = DB_DataObject::factory($this->ontable);
+ $ret->get($this->onid);
+ return $ret;
+ }
+
+
+ $this->ontable = $obj->tableName();
+ $this->onid = $obj->id; /// assumes our nice standard of using ids..
+ return $obj;
+ }
function toRooArray($req = array()) {
- if (empty($req['query']['imagesize'])) {
- return $this->toArray();
- }
+ // echo '<PRE>';print_r($req);exit;
$ret= $this->toArray();
- $ret['url'] = $this->URL($req['query']['imagesize']);
+
+ static $ff = false;
+ if (!$ff) {
+ $ff = HTML_FlexyFramework::get();
+ }
+ $ret['public_baseURL'] = isset($ff->Pman_Images['public_baseURL']) ?
+ $ff->Pman_Images['public_baseURL'] : $ff->baseURL;
+
+ if (!empty($req['query']['imagesize'])) {
+ $baseURL = isset($req['query']['imageBaseURL']) ? $req['query']['imageBaseURL'] : false;
+
+ $ret['url'] = $this->URL(-1, '/Images/Download',$baseURL);
+
+ $ret['url_view'] = $this->URL(-1, '/Images',$baseURL);
+
+ if (!empty($req['query']['imagesize'])) {
+ $ret['url_thumb'] = $this->URL($req['query']['imagesize'], '/Images/Thumb',$baseURL);
+ }
+ }
+
+
return $ret;
}
-
- function URL($size, $provider = '/Images/Thumb')
+
+ /**
+ * URL - create a url for the image.
+ * size - use -1 to show full size.
+ * provier = baseURL + /Images/Thumb ... use '/Images/' for full
+ *
+ *
+ */
+ function URL($size , $provider = '/Images/Thumb', $baseURL=false)
{
if (!$this->id) {
return 'about:blank';
}
+
+ $ff = HTML_FlexyFramework::get();
+ $baseURL = $baseURL ? $baseURL : $ff->baseURL ;
+ if (preg_match('#^http[s]*://#', $provider)) {
+ $baseURL = '';
+ }
+
if ($size < 0) {
- return $ff->baseURL . $provider . "/{$this->id}/{$this->filename}";
+ return $baseURL . $provider . "/{$this->id}/{$this->filename}";
}
- $size = max(100, (int) $size);
- $size = min(1024, (int) $size);
+ //-- max?
+ //$size = max(100, (int) $size);
+ //$size = min(1024, (int) $size);
- $ff = HTML_FlexyFramework::get();
- return $ff->baseURL . $provider . "/$size/{$this->id}/{$this->filename}";
+
+ return $baseURL . $provider . "/$size/{$this->id}/{$this->filename}";
}
/**
* size could be 123x345
$sz = explode('x', $size);
$sx = $sz[0];
- var_dump($sz);
- if (!$this->id) {
+ //var_dump($sz);
+ if (!$this->id || empty($this->width)) {
$this->height = $sx;
$this->width = empty($sz[1]) ? $sx : $sz[1];
+ $sy = $this->width ;
}
if (empty($sz[1])) {
- $ratio = $this->height/ ($this->width *1.0);
- $sy = $ration * $sx;
+ $ratio = empty($this->width) ? 1 : $this->height/ ($this->width *1.0);
+ $sy = $ratio * $sx;
+ } else {
+ $sy = $sz[1];
}
// create it?
}
-
-
+
+ /**
+ * to Fileconvert object..
+ *
+ *
+ *
+ */
+ function toFileConvert()
+ {
+ require_once 'File/Convert.php';
+ $fc = new File_Convert($this->getStoreName(), $this->mimetype);
+ return $fc;
+
+ }
+ /**
+ *
+ *
+ *
+ */
function setFromRoo($ar, $roo)
{
// not sure why we do this..
+
+
+
+
// if imgtype starts with '-' ? then we set the 'old' (probably to delete later)
if (!empty($ar['imgtype']) && !empty($ar['ontable']) && !empty($ar['onid']) && ($ar['imgtype'][0] == '-')) {
$this->setFrom($ar);
}
-
+ if (!empty($ar['_copy_from'])) {
+
+ if (!$this->checkPerm( 'A' , $roo->authUser)) {
+ $roo->jerr("IMAGE UPLOAD PERMISSION DENIED");
+ }
+
+ $copy = DB_DataObject::factory('Images');
+ $copy->get($ar['_copy_from']);
+ $this->setFrom($copy->toArray());
+ $this->setFrom($ar);
+ $this->createFrom($copy->getStoreName());
+
+ $roo->addEvent("ADD", $this, $this->toEventString());
+
+ $r = DB_DataObject::factory($this->tableName());
+ $r->id = $this->id;
+ $roo->loadMap($r);
+ $r->limit(1);
+ $r->find(true);
+ $roo->jok($r->toArray());
+
+
+ }
// FIXME - we should be checking perms here...
- //if (method_exists($x, 'checkPerm') && !$x->checkPerm('E', $this->authUser)) {
- // $this->jerr("PERMISSION DENIED");
- // }
+
// this should be doign update
$this->setFrom($ar);
+
+ if (!$this->checkPerm($this->id ? 'A' : 'E', $roo->authUser)) {
+ $roo->jerr("IMAGE UPLOAD PERMISSION DENIED");
+ }
if (!isset($_FILES['imageUpload'])) {
return; // standard update...
}
if ( !$this->onUpload($this)) {
- $this->jerr("File upload failed");
+ $roo->jerr("File upload failed : ". $this->err);
}
+
$roo->addEvent("ADD", $this, $this->toEventString());
$r = DB_DataObject::factory($this->tableName());
$roo->jok($r->toArray());
}
+
function toEventString()
{