Exemplo n.º 1
0
 function post()
 {
     if (\Idno\Core\Idno::site()->session()->isLoggedOn()) {
         if (!empty($_FILES['file']['tmp_name'])) {
             if (!\Idno\Core\Idno::site()->triggerEvent("file/upload", [], true)) {
                 exit;
             }
             if (\Idno\Entities\File::isImage($_FILES['file']['tmp_name'])) {
                 $return = false;
                 $file = false;
                 if ($file = \Idno\Entities\File::createThumbnailFromFile($_FILES['file']['tmp_name'], $_FILES['file']['name'], 1024)) {
                     $return = true;
                     $returnfile = new \stdClass();
                     $returnfile->file = ['_id' => $file];
                     $file = $returnfile;
                 } else {
                     if ($file = \Idno\Entities\File::createFromFile($_FILES['file']['tmp_name'], $_FILES['file']['name'], $_FILES['file']['type'], true)) {
                         $return = true;
                     }
                 }
                 if ($return) {
                     $t = \Idno\Core\Idno::site()->template();
                     $t->file = $file;
                     echo $t->draw('file/picker/donejs');
                     exit;
                 }
             } else {
                 Idno::site()->session()->addErrorMessage("You can only upload images.");
             }
         }
         $this->forward($_SERVER['HTTP_REDIRECT']);
     }
 }
Exemplo n.º 2
0
 /**
  * Saves changes to this object based on user input
  * @return bool
  */
 function saveDataFromInput()
 {
     if (empty($this->_id)) {
         $new = true;
     } else {
         $new = false;
     }
     $this->title = \Idno\Core\site()->currentPage()->getInput('title');
     $this->body = \Idno\Core\site()->currentPage()->getInput('body');
     $this->tags = \Idno\Core\site()->currentPage()->getInput('tags');
     $this->setAccess('PUBLIC');
     if ($time = \Idno\Core\site()->currentPage()->getInput('created')) {
         if ($time = strtotime($time)) {
             $this->created = $time;
         }
     }
     // Get photo
     if ($new) {
         if (!empty($_FILES['photo']['tmp_name'])) {
             if (\Idno\Entities\File::isImage($_FILES['photo']['tmp_name'])) {
                 if ($photo = \Idno\Entities\File::createFromFile($_FILES['photo']['tmp_name'], $_FILES['photo']['name'], $_FILES['photo']['type'], true)) {
                     $this->attachFile($photo);
                     // Now get some smaller thumbnails, with the option to override sizes
                     $sizes = \Idno\Core\site()->events()->dispatch('photo/thumbnail/getsizes', new \Idno\Core\Event(array('sizes' => ['large' => 800, 'medium' => 400, 'small' => 200])));
                     foreach ($sizes->data()['sizes'] as $label => $size) {
                         $filename = $_FILES['photo']['name'];
                         // Experiment: let's not save thumbnails for GIFs, in order to enable animated GIF posting.
                         if ($_FILES['photo']['type'] != 'image/gif') {
                             if ($thumbnail = \Idno\Entities\File::createThumbnailFromFile($_FILES['photo']['tmp_name'], "{$filename}_{$label}", $size)) {
                                 $varname = "thumbnail_{$label}";
                                 $this->{$varname} = \Idno\Core\site()->config()->url . 'file/' . $thumbnail;
                                 $varname = "thumbnail_{$label}_id";
                                 $this->{$varname} = substr($thumbnail, 0, strpos($thumbnail, '/'));
                             }
                         }
                     }
                 } else {
                     \Idno\Core\site()->session()->addMessage('Image wasn\'t attached.');
                 }
             } else {
                 \Idno\Core\site()->session()->addMessage('This doesn\'t seem to be an image ..');
             }
         } else {
             \Idno\Core\site()->session()->addMessage('We couldn\'t access your image. Please try again.');
             return false;
         }
     }
     if ($this->save()) {
         if ($new) {
             $this->addToFeed();
         }
         // Add it to the Activity Streams feed
         \Idno\Core\Webmention::pingMentions($this->getURL(), \Idno\Core\site()->template()->parseURLs($this->getTitle() . ' ' . $this->getDescription()));
         return true;
     } else {
         return false;
     }
 }
Exemplo n.º 3
0
 function postContent()
 {
     $this->adminGatekeeper();
     // Flag that a site export has been requested
     \Idno\Core\Idno::site()->config()->export_last_requested = time();
     \Idno\Core\Idno::site()->config()->export_in_progress = 1;
     \Idno\Core\Idno::site()->config()->save();
     $this->forward(\Idno\Core\Idno::site()->config()->getDisplayURL() . 'admin/export/', false);
     ignore_user_abort(true);
     // This is dangerous, but we need export to continue
     session_write_close();
     header('Connection: close');
     header('Content-length: ' . (string) ob_get_length());
     @ob_end_flush();
     // Return output to the browser
     @ob_end_clean();
     @flush();
     sleep(10);
     // Pause
     set_time_limit(0);
     // Eliminate time limit - this could take a while
     // Remove the previous export file
     if (!empty(\Idno\Core\Idno::site()->config()->export_file_id)) {
         if ($file = File::getByID(\Idno\Core\Idno::site()->config()->export_file_id)) {
             $file->remove();
             \Idno\Core\Idno::site()->config()->export_file_id = false;
             \Idno\Core\Idno::site()->config()->export_filename = false;
             \Idno\Core\Idno::site()->config()->save();
         }
     }
     if ($path = Migration::createCompressedArchive()) {
         $filename = \Idno\Core\Idno::site()->config()->host . '.zip';
         /*                    header('Content-disposition: attachment;filename=' . $filename);
                               if ($fp = fopen($path, 'r')) {
                                   while ($content = fread($fp, 4096)) {
                                       echo $content;
                                   }
                               }
                               fclose($fp);*/
         if ($file = File::createFromFile($path, $filename)) {
             @unlink($path);
             \Idno\Core\Idno::site()->config()->export_filename = $filename;
             \Idno\Core\Idno::site()->config()->export_file_id = $file;
             \Idno\Core\Idno::site()->config()->export_in_progress = 0;
             \Idno\Core\Idno::site()->config()->save();
             $mail = new Email();
             $mail->setHTMLBodyFromTemplate('admin/export');
             $mail->setTextBodyFromTemplate('admin/export');
             $mail->addTo(\Idno\Core\Idno::site()->session()->currentUser()->email);
             $mail->setSubject("Your data export is ready");
             $mail->send();
         }
         exit;
     }
 }
Exemplo n.º 4
0
 function post()
 {
     if (\Idno\Core\site()->session()->isLoggedOn()) {
         if (!empty($_FILES['file']['tmp_name'])) {
             if ($file = \Idno\Entities\File::createFromFile($_FILES['file']['tmp_name'], $_FILES['file']['name'], $_FILES['file']['type'], true)) {
                 echo json_encode(\Idno\Core\site()->config()->url . 'file/' . $file->file['_id']);
                 error_log(\Idno\Core\site()->config()->url . 'file/' . $file->file['_id']);
             }
         }
     }
 }
Exemplo n.º 5
0
 function post()
 {
     if (\Idno\Core\site()->session()->isLoggedOn()) {
         if (!empty($_FILES['file']['tmp_name'])) {
             if (!\Idno\Core\site()->triggerEvent("file/upload", [], true)) {
                 exit;
             }
             if ($file = \Idno\Entities\File::createFromFile($_FILES['file']['tmp_name'], $_FILES['file']['name'], $_FILES['file']['type'], true, true)) {
                 echo json_encode(\Idno\Core\site()->config()->url . 'file/' . $file->file['_id']);
             }
         }
     }
 }
