try using alternative search for filename
[Pman.Core] / DataObjects / Images.php
1 <?php
2 /**
3  * Table Definition for Images
4  */
5 class_exists('DB_DataObject') ? '' : require_once 'DB/DataObject.php';
6
7 class Pman_Core_DataObjects_Images extends DB_DataObject 
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11
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
29     
30     /* the code above is auto generated do not remove the tag below */
31     ###END_AUTOCODE
32     function applyFilters($q, $au, $roo)
33     {
34         $tn = $this->tableName();
35         
36         if(!empty($q['search']['filename'])){
37             $this->whereAdd("
38                 $tn.filename LIKE '%{$this->escape($q['search']['filename'])}%' OR $tn.title LIKE '%{$this->escape($q['search']['filename'])}%'
39             ");
40         }
41
42         if(!empty($q['_to_base64']) && !empty($q['image_id'])) {
43             $i = DB_DataObject::factory("Images");
44             $i->get($q['image_id']);
45             $roo->jok($i->toBase64());
46         }
47         
48
49     }
50     
51     function checkPerm($lvl, $au)
52     {
53         // default permissons are to
54         // allow create / edit / if the user has
55         
56         if (!$au) {
57             return false;
58         }
59         
60         $o = $this->object();
61         //print_r($o);
62         if ($o &&  method_exists($o, 'checkPerm')) {
63             // edit permissions on related object needed...
64             return $o->checkPerm( $lvl == 'S' ? 'S' : 'E' , $au);
65             
66         }
67         
68         return true; //// ??? not really that safe...
69         
70     }
71     
72     function beforeInsert($q, $roo) 
73     {
74         if (isset($q['_remote_upload'])) {
75             require_once 'System.php';
76             
77             $tmpdir  = System::mktemp("-d remote_upload");
78             
79             $path = $tmpdir . '/' . basename($q['_remote_upload']);
80             
81             if(!file_exists($path)){
82                file_put_contents($path, file_get_contents($q['_remote_upload'])); 
83             }
84             
85             $imageInfo = getimagesize($path);
86             
87             require_once 'File/MimeType.php';
88             $y = new File_MimeType();
89             $ext = $y->toExt(trim((string) $imageInfo['mime'] ));
90             
91             if (!preg_match("/\." . $ext."$/", $path, $matches)) {
92                 rename($path,$path.".".$ext);
93                 $path.= ".".$ext;
94             }
95             
96             if (!$this->createFrom($path)) {
97                 $roo->jerr("erro making image" . $q['_remote_upload']);
98             }
99             
100             if(!empty($q['_return_after_create'])){
101                 return;
102             }
103             
104             $roo->addEvent("ADD", $this, $this->toEventString());
105         
106             $r = DB_DataObject::factory($this->tableName());
107             $r->id = $this->id;
108             $roo->loadMap($r);
109             $r->limit(1);
110             $r->find(true);
111             $roo->jok($r->URL(-1,'/Images') . '#attachment-'.  $r->id);
112         }
113         
114     }
115     
116      
117     /**
118      * create an email from file.
119      * these must have been set first.
120      * ontable / onid.
121      * 
122      */
123     function createFrom($file, $filename=false)
124     {
125         // copy the file into the storage area..
126         if (!file_exists($file) || !filesize($file)) {
127             $this->err = "File $file did not exist or is 0 size";
128             return false;
129         }
130         
131         $filename = empty($filename) ? $file : $filename;
132         
133         if (empty($this->mimetype)) {
134             require_once 'File/MimeType.php';
135             $y = new File_MimeType();
136             $this->mimetype = $y->fromFilename($filename);
137         }
138         
139         $this->mimetype = strtolower($this->mimetype);
140         
141         $mta = explode('/', $this->mimetype);
142         if (array_shift($mta) == 'image') { 
143         
144             $imgs = @getimagesize($file);
145             
146             if (empty($imgs) || empty($imgs[0]) || empty($imgs[1])) {
147                 // it's a file!!!!
148             } else {
149                 list($this->width , $this->height)  = $imgs;
150             }
151         }
152         
153         if($this->mimetype == 'application/pdf'){
154             $this->no_of_pages = $this->getPdfPages($file);
155         }
156         
157         $this->filesize = filesize($file);
158         $this->created = date('Y-m-d H:i:s');
159          
160         
161         if (empty($this->filename)) {
162             $this->filename = basename($filename);
163         }
164         
165         //DB_DataObject::debugLevel(1);
166         if (!$this->id) {
167             $this->insert();
168         } else {
169             $this->update();
170         }
171         
172         
173         
174         $f = $this->getStoreName();
175         $dest = dirname($f);
176         if (!file_exists($dest)) {
177             // currently this is 0775 due to problems using shared hosing (FTP)
178             // it makes all the files unaccessable..
179             // you can normally solve this by giving the storedirectory better perms
180             // if needed on a dedicated server..
181             $oldumask = umask(0);
182             mkdir($dest, 0775, true);
183             umask($oldumask);  
184         }
185         
186         copy($file,$f);
187         
188         // fill in details..
189         
190         /* thumbnails */
191         
192      
193        // $this->createThumbnail(0,50);
194         return true;
195         
196     }
197
198     /**
199      * Calculate target file name
200      *
201      * @return - target file name
202      */
203     function getStoreName($alt = false) 
204     {
205         $opts = HTML_FlexyFramework::get()->Pman;
206         $fn = preg_replace('/[^a-z0-9_\.]+/i', '_', $this->filename);
207         if ($alt) {
208             $fn = preg_replace('/[^a-z0-9\.]+/i', '_', $this->filename);
209         }
210         return implode( '/', array(
211             $opts['storedir'], '_images_', date('Y/m', strtotime($this->created)), $this->id . '-'. $fn
212         ));
213           
214     }
215     
216     /**
217      * does the files exist?
218      */
219     function exists()
220     {
221         clearstatcache();
222         //var_dump($this->getStoreName());
223         return file_exists($this->getStoreName());
224     }
225     
226     
227     /**
228      * deletes all the image instances of it...
229      * 
230      * 
231      */
232     function beforeDelete($dependants_array, $roo)
233     {
234         
235         if (!empty($dependants_array)) {
236             return;
237         }
238         
239         $opts = HTML_FlexyFramework::get()->Pman;
240         $deldir = $opts['storedir']. '/_deleted_images_';
241         clearstatcache();
242         if (!file_exists( $deldir )) {
243             @mkdir($deldir, 0755); // not sure why we are erroring here.. after checking - maybe permissions?
244         }
245             
246         $fn = $this->getStoreName();
247         $b = basename($fn);
248         if (file_exists($fn)) {
249             
250             if (file_exists($deldir . '/'. $b)) {
251                 unlink($fn);
252             } else {
253                 rename($fn, $deldir .'/'. $b);
254             }
255             
256             
257         }
258         // delete thumbs..
259         
260         $d = dirname($fn);
261         if (file_exists($d)) {
262                 
263             $dh = opendir($d);
264             while (false !== ($fn = readdir($dh))) {
265                 if (substr($fn, 0, strlen($b)) == $b) {
266                     
267                     if (file_exists($deldir . '/'. $fn)) {
268                         unlink($d. '/'. $fn);
269                         continue;
270                     }
271                     rename($d. '/'. $fn, $deldir .'/'. $fn);
272                     
273                 }
274             }
275         }
276         
277     }
278     /**
279      * check mimetype against type
280      * - eg. img.is(#image#)
281      *
282      */
283     function is($type)
284     {
285         if (empty($this->mimetype)) {
286             return false;
287         }
288         return 0 === strcasecmp($type, array_shift(explode('/',$this->mimetype)));
289     }
290   
291     /**
292      * onUpload (singlely attached image to a table)
293      */
294     
295     function onUploadWithTbl($tbl,  $fld)
296     {
297         if ( $tbl->__table == 'Images') {
298             return; // not upload to self...
299         }
300         if (empty($_FILES['imageUpload']['tmp_name']) || 
301             empty($_FILES['imageUpload']['name']) || 
302             empty($_FILES['imageUpload']['type'])
303         ) {
304             return false;
305         }
306         if ($tbl->$fld) {
307             HTML_FlexyFramework::get()->page->jerr("updating images is disabled");
308             exit;
309             $image = DB_DataObject::factory('Images');
310             $image->get($tbl->$fld);
311             $image->beforeDelete();
312             $image->delete();
313         }
314         
315         $image = DB_DataObject::factory('Images');
316         $image->onid = $tbl->id;
317         $image->ontable = $tbl->__table;
318         $image->filename = $_FILES['imageUpload']['name']; 
319         $image->mimetype = $_FILES['imageUpload']['type'];
320        
321         if (!$image->createFrom($_FILES['imageUpload']['tmp_name'])) {
322             return false;
323         }
324         $old = clone($tbl);
325         $tbl->$fld = $image->id;
326         $tbl->update($old);
327          
328     }
329     
330     // direct via roo...
331     /// ctrl not used??
332     function onUpload($roo, $table = false, $file = false)
333     {
334         
335         if ($table !== false) {
336             $this->ontable = $table->tableName();
337             $this->onid = $table->pid();
338         }
339         
340         if ($file === false) {
341             $file = isset($_FILES['imageUpload']) ? $_FILES['imageUpload'] : array();
342         }
343         
344         //print_r($_FILES); echo $_FILES['imageUpload']['type'];exit;
345         if (empty($file['tmp_name']) || 
346             empty($file['name']) || 
347             empty($file['type'])
348         ) {
349             
350             $emap = array( 
351                 0=>"There is no error, the file uploaded with success", 
352                 1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini", 
353                 2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form" ,
354                 3=>"The uploaded file was only partially uploaded",
355                 4=>"No file was uploaded",
356                 6=>"Missing a temporary folder" 
357             ); 
358             $estr = (empty($file['error']) ? '?': $emap[$file['error']]);
359             $this->err = "Missing file details : Error=". $estr;
360             return false;
361         }
362         
363         if ($this->id) {
364             HTML_FlexyFramework::get()->page->jerr("updating images is disabled");
365             exit;
366             $this->beforeDelete();
367         }
368         if ( empty($this->ontable)) {
369             $this->err = "Missing  ontable";
370             return false;
371         }
372         
373         if (!empty($this->imgtype) && $this->imgtype[0] == '-' && !empty($this->onid)) {
374             // then its an upload 
375             $img  = DB_DataObject::factory('Images');
376             $img->onid = $this->onid;
377             $img->ontable = $this->ontable;
378             $img->imgtype = $this->imgtype;
379             
380             $img->find();
381             while ($img->fetch()) {
382                 HTML_FlexyFramework::get()->page->jerr("updating images is disabled");
383                 exit;
384                 $img->beforeDelete();
385                 $img->delete();
386             }
387             
388         }
389         
390         
391         
392         require_once 'File/MimeType.php';
393         $y = new File_MimeType();
394         $this->mimetype = $file['type'];
395         if (in_array($this->mimetype, array(
396                         'text/application',
397                         'application/octet-stream',
398                         'image/x-png',  // WTF does this?
399                         'image/pjpeg',  // WTF does this?
400                         'application/x-apple-msg-attachment', /// apple doing it's magic...
401                         'application/vnd.ms-excel',   /// sometimes windows reports csv as excel???
402                         'application/csv-tab-delimited-table', // windows again!!?
403                 ))) { // weird tyeps..
404             $inf = pathinfo($file['name']);
405             $this->mimetype  = $y->fromExt($inf['extension']);
406         }
407         
408         
409         $ext = $y->toExt(trim((string) $this->mimetype ));
410         
411         $this->filename = empty($this->filename) ? 
412             $file['name'] : ($this->filename .'.'. $ext); 
413         
414         
415         
416         if (!$this->createFrom($file['tmp_name'])) {
417             $this->err  =  isset($this->err)  ?  $this->err  : "createFrom Image failed";
418             return false;
419         }
420         return true;
421          
422     }
423      
424     
425     
426     /**
427      * return a list of images for an object, optionally with a mime regex.
428      * eg. '%/pdf' or 'image/%'
429      *
430      * usage:
431      *
432      * $i = DB_DataObject::factory('Images');
433      * $i->imgtype = 'LOGO';
434      * $ar = $i->gather($somedataobject, 'image/%');
435      * 
436      * @param {DB_DataObject} dataobject  = the object to gather data on.
437      * @param {String} mimelike  LIKE query to use for search
438      
439      */
440     function gather($obj, $mime_like='', $opts=array())
441     {
442         //DB_DataObject::debugLevel(1);
443         if (empty($obj->id)) {
444             return array();
445         }
446         
447         $c = clone($this);
448         $c->whereAddIn($this->tableName() . '.ontable', array( $obj->tableName(), $obj->__table) , 'string');
449         $c->onid = $obj->id;
450         $c->autoJoin();
451         if (!empty($mime_like)) {
452             $c->whereAdd("Images.mimetype LIKE '". $c->escape($mime_like) ."'");
453         }
454         $c->orderBy('created DESC');
455
456         return $c->fetchAll();
457     }
458      
459     
460     /**
461     * set or get the dataobject this image is associated with
462     * @param DB_DataObject $obj An object to associate this image with
463     *        (does not store it - you need to call update() to do that)
464     * @return DB_DataObject the dataobject this image is attached to.
465     */
466     function object($obj=false)
467     {
468         if ($obj === false) {
469             if (empty($this->ontable) || empty($this->onid)) {
470                 return false;
471             }
472             $ret = DB_DataObject::factory($this->ontable);
473             $ret->get($this->onid);
474             return $ret;
475         }
476         
477         
478         $this->ontable = $obj->tableName();
479         $this->onid = $obj->id; /// assumes our nice standard of using ids..
480         return $obj;
481     }
482     
483      
484     function toRooArray($req)
485     {
486         
487         $ret= $this->toArray();
488       
489          
490         $ff = HTML_FlexyFramework::get();
491         
492         
493         $ret['public_baseURL'] = isset($ff->Pman_Images['public_baseURL']) ?
494                     $ff->Pman_Images['public_baseURL'] : $ff->baseURL;
495         
496         if (!empty($req['query']['imagesize'])) {
497             // query/imageBaseURL ... depricated...? -- set it in config?
498             
499             $baseURL = isset($req['query']['imageBaseURL']) ? $req['query']['imageBaseURL'] : $ret['public_baseURL'];
500             
501             $ret['url'] = $this->URL(-1, '/Images/Download',$baseURL);
502             
503             $ret['url_view'] = $this->URL(-1, '/Images',$baseURL);    
504             
505             if (!empty($req['query']['imagesize'])) {
506                 $ret['url_thumb'] = $this->URL($req['query']['imagesize'], '/Images/Thumb',$baseURL);
507             }
508             
509             
510         }
511         $ret['shorten_name']   = $this->shorten_name();
512         
513         return $ret;
514     }
515     
516     /**
517      * URL - create  a url for the image.
518      * size - use -1 to show full size.
519      * provier = baseURL + /Images/Thumb ... use '/Images/' for full
520      * 
521      * 
522      */
523     function URL($size , $provider = '/Images/Thumb', $baseURL=false)
524     {
525         if (!$this->id) {
526             return 'about:blank';
527         }
528         if (!$this->exists()) {
529             return 'about:missing';
530         }
531         
532         $shorten_name = $this->shorten_name();
533         
534         $ff = HTML_FlexyFramework::get();
535         $baseURL = $baseURL ? $baseURL : $ff->baseURL ;
536         if (preg_match('#^http[s]*://#', $provider)) {
537             $baseURL = '';
538         }
539        
540         if ($size < 0) {
541             $provider = preg_replace('#/Thumb$#', '', $provider);
542             
543             return $baseURL . $provider . "/{$this->id}/{$shorten_name}"; // -- this breaks the rss feed #image-{$this->id}";
544         }
545         //-- max?
546         //$size = max(100, (int) $size);
547         //$size = min(1024, (int) $size);
548         // the size should 200x150 to convert
549         $sizear = preg_split('/(x|c)/', $size);
550         if(!isset($sizear[1])){
551             $sizear[1] =   0; // 0x with '0' is a box? why
552         }
553         
554         $size = implode(strpos($size,'c') > -1 ? 'c' : 'x', $sizear);
555 //        print_r($size);
556         $fc = $this->toFileConvert();
557 //        print_r($size);
558 //        exit;
559         $mt = $this->mimetype;
560         if (!preg_match('#^image/#i',$mt)) {
561             $mt = 'image/jpeg';
562         }
563         
564         $fc->convert($mt, $size);
565         
566         return $baseURL . $provider . "/$size/{$this->id}/{$shorten_name}"; // -- this breaks the rss feed #image-{$this->id}";
567     }
568     
569     function getFromHashURL($url)
570     {
571         $id = false;
572         if (preg_match('/#image-([0-9]+)$/', $url, $matches)) {
573             $id = $matches[1];
574         } else if (preg_match('#Images/Thumb/[^/]+/([0-9]+)/#', $url, $matches)) {
575             $id = $matches[1];
576         } else if (preg_match('#Images/([0-9]+)/#', $url, $matches)) {
577             $id = $matches[1];
578         }
579         
580         if ($id === false ||  $id < 1) {
581             return false;
582         }
583         
584         $img = DB_DAtaObject::Factory('images');
585         if ($img->get($id)) {
586             return $img;
587         }
588         return false;
589     }
590     
591     
592     function shorten_name()
593     {
594         if(empty($this->filename)) {
595             return;
596         }
597         
598         $filename = explode('.', $this->filename);
599         $ext = array_pop($filename);
600         $name = preg_replace("/[^A-Z0-9.]+/i", '-', implode('-', $filename)) ;
601         
602         if(strlen($name) > 32) {
603             $name = substr($name, 0, 32);
604         }
605         
606         $shorten_name = "{$name}.{$ext}";
607         
608         return $shorten_name;
609     }
610     /**
611      * size could be 123x345
612      * 
613      * 
614      */
615     function toHTML($size, $provider = '/Images/Thumb', $extra = '') 
616     {
617         
618         
619         
620         $sz = explode('x', $size);
621         $sx = $sz[0];
622         //var_dump($sz);
623         if (!$this->id || empty($this->width)) {
624             $this->height = $sx;
625             $this->width = empty($sz[1]) ? $sx : $sz[1];
626             $sy = $this->width ;
627         }
628         if (empty($sz[1])) {
629             $ratio =  empty($this->width) ? 1 : $this->height/ ($this->width *1.0);
630             $sy = intval($ratio * $sx);
631         } else {
632             $sy = $sz[1];
633         }
634         // create it?
635        
636         if (strlen($this->title)) {
637             $extra = ' title="'. htmlspecialchars($this->title) . '"';
638         }
639         
640         return '<img src="' . $this->URL($size, $provider) . '"' .
641                 $extra .
642                 ' width="'. $sx . '"' .
643                 ' height="'. $sy . '">';
644         
645         
646     }
647     
648     /**
649      * 
650      * #2142 [new] CMS - image link urls
651      * 
652      * 
653      * 
654      */
655     function toLinkHTML($size, $provider = '/Images/Thumb')
656     {
657         if(empty($this->linkurl)){
658             return $this->toHTML($size, $provider = '/Images/Thumb');
659         }
660         
661         return '<a href="'.$this->linkurl.'" target="_blank">'.$this->toHTML($size, $provider = '/Images/Thumb').'</a>';
662         
663     }
664     
665     
666     /**
667      * to Fileconvert object..
668      *
669      *
670      *
671      */
672     function toFileConvert()
673     {
674         $fn = $this->getStoreName();
675         if (!file_exists($fn)) {
676             $fn = $this->getStoreName(true);
677         }
678         
679         require_once 'File/Convert.php';
680         $fc = new File_Convert($this->getStoreName(), $this->mimetype);
681         return $fc;
682         
683     }
684     
685     function fileExt()
686     {
687         require_once 'File/MimeType.php';
688         
689         $y = new File_MimeType();
690         return  $y->toExt($this->mimetype);
691         
692         
693     }
694     
695     /**
696      *
697      *
698      *
699      */
700     
701     
702     function setFromRoo($ar, $roo)
703     {
704         // not sure why we do this.. 
705         
706         // if imgtype starts with '-' ? then we set the 'old' (probably to delete later)
707         if (!empty($ar['imgtype']) && !empty($ar['ontable']) && !empty($ar['onid']) && ($ar['imgtype'][0] == '-')) {
708             $this->setFrom($ar);
709             $this->limit(1);
710             if ($this->find(true)) {
711                 $roo->old = clone($this);
712             }
713         }   
714             
715         
716         if (!empty($ar['_copy_from'])) {
717             
718             if (!$this->checkPerm( 'A' , $roo->authUser))  {
719                 $roo->jerr("IMAGE UPLOAD PERMISSION DENIED");
720             }
721             
722             $copy = DB_DataObject::factory('Images');
723             $copy->get($ar['_copy_from']);
724             $this->setFrom($copy->toArray());
725             $this->setFrom($ar);
726             $this->createFrom($copy->getStoreName());
727             
728             $roo->addEvent("ADD", $this, $this->toEventString());
729             
730             $r = DB_DataObject::factory($this->tableName());
731             
732             $r->id = $this->id;
733             $roo->loadMap($r);
734             $r->limit(1);
735             $r->find(true);
736             $roo->jok($r->toRooArray($ar));
737             
738             
739         }
740         
741          
742         
743         // FIXME - we should be checking perms here...
744        
745         // this should be doign update
746         $this->setFrom($ar);
747          
748         if (!$this->checkPerm($this->id ? 'A' : 'E', $roo->authUser))  {
749             $roo->jerr("IMAGE UPLOAD PERMISSION DENIED");
750         }
751          
752         
753         if (!isset($_FILES['imageUpload'])) {
754             return; // standard update...
755         }
756         
757         
758 //        print_r(!$this->onUpload($this));
759         
760         if ( !$this->onUpload($this)) { 
761             $roo->jerr("File upload failed : error = ". (!empty($this->err) ? $this->err : ''));
762         }
763         
764         $this->addEvent($ar, $roo);
765         
766         $r = DB_DataObject::factory($this->tableName());
767         $r->id = $this->id;
768         $roo->loadMap($r);
769         $r->limit(1);
770         $r->find(true);
771         $roo->jok($r->toRooArray($ar));
772          
773     }
774     
775     function addEvent($ar, $roo)
776     {
777         $roo->addEvent("ADD", $this, $this->toEventString());
778     }
779     
780     function toEventString()
781     {
782         
783         //$p = DB_DataObject::factory($this->ontable);
784         //if (!is_$p) {
785         //    return "ERROR unknown table? {$this->ontable}";
786        // }
787         //$p->get($p->onid);
788         
789         return $this->filename .' - on ' . $this->ontable . ':' . $this->onid;
790         //$p->toEventString();
791     }
792     
793     function onUploadFromData($data, $roo)
794     {
795         if (empty($data)) {
796             $this->err = "Missing file details";
797             return false;
798         }
799         
800         if ($this->id) {
801             HTML_FlexyFramework::get()->page->jerr("updating images is disabled");
802             exit;
803             $this->beforeDelete();
804         }
805         
806         if (empty($this->ontable)) {
807             $this->err = "Missing  ontable";
808             return false;
809         }
810         
811         if (!empty($this->imgtype) && $this->imgtype[0] == '-' && !empty($this->onid)) {
812             // then its an upload 
813             $img  = DB_DataObject::factory('Images');
814             $img->onid = $this->onid;
815             $img->ontable = $this->ontable;
816             $img->imgtype = $this->imgtype;
817             
818             $img->find();
819             while ($img->fetch()) {
820                 HTML_FlexyFramework::get()->page->jerr("updating images is disabled");
821                 exit;
822                 $img->beforeDelete();
823                 $img->delete();
824             }
825             
826         }
827         
828         require_once 'File/MimeType.php';
829         $y = new File_MimeType();
830         
831         if (in_array($this->mimetype, array(
832                         'text/application',
833                         'application/octet-stream',
834                         'image/x-png',  // WTF does this?
835                         'image/pjpeg',  // WTF does this?
836                         'application/x-apple-msg-attachment', /// apple doing it's magic...
837                         'application/vnd.ms-excel',   /// sometimes windows reports csv as excel???
838                         'application/csv-tab-delimited-table', // windows again!!?
839                 ))) { // weird tyeps..
840             $inf = pathinfo($this->filename);
841             $this->mimetype  = $y->fromExt($inf['extension']);
842         }
843         
844         $ext = $y->toExt(trim((string) $this->mimetype ));
845         
846         $explode_filename = explode('.', $this->filename);
847         if(array_pop($explode_filename) != $ext){
848             $this->filename = $this->filename .'.'. $ext; 
849         }
850         
851         if (!$this->createFromData($data)) {
852             return false;
853         }
854         
855         return true;
856          
857     }
858     
859     function createFromData($data)
860     {   
861         
862         if (0 === strpos($data, "data:")) {
863             // data:image/png;base64, 
864             $data = substr($data,5);
865             $bits = explode(";", $data);
866             $this->mimetype = $bits[0];
867         }
868         static $imgid = 1;
869         if (empty($this->filename)) {
870             require_once 'File/MimeType.php';
871             $y = new File_MimeType();
872             $this->filename = 'image-'.$imgid++.'.'.$y->toExt($this->mimetype);
873         }
874         
875         
876         $this->mimetype = strtolower($this->mimetype);
877         if ($this->mimetype == 'image/jpg') {
878             $this->mimetype = 'image/jpeg';
879         }
880         
881         
882         $explode_mimetype = explode('/', $this->mimetype);
883         
884         if (array_shift($explode_mimetype) == 'image') { 
885         
886             $imgs = @getimagesize('data://'. $data);
887             
888             if (!empty($imgs) && !empty($imgs[0]) && !empty($imgs[1])) {
889                 list($this->width , $this->height)  = $imgs;
890             }
891         }
892         
893         $this->created = date('Y-m-d H:i:s');
894         
895         if (!$this->id) {
896             $this->insert();
897         } else {
898             $this->update();
899         }
900         
901         $f = $this->getStoreName();
902         $dest = dirname($f);
903         if (!file_exists($dest)) {
904             $oldumask = umask(0);
905             mkdir($dest, 0775, true);
906             umask($oldumask);  
907         }
908         
909         file_put_contents($f, file_get_contents("data://" . $data));
910         //var_dump($f);exit;
911         $o = clone($this);
912         
913         $this->filesize = filesize($f);
914         
915         if($this->mimetype == 'application/pdf'){
916             $this->no_of_pages = $this->getPdfPages($f);
917         }
918         
919         $this->update($o);
920         
921         return true;
922         
923     }
924     
925     function toBase64($rotate = false, $scaleWidth = 0, $scaleHeight = 0)
926     {
927         if(!preg_match('/^image\//', $this->mimetype)){
928             return false;
929         }
930         
931         $file = $this->getStoreName();
932
933         if(!file_exists($file)){
934             return false;
935         }
936         
937         $data = file_get_contents($file);
938         
939         if(!empty($scaleWidth) || !empty($scaleHeight)){
940             $data = $this->scale(false, $scaleWidth, $scaleHeight);
941         }
942         
943         if($rotate){
944             $data = $this->rotate($data);
945         }
946         
947         $base64 = 'data:' . $this->mimetype . ';base64,' . base64_encode($data);
948         
949         return $base64;
950     }
951     
952     function getPdfPages($file)
953     {
954         require_once 'System.php';
955         
956         $page = 0;
957
958         $pdfinfo = System::which('pdfinfo');
959
960         if (!file_exists($file) || empty($pdfinfo)) {
961             return $page;
962         }
963         
964         $cmd = "{$pdfinfo} {$file}";
965
966         $ret = `$cmd`;
967
968         $info = explode("\n", $ret);
969
970         foreach ($info as $i){
971
972             if(!preg_match('/^Pages:[\s]*([0-9]+)/', $i, $matches)){
973                 continue;
974             }
975             
976             $page = (empty($matches[1])) ? 0 : $matches[1];
977         }
978         
979         return $page;
980     }
981     
982     function rotate($imageBlob = false)
983     {
984         if(empty($imageBlob)){
985             $imagick = new Imagick($this->getStoreName());
986         } else {
987             $imagick = new Imagick();
988             $imagick->readImageBlob($imageBlob);
989         }
990         
991         $orientation = $imagick->getImageOrientation(); 
992         
993         switch($orientation) { 
994             case Imagick::ORIENTATION_BOTTOMRIGHT: 
995                 $imagick->rotateimage(new ImagickPixel('#00000000'), 180); // rotate 180 degrees 
996             break; 
997
998             case Imagick::ORIENTATION_RIGHTTOP: 
999                 $imagick->rotateimage(new ImagickPixel('#00000000'), 90); // rotate 90 degrees CW 
1000             break; 
1001
1002             case Imagick::ORIENTATION_LEFTBOTTOM: 
1003                 $imagick->rotateimage(new ImagickPixel('#00000000'), -90); // rotate 90 degrees CCW 
1004             break; 
1005         }
1006         
1007         return $imagick->getImageBlob();
1008     }
1009     
1010     function scale($imageBlob = false, $width = 0, $height = 0)
1011     {
1012         if(empty($imageBlob)){
1013             $imagick = new Imagick($this->getStoreName());
1014         } else {
1015             $imagick = new Imagick();
1016             $imagick->readImageBlob($imageBlob);
1017         }
1018         
1019         $imagick->resizeimage($width, $height, Imagick::FILTER_LANCZOS, true, true);
1020         
1021         return $imagick->getImageBlob();
1022         
1023     }
1024     
1025  }