Example #1
0
 public static function link_tags()
 {
     $args = func_get_args();
     $styles = array();
     foreach ($args as $ai => $arg) {
         if (is_array($arg)) {
             foreach ($arg as $aai => $a) {
                 $styles[] = $a;
             }
         } else {
             $styles[] = $arg;
         }
     }
     $tags = array();
     foreach ($styles as $si => $style) {
         if (self::$convert_less) {
             $style = preg_replace('/\\.less$/', '.css', $style);
         }
         if (preg_match('/\\.less$/', $style)) {
             // add revision as GET param to avoid old, cached css/less
             if ($rev = Sourcemap::revision()) {
                 $style .= '?_v=' . $rev;
             }
             $tags[] = '<link rel="stylesheet/less" href="' . $style . '" type="text/css" />';
         } else {
             // add revision as GET param to avoid old, cached css/less
             if ($rev = Sourcemap::revision()) {
                 $style .= '?_v=' . $rev;
             }
             $tags[] = '<link rel="stylesheet" href="' . $style . '" type="text/css"/>';
         }
     }
     $tags = join("\n", $tags);
     return $tags;
 }
Example #2
0
 public function __construct($url = null)
 {
     $this->url = $url;
     $this->method = self::GET;
     $this->user_agent = sprintf('Sourcemap HTTP Client (%d)', Sourcemap::revision());
     $this->parameters = array();
     $this->headers = array();
     $this->_ch = curl_init();
     curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($this->_ch, CURLOPT_HEADER, true);
 }
Example #3
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.';
     }
 }
 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 . '.');
     }
 }
Example #5
0
 public static function script_tags()
 {
     $args = func_get_args();
     $scripts = call_user_func_array(array('self', 'scripts'), $args);
     $tags = array();
     if ($rev = Sourcemap::revision()) {
         $rev = "_v={$rev}";
     } else {
         $rev = '';
     }
     foreach ($scripts as $i => $script) {
         if ($rev && !preg_match('/^https?:\\/\\//', $script)) {
             if (strstr($script, '?')) {
                 $script .= "&{$rev}";
             } else {
                 $script .= "?{$rev}";
             }
         }
         $tags[] = HTML::script($script);
     }
     return join("\n", $tags);
 }
Example #6
0
 public static function enqueue($type, $params = null)
 {
     if (Kohana::config('sourcemap.job_queue')) {
         if (!self::$job_queue) {
             Sourcemap::$job_queue = Sourcemap_Job_Queue::instance(Kohana::config('sourcemap.job_queue_host'), Kohana::config('sourcemap.job_queue_port'));
         }
         $job = Sourcemap_Job::factory($type, $params);
         return Sourcemap::$job_queue->enqueue($job);
     }
     return false;
 }
 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.');
 }
Example #8
0
if (isset(Kohana::config('sourcemap')->cache_dir)) {
    Kohana::$cache_dir = Kohana::config('sourcemap')->cache_dir;
}
/**
 * Enable modules. Modules are referenced by a relative or absolute path.
 */
Kohana::modules(array('auth' => MODPATH . 'auth', 'cache' => MODPATH . 'cache', 'database' => MODPATH . 'database', 'orm' => MODPATH . 'orm', 'pagination' => MODPATH . 'pagination', 'sitemap' => MODPATH . 'sitemap', 'recaptcha' => MODPATH . 'recaptcha'));
Kohana::add_include_path(SOURCEMAP_SITES_PATH . SOURCEMAP_SITE . '/');
if (is_file(SOURCEMAP_SITES_PATH . SOURCEMAP_SITE . '/bootstrap' . EXT)) {
    require SOURCEMAP_SITES_PATH . SOURCEMAP_SITE . '/bootstrap' . EXT;
}
if (is_dir(SOURCEMAP_SITES_PATH . SOURCEMAP_SITE . '/config/')) {
    $site_config_dir = SOURCEMAP_SITES_PATH . SOURCEMAP_SITE . '/config/';
    Kohana::$config->attach(new Kohana_Config_File($site_config_dir));
}
Sourcemap::init();
/**
 * Set the default (all sites) routes. Each route must have a minimum of a name, a URI and a set of
 * defaults for the URI. May be overridden in sites/<site>/bootstrap.php.
 */
if (!defined('SUPPRESS_DEFAULT_ROUTES')) {
    Route::set('services', 'services(/<controller>(/<id>(.<format>)))')->defaults(array('directory' => 'services', 'controller' => 'services', 'action' => 'index'));
    Route::set('default', '(<controller>(/<action>(/<id>)))')->defaults(array('controller' => 'welcome', 'action' => 'index'));
}
if (!defined('SUPPRESS_REQUEST')) {
    /**
     * Execute the main request. A source of the URI can be passed, eg: $_SERVER['PATH_INFO'].
     * If no source is specified, the URI will be automatically detected.
     */
    try {
        $response = Request::instance()->execute()->send_headers()->response;