Exemplo n.º 6
0
 function postContent()
 {
     $this->adminGatekeeper();
     // Admins only
     if ($profile_user = $this->getInput('profile_user')) {
         \Idno\Core\Idno::site()->config->config['cherwell']['profile_user'] = $profile_user;
     }
     if (!empty($_FILES['background']) && $this->getInput('action') != 'clear') {
         if (in_array($_FILES['background']['type'], array('image/png', 'image/jpg', 'image/jpeg', 'image/gif'))) {
             if (getimagesize($_FILES['background']['tmp_name'])) {
                 if ($background = \Idno\Entities\File::createFromFile($_FILES['background']['tmp_name'], $_FILES['background']['name'])) {
                     // Remove previous bg
                     if (!empty(\Idno\Core\Idno::site()->config()->cherwell['bg_id'])) {
                         if ($file = File::getByID(\Idno\Core\Idno::site()->config()->cherwell['bg_id'])) {
                             if (is_callable([$file, 'delete'])) {
                                 // TODO: really need some abstraction here.
                                 $file->delete();
                             } else {
                                 if (is_callable([$file, 'remove'])) {
                                     $file->remove();
                                 }
                             }
                         }
                     }
                     \Idno\Core\Idno::site()->config->config['cherwell']['bg_id'] = $background;
                     $background = \Idno\Core\Idno::site()->config()->getDisplayURL() . 'file/' . $background;
                     \Idno\Core\Idno::site()->config->config['cherwell']['bg'] = $background;
                 }
             }
         }
     } else {
         // Remove previous bg
         if (!empty(\Idno\Core\Idno::site()->config()->cherwell['bg_id'])) {
             if ($file = File::getByID(\Idno\Core\Idno::site()->config()->cherwell['bg_id'])) {
                 if (is_callable([$file, 'delete'])) {
                     $file->delete();
                 } else {
                     if (is_callable([$file, 'remove'])) {
                         $file->remove();
                     }
                 }
             }
         }
         \Idno\Core\Idno::site()->config->cherwell = [];
     }
     \Idno\Core\Idno::site()->config->save();
     $this->forward(\Idno\Core\Idno::site()->config()->getDisplayURL() . 'admin/cherwell/');
 }
Exemplo n.º 7
0
 function post()
 {
     if (\Idno\Core\site()->session()->isLoggedOn()) {
         if (!empty($_FILES['file']['tmp_name'])) {
             if (!\Idno\Core\site()->triggerEvent("file/upload", [], true)) {
                 exit;
             }
             if ($file = \Idno\Entities\File::createFromFile($_FILES['file']['tmp_name'], $_FILES['file']['name'], $_FILES['file']['type'], true)) {
                 $t = \Idno\Core\site()->template();
                 $t->file = $file;
                 echo $t->draw('file/picker/donejs');
                 exit;
             }
         }
     }
 }
Exemplo n.º 8
0
 function saveDataFromInput()
 {
     if (empty($this->_id)) {
         $new = true;
     } else {
         $new = false;
     }
     if ($new) {
         if (!\Idno\Core\Idno::site()->triggerEvent("file/upload", [], true)) {
             return false;
         }
     }
     $body = \Idno\Core\Idno::site()->currentPage()->getInput('body');
     if (!empty($_FILES['comic']['tmp_name']) || !empty($this->_id)) {
         $this->body = $body;
         $this->title = \Idno\Core\Idno::site()->currentPage()->getInput('title');
         $this->description = \Idno\Core\Idno::site()->currentPage()->getInput('description');
         if ($time = \Idno\Core\Idno::site()->currentPage()->getInput('created')) {
             if ($time = strtotime($time)) {
                 $this->created = $time;
             }
         }
         if (!empty($_FILES['comic']['tmp_name'])) {
             if (\Idno\Entities\File::isImage($_FILES['comic']['tmp_name'])) {
                 if ($size = getimagesize($_FILES['comic']['tmp_name'])) {
                     $this->width = $size[0];
                     $this->height = $size[1];
                 }
                 if ($comic = \Idno\Entities\File::createFromFile($_FILES['comic']['tmp_name'], $_FILES['comic']['name'], $_FILES['comic']['type'], true)) {
                     $this->attachFile($comic);
                 }
             }
         }
         $this->setAccess(\Idno\Core\Idno::site()->currentPage()->getInput('access', 'PUBLIC'));
         if ($this->save($new)) {
             if ($this->getAccess() == 'PUBLIC') {
                 \Idno\Core\Webmention::pingMentions($this->getURL(), \Idno\Core\Idno::site()->template()->parseURLs($this->getDescription()));
             }
             return true;
         }
     } else {
         \Idno\Core\Idno::site()->session()->addErrorMessage('You can\'t save an empty comic.');
     }
     return false;
 }
Exemplo n.º 9
0
 /**
  * Saves changes to this object based on user input
  * @return bool
  */
 function saveDataFromInput()
 {
     if (empty($this->_id)) {
         $new = true;
     } else {
         $new = false;
     }
     $this->title = \Idno\Core\site()->currentPage()->getInput('title');
     $this->body = \Idno\Core\site()->currentPage()->getInput('body');
     $this->setAccess('PUBLIC');
     // Get photo
     if ($new) {
         if (!empty($_FILES['photo']['tmp_name'])) {
             if (\Idno\Entities\File::isImage($_FILES['photo']['tmp_name'])) {
                 if ($photo = \Idno\Entities\File::createFromFile($_FILES['photo']['tmp_name'], $_FILES['photo']['name'], $_FILES['photo']['type'], true)) {
                     $this->attachFile($photo);
                     if ($thumbnail = \Idno\Entities\File::createThumbnailFromFile($_FILES['photo']['tmp_name'], $_FILES['photo']['name'])) {
                         $this->thumbnail = \Idno\Core\site()->config()->url . 'file/' . $thumbnail;
                         $this->thumbnail_id = substr($thumbnail, 0, strpos($thumbnail, '/'));
                     }
                 } else {
                     \Idno\Core\site()->session()->addMessage('Image wasn\'t attached.');
                 }
             } else {
                 \Idno\Core\site()->session()->addMessage('This doesn\'t seem to be an image ..');
             }
         } else {
             \Idno\Core\site()->session()->addMessage('We couldn\'t access your image. Please try again.');
             return false;
         }
     }
     if ($this->save()) {
         if ($new) {
             $this->addToFeed();
         }
         // Add it to the Activity Streams feed
         \Idno\Core\Webmention::pingMentions($this->getURL(), \Idno\Core\site()->template()->parseURLs($this->getDescription()));
         \Idno\Core\site()->session()->addMessage('Your photo was successfully saved.');
         return true;
     } else {
         return false;
     }
 }
