Esempio n. 1
0
 /**
  * Fetches an absolute site URL based on a URI segment.
  *
  *     echo URL::site('foo/bar');
  *
  * @param   string  $uri        Site URI to convert
  * @param   mixed   $protocol   Protocol string or [Request] class to use protocol from
  * @param   boolean $index      Include the index_page in the URL
  * @return  string
  * @uses    URL::base
  */
 public static function site($uri = '', $protocol = NULL, $index = TRUE)
 {
     // Chop off possible scheme, host, port, user and pass parts
     $path = preg_replace('~^[-a-z0-9+.]++://[^/]++/?~', '', trim($uri, '/'));
     // Encode all non-ASCII characters, as per RFC 1738
     if (mb_detect_encoding($path, 'ASCII') === TRUE) {
         $path = parent::title($path, '-', TRUE);
     }
     // Concat the URL
     return URL::base($protocol, $index) . $path;
 }
Esempio n. 2
0
 /**
  * Convert a phrase to a URL-safe title. Overwriten original to ascii only depending on language
  *
  *     echo URL::title('My Blog Post'); // "my-blog-post"
  *
  * @param   string   $title       Phrase to convert
  * @param   string   $separator   Word separator (any single character)
  * @param   boolean  $ascii_only  Transliterate to ASCII?
  * @return  string
  * @uses    UTF8::transliterate_to_ascii
  */
 public static function title($title, $separator = '-', $ascii_only = NULL)
 {
     /**
      * this hack is to add tohse languages that are not in ascii, so we add them to the array
      * @var boolean
      */
     if ($ascii_only === NULL) {
         $ascii_only = in_array(i18n::$locale, array('hi_IN', 'ar', 'ur_PK', 'ru_RU', 'bn_BD', 'ml_IN', 'ja_JP')) ? FALSE : TRUE;
     }
     return parent::title($title, $separator, $ascii_only);
 }
Esempio n. 3
0
 /**
  * 
  * @param string $uri
  * @param string $protocol
  * @param boolean $index
  * @return string
  */
 public static function frontend($uri = '', $protocol = NULL, $index = TRUE)
 {
     $uri_components = parse_url($uri);
     $hash = $query_string = '';
     if (strpos($uri, '#') !== FALSE) {
         list($uri, $hash) = preg_split('/#/', $uri);
         $hash = '#' . $hash;
     }
     if (strpos($uri, '?') !== FALSE) {
         list($uri, $query_string) = preg_split('/\\?/', $uri);
         $query_string = '?' . $query_string;
     }
     if (IS_INSTALLED and !empty($uri) and $uri != '/' and !URL::has_suffix($uri)) {
         $uri = rtrim($uri, '/') . URL_SUFFIX . $query_string . $hash;
     }
     unset($hash, $query_string);
     return parent::site($uri, $protocol, $index);
 }
Esempio n. 4
0
 public function action_static($supplychain_id, $sz = null)
 {
     if (!is_numeric($supplychain_id)) {
         $supplychain_id = $this->_match_alias($supplychain_id);
     }
     $supplychain = ORM::factory('supplychain', $supplychain_id);
     $szdim = false;
     $valid_size = false;
     $image_sizes = Sourcemap_Map_Static::$image_sizes;
     $image_thumbs = Sourcemap_Map_Static::$image_thumbs;
     do {
         if (isset($image_sizes[$sz])) {
             $valid_size = true;
             $szdim = $image_sizes[$sz];
         } elseif ($sz == 'o') {
             $valid_size = true;
             $szdim = array(1024, 780);
         } else {
             foreach ($image_thumbs as $tk => $tv) {
                 if ("th-{$tk}" == $sz) {
                     $valid_size = true;
                     $szdim = $tv;
                     break;
                 }
             }
         }
         if (!$valid_size) {
             $sz = Sourcemap_Map_Static::$default_image_size;
         }
     } while (!$valid_size);
     if ($supplychain->loaded()) {
         $current_user_id = Auth::instance()->logged_in() ? (int) Auth::instance()->get_user()->id : 0;
         $owner_id = (int) $supplychain->user_id;
         if ($supplychain->user_can($current_user_id, Sourcemap::READ)) {
             header('Content-Type: image/png');
             //header('Cache-Control: private,max-age=600');
             $ckeyfmt = "static-map-%010d-%s-png";
             //$cache_key = sprintf($ckeyfmt, $supplychain_id, $sz);
             $cache_key = Sourcemap_Map_Static::cache_key($supplychain_id, $sz);
             $exists = Cache::instance()->get($cache_key);
             if ($exists) {
                 header('X-Cache-Hit: true');
                 print $exists;
             } else {
                 // make blank image and enqueue job to generate
                 $maptic_url = Kohana::config('sourcemap.maptic_baseurl') . sprintf('%s-static-map-sc%06d-%s.png', Sourcemap::$env, $supplychain_id, $sz);
                 $fetched = @file_get_contents($maptic_url);
                 if ($fetched) {
                     print $fetched;
                     Cache::instance()->set($cache_key, $fetched, 300);
                     exit;
                 } elseif ($pimg = imagecreatefrompng(self::placeholder_image())) {
                     // pass
                     $pimgw = imagesx($pimg);
                     $pimgh = imagesy($pimg);
                     if (count($szdim) == 2) {
                         $rpimgw = $szdim[0];
                         $rpimgh = $szdim[1];
                     } elseif (count($szdim) == 4) {
                         $rpimgw = $szdim[2] - $szdim[0];
                         $rpimgh = $szdim[3] - $szdim[1];
                     }
                     $rpimg = imagecreatetruecolor($rpimgw, $rpimgh);
                     imagecopyresampled($rpimg, $pimg, 0, 0, 0, 0, $rpimgw, $rpimgh, $pimgw, $pimgh);
                     imagedestroy($pimg);
                     $pimg = $rpimg;
                 } else {
                     $pimg = imagecreatetruecolor($szdim[0], $szdim[1]);
                     imagecolorallocate($pimg, 0, 0, 255);
                 }
                 imagepng($pimg);
                 Sourcemap::enqueue(Sourcemap_Job::STATICMAPGEN, array('baseurl' => Kohana_URL::site('/', true), 'environment' => Sourcemap::$env, 'supplychain_id' => $supplychain->id, 'sizes' => Sourcemap_Map_Static::$image_sizes, 'thumbs' => Sourcemap_Map_Static::$image_thumbs));
             }
             exit;
         } else {
             $this->request->status = 403;
             $this->layout = View::factory('layout/error');
             $this->template = View::factory('error');
             $this->template->error_message = 'This map is private.';
         }
     } else {
         $this->request->status = 404;
         $this->layout = View::factory('layout/error');
         $this->template = View::factory('error');
         $this->template->error_message = 'That map could not be found.';
     }
 }
