Ejemplo n.º 1
0
 static function add_media($id)
 {
     $upload = MGet::file();
     if (!$upload) {
         return null;
     }
     if (!isset($upload['name'])) {
         return null;
     }
     if (!isset($upload['size'])) {
         return null;
     }
     if (!isset($upload['tmp'])) {
         return null;
     }
     if (!MValidate::mime_type($upload['tmp'], 'image')) {
         return null;
     }
     if (!MValidate::file_size($upload['size'])) {
         return null;
     }
     $title = null;
     $name_array = explode('.', $upload['name']);
     if (isset($name_array[0]) && strlen($name_array[0]) > 0) {
         if (MValidate::title($name_array[0])) {
             $title = $name_array[0];
         }
     }
     $mapi_url = mapi_url_current();
     $url = '';
     if (isset($mapi_url['host']) && strlen($mapi_url['host']) > 0) {
         $url .= $mapi_url['protocol'] . '://';
         $url .= $mapi_url['host'];
         if ('80' != $mapi_url['port']) {
             $url .= ':' . $mapi_url['port'];
         }
         $url .= str_replace('manager', 'media', RPATH);
         $url .= '/contents/';
         $url .= sha1_file($upload['tmp']) . '_' . $upload['name'];
     }
     if (!strlen($url) > 0) {
         return null;
     }
     $upload['path'] = APATH . '/media/contents/' . sha1_file($upload['tmp']) . '_' . $upload['name'];
     if (!move_uploaded_file($upload['tmp'], $upload['path'])) {
         return null;
     }
     $content = self::get_content($id);
     if ($content) {
         $img = ORM::for_table('content_media')->create();
         $img->external_id = $id;
         $img->title = $title;
         $img->url = $url;
         $img->save();
     }
 }