Exemplo n.º 10
0
 function saveDataFromInput()
 {
     if (empty($this->_id)) {
         $new = true;
     } else {
         $new = false;
     }
     $body = \Idno\Core\site()->currentPage()->getInput('body');
     if (!empty($_FILES['comic']['tmp_name']) || !empty($this->_id)) {
         $this->body = $body;
         $this->title = \Idno\Core\site()->currentPage()->getInput('title');
         $this->description = \Idno\Core\site()->currentPage()->getInput('description');
         if ($time = \Idno\Core\site()->currentPage()->getInput('created')) {
             if ($time = strtotime($time)) {
                 $this->created = $time;
             }
         }
         if (!empty($_FILES['comic']['tmp_name'])) {
             if (\Idno\Entities\File::isImage($_FILES['comic']['tmp_name'])) {
                 if ($size = getimagesize($_FILES['comic']['tmp_name'])) {
                     $this->width = $size[0];
                     $this->height = $size[1];
                 }
                 if ($comic = \Idno\Entities\File::createFromFile($_FILES['comic']['tmp_name'], $_FILES['comic']['name'], $_FILES['comic']['type'], true)) {
                     $this->attachFile($comic);
                 }
             }
         }
         $this->setAccess('PUBLIC');
         if ($this->save()) {
             if ($new) {
                 // Add it to the Activity Streams feed
                 $this->addToFeed();
             }
             \Idno\Core\Webmention::pingMentions($this->getURL(), \Idno\Core\site()->template()->parseURLs($this->getDescription()));
             return true;
         }
     } else {
         \Idno\Core\site()->session()->addMessage('You can\'t save an empty comic.');
     }
     return false;
 }
Exemplo n.º 11
0
 function postContent()
 {
     $this->gatekeeper();
     // Logged-in only please
     $user = \Idno\Core\site()->session()->currentUser();
     $name = $this->getInput('name');
     //$handle = $this->getInput('handle');
     $email = $this->getInput('email');
     $password = $this->getInput('password');
     $password2 = $this->getInput('password2');
     if (!empty($name)) {
         $user->setTitle($name);
     }
     if (!empty($email) && $email != $user->email && filter_var($email, FILTER_VALIDATE_EMAIL)) {
         if (!\Idno\Entities\User::getByEmail($email)) {
             $user->email = $email;
         } else {
             \Idno\Core\site()->session()->addMessage('Someone is already using ' . $email . ' as their email address.', 'alert-error');
         }
     }
     if (!empty($password) && $password == $password2) {
         $user->setPassword($password);
     }
     if (!empty($_FILES['avatar'])) {
         if (in_array($_FILES['avatar']['type'], array('image/png', 'image/jpg', 'image/jpeg', 'image/gif'))) {
             if (getimagesize($_FILES['avatar']['tmp_name'])) {
                 if ($icon = \Idno\Entities\File::createThumbnailFromFile($_FILES['avatar']['tmp_name'], $_FILES['avatar']['name'], 300)) {
                     $user->icon = (string) $icon;
                 } else {
                     if ($icon = \Idno\Entities\File::createFromFile($_FILES['avatar']['tmp_name'], $_FILES['avatar']['name'])) {
                         $user->icon = (string) $icon;
                     }
                 }
             }
         }
     }
     if ($user->save()) {
         \Idno\Core\site()->session()->addMessage("Your details were saved.");
     }
     $this->forward($_SERVER['HTTP_REFERER']);
 }
Exemplo n.º 12
0
 /**
  * Saves changes to this object based on user input
  * @return bool
  */
 function saveDataFromInput()
 {
     if (empty($this->_id)) {
         $new = true;
     } else {
         $new = false;
     }
     $this->title = \Idno\Core\site()->currentPage()->getInput('title');
     $this->body = \Idno\Core\site()->currentPage()->getInput('body');
     $this->setAccess('PUBLIC');
     // Get video
     if ($new) {
         if (!empty($_FILES['video']['tmp_name'])) {
             if (true) {
                 if ($video = \Idno\Entities\File::createFromFile($_FILES['video']['tmp_name'], $_FILES['video']['name'], $_FILES['video']['type'], true)) {
                     $this->attachFile($video);
                 } else {
                     \Idno\Core\site()->session()->addMessage('Video wasn\'t attached.');
                 }
             } else {
                 \Idno\Core\site()->session()->addMessage('This doesn\'t seem to be a video ..');
             }
         } else {
             \Idno\Core\site()->session()->addMessage('We couldn\'t access your video. Please try again.');
             return false;
         }
     }
     if ($this->save()) {
         if ($new) {
             $this->addToFeed();
         }
         // Add it to the Activity Streams feed
         \Idno\Core\Webmention::pingMentions($this->getURL(), \Idno\Core\site()->template()->parseURLs($this->getDescription()));
         \Idno\Core\site()->session()->addMessage('Your video was successfully saved.');
         return true;
     } else {
         return false;
     }
 }
Exemplo n.º 13
0
 static function importImagesFromBodyHTML($body, $src_url)
 {
     $doc = new \DOMDocument();
     if (@$doc->loadHTML($body)) {
         if ($images = $doc->getElementsByTagName('img')) {
             foreach ($images as $image) {
                 $src = $image->getAttribute('src');
                 if (substr_count($src, $src_url)) {
                     $dir = site()->config()->getTempDir();
                     $name = md5($src);
                     $newname = $dir . $name . basename($src);
                     if (@file_put_contents($newname, fopen($src, 'r'))) {
                         switch (strtolower(pathinfo($src, PATHINFO_EXTENSION))) {
                             case 'jpg':
                             case 'jpeg':
                                 $mime = 'image/jpg';
                                 break;
                             case 'gif':
                                 $mime = 'image/gif';
                                 break;
                             case 'png':
                                 $mime = 'image/png';
                                 break;
                             default:
                                 $mime = 'application/octet-stream';
                         }
                         if ($file = File::createFromFile($newname, basename($src), $mime, true)) {
                             $newsrc = \Idno\Core\site()->config()->getURL() . 'file/' . $file->file['_id'];
                             $body = str_replace($src, $newsrc, $body);
                             @unlink($newname);
                         }
                     }
                 }
             }
         }
     }
 }