Esempio n. 5
0
 public static function current($protocol = null, $index = true)
 {
     $uri = Request::initial()->uri();
     return parent::site($uri, $protocol, $index);
 }
Esempio n. 6
0
 public function action_refresh_supplychain($id)
 {
     $supplychain = ORM::factory('supplychain', $id);
     if ($supplychain->loaded()) {
         // pass
     } else {
         Message::instance()->set('Invalid supplychain.');
         $this->request->redirect('admin/supplychains/');
     }
     try {
         // Delete and recreate cache entry
         Cache::instance()->delete('supplychain-' . $id);
         Message::instance()->set('Primary cache entry for supplychain ' . $id . ' deleted.', Message::INFO);
         if (Sourcemap_Search_Index::should_index($id)) {
             Message::instance()->set('Supplychain ' . $id . ' re-indexed.', Message::INFO);
             Sourcemap_Search_Index::update($id);
         } else {
             Message::instance()->set('Supplychain ' . $id . ' de-indexed.', Message::INFO);
             Sourcemap_Search_Index::delete($id);
         }
         // Recreate static image
         $szs = Sourcemap_Map_Static::$image_sizes;
         foreach ($szs as $snm => $sz) {
             $ckey = Sourcemap_Map_Static::cache_key($id, $snm);
             Cache::instance()->delete($ckey);
         }
         Message::instance()->set('Removed cached static map images for map ' . $id . '.', Message::INFO);
         Sourcemap::enqueue(Sourcemap_Job::STATICMAPGEN, array('baseurl' => Kohana_URL::site('/', true), 'environment' => Sourcemap::$env, 'supplychain_id' => (int) $id, 'sizes' => Sourcemap_Map_Static::$image_sizes, 'thumbs' => Sourcemap_Map_Static::$image_thumbs));
         Message::instance()->set('Queued job for new static maps. Should regenerate within 30-60 seconds.', Message::INFO);
         // I don't know that lotus, et al. want to update
         // the modified time. I think we just want to trigger
         // a reload/redraw for cache and static maps, respectively.
         //$sc = ORM::factory('supplychain', $id);
         //$sc->modified = time();
         //$sc->save();
         $this->request->redirect("admin/supplychains/{$id}");
     } catch (Exception $e) {
         Message::instance()->set('Could not refresh supplychain ' . $id . '.');
     }
 }
Esempio n. 7
0
 public function action_put()
 {
     $id = $this->request->param('id', false);
     if (!$id) {
         return $this->_bad_request('No id.');
     }
     if (!($supplychain = ORM::factory('supplychain', $id))) {
         return $this->_not_found('That supplychain does not exist.');
     }
     $current_user = $this->get_current_user();
     if (!$current_user) {
         return $this->_forbidden('You must be signed in to create or edit supplychains.');
     }
     if (!$supplychain->user_can($current_user->id, Sourcemap::WRITE)) {
         // TODO: use user_can method
         $user_groups = ORM::factory('user', $current_user->id)->groups->find_all()->as_array('id', true);
         return $this->_forbidden('You do not have permission to edit this supplychain.');
     }
     $put = $this->request->put_data;
     try {
         if ($this->_validate_raw_supplychain($put)) {
             $raw_sc = $put->supplychain;
             $retries = 3;
             $retry = true;
             while ($retry && $retries--) {
                 $ecode = false;
                 try {
                     $supplychain->save_raw_supplychain($raw_sc, $id);
                     $retry = false;
                 } catch (Exception $e) {
                     $ecode = $supplychain->get_db()->get_pdo()->errorCode();
                     if ($ecode === 40001) {
                         $retry = true;
                     }
                 }
                 if ($ecode !== false && $ecode === 40001) {
                     throw new Exception('Dammit, Jim! The data! The data is not being saved!');
                 }
             }
             Sourcemap::enqueue(Sourcemap_Job::STATICMAPGEN, array('baseurl' => Kohana_URL::site('/', true), 'environment' => Sourcemap::$env, 'supplychain_id' => (int) $id, 'sizes' => Sourcemap_Map_Static::$image_sizes, 'thumbs' => Sourcemap_Map_Static::$image_thumbs));
         } else {
             throw new Exception('Invalid supplychain data: ' . $e);
         }
     } catch (Exception $e) {
         return $this->_bad_request('Could not save supplychain: ' . $e->getMessage());
     }
     $this->request->status = 202;
     $this->response = (object) array('success' => 'Supplychain updated.');
 }