Exemplo n.º 14
0
 /**
  * Given a path to an image on disk, generates and saves a thumbnail with maximum dimension $max_dimension.
  * @param string $file_path Path to the file.
  * @param string $filename Filename that the file should have on download.
  * @param int $max_dimension The maximum number of pixels the thumbnail image should be along its longest side.
  * @param bool $square If this is set to true, the thumbnail will be made square.
  * @return bool|id
  */
 public static function createThumbnailFromFile($file_path, $filename, $max_dimension = 800, $square = false)
 {
     $thumbnail = false;
     if ($photo_information = getimagesize($file_path)) {
         if ($photo_information[0] > $max_dimension || $photo_information[1] > $max_dimension) {
             switch ($photo_information['mime']) {
                 case 'image/jpeg':
                     $image = imagecreatefromjpeg($file_path);
                     break;
                 case 'image/png':
                     $image = imagecreatefrompng($file_path);
                     $background = imagecolorallocatealpha($image, 0, 0, 0, 127);
                     imagecolortransparent($image, $background);
                     break;
                 case 'image/gif':
                     $image = imagecreatefromgif($file_path);
                     $background = imagecolorallocatealpha($image, 0, 0, 0, 127);
                     imagecolortransparent($image, $background);
                     break;
             }
             if (!empty($image)) {
                 if ($photo_information[0] > $photo_information[1]) {
                     $width = $max_dimension;
                     $height = round($photo_information[1] * ($max_dimension / $photo_information[0]));
                 } else {
                     $height = $max_dimension;
                     $width = round($photo_information[0] * ($max_dimension / $photo_information[1]));
                 }
                 if ($square) {
                     if ($width > $height) {
                         $new_height = $max_dimension;
                         $new_width = $max_dimension;
                         $original_height = $photo_information[1];
                         $original_width = $photo_information[1];
                         $offset_x = round(($photo_information[0] - $photo_information[1]) / 2);
                         $offset_y = 0;
                     } else {
                         $new_height = $max_dimension;
                         $new_width = $max_dimension;
                         $original_height = $photo_information[0];
                         $original_width = $photo_information[0];
                         $offset_x = 0;
                         $offset_y = round(($photo_information[1] - $photo_information[0]) / 2);
                     }
                 } else {
                     $new_height = $height;
                     $new_width = $width;
                     $original_height = $photo_information[1];
                     $original_width = $photo_information[0];
                     $offset_x = 0;
                     $offset_y = 0;
                 }
                 $image_copy = imagecreatetruecolor($new_width, $new_height);
                 imagealphablending($image_copy, false);
                 imagesavealpha($image_copy, true);
                 imagecopyresampled($image_copy, $image, 0, 0, $offset_x, $offset_y, $new_width, $new_height, $original_width, $original_height);
                 $tmp_dir = dirname($file_path);
                 switch ($photo_information['mime']) {
                     case 'image/jpeg':
                         imagejpeg($image_copy, $tmp_dir . '/' . $filename . '.jpg', 85);
                         $thumbnail = \Idno\Entities\File::createFromFile($tmp_dir . '/' . $filename . '.jpg', "thumb_{$max_dimension}.jpg", 'image/jpeg') . '/thumb.jpg';
                         @unlink($tmp_dir . '/' . $filename . '.jpg');
                         break;
                     case 'image/png':
                         imagepng($image_copy, $tmp_dir . '/' . $filename . '.png');
                         $thumbnail = \Idno\Entities\File::createFromFile($tmp_dir . '/' . $filename . '.png', "thumb_{$max_dimension}.png", 'image/png') . '/thumb.png';
                         @unlink($tmp_dir . '/' . $filename . '.png');
                         break;
                     case 'image/gif':
                         imagegif($image_copy, $tmp_dir . '/' . $filename . '.gif');
                         $thumbnail = \Idno\Entities\File::createFromFile($tmp_dir . '/' . $filename . '.gif', "thumb_{$max_dimension}.gif", 'image/gif') . '/thumb.gif';
                         @unlink($tmp_dir . '/' . $filename . '.gif');
                         break;
                 }
             }
         } else {
         }
         return $thumbnail;
     }
     return false;
 }
Exemplo n.º 15
0
 /**
  * Given a path to an image on disk, generates and saves a thumbnail with maximum dimension $max_dimension.
  * @param string $file_path Path to the file.
  * @param string $filename Filename that the file should have on download.
  * @param int $max_dimension The maximum number of pixels the thumbnail image should be along its longest side.
  * @return bool|MongoID
  */
 public static function createThumbnailFromFile($file_path, $filename, $max_dimension = 800)
 {
     $thumbnail = false;
     if ($photo_information = getimagesize($file_path)) {
         if ($photo_information[0] > $max_dimension || $photo_information[1] > $max_dimension) {
             switch ($photo_information['mime']) {
                 case 'image/jpeg':
                     $image = imagecreatefromjpeg($file_path);
                     break;
                 case 'image/png':
                     $image = imagecreatefrompng($file_path);
                     break;
                 case 'image/gif':
                     $image = imagecreatefromgif($file_path);
                     break;
             }
             if (!empty($image)) {
                 if ($photo_information[0] > $photo_information[1]) {
                     $width = $max_dimension;
                     $height = round($photo_information[1] * ($max_dimension / $photo_information[0]));
                 } else {
                     $height = $max_dimension;
                     $width = round($photo_information[0] * ($max_dimension / $photo_information[1]));
                 }
                 $image_copy = imagecreatetruecolor($width, $height);
                 imagecopyresampled($image_copy, $image, 0, 0, 0, 0, $width, $height, $photo_information[0], $photo_information[1]);
                 if (is_callable('exif_read_data')) {
                     $exif = exif_read_data($file_path);
                     if (!empty($exif['Orientation'])) {
                         switch ($exif['Orientation']) {
                             case 8:
                                 $image_copy = imagerotate($image_copy, 90, 0);
                                 break;
                             case 3:
                                 $image_copy = imagerotate($image_copy, 180, 0);
                                 break;
                             case 6:
                                 $image_copy = imagerotate($image_copy, -90, 0);
                                 break;
                         }
                     }
                 }
                 $tmp_dir = dirname($file_path);
                 switch ($photo_information['mime']) {
                     case 'image/jpeg':
                         imagejpeg($image_copy, $tmp_dir . '/' . $filename . '.jpg');
                         $thumbnail = \Idno\Entities\File::createFromFile($tmp_dir . '/' . $filename . '.jpg', 'thumb.jpg', 'image/jpeg') . '/thumb.jpg';
                         @unlink($tmp_dir . '/' . $filename . '.jpg');
                         break;
                     case 'image/png':
                         imagepng($image_copy, $tmp_dir . '/' . $filename . '.png');
                         $thumbnail = \Idno\Entities\File::createFromFile($tmp_dir . '/' . $filename . '.png', 'thumb.png', 'image/png') . '/thumb.png';
                         @unlink($tmp_dir . '/' . $filename . '.png');
                         break;
                     case 'image/gif':
                         imagegif($image_copy, $tmp_dir . '/' . $filename . '.gif');
                         $thumbnail = \Idno\Entities\File::createFromFile($tmp_dir . '/' . $filename . '.gif', 'thumb.gif', 'image/gif') . '/thumb.gif';
                         @unlink($tmp_dir . '/' . $filename . '.gif');
                         break;
                 }
             }
         } else {
         }
         return $thumbnail;
     }
 }
Exemplo n.º 16
0
 /**
  * Saves changes to this object based on user input
  * @return bool
  */
 function saveDataFromInput()
 {
     if (empty($this->_id)) {
         $new = true;
     } else {
         $new = false;
     }
     if ($new) {
         if (!\Idno\Core\Idno::site()->triggerEvent("file/upload", [], true)) {
             return false;
         }
     }
     $this->title = \Idno\Core\Idno::site()->currentPage()->getInput('title');
     $this->body = \Idno\Core\Idno::site()->currentPage()->getInput('body');
     $this->tags = \Idno\Core\Idno::site()->currentPage()->getInput('tags');
     $access = \Idno\Core\Idno::site()->currentPage()->getInput('access');
     $this->setAccess($access);
     if ($time = \Idno\Core\Idno::site()->currentPage()->getInput('created')) {
         if ($time = strtotime($time)) {
             $this->created = $time;
         }
     }
     // Get photo
     if ($new) {
         if (!empty($_FILES['photo']['tmp_name'])) {
             if (\Idno\Entities\File::isImage($_FILES['photo']['tmp_name']) || \Idno\Entities\File::isSVG($_FILES['photo']['tmp_name'], $_FILES['photo']['name'])) {
                 // Extract exif data so we can rotate
                 if (is_callable('exif_read_data') && $_FILES['photo']['type'] == 'image/jpeg') {
                     try {
                         if (function_exists('exif_read_data')) {
                             if ($exif = exif_read_data($_FILES['photo']['tmp_name'])) {
                                 $this->exif = base64_encode(serialize($exif));
                                 // Yes, this is rough, but exif contains binary data that can not be saved in mongo
                             }
                         }
                     } catch (\Exception $e) {
                         $exif = false;
                     }
                 } else {
                     $exif = false;
                 }
                 if ($photo = \Idno\Entities\File::createFromFile($_FILES['photo']['tmp_name'], $_FILES['photo']['name'], $_FILES['photo']['type'], true, true)) {
                     $this->attachFile($photo);
                     // Now get some smaller thumbnails, with the option to override sizes
                     $sizes = \Idno\Core\Idno::site()->events()->dispatch('photo/thumbnail/getsizes', new \Idno\Core\Event(array('sizes' => array('large' => 800, 'medium' => 400, 'small' => 200))));
                     $eventdata = $sizes->data();
                     foreach ($eventdata['sizes'] as $label => $size) {
                         $filename = $_FILES['photo']['name'];
                         // Experiment: let's not save thumbnails for GIFs, in order to enable animated GIF posting.
                         if ($_FILES['photo']['type'] != 'image/gif') {
                             if ($thumbnail = \Idno\Entities\File::createThumbnailFromFile($_FILES['photo']['tmp_name'], "{$filename}_{$label}", $size, false)) {
                                 $varname = "thumbnail_{$label}";
                                 $this->{$varname} = \Idno\Core\Idno::site()->config()->url . 'file/' . $thumbnail;
                                 $varname = "thumbnail_{$label}_id";
                                 $this->{$varname} = substr($thumbnail, 0, strpos($thumbnail, '/'));
                             }
                         }
                     }
                 } else {
                     \Idno\Core\Idno::site()->session()->addErrorMessage('Image wasn\'t attached.');
                 }
             } else {
                 \Idno\Core\Idno::site()->session()->addErrorMessage('This doesn\'t seem to be an image ..');
             }
         } else {
             \Idno\Core\Idno::site()->session()->addErrorMessage('We couldn\'t access your image. Please try again.');
             return false;
         }
     }
     if ($this->save($new)) {
         \Idno\Core\Webmention::pingMentions($this->getURL(), \Idno\Core\Idno::site()->template()->parseURLs($this->getTitle() . ' ' . $this->getDescription()));
         return true;
     } else {
         return false;
     }
 }
Exemplo n.º 17
0
 /**
  * Retrieve a user's icon from a given homepage
  * @param $content The content of the page
  * @param $url The URL of the page
  * @return $icon_url
  */
 static function getIconFromWebsiteContent($content, $url)
 {
     if ($mf2 = self::parseContent($content, $url)) {
         $mf2 = (array) $mf2;
         foreach ($mf2['items'] as $item) {
             // Figure out what kind of Microformats 2 item we have
             if (!empty($item['type']) && is_array($item['type'])) {
                 foreach ($item['type'] as $type) {
                     switch ($type) {
                         case 'h-card':
                             if (!empty($item['properties'])) {
                                 if (!empty($item['properties']['name'])) {
                                     $mentions['owner']['name'] = $item['properties']['name'][0];
                                 }
                                 if (!empty($item['properties']['url'])) {
                                     $mentions['owner']['url'] = $item['properties']['url'][0];
                                 }
                                 if (!empty($item['properties']['photo'])) {
                                     //$mentions['owner']['photo'] = $item['properties']['photo'][0];
                                     $tmpfname = tempnam(sys_get_temp_dir(), 'webmention_avatar');
                                     file_put_contents($tmpfname, \Idno\Core\Webservice::file_get_contents($item['properties']['photo'][0]));
                                     $name = md5($item['properties']['url'][0]);
                                     // TODO: Don't update the cache image for every webmention
                                     if ($icon = \Idno\Entities\File::createThumbnailFromFile($tmpfname, $name, 300)) {
                                         return \Idno\Core\Idno::site()->config()->url . 'file/' . (string) $icon;
                                     } else {
                                         if ($icon = \Idno\Entities\File::createFromFile($tmpfname, $name)) {
                                             return \Idno\Core\Idno::site()->config()->url . 'file/' . (string) $icon;
                                         }
                                     }
                                     unlink($tmpfname);
                                 }
                             }
                             break;
                     }
                 }
             }
         }
     }
     return false;
 }
Exemplo n.º 18
0
 /**
  * Save form input
  * @param \Idno\Common\Page $page
  * @return bool|\Idno\Common\false|\Idno\Common\true|\Idno\Core\false|\Idno\Core\MongoID|null
  */
 function saveDataFromInput()
 {
     if (!$this->canEdit()) {
         return false;
     }
     $profile = \Idno\Core\Idno::site()->currentPage()->getInput('profile');
     if (!empty($profile)) {
         $this->profile = $profile;
     }
     if ($name = \Idno\Core\Idno::site()->currentPage()->getInput('name')) {
         $this->setName($name);
     }
     if (!empty($_FILES['avatar'])) {
         if (in_array($_FILES['avatar']['type'], array('image/png', 'image/jpg', 'image/jpeg', 'image/gif'))) {
             if (getimagesize($_FILES['avatar']['tmp_name'])) {
                 if ($icon = \Idno\Entities\File::createThumbnailFromFile($_FILES['avatar']['tmp_name'], $_FILES['avatar']['name'], 300, true)) {
                     $this->icon = (string) $icon;
                 } else {
                     if ($icon = \Idno\Entities\File::createFromFile($_FILES['avatar']['tmp_name'], $_FILES['avatar']['name'])) {
                         $this->icon = (string) $icon;
                     }
                 }
             }
         }
     }
     return $this->save();
 }
Exemplo n.º 19
0
 function post()
 {
     $this->gatekeeper();
     // If we're here, we're authorized
     \Idno\Core\Idno::site()->triggerEvent('indiepub/post/start', ['page' => $this]);
     // Get details
     $type = $this->getInput('h', 'entry');
     $content = $this->getInput('content');
     $name = $this->getInput('name');
     $in_reply_to = $this->getInput('in-reply-to');
     $syndicate = $this->getInput('mp-syndicate-to', $this->getInput('syndicate-to'));
     $posse_link = $this->getInput('syndication');
     $like_of = $this->getInput('like-of');
     $repost_of = $this->getInput('repost-of');
     $categories = $this->getInput('category');
     $mp_type = $this->getInput('mp-type');
     if (!empty($mp_type)) {
         $type = $mp_type;
     }
     if ($type == 'entry') {
         $type = 'note';
         if (!empty($_FILES['photo'])) {
             $type = 'photo';
         } else {
             if ($photo_url = $this->getInput('photo')) {
                 $type = 'photo';
                 $success = $this->uploadFromUrl($photo_url);
                 if (!$success) {
                     \Idno\Core\Idno::site()->triggerEvent('indiepub/post/failure', ['page' => $this]);
                     $this->setResponse(500);
                     echo "Failed uploading photo from {$photo_url}";
                     exit;
                 }
             } else {
                 if (!empty($name)) {
                     $type = 'article';
                 }
             }
         }
     }
     if ($type == 'checkin') {
         $place_name = $this->getInput('place_name');
         $location = $this->getInput('location');
         $photo = $this->getInput('photo');
         $latlong = explode(",", $location);
         $lat = str_ireplace("geo:", "", $latlong[0]);
         $long = $latlong[1];
         $q = \IdnoPlugins\Checkin\Checkin::queryLatLong($lat, $long);
         $user_address = $q['display_name'];
         if (!empty($_FILES['photo'])) {
             $id = \Idno\Entities\File::createFromFile($_FILES['photo']['tmp_name'], $_FILES['photo']['name'], $_FILES['photo']['type']);
             $photo = \Idno\Core\Idno::site()->config()->url . 'file/' . $id;
         }
         if (!empty($photo)) {
             $htmlPhoto = '<p><img style="display: block; margin-left: auto; margin-right: auto;" src="' . $photo . '" alt="' . $place_name . '"  /></p>';
         }
     }
     if ($type == 'photo' && empty($name) && !empty($content)) {
         $name = $content;
         $content = '';
     }
     if (!empty($like_of)) {
         $type = 'like';
     }
     if (!empty($repost_of)) {
         $type = 'repost';
     }
     // setting all categories as hashtags into content field
     if (is_array($categories)) {
         $hashtags = "";
         foreach ($categories as $category) {
             $category = trim($category);
             if ($category) {
                 if (str_word_count($category) > 1) {
                     $category = str_replace("'", " ", $category);
                     $category = ucwords($category);
                     $category = str_replace(" ", "", $category);
                 }
                 $hashtags .= " #{$category}";
             }
         }
         $title_words = explode(" ", $name);
         $name = "";
         foreach ($title_words as $word) {
             if (substr($word, 0, 1) !== "#") {
                 $name .= "{$word} ";
             }
         }
     }
     // Get an appropriate plugin, given the content type
     if ($contentType = ContentType::getRegisteredForIndieWebPostType($type)) {
         if ($entity = $contentType->createEntity()) {
             if (is_array($content)) {
                 $content_value = '';
                 if (!empty($content['html'])) {
                     $content_value = $content['html'];
                 } else {
                     if (!empty($content['value'])) {
                         $content_value = $content['value'];
                     }
                 }
             } else {
                 $content_value = $content;
             }
             if (!empty($posse_link)) {
                 $posse_service = parse_url($posse_link, PHP_URL_HOST);
                 $entity->setPosseLink(str_replace('.com', '', $posse_service), $posse_link, '', '');
             }
             $hashtags = empty($hashtags) ? "" : "<p>" . $hashtags . "</p>";
             $htmlPhoto = empty($htmlPhoto) ? "" : "<p>" . $htmlPhoto . "</p>";
             $this->setInput('title', $name);
             $this->setInput('body', $htmlPhoto . $content_value . $hashtags);
             $this->setInput('inreplyto', $in_reply_to);
             $this->setInput('like-of', $like_of);
             $this->setInput('repost-of', $repost_of);
             $this->setInput('access', 'PUBLIC');
             if ($type == 'checkin') {
                 $this->setInput('lat', $lat);
                 $this->setInput('long', $long);
                 $this->setInput('user_address', $user_address);
                 $this->setInput('placename', $place_name);
             }
             if ($created = $this->getInput('published')) {
                 $this->setInput('created', $created);
             }
             if (!empty($syndicate)) {
                 if (is_array($syndicate)) {
                     $syndication = $syndicate;
                 } else {
                     $syndication = array(trim(str_replace('.com', '', $syndicate)));
                 }
                 \Idno\Core\Idno::site()->logging()->info("Setting syndication: {$syndication}");
                 $this->setInput('syndication', $syndication);
             }
             if ($entity->saveDataFromInput()) {
                 \Idno\Core\Idno::site()->triggerEvent('indiepub/post/success', ['page' => $this, 'object' => $entity]);
                 $this->setResponse(201);
                 header('Location: ' . $entity->getURL());
                 exit;
             } else {
                 \Idno\Core\Idno::site()->triggerEvent('indiepub/post/failure', ['page' => $this]);
                 $this->setResponse(500);
                 echo "Couldn't create {$type}";
                 exit;
             }
         }
     } else {
         \Idno\Core\Idno::site()->triggerEvent('indiepub/post/failure', ['page' => $this]);
         $this->setResponse(500);
         echo "Couldn't find content type {$type}";
         exit;
     }
 }
Exemplo n.º 20
0
 function saveDataFromInput()
 {
     if (empty($this->_id)) {
         $new = true;
     } else {
         $new = false;
     }
     $body = \Idno\Core\site()->currentPage()->getInput('body');
     if (!empty($body)) {
         $this->body = $body;
         $this->title = \Idno\Core\site()->currentPage()->getInput('title');
         $this->rating = \Idno\Core\site()->currentPage()->getInput('rating');
         $this->productName = \Idno\Core\site()->currentPage()->getInput('productName');
         $this->productCategory = \Idno\Core\site()->currentPage()->getInput('productCategory');
         $this->productLink = \Idno\Core\site()->currentPage()->getInput('productLink');
         $access = \Idno\Core\site()->currentPage()->getInput('access');
         $this->setAccess($access);
         if ($time = \Idno\Core\site()->currentPage()->getInput('created')) {
             if ($time = strtotime($time)) {
                 $this->created = $time;
             }
         }
         if ($new) {
             if (!empty($_FILES['photo']['tmp_name'])) {
                 if (\Idno\Entities\File::isImage($_FILES['photo']['tmp_name'])) {
                     // Extract exif data so we can rotate
                     if (is_callable('exif_read_data') && $_FILES['photo']['type'] == 'image/jpeg') {
                         try {
                             if (function_exists('exif_read_data')) {
                                 if ($exif = exif_read_data($_FILES['photo']['tmp_name'])) {
                                     $this->exif = base64_encode(serialize($exif));
                                     // Yes, this is rough, but exif contains binary data that can not be saved in mongo
                                 }
                             }
                         } catch (\Exception $e) {
                             $exif = false;
                         }
                     } else {
                         $exif = false;
                     }
                     if ($photo = \Idno\Entities\File::createFromFile($_FILES['photo']['tmp_name'], $_FILES['photo']['name'], $_FILES['photo']['type'], true, true)) {
                         $this->attachFile($photo);
                         // Now get some smaller thumbnails, with the option to override sizes
                         $sizes = \Idno\Core\site()->events()->dispatch('photo/thumbnail/getsizes', new \Idno\Core\Event(array('sizes' => array('large' => 800, 'medium' => 400, 'small' => 200))));
                         $eventdata = $sizes->data();
                         foreach ($eventdata['sizes'] as $label => $size) {
                             $filename = $_FILES['photo']['name'];
                             if ($thumbnail = \Idno\Entities\File::createThumbnailFromFile($_FILES['photo']['tmp_name'], "{$filename}_{$label}", $size, false)) {
                                 $varname = "thumbnail_{$label}";
                                 $this->{$varname} = \Idno\Core\site()->config()->url . 'file/' . $thumbnail;
                                 $varname = "thumbnail_{$label}_id";
                                 $this->{$varname} = substr($thumbnail, 0, strpos($thumbnail, '/'));
                             }
                         }
                     }
                 } else {
                     \Idno\Core\site()->session()->addErrorMessage('This doesn\'t seem to be an image ..');
                 }
             }
         }
         if ($this->save($new)) {
             $autosave = new Autosave();
             $autosave->clearContext('review');
             \Idno\Core\Webmention::pingMentions($this->getURL(), \Idno\Core\site()->template()->parseURLs($this->getTitle() . ' ' . $this->getDescription()));
             return true;
         }
     } else {
         \Idno\Core\site()->session()->addErrorMessage('You can\'t save an empty entry.');
     }
     return false;
 }
Exemplo n.º 21
0
 /**
  * Recursive helper function for parsing webmentions.
  *
  * @param $item
  * @param $mentions
  * @return array
  */
 function addWebmentionItem($item, $mentions, $source, $target, $title = '')
 {
     if (!empty($item['properties']['author'])) {
         foreach ($item['properties']['author'] as $author) {
             if (!empty($author['type'])) {
                 foreach ($author['type'] as $type) {
                     if ($type == 'h-card') {
                         if (!empty($author['properties']['name'])) {
                             $mentions['owner']['name'] = $author['properties']['name'][0];
                         }
                         if (!empty($author['properties']['url'])) {
                             $mentions['owner']['url'] = $author['properties']['url'][0];
                         }
                         if (!empty($author['properties']['photo'])) {
                             //$mentions['owner']['photo'] = $author['properties']['photo'][0];
                             $tmpfname = tempnam(sys_get_temp_dir(), 'webmention_avatar');
                             file_put_contents($tmpfname, \Idno\Core\Webservice::file_get_contents($author['properties']['photo'][0]));
                             $name = md5($author['properties']['url'][0]);
                             // TODO: Don't update the cache image for every webmention
                             if ($icon = \Idno\Entities\File::createThumbnailFromFile($tmpfname, $name, 300)) {
                                 $mentions['owner']['photo'] = \Idno\Core\Idno::site()->config()->url . 'file/' . (string) $icon;
                             } else {
                                 if ($icon = \Idno\Entities\File::createFromFile($tmpfname, $name)) {
                                     $mentions['owner']['photo'] = \Idno\Core\Idno::site()->config()->url . 'file/' . (string) $icon;
                                 }
                             }
                             unlink($tmpfname);
                         }
                     }
                 }
             }
         }
     }
     if (!empty($item['type'])) {
         if (in_array('h-entry', $item['type'])) {
             $mention = array();
             if (!empty($item['properties'])) {
                 if (!empty($item['properties']['content'])) {
                     $mention['content'] = '';
                     if (is_array($item['properties']['content'])) {
                         foreach ($item['properties']['content'] as $content) {
                             if (!empty($content['value'])) {
                                 $parsed_content = \Idno\Core\Idno::site()->template()->sanitize_html($content['value']);
                                 if (!substr_count($mention['content'], $parsed_content)) {
                                     $mention['content'] .= $parsed_content;
                                 }
                             }
                         }
                     } else {
                         $mention['content'] = $item['properties']['content'];
                     }
                 } else {
                     if (!empty($item['properties']['summary'])) {
                         if (is_array($item['properties']['summary'])) {
                             $mention['content'] = \Idno\Core\Idno::site()->template()->sanitize_html(implode(' ', $item['properties']['summary']));
                         } else {
                             $mention['content'] = $item['properties']['summary'];
                         }
                     } else {
                         if (!empty($item['properties']['name'])) {
                             if (is_array($item['properties']['name'])) {
                                 $mention['content'] = \Idno\Core\Idno::site()->template()->sanitize_html(implode(' ', $item['properties']['name']));
                             } else {
                                 $mention['content'] = $item['properties']['name'];
                             }
                         }
                     }
                 }
                 if (!empty($item['properties']['published'])) {
                     if (is_array($item['properties']['published'])) {
                         $mention['created'] = @strtotime(array_shift(array_pop($item['properties']['published'])));
                     } else {
                         $mention['created'] = @strtotime($item['properties']['content']);
                     }
                 }
                 if (empty($mention['created'])) {
                     $mention['created'] = time();
                 }
                 if (!empty($item['properties']['url'])) {
                     if (!empty($item['properties']['uid'])) {
                         $mention['url'] = array_intersect($item['properties']['uid'], $item['properties']['url']);
                     }
                     if (empty($mention['url'])) {
                         $mention['url'] = $item['properties']['url'];
                     }
                 }
                 if (!empty($item['properties']['like']) && is_array($item['properties']['like'])) {
                     if (in_array($target, static::getStringURLs($item['properties']['like']))) {
                         $mention['type'] = 'like';
                     }
                 }
                 if (!empty($item['properties']['like-of']) && is_array($item['properties']['like-of'])) {
                     if (in_array($target, static::getStringURLs($item['properties']['like-of']))) {
                         $mention['type'] = 'like';
                     }
                 }
                 if (!empty($item['properties']['rsvp']) && is_array($item['properties']['rsvp'])) {
                     $mention['type'] = 'rsvp';
                     $mention['content'] = implode(' ', $item['properties']['rsvp']);
                 }
                 foreach (array('share', 'repost', 'repost-of') as $verb) {
                     if (!empty($item['properties'][$verb]) && is_array($item['properties'][$verb])) {
                         if (in_array($target, static::getStringURLs($item['properties'][$verb]))) {
                             $mention['type'] = 'share';
                         }
                     }
                 }
                 if (!empty($item['properties']['in-reply-to']) && is_array($item['properties']['in-reply-to'])) {
                     if (in_array($target, static::getStringURLs($item['properties']['in-reply-to']))) {
                         $mention['type'] = 'reply';
                     }
                 }
                 if (empty($mention['type'])) {
                     $mention['type'] = 'mention';
                 }
             }
             if (empty($mention['content'])) {
                 $mention['content'] = '';
             }
             $mention['title'] = $title;
             if (!empty($mention['type'])) {
                 $mentions['mentions'][] = $mention;
             }
         }
     }
     if (in_array('h-feed', $item['type'])) {
         if (!empty($item['children'])) {
             foreach ($item['children'] as $child) {
                 $mentions = $this->addWebmentionItem($child, $mentions, $source, $target, $title);
             }
         }
     }
     return $mentions;
 }
Exemplo n.º 22
0
 private function parseHCard($hcard)
 {
     $owner = [];
     if (!empty($hcard['properties']['name'])) {
         $owner['name'] = $hcard['properties']['name'][0];
     }
     if (!empty($hcard['properties']['url'])) {
         $owner['url'] = $hcard['properties']['url'][0];
     }
     if (!empty($hcard['properties']['photo'])) {
         //$owner['photo'] = $hcard['properties']['photo'][0];
         $tmpfname = tempnam(sys_get_temp_dir(), 'webmention_avatar');
         file_put_contents($tmpfname, \Idno\Core\Webservice::file_get_contents($hcard['properties']['photo'][0]));
         $name = md5($hcard['properties']['url'][0]);
         // TODO: Don't update the cache image for every webmention
         if ($icon = \Idno\Entities\File::createThumbnailFromFile($tmpfname, $name, 300)) {
             $owner['photo'] = \Idno\Core\Idno::site()->config()->url . 'file/' . (string) $icon;
         } else {
             if ($icon = \Idno\Entities\File::createFromFile($tmpfname, $name)) {
                 $owner['photo'] = \Idno\Core\Idno::site()->config()->url . 'file/' . (string) $icon;
             }
         }
         unlink($tmpfname);
     }
     return $owner;
 }
Exemplo n.º 23
0
 /**
  * Saves changes to this object based on user input
  * @return bool
  */
 function saveDataFromInput()
 {
     if (empty($this->_id)) {
         $new = true;
     } else {
         $new = false;
     }
     if ($new) {
         if (!\Idno\Core\Idno::site()->triggerEvent("file/upload", [], true)) {
             return false;
         }
     }
     $this->title = \Idno\Core\Idno::site()->currentPage()->getInput('title');
     $this->body = \Idno\Core\Idno::site()->currentPage()->getInput('body');
     $this->tags = \Idno\Core\Idno::site()->currentPage()->getInput('tags');
     $access = \Idno\Core\Idno::site()->currentPage()->getInput('access');
     $this->setAccess($access);
     if ($time = \Idno\Core\Idno::site()->currentPage()->getInput('created')) {
         if ($time = strtotime($time)) {
             $this->created = $time;
         }
     }
     // This flag will tell us if it's safe to save the object later on
     if ($new) {
         $ok = false;
     } else {
         $ok = true;
     }
     // Get media
     if ($new) {
         // This is awful, but unfortunately, browsers can't be trusted to send the right mimetype.
         $ext = pathinfo($_FILES['media']['name'], PATHINFO_EXTENSION);
         if (!empty($ext)) {
             if (in_array($ext, array('mp4', 'mov', 'webm', 'ogg', 'mpeg', 'mp3', 'vorbis'))) {
                 $media_file = $_FILES['media'];
                 if ($media_file['type'] == 'application/octet-stream') {
                     switch ($ext) {
                         case 'mp4':
                             $media_file['type'] = 'video/mp4';
                             break;
                         case 'mov':
                             $media_file['type'] = 'video/mov';
                             break;
                         case 'webm':
                             $media_file['type'] = 'video/webm';
                             break;
                         case 'ogg':
                             $media_file['type'] = 'audio/ogg';
                             break;
                         case 'mp3':
                             $media_file['type'] = 'audio/mpeg';
                             break;
                         case 'mpeg':
                             $media_file['type'] = 'video/mpeg';
                             break;
                         case 'ogv':
                             $media_file['type'] = 'audio/ogv';
                             break;
                     }
                 }
                 $this->media_type = $media_file['type'];
                 if ($media = \Idno\Entities\File::createFromFile($media_file['tmp_name'], $media_file['name'], $media_file['type'], true)) {
                     $this->attachFile($media);
                     $ok = true;
                 } else {
                     \Idno\Core\Idno::site()->session()->addErrorMessage('Media wasn\'t attached.');
                 }
             } else {
                 \Idno\Core\Idno::site()->session()->addErrorMessage('This doesn\'t seem to be a media file .. ' . $_FILES['media']['type']);
             }
         } else {
             \Idno\Core\Idno::site()->session()->addErrorMessage('We couldn\'t access your media. Please try again.');
             return false;
         }
     }
     // If a media file wasn't attached, don't save the file.
     if (!$ok) {
         return false;
     }
     if ($this->save($new)) {
         \Idno\Core\Webmention::pingMentions($this->getURL(), \Idno\Core\Idno::site()->template()->parseURLs($this->getTitle() . ' ' . $this->getDescription()));
         return true;
     } else {
         return false;
     }
 }
Exemplo n.º 24
0
 /**
  * Given a path to an image on disk, generates and saves a thumbnail with maximum dimension $max_dimension.
  * @param string $file_path Path to the file.
  * @param string $filename Filename that the file should have on download.
  * @param int $max_dimension The maximum number of pixels the thumbnail image should be along its longest side.
  * @param bool $square If this is set to true, the thumbnail will be made square.
  * @return bool|id
  */
 public static function createThumbnailFromFile($file_path, $filename, $max_dimension = 800, $square = false)
 {
     $thumbnail = false;
     // Rotate image where appropriate
     if (is_callable('exif_read_data')) {
         try {
             if ($exif = exif_read_data($file_path)) {
                 if (!empty($exif['Orientation'])) {
                     $orientation = $exif['Orientation'];
                 }
             }
         } catch (\Exception $e) {
         }
     }
     if ($photo_information = getimagesize($file_path)) {
         if ($photo_information[0] > $max_dimension || $photo_information[1] > $max_dimension) {
             switch ($photo_information['mime']) {
                 case 'image/jpeg':
                     $image = imagecreatefromjpeg($file_path);
                     break;
                 case 'image/png':
                     $image = imagecreatefrompng($file_path);
                     $background = imagecolorallocatealpha($image, 0, 0, 0, 127);
                     imagecolortransparent($image, $background);
                     break;
                 case 'image/gif':
                     $image = imagecreatefromgif($file_path);
                     $background = imagecolorallocatealpha($image, 0, 0, 0, 127);
                     imagecolortransparent($image, $background);
                     break;
             }
             if (!empty($image)) {
                 if (isset($orientation)) {
                     switch ($orientation) {
                         case 8:
                             $image = imagerotate($image, 90, 0);
                             break;
                         case 3:
                             $image = imagerotate($image, 180, 0);
                             break;
                         case 6:
                             $image = imagerotate($image, -90, 0);
                             break;
                     }
                 }
                 $existing_width = imagesx($image);
                 $existing_height = imagesy($image);
                 if ($existing_width > $existing_height) {
                     $width = $max_dimension;
                     $height = round($existing_height * ($max_dimension / $existing_width));
                 } else {
                     $height = $max_dimension;
                     $width = round($existing_width * ($max_dimension / $existing_height));
                 }
                 if ($square) {
                     if ($width > $height) {
                         $new_height = $max_dimension;
                         $new_width = $max_dimension;
                         $original_height = $existing_height;
                         $original_width = $existing_height;
                         $offset_x = round(($existing_width - $existing_height) / 2);
                         $offset_y = 0;
                     } else {
                         $new_height = $max_dimension;
                         $new_width = $max_dimension;
                         $original_height = $existing_width;
                         $original_width = $existing_width;
                         $offset_x = 0;
                         $offset_y = round(($existing_height - $existing_width) / 2);
                     }
                 } else {
                     $new_height = $height;
                     $new_width = $width;
                     $original_height = $photo_information[1];
                     $original_width = $photo_information[0];
                     $offset_x = 0;
                     $offset_y = 0;
                 }
                 $image_copy = imagecreatetruecolor($new_width, $new_height);
                 imagealphablending($image_copy, false);
                 imagesavealpha($image_copy, true);
                 imagecopyresampled($image_copy, $image, 0, 0, $offset_x, $offset_y, $new_width, $new_height, $original_width, $original_height);
                 $tmp_dir = dirname($file_path);
                 switch ($photo_information['mime']) {
                     case 'image/jpeg':
                         imagejpeg($image_copy, $tmp_dir . '/' . $filename . '.jpg', 85);
                         $thumbnail = \Idno\Entities\File::createFromFile($tmp_dir . '/' . $filename . '.jpg', "thumb_{$max_dimension}.jpg", 'image/jpeg') . '/thumb.jpg';
                         @unlink($tmp_dir . '/' . $filename . '.jpg');
                         break;
                     case 'image/png':
                         imagepng($image_copy, $tmp_dir . '/' . $filename . '.png');
                         $thumbnail = \Idno\Entities\File::createFromFile($tmp_dir . '/' . $filename . '.png', "thumb_{$max_dimension}.png", 'image/png') . '/thumb.png';
                         @unlink($tmp_dir . '/' . $filename . '.png');
                         break;
                     case 'image/gif':
                         imagegif($image_copy, $tmp_dir . '/' . $filename . '.gif');
                         $thumbnail = \Idno\Entities\File::createFromFile($tmp_dir . '/' . $filename . '.gif', "thumb_{$max_dimension}.gif", 'image/gif') . '/thumb.gif';
                         @unlink($tmp_dir . '/' . $filename . '.gif');
                         break;
                 }
             }
         } else {
         }
         return $thumbnail;
     }
     return false;